blob: 99df0d76157ce10e21c497f5d4f71088e2249521 [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
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200266/* Find VXLAN socket based on network namespace, address family and UDP port */
267static struct vxlan_sock *vxlan_find_sock(struct net *net,
268 sa_family_t family, __be16 port)
stephen hemminger553675f2013-05-16 11:35:20 +0000269{
270 struct vxlan_sock *vs;
271
272 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200273 if (inet_sk(vs->sock->sk)->inet_sport == port &&
274 inet_sk(vs->sock->sk)->sk.sk_family == family)
stephen hemminger553675f2013-05-16 11:35:20 +0000275 return vs;
276 }
277 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000278}
279
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700280static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000281{
282 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000283
stephen hemminger553675f2013-05-16 11:35:20 +0000284 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000285 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000286 return vxlan;
287 }
288
289 return NULL;
290}
291
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700292/* Look up VNI in a per net namespace table */
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200293static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
294 sa_family_t family, __be16 port)
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700295{
296 struct vxlan_sock *vs;
297
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -0200298 vs = vxlan_find_sock(net, family, port);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -0700299 if (!vs)
300 return NULL;
301
302 return vxlan_vs_find_vni(vs, id);
303}
304
stephen hemmingerd3428942012-10-01 12:32:35 +0000305/* Fill in neighbour message in skbuff. */
306static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
Stephen Hemminger234f5b72013-06-17 14:16:41 -0700307 const struct vxlan_fdb *fdb,
308 u32 portid, u32 seq, int type, unsigned int flags,
309 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000310{
311 unsigned long now = jiffies;
312 struct nda_cacheinfo ci;
313 struct nlmsghdr *nlh;
314 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000315 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000316
317 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
318 if (nlh == NULL)
319 return -EMSGSIZE;
320
321 ndm = nlmsg_data(nlh);
322 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000323
324 send_eth = send_ip = true;
325
326 if (type == RTM_GETNEIGH) {
327 ndm->ndm_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +0800328 send_ip = !vxlan_addr_any(&rdst->remote_ip);
David Stevense4f67ad2012-11-20 02:50:14 +0000329 send_eth = !is_zero_ether_addr(fdb->eth_addr);
330 } else
331 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000332 ndm->ndm_state = fdb->state;
333 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000334 ndm->ndm_flags = fdb->flags;
Jun Zhao545469f2014-07-26 00:38:59 +0800335 ndm->ndm_type = RTN_UNICAST;
stephen hemmingerd3428942012-10-01 12:32:35 +0000336
David Stevense4f67ad2012-11-20 02:50:14 +0000337 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000338 goto nla_put_failure;
339
Cong Wange4c7ed42013-08-31 13:44:33 +0800340 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
David Stevens66817122013-03-15 04:35:51 +0000341 goto nla_put_failure;
342
stephen hemminger823aa872013-04-27 11:31:57 +0000343 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000344 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
345 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000346 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
Pravin B Shelar60d9d4c2013-06-20 00:26:31 -0700347 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
David Stevens66817122013-03-15 04:35:51 +0000348 goto nla_put_failure;
349 if (rdst->remote_ifindex &&
350 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000351 goto nla_put_failure;
352
353 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
354 ci.ndm_confirmed = 0;
355 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
356 ci.ndm_refcnt = 0;
357
358 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
359 goto nla_put_failure;
360
361 return nlmsg_end(skb, nlh);
362
363nla_put_failure:
364 nlmsg_cancel(skb, nlh);
365 return -EMSGSIZE;
366}
367
368static inline size_t vxlan_nlmsg_size(void)
369{
370 return NLMSG_ALIGN(sizeof(struct ndmsg))
371 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Cong Wange4c7ed42013-08-31 13:44:33 +0800372 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000373 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000374 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
375 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000376 + nla_total_size(sizeof(struct nda_cacheinfo));
377}
378
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200379static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
380 struct vxlan_rdst *rd, int type)
stephen hemmingerd3428942012-10-01 12:32:35 +0000381{
382 struct net *net = dev_net(vxlan->dev);
383 struct sk_buff *skb;
384 int err = -ENOBUFS;
385
386 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
387 if (skb == NULL)
388 goto errout;
389
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200390 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
stephen hemmingerd3428942012-10-01 12:32:35 +0000391 if (err < 0) {
392 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
393 WARN_ON(err == -EMSGSIZE);
394 kfree_skb(skb);
395 goto errout;
396 }
397
398 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
399 return;
400errout:
401 if (err < 0)
402 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
403}
404
Cong Wange4c7ed42013-08-31 13:44:33 +0800405static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
David Stevense4f67ad2012-11-20 02:50:14 +0000406{
407 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700408 struct vxlan_fdb f = {
409 .state = NUD_STALE,
410 };
411 struct vxlan_rdst remote = {
Cong Wange4c7ed42013-08-31 13:44:33 +0800412 .remote_ip = *ipa, /* goes to NDA_DST */
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700413 .remote_vni = VXLAN_N_VID,
414 };
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700415
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200416 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000417}
418
419static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
420{
Stephen Hemmingerbb3fd682013-06-17 14:16:40 -0700421 struct vxlan_fdb f = {
422 .state = NUD_STALE,
423 };
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200424 struct vxlan_rdst remote = { };
David Stevense4f67ad2012-11-20 02:50:14 +0000425
David Stevense4f67ad2012-11-20 02:50:14 +0000426 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
427
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200428 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
David Stevense4f67ad2012-11-20 02:50:14 +0000429}
430
stephen hemmingerd3428942012-10-01 12:32:35 +0000431/* Hash Ethernet address */
432static u32 eth_hash(const unsigned char *addr)
433{
434 u64 value = get_unaligned((u64 *)addr);
435
436 /* only want 6 bytes */
437#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000438 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000439#else
440 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000441#endif
442 return hash_64(value, FDB_HASH_BITS);
443}
444
445/* Hash chain to use given mac address */
446static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
447 const u8 *mac)
448{
449 return &vxlan->fdb_head[eth_hash(mac)];
450}
451
452/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000453static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000454 const u8 *mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000455{
456 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
457 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000458
Sasha Levinb67bfe02013-02-27 17:06:00 -0800459 hlist_for_each_entry_rcu(f, head, hlist) {
Joe Perches7367d0b2013-09-01 11:51:23 -0700460 if (ether_addr_equal(mac, f->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000461 return f;
462 }
463
464 return NULL;
465}
466
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000467static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
468 const u8 *mac)
469{
470 struct vxlan_fdb *f;
471
472 f = __vxlan_find_mac(vxlan, mac);
473 if (f)
474 f->used = jiffies;
475
476 return f;
477}
478
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300479/* caller should hold vxlan->hash_lock */
480static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800481 union vxlan_addr *ip, __be16 port,
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300482 __u32 vni, __u32 ifindex)
483{
484 struct vxlan_rdst *rd;
485
486 list_for_each_entry(rd, &f->remotes, list) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800487 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300488 rd->remote_port == port &&
489 rd->remote_vni == vni &&
490 rd->remote_ifindex == ifindex)
491 return rd;
492 }
493
494 return NULL;
495}
496
Thomas Richter906dc182013-07-19 17:20:07 +0200497/* Replace destination of unicast mac */
498static int vxlan_fdb_replace(struct vxlan_fdb *f,
Cong Wange4c7ed42013-08-31 13:44:33 +0800499 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
Thomas Richter906dc182013-07-19 17:20:07 +0200500{
501 struct vxlan_rdst *rd;
502
503 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
504 if (rd)
505 return 0;
506
507 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
508 if (!rd)
509 return 0;
Cong Wange4c7ed42013-08-31 13:44:33 +0800510 rd->remote_ip = *ip;
Thomas Richter906dc182013-07-19 17:20:07 +0200511 rd->remote_port = port;
512 rd->remote_vni = vni;
513 rd->remote_ifindex = ifindex;
514 return 1;
515}
516
David Stevens66817122013-03-15 04:35:51 +0000517/* Add/update destinations for multicast */
518static int vxlan_fdb_append(struct vxlan_fdb *f,
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200519 union vxlan_addr *ip, __be16 port, __u32 vni,
520 __u32 ifindex, struct vxlan_rdst **rdp)
David Stevens66817122013-03-15 04:35:51 +0000521{
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700522 struct vxlan_rdst *rd;
David Stevens66817122013-03-15 04:35:51 +0000523
Mike Rapoporta5e7c102013-06-25 16:01:52 +0300524 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
525 if (rd)
526 return 0;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700527
David Stevens66817122013-03-15 04:35:51 +0000528 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
529 if (rd == NULL)
530 return -ENOBUFS;
Cong Wange4c7ed42013-08-31 13:44:33 +0800531 rd->remote_ip = *ip;
David Stevens66817122013-03-15 04:35:51 +0000532 rd->remote_port = port;
533 rd->remote_vni = vni;
534 rd->remote_ifindex = ifindex;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700535
536 list_add_tail_rcu(&rd->list, &f->remotes);
537
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200538 *rdp = rd;
David Stevens66817122013-03-15 04:35:51 +0000539 return 1;
540}
541
Tom Herbertdfd86452015-01-12 17:00:38 -0800542static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
543 unsigned int off,
544 struct vxlanhdr *vh, size_t hdrlen,
545 u32 data)
546{
547 size_t start, offset, plen;
548 __wsum delta;
549
550 if (skb->remcsum_offload)
551 return vh;
552
553 if (!NAPI_GRO_CB(skb)->csum_valid)
554 return NULL;
555
556 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
557 offset = start + ((data & VXLAN_RCO_UDP) ?
558 offsetof(struct udphdr, check) :
559 offsetof(struct tcphdr, check));
560
561 plen = hdrlen + offset + sizeof(u16);
562
563 /* Pull checksum that will be written */
564 if (skb_gro_header_hard(skb, off + plen)) {
565 vh = skb_gro_header_slow(skb, off + plen, off);
566 if (!vh)
567 return NULL;
568 }
569
570 delta = remcsum_adjust((void *)vh + hdrlen,
571 NAPI_GRO_CB(skb)->csum, start, offset);
572
573 /* Adjust skb->csum since we changed the packet */
574 skb->csum = csum_add(skb->csum, delta);
575 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
576
577 skb->remcsum_offload = 1;
578
579 return vh;
580}
581
Tom Herberta2b12f32015-01-12 17:00:37 -0800582static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
583 struct sk_buff *skb,
584 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200585{
586 struct sk_buff *p, **pp = NULL;
587 struct vxlanhdr *vh, *vh2;
Jesse Gross9b174d82014-12-30 19:10:15 -0800588 unsigned int hlen, off_vx;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200589 int flush = 1;
Tom Herbertdfd86452015-01-12 17:00:38 -0800590 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
591 udp_offloads);
592 u32 flags;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200593
594 off_vx = skb_gro_offset(skb);
595 hlen = off_vx + sizeof(*vh);
596 vh = skb_gro_header_fast(skb, off_vx);
597 if (skb_gro_header_hard(skb, hlen)) {
598 vh = skb_gro_header_slow(skb, hlen, off_vx);
599 if (unlikely(!vh))
600 goto out;
601 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200602
Tom Herbertdfd86452015-01-12 17:00:38 -0800603 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
604 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
605
606 flags = ntohl(vh->vx_flags);
607
608 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
609 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
610 ntohl(vh->vx_vni));
611
612 if (!vh)
613 goto out;
614 }
615
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200616 flush = 0;
617
618 for (p = *head; p; p = p->next) {
619 if (!NAPI_GRO_CB(p)->same_flow)
620 continue;
621
622 vh2 = (struct vxlanhdr *)(p->data + off_vx);
Jesse Gross9b174d82014-12-30 19:10:15 -0800623 if (vh->vx_vni != vh2->vx_vni) {
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200624 NAPI_GRO_CB(p)->same_flow = 0;
625 continue;
626 }
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200627 }
628
Jesse Gross9b174d82014-12-30 19:10:15 -0800629 pp = eth_gro_receive(head, skb);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200630
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200631out:
632 NAPI_GRO_CB(skb)->flush |= flush;
633
634 return pp;
635}
636
Tom Herberta2b12f32015-01-12 17:00:37 -0800637static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
638 struct udp_offload *uoff)
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200639{
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800640 udp_tunnel_gro_complete(skb, nhoff);
641
Jesse Gross9b174d82014-12-30 19:10:15 -0800642 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200643}
644
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700645/* Notify netdevs that UDP port started listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200646static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700647{
648 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200649 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700650 struct net *net = sock_net(sk);
651 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700652 __be16 port = inet_sk(sk)->inet_sport;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200653 int err;
654
655 if (sa_family == AF_INET) {
656 err = udp_add_offload(&vs->udp_offloads);
657 if (err)
658 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
659 }
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700660
661 rcu_read_lock();
662 for_each_netdev_rcu(net, dev) {
663 if (dev->netdev_ops->ndo_add_vxlan_port)
664 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
665 port);
666 }
667 rcu_read_unlock();
668}
669
670/* Notify netdevs that UDP port is no more listening */
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200671static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700672{
673 struct net_device *dev;
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200674 struct sock *sk = vs->sock->sk;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700675 struct net *net = sock_net(sk);
676 sa_family_t sa_family = sk->sk_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -0700677 __be16 port = inet_sk(sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700678
679 rcu_read_lock();
680 for_each_netdev_rcu(net, dev) {
681 if (dev->netdev_ops->ndo_del_vxlan_port)
682 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
683 port);
684 }
685 rcu_read_unlock();
Or Gerlitzdc01e7d2014-01-20 13:59:21 +0200686
687 if (sa_family == AF_INET)
688 udp_del_offload(&vs->udp_offloads);
Joseph Gasparakis53cf52752013-09-04 02:13:38 -0700689}
690
stephen hemmingerd3428942012-10-01 12:32:35 +0000691/* Add new entry to forwarding table -- assumes lock held */
692static int vxlan_fdb_create(struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800693 const u8 *mac, union vxlan_addr *ip,
David Stevens66817122013-03-15 04:35:51 +0000694 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000695 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000696 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000697{
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200698 struct vxlan_rdst *rd = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000699 struct vxlan_fdb *f;
700 int notify = 0;
701
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000702 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000703 if (f) {
704 if (flags & NLM_F_EXCL) {
705 netdev_dbg(vxlan->dev,
706 "lost race to create %pM\n", mac);
707 return -EEXIST;
708 }
709 if (f->state != state) {
710 f->state = state;
711 f->updated = jiffies;
712 notify = 1;
713 }
David Stevensae884082013-04-19 00:36:26 +0000714 if (f->flags != ndm_flags) {
715 f->flags = ndm_flags;
716 f->updated = jiffies;
717 notify = 1;
718 }
Thomas Richter906dc182013-07-19 17:20:07 +0200719 if ((flags & NLM_F_REPLACE)) {
720 /* Only change unicasts */
721 if (!(is_multicast_ether_addr(f->eth_addr) ||
722 is_zero_ether_addr(f->eth_addr))) {
723 int rc = vxlan_fdb_replace(f, ip, port, vni,
724 ifindex);
725
726 if (rc < 0)
727 return rc;
728 notify |= rc;
729 } else
730 return -EOPNOTSUPP;
731 }
David Stevens66817122013-03-15 04:35:51 +0000732 if ((flags & NLM_F_APPEND) &&
Mike Rapoport58e4c762013-06-25 16:01:56 +0300733 (is_multicast_ether_addr(f->eth_addr) ||
734 is_zero_ether_addr(f->eth_addr))) {
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200735 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
736 &rd);
David Stevens66817122013-03-15 04:35:51 +0000737
738 if (rc < 0)
739 return rc;
740 notify |= rc;
741 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000742 } else {
743 if (!(flags & NLM_F_CREATE))
744 return -ENOENT;
745
746 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
747 return -ENOSPC;
748
Thomas Richter906dc182013-07-19 17:20:07 +0200749 /* Disallow replace to add a multicast entry */
750 if ((flags & NLM_F_REPLACE) &&
751 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
752 return -EOPNOTSUPP;
753
Cong Wange4c7ed42013-08-31 13:44:33 +0800754 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000755 f = kmalloc(sizeof(*f), GFP_ATOMIC);
756 if (!f)
757 return -ENOMEM;
758
759 notify = 1;
stephen hemmingerd3428942012-10-01 12:32:35 +0000760 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000761 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000762 f->updated = f->used = jiffies;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700763 INIT_LIST_HEAD(&f->remotes);
stephen hemmingerd3428942012-10-01 12:32:35 +0000764 memcpy(f->eth_addr, mac, ETH_ALEN);
765
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200766 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700767
stephen hemmingerd3428942012-10-01 12:32:35 +0000768 ++vxlan->addrcnt;
769 hlist_add_head_rcu(&f->hlist,
770 vxlan_fdb_head(vxlan, mac));
771 }
772
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200773 if (notify) {
774 if (rd == NULL)
775 rd = first_remote_rtnl(f);
776 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
777 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000778
779 return 0;
780}
781
Wei Yongjun6706c822013-04-11 19:00:35 +0000782static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000783{
784 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700785 struct vxlan_rdst *rd, *nd;
David Stevens66817122013-03-15 04:35:51 +0000786
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700787 list_for_each_entry_safe(rd, nd, &f->remotes, list)
David Stevens66817122013-03-15 04:35:51 +0000788 kfree(rd);
David Stevens66817122013-03-15 04:35:51 +0000789 kfree(f);
790}
791
stephen hemmingerd3428942012-10-01 12:32:35 +0000792static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
793{
794 netdev_dbg(vxlan->dev,
795 "delete %pM\n", f->eth_addr);
796
797 --vxlan->addrcnt;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200798 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +0000799
800 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000801 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000802}
803
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300804static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
Cong Wange4c7ed42013-08-31 13:44:33 +0800805 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300806{
807 struct net *net = dev_net(vxlan->dev);
Cong Wange4c7ed42013-08-31 13:44:33 +0800808 int err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300809
810 if (tb[NDA_DST]) {
Cong Wange4c7ed42013-08-31 13:44:33 +0800811 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
812 if (err)
813 return err;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300814 } else {
Cong Wange4c7ed42013-08-31 13:44:33 +0800815 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
816 if (remote->sa.sa_family == AF_INET) {
817 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
818 ip->sa.sa_family = AF_INET;
819#if IS_ENABLED(CONFIG_IPV6)
820 } else {
821 ip->sin6.sin6_addr = in6addr_any;
822 ip->sa.sa_family = AF_INET6;
823#endif
824 }
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300825 }
826
827 if (tb[NDA_PORT]) {
828 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
829 return -EINVAL;
830 *port = nla_get_be16(tb[NDA_PORT]);
831 } else {
832 *port = vxlan->dst_port;
833 }
834
835 if (tb[NDA_VNI]) {
836 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
837 return -EINVAL;
838 *vni = nla_get_u32(tb[NDA_VNI]);
839 } else {
840 *vni = vxlan->default_dst.remote_vni;
841 }
842
843 if (tb[NDA_IFINDEX]) {
844 struct net_device *tdev;
845
846 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
847 return -EINVAL;
848 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Ying Xue73763942014-01-15 10:23:41 +0800849 tdev = __dev_get_by_index(net, *ifindex);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300850 if (!tdev)
851 return -EADDRNOTAVAIL;
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300852 } else {
853 *ifindex = 0;
854 }
855
856 return 0;
857}
858
stephen hemmingerd3428942012-10-01 12:32:35 +0000859/* Add static entry (via netlink) */
860static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
861 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100862 const unsigned char *addr, u16 vid, u16 flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000863{
864 struct vxlan_dev *vxlan = netdev_priv(dev);
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300865 /* struct net *net = dev_net(vxlan->dev); */
Cong Wange4c7ed42013-08-31 13:44:33 +0800866 union vxlan_addr ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000867 __be16 port;
868 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000869 int err;
870
871 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
872 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
873 ndm->ndm_state);
874 return -EINVAL;
875 }
876
877 if (tb[NDA_DST] == NULL)
878 return -EINVAL;
879
Mike Rapoportf0b074b2013-06-25 16:01:53 +0300880 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
881 if (err)
882 return err;
David Stevens66817122013-03-15 04:35:51 +0000883
Mike Rapoport5933a7b2014-04-01 09:23:01 +0300884 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
885 return -EAFNOSUPPORT;
886
stephen hemmingerd3428942012-10-01 12:32:35 +0000887 spin_lock_bh(&vxlan->hash_lock);
Cong Wange4c7ed42013-08-31 13:44:33 +0800888 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000889 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000890 spin_unlock_bh(&vxlan->hash_lock);
891
892 return err;
893}
894
895/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000896static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
897 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +0100898 const unsigned char *addr, u16 vid)
stephen hemmingerd3428942012-10-01 12:32:35 +0000899{
900 struct vxlan_dev *vxlan = netdev_priv(dev);
901 struct vxlan_fdb *f;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300902 struct vxlan_rdst *rd = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +0800903 union vxlan_addr ip;
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300904 __be16 port;
905 u32 vni, ifindex;
906 int err;
907
908 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
909 if (err)
910 return err;
911
912 err = -ENOENT;
stephen hemmingerd3428942012-10-01 12:32:35 +0000913
914 spin_lock_bh(&vxlan->hash_lock);
915 f = vxlan_find_mac(vxlan, addr);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300916 if (!f)
917 goto out;
918
Cong Wange4c7ed42013-08-31 13:44:33 +0800919 if (!vxlan_addr_any(&ip)) {
920 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300921 if (!rd)
922 goto out;
stephen hemmingerd3428942012-10-01 12:32:35 +0000923 }
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300924
925 err = 0;
926
927 /* remove a destination if it's not the only one on the list,
928 * otherwise destroy the fdb entry
929 */
930 if (rd && !list_is_singular(&f->remotes)) {
931 list_del_rcu(&rd->list);
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +0200932 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
Wei Yongjundcdc7a52013-08-17 07:32:09 +0800933 kfree_rcu(rd, rcu);
Mike Rapoportbc7892b2013-06-25 16:01:54 +0300934 goto out;
935 }
936
937 vxlan_fdb_destroy(vxlan, f);
938
939out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000940 spin_unlock_bh(&vxlan->hash_lock);
941
942 return err;
943}
944
945/* Dump forwarding table */
946static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -0400947 struct net_device *dev,
948 struct net_device *filter_dev, int idx)
stephen hemmingerd3428942012-10-01 12:32:35 +0000949{
950 struct vxlan_dev *vxlan = netdev_priv(dev);
951 unsigned int h;
952
953 for (h = 0; h < FDB_HASH_SIZE; ++h) {
954 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000955 int err;
956
Sasha Levinb67bfe02013-02-27 17:06:00 -0800957 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000958 struct vxlan_rdst *rd;
stephen hemmingerd3428942012-10-01 12:32:35 +0000959
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700960 if (idx < cb->args[0])
961 goto skip;
962
963 list_for_each_entry_rcu(rd, &f->remotes, list) {
David Stevens66817122013-03-15 04:35:51 +0000964 err = vxlan_fdb_info(skb, vxlan, f,
965 NETLINK_CB(cb->skb).portid,
966 cb->nlh->nlmsg_seq,
967 RTM_NEWNEIGH,
968 NLM_F_MULTI, rd);
969 if (err < 0)
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700970 goto out;
David Stevens66817122013-03-15 04:35:51 +0000971 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700972skip:
973 ++idx;
stephen hemmingerd3428942012-10-01 12:32:35 +0000974 }
975 }
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700976out:
stephen hemmingerd3428942012-10-01 12:32:35 +0000977 return idx;
978}
979
980/* Watch incoming packets to learn mapping between Ethernet address
981 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700982 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000983 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700984static bool vxlan_snoop(struct net_device *dev,
Cong Wange4c7ed42013-08-31 13:44:33 +0800985 union vxlan_addr *src_ip, const u8 *src_mac)
stephen hemmingerd3428942012-10-01 12:32:35 +0000986{
987 struct vxlan_dev *vxlan = netdev_priv(dev);
988 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000989
990 f = vxlan_find_mac(vxlan, src_mac);
991 if (likely(f)) {
stephen hemminger5ca54612013-08-04 17:17:39 -0700992 struct vxlan_rdst *rdst = first_remote_rcu(f);
Stephen Hemminger3e61aa82013-06-17 14:16:12 -0700993
Cong Wange4c7ed42013-08-31 13:44:33 +0800994 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700995 return false;
996
997 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700998 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700999 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +00001000
1001 if (net_ratelimit())
1002 netdev_info(dev,
Cong Wange4c7ed42013-08-31 13:44:33 +08001003 "%pM migrated from %pIS to %pIS\n",
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07001004 src_mac, &rdst->remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +00001005
Cong Wange4c7ed42013-08-31 13:44:33 +08001006 rdst->remote_ip = *src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +00001007 f->updated = jiffies;
Nicolas Dichtel9e4b93f2014-04-22 15:01:30 +02001008 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
stephen hemmingerd3428942012-10-01 12:32:35 +00001009 } else {
1010 /* learned new entry */
1011 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -07001012
1013 /* close off race between vxlan_flush and incoming packets */
1014 if (netif_running(dev))
1015 vxlan_fdb_create(vxlan, src_mac, src_ip,
1016 NUD_REACHABLE,
1017 NLM_F_EXCL|NLM_F_CREATE,
1018 vxlan->dst_port,
1019 vxlan->default_dst.remote_vni,
1020 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +00001021 spin_unlock(&vxlan->hash_lock);
1022 }
stephen hemminger26a41ae62013-06-17 12:09:58 -07001023
1024 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +00001025}
1026
stephen hemmingerd3428942012-10-01 12:32:35 +00001027/* See if multicast group is already in use by other ID */
Gao feng95ab0992013-12-10 16:37:33 +08001028static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
stephen hemmingerd3428942012-10-01 12:32:35 +00001029{
stephen hemminger553675f2013-05-16 11:35:20 +00001030 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +00001031
Gao feng95ab0992013-12-10 16:37:33 +08001032 /* The vxlan_sock is only used by dev, leaving group has
1033 * no effect on other vxlan devices.
1034 */
1035 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1036 return false;
1037
stephen hemminger553675f2013-05-16 11:35:20 +00001038 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
Gao feng95ab0992013-12-10 16:37:33 +08001039 if (!netif_running(vxlan->dev) || vxlan == dev)
stephen hemminger553675f2013-05-16 11:35:20 +00001040 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +00001041
Gao feng95ab0992013-12-10 16:37:33 +08001042 if (vxlan->vn_sock != dev->vn_sock)
1043 continue;
1044
1045 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1046 &dev->default_dst.remote_ip))
1047 continue;
1048
1049 if (vxlan->default_dst.remote_ifindex !=
1050 dev->default_dst.remote_ifindex)
1051 continue;
1052
1053 return true;
stephen hemminger553675f2013-05-16 11:35:20 +00001054 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001055
1056 return false;
1057}
1058
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001059static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001060{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001061 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001062}
1063
Pravin B Shelar012a5722013-08-19 11:23:07 -07001064void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001065{
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07001066 struct sock *sk = vs->sock->sk;
1067 struct net *net = sock_net(sk);
1068 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar012a5722013-08-19 11:23:07 -07001069
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001070 if (!atomic_dec_and_test(&vs->refcnt))
1071 return;
1072
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001073 spin_lock(&vn->sock_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001074 hlist_del_rcu(&vs->hlist);
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02001075 vxlan_notify_del_rx_port(vs);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07001076 spin_unlock(&vn->sock_lock);
1077
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001078 queue_work(vxlan_wq, &vs->del_work);
1079}
Pravin B Shelar012a5722013-08-19 11:23:07 -07001080EXPORT_SYMBOL_GPL(vxlan_sock_release);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001081
stephen hemminger3fc2de22013-07-18 08:40:15 -07001082/* Callback to update multicast group membership when first VNI on
1083 * multicast asddress is brought up
1084 * Done as workqueue because ip_mc_join_group acquires RTNL.
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001085 */
stephen hemminger3fc2de22013-07-18 08:40:15 -07001086static void vxlan_igmp_join(struct work_struct *work)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001087{
stephen hemminger3fc2de22013-07-18 08:40:15 -07001088 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001089 struct vxlan_sock *vs = vxlan->vn_sock;
1090 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001091 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1092 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +00001093
stephen hemmingerd3428942012-10-01 12:32:35 +00001094 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001095 if (ip->sa.sa_family == AF_INET) {
1096 struct ip_mreqn mreq = {
1097 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1098 .imr_ifindex = ifindex,
1099 };
1100
1101 ip_mc_join_group(sk, &mreq);
1102#if IS_ENABLED(CONFIG_IPV6)
1103 } else {
1104 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1105 &ip->sin6.sin6_addr);
1106#endif
1107 }
stephen hemminger3fc2de22013-07-18 08:40:15 -07001108 release_sock(sk);
1109
Pravin B Shelar012a5722013-08-19 11:23:07 -07001110 vxlan_sock_release(vs);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001111 dev_put(vxlan->dev);
1112}
1113
1114/* Inverse of vxlan_igmp_join when last VNI is brought down */
1115static void vxlan_igmp_leave(struct work_struct *work)
1116{
1117 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
stephen hemminger3fc2de22013-07-18 08:40:15 -07001118 struct vxlan_sock *vs = vxlan->vn_sock;
1119 struct sock *sk = vs->sock->sk;
Cong Wange4c7ed42013-08-31 13:44:33 +08001120 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1121 int ifindex = vxlan->default_dst.remote_ifindex;
stephen hemminger3fc2de22013-07-18 08:40:15 -07001122
1123 lock_sock(sk);
Cong Wange4c7ed42013-08-31 13:44:33 +08001124 if (ip->sa.sa_family == AF_INET) {
1125 struct ip_mreqn mreq = {
1126 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1127 .imr_ifindex = ifindex,
1128 };
1129
1130 ip_mc_leave_group(sk, &mreq);
1131#if IS_ENABLED(CONFIG_IPV6)
1132 } else {
1133 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1134 &ip->sin6.sin6_addr);
1135#endif
1136 }
1137
stephen hemmingerd3428942012-10-01 12:32:35 +00001138 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +00001139
Pravin B Shelar012a5722013-08-19 11:23:07 -07001140 vxlan_sock_release(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001141 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001142}
1143
Tom Herbertdfd86452015-01-12 17:00:38 -08001144static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1145 size_t hdrlen, u32 data)
1146{
1147 size_t start, offset, plen;
1148 __wsum delta;
1149
1150 if (skb->remcsum_offload) {
1151 /* Already processed in GRO path */
1152 skb->remcsum_offload = 0;
1153 return vh;
1154 }
1155
1156 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1157 offset = start + ((data & VXLAN_RCO_UDP) ?
1158 offsetof(struct udphdr, check) :
1159 offsetof(struct tcphdr, check));
1160
1161 plen = hdrlen + offset + sizeof(u16);
1162
1163 if (!pskb_may_pull(skb, plen))
1164 return NULL;
1165
1166 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1167
1168 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE))
1169 __skb_checksum_complete(skb);
1170
1171 delta = remcsum_adjust((void *)vh + hdrlen,
1172 skb->csum, start, offset);
1173
1174 /* Adjust skb->csum since we changed the packet */
1175 skb->csum = csum_add(skb->csum, delta);
1176
1177 return vh;
1178}
1179
stephen hemmingerd3428942012-10-01 12:32:35 +00001180/* Callback from net/ipv4/udp.c to receive packets */
1181static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1182{
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001183 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001184 struct vxlanhdr *vxh;
Tom Herbert3bf39472015-01-08 12:31:18 -08001185 u32 flags, vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001186
stephen hemmingerd3428942012-10-01 12:32:35 +00001187 /* Need Vxlan and inner Ethernet header to be present */
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001188 if (!pskb_may_pull(skb, VXLAN_HLEN))
stephen hemmingerd3428942012-10-01 12:32:35 +00001189 goto error;
1190
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001191 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
Tom Herbert3bf39472015-01-08 12:31:18 -08001192 flags = ntohl(vxh->vx_flags);
1193 vni = ntohl(vxh->vx_vni);
1194
1195 if (flags & VXLAN_HF_VNI) {
1196 flags &= ~VXLAN_HF_VNI;
1197 } else {
1198 /* VNI flag always required to be set */
1199 goto bad_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +00001200 }
1201
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001202 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
stephen hemmingerd3428942012-10-01 12:32:35 +00001203 goto drop;
Tom Herbertdfd86452015-01-12 17:00:38 -08001204 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
stephen hemmingerd3428942012-10-01 12:32:35 +00001205
Pravin B Shelar559835ea2013-09-24 10:25:40 -07001206 vs = rcu_dereference_sk_user_data(sk);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001207 if (!vs)
stephen hemmingerd3428942012-10-01 12:32:35 +00001208 goto drop;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001209
Tom Herbertdfd86452015-01-12 17:00:38 -08001210 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1211 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1212 if (!vxh)
1213 goto drop;
1214
1215 flags &= ~VXLAN_HF_RCO;
1216 vni &= VXLAN_VID_MASK;
1217 }
1218
1219 if (flags || (vni & ~VXLAN_VID_MASK)) {
Tom Herbert3bf39472015-01-08 12:31:18 -08001220 /* If there are any unprocessed flags remaining treat
1221 * this as a malformed packet. This behavior diverges from
1222 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1223 * in reserved fields are to be ignored. The approach here
1224 * maintains compatbility with previous stack code, and also
1225 * is more robust and provides a little more security in
1226 * adding extensions to VXLAN.
1227 */
1228
1229 goto bad_flags;
1230 }
1231
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001232 vs->rcv(vs, skb, vxh->vx_vni);
1233 return 0;
1234
1235drop:
1236 /* Consume bad packet */
1237 kfree_skb(skb);
1238 return 0;
1239
Tom Herbert3bf39472015-01-08 12:31:18 -08001240bad_flags:
1241 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1242 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1243
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001244error:
1245 /* Return non vxlan pkt */
1246 return 1;
1247}
1248
1249static void vxlan_rcv(struct vxlan_sock *vs,
1250 struct sk_buff *skb, __be32 vx_vni)
1251{
Cong Wange4c7ed42013-08-31 13:44:33 +08001252 struct iphdr *oip = NULL;
1253 struct ipv6hdr *oip6 = NULL;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001254 struct vxlan_dev *vxlan;
Li RongQing8f849852014-01-04 13:57:59 +08001255 struct pcpu_sw_netstats *stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001256 union vxlan_addr saddr;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001257 __u32 vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001258 int err = 0;
1259 union vxlan_addr *remote_ip;
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001260
1261 vni = ntohl(vx_vni) >> 8;
1262 /* Is this VNI defined? */
1263 vxlan = vxlan_vs_find_vni(vs, vni);
1264 if (!vxlan)
1265 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001266
Cong Wange4c7ed42013-08-31 13:44:33 +08001267 remote_ip = &vxlan->default_dst.remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001268 skb_reset_mac_header(skb);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001269 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
stephen hemmingerd3428942012-10-01 12:32:35 +00001270 skb->protocol = eth_type_trans(skb, vxlan->dev);
Tom Herbertf79b0642014-06-14 23:24:36 -07001271 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
stephen hemmingerd3428942012-10-01 12:32:35 +00001272
1273 /* Ignore packet loops (and multicast echo) */
Joe Perches7367d0b2013-09-01 11:51:23 -07001274 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001275 goto drop;
1276
Pravin B Shelar7ce04752013-08-19 11:22:54 -07001277 /* Re-examine inner Ethernet packet */
Cong Wange4c7ed42013-08-31 13:44:33 +08001278 if (remote_ip->sa.sa_family == AF_INET) {
1279 oip = ip_hdr(skb);
1280 saddr.sin.sin_addr.s_addr = oip->saddr;
1281 saddr.sa.sa_family = AF_INET;
1282#if IS_ENABLED(CONFIG_IPV6)
1283 } else {
1284 oip6 = ipv6_hdr(skb);
1285 saddr.sin6.sin6_addr = oip6->saddr;
1286 saddr.sa.sa_family = AF_INET6;
1287#endif
1288 }
1289
stephen hemminger26a41ae62013-06-17 12:09:58 -07001290 if ((vxlan->flags & VXLAN_F_LEARN) &&
Cong Wange4c7ed42013-08-31 13:44:33 +08001291 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
stephen hemminger26a41ae62013-06-17 12:09:58 -07001292 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001293
stephen hemmingerd3428942012-10-01 12:32:35 +00001294 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001295
Cong Wange4c7ed42013-08-31 13:44:33 +08001296 if (oip6)
1297 err = IP6_ECN_decapsulate(oip6, skb);
1298 if (oip)
1299 err = IP_ECN_decapsulate(oip, skb);
1300
stephen hemmingerd3428942012-10-01 12:32:35 +00001301 if (unlikely(err)) {
Cong Wange4c7ed42013-08-31 13:44:33 +08001302 if (log_ecn_error) {
1303 if (oip6)
1304 net_info_ratelimited("non-ECT from %pI6\n",
1305 &oip6->saddr);
1306 if (oip)
1307 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1308 &oip->saddr, oip->tos);
1309 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001310 if (err > 1) {
1311 ++vxlan->dev->stats.rx_frame_errors;
1312 ++vxlan->dev->stats.rx_errors;
1313 goto drop;
1314 }
1315 }
1316
Pravin B Shelare8171042013-03-25 14:49:46 +00001317 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001318 u64_stats_update_begin(&stats->syncp);
1319 stats->rx_packets++;
1320 stats->rx_bytes += skb->len;
1321 u64_stats_update_end(&stats->syncp);
1322
1323 netif_rx(skb);
1324
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07001325 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001326drop:
1327 /* Consume bad packet */
1328 kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001329}
1330
David Stevense4f67ad2012-11-20 02:50:14 +00001331static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1332{
1333 struct vxlan_dev *vxlan = netdev_priv(dev);
1334 struct arphdr *parp;
1335 u8 *arpptr, *sha;
1336 __be32 sip, tip;
1337 struct neighbour *n;
1338
1339 if (dev->flags & IFF_NOARP)
1340 goto out;
1341
1342 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1343 dev->stats.tx_dropped++;
1344 goto out;
1345 }
1346 parp = arp_hdr(skb);
1347
1348 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1349 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1350 parp->ar_pro != htons(ETH_P_IP) ||
1351 parp->ar_op != htons(ARPOP_REQUEST) ||
1352 parp->ar_hln != dev->addr_len ||
1353 parp->ar_pln != 4)
1354 goto out;
1355 arpptr = (u8 *)parp + sizeof(struct arphdr);
1356 sha = arpptr;
1357 arpptr += dev->addr_len; /* sha */
1358 memcpy(&sip, arpptr, sizeof(sip));
1359 arpptr += sizeof(sip);
1360 arpptr += dev->addr_len; /* tha */
1361 memcpy(&tip, arpptr, sizeof(tip));
1362
1363 if (ipv4_is_loopback(tip) ||
1364 ipv4_is_multicast(tip))
1365 goto out;
1366
1367 n = neigh_lookup(&arp_tbl, &tip, dev);
1368
1369 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +00001370 struct vxlan_fdb *f;
1371 struct sk_buff *reply;
1372
1373 if (!(n->nud_state & NUD_CONNECTED)) {
1374 neigh_release(n);
1375 goto out;
1376 }
1377
1378 f = vxlan_find_mac(vxlan, n->ha);
Cong Wange4c7ed42013-08-31 13:44:33 +08001379 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
David Stevense4f67ad2012-11-20 02:50:14 +00001380 /* bridge-local neighbor */
1381 neigh_release(n);
1382 goto out;
1383 }
1384
1385 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1386 n->ha, sha);
1387
1388 neigh_release(n);
1389
David Stevens73461352014-03-18 12:32:29 -04001390 if (reply == NULL)
1391 goto out;
1392
David Stevense4f67ad2012-11-20 02:50:14 +00001393 skb_reset_mac_header(reply);
1394 __skb_pull(reply, skb_network_offset(reply));
1395 reply->ip_summed = CHECKSUM_UNNECESSARY;
1396 reply->pkt_type = PACKET_HOST;
1397
1398 if (netif_rx_ni(reply) == NET_RX_DROP)
1399 dev->stats.rx_dropped++;
Cong Wange4c7ed42013-08-31 13:44:33 +08001400 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1401 union vxlan_addr ipa = {
1402 .sin.sin_addr.s_addr = tip,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001403 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001404 };
1405
1406 vxlan_ip_miss(dev, &ipa);
1407 }
David Stevense4f67ad2012-11-20 02:50:14 +00001408out:
1409 consume_skb(skb);
1410 return NETDEV_TX_OK;
1411}
1412
Cong Wangf564f452013-08-31 13:44:36 +08001413#if IS_ENABLED(CONFIG_IPV6)
David Stevens4b29dba2014-03-24 10:39:58 -04001414static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1415 struct neighbour *n, bool isrouter)
1416{
1417 struct net_device *dev = request->dev;
1418 struct sk_buff *reply;
1419 struct nd_msg *ns, *na;
1420 struct ipv6hdr *pip6;
1421 u8 *daddr;
1422 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1423 int ns_olen;
1424 int i, len;
1425
1426 if (dev == NULL)
1427 return NULL;
1428
1429 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1430 sizeof(*na) + na_olen + dev->needed_tailroom;
1431 reply = alloc_skb(len, GFP_ATOMIC);
1432 if (reply == NULL)
1433 return NULL;
1434
1435 reply->protocol = htons(ETH_P_IPV6);
1436 reply->dev = dev;
1437 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1438 skb_push(reply, sizeof(struct ethhdr));
1439 skb_set_mac_header(reply, 0);
1440
1441 ns = (struct nd_msg *)skb_transport_header(request);
1442
1443 daddr = eth_hdr(request)->h_source;
1444 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1445 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1446 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1447 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1448 break;
1449 }
1450 }
1451
1452 /* Ethernet header */
1453 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1454 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1455 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1456 reply->protocol = htons(ETH_P_IPV6);
1457
1458 skb_pull(reply, sizeof(struct ethhdr));
1459 skb_set_network_header(reply, 0);
1460 skb_put(reply, sizeof(struct ipv6hdr));
1461
1462 /* IPv6 header */
1463
1464 pip6 = ipv6_hdr(reply);
1465 memset(pip6, 0, sizeof(struct ipv6hdr));
1466 pip6->version = 6;
1467 pip6->priority = ipv6_hdr(request)->priority;
1468 pip6->nexthdr = IPPROTO_ICMPV6;
1469 pip6->hop_limit = 255;
1470 pip6->daddr = ipv6_hdr(request)->saddr;
1471 pip6->saddr = *(struct in6_addr *)n->primary_key;
1472
1473 skb_pull(reply, sizeof(struct ipv6hdr));
1474 skb_set_transport_header(reply, 0);
1475
1476 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1477
1478 /* Neighbor Advertisement */
1479 memset(na, 0, sizeof(*na)+na_olen);
1480 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1481 na->icmph.icmp6_router = isrouter;
1482 na->icmph.icmp6_override = 1;
1483 na->icmph.icmp6_solicited = 1;
1484 na->target = ns->target;
1485 ether_addr_copy(&na->opt[2], n->ha);
1486 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1487 na->opt[1] = na_olen >> 3;
1488
1489 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1490 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1491 csum_partial(na, sizeof(*na)+na_olen, 0));
1492
1493 pip6->payload_len = htons(sizeof(*na)+na_olen);
1494
1495 skb_push(reply, sizeof(struct ipv6hdr));
1496
1497 reply->ip_summed = CHECKSUM_UNNECESSARY;
1498
1499 return reply;
1500}
1501
Cong Wangf564f452013-08-31 13:44:36 +08001502static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1503{
1504 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens4b29dba2014-03-24 10:39:58 -04001505 struct nd_msg *msg;
Cong Wangf564f452013-08-31 13:44:36 +08001506 const struct ipv6hdr *iphdr;
1507 const struct in6_addr *saddr, *daddr;
David Stevens4b29dba2014-03-24 10:39:58 -04001508 struct neighbour *n;
1509 struct inet6_dev *in6_dev;
Cong Wangf564f452013-08-31 13:44:36 +08001510
1511 in6_dev = __in6_dev_get(dev);
1512 if (!in6_dev)
1513 goto out;
1514
Cong Wangf564f452013-08-31 13:44:36 +08001515 iphdr = ipv6_hdr(skb);
1516 saddr = &iphdr->saddr;
1517 daddr = &iphdr->daddr;
1518
Cong Wangf564f452013-08-31 13:44:36 +08001519 msg = (struct nd_msg *)skb_transport_header(skb);
1520 if (msg->icmph.icmp6_code != 0 ||
1521 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1522 goto out;
1523
David Stevens4b29dba2014-03-24 10:39:58 -04001524 if (ipv6_addr_loopback(daddr) ||
1525 ipv6_addr_is_multicast(&msg->target))
1526 goto out;
1527
1528 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
Cong Wangf564f452013-08-31 13:44:36 +08001529
1530 if (n) {
1531 struct vxlan_fdb *f;
David Stevens4b29dba2014-03-24 10:39:58 -04001532 struct sk_buff *reply;
Cong Wangf564f452013-08-31 13:44:36 +08001533
1534 if (!(n->nud_state & NUD_CONNECTED)) {
1535 neigh_release(n);
1536 goto out;
1537 }
1538
1539 f = vxlan_find_mac(vxlan, n->ha);
1540 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1541 /* bridge-local neighbor */
1542 neigh_release(n);
1543 goto out;
1544 }
1545
David Stevens4b29dba2014-03-24 10:39:58 -04001546 reply = vxlan_na_create(skb, n,
1547 !!(f ? f->flags & NTF_ROUTER : 0));
1548
Cong Wangf564f452013-08-31 13:44:36 +08001549 neigh_release(n);
David Stevens4b29dba2014-03-24 10:39:58 -04001550
1551 if (reply == NULL)
1552 goto out;
1553
1554 if (netif_rx_ni(reply) == NET_RX_DROP)
1555 dev->stats.rx_dropped++;
1556
Cong Wangf564f452013-08-31 13:44:36 +08001557 } else if (vxlan->flags & VXLAN_F_L3MISS) {
David Stevens4b29dba2014-03-24 10:39:58 -04001558 union vxlan_addr ipa = {
1559 .sin6.sin6_addr = msg->target,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001560 .sin6.sin6_family = AF_INET6,
David Stevens4b29dba2014-03-24 10:39:58 -04001561 };
1562
Cong Wangf564f452013-08-31 13:44:36 +08001563 vxlan_ip_miss(dev, &ipa);
1564 }
1565
1566out:
1567 consume_skb(skb);
1568 return NETDEV_TX_OK;
1569}
1570#endif
1571
David Stevense4f67ad2012-11-20 02:50:14 +00001572static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1573{
1574 struct vxlan_dev *vxlan = netdev_priv(dev);
1575 struct neighbour *n;
David Stevense4f67ad2012-11-20 02:50:14 +00001576
1577 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1578 return false;
1579
1580 n = NULL;
1581 switch (ntohs(eth_hdr(skb)->h_proto)) {
1582 case ETH_P_IP:
Cong Wange15a00a2013-08-31 13:44:34 +08001583 {
1584 struct iphdr *pip;
1585
David Stevense4f67ad2012-11-20 02:50:14 +00001586 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1587 return false;
1588 pip = ip_hdr(skb);
1589 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001590 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1591 union vxlan_addr ipa = {
1592 .sin.sin_addr.s_addr = pip->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001593 .sin.sin_family = AF_INET,
Cong Wange4c7ed42013-08-31 13:44:33 +08001594 };
1595
1596 vxlan_ip_miss(dev, &ipa);
1597 return false;
1598 }
1599
David Stevense4f67ad2012-11-20 02:50:14 +00001600 break;
Cong Wange15a00a2013-08-31 13:44:34 +08001601 }
1602#if IS_ENABLED(CONFIG_IPV6)
1603 case ETH_P_IPV6:
1604 {
1605 struct ipv6hdr *pip6;
1606
1607 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1608 return false;
1609 pip6 = ipv6_hdr(skb);
1610 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1611 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1612 union vxlan_addr ipa = {
1613 .sin6.sin6_addr = pip6->daddr,
Gerhard Stenzela45e92a2014-08-22 21:34:16 +02001614 .sin6.sin6_family = AF_INET6,
Cong Wange15a00a2013-08-31 13:44:34 +08001615 };
1616
1617 vxlan_ip_miss(dev, &ipa);
1618 return false;
1619 }
1620
1621 break;
1622 }
1623#endif
David Stevense4f67ad2012-11-20 02:50:14 +00001624 default:
1625 return false;
1626 }
1627
1628 if (n) {
1629 bool diff;
1630
Joe Perches7367d0b2013-09-01 11:51:23 -07001631 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
David Stevense4f67ad2012-11-20 02:50:14 +00001632 if (diff) {
1633 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1634 dev->addr_len);
1635 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1636 }
1637 neigh_release(n);
1638 return diff;
Cong Wange4c7ed42013-08-31 13:44:33 +08001639 }
1640
David Stevense4f67ad2012-11-20 02:50:14 +00001641 return false;
1642}
1643
Cong Wange4c7ed42013-08-31 13:44:33 +08001644#if IS_ENABLED(CONFIG_IPV6)
Nicolas Dichtel11796182013-09-02 15:34:55 +02001645static int vxlan6_xmit_skb(struct vxlan_sock *vs,
Cong Wange4c7ed42013-08-31 13:44:33 +08001646 struct dst_entry *dst, struct sk_buff *skb,
1647 struct net_device *dev, struct in6_addr *saddr,
1648 struct in6_addr *daddr, __u8 prio, __u8 ttl,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001649 __be16 src_port, __be16 dst_port, __be32 vni,
1650 bool xnet)
Cong Wange4c7ed42013-08-31 13:44:33 +08001651{
Cong Wange4c7ed42013-08-31 13:44:33 +08001652 struct vxlanhdr *vxh;
Cong Wange4c7ed42013-08-31 13:44:33 +08001653 int min_headroom;
1654 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001655 bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk);
Tom Herbertdfd86452015-01-12 17:00:38 -08001656 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1657 u16 hdrlen = sizeof(struct vxlanhdr);
Cong Wange4c7ed42013-08-31 13:44:33 +08001658
Tom Herbertdfd86452015-01-12 17:00:38 -08001659 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1660 skb->ip_summed == CHECKSUM_PARTIAL) {
1661 int csum_start = skb_checksum_start_offset(skb);
1662
1663 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1664 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1665 (skb->csum_offset == offsetof(struct udphdr, check) ||
1666 skb->csum_offset == offsetof(struct tcphdr, check))) {
1667 udp_sum = false;
1668 type |= SKB_GSO_TUNNEL_REMCSUM;
1669 }
1670 }
1671
1672 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001673 if (IS_ERR(skb)) {
1674 err = -EINVAL;
1675 goto err;
1676 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001677
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001678 skb_scrub_packet(skb, xnet);
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001679
Cong Wange4c7ed42013-08-31 13:44:33 +08001680 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1681 + VXLAN_HLEN + sizeof(struct ipv6hdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001682 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Cong Wange4c7ed42013-08-31 13:44:33 +08001683
1684 /* Need space for new headers (invalidates iph ptr) */
1685 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001686 if (unlikely(err)) {
1687 kfree_skb(skb);
1688 goto err;
1689 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001690
Jiri Pirko59682502014-11-19 14:04:59 +01001691 skb = vlan_hwaccel_push_inside(skb);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001692 if (WARN_ON(!skb)) {
1693 err = -ENOMEM;
1694 goto err;
1695 }
Cong Wange4c7ed42013-08-31 13:44:33 +08001696
1697 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001698 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Cong Wange4c7ed42013-08-31 13:44:33 +08001699 vxh->vx_vni = vni;
1700
Tom Herbertdfd86452015-01-12 17:00:38 -08001701 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1702 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1703 VXLAN_RCO_SHIFT;
1704
1705 if (skb->csum_offset == offsetof(struct udphdr, check))
1706 data |= VXLAN_RCO_UDP;
1707
1708 vxh->vx_vni |= htonl(data);
1709 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1710
1711 if (!skb_is_gso(skb)) {
1712 skb->ip_summed = CHECKSUM_NONE;
1713 skb->encapsulation = 0;
1714 }
1715 }
1716
Tom Herbert996c9fd2014-09-29 20:22:33 -07001717 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1718
Andy Zhouacbf74a2014-09-16 17:31:18 -07001719 udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio,
1720 ttl, src_port, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001721 return 0;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001722err:
1723 dst_release(dst);
1724 return err;
Cong Wange4c7ed42013-08-31 13:44:33 +08001725}
1726#endif
1727
Nicolas Dichtel11796182013-09-02 15:34:55 +02001728int vxlan_xmit_skb(struct vxlan_sock *vs,
Pravin B Shelar49560532013-08-19 11:23:17 -07001729 struct rtable *rt, struct sk_buff *skb,
1730 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001731 __be16 src_port, __be16 dst_port, __be32 vni, bool xnet)
Pravin B Shelar49560532013-08-19 11:23:17 -07001732{
1733 struct vxlanhdr *vxh;
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001734 int min_headroom;
Pravin B Shelar49560532013-08-19 11:23:17 -07001735 int err;
Andy Zhouacbf74a2014-09-16 17:31:18 -07001736 bool udp_sum = !vs->sock->sk->sk_no_check_tx;
Tom Herbertdfd86452015-01-12 17:00:38 -08001737 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1738 u16 hdrlen = sizeof(struct vxlanhdr);
Pravin B Shelar49560532013-08-19 11:23:17 -07001739
Tom Herbertdfd86452015-01-12 17:00:38 -08001740 if ((vs->flags & VXLAN_F_REMCSUM_TX) &&
1741 skb->ip_summed == CHECKSUM_PARTIAL) {
1742 int csum_start = skb_checksum_start_offset(skb);
1743
1744 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1745 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1746 (skb->csum_offset == offsetof(struct udphdr, check) ||
1747 skb->csum_offset == offsetof(struct tcphdr, check))) {
1748 udp_sum = false;
1749 type |= SKB_GSO_TUNNEL_REMCSUM;
1750 }
1751 }
1752
1753 skb = iptunnel_handle_offloads(skb, udp_sum, type);
Tom Herbert359a0ea2014-06-04 17:20:29 -07001754 if (IS_ERR(skb))
Pravin B Shelar74f47272014-12-23 16:20:36 -08001755 return PTR_ERR(skb);
Pravin B Shelar49560532013-08-19 11:23:17 -07001756
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001757 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001758 + VXLAN_HLEN + sizeof(struct iphdr)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001759 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001760
1761 /* Need space for new headers (invalidates iph ptr) */
1762 err = skb_cow_head(skb, min_headroom);
Pravin B Shelar74f47272014-12-23 16:20:36 -08001763 if (unlikely(err)) {
1764 kfree_skb(skb);
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001765 return err;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001766 }
Pravin B Shelar649c5b82013-08-19 11:23:22 -07001767
Jiri Pirko59682502014-11-19 14:04:59 +01001768 skb = vlan_hwaccel_push_inside(skb);
1769 if (WARN_ON(!skb))
1770 return -ENOMEM;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07001771
Pravin B Shelar49560532013-08-19 11:23:17 -07001772 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
Tom Herbert3bf39472015-01-08 12:31:18 -08001773 vxh->vx_flags = htonl(VXLAN_HF_VNI);
Pravin B Shelar49560532013-08-19 11:23:17 -07001774 vxh->vx_vni = vni;
1775
Tom Herbertdfd86452015-01-12 17:00:38 -08001776 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1777 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1778 VXLAN_RCO_SHIFT;
1779
1780 if (skb->csum_offset == offsetof(struct udphdr, check))
1781 data |= VXLAN_RCO_UDP;
1782
1783 vxh->vx_vni |= htonl(data);
1784 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1785
1786 if (!skb_is_gso(skb)) {
1787 skb->ip_summed = CHECKSUM_NONE;
1788 skb->encapsulation = 0;
1789 }
1790 }
1791
Tom Herbert996c9fd2014-09-29 20:22:33 -07001792 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1793
Andy Zhouacbf74a2014-09-16 17:31:18 -07001794 return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos,
1795 ttl, df, src_port, dst_port, xnet);
Pravin B Shelar49560532013-08-19 11:23:17 -07001796}
1797EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1798
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001799/* Bypass encapsulation if the destination is local */
1800static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1801 struct vxlan_dev *dst_vxlan)
1802{
Li RongQing8f849852014-01-04 13:57:59 +08001803 struct pcpu_sw_netstats *tx_stats, *rx_stats;
Cong Wange4c7ed42013-08-31 13:44:33 +08001804 union vxlan_addr loopback;
1805 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
Li RongQingce6502a2014-10-16 08:49:41 +08001806 struct net_device *dev = skb->dev;
1807 int len = skb->len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001808
Li RongQing8f849852014-01-04 13:57:59 +08001809 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1810 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001811 skb->pkt_type = PACKET_HOST;
1812 skb->encapsulation = 0;
1813 skb->dev = dst_vxlan->dev;
1814 __skb_pull(skb, skb_network_offset(skb));
1815
Cong Wange4c7ed42013-08-31 13:44:33 +08001816 if (remote_ip->sa.sa_family == AF_INET) {
1817 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1818 loopback.sa.sa_family = AF_INET;
1819#if IS_ENABLED(CONFIG_IPV6)
1820 } else {
1821 loopback.sin6.sin6_addr = in6addr_loopback;
1822 loopback.sa.sa_family = AF_INET6;
1823#endif
1824 }
1825
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001826 if (dst_vxlan->flags & VXLAN_F_LEARN)
Cong Wange4c7ed42013-08-31 13:44:33 +08001827 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001828
1829 u64_stats_update_begin(&tx_stats->syncp);
1830 tx_stats->tx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001831 tx_stats->tx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001832 u64_stats_update_end(&tx_stats->syncp);
1833
1834 if (netif_rx(skb) == NET_RX_SUCCESS) {
1835 u64_stats_update_begin(&rx_stats->syncp);
1836 rx_stats->rx_packets++;
Li RongQingce6502a2014-10-16 08:49:41 +08001837 rx_stats->rx_bytes += len;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001838 u64_stats_update_end(&rx_stats->syncp);
1839 } else {
Li RongQingce6502a2014-10-16 08:49:41 +08001840 dev->stats.rx_dropped++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001841 }
1842}
1843
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001844static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1845 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001846{
1847 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08001848 struct rtable *rt = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +00001849 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001850 struct flowi4 fl4;
Cong Wange4c7ed42013-08-31 13:44:33 +08001851 union vxlan_addr *dst;
1852 __be16 src_port = 0, dst_port;
Stephen Hemminger234f5b72013-06-17 14:16:41 -07001853 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001854 __be16 df = 0;
1855 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001856 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001857
stephen hemminger823aa872013-04-27 11:31:57 +00001858 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001859 vni = rdst->remote_vni;
Cong Wange4c7ed42013-08-31 13:44:33 +08001860 dst = &rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001861
Cong Wange4c7ed42013-08-31 13:44:33 +08001862 if (vxlan_addr_any(dst)) {
David Stevense4f67ad2012-11-20 02:50:14 +00001863 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001864 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001865 vxlan_encap_bypass(skb, vxlan, vxlan);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001866 return;
David Stevense4f67ad2012-11-20 02:50:14 +00001867 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001868 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001869 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001870
stephen hemmingerd3428942012-10-01 12:32:35 +00001871 old_iph = ip_hdr(skb);
1872
stephen hemmingerd3428942012-10-01 12:32:35 +00001873 ttl = vxlan->ttl;
Cong Wange4c7ed42013-08-31 13:44:33 +08001874 if (!ttl && vxlan_addr_multicast(dst))
stephen hemmingerd3428942012-10-01 12:32:35 +00001875 ttl = 1;
1876
1877 tos = vxlan->tos;
1878 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001879 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001880
Tom Herbert535fb8d2014-07-01 21:32:49 -07001881 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1882 vxlan->port_max, true);
stephen hemmingerd3428942012-10-01 12:32:35 +00001883
Cong Wange4c7ed42013-08-31 13:44:33 +08001884 if (dst->sa.sa_family == AF_INET) {
1885 memset(&fl4, 0, sizeof(fl4));
1886 fl4.flowi4_oif = rdst->remote_ifindex;
1887 fl4.flowi4_tos = RT_TOS(tos);
1888 fl4.daddr = dst->sin.sin_addr.s_addr;
1889 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
stephen hemmingerca78f182012-10-09 20:35:48 +00001890
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001891 rt = ip_route_output_key(vxlan->net, &fl4);
Cong Wange4c7ed42013-08-31 13:44:33 +08001892 if (IS_ERR(rt)) {
1893 netdev_dbg(dev, "no route to %pI4\n",
1894 &dst->sin.sin_addr.s_addr);
1895 dev->stats.tx_carrier_errors++;
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001896 goto tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001897 }
1898
1899 if (rt->dst.dev == dev) {
1900 netdev_dbg(dev, "circular route to %pI4\n",
1901 &dst->sin.sin_addr.s_addr);
1902 dev->stats.collisions++;
Fan Dufffc15a2013-12-09 10:33:53 +08001903 goto rt_tx_error;
Cong Wange4c7ed42013-08-31 13:44:33 +08001904 }
1905
1906 /* Bypass encapsulation if the destination is local */
1907 if (rt->rt_flags & RTCF_LOCAL &&
1908 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1909 struct vxlan_dev *dst_vxlan;
1910
1911 ip_rt_put(rt);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001912 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1913 dst->sa.sa_family, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001914 if (!dst_vxlan)
1915 goto tx_error;
1916 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1917 return;
1918 }
1919
1920 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1921 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1922
Andy Zhou3c4d1da2014-09-23 01:44:51 -07001923 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
1924 fl4.saddr, dst->sin.sin_addr.s_addr,
1925 tos, ttl, df, src_port, dst_port,
1926 htonl(vni << 8),
1927 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Pravin B Shelar74f47272014-12-23 16:20:36 -08001928 if (err < 0) {
1929 /* skb is already freed. */
1930 skb = NULL;
Cong Wange4c7ed42013-08-31 13:44:33 +08001931 goto rt_tx_error;
Pravin B Shelar74f47272014-12-23 16:20:36 -08001932 }
1933
Cong Wange4c7ed42013-08-31 13:44:33 +08001934 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1935#if IS_ENABLED(CONFIG_IPV6)
1936 } else {
1937 struct sock *sk = vxlan->vn_sock->sock->sk;
1938 struct dst_entry *ndst;
1939 struct flowi6 fl6;
1940 u32 flags;
1941
1942 memset(&fl6, 0, sizeof(fl6));
1943 fl6.flowi6_oif = rdst->remote_ifindex;
1944 fl6.daddr = dst->sin6.sin6_addr;
1945 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
Cong Wang8c1bb792013-09-02 10:06:51 +08001946 fl6.flowi6_proto = IPPROTO_UDP;
Cong Wange4c7ed42013-08-31 13:44:33 +08001947
1948 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1949 netdev_dbg(dev, "no route to %pI6\n",
1950 &dst->sin6.sin6_addr);
1951 dev->stats.tx_carrier_errors++;
1952 goto tx_error;
1953 }
1954
1955 if (ndst->dev == dev) {
1956 netdev_dbg(dev, "circular route to %pI6\n",
1957 &dst->sin6.sin6_addr);
1958 dst_release(ndst);
1959 dev->stats.collisions++;
1960 goto tx_error;
1961 }
1962
1963 /* Bypass encapsulation if the destination is local */
1964 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1965 if (flags & RTF_LOCAL &&
1966 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1967 struct vxlan_dev *dst_vxlan;
1968
1969 dst_release(ndst);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02001970 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1971 dst->sa.sa_family, dst_port);
Cong Wange4c7ed42013-08-31 13:44:33 +08001972 if (!dst_vxlan)
1973 goto tx_error;
1974 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1975 return;
1976 }
1977
1978 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1979
Nicolas Dichtel11796182013-09-02 15:34:55 +02001980 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
Cong Wange4c7ed42013-08-31 13:44:33 +08001981 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02001982 src_port, dst_port, htonl(vni << 8),
1983 !net_eq(vxlan->net, dev_net(vxlan->dev)));
Cong Wange4c7ed42013-08-31 13:44:33 +08001984#endif
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001985 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001986
Stephen Hemminger4ad16932013-06-17 14:16:11 -07001987 return;
stephen hemmingerd3428942012-10-01 12:32:35 +00001988
1989drop:
1990 dev->stats.tx_dropped++;
1991 goto tx_free;
1992
Pravin B Shelar49560532013-08-19 11:23:17 -07001993rt_tx_error:
1994 ip_rt_put(rt);
stephen hemmingerd3428942012-10-01 12:32:35 +00001995tx_error:
1996 dev->stats.tx_errors++;
1997tx_free:
1998 dev_kfree_skb(skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001999}
2000
David Stevens66817122013-03-15 04:35:51 +00002001/* Transmit local packets over Vxlan
2002 *
2003 * Outer IP header inherits ECN and DF from inner header.
2004 * Outer UDP destination is the VXLAN assigned port.
2005 * source port is based on hash of flow
2006 */
2007static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2008{
2009 struct vxlan_dev *vxlan = netdev_priv(dev);
2010 struct ethhdr *eth;
2011 bool did_rsc = false;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002012 struct vxlan_rdst *rdst, *fdst = NULL;
David Stevens66817122013-03-15 04:35:51 +00002013 struct vxlan_fdb *f;
David Stevens66817122013-03-15 04:35:51 +00002014
2015 skb_reset_mac_header(skb);
2016 eth = eth_hdr(skb);
2017
Cong Wangf564f452013-08-31 13:44:36 +08002018 if ((vxlan->flags & VXLAN_F_PROXY)) {
2019 if (ntohs(eth->h_proto) == ETH_P_ARP)
2020 return arp_reduce(dev, skb);
2021#if IS_ENABLED(CONFIG_IPV6)
2022 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
Li RongQing91269e32014-10-16 09:17:18 +08002023 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2024 + sizeof(struct nd_msg)) &&
Cong Wangf564f452013-08-31 13:44:36 +08002025 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2026 struct nd_msg *msg;
2027
2028 msg = (struct nd_msg *)skb_transport_header(skb);
2029 if (msg->icmph.icmp6_code == 0 &&
2030 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2031 return neigh_reduce(dev, skb);
2032 }
Li RongQing7a9f5262014-10-17 14:06:16 +08002033 eth = eth_hdr(skb);
Cong Wangf564f452013-08-31 13:44:36 +08002034#endif
2035 }
David Stevens66817122013-03-15 04:35:51 +00002036
2037 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00002038 did_rsc = false;
2039
2040 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
Cong Wange15a00a2013-08-31 13:44:34 +08002041 (ntohs(eth->h_proto) == ETH_P_IP ||
2042 ntohs(eth->h_proto) == ETH_P_IPV6)) {
David Stevensae884082013-04-19 00:36:26 +00002043 did_rsc = route_shortcircuit(dev, skb);
2044 if (did_rsc)
2045 f = vxlan_find_mac(vxlan, eth->h_dest);
2046 }
2047
David Stevens66817122013-03-15 04:35:51 +00002048 if (f == NULL) {
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002049 f = vxlan_find_mac(vxlan, all_zeros_mac);
2050 if (f == NULL) {
2051 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2052 !is_multicast_ether_addr(eth->h_dest))
2053 vxlan_fdb_miss(vxlan, eth->h_dest);
David Stevens66817122013-03-15 04:35:51 +00002054
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002055 dev->stats.tx_dropped++;
Eric Dumazet8f646c92014-01-06 09:54:31 -08002056 kfree_skb(skb);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002057 return NETDEV_TX_OK;
Stephen Hemminger3e61aa82013-06-17 14:16:12 -07002058 }
David Stevens66817122013-03-15 04:35:51 +00002059 }
2060
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002061 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2062 struct sk_buff *skb1;
2063
Eric Dumazet8f646c92014-01-06 09:54:31 -08002064 if (!fdst) {
2065 fdst = rdst;
2066 continue;
2067 }
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002068 skb1 = skb_clone(skb, GFP_ATOMIC);
2069 if (skb1)
2070 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2071 }
2072
Eric Dumazet8f646c92014-01-06 09:54:31 -08002073 if (fdst)
2074 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2075 else
2076 kfree_skb(skb);
Stephen Hemminger4ad16932013-06-17 14:16:11 -07002077 return NETDEV_TX_OK;
David Stevens66817122013-03-15 04:35:51 +00002078}
2079
stephen hemmingerd3428942012-10-01 12:32:35 +00002080/* Walk the forwarding table and purge stale entries */
2081static void vxlan_cleanup(unsigned long arg)
2082{
2083 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2084 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2085 unsigned int h;
2086
2087 if (!netif_running(vxlan->dev))
2088 return;
2089
2090 spin_lock_bh(&vxlan->hash_lock);
2091 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2092 struct hlist_node *p, *n;
2093 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2094 struct vxlan_fdb *f
2095 = container_of(p, struct vxlan_fdb, hlist);
2096 unsigned long timeout;
2097
stephen hemminger3c172862012-10-26 06:24:34 +00002098 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00002099 continue;
2100
2101 timeout = f->used + vxlan->age_interval * HZ;
2102 if (time_before_eq(timeout, jiffies)) {
2103 netdev_dbg(vxlan->dev,
2104 "garbage collect %pM\n",
2105 f->eth_addr);
2106 f->state = NUD_STALE;
2107 vxlan_fdb_destroy(vxlan, f);
2108 } else if (time_before(timeout, next_timer))
2109 next_timer = timeout;
2110 }
2111 }
2112 spin_unlock_bh(&vxlan->hash_lock);
2113
2114 mod_timer(&vxlan->age_timer, next_timer);
2115}
2116
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002117static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2118{
2119 __u32 vni = vxlan->default_dst.remote_vni;
2120
2121 vxlan->vn_sock = vs;
2122 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2123}
2124
stephen hemmingerd3428942012-10-01 12:32:35 +00002125/* Setup stats when device is created */
2126static int vxlan_init(struct net_device *dev)
2127{
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002128 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002129 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002130 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002131 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002132
WANG Cong1c213bd2014-02-13 11:46:28 -08002133 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Pravin B Shelare8171042013-03-25 14:49:46 +00002134 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00002135 return -ENOMEM;
2136
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002137 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002138 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2139 vxlan->dst_port);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002140 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002141 /* If we have a socket with same port already, reuse it */
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002142 vxlan_vs_add_dev(vs, vxlan);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002143 } else {
2144 /* otherwise make new socket outside of RTNL */
2145 dev_hold(dev);
2146 queue_work(vxlan_wq, &vxlan->sock_work);
2147 }
2148 spin_unlock(&vn->sock_lock);
2149
stephen hemmingerd3428942012-10-01 12:32:35 +00002150 return 0;
2151}
2152
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002153static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002154{
2155 struct vxlan_fdb *f;
2156
2157 spin_lock_bh(&vxlan->hash_lock);
2158 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2159 if (f)
2160 vxlan_fdb_destroy(vxlan, f);
2161 spin_unlock_bh(&vxlan->hash_lock);
2162}
2163
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002164static void vxlan_uninit(struct net_device *dev)
2165{
2166 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002167 struct vxlan_sock *vs = vxlan->vn_sock;
2168
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002169 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002170
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002171 if (vs)
Pravin B Shelar012a5722013-08-19 11:23:07 -07002172 vxlan_sock_release(vs);
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002173 free_percpu(dev->tstats);
2174}
2175
stephen hemmingerd3428942012-10-01 12:32:35 +00002176/* Start ageing timer and join group when device is brought up */
2177static int vxlan_open(struct net_device *dev)
2178{
2179 struct vxlan_dev *vxlan = netdev_priv(dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002180 struct vxlan_sock *vs = vxlan->vn_sock;
2181
2182 /* socket hasn't been created */
2183 if (!vs)
2184 return -ENOTCONN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002185
Gao feng79d4a942013-12-10 16:37:32 +08002186 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002187 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002188 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002189 queue_work(vxlan_wq, &vxlan->igmp_join);
stephen hemmingerd3428942012-10-01 12:32:35 +00002190 }
2191
2192 if (vxlan->age_interval)
2193 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2194
2195 return 0;
2196}
2197
2198/* Purge the forwarding table */
2199static void vxlan_flush(struct vxlan_dev *vxlan)
2200{
Cong Wang31fec5a2013-05-27 22:35:52 +00002201 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002202
2203 spin_lock_bh(&vxlan->hash_lock);
2204 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2205 struct hlist_node *p, *n;
2206 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2207 struct vxlan_fdb *f
2208 = container_of(p, struct vxlan_fdb, hlist);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002209 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2210 if (!is_zero_ether_addr(f->eth_addr))
2211 vxlan_fdb_destroy(vxlan, f);
stephen hemmingerd3428942012-10-01 12:32:35 +00002212 }
2213 }
2214 spin_unlock_bh(&vxlan->hash_lock);
2215}
2216
2217/* Cleanup timer and forwarding table on shutdown */
2218static int vxlan_stop(struct net_device *dev)
2219{
2220 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002221 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002222 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00002223
Cong Wange4c7ed42013-08-31 13:44:33 +08002224 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
Gao feng95ab0992013-12-10 16:37:33 +08002225 !vxlan_group_used(vn, vxlan)) {
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002226 vxlan_sock_hold(vs);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002227 dev_hold(dev);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002228 queue_work(vxlan_wq, &vxlan->igmp_leave);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07002229 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002230
2231 del_timer_sync(&vxlan->age_timer);
2232
2233 vxlan_flush(vxlan);
2234
2235 return 0;
2236}
2237
stephen hemmingerd3428942012-10-01 12:32:35 +00002238/* Stub, nothing needs to be done. */
2239static void vxlan_set_multicast_list(struct net_device *dev)
2240{
2241}
2242
Daniel Borkmann345010b2013-12-18 00:21:08 +01002243static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2244{
2245 struct vxlan_dev *vxlan = netdev_priv(dev);
2246 struct vxlan_rdst *dst = &vxlan->default_dst;
2247 struct net_device *lowerdev;
2248 int max_mtu;
2249
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002250 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
Daniel Borkmann345010b2013-12-18 00:21:08 +01002251 if (lowerdev == NULL)
2252 return eth_change_mtu(dev, new_mtu);
2253
2254 if (dst->remote_ip.sa.sa_family == AF_INET6)
2255 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2256 else
2257 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2258
2259 if (new_mtu < 68 || new_mtu > max_mtu)
2260 return -EINVAL;
2261
2262 dev->mtu = new_mtu;
2263 return 0;
2264}
2265
stephen hemmingerd3428942012-10-01 12:32:35 +00002266static const struct net_device_ops vxlan_netdev_ops = {
2267 .ndo_init = vxlan_init,
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002268 .ndo_uninit = vxlan_uninit,
stephen hemmingerd3428942012-10-01 12:32:35 +00002269 .ndo_open = vxlan_open,
2270 .ndo_stop = vxlan_stop,
2271 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00002272 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00002273 .ndo_set_rx_mode = vxlan_set_multicast_list,
Daniel Borkmann345010b2013-12-18 00:21:08 +01002274 .ndo_change_mtu = vxlan_change_mtu,
stephen hemmingerd3428942012-10-01 12:32:35 +00002275 .ndo_validate_addr = eth_validate_addr,
2276 .ndo_set_mac_address = eth_mac_addr,
2277 .ndo_fdb_add = vxlan_fdb_add,
2278 .ndo_fdb_del = vxlan_fdb_delete,
2279 .ndo_fdb_dump = vxlan_fdb_dump,
2280};
2281
2282/* Info for udev, that this is a virtual tunnel endpoint */
2283static struct device_type vxlan_type = {
2284 .name = "vxlan",
2285};
2286
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002287/* Calls the ndo_add_vxlan_port of the caller in order to
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002288 * supply the listening VXLAN udp ports. Callers are expected
2289 * to implement the ndo_add_vxlan_port.
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002290 */
2291void vxlan_get_rx_port(struct net_device *dev)
2292{
2293 struct vxlan_sock *vs;
2294 struct net *net = dev_net(dev);
2295 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2296 sa_family_t sa_family;
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002297 __be16 port;
2298 unsigned int i;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002299
2300 spin_lock(&vn->sock_lock);
2301 for (i = 0; i < PORT_HASH_SIZE; ++i) {
Joseph Gasparakis35e42372013-09-13 07:34:13 -07002302 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2303 port = inet_sk(vs->sock->sk)->inet_sport;
Joseph Gasparakis53cf52752013-09-04 02:13:38 -07002304 sa_family = vs->sock->sk->sk_family;
2305 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2306 port);
2307 }
2308 }
2309 spin_unlock(&vn->sock_lock);
2310}
2311EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2312
stephen hemmingerd3428942012-10-01 12:32:35 +00002313/* Initialize the device structure. */
2314static void vxlan_setup(struct net_device *dev)
2315{
2316 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00002317 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002318
2319 eth_hw_addr_random(dev);
2320 ether_setup(dev);
Cong Wange4c7ed42013-08-31 13:44:33 +08002321 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
Cong Wang2853af62014-06-12 11:53:10 -07002322 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
Cong Wange4c7ed42013-08-31 13:44:33 +08002323 else
Cong Wang2853af62014-06-12 11:53:10 -07002324 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00002325
2326 dev->netdev_ops = &vxlan_netdev_ops;
Stephen Hemmingerebf40632013-06-17 14:16:11 -07002327 dev->destructor = free_netdev;
stephen hemmingerd3428942012-10-01 12:32:35 +00002328 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2329
2330 dev->tx_queue_len = 0;
2331 dev->features |= NETIF_F_LLTX;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00002332 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002333 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002334 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002335
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002336 dev->vlan_features = dev->features;
2337 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00002338 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00002339 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Pravin B Shelar1eaa8172013-08-19 11:23:29 -07002340 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Eric Dumazet02875872014-10-05 18:38:35 -07002341 netif_keep_dst(dev);
stephen hemminger6602d002012-12-31 12:00:21 +00002342 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00002343
stephen hemminger553675f2013-05-16 11:35:20 +00002344 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002345 spin_lock_init(&vxlan->hash_lock);
stephen hemminger3fc2de22013-07-18 08:40:15 -07002346 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2347 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002348 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00002349
2350 init_timer_deferrable(&vxlan->age_timer);
2351 vxlan->age_timer.function = vxlan_cleanup;
2352 vxlan->age_timer.data = (unsigned long) vxlan;
2353
stephen hemminger823aa872013-04-27 11:31:57 +00002354 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00002355
stephen hemmingerd3428942012-10-01 12:32:35 +00002356 vxlan->dev = dev;
2357
2358 for (h = 0; h < FDB_HASH_SIZE; ++h)
2359 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2360}
2361
2362static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2363 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00002364 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002365 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002366 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2367 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
Cong Wange4c7ed42013-08-31 13:44:33 +08002368 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00002369 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2370 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2371 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2372 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2373 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00002374 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00002375 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2376 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2377 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2378 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00002379 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
Tom Herbert5c91ae02014-11-06 18:06:01 -08002380 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2381 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2382 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
Tom Herbertdfd86452015-01-12 17:00:38 -08002383 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2384 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
stephen hemmingerd3428942012-10-01 12:32:35 +00002385};
2386
2387static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2388{
2389 if (tb[IFLA_ADDRESS]) {
2390 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2391 pr_debug("invalid link address (not ethernet)\n");
2392 return -EINVAL;
2393 }
2394
2395 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2396 pr_debug("invalid all zero ethernet address\n");
2397 return -EADDRNOTAVAIL;
2398 }
2399 }
2400
2401 if (!data)
2402 return -EINVAL;
2403
2404 if (data[IFLA_VXLAN_ID]) {
2405 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2406 if (id >= VXLAN_VID_MASK)
2407 return -ERANGE;
2408 }
2409
stephen hemminger05f47d62012-10-09 20:35:50 +00002410 if (data[IFLA_VXLAN_PORT_RANGE]) {
2411 const struct ifla_vxlan_port_range *p
2412 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2413
2414 if (ntohs(p->high) < ntohs(p->low)) {
2415 pr_debug("port range %u .. %u not valid\n",
2416 ntohs(p->low), ntohs(p->high));
2417 return -EINVAL;
2418 }
2419 }
2420
stephen hemmingerd3428942012-10-01 12:32:35 +00002421 return 0;
2422}
2423
Yan Burman1b13c972013-01-29 23:43:07 +00002424static void vxlan_get_drvinfo(struct net_device *netdev,
2425 struct ethtool_drvinfo *drvinfo)
2426{
2427 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2428 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2429}
2430
2431static const struct ethtool_ops vxlan_ethtool_ops = {
2432 .get_drvinfo = vxlan_get_drvinfo,
2433 .get_link = ethtool_op_get_link,
2434};
2435
stephen hemminger553675f2013-05-16 11:35:20 +00002436static void vxlan_del_work(struct work_struct *work)
2437{
2438 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002439 udp_tunnel_sock_release(vs->sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002440 kfree_rcu(vs, rcu);
2441}
2442
Tom Herbert3ee64f32014-07-13 19:49:42 -07002443static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2444 __be16 port, u32 flags)
stephen hemminger553675f2013-05-16 11:35:20 +00002445{
Cong Wange4c7ed42013-08-31 13:44:33 +08002446 struct socket *sock;
Tom Herbert3ee64f32014-07-13 19:49:42 -07002447 struct udp_port_cfg udp_conf;
2448 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002449
Tom Herbert3ee64f32014-07-13 19:49:42 -07002450 memset(&udp_conf, 0, sizeof(udp_conf));
2451
2452 if (ipv6) {
2453 udp_conf.family = AF_INET6;
2454 udp_conf.use_udp6_tx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002455 !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002456 udp_conf.use_udp6_rx_checksums =
Alexander Duyck3dc2b6a2014-11-24 20:08:38 -08002457 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
Tom Herbert3ee64f32014-07-13 19:49:42 -07002458 } else {
2459 udp_conf.family = AF_INET;
2460 udp_conf.local_ip.s_addr = INADDR_ANY;
2461 udp_conf.use_udp_checksums =
2462 !!(flags & VXLAN_F_UDP_CSUM);
Cong Wange4c7ed42013-08-31 13:44:33 +08002463 }
2464
Tom Herbert3ee64f32014-07-13 19:49:42 -07002465 udp_conf.local_udp_port = port;
Cong Wange4c7ed42013-08-31 13:44:33 +08002466
Tom Herbert3ee64f32014-07-13 19:49:42 -07002467 /* Open UDP socket */
2468 err = udp_sock_create(net, &udp_conf, &sock);
2469 if (err < 0)
2470 return ERR_PTR(err);
Cong Wange4c7ed42013-08-31 13:44:33 +08002471
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002472 return sock;
Cong Wange4c7ed42013-08-31 13:44:33 +08002473}
2474
2475/* Create new listen socket if needed */
2476static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002477 vxlan_rcv_t *rcv, void *data,
2478 u32 flags)
Cong Wange4c7ed42013-08-31 13:44:33 +08002479{
2480 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2481 struct vxlan_sock *vs;
2482 struct socket *sock;
Cong Wang31fec5a2013-05-27 22:35:52 +00002483 unsigned int h;
Tom Herbert359a0ea2014-06-04 17:20:29 -07002484 bool ipv6 = !!(flags & VXLAN_F_IPV6);
Andy Zhouacbf74a2014-09-16 17:31:18 -07002485 struct udp_tunnel_sock_cfg tunnel_cfg;
stephen hemminger553675f2013-05-16 11:35:20 +00002486
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002487 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
Cong Wange4c7ed42013-08-31 13:44:33 +08002488 if (!vs)
stephen hemminger553675f2013-05-16 11:35:20 +00002489 return ERR_PTR(-ENOMEM);
2490
2491 for (h = 0; h < VNI_HASH_SIZE; ++h)
2492 INIT_HLIST_HEAD(&vs->vni_list[h]);
2493
2494 INIT_WORK(&vs->del_work, vxlan_del_work);
2495
Tom Herbert3ee64f32014-07-13 19:49:42 -07002496 sock = vxlan_create_sock(net, ipv6, port, flags);
Zhi Yong Wu39deb2c2013-10-28 14:01:48 +08002497 if (IS_ERR(sock)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002498 kfree(vs);
Duan Jionge50fddc2013-11-01 13:09:43 +08002499 return ERR_CAST(sock);
stephen hemminger553675f2013-05-16 11:35:20 +00002500 }
2501
Cong Wange4c7ed42013-08-31 13:44:33 +08002502 vs->sock = sock;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002503 atomic_set(&vs->refcnt, 1);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002504 vs->rcv = rcv;
Pravin B Shelar012a5722013-08-19 11:23:07 -07002505 vs->data = data;
Tom Herbertdfd86452015-01-12 17:00:38 -08002506 vs->flags = flags;
stephen hemminger553675f2013-05-16 11:35:20 +00002507
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002508 /* Initialize the vxlan udp offloads structure */
2509 vs->udp_offloads.port = port;
2510 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2511 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2512
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002513 spin_lock(&vn->sock_lock);
2514 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
Or Gerlitzdc01e7d2014-01-20 13:59:21 +02002515 vxlan_notify_add_rx_port(vs);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002516 spin_unlock(&vn->sock_lock);
stephen hemminger553675f2013-05-16 11:35:20 +00002517
2518 /* Mark socket as an encapsulation socket. */
Andy Zhouacbf74a2014-09-16 17:31:18 -07002519 tunnel_cfg.sk_user_data = vs;
2520 tunnel_cfg.encap_type = 1;
2521 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2522 tunnel_cfg.encap_destroy = NULL;
2523
2524 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Cong Wange4c7ed42013-08-31 13:44:33 +08002525
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002526 return vs;
2527}
stephen hemminger553675f2013-05-16 11:35:20 +00002528
Pravin B Shelar012a5722013-08-19 11:23:07 -07002529struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2530 vxlan_rcv_t *rcv, void *data,
Tom Herbert359a0ea2014-06-04 17:20:29 -07002531 bool no_share, u32 flags)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002532{
2533 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2534 struct vxlan_sock *vs;
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002535 bool ipv6 = flags & VXLAN_F_IPV6;
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002536
Tom Herbert359a0ea2014-06-04 17:20:29 -07002537 vs = vxlan_socket_create(net, port, rcv, data, flags);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002538 if (!IS_ERR(vs))
2539 return vs;
2540
Pravin B Shelar012a5722013-08-19 11:23:07 -07002541 if (no_share) /* Return error if sharing is not allowed. */
2542 return vs;
2543
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002544 spin_lock(&vn->sock_lock);
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002545 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port);
Marcelo Leitner00c83b02014-12-11 10:02:22 -02002546 if (vs && ((vs->rcv != rcv) ||
2547 !atomic_add_unless(&vs->refcnt, 1, 0)))
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002548 vs = ERR_PTR(-EBUSY);
Pravin B Shelar5cfccc52013-08-19 11:23:02 -07002549 spin_unlock(&vn->sock_lock);
2550
2551 if (!vs)
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002552 vs = ERR_PTR(-EINVAL);
2553
stephen hemminger553675f2013-05-16 11:35:20 +00002554 return vs;
2555}
Pravin B Shelar012a5722013-08-19 11:23:07 -07002556EXPORT_SYMBOL_GPL(vxlan_sock_add);
stephen hemminger553675f2013-05-16 11:35:20 +00002557
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002558/* Scheduled at device creation to bind to a socket */
2559static void vxlan_sock_work(struct work_struct *work)
2560{
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002561 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002562 struct net *net = vxlan->net;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002563 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002564 __be16 port = vxlan->dst_port;
2565 struct vxlan_sock *nvs;
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002566
Tom Herbert359a0ea2014-06-04 17:20:29 -07002567 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002568 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002569 if (!IS_ERR(nvs))
2570 vxlan_vs_add_dev(nvs, vxlan);
2571 spin_unlock(&vn->sock_lock);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002572
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002573 dev_put(vxlan->dev);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002574}
2575
stephen hemmingerd3428942012-10-01 12:32:35 +00002576static int vxlan_newlink(struct net *net, struct net_device *dev,
2577 struct nlattr *tb[], struct nlattr *data[])
2578{
stephen hemminger553675f2013-05-16 11:35:20 +00002579 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002580 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002581 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemmingerd3428942012-10-01 12:32:35 +00002582 __u32 vni;
2583 int err;
Cong Wange4c7ed42013-08-31 13:44:33 +08002584 bool use_ipv6 = false;
stephen hemmingerd3428942012-10-01 12:32:35 +00002585
2586 if (!data[IFLA_VXLAN_ID])
2587 return -EINVAL;
2588
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002589 vxlan->net = dev_net(dev);
2590
stephen hemmingerd3428942012-10-01 12:32:35 +00002591 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002592 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00002593
Mike Rapoport5933a7b2014-04-01 09:23:01 +03002594 /* Unless IPv6 is explicitly requested, assume IPv4 */
2595 dst->remote_ip.sa.sa_family = AF_INET;
Cong Wange4c7ed42013-08-31 13:44:33 +08002596 if (data[IFLA_VXLAN_GROUP]) {
2597 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
Cong Wange4c7ed42013-08-31 13:44:33 +08002598 } else if (data[IFLA_VXLAN_GROUP6]) {
2599 if (!IS_ENABLED(CONFIG_IPV6))
2600 return -EPFNOSUPPORT;
stephen hemmingerd3428942012-10-01 12:32:35 +00002601
Cong Wange4c7ed42013-08-31 13:44:33 +08002602 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2603 sizeof(struct in6_addr));
2604 dst->remote_ip.sa.sa_family = AF_INET6;
2605 use_ipv6 = true;
2606 }
2607
2608 if (data[IFLA_VXLAN_LOCAL]) {
2609 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2610 vxlan->saddr.sa.sa_family = AF_INET;
2611 } else if (data[IFLA_VXLAN_LOCAL6]) {
2612 if (!IS_ENABLED(CONFIG_IPV6))
2613 return -EPFNOSUPPORT;
2614
2615 /* TODO: respect scope id */
2616 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2617 sizeof(struct in6_addr));
2618 vxlan->saddr.sa.sa_family = AF_INET6;
2619 use_ipv6 = true;
2620 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002621
stephen hemminger34e02aa2012-10-09 20:35:53 +00002622 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00002623 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00002624 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00002625 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00002626
stephen hemminger34e02aa2012-10-09 20:35:53 +00002627 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00002628 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00002629 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00002630 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00002631
Cong Wange4c7ed42013-08-31 13:44:33 +08002632#if IS_ENABLED(CONFIG_IPV6)
2633 if (use_ipv6) {
2634 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2635 if (idev && idev->cnf.disable_ipv6) {
2636 pr_info("IPv6 is disabled via sysctl\n");
2637 return -EPERM;
2638 }
2639 vxlan->flags |= VXLAN_F_IPV6;
2640 }
2641#endif
2642
stephen hemminger34e02aa2012-10-09 20:35:53 +00002643 if (!tb[IFLA_MTU])
Cong Wange4c7ed42013-08-31 13:44:33 +08002644 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00002645
Cong Wang2853af62014-06-12 11:53:10 -07002646 dev->needed_headroom = lowerdev->hard_header_len +
Cong Wange4c7ed42013-08-31 13:44:33 +08002647 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
fan.du7bda7012014-01-03 10:18:58 +08002648 } else if (use_ipv6)
2649 vxlan->flags |= VXLAN_F_IPV6;
stephen hemmingerd3428942012-10-01 12:32:35 +00002650
2651 if (data[IFLA_VXLAN_TOS])
2652 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2653
Vincent Bernatafb97182012-10-30 10:27:16 +00002654 if (data[IFLA_VXLAN_TTL])
2655 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2656
stephen hemmingerd3428942012-10-01 12:32:35 +00002657 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00002658 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00002659
2660 if (data[IFLA_VXLAN_AGEING])
2661 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2662 else
2663 vxlan->age_interval = FDB_AGE_DEFAULT;
2664
David Stevense4f67ad2012-11-20 02:50:14 +00002665 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2666 vxlan->flags |= VXLAN_F_PROXY;
2667
2668 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2669 vxlan->flags |= VXLAN_F_RSC;
2670
2671 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2672 vxlan->flags |= VXLAN_F_L2MISS;
2673
2674 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2675 vxlan->flags |= VXLAN_F_L3MISS;
2676
stephen hemmingerd3428942012-10-01 12:32:35 +00002677 if (data[IFLA_VXLAN_LIMIT])
2678 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2679
stephen hemminger05f47d62012-10-09 20:35:50 +00002680 if (data[IFLA_VXLAN_PORT_RANGE]) {
2681 const struct ifla_vxlan_port_range *p
2682 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2683 vxlan->port_min = ntohs(p->low);
2684 vxlan->port_max = ntohs(p->high);
2685 }
2686
stephen hemminger823aa872013-04-27 11:31:57 +00002687 if (data[IFLA_VXLAN_PORT])
2688 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2689
Tom Herbert359a0ea2014-06-04 17:20:29 -07002690 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2691 vxlan->flags |= VXLAN_F_UDP_CSUM;
2692
2693 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2694 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2695 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2696
2697 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2698 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2699 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2700
Tom Herbertdfd86452015-01-12 17:00:38 -08002701 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2702 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2703 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2704
2705 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2706 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2707 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2708
Marcelo Leitner19ca9fc2014-11-13 14:43:08 -02002709 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2710 vxlan->dst_port)) {
stephen hemminger553675f2013-05-16 11:35:20 +00002711 pr_info("duplicate VNI %u\n", vni);
2712 return -EEXIST;
2713 }
2714
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002715 dev->ethtool_ops = &vxlan_ethtool_ops;
Yan Burman1b13c972013-01-29 23:43:07 +00002716
Sridhar Samudrala2936b6a2013-09-17 12:12:40 -07002717 /* create an fdb entry for a valid default destination */
2718 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2719 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2720 &vxlan->default_dst.remote_ip,
2721 NUD_REACHABLE|NUD_PERMANENT,
2722 NLM_F_EXCL|NLM_F_CREATE,
2723 vxlan->dst_port,
2724 vxlan->default_dst.remote_vni,
2725 vxlan->default_dst.remote_ifindex,
2726 NTF_SELF);
2727 if (err)
2728 return err;
2729 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002730
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002731 err = register_netdevice(dev);
2732 if (err) {
Stephen Hemmingerba609e92013-06-25 17:06:01 -07002733 vxlan_fdb_delete_default(vxlan);
Mike Rapoportafbd8ba2013-06-25 16:01:51 +03002734 return err;
2735 }
2736
stephen hemminger553675f2013-05-16 11:35:20 +00002737 list_add(&vxlan->next, &vn->vxlan_list);
stephen hemminger553675f2013-05-16 11:35:20 +00002738
2739 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00002740}
2741
2742static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2743{
2744 struct vxlan_dev *vxlan = netdev_priv(dev);
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002745 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00002746
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002747 spin_lock(&vn->sock_lock);
Pravin B Shelar9c2e24e2013-08-19 11:22:48 -07002748 if (!hlist_unhashed(&vxlan->hlist))
2749 hlist_del_rcu(&vxlan->hlist);
stephen hemmingerfe5c3562013-07-13 10:18:18 -07002750 spin_unlock(&vn->sock_lock);
2751
stephen hemminger553675f2013-05-16 11:35:20 +00002752 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00002753 unregister_netdevice_queue(dev, head);
2754}
2755
2756static size_t vxlan_get_size(const struct net_device *dev)
2757{
2758
2759 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
Cong Wange4c7ed42013-08-31 13:44:33 +08002760 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002761 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
Cong Wange4c7ed42013-08-31 13:44:33 +08002762 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
stephen hemmingerd3428942012-10-01 12:32:35 +00002763 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2764 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2765 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00002766 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2767 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2768 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2769 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00002770 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2771 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00002772 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
Tom Herbert359a0ea2014-06-04 17:20:29 -07002773 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2774 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2775 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2776 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
Tom Herbertdfd86452015-01-12 17:00:38 -08002777 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2778 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
stephen hemmingerd3428942012-10-01 12:32:35 +00002779 0;
2780}
2781
2782static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2783{
2784 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00002785 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00002786 struct ifla_vxlan_port_range ports = {
2787 .low = htons(vxlan->port_min),
2788 .high = htons(vxlan->port_max),
2789 };
stephen hemmingerd3428942012-10-01 12:32:35 +00002790
Atzm Watanabec7995c42013-04-16 02:50:52 +00002791 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00002792 goto nla_put_failure;
2793
Cong Wange4c7ed42013-08-31 13:44:33 +08002794 if (!vxlan_addr_any(&dst->remote_ip)) {
2795 if (dst->remote_ip.sa.sa_family == AF_INET) {
2796 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2797 dst->remote_ip.sin.sin_addr.s_addr))
2798 goto nla_put_failure;
2799#if IS_ENABLED(CONFIG_IPV6)
2800 } else {
2801 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2802 &dst->remote_ip.sin6.sin6_addr))
2803 goto nla_put_failure;
2804#endif
2805 }
2806 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002807
Atzm Watanabec7995c42013-04-16 02:50:52 +00002808 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00002809 goto nla_put_failure;
2810
Cong Wange4c7ed42013-08-31 13:44:33 +08002811 if (!vxlan_addr_any(&vxlan->saddr)) {
2812 if (vxlan->saddr.sa.sa_family == AF_INET) {
2813 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2814 vxlan->saddr.sin.sin_addr.s_addr))
2815 goto nla_put_failure;
2816#if IS_ENABLED(CONFIG_IPV6)
2817 } else {
2818 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2819 &vxlan->saddr.sin6.sin6_addr))
2820 goto nla_put_failure;
2821#endif
2822 }
2823 }
stephen hemmingerd3428942012-10-01 12:32:35 +00002824
2825 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2826 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00002827 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2828 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2829 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2830 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2831 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2832 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2833 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2834 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2835 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00002836 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00002837 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
Tom Herbert359a0ea2014-06-04 17:20:29 -07002838 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2839 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2840 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2841 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2842 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2843 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
Tom Herbertdfd86452015-01-12 17:00:38 -08002844 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2845 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2846 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2847 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2848 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
stephen hemmingerd3428942012-10-01 12:32:35 +00002849 goto nla_put_failure;
2850
stephen hemminger05f47d62012-10-09 20:35:50 +00002851 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2852 goto nla_put_failure;
2853
stephen hemmingerd3428942012-10-01 12:32:35 +00002854 return 0;
2855
2856nla_put_failure:
2857 return -EMSGSIZE;
2858}
2859
2860static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2861 .kind = "vxlan",
2862 .maxtype = IFLA_VXLAN_MAX,
2863 .policy = vxlan_policy,
2864 .priv_size = sizeof(struct vxlan_dev),
2865 .setup = vxlan_setup,
2866 .validate = vxlan_validate,
2867 .newlink = vxlan_newlink,
2868 .dellink = vxlan_dellink,
2869 .get_size = vxlan_get_size,
2870 .fill_info = vxlan_fill_info,
2871};
2872
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002873static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2874 struct net_device *dev)
2875{
2876 struct vxlan_dev *vxlan, *next;
2877 LIST_HEAD(list_kill);
2878
2879 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2880 struct vxlan_rdst *dst = &vxlan->default_dst;
2881
2882 /* In case we created vxlan device with carrier
2883 * and we loose the carrier due to module unload
2884 * we also need to remove vxlan device. In other
2885 * cases, it's not necessary and remote_ifindex
2886 * is 0 here, so no matches.
2887 */
2888 if (dst->remote_ifindex == dev->ifindex)
2889 vxlan_dellink(vxlan->dev, &list_kill);
2890 }
2891
2892 unregister_netdevice_many(&list_kill);
2893}
2894
2895static int vxlan_lowerdev_event(struct notifier_block *unused,
2896 unsigned long event, void *ptr)
2897{
2898 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002899 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002900
Daniel Borkmann783c1462014-01-22 21:07:53 +01002901 if (event == NETDEV_UNREGISTER)
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002902 vxlan_handle_lowerdev_unregister(vn, dev);
2903
2904 return NOTIFY_DONE;
2905}
2906
2907static struct notifier_block vxlan_notifier_block __read_mostly = {
2908 .notifier_call = vxlan_lowerdev_event,
2909};
2910
stephen hemmingerd3428942012-10-01 12:32:35 +00002911static __net_init int vxlan_init_net(struct net *net)
2912{
2913 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00002914 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00002915
stephen hemminger553675f2013-05-16 11:35:20 +00002916 INIT_LIST_HEAD(&vn->vxlan_list);
Stephen Hemminger1c51a912013-06-17 14:16:11 -07002917 spin_lock_init(&vn->sock_lock);
stephen hemmingerd3428942012-10-01 12:32:35 +00002918
stephen hemminger553675f2013-05-16 11:35:20 +00002919 for (h = 0; h < PORT_HASH_SIZE; ++h)
2920 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00002921
2922 return 0;
2923}
2924
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002925static void __net_exit vxlan_exit_net(struct net *net)
2926{
2927 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2928 struct vxlan_dev *vxlan, *next;
2929 struct net_device *dev, *aux;
2930 LIST_HEAD(list);
2931
2932 rtnl_lock();
2933 for_each_netdev_safe(net, dev, aux)
2934 if (dev->rtnl_link_ops == &vxlan_link_ops)
2935 unregister_netdevice_queue(dev, &list);
2936
2937 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2938 /* If vxlan->dev is in the same netns, it has already been added
2939 * to the list by the previous loop.
2940 */
2941 if (!net_eq(dev_net(vxlan->dev), net))
2942 unregister_netdevice_queue(dev, &list);
2943 }
2944
2945 unregister_netdevice_many(&list);
2946 rtnl_unlock();
2947}
2948
stephen hemmingerd3428942012-10-01 12:32:35 +00002949static struct pernet_operations vxlan_net_ops = {
2950 .init = vxlan_init_net,
Nicolas Dichtelf01ec1c2014-04-24 10:02:49 +02002951 .exit = vxlan_exit_net,
stephen hemmingerd3428942012-10-01 12:32:35 +00002952 .id = &vxlan_net_id,
2953 .size = sizeof(struct vxlan_net),
2954};
2955
2956static int __init vxlan_init_module(void)
2957{
2958 int rc;
2959
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002960 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2961 if (!vxlan_wq)
2962 return -ENOMEM;
2963
stephen hemmingerd3428942012-10-01 12:32:35 +00002964 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2965
Daniel Borkmann783c1462014-01-22 21:07:53 +01002966 rc = register_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002967 if (rc)
2968 goto out1;
2969
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002970 rc = register_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002971 if (rc)
2972 goto out2;
2973
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002974 rc = rtnl_link_register(&vxlan_link_ops);
2975 if (rc)
2976 goto out3;
stephen hemmingerd3428942012-10-01 12:32:35 +00002977
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002978 return 0;
2979out3:
2980 unregister_netdevice_notifier(&vxlan_notifier_block);
stephen hemmingerd3428942012-10-01 12:32:35 +00002981out2:
Daniel Borkmann783c1462014-01-22 21:07:53 +01002982 unregister_pernet_subsys(&vxlan_net_ops);
stephen hemmingerd3428942012-10-01 12:32:35 +00002983out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002984 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00002985 return rc;
2986}
Cong Wang7332a132013-05-27 22:35:53 +00002987late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00002988
2989static void __exit vxlan_cleanup_module(void)
2990{
Stephen Hemmingerb7153982013-06-17 14:16:09 -07002991 rtnl_link_unregister(&vxlan_link_ops);
Daniel Borkmannacaf4e7092014-01-13 18:41:19 +01002992 unregister_netdevice_notifier(&vxlan_notifier_block);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07002993 destroy_workqueue(vxlan_wq);
Daniel Borkmann783c1462014-01-22 21:07:53 +01002994 unregister_pernet_subsys(&vxlan_net_ops);
2995 /* rcu_barrier() is called by netns */
stephen hemmingerd3428942012-10-01 12:32:35 +00002996}
2997module_exit(vxlan_cleanup_module);
2998
2999MODULE_LICENSE("GPL");
3000MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00003001MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
Jesse Brandeburgead51392014-01-17 11:00:33 -08003002MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
stephen hemmingerd3428942012-10-01 12:32:35 +00003003MODULE_ALIAS_RTNL_LINK("vxlan");