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