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