if_tun.c revision 263140
1/* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */ 2 3/*- 4 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> 5 * Nottingham University 1987. 6 * 7 * This source may be freely distributed, however I would be interested 8 * in any changes that are made. 9 * 10 * This driver takes packets off the IP i/f and hands them up to a 11 * user process to have its wicked way with. This driver has it's 12 * roots in a similar driver written by Phil Cockcroft (formerly) at 13 * UCL. This driver is based much more on read/write/poll mode of 14 * operation though. 15 * 16 * $FreeBSD: head/sys/net/if_tun.c 263140 2014-03-14 02:58:48Z glebius $ 17 */ 18 19#include "opt_atalk.h" 20#include "opt_inet.h" 21#include "opt_inet6.h" 22 23#include <sys/param.h> 24#include <sys/priv.h> 25#include <sys/proc.h> 26#include <sys/systm.h> 27#include <sys/jail.h> 28#include <sys/mbuf.h> 29#include <sys/module.h> 30#include <sys/socket.h> 31#include <sys/fcntl.h> 32#include <sys/filio.h> 33#include <sys/sockio.h> 34#include <sys/ttycom.h> 35#include <sys/poll.h> 36#include <sys/selinfo.h> 37#include <sys/signalvar.h> 38#include <sys/filedesc.h> 39#include <sys/kernel.h> 40#include <sys/sysctl.h> 41#include <sys/conf.h> 42#include <sys/uio.h> 43#include <sys/malloc.h> 44#include <sys/random.h> 45 46#include <net/if.h> 47#include <net/if_var.h> 48#include <net/if_clone.h> 49#include <net/if_types.h> 50#include <net/netisr.h> 51#include <net/route.h> 52#include <net/vnet.h> 53#ifdef INET 54#include <netinet/in.h> 55#endif 56#include <net/bpf.h> 57#include <net/if_tun.h> 58 59#include <sys/queue.h> 60#include <sys/condvar.h> 61 62#include <security/mac/mac_framework.h> 63 64/* 65 * tun_list is protected by global tunmtx. Other mutable fields are 66 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is 67 * static for the duration of a tunnel interface. 68 */ 69struct tun_softc { 70 TAILQ_ENTRY(tun_softc) tun_list; 71 struct cdev *tun_dev; 72 u_short tun_flags; /* misc flags */ 73#define TUN_OPEN 0x0001 74#define TUN_INITED 0x0002 75#define TUN_RCOLL 0x0004 76#define TUN_IASET 0x0008 77#define TUN_DSTADDR 0x0010 78#define TUN_LMODE 0x0020 79#define TUN_RWAIT 0x0040 80#define TUN_ASYNC 0x0080 81#define TUN_IFHEAD 0x0100 82 83#define TUN_READY (TUN_OPEN | TUN_INITED) 84 85 /* 86 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this 87 * actually needed? Can we just return EBUSY if already open? 88 * Problem is that this involved inherent races when a tun device 89 * is handed off from one process to another, as opposed to just 90 * being slightly stale informationally. 91 */ 92 pid_t tun_pid; /* owning pid */ 93 struct ifnet *tun_ifp; /* the interface */ 94 struct sigio *tun_sigio; /* information for async I/O */ 95 struct selinfo tun_rsel; /* read select */ 96 struct mtx tun_mtx; /* protect mutable softc fields */ 97 struct cv tun_cv; /* protect against ref'd dev destroy */ 98}; 99#define TUN2IFP(sc) ((sc)->tun_ifp) 100 101#define TUNDEBUG if (tundebug) if_printf 102 103/* 104 * All mutable global variables in if_tun are locked using tunmtx, with 105 * the exception of tundebug, which is used unlocked, and tunclones, 106 * which is static after setup. 107 */ 108static struct mtx tunmtx; 109static const char tunname[] = "tun"; 110static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface"); 111static int tundebug = 0; 112static int tundclone = 1; 113static struct clonedevs *tunclones; 114static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead); 115SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, ""); 116 117SYSCTL_DECL(_net_link); 118static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0, 119 "IP tunnel software network interface."); 120SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0, 121 "Enable legacy devfs interface creation."); 122 123TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone); 124 125static void tunclone(void *arg, struct ucred *cred, char *name, 126 int namelen, struct cdev **dev); 127static void tuncreate(const char *name, struct cdev *dev); 128static int tunifioctl(struct ifnet *, u_long, caddr_t); 129static void tuninit(struct ifnet *); 130static int tunmodevent(module_t, int, void *); 131static int tunoutput(struct ifnet *, struct mbuf *, 132 const struct sockaddr *, struct route *ro); 133static void tunstart(struct ifnet *); 134 135static int tun_clone_create(struct if_clone *, int, caddr_t); 136static void tun_clone_destroy(struct ifnet *); 137static struct if_clone *tun_cloner; 138 139static d_open_t tunopen; 140static d_close_t tunclose; 141static d_read_t tunread; 142static d_write_t tunwrite; 143static d_ioctl_t tunioctl; 144static d_poll_t tunpoll; 145static d_kqfilter_t tunkqfilter; 146 147static int tunkqread(struct knote *, long); 148static int tunkqwrite(struct knote *, long); 149static void tunkqdetach(struct knote *); 150 151static struct filterops tun_read_filterops = { 152 .f_isfd = 1, 153 .f_attach = NULL, 154 .f_detach = tunkqdetach, 155 .f_event = tunkqread, 156}; 157 158static struct filterops tun_write_filterops = { 159 .f_isfd = 1, 160 .f_attach = NULL, 161 .f_detach = tunkqdetach, 162 .f_event = tunkqwrite, 163}; 164 165static struct cdevsw tun_cdevsw = { 166 .d_version = D_VERSION, 167 .d_flags = D_NEEDMINOR, 168 .d_open = tunopen, 169 .d_close = tunclose, 170 .d_read = tunread, 171 .d_write = tunwrite, 172 .d_ioctl = tunioctl, 173 .d_poll = tunpoll, 174 .d_kqfilter = tunkqfilter, 175 .d_name = tunname, 176}; 177 178static int 179tun_clone_create(struct if_clone *ifc, int unit, caddr_t params) 180{ 181 struct cdev *dev; 182 int i; 183 184 /* find any existing device, or allocate new unit number */ 185 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0); 186 if (i) { 187 /* No preexisting struct cdev *, create one */ 188 dev = make_dev(&tun_cdevsw, unit, 189 UID_UUCP, GID_DIALER, 0600, "%s%d", tunname, unit); 190 } 191 tuncreate(tunname, dev); 192 193 return (0); 194} 195 196static void 197tunclone(void *arg, struct ucred *cred, char *name, int namelen, 198 struct cdev **dev) 199{ 200 char devname[SPECNAMELEN + 1]; 201 int u, i, append_unit; 202 203 if (*dev != NULL) 204 return; 205 206 /* 207 * If tun cloning is enabled, only the superuser can create an 208 * interface. 209 */ 210 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0) 211 return; 212 213 if (strcmp(name, tunname) == 0) { 214 u = -1; 215 } else if (dev_stdclone(name, NULL, tunname, &u) != 1) 216 return; /* Don't recognise the name */ 217 if (u != -1 && u > IF_MAXUNIT) 218 return; /* Unit number too high */ 219 220 if (u == -1) 221 append_unit = 1; 222 else 223 append_unit = 0; 224 225 CURVNET_SET(CRED_TO_VNET(cred)); 226 /* find any existing device, or allocate new unit number */ 227 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0); 228 if (i) { 229 if (append_unit) { 230 namelen = snprintf(devname, sizeof(devname), "%s%d", 231 name, u); 232 name = devname; 233 } 234 /* No preexisting struct cdev *, create one */ 235 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred, 236 UID_UUCP, GID_DIALER, 0600, "%s", name); 237 } 238 239 if_clone_create(name, namelen, NULL); 240 CURVNET_RESTORE(); 241} 242 243static void 244tun_destroy(struct tun_softc *tp) 245{ 246 struct cdev *dev; 247 248 mtx_lock(&tp->tun_mtx); 249 if ((tp->tun_flags & TUN_OPEN) != 0) 250 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx); 251 else 252 mtx_unlock(&tp->tun_mtx); 253 254 CURVNET_SET(TUN2IFP(tp)->if_vnet); 255 dev = tp->tun_dev; 256 bpfdetach(TUN2IFP(tp)); 257 if_detach(TUN2IFP(tp)); 258 if_free(TUN2IFP(tp)); 259 destroy_dev(dev); 260 seldrain(&tp->tun_rsel); 261 knlist_clear(&tp->tun_rsel.si_note, 0); 262 knlist_destroy(&tp->tun_rsel.si_note); 263 mtx_destroy(&tp->tun_mtx); 264 cv_destroy(&tp->tun_cv); 265 free(tp, M_TUN); 266 CURVNET_RESTORE(); 267} 268 269static void 270tun_clone_destroy(struct ifnet *ifp) 271{ 272 struct tun_softc *tp = ifp->if_softc; 273 274 mtx_lock(&tunmtx); 275 TAILQ_REMOVE(&tunhead, tp, tun_list); 276 mtx_unlock(&tunmtx); 277 tun_destroy(tp); 278} 279 280static int 281tunmodevent(module_t mod, int type, void *data) 282{ 283 static eventhandler_tag tag; 284 struct tun_softc *tp; 285 286 switch (type) { 287 case MOD_LOAD: 288 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF); 289 clone_setup(&tunclones); 290 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000); 291 if (tag == NULL) 292 return (ENOMEM); 293 tun_cloner = if_clone_simple(tunname, tun_clone_create, 294 tun_clone_destroy, 0); 295 break; 296 case MOD_UNLOAD: 297 if_clone_detach(tun_cloner); 298 EVENTHANDLER_DEREGISTER(dev_clone, tag); 299 drain_dev_clone_events(); 300 301 mtx_lock(&tunmtx); 302 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) { 303 TAILQ_REMOVE(&tunhead, tp, tun_list); 304 mtx_unlock(&tunmtx); 305 tun_destroy(tp); 306 mtx_lock(&tunmtx); 307 } 308 mtx_unlock(&tunmtx); 309 clone_cleanup(&tunclones); 310 mtx_destroy(&tunmtx); 311 break; 312 default: 313 return EOPNOTSUPP; 314 } 315 return 0; 316} 317 318static moduledata_t tun_mod = { 319 "if_tun", 320 tunmodevent, 321 0 322}; 323 324DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 325MODULE_VERSION(if_tun, 1); 326 327static void 328tunstart(struct ifnet *ifp) 329{ 330 struct tun_softc *tp = ifp->if_softc; 331 struct mbuf *m; 332 333 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname); 334 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 335 IFQ_LOCK(&ifp->if_snd); 336 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 337 if (m == NULL) { 338 IFQ_UNLOCK(&ifp->if_snd); 339 return; 340 } 341 IFQ_UNLOCK(&ifp->if_snd); 342 } 343 344 mtx_lock(&tp->tun_mtx); 345 if (tp->tun_flags & TUN_RWAIT) { 346 tp->tun_flags &= ~TUN_RWAIT; 347 wakeup(tp); 348 } 349 selwakeuppri(&tp->tun_rsel, PZERO + 1); 350 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0); 351 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) { 352 mtx_unlock(&tp->tun_mtx); 353 pgsigio(&tp->tun_sigio, SIGIO, 0); 354 } else 355 mtx_unlock(&tp->tun_mtx); 356} 357 358/* XXX: should return an error code so it can fail. */ 359static void 360tuncreate(const char *name, struct cdev *dev) 361{ 362 struct tun_softc *sc; 363 struct ifnet *ifp; 364 365 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO); 366 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF); 367 cv_init(&sc->tun_cv, "tun_condvar"); 368 sc->tun_flags = TUN_INITED; 369 sc->tun_dev = dev; 370 mtx_lock(&tunmtx); 371 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list); 372 mtx_unlock(&tunmtx); 373 374 ifp = sc->tun_ifp = if_alloc(IFT_PPP); 375 if (ifp == NULL) 376 panic("%s%d: failed to if_alloc() interface.\n", 377 name, dev2unit(dev)); 378 if_initname(ifp, name, dev2unit(dev)); 379 ifp->if_mtu = TUNMTU; 380 ifp->if_ioctl = tunifioctl; 381 ifp->if_output = tunoutput; 382 ifp->if_start = tunstart; 383 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 384 ifp->if_softc = sc; 385 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 386 ifp->if_snd.ifq_drv_maxlen = 0; 387 IFQ_SET_READY(&ifp->if_snd); 388 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx); 389 ifp->if_capabilities |= IFCAP_LINKSTATE; 390 ifp->if_capenable |= IFCAP_LINKSTATE; 391 392 if_attach(ifp); 393 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 394 dev->si_drv1 = sc; 395 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n", 396 ifp->if_xname, dev2unit(dev)); 397} 398 399static int 400tunopen(struct cdev *dev, int flag, int mode, struct thread *td) 401{ 402 struct ifnet *ifp; 403 struct tun_softc *tp; 404 405 /* 406 * XXXRW: Non-atomic test and set of dev->si_drv1 requires 407 * synchronization. 408 */ 409 tp = dev->si_drv1; 410 if (!tp) { 411 tuncreate(tunname, dev); 412 tp = dev->si_drv1; 413 } 414 415 /* 416 * XXXRW: This use of tun_pid is subject to error due to the 417 * fact that a reference to the tunnel can live beyond the 418 * death of the process that created it. Can we replace this 419 * with a simple busy flag? 420 */ 421 mtx_lock(&tp->tun_mtx); 422 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) { 423 mtx_unlock(&tp->tun_mtx); 424 return (EBUSY); 425 } 426 tp->tun_pid = td->td_proc->p_pid; 427 428 tp->tun_flags |= TUN_OPEN; 429 ifp = TUN2IFP(tp); 430 if_link_state_change(ifp, LINK_STATE_UP); 431 TUNDEBUG(ifp, "open\n"); 432 mtx_unlock(&tp->tun_mtx); 433 434 return (0); 435} 436 437/* 438 * tunclose - close the device - mark i/f down & delete 439 * routing info 440 */ 441static int 442tunclose(struct cdev *dev, int foo, int bar, struct thread *td) 443{ 444 struct tun_softc *tp; 445 struct ifnet *ifp; 446 447 tp = dev->si_drv1; 448 ifp = TUN2IFP(tp); 449 450 mtx_lock(&tp->tun_mtx); 451 tp->tun_flags &= ~TUN_OPEN; 452 tp->tun_pid = 0; 453 454 /* 455 * junk all pending output 456 */ 457 CURVNET_SET(ifp->if_vnet); 458 IFQ_PURGE(&ifp->if_snd); 459 460 if (ifp->if_flags & IFF_UP) { 461 mtx_unlock(&tp->tun_mtx); 462 if_down(ifp); 463 mtx_lock(&tp->tun_mtx); 464 } 465 466 /* Delete all addresses and routes which reference this interface. */ 467 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 468 struct ifaddr *ifa; 469 470 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 471 mtx_unlock(&tp->tun_mtx); 472 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 473 /* deal w/IPv4 PtP destination; unlocked read */ 474 if (ifa->ifa_addr->sa_family == AF_INET) { 475 rtinit(ifa, (int)RTM_DELETE, 476 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 477 } else { 478 rtinit(ifa, (int)RTM_DELETE, 0); 479 } 480 } 481 if_purgeaddrs(ifp); 482 mtx_lock(&tp->tun_mtx); 483 } 484 if_link_state_change(ifp, LINK_STATE_DOWN); 485 CURVNET_RESTORE(); 486 487 funsetown(&tp->tun_sigio); 488 selwakeuppri(&tp->tun_rsel, PZERO + 1); 489 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0); 490 TUNDEBUG (ifp, "closed\n"); 491 492 cv_broadcast(&tp->tun_cv); 493 mtx_unlock(&tp->tun_mtx); 494 return (0); 495} 496 497static void 498tuninit(struct ifnet *ifp) 499{ 500 struct tun_softc *tp = ifp->if_softc; 501#ifdef INET 502 struct ifaddr *ifa; 503#endif 504 505 TUNDEBUG(ifp, "tuninit\n"); 506 507 mtx_lock(&tp->tun_mtx); 508 ifp->if_flags |= IFF_UP; 509 ifp->if_drv_flags |= IFF_DRV_RUNNING; 510 getmicrotime(&ifp->if_lastchange); 511 512#ifdef INET 513 if_addr_rlock(ifp); 514 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 515 if (ifa->ifa_addr->sa_family == AF_INET) { 516 struct sockaddr_in *si; 517 518 si = (struct sockaddr_in *)ifa->ifa_addr; 519 if (si->sin_addr.s_addr) 520 tp->tun_flags |= TUN_IASET; 521 522 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 523 if (si && si->sin_addr.s_addr) 524 tp->tun_flags |= TUN_DSTADDR; 525 } 526 } 527 if_addr_runlock(ifp); 528#endif 529 mtx_unlock(&tp->tun_mtx); 530} 531 532/* 533 * Process an ioctl request. 534 */ 535static int 536tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 537{ 538 struct ifreq *ifr = (struct ifreq *)data; 539 struct tun_softc *tp = ifp->if_softc; 540 struct ifstat *ifs; 541 int error = 0; 542 543 switch(cmd) { 544 case SIOCGIFSTATUS: 545 ifs = (struct ifstat *)data; 546 mtx_lock(&tp->tun_mtx); 547 if (tp->tun_pid) 548 snprintf(ifs->ascii, sizeof(ifs->ascii), 549 "\tOpened by PID %d\n", tp->tun_pid); 550 else 551 ifs->ascii[0] = '\0'; 552 mtx_unlock(&tp->tun_mtx); 553 break; 554 case SIOCSIFADDR: 555 tuninit(ifp); 556 TUNDEBUG(ifp, "address set\n"); 557 break; 558 case SIOCSIFMTU: 559 ifp->if_mtu = ifr->ifr_mtu; 560 TUNDEBUG(ifp, "mtu set\n"); 561 break; 562 case SIOCSIFFLAGS: 563 case SIOCADDMULTI: 564 case SIOCDELMULTI: 565 break; 566 default: 567 error = EINVAL; 568 } 569 return (error); 570} 571 572/* 573 * tunoutput - queue packets from higher level ready to put out. 574 */ 575static int 576tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst, 577 struct route *ro) 578{ 579 struct tun_softc *tp = ifp->if_softc; 580 u_short cached_tun_flags; 581 int error; 582 u_int32_t af; 583 584 TUNDEBUG (ifp, "tunoutput\n"); 585 586#ifdef MAC 587 error = mac_ifnet_check_transmit(ifp, m0); 588 if (error) { 589 m_freem(m0); 590 return (error); 591 } 592#endif 593 594 /* Could be unlocked read? */ 595 mtx_lock(&tp->tun_mtx); 596 cached_tun_flags = tp->tun_flags; 597 mtx_unlock(&tp->tun_mtx); 598 if ((cached_tun_flags & TUN_READY) != TUN_READY) { 599 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 600 m_freem (m0); 601 return (EHOSTDOWN); 602 } 603 604 if ((ifp->if_flags & IFF_UP) != IFF_UP) { 605 m_freem (m0); 606 return (EHOSTDOWN); 607 } 608 609 /* BPF writes need to be handled specially. */ 610 if (dst->sa_family == AF_UNSPEC) 611 bcopy(dst->sa_data, &af, sizeof(af)); 612 else 613 af = dst->sa_family; 614 615 if (bpf_peers_present(ifp->if_bpf)) 616 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0); 617 618 /* prepend sockaddr? this may abort if the mbuf allocation fails */ 619 if (cached_tun_flags & TUN_LMODE) { 620 /* allocate space for sockaddr */ 621 M_PREPEND(m0, dst->sa_len, M_NOWAIT); 622 623 /* if allocation failed drop packet */ 624 if (m0 == NULL) { 625 ifp->if_iqdrops++; 626 ifp->if_oerrors++; 627 return (ENOBUFS); 628 } else { 629 bcopy(dst, m0->m_data, dst->sa_len); 630 } 631 } 632 633 if (cached_tun_flags & TUN_IFHEAD) { 634 /* Prepend the address family */ 635 M_PREPEND(m0, 4, M_NOWAIT); 636 637 /* if allocation failed drop packet */ 638 if (m0 == NULL) { 639 ifp->if_iqdrops++; 640 ifp->if_oerrors++; 641 return (ENOBUFS); 642 } else 643 *(u_int32_t *)m0->m_data = htonl(af); 644 } else { 645#ifdef INET 646 if (af != AF_INET) 647#endif 648 { 649 m_freem(m0); 650 return (EAFNOSUPPORT); 651 } 652 } 653 654 error = (ifp->if_transmit)(ifp, m0); 655 if (error) 656 return (ENOBUFS); 657 ifp->if_opackets++; 658 return (0); 659} 660 661/* 662 * the cdevsw interface is now pretty minimal. 663 */ 664static int 665tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, 666 struct thread *td) 667{ 668 int error; 669 struct tun_softc *tp = dev->si_drv1; 670 struct tuninfo *tunp; 671 672 switch (cmd) { 673 case TUNSIFINFO: 674 tunp = (struct tuninfo *)data; 675 if (tunp->mtu < IF_MINMTU) 676 return (EINVAL); 677 if (TUN2IFP(tp)->if_mtu != tunp->mtu) { 678 error = priv_check(td, PRIV_NET_SETIFMTU); 679 if (error) 680 return (error); 681 } 682 mtx_lock(&tp->tun_mtx); 683 TUN2IFP(tp)->if_mtu = tunp->mtu; 684 TUN2IFP(tp)->if_type = tunp->type; 685 TUN2IFP(tp)->if_baudrate = tunp->baudrate; 686 mtx_unlock(&tp->tun_mtx); 687 break; 688 case TUNGIFINFO: 689 tunp = (struct tuninfo *)data; 690 mtx_lock(&tp->tun_mtx); 691 tunp->mtu = TUN2IFP(tp)->if_mtu; 692 tunp->type = TUN2IFP(tp)->if_type; 693 tunp->baudrate = TUN2IFP(tp)->if_baudrate; 694 mtx_unlock(&tp->tun_mtx); 695 break; 696 case TUNSDEBUG: 697 tundebug = *(int *)data; 698 break; 699 case TUNGDEBUG: 700 *(int *)data = tundebug; 701 break; 702 case TUNSLMODE: 703 mtx_lock(&tp->tun_mtx); 704 if (*(int *)data) { 705 tp->tun_flags |= TUN_LMODE; 706 tp->tun_flags &= ~TUN_IFHEAD; 707 } else 708 tp->tun_flags &= ~TUN_LMODE; 709 mtx_unlock(&tp->tun_mtx); 710 break; 711 case TUNSIFHEAD: 712 mtx_lock(&tp->tun_mtx); 713 if (*(int *)data) { 714 tp->tun_flags |= TUN_IFHEAD; 715 tp->tun_flags &= ~TUN_LMODE; 716 } else 717 tp->tun_flags &= ~TUN_IFHEAD; 718 mtx_unlock(&tp->tun_mtx); 719 break; 720 case TUNGIFHEAD: 721 mtx_lock(&tp->tun_mtx); 722 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0; 723 mtx_unlock(&tp->tun_mtx); 724 break; 725 case TUNSIFMODE: 726 /* deny this if UP */ 727 if (TUN2IFP(tp)->if_flags & IFF_UP) 728 return(EBUSY); 729 730 switch (*(int *)data & ~IFF_MULTICAST) { 731 case IFF_POINTOPOINT: 732 case IFF_BROADCAST: 733 mtx_lock(&tp->tun_mtx); 734 TUN2IFP(tp)->if_flags &= 735 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); 736 TUN2IFP(tp)->if_flags |= *(int *)data; 737 mtx_unlock(&tp->tun_mtx); 738 break; 739 default: 740 return(EINVAL); 741 } 742 break; 743 case TUNSIFPID: 744 mtx_lock(&tp->tun_mtx); 745 tp->tun_pid = curthread->td_proc->p_pid; 746 mtx_unlock(&tp->tun_mtx); 747 break; 748 case FIONBIO: 749 break; 750 case FIOASYNC: 751 mtx_lock(&tp->tun_mtx); 752 if (*(int *)data) 753 tp->tun_flags |= TUN_ASYNC; 754 else 755 tp->tun_flags &= ~TUN_ASYNC; 756 mtx_unlock(&tp->tun_mtx); 757 break; 758 case FIONREAD: 759 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) { 760 struct mbuf *mb; 761 IFQ_LOCK(&TUN2IFP(tp)->if_snd); 762 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb); 763 for (*(int *)data = 0; mb != NULL; mb = mb->m_next) 764 *(int *)data += mb->m_len; 765 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd); 766 } else 767 *(int *)data = 0; 768 break; 769 case FIOSETOWN: 770 return (fsetown(*(int *)data, &tp->tun_sigio)); 771 772 case FIOGETOWN: 773 *(int *)data = fgetown(&tp->tun_sigio); 774 return (0); 775 776 /* This is deprecated, FIOSETOWN should be used instead. */ 777 case TIOCSPGRP: 778 return (fsetown(-(*(int *)data), &tp->tun_sigio)); 779 780 /* This is deprecated, FIOGETOWN should be used instead. */ 781 case TIOCGPGRP: 782 *(int *)data = -fgetown(&tp->tun_sigio); 783 return (0); 784 785 default: 786 return (ENOTTY); 787 } 788 return (0); 789} 790 791/* 792 * The cdevsw read interface - reads a packet at a time, or at 793 * least as much of a packet as can be read. 794 */ 795static int 796tunread(struct cdev *dev, struct uio *uio, int flag) 797{ 798 struct tun_softc *tp = dev->si_drv1; 799 struct ifnet *ifp = TUN2IFP(tp); 800 struct mbuf *m; 801 int error=0, len; 802 803 TUNDEBUG (ifp, "read\n"); 804 mtx_lock(&tp->tun_mtx); 805 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 806 mtx_unlock(&tp->tun_mtx); 807 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 808 return (EHOSTDOWN); 809 } 810 811 tp->tun_flags &= ~TUN_RWAIT; 812 813 do { 814 IFQ_DEQUEUE(&ifp->if_snd, m); 815 if (m == NULL) { 816 if (flag & O_NONBLOCK) { 817 mtx_unlock(&tp->tun_mtx); 818 return (EWOULDBLOCK); 819 } 820 tp->tun_flags |= TUN_RWAIT; 821 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1), 822 "tunread", 0); 823 if (error != 0) { 824 mtx_unlock(&tp->tun_mtx); 825 return (error); 826 } 827 } 828 } while (m == NULL); 829 mtx_unlock(&tp->tun_mtx); 830 831 while (m && uio->uio_resid > 0 && error == 0) { 832 len = min(uio->uio_resid, m->m_len); 833 if (len != 0) 834 error = uiomove(mtod(m, void *), len, uio); 835 m = m_free(m); 836 } 837 838 if (m) { 839 TUNDEBUG(ifp, "Dropping mbuf\n"); 840 m_freem(m); 841 } 842 return (error); 843} 844 845/* 846 * the cdevsw write interface - an atomic write is a packet - or else! 847 */ 848static int 849tunwrite(struct cdev *dev, struct uio *uio, int flag) 850{ 851 struct tun_softc *tp = dev->si_drv1; 852 struct ifnet *ifp = TUN2IFP(tp); 853 struct mbuf *m; 854 uint32_t family; 855 int isr; 856 857 TUNDEBUG(ifp, "tunwrite\n"); 858 859 if ((ifp->if_flags & IFF_UP) != IFF_UP) 860 /* ignore silently */ 861 return (0); 862 863 if (uio->uio_resid == 0) 864 return (0); 865 866 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) { 867 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid); 868 return (EIO); 869 } 870 871 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) { 872 ifp->if_ierrors++; 873 return (ENOBUFS); 874 } 875 876 m->m_pkthdr.rcvif = ifp; 877#ifdef MAC 878 mac_ifnet_create_mbuf(ifp, m); 879#endif 880 881 /* Could be unlocked read? */ 882 mtx_lock(&tp->tun_mtx); 883 if (tp->tun_flags & TUN_IFHEAD) { 884 mtx_unlock(&tp->tun_mtx); 885 if (m->m_len < sizeof(family) && 886 (m = m_pullup(m, sizeof(family))) == NULL) 887 return (ENOBUFS); 888 family = ntohl(*mtod(m, u_int32_t *)); 889 m_adj(m, sizeof(family)); 890 } else { 891 mtx_unlock(&tp->tun_mtx); 892 family = AF_INET; 893 } 894 895 BPF_MTAP2(ifp, &family, sizeof(family), m); 896 897 switch (family) { 898#ifdef INET 899 case AF_INET: 900 isr = NETISR_IP; 901 break; 902#endif 903#ifdef INET6 904 case AF_INET6: 905 isr = NETISR_IPV6; 906 break; 907#endif 908#ifdef NETATALK 909 case AF_APPLETALK: 910 isr = NETISR_ATALK2; 911 break; 912#endif 913 default: 914 m_freem(m); 915 return (EAFNOSUPPORT); 916 } 917 if (harvest.point_to_point) 918 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_TUN); 919 ifp->if_ibytes += m->m_pkthdr.len; 920 ifp->if_ipackets++; 921 CURVNET_SET(ifp->if_vnet); 922 M_SETFIB(m, ifp->if_fib); 923 netisr_dispatch(isr, m); 924 CURVNET_RESTORE(); 925 return (0); 926} 927 928/* 929 * tunpoll - the poll interface, this is only useful on reads 930 * really. The write detect always returns true, write never blocks 931 * anyway, it either accepts the packet or drops it. 932 */ 933static int 934tunpoll(struct cdev *dev, int events, struct thread *td) 935{ 936 struct tun_softc *tp = dev->si_drv1; 937 struct ifnet *ifp = TUN2IFP(tp); 938 int revents = 0; 939 struct mbuf *m; 940 941 TUNDEBUG(ifp, "tunpoll\n"); 942 943 if (events & (POLLIN | POLLRDNORM)) { 944 IFQ_LOCK(&ifp->if_snd); 945 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 946 if (m != NULL) { 947 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len); 948 revents |= events & (POLLIN | POLLRDNORM); 949 } else { 950 TUNDEBUG(ifp, "tunpoll waiting\n"); 951 selrecord(td, &tp->tun_rsel); 952 } 953 IFQ_UNLOCK(&ifp->if_snd); 954 } 955 if (events & (POLLOUT | POLLWRNORM)) 956 revents |= events & (POLLOUT | POLLWRNORM); 957 958 return (revents); 959} 960 961/* 962 * tunkqfilter - support for the kevent() system call. 963 */ 964static int 965tunkqfilter(struct cdev *dev, struct knote *kn) 966{ 967 struct tun_softc *tp = dev->si_drv1; 968 struct ifnet *ifp = TUN2IFP(tp); 969 970 switch(kn->kn_filter) { 971 case EVFILT_READ: 972 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n", 973 ifp->if_xname, dev2unit(dev)); 974 kn->kn_fop = &tun_read_filterops; 975 break; 976 977 case EVFILT_WRITE: 978 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n", 979 ifp->if_xname, dev2unit(dev)); 980 kn->kn_fop = &tun_write_filterops; 981 break; 982 983 default: 984 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n", 985 ifp->if_xname, dev2unit(dev)); 986 return(EINVAL); 987 } 988 989 kn->kn_hook = tp; 990 knlist_add(&tp->tun_rsel.si_note, kn, 0); 991 992 return (0); 993} 994 995/* 996 * Return true of there is data in the interface queue. 997 */ 998static int 999tunkqread(struct knote *kn, long hint) 1000{ 1001 int ret; 1002 struct tun_softc *tp = kn->kn_hook; 1003 struct cdev *dev = tp->tun_dev; 1004 struct ifnet *ifp = TUN2IFP(tp); 1005 1006 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) { 1007 TUNDEBUG(ifp, 1008 "%s have data in the queue. Len = %d, minor = %#x\n", 1009 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev)); 1010 ret = 1; 1011 } else { 1012 TUNDEBUG(ifp, 1013 "%s waiting for data, minor = %#x\n", ifp->if_xname, 1014 dev2unit(dev)); 1015 ret = 0; 1016 } 1017 1018 return (ret); 1019} 1020 1021/* 1022 * Always can write, always return MTU in kn->data. 1023 */ 1024static int 1025tunkqwrite(struct knote *kn, long hint) 1026{ 1027 struct tun_softc *tp = kn->kn_hook; 1028 struct ifnet *ifp = TUN2IFP(tp); 1029 1030 kn->kn_data = ifp->if_mtu; 1031 1032 return (1); 1033} 1034 1035static void 1036tunkqdetach(struct knote *kn) 1037{ 1038 struct tun_softc *tp = kn->kn_hook; 1039 1040 knlist_remove(&tp->tun_rsel.si_note, kn, 0); 1041} 1042