blob: 31bac2a21ce302562edcf0b3a7b3786e3ad2054b [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
stephen hemmingerd3428942012-10-01 12:32:35 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
Pravin B Shelar1eaa8172013-08-19 11:23:29 -070027#include <linux/if_vlan.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000028#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000029#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000030#include <net/arp.h>
31#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000032#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000033#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/icmp.h>
35#include <net/udp.h>
Tom Herbert3ee64f32014-07-13 19:49:42 -070036#include <net/udp_tunnel.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000037#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Pravin B Shelar012a5722013-08-19 11:23:07 -070043#include <net/vxlan.h>
Or Gerlitzdc01e7d2014-01-20 13:59:21 +020044#include <net/protocol.h>
Andy Zhouacbf74a2014-09-16 17:31:18 -070045#include <net/udp_tunnel.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080046#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
Cong Wang660d98c2013-09-02 10:06:52 +080050#include <net/ip6_checksum.h>
Cong Wange4c7ed42013-08-31 13:44:33 +080051#endif
stephen hemmingerd3428942012-10-01 12:32:35 +000052
53#define VXLAN_VERSION "0.1"
54
stephen hemminger553675f2013-05-16 11:35:20 +000055#define PORT_HASH_BITS 8
56#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000057#define VNI_HASH_BITS 10
58#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59#define FDB_HASH_BITS 8
60#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61#define FDB_AGE_DEFAULT 300 /* 5 min */
62#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
63
stephen hemminger23c578b2013-04-27 11:31:53 +000064/* UDP port for VXLAN traffic.
65 * The IANA assigned port is 4789, but the Linux default is 8472
Stephen Hemminger234f5b72013-06-17 14:16:41 -070066 * for compatibility with early adopters.
stephen hemminger23c578b2013-04-27 11:31:53 +000067 */
Stephen Hemminger9daaa392013-06-17 14:16:12 -070068static unsigned short vxlan_port __read_mostly = 8472;
69module_param_named(udp_port, vxlan_port, ushort, 0444);
stephen hemmingerd3428942012-10-01 12:32:35 +000070MODULE_PARM_DESC(udp_port, "Destination UDP port");
71
72static bool log_ecn_error = true;
73module_param(log_ecn_error, bool, 0644);
74MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
75
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -070076static int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000077
Mike Rapoportafbd8ba2013-06-25 16:01:51 +030078static const u8 all_zeros_mac[ETH_ALEN];
79
stephen hemminger553675f2013-05-16 11:35:20 +000080/* per-network namespace private data for this module */
81struct vxlan_net {
82 struct list_head vxlan_list;
83 struct hlist_head sock_list[PORT_HASH_SIZE];
Stephen Hemminger1c51a912013-06-17 14:16:11 -070084 spinlock_t sock_lock;
stephen hemminger553675f2013-05-16 11:35:20 +000085};
86
Cong Wange4c7ed42013-08-31 13:44:33 +080087union vxlan_addr {
88 struct sockaddr_in sin;
89 struct sockaddr_in6 sin6;
90 struct sockaddr sa;
91};
92
David Stevens66817122013-03-15 04:35:51 +000093struct vxlan_rdst {
Cong Wange4c7ed42013-08-31 13:44:33 +080094 union vxlan_addr remote_ip;
David Stevens66817122013-03-15 04:35:51 +000095 __be16 remote_port;
96 u32 remote_vni;
97 u32 remote_ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -070098 struct list_head list;
Mike Rapoportbc7892b2013-06-25 16:01:54 +030099 struct rcu_head rcu;
David Stevens66817122013-03-15 04:35:51 +0000100};
101
stephen hemmingerd3428942012-10-01 12:32:35 +0000102/* Forwarding table entry */
103struct vxlan_fdb {
104 struct hlist_node hlist; /* linked list of entries */
105 struct rcu_head rcu;
106 unsigned long updated; /* jiffies */
107 unsigned long used;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700108 struct list_head remotes;
stephen hemmingerd3428942012-10-01 12:32:35 +0000109 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000110 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000111 u8 eth_addr[ETH_ALEN];
112};
113
stephen hemmingerd3428942012-10-01 12:32:35 +0000114/* Pseudo network device */
115struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000116 struct hlist_node hlist; /* vni hash table */
117 struct list_head next; /* vxlan's per namespace list */
118 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000119 struct net_device *dev;
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +0200120 struct net *net; /* netns for packet i/o */
Atzm Watanabec7995c42013-04-16 02:50:52 +0000121 struct vxlan_rdst default_dst; /* default destination */
Cong Wange4c7ed42013-08-31 13:44:33 +0800122 union vxlan_addr saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000123 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000124 __u16 port_min; /* source port range */
125 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000126 __u8 tos; /* TOS override */
127 __u8 ttl;
Tom Herbert359a0ea2014-06-04 17:20:29 -0700128 u32 flags; /* VXLAN_F_* in vxlan.h */
stephen hemmingerd3428942012-10-01 12:32:35 +0000129
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700130 struct work_struct sock_work;
stephen hemminger3fc2de22013-07-18 08:40:15 -0700131 struct work_struct igmp_join;
132 struct work_struct igmp_leave;
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700133
stephen hemmingerd3428942012-10-01 12:32:35 +0000134 unsigned long age_interval;
135 struct timer_list age_timer;
136 spinlock_t hash_lock;
137 unsigned int addrcnt;
138 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000139
140 struct hlist_head fdb_head[FDB_HASH_SIZE];
141};
142
143/* salt for hash table */
144static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700145static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000146
Stephen Hemminger1c51a912013-06-17 14:16:11 -0700147static void vxlan_sock_work(struct work_struct *work);
148
Cong Wange4c7ed42013-08-31 13:44:33 +0800149#if IS_ENABLED(CONFIG_IPV6)
150static inline
151bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
152{
153 if (a->sa.sa_family != b->sa.sa_family)
154 return false;
155 if (a->sa.sa_family == AF_INET6)
156 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
157 else
158 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
159}
160
161static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
162{
163 if (ipa->sa.sa_family == AF_INET6)
164 return ipv6_addr_any(&ipa->sin6.sin6_addr);
165 else
166 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
167}
168
169static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
170{
171 if (ipa->sa.sa_family == AF_INET6)
172 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
173 else
174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
175}
176
177static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
178{
179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
180 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
181 ip->sa.sa_family = AF_INET6;
182 return 0;
183 } else if (nla_len(nla) >= sizeof(__be32)) {
184 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
185 ip->sa.sa_family = AF_INET;
186 return 0;
187 } else {
188 return -EAFNOSUPPORT;
189 }
190}
191
192static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
193 const union vxlan_addr *ip)
194{
195 if (ip->sa.sa_family == AF_INET6)
196 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
197 else
198 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
199}
200
201#else /* !CONFIG_IPV6 */
202
203static inline
204bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
205{
206 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
207}
208
209static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
210{
211 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
212}
213
214static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
215{
216 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
217}
218
219static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
220{
221 if (nla_len(nla) >= sizeof(struct in6_addr)) {
222 return -EAFNOSUPPORT;
223 } else if (nla_len(nla) >= sizeof(__be32)) {
224 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
225 ip->sa.sa_family = AF_INET;
226 return 0;
227 } else {
228 return -EAFNOSUPPORT;
229 }
230}
231
232static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
233 const union vxlan_addr *ip)
234{
235 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
236}
237#endif
238
stephen hemminger553675f2013-05-16 11:35:20 +0000239/* Virtual Network hash table head */
240static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
241{
242 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
243}
244
245/* Socket hash table head */
246static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000247{
248 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
249
stephen hemminger553675f2013-05-16 11:35:20 +0000250 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
251}
252
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700253/* First remote destination for a forwarding entry.
254 * Guaranteed to be non-NULL because remotes are never deleted.
255 */
stephen hemminger5ca54612013-08-04 17:17:39 -0700256static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700257{
stephen hemminger5ca54612013-08-04 17:17:39 -0700258 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
259}
260
261static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
262{
263 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700264}
265
Thomas Grafac5132d2015-01-15 03:53:56 +0100266/* Find VXLAN socket based on network namespace, address family and UDP port
267 * and enabled unshareable flags.
268 */
269static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
270 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000271{
272 struct vxlan_sock *vs;
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800273
274 flags &= VXLAN_F_RCV_FLAGS;
stephen hemminger553675f2013-05-16 11:35:20 +0000275
276 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200277 if (inet_sk(vs->sock->sk)->inet_sport == port &&
Thomas Grafac5132d2015-01-15 03:53:56 +0100278 inet_sk(vs->sock->sk)->sk.sk_family == family &&
Tom Herbertaf33c1a2015-01-20 11:23:05 -0800279 vs->flags == flags)
stephen hemminger553675f2013-05-16 11:35:20 +0000280 return vs;
281 }
282 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000283}
284
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700285static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000286{
287 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000288
stephen hemminger553675f2013-05-16 11:35:20 +0000289 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000290 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000291 return vxlan;
292 }
293
294 return NULL;
295}
296
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700297/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200298static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
Thomas Grafac5132d2015-01-15 03:53:56 +0100299 sa_family_t family, __be16 port,
300 u32 flags)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700301{
302 struct vxlan_sock *vs;
303
Thomas Grafac5132d2015-01-15 03:53:56 +0100304 vs = vxlan_find_sock(net, family, port, flags);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700305 if (!vs)
306 return NULL;
307
308 return vxlan_vs_find_vni(vs, id);
309}
310
stephen hemmingerd3428942012-10-01 12:32:35 +0000311/* Fill in neighbour message in skbuff. */
312static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700313 const struct vxlan_fdb *fdb,
314 u32 portid, u32 seq, int type, unsigned int flags,
315 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000316{
317 unsigned long now = jiffies;
318 struct nda_cacheinfo ci;
319 struct nlmsghdr *nlh;
320 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000321 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000322
323 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
324 if (nlh == NULL)
325 return -EMSGSIZE;
326
327 ndm = nlmsg_data(nlh);
328 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000329
330 send_eth = send_ip = true;
331
332 if (type == RTM_GETNEIGH) {
333 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800334 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000335 send_eth = !is_zero_ether_addr(fdb->eth_addr);
336 } else
337 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000338 ndm->ndm_state = fdb->state;
339 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000340 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800341 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000342
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100343 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
Nicolas Dichtel49670822015-01-26 14:10:53 +0100344 nla_put_s32(skb, NDA_LINK_NETNSID,
345 peernet2id(dev_net(vxlan->dev), vxlan->net)))
Nicolas Dichtel193523b2015-01-20 15:15:47 +0100346 goto nla_put_failure;
347
David Stevense4f67ad2012-11-20 02:50:14 +0000348 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000349 goto nla_put_failure;
350
Cong Wange4c7ed42013-08-31 13:44:33 +0800351 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000352 goto nla_put_failure;
353
stephen hemminger823aa872013-04-27 11:31:57 +0000354 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000355 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
356 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000357 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700358 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000359 goto nla_put_failure;
360 if (rdst->remote_ifindex &&
361 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000362 goto nla_put_failure;
363
364 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
365 ci.ndm_confirmed = 0;
366 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
367 ci.ndm_refcnt = 0;
368
369 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
370 goto nla_put_failure;
371
Johannes Berg053c0952015-01-16 22:09:00 +0100372 nlmsg_end(skb, nlh);
373 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000374
375nla_put_failure:
376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
378}
379
380static inline size_t vxlan_nlmsg_size(void)
381{
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
Nicolas Dichtel49670822015-01-26 14:10:53 +0100388 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
stephen hemmingerd3428942012-10-01 12:32:35 +0000389 + nla_total_size(sizeof(struct nda_cacheinfo));
390}
391
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200392static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
393 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000394{
395 struct net *net = dev_net(vxlan->dev);
396 struct sk_buff *skb;
397 int err = -ENOBUFS;
398
399 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
400 if (skb == NULL)
401 goto errout;
402
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200403 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000404 if (err < 0) {
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err == -EMSGSIZE);
407 kfree_skb(skb);
408 goto errout;
409 }
410
411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
412 return;
413errout:
414 if (err < 0)
415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
416}
417
Cong Wange4c7ed42013-08-31 13:44:33 +0800418static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000419{
420 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700421 struct vxlan_fdb f = {
422 .state = NUD_STALE,
423 };
424 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800425 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700426 .remote_vni = VXLAN_N_VID,
427 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700428
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200429 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000430}
431
432static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
433{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700434 struct vxlan_fdb f = {
435 .state = NUD_STALE,
436 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200437 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000438
David Stevense4f67ad2012-11-20 02:50:14 +0000439 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
440
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200441 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000442}
443
stephen hemmingerd3428942012-10-01 12:32:35 +0000444/* Hash Ethernet address */
445static u32 eth_hash(const unsigned char *addr)
446{
447 u64 value = get_unaligned((u64 *)addr);
448
449 /* only want 6 bytes */
450#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000451 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000452#else
453 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000454#endif
455 return hash_64(value, FDB_HASH_BITS);
456}
457
458/* Hash chain to use given mac address */
459static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
460 const u8 *mac)
461{
462 return &vxlan->fdb_head[eth_hash(mac)];
463}
464
465/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000466static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000467 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000468{
469 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
470 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000471
Sasha Levinb67bfe02013-02-27 17:06:00 -0800472 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700473 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000474 return f;
475 }
476
477 return NULL;
478}
479
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000480static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
481 const u8 *mac)
482{
483 struct vxlan_fdb *f;
484
485 f = __vxlan_find_mac(vxlan, mac);
486 if (f)
487 f->used = jiffies;
488
489 return f;
490}
491
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300492/* caller should hold vxlan->hash_lock */
493static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800494 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300495 __u32 vni, __u32 ifindex)
496{
497 struct vxlan_rdst *rd;
498
499 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800500 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300501 rd->remote_port == port &&
502 rd->remote_vni == vni &&
503 rd->remote_ifindex == ifindex)
504 return rd;
505 }
506
507 return NULL;
508}
509
Thomas Richter906dc182013-07-19 17:20:07 +0200510/* Replace destination of unicast mac */
511static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800512 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200513{
514 struct vxlan_rdst *rd;
515
516 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
517 if (rd)
518 return 0;
519
520 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
521 if (!rd)
522 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800523 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200524 rd->remote_port = port;
525 rd->remote_vni = vni;
526 rd->remote_ifindex = ifindex;
527 return 1;
528}
529
David Stevens66817122013-03-15 04:35:51 +0000530/* Add/update destinations for multicast */
531static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200532 union vxlan_addr *ip, __be16 port, __u32 vni,
533 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000534{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700535 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000536
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300537 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
538 if (rd)
539 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700540
David Stevens66817122013-03-15 04:35:51 +0000541 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
542 if (rd == NULL)
543 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800544 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000545 rd->remote_port = port;
546 rd->remote_vni = vni;
547 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700548
549 list_add_tail_rcu(&rd->list, &f->remotes);
550
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200551 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000552 return 1;
553}
554
Tom Herbertdfd86452015-01-12 17:00:38 -0800555static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
556 unsigned int off,
557 struct vxlanhdr *vh, size_t hdrlen,
558 u32 data)
559{
560 size_t start, offset, plen;
561 __wsum delta;
562
563 if (skb->remcsum_offload)
564 return vh;
565
566 if (!NAPI_GRO_CB(skb)->csum_valid)
567 return NULL;
568
569 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
570 offset = start + ((data & VXLAN_RCO_UDP) ?
571 offsetof(struct udphdr, check) :
572 offsetof(struct tcphdr, check));
573
574 plen = hdrlen + offset + sizeof(u16);
575
576 /* Pull checksum that will be written */
577 if (skb_gro_header_hard(skb, off + plen)) {
578 vh = skb_gro_header_slow(skb, off + plen, off);
579 if (!vh)
580 return NULL;
581 }
582
583 delta = remcsum_adjust((void *)vh + hdrlen,
584 NAPI_GRO_CB(skb)->csum, start, offset);
585
586 /* Adjust skb->csum since we changed the packet */
587 skb->csum = csum_add(skb->csum, delta);
588 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
589
590 skb->remcsum_offload = 1;
591
592 return vh;
593}
594
Tom Herberta2b12f32015-01-12 17:00:37 -0800595static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
596 struct sk_buff *skb,
597 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200598{
599 struct sk_buff *p, **pp = NULL;
600 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800601 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200602 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800603 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
604 udp_offloads);
605 u32 flags;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200606
607 off_vx = skb_gro_offset(skb);
608 hlen = off_vx + sizeof(*vh);
609 vh = skb_gro_header_fast(skb, off_vx);
610 if (skb_gro_header_hard(skb, hlen)) {
611 vh = skb_gro_header_slow(skb, hlen, off_vx);
612 if (unlikely(!vh))
613 goto out;
614 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200615
Tom Herbertdfd86452015-01-12 17:00:38 -0800616 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
617 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
618
619 flags = ntohl(vh->vx_flags);
620
621 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
622 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
623 ntohl(vh->vx_vni));
624
625 if (!vh)
626 goto out;
627 }
628
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200629 flush = 0;
630
631 for (p = *head; p; p = p->next) {
632 if (!NAPI_GRO_CB(p)->same_flow)
633 continue;
634
635 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Thomas Graf35114942015-01-15 03:53:55 +0100636 if (vh->vx_flags != vh2->vx_flags ||
637 vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200638 NAPI_GRO_CB(p)->same_flow = 0;
639 continue;
640 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200641 }
642
Jesse Gross9b174d82014-12-30 19:10:15 -0800643 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200644
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200645out:
646 NAPI_GRO_CB(skb)->flush |= flush;
647
648 return pp;
649}
650
Tom Herberta2b12f32015-01-12 17:00:37 -0800651static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
652 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200653{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800654 udp_tunnel_gro_complete(skb, nhoff);
655
Jesse Gross9b174d82014-12-30 19:10:15 -0800656 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200657}
658
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700659/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200660static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700661{
662 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200663 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700664 struct net *net = sock_net(sk);
665 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700666 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200667 int err;
668
669 if (sa_family == AF_INET) {
670 err = udp_add_offload(&vs->udp_offloads);
671 if (err)
672 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
673 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700674
675 rcu_read_lock();
676 for_each_netdev_rcu(net, dev) {
677 if (dev->netdev_ops->ndo_add_vxlan_port)
678 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
679 port);
680 }
681 rcu_read_unlock();
682}
683
684/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200685static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700686{
687 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200688 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700689 struct net *net = sock_net(sk);
690 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700691 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700692
693 rcu_read_lock();
694 for_each_netdev_rcu(net, dev) {
695 if (dev->netdev_ops->ndo_del_vxlan_port)
696 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
697 port);
698 }
699 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200700
701 if (sa_family == AF_INET)
702 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700703}
704
stephen hemmingerd3428942012-10-01 12:32:35 +0000705/* Add new entry to forwarding table -- assumes lock held */
706static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800707 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000708 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000709 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000710 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000711{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200712 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000713 struct vxlan_fdb *f;
714 int notify = 0;
715
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000716 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000717 if (f) {
718 if (flags & NLM_F_EXCL) {
719 netdev_dbg(vxlan->dev,
720 "lost race to create %pM\n", mac);
721 return -EEXIST;
722 }
723 if (f->state != state) {
724 f->state = state;
725 f->updated = jiffies;
726 notify = 1;
727 }
David Stevensae884082013-04-19 00:36:26 +0000728 if (f->flags != ndm_flags) {
729 f->flags = ndm_flags;
730 f->updated = jiffies;
731 notify = 1;
732 }
Thomas Richter906dc182013-07-19 17:20:07 +0200733 if ((flags & NLM_F_REPLACE)) {
734 /* Only change unicasts */
735 if (!(is_multicast_ether_addr(f->eth_addr) ||
736 is_zero_ether_addr(f->eth_addr))) {
737 int rc = vxlan_fdb_replace(f, ip, port, vni,
738 ifindex);
739
740 if (rc < 0)
741 return rc;
742 notify |= rc;
743 } else
744 return -EOPNOTSUPP;
745 }
David Stevens66817122013-03-15 04:35:51 +0000746 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300747 (is_multicast_ether_addr(f->eth_addr) ||
748 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200749 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
750 &rd);
David Stevens66817122013-03-15 04:35:51 +0000751
752 if (rc < 0)
753 return rc;
754 notify |= rc;
755 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000756 } else {
757 if (!(flags & NLM_F_CREATE))
758 return -ENOENT;
759
760 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
761 return -ENOSPC;
762
Thomas Richter906dc182013-07-19 17:20:07 +0200763 /* Disallow replace to add a multicast entry */
764 if ((flags & NLM_F_REPLACE) &&
765 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
766 return -EOPNOTSUPP;
767
Cong Wange4c7ed42013-08-31 13:44:33 +0800768 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000769 f = kmalloc(sizeof(*f), GFP_ATOMIC);
770 if (!f)
771 return -ENOMEM;
772
773 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000774 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000775 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000776 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700777 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000778 memcpy(f->eth_addr, mac, ETH_ALEN);
779
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200780 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700781
stephen hemmingerd3428942012-10-01 12:32:35 +0000782 ++vxlan->addrcnt;
783 hlist_add_head_rcu(&f->hlist,
784 vxlan_fdb_head(vxlan, mac));
785 }
786
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200787 if (notify) {
788 if (rd == NULL)
789 rd = first_remote_rtnl(f);
790 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
791 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000792
793 return 0;
794}
795
Wei Yongjun6706c822013-04-11 19:00:35 +0000796static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000797{
798 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700799 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000800
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700801 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000802 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000803 kfree(f);
804}
805
stephen hemmingerd3428942012-10-01 12:32:35 +0000806static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
807{
808 netdev_dbg(vxlan->dev,
809 "delete %pM\n", f->eth_addr);
810
811 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200812 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000813
814 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000815 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000816}
817
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300818static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800819 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300820{
821 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800822 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300823
824 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800825 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
826 if (err)
827 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300828 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800829 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
830 if (remote->sa.sa_family == AF_INET) {
831 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
832 ip->sa.sa_family = AF_INET;
833#if IS_ENABLED(CONFIG_IPV6)
834 } else {
835 ip->sin6.sin6_addr = in6addr_any;
836 ip->sa.sa_family = AF_INET6;
837#endif
838 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300839 }
840
841 if (tb[NDA_PORT]) {
842 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
843 return -EINVAL;
844 *port = nla_get_be16(tb[NDA_PORT]);
845 } else {
846 *port = vxlan->dst_port;
847 }
848
849 if (tb[NDA_VNI]) {
850 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
851 return -EINVAL;
852 *vni = nla_get_u32(tb[NDA_VNI]);
853 } else {
854 *vni = vxlan->default_dst.remote_vni;
855 }
856
857 if (tb[NDA_IFINDEX]) {
858 struct net_device *tdev;
859
860 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
861 return -EINVAL;
862 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800863 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300864 if (!tdev)
865 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300866 } else {
867 *ifindex = 0;
868 }
869
870 return 0;
871}
872
stephen hemmingerd3428942012-10-01 12:32:35 +0000873/* Add static entry (via netlink) */
874static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
875 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100876 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000877{
878 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300879 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800880 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000881 __be16 port;
882 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000883 int err;
884
885 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
886 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
887 ndm->ndm_state);
888 return -EINVAL;
889 }
890
891 if (tb[NDA_DST] == NULL)
892 return -EINVAL;
893
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300894 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
895 if (err)
896 return err;
David Stevens66817122013-03-15 04:35:51 +0000897
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300898 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
899 return -EAFNOSUPPORT;
900
stephen hemmingerd3428942012-10-01 12:32:35 +0000901 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800902 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000903 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000904 spin_unlock_bh(&vxlan->hash_lock);
905
906 return err;
907}
908
909/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000910static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
911 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100912 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000913{
914 struct vxlan_dev *vxlan = netdev_priv(dev);
915 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300916 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800917 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300918 __be16 port;
919 u32 vni, ifindex;
920 int err;
921
922 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
923 if (err)
924 return err;
925
926 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000927
928 spin_lock_bh(&vxlan->hash_lock);
929 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300930 if (!f)
931 goto out;
932
Cong Wange4c7ed42013-08-31 13:44:33 +0800933 if (!vxlan_addr_any(&ip)) {
934 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300935 if (!rd)
936 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000937 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300938
939 err = 0;
940
941 /* remove a destination if it's not the only one on the list,
942 * otherwise destroy the fdb entry
943 */
944 if (rd && !list_is_singular(&f->remotes)) {
945 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200946 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800947 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300948 goto out;
949 }
950
951 vxlan_fdb_destroy(vxlan, f);
952
953out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000954 spin_unlock_bh(&vxlan->hash_lock);
955
956 return err;
957}
958
959/* Dump forwarding table */
960static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400961 struct net_device *dev,
962 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000963{
964 struct vxlan_dev *vxlan = netdev_priv(dev);
965 unsigned int h;
966
967 for (h = 0; h < FDB_HASH_SIZE; ++h) {
968 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000969 int err;
970
Sasha Levinb67bfe02013-02-27 17:06:00 -0800971 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000972 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000973
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700974 if (idx < cb->args[0])
975 goto skip;
976
977 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000978 err = vxlan_fdb_info(skb, vxlan, f,
979 NETLINK_CB(cb->skb).portid,
980 cb->nlh->nlmsg_seq,
981 RTM_NEWNEIGH,
982 NLM_F_MULTI, rd);
983 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700984 goto out;
David Stevens66817122013-03-15 04:35:51 +0000985 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700986skip:
987 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000988 }
989 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700990out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000991 return idx;
992}
993
994/* Watch incoming packets to learn mapping between Ethernet address
995 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700996 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000997 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700998static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800999 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +00001000{
1001 struct vxlan_dev *vxlan = netdev_priv(dev);
1002 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +00001003
1004 f = vxlan_find_mac(vxlan, src_mac);
1005 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -07001006 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001007
Cong Wange4c7ed42013-08-31 13:44:33 +08001008 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001009 return false;
1010
1011 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -07001012 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -07001013 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001014
1015 if (net_ratelimit())
1016 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001017 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001018 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +00001019
Cong Wange4c7ed42013-08-31 13:44:33 +08001020 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001021 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001022 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001023 } else {
1024 /* learned new entry */
1025 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001026
1027 /* close off race between vxlan_flush and incoming packets */
1028 if (netif_running(dev))
1029 vxlan_fdb_create(vxlan, src_mac, src_ip,
1030 NUD_REACHABLE,
1031 NLM_F_EXCL|NLM_F_CREATE,
1032 vxlan->dst_port,
1033 vxlan->default_dst.remote_vni,
1034 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001035 spin_unlock(&vxlan->hash_lock);
1036 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001037
1038 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001039}
1040
stephen hemmingerd3428942012-10-01 12:32:35 +00001041/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001042static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001043{
stephen hemminger553675f2013-05-16 11:35:20 +00001044 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001045
Gao feng95ab0992013-12-10 16:37:33 +08001046 /* The vxlan_sock is only used by dev, leaving group has
1047 * no effect on other vxlan devices.
1048 */
1049 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1050 return false;
1051
stephen hemminger553675f2013-05-16 11:35:20 +00001052 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001053 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001054 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001055
Gao feng95ab0992013-12-10 16:37:33 +08001056 if (vxlan->vn_sock != dev->vn_sock)
1057 continue;
1058
1059 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1060 &dev->default_dst.remote_ip))
1061 continue;
1062
1063 if (vxlan->default_dst.remote_ifindex !=
1064 dev->default_dst.remote_ifindex)
1065 continue;
1066
1067 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001068 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001069
1070 return false;
1071}
1072
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001073static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001074{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001075 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001076}
1077
Pravin B Shelar012a5722013-08-19 11:23:07 -07001078void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001079{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001080 struct sock *sk = vs->sock->sk;
1081 struct net *net = sock_net(sk);
1082 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001083
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001084 if (!atomic_dec_and_test(&vs->refcnt))
1085 return;
1086
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001087 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001088 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001089 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001090 spin_unlock(&vn->sock_lock);
1091
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001092 queue_work(vxlan_wq, &vs->del_work);
1093}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001094EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001095
stephen hemminger3fc2de22013-07-18 08:40:15 -07001096/* Callback to update multicast group membership when first VNI on
1097 * multicast asddress is brought up
1098 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001099 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001100static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001101{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001102 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001103 struct vxlan_sock *vs = vxlan->vn_sock;
1104 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001105 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1106 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001107
stephen hemmingerd3428942012-10-01 12:32:35 +00001108 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001109 if (ip->sa.sa_family == AF_INET) {
1110 struct ip_mreqn mreq = {
1111 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1112 .imr_ifindex = ifindex,
1113 };
1114
1115 ip_mc_join_group(sk, &mreq);
1116#if IS_ENABLED(CONFIG_IPV6)
1117 } else {
1118 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1119 &ip->sin6.sin6_addr);
1120#endif
1121 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001122 release_sock(sk);
1123
Pravin B Shelar012a5722013-08-19 11:23:07 -07001124 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001125 dev_put(vxlan->dev);
1126}
1127
1128/* Inverse of vxlan_igmp_join when last VNI is brought down */
1129static void vxlan_igmp_leave(struct work_struct *work)
1130{
1131 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001132 struct vxlan_sock *vs = vxlan->vn_sock;
1133 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001134 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1135 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001136
1137 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001138 if (ip->sa.sa_family == AF_INET) {
1139 struct ip_mreqn mreq = {
1140 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1141 .imr_ifindex = ifindex,
1142 };
1143
1144 ip_mc_leave_group(sk, &mreq);
1145#if IS_ENABLED(CONFIG_IPV6)
1146 } else {
1147 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1148 &ip->sin6.sin6_addr);
1149#endif
1150 }
1151
stephen hemmingerd3428942012-10-01 12:32:35 +00001152 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001153
Pravin B Shelar012a5722013-08-19 11:23:07 -07001154 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001155 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001156}
1157
Tom Herbertdfd86452015-01-12 17:00:38 -08001158static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1159 size_t hdrlen, u32 data)
1160{
1161 size_t start, offset, plen;
1162 __wsum delta;
1163
1164 if (skb->remcsum_offload) {
1165 /* Already processed in GRO path */
1166 skb->remcsum_offload = 0;
1167 return vh;
1168 }
1169
1170 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1171 offset = start + ((data & VXLAN_RCO_UDP) ?
1172 offsetof(struct udphdr, check) :
1173 offsetof(struct tcphdr, check));
1174
1175 plen = hdrlen + offset + sizeof(u16);
1176
1177 if (!pskb_may_pull(skb, plen))
1178 return NULL;
1179
1180 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1181
1182 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE))
1183 __skb_checksum_complete(skb);
1184
1185 delta = remcsum_adjust((void *)vh + hdrlen,
1186 skb->csum, start, offset);
1187
1188 /* Adjust skb->csum since we changed the packet */
1189 skb->csum = csum_add(skb->csum, delta);
1190
1191 return vh;
1192}
1193
stephen hemmingerd3428942012-10-01 12:32:35 +00001194/* Callback from net/ipv4/udp.c to receive packets */
1195static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1196{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001197 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001198 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001199 u32 flags, vni;
Thomas Graf35114942015-01-15 03:53:55 +01001200 struct vxlan_metadata md = {0};
stephen hemmingerd3428942012-10-01 12:32:35 +00001201
stephen hemmingerd3428942012-10-01 12:32:35 +00001202 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001203 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001204 goto error;
1205
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001206 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001207 flags = ntohl(vxh->vx_flags);
1208 vni = ntohl(vxh->vx_vni);
1209
1210 if (flags & VXLAN_HF_VNI) {
1211 flags &= ~VXLAN_HF_VNI;
1212 } else {
1213 /* VNI flag always required to be set */
1214 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001215 }
1216
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001217 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001218 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001219 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001220
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001221 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001222 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001223 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001224
Tom Herbertdfd86452015-01-12 17:00:38 -08001225 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1226 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1227 if (!vxh)
1228 goto drop;
1229
1230 flags &= ~VXLAN_HF_RCO;
1231 vni &= VXLAN_VID_MASK;
1232 }
1233
Thomas Graf35114942015-01-15 03:53:55 +01001234 /* For backwards compatibility, only allow reserved fields to be
1235 * used by VXLAN extensions if explicitly requested.
1236 */
1237 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1238 struct vxlanhdr_gbp *gbp;
1239
1240 gbp = (struct vxlanhdr_gbp *)vxh;
1241 md.gbp = ntohs(gbp->policy_id);
1242
1243 if (gbp->dont_learn)
1244 md.gbp |= VXLAN_GBP_DONT_LEARN;
1245
1246 if (gbp->policy_applied)
1247 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1248
1249 flags &= ~VXLAN_GBP_USED_BITS;
1250 }
1251
Tom Herbertdfd86452015-01-12 17:00:38 -08001252 if (flags || (vni & ~VXLAN_VID_MASK)) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001253 /* If there are any unprocessed flags remaining treat
1254 * this as a malformed packet. This behavior diverges from
1255 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1256 * in reserved fields are to be ignored. The approach here
1257 * maintains compatbility with previous stack code, and also
1258 * is more robust and provides a little more security in
1259 * adding extensions to VXLAN.
1260 */
1261
1262 goto bad_flags;
1263 }
1264
Thomas Graf35114942015-01-15 03:53:55 +01001265 md.vni = vxh->vx_vni;
1266 vs->rcv(vs, skb, &md);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001267 return 0;
1268
1269drop:
1270 /* Consume bad packet */
1271 kfree_skb(skb);
1272 return 0;
1273
Tom Herbert3bf39472015-01-08 12:31:18 -08001274bad_flags:
1275 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1276 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1277
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001278error:
1279 /* Return non vxlan pkt */
1280 return 1;
1281}
1282
Thomas Graf35114942015-01-15 03:53:55 +01001283static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1284 struct vxlan_metadata *md)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001285{
Cong Wange4c7ed42013-08-31 13:44:33 +08001286 struct iphdr *oip = NULL;
1287 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001288 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001289 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001290 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001291 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001292 int err = 0;
1293 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001294
Thomas Graf35114942015-01-15 03:53:55 +01001295 vni = ntohl(md->vni) >> 8;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001296 /* Is this VNI defined? */
1297 vxlan = vxlan_vs_find_vni(vs, vni);
1298 if (!vxlan)
1299 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001300
Cong Wange4c7ed42013-08-31 13:44:33 +08001301 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001302 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001303 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001304 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001305 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001306
1307 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001308 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001309 goto drop;
1310
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001311 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001312 if (remote_ip->sa.sa_family == AF_INET) {
1313 oip = ip_hdr(skb);
1314 saddr.sin.sin_addr.s_addr = oip->saddr;
1315 saddr.sa.sa_family = AF_INET;
1316#if IS_ENABLED(CONFIG_IPV6)
1317 } else {
1318 oip6 = ipv6_hdr(skb);
1319 saddr.sin6.sin6_addr = oip6->saddr;
1320 saddr.sa.sa_family = AF_INET6;
1321#endif
1322 }
1323
stephen hemminger26a41ae62013-06-17 12:09:58 -07001324 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001325 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001326 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001327
stephen hemmingerd3428942012-10-01 12:32:35 +00001328 skb_reset_network_header(skb);
Thomas Graf35114942015-01-15 03:53:55 +01001329 skb->mark = md->gbp;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001330
Cong Wange4c7ed42013-08-31 13:44:33 +08001331 if (oip6)
1332 err = IP6_ECN_decapsulate(oip6, skb);
1333 if (oip)
1334 err = IP_ECN_decapsulate(oip, skb);
1335
stephen hemmingerd3428942012-10-01 12:32:35 +00001336 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001337 if (log_ecn_error) {
1338 if (oip6)
1339 net_info_ratelimited("non-ECT from %pI6\n",
1340 &oip6->saddr);
1341 if (oip)
1342 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1343 &oip->saddr, oip->tos);
1344 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001345 if (err > 1) {
1346 ++vxlan->dev->stats.rx_frame_errors;
1347 ++vxlan->dev->stats.rx_errors;
1348 goto drop;
1349 }
1350 }
1351
Pravin B Shelare8171042013-03-25 14:49:46 +00001352 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001353 u64_stats_update_begin(&stats->syncp);
1354 stats->rx_packets++;
1355 stats->rx_bytes += skb->len;
1356 u64_stats_update_end(&stats->syncp);
1357
1358 netif_rx(skb);
1359
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001360 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001361drop:
1362 /* Consume bad packet */
1363 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001364}
1365
David Stevense4f67ad2012-11-20 02:50:14 +00001366static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1367{
1368 struct vxlan_dev *vxlan = netdev_priv(dev);
1369 struct arphdr *parp;
1370 u8 *arpptr, *sha;
1371 __be32 sip, tip;
1372 struct neighbour *n;
1373
1374 if (dev->flags & IFF_NOARP)
1375 goto out;
1376
1377 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1378 dev->stats.tx_dropped++;
1379 goto out;
1380 }
1381 parp = arp_hdr(skb);
1382
1383 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1384 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1385 parp->ar_pro != htons(ETH_P_IP) ||
1386 parp->ar_op != htons(ARPOP_REQUEST) ||
1387 parp->ar_hln != dev->addr_len ||
1388 parp->ar_pln != 4)
1389 goto out;
1390 arpptr = (u8 *)parp + sizeof(struct arphdr);
1391 sha = arpptr;
1392 arpptr += dev->addr_len; /* sha */
1393 memcpy(&sip, arpptr, sizeof(sip));
1394 arpptr += sizeof(sip);
1395 arpptr += dev->addr_len; /* tha */
1396 memcpy(&tip, arpptr, sizeof(tip));
1397
1398 if (ipv4_is_loopback(tip) ||
1399 ipv4_is_multicast(tip))
1400 goto out;
1401
1402 n = neigh_lookup(&arp_tbl, &tip, dev);
1403
1404 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001405 struct vxlan_fdb *f;
1406 struct sk_buff *reply;
1407
1408 if (!(n->nud_state & NUD_CONNECTED)) {
1409 neigh_release(n);
1410 goto out;
1411 }
1412
1413 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001414 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001415 /* bridge-local neighbor */
1416 neigh_release(n);
1417 goto out;
1418 }
1419
1420 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1421 n->ha, sha);
1422
1423 neigh_release(n);
1424
David Stevens73461352014-03-18 12:32:29 -04001425 if (reply == NULL)
1426 goto out;
1427
David Stevense4f67ad2012-11-20 02:50:14 +00001428 skb_reset_mac_header(reply);
1429 __skb_pull(reply, skb_network_offset(reply));
1430 reply->ip_summed = CHECKSUM_UNNECESSARY;
1431 reply->pkt_type = PACKET_HOST;
1432
1433 if (netif_rx_ni(reply) == NET_RX_DROP)
1434 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001435 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1436 union vxlan_addr ipa = {
1437 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001438 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001439 };
1440
1441 vxlan_ip_miss(dev, &ipa);
1442 }
David Stevense4f67ad2012-11-20 02:50:14 +00001443out:
1444 consume_skb(skb);
1445 return NETDEV_TX_OK;
1446}
1447
Cong Wangf564f452013-08-31 13:44:36 +08001448#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001449static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1450 struct neighbour *n, bool isrouter)
1451{
1452 struct net_device *dev = request->dev;
1453 struct sk_buff *reply;
1454 struct nd_msg *ns, *na;
1455 struct ipv6hdr *pip6;
1456 u8 *daddr;
1457 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1458 int ns_olen;
1459 int i, len;
1460
1461 if (dev == NULL)
1462 return NULL;
1463
1464 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1465 sizeof(*na) + na_olen + dev->needed_tailroom;
1466 reply = alloc_skb(len, GFP_ATOMIC);
1467 if (reply == NULL)
1468 return NULL;
1469
1470 reply->protocol = htons(ETH_P_IPV6);
1471 reply->dev = dev;
1472 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1473 skb_push(reply, sizeof(struct ethhdr));
1474 skb_set_mac_header(reply, 0);
1475
1476 ns = (struct nd_msg *)skb_transport_header(request);
1477
1478 daddr = eth_hdr(request)->h_source;
1479 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1480 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1481 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1482 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1483 break;
1484 }
1485 }
1486
1487 /* Ethernet header */
1488 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1489 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1490 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1491 reply->protocol = htons(ETH_P_IPV6);
1492
1493 skb_pull(reply, sizeof(struct ethhdr));
1494 skb_set_network_header(reply, 0);
1495 skb_put(reply, sizeof(struct ipv6hdr));
1496
1497 /* IPv6 header */
1498
1499 pip6 = ipv6_hdr(reply);
1500 memset(pip6, 0, sizeof(struct ipv6hdr));
1501 pip6->version = 6;
1502 pip6->priority = ipv6_hdr(request)->priority;
1503 pip6->nexthdr = IPPROTO_ICMPV6;
1504 pip6->hop_limit = 255;
1505 pip6->daddr = ipv6_hdr(request)->saddr;
1506 pip6->saddr = *(struct in6_addr *)n->primary_key;
1507
1508 skb_pull(reply, sizeof(struct ipv6hdr));
1509 skb_set_transport_header(reply, 0);
1510
1511 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1512
1513 /* Neighbor Advertisement */
1514 memset(na, 0, sizeof(*na)+na_olen);
1515 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1516 na->icmph.icmp6_router = isrouter;
1517 na->icmph.icmp6_override = 1;
1518 na->icmph.icmp6_solicited = 1;
1519 na->target = ns->target;
1520 ether_addr_copy(&na->opt[2], n->ha);
1521 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1522 na->opt[1] = na_olen >> 3;
1523
1524 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1525 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1526 csum_partial(na, sizeof(*na)+na_olen, 0));
1527
1528 pip6->payload_len = htons(sizeof(*na)+na_olen);
1529
1530 skb_push(reply, sizeof(struct ipv6hdr));
1531
1532 reply->ip_summed = CHECKSUM_UNNECESSARY;
1533
1534 return reply;
1535}
1536
Cong Wangf564f452013-08-31 13:44:36 +08001537static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1538{
1539 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001540 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001541 const struct ipv6hdr *iphdr;
1542 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001543 struct neighbour *n;
1544 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001545
1546 in6_dev = __in6_dev_get(dev);
1547 if (!in6_dev)
1548 goto out;
1549
Cong Wangf564f452013-08-31 13:44:36 +08001550 iphdr = ipv6_hdr(skb);
1551 saddr = &iphdr->saddr;
1552 daddr = &iphdr->daddr;
1553
Cong Wangf564f452013-08-31 13:44:36 +08001554 msg = (struct nd_msg *)skb_transport_header(skb);
1555 if (msg->icmph.icmp6_code != 0 ||
1556 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1557 goto out;
1558
David Stevens4b29dba2014-03-24 10:39:58 -04001559 if (ipv6_addr_loopback(daddr) ||
1560 ipv6_addr_is_multicast(&msg->target))
1561 goto out;
1562
1563 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001564
1565 if (n) {
1566 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001567 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001568
1569 if (!(n->nud_state & NUD_CONNECTED)) {
1570 neigh_release(n);
1571 goto out;
1572 }
1573
1574 f = vxlan_find_mac(vxlan, n->ha);
1575 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1576 /* bridge-local neighbor */
1577 neigh_release(n);
1578 goto out;
1579 }
1580
David Stevens4b29dba2014-03-24 10:39:58 -04001581 reply = vxlan_na_create(skb, n,
1582 !!(f ? f->flags & NTF_ROUTER : 0));
1583
Cong Wangf564f452013-08-31 13:44:36 +08001584 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001585
1586 if (reply == NULL)
1587 goto out;
1588
1589 if (netif_rx_ni(reply) == NET_RX_DROP)
1590 dev->stats.rx_dropped++;
1591
Cong Wangf564f452013-08-31 13:44:36 +08001592 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001593 union vxlan_addr ipa = {
1594 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001595 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001596 };
1597
Cong Wangf564f452013-08-31 13:44:36 +08001598 vxlan_ip_miss(dev, &ipa);
1599 }
1600
1601out:
1602 consume_skb(skb);
1603 return NETDEV_TX_OK;
1604}
1605#endif
1606
David Stevense4f67ad2012-11-20 02:50:14 +00001607static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1608{
1609 struct vxlan_dev *vxlan = netdev_priv(dev);
1610 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001611
1612 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1613 return false;
1614
1615 n = NULL;
1616 switch (ntohs(eth_hdr(skb)->h_proto)) {
1617 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001618 {
1619 struct iphdr *pip;
1620
David Stevense4f67ad2012-11-20 02:50:14 +00001621 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1622 return false;
1623 pip = ip_hdr(skb);
1624 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001625 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1626 union vxlan_addr ipa = {
1627 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001628 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001629 };
1630
1631 vxlan_ip_miss(dev, &ipa);
1632 return false;
1633 }
1634
David Stevense4f67ad2012-11-20 02:50:14 +00001635 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001636 }
1637#if IS_ENABLED(CONFIG_IPV6)
1638 case ETH_P_IPV6:
1639 {
1640 struct ipv6hdr *pip6;
1641
1642 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1643 return false;
1644 pip6 = ipv6_hdr(skb);
1645 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1646 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1647 union vxlan_addr ipa = {
1648 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001649 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001650 };
1651
1652 vxlan_ip_miss(dev, &ipa);
1653 return false;
1654 }
1655
1656 break;
1657 }
1658#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001659 default:
1660 return false;
1661 }
1662
1663 if (n) {
1664 bool diff;
1665
Joe Perches7367d0b2013-09-01 11:51:23 -07001666 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001667 if (diff) {
1668 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1669 dev->addr_len);
1670 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1671 }
1672 neigh_release(n);
1673 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001674 }
1675
David Stevense4f67ad2012-11-20 02:50:14 +00001676 return false;
1677}
1678
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001679static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
Thomas Graf35114942015-01-15 03:53:55 +01001680 struct vxlan_metadata *md)
1681{
1682 struct vxlanhdr_gbp *gbp;
1683
1684 gbp = (struct vxlanhdr_gbp *)vxh;
1685 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1686
1687 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1688 gbp->dont_learn = 1;
1689
1690 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1691 gbp->policy_applied = 1;
1692
1693 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1694}
1695
Cong Wange4c7ed42013-08-31 13:44:33 +08001696#if IS_ENABLED(CONFIG_IPV6)
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001697static int vxlan6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001698 struct net_device *dev, struct in6_addr *saddr,
1699 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Thomas Graf35114942015-01-15 03:53:55 +01001700 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001701 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Cong Wange4c7ed42013-08-31 13:44:33 +08001702{
Cong Wange4c7ed42013-08-31 13:44:33 +08001703 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001704 int min_headroom;
1705 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001706 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbertdfd86452015-01-12 17:00:38 -08001707 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1708 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001709
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001710 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001711 skb->ip_summed == CHECKSUM_PARTIAL) {
1712 int csum_start = skb_checksum_start_offset(skb);
1713
1714 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1715 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1716 (skb->csum_offset == offsetof(struct udphdr, check) ||
1717 skb->csum_offset == offsetof(struct tcphdr, check))) {
1718 udp_sum = false;
1719 type |= SKB_GSO_TUNNEL_REMCSUM;
1720 }
1721 }
1722
1723 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001724 if (IS_ERR(skb)) {
1725 err = -EINVAL;
1726 goto err;
1727 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001728
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001729 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001730
Cong Wange4c7ed42013-08-31 13:44:33 +08001731 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1732 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001733 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001734
1735 /* Need space for new headers (invalidates iph ptr) */
1736 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001737 if (unlikely(err)) {
1738 kfree_skb(skb);
1739 goto err;
1740 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001741
Jiri Pirko59682502014-11-19 14:04:59 +01001742 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001743 if (WARN_ON(!skb)) {
1744 err = -ENOMEM;
1745 goto err;
1746 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001747
1748 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001749 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001750 vxh->vx_vni = md->vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001751
Tom Herbertdfd86452015-01-12 17:00:38 -08001752 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1753 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1754 VXLAN_RCO_SHIFT;
1755
1756 if (skb->csum_offset == offsetof(struct udphdr, check))
1757 data |= VXLAN_RCO_UDP;
1758
1759 vxh->vx_vni |= htonl(data);
1760 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1761
1762 if (!skb_is_gso(skb)) {
1763 skb->ip_summed = CHECKSUM_NONE;
1764 skb->encapsulation = 0;
1765 }
1766 }
1767
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001768 if (vxflags & VXLAN_F_GBP)
1769 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001770
Tom Herbert996c9fd2014-09-29 20:22:33 -07001771 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1772
Tom Herbertd998f8e2015-01-20 11:23:04 -08001773 udp_tunnel6_xmit_skb(dst, skb, dev, saddr, daddr, prio,
1774 ttl, src_port, dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001775 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
Cong Wange4c7ed42013-08-31 13:44:33 +08001776 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001777err:
1778 dst_release(dst);
1779 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001780}
1781#endif
1782
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001783int vxlan_xmit_skb(struct rtable *rt, struct sk_buff *skb,
Pravin B Shelar49560532013-08-19 11:23:17 -07001784 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Thomas Graf35114942015-01-15 03:53:55 +01001785 __be16 src_port, __be16 dst_port,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001786 struct vxlan_metadata *md, bool xnet, u32 vxflags)
Pravin B Shelar49560532013-08-19 11:23:17 -07001787{
1788 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001789 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001790 int err;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001791 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
Tom Herbertdfd86452015-01-12 17:00:38 -08001792 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1793 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001794
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001795 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
Tom Herbertdfd86452015-01-12 17:00:38 -08001796 skb->ip_summed == CHECKSUM_PARTIAL) {
1797 int csum_start = skb_checksum_start_offset(skb);
1798
1799 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1800 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1801 (skb->csum_offset == offsetof(struct udphdr, check) ||
1802 skb->csum_offset == offsetof(struct tcphdr, check))) {
1803 udp_sum = false;
1804 type |= SKB_GSO_TUNNEL_REMCSUM;
1805 }
1806 }
1807
1808 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001809 if (IS_ERR(skb))
Pravin B Shelar74f47272014-12-23 16:20:36 -08001810 return PTR_ERR(skb);
Pravin B Shelar49560532013-08-19 11:23:17 -07001811
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001812 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001813 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001814 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001815
1816 /* Need space for new headers (invalidates iph ptr) */
1817 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001818 if (unlikely(err)) {
1819 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001820 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001821 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001822
Jiri Pirko59682502014-11-19 14:04:59 +01001823 skb = vlan_hwaccel_push_inside(skb);
1824 if (WARN_ON(!skb))
1825 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001826
Pravin B Shelar49560532013-08-19 11:23:17 -07001827 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001828 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Thomas Graf35114942015-01-15 03:53:55 +01001829 vxh->vx_vni = md->vni;
Pravin B Shelar49560532013-08-19 11:23:17 -07001830
Tom Herbertdfd86452015-01-12 17:00:38 -08001831 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1832 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1833 VXLAN_RCO_SHIFT;
1834
1835 if (skb->csum_offset == offsetof(struct udphdr, check))
1836 data |= VXLAN_RCO_UDP;
1837
1838 vxh->vx_vni |= htonl(data);
1839 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1840
1841 if (!skb_is_gso(skb)) {
1842 skb->ip_summed = CHECKSUM_NONE;
1843 skb->encapsulation = 0;
1844 }
1845 }
1846
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001847 if (vxflags & VXLAN_F_GBP)
1848 vxlan_build_gbp_hdr(vxh, vxflags, md);
Thomas Graf35114942015-01-15 03:53:55 +01001849
Tom Herbert996c9fd2014-09-29 20:22:33 -07001850 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1851
Tom Herbertd998f8e2015-01-20 11:23:04 -08001852 return udp_tunnel_xmit_skb(rt, skb, src, dst, tos,
1853 ttl, df, src_port, dst_port, xnet,
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001854 !(vxflags & VXLAN_F_UDP_CSUM));
Pravin B Shelar49560532013-08-19 11:23:17 -07001855}
1856EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1857
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001858/* Bypass encapsulation if the destination is local */
1859static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1860 struct vxlan_dev *dst_vxlan)
1861{
Li RongQing8f849852014-01-04 13:57:59 +08001862 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001863 union vxlan_addr loopback;
1864 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001865 struct net_device *dev = skb->dev;
1866 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001867
Li RongQing8f849852014-01-04 13:57:59 +08001868 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1869 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001870 skb->pkt_type = PACKET_HOST;
1871 skb->encapsulation = 0;
1872 skb->dev = dst_vxlan->dev;
1873 __skb_pull(skb, skb_network_offset(skb));
1874
Cong Wange4c7ed42013-08-31 13:44:33 +08001875 if (remote_ip->sa.sa_family == AF_INET) {
1876 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1877 loopback.sa.sa_family = AF_INET;
1878#if IS_ENABLED(CONFIG_IPV6)
1879 } else {
1880 loopback.sin6.sin6_addr = in6addr_loopback;
1881 loopback.sa.sa_family = AF_INET6;
1882#endif
1883 }
1884
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001885 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001886 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001887
1888 u64_stats_update_begin(&tx_stats->syncp);
1889 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001890 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001891 u64_stats_update_end(&tx_stats->syncp);
1892
1893 if (netif_rx(skb) == NET_RX_SUCCESS) {
1894 u64_stats_update_begin(&rx_stats->syncp);
1895 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001896 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001897 u64_stats_update_end(&rx_stats->syncp);
1898 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001899 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001900 }
1901}
1902
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001903static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1904 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001905{
1906 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001907 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001908 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001909 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001910 union vxlan_addr *dst;
Thomas Graf35114942015-01-15 03:53:55 +01001911 struct vxlan_metadata md;
Cong Wange4c7ed42013-08-31 13:44:33 +08001912 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001913 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001914 __be16 df = 0;
1915 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001916 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001917
stephen hemminger823aa872013-04-27 11:31:57 +00001918 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001919 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001920 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001921
Cong Wange4c7ed42013-08-31 13:44:33 +08001922 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001923 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001924 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001925 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001926 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001927 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001928 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001929 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001930
stephen hemmingerd3428942012-10-01 12:32:35 +00001931 old_iph = ip_hdr(skb);
1932
stephen hemmingerd3428942012-10-01 12:32:35 +00001933 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001934 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001935 ttl = 1;
1936
1937 tos = vxlan->tos;
1938 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001939 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001940
Tom Herbert535fb8d2014-07-01 21:32:49 -07001941 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1942 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001943
Cong Wange4c7ed42013-08-31 13:44:33 +08001944 if (dst->sa.sa_family == AF_INET) {
1945 memset(&fl4, 0, sizeof(fl4));
1946 fl4.flowi4_oif = rdst->remote_ifindex;
1947 fl4.flowi4_tos = RT_TOS(tos);
1948 fl4.daddr = dst->sin.sin_addr.s_addr;
1949 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001950
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001951 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001952 if (IS_ERR(rt)) {
1953 netdev_dbg(dev, "no route to %pI4\n",
1954 &dst->sin.sin_addr.s_addr);
1955 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001956 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001957 }
1958
1959 if (rt->dst.dev == dev) {
1960 netdev_dbg(dev, "circular route to %pI4\n",
1961 &dst->sin.sin_addr.s_addr);
1962 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001963 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001964 }
1965
1966 /* Bypass encapsulation if the destination is local */
1967 if (rt->rt_flags & RTCF_LOCAL &&
1968 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1969 struct vxlan_dev *dst_vxlan;
1970
1971 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001972 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01001973 dst->sa.sa_family, dst_port,
1974 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08001975 if (!dst_vxlan)
1976 goto tx_error;
1977 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1978 return;
1979 }
1980
1981 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1982 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
Thomas Graf35114942015-01-15 03:53:55 +01001983 md.vni = htonl(vni << 8);
1984 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08001985
Tom Herbertaf33c1a2015-01-20 11:23:05 -08001986 err = vxlan_xmit_skb(rt, skb, fl4.saddr,
1987 dst->sin.sin_addr.s_addr, tos, ttl, df,
1988 src_port, dst_port, &md,
1989 !net_eq(vxlan->net, dev_net(vxlan->dev)),
1990 vxlan->flags);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001991 if (err < 0) {
1992 /* skb is already freed. */
1993 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001994 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001995 }
1996
Cong Wange4c7ed42013-08-31 13:44:33 +08001997 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1998#if IS_ENABLED(CONFIG_IPV6)
1999 } else {
2000 struct sock *sk = vxlan->vn_sock->sock->sk;
2001 struct dst_entry *ndst;
2002 struct flowi6 fl6;
2003 u32 flags;
2004
2005 memset(&fl6, 0, sizeof(fl6));
2006 fl6.flowi6_oif = rdst->remote_ifindex;
2007 fl6.daddr = dst->sin6.sin6_addr;
2008 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08002009 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08002010
2011 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
2012 netdev_dbg(dev, "no route to %pI6\n",
2013 &dst->sin6.sin6_addr);
2014 dev->stats.tx_carrier_errors++;
2015 goto tx_error;
2016 }
2017
2018 if (ndst->dev == dev) {
2019 netdev_dbg(dev, "circular route to %pI6\n",
2020 &dst->sin6.sin6_addr);
2021 dst_release(ndst);
2022 dev->stats.collisions++;
2023 goto tx_error;
2024 }
2025
2026 /* Bypass encapsulation if the destination is local */
2027 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2028 if (flags & RTF_LOCAL &&
2029 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2030 struct vxlan_dev *dst_vxlan;
2031
2032 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002033 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
Thomas Grafac5132d2015-01-15 03:53:56 +01002034 dst->sa.sa_family, dst_port,
2035 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002036 if (!dst_vxlan)
2037 goto tx_error;
2038 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2039 return;
2040 }
2041
2042 ttl = ttl ? : ip6_dst_hoplimit(ndst);
Thomas Graf35114942015-01-15 03:53:55 +01002043 md.vni = htonl(vni << 8);
2044 md.gbp = skb->mark;
Cong Wange4c7ed42013-08-31 13:44:33 +08002045
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002046 err = vxlan6_xmit_skb(ndst, skb, dev, &fl6.saddr, &fl6.daddr,
2047 0, ttl, src_port, dst_port, &md,
2048 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2049 vxlan->flags);
Cong Wange4c7ed42013-08-31 13:44:33 +08002050#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00002051 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002052
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002053 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00002054
2055drop:
2056 dev->stats.tx_dropped++;
2057 goto tx_free;
2058
Pravin B Shelar49560532013-08-19 11:23:17 -07002059rt_tx_error:
2060 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00002061tx_error:
2062 dev->stats.tx_errors++;
2063tx_free:
2064 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00002065}
2066
David Stevens66817122013-03-15 04:35:51 +00002067/* Transmit local packets over Vxlan
2068 *
2069 * Outer IP header inherits ECN and DF from inner header.
2070 * Outer UDP destination is the VXLAN assigned port.
2071 * source port is based on hash of flow
2072 */
2073static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2074{
2075 struct vxlan_dev *vxlan = netdev_priv(dev);
2076 struct ethhdr *eth;
2077 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002078 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002079 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002080
2081 skb_reset_mac_header(skb);
2082 eth = eth_hdr(skb);
2083
Cong Wangf564f452013-08-31 13:44:36 +08002084 if ((vxlan->flags & VXLAN_F_PROXY)) {
2085 if (ntohs(eth->h_proto) == ETH_P_ARP)
2086 return arp_reduce(dev, skb);
2087#if IS_ENABLED(CONFIG_IPV6)
2088 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002089 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2090 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002091 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2092 struct nd_msg *msg;
2093
2094 msg = (struct nd_msg *)skb_transport_header(skb);
2095 if (msg->icmph.icmp6_code == 0 &&
2096 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2097 return neigh_reduce(dev, skb);
2098 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002099 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002100#endif
2101 }
David Stevens66817122013-03-15 04:35:51 +00002102
2103 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002104 did_rsc = false;
2105
2106 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002107 (ntohs(eth->h_proto) == ETH_P_IP ||
2108 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002109 did_rsc = route_shortcircuit(dev, skb);
2110 if (did_rsc)
2111 f = vxlan_find_mac(vxlan, eth->h_dest);
2112 }
2113
David Stevens66817122013-03-15 04:35:51 +00002114 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002115 f = vxlan_find_mac(vxlan, all_zeros_mac);
2116 if (f == NULL) {
2117 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2118 !is_multicast_ether_addr(eth->h_dest))
2119 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002120
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002121 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002122 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002123 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002124 }
David Stevens66817122013-03-15 04:35:51 +00002125 }
2126
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002127 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2128 struct sk_buff *skb1;
2129
Eric Dumazet8f646c92014-01-06 09:54:31 -08002130 if (!fdst) {
2131 fdst = rdst;
2132 continue;
2133 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002134 skb1 = skb_clone(skb, GFP_ATOMIC);
2135 if (skb1)
2136 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2137 }
2138
Eric Dumazet8f646c92014-01-06 09:54:31 -08002139 if (fdst)
2140 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2141 else
2142 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002143 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002144}
2145
stephen hemmingerd3428942012-10-01 12:32:35 +00002146/* Walk the forwarding table and purge stale entries */
2147static void vxlan_cleanup(unsigned long arg)
2148{
2149 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2150 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2151 unsigned int h;
2152
2153 if (!netif_running(vxlan->dev))
2154 return;
2155
2156 spin_lock_bh(&vxlan->hash_lock);
2157 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2158 struct hlist_node *p, *n;
2159 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2160 struct vxlan_fdb *f
2161 = container_of(p, struct vxlan_fdb, hlist);
2162 unsigned long timeout;
2163
stephen hemminger3c172862012-10-26 06:24:34 +00002164 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002165 continue;
2166
2167 timeout = f->used + vxlan->age_interval * HZ;
2168 if (time_before_eq(timeout, jiffies)) {
2169 netdev_dbg(vxlan->dev,
2170 "garbage collect %pM\n",
2171 f->eth_addr);
2172 f->state = NUD_STALE;
2173 vxlan_fdb_destroy(vxlan, f);
2174 } else if (time_before(timeout, next_timer))
2175 next_timer = timeout;
2176 }
2177 }
2178 spin_unlock_bh(&vxlan->hash_lock);
2179
2180 mod_timer(&vxlan->age_timer, next_timer);
2181}
2182
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002183static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2184{
2185 __u32 vni = vxlan->default_dst.remote_vni;
2186
2187 vxlan->vn_sock = vs;
2188 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2189}
2190
stephen hemmingerd3428942012-10-01 12:32:35 +00002191/* Setup stats when device is created */
2192static int vxlan_init(struct net_device *dev)
2193{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002194 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002195 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002196 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002197 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002198
WANG Cong1c213bd2014-02-13 11:46:28 -08002199 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002200 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002201 return -ENOMEM;
2202
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002203 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002204 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002205 vxlan->dst_port, vxlan->flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002206 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002207 /* If we have a socket with same port already, reuse it */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002208 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002209 } else {
2210 /* otherwise make new socket outside of RTNL */
2211 dev_hold(dev);
2212 queue_work(vxlan_wq, &vxlan->sock_work);
2213 }
2214 spin_unlock(&vn->sock_lock);
2215
stephen hemmingerd3428942012-10-01 12:32:35 +00002216 return 0;
2217}
2218
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002219static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002220{
2221 struct vxlan_fdb *f;
2222
2223 spin_lock_bh(&vxlan->hash_lock);
2224 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2225 if (f)
2226 vxlan_fdb_destroy(vxlan, f);
2227 spin_unlock_bh(&vxlan->hash_lock);
2228}
2229
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002230static void vxlan_uninit(struct net_device *dev)
2231{
2232 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002233 struct vxlan_sock *vs = vxlan->vn_sock;
2234
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002235 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002236
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002237 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002238 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002239 free_percpu(dev->tstats);
2240}
2241
stephen hemmingerd3428942012-10-01 12:32:35 +00002242/* Start ageing timer and join group when device is brought up */
2243static int vxlan_open(struct net_device *dev)
2244{
2245 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002246 struct vxlan_sock *vs = vxlan->vn_sock;
2247
2248 /* socket hasn't been created */
2249 if (!vs)
2250 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002251
Gao feng79d4a942013-12-10 16:37:32 +08002252 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002253 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002254 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002255 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002256 }
2257
2258 if (vxlan->age_interval)
2259 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2260
2261 return 0;
2262}
2263
2264/* Purge the forwarding table */
2265static void vxlan_flush(struct vxlan_dev *vxlan)
2266{
Cong Wang31fec5a2013-05-27 22:35:52 +00002267 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002268
2269 spin_lock_bh(&vxlan->hash_lock);
2270 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2271 struct hlist_node *p, *n;
2272 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2273 struct vxlan_fdb *f
2274 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002275 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2276 if (!is_zero_ether_addr(f->eth_addr))
2277 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002278 }
2279 }
2280 spin_unlock_bh(&vxlan->hash_lock);
2281}
2282
2283/* Cleanup timer and forwarding table on shutdown */
2284static int vxlan_stop(struct net_device *dev)
2285{
2286 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002287 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002288 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002289
Cong Wange4c7ed42013-08-31 13:44:33 +08002290 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002291 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002292 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002293 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002294 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002295 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002296
2297 del_timer_sync(&vxlan->age_timer);
2298
2299 vxlan_flush(vxlan);
2300
2301 return 0;
2302}
2303
stephen hemmingerd3428942012-10-01 12:32:35 +00002304/* Stub, nothing needs to be done. */
2305static void vxlan_set_multicast_list(struct net_device *dev)
2306{
2307}
2308
Daniel Borkmann345010b2013-12-18 00:21:08 +01002309static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2310{
2311 struct vxlan_dev *vxlan = netdev_priv(dev);
2312 struct vxlan_rdst *dst = &vxlan->default_dst;
2313 struct net_device *lowerdev;
2314 int max_mtu;
2315
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002316 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002317 if (lowerdev == NULL)
2318 return eth_change_mtu(dev, new_mtu);
2319
2320 if (dst->remote_ip.sa.sa_family == AF_INET6)
2321 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2322 else
2323 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2324
2325 if (new_mtu < 68 || new_mtu > max_mtu)
2326 return -EINVAL;
2327
2328 dev->mtu = new_mtu;
2329 return 0;
2330}
2331
stephen hemmingerd3428942012-10-01 12:32:35 +00002332static const struct net_device_ops vxlan_netdev_ops = {
2333 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002334 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002335 .ndo_open = vxlan_open,
2336 .ndo_stop = vxlan_stop,
2337 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002338 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002339 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002340 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002341 .ndo_validate_addr = eth_validate_addr,
2342 .ndo_set_mac_address = eth_mac_addr,
2343 .ndo_fdb_add = vxlan_fdb_add,
2344 .ndo_fdb_del = vxlan_fdb_delete,
2345 .ndo_fdb_dump = vxlan_fdb_dump,
2346};
2347
2348/* Info for udev, that this is a virtual tunnel endpoint */
2349static struct device_type vxlan_type = {
2350 .name = "vxlan",
2351};
2352
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002353/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002354 * supply the listening VXLAN udp ports. Callers are expected
2355 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002356 */
2357void vxlan_get_rx_port(struct net_device *dev)
2358{
2359 struct vxlan_sock *vs;
2360 struct net *net = dev_net(dev);
2361 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2362 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002363 __be16 port;
2364 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002365
2366 spin_lock(&vn->sock_lock);
2367 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002368 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2369 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002370 sa_family = vs->sock->sk->sk_family;
2371 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2372 port);
2373 }
2374 }
2375 spin_unlock(&vn->sock_lock);
2376}
2377EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2378
stephen hemmingerd3428942012-10-01 12:32:35 +00002379/* Initialize the device structure. */
2380static void vxlan_setup(struct net_device *dev)
2381{
2382 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002383 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002384
2385 eth_hw_addr_random(dev);
2386 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002387 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002388 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002389 else
Cong Wang2853af62014-06-12 11:53:10 -07002390 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002391
2392 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002393 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002394 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2395
2396 dev->tx_queue_len = 0;
2397 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002398 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002399 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002400 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002401
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002402 dev->vlan_features = dev->features;
2403 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002404 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002405 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002406 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002407 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002408 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002409
stephen hemminger553675f2013-05-16 11:35:20 +00002410 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002411 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002412 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2413 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002414 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002415
2416 init_timer_deferrable(&vxlan->age_timer);
2417 vxlan->age_timer.function = vxlan_cleanup;
2418 vxlan->age_timer.data = (unsigned long) vxlan;
2419
stephen hemminger823aa872013-04-27 11:31:57 +00002420 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002421
stephen hemmingerd3428942012-10-01 12:32:35 +00002422 vxlan->dev = dev;
2423
2424 for (h = 0; h < FDB_HASH_SIZE; ++h)
2425 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2426}
2427
2428static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2429 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002430 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002431 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002432 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2433 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002434 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002435 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2436 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2437 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2438 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2439 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002440 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002441 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2442 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2443 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2444 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002445 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002446 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2447 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2448 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002449 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2450 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
Thomas Graf35114942015-01-15 03:53:55 +01002451 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
stephen hemmingerd3428942012-10-01 12:32:35 +00002452};
2453
2454static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2455{
2456 if (tb[IFLA_ADDRESS]) {
2457 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2458 pr_debug("invalid link address (not ethernet)\n");
2459 return -EINVAL;
2460 }
2461
2462 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2463 pr_debug("invalid all zero ethernet address\n");
2464 return -EADDRNOTAVAIL;
2465 }
2466 }
2467
2468 if (!data)
2469 return -EINVAL;
2470
2471 if (data[IFLA_VXLAN_ID]) {
2472 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2473 if (id >= VXLAN_VID_MASK)
2474 return -ERANGE;
2475 }
2476
stephen hemminger05f47d62012-10-09 20:35:50 +00002477 if (data[IFLA_VXLAN_PORT_RANGE]) {
2478 const struct ifla_vxlan_port_range *p
2479 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2480
2481 if (ntohs(p->high) < ntohs(p->low)) {
2482 pr_debug("port range %u .. %u not valid\n",
2483 ntohs(p->low), ntohs(p->high));
2484 return -EINVAL;
2485 }
2486 }
2487
stephen hemmingerd3428942012-10-01 12:32:35 +00002488 return 0;
2489}
2490
Yan Burman1b13c972013-01-29 23:43:07 +00002491static void vxlan_get_drvinfo(struct net_device *netdev,
2492 struct ethtool_drvinfo *drvinfo)
2493{
2494 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2495 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2496}
2497
2498static const struct ethtool_ops vxlan_ethtool_ops = {
2499 .get_drvinfo = vxlan_get_drvinfo,
2500 .get_link = ethtool_op_get_link,
2501};
2502
stephen hemminger553675f2013-05-16 11:35:20 +00002503static void vxlan_del_work(struct work_struct *work)
2504{
2505 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002506 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002507 kfree_rcu(vs, rcu);
2508}
2509
Tom Herbert3ee64f32014-07-13 19:49:42 -07002510static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2511 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002512{
Cong Wange4c7ed42013-08-31 13:44:33 +08002513 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002514 struct udp_port_cfg udp_conf;
2515 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002516
Tom Herbert3ee64f32014-07-13 19:49:42 -07002517 memset(&udp_conf, 0, sizeof(udp_conf));
2518
2519 if (ipv6) {
2520 udp_conf.family = AF_INET6;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002521 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002522 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002523 } else {
2524 udp_conf.family = AF_INET;
2525 udp_conf.local_ip.s_addr = INADDR_ANY;
Cong Wange4c7ed42013-08-31 13:44:33 +08002526 }
2527
Tom Herbert3ee64f32014-07-13 19:49:42 -07002528 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002529
Tom Herbert3ee64f32014-07-13 19:49:42 -07002530 /* Open UDP socket */
2531 err = udp_sock_create(net, &udp_conf, &sock);
2532 if (err < 0)
2533 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002534
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002535 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002536}
2537
2538/* Create new listen socket if needed */
2539static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002540 vxlan_rcv_t *rcv, void *data,
2541 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002542{
2543 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2544 struct vxlan_sock *vs;
2545 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002546 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002547 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002548 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002549
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002550 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002551 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002552 return ERR_PTR(-ENOMEM);
2553
2554 for (h = 0; h < VNI_HASH_SIZE; ++h)
2555 INIT_HLIST_HEAD(&vs->vni_list[h]);
2556
2557 INIT_WORK(&vs->del_work, vxlan_del_work);
2558
Tom Herbert3ee64f32014-07-13 19:49:42 -07002559 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002560 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002561 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002562 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002563 }
2564
Cong Wange4c7ed42013-08-31 13:44:33 +08002565 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002566 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002567 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002568 vs->data = data;
Tom Herbertaf33c1a2015-01-20 11:23:05 -08002569 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
stephen hemminger553675f2013-05-16 11:35:20 +00002570
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002571 /* Initialize the vxlan udp offloads structure */
2572 vs->udp_offloads.port = port;
2573 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2574 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2575
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002576 spin_lock(&vn->sock_lock);
2577 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002578 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002579 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002580
2581 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002582 tunnel_cfg.sk_user_data = vs;
2583 tunnel_cfg.encap_type = 1;
2584 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2585 tunnel_cfg.encap_destroy = NULL;
2586
2587 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002588
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002589 return vs;
2590}
stephen hemminger553675f2013-05-16 11:35:20 +00002591
Pravin B Shelar012a5722013-08-19 11:23:07 -07002592struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2593 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002594 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002595{
2596 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2597 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002598 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002599
Tom Herbert359a0ea2014-06-04 17:20:29 -07002600 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002601 if (!IS_ERR(vs))
2602 return vs;
2603
Pravin B Shelar012a5722013-08-19 11:23:07 -07002604 if (no_share) /* Return error if sharing is not allowed. */
2605 return vs;
2606
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002607 spin_lock(&vn->sock_lock);
Thomas Grafac5132d2015-01-15 03:53:56 +01002608 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002609 if (vs && ((vs->rcv != rcv) ||
2610 !atomic_add_unless(&vs->refcnt, 1, 0)))
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002611 vs = ERR_PTR(-EBUSY);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002612 spin_unlock(&vn->sock_lock);
2613
2614 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002615 vs = ERR_PTR(-EINVAL);
2616
stephen hemminger553675f2013-05-16 11:35:20 +00002617 return vs;
2618}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002619EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002620
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002621/* Scheduled at device creation to bind to a socket */
2622static void vxlan_sock_work(struct work_struct *work)
2623{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002624 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002625 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002626 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002627 __be16 port = vxlan->dst_port;
2628 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002629
Tom Herbert359a0ea2014-06-04 17:20:29 -07002630 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002631 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002632 if (!IS_ERR(nvs))
2633 vxlan_vs_add_dev(nvs, vxlan);
2634 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002635
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002636 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002637}
2638
stephen hemmingerd3428942012-10-01 12:32:35 +00002639static int vxlan_newlink(struct net *net, struct net_device *dev,
2640 struct nlattr *tb[], struct nlattr *data[])
2641{
stephen hemminger553675f2013-05-16 11:35:20 +00002642 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002643 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002644 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002645 __u32 vni;
2646 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002647 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002648
2649 if (!data[IFLA_VXLAN_ID])
2650 return -EINVAL;
2651
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002652 vxlan->net = dev_net(dev);
2653
stephen hemmingerd3428942012-10-01 12:32:35 +00002654 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002655 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002656
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002657 /* Unless IPv6 is explicitly requested, assume IPv4 */
2658 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002659 if (data[IFLA_VXLAN_GROUP]) {
2660 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002661 } else if (data[IFLA_VXLAN_GROUP6]) {
2662 if (!IS_ENABLED(CONFIG_IPV6))
2663 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002664
Cong Wange4c7ed42013-08-31 13:44:33 +08002665 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2666 sizeof(struct in6_addr));
2667 dst->remote_ip.sa.sa_family = AF_INET6;
2668 use_ipv6 = true;
2669 }
2670
2671 if (data[IFLA_VXLAN_LOCAL]) {
2672 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2673 vxlan->saddr.sa.sa_family = AF_INET;
2674 } else if (data[IFLA_VXLAN_LOCAL6]) {
2675 if (!IS_ENABLED(CONFIG_IPV6))
2676 return -EPFNOSUPPORT;
2677
2678 /* TODO: respect scope id */
2679 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2680 sizeof(struct in6_addr));
2681 vxlan->saddr.sa.sa_family = AF_INET6;
2682 use_ipv6 = true;
2683 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002684
stephen hemminger34e02aa2012-10-09 20:35:53 +00002685 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002686 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002687 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002688 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002689
stephen hemminger34e02aa2012-10-09 20:35:53 +00002690 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002691 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002692 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002693 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002694
Cong Wange4c7ed42013-08-31 13:44:33 +08002695#if IS_ENABLED(CONFIG_IPV6)
2696 if (use_ipv6) {
2697 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2698 if (idev && idev->cnf.disable_ipv6) {
2699 pr_info("IPv6 is disabled via sysctl\n");
2700 return -EPERM;
2701 }
2702 vxlan->flags |= VXLAN_F_IPV6;
2703 }
2704#endif
2705
stephen hemminger34e02aa2012-10-09 20:35:53 +00002706 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002707 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002708
Cong Wang2853af62014-06-12 11:53:10 -07002709 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002710 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002711 } else if (use_ipv6)
2712 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002713
2714 if (data[IFLA_VXLAN_TOS])
2715 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2716
Vincent Bernatafb97182012-10-30 10:27:16 +00002717 if (data[IFLA_VXLAN_TTL])
2718 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2719
stephen hemmingerd3428942012-10-01 12:32:35 +00002720 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002721 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002722
2723 if (data[IFLA_VXLAN_AGEING])
2724 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2725 else
2726 vxlan->age_interval = FDB_AGE_DEFAULT;
2727
David Stevense4f67ad2012-11-20 02:50:14 +00002728 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2729 vxlan->flags |= VXLAN_F_PROXY;
2730
2731 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2732 vxlan->flags |= VXLAN_F_RSC;
2733
2734 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2735 vxlan->flags |= VXLAN_F_L2MISS;
2736
2737 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2738 vxlan->flags |= VXLAN_F_L3MISS;
2739
stephen hemmingerd3428942012-10-01 12:32:35 +00002740 if (data[IFLA_VXLAN_LIMIT])
2741 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2742
stephen hemminger05f47d62012-10-09 20:35:50 +00002743 if (data[IFLA_VXLAN_PORT_RANGE]) {
2744 const struct ifla_vxlan_port_range *p
2745 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2746 vxlan->port_min = ntohs(p->low);
2747 vxlan->port_max = ntohs(p->high);
2748 }
2749
stephen hemminger823aa872013-04-27 11:31:57 +00002750 if (data[IFLA_VXLAN_PORT])
2751 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2752
Tom Herbert359a0ea2014-06-04 17:20:29 -07002753 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2754 vxlan->flags |= VXLAN_F_UDP_CSUM;
2755
2756 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2757 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2758 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2759
2760 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2761 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2762 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2763
Tom Herbertdfd86452015-01-12 17:00:38 -08002764 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2765 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2766 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2767
2768 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2769 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2770 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2771
Thomas Graf35114942015-01-15 03:53:55 +01002772 if (data[IFLA_VXLAN_GBP])
2773 vxlan->flags |= VXLAN_F_GBP;
2774
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002775 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
Thomas Grafac5132d2015-01-15 03:53:56 +01002776 vxlan->dst_port, vxlan->flags)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002777 pr_info("duplicate VNI %u\n", vni);
2778 return -EEXIST;
2779 }
2780
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002781 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002782
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002783 /* create an fdb entry for a valid default destination */
2784 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2785 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2786 &vxlan->default_dst.remote_ip,
2787 NUD_REACHABLE|NUD_PERMANENT,
2788 NLM_F_EXCL|NLM_F_CREATE,
2789 vxlan->dst_port,
2790 vxlan->default_dst.remote_vni,
2791 vxlan->default_dst.remote_ifindex,
2792 NTF_SELF);
2793 if (err)
2794 return err;
2795 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002796
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002797 err = register_netdevice(dev);
2798 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002799 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002800 return err;
2801 }
2802
stephen hemminger553675f2013-05-16 11:35:20 +00002803 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002804
2805 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002806}
2807
2808static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2809{
2810 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002811 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002812
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002813 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002814 if (!hlist_unhashed(&vxlan->hlist))
2815 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002816 spin_unlock(&vn->sock_lock);
2817
stephen hemminger553675f2013-05-16 11:35:20 +00002818 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002819 unregister_netdevice_queue(dev, head);
2820}
2821
2822static size_t vxlan_get_size(const struct net_device *dev)
2823{
2824
2825 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002826 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002827 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002828 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002829 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2831 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002832 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2833 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2834 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2835 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002836 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2837 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002838 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002839 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2840 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2841 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2842 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002843 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2844 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002845 0;
2846}
2847
2848static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2849{
2850 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002851 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002852 struct ifla_vxlan_port_range ports = {
2853 .low = htons(vxlan->port_min),
2854 .high = htons(vxlan->port_max),
2855 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002856
Atzm Watanabec7995c42013-04-16 02:50:52 +00002857 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002858 goto nla_put_failure;
2859
Cong Wange4c7ed42013-08-31 13:44:33 +08002860 if (!vxlan_addr_any(&dst->remote_ip)) {
2861 if (dst->remote_ip.sa.sa_family == AF_INET) {
2862 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2863 dst->remote_ip.sin.sin_addr.s_addr))
2864 goto nla_put_failure;
2865#if IS_ENABLED(CONFIG_IPV6)
2866 } else {
2867 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2868 &dst->remote_ip.sin6.sin6_addr))
2869 goto nla_put_failure;
2870#endif
2871 }
2872 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002873
Atzm Watanabec7995c42013-04-16 02:50:52 +00002874 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002875 goto nla_put_failure;
2876
Cong Wange4c7ed42013-08-31 13:44:33 +08002877 if (!vxlan_addr_any(&vxlan->saddr)) {
2878 if (vxlan->saddr.sa.sa_family == AF_INET) {
2879 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2880 vxlan->saddr.sin.sin_addr.s_addr))
2881 goto nla_put_failure;
2882#if IS_ENABLED(CONFIG_IPV6)
2883 } else {
2884 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2885 &vxlan->saddr.sin6.sin6_addr))
2886 goto nla_put_failure;
2887#endif
2888 }
2889 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002890
2891 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2892 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002893 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2894 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2895 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2896 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2897 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2898 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2899 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2900 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2901 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002902 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002903 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002904 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2905 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2906 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2907 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2908 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2909 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002910 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2911 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2912 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2913 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2914 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002915 goto nla_put_failure;
2916
stephen hemminger05f47d62012-10-09 20:35:50 +00002917 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2918 goto nla_put_failure;
2919
Thomas Graf35114942015-01-15 03:53:55 +01002920 if (vxlan->flags & VXLAN_F_GBP &&
2921 nla_put_flag(skb, IFLA_VXLAN_GBP))
2922 goto nla_put_failure;
2923
stephen hemmingerd3428942012-10-01 12:32:35 +00002924 return 0;
2925
2926nla_put_failure:
2927 return -EMSGSIZE;
2928}
2929
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002930static struct net *vxlan_get_link_net(const struct net_device *dev)
2931{
2932 struct vxlan_dev *vxlan = netdev_priv(dev);
2933
2934 return vxlan->net;
2935}
2936
stephen hemmingerd3428942012-10-01 12:32:35 +00002937static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2938 .kind = "vxlan",
2939 .maxtype = IFLA_VXLAN_MAX,
2940 .policy = vxlan_policy,
2941 .priv_size = sizeof(struct vxlan_dev),
2942 .setup = vxlan_setup,
2943 .validate = vxlan_validate,
2944 .newlink = vxlan_newlink,
2945 .dellink = vxlan_dellink,
2946 .get_size = vxlan_get_size,
2947 .fill_info = vxlan_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002948 .get_link_net = vxlan_get_link_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002949};
2950
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002951static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2952 struct net_device *dev)
2953{
2954 struct vxlan_dev *vxlan, *next;
2955 LIST_HEAD(list_kill);
2956
2957 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2958 struct vxlan_rdst *dst = &vxlan->default_dst;
2959
2960 /* In case we created vxlan device with carrier
2961 * and we loose the carrier due to module unload
2962 * we also need to remove vxlan device. In other
2963 * cases, it's not necessary and remote_ifindex
2964 * is 0 here, so no matches.
2965 */
2966 if (dst->remote_ifindex == dev->ifindex)
2967 vxlan_dellink(vxlan->dev, &list_kill);
2968 }
2969
2970 unregister_netdevice_many(&list_kill);
2971}
2972
2973static int vxlan_lowerdev_event(struct notifier_block *unused,
2974 unsigned long event, void *ptr)
2975{
2976 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002977 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002978
Daniel Borkmann783c1462014-01-22 21:07:53 +01002979 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002980 vxlan_handle_lowerdev_unregister(vn, dev);
2981
2982 return NOTIFY_DONE;
2983}
2984
2985static struct notifier_block vxlan_notifier_block __read_mostly = {
2986 .notifier_call = vxlan_lowerdev_event,
2987};
2988
stephen hemmingerd3428942012-10-01 12:32:35 +00002989static __net_init int vxlan_init_net(struct net *net)
2990{
2991 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002992 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002993
stephen hemminger553675f2013-05-16 11:35:20 +00002994 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002995 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002996
stephen hemminger553675f2013-05-16 11:35:20 +00002997 for (h = 0; h < PORT_HASH_SIZE; ++h)
2998 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002999
3000 return 0;
3001}
3002
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003003static void __net_exit vxlan_exit_net(struct net *net)
3004{
3005 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3006 struct vxlan_dev *vxlan, *next;
3007 struct net_device *dev, *aux;
3008 LIST_HEAD(list);
3009
3010 rtnl_lock();
3011 for_each_netdev_safe(net, dev, aux)
3012 if (dev->rtnl_link_ops == &vxlan_link_ops)
3013 unregister_netdevice_queue(dev, &list);
3014
3015 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3016 /* If vxlan->dev is in the same netns, it has already been added
3017 * to the list by the previous loop.
3018 */
3019 if (!net_eq(dev_net(vxlan->dev), net))
3020 unregister_netdevice_queue(dev, &list);
3021 }
3022
3023 unregister_netdevice_many(&list);
3024 rtnl_unlock();
3025}
3026
stephen hemmingerd3428942012-10-01 12:32:35 +00003027static struct pernet_operations vxlan_net_ops = {
3028 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02003029 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00003030 .id = &vxlan_net_id,
3031 .size = sizeof(struct vxlan_net),
3032};
3033
3034static int __init vxlan_init_module(void)
3035{
3036 int rc;
3037
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003038 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3039 if (!vxlan_wq)
3040 return -ENOMEM;
3041
stephen hemmingerd3428942012-10-01 12:32:35 +00003042 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3043
Daniel Borkmann783c1462014-01-22 21:07:53 +01003044 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003045 if (rc)
3046 goto out1;
3047
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003048 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003049 if (rc)
3050 goto out2;
3051
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003052 rc = rtnl_link_register(&vxlan_link_ops);
3053 if (rc)
3054 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00003055
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003056 return 0;
3057out3:
3058 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00003059out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01003060 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00003061out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003062 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00003063 return rc;
3064}
Cong Wang7332a132013-05-27 22:35:53 +00003065late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00003066
3067static void __exit vxlan_cleanup_module(void)
3068{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07003069 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01003070 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07003071 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01003072 unregister_pernet_subsys(&vxlan_net_ops);
3073 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00003074}
3075module_exit(vxlan_cleanup_module);
3076
3077MODULE_LICENSE("GPL");
3078MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003079MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003080MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003081MODULE_ALIAS_RTNL_LINK("vxlan");