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