]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_tap.c
tap(4): Add a MODULE_VERSION
[FreeBSD/FreeBSD.git] / sys / net / if_tap.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * BASED ON:
29  * -------------------------------------------------------------------------
30  *
31  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
32  * Nottingham University 1987.
33  */
34
35 /*
36  * $FreeBSD$
37  * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
38  */
39
40 #include "opt_inet.h"
41
42 #include <sys/param.h>
43 #include <sys/conf.h>
44 #include <sys/lock.h>
45 #include <sys/fcntl.h>
46 #include <sys/filio.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/module.h>
52 #include <sys/poll.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/selinfo.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/sx.h>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
62 #include <sys/ttycom.h>
63 #include <sys/uio.h>
64 #include <sys/queue.h>
65
66 #include <net/bpf.h>
67 #include <net/ethernet.h>
68 #include <net/if.h>
69 #include <net/if_var.h>
70 #include <net/if_clone.h>
71 #include <net/if_dl.h>
72 #include <net/if_media.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
75 #include <net/vnet.h>
76
77 #include <netinet/in.h>
78
79 #include <net/if_tapvar.h>
80 #include <net/if_tap.h>
81
82 #define CDEV_NAME       "tap"
83 #define TAPDEBUG        if (tapdebug) printf
84
85 static const char tapname[] = "tap";
86 static const char vmnetname[] = "vmnet";
87 #define TAPMAXUNIT      0x7fff
88 #define VMNET_DEV_MASK  CLONE_FLAG0
89
90 /* module */
91 static int              tapmodevent(module_t, int, void *);
92
93 /* device */
94 static void             tapclone(void *, struct ucred *, char *, int,
95                             struct cdev **);
96 static void             tapcreate(struct cdev *);
97
98 /* network interface */
99 static void             tapifstart(struct ifnet *);
100 static int              tapifioctl(struct ifnet *, u_long, caddr_t);
101 static void             tapifinit(void *);
102
103 static int              tap_clone_create(struct if_clone *, int, caddr_t);
104 static void             tap_clone_destroy(struct ifnet *);
105 static struct if_clone *tap_cloner;
106 static int              vmnet_clone_create(struct if_clone *, int, caddr_t);
107 static void             vmnet_clone_destroy(struct ifnet *);
108 static struct if_clone *vmnet_cloner;
109
110 /* character device */
111 static d_open_t         tapopen;
112 static d_close_t        tapclose;
113 static d_read_t         tapread;
114 static d_write_t        tapwrite;
115 static d_ioctl_t        tapioctl;
116 static d_poll_t         tappoll;
117 static d_kqfilter_t     tapkqfilter;
118
119 /* kqueue(2) */
120 static int              tapkqread(struct knote *, long);
121 static int              tapkqwrite(struct knote *, long);
122 static void             tapkqdetach(struct knote *);
123
124 static struct filterops tap_read_filterops = {
125         .f_isfd =       1,
126         .f_attach =     NULL,
127         .f_detach =     tapkqdetach,
128         .f_event =      tapkqread,
129 };
130
131 static struct filterops tap_write_filterops = {
132         .f_isfd =       1,
133         .f_attach =     NULL,
134         .f_detach =     tapkqdetach,
135         .f_event =      tapkqwrite,
136 };
137
138 static struct cdevsw    tap_cdevsw = {
139         .d_version =    D_VERSION,
140         .d_flags =      D_NEEDMINOR,
141         .d_open =       tapopen,
142         .d_close =      tapclose,
143         .d_read =       tapread,
144         .d_write =      tapwrite,
145         .d_ioctl =      tapioctl,
146         .d_poll =       tappoll,
147         .d_name =       CDEV_NAME,
148         .d_kqfilter =   tapkqfilter,
149 };
150
151 /*
152  * All global variables in if_tap.c are locked with tapmtx, with the
153  * exception of tapdebug, which is accessed unlocked; tapclones is
154  * static at runtime.
155  */
156 static struct mtx               tapmtx;
157 static int                      tapdebug = 0;        /* debug flag   */
158 static int                      tapuopen = 0;        /* allow user open() */
159 static int                      tapuponopen = 0;    /* IFF_UP on open() */
160 static int                      tapdclone = 1;  /* enable devfs cloning */
161 static SLIST_HEAD(, tap_softc)  taphead;             /* first device */
162 static struct clonedevs         *tapclones;
163
164 MALLOC_DECLARE(M_TAP);
165 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
166 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
167
168 static struct sx tap_ioctl_sx;
169 SX_SYSINIT(tap_ioctl_sx, &tap_ioctl_sx, "tap_ioctl");
170
171 SYSCTL_DECL(_net_link);
172 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
173     "Ethernet tunnel software network interface");
174 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
175         "Allow user to open /dev/tap (based on node permissions)");
176 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
177         "Bring interface up when /dev/tap is opened");
178 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
179         "Enable legacy devfs interface creation");
180 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
181
182 DEV_MODULE(if_tap, tapmodevent, NULL);
183 MODULE_VERSION(if_tun, 1);
184
185 static int
186 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
187 {
188         struct cdev *dev;
189         int i;
190
191         /* Find any existing device, or allocate new unit number. */
192         i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, 0);
193         if (i) {
194                 dev = make_dev(&tap_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600,
195                     "%s%d", tapname, unit);
196         }
197
198         tapcreate(dev);
199         return (0);
200 }
201
202 /* vmnet devices are tap devices in disguise */
203 static int
204 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
205 {
206         struct cdev *dev;
207         int i;
208
209         /* Find any existing device, or allocate new unit number. */
210         i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, VMNET_DEV_MASK);
211         if (i) {
212                 dev = make_dev(&tap_cdevsw, unit | VMNET_DEV_MASK, UID_ROOT,
213                     GID_WHEEL, 0600, "%s%d", vmnetname, unit);
214         }
215
216         tapcreate(dev);
217         return (0);
218 }
219
220 static void
221 tap_destroy(struct tap_softc *tp)
222 {
223         struct ifnet *ifp = tp->tap_ifp;
224
225         CURVNET_SET(ifp->if_vnet);
226         sx_xlock(&tap_ioctl_sx);
227         ifp->if_softc = NULL;
228         sx_xunlock(&tap_ioctl_sx);
229
230         destroy_dev(tp->tap_dev);
231         seldrain(&tp->tap_rsel);
232         knlist_clear(&tp->tap_rsel.si_note, 0);
233         knlist_destroy(&tp->tap_rsel.si_note);
234         ether_ifdetach(ifp);
235         if_free(ifp);
236
237         mtx_destroy(&tp->tap_mtx);
238         free(tp, M_TAP);
239         CURVNET_RESTORE();
240 }
241
242 static void
243 tap_clone_destroy(struct ifnet *ifp)
244 {
245         struct tap_softc *tp = ifp->if_softc;
246
247         mtx_lock(&tapmtx);
248         SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
249         mtx_unlock(&tapmtx);
250         tap_destroy(tp);
251 }
252
253 /* vmnet devices are tap devices in disguise */
254 static void
255 vmnet_clone_destroy(struct ifnet *ifp)
256 {
257         tap_clone_destroy(ifp);
258 }
259
260 /*
261  * tapmodevent
262  *
263  * module event handler
264  */
265 static int
266 tapmodevent(module_t mod, int type, void *data)
267 {
268         static eventhandler_tag  eh_tag = NULL;
269         struct tap_softc        *tp = NULL;
270         struct ifnet            *ifp = NULL;
271
272         switch (type) {
273         case MOD_LOAD:
274
275                 /* intitialize device */
276
277                 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
278                 SLIST_INIT(&taphead);
279
280                 clone_setup(&tapclones);
281                 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
282                 if (eh_tag == NULL) {
283                         clone_cleanup(&tapclones);
284                         mtx_destroy(&tapmtx);
285                         return (ENOMEM);
286                 }
287                 tap_cloner = if_clone_simple(tapname, tap_clone_create,
288                     tap_clone_destroy, 0);
289                 vmnet_cloner = if_clone_simple(vmnetname, vmnet_clone_create,
290                     vmnet_clone_destroy, 0);
291                 return (0);
292
293         case MOD_UNLOAD:
294                 /*
295                  * The EBUSY algorithm here can't quite atomically
296                  * guarantee that this is race-free since we have to
297                  * release the tap mtx to deregister the clone handler.
298                  */
299                 mtx_lock(&tapmtx);
300                 SLIST_FOREACH(tp, &taphead, tap_next) {
301                         mtx_lock(&tp->tap_mtx);
302                         if (tp->tap_flags & TAP_OPEN) {
303                                 mtx_unlock(&tp->tap_mtx);
304                                 mtx_unlock(&tapmtx);
305                                 return (EBUSY);
306                         }
307                         mtx_unlock(&tp->tap_mtx);
308                 }
309                 mtx_unlock(&tapmtx);
310
311                 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
312                 if_clone_detach(tap_cloner);
313                 if_clone_detach(vmnet_cloner);
314                 drain_dev_clone_events();
315
316                 mtx_lock(&tapmtx);
317                 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
318                         SLIST_REMOVE_HEAD(&taphead, tap_next);
319                         mtx_unlock(&tapmtx);
320
321                         ifp = tp->tap_ifp;
322
323                         TAPDEBUG("detaching %s\n", ifp->if_xname);
324
325                         tap_destroy(tp);
326                         mtx_lock(&tapmtx);
327                 }
328                 mtx_unlock(&tapmtx);
329                 clone_cleanup(&tapclones);
330
331                 mtx_destroy(&tapmtx);
332
333                 break;
334
335         default:
336                 return (EOPNOTSUPP);
337         }
338
339         return (0);
340 } /* tapmodevent */
341
342
343 /*
344  * DEVFS handler
345  *
346  * We need to support two kind of devices - tap and vmnet
347  */
348 static void
349 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
350 {
351         char            devname[SPECNAMELEN + 1];
352         int             i, unit, append_unit;
353         int             extra;
354
355         if (*dev != NULL)
356                 return;
357
358         if (!tapdclone ||
359             (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE) != 0))
360                 return;
361
362         unit = 0;
363         append_unit = 0;
364         extra = 0;
365
366         /* We're interested in only tap/vmnet devices. */
367         if (strcmp(name, tapname) == 0) {
368                 unit = -1;
369         } else if (strcmp(name, vmnetname) == 0) {
370                 unit = -1;
371                 extra = VMNET_DEV_MASK;
372         } else if (dev_stdclone(name, NULL, tapname, &unit) != 1) {
373                 if (dev_stdclone(name, NULL, vmnetname, &unit) != 1) {
374                         return;
375                 } else {
376                         extra = VMNET_DEV_MASK;
377                 }
378         }
379
380         if (unit == -1)
381                 append_unit = 1;
382
383         CURVNET_SET(CRED_TO_VNET(cred));
384         /* find any existing device, or allocate new unit number */
385         i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
386         if (i) {
387                 if (append_unit) {
388                         /*
389                          * We were passed 'tun' or 'tap', with no unit specified
390                          * so we'll need to append it now.
391                          */
392                         namelen = snprintf(devname, sizeof(devname), "%s%d", name,
393                             unit);
394                         name = devname;
395                 }
396
397                 *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
398                      cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
399         }
400
401         if_clone_create(name, namelen, NULL);
402         CURVNET_RESTORE();
403 } /* tapclone */
404
405
406 /*
407  * tapcreate
408  *
409  * to create interface
410  */
411 static void
412 tapcreate(struct cdev *dev)
413 {
414         struct ifnet            *ifp = NULL;
415         struct tap_softc        *tp = NULL;
416         unsigned short           macaddr_hi;
417         uint32_t                 macaddr_mid;
418         int                      unit;
419         const char              *name = NULL;
420         u_char                  eaddr[6];
421
422         /* allocate driver storage and create device */
423         tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
424         mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
425         mtx_lock(&tapmtx);
426         SLIST_INSERT_HEAD(&taphead, tp, tap_next);
427         mtx_unlock(&tapmtx);
428
429         unit = dev2unit(dev);
430
431         /* select device: tap or vmnet */
432         if (unit & VMNET_DEV_MASK) {
433                 name = vmnetname;
434                 tp->tap_flags |= TAP_VMNET;
435         } else
436                 name = tapname;
437
438         unit &= TAPMAXUNIT;
439
440         TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
441
442         /* generate fake MAC address: 00 bd xx xx xx unit_no */
443         macaddr_hi = htons(0x00bd);
444         macaddr_mid = (uint32_t) ticks;
445         bcopy(&macaddr_hi, eaddr, sizeof(short));
446         bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
447         eaddr[5] = (u_char)unit;
448
449         /* fill the rest and attach interface */
450         ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
451         if (ifp == NULL)
452                 panic("%s%d: can not if_alloc()", name, unit);
453         ifp->if_softc = tp;
454         if_initname(ifp, name, unit);
455         ifp->if_init = tapifinit;
456         ifp->if_start = tapifstart;
457         ifp->if_ioctl = tapifioctl;
458         ifp->if_mtu = ETHERMTU;
459         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
460         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
461         ifp->if_capabilities |= IFCAP_LINKSTATE;
462         ifp->if_capenable |= IFCAP_LINKSTATE;
463
464         dev->si_drv1 = tp;
465         tp->tap_dev = dev;
466
467         ether_ifattach(ifp, eaddr);
468
469         mtx_lock(&tp->tap_mtx);
470         tp->tap_flags |= TAP_INITED;
471         mtx_unlock(&tp->tap_mtx);
472
473         knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
474
475         TAPDEBUG("interface %s is created. minor = %#x\n", 
476                 ifp->if_xname, dev2unit(dev));
477 } /* tapcreate */
478
479
480 /*
481  * tapopen
482  *
483  * to open tunnel. must be superuser
484  */
485 static int
486 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
487 {
488         struct tap_softc        *tp = NULL;
489         struct ifnet            *ifp = NULL;
490         int                      error;
491
492         if (tapuopen == 0) {
493                 error = priv_check(td, PRIV_NET_TAP);
494                 if (error)
495                         return (error);
496         }
497
498         if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
499                 return (ENXIO);
500
501         tp = dev->si_drv1;
502
503         mtx_lock(&tp->tap_mtx);
504         if (tp->tap_flags & TAP_OPEN) {
505                 mtx_unlock(&tp->tap_mtx);
506                 return (EBUSY);
507         }
508
509         bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
510         tp->tap_pid = td->td_proc->p_pid;
511         tp->tap_flags |= TAP_OPEN;
512         ifp = tp->tap_ifp;
513
514         ifp->if_drv_flags |= IFF_DRV_RUNNING;
515         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
516         if (tapuponopen)
517                 ifp->if_flags |= IFF_UP;
518         if_link_state_change(ifp, LINK_STATE_UP);
519         mtx_unlock(&tp->tap_mtx);
520
521         TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
522
523         return (0);
524 } /* tapopen */
525
526
527 /*
528  * tapclose
529  *
530  * close the device - mark i/f down & delete routing info
531  */
532 static int
533 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
534 {
535         struct ifaddr           *ifa;
536         struct tap_softc        *tp = dev->si_drv1;
537         struct ifnet            *ifp = tp->tap_ifp;
538
539         /* junk all pending output */
540         mtx_lock(&tp->tap_mtx);
541         CURVNET_SET(ifp->if_vnet);
542         IF_DRAIN(&ifp->if_snd);
543
544         /*
545          * Do not bring the interface down, and do not anything with
546          * interface, if we are in VMnet mode. Just close the device.
547          */
548         if (((tp->tap_flags & TAP_VMNET) == 0) &&
549             (ifp->if_flags & (IFF_UP | IFF_LINK0)) == IFF_UP) {
550                 mtx_unlock(&tp->tap_mtx);
551                 if_down(ifp);
552                 mtx_lock(&tp->tap_mtx);
553                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
554                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
555                         mtx_unlock(&tp->tap_mtx);
556                         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
557                                 rtinit(ifa, (int)RTM_DELETE, 0);
558                         }
559                         if_purgeaddrs(ifp);
560                         mtx_lock(&tp->tap_mtx);
561                 }
562         }
563
564         if_link_state_change(ifp, LINK_STATE_DOWN);
565         CURVNET_RESTORE();
566
567         funsetown(&tp->tap_sigio);
568         selwakeuppri(&tp->tap_rsel, PZERO+1);
569         KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
570
571         tp->tap_flags &= ~TAP_OPEN;
572         tp->tap_pid = 0;
573         mtx_unlock(&tp->tap_mtx);
574
575         TAPDEBUG("%s is closed. minor = %#x\n", 
576                 ifp->if_xname, dev2unit(dev));
577
578         return (0);
579 } /* tapclose */
580
581
582 /*
583  * tapifinit
584  *
585  * network interface initialization function
586  */
587 static void
588 tapifinit(void *xtp)
589 {
590         struct tap_softc        *tp = (struct tap_softc *)xtp;
591         struct ifnet            *ifp = tp->tap_ifp;
592
593         TAPDEBUG("initializing %s\n", ifp->if_xname);
594
595         mtx_lock(&tp->tap_mtx);
596         ifp->if_drv_flags |= IFF_DRV_RUNNING;
597         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
598         mtx_unlock(&tp->tap_mtx);
599
600         /* attempt to start output */
601         tapifstart(ifp);
602 } /* tapifinit */
603
604
605 /*
606  * tapifioctl
607  *
608  * Process an ioctl request on network interface
609  */
610 static int
611 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
612 {
613         struct tap_softc        *tp;
614         struct ifreq            *ifr = (struct ifreq *)data;
615         struct ifstat           *ifs = NULL;
616         struct ifmediareq       *ifmr = NULL;
617         int                      dummy, error = 0;
618
619         sx_xlock(&tap_ioctl_sx);
620         tp = ifp->if_softc;
621         if (tp == NULL) {
622                 error = ENXIO;
623                 goto bad;
624         }
625         switch (cmd) {
626                 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
627                 case SIOCADDMULTI:
628                 case SIOCDELMULTI:
629                         break;
630
631                 case SIOCGIFMEDIA:
632                         ifmr = (struct ifmediareq *)data;
633                         dummy = ifmr->ifm_count;
634                         ifmr->ifm_count = 1;
635                         ifmr->ifm_status = IFM_AVALID;
636                         ifmr->ifm_active = IFM_ETHER;
637                         if (tp->tap_flags & TAP_OPEN)
638                                 ifmr->ifm_status |= IFM_ACTIVE;
639                         ifmr->ifm_current = ifmr->ifm_active;
640                         if (dummy >= 1) {
641                                 int media = IFM_ETHER;
642                                 error = copyout(&media, ifmr->ifm_ulist,
643                                     sizeof(int));
644                         }
645                         break;
646
647                 case SIOCSIFMTU:
648                         ifp->if_mtu = ifr->ifr_mtu;
649                         break;
650
651                 case SIOCGIFSTATUS:
652                         ifs = (struct ifstat *)data;
653                         mtx_lock(&tp->tap_mtx);
654                         if (tp->tap_pid != 0)
655                                 snprintf(ifs->ascii, sizeof(ifs->ascii),
656                                         "\tOpened by PID %d\n", tp->tap_pid);
657                         else
658                                 ifs->ascii[0] = '\0';
659                         mtx_unlock(&tp->tap_mtx);
660                         break;
661
662                 default:
663                         error = ether_ioctl(ifp, cmd, data);
664                         break;
665         }
666
667 bad:
668         sx_xunlock(&tap_ioctl_sx);
669         return (error);
670 } /* tapifioctl */
671
672
673 /*
674  * tapifstart
675  *
676  * queue packets from higher level ready to put out
677  */
678 static void
679 tapifstart(struct ifnet *ifp)
680 {
681         struct tap_softc        *tp = ifp->if_softc;
682
683         TAPDEBUG("%s starting\n", ifp->if_xname);
684
685         /*
686          * do not junk pending output if we are in VMnet mode.
687          * XXX: can this do any harm because of queue overflow?
688          */
689
690         mtx_lock(&tp->tap_mtx);
691         if (((tp->tap_flags & TAP_VMNET) == 0) &&
692             ((tp->tap_flags & TAP_READY) != TAP_READY)) {
693                 struct mbuf *m;
694
695                 /* Unlocked read. */
696                 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname, 
697                     tp->tap_flags);
698
699                 for (;;) {
700                         IF_DEQUEUE(&ifp->if_snd, m);
701                         if (m != NULL) {
702                                 m_freem(m);
703                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
704                         } else
705                                 break;
706                 }
707                 mtx_unlock(&tp->tap_mtx);
708
709                 return;
710         }
711
712         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
713
714         if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
715                 if (tp->tap_flags & TAP_RWAIT) {
716                         tp->tap_flags &= ~TAP_RWAIT;
717                         wakeup(tp);
718                 }
719
720                 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
721                         mtx_unlock(&tp->tap_mtx);
722                         pgsigio(&tp->tap_sigio, SIGIO, 0);
723                         mtx_lock(&tp->tap_mtx);
724                 }
725
726                 selwakeuppri(&tp->tap_rsel, PZERO+1);
727                 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
728                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
729         }
730
731         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
732         mtx_unlock(&tp->tap_mtx);
733 } /* tapifstart */
734
735
736 /*
737  * tapioctl
738  *
739  * the cdevsw interface is now pretty minimal
740  */
741 static int
742 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
743 {
744         struct ifreq             ifr;
745         struct tap_softc        *tp = dev->si_drv1;
746         struct ifnet            *ifp = tp->tap_ifp;
747         struct tapinfo          *tapp = NULL;
748         int                      f;
749         int                      error;
750 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
751     defined(COMPAT_FREEBSD4)
752         int                      ival;
753 #endif
754
755         switch (cmd) {
756                 case TAPSIFINFO:
757                         tapp = (struct tapinfo *)data;
758                         if (ifp->if_type != tapp->type)
759                                 return (EPROTOTYPE);
760                         mtx_lock(&tp->tap_mtx);
761                         if (ifp->if_mtu != tapp->mtu) {
762                                 strlcpy(ifr.ifr_name, if_name(ifp), IFNAMSIZ);
763                                 ifr.ifr_mtu = tapp->mtu;
764                                 CURVNET_SET(ifp->if_vnet);
765                                 error = ifhwioctl(SIOCSIFMTU, ifp,
766                                     (caddr_t)&ifr, td);
767                                 CURVNET_RESTORE();
768                                 if (error) {
769                                         mtx_unlock(&tp->tap_mtx);
770                                         return (error);
771                                 }
772                         }
773                         ifp->if_baudrate = tapp->baudrate;
774                         mtx_unlock(&tp->tap_mtx);
775                         break;
776
777                 case TAPGIFINFO:
778                         tapp = (struct tapinfo *)data;
779                         mtx_lock(&tp->tap_mtx);
780                         tapp->mtu = ifp->if_mtu;
781                         tapp->type = ifp->if_type;
782                         tapp->baudrate = ifp->if_baudrate;
783                         mtx_unlock(&tp->tap_mtx);
784                         break;
785
786                 case TAPSDEBUG:
787                         tapdebug = *(int *)data;
788                         break;
789
790                 case TAPGDEBUG:
791                         *(int *)data = tapdebug;
792                         break;
793
794                 case TAPGIFNAME: {
795                         struct ifreq    *ifr = (struct ifreq *) data;
796
797                         strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
798                         } break;
799
800                 case FIONBIO:
801                         break;
802
803                 case FIOASYNC:
804                         mtx_lock(&tp->tap_mtx);
805                         if (*(int *)data)
806                                 tp->tap_flags |= TAP_ASYNC;
807                         else
808                                 tp->tap_flags &= ~TAP_ASYNC;
809                         mtx_unlock(&tp->tap_mtx);
810                         break;
811
812                 case FIONREAD:
813                         if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
814                                 struct mbuf *mb;
815
816                                 IFQ_LOCK(&ifp->if_snd);
817                                 IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
818                                 for (*(int *)data = 0; mb != NULL;
819                                      mb = mb->m_next)
820                                         *(int *)data += mb->m_len;
821                                 IFQ_UNLOCK(&ifp->if_snd);
822                         } else
823                                 *(int *)data = 0;
824                         break;
825
826                 case FIOSETOWN:
827                         return (fsetown(*(int *)data, &tp->tap_sigio));
828
829                 case FIOGETOWN:
830                         *(int *)data = fgetown(&tp->tap_sigio);
831                         return (0);
832
833                 /* this is deprecated, FIOSETOWN should be used instead */
834                 case TIOCSPGRP:
835                         return (fsetown(-(*(int *)data), &tp->tap_sigio));
836
837                 /* this is deprecated, FIOGETOWN should be used instead */
838                 case TIOCGPGRP:
839                         *(int *)data = -fgetown(&tp->tap_sigio);
840                         return (0);
841
842                 /* VMware/VMnet port ioctl's */
843
844 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
845     defined(COMPAT_FREEBSD4)
846                 case _IO('V', 0):
847                         ival = IOCPARM_IVAL(data);
848                         data = (caddr_t)&ival;
849                         /* FALLTHROUGH */
850 #endif
851                 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
852                         f = *(int *)data;
853                         f &= 0x0fff;
854                         f &= ~IFF_CANTCHANGE;
855                         f |= IFF_UP;
856
857                         mtx_lock(&tp->tap_mtx);
858                         ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
859                         mtx_unlock(&tp->tap_mtx);
860                         break;
861
862                 case SIOCGIFADDR:       /* get MAC address of the remote side */
863                         mtx_lock(&tp->tap_mtx);
864                         bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
865                         mtx_unlock(&tp->tap_mtx);
866                         break;
867
868                 case SIOCSIFADDR:       /* set MAC address of the remote side */
869                         mtx_lock(&tp->tap_mtx);
870                         bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
871                         mtx_unlock(&tp->tap_mtx);
872                         break;
873
874                 default:
875                         return (ENOTTY);
876         }
877         return (0);
878 } /* tapioctl */
879
880
881 /*
882  * tapread
883  *
884  * the cdevsw read interface - reads a packet at a time, or at
885  * least as much of a packet as can be read
886  */
887 static int
888 tapread(struct cdev *dev, struct uio *uio, int flag)
889 {
890         struct tap_softc        *tp = dev->si_drv1;
891         struct ifnet            *ifp = tp->tap_ifp;
892         struct mbuf             *m = NULL;
893         int                      error = 0, len;
894
895         TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
896
897         mtx_lock(&tp->tap_mtx);
898         if ((tp->tap_flags & TAP_READY) != TAP_READY) {
899                 mtx_unlock(&tp->tap_mtx);
900
901                 /* Unlocked read. */
902                 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
903                         ifp->if_xname, dev2unit(dev), tp->tap_flags);
904
905                 return (EHOSTDOWN);
906         }
907
908         tp->tap_flags &= ~TAP_RWAIT;
909
910         /* sleep until we get a packet */
911         do {
912                 IF_DEQUEUE(&ifp->if_snd, m);
913
914                 if (m == NULL) {
915                         if (flag & O_NONBLOCK) {
916                                 mtx_unlock(&tp->tap_mtx);
917                                 return (EWOULDBLOCK);
918                         }
919
920                         tp->tap_flags |= TAP_RWAIT;
921                         error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
922                             "taprd", 0);
923                         if (error) {
924                                 mtx_unlock(&tp->tap_mtx);
925                                 return (error);
926                         }
927                 }
928         } while (m == NULL);
929         mtx_unlock(&tp->tap_mtx);
930
931         /* feed packet to bpf */
932         BPF_MTAP(ifp, m);
933
934         /* xfer packet to user space */
935         while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
936                 len = min(uio->uio_resid, m->m_len);
937                 if (len == 0)
938                         break;
939
940                 error = uiomove(mtod(m, void *), len, uio);
941                 m = m_free(m);
942         }
943
944         if (m != NULL) {
945                 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname, 
946                         dev2unit(dev));
947                 m_freem(m);
948         }
949
950         return (error);
951 } /* tapread */
952
953
954 /*
955  * tapwrite
956  *
957  * the cdevsw write interface - an atomic write is a packet - or else!
958  */
959 static int
960 tapwrite(struct cdev *dev, struct uio *uio, int flag)
961 {
962         struct ether_header     *eh;
963         struct tap_softc        *tp = dev->si_drv1;
964         struct ifnet            *ifp = tp->tap_ifp;
965         struct mbuf             *m;
966
967         TAPDEBUG("%s writing, minor = %#x\n", 
968                 ifp->if_xname, dev2unit(dev));
969
970         if (uio->uio_resid == 0)
971                 return (0);
972
973         if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
974                 TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
975                         ifp->if_xname, uio->uio_resid, dev2unit(dev));
976
977                 return (EIO);
978         }
979
980         if ((m = m_uiotombuf(uio, M_NOWAIT, 0, ETHER_ALIGN,
981             M_PKTHDR)) == NULL) {
982                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
983                 return (ENOBUFS);
984         }
985
986         m->m_pkthdr.rcvif = ifp;
987
988         /*
989          * Only pass a unicast frame to ether_input(), if it would actually
990          * have been received by non-virtual hardware.
991          */
992         if (m->m_len < sizeof(struct ether_header)) {
993                 m_freem(m);
994                 return (0);
995         }
996         eh = mtod(m, struct ether_header *);
997
998         if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
999             !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1000             bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1001                 m_freem(m);
1002                 return (0);
1003         }
1004
1005         /* Pass packet up to parent. */
1006         CURVNET_SET(ifp->if_vnet);
1007         (*ifp->if_input)(ifp, m);
1008         CURVNET_RESTORE();
1009         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); /* ibytes are counted in parent */
1010
1011         return (0);
1012 } /* tapwrite */
1013
1014
1015 /*
1016  * tappoll
1017  *
1018  * the poll interface, this is only useful on reads
1019  * really. the write detect always returns true, write never blocks
1020  * anyway, it either accepts the packet or drops it
1021  */
1022 static int
1023 tappoll(struct cdev *dev, int events, struct thread *td)
1024 {
1025         struct tap_softc        *tp = dev->si_drv1;
1026         struct ifnet            *ifp = tp->tap_ifp;
1027         int                      revents = 0;
1028
1029         TAPDEBUG("%s polling, minor = %#x\n", 
1030                 ifp->if_xname, dev2unit(dev));
1031
1032         if (events & (POLLIN | POLLRDNORM)) {
1033                 IFQ_LOCK(&ifp->if_snd);
1034                 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1035                         TAPDEBUG("%s have data in queue. len = %d, " \
1036                                 "minor = %#x\n", ifp->if_xname,
1037                                 ifp->if_snd.ifq_len, dev2unit(dev));
1038
1039                         revents |= (events & (POLLIN | POLLRDNORM));
1040                 } else {
1041                         TAPDEBUG("%s waiting for data, minor = %#x\n",
1042                                 ifp->if_xname, dev2unit(dev));
1043
1044                         selrecord(td, &tp->tap_rsel);
1045                 }
1046                 IFQ_UNLOCK(&ifp->if_snd);
1047         }
1048
1049         if (events & (POLLOUT | POLLWRNORM))
1050                 revents |= (events & (POLLOUT | POLLWRNORM));
1051
1052         return (revents);
1053 } /* tappoll */
1054
1055
1056 /*
1057  * tap_kqfilter
1058  *
1059  * support for kevent() system call
1060  */
1061 static int
1062 tapkqfilter(struct cdev *dev, struct knote *kn)
1063 {
1064         struct tap_softc        *tp = dev->si_drv1;
1065         struct ifnet            *ifp = tp->tap_ifp;
1066
1067         switch (kn->kn_filter) {
1068         case EVFILT_READ:
1069                 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1070                         ifp->if_xname, dev2unit(dev));
1071                 kn->kn_fop = &tap_read_filterops;
1072                 break;
1073
1074         case EVFILT_WRITE:
1075                 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1076                         ifp->if_xname, dev2unit(dev));
1077                 kn->kn_fop = &tap_write_filterops;
1078                 break;
1079
1080         default:
1081                 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1082                         ifp->if_xname, dev2unit(dev));
1083                 return (EINVAL);
1084                 /* NOT REACHED */
1085         }
1086
1087         kn->kn_hook = tp;
1088         knlist_add(&tp->tap_rsel.si_note, kn, 0);
1089
1090         return (0);
1091 } /* tapkqfilter */
1092
1093
1094 /*
1095  * tap_kqread
1096  * 
1097  * Return true if there is data in the interface queue
1098  */
1099 static int
1100 tapkqread(struct knote *kn, long hint)
1101 {
1102         int                      ret;
1103         struct tap_softc        *tp = kn->kn_hook;
1104         struct cdev             *dev = tp->tap_dev;
1105         struct ifnet            *ifp = tp->tap_ifp;
1106
1107         if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1108                 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1109                         ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1110                 ret = 1;
1111         } else {
1112                 TAPDEBUG("%s waiting for data, minor = %#x\n",
1113                         ifp->if_xname, dev2unit(dev));
1114                 ret = 0;
1115         }
1116
1117         return (ret);
1118 } /* tapkqread */
1119
1120
1121 /*
1122  * tap_kqwrite
1123  *
1124  * Always can write. Return the MTU in kn->data
1125  */
1126 static int
1127 tapkqwrite(struct knote *kn, long hint)
1128 {
1129         struct tap_softc        *tp = kn->kn_hook;
1130         struct ifnet            *ifp = tp->tap_ifp;
1131
1132         kn->kn_data = ifp->if_mtu;
1133
1134         return (1);
1135 } /* tapkqwrite */
1136
1137
1138 static void
1139 tapkqdetach(struct knote *kn)
1140 {
1141         struct tap_softc        *tp = kn->kn_hook;
1142
1143         knlist_remove(&tp->tap_rsel.si_note, kn, 0);
1144 } /* tapkqdetach */
1145