]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_tuntap.c
if_tuntap: add a busy/unbusy mechanism, replace destroy OPEN check
[FreeBSD/FreeBSD.git] / sys / net / if_tuntap.c
1 /*      $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $  */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
6  * All rights reserved.
7  * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * BASED ON:
32  * -------------------------------------------------------------------------
33  *
34  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
35  * Nottingham University 1987.
36  *
37  * This source may be freely distributed, however I would be interested
38  * in any changes that are made.
39  *
40  * This driver takes packets off the IP i/f and hands them up to a
41  * user process to have its wicked way with. This driver has it's
42  * roots in a similar driver written by Phil Cockcroft (formerly) at
43  * UCL. This driver is based much more on read/write/poll mode of
44  * operation though.
45  *
46  * $FreeBSD$
47  */
48
49 #include "opt_inet.h"
50 #include "opt_inet6.h"
51
52 #include <sys/param.h>
53 #include <sys/lock.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/systm.h>
57 #include <sys/jail.h>
58 #include <sys/mbuf.h>
59 #include <sys/module.h>
60 #include <sys/socket.h>
61 #include <sys/eventhandler.h>
62 #include <sys/fcntl.h>
63 #include <sys/filio.h>
64 #include <sys/sockio.h>
65 #include <sys/sx.h>
66 #include <sys/ttycom.h>
67 #include <sys/poll.h>
68 #include <sys/selinfo.h>
69 #include <sys/signalvar.h>
70 #include <sys/filedesc.h>
71 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
73 #include <sys/conf.h>
74 #include <sys/uio.h>
75 #include <sys/malloc.h>
76 #include <sys/random.h>
77 #include <sys/ctype.h>
78
79 #include <net/ethernet.h>
80 #include <net/if.h>
81 #include <net/if_var.h>
82 #include <net/if_clone.h>
83 #include <net/if_dl.h>
84 #include <net/if_media.h>
85 #include <net/if_types.h>
86 #include <net/netisr.h>
87 #include <net/route.h>
88 #include <net/vnet.h>
89 #ifdef INET
90 #include <netinet/in.h>
91 #endif
92 #include <net/bpf.h>
93 #include <net/if_tap.h>
94 #include <net/if_tun.h>
95
96 #include <sys/queue.h>
97 #include <sys/condvar.h>
98 #include <security/mac/mac_framework.h>
99
100 struct tuntap_driver;
101
102 /*
103  * tun_list is protected by global tunmtx.  Other mutable fields are
104  * protected by tun->tun_mtx, or by their owning subsystem.  tun_dev is
105  * static for the duration of a tunnel interface.
106  */
107 struct tuntap_softc {
108         TAILQ_ENTRY(tuntap_softc)        tun_list;
109         struct cdev                     *tun_dev;
110         u_short                          tun_flags;     /* misc flags */
111 #define TUN_OPEN        0x0001
112 #define TUN_INITED      0x0002
113 #define TUN_IASET       0x0008
114 #define TUN_DSTADDR     0x0010
115 #define TUN_LMODE       0x0020
116 #define TUN_RWAIT       0x0040
117 #define TUN_ASYNC       0x0080
118 #define TUN_IFHEAD      0x0100
119 #define TUN_DYING       0x0200
120 #define TUN_L2          0x0400
121 #define TUN_VMNET       0x0800
122
123 #define TUN_DRIVER_IDENT_MASK   (TUN_L2 | TUN_VMNET)
124 #define TUN_READY               (TUN_OPEN | TUN_INITED)
125
126         pid_t                    tun_pid;       /* owning pid */
127         struct ifnet            *tun_ifp;       /* the interface */
128         struct sigio            *tun_sigio;     /* async I/O info */
129         struct tuntap_driver    *tun_drv;       /* appropriate driver */
130         struct selinfo           tun_rsel;      /* read select */
131         struct mtx               tun_mtx;       /* softc field mutex */
132         struct cv                tun_cv;        /* for ref'd dev destroy */
133         struct ether_addr        tun_ether;     /* remote address */
134         int                      tun_busy;      /* busy count */
135 };
136 #define TUN2IFP(sc)     ((sc)->tun_ifp)
137
138 #define TUNDEBUG        if (tundebug) if_printf
139
140 #define TUN_LOCK(tp)            mtx_lock(&(tp)->tun_mtx)
141 #define TUN_UNLOCK(tp)          mtx_unlock(&(tp)->tun_mtx)
142 #define TUN_LOCK_ASSERT(tp)     mtx_assert(&(tp)->tun_mtx, MA_OWNED);
143
144 #define TUN_VMIO_FLAG_MASK      0x0fff
145
146 /*
147  * All mutable global variables in if_tun are locked using tunmtx, with
148  * the exception of tundebug, which is used unlocked, and the drivers' *clones,
149  * which are static after setup.
150  */
151 static struct mtx tunmtx;
152 static eventhandler_tag tag;
153 static const char tunname[] = "tun";
154 static const char tapname[] = "tap";
155 static const char vmnetname[] = "vmnet";
156 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
157 static int tundebug = 0;
158 static int tundclone = 1;
159 static int tap_allow_uopen = 0; /* allow user open() */
160 static int tapuponopen = 0;     /* IFF_UP on open() */
161 static int tapdclone = 1;       /* enable devfs cloning */
162
163 static TAILQ_HEAD(,tuntap_softc)        tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
164 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
165
166 static struct sx tun_ioctl_sx;
167 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
168
169 SYSCTL_DECL(_net_link);
170 /* tun */
171 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
172     "IP tunnel software network interface");
173 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
174     "Enable legacy devfs interface creation");
175
176 /* tap */
177 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
178     "Ethernet tunnel software network interface");
179 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
180     "Allow user to open /dev/tap (based on node permissions)");
181 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
182     "Bring interface up when /dev/tap is opened");
183 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
184     "Enable legacy devfs interface creation");
185 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
186
187 static int      tun_busy_locked(struct tuntap_softc *tp);
188 static void     tun_unbusy_locked(struct tuntap_softc *tp);
189 static int      tun_busy(struct tuntap_softc *tp);
190 static void     tun_unbusy(struct tuntap_softc *tp);
191
192 static int      tuntap_name2info(const char *name, int *unit, int *flags);
193 static void     tunclone(void *arg, struct ucred *cred, char *name,
194                     int namelen, struct cdev **dev);
195 static void     tuncreate(struct cdev *dev, struct tuntap_driver *);
196 static int      tunifioctl(struct ifnet *, u_long, caddr_t);
197 static void     tuninit(struct ifnet *);
198 static void     tunifinit(void *xtp);
199 static int      tuntapmodevent(module_t, int, void *);
200 static int      tunoutput(struct ifnet *, struct mbuf *,
201                     const struct sockaddr *, struct route *ro);
202 static void     tunstart(struct ifnet *);
203 static void     tunstart_l2(struct ifnet *);
204
205 static int      tun_clone_match(struct if_clone *ifc, const char *name);
206 static int      tap_clone_match(struct if_clone *ifc, const char *name);
207 static int      vmnet_clone_match(struct if_clone *ifc, const char *name);
208 static int      tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
209 static int      tun_clone_destroy(struct if_clone *, struct ifnet *);
210
211 static d_open_t         tunopen;
212 static d_close_t        tunclose;
213 static d_read_t         tunread;
214 static d_write_t        tunwrite;
215 static d_ioctl_t        tunioctl;
216 static d_poll_t         tunpoll;
217 static d_kqfilter_t     tunkqfilter;
218
219 static int              tunkqread(struct knote *, long);
220 static int              tunkqwrite(struct knote *, long);
221 static void             tunkqdetach(struct knote *);
222
223 static struct filterops tun_read_filterops = {
224         .f_isfd =       1,
225         .f_attach =     NULL,
226         .f_detach =     tunkqdetach,
227         .f_event =      tunkqread,
228 };
229
230 static struct filterops tun_write_filterops = {
231         .f_isfd =       1,
232         .f_attach =     NULL,
233         .f_detach =     tunkqdetach,
234         .f_event =      tunkqwrite,
235 };
236
237 static struct tuntap_driver {
238         struct cdevsw            cdevsw;
239         int                      ident_flags;
240         struct unrhdr           *unrhdr;
241         struct clonedevs        *clones;
242         ifc_match_t             *clone_match_fn;
243         ifc_create_t            *clone_create_fn;
244         ifc_destroy_t           *clone_destroy_fn;
245 } tuntap_drivers[] = {
246         {
247                 .ident_flags =  0,
248                 .cdevsw =       {
249                     .d_version =        D_VERSION,
250                     .d_flags =          D_NEEDMINOR,
251                     .d_open =           tunopen,
252                     .d_close =          tunclose,
253                     .d_read =           tunread,
254                     .d_write =          tunwrite,
255                     .d_ioctl =          tunioctl,
256                     .d_poll =           tunpoll,
257                     .d_kqfilter =       tunkqfilter,
258                     .d_name =           tunname,
259                 },
260                 .clone_match_fn =       tun_clone_match,
261                 .clone_create_fn =      tun_clone_create,
262                 .clone_destroy_fn =     tun_clone_destroy,
263         },
264         {
265                 .ident_flags =  TUN_L2,
266                 .cdevsw =       {
267                     .d_version =        D_VERSION,
268                     .d_flags =          D_NEEDMINOR,
269                     .d_open =           tunopen,
270                     .d_close =          tunclose,
271                     .d_read =           tunread,
272                     .d_write =          tunwrite,
273                     .d_ioctl =          tunioctl,
274                     .d_poll =           tunpoll,
275                     .d_kqfilter =       tunkqfilter,
276                     .d_name =           tapname,
277                 },
278                 .clone_match_fn =       tap_clone_match,
279                 .clone_create_fn =      tun_clone_create,
280                 .clone_destroy_fn =     tun_clone_destroy,
281         },
282         {
283                 .ident_flags =  TUN_L2 | TUN_VMNET,
284                 .cdevsw =       {
285                     .d_version =        D_VERSION,
286                     .d_flags =          D_NEEDMINOR,
287                     .d_open =           tunopen,
288                     .d_close =          tunclose,
289                     .d_read =           tunread,
290                     .d_write =          tunwrite,
291                     .d_ioctl =          tunioctl,
292                     .d_poll =           tunpoll,
293                     .d_kqfilter =       tunkqfilter,
294                     .d_name =           vmnetname,
295                 },
296                 .clone_match_fn =       vmnet_clone_match,
297                 .clone_create_fn =      tun_clone_create,
298                 .clone_destroy_fn =     tun_clone_destroy,
299         },
300 };
301
302 struct tuntap_driver_cloner {
303         SLIST_ENTRY(tuntap_driver_cloner)        link;
304         struct tuntap_driver                    *drv;
305         struct if_clone                         *cloner;
306 };
307
308 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
309     SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
310
311 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
312
313 /*
314  * Mechanism for marking a tunnel device as busy so that we can safely do some
315  * orthogonal operations (such as operations on devices) without racing against
316  * tun_destroy.  tun_destroy will wait on the condvar if we're at all busy or
317  * open, to be woken up when the condition is alleviated.
318  */
319 static int
320 tun_busy_locked(struct tuntap_softc *tp)
321 {
322
323         TUN_LOCK_ASSERT(tp);
324         if ((tp->tun_flags & TUN_DYING) != 0) {
325                 /*
326                  * Perhaps unintuitive, but the device is busy going away.
327                  * Other interpretations of EBUSY from tun_busy make little
328                  * sense, since making a busy device even more busy doesn't
329                  * sound like a problem.
330                  */
331                 return (EBUSY);
332         }
333
334         ++tp->tun_busy;
335         return (0);
336 }
337
338 static void
339 tun_unbusy_locked(struct tuntap_softc *tp)
340 {
341
342         TUN_LOCK_ASSERT(tp);
343         KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
344
345         --tp->tun_busy;
346         /* Wake up anything that may be waiting on our busy tunnel. */
347         if (tp->tun_busy == 0)
348                 cv_broadcast(&tp->tun_cv);
349 }
350
351 static int
352 tun_busy(struct tuntap_softc *tp)
353 {
354         int ret;
355
356         TUN_LOCK(tp);
357         ret = tun_busy_locked(tp);
358         TUN_UNLOCK(tp);
359         return (ret);
360 }
361
362
363 static void
364 tun_unbusy(struct tuntap_softc *tp)
365 {
366
367         TUN_LOCK(tp);
368         tun_unbusy_locked(tp);
369         TUN_UNLOCK(tp);
370 }
371
372 /*
373  * Sets unit and/or flags given the device name.  Must be called with correct
374  * vnet context.
375  */
376 static int
377 tuntap_name2info(const char *name, int *outunit, int *outflags)
378 {
379         struct tuntap_driver *drv;
380         struct tuntap_driver_cloner *drvc;
381         char *dname;
382         int flags, unit;
383         bool found;
384
385         if (name == NULL)
386                 return (EINVAL);
387
388         /*
389          * Needed for dev_stdclone, but dev_stdclone will not modify, it just
390          * wants to be able to pass back a char * through the second param. We
391          * will always set that as NULL here, so we'll fake it.
392          */
393         dname = __DECONST(char *, name);
394         found = false;
395
396         KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
397             ("tuntap_driver_cloners failed to initialize"));
398         SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
399                 KASSERT(drvc->drv != NULL,
400                     ("tuntap_driver_cloners entry not properly initialized"));
401                 drv = drvc->drv;
402
403                 if (strcmp(name, drv->cdevsw.d_name) == 0) {
404                         found = true;
405                         unit = -1;
406                         flags = drv->ident_flags;
407                         break;
408                 }
409
410                 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
411                         found = true;
412                         flags = drv->ident_flags;
413                         break;
414                 }
415         }
416
417         if (!found)
418                 return (ENXIO);
419
420         if (outunit != NULL)
421                 *outunit = unit;
422         if (outflags != NULL)
423                 *outflags = flags;
424         return (0);
425 }
426
427 /*
428  * Get driver information from a set of flags specified.  Masks the identifying
429  * part of the flags and compares it against all of the available
430  * tuntap_drivers. Must be called with correct vnet context.
431  */
432 static struct tuntap_driver *
433 tuntap_driver_from_flags(int tun_flags)
434 {
435         struct tuntap_driver *drv;
436         struct tuntap_driver_cloner *drvc;
437
438         KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
439             ("tuntap_driver_cloners failed to initialize"));
440         SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
441                 KASSERT(drvc->drv != NULL,
442                     ("tuntap_driver_cloners entry not properly initialized"));
443                 drv = drvc->drv;
444                 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
445                         return (drv);
446         }
447
448         return (NULL);
449 }
450
451
452
453 static int
454 tun_clone_match(struct if_clone *ifc, const char *name)
455 {
456         int tunflags;
457
458         if (tuntap_name2info(name, NULL, &tunflags) == 0) {
459                 if ((tunflags & TUN_L2) == 0)
460                         return (1);
461         }
462
463         return (0);
464 }
465
466 static int
467 tap_clone_match(struct if_clone *ifc, const char *name)
468 {
469         int tunflags;
470
471         if (tuntap_name2info(name, NULL, &tunflags) == 0) {
472                 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
473                         return (1);
474         }
475
476         return (0);
477 }
478
479 static int
480 vmnet_clone_match(struct if_clone *ifc, const char *name)
481 {
482         int tunflags;
483
484         if (tuntap_name2info(name, NULL, &tunflags) == 0) {
485                 if ((tunflags & TUN_VMNET) != 0)
486                         return (1);
487         }
488
489         return (0);
490 }
491
492 static int
493 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
494 {
495         struct tuntap_driver *drv;
496         struct cdev *dev;
497         int err, i, tunflags, unit;
498
499         tunflags = 0;
500         /* The name here tells us exactly what we're creating */
501         err = tuntap_name2info(name, &unit, &tunflags);
502         if (err != 0)
503                 return (err);
504
505         drv = tuntap_driver_from_flags(tunflags);
506         if (drv == NULL)
507                 return (ENXIO);
508
509         if (unit != -1) {
510                 /* If this unit number is still available that's okay. */
511                 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
512                         return (EEXIST);
513         } else {
514                 unit = alloc_unr(drv->unrhdr);
515         }
516
517         snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
518
519         /* find any existing device, or allocate new unit number */
520         i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
521         if (i) {
522                 /* No preexisting struct cdev *, create one */
523                 dev = make_dev(&drv->cdevsw, unit, UID_UUCP, GID_DIALER, 0600,
524                     "%s%d", drv->cdevsw.d_name, unit);
525         }
526
527         tuncreate(dev, drv);
528
529         return (0);
530 }
531
532 static void
533 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
534     struct cdev **dev)
535 {
536         char devname[SPECNAMELEN + 1];
537         struct tuntap_driver *drv;
538         int append_unit, i, u, tunflags;
539         bool mayclone;
540
541         if (*dev != NULL)
542                 return;
543
544         tunflags = 0;
545         CURVNET_SET(CRED_TO_VNET(cred));
546         if (tuntap_name2info(name, &u, &tunflags) != 0)
547                 goto out;       /* Not recognized */
548
549         if (u != -1 && u > IF_MAXUNIT)
550                 goto out;       /* Unit number too high */
551
552         mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
553         if ((tunflags & TUN_L2) != 0) {
554                 /* tap/vmnet allow user open with a sysctl */
555                 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
556         } else {
557                 mayclone = mayclone && tundclone;
558         }
559
560         /*
561          * If tun cloning is enabled, only the superuser can create an
562          * interface.
563          */
564         if (!mayclone)
565                 goto out;
566
567         if (u == -1)
568                 append_unit = 1;
569         else
570                 append_unit = 0;
571
572         drv = tuntap_driver_from_flags(tunflags);
573         if (drv == NULL)
574                 goto out;
575
576         /* find any existing device, or allocate new unit number */
577         i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
578         if (i) {
579                 if (append_unit) {
580                         namelen = snprintf(devname, sizeof(devname), "%s%d",
581                             name, u);
582                         name = devname;
583                 }
584                 /* No preexisting struct cdev *, create one */
585                 *dev = make_dev_credf(MAKEDEV_REF, &drv->cdevsw, u, cred,
586                     UID_UUCP, GID_DIALER, 0600, "%s", name);
587         }
588
589         if_clone_create(name, namelen, NULL);
590 out:
591         CURVNET_RESTORE();
592 }
593
594 static void
595 tun_destroy(struct tuntap_softc *tp)
596 {
597
598         TUN_LOCK(tp);
599         tp->tun_flags |= TUN_DYING;
600         if (tp->tun_busy != 0)
601                 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
602         else
603                 TUN_UNLOCK(tp);
604
605         CURVNET_SET(TUN2IFP(tp)->if_vnet);
606
607         destroy_dev(tp->tun_dev);
608         seldrain(&tp->tun_rsel);
609         knlist_clear(&tp->tun_rsel.si_note, 0);
610         knlist_destroy(&tp->tun_rsel.si_note);
611         if ((tp->tun_flags & TUN_L2) != 0) {
612                 ether_ifdetach(TUN2IFP(tp));
613         } else {
614                 bpfdetach(TUN2IFP(tp));
615                 if_detach(TUN2IFP(tp));
616         }
617         sx_xlock(&tun_ioctl_sx);
618         TUN2IFP(tp)->if_softc = NULL;
619         sx_xunlock(&tun_ioctl_sx);
620         free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
621         if_free(TUN2IFP(tp));
622         mtx_destroy(&tp->tun_mtx);
623         cv_destroy(&tp->tun_cv);
624         free(tp, M_TUN);
625         CURVNET_RESTORE();
626 }
627
628 static int
629 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
630 {
631         struct tuntap_softc *tp = ifp->if_softc;
632
633         mtx_lock(&tunmtx);
634         TAILQ_REMOVE(&tunhead, tp, tun_list);
635         mtx_unlock(&tunmtx);
636         tun_destroy(tp);
637
638         return (0);
639 }
640
641 static void
642 vnet_tun_init(const void *unused __unused)
643 {
644         struct tuntap_driver *drv;
645         struct tuntap_driver_cloner *drvc;
646         int i;
647
648         for (i = 0; i < nitems(tuntap_drivers); ++i) {
649                 drv = &tuntap_drivers[i];
650                 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
651
652                 drvc->drv = drv;
653                 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
654                     drv->clone_match_fn, drv->clone_create_fn,
655                     drv->clone_destroy_fn);
656                 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
657         };
658 }
659 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
660                 vnet_tun_init, NULL);
661
662 static void
663 vnet_tun_uninit(const void *unused __unused)
664 {
665         struct tuntap_driver_cloner *drvc;
666
667         while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
668                 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
669                 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
670
671                 if_clone_detach(drvc->cloner);
672                 free(drvc, M_TUN);
673         }
674 }
675 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
676     vnet_tun_uninit, NULL);
677
678 static void
679 tun_uninit(const void *unused __unused)
680 {
681         struct tuntap_driver *drv;
682         struct tuntap_softc *tp;
683         int i;
684
685         EVENTHANDLER_DEREGISTER(dev_clone, tag);
686         drain_dev_clone_events();
687
688         mtx_lock(&tunmtx);
689         while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
690                 TAILQ_REMOVE(&tunhead, tp, tun_list);
691                 mtx_unlock(&tunmtx);
692                 tun_destroy(tp);
693                 mtx_lock(&tunmtx);
694         }
695         mtx_unlock(&tunmtx);
696         for (i = 0; i < nitems(tuntap_drivers); ++i) {
697                 drv = &tuntap_drivers[i];
698                 delete_unrhdr(drv->unrhdr);
699                 clone_cleanup(&drv->clones);
700         }
701         mtx_destroy(&tunmtx);
702 }
703 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
704
705 static int
706 tuntapmodevent(module_t mod, int type, void *data)
707 {
708         struct tuntap_driver *drv;
709         int i;
710
711         switch (type) {
712         case MOD_LOAD:
713                 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
714                 for (i = 0; i < nitems(tuntap_drivers); ++i) {
715                         drv = &tuntap_drivers[i];
716                         clone_setup(&drv->clones);
717                         drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
718                 }
719                 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
720                 if (tag == NULL)
721                         return (ENOMEM);
722                 break;
723         case MOD_UNLOAD:
724                 /* See tun_uninit, so it's done after the vnet_sysuninit() */
725                 break;
726         default:
727                 return EOPNOTSUPP;
728         }
729         return 0;
730 }
731
732 static moduledata_t tuntap_mod = {
733         "if_tuntap",
734         tuntapmodevent,
735         0
736 };
737
738 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
739 MODULE_VERSION(if_tuntap, 1);
740 MODULE_VERSION(if_tun, 1);
741 MODULE_VERSION(if_tap, 1);
742
743 static void
744 tunstart(struct ifnet *ifp)
745 {
746         struct tuntap_softc *tp = ifp->if_softc;
747         struct mbuf *m;
748
749         TUNDEBUG(ifp, "starting\n");
750         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
751                 IFQ_LOCK(&ifp->if_snd);
752                 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
753                 if (m == NULL) {
754                         IFQ_UNLOCK(&ifp->if_snd);
755                         return;
756                 }
757                 IFQ_UNLOCK(&ifp->if_snd);
758         }
759
760         TUN_LOCK(tp);
761         if (tp->tun_flags & TUN_RWAIT) {
762                 tp->tun_flags &= ~TUN_RWAIT;
763                 wakeup(tp);
764         }
765         selwakeuppri(&tp->tun_rsel, PZERO + 1);
766         KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
767         if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
768                 TUN_UNLOCK(tp);
769                 pgsigio(&tp->tun_sigio, SIGIO, 0);
770         } else
771                 TUN_UNLOCK(tp);
772 }
773
774 /*
775  * tunstart_l2
776  *
777  * queue packets from higher level ready to put out
778  */
779 static void
780 tunstart_l2(struct ifnet *ifp)
781 {
782         struct tuntap_softc     *tp = ifp->if_softc;
783
784         TUNDEBUG(ifp, "starting\n");
785
786         /*
787          * do not junk pending output if we are in VMnet mode.
788          * XXX: can this do any harm because of queue overflow?
789          */
790
791         TUN_LOCK(tp);
792         if (((tp->tun_flags & TUN_VMNET) == 0) &&
793             ((tp->tun_flags & TUN_READY) != TUN_READY)) {
794                 struct mbuf *m;
795
796                 /* Unlocked read. */
797                 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
798
799                 for (;;) {
800                         IF_DEQUEUE(&ifp->if_snd, m);
801                         if (m != NULL) {
802                                 m_freem(m);
803                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
804                         } else
805                                 break;
806                 }
807                 TUN_UNLOCK(tp);
808
809                 return;
810         }
811
812         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
813
814         if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
815                 if (tp->tun_flags & TUN_RWAIT) {
816                         tp->tun_flags &= ~TUN_RWAIT;
817                         wakeup(tp);
818                 }
819
820                 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
821                         TUN_UNLOCK(tp);
822                         pgsigio(&tp->tun_sigio, SIGIO, 0);
823                         TUN_LOCK(tp);
824                 }
825
826                 selwakeuppri(&tp->tun_rsel, PZERO+1);
827                 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
828                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
829         }
830
831         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
832         TUN_UNLOCK(tp);
833 } /* tunstart_l2 */
834
835
836 /* XXX: should return an error code so it can fail. */
837 static void
838 tuncreate(struct cdev *dev, struct tuntap_driver *drv)
839 {
840         struct tuntap_softc *sc;
841         struct ifnet *ifp;
842         struct ether_addr eaddr;
843         int iflags;
844         u_char type;
845
846         sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
847         mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
848         cv_init(&sc->tun_cv, "tun_condvar");
849         sc->tun_flags = drv->ident_flags;
850         sc->tun_dev = dev;
851         sc->tun_drv = drv;
852         mtx_lock(&tunmtx);
853         TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
854         mtx_unlock(&tunmtx);
855
856         iflags = IFF_MULTICAST;
857         if ((sc->tun_flags & TUN_L2) != 0) {
858                 type = IFT_ETHER;
859                 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
860         } else {
861                 type = IFT_PPP;
862                 iflags |= IFF_POINTOPOINT;
863         }
864         ifp = sc->tun_ifp = if_alloc(type);
865         if (ifp == NULL)
866                 panic("%s%d: failed to if_alloc() interface.\n",
867                     drv->cdevsw.d_name, dev2unit(dev));
868         ifp->if_softc = sc;
869         if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
870         ifp->if_ioctl = tunifioctl;
871         ifp->if_flags = iflags;
872         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
873         knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
874         ifp->if_capabilities |= IFCAP_LINKSTATE;
875         ifp->if_capenable |= IFCAP_LINKSTATE;
876
877         if ((sc->tun_flags & TUN_L2) != 0) {
878                 ifp->if_mtu = ETHERMTU;
879                 ifp->if_init = tunifinit;
880                 ifp->if_start = tunstart_l2;
881
882                 ether_gen_addr(ifp, &eaddr);
883                 ether_ifattach(ifp, eaddr.octet);
884         } else {
885                 ifp->if_mtu = TUNMTU;
886                 ifp->if_start = tunstart;
887                 ifp->if_output = tunoutput;
888
889                 ifp->if_snd.ifq_drv_maxlen = 0;
890                 IFQ_SET_READY(&ifp->if_snd);
891
892                 if_attach(ifp);
893                 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
894         }
895         dev->si_drv1 = sc;
896
897         TUN_LOCK(sc);
898         sc->tun_flags |= TUN_INITED;
899         TUN_UNLOCK(sc);
900
901         TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
902             ifp->if_xname, dev2unit(dev));
903 }
904
905 static int
906 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
907 {
908         struct ifnet    *ifp;
909         struct tuntap_driver *drv;
910         struct tuntap_softc *tp;
911         int error, tunflags;
912
913         tunflags = 0;
914         CURVNET_SET(TD_TO_VNET(td));
915         error = tuntap_name2info(dev->si_name, NULL, &tunflags);
916         if (error != 0) {
917                 CURVNET_RESTORE();
918                 return (error); /* Shouldn't happen */
919         }
920
921         if ((tunflags & TUN_L2) != 0) {
922                 /* Restrict? */
923                 if (tap_allow_uopen == 0) {
924                         error = priv_check(td, PRIV_NET_TAP);
925                         if (error != 0) {
926                                 CURVNET_RESTORE();
927                                 return (error);
928                         }
929                 }
930         }
931
932         /*
933          * XXXRW: Non-atomic test and set of dev->si_drv1 requires
934          * synchronization.
935          */
936         tp = dev->si_drv1;
937         if (!tp) {
938                 drv = tuntap_driver_from_flags(tunflags);
939                 if (drv == NULL) {
940                         CURVNET_RESTORE();
941                         return (ENXIO);
942                 }
943                 tuncreate(dev, drv);
944                 tp = dev->si_drv1;
945         }
946
947         TUN_LOCK(tp);
948         if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
949                 TUN_UNLOCK(tp);
950                 CURVNET_RESTORE();
951                 return (EBUSY);
952         }
953
954         error = tun_busy_locked(tp);
955         KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
956         ifp = TUN2IFP(tp);
957
958         if ((tp->tun_flags & TUN_L2) != 0) {
959                 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
960                     sizeof(tp->tun_ether.octet));
961
962                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
963                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
964
965                 if (tapuponopen)
966                         ifp->if_flags |= IFF_UP;
967         }
968
969         tp->tun_pid = td->td_proc->p_pid;
970         tp->tun_flags |= TUN_OPEN;
971
972         if_link_state_change(ifp, LINK_STATE_UP);
973         TUNDEBUG(ifp, "open\n");
974         TUN_UNLOCK(tp);
975         CURVNET_RESTORE();
976         return (0);
977 }
978
979 /*
980  * tunclose - close the device - mark i/f down & delete
981  * routing info
982  */
983 static  int
984 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
985 {
986         struct tuntap_softc *tp;
987         struct ifnet *ifp;
988         bool l2tun;
989
990         tp = dev->si_drv1;
991         ifp = TUN2IFP(tp);
992
993         TUN_LOCK(tp);
994         /*
995          * Simply close the device if this isn't the controlling process.  This
996          * may happen if, for instance, the tunnel has been handed off to
997          * another process.  The original controller should be able to close it
998          * without putting us into an inconsistent state.
999          */
1000         if (td->td_proc->p_pid != tp->tun_pid) {
1001                 TUN_UNLOCK(tp);
1002                 return (0);
1003         }
1004
1005         /*
1006          * junk all pending output
1007          */
1008         CURVNET_SET(ifp->if_vnet);
1009
1010         l2tun = false;
1011         if ((tp->tun_flags & TUN_L2) != 0) {
1012                 l2tun = true;
1013                 IF_DRAIN(&ifp->if_snd);
1014         } else {
1015                 IFQ_PURGE(&ifp->if_snd);
1016         }
1017
1018         /* For vmnet, we won't do most of the address/route bits */
1019         if ((tp->tun_flags & TUN_VMNET) != 0 ||
1020             (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1021                 goto out;
1022
1023         if (ifp->if_flags & IFF_UP) {
1024                 TUN_UNLOCK(tp);
1025                 if_down(ifp);
1026                 TUN_LOCK(tp);
1027         }
1028
1029         /* Delete all addresses and routes which reference this interface. */
1030         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1031                 struct ifaddr *ifa;
1032
1033                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1034                 TUN_UNLOCK(tp);
1035                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1036                         /* deal w/IPv4 PtP destination; unlocked read */
1037                         if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1038                                 rtinit(ifa, (int)RTM_DELETE,
1039                                     tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1040                         } else {
1041                                 rtinit(ifa, (int)RTM_DELETE, 0);
1042                         }
1043                 }
1044                 if_purgeaddrs(ifp);
1045                 TUN_LOCK(tp);
1046         }
1047
1048 out:
1049         if_link_state_change(ifp, LINK_STATE_DOWN);
1050         CURVNET_RESTORE();
1051
1052         funsetown(&tp->tun_sigio);
1053         selwakeuppri(&tp->tun_rsel, PZERO + 1);
1054         KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1055         TUNDEBUG (ifp, "closed\n");
1056         tp->tun_flags &= ~TUN_OPEN;
1057         tp->tun_pid = 0;
1058
1059         tun_unbusy_locked(tp);
1060         TUN_UNLOCK(tp);
1061         return (0);
1062 }
1063
1064 static void
1065 tuninit(struct ifnet *ifp)
1066 {
1067         struct tuntap_softc *tp = ifp->if_softc;
1068 #ifdef INET
1069         struct ifaddr *ifa;
1070 #endif
1071
1072         TUNDEBUG(ifp, "tuninit\n");
1073
1074         TUN_LOCK(tp);
1075         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1076         if ((tp->tun_flags & TUN_L2) == 0) {
1077                 ifp->if_flags |= IFF_UP;
1078                 getmicrotime(&ifp->if_lastchange);
1079 #ifdef INET
1080                 if_addr_rlock(ifp);
1081                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1082                         if (ifa->ifa_addr->sa_family == AF_INET) {
1083                                 struct sockaddr_in *si;
1084
1085                                 si = (struct sockaddr_in *)ifa->ifa_addr;
1086                                 if (si->sin_addr.s_addr)
1087                                         tp->tun_flags |= TUN_IASET;
1088
1089                                 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1090                                 if (si && si->sin_addr.s_addr)
1091                                         tp->tun_flags |= TUN_DSTADDR;
1092                         }
1093                 }
1094                 if_addr_runlock(ifp);
1095 #endif
1096                 TUN_UNLOCK(tp);
1097         } else {
1098                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1099                 TUN_UNLOCK(tp);
1100                 /* attempt to start output */
1101                 tunstart_l2(ifp);
1102         }
1103
1104 }
1105
1106 /*
1107  * Used only for l2 tunnel.
1108  */
1109 static void
1110 tunifinit(void *xtp)
1111 {
1112         struct tuntap_softc *tp;
1113
1114         tp = (struct tuntap_softc *)xtp;
1115         tuninit(tp->tun_ifp);
1116 }
1117
1118 /*
1119  * Process an ioctl request.
1120  */
1121 static int
1122 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1123 {
1124         struct ifreq *ifr = (struct ifreq *)data;
1125         struct tuntap_softc *tp;
1126         struct ifstat *ifs;
1127         struct ifmediareq       *ifmr;
1128         int             dummy, error = 0;
1129         bool            l2tun;
1130
1131         ifmr = NULL;
1132         sx_xlock(&tun_ioctl_sx);
1133         tp = ifp->if_softc;
1134         if (tp == NULL) {
1135                 error = ENXIO;
1136                 goto bad;
1137         }
1138         l2tun = (tp->tun_flags & TUN_L2) != 0;
1139         switch(cmd) {
1140         case SIOCGIFSTATUS:
1141                 ifs = (struct ifstat *)data;
1142                 TUN_LOCK(tp);
1143                 if (tp->tun_pid)
1144                         snprintf(ifs->ascii, sizeof(ifs->ascii),
1145                             "\tOpened by PID %d\n", tp->tun_pid);
1146                 else
1147                         ifs->ascii[0] = '\0';
1148                 TUN_UNLOCK(tp);
1149                 break;
1150         case SIOCSIFADDR:
1151                 if (l2tun)
1152                         error = ether_ioctl(ifp, cmd, data);
1153                 else
1154                         tuninit(ifp);
1155                 if (error == 0)
1156                     TUNDEBUG(ifp, "address set\n");
1157                 break;
1158         case SIOCSIFMTU:
1159                 ifp->if_mtu = ifr->ifr_mtu;
1160                 TUNDEBUG(ifp, "mtu set\n");
1161                 break;
1162         case SIOCSIFFLAGS:
1163         case SIOCADDMULTI:
1164         case SIOCDELMULTI:
1165                 break;
1166         case SIOCGIFMEDIA:
1167                 if (!l2tun) {
1168                         error = EINVAL;
1169                         break;
1170                 }
1171
1172                 ifmr = (struct ifmediareq *)data;
1173                 dummy = ifmr->ifm_count;
1174                 ifmr->ifm_count = 1;
1175                 ifmr->ifm_status = IFM_AVALID;
1176                 ifmr->ifm_active = IFM_ETHER;
1177                 if (tp->tun_flags & TUN_OPEN)
1178                         ifmr->ifm_status |= IFM_ACTIVE;
1179                 ifmr->ifm_current = ifmr->ifm_active;
1180                 if (dummy >= 1) {
1181                         int media = IFM_ETHER;
1182                         error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1183                 }
1184                 break;
1185         default:
1186                 if (l2tun) {
1187                         error = ether_ioctl(ifp, cmd, data);
1188                 } else {
1189                         error = EINVAL;
1190                 }
1191         }
1192 bad:
1193         sx_xunlock(&tun_ioctl_sx);
1194         return (error);
1195 }
1196
1197 /*
1198  * tunoutput - queue packets from higher level ready to put out.
1199  */
1200 static int
1201 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1202     struct route *ro)
1203 {
1204         struct tuntap_softc *tp = ifp->if_softc;
1205         u_short cached_tun_flags;
1206         int error;
1207         u_int32_t af;
1208
1209         TUNDEBUG (ifp, "tunoutput\n");
1210
1211 #ifdef MAC
1212         error = mac_ifnet_check_transmit(ifp, m0);
1213         if (error) {
1214                 m_freem(m0);
1215                 return (error);
1216         }
1217 #endif
1218
1219         /* Could be unlocked read? */
1220         TUN_LOCK(tp);
1221         cached_tun_flags = tp->tun_flags;
1222         TUN_UNLOCK(tp);
1223         if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1224                 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1225                 m_freem (m0);
1226                 return (EHOSTDOWN);
1227         }
1228
1229         if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1230                 m_freem (m0);
1231                 return (EHOSTDOWN);
1232         }
1233
1234         /* BPF writes need to be handled specially. */
1235         if (dst->sa_family == AF_UNSPEC)
1236                 bcopy(dst->sa_data, &af, sizeof(af));
1237         else
1238                 af = dst->sa_family;
1239
1240         if (bpf_peers_present(ifp->if_bpf))
1241                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1242
1243         /* prepend sockaddr? this may abort if the mbuf allocation fails */
1244         if (cached_tun_flags & TUN_LMODE) {
1245                 /* allocate space for sockaddr */
1246                 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1247
1248                 /* if allocation failed drop packet */
1249                 if (m0 == NULL) {
1250                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1251                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1252                         return (ENOBUFS);
1253                 } else {
1254                         bcopy(dst, m0->m_data, dst->sa_len);
1255                 }
1256         }
1257
1258         if (cached_tun_flags & TUN_IFHEAD) {
1259                 /* Prepend the address family */
1260                 M_PREPEND(m0, 4, M_NOWAIT);
1261
1262                 /* if allocation failed drop packet */
1263                 if (m0 == NULL) {
1264                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1265                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1266                         return (ENOBUFS);
1267                 } else
1268                         *(u_int32_t *)m0->m_data = htonl(af);
1269         } else {
1270 #ifdef INET
1271                 if (af != AF_INET)
1272 #endif
1273                 {
1274                         m_freem(m0);
1275                         return (EAFNOSUPPORT);
1276                 }
1277         }
1278
1279         error = (ifp->if_transmit)(ifp, m0);
1280         if (error)
1281                 return (ENOBUFS);
1282         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1283         return (0);
1284 }
1285
1286 /*
1287  * the cdevsw interface is now pretty minimal.
1288  */
1289 static  int
1290 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1291     struct thread *td)
1292 {
1293         struct ifreq ifr, *ifrp;
1294         struct tuntap_softc *tp = dev->si_drv1;
1295         struct tuninfo *tunp;
1296         int error, iflags;
1297 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1298     defined(COMPAT_FREEBSD4)
1299         int     ival;
1300 #endif
1301         bool    l2tun;
1302
1303         l2tun = (tp->tun_flags & TUN_L2) != 0;
1304         if (l2tun) {
1305                 /* tap specific ioctls */
1306                 switch(cmd) {
1307                 /* VMware/VMnet port ioctl's */
1308 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1309     defined(COMPAT_FREEBSD4)
1310                 case _IO('V', 0):
1311                         ival = IOCPARM_IVAL(data);
1312                         data = (caddr_t)&ival;
1313                         /* FALLTHROUGH */
1314 #endif
1315                 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1316                         iflags = *(int *)data;
1317                         iflags &= TUN_VMIO_FLAG_MASK;
1318                         iflags &= ~IFF_CANTCHANGE;
1319                         iflags |= IFF_UP;
1320
1321                         TUN_LOCK(tp);
1322                         TUN2IFP(tp)->if_flags = iflags |
1323                             (TUN2IFP(tp)->if_flags & IFF_CANTCHANGE);
1324                         TUN_UNLOCK(tp);
1325
1326                         return (0);
1327                 case SIOCGIFADDR:       /* get MAC address of the remote side */
1328                         TUN_LOCK(tp);
1329                         bcopy(&tp->tun_ether.octet, data,
1330                             sizeof(tp->tun_ether.octet));
1331                         TUN_UNLOCK(tp);
1332
1333                         return (0);
1334                 case SIOCSIFADDR:       /* set MAC address of the remote side */
1335                         TUN_LOCK(tp);
1336                         bcopy(data, &tp->tun_ether.octet,
1337                             sizeof(tp->tun_ether.octet));
1338                         TUN_UNLOCK(tp);
1339
1340                         return (0);
1341                 }
1342
1343                 /* Fall through to the common ioctls if unhandled */
1344         } else {
1345                 switch (cmd) {
1346                 case TUNSLMODE:
1347                         TUN_LOCK(tp);
1348                         if (*(int *)data) {
1349                                 tp->tun_flags |= TUN_LMODE;
1350                                 tp->tun_flags &= ~TUN_IFHEAD;
1351                         } else
1352                                 tp->tun_flags &= ~TUN_LMODE;
1353                         TUN_UNLOCK(tp);
1354
1355                         return (0);
1356                 case TUNSIFHEAD:
1357                         TUN_LOCK(tp);
1358                         if (*(int *)data) {
1359                                 tp->tun_flags |= TUN_IFHEAD;
1360                                 tp->tun_flags &= ~TUN_LMODE;
1361                         } else
1362                                 tp->tun_flags &= ~TUN_IFHEAD;
1363                         TUN_UNLOCK(tp);
1364
1365                         return (0);
1366                 case TUNGIFHEAD:
1367                         TUN_LOCK(tp);
1368                         *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1369                         TUN_UNLOCK(tp);
1370
1371                         return (0);
1372                 case TUNSIFMODE:
1373                         /* deny this if UP */
1374                         if (TUN2IFP(tp)->if_flags & IFF_UP)
1375                                 return (EBUSY);
1376
1377                         switch (*(int *)data & ~IFF_MULTICAST) {
1378                         case IFF_POINTOPOINT:
1379                         case IFF_BROADCAST:
1380                                 TUN_LOCK(tp);
1381                                 TUN2IFP(tp)->if_flags &=
1382                                     ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1383                                 TUN2IFP(tp)->if_flags |= *(int *)data;
1384                                 TUN_UNLOCK(tp);
1385
1386                                 break;
1387                         default:
1388                                 return (EINVAL);
1389                         }
1390
1391                         return (0);
1392                 case TUNSIFPID:
1393                         TUN_LOCK(tp);
1394                         tp->tun_pid = curthread->td_proc->p_pid;
1395                         TUN_UNLOCK(tp);
1396
1397                         return (0);
1398                 }
1399                 /* Fall through to the common ioctls if unhandled */
1400         }
1401
1402         switch (cmd) {
1403         case TUNGIFNAME:
1404                 ifrp = (struct ifreq *)data;
1405                 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1406
1407                 return (0);
1408         case TUNSIFINFO:
1409                 tunp = (struct tuninfo *)data;
1410                 if (TUN2IFP(tp)->if_type != tunp->type)
1411                         return (EPROTOTYPE);
1412                 TUN_LOCK(tp);
1413                 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1414                         strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1415                         ifr.ifr_mtu = tunp->mtu;
1416                         CURVNET_SET(TUN2IFP(tp)->if_vnet);
1417                         error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1418                             (caddr_t)&ifr, td);
1419                         CURVNET_RESTORE();
1420                         if (error) {
1421                                 TUN_UNLOCK(tp);
1422                                 return (error);
1423                         }
1424                 }
1425                 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1426                 TUN_UNLOCK(tp);
1427                 break;
1428         case TUNGIFINFO:
1429                 tunp = (struct tuninfo *)data;
1430                 TUN_LOCK(tp);
1431                 tunp->mtu = TUN2IFP(tp)->if_mtu;
1432                 tunp->type = TUN2IFP(tp)->if_type;
1433                 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1434                 TUN_UNLOCK(tp);
1435                 break;
1436         case TUNSDEBUG:
1437                 tundebug = *(int *)data;
1438                 break;
1439         case TUNGDEBUG:
1440                 *(int *)data = tundebug;
1441                 break;
1442         case FIONBIO:
1443                 break;
1444         case FIOASYNC:
1445                 TUN_LOCK(tp);
1446                 if (*(int *)data)
1447                         tp->tun_flags |= TUN_ASYNC;
1448                 else
1449                         tp->tun_flags &= ~TUN_ASYNC;
1450                 TUN_UNLOCK(tp);
1451                 break;
1452         case FIONREAD:
1453                 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1454                         struct mbuf *mb;
1455                         IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1456                         IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1457                         for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1458                                 *(int *)data += mb->m_len;
1459                         IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1460                 } else
1461                         *(int *)data = 0;
1462                 break;
1463         case FIOSETOWN:
1464                 return (fsetown(*(int *)data, &tp->tun_sigio));
1465
1466         case FIOGETOWN:
1467                 *(int *)data = fgetown(&tp->tun_sigio);
1468                 return (0);
1469
1470         /* This is deprecated, FIOSETOWN should be used instead. */
1471         case TIOCSPGRP:
1472                 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1473
1474         /* This is deprecated, FIOGETOWN should be used instead. */
1475         case TIOCGPGRP:
1476                 *(int *)data = -fgetown(&tp->tun_sigio);
1477                 return (0);
1478
1479         default:
1480                 return (ENOTTY);
1481         }
1482         return (0);
1483 }
1484
1485 /*
1486  * The cdevsw read interface - reads a packet at a time, or at
1487  * least as much of a packet as can be read.
1488  */
1489 static  int
1490 tunread(struct cdev *dev, struct uio *uio, int flag)
1491 {
1492         struct tuntap_softc *tp = dev->si_drv1;
1493         struct ifnet    *ifp = TUN2IFP(tp);
1494         struct mbuf     *m;
1495         int             error=0, len;
1496
1497         TUNDEBUG (ifp, "read\n");
1498         TUN_LOCK(tp);
1499         if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1500                 TUN_UNLOCK(tp);
1501                 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1502                 return (EHOSTDOWN);
1503         }
1504
1505         tp->tun_flags &= ~TUN_RWAIT;
1506
1507         for (;;) {
1508                 IFQ_DEQUEUE(&ifp->if_snd, m);
1509                 if (m != NULL)
1510                         break;
1511                 if (flag & O_NONBLOCK) {
1512                         TUN_UNLOCK(tp);
1513                         return (EWOULDBLOCK);
1514                 }
1515                 tp->tun_flags |= TUN_RWAIT;
1516                 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1517                     "tunread", 0);
1518                 if (error != 0) {
1519                         TUN_UNLOCK(tp);
1520                         return (error);
1521                 }
1522         }
1523         TUN_UNLOCK(tp);
1524
1525         if ((tp->tun_flags & TUN_L2) != 0)
1526                 BPF_MTAP(ifp, m);
1527
1528         while (m && uio->uio_resid > 0 && error == 0) {
1529                 len = min(uio->uio_resid, m->m_len);
1530                 if (len != 0)
1531                         error = uiomove(mtod(m, void *), len, uio);
1532                 m = m_free(m);
1533         }
1534
1535         if (m) {
1536                 TUNDEBUG(ifp, "Dropping mbuf\n");
1537                 m_freem(m);
1538         }
1539         return (error);
1540 }
1541
1542 static int
1543 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m)
1544 {
1545         struct ether_header *eh;
1546         struct ifnet *ifp;
1547
1548         ifp = TUN2IFP(tp);
1549
1550         /*
1551          * Only pass a unicast frame to ether_input(), if it would
1552          * actually have been received by non-virtual hardware.
1553          */
1554         if (m->m_len < sizeof(struct ether_header)) {
1555                 m_freem(m);
1556                 return (0);
1557         }
1558
1559         eh = mtod(m, struct ether_header *);
1560
1561         if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1562             !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1563             bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1564                 m_freem(m);
1565                 return (0);
1566         }
1567
1568         /* Pass packet up to parent. */
1569         CURVNET_SET(ifp->if_vnet);
1570         (*ifp->if_input)(ifp, m);
1571         CURVNET_RESTORE();
1572         /* ibytes are counted in parent */
1573         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1574         return (0);
1575 }
1576
1577 static int
1578 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1579 {
1580         struct ifnet *ifp;
1581         int family, isr;
1582
1583         ifp = TUN2IFP(tp);
1584         /* Could be unlocked read? */
1585         TUN_LOCK(tp);
1586         if (tp->tun_flags & TUN_IFHEAD) {
1587                 TUN_UNLOCK(tp);
1588                 if (m->m_len < sizeof(family) &&
1589                 (m = m_pullup(m, sizeof(family))) == NULL)
1590                         return (ENOBUFS);
1591                 family = ntohl(*mtod(m, u_int32_t *));
1592                 m_adj(m, sizeof(family));
1593         } else {
1594                 TUN_UNLOCK(tp);
1595                 family = AF_INET;
1596         }
1597
1598         BPF_MTAP2(ifp, &family, sizeof(family), m);
1599
1600         switch (family) {
1601 #ifdef INET
1602         case AF_INET:
1603                 isr = NETISR_IP;
1604                 break;
1605 #endif
1606 #ifdef INET6
1607         case AF_INET6:
1608                 isr = NETISR_IPV6;
1609                 break;
1610 #endif
1611         default:
1612                 m_freem(m);
1613                 return (EAFNOSUPPORT);
1614         }
1615         random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1616         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1617         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1618         CURVNET_SET(ifp->if_vnet);
1619         M_SETFIB(m, ifp->if_fib);
1620         netisr_dispatch(isr, m);
1621         CURVNET_RESTORE();
1622         return (0);
1623 }
1624
1625 /*
1626  * the cdevsw write interface - an atomic write is a packet - or else!
1627  */
1628 static  int
1629 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1630 {
1631         struct tuntap_softc *tp;
1632         struct ifnet    *ifp;
1633         struct mbuf     *m;
1634         uint32_t        mru;
1635         int             align;
1636         bool            l2tun;
1637
1638         tp = dev->si_drv1;
1639         ifp = TUN2IFP(tp);
1640         TUNDEBUG(ifp, "tunwrite\n");
1641         if ((ifp->if_flags & IFF_UP) != IFF_UP)
1642                 /* ignore silently */
1643                 return (0);
1644
1645         if (uio->uio_resid == 0)
1646                 return (0);
1647
1648         l2tun = (tp->tun_flags & TUN_L2) != 0;
1649         align = 0;
1650         mru = l2tun ? TAPMRU : TUNMRU;
1651         if (l2tun)
1652                 align = ETHER_ALIGN;
1653         else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1654                 mru += sizeof(uint32_t);        /* family */
1655         if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1656                 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1657                 return (EIO);
1658         }
1659
1660         if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1661                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1662                 return (ENOBUFS);
1663         }
1664
1665         m->m_pkthdr.rcvif = ifp;
1666 #ifdef MAC
1667         mac_ifnet_create_mbuf(ifp, m);
1668 #endif
1669
1670         if (l2tun)
1671                 return (tunwrite_l2(tp, m));
1672
1673         return (tunwrite_l3(tp, m));
1674 }
1675
1676 /*
1677  * tunpoll - the poll interface, this is only useful on reads
1678  * really. The write detect always returns true, write never blocks
1679  * anyway, it either accepts the packet or drops it.
1680  */
1681 static  int
1682 tunpoll(struct cdev *dev, int events, struct thread *td)
1683 {
1684         struct tuntap_softc *tp = dev->si_drv1;
1685         struct ifnet    *ifp = TUN2IFP(tp);
1686         int             revents = 0;
1687
1688         TUNDEBUG(ifp, "tunpoll\n");
1689
1690         if (events & (POLLIN | POLLRDNORM)) {
1691                 IFQ_LOCK(&ifp->if_snd);
1692                 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1693                         TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1694                         revents |= events & (POLLIN | POLLRDNORM);
1695                 } else {
1696                         TUNDEBUG(ifp, "tunpoll waiting\n");
1697                         selrecord(td, &tp->tun_rsel);
1698                 }
1699                 IFQ_UNLOCK(&ifp->if_snd);
1700         }
1701         revents |= events & (POLLOUT | POLLWRNORM);
1702
1703         return (revents);
1704 }
1705
1706 /*
1707  * tunkqfilter - support for the kevent() system call.
1708  */
1709 static int
1710 tunkqfilter(struct cdev *dev, struct knote *kn)
1711 {
1712         struct tuntap_softc     *tp = dev->si_drv1;
1713         struct ifnet    *ifp = TUN2IFP(tp);
1714
1715         switch(kn->kn_filter) {
1716         case EVFILT_READ:
1717                 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1718                     ifp->if_xname, dev2unit(dev));
1719                 kn->kn_fop = &tun_read_filterops;
1720                 break;
1721
1722         case EVFILT_WRITE:
1723                 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1724                     ifp->if_xname, dev2unit(dev));
1725                 kn->kn_fop = &tun_write_filterops;
1726                 break;
1727
1728         default:
1729                 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1730                     ifp->if_xname, dev2unit(dev));
1731                 return(EINVAL);
1732         }
1733
1734         kn->kn_hook = tp;
1735         knlist_add(&tp->tun_rsel.si_note, kn, 0);
1736
1737         return (0);
1738 }
1739
1740 /*
1741  * Return true of there is data in the interface queue.
1742  */
1743 static int
1744 tunkqread(struct knote *kn, long hint)
1745 {
1746         int                     ret;
1747         struct tuntap_softc     *tp = kn->kn_hook;
1748         struct cdev             *dev = tp->tun_dev;
1749         struct ifnet    *ifp = TUN2IFP(tp);
1750
1751         if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1752                 TUNDEBUG(ifp,
1753                     "%s have data in the queue.  Len = %d, minor = %#x\n",
1754                     ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1755                 ret = 1;
1756         } else {
1757                 TUNDEBUG(ifp,
1758                     "%s waiting for data, minor = %#x\n", ifp->if_xname,
1759                     dev2unit(dev));
1760                 ret = 0;
1761         }
1762
1763         return (ret);
1764 }
1765
1766 /*
1767  * Always can write, always return MTU in kn->data.
1768  */
1769 static int
1770 tunkqwrite(struct knote *kn, long hint)
1771 {
1772         struct tuntap_softc     *tp = kn->kn_hook;
1773         struct ifnet    *ifp = TUN2IFP(tp);
1774
1775         kn->kn_data = ifp->if_mtu;
1776
1777         return (1);
1778 }
1779
1780 static void
1781 tunkqdetach(struct knote *kn)
1782 {
1783         struct tuntap_softc     *tp = kn->kn_hook;
1784
1785         knlist_remove(&tp->tun_rsel.si_note, kn, 0);
1786 }