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