blob: 30310a63475ab7b67a2f8dc8174dd5e7fa340e7a [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,
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800558 u32 data, struct gro_remcsum *grc)
Tom Herbertdfd86452015-01-12 17:00:38 -0800559{
560 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -0800561
562 if (skb->remcsum_offload)
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800563 return NULL;
Tom Herbertdfd86452015-01-12 17:00:38 -0800564
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 Herbert26c4f7d2015-02-10 16:30:27 -0800582 skb_gro_remcsum_process(skb, (void *)vh + hdrlen,
583 start, offset, grc);
Tom Herbertdfd86452015-01-12 17:00:38 -0800584
585 skb->remcsum_offload = 1;
586
587 return vh;
588}
589
Tom Herberta2b12f32015-01-12 17:00:37 -0800590static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
591 struct sk_buff *skb,
592 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200593{
594 struct sk_buff *p, **pp = NULL;
595 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800596 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200597 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800598 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
599 udp_offloads);
600 u32 flags;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800601 struct gro_remcsum grc;
602
603 skb_gro_remcsum_init(&grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200604
605 off_vx = skb_gro_offset(skb);
606 hlen = off_vx + sizeof(*vh);
607 vh = skb_gro_header_fast(skb, off_vx);
608 if (skb_gro_header_hard(skb, hlen)) {
609 vh = skb_gro_header_slow(skb, hlen, off_vx);
610 if (unlikely(!vh))
611 goto out;
612 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200613
Tom Herbertdfd86452015-01-12 17:00:38 -0800614 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
615 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
616
617 flags = ntohl(vh->vx_flags);
618
619 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
620 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800621 ntohl(vh->vx_vni), &grc);
Tom Herbertdfd86452015-01-12 17:00:38 -0800622
623 if (!vh)
624 goto out;
625 }
626
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200627 flush = 0;
628
629 for (p = *head; p; p = p->next) {
630 if (!NAPI_GRO_CB(p)->same_flow)
631 continue;
632
633 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100634 if (vh->vx_flags != vh2->vx_flags ||
635 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200636 NAPI_GRO_CB(p)->same_flow = 0;
637 continue;
638 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200639 }
640
Jesse Gross9b174d82014-12-30 19:10:15 -0800641 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200642
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200643out:
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800644 skb_gro_remcsum_cleanup(skb, &grc);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200645 NAPI_GRO_CB(skb)->flush |= flush;
646
647 return pp;
648}
649
Tom Herberta2b12f32015-01-12 17:00:37 -0800650static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
651 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200652{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800653 udp_tunnel_gro_complete(skb, nhoff);
654
Jesse Gross9b174d82014-12-30 19:10:15 -0800655 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200656}
657
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700658/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200659static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700660{
661 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200662 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700663 struct net *net = sock_net(sk);
664 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700665 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200666 int err;
667
668 if (sa_family == AF_INET) {
669 err = udp_add_offload(&vs->udp_offloads);
670 if (err)
671 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
672 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700673
674 rcu_read_lock();
675 for_each_netdev_rcu(net, dev) {
676 if (dev->netdev_ops->ndo_add_vxlan_port)
677 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
678 port);
679 }
680 rcu_read_unlock();
681}
682
683/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200684static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700685{
686 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200687 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700688 struct net *net = sock_net(sk);
689 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700690 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700691
692 rcu_read_lock();
693 for_each_netdev_rcu(net, dev) {
694 if (dev->netdev_ops->ndo_del_vxlan_port)
695 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
696 port);
697 }
698 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200699
700 if (sa_family == AF_INET)
701 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700702}
703
stephen hemmingerd3428942012-10-01 12:32:35 +0000704/* Add new entry to forwarding table -- assumes lock held */
705static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800706 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000707 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000708 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000709 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000710{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200711 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000712 struct vxlan_fdb *f;
713 int notify = 0;
714
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000715 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000716 if (f) {
717 if (flags & NLM_F_EXCL) {
718 netdev_dbg(vxlan->dev,
719 "lost race to create %pM\n", mac);
720 return -EEXIST;
721 }
722 if (f->state != state) {
723 f->state = state;
724 f->updated = jiffies;
725 notify = 1;
726 }
David Stevensae884082013-04-19 00:36:26 +0000727 if (f->flags != ndm_flags) {
728 f->flags = ndm_flags;
729 f->updated = jiffies;
730 notify = 1;
731 }
Thomas Richter906dc182013-07-19 17:20:07 +0200732 if ((flags & NLM_F_REPLACE)) {
733 /* Only change unicasts */
734 if (!(is_multicast_ether_addr(f->eth_addr) ||
735 is_zero_ether_addr(f->eth_addr))) {
736 int rc = vxlan_fdb_replace(f, ip, port, vni,
737 ifindex);
738
739 if (rc < 0)
740 return rc;
741 notify |= rc;
742 } else
743 return -EOPNOTSUPP;
744 }
David Stevens66817122013-03-15 04:35:51 +0000745 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300746 (is_multicast_ether_addr(f->eth_addr) ||
747 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200748 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
749 &rd);
David Stevens66817122013-03-15 04:35:51 +0000750
751 if (rc < 0)
752 return rc;
753 notify |= rc;
754 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000755 } else {
756 if (!(flags & NLM_F_CREATE))
757 return -ENOENT;
758
759 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
760 return -ENOSPC;
761
Thomas Richter906dc182013-07-19 17:20:07 +0200762 /* Disallow replace to add a multicast entry */
763 if ((flags & NLM_F_REPLACE) &&
764 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
765 return -EOPNOTSUPP;
766
Cong Wange4c7ed42013-08-31 13:44:33 +0800767 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000768 f = kmalloc(sizeof(*f), GFP_ATOMIC);
769 if (!f)
770 return -ENOMEM;
771
772 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000773 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000774 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000775 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700776 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000777 memcpy(f->eth_addr, mac, ETH_ALEN);
778
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200779 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700780
stephen hemmingerd3428942012-10-01 12:32:35 +0000781 ++vxlan->addrcnt;
782 hlist_add_head_rcu(&f->hlist,
783 vxlan_fdb_head(vxlan, mac));
784 }
785
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200786 if (notify) {
787 if (rd == NULL)
788 rd = first_remote_rtnl(f);
789 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
790 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000791
792 return 0;
793}
794
Wei Yongjun6706c822013-04-11 19:00:35 +0000795static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000796{
797 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700798 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000799
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700800 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000801 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000802 kfree(f);
803}
804
stephen hemmingerd3428942012-10-01 12:32:35 +0000805static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
806{
807 netdev_dbg(vxlan->dev,
808 "delete %pM\n", f->eth_addr);
809
810 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200811 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000812
813 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000814 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000815}
816
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300817static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800818 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300819{
820 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800821 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300822
823 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800824 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
825 if (err)
826 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300827 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800828 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
829 if (remote->sa.sa_family == AF_INET) {
830 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
831 ip->sa.sa_family = AF_INET;
832#if IS_ENABLED(CONFIG_IPV6)
833 } else {
834 ip->sin6.sin6_addr = in6addr_any;
835 ip->sa.sa_family = AF_INET6;
836#endif
837 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300838 }
839
840 if (tb[NDA_PORT]) {
841 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
842 return -EINVAL;
843 *port = nla_get_be16(tb[NDA_PORT]);
844 } else {
845 *port = vxlan->dst_port;
846 }
847
848 if (tb[NDA_VNI]) {
849 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
850 return -EINVAL;
851 *vni = nla_get_u32(tb[NDA_VNI]);
852 } else {
853 *vni = vxlan->default_dst.remote_vni;
854 }
855
856 if (tb[NDA_IFINDEX]) {
857 struct net_device *tdev;
858
859 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
860 return -EINVAL;
861 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800862 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300863 if (!tdev)
864 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300865 } else {
866 *ifindex = 0;
867 }
868
869 return 0;
870}
871
stephen hemmingerd3428942012-10-01 12:32:35 +0000872/* Add static entry (via netlink) */
873static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
874 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100875 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000876{
877 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300878 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800879 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000880 __be16 port;
881 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000882 int err;
883
884 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
885 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
886 ndm->ndm_state);
887 return -EINVAL;
888 }
889
890 if (tb[NDA_DST] == NULL)
891 return -EINVAL;
892
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300893 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
894 if (err)
895 return err;
David Stevens66817122013-03-15 04:35:51 +0000896
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300897 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
898 return -EAFNOSUPPORT;
899
stephen hemmingerd3428942012-10-01 12:32:35 +0000900 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800901 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000902 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000903 spin_unlock_bh(&vxlan->hash_lock);
904
905 return err;
906}
907
908/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000909static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
910 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100911 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000912{
913 struct vxlan_dev *vxlan = netdev_priv(dev);
914 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300915 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800916 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300917 __be16 port;
918 u32 vni, ifindex;
919 int err;
920
921 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
922 if (err)
923 return err;
924
925 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000926
927 spin_lock_bh(&vxlan->hash_lock);
928 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300929 if (!f)
930 goto out;
931
Cong Wange4c7ed42013-08-31 13:44:33 +0800932 if (!vxlan_addr_any(&ip)) {
933 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300934 if (!rd)
935 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000936 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300937
938 err = 0;
939
940 /* remove a destination if it's not the only one on the list,
941 * otherwise destroy the fdb entry
942 */
943 if (rd && !list_is_singular(&f->remotes)) {
944 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200945 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800946 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300947 goto out;
948 }
949
950 vxlan_fdb_destroy(vxlan, f);
951
952out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000953 spin_unlock_bh(&vxlan->hash_lock);
954
955 return err;
956}
957
958/* Dump forwarding table */
959static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400960 struct net_device *dev,
961 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000962{
963 struct vxlan_dev *vxlan = netdev_priv(dev);
964 unsigned int h;
965
966 for (h = 0; h < FDB_HASH_SIZE; ++h) {
967 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000968 int err;
969
Sasha Levinb67bfe02013-02-27 17:06:00 -0800970 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000971 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000972
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700973 if (idx < cb->args[0])
974 goto skip;
975
976 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000977 err = vxlan_fdb_info(skb, vxlan, f,
978 NETLINK_CB(cb->skb).portid,
979 cb->nlh->nlmsg_seq,
980 RTM_NEWNEIGH,
981 NLM_F_MULTI, rd);
982 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700983 goto out;
David Stevens66817122013-03-15 04:35:51 +0000984 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700985skip:
986 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000987 }
988 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700989out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000990 return idx;
991}
992
993/* Watch incoming packets to learn mapping between Ethernet address
994 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700995 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000996 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700997static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800998 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000999{
1000 struct vxlan_dev *vxlan = netdev_priv(dev);
1001 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001002
1003 f = vxlan_find_mac(vxlan, src_mac);
1004 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001005 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001006
Cong Wange4c7ed42013-08-31 13:44:33 +08001007 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001008 return false;
1009
1010 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001011 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001012 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001013
1014 if (net_ratelimit())
1015 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001016 "%pM migrated from %pIS to %pIS\n",
Rasmus Villemoesa4870f72015-02-07 03:17:31 +01001017 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
stephen hemmingerd3428942012-10-01 12:32:35 +00001018
Cong Wange4c7ed42013-08-31 13:44:33 +08001019 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001020 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001021 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001022 } else {
1023 /* learned new entry */
1024 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001025
1026 /* close off race between vxlan_flush and incoming packets */
1027 if (netif_running(dev))
1028 vxlan_fdb_create(vxlan, src_mac, src_ip,
1029 NUD_REACHABLE,
1030 NLM_F_EXCL|NLM_F_CREATE,
1031 vxlan->dst_port,
1032 vxlan->default_dst.remote_vni,
1033 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001034 spin_unlock(&vxlan->hash_lock);
1035 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001036
1037 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001038}
1039
stephen hemmingerd3428942012-10-01 12:32:35 +00001040/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001041static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001042{
stephen hemminger553675f2013-05-16 11:35:20 +00001043 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001044
Gao feng95ab0992013-12-10 16:37:33 +08001045 /* The vxlan_sock is only used by dev, leaving group has
1046 * no effect on other vxlan devices.
1047 */
1048 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1049 return false;
1050
stephen hemminger553675f2013-05-16 11:35:20 +00001051 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001052 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001053 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001054
Gao feng95ab0992013-12-10 16:37:33 +08001055 if (vxlan->vn_sock != dev->vn_sock)
1056 continue;
1057
1058 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1059 &dev->default_dst.remote_ip))
1060 continue;
1061
1062 if (vxlan->default_dst.remote_ifindex !=
1063 dev->default_dst.remote_ifindex)
1064 continue;
1065
1066 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001067 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001068
1069 return false;
1070}
1071
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001072static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001073{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001074 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001075}
1076
Pravin B Shelar012a5722013-08-19 11:23:07 -07001077void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001078{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001079 struct sock *sk = vs->sock->sk;
1080 struct net *net = sock_net(sk);
1081 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001082
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001083 if (!atomic_dec_and_test(&vs->refcnt))
1084 return;
1085
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001086 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001087 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001088 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001089 spin_unlock(&vn->sock_lock);
1090
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001091 queue_work(vxlan_wq, &vs->del_work);
1092}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001093EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001094
stephen hemminger3fc2de22013-07-18 08:40:15 -07001095/* Callback to update multicast group membership when first VNI on
1096 * multicast asddress is brought up
1097 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001098 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001099static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001100{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001101 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001102 struct vxlan_sock *vs = vxlan->vn_sock;
1103 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001104 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1105 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001106
stephen hemmingerd3428942012-10-01 12:32:35 +00001107 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001108 if (ip->sa.sa_family == AF_INET) {
1109 struct ip_mreqn mreq = {
1110 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1111 .imr_ifindex = ifindex,
1112 };
1113
1114 ip_mc_join_group(sk, &mreq);
1115#if IS_ENABLED(CONFIG_IPV6)
1116 } else {
1117 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1118 &ip->sin6.sin6_addr);
1119#endif
1120 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001121 release_sock(sk);
1122
Pravin B Shelar012a5722013-08-19 11:23:07 -07001123 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001124 dev_put(vxlan->dev);
1125}
1126
1127/* Inverse of vxlan_igmp_join when last VNI is brought down */
1128static void vxlan_igmp_leave(struct work_struct *work)
1129{
1130 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001131 struct vxlan_sock *vs = vxlan->vn_sock;
1132 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001133 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1134 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001135
1136 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001137 if (ip->sa.sa_family == AF_INET) {
1138 struct ip_mreqn mreq = {
1139 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1140 .imr_ifindex = ifindex,
1141 };
1142
1143 ip_mc_leave_group(sk, &mreq);
1144#if IS_ENABLED(CONFIG_IPV6)
1145 } else {
1146 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1147 &ip->sin6.sin6_addr);
1148#endif
1149 }
1150
stephen hemmingerd3428942012-10-01 12:32:35 +00001151 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001152
Pravin B Shelar012a5722013-08-19 11:23:07 -07001153 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001154 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001155}
1156
Tom Herbertdfd86452015-01-12 17:00:38 -08001157static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1158 size_t hdrlen, u32 data)
1159{
1160 size_t start, offset, plen;
Tom Herbertdfd86452015-01-12 17:00:38 -08001161
Tom Herbertdfd86452015-01-12 17:00:38 -08001162 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1163 offset = start + ((data & VXLAN_RCO_UDP) ?
1164 offsetof(struct udphdr, check) :
1165 offsetof(struct tcphdr, check));
1166
1167 plen = hdrlen + offset + sizeof(u16);
1168
1169 if (!pskb_may_pull(skb, plen))
1170 return NULL;
1171
1172 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1173
Tom Herbertdcdc8992015-02-02 16:07:34 -08001174 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset);
Tom Herbertdfd86452015-01-12 17:00:38 -08001175
1176 return vh;
1177}
1178
stephen hemmingerd3428942012-10-01 12:32:35 +00001179/* Callback from net/ipv4/udp.c to receive packets */
1180static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1181{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001182 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001183 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001184 u32 flags, vni;
Thomas Graf35114942015-01-15 03:53:55 +01001185 struct vxlan_metadata md = {0};
stephen hemmingerd3428942012-10-01 12:32:35 +00001186
stephen hemmingerd3428942012-10-01 12:32:35 +00001187 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001188 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001189 goto error;
1190
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001191 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001192 flags = ntohl(vxh->vx_flags);
1193 vni = ntohl(vxh->vx_vni);
1194
1195 if (flags & VXLAN_HF_VNI) {
1196 flags &= ~VXLAN_HF_VNI;
1197 } else {
1198 /* VNI flag always required to be set */
1199 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001200 }
1201
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001202 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001203 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001204 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001205
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001206 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001207 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001208 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001209
Tom Herbertdfd86452015-01-12 17:00:38 -08001210 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1211 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1212 if (!vxh)
1213 goto drop;
1214
1215 flags &= ~VXLAN_HF_RCO;
1216 vni &= VXLAN_VID_MASK;
1217 }
1218
Thomas Graf35114942015-01-15 03:53:55 +01001219 /* For backwards compatibility, only allow reserved fields to be
1220 * used by VXLAN extensions if explicitly requested.
1221 */
1222 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1223 struct vxlanhdr_gbp *gbp;
1224
1225 gbp = (struct vxlanhdr_gbp *)vxh;
1226 md.gbp = ntohs(gbp->policy_id);
1227
1228 if (gbp->dont_learn)
1229 md.gbp |= VXLAN_GBP_DONT_LEARN;
1230
1231 if (gbp->policy_applied)
1232 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1233
1234 flags &= ~VXLAN_GBP_USED_BITS;
1235 }
1236
Tom Herbertdfd86452015-01-12 17:00:38 -08001237 if (flags || (vni & ~VXLAN_VID_MASK)) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001238 /* If there are any unprocessed flags remaining treat
1239 * this as a malformed packet. This behavior diverges from
1240 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1241 * in reserved fields are to be ignored. The approach here
1242 * maintains compatbility with previous stack code, and also
1243 * is more robust and provides a little more security in
1244 * adding extensions to VXLAN.
1245 */
1246
1247 goto bad_flags;
1248 }
1249
Thomas Graf35114942015-01-15 03:53:55 +01001250 md.vni = vxh->vx_vni;
1251 vs->rcv(vs, skb, &md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001252 return 0;
1253
1254drop:
1255 /* Consume bad packet */
1256 kfree_skb(skb);
1257 return 0;
1258
Tom Herbert3bf39472015-01-08 12:31:18 -08001259bad_flags:
1260 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1261 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1262
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001263error:
1264 /* Return non vxlan pkt */
1265 return 1;
1266}
1267
Thomas Graf35114942015-01-15 03:53:55 +01001268static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1269 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001270{
Cong Wange4c7ed42013-08-31 13:44:33 +08001271 struct iphdr *oip = NULL;
1272 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001273 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001274 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001275 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001276 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001277 int err = 0;
1278 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001279
Thomas Graf35114942015-01-15 03:53:55 +01001280 vni = ntohl(md->vni) >> 8;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001281 /* Is this VNI defined? */
1282 vxlan = vxlan_vs_find_vni(vs, vni);
1283 if (!vxlan)
1284 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001285
Cong Wange4c7ed42013-08-31 13:44:33 +08001286 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001287 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001288 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001289 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001290 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001291
1292 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001293 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001294 goto drop;
1295
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001296 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001297 if (remote_ip->sa.sa_family == AF_INET) {
1298 oip = ip_hdr(skb);
1299 saddr.sin.sin_addr.s_addr = oip->saddr;
1300 saddr.sa.sa_family = AF_INET;
1301#if IS_ENABLED(CONFIG_IPV6)
1302 } else {
1303 oip6 = ipv6_hdr(skb);
1304 saddr.sin6.sin6_addr = oip6->saddr;
1305 saddr.sa.sa_family = AF_INET6;
1306#endif
1307 }
1308
stephen hemminger26a41ae62013-06-17 12:09:58 -07001309 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001310 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001311 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001312
stephen hemmingerd3428942012-10-01 12:32:35 +00001313 skb_reset_network_header(skb);
Thomas Graf35114942015-01-15 03:53:55 +01001314 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001315
Cong Wange4c7ed42013-08-31 13:44:33 +08001316 if (oip6)
1317 err = IP6_ECN_decapsulate(oip6, skb);
1318 if (oip)
1319 err = IP_ECN_decapsulate(oip, skb);
1320
stephen hemmingerd3428942012-10-01 12:32:35 +00001321 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001322 if (log_ecn_error) {
1323 if (oip6)
1324 net_info_ratelimited("non-ECT from %pI6\n",
1325 &oip6->saddr);
1326 if (oip)
1327 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1328 &oip->saddr, oip->tos);
1329 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001330 if (err > 1) {
1331 ++vxlan->dev->stats.rx_frame_errors;
1332 ++vxlan->dev->stats.rx_errors;
1333 goto drop;
1334 }
1335 }
1336
Pravin B Shelare8171042013-03-25 14:49:46 +00001337 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001338 u64_stats_update_begin(&stats->syncp);
1339 stats->rx_packets++;
1340 stats->rx_bytes += skb->len;
1341 u64_stats_update_end(&stats->syncp);
1342
1343 netif_rx(skb);
1344
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001345 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001346drop:
1347 /* Consume bad packet */
1348 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001349}
1350
David Stevense4f67ad2012-11-20 02:50:14 +00001351static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1352{
1353 struct vxlan_dev *vxlan = netdev_priv(dev);
1354 struct arphdr *parp;
1355 u8 *arpptr, *sha;
1356 __be32 sip, tip;
1357 struct neighbour *n;
1358
1359 if (dev->flags & IFF_NOARP)
1360 goto out;
1361
1362 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1363 dev->stats.tx_dropped++;
1364 goto out;
1365 }
1366 parp = arp_hdr(skb);
1367
1368 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1369 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1370 parp->ar_pro != htons(ETH_P_IP) ||
1371 parp->ar_op != htons(ARPOP_REQUEST) ||
1372 parp->ar_hln != dev->addr_len ||
1373 parp->ar_pln != 4)
1374 goto out;
1375 arpptr = (u8 *)parp + sizeof(struct arphdr);
1376 sha = arpptr;
1377 arpptr += dev->addr_len; /* sha */
1378 memcpy(&sip, arpptr, sizeof(sip));
1379 arpptr += sizeof(sip);
1380 arpptr += dev->addr_len; /* tha */
1381 memcpy(&tip, arpptr, sizeof(tip));
1382
1383 if (ipv4_is_loopback(tip) ||
1384 ipv4_is_multicast(tip))
1385 goto out;
1386
1387 n = neigh_lookup(&arp_tbl, &tip, dev);
1388
1389 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001390 struct vxlan_fdb *f;
1391 struct sk_buff *reply;
1392
1393 if (!(n->nud_state & NUD_CONNECTED)) {
1394 neigh_release(n);
1395 goto out;
1396 }
1397
1398 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001399 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001400 /* bridge-local neighbor */
1401 neigh_release(n);
1402 goto out;
1403 }
1404
1405 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1406 n->ha, sha);
1407
1408 neigh_release(n);
1409
David Stevens73461352014-03-18 12:32:29 -04001410 if (reply == NULL)
1411 goto out;
1412
David Stevense4f67ad2012-11-20 02:50:14 +00001413 skb_reset_mac_header(reply);
1414 __skb_pull(reply, skb_network_offset(reply));
1415 reply->ip_summed = CHECKSUM_UNNECESSARY;
1416 reply->pkt_type = PACKET_HOST;
1417
1418 if (netif_rx_ni(reply) == NET_RX_DROP)
1419 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001420 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1421 union vxlan_addr ipa = {
1422 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001423 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001424 };
1425
1426 vxlan_ip_miss(dev, &ipa);
1427 }
David Stevense4f67ad2012-11-20 02:50:14 +00001428out:
1429 consume_skb(skb);
1430 return NETDEV_TX_OK;
1431}
1432
Cong Wangf564f452013-08-31 13:44:36 +08001433#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001434static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1435 struct neighbour *n, bool isrouter)
1436{
1437 struct net_device *dev = request->dev;
1438 struct sk_buff *reply;
1439 struct nd_msg *ns, *na;
1440 struct ipv6hdr *pip6;
1441 u8 *daddr;
1442 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1443 int ns_olen;
1444 int i, len;
1445
1446 if (dev == NULL)
1447 return NULL;
1448
1449 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1450 sizeof(*na) + na_olen + dev->needed_tailroom;
1451 reply = alloc_skb(len, GFP_ATOMIC);
1452 if (reply == NULL)
1453 return NULL;
1454
1455 reply->protocol = htons(ETH_P_IPV6);
1456 reply->dev = dev;
1457 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1458 skb_push(reply, sizeof(struct ethhdr));
1459 skb_set_mac_header(reply, 0);
1460
1461 ns = (struct nd_msg *)skb_transport_header(request);
1462
1463 daddr = eth_hdr(request)->h_source;
1464 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1465 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1466 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1467 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1468 break;
1469 }
1470 }
1471
1472 /* Ethernet header */
1473 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1474 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1475 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1476 reply->protocol = htons(ETH_P_IPV6);
1477
1478 skb_pull(reply, sizeof(struct ethhdr));
1479 skb_set_network_header(reply, 0);
1480 skb_put(reply, sizeof(struct ipv6hdr));
1481
1482 /* IPv6 header */
1483
1484 pip6 = ipv6_hdr(reply);
1485 memset(pip6, 0, sizeof(struct ipv6hdr));
1486 pip6->version = 6;
1487 pip6->priority = ipv6_hdr(request)->priority;
1488 pip6->nexthdr = IPPROTO_ICMPV6;
1489 pip6->hop_limit = 255;
1490 pip6->daddr = ipv6_hdr(request)->saddr;
1491 pip6->saddr = *(struct in6_addr *)n->primary_key;
1492
1493 skb_pull(reply, sizeof(struct ipv6hdr));
1494 skb_set_transport_header(reply, 0);
1495
1496 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1497
1498 /* Neighbor Advertisement */
1499 memset(na, 0, sizeof(*na)+na_olen);
1500 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1501 na->icmph.icmp6_router = isrouter;
1502 na->icmph.icmp6_override = 1;
1503 na->icmph.icmp6_solicited = 1;
1504 na->target = ns->target;
1505 ether_addr_copy(&na->opt[2], n->ha);
1506 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1507 na->opt[1] = na_olen >> 3;
1508
1509 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1510 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1511 csum_partial(na, sizeof(*na)+na_olen, 0));
1512
1513 pip6->payload_len = htons(sizeof(*na)+na_olen);
1514
1515 skb_push(reply, sizeof(struct ipv6hdr));
1516
1517 reply->ip_summed = CHECKSUM_UNNECESSARY;
1518
1519 return reply;
1520}
1521
Cong Wangf564f452013-08-31 13:44:36 +08001522static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1523{
1524 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001525 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001526 const struct ipv6hdr *iphdr;
1527 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001528 struct neighbour *n;
1529 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001530
1531 in6_dev = __in6_dev_get(dev);
1532 if (!in6_dev)
1533 goto out;
1534
Cong Wangf564f452013-08-31 13:44:36 +08001535 iphdr = ipv6_hdr(skb);
1536 saddr = &iphdr->saddr;
1537 daddr = &iphdr->daddr;
1538
Cong Wangf564f452013-08-31 13:44:36 +08001539 msg = (struct nd_msg *)skb_transport_header(skb);
1540 if (msg->icmph.icmp6_code != 0 ||
1541 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1542 goto out;
1543
David Stevens4b29dba2014-03-24 10:39:58 -04001544 if (ipv6_addr_loopback(daddr) ||
1545 ipv6_addr_is_multicast(&msg->target))
1546 goto out;
1547
1548 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001549
1550 if (n) {
1551 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001552 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001553
1554 if (!(n->nud_state & NUD_CONNECTED)) {
1555 neigh_release(n);
1556 goto out;
1557 }
1558
1559 f = vxlan_find_mac(vxlan, n->ha);
1560 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1561 /* bridge-local neighbor */
1562 neigh_release(n);
1563 goto out;
1564 }
1565
David Stevens4b29dba2014-03-24 10:39:58 -04001566 reply = vxlan_na_create(skb, n,
1567 !!(f ? f->flags & NTF_ROUTER : 0));
1568
Cong Wangf564f452013-08-31 13:44:36 +08001569 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001570
1571 if (reply == NULL)
1572 goto out;
1573
1574 if (netif_rx_ni(reply) == NET_RX_DROP)
1575 dev->stats.rx_dropped++;
1576
Cong Wangf564f452013-08-31 13:44:36 +08001577 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001578 union vxlan_addr ipa = {
1579 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001580 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001581 };
1582
Cong Wangf564f452013-08-31 13:44:36 +08001583 vxlan_ip_miss(dev, &ipa);
1584 }
1585
1586out:
1587 consume_skb(skb);
1588 return NETDEV_TX_OK;
1589}
1590#endif
1591
David Stevense4f67ad2012-11-20 02:50:14 +00001592static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1593{
1594 struct vxlan_dev *vxlan = netdev_priv(dev);
1595 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001596
1597 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1598 return false;
1599
1600 n = NULL;
1601 switch (ntohs(eth_hdr(skb)->h_proto)) {
1602 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001603 {
1604 struct iphdr *pip;
1605
David Stevense4f67ad2012-11-20 02:50:14 +00001606 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1607 return false;
1608 pip = ip_hdr(skb);
1609 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001610 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1611 union vxlan_addr ipa = {
1612 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001613 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001614 };
1615
1616 vxlan_ip_miss(dev, &ipa);
1617 return false;
1618 }
1619
David Stevense4f67ad2012-11-20 02:50:14 +00001620 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001621 }
1622#if IS_ENABLED(CONFIG_IPV6)
1623 case ETH_P_IPV6:
1624 {
1625 struct ipv6hdr *pip6;
1626
1627 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1628 return false;
1629 pip6 = ipv6_hdr(skb);
1630 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1631 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1632 union vxlan_addr ipa = {
1633 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001634 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001635 };
1636
1637 vxlan_ip_miss(dev, &ipa);
1638 return false;
1639 }
1640
1641 break;
1642 }
1643#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001644 default:
1645 return false;
1646 }
1647
1648 if (n) {
1649 bool diff;
1650
Joe Perches7367d0b2013-09-01 11:51:23 -07001651 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001652 if (diff) {
1653 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1654 dev->addr_len);
1655 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1656 }
1657 neigh_release(n);
1658 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001659 }
1660
David Stevense4f67ad2012-11-20 02:50:14 +00001661 return false;
1662}
1663
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001664static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001665 struct vxlan_metadata *md)
1666{
1667 struct vxlanhdr_gbp *gbp;
1668
Thomas Grafdb79a622015-02-04 17:00:04 +01001669 if (!md->gbp)
1670 return;
1671
Thomas Graf35114942015-01-15 03:53:55 +01001672 gbp = (struct vxlanhdr_gbp *)vxh;
1673 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1674
1675 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1676 gbp->dont_learn = 1;
1677
1678 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1679 gbp->policy_applied = 1;
1680
1681 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1682}
1683
Cong Wange4c7ed42013-08-31 13:44:33 +08001684#if IS_ENABLED(CONFIG_IPV6)
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001685static int vxlan6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001686 struct net_device *dev, struct in6_addr *saddr,
1687 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001688 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001689 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Cong Wange4c7ed42013-08-31 13:44:33 +08001690{
Cong Wange4c7ed42013-08-31 13:44:33 +08001691 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001692 int min_headroom;
1693 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001694 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbertdfd86452015-01-12 17:00:38 -08001695 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1696 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001697
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001698 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001699 skb->ip_summed == CHECKSUM_PARTIAL) {
1700 int csum_start = skb_checksum_start_offset(skb);
1701
1702 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1703 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1704 (skb->csum_offset == offsetof(struct udphdr, check) ||
1705 skb->csum_offset == offsetof(struct tcphdr, check))) {
1706 udp_sum = false;
1707 type |= SKB_GSO_TUNNEL_REMCSUM;
1708 }
1709 }
1710
1711 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001712 if (IS_ERR(skb)) {
1713 err = -EINVAL;
1714 goto err;
1715 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001716
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001717 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001718
Cong Wange4c7ed42013-08-31 13:44:33 +08001719 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1720 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001721 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001722
1723 /* Need space for new headers (invalidates iph ptr) */
1724 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001725 if (unlikely(err)) {
1726 kfree_skb(skb);
1727 goto err;
1728 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001729
Jiri Pirko59682502014-11-19 14:04:59 +01001730 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001731 if (WARN_ON(!skb)) {
1732 err = -ENOMEM;
1733 goto err;
1734 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001735
1736 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001737 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001738 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001739
Tom Herbertdfd86452015-01-12 17:00:38 -08001740 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1741 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1742 VXLAN_RCO_SHIFT;
1743
1744 if (skb->csum_offset == offsetof(struct udphdr, check))
1745 data |= VXLAN_RCO_UDP;
1746
1747 vxh->vx_vni |= htonl(data);
1748 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1749
1750 if (!skb_is_gso(skb)) {
1751 skb->ip_summed = CHECKSUM_NONE;
1752 skb->encapsulation = 0;
1753 }
1754 }
1755
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001756 if (vxflags & VXLAN_F_GBP)
1757 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001758
Tom Herbert996c9fd2014-09-29 20:22:33 -07001759 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1760
Tom Herbertd998f8e2015-01-20 11:23:04 -08001761 udp_tunnel6_xmit_skb(dst, skb, dev, saddr, daddr, prio,
1762 ttl, src_port, dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001763 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
Cong Wange4c7ed42013-08-31 13:44:33 +08001764 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001765err:
1766 dst_release(dst);
1767 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001768}
1769#endif
1770
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001771int vxlan_xmit_skb(struct rtable *rt, struct sk_buff *skb,
Pravin B Shelar49560532013-08-19 11:23:17 -07001772 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001773 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001774 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Pravin B Shelar49560532013-08-19 11:23:17 -07001775{
1776 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001777 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001778 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001779 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
Tom Herbertdfd86452015-01-12 17:00:38 -08001780 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1781 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001782
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001783 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001784 skb->ip_summed == CHECKSUM_PARTIAL) {
1785 int csum_start = skb_checksum_start_offset(skb);
1786
1787 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1788 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1789 (skb->csum_offset == offsetof(struct udphdr, check) ||
1790 skb->csum_offset == offsetof(struct tcphdr, check))) {
1791 udp_sum = false;
1792 type |= SKB_GSO_TUNNEL_REMCSUM;
1793 }
1794 }
1795
1796 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001797 if (IS_ERR(skb))
Pravin B Shelar74f47272014-12-23 16:20:36 -08001798 return PTR_ERR(skb);
Pravin B Shelar49560532013-08-19 11:23:17 -07001799
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001800 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001801 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001802 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001803
1804 /* Need space for new headers (invalidates iph ptr) */
1805 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001806 if (unlikely(err)) {
1807 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001808 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001809 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001810
Jiri Pirko59682502014-11-19 14:04:59 +01001811 skb = vlan_hwaccel_push_inside(skb);
1812 if (WARN_ON(!skb))
1813 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001814
Pravin B Shelar49560532013-08-19 11:23:17 -07001815 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001816 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001817 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001818
Tom Herbertdfd86452015-01-12 17:00:38 -08001819 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1820 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1821 VXLAN_RCO_SHIFT;
1822
1823 if (skb->csum_offset == offsetof(struct udphdr, check))
1824 data |= VXLAN_RCO_UDP;
1825
1826 vxh->vx_vni |= htonl(data);
1827 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1828
1829 if (!skb_is_gso(skb)) {
1830 skb->ip_summed = CHECKSUM_NONE;
1831 skb->encapsulation = 0;
1832 }
1833 }
1834
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001835 if (vxflags & VXLAN_F_GBP)
1836 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001837
Tom Herbert996c9fd2014-09-29 20:22:33 -07001838 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1839
Tom Herbertd998f8e2015-01-20 11:23:04 -08001840 return udp_tunnel_xmit_skb(rt, skb, src, dst, tos,
1841 ttl, df, src_port, dst_port, xnet,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001842 !(vxflags & VXLAN_F_UDP_CSUM));
Pravin B Shelar49560532013-08-19 11:23:17 -07001843}
1844EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1845
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001846/* Bypass encapsulation if the destination is local */
1847static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1848 struct vxlan_dev *dst_vxlan)
1849{
Li RongQing8f849852014-01-04 13:57:59 +08001850 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001851 union vxlan_addr loopback;
1852 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001853 struct net_device *dev = skb->dev;
1854 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001855
Li RongQing8f849852014-01-04 13:57:59 +08001856 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1857 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001858 skb->pkt_type = PACKET_HOST;
1859 skb->encapsulation = 0;
1860 skb->dev = dst_vxlan->dev;
1861 __skb_pull(skb, skb_network_offset(skb));
1862
Cong Wange4c7ed42013-08-31 13:44:33 +08001863 if (remote_ip->sa.sa_family == AF_INET) {
1864 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1865 loopback.sa.sa_family = AF_INET;
1866#if IS_ENABLED(CONFIG_IPV6)
1867 } else {
1868 loopback.sin6.sin6_addr = in6addr_loopback;
1869 loopback.sa.sa_family = AF_INET6;
1870#endif
1871 }
1872
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001873 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001874 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001875
1876 u64_stats_update_begin(&tx_stats->syncp);
1877 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001878 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001879 u64_stats_update_end(&tx_stats->syncp);
1880
1881 if (netif_rx(skb) == NET_RX_SUCCESS) {
1882 u64_stats_update_begin(&rx_stats->syncp);
1883 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001884 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001885 u64_stats_update_end(&rx_stats->syncp);
1886 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001887 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001888 }
1889}
1890
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001891static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1892 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001893{
1894 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001895 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001896 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001897 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001898 union vxlan_addr *dst;
Thomas Graf35114942015-01-15 03:53:55 +01001899 struct vxlan_metadata md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001900 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001901 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001902 __be16 df = 0;
1903 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001904 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001905
stephen hemminger823aa872013-04-27 11:31:57 +00001906 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001907 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001908 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001909
Cong Wange4c7ed42013-08-31 13:44:33 +08001910 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001911 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001912 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001913 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001914 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001915 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001916 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001917 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001918
stephen hemmingerd3428942012-10-01 12:32:35 +00001919 old_iph = ip_hdr(skb);
1920
stephen hemmingerd3428942012-10-01 12:32:35 +00001921 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001922 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001923 ttl = 1;
1924
1925 tos = vxlan->tos;
1926 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001927 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001928
Tom Herbert535fb8d2014-07-01 21:32:49 -07001929 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1930 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001931
Cong Wange4c7ed42013-08-31 13:44:33 +08001932 if (dst->sa.sa_family == AF_INET) {
1933 memset(&fl4, 0, sizeof(fl4));
1934 fl4.flowi4_oif = rdst->remote_ifindex;
1935 fl4.flowi4_tos = RT_TOS(tos);
1936 fl4.daddr = dst->sin.sin_addr.s_addr;
1937 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001938
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001939 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001940 if (IS_ERR(rt)) {
1941 netdev_dbg(dev, "no route to %pI4\n",
1942 &dst->sin.sin_addr.s_addr);
1943 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001944 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001945 }
1946
1947 if (rt->dst.dev == dev) {
1948 netdev_dbg(dev, "circular route to %pI4\n",
1949 &dst->sin.sin_addr.s_addr);
1950 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001951 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001952 }
1953
1954 /* Bypass encapsulation if the destination is local */
1955 if (rt->rt_flags & RTCF_LOCAL &&
1956 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1957 struct vxlan_dev *dst_vxlan;
1958
1959 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001960 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01001961 dst->sa.sa_family, dst_port,
1962 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08001963 if (!dst_vxlan)
1964 goto tx_error;
1965 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1966 return;
1967 }
1968
1969 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1970 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Graf35114942015-01-15 03:53:55 +01001971 md.vni = htonl(vni << 8);
1972 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08001973
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001974 err = vxlan_xmit_skb(rt, skb, fl4.saddr,
1975 dst->sin.sin_addr.s_addr, tos, ttl, df,
1976 src_port, dst_port, &md,
1977 !net_eq(vxlan->net, dev_net(vxlan->dev)),
1978 vxlan->flags);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001979 if (err < 0) {
1980 /* skb is already freed. */
1981 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001982 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001983 }
1984
Cong Wange4c7ed42013-08-31 13:44:33 +08001985 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1986#if IS_ENABLED(CONFIG_IPV6)
1987 } else {
1988 struct sock *sk = vxlan->vn_sock->sock->sk;
1989 struct dst_entry *ndst;
1990 struct flowi6 fl6;
1991 u32 flags;
1992
1993 memset(&fl6, 0, sizeof(fl6));
1994 fl6.flowi6_oif = rdst->remote_ifindex;
1995 fl6.daddr = dst->sin6.sin6_addr;
1996 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001997 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001998
1999 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2000 netdev_dbg(dev, "no route to %pI6\n",
2001 &dst->sin6.sin6_addr);
2002 dev->stats.tx_carrier_errors++;
2003 goto tx_error;
2004 }
2005
2006 if (ndst->dev == dev) {
2007 netdev_dbg(dev, "circular route to %pI6\n",
2008 &dst->sin6.sin6_addr);
2009 dst_release(ndst);
2010 dev->stats.collisions++;
2011 goto tx_error;
2012 }
2013
2014 /* Bypass encapsulation if the destination is local */
2015 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2016 if (flags & RTF_LOCAL &&
2017 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2018 struct vxlan_dev *dst_vxlan;
2019
2020 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002021 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002022 dst->sa.sa_family, dst_port,
2023 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002024 if (!dst_vxlan)
2025 goto tx_error;
2026 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2027 return;
2028 }
2029
2030 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Graf35114942015-01-15 03:53:55 +01002031 md.vni = htonl(vni << 8);
2032 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002033
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002034 err = vxlan6_xmit_skb(ndst, skb, dev, &fl6.saddr, &fl6.daddr,
2035 0, ttl, src_port, dst_port, &md,
2036 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2037 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002038#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002039 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002040
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002041 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002042
2043drop:
2044 dev->stats.tx_dropped++;
2045 goto tx_free;
2046
Pravin B Shelar49560532013-08-19 11:23:17 -07002047rt_tx_error:
2048 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002049tx_error:
2050 dev->stats.tx_errors++;
2051tx_free:
2052 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002053}
2054
David Stevens66817122013-03-15 04:35:51 +00002055/* Transmit local packets over Vxlan
2056 *
2057 * Outer IP header inherits ECN and DF from inner header.
2058 * Outer UDP destination is the VXLAN assigned port.
2059 * source port is based on hash of flow
2060 */
2061static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2062{
2063 struct vxlan_dev *vxlan = netdev_priv(dev);
2064 struct ethhdr *eth;
2065 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002066 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002067 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002068
2069 skb_reset_mac_header(skb);
2070 eth = eth_hdr(skb);
2071
Cong Wangf564f452013-08-31 13:44:36 +08002072 if ((vxlan->flags & VXLAN_F_PROXY)) {
2073 if (ntohs(eth->h_proto) == ETH_P_ARP)
2074 return arp_reduce(dev, skb);
2075#if IS_ENABLED(CONFIG_IPV6)
2076 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002077 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2078 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002079 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2080 struct nd_msg *msg;
2081
2082 msg = (struct nd_msg *)skb_transport_header(skb);
2083 if (msg->icmph.icmp6_code == 0 &&
2084 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2085 return neigh_reduce(dev, skb);
2086 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002087 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002088#endif
2089 }
David Stevens66817122013-03-15 04:35:51 +00002090
2091 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002092 did_rsc = false;
2093
2094 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002095 (ntohs(eth->h_proto) == ETH_P_IP ||
2096 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002097 did_rsc = route_shortcircuit(dev, skb);
2098 if (did_rsc)
2099 f = vxlan_find_mac(vxlan, eth->h_dest);
2100 }
2101
David Stevens66817122013-03-15 04:35:51 +00002102 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002103 f = vxlan_find_mac(vxlan, all_zeros_mac);
2104 if (f == NULL) {
2105 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2106 !is_multicast_ether_addr(eth->h_dest))
2107 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002108
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002109 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002110 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002111 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002112 }
David Stevens66817122013-03-15 04:35:51 +00002113 }
2114
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002115 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2116 struct sk_buff *skb1;
2117
Eric Dumazet8f646c92014-01-06 09:54:31 -08002118 if (!fdst) {
2119 fdst = rdst;
2120 continue;
2121 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002122 skb1 = skb_clone(skb, GFP_ATOMIC);
2123 if (skb1)
2124 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2125 }
2126
Eric Dumazet8f646c92014-01-06 09:54:31 -08002127 if (fdst)
2128 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2129 else
2130 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002131 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002132}
2133
stephen hemmingerd3428942012-10-01 12:32:35 +00002134/* Walk the forwarding table and purge stale entries */
2135static void vxlan_cleanup(unsigned long arg)
2136{
2137 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2138 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2139 unsigned int h;
2140
2141 if (!netif_running(vxlan->dev))
2142 return;
2143
2144 spin_lock_bh(&vxlan->hash_lock);
2145 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2146 struct hlist_node *p, *n;
2147 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2148 struct vxlan_fdb *f
2149 = container_of(p, struct vxlan_fdb, hlist);
2150 unsigned long timeout;
2151
stephen hemminger3c172862012-10-26 06:24:34 +00002152 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002153 continue;
2154
2155 timeout = f->used + vxlan->age_interval * HZ;
2156 if (time_before_eq(timeout, jiffies)) {
2157 netdev_dbg(vxlan->dev,
2158 "garbage collect %pM\n",
2159 f->eth_addr);
2160 f->state = NUD_STALE;
2161 vxlan_fdb_destroy(vxlan, f);
2162 } else if (time_before(timeout, next_timer))
2163 next_timer = timeout;
2164 }
2165 }
2166 spin_unlock_bh(&vxlan->hash_lock);
2167
2168 mod_timer(&vxlan->age_timer, next_timer);
2169}
2170
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002171static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2172{
2173 __u32 vni = vxlan->default_dst.remote_vni;
2174
2175 vxlan->vn_sock = vs;
2176 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2177}
2178
stephen hemmingerd3428942012-10-01 12:32:35 +00002179/* Setup stats when device is created */
2180static int vxlan_init(struct net_device *dev)
2181{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002182 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002183 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002184 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002185 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002186
WANG Cong1c213bd2014-02-13 11:46:28 -08002187 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002188 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002189 return -ENOMEM;
2190
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002191 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002192 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002193 vxlan->dst_port, vxlan->flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002194 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002195 /* If we have a socket with same port already, reuse it */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002196 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002197 } else {
2198 /* otherwise make new socket outside of RTNL */
2199 dev_hold(dev);
2200 queue_work(vxlan_wq, &vxlan->sock_work);
2201 }
2202 spin_unlock(&vn->sock_lock);
2203
stephen hemmingerd3428942012-10-01 12:32:35 +00002204 return 0;
2205}
2206
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002207static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002208{
2209 struct vxlan_fdb *f;
2210
2211 spin_lock_bh(&vxlan->hash_lock);
2212 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2213 if (f)
2214 vxlan_fdb_destroy(vxlan, f);
2215 spin_unlock_bh(&vxlan->hash_lock);
2216}
2217
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002218static void vxlan_uninit(struct net_device *dev)
2219{
2220 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002221 struct vxlan_sock *vs = vxlan->vn_sock;
2222
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002223 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002224
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002225 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002226 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002227 free_percpu(dev->tstats);
2228}
2229
stephen hemmingerd3428942012-10-01 12:32:35 +00002230/* Start ageing timer and join group when device is brought up */
2231static int vxlan_open(struct net_device *dev)
2232{
2233 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002234 struct vxlan_sock *vs = vxlan->vn_sock;
2235
2236 /* socket hasn't been created */
2237 if (!vs)
2238 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002239
Gao feng79d4a942013-12-10 16:37:32 +08002240 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002241 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002242 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002243 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002244 }
2245
2246 if (vxlan->age_interval)
2247 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2248
2249 return 0;
2250}
2251
2252/* Purge the forwarding table */
2253static void vxlan_flush(struct vxlan_dev *vxlan)
2254{
Cong Wang31fec5a2013-05-27 22:35:52 +00002255 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002256
2257 spin_lock_bh(&vxlan->hash_lock);
2258 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2259 struct hlist_node *p, *n;
2260 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2261 struct vxlan_fdb *f
2262 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002263 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2264 if (!is_zero_ether_addr(f->eth_addr))
2265 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002266 }
2267 }
2268 spin_unlock_bh(&vxlan->hash_lock);
2269}
2270
2271/* Cleanup timer and forwarding table on shutdown */
2272static int vxlan_stop(struct net_device *dev)
2273{
2274 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002275 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002276 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002277
Cong Wange4c7ed42013-08-31 13:44:33 +08002278 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002279 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002280 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002281 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002282 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002283 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002284
2285 del_timer_sync(&vxlan->age_timer);
2286
2287 vxlan_flush(vxlan);
2288
2289 return 0;
2290}
2291
stephen hemmingerd3428942012-10-01 12:32:35 +00002292/* Stub, nothing needs to be done. */
2293static void vxlan_set_multicast_list(struct net_device *dev)
2294{
2295}
2296
Daniel Borkmann345010b2013-12-18 00:21:08 +01002297static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2298{
2299 struct vxlan_dev *vxlan = netdev_priv(dev);
2300 struct vxlan_rdst *dst = &vxlan->default_dst;
2301 struct net_device *lowerdev;
2302 int max_mtu;
2303
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002304 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002305 if (lowerdev == NULL)
2306 return eth_change_mtu(dev, new_mtu);
2307
2308 if (dst->remote_ip.sa.sa_family == AF_INET6)
2309 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2310 else
2311 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2312
2313 if (new_mtu < 68 || new_mtu > max_mtu)
2314 return -EINVAL;
2315
2316 dev->mtu = new_mtu;
2317 return 0;
2318}
2319
stephen hemmingerd3428942012-10-01 12:32:35 +00002320static const struct net_device_ops vxlan_netdev_ops = {
2321 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002322 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002323 .ndo_open = vxlan_open,
2324 .ndo_stop = vxlan_stop,
2325 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002326 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002327 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002328 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002329 .ndo_validate_addr = eth_validate_addr,
2330 .ndo_set_mac_address = eth_mac_addr,
2331 .ndo_fdb_add = vxlan_fdb_add,
2332 .ndo_fdb_del = vxlan_fdb_delete,
2333 .ndo_fdb_dump = vxlan_fdb_dump,
2334};
2335
2336/* Info for udev, that this is a virtual tunnel endpoint */
2337static struct device_type vxlan_type = {
2338 .name = "vxlan",
2339};
2340
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002341/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002342 * supply the listening VXLAN udp ports. Callers are expected
2343 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002344 */
2345void vxlan_get_rx_port(struct net_device *dev)
2346{
2347 struct vxlan_sock *vs;
2348 struct net *net = dev_net(dev);
2349 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2350 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002351 __be16 port;
2352 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002353
2354 spin_lock(&vn->sock_lock);
2355 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002356 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2357 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002358 sa_family = vs->sock->sk->sk_family;
2359 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2360 port);
2361 }
2362 }
2363 spin_unlock(&vn->sock_lock);
2364}
2365EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2366
stephen hemmingerd3428942012-10-01 12:32:35 +00002367/* Initialize the device structure. */
2368static void vxlan_setup(struct net_device *dev)
2369{
2370 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002371 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002372
2373 eth_hw_addr_random(dev);
2374 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002375 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002376 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002377 else
Cong Wang2853af62014-06-12 11:53:10 -07002378 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002379
2380 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002381 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002382 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2383
2384 dev->tx_queue_len = 0;
2385 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002386 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002387 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002388 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002389
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002390 dev->vlan_features = dev->features;
2391 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002392 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002393 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002394 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002395 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002396 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002397
stephen hemminger553675f2013-05-16 11:35:20 +00002398 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002399 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002400 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2401 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002402 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002403
2404 init_timer_deferrable(&vxlan->age_timer);
2405 vxlan->age_timer.function = vxlan_cleanup;
2406 vxlan->age_timer.data = (unsigned long) vxlan;
2407
stephen hemminger823aa872013-04-27 11:31:57 +00002408 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002409
stephen hemmingerd3428942012-10-01 12:32:35 +00002410 vxlan->dev = dev;
2411
2412 for (h = 0; h < FDB_HASH_SIZE; ++h)
2413 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2414}
2415
2416static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2417 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002418 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002419 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002420 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2421 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002422 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002423 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2424 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2425 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2426 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2427 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002428 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002429 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2430 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2431 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2432 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002433 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002434 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2435 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2436 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002437 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2438 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002439 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
stephen hemmingerd3428942012-10-01 12:32:35 +00002440};
2441
2442static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2443{
2444 if (tb[IFLA_ADDRESS]) {
2445 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2446 pr_debug("invalid link address (not ethernet)\n");
2447 return -EINVAL;
2448 }
2449
2450 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2451 pr_debug("invalid all zero ethernet address\n");
2452 return -EADDRNOTAVAIL;
2453 }
2454 }
2455
2456 if (!data)
2457 return -EINVAL;
2458
2459 if (data[IFLA_VXLAN_ID]) {
2460 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2461 if (id >= VXLAN_VID_MASK)
2462 return -ERANGE;
2463 }
2464
stephen hemminger05f47d62012-10-09 20:35:50 +00002465 if (data[IFLA_VXLAN_PORT_RANGE]) {
2466 const struct ifla_vxlan_port_range *p
2467 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2468
2469 if (ntohs(p->high) < ntohs(p->low)) {
2470 pr_debug("port range %u .. %u not valid\n",
2471 ntohs(p->low), ntohs(p->high));
2472 return -EINVAL;
2473 }
2474 }
2475
stephen hemmingerd3428942012-10-01 12:32:35 +00002476 return 0;
2477}
2478
Yan Burman1b13c972013-01-29 23:43:07 +00002479static void vxlan_get_drvinfo(struct net_device *netdev,
2480 struct ethtool_drvinfo *drvinfo)
2481{
2482 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2483 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2484}
2485
2486static const struct ethtool_ops vxlan_ethtool_ops = {
2487 .get_drvinfo = vxlan_get_drvinfo,
2488 .get_link = ethtool_op_get_link,
2489};
2490
stephen hemminger553675f2013-05-16 11:35:20 +00002491static void vxlan_del_work(struct work_struct *work)
2492{
2493 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002494 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002495 kfree_rcu(vs, rcu);
2496}
2497
Tom Herbert3ee64f32014-07-13 19:49:42 -07002498static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2499 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002500{
Cong Wange4c7ed42013-08-31 13:44:33 +08002501 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002502 struct udp_port_cfg udp_conf;
2503 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002504
Tom Herbert3ee64f32014-07-13 19:49:42 -07002505 memset(&udp_conf, 0, sizeof(udp_conf));
2506
2507 if (ipv6) {
2508 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002509 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002510 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002511 } else {
2512 udp_conf.family = AF_INET;
2513 udp_conf.local_ip.s_addr = INADDR_ANY;
Cong Wange4c7ed42013-08-31 13:44:33 +08002514 }
2515
Tom Herbert3ee64f32014-07-13 19:49:42 -07002516 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002517
Tom Herbert3ee64f32014-07-13 19:49:42 -07002518 /* Open UDP socket */
2519 err = udp_sock_create(net, &udp_conf, &sock);
2520 if (err < 0)
2521 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002522
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002523 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002524}
2525
2526/* Create new listen socket if needed */
2527static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002528 vxlan_rcv_t *rcv, void *data,
2529 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002530{
2531 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2532 struct vxlan_sock *vs;
2533 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002534 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002535 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002536 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002537
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002538 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002539 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002540 return ERR_PTR(-ENOMEM);
2541
2542 for (h = 0; h < VNI_HASH_SIZE; ++h)
2543 INIT_HLIST_HEAD(&vs->vni_list[h]);
2544
2545 INIT_WORK(&vs->del_work, vxlan_del_work);
2546
Tom Herbert3ee64f32014-07-13 19:49:42 -07002547 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002548 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002549 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002550 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002551 }
2552
Cong Wange4c7ed42013-08-31 13:44:33 +08002553 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002554 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002555 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002556 vs->data = data;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002557 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002558
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002559 /* Initialize the vxlan udp offloads structure */
2560 vs->udp_offloads.port = port;
2561 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2562 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2563
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002564 spin_lock(&vn->sock_lock);
2565 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002566 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002567 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002568
2569 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002570 tunnel_cfg.sk_user_data = vs;
2571 tunnel_cfg.encap_type = 1;
2572 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2573 tunnel_cfg.encap_destroy = NULL;
2574
2575 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002576
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002577 return vs;
2578}
stephen hemminger553675f2013-05-16 11:35:20 +00002579
Pravin B Shelar012a5722013-08-19 11:23:07 -07002580struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2581 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002582 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002583{
2584 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2585 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002586 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002587
Tom Herbert359a0ea2014-06-04 17:20:29 -07002588 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002589 if (!IS_ERR(vs))
2590 return vs;
2591
Pravin B Shelar012a5722013-08-19 11:23:07 -07002592 if (no_share) /* Return error if sharing is not allowed. */
2593 return vs;
2594
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002595 spin_lock(&vn->sock_lock);
Thomas Grafac5132d2015-01-15 03:53:56 +01002596 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002597 if (vs && ((vs->rcv != rcv) ||
2598 !atomic_add_unless(&vs->refcnt, 1, 0)))
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002599 vs = ERR_PTR(-EBUSY);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002600 spin_unlock(&vn->sock_lock);
2601
2602 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002603 vs = ERR_PTR(-EINVAL);
2604
stephen hemminger553675f2013-05-16 11:35:20 +00002605 return vs;
2606}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002607EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002608
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002609/* Scheduled at device creation to bind to a socket */
2610static void vxlan_sock_work(struct work_struct *work)
2611{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002612 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002613 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002614 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002615 __be16 port = vxlan->dst_port;
2616 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002617
Tom Herbert359a0ea2014-06-04 17:20:29 -07002618 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002619 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002620 if (!IS_ERR(nvs))
2621 vxlan_vs_add_dev(nvs, vxlan);
2622 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002623
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002624 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002625}
2626
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002627static int vxlan_newlink(struct net *src_net, struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +00002628 struct nlattr *tb[], struct nlattr *data[])
2629{
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002630 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002631 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002632 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002633 __u32 vni;
2634 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002635 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002636
2637 if (!data[IFLA_VXLAN_ID])
2638 return -EINVAL;
2639
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002640 vxlan->net = src_net;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002641
stephen hemmingerd3428942012-10-01 12:32:35 +00002642 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002643 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002644
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002645 /* Unless IPv6 is explicitly requested, assume IPv4 */
2646 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002647 if (data[IFLA_VXLAN_GROUP]) {
2648 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002649 } else if (data[IFLA_VXLAN_GROUP6]) {
2650 if (!IS_ENABLED(CONFIG_IPV6))
2651 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002652
Cong Wange4c7ed42013-08-31 13:44:33 +08002653 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2654 sizeof(struct in6_addr));
2655 dst->remote_ip.sa.sa_family = AF_INET6;
2656 use_ipv6 = true;
2657 }
2658
2659 if (data[IFLA_VXLAN_LOCAL]) {
2660 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2661 vxlan->saddr.sa.sa_family = AF_INET;
2662 } else if (data[IFLA_VXLAN_LOCAL6]) {
2663 if (!IS_ENABLED(CONFIG_IPV6))
2664 return -EPFNOSUPPORT;
2665
2666 /* TODO: respect scope id */
2667 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2668 sizeof(struct in6_addr));
2669 vxlan->saddr.sa.sa_family = AF_INET6;
2670 use_ipv6 = true;
2671 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002672
stephen hemminger34e02aa2012-10-09 20:35:53 +00002673 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002674 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002675 struct net_device *lowerdev
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002676 = __dev_get_by_index(src_net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002677
stephen hemminger34e02aa2012-10-09 20:35:53 +00002678 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002679 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002680 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002681 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002682
Cong Wange4c7ed42013-08-31 13:44:33 +08002683#if IS_ENABLED(CONFIG_IPV6)
2684 if (use_ipv6) {
2685 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2686 if (idev && idev->cnf.disable_ipv6) {
2687 pr_info("IPv6 is disabled via sysctl\n");
2688 return -EPERM;
2689 }
2690 vxlan->flags |= VXLAN_F_IPV6;
2691 }
2692#endif
2693
stephen hemminger34e02aa2012-10-09 20:35:53 +00002694 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002695 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002696
Cong Wang2853af62014-06-12 11:53:10 -07002697 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002698 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002699 } else if (use_ipv6)
2700 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002701
2702 if (data[IFLA_VXLAN_TOS])
2703 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2704
Vincent Bernatafb97182012-10-30 10:27:16 +00002705 if (data[IFLA_VXLAN_TTL])
2706 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2707
stephen hemmingerd3428942012-10-01 12:32:35 +00002708 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002709 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002710
2711 if (data[IFLA_VXLAN_AGEING])
2712 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2713 else
2714 vxlan->age_interval = FDB_AGE_DEFAULT;
2715
David Stevense4f67ad2012-11-20 02:50:14 +00002716 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2717 vxlan->flags |= VXLAN_F_PROXY;
2718
2719 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2720 vxlan->flags |= VXLAN_F_RSC;
2721
2722 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2723 vxlan->flags |= VXLAN_F_L2MISS;
2724
2725 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2726 vxlan->flags |= VXLAN_F_L3MISS;
2727
stephen hemmingerd3428942012-10-01 12:32:35 +00002728 if (data[IFLA_VXLAN_LIMIT])
2729 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2730
stephen hemminger05f47d62012-10-09 20:35:50 +00002731 if (data[IFLA_VXLAN_PORT_RANGE]) {
2732 const struct ifla_vxlan_port_range *p
2733 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2734 vxlan->port_min = ntohs(p->low);
2735 vxlan->port_max = ntohs(p->high);
2736 }
2737
stephen hemminger823aa872013-04-27 11:31:57 +00002738 if (data[IFLA_VXLAN_PORT])
2739 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2740
Tom Herbert359a0ea2014-06-04 17:20:29 -07002741 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2742 vxlan->flags |= VXLAN_F_UDP_CSUM;
2743
2744 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2745 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2746 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2747
2748 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2749 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2750 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2751
Tom Herbertdfd86452015-01-12 17:00:38 -08002752 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2753 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2754 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2755
2756 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2757 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2758 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2759
Thomas Graf35114942015-01-15 03:53:55 +01002760 if (data[IFLA_VXLAN_GBP])
2761 vxlan->flags |= VXLAN_F_GBP;
2762
Nicolas Dichtel33564bb2015-01-26 22:28:14 +01002763 if (vxlan_find_vni(src_net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002764 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002765 pr_info("duplicate VNI %u\n", vni);
2766 return -EEXIST;
2767 }
2768
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002769 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002770
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002771 /* create an fdb entry for a valid default destination */
2772 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2773 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2774 &vxlan->default_dst.remote_ip,
2775 NUD_REACHABLE|NUD_PERMANENT,
2776 NLM_F_EXCL|NLM_F_CREATE,
2777 vxlan->dst_port,
2778 vxlan->default_dst.remote_vni,
2779 vxlan->default_dst.remote_ifindex,
2780 NTF_SELF);
2781 if (err)
2782 return err;
2783 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002784
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002785 err = register_netdevice(dev);
2786 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002787 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002788 return err;
2789 }
2790
stephen hemminger553675f2013-05-16 11:35:20 +00002791 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002792
2793 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002794}
2795
2796static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2797{
2798 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002799 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002800
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002801 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002802 if (!hlist_unhashed(&vxlan->hlist))
2803 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002804 spin_unlock(&vn->sock_lock);
2805
stephen hemminger553675f2013-05-16 11:35:20 +00002806 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002807 unregister_netdevice_queue(dev, head);
2808}
2809
2810static size_t vxlan_get_size(const struct net_device *dev)
2811{
2812
2813 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002814 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002815 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002816 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002817 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2818 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2819 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002820 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2821 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2822 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2823 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002824 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2825 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002826 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002827 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2828 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2829 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2832 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002833 0;
2834}
2835
2836static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2837{
2838 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002839 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002840 struct ifla_vxlan_port_range ports = {
2841 .low = htons(vxlan->port_min),
2842 .high = htons(vxlan->port_max),
2843 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002844
Atzm Watanabec7995c42013-04-16 02:50:52 +00002845 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002846 goto nla_put_failure;
2847
Cong Wange4c7ed42013-08-31 13:44:33 +08002848 if (!vxlan_addr_any(&dst->remote_ip)) {
2849 if (dst->remote_ip.sa.sa_family == AF_INET) {
2850 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2851 dst->remote_ip.sin.sin_addr.s_addr))
2852 goto nla_put_failure;
2853#if IS_ENABLED(CONFIG_IPV6)
2854 } else {
2855 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2856 &dst->remote_ip.sin6.sin6_addr))
2857 goto nla_put_failure;
2858#endif
2859 }
2860 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002861
Atzm Watanabec7995c42013-04-16 02:50:52 +00002862 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002863 goto nla_put_failure;
2864
Cong Wange4c7ed42013-08-31 13:44:33 +08002865 if (!vxlan_addr_any(&vxlan->saddr)) {
2866 if (vxlan->saddr.sa.sa_family == AF_INET) {
2867 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2868 vxlan->saddr.sin.sin_addr.s_addr))
2869 goto nla_put_failure;
2870#if IS_ENABLED(CONFIG_IPV6)
2871 } else {
2872 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2873 &vxlan->saddr.sin6.sin6_addr))
2874 goto nla_put_failure;
2875#endif
2876 }
2877 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002878
2879 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2880 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002881 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2882 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2883 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2884 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2885 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2886 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2887 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2888 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2889 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002890 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002891 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002892 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2893 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2894 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2895 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2896 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2897 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002898 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2899 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2900 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2901 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2902 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002903 goto nla_put_failure;
2904
stephen hemminger05f47d62012-10-09 20:35:50 +00002905 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2906 goto nla_put_failure;
2907
Thomas Graf35114942015-01-15 03:53:55 +01002908 if (vxlan->flags & VXLAN_F_GBP &&
2909 nla_put_flag(skb, IFLA_VXLAN_GBP))
2910 goto nla_put_failure;
2911
stephen hemmingerd3428942012-10-01 12:32:35 +00002912 return 0;
2913
2914nla_put_failure:
2915 return -EMSGSIZE;
2916}
2917
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002918static struct net *vxlan_get_link_net(const struct net_device *dev)
2919{
2920 struct vxlan_dev *vxlan = netdev_priv(dev);
2921
2922 return vxlan->net;
2923}
2924
stephen hemmingerd3428942012-10-01 12:32:35 +00002925static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2926 .kind = "vxlan",
2927 .maxtype = IFLA_VXLAN_MAX,
2928 .policy = vxlan_policy,
2929 .priv_size = sizeof(struct vxlan_dev),
2930 .setup = vxlan_setup,
2931 .validate = vxlan_validate,
2932 .newlink = vxlan_newlink,
2933 .dellink = vxlan_dellink,
2934 .get_size = vxlan_get_size,
2935 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002936 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002937};
2938
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002939static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2940 struct net_device *dev)
2941{
2942 struct vxlan_dev *vxlan, *next;
2943 LIST_HEAD(list_kill);
2944
2945 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2946 struct vxlan_rdst *dst = &vxlan->default_dst;
2947
2948 /* In case we created vxlan device with carrier
2949 * and we loose the carrier due to module unload
2950 * we also need to remove vxlan device. In other
2951 * cases, it's not necessary and remote_ifindex
2952 * is 0 here, so no matches.
2953 */
2954 if (dst->remote_ifindex == dev->ifindex)
2955 vxlan_dellink(vxlan->dev, &list_kill);
2956 }
2957
2958 unregister_netdevice_many(&list_kill);
2959}
2960
2961static int vxlan_lowerdev_event(struct notifier_block *unused,
2962 unsigned long event, void *ptr)
2963{
2964 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002965 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002966
Daniel Borkmann783c1462014-01-22 21:07:53 +01002967 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002968 vxlan_handle_lowerdev_unregister(vn, dev);
2969
2970 return NOTIFY_DONE;
2971}
2972
2973static struct notifier_block vxlan_notifier_block __read_mostly = {
2974 .notifier_call = vxlan_lowerdev_event,
2975};
2976
stephen hemmingerd3428942012-10-01 12:32:35 +00002977static __net_init int vxlan_init_net(struct net *net)
2978{
2979 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002980 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002981
stephen hemminger553675f2013-05-16 11:35:20 +00002982 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002983 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002984
stephen hemminger553675f2013-05-16 11:35:20 +00002985 for (h = 0; h < PORT_HASH_SIZE; ++h)
2986 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002987
2988 return 0;
2989}
2990
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002991static void __net_exit vxlan_exit_net(struct net *net)
2992{
2993 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2994 struct vxlan_dev *vxlan, *next;
2995 struct net_device *dev, *aux;
2996 LIST_HEAD(list);
2997
2998 rtnl_lock();
2999 for_each_netdev_safe(net, dev, aux)
3000 if (dev->rtnl_link_ops == &vxlan_link_ops)
3001 unregister_netdevice_queue(dev, &list);
3002
3003 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3004 /* If vxlan->dev is in the same netns, it has already been added
3005 * to the list by the previous loop.
3006 */
3007 if (!net_eq(dev_net(vxlan->dev), net))
3008 unregister_netdevice_queue(dev, &list);
3009 }
3010
3011 unregister_netdevice_many(&list);
3012 rtnl_unlock();
3013}
3014
stephen hemmingerd3428942012-10-01 12:32:35 +00003015static struct pernet_operations vxlan_net_ops = {
3016 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003017 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003018 .id = &vxlan_net_id,
3019 .size = sizeof(struct vxlan_net),
3020};
3021
3022static int __init vxlan_init_module(void)
3023{
3024 int rc;
3025
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003026 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3027 if (!vxlan_wq)
3028 return -ENOMEM;
3029
stephen hemmingerd3428942012-10-01 12:32:35 +00003030 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3031
Daniel Borkmann783c1462014-01-22 21:07:53 +01003032 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003033 if (rc)
3034 goto out1;
3035
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003036 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003037 if (rc)
3038 goto out2;
3039
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003040 rc = rtnl_link_register(&vxlan_link_ops);
3041 if (rc)
3042 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003043
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003044 return 0;
3045out3:
3046 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003047out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003048 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003049out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003050 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003051 return rc;
3052}
Cong Wang7332a132013-05-27 22:35:53 +00003053late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003054
3055static void __exit vxlan_cleanup_module(void)
3056{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003057 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003058 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003059 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003060 unregister_pernet_subsys(&vxlan_net_ops);
3061 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003062}
3063module_exit(vxlan_cleanup_module);
3064
3065MODULE_LICENSE("GPL");
3066MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003067MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003068MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003069MODULE_ALIAS_RTNL_LINK("vxlan");