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