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