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