drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / vxlan.c
CommitLineData
d342894c 1/*
eb5ce439 2 * VXLAN: Virtual eXtensible Local Area Network
d342894c 3 *
3b8df3c6 4 * Copyright (c) 2012-2013 Vyatta Inc.
d342894c 5 *
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.
9 *
10 * TODO
d342894c 11 * - IPv6 (not in RFC)
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/slab.h>
21#include <linux/skbuff.h>
22#include <linux/rculist.h>
23#include <linux/netdevice.h>
24#include <linux/in.h>
25#include <linux/ip.h>
26#include <linux/udp.h>
27#include <linux/igmp.h>
28#include <linux/etherdevice.h>
29#include <linux/if_ether.h>
d342894c 30#include <linux/hash.h>
1b13c97f 31#include <linux/ethtool.h>
e4f67add
DS
32#include <net/arp.h>
33#include <net/ndisc.h>
d342894c 34#include <net/ip.h>
c5441932 35#include <net/ip_tunnels.h>
d342894c 36#include <net/icmp.h>
37#include <net/udp.h>
38#include <net/rtnetlink.h>
39#include <net/route.h>
40#include <net/dsfield.h>
41#include <net/inet_ecn.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44
45#define VXLAN_VERSION "0.1"
46
47#define VNI_HASH_BITS 10
48#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
49#define FDB_HASH_BITS 8
50#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
51#define FDB_AGE_DEFAULT 300 /* 5 min */
52#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
53
54#define VXLAN_N_VID (1u << 24)
55#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
52b702ff
AD
56/* IP header + UDP + VXLAN + Ethernet header */
57#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
d342894c 58
59#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
60
61/* VXLAN protocol header */
62struct vxlanhdr {
63 __be32 vx_flags;
64 __be32 vx_vni;
65};
66
23c578bf 67/* UDP port for VXLAN traffic.
68 * The IANA assigned port is 4789, but the Linux default is 8472
69 * for compatability with early adopters.
70 */
d342894c 71static unsigned int vxlan_port __read_mostly = 8472;
72module_param_named(udp_port, vxlan_port, uint, 0444);
73MODULE_PARM_DESC(udp_port, "Destination UDP port");
74
75static bool log_ecn_error = true;
76module_param(log_ecn_error, bool, 0644);
77MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
78
79/* per-net private data for this module */
80static unsigned int vxlan_net_id;
81struct vxlan_net {
82 struct socket *sock; /* UDP encap socket */
83 struct hlist_head vni_list[VNI_HASH_SIZE];
84};
85
6681712d
DS
86struct vxlan_rdst {
87 struct rcu_head rcu;
88 __be32 remote_ip;
89 __be16 remote_port;
90 u32 remote_vni;
91 u32 remote_ifindex;
92 struct vxlan_rdst *remote_next;
93};
94
d342894c 95/* Forwarding table entry */
96struct vxlan_fdb {
97 struct hlist_node hlist; /* linked list of entries */
98 struct rcu_head rcu;
99 unsigned long updated; /* jiffies */
100 unsigned long used;
6681712d 101 struct vxlan_rdst remote;
d342894c 102 u16 state; /* see ndm_state */
ae884082 103 u8 flags; /* see ndm_flags */
d342894c 104 u8 eth_addr[ETH_ALEN];
105};
106
d342894c 107/* Pseudo network device */
108struct vxlan_dev {
109 struct hlist_node hlist;
110 struct net_device *dev;
c7995c43 111 struct vxlan_rdst default_dst; /* default destination */
d342894c 112 __be32 saddr; /* source address */
823aa873 113 __be16 dst_port;
05f47d69 114 __u16 port_min; /* source port range */
115 __u16 port_max;
d342894c 116 __u8 tos; /* TOS override */
117 __u8 ttl;
e4f67add 118 u32 flags; /* VXLAN_F_* below */
d342894c 119
120 unsigned long age_interval;
121 struct timer_list age_timer;
122 spinlock_t hash_lock;
123 unsigned int addrcnt;
124 unsigned int addrmax;
d342894c 125
126 struct hlist_head fdb_head[FDB_HASH_SIZE];
127};
128
e4f67add
DS
129#define VXLAN_F_LEARN 0x01
130#define VXLAN_F_PROXY 0x02
131#define VXLAN_F_RSC 0x04
132#define VXLAN_F_L2MISS 0x08
133#define VXLAN_F_L3MISS 0x10
134
d342894c 135/* salt for hash table */
136static u32 vxlan_salt __read_mostly;
137
138static inline struct hlist_head *vni_head(struct net *net, u32 id)
139{
140 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
141
142 return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
143}
144
145/* Look up VNI in a per net namespace table */
146static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
147{
148 struct vxlan_dev *vxlan;
d342894c 149
b67bfe0d 150 hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) {
c7995c43 151 if (vxlan->default_dst.remote_vni == id)
d342894c 152 return vxlan;
153 }
154
155 return NULL;
156}
157
158/* Fill in neighbour message in skbuff. */
159static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
160 const struct vxlan_fdb *fdb,
6681712d
DS
161 u32 portid, u32 seq, int type, unsigned int flags,
162 const struct vxlan_rdst *rdst)
d342894c 163{
164 unsigned long now = jiffies;
165 struct nda_cacheinfo ci;
166 struct nlmsghdr *nlh;
167 struct ndmsg *ndm;
e4f67add 168 bool send_ip, send_eth;
d342894c 169
170 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
171 if (nlh == NULL)
172 return -EMSGSIZE;
173
174 ndm = nlmsg_data(nlh);
175 memset(ndm, 0, sizeof(*ndm));
e4f67add
DS
176
177 send_eth = send_ip = true;
178
179 if (type == RTM_GETNEIGH) {
180 ndm->ndm_family = AF_INET;
6681712d 181 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
e4f67add
DS
182 send_eth = !is_zero_ether_addr(fdb->eth_addr);
183 } else
184 ndm->ndm_family = AF_BRIDGE;
d342894c 185 ndm->ndm_state = fdb->state;
186 ndm->ndm_ifindex = vxlan->dev->ifindex;
ae884082 187 ndm->ndm_flags = fdb->flags;
d342894c 188 ndm->ndm_type = NDA_DST;
189
e4f67add 190 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
d342894c 191 goto nla_put_failure;
192
6681712d
DS
193 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
194 goto nla_put_failure;
195
823aa873 196 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
6681712d
DS
197 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
198 goto nla_put_failure;
c7995c43 199 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
6681712d
DS
200 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
201 goto nla_put_failure;
202 if (rdst->remote_ifindex &&
203 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
d342894c 204 goto nla_put_failure;
205
206 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
207 ci.ndm_confirmed = 0;
208 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
209 ci.ndm_refcnt = 0;
210
211 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
212 goto nla_put_failure;
213
214 return nlmsg_end(skb, nlh);
215
216nla_put_failure:
217 nlmsg_cancel(skb, nlh);
218 return -EMSGSIZE;
219}
220
221static inline size_t vxlan_nlmsg_size(void)
222{
223 return NLMSG_ALIGN(sizeof(struct ndmsg))
224 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
225 + nla_total_size(sizeof(__be32)) /* NDA_DST */
73cf3317 226 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
6681712d
DS
227 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
228 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
d342894c 229 + nla_total_size(sizeof(struct nda_cacheinfo));
230}
231
232static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
233 const struct vxlan_fdb *fdb, int type)
234{
235 struct net *net = dev_net(vxlan->dev);
236 struct sk_buff *skb;
237 int err = -ENOBUFS;
238
239 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
240 if (skb == NULL)
241 goto errout;
242
6681712d 243 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
d342894c 244 if (err < 0) {
245 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
246 WARN_ON(err == -EMSGSIZE);
247 kfree_skb(skb);
248 goto errout;
249 }
250
251 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
252 return;
253errout:
254 if (err < 0)
255 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
256}
257
e4f67add
DS
258static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
259{
260 struct vxlan_dev *vxlan = netdev_priv(dev);
261 struct vxlan_fdb f;
262
263 memset(&f, 0, sizeof f);
264 f.state = NUD_STALE;
6681712d
DS
265 f.remote.remote_ip = ipa; /* goes to NDA_DST */
266 f.remote.remote_vni = VXLAN_N_VID;
e4f67add
DS
267
268 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
269}
270
271static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
272{
273 struct vxlan_fdb f;
274
275 memset(&f, 0, sizeof f);
276 f.state = NUD_STALE;
277 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
278
279 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
280}
281
d342894c 282/* Hash Ethernet address */
283static u32 eth_hash(const unsigned char *addr)
284{
285 u64 value = get_unaligned((u64 *)addr);
286
287 /* only want 6 bytes */
288#ifdef __BIG_ENDIAN
d342894c 289 value >>= 16;
321fb991 290#else
291 value <<= 16;
d342894c 292#endif
293 return hash_64(value, FDB_HASH_BITS);
294}
295
296/* Hash chain to use given mac address */
297static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
298 const u8 *mac)
299{
300 return &vxlan->fdb_head[eth_hash(mac)];
301}
302
303/* Look up Ethernet address in forwarding table */
014be2c8 304static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
d342894c 305 const u8 *mac)
306
307{
308 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
309 struct vxlan_fdb *f;
d342894c 310
b67bfe0d 311 hlist_for_each_entry_rcu(f, head, hlist) {
d342894c 312 if (compare_ether_addr(mac, f->eth_addr) == 0)
313 return f;
314 }
315
316 return NULL;
317}
318
014be2c8
SS
319static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
320 const u8 *mac)
321{
322 struct vxlan_fdb *f;
323
324 f = __vxlan_find_mac(vxlan, mac);
325 if (f)
326 f->used = jiffies;
327
328 return f;
329}
330
6681712d
DS
331/* Add/update destinations for multicast */
332static int vxlan_fdb_append(struct vxlan_fdb *f,
73cf3317 333 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
6681712d
DS
334{
335 struct vxlan_rdst *rd_prev, *rd;
336
337 rd_prev = NULL;
338 for (rd = &f->remote; rd; rd = rd->remote_next) {
339 if (rd->remote_ip == ip &&
340 rd->remote_port == port &&
341 rd->remote_vni == vni &&
342 rd->remote_ifindex == ifindex)
343 return 0;
344 rd_prev = rd;
345 }
346 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
347 if (rd == NULL)
348 return -ENOBUFS;
349 rd->remote_ip = ip;
350 rd->remote_port = port;
351 rd->remote_vni = vni;
352 rd->remote_ifindex = ifindex;
353 rd->remote_next = NULL;
354 rd_prev->remote_next = rd;
355 return 1;
356}
357
d342894c 358/* Add new entry to forwarding table -- assumes lock held */
359static int vxlan_fdb_create(struct vxlan_dev *vxlan,
360 const u8 *mac, __be32 ip,
6681712d 361 __u16 state, __u16 flags,
73cf3317 362 __be16 port, __u32 vni, __u32 ifindex,
ae884082 363 __u8 ndm_flags)
d342894c 364{
365 struct vxlan_fdb *f;
366 int notify = 0;
367
014be2c8 368 f = __vxlan_find_mac(vxlan, mac);
d342894c 369 if (f) {
370 if (flags & NLM_F_EXCL) {
371 netdev_dbg(vxlan->dev,
372 "lost race to create %pM\n", mac);
373 return -EEXIST;
374 }
375 if (f->state != state) {
376 f->state = state;
377 f->updated = jiffies;
378 notify = 1;
379 }
ae884082
DS
380 if (f->flags != ndm_flags) {
381 f->flags = ndm_flags;
382 f->updated = jiffies;
383 notify = 1;
384 }
6681712d
DS
385 if ((flags & NLM_F_APPEND) &&
386 is_multicast_ether_addr(f->eth_addr)) {
387 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
388
389 if (rc < 0)
390 return rc;
391 notify |= rc;
392 }
d342894c 393 } else {
394 if (!(flags & NLM_F_CREATE))
395 return -ENOENT;
396
397 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
398 return -ENOSPC;
399
400 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
401 f = kmalloc(sizeof(*f), GFP_ATOMIC);
402 if (!f)
403 return -ENOMEM;
404
405 notify = 1;
6681712d
DS
406 f->remote.remote_ip = ip;
407 f->remote.remote_port = port;
408 f->remote.remote_vni = vni;
409 f->remote.remote_ifindex = ifindex;
410 f->remote.remote_next = NULL;
d342894c 411 f->state = state;
ae884082 412 f->flags = ndm_flags;
d342894c 413 f->updated = f->used = jiffies;
414 memcpy(f->eth_addr, mac, ETH_ALEN);
415
416 ++vxlan->addrcnt;
417 hlist_add_head_rcu(&f->hlist,
418 vxlan_fdb_head(vxlan, mac));
419 }
420
421 if (notify)
422 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
423
424 return 0;
425}
426
6706c82e 427static void vxlan_fdb_free(struct rcu_head *head)
6681712d
DS
428{
429 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
430
431 while (f->remote.remote_next) {
432 struct vxlan_rdst *rd = f->remote.remote_next;
433
434 f->remote.remote_next = rd->remote_next;
435 kfree(rd);
436 }
437 kfree(f);
438}
439
d342894c 440static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
441{
442 netdev_dbg(vxlan->dev,
443 "delete %pM\n", f->eth_addr);
444
445 --vxlan->addrcnt;
446 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
447
448 hlist_del_rcu(&f->hlist);
6681712d 449 call_rcu(&f->rcu, vxlan_fdb_free);
d342894c 450}
451
452/* Add static entry (via netlink) */
453static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
454 struct net_device *dev,
455 const unsigned char *addr, u16 flags)
456{
457 struct vxlan_dev *vxlan = netdev_priv(dev);
6681712d 458 struct net *net = dev_net(vxlan->dev);
d342894c 459 __be32 ip;
73cf3317 460 __be16 port;
461 u32 vni, ifindex;
d342894c 462 int err;
463
464 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
465 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
466 ndm->ndm_state);
467 return -EINVAL;
468 }
469
470 if (tb[NDA_DST] == NULL)
471 return -EINVAL;
472
473 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
474 return -EAFNOSUPPORT;
475
476 ip = nla_get_be32(tb[NDA_DST]);
477
6681712d 478 if (tb[NDA_PORT]) {
73cf3317 479 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
6681712d 480 return -EINVAL;
73cf3317 481 port = nla_get_be16(tb[NDA_PORT]);
6681712d 482 } else
823aa873 483 port = vxlan->dst_port;
6681712d
DS
484
485 if (tb[NDA_VNI]) {
486 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
487 return -EINVAL;
488 vni = nla_get_u32(tb[NDA_VNI]);
489 } else
c7995c43 490 vni = vxlan->default_dst.remote_vni;
6681712d
DS
491
492 if (tb[NDA_IFINDEX]) {
5abb0029 493 struct net_device *tdev;
6681712d
DS
494
495 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
496 return -EINVAL;
497 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
5abb0029
PS
498 tdev = dev_get_by_index(net, ifindex);
499 if (!tdev)
6681712d 500 return -EADDRNOTAVAIL;
5abb0029 501 dev_put(tdev);
6681712d
DS
502 } else
503 ifindex = 0;
504
d342894c 505 spin_lock_bh(&vxlan->hash_lock);
73cf3317 506 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
507 port, vni, ifindex, ndm->ndm_flags);
d342894c 508 spin_unlock_bh(&vxlan->hash_lock);
509
510 return err;
511}
512
513/* Delete entry (via netlink) */
1690be63
VY
514static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
515 struct net_device *dev,
d342894c 516 const unsigned char *addr)
517{
518 struct vxlan_dev *vxlan = netdev_priv(dev);
519 struct vxlan_fdb *f;
520 int err = -ENOENT;
521
522 spin_lock_bh(&vxlan->hash_lock);
523 f = vxlan_find_mac(vxlan, addr);
524 if (f) {
525 vxlan_fdb_destroy(vxlan, f);
526 err = 0;
527 }
528 spin_unlock_bh(&vxlan->hash_lock);
529
530 return err;
531}
532
533/* Dump forwarding table */
534static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
535 struct net_device *dev, int idx)
536{
537 struct vxlan_dev *vxlan = netdev_priv(dev);
538 unsigned int h;
539
540 for (h = 0; h < FDB_HASH_SIZE; ++h) {
541 struct vxlan_fdb *f;
d342894c 542 int err;
543
b67bfe0d 544 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
6681712d
DS
545 struct vxlan_rdst *rd;
546 for (rd = &f->remote; rd; rd = rd->remote_next) {
547 if (idx < cb->args[0])
548 goto skip;
549
550 err = vxlan_fdb_info(skb, vxlan, f,
551 NETLINK_CB(cb->skb).portid,
552 cb->nlh->nlmsg_seq,
553 RTM_NEWNEIGH,
554 NLM_F_MULTI, rd);
555 if (err < 0)
556 break;
d342894c 557skip:
6681712d
DS
558 ++idx;
559 }
d342894c 560 }
561 }
562
563 return idx;
564}
565
566/* Watch incoming packets to learn mapping between Ethernet address
567 * and Tunnel endpoint.
26a41ae6 568 * Return true if packet is bogus and should be droppped.
d342894c 569 */
26a41ae6 570static bool vxlan_snoop(struct net_device *dev,
d342894c 571 __be32 src_ip, const u8 *src_mac)
572{
573 struct vxlan_dev *vxlan = netdev_priv(dev);
574 struct vxlan_fdb *f;
d342894c 575
576 f = vxlan_find_mac(vxlan, src_mac);
577 if (likely(f)) {
6681712d 578 if (likely(f->remote.remote_ip == src_ip))
26a41ae6 579 return false;
580
581 /* Don't migrate static entries, drop packets */
eb064c3b 582 if (f->state & NUD_NOARP)
26a41ae6 583 return true;
d342894c 584
585 if (net_ratelimit())
586 netdev_info(dev,
587 "%pM migrated from %pI4 to %pI4\n",
6681712d 588 src_mac, &f->remote.remote_ip, &src_ip);
d342894c 589
6681712d 590 f->remote.remote_ip = src_ip;
d342894c 591 f->updated = jiffies;
592 } else {
593 /* learned new entry */
594 spin_lock(&vxlan->hash_lock);
3bf74b1a 595
596 /* close off race between vxlan_flush and incoming packets */
597 if (netif_running(dev))
598 vxlan_fdb_create(vxlan, src_mac, src_ip,
599 NUD_REACHABLE,
600 NLM_F_EXCL|NLM_F_CREATE,
601 vxlan->dst_port,
602 vxlan->default_dst.remote_vni,
603 0, NTF_SELF);
d342894c 604 spin_unlock(&vxlan->hash_lock);
605 }
26a41ae6 606
607 return false;
d342894c 608}
609
610
611/* See if multicast group is already in use by other ID */
612static bool vxlan_group_used(struct vxlan_net *vn,
613 const struct vxlan_dev *this)
614{
615 const struct vxlan_dev *vxlan;
d342894c 616 unsigned h;
617
618 for (h = 0; h < VNI_HASH_SIZE; ++h)
b67bfe0d 619 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
d342894c 620 if (vxlan == this)
621 continue;
622
623 if (!netif_running(vxlan->dev))
624 continue;
625
c7995c43 626 if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
d342894c 627 return true;
628 }
629
630 return false;
631}
632
633/* kernel equivalent to IP_ADD_MEMBERSHIP */
634static int vxlan_join_group(struct net_device *dev)
635{
636 struct vxlan_dev *vxlan = netdev_priv(dev);
637 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
638 struct sock *sk = vn->sock->sk;
639 struct ip_mreqn mreq = {
c7995c43
AW
640 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
641 .imr_ifindex = vxlan->default_dst.remote_ifindex,
d342894c 642 };
643 int err;
644
645 /* Already a member of group */
646 if (vxlan_group_used(vn, vxlan))
647 return 0;
648
649 /* Need to drop RTNL to call multicast join */
650 rtnl_unlock();
651 lock_sock(sk);
652 err = ip_mc_join_group(sk, &mreq);
653 release_sock(sk);
654 rtnl_lock();
655
656 return err;
657}
658
659
660/* kernel equivalent to IP_DROP_MEMBERSHIP */
661static int vxlan_leave_group(struct net_device *dev)
662{
663 struct vxlan_dev *vxlan = netdev_priv(dev);
664 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
665 int err = 0;
666 struct sock *sk = vn->sock->sk;
667 struct ip_mreqn mreq = {
c7995c43
AW
668 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
669 .imr_ifindex = vxlan->default_dst.remote_ifindex,
d342894c 670 };
671
672 /* Only leave group when last vxlan is done. */
673 if (vxlan_group_used(vn, vxlan))
674 return 0;
675
676 /* Need to drop RTNL to call multicast leave */
677 rtnl_unlock();
678 lock_sock(sk);
679 err = ip_mc_leave_group(sk, &mreq);
680 release_sock(sk);
681 rtnl_lock();
682
683 return err;
684}
685
686/* Callback from net/ipv4/udp.c to receive packets */
687static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
688{
689 struct iphdr *oip;
690 struct vxlanhdr *vxh;
691 struct vxlan_dev *vxlan;
e8171045 692 struct pcpu_tstats *stats;
d342894c 693 __u32 vni;
694 int err;
695
696 /* pop off outer UDP header */
697 __skb_pull(skb, sizeof(struct udphdr));
698
699 /* Need Vxlan and inner Ethernet header to be present */
700 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
701 goto error;
702
703 /* Drop packets with reserved bits set */
704 vxh = (struct vxlanhdr *) skb->data;
705 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
706 (vxh->vx_vni & htonl(0xff))) {
707 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
708 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
709 goto error;
710 }
711
712 __skb_pull(skb, sizeof(struct vxlanhdr));
d342894c 713
714 /* Is this VNI defined? */
715 vni = ntohl(vxh->vx_vni) >> 8;
716 vxlan = vxlan_find_vni(sock_net(sk), vni);
717 if (!vxlan) {
718 netdev_dbg(skb->dev, "unknown vni %d\n", vni);
719 goto drop;
720 }
721
722 if (!pskb_may_pull(skb, ETH_HLEN)) {
723 vxlan->dev->stats.rx_length_errors++;
724 vxlan->dev->stats.rx_errors++;
725 goto drop;
726 }
727
e4f67add
DS
728 skb_reset_mac_header(skb);
729
d342894c 730 /* Re-examine inner Ethernet packet */
731 oip = ip_hdr(skb);
732 skb->protocol = eth_type_trans(skb, vxlan->dev);
d342894c 733
734 /* Ignore packet loops (and multicast echo) */
735 if (compare_ether_addr(eth_hdr(skb)->h_source,
736 vxlan->dev->dev_addr) == 0)
737 goto drop;
738
26a41ae6 739 if ((vxlan->flags & VXLAN_F_LEARN) &&
740 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
741 goto drop;
d342894c 742
743 __skb_tunnel_rx(skb, vxlan->dev);
744 skb_reset_network_header(skb);
0afb1666
JG
745
746 /* If the NIC driver gave us an encapsulated packet with
747 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
748 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
749 * for us. Otherwise force the upper layers to verify it.
750 */
751 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
752 !(vxlan->dev->features & NETIF_F_RXCSUM))
753 skb->ip_summed = CHECKSUM_NONE;
754
755 skb->encapsulation = 0;
d342894c 756
757 err = IP_ECN_decapsulate(oip, skb);
758 if (unlikely(err)) {
759 if (log_ecn_error)
760 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
761 &oip->saddr, oip->tos);
762 if (err > 1) {
763 ++vxlan->dev->stats.rx_frame_errors;
764 ++vxlan->dev->stats.rx_errors;
765 goto drop;
766 }
767 }
768
e8171045 769 stats = this_cpu_ptr(vxlan->dev->tstats);
d342894c 770 u64_stats_update_begin(&stats->syncp);
771 stats->rx_packets++;
772 stats->rx_bytes += skb->len;
773 u64_stats_update_end(&stats->syncp);
774
775 netif_rx(skb);
776
777 return 0;
778error:
779 /* Put UDP header back */
780 __skb_push(skb, sizeof(struct udphdr));
781
782 return 1;
783drop:
784 /* Consume bad packet */
785 kfree_skb(skb);
786 return 0;
787}
788
e4f67add
DS
789static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
790{
791 struct vxlan_dev *vxlan = netdev_priv(dev);
792 struct arphdr *parp;
793 u8 *arpptr, *sha;
794 __be32 sip, tip;
795 struct neighbour *n;
796
797 if (dev->flags & IFF_NOARP)
798 goto out;
799
800 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
801 dev->stats.tx_dropped++;
802 goto out;
803 }
804 parp = arp_hdr(skb);
805
806 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
807 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
808 parp->ar_pro != htons(ETH_P_IP) ||
809 parp->ar_op != htons(ARPOP_REQUEST) ||
810 parp->ar_hln != dev->addr_len ||
811 parp->ar_pln != 4)
812 goto out;
813 arpptr = (u8 *)parp + sizeof(struct arphdr);
814 sha = arpptr;
815 arpptr += dev->addr_len; /* sha */
816 memcpy(&sip, arpptr, sizeof(sip));
817 arpptr += sizeof(sip);
818 arpptr += dev->addr_len; /* tha */
819 memcpy(&tip, arpptr, sizeof(tip));
820
821 if (ipv4_is_loopback(tip) ||
822 ipv4_is_multicast(tip))
823 goto out;
824
825 n = neigh_lookup(&arp_tbl, &tip, dev);
826
827 if (n) {
e4f67add
DS
828 struct vxlan_fdb *f;
829 struct sk_buff *reply;
830
831 if (!(n->nud_state & NUD_CONNECTED)) {
832 neigh_release(n);
833 goto out;
834 }
835
836 f = vxlan_find_mac(vxlan, n->ha);
6681712d 837 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
e4f67add
DS
838 /* bridge-local neighbor */
839 neigh_release(n);
840 goto out;
841 }
842
843 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
844 n->ha, sha);
845
846 neigh_release(n);
847
c3c4a8c1
DS
848 if (reply == NULL)
849 goto out;
850
e4f67add
DS
851 skb_reset_mac_header(reply);
852 __skb_pull(reply, skb_network_offset(reply));
853 reply->ip_summed = CHECKSUM_UNNECESSARY;
854 reply->pkt_type = PACKET_HOST;
855
856 if (netif_rx_ni(reply) == NET_RX_DROP)
857 dev->stats.rx_dropped++;
858 } else if (vxlan->flags & VXLAN_F_L3MISS)
859 vxlan_ip_miss(dev, tip);
860out:
861 consume_skb(skb);
862 return NETDEV_TX_OK;
863}
864
865static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
866{
867 struct vxlan_dev *vxlan = netdev_priv(dev);
868 struct neighbour *n;
869 struct iphdr *pip;
870
871 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
872 return false;
873
874 n = NULL;
875 switch (ntohs(eth_hdr(skb)->h_proto)) {
876 case ETH_P_IP:
877 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
878 return false;
879 pip = ip_hdr(skb);
880 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
881 break;
882 default:
883 return false;
884 }
885
886 if (n) {
887 bool diff;
888
889 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
890 if (diff) {
891 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
892 dev->addr_len);
893 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
894 }
895 neigh_release(n);
896 return diff;
897 } else if (vxlan->flags & VXLAN_F_L3MISS)
898 vxlan_ip_miss(dev, pip->daddr);
899 return false;
900}
901
1cad8715 902static void vxlan_sock_free(struct sk_buff *skb)
903{
904 sock_put(skb->sk);
905}
906
907/* On transmit, associate with the tunnel socket */
908static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
909{
910 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
911 struct sock *sk = vn->sock->sk;
912
913 skb_orphan(skb);
914 sock_hold(sk);
915 skb->sk = sk;
916 skb->destructor = vxlan_sock_free;
917}
918
05f47d69 919/* Compute source port for outgoing packet
920 * first choice to use L4 flow hash since it will spread
921 * better and maybe available from hardware
922 * secondary choice is to use jhash on the Ethernet header
923 */
7d836a76 924static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
05f47d69 925{
926 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
927 u32 hash;
928
929 hash = skb_get_rxhash(skb);
930 if (!hash)
931 hash = jhash(skb->data, 2 * ETH_ALEN,
932 (__force u32) skb->protocol);
933
7d836a76 934 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
05f47d69 935}
936
05c0db08
PS
937static int handle_offloads(struct sk_buff *skb)
938{
939 if (skb_is_gso(skb)) {
940 int err = skb_unclone(skb, GFP_ATOMIC);
941 if (unlikely(err))
942 return err;
943
f6ace502 944 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
05c0db08
PS
945 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
946 skb->ip_summed = CHECKSUM_NONE;
947
948 return 0;
949}
950
9dcc71e1
SS
951/* Bypass encapsulation if the destination is local */
952static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
953 struct vxlan_dev *dst_vxlan)
954{
955 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
956 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
957
958 skb->pkt_type = PACKET_HOST;
959 skb->encapsulation = 0;
960 skb->dev = dst_vxlan->dev;
961 __skb_pull(skb, skb_network_offset(skb));
962
963 if (dst_vxlan->flags & VXLAN_F_LEARN)
9d9f163c
MR
964 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
965 eth_hdr(skb)->h_source);
9dcc71e1
SS
966
967 u64_stats_update_begin(&tx_stats->syncp);
968 tx_stats->tx_packets++;
969 tx_stats->tx_bytes += skb->len;
970 u64_stats_update_end(&tx_stats->syncp);
971
972 if (netif_rx(skb) == NET_RX_SUCCESS) {
973 u64_stats_update_begin(&rx_stats->syncp);
974 rx_stats->rx_packets++;
975 rx_stats->rx_bytes += skb->len;
976 u64_stats_update_end(&rx_stats->syncp);
977 } else {
978 skb->dev->stats.rx_dropped++;
979 }
980}
981
6681712d
DS
982static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
983 struct vxlan_rdst *rdst, bool did_rsc)
d342894c 984{
985 struct vxlan_dev *vxlan = netdev_priv(dev);
986 struct rtable *rt;
d342894c 987 const struct iphdr *old_iph;
988 struct iphdr *iph;
989 struct vxlanhdr *vxh;
990 struct udphdr *uh;
991 struct flowi4 fl4;
d342894c 992 __be32 dst;
7d836a76 993 __be16 src_port, dst_port;
6681712d 994 u32 vni;
d342894c 995 __be16 df = 0;
996 __u8 tos, ttl;
d342894c 997
823aa873 998 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
6681712d
DS
999 vni = rdst->remote_vni;
1000 dst = rdst->remote_ip;
e4f67add
DS
1001
1002 if (!dst) {
1003 if (did_rsc) {
e4f67add 1004 /* short-circuited back to local bridge */
9dcc71e1 1005 vxlan_encap_bypass(skb, vxlan, vxlan);
e4f67add
DS
1006 return NETDEV_TX_OK;
1007 }
ef59febe 1008 goto drop;
e4f67add 1009 }
ef59febe 1010
d6727fe3
JG
1011 if (!skb->encapsulation) {
1012 skb_reset_inner_headers(skb);
1013 skb->encapsulation = 1;
1014 }
1015
d342894c 1016 /* Need space for new headers (invalidates iph ptr) */
1017 if (skb_cow_head(skb, VXLAN_HEADROOM))
1018 goto drop;
1019
d342894c 1020 old_iph = ip_hdr(skb);
1021
d342894c 1022 ttl = vxlan->ttl;
1023 if (!ttl && IN_MULTICAST(ntohl(dst)))
1024 ttl = 1;
1025
1026 tos = vxlan->tos;
1027 if (tos == 1)
206aaafc 1028 tos = ip_tunnel_get_dsfield(old_iph, skb);
d342894c 1029
05f47d69 1030 src_port = vxlan_src_port(vxlan, skb);
d342894c 1031
ca78f181 1032 memset(&fl4, 0, sizeof(fl4));
6681712d 1033 fl4.flowi4_oif = rdst->remote_ifindex;
ca78f181 1034 fl4.flowi4_tos = RT_TOS(tos);
1035 fl4.daddr = dst;
1036 fl4.saddr = vxlan->saddr;
1037
1038 rt = ip_route_output_key(dev_net(dev), &fl4);
d342894c 1039 if (IS_ERR(rt)) {
1040 netdev_dbg(dev, "no route to %pI4\n", &dst);
1041 dev->stats.tx_carrier_errors++;
1042 goto tx_error;
1043 }
1044
1045 if (rt->dst.dev == dev) {
1046 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1047 ip_rt_put(rt);
1048 dev->stats.collisions++;
1049 goto tx_error;
1050 }
1051
9dcc71e1 1052 /* Bypass encapsulation if the destination is local */
ab09a6d0
MR
1053 if (rt->rt_flags & RTCF_LOCAL &&
1054 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
9dcc71e1
SS
1055 struct vxlan_dev *dst_vxlan;
1056
1057 ip_rt_put(rt);
1058 dst_vxlan = vxlan_find_vni(dev_net(dev), vni);
1059 if (!dst_vxlan)
1060 goto tx_error;
1061 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1062 return NETDEV_TX_OK;
1063 }
1064
d342894c 1065 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1066 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1067 IPSKB_REROUTED);
1068 skb_dst_drop(skb);
1069 skb_dst_set(skb, &rt->dst);
1070
1071 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1072 vxh->vx_flags = htonl(VXLAN_FLAGS);
6681712d 1073 vxh->vx_vni = htonl(vni << 8);
d342894c 1074
1075 __skb_push(skb, sizeof(*uh));
1076 skb_reset_transport_header(skb);
1077 uh = udp_hdr(skb);
1078
73cf3317 1079 uh->dest = dst_port;
7d836a76 1080 uh->source = src_port;
d342894c 1081
1082 uh->len = htons(skb->len);
1083 uh->check = 0;
1084
1085 __skb_push(skb, sizeof(*iph));
1086 skb_reset_network_header(skb);
1087 iph = ip_hdr(skb);
1088 iph->version = 4;
1089 iph->ihl = sizeof(struct iphdr) >> 2;
1090 iph->frag_off = df;
1091 iph->protocol = IPPROTO_UDP;
206aaafc 1092 iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
ca78f181 1093 iph->daddr = dst;
d342894c 1094 iph->saddr = fl4.saddr;
1095 iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
ff1f69a8 1096 __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
d342894c 1097
88c4c066
ZM
1098 nf_reset(skb);
1099
1cad8715 1100 vxlan_set_owner(dev, skb);
1101
05c0db08
PS
1102 if (handle_offloads(skb))
1103 goto drop;
d342894c 1104
6aed0c8b 1105 iptunnel_xmit(skb, dev);
d342894c 1106 return NETDEV_TX_OK;
1107
1108drop:
1109 dev->stats.tx_dropped++;
1110 goto tx_free;
1111
1112tx_error:
1113 dev->stats.tx_errors++;
1114tx_free:
1115 dev_kfree_skb(skb);
1116 return NETDEV_TX_OK;
1117}
1118
6681712d
DS
1119/* Transmit local packets over Vxlan
1120 *
1121 * Outer IP header inherits ECN and DF from inner header.
1122 * Outer UDP destination is the VXLAN assigned port.
1123 * source port is based on hash of flow
1124 */
1125static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1126{
1127 struct vxlan_dev *vxlan = netdev_priv(dev);
1128 struct ethhdr *eth;
1129 bool did_rsc = false;
c7995c43 1130 struct vxlan_rdst *rdst0, *rdst;
6681712d
DS
1131 struct vxlan_fdb *f;
1132 int rc1, rc;
1133
1134 skb_reset_mac_header(skb);
1135 eth = eth_hdr(skb);
1136
1137 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1138 return arp_reduce(dev, skb);
6681712d
DS
1139
1140 f = vxlan_find_mac(vxlan, eth->h_dest);
ae884082
DS
1141 did_rsc = false;
1142
1143 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1144 ntohs(eth->h_proto) == ETH_P_IP) {
1145 did_rsc = route_shortcircuit(dev, skb);
1146 if (did_rsc)
1147 f = vxlan_find_mac(vxlan, eth->h_dest);
1148 }
1149
6681712d 1150 if (f == NULL) {
c7995c43
AW
1151 rdst0 = &vxlan->default_dst;
1152
1153 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
6681712d
DS
1154 (vxlan->flags & VXLAN_F_L2MISS) &&
1155 !is_multicast_ether_addr(eth->h_dest))
1156 vxlan_fdb_miss(vxlan, eth->h_dest);
1157 } else
1158 rdst0 = &f->remote;
1159
1160 rc = NETDEV_TX_OK;
1161
1162 /* if there are multiple destinations, send copies */
1163 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1164 struct sk_buff *skb1;
1165
1166 skb1 = skb_clone(skb, GFP_ATOMIC);
7aa27238 1167 if (skb1) {
1168 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1169 if (rc == NETDEV_TX_OK)
1170 rc = rc1;
1171 }
6681712d
DS
1172 }
1173
1174 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1175 if (rc == NETDEV_TX_OK)
1176 rc = rc1;
1177 return rc;
1178}
1179
d342894c 1180/* Walk the forwarding table and purge stale entries */
1181static void vxlan_cleanup(unsigned long arg)
1182{
1183 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1184 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1185 unsigned int h;
1186
1187 if (!netif_running(vxlan->dev))
1188 return;
1189
1190 spin_lock_bh(&vxlan->hash_lock);
1191 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1192 struct hlist_node *p, *n;
1193 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1194 struct vxlan_fdb *f
1195 = container_of(p, struct vxlan_fdb, hlist);
1196 unsigned long timeout;
1197
3c172868 1198 if (f->state & NUD_PERMANENT)
d342894c 1199 continue;
1200
1201 timeout = f->used + vxlan->age_interval * HZ;
1202 if (time_before_eq(timeout, jiffies)) {
1203 netdev_dbg(vxlan->dev,
1204 "garbage collect %pM\n",
1205 f->eth_addr);
1206 f->state = NUD_STALE;
1207 vxlan_fdb_destroy(vxlan, f);
1208 } else if (time_before(timeout, next_timer))
1209 next_timer = timeout;
1210 }
1211 }
1212 spin_unlock_bh(&vxlan->hash_lock);
1213
1214 mod_timer(&vxlan->age_timer, next_timer);
1215}
1216
1217/* Setup stats when device is created */
1218static int vxlan_init(struct net_device *dev)
1219{
e8171045
PS
1220 dev->tstats = alloc_percpu(struct pcpu_tstats);
1221 if (!dev->tstats)
d342894c 1222 return -ENOMEM;
1223
1224 return 0;
1225}
1226
1227/* Start ageing timer and join group when device is brought up */
1228static int vxlan_open(struct net_device *dev)
1229{
1230 struct vxlan_dev *vxlan = netdev_priv(dev);
1231 int err;
1232
c7995c43 1233 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
d342894c 1234 err = vxlan_join_group(dev);
1235 if (err)
1236 return err;
1237 }
1238
1239 if (vxlan->age_interval)
1240 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1241
1242 return 0;
1243}
1244
1245/* Purge the forwarding table */
1246static void vxlan_flush(struct vxlan_dev *vxlan)
1247{
1248 unsigned h;
1249
1250 spin_lock_bh(&vxlan->hash_lock);
1251 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1252 struct hlist_node *p, *n;
1253 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1254 struct vxlan_fdb *f
1255 = container_of(p, struct vxlan_fdb, hlist);
1256 vxlan_fdb_destroy(vxlan, f);
1257 }
1258 }
1259 spin_unlock_bh(&vxlan->hash_lock);
1260}
1261
1262/* Cleanup timer and forwarding table on shutdown */
1263static int vxlan_stop(struct net_device *dev)
1264{
1265 struct vxlan_dev *vxlan = netdev_priv(dev);
1266
c7995c43 1267 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
d342894c 1268 vxlan_leave_group(dev);
1269
1270 del_timer_sync(&vxlan->age_timer);
1271
1272 vxlan_flush(vxlan);
1273
1274 return 0;
1275}
1276
d342894c 1277/* Stub, nothing needs to be done. */
1278static void vxlan_set_multicast_list(struct net_device *dev)
1279{
1280}
1281
1282static const struct net_device_ops vxlan_netdev_ops = {
1283 .ndo_init = vxlan_init,
1284 .ndo_open = vxlan_open,
1285 .ndo_stop = vxlan_stop,
1286 .ndo_start_xmit = vxlan_xmit,
e8171045 1287 .ndo_get_stats64 = ip_tunnel_get_stats64,
d342894c 1288 .ndo_set_rx_mode = vxlan_set_multicast_list,
1289 .ndo_change_mtu = eth_change_mtu,
1290 .ndo_validate_addr = eth_validate_addr,
1291 .ndo_set_mac_address = eth_mac_addr,
1292 .ndo_fdb_add = vxlan_fdb_add,
1293 .ndo_fdb_del = vxlan_fdb_delete,
1294 .ndo_fdb_dump = vxlan_fdb_dump,
1295};
1296
1297/* Info for udev, that this is a virtual tunnel endpoint */
1298static struct device_type vxlan_type = {
1299 .name = "vxlan",
1300};
1301
1302static void vxlan_free(struct net_device *dev)
1303{
e8171045 1304 free_percpu(dev->tstats);
d342894c 1305 free_netdev(dev);
1306}
1307
1308/* Initialize the device structure. */
1309static void vxlan_setup(struct net_device *dev)
1310{
1311 struct vxlan_dev *vxlan = netdev_priv(dev);
1312 unsigned h;
05f47d69 1313 int low, high;
d342894c 1314
1315 eth_hw_addr_random(dev);
1316 ether_setup(dev);
1d7716d9 1317 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
d342894c 1318
1319 dev->netdev_ops = &vxlan_netdev_ops;
1320 dev->destructor = vxlan_free;
1321 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1322
1323 dev->tx_queue_len = 0;
1324 dev->features |= NETIF_F_LLTX;
1325 dev->features |= NETIF_F_NETNS_LOCAL;
d6727fe3 1326 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
0afb1666 1327 dev->features |= NETIF_F_RXCSUM;
05c0db08 1328 dev->features |= NETIF_F_GSO_SOFTWARE;
0afb1666
JG
1329
1330 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
05c0db08 1331 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
d342894c 1332 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
6602d007 1333 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
d342894c 1334
1335 spin_lock_init(&vxlan->hash_lock);
1336
1337 init_timer_deferrable(&vxlan->age_timer);
1338 vxlan->age_timer.function = vxlan_cleanup;
1339 vxlan->age_timer.data = (unsigned long) vxlan;
1340
05f47d69 1341 inet_get_local_port_range(&low, &high);
1342 vxlan->port_min = low;
1343 vxlan->port_max = high;
823aa873 1344 vxlan->dst_port = htons(vxlan_port);
05f47d69 1345
d342894c 1346 vxlan->dev = dev;
1347
1348 for (h = 0; h < FDB_HASH_SIZE; ++h)
1349 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1350}
1351
1352static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1353 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
5d174dd8 1354 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
d342894c 1355 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1356 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1357 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1358 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1359 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1360 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1361 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
05f47d69 1362 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
e4f67add
DS
1363 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1364 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1365 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1366 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
823aa873 1367 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
d342894c 1368};
1369
1370static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1371{
1372 if (tb[IFLA_ADDRESS]) {
1373 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1374 pr_debug("invalid link address (not ethernet)\n");
1375 return -EINVAL;
1376 }
1377
1378 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1379 pr_debug("invalid all zero ethernet address\n");
1380 return -EADDRNOTAVAIL;
1381 }
1382 }
1383
1384 if (!data)
1385 return -EINVAL;
1386
1387 if (data[IFLA_VXLAN_ID]) {
1388 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
e5c6b9c5 1389 if (id >= VXLAN_N_VID)
d342894c 1390 return -ERANGE;
1391 }
1392
05f47d69 1393 if (data[IFLA_VXLAN_PORT_RANGE]) {
1394 const struct ifla_vxlan_port_range *p
1395 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1396
1397 if (ntohs(p->high) < ntohs(p->low)) {
1398 pr_debug("port range %u .. %u not valid\n",
1399 ntohs(p->low), ntohs(p->high));
1400 return -EINVAL;
1401 }
1402 }
1403
d342894c 1404 return 0;
1405}
1406
1b13c97f
YB
1407static void vxlan_get_drvinfo(struct net_device *netdev,
1408 struct ethtool_drvinfo *drvinfo)
1409{
1410 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1411 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1412}
1413
1414static const struct ethtool_ops vxlan_ethtool_ops = {
1415 .get_drvinfo = vxlan_get_drvinfo,
1416 .get_link = ethtool_op_get_link,
1417};
1418
d342894c 1419static int vxlan_newlink(struct net *net, struct net_device *dev,
1420 struct nlattr *tb[], struct nlattr *data[])
1421{
1422 struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 1423 struct vxlan_rdst *dst = &vxlan->default_dst;
d342894c 1424 __u32 vni;
1425 int err;
1426
1427 if (!data[IFLA_VXLAN_ID])
1428 return -EINVAL;
1429
1430 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1431 if (vxlan_find_vni(net, vni)) {
1432 pr_info("duplicate VNI %u\n", vni);
1433 return -EEXIST;
1434 }
c7995c43 1435 dst->remote_vni = vni;
d342894c 1436
5d174dd8 1437 if (data[IFLA_VXLAN_GROUP])
1438 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
d342894c 1439
1440 if (data[IFLA_VXLAN_LOCAL])
1441 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1442
34e02aa1 1443 if (data[IFLA_VXLAN_LINK] &&
c7995c43 1444 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
34e02aa1 1445 struct net_device *lowerdev
c7995c43 1446 = __dev_get_by_index(net, dst->remote_ifindex);
34e02aa1 1447
1448 if (!lowerdev) {
c7995c43 1449 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
34e02aa1 1450 return -ENODEV;
1451 }
d342894c 1452
34e02aa1 1453 if (!tb[IFLA_MTU])
d342894c 1454 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1ba56fb4
AD
1455
1456 /* update header length based on lower device */
1d7716d9 1457 dev->needed_headroom = lowerdev->hard_header_len +
1ba56fb4 1458 VXLAN_HEADROOM;
d342894c 1459 }
1460
1461 if (data[IFLA_VXLAN_TOS])
1462 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1463
afb97186
VB
1464 if (data[IFLA_VXLAN_TTL])
1465 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1466
d342894c 1467 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
e4f67add 1468 vxlan->flags |= VXLAN_F_LEARN;
d342894c 1469
1470 if (data[IFLA_VXLAN_AGEING])
1471 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1472 else
1473 vxlan->age_interval = FDB_AGE_DEFAULT;
1474
e4f67add
DS
1475 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1476 vxlan->flags |= VXLAN_F_PROXY;
1477
1478 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1479 vxlan->flags |= VXLAN_F_RSC;
1480
1481 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1482 vxlan->flags |= VXLAN_F_L2MISS;
1483
1484 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1485 vxlan->flags |= VXLAN_F_L3MISS;
1486
d342894c 1487 if (data[IFLA_VXLAN_LIMIT])
1488 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1489
05f47d69 1490 if (data[IFLA_VXLAN_PORT_RANGE]) {
1491 const struct ifla_vxlan_port_range *p
1492 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1493 vxlan->port_min = ntohs(p->low);
1494 vxlan->port_max = ntohs(p->high);
1495 }
1496
823aa873 1497 if (data[IFLA_VXLAN_PORT])
1498 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1499
1b13c97f
YB
1500 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1501
d342894c 1502 err = register_netdevice(dev);
1503 if (!err)
c7995c43 1504 hlist_add_head_rcu(&vxlan->hlist, vni_head(net, dst->remote_vni));
d342894c 1505
1506 return err;
1507}
1508
1509static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1510{
1511 struct vxlan_dev *vxlan = netdev_priv(dev);
1512
1513 hlist_del_rcu(&vxlan->hlist);
1514
1515 unregister_netdevice_queue(dev, head);
1516}
1517
1518static size_t vxlan_get_size(const struct net_device *dev)
1519{
1520
1521 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
5d174dd8 1522 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
d342894c 1523 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1524 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1525 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1526 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1527 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
e4f67add
DS
1528 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1529 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1530 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1531 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
d342894c 1532 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1533 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
05f47d69 1534 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
823aa873 1535 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
d342894c 1536 0;
1537}
1538
1539static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1540{
1541 const struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 1542 const struct vxlan_rdst *dst = &vxlan->default_dst;
05f47d69 1543 struct ifla_vxlan_port_range ports = {
1544 .low = htons(vxlan->port_min),
1545 .high = htons(vxlan->port_max),
1546 };
d342894c 1547
c7995c43 1548 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
d342894c 1549 goto nla_put_failure;
1550
5d174dd8 1551 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
d342894c 1552 goto nla_put_failure;
1553
c7995c43 1554 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
d342894c 1555 goto nla_put_failure;
1556
7c41c42c 1557 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
d342894c 1558 goto nla_put_failure;
1559
1560 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1561 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
e4f67add
DS
1562 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1563 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1564 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1565 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1566 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1567 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1568 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1569 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1570 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
d342894c 1571 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
823aa873 1572 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1573 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
d342894c 1574 goto nla_put_failure;
1575
05f47d69 1576 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1577 goto nla_put_failure;
1578
d342894c 1579 return 0;
1580
1581nla_put_failure:
1582 return -EMSGSIZE;
1583}
1584
1585static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1586 .kind = "vxlan",
1587 .maxtype = IFLA_VXLAN_MAX,
1588 .policy = vxlan_policy,
1589 .priv_size = sizeof(struct vxlan_dev),
1590 .setup = vxlan_setup,
1591 .validate = vxlan_validate,
1592 .newlink = vxlan_newlink,
1593 .dellink = vxlan_dellink,
1594 .get_size = vxlan_get_size,
1595 .fill_info = vxlan_fill_info,
1596};
1597
1598static __net_init int vxlan_init_net(struct net *net)
1599{
1600 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1601 struct sock *sk;
1602 struct sockaddr_in vxlan_addr = {
1603 .sin_family = AF_INET,
1604 .sin_addr.s_addr = htonl(INADDR_ANY),
1605 };
1606 int rc;
1607 unsigned h;
1608
1609 /* Create UDP socket for encapsulation receive. */
1610 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
1611 if (rc < 0) {
1612 pr_debug("UDP socket create failed\n");
1613 return rc;
1614 }
bfe1b9b1 1615 /* Put in proper namespace */
1616 sk = vn->sock->sk;
1617 sk_change_net(sk, net);
d342894c 1618
1619 vxlan_addr.sin_port = htons(vxlan_port);
1620
1621 rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
1622 sizeof(vxlan_addr));
1623 if (rc < 0) {
1624 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1625 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
bfe1b9b1 1626 sk_release_kernel(sk);
d342894c 1627 vn->sock = NULL;
1628 return rc;
1629 }
1630
1631 /* Disable multicast loopback */
d342894c 1632 inet_sk(sk)->mc_loop = 0;
1633
1634 /* Mark socket as an encapsulation socket. */
1635 udp_sk(sk)->encap_type = 1;
1636 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1637 udp_encap_enable();
1638
1639 for (h = 0; h < VNI_HASH_SIZE; ++h)
1640 INIT_HLIST_HEAD(&vn->vni_list[h]);
1641
1642 return 0;
1643}
1644
1645static __net_exit void vxlan_exit_net(struct net *net)
1646{
1647 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
9cb6cb7e
ZM
1648 struct vxlan_dev *vxlan;
1649 unsigned h;
1650
1651 rtnl_lock();
1652 for (h = 0; h < VNI_HASH_SIZE; ++h)
1653 hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist)
1654 dev_close(vxlan->dev);
1655 rtnl_unlock();
d342894c 1656
1657 if (vn->sock) {
bfe1b9b1 1658 sk_release_kernel(vn->sock->sk);
d342894c 1659 vn->sock = NULL;
1660 }
1661}
1662
1663static struct pernet_operations vxlan_net_ops = {
1664 .init = vxlan_init_net,
1665 .exit = vxlan_exit_net,
1666 .id = &vxlan_net_id,
1667 .size = sizeof(struct vxlan_net),
1668};
1669
1670static int __init vxlan_init_module(void)
1671{
1672 int rc;
1673
1674 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1675
1676 rc = register_pernet_device(&vxlan_net_ops);
1677 if (rc)
1678 goto out1;
1679
1680 rc = rtnl_link_register(&vxlan_link_ops);
1681 if (rc)
1682 goto out2;
1683
1684 return 0;
1685
1686out2:
1687 unregister_pernet_device(&vxlan_net_ops);
1688out1:
1689 return rc;
1690}
1691module_init(vxlan_init_module);
1692
1693static void __exit vxlan_cleanup_module(void)
1694{
1695 rtnl_link_unregister(&vxlan_link_ops);
1696 unregister_pernet_device(&vxlan_net_ops);
6681712d 1697 rcu_barrier();
d342894c 1698}
1699module_exit(vxlan_cleanup_module);
1700
1701MODULE_LICENSE("GPL");
1702MODULE_VERSION(VXLAN_VERSION);
3b8df3c6 1703MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
d342894c 1704MODULE_ALIAS_RTNL_LINK("vxlan");