if_tap.c revision 256008
1139823Simp/*- 263670Snsayer * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com> 363670Snsayer * All rights reserved. 463670Snsayer * 563670Snsayer * Redistribution and use in source and binary forms, with or without 663670Snsayer * modification, are permitted provided that the following conditions 763670Snsayer * are met: 863670Snsayer * 1. Redistributions of source code must retain the above copyright 963670Snsayer * notice, this list of conditions and the following disclaimer. 1063670Snsayer * 2. Redistributions in binary form must reproduce the above copyright 1163670Snsayer * notice, this list of conditions and the following disclaimer in the 1263670Snsayer * documentation and/or other materials provided with the distribution. 1363670Snsayer * 1463670Snsayer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1563670Snsayer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1663670Snsayer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1763670Snsayer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1863670Snsayer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1963670Snsayer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2063670Snsayer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2163670Snsayer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2263670Snsayer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2363670Snsayer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2463670Snsayer * SUCH DAMAGE. 2563670Snsayer * 2663670Snsayer * BASED ON: 2763670Snsayer * ------------------------------------------------------------------------- 2863670Snsayer * 2963670Snsayer * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> 3063670Snsayer * Nottingham University 1987. 3163670Snsayer */ 3263670Snsayer 3363670Snsayer/* 3463670Snsayer * $FreeBSD: head/sys/net/if_tap.c 256008 2013-10-02 20:44:36Z glebius $ 3563803Snsayer * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $ 3663670Snsayer */ 3763670Snsayer 38162711Sru#include "opt_compat.h" 3963670Snsayer#include "opt_inet.h" 4063670Snsayer 4163670Snsayer#include <sys/param.h> 4263670Snsayer#include <sys/conf.h> 43139207Sphk#include <sys/fcntl.h> 4463670Snsayer#include <sys/filio.h> 45236724Strociny#include <sys/jail.h> 4663670Snsayer#include <sys/kernel.h> 4763670Snsayer#include <sys/malloc.h> 4863670Snsayer#include <sys/mbuf.h> 49129880Sphk#include <sys/module.h> 5063670Snsayer#include <sys/poll.h> 51164033Srwatson#include <sys/priv.h> 5263670Snsayer#include <sys/proc.h> 53139207Sphk#include <sys/selinfo.h> 5463670Snsayer#include <sys/signalvar.h> 5563670Snsayer#include <sys/socket.h> 5663670Snsayer#include <sys/sockio.h> 5763670Snsayer#include <sys/sysctl.h> 5863670Snsayer#include <sys/systm.h> 5963670Snsayer#include <sys/ttycom.h> 6063670Snsayer#include <sys/uio.h> 6183043Sbrooks#include <sys/queue.h> 6263670Snsayer 6363670Snsayer#include <net/bpf.h> 6463670Snsayer#include <net/ethernet.h> 6563670Snsayer#include <net/if.h> 66166497Sbms#include <net/if_clone.h> 67152315Sru#include <net/if_dl.h> 68238183Semaste#include <net/if_media.h> 69236725Strociny#include <net/if_types.h> 7063670Snsayer#include <net/route.h> 71236724Strociny#include <net/vnet.h> 7263670Snsayer 7363670Snsayer#include <netinet/in.h> 7463670Snsayer 7563670Snsayer#include <net/if_tapvar.h> 7663670Snsayer#include <net/if_tap.h> 7763670Snsayer 7863670Snsayer 7963670Snsayer#define CDEV_NAME "tap" 8063670Snsayer#define TAPDEBUG if (tapdebug) printf 8163670Snsayer 82241610Sglebiusstatic const char tapname[] = "tap"; 83241610Sglebiusstatic const char vmnetname[] = "vmnet"; 8483043Sbrooks#define TAPMAXUNIT 0x7fff 85126077Sphk#define VMNET_DEV_MASK CLONE_FLAG0 8663670Snsayer 8763670Snsayer/* module */ 88111742Sdesstatic int tapmodevent(module_t, int, void *); 8963670Snsayer 9063670Snsayer/* device */ 91148868Srwatsonstatic void tapclone(void *, struct ucred *, char *, int, 92148868Srwatson struct cdev **); 93130585Sphkstatic void tapcreate(struct cdev *); 9463670Snsayer 9563670Snsayer/* network interface */ 9693084Sbdestatic void tapifstart(struct ifnet *); 9793084Sbdestatic int tapifioctl(struct ifnet *, u_long, caddr_t); 9893084Sbdestatic void tapifinit(void *); 9963670Snsayer 100166497Sbmsstatic int tap_clone_create(struct if_clone *, int, caddr_t); 101166497Sbmsstatic void tap_clone_destroy(struct ifnet *); 102241610Sglebiusstatic struct if_clone *tap_cloner; 103166497Sbmsstatic int vmnet_clone_create(struct if_clone *, int, caddr_t); 104166497Sbmsstatic void vmnet_clone_destroy(struct ifnet *); 105241610Sglebiusstatic struct if_clone *vmnet_cloner; 106166497Sbms 10763670Snsayer/* character device */ 10863670Snsayerstatic d_open_t tapopen; 10963670Snsayerstatic d_close_t tapclose; 11063670Snsayerstatic d_read_t tapread; 11163670Snsayerstatic d_write_t tapwrite; 11263670Snsayerstatic d_ioctl_t tapioctl; 11363670Snsayerstatic d_poll_t tappoll; 114156783Semaxstatic d_kqfilter_t tapkqfilter; 11563670Snsayer 116156783Semax/* kqueue(2) */ 117156783Semaxstatic int tapkqread(struct knote *, long); 118156783Semaxstatic int tapkqwrite(struct knote *, long); 119156783Semaxstatic void tapkqdetach(struct knote *); 120156783Semax 121156783Semaxstatic struct filterops tap_read_filterops = { 122156783Semax .f_isfd = 1, 123156783Semax .f_attach = NULL, 124156783Semax .f_detach = tapkqdetach, 125156783Semax .f_event = tapkqread, 126156783Semax}; 127156783Semax 128156783Semaxstatic struct filterops tap_write_filterops = { 129156783Semax .f_isfd = 1, 130156783Semax .f_attach = NULL, 131156783Semax .f_detach = tapkqdetach, 132156783Semax .f_event = tapkqwrite, 133156783Semax}; 134156783Semax 13563670Snsayerstatic struct cdevsw tap_cdevsw = { 136126080Sphk .d_version = D_VERSION, 137226500Sed .d_flags = D_NEEDMINOR, 138111815Sphk .d_open = tapopen, 139111815Sphk .d_close = tapclose, 140111815Sphk .d_read = tapread, 141111815Sphk .d_write = tapwrite, 142111815Sphk .d_ioctl = tapioctl, 143111815Sphk .d_poll = tappoll, 144111815Sphk .d_name = CDEV_NAME, 145156783Semax .d_kqfilter = tapkqfilter, 14663670Snsayer}; 14763670Snsayer 148127003Srwatson/* 149127003Srwatson * All global variables in if_tap.c are locked with tapmtx, with the 150127003Srwatson * exception of tapdebug, which is accessed unlocked; tapclones is 151127003Srwatson * static at runtime. 152127003Srwatson */ 153127003Srwatsonstatic struct mtx tapmtx; 15483043Sbrooksstatic int tapdebug = 0; /* debug flag */ 155167713Sbmsstatic int tapuopen = 0; /* allow user open() */ 156167713Sbmsstatic int tapuponopen = 0; /* IFF_UP on open() */ 157166497Sbmsstatic int tapdclone = 1; /* enable devfs cloning */ 15883043Sbrooksstatic SLIST_HEAD(, tap_softc) taphead; /* first device */ 159126077Sphkstatic struct clonedevs *tapclones; 16063670Snsayer 16163670SnsayerMALLOC_DECLARE(M_TAP); 16263670SnsayerMALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface"); 16363670SnsayerSYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, ""); 164144979Smdodd 165144979SmdoddSYSCTL_DECL(_net_link); 166227309Sedstatic SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0, 167144979Smdodd "Ethernet tunnel software network interface"); 168144979SmdoddSYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0, 169144979Smdodd "Allow user to open /dev/tap (based on node permissions)"); 170167713SbmsSYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0, 171167713Sbms "Bring interface up when /dev/tap is opened"); 172166497SbmsSYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tapdclone, 0, 173166497Sbms "Enably legacy devfs interface creation"); 174144979SmdoddSYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, ""); 175144979Smdodd 176166497SbmsTUNABLE_INT("net.link.tap.devfs_cloning", &tapdclone); 177166497Sbms 17863670SnsayerDEV_MODULE(if_tap, tapmodevent, NULL); 17963670Snsayer 180166497Sbmsstatic int 181166497Sbmstap_clone_create(struct if_clone *ifc, int unit, caddr_t params) 182166497Sbms{ 183166497Sbms struct cdev *dev; 184166497Sbms int i; 185166497Sbms 186241610Sglebius /* Find any existing device, or allocate new unit number. */ 187241610Sglebius i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, 0); 188166497Sbms if (i) { 189243615Sdavidxu dev = make_dev(&tap_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, 190241610Sglebius "%s%d", tapname, unit); 191166497Sbms } 192166497Sbms 193166497Sbms tapcreate(dev); 194166497Sbms return (0); 195166497Sbms} 196166497Sbms 197166497Sbms/* vmnet devices are tap devices in disguise */ 198166497Sbmsstatic int 199166497Sbmsvmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params) 200166497Sbms{ 201241610Sglebius struct cdev *dev; 202241610Sglebius int i; 203241610Sglebius 204241610Sglebius /* Find any existing device, or allocate new unit number. */ 205241610Sglebius i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, VMNET_DEV_MASK); 206241610Sglebius if (i) { 207241610Sglebius dev = make_dev(&tap_cdevsw, unit | VMNET_DEV_MASK, UID_ROOT, 208241610Sglebius GID_WHEEL, 0600, "%s%d", tapname, unit); 209241610Sglebius } 210241610Sglebius 211241610Sglebius tapcreate(dev); 212241610Sglebius return (0); 213166497Sbms} 214166497Sbms 215166497Sbmsstatic void 216166497Sbmstap_destroy(struct tap_softc *tp) 217166497Sbms{ 218166497Sbms struct ifnet *ifp = tp->tap_ifp; 219166497Sbms 220236724Strociny CURVNET_SET(ifp->if_vnet); 221240938Semaste destroy_dev(tp->tap_dev); 222225177Sattilio seldrain(&tp->tap_rsel); 223256008Sglebius knlist_clear(&tp->tap_rsel.si_note, 0); 224166497Sbms knlist_destroy(&tp->tap_rsel.si_note); 225166497Sbms ether_ifdetach(ifp); 226227459Sbrooks if_free(ifp); 227166497Sbms 228166497Sbms mtx_destroy(&tp->tap_mtx); 229166497Sbms free(tp, M_TAP); 230236724Strociny CURVNET_RESTORE(); 231166497Sbms} 232166497Sbms 233166497Sbmsstatic void 234166497Sbmstap_clone_destroy(struct ifnet *ifp) 235166497Sbms{ 236166497Sbms struct tap_softc *tp = ifp->if_softc; 237166497Sbms 238166497Sbms mtx_lock(&tapmtx); 239166497Sbms SLIST_REMOVE(&taphead, tp, tap_softc, tap_next); 240166497Sbms mtx_unlock(&tapmtx); 241166497Sbms tap_destroy(tp); 242166497Sbms} 243166497Sbms 244166497Sbms/* vmnet devices are tap devices in disguise */ 245166497Sbmsstatic void 246166497Sbmsvmnet_clone_destroy(struct ifnet *ifp) 247166497Sbms{ 248166497Sbms tap_clone_destroy(ifp); 249166497Sbms} 250166497Sbms 25163670Snsayer/* 25263670Snsayer * tapmodevent 25363670Snsayer * 25463670Snsayer * module event handler 25563670Snsayer */ 25663670Snsayerstatic int 257156783Semaxtapmodevent(module_t mod, int type, void *data) 25863670Snsayer{ 25983043Sbrooks static eventhandler_tag eh_tag = NULL; 26083043Sbrooks struct tap_softc *tp = NULL; 26183043Sbrooks struct ifnet *ifp = NULL; 26263670Snsayer 26363670Snsayer switch (type) { 26463670Snsayer case MOD_LOAD: 26563670Snsayer 26683043Sbrooks /* intitialize device */ 26783043Sbrooks 268127003Srwatson mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF); 26983043Sbrooks SLIST_INIT(&taphead); 27083043Sbrooks 271126845Sphk clone_setup(&tapclones); 27271602Sphk eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000); 273127003Srwatson if (eh_tag == NULL) { 274127170Srwatson clone_cleanup(&tapclones); 275127003Srwatson mtx_destroy(&tapmtx); 276126077Sphk return (ENOMEM); 277127003Srwatson } 278241610Sglebius tap_cloner = if_clone_simple(tapname, tap_clone_create, 279241610Sglebius tap_clone_destroy, 0); 280241610Sglebius vmnet_cloner = if_clone_simple(vmnetname, vmnet_clone_create, 281241610Sglebius vmnet_clone_destroy, 0); 28283043Sbrooks return (0); 28363670Snsayer 28483043Sbrooks case MOD_UNLOAD: 285127003Srwatson /* 286127003Srwatson * The EBUSY algorithm here can't quite atomically 287127003Srwatson * guarantee that this is race-free since we have to 288127003Srwatson * release the tap mtx to deregister the clone handler. 289127003Srwatson */ 290127003Srwatson mtx_lock(&tapmtx); 291127003Srwatson SLIST_FOREACH(tp, &taphead, tap_next) { 292127098Srwatson mtx_lock(&tp->tap_mtx); 293127003Srwatson if (tp->tap_flags & TAP_OPEN) { 294127098Srwatson mtx_unlock(&tp->tap_mtx); 295127003Srwatson mtx_unlock(&tapmtx); 29683043Sbrooks return (EBUSY); 297127003Srwatson } 298127098Srwatson mtx_unlock(&tp->tap_mtx); 299127003Srwatson } 300127003Srwatson mtx_unlock(&tapmtx); 30183043Sbrooks 30271602Sphk EVENTHANDLER_DEREGISTER(dev_clone, eh_tag); 303241610Sglebius if_clone_detach(tap_cloner); 304241610Sglebius if_clone_detach(vmnet_cloner); 305204464Skib drain_dev_clone_events(); 30663670Snsayer 307127003Srwatson mtx_lock(&tapmtx); 30883043Sbrooks while ((tp = SLIST_FIRST(&taphead)) != NULL) { 30983043Sbrooks SLIST_REMOVE_HEAD(&taphead, tap_next); 310127003Srwatson mtx_unlock(&tapmtx); 31183043Sbrooks 312147256Sbrooks ifp = tp->tap_ifp; 31383043Sbrooks 314121816Sbrooks TAPDEBUG("detaching %s\n", ifp->if_xname); 31583043Sbrooks 316166497Sbms tap_destroy(tp); 317127003Srwatson mtx_lock(&tapmtx); 31883043Sbrooks } 319127003Srwatson mtx_unlock(&tapmtx); 320126077Sphk clone_cleanup(&tapclones); 32163670Snsayer 322135354Srwatson mtx_destroy(&tapmtx); 323135354Srwatson 32483043Sbrooks break; 32563670Snsayer 32663670Snsayer default: 32763670Snsayer return (EOPNOTSUPP); 32863670Snsayer } 32963670Snsayer 33063670Snsayer return (0); 33163670Snsayer} /* tapmodevent */ 33263670Snsayer 33363670Snsayer 33463670Snsayer/* 33571602Sphk * DEVFS handler 33671602Sphk * 33771602Sphk * We need to support two kind of devices - tap and vmnet 33871602Sphk */ 33971602Sphkstatic void 340156783Semaxtapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev) 34171602Sphk{ 342166497Sbms char devname[SPECNAMELEN + 1]; 343166497Sbms int i, unit, append_unit; 344166438Sbms int extra; 34571602Sphk 346130640Sphk if (*dev != NULL) 34771602Sphk return; 34871602Sphk 349166514Sbms if (!tapdclone || 350166514Sbms (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)) 351166497Sbms return; 352166497Sbms 353166497Sbms unit = 0; 354166497Sbms append_unit = 0; 355126077Sphk extra = 0; 356166497Sbms 357166497Sbms /* We're interested in only tap/vmnet devices. */ 358241610Sglebius if (strcmp(name, tapname) == 0) { 359126077Sphk unit = -1; 360241610Sglebius } else if (strcmp(name, vmnetname) == 0) { 361126077Sphk unit = -1; 362126077Sphk extra = VMNET_DEV_MASK; 363241610Sglebius } else if (dev_stdclone(name, NULL, tapname, &unit) != 1) { 364241610Sglebius if (dev_stdclone(name, NULL, vmnetname, &unit) != 1) { 365126077Sphk return; 366166497Sbms } else { 367166497Sbms extra = VMNET_DEV_MASK; 368166497Sbms } 36983043Sbrooks } 37071602Sphk 371166497Sbms if (unit == -1) 372166497Sbms append_unit = 1; 373166497Sbms 374236724Strociny CURVNET_SET(CRED_TO_VNET(cred)); 375126077Sphk /* find any existing device, or allocate new unit number */ 376126077Sphk i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra); 377126077Sphk if (i) { 378166497Sbms if (append_unit) { 379166497Sbms /* 380166497Sbms * We were passed 'tun' or 'tap', with no unit specified 381166497Sbms * so we'll need to append it now. 382166497Sbms */ 383166497Sbms namelen = snprintf(devname, sizeof(devname), "%s%d", name, 384166497Sbms unit); 385166497Sbms name = devname; 386166497Sbms } 387166497Sbms 388204464Skib *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra, 389204464Skib cred, UID_ROOT, GID_WHEEL, 0600, "%s", name); 39071602Sphk } 391166497Sbms 392166497Sbms if_clone_create(name, namelen, NULL); 393236724Strociny CURVNET_RESTORE(); 39471602Sphk} /* tapclone */ 39571602Sphk 39671602Sphk 39771602Sphk/* 39863670Snsayer * tapcreate 39963670Snsayer * 40063670Snsayer * to create interface 40163670Snsayer */ 40263670Snsayerstatic void 403156783Semaxtapcreate(struct cdev *dev) 40463670Snsayer{ 40563670Snsayer struct ifnet *ifp = NULL; 40663670Snsayer struct tap_softc *tp = NULL; 40763670Snsayer unsigned short macaddr_hi; 408178221Semax uint32_t macaddr_mid; 409213028Sjhb int unit; 410241610Sglebius const char *name = NULL; 411147256Sbrooks u_char eaddr[6]; 41263670Snsayer 41363670Snsayer /* allocate driver storage and create device */ 414184205Sdes tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO); 415127098Srwatson mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF); 416127003Srwatson mtx_lock(&tapmtx); 41783043Sbrooks SLIST_INSERT_HEAD(&taphead, tp, tap_next); 418127003Srwatson mtx_unlock(&tapmtx); 41963670Snsayer 420126796Sphk unit = dev2unit(dev); 42183043Sbrooks 42263670Snsayer /* select device: tap or vmnet */ 423126796Sphk if (unit & VMNET_DEV_MASK) { 424241610Sglebius name = vmnetname; 42563803Snsayer tp->tap_flags |= TAP_VMNET; 42683043Sbrooks } else 427241610Sglebius name = tapname; 42863670Snsayer 429126796Sphk unit &= TAPMAXUNIT; 430126796Sphk 431183397Sed TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev)); 43283043Sbrooks 43363670Snsayer /* generate fake MAC address: 00 bd xx xx xx unit_no */ 43463670Snsayer macaddr_hi = htons(0x00bd); 435178221Semax macaddr_mid = (uint32_t) ticks; 436147256Sbrooks bcopy(&macaddr_hi, eaddr, sizeof(short)); 437178221Semax bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t)); 438147256Sbrooks eaddr[5] = (u_char)unit; 43963670Snsayer 440111742Sdes /* fill the rest and attach interface */ 441147256Sbrooks ifp = tp->tap_ifp = if_alloc(IFT_ETHER); 442147256Sbrooks if (ifp == NULL) 443147256Sbrooks panic("%s%d: can not if_alloc()", name, unit); 44463670Snsayer ifp->if_softc = tp; 445121816Sbrooks if_initname(ifp, name, unit); 44663670Snsayer ifp->if_init = tapifinit; 44763670Snsayer ifp->if_start = tapifstart; 44863670Snsayer ifp->if_ioctl = tapifioctl; 44963670Snsayer ifp->if_mtu = ETHERMTU; 45063670Snsayer ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST); 451213028Sjhb IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 452205222Sqingli ifp->if_capabilities |= IFCAP_LINKSTATE; 453205222Sqingli ifp->if_capenable |= IFCAP_LINKSTATE; 45463670Snsayer 45583043Sbrooks dev->si_drv1 = tp; 456126077Sphk tp->tap_dev = dev; 45783043Sbrooks 458147256Sbrooks ether_ifattach(ifp, eaddr); 45963670Snsayer 460127098Srwatson mtx_lock(&tp->tap_mtx); 46163803Snsayer tp->tap_flags |= TAP_INITED; 462127098Srwatson mtx_unlock(&tp->tap_mtx); 46363803Snsayer 464213028Sjhb knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx); 465158697Semax 466121816Sbrooks TAPDEBUG("interface %s is created. minor = %#x\n", 467183397Sed ifp->if_xname, dev2unit(dev)); 46863670Snsayer} /* tapcreate */ 46963670Snsayer 47063670Snsayer 47163670Snsayer/* 472111742Sdes * tapopen 47363670Snsayer * 47463670Snsayer * to open tunnel. must be superuser 47563670Snsayer */ 47663670Snsayerstatic int 477156783Semaxtapopen(struct cdev *dev, int flag, int mode, struct thread *td) 47863670Snsayer{ 47963670Snsayer struct tap_softc *tp = NULL; 480133460Semax struct ifnet *ifp = NULL; 481213028Sjhb int error; 48263670Snsayer 483164033Srwatson if (tapuopen == 0) { 484164033Srwatson error = priv_check(td, PRIV_NET_TAP); 485164033Srwatson if (error) 486164033Srwatson return (error); 487164033Srwatson } 48863670Snsayer 489126796Sphk if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT) 490126796Sphk return (ENXIO); 49183043Sbrooks 49263670Snsayer tp = dev->si_drv1; 49363670Snsayer 494127165Srwatson mtx_lock(&tp->tap_mtx); 495127165Srwatson if (tp->tap_flags & TAP_OPEN) { 496127165Srwatson mtx_unlock(&tp->tap_mtx); 497127165Srwatson return (EBUSY); 498127165Srwatson } 49963670Snsayer 500152315Sru bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr)); 50183366Sjulian tp->tap_pid = td->td_proc->p_pid; 50263670Snsayer tp->tap_flags |= TAP_OPEN; 503147256Sbrooks ifp = tp->tap_ifp; 50463670Snsayer 505148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 506148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 507167713Sbms if (tapuponopen) 508167713Sbms ifp->if_flags |= IFF_UP; 509205024Sqingli if_link_state_change(ifp, LINK_STATE_UP); 510213028Sjhb mtx_unlock(&tp->tap_mtx); 51163670Snsayer 512183397Sed TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev)); 513133460Semax 51463670Snsayer return (0); 51563670Snsayer} /* tapopen */ 51663670Snsayer 51763670Snsayer 51863670Snsayer/* 51963670Snsayer * tapclose 52063670Snsayer * 52163670Snsayer * close the device - mark i/f down & delete routing info 52263670Snsayer */ 52363670Snsayerstatic int 524156783Semaxtapclose(struct cdev *dev, int foo, int bar, struct thread *td) 52563670Snsayer{ 526156783Semax struct ifaddr *ifa; 52763670Snsayer struct tap_softc *tp = dev->si_drv1; 528147256Sbrooks struct ifnet *ifp = tp->tap_ifp; 52963670Snsayer 53063670Snsayer /* junk all pending output */ 531213028Sjhb mtx_lock(&tp->tap_mtx); 532236724Strociny CURVNET_SET(ifp->if_vnet); 53383043Sbrooks IF_DRAIN(&ifp->if_snd); 53463670Snsayer 53563803Snsayer /* 53663803Snsayer * do not bring the interface down, and do not anything with 53763803Snsayer * interface, if we are in VMnet mode. just close the device. 53863803Snsayer */ 53963803Snsayer 54063803Snsayer if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) { 541127098Srwatson mtx_unlock(&tp->tap_mtx); 54263670Snsayer if_down(ifp); 543213028Sjhb mtx_lock(&tp->tap_mtx); 544148887Srwatson if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 545213028Sjhb ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 546213028Sjhb mtx_unlock(&tp->tap_mtx); 54763803Snsayer TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 548146620Speadar rtinit(ifa, (int)RTM_DELETE, 0); 54963670Snsayer } 550146620Speadar if_purgeaddrs(ifp); 551213028Sjhb mtx_lock(&tp->tap_mtx); 55263670Snsayer } 553213028Sjhb } 55463670Snsayer 555205024Sqingli if_link_state_change(ifp, LINK_STATE_DOWN); 556236724Strociny CURVNET_RESTORE(); 557236724Strociny 55896122Salfred funsetown(&tp->tap_sigio); 559122352Stanimura selwakeuppri(&tp->tap_rsel, PZERO+1); 560213028Sjhb KNOTE_LOCKED(&tp->tap_rsel.si_note, 0); 56163670Snsayer 56263670Snsayer tp->tap_flags &= ~TAP_OPEN; 56363670Snsayer tp->tap_pid = 0; 564127098Srwatson mtx_unlock(&tp->tap_mtx); 56563670Snsayer 566121816Sbrooks TAPDEBUG("%s is closed. minor = %#x\n", 567183397Sed ifp->if_xname, dev2unit(dev)); 56863670Snsayer 56963670Snsayer return (0); 57063670Snsayer} /* tapclose */ 57163670Snsayer 57263670Snsayer 57363670Snsayer/* 57463670Snsayer * tapifinit 57563670Snsayer * 57663670Snsayer * network interface initialization function 57763670Snsayer */ 57863670Snsayerstatic void 579156783Semaxtapifinit(void *xtp) 58063670Snsayer{ 58163670Snsayer struct tap_softc *tp = (struct tap_softc *)xtp; 582147256Sbrooks struct ifnet *ifp = tp->tap_ifp; 58363670Snsayer 584121816Sbrooks TAPDEBUG("initializing %s\n", ifp->if_xname); 58563670Snsayer 586213028Sjhb mtx_lock(&tp->tap_mtx); 587148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 588148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 589213028Sjhb mtx_unlock(&tp->tap_mtx); 59063670Snsayer 59163670Snsayer /* attempt to start output */ 59263670Snsayer tapifstart(ifp); 59363670Snsayer} /* tapifinit */ 59463670Snsayer 59563670Snsayer 59663670Snsayer/* 59763670Snsayer * tapifioctl 59863670Snsayer * 59963670Snsayer * Process an ioctl request on network interface 60063670Snsayer */ 601105228Sphkstatic int 602156783Semaxtapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 60363670Snsayer{ 604160376Sbrooks struct tap_softc *tp = ifp->if_softc; 605189866Sscf struct ifreq *ifr = (struct ifreq *)data; 60663670Snsayer struct ifstat *ifs = NULL; 607238183Semaste struct ifmediareq *ifmr = NULL; 608238183Semaste int dummy, error = 0; 60963670Snsayer 61063670Snsayer switch (cmd) { 61163670Snsayer case SIOCSIFFLAGS: /* XXX -- just like vmnet does */ 61263670Snsayer case SIOCADDMULTI: 61363670Snsayer case SIOCDELMULTI: 61483043Sbrooks break; 61563670Snsayer 616238183Semaste case SIOCGIFMEDIA: 617238183Semaste ifmr = (struct ifmediareq *)data; 618238183Semaste dummy = ifmr->ifm_count; 619238183Semaste ifmr->ifm_count = 1; 620238183Semaste ifmr->ifm_status = IFM_AVALID; 621238183Semaste ifmr->ifm_active = IFM_ETHER; 622238183Semaste if (tp->tap_flags & TAP_OPEN) 623238183Semaste ifmr->ifm_status |= IFM_ACTIVE; 624238183Semaste ifmr->ifm_current = ifmr->ifm_active; 625238183Semaste if (dummy >= 1) { 626238183Semaste int media = IFM_ETHER; 627238183Semaste error = copyout(&media, ifmr->ifm_ulist, 628238183Semaste sizeof(int)); 629238183Semaste } 630238183Semaste break; 631238183Semaste 632189866Sscf case SIOCSIFMTU: 633189866Sscf ifp->if_mtu = ifr->ifr_mtu; 634189866Sscf break; 635189866Sscf 63663670Snsayer case SIOCGIFSTATUS: 63763670Snsayer ifs = (struct ifstat *)data; 63863670Snsayer dummy = strlen(ifs->ascii); 639127098Srwatson mtx_lock(&tp->tap_mtx); 64063670Snsayer if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii)) 64163670Snsayer snprintf(ifs->ascii + dummy, 64263670Snsayer sizeof(ifs->ascii) - dummy, 64363670Snsayer "\tOpened by PID %d\n", tp->tap_pid); 644127098Srwatson mtx_unlock(&tp->tap_mtx); 64583043Sbrooks break; 64663670Snsayer 64763670Snsayer default: 648238183Semaste error = ether_ioctl(ifp, cmd, data); 649238183Semaste break; 65063670Snsayer } 65163670Snsayer 652238183Semaste return (error); 65363670Snsayer} /* tapifioctl */ 65463670Snsayer 65563670Snsayer 65663670Snsayer/* 657111742Sdes * tapifstart 658111742Sdes * 65963670Snsayer * queue packets from higher level ready to put out 66063670Snsayer */ 66163670Snsayerstatic void 662156783Semaxtapifstart(struct ifnet *ifp) 66363670Snsayer{ 66463670Snsayer struct tap_softc *tp = ifp->if_softc; 66563670Snsayer 666121816Sbrooks TAPDEBUG("%s starting\n", ifp->if_xname); 66763670Snsayer 66863803Snsayer /* 66963803Snsayer * do not junk pending output if we are in VMnet mode. 67063803Snsayer * XXX: can this do any harm because of queue overflow? 67163803Snsayer */ 67263803Snsayer 673127098Srwatson mtx_lock(&tp->tap_mtx); 674111742Sdes if (((tp->tap_flags & TAP_VMNET) == 0) && 67563803Snsayer ((tp->tap_flags & TAP_READY) != TAP_READY)) { 676213028Sjhb struct mbuf *m; 67763670Snsayer 678127098Srwatson /* Unlocked read. */ 679121816Sbrooks TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname, 680121816Sbrooks tp->tap_flags); 68163670Snsayer 682213028Sjhb for (;;) { 68363670Snsayer IF_DEQUEUE(&ifp->if_snd, m); 684213028Sjhb if (m != NULL) { 68563670Snsayer m_freem(m); 686213028Sjhb ifp->if_oerrors++; 687213028Sjhb } else 688213028Sjhb break; 689213028Sjhb } 690213028Sjhb mtx_unlock(&tp->tap_mtx); 69163670Snsayer 69263670Snsayer return; 69363670Snsayer } 69463670Snsayer 695148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 69663670Snsayer 697213028Sjhb if (!IFQ_IS_EMPTY(&ifp->if_snd)) { 69863670Snsayer if (tp->tap_flags & TAP_RWAIT) { 69963670Snsayer tp->tap_flags &= ~TAP_RWAIT; 700111748Sdes wakeup(tp); 70163670Snsayer } 70263670Snsayer 703127098Srwatson if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) { 704127098Srwatson mtx_unlock(&tp->tap_mtx); 70595883Salfred pgsigio(&tp->tap_sigio, SIGIO, 0); 706213028Sjhb mtx_lock(&tp->tap_mtx); 707213028Sjhb } 70863670Snsayer 709122352Stanimura selwakeuppri(&tp->tap_rsel, PZERO+1); 710213028Sjhb KNOTE_LOCKED(&tp->tap_rsel.si_note, 0); 71163670Snsayer ifp->if_opackets ++; /* obytes are counted in ether_output */ 71263670Snsayer } 71363670Snsayer 714148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 715213028Sjhb mtx_unlock(&tp->tap_mtx); 71663670Snsayer} /* tapifstart */ 71763670Snsayer 71863670Snsayer 71963670Snsayer/* 72063670Snsayer * tapioctl 72163670Snsayer * 72263670Snsayer * the cdevsw interface is now pretty minimal 72363670Snsayer */ 72463670Snsayerstatic int 725156783Semaxtapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 72663670Snsayer{ 72763670Snsayer struct tap_softc *tp = dev->si_drv1; 728147256Sbrooks struct ifnet *ifp = tp->tap_ifp; 729111742Sdes struct tapinfo *tapp = NULL; 730102052Ssobomax int f; 731162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 732162711Sru defined(COMPAT_FREEBSD4) 733162711Sru int ival; 734162711Sru#endif 73563670Snsayer 73663670Snsayer switch (cmd) { 737111742Sdes case TAPSIFINFO: 738111742Sdes tapp = (struct tapinfo *)data; 739213028Sjhb mtx_lock(&tp->tap_mtx); 740111742Sdes ifp->if_mtu = tapp->mtu; 741111742Sdes ifp->if_type = tapp->type; 742111742Sdes ifp->if_baudrate = tapp->baudrate; 743213028Sjhb mtx_unlock(&tp->tap_mtx); 744111742Sdes break; 74563670Snsayer 746111742Sdes case TAPGIFINFO: 747111742Sdes tapp = (struct tapinfo *)data; 748213028Sjhb mtx_lock(&tp->tap_mtx); 749111742Sdes tapp->mtu = ifp->if_mtu; 750111742Sdes tapp->type = ifp->if_type; 751111742Sdes tapp->baudrate = ifp->if_baudrate; 752213028Sjhb mtx_unlock(&tp->tap_mtx); 753111742Sdes break; 75463670Snsayer 75563670Snsayer case TAPSDEBUG: 756159079Smarius tapdebug = *(int *)data; 75783043Sbrooks break; 75863670Snsayer 75963670Snsayer case TAPGDEBUG: 760159079Smarius *(int *)data = tapdebug; 76183043Sbrooks break; 76263670Snsayer 763182880Semax case TAPGIFNAME: { 764182880Semax struct ifreq *ifr = (struct ifreq *) data; 765182880Semax 766182880Semax strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ); 767182880Semax } break; 768182880Semax 76963670Snsayer case FIONBIO: 77083043Sbrooks break; 77163670Snsayer 77263670Snsayer case FIOASYNC: 773127098Srwatson mtx_lock(&tp->tap_mtx); 774159079Smarius if (*(int *)data) 77563670Snsayer tp->tap_flags |= TAP_ASYNC; 77663670Snsayer else 77763670Snsayer tp->tap_flags &= ~TAP_ASYNC; 778127098Srwatson mtx_unlock(&tp->tap_mtx); 77983043Sbrooks break; 78063670Snsayer 78163670Snsayer case FIONREAD: 782213028Sjhb if (!IFQ_IS_EMPTY(&ifp->if_snd)) { 783213028Sjhb struct mbuf *mb; 78463670Snsayer 785213028Sjhb IFQ_LOCK(&ifp->if_snd); 786213028Sjhb IFQ_POLL_NOLOCK(&ifp->if_snd, mb); 787213028Sjhb for (*(int *)data = 0; mb != NULL; 788213028Sjhb mb = mb->m_next) 789159079Smarius *(int *)data += mb->m_len; 790213028Sjhb IFQ_UNLOCK(&ifp->if_snd); 79183043Sbrooks } else 792159079Smarius *(int *)data = 0; 79383043Sbrooks break; 79463670Snsayer 79563670Snsayer case FIOSETOWN: 796159079Smarius return (fsetown(*(int *)data, &tp->tap_sigio)); 79763670Snsayer 79863670Snsayer case FIOGETOWN: 799159079Smarius *(int *)data = fgetown(&tp->tap_sigio); 80063670Snsayer return (0); 80163670Snsayer 80263670Snsayer /* this is deprecated, FIOSETOWN should be used instead */ 80363670Snsayer case TIOCSPGRP: 804159079Smarius return (fsetown(-(*(int *)data), &tp->tap_sigio)); 80563670Snsayer 80663670Snsayer /* this is deprecated, FIOGETOWN should be used instead */ 80763670Snsayer case TIOCGPGRP: 808159079Smarius *(int *)data = -fgetown(&tp->tap_sigio); 80963670Snsayer return (0); 81063670Snsayer 81163670Snsayer /* VMware/VMnet port ioctl's */ 81263670Snsayer 813162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 814162711Sru defined(COMPAT_FREEBSD4) 815162711Sru case _IO('V', 0): 816162711Sru ival = IOCPARM_IVAL(data); 817162711Sru data = (caddr_t)&ival; 818162711Sru /* FALLTHROUGH */ 819162711Sru#endif 82083043Sbrooks case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */ 821162711Sru f = *(int *)data; 82263670Snsayer f &= 0x0fff; 82363670Snsayer f &= ~IFF_CANTCHANGE; 82463670Snsayer f |= IFF_UP; 82563670Snsayer 826213028Sjhb mtx_lock(&tp->tap_mtx); 82763670Snsayer ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE); 828213028Sjhb mtx_unlock(&tp->tap_mtx); 82983043Sbrooks break; 83063670Snsayer 83163861Snsayer case OSIOCGIFADDR: /* get MAC address of the remote side */ 83263670Snsayer case SIOCGIFADDR: 833127165Srwatson mtx_lock(&tp->tap_mtx); 83463861Snsayer bcopy(tp->ether_addr, data, sizeof(tp->ether_addr)); 835127165Srwatson mtx_unlock(&tp->tap_mtx); 83683043Sbrooks break; 83763670Snsayer 83863861Snsayer case SIOCSIFADDR: /* set MAC address of the remote side */ 839127165Srwatson mtx_lock(&tp->tap_mtx); 84063861Snsayer bcopy(data, tp->ether_addr, sizeof(tp->ether_addr)); 841127165Srwatson mtx_unlock(&tp->tap_mtx); 84283043Sbrooks break; 84363670Snsayer 84463670Snsayer default: 84563670Snsayer return (ENOTTY); 84663670Snsayer } 84763670Snsayer return (0); 84863670Snsayer} /* tapioctl */ 84963670Snsayer 85063670Snsayer 85163670Snsayer/* 85263670Snsayer * tapread 85363670Snsayer * 85463670Snsayer * the cdevsw read interface - reads a packet at a time, or at 85563670Snsayer * least as much of a packet as can be read 85663670Snsayer */ 85763670Snsayerstatic int 858156783Semaxtapread(struct cdev *dev, struct uio *uio, int flag) 85963670Snsayer{ 86063670Snsayer struct tap_softc *tp = dev->si_drv1; 861147256Sbrooks struct ifnet *ifp = tp->tap_ifp; 86290227Sdillon struct mbuf *m = NULL; 863213028Sjhb int error = 0, len; 86463670Snsayer 865183397Sed TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev)); 86663670Snsayer 867127098Srwatson mtx_lock(&tp->tap_mtx); 86863670Snsayer if ((tp->tap_flags & TAP_READY) != TAP_READY) { 869127098Srwatson mtx_unlock(&tp->tap_mtx); 870127098Srwatson 871127098Srwatson /* Unlocked read. */ 872121816Sbrooks TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n", 873183397Sed ifp->if_xname, dev2unit(dev), tp->tap_flags); 87463803Snsayer 87563670Snsayer return (EHOSTDOWN); 87663670Snsayer } 87763670Snsayer 87863670Snsayer tp->tap_flags &= ~TAP_RWAIT; 87963670Snsayer 88063670Snsayer /* sleep until we get a packet */ 88163670Snsayer do { 88290227Sdillon IF_DEQUEUE(&ifp->if_snd, m); 88363670Snsayer 88490227Sdillon if (m == NULL) { 885213028Sjhb if (flag & O_NONBLOCK) { 886213028Sjhb mtx_unlock(&tp->tap_mtx); 88763670Snsayer return (EWOULDBLOCK); 888213028Sjhb } 889111742Sdes 89063670Snsayer tp->tap_flags |= TAP_RWAIT; 891213028Sjhb error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1), 892213028Sjhb "taprd", 0); 893213028Sjhb if (error) { 894213028Sjhb mtx_unlock(&tp->tap_mtx); 89563670Snsayer return (error); 896213028Sjhb } 89763670Snsayer } 89890227Sdillon } while (m == NULL); 899213028Sjhb mtx_unlock(&tp->tap_mtx); 90063670Snsayer 90163670Snsayer /* feed packet to bpf */ 902106939Ssam BPF_MTAP(ifp, m); 90363670Snsayer 90463670Snsayer /* xfer packet to user space */ 90590227Sdillon while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) { 90690227Sdillon len = min(uio->uio_resid, m->m_len); 90763670Snsayer if (len == 0) 90863670Snsayer break; 90963670Snsayer 910111741Sdes error = uiomove(mtod(m, void *), len, uio); 91190227Sdillon m = m_free(m); 91263670Snsayer } 91363670Snsayer 91490227Sdillon if (m != NULL) { 915121816Sbrooks TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname, 916183397Sed dev2unit(dev)); 91790227Sdillon m_freem(m); 91863670Snsayer } 91963670Snsayer 92063670Snsayer return (error); 92163670Snsayer} /* tapread */ 92263670Snsayer 92363670Snsayer 92463670Snsayer/* 92563670Snsayer * tapwrite 92663670Snsayer * 92763670Snsayer * the cdevsw write interface - an atomic write is a packet - or else! 92863670Snsayer */ 92963670Snsayerstatic int 930156783Semaxtapwrite(struct cdev *dev, struct uio *uio, int flag) 93163670Snsayer{ 932166443Sbms struct ether_header *eh; 93363670Snsayer struct tap_softc *tp = dev->si_drv1; 934147256Sbrooks struct ifnet *ifp = tp->tap_ifp; 935137101Sglebius struct mbuf *m; 93663670Snsayer 937240945Semaste TAPDEBUG("%s writing, minor = %#x\n", 938183397Sed ifp->if_xname, dev2unit(dev)); 93963670Snsayer 94063670Snsayer if (uio->uio_resid == 0) 94163670Snsayer return (0); 94263670Snsayer 94363670Snsayer if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) { 944194990Skib TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n", 945183397Sed ifp->if_xname, uio->uio_resid, dev2unit(dev)); 94663803Snsayer 94763670Snsayer return (EIO); 94863670Snsayer } 94963670Snsayer 950243882Sglebius if ((m = m_uiotombuf(uio, M_NOWAIT, 0, ETHER_ALIGN, 951163915Sandre M_PKTHDR)) == NULL) { 95263670Snsayer ifp->if_ierrors ++; 953163986Scsjp return (ENOBUFS); 95463670Snsayer } 95563670Snsayer 956137101Sglebius m->m_pkthdr.rcvif = ifp; 957111742Sdes 958166443Sbms /* 959166443Sbms * Only pass a unicast frame to ether_input(), if it would actually 960166443Sbms * have been received by non-virtual hardware. 961166443Sbms */ 962166443Sbms if (m->m_len < sizeof(struct ether_header)) { 963166443Sbms m_freem(m); 964166443Sbms return (0); 965166443Sbms } 966166443Sbms eh = mtod(m, struct ether_header *); 967166443Sbms 968166443Sbms if (eh && (ifp->if_flags & IFF_PROMISC) == 0 && 969166443Sbms !ETHER_IS_MULTICAST(eh->ether_dhost) && 970166443Sbms bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) { 971166443Sbms m_freem(m); 972166443Sbms return (0); 973166443Sbms } 974166443Sbms 975106939Ssam /* Pass packet up to parent. */ 976236724Strociny CURVNET_SET(ifp->if_vnet); 977137101Sglebius (*ifp->if_input)(ifp, m); 978236724Strociny CURVNET_RESTORE(); 979106939Ssam ifp->if_ipackets ++; /* ibytes are counted in parent */ 98063670Snsayer 98163670Snsayer return (0); 98263670Snsayer} /* tapwrite */ 98363670Snsayer 98463670Snsayer 98563670Snsayer/* 98663670Snsayer * tappoll 98763670Snsayer * 98863670Snsayer * the poll interface, this is only useful on reads 98963670Snsayer * really. the write detect always returns true, write never blocks 99063670Snsayer * anyway, it either accepts the packet or drops it 99163670Snsayer */ 99263670Snsayerstatic int 993156783Semaxtappoll(struct cdev *dev, int events, struct thread *td) 99463670Snsayer{ 99563670Snsayer struct tap_softc *tp = dev->si_drv1; 996147256Sbrooks struct ifnet *ifp = tp->tap_ifp; 997213028Sjhb int revents = 0; 99863670Snsayer 999121816Sbrooks TAPDEBUG("%s polling, minor = %#x\n", 1000183397Sed ifp->if_xname, dev2unit(dev)); 100163670Snsayer 100263670Snsayer if (events & (POLLIN | POLLRDNORM)) { 1003213028Sjhb IFQ_LOCK(&ifp->if_snd); 1004213028Sjhb if (!IFQ_IS_EMPTY(&ifp->if_snd)) { 1005121816Sbrooks TAPDEBUG("%s have data in queue. len = %d, " \ 1006121816Sbrooks "minor = %#x\n", ifp->if_xname, 1007183397Sed ifp->if_snd.ifq_len, dev2unit(dev)); 100863803Snsayer 100963670Snsayer revents |= (events & (POLLIN | POLLRDNORM)); 101083043Sbrooks } else { 1011121816Sbrooks TAPDEBUG("%s waiting for data, minor = %#x\n", 1012183397Sed ifp->if_xname, dev2unit(dev)); 101363803Snsayer 101483805Sjhb selrecord(td, &tp->tap_rsel); 101563670Snsayer } 1016213028Sjhb IFQ_UNLOCK(&ifp->if_snd); 101763670Snsayer } 101863670Snsayer 101963670Snsayer if (events & (POLLOUT | POLLWRNORM)) 102063670Snsayer revents |= (events & (POLLOUT | POLLWRNORM)); 102163670Snsayer 102263670Snsayer return (revents); 102363670Snsayer} /* tappoll */ 1024156783Semax 1025156783Semax 1026156783Semax/* 1027156783Semax * tap_kqfilter 1028156783Semax * 1029156783Semax * support for kevent() system call 1030156783Semax */ 1031156783Semaxstatic int 1032156783Semaxtapkqfilter(struct cdev *dev, struct knote *kn) 1033156783Semax{ 1034156783Semax struct tap_softc *tp = dev->si_drv1; 1035156783Semax struct ifnet *ifp = tp->tap_ifp; 1036156783Semax 1037156783Semax switch (kn->kn_filter) { 1038156783Semax case EVFILT_READ: 1039156783Semax TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n", 1040183397Sed ifp->if_xname, dev2unit(dev)); 1041156783Semax kn->kn_fop = &tap_read_filterops; 1042156783Semax break; 1043156783Semax 1044156783Semax case EVFILT_WRITE: 1045156783Semax TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n", 1046183397Sed ifp->if_xname, dev2unit(dev)); 1047156783Semax kn->kn_fop = &tap_write_filterops; 1048156783Semax break; 1049156783Semax 1050156783Semax default: 1051156783Semax TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n", 1052183397Sed ifp->if_xname, dev2unit(dev)); 1053156783Semax return (EINVAL); 1054156783Semax /* NOT REACHED */ 1055156783Semax } 1056156783Semax 1057213028Sjhb kn->kn_hook = tp; 1058156783Semax knlist_add(&tp->tap_rsel.si_note, kn, 0); 1059156783Semax 1060156783Semax return (0); 1061156783Semax} /* tapkqfilter */ 1062156783Semax 1063156783Semax 1064156783Semax/* 1065156783Semax * tap_kqread 1066156783Semax * 1067156783Semax * Return true if there is data in the interface queue 1068156783Semax */ 1069156783Semaxstatic int 1070156783Semaxtapkqread(struct knote *kn, long hint) 1071156783Semax{ 1072213028Sjhb int ret; 1073213028Sjhb struct tap_softc *tp = kn->kn_hook; 1074213028Sjhb struct cdev *dev = tp->tap_dev; 1075156783Semax struct ifnet *ifp = tp->tap_ifp; 1076156783Semax 1077156783Semax if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) { 1078156783Semax TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n", 1079183397Sed ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev)); 1080156783Semax ret = 1; 1081156783Semax } else { 1082156783Semax TAPDEBUG("%s waiting for data, minor = %#x\n", 1083183397Sed ifp->if_xname, dev2unit(dev)); 1084156783Semax ret = 0; 1085156783Semax } 1086156783Semax 1087156783Semax return (ret); 1088156783Semax} /* tapkqread */ 1089156783Semax 1090156783Semax 1091156783Semax/* 1092156783Semax * tap_kqwrite 1093156783Semax * 1094156783Semax * Always can write. Return the MTU in kn->data 1095156783Semax */ 1096156783Semaxstatic int 1097156783Semaxtapkqwrite(struct knote *kn, long hint) 1098156783Semax{ 1099213028Sjhb struct tap_softc *tp = kn->kn_hook; 1100156783Semax struct ifnet *ifp = tp->tap_ifp; 1101156783Semax 1102156783Semax kn->kn_data = ifp->if_mtu; 1103156783Semax 1104156783Semax return (1); 1105156783Semax} /* tapkqwrite */ 1106156783Semax 1107156783Semax 1108156783Semaxstatic void 1109156783Semaxtapkqdetach(struct knote *kn) 1110156783Semax{ 1111213028Sjhb struct tap_softc *tp = kn->kn_hook; 1112156783Semax 1113156783Semax knlist_remove(&tp->tap_rsel.si_note, kn, 0); 1114156783Semax} /* tapkqdetach */ 1115156783Semax 1116