if_tun.c revision 249925
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 249925 2013-04-26 12:50:32Z glebius $ 17 */ 18 19#include "opt_atalk.h" 20#include "opt_inet.h" 21#include "opt_inet6.h" 22#include "opt_ipx.h" 23 24#include <sys/param.h> 25#include <sys/priv.h> 26#include <sys/proc.h> 27#include <sys/systm.h> 28#include <sys/jail.h> 29#include <sys/mbuf.h> 30#include <sys/module.h> 31#include <sys/socket.h> 32#include <sys/fcntl.h> 33#include <sys/filio.h> 34#include <sys/sockio.h> 35#include <sys/ttycom.h> 36#include <sys/poll.h> 37#include <sys/selinfo.h> 38#include <sys/signalvar.h> 39#include <sys/filedesc.h> 40#include <sys/kernel.h> 41#include <sys/sysctl.h> 42#include <sys/conf.h> 43#include <sys/uio.h> 44#include <sys/malloc.h> 45#include <sys/random.h> 46 47#include <net/if.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_destroy(&tp->tun_rsel.si_note); 262 mtx_destroy(&tp->tun_mtx); 263 cv_destroy(&tp->tun_cv); 264 free(tp, M_TUN); 265 CURVNET_RESTORE(); 266} 267 268static void 269tun_clone_destroy(struct ifnet *ifp) 270{ 271 struct tun_softc *tp = ifp->if_softc; 272 273 mtx_lock(&tunmtx); 274 TAILQ_REMOVE(&tunhead, tp, tun_list); 275 mtx_unlock(&tunmtx); 276 tun_destroy(tp); 277} 278 279static int 280tunmodevent(module_t mod, int type, void *data) 281{ 282 static eventhandler_tag tag; 283 struct tun_softc *tp; 284 285 switch (type) { 286 case MOD_LOAD: 287 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF); 288 clone_setup(&tunclones); 289 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000); 290 if (tag == NULL) 291 return (ENOMEM); 292 tun_cloner = if_clone_simple(tunname, tun_clone_create, 293 tun_clone_destroy, 0); 294 break; 295 case MOD_UNLOAD: 296 if_clone_detach(tun_cloner); 297 EVENTHANDLER_DEREGISTER(dev_clone, tag); 298 drain_dev_clone_events(); 299 300 mtx_lock(&tunmtx); 301 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) { 302 TAILQ_REMOVE(&tunhead, tp, tun_list); 303 mtx_unlock(&tunmtx); 304 tun_destroy(tp); 305 mtx_lock(&tunmtx); 306 } 307 mtx_unlock(&tunmtx); 308 clone_cleanup(&tunclones); 309 mtx_destroy(&tunmtx); 310 break; 311 default: 312 return EOPNOTSUPP; 313 } 314 return 0; 315} 316 317static moduledata_t tun_mod = { 318 "if_tun", 319 tunmodevent, 320 0 321}; 322 323DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 324 325static void 326tunstart(struct ifnet *ifp) 327{ 328 struct tun_softc *tp = ifp->if_softc; 329 struct mbuf *m; 330 331 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname); 332 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 333 IFQ_LOCK(&ifp->if_snd); 334 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 335 if (m == NULL) { 336 IFQ_UNLOCK(&ifp->if_snd); 337 return; 338 } 339 IFQ_UNLOCK(&ifp->if_snd); 340 } 341 342 mtx_lock(&tp->tun_mtx); 343 if (tp->tun_flags & TUN_RWAIT) { 344 tp->tun_flags &= ~TUN_RWAIT; 345 wakeup(tp); 346 } 347 selwakeuppri(&tp->tun_rsel, PZERO + 1); 348 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0); 349 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) { 350 mtx_unlock(&tp->tun_mtx); 351 pgsigio(&tp->tun_sigio, SIGIO, 0); 352 } else 353 mtx_unlock(&tp->tun_mtx); 354} 355 356/* XXX: should return an error code so it can fail. */ 357static void 358tuncreate(const char *name, struct cdev *dev) 359{ 360 struct tun_softc *sc; 361 struct ifnet *ifp; 362 363 dev->si_flags &= ~SI_CHEAPCLONE; 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 sprintf(ifs->ascii + strlen(ifs->ascii), 549 "\tOpened by PID %d\n", tp->tun_pid); 550 mtx_unlock(&tp->tun_mtx); 551 break; 552 case SIOCSIFADDR: 553 tuninit(ifp); 554 TUNDEBUG(ifp, "address set\n"); 555 break; 556 case SIOCSIFDSTADDR: 557 tuninit(ifp); 558 TUNDEBUG(ifp, "destination address set\n"); 559 break; 560 case SIOCSIFMTU: 561 ifp->if_mtu = ifr->ifr_mtu; 562 TUNDEBUG(ifp, "mtu set\n"); 563 break; 564 case SIOCSIFFLAGS: 565 case SIOCADDMULTI: 566 case SIOCDELMULTI: 567 break; 568 default: 569 error = EINVAL; 570 } 571 return (error); 572} 573 574/* 575 * tunoutput - queue packets from higher level ready to put out. 576 */ 577static int 578tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst, 579 struct route *ro) 580{ 581 struct tun_softc *tp = ifp->if_softc; 582 u_short cached_tun_flags; 583 int error; 584 u_int32_t af; 585 586 TUNDEBUG (ifp, "tunoutput\n"); 587 588#ifdef MAC 589 error = mac_ifnet_check_transmit(ifp, m0); 590 if (error) { 591 m_freem(m0); 592 return (error); 593 } 594#endif 595 596 /* Could be unlocked read? */ 597 mtx_lock(&tp->tun_mtx); 598 cached_tun_flags = tp->tun_flags; 599 mtx_unlock(&tp->tun_mtx); 600 if ((cached_tun_flags & TUN_READY) != TUN_READY) { 601 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 602 m_freem (m0); 603 return (EHOSTDOWN); 604 } 605 606 if ((ifp->if_flags & IFF_UP) != IFF_UP) { 607 m_freem (m0); 608 return (EHOSTDOWN); 609 } 610 611 /* BPF writes need to be handled specially. */ 612 if (dst->sa_family == AF_UNSPEC) 613 bcopy(dst->sa_data, &af, sizeof(af)); 614 else 615 af = dst->sa_family; 616 617 if (bpf_peers_present(ifp->if_bpf)) 618 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0); 619 620 /* prepend sockaddr? this may abort if the mbuf allocation fails */ 621 if (cached_tun_flags & TUN_LMODE) { 622 /* allocate space for sockaddr */ 623 M_PREPEND(m0, dst->sa_len, M_NOWAIT); 624 625 /* if allocation failed drop packet */ 626 if (m0 == NULL) { 627 ifp->if_iqdrops++; 628 ifp->if_oerrors++; 629 return (ENOBUFS); 630 } else { 631 bcopy(dst, m0->m_data, dst->sa_len); 632 } 633 } 634 635 if (cached_tun_flags & TUN_IFHEAD) { 636 /* Prepend the address family */ 637 M_PREPEND(m0, 4, M_NOWAIT); 638 639 /* if allocation failed drop packet */ 640 if (m0 == NULL) { 641 ifp->if_iqdrops++; 642 ifp->if_oerrors++; 643 return (ENOBUFS); 644 } else 645 *(u_int32_t *)m0->m_data = htonl(af); 646 } else { 647#ifdef INET 648 if (af != AF_INET) 649#endif 650 { 651 m_freem(m0); 652 return (EAFNOSUPPORT); 653 } 654 } 655 656 error = (ifp->if_transmit)(ifp, m0); 657 if (error) 658 return (ENOBUFS); 659 ifp->if_opackets++; 660 return (0); 661} 662 663/* 664 * the cdevsw interface is now pretty minimal. 665 */ 666static int 667tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, 668 struct thread *td) 669{ 670 int error; 671 struct tun_softc *tp = dev->si_drv1; 672 struct tuninfo *tunp; 673 674 switch (cmd) { 675 case TUNSIFINFO: 676 tunp = (struct tuninfo *)data; 677 if (tunp->mtu < IF_MINMTU) 678 return (EINVAL); 679 if (TUN2IFP(tp)->if_mtu != tunp->mtu) { 680 error = priv_check(td, PRIV_NET_SETIFMTU); 681 if (error) 682 return (error); 683 } 684 mtx_lock(&tp->tun_mtx); 685 TUN2IFP(tp)->if_mtu = tunp->mtu; 686 TUN2IFP(tp)->if_type = tunp->type; 687 TUN2IFP(tp)->if_baudrate = tunp->baudrate; 688 mtx_unlock(&tp->tun_mtx); 689 break; 690 case TUNGIFINFO: 691 tunp = (struct tuninfo *)data; 692 mtx_lock(&tp->tun_mtx); 693 tunp->mtu = TUN2IFP(tp)->if_mtu; 694 tunp->type = TUN2IFP(tp)->if_type; 695 tunp->baudrate = TUN2IFP(tp)->if_baudrate; 696 mtx_unlock(&tp->tun_mtx); 697 break; 698 case TUNSDEBUG: 699 tundebug = *(int *)data; 700 break; 701 case TUNGDEBUG: 702 *(int *)data = tundebug; 703 break; 704 case TUNSLMODE: 705 mtx_lock(&tp->tun_mtx); 706 if (*(int *)data) { 707 tp->tun_flags |= TUN_LMODE; 708 tp->tun_flags &= ~TUN_IFHEAD; 709 } else 710 tp->tun_flags &= ~TUN_LMODE; 711 mtx_unlock(&tp->tun_mtx); 712 break; 713 case TUNSIFHEAD: 714 mtx_lock(&tp->tun_mtx); 715 if (*(int *)data) { 716 tp->tun_flags |= TUN_IFHEAD; 717 tp->tun_flags &= ~TUN_LMODE; 718 } else 719 tp->tun_flags &= ~TUN_IFHEAD; 720 mtx_unlock(&tp->tun_mtx); 721 break; 722 case TUNGIFHEAD: 723 mtx_lock(&tp->tun_mtx); 724 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0; 725 mtx_unlock(&tp->tun_mtx); 726 break; 727 case TUNSIFMODE: 728 /* deny this if UP */ 729 if (TUN2IFP(tp)->if_flags & IFF_UP) 730 return(EBUSY); 731 732 switch (*(int *)data & ~IFF_MULTICAST) { 733 case IFF_POINTOPOINT: 734 case IFF_BROADCAST: 735 mtx_lock(&tp->tun_mtx); 736 TUN2IFP(tp)->if_flags &= 737 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); 738 TUN2IFP(tp)->if_flags |= *(int *)data; 739 mtx_unlock(&tp->tun_mtx); 740 break; 741 default: 742 return(EINVAL); 743 } 744 break; 745 case TUNSIFPID: 746 mtx_lock(&tp->tun_mtx); 747 tp->tun_pid = curthread->td_proc->p_pid; 748 mtx_unlock(&tp->tun_mtx); 749 break; 750 case FIONBIO: 751 break; 752 case FIOASYNC: 753 mtx_lock(&tp->tun_mtx); 754 if (*(int *)data) 755 tp->tun_flags |= TUN_ASYNC; 756 else 757 tp->tun_flags &= ~TUN_ASYNC; 758 mtx_unlock(&tp->tun_mtx); 759 break; 760 case FIONREAD: 761 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) { 762 struct mbuf *mb; 763 IFQ_LOCK(&TUN2IFP(tp)->if_snd); 764 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb); 765 for (*(int *)data = 0; mb != NULL; mb = mb->m_next) 766 *(int *)data += mb->m_len; 767 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd); 768 } else 769 *(int *)data = 0; 770 break; 771 case FIOSETOWN: 772 return (fsetown(*(int *)data, &tp->tun_sigio)); 773 774 case FIOGETOWN: 775 *(int *)data = fgetown(&tp->tun_sigio); 776 return (0); 777 778 /* This is deprecated, FIOSETOWN should be used instead. */ 779 case TIOCSPGRP: 780 return (fsetown(-(*(int *)data), &tp->tun_sigio)); 781 782 /* This is deprecated, FIOGETOWN should be used instead. */ 783 case TIOCGPGRP: 784 *(int *)data = -fgetown(&tp->tun_sigio); 785 return (0); 786 787 default: 788 return (ENOTTY); 789 } 790 return (0); 791} 792 793/* 794 * The cdevsw read interface - reads a packet at a time, or at 795 * least as much of a packet as can be read. 796 */ 797static int 798tunread(struct cdev *dev, struct uio *uio, int flag) 799{ 800 struct tun_softc *tp = dev->si_drv1; 801 struct ifnet *ifp = TUN2IFP(tp); 802 struct mbuf *m; 803 int error=0, len; 804 805 TUNDEBUG (ifp, "read\n"); 806 mtx_lock(&tp->tun_mtx); 807 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 808 mtx_unlock(&tp->tun_mtx); 809 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 810 return (EHOSTDOWN); 811 } 812 813 tp->tun_flags &= ~TUN_RWAIT; 814 815 do { 816 IFQ_DEQUEUE(&ifp->if_snd, m); 817 if (m == NULL) { 818 if (flag & O_NONBLOCK) { 819 mtx_unlock(&tp->tun_mtx); 820 return (EWOULDBLOCK); 821 } 822 tp->tun_flags |= TUN_RWAIT; 823 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1), 824 "tunread", 0); 825 if (error != 0) { 826 mtx_unlock(&tp->tun_mtx); 827 return (error); 828 } 829 } 830 } while (m == NULL); 831 mtx_unlock(&tp->tun_mtx); 832 833 while (m && uio->uio_resid > 0 && error == 0) { 834 len = min(uio->uio_resid, m->m_len); 835 if (len != 0) 836 error = uiomove(mtod(m, void *), len, uio); 837 m = m_free(m); 838 } 839 840 if (m) { 841 TUNDEBUG(ifp, "Dropping mbuf\n"); 842 m_freem(m); 843 } 844 return (error); 845} 846 847/* 848 * the cdevsw write interface - an atomic write is a packet - or else! 849 */ 850static int 851tunwrite(struct cdev *dev, struct uio *uio, int flag) 852{ 853 struct tun_softc *tp = dev->si_drv1; 854 struct ifnet *ifp = TUN2IFP(tp); 855 struct mbuf *m; 856 uint32_t family; 857 int isr; 858 859 TUNDEBUG(ifp, "tunwrite\n"); 860 861 if ((ifp->if_flags & IFF_UP) != IFF_UP) 862 /* ignore silently */ 863 return (0); 864 865 if (uio->uio_resid == 0) 866 return (0); 867 868 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) { 869 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid); 870 return (EIO); 871 } 872 873 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) { 874 ifp->if_ierrors++; 875 return (ENOBUFS); 876 } 877 878 m->m_pkthdr.rcvif = ifp; 879#ifdef MAC 880 mac_ifnet_create_mbuf(ifp, m); 881#endif 882 883 /* Could be unlocked read? */ 884 mtx_lock(&tp->tun_mtx); 885 if (tp->tun_flags & TUN_IFHEAD) { 886 mtx_unlock(&tp->tun_mtx); 887 if (m->m_len < sizeof(family) && 888 (m = m_pullup(m, sizeof(family))) == NULL) 889 return (ENOBUFS); 890 family = ntohl(*mtod(m, u_int32_t *)); 891 m_adj(m, sizeof(family)); 892 } else { 893 mtx_unlock(&tp->tun_mtx); 894 family = AF_INET; 895 } 896 897 BPF_MTAP2(ifp, &family, sizeof(family), m); 898 899 switch (family) { 900#ifdef INET 901 case AF_INET: 902 isr = NETISR_IP; 903 break; 904#endif 905#ifdef INET6 906 case AF_INET6: 907 isr = NETISR_IPV6; 908 break; 909#endif 910#ifdef IPX 911 case AF_IPX: 912 isr = NETISR_IPX; 913 break; 914#endif 915#ifdef NETATALK 916 case AF_APPLETALK: 917 isr = NETISR_ATALK2; 918 break; 919#endif 920 default: 921 m_freem(m); 922 return (EAFNOSUPPORT); 923 } 924 /* First chunk of an mbuf contains good junk */ 925 if (harvest.point_to_point) 926 random_harvest(m, 16, 3, 0, RANDOM_NET); 927 ifp->if_ibytes += m->m_pkthdr.len; 928 ifp->if_ipackets++; 929 CURVNET_SET(ifp->if_vnet); 930 M_SETFIB(m, ifp->if_fib); 931 netisr_dispatch(isr, m); 932 CURVNET_RESTORE(); 933 return (0); 934} 935 936/* 937 * tunpoll - the poll interface, this is only useful on reads 938 * really. The write detect always returns true, write never blocks 939 * anyway, it either accepts the packet or drops it. 940 */ 941static int 942tunpoll(struct cdev *dev, int events, struct thread *td) 943{ 944 struct tun_softc *tp = dev->si_drv1; 945 struct ifnet *ifp = TUN2IFP(tp); 946 int revents = 0; 947 struct mbuf *m; 948 949 TUNDEBUG(ifp, "tunpoll\n"); 950 951 if (events & (POLLIN | POLLRDNORM)) { 952 IFQ_LOCK(&ifp->if_snd); 953 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 954 if (m != NULL) { 955 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len); 956 revents |= events & (POLLIN | POLLRDNORM); 957 } else { 958 TUNDEBUG(ifp, "tunpoll waiting\n"); 959 selrecord(td, &tp->tun_rsel); 960 } 961 IFQ_UNLOCK(&ifp->if_snd); 962 } 963 if (events & (POLLOUT | POLLWRNORM)) 964 revents |= events & (POLLOUT | POLLWRNORM); 965 966 return (revents); 967} 968 969/* 970 * tunkqfilter - support for the kevent() system call. 971 */ 972static int 973tunkqfilter(struct cdev *dev, struct knote *kn) 974{ 975 struct tun_softc *tp = dev->si_drv1; 976 struct ifnet *ifp = TUN2IFP(tp); 977 978 switch(kn->kn_filter) { 979 case EVFILT_READ: 980 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n", 981 ifp->if_xname, dev2unit(dev)); 982 kn->kn_fop = &tun_read_filterops; 983 break; 984 985 case EVFILT_WRITE: 986 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n", 987 ifp->if_xname, dev2unit(dev)); 988 kn->kn_fop = &tun_write_filterops; 989 break; 990 991 default: 992 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n", 993 ifp->if_xname, dev2unit(dev)); 994 return(EINVAL); 995 } 996 997 kn->kn_hook = tp; 998 knlist_add(&tp->tun_rsel.si_note, kn, 0); 999 1000 return (0); 1001} 1002 1003/* 1004 * Return true of there is data in the interface queue. 1005 */ 1006static int 1007tunkqread(struct knote *kn, long hint) 1008{ 1009 int ret; 1010 struct tun_softc *tp = kn->kn_hook; 1011 struct cdev *dev = tp->tun_dev; 1012 struct ifnet *ifp = TUN2IFP(tp); 1013 1014 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) { 1015 TUNDEBUG(ifp, 1016 "%s have data in the queue. Len = %d, minor = %#x\n", 1017 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev)); 1018 ret = 1; 1019 } else { 1020 TUNDEBUG(ifp, 1021 "%s waiting for data, minor = %#x\n", ifp->if_xname, 1022 dev2unit(dev)); 1023 ret = 0; 1024 } 1025 1026 return (ret); 1027} 1028 1029/* 1030 * Always can write, always return MTU in kn->data. 1031 */ 1032static int 1033tunkqwrite(struct knote *kn, long hint) 1034{ 1035 struct tun_softc *tp = kn->kn_hook; 1036 struct ifnet *ifp = TUN2IFP(tp); 1037 1038 kn->kn_data = ifp->if_mtu; 1039 1040 return (1); 1041} 1042 1043static void 1044tunkqdetach(struct knote *kn) 1045{ 1046 struct tun_softc *tp = kn->kn_hook; 1047 1048 knlist_remove(&tp->tun_rsel.si_note, kn, 0); 1049} 1050