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