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