Index: head/sys/dev/snp/snp.c =================================================================== --- head/sys/dev/snp/snp.c (revision 126187) +++ head/sys/dev/snp/snp.c (revision 126188) @@ -1,656 +1,655 @@ /* * Copyright (c) 1995 Ugen J.S.Antsilevich * * Redistribution and use in source forms, with and without modification, * are permitted provided that this entire comment appears intact. * * Redistribution in binary form may occur without any restrictions. * Obviously, it would be nice if you gave credit where credit is due * but requiring it would be too onerous. * * This software is provided ``AS IS'' without any warranties of any kind. * * Snoop stuff. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include static l_close_t snplclose; static l_write_t snplwrite; static d_open_t snpopen; static d_close_t snpclose; static d_read_t snpread; static d_write_t snpwrite; static d_ioctl_t snpioctl; static d_poll_t snppoll; static struct cdevsw snp_cdevsw = { .d_version = D_VERSION, - .d_flags = D_NEEDGIANT, + .d_flags = D_PSEUDO | D_NEEDGIANT, .d_open = snpopen, .d_close = snpclose, .d_read = snpread, .d_write = snpwrite, .d_ioctl = snpioctl, .d_poll = snppoll, .d_name = "snp", - .d_flags = D_PSEUDO, }; static struct linesw snpdisc = { ttyopen, snplclose, ttread, snplwrite, l_nullioctl, ttyinput, ttstart, ttymodem }; /* * This is the main snoop per-device structure. */ struct snoop { LIST_ENTRY(snoop) snp_list; /* List glue. */ int snp_unit; /* Device number. */ dev_t snp_target; /* Target tty device. */ struct tty *snp_tty; /* Target tty pointer. */ u_long snp_len; /* Possible length. */ u_long snp_base; /* Data base. */ u_long snp_blen; /* Used length. */ caddr_t snp_buf; /* Allocation pointer. */ int snp_flags; /* Flags. */ struct selinfo snp_sel; /* Select info. */ int snp_olddisc; /* Old line discipline. */ }; /* * Possible flags. */ #define SNOOP_ASYNC 0x0002 #define SNOOP_OPEN 0x0004 #define SNOOP_RWAIT 0x0008 #define SNOOP_OFLOW 0x0010 #define SNOOP_DOWN 0x0020 /* * Other constants. */ #define SNOOP_MINLEN (4*1024) /* This should be power of 2. * 4K tested to be the minimum * for which on normal tty * usage there is no need to * allocate more. */ #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough * If we grow more,something * really bad in this world.. */ static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data"); /* * The number of the "snoop" line discipline. This gets determined at * module load time. */ static int snooplinedisc; static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist); static struct clonedevs *snpclones; static struct tty *snpdevtotty(dev_t dev); static void snp_clone(void *arg, char *name, int namelen, dev_t *dev); static int snp_detach(struct snoop *snp); static int snp_down(struct snoop *snp); static int snp_in(struct snoop *snp, char *buf, int n); static int snp_modevent(module_t mod, int what, void *arg); static int snplclose(tp, flag) struct tty *tp; int flag; { struct snoop *snp; int error; snp = tp->t_sc; error = snp_down(snp); if (error != 0) return (error); error = ttylclose(tp, flag); return (error); } static int snplwrite(tp, uio, flag) struct tty *tp; struct uio *uio; int flag; { struct iovec iov; struct uio uio2; struct snoop *snp; int error, ilen; char *ibuf; error = 0; ibuf = NULL; snp = tp->t_sc; while (uio->uio_resid > 0) { ilen = imin(512, uio->uio_resid); ibuf = malloc(ilen, M_SNP, M_WAITOK); error = uiomove(ibuf, ilen, uio); if (error != 0) break; snp_in(snp, ibuf, ilen); /* Hackish, but probably the least of all evils. */ iov.iov_base = ibuf; iov.iov_len = ilen; uio2.uio_iov = &iov; uio2.uio_iovcnt = 1; uio2.uio_offset = 0; uio2.uio_resid = ilen; uio2.uio_segflg = UIO_SYSSPACE; uio2.uio_rw = UIO_WRITE; uio2.uio_td = uio->uio_td; error = ttwrite(tp, &uio2, flag); if (error != 0) break; free(ibuf, M_SNP); ibuf = NULL; } if (ibuf != NULL) free(ibuf, M_SNP); return (error); } static struct tty * snpdevtotty(dev) dev_t dev; { struct cdevsw *cdp; cdp = devsw(dev); if (cdp == NULL || (cdp->d_flags & D_TTY) == 0) return (NULL); return (dev->si_tty); } #define SNP_INPUT_BUF 5 /* This is even too much, the maximal * interactive mode write is 3 bytes * length for function keys... */ static int snpwrite(dev, uio, flag) dev_t dev; struct uio *uio; int flag; { struct snoop *snp; struct tty *tp; int error, i, len; unsigned char c[SNP_INPUT_BUF]; snp = dev->si_drv1; tp = snp->snp_tty; if (tp == NULL) return (EIO); if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc) goto tty_input; printf("snp%d: attempt to write to bad tty\n", snp->snp_unit); return (EIO); tty_input: if (!(tp->t_state & TS_ISOPEN)) return (EIO); while (uio->uio_resid > 0) { len = imin(uio->uio_resid, SNP_INPUT_BUF); if ((error = uiomove(c, len, uio)) != 0) return (error); for (i=0; i < len; i++) { if (ttyinput(c[i], tp)) return (EIO); } } return (0); } static int snpread(dev, uio, flag) dev_t dev; struct uio *uio; int flag; { struct snoop *snp; int error, len, n, nblen, s; caddr_t from; char *nbuf; snp = dev->si_drv1; KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen, ("snoop buffer error")); if (snp->snp_tty == NULL) return (EIO); snp->snp_flags &= ~SNOOP_RWAIT; do { if (snp->snp_len == 0) { if (flag & IO_NDELAY) return (EWOULDBLOCK); snp->snp_flags |= SNOOP_RWAIT; error = tsleep(snp, (PZERO + 1) | PCATCH, "snprd", 0); if (error != 0) return (error); } } while (snp->snp_len == 0); n = snp->snp_len; error = 0; while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) { len = min((unsigned)uio->uio_resid, snp->snp_len); from = (caddr_t)(snp->snp_buf + snp->snp_base); if (len == 0) break; error = uiomove(from, len, uio); snp->snp_base += len; snp->snp_len -= len; } if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) { snp->snp_flags &= ~SNOOP_OFLOW; } s = spltty(); nblen = snp->snp_blen; if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) { while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN) nblen = nblen / 2; if ((nbuf = malloc(nblen, M_SNP, M_NOWAIT)) != NULL) { bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); free(snp->snp_buf, M_SNP); snp->snp_buf = nbuf; snp->snp_blen = nblen; snp->snp_base = 0; } } splx(s); return (error); } static int snp_in(snp, buf, n) struct snoop *snp; char *buf; int n; { int s_free, s_tail; int s, len, nblen; caddr_t from, to; char *nbuf; KASSERT(n >= 0, ("negative snoop char count")); if (n == 0) return (0); if (snp->snp_flags & SNOOP_DOWN) { printf("snp%d: more data to down interface\n", snp->snp_unit); return (0); } if (snp->snp_flags & SNOOP_OFLOW) { printf("snp%d: buffer overflow\n", snp->snp_unit); /* * On overflow we just repeat the standart close * procedure...yes , this is waste of space but.. Then next * read from device will fail if one would recall he is * snooping and retry... */ return (snp_down(snp)); } s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base); s_free = snp->snp_blen - snp->snp_len; if (n > s_free) { s = spltty(); nblen = snp->snp_blen; while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) { nblen = snp->snp_blen * 2; s_free = nblen - (snp->snp_len + snp->snp_base); } if ((n <= s_free) && (nbuf = malloc(nblen, M_SNP, M_NOWAIT))) { bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); free(snp->snp_buf, M_SNP); snp->snp_buf = nbuf; snp->snp_blen = nblen; snp->snp_base = 0; } else { snp->snp_flags |= SNOOP_OFLOW; if (snp->snp_flags & SNOOP_RWAIT) { snp->snp_flags &= ~SNOOP_RWAIT; wakeup(snp); } splx(s); return (0); } splx(s); } if (n > s_tail) { from = (caddr_t)(snp->snp_buf + snp->snp_base); to = (caddr_t)(snp->snp_buf); len = snp->snp_len; bcopy(from, to, len); snp->snp_base = 0; } to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len); bcopy(buf, to, n); snp->snp_len += n; if (snp->snp_flags & SNOOP_RWAIT) { snp->snp_flags &= ~SNOOP_RWAIT; wakeup(snp); } selwakeuppri(&snp->snp_sel, PZERO + 1); return (n); } static int snpopen(dev, flag, mode, td) dev_t dev; int flag, mode; struct thread *td; { struct snoop *snp; if (dev->si_drv1 == NULL) { dev->si_flags &= ~SI_CHEAPCLONE; dev->si_drv1 = snp = malloc(sizeof(*snp), M_SNP, M_WAITOK | M_ZERO); snp->snp_unit = dev2unit(dev); } else return (EBUSY); /* * We intentionally do not OR flags with SNOOP_OPEN, but set them so * all previous settings (especially SNOOP_OFLOW) will be cleared. */ snp->snp_flags = SNOOP_OPEN; snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK); snp->snp_blen = SNOOP_MINLEN; snp->snp_base = 0; snp->snp_len = 0; /* * snp_tty == NULL is for inactive snoop devices. */ snp->snp_tty = NULL; snp->snp_target = NODEV; LIST_INSERT_HEAD(&snp_sclist, snp, snp_list); return (0); } static int snp_detach(snp) struct snoop *snp; { struct tty *tp; snp->snp_base = 0; snp->snp_len = 0; /* * If line disc. changed we do not touch this pointer, SLIP/PPP will * change it anyway. */ tp = snp->snp_tty; if (tp == NULL) goto detach_notty; if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && tp->t_line == snooplinedisc) { tp->t_sc = NULL; tp->t_state &= ~TS_SNOOP; tp->t_line = snp->snp_olddisc; } else printf("snp%d: bad attached tty data\n", snp->snp_unit); snp->snp_tty = NULL; snp->snp_target = NODEV; detach_notty: selwakeuppri(&snp->snp_sel, PZERO + 1); if ((snp->snp_flags & SNOOP_OPEN) == 0) free(snp, M_SNP); return (0); } static int snpclose(dev, flags, fmt, td) dev_t dev; int flags; int fmt; struct thread *td; { struct snoop *snp; snp = dev->si_drv1; snp->snp_blen = 0; LIST_REMOVE(snp, snp_list); free(snp->snp_buf, M_SNP); snp->snp_flags &= ~SNOOP_OPEN; dev->si_drv1 = NULL; destroy_dev(dev); return (snp_detach(snp)); } static int snp_down(snp) struct snoop *snp; { if (snp->snp_blen != SNOOP_MINLEN) { free(snp->snp_buf, M_SNP); snp->snp_buf = malloc(SNOOP_MINLEN, M_SNP, M_WAITOK); snp->snp_blen = SNOOP_MINLEN; } snp->snp_flags |= SNOOP_DOWN; return (snp_detach(snp)); } static int snpioctl(dev, cmd, data, flags, td) dev_t dev; u_long cmd; caddr_t data; int flags; struct thread *td; { struct snoop *snp; struct tty *tp, *tpo; dev_t tdev; int s; snp = dev->si_drv1; switch (cmd) { case SNPSTTY: tdev = udev2dev(*((udev_t *)data)); if (tdev == NODEV) return (snp_down(snp)); tp = snpdevtotty(tdev); if (!tp) return (EINVAL); if (tp->t_state & TS_SNOOP) return (EBUSY); s = spltty(); if (snp->snp_target == NODEV) { tpo = snp->snp_tty; if (tpo) tpo->t_state &= ~TS_SNOOP; } tp->t_sc = (caddr_t)snp; tp->t_state |= TS_SNOOP; snp->snp_olddisc = tp->t_line; tp->t_line = snooplinedisc; snp->snp_tty = tp; snp->snp_target = tdev; /* * Clean overflow and down flags - * we'll have a chance to get them in the future :))) */ snp->snp_flags &= ~SNOOP_OFLOW; snp->snp_flags &= ~SNOOP_DOWN; splx(s); break; case SNPGTTY: /* * We keep snp_target field specially to make * SNPGTTY happy, else we can't know what is device * major/minor for tty. */ *((udev_t *)data) = dev2udev(snp->snp_target); break; case FIONBIO: break; case FIOASYNC: if (*(int *)data) snp->snp_flags |= SNOOP_ASYNC; else snp->snp_flags &= ~SNOOP_ASYNC; break; case FIONREAD: s = spltty(); if (snp->snp_tty != NULL) *(int *)data = snp->snp_len; else if (snp->snp_flags & SNOOP_DOWN) { if (snp->snp_flags & SNOOP_OFLOW) *(int *)data = SNP_OFLOW; else *(int *)data = SNP_TTYCLOSE; } else { *(int *)data = SNP_DETACH; } splx(s); break; default: return (ENOTTY); } return (0); } static int snppoll(dev, events, td) dev_t dev; int events; struct thread *td; { struct snoop *snp; int revents; snp = dev->si_drv1; revents = 0; /* * If snoop is down, we don't want to poll() forever so we return 1. * Caller should see if we down via FIONREAD ioctl(). The last should * return -1 to indicate down state. */ if (events & (POLLIN | POLLRDNORM)) { if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0) revents |= events & (POLLIN | POLLRDNORM); else selrecord(td, &snp->snp_sel); } return (revents); } static void snp_clone(arg, name, namelen, dev) void *arg; char *name; int namelen; dev_t *dev; { int u, i; if (*dev != NODEV) return; if (dev_stdclone(name, NULL, "snp", &u) != 1) return; i = clone_create(&snpclones, &snp_cdevsw, &u, dev, 0); if (i) *dev = make_dev(&snp_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600, "snp%d", u); if (*dev != NULL) (*dev)->si_flags |= SI_CHEAPCLONE; } static int snp_modevent(mod, type, data) module_t mod; int type; void *data; { static eventhandler_tag eh_tag; switch (type) { case MOD_LOAD: /* XXX error checking. */ eh_tag = EVENTHANDLER_REGISTER(dev_clone, snp_clone, 0, 1000); snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc); break; case MOD_UNLOAD: if (!LIST_EMPTY(&snp_sclist)) return (EBUSY); EVENTHANDLER_DEREGISTER(dev_clone, eh_tag); clone_cleanup(&snpclones); ldisc_deregister(snooplinedisc); break; default: break; } return (0); } static moduledata_t snp_mod = { "snp", snp_modevent, NULL }; DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); Index: head/sys/net/if_tap.c =================================================================== --- head/sys/net/if_tap.c (revision 126187) +++ head/sys/net/if_tap.c (revision 126188) @@ -1,822 +1,821 @@ /* * Copyright (C) 1999-2000 by Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * BASED ON: * ------------------------------------------------------------------------- * * Copyright (c) 1988, Julian Onions * Nottingham University 1987. */ /* * $FreeBSD$ * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $ */ #include "opt_inet.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define CDEV_NAME "tap" #define TAPDEBUG if (tapdebug) printf #define TAP "tap" #define VMNET "vmnet" #define TAPMAXUNIT 0x7fff #define VMNET_DEV_MASK CLONE_FLAG0 /* module */ static int tapmodevent(module_t, int, void *); /* device */ static void tapclone(void *, char *, int, dev_t *); static void tapcreate(dev_t); /* network interface */ static void tapifstart(struct ifnet *); static int tapifioctl(struct ifnet *, u_long, caddr_t); static void tapifinit(void *); /* character device */ static d_open_t tapopen; static d_close_t tapclose; static d_read_t tapread; static d_write_t tapwrite; static d_ioctl_t tapioctl; static d_poll_t tappoll; static struct cdevsw tap_cdevsw = { .d_version = D_VERSION, - .d_flags = D_NEEDGIANT, + .d_flags = D_PSEUDO | D_NEEDGIANT, .d_open = tapopen, .d_close = tapclose, .d_read = tapread, .d_write = tapwrite, .d_ioctl = tapioctl, .d_poll = tappoll, .d_name = CDEV_NAME, - .d_flags = D_PSEUDO, }; static int tapdebug = 0; /* debug flag */ static SLIST_HEAD(, tap_softc) taphead; /* first device */ static struct clonedevs *tapclones; MALLOC_DECLARE(M_TAP); MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface"); SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, ""); DEV_MODULE(if_tap, tapmodevent, NULL); /* * tapmodevent * * module event handler */ static int tapmodevent(mod, type, data) module_t mod; int type; void *data; { static eventhandler_tag eh_tag = NULL; struct tap_softc *tp = NULL; struct ifnet *ifp = NULL; int s; switch (type) { case MOD_LOAD: /* intitialize device */ SLIST_INIT(&taphead); eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000); if (eh_tag == NULL) return (ENOMEM); return (0); case MOD_UNLOAD: SLIST_FOREACH(tp, &taphead, tap_next) if (tp->tap_flags & TAP_OPEN) return (EBUSY); EVENTHANDLER_DEREGISTER(dev_clone, eh_tag); while ((tp = SLIST_FIRST(&taphead)) != NULL) { SLIST_REMOVE_HEAD(&taphead, tap_next); ifp = &tp->tap_if; TAPDEBUG("detaching %s\n", ifp->if_xname); KASSERT(!(tp->tap_flags & TAP_OPEN), ("%s flags is out of sync", ifp->if_xname)); destroy_dev(tp->tap_dev); s = splimp(); ether_ifdetach(ifp); splx(s); free(tp, M_TAP); } clone_cleanup(&tapclones); break; default: return (EOPNOTSUPP); } return (0); } /* tapmodevent */ /* * DEVFS handler * * We need to support two kind of devices - tap and vmnet */ static void tapclone(arg, name, namelen, dev) void *arg; char *name; int namelen; dev_t *dev; { u_int extra; int i, unit; char *device_name = name; if (*dev != NODEV) return; device_name = TAP; extra = 0; if (strcmp(name, TAP) == 0) { unit = -1; } else if (strcmp(name, VMNET) == 0) { device_name = VMNET; extra = VMNET_DEV_MASK; unit = -1; } else if (dev_stdclone(name, NULL, device_name, &unit) != 1) { device_name = VMNET; extra = VMNET_DEV_MASK; if (dev_stdclone(name, NULL, device_name, &unit) != 1) return; } /* find any existing device, or allocate new unit number */ i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra); if (i) { *dev = make_dev(&tap_cdevsw, unit2minor(unit) | extra, UID_ROOT, GID_WHEEL, 0600, "%s%d", device_name, unit); if (*dev != NULL) (*dev)->si_flags |= SI_CHEAPCLONE; } } /* tapclone */ /* * tapcreate * * to create interface */ static void tapcreate(dev) dev_t dev; { struct ifnet *ifp = NULL; struct tap_softc *tp = NULL; unsigned short macaddr_hi; int unit, s; char *name = NULL; dev->si_flags &= ~SI_CHEAPCLONE; /* allocate driver storage and create device */ MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK | M_ZERO); SLIST_INSERT_HEAD(&taphead, tp, tap_next); unit = dev2unit(dev) & TAPMAXUNIT; /* select device: tap or vmnet */ if (minor(dev) & VMNET_DEV_MASK) { name = VMNET; tp->tap_flags |= TAP_VMNET; } else name = TAP; TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, minor(dev)); if (!(dev->si_flags & SI_NAMED)) dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600, "%s%d", name, unit); /* generate fake MAC address: 00 bd xx xx xx unit_no */ macaddr_hi = htons(0x00bd); bcopy(&macaddr_hi, &tp->arpcom.ac_enaddr[0], sizeof(short)); bcopy(&ticks, &tp->arpcom.ac_enaddr[2], sizeof(long)); tp->arpcom.ac_enaddr[5] = (u_char)unit; /* fill the rest and attach interface */ ifp = &tp->tap_if; ifp->if_softc = tp; if_initname(ifp, name, unit); ifp->if_init = tapifinit; ifp->if_start = tapifstart; ifp->if_ioctl = tapifioctl; ifp->if_mtu = ETHERMTU; ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST); ifp->if_snd.ifq_maxlen = ifqmaxlen; dev->si_drv1 = tp; tp->tap_dev = dev; s = splimp(); ether_ifattach(ifp, tp->arpcom.ac_enaddr); splx(s); tp->tap_flags |= TAP_INITED; TAPDEBUG("interface %s is created. minor = %#x\n", ifp->if_xname, minor(dev)); } /* tapcreate */ /* * tapopen * * to open tunnel. must be superuser */ static int tapopen(dev, flag, mode, td) dev_t dev; int flag; int mode; struct thread *td; { struct tap_softc *tp = NULL; int unit, error; if ((error = suser(td)) != 0) return (error); unit = dev2unit(dev) & TAPMAXUNIT; tp = dev->si_drv1; if (tp == NULL) { tapcreate(dev); tp = dev->si_drv1; } KASSERT(!(tp->tap_flags & TAP_OPEN), ("%s flags is out of sync", tp->tap_if.if_xname)); bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr)); tp->tap_pid = td->td_proc->p_pid; tp->tap_flags |= TAP_OPEN; TAPDEBUG("%s is open. minor = %#x\n", tp->tap_if.if_xname, minor(dev)); return (0); } /* tapopen */ /* * tapclose * * close the device - mark i/f down & delete routing info */ static int tapclose(dev, foo, bar, td) dev_t dev; int foo; int bar; struct thread *td; { struct tap_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tap_if; int s; /* junk all pending output */ IF_DRAIN(&ifp->if_snd); /* * do not bring the interface down, and do not anything with * interface, if we are in VMnet mode. just close the device. */ if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) { s = splimp(); if_down(ifp); if (ifp->if_flags & IFF_RUNNING) { /* find internet addresses and delete routes */ struct ifaddr *ifa = NULL; TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { if (ifa->ifa_addr->sa_family == AF_INET) { rtinit(ifa, (int)RTM_DELETE, 0); /* remove address from interface */ bzero(ifa->ifa_addr, sizeof(*(ifa->ifa_addr))); bzero(ifa->ifa_dstaddr, sizeof(*(ifa->ifa_dstaddr))); bzero(ifa->ifa_netmask, sizeof(*(ifa->ifa_netmask))); } } ifp->if_flags &= ~IFF_RUNNING; } splx(s); } funsetown(&tp->tap_sigio); selwakeuppri(&tp->tap_rsel, PZERO+1); tp->tap_flags &= ~TAP_OPEN; tp->tap_pid = 0; TAPDEBUG("%s is closed. minor = %#x\n", ifp->if_xname, minor(dev)); return (0); } /* tapclose */ /* * tapifinit * * network interface initialization function */ static void tapifinit(xtp) void *xtp; { struct tap_softc *tp = (struct tap_softc *)xtp; struct ifnet *ifp = &tp->tap_if; TAPDEBUG("initializing %s\n", ifp->if_xname); ifp->if_flags |= IFF_RUNNING; ifp->if_flags &= ~IFF_OACTIVE; /* attempt to start output */ tapifstart(ifp); } /* tapifinit */ /* * tapifioctl * * Process an ioctl request on network interface */ static int tapifioctl(ifp, cmd, data) struct ifnet *ifp; u_long cmd; caddr_t data; { struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc); struct ifstat *ifs = NULL; int s, dummy; switch (cmd) { case SIOCSIFFLAGS: /* XXX -- just like vmnet does */ case SIOCADDMULTI: case SIOCDELMULTI: break; case SIOCGIFSTATUS: s = splimp(); ifs = (struct ifstat *)data; dummy = strlen(ifs->ascii); if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii)) snprintf(ifs->ascii + dummy, sizeof(ifs->ascii) - dummy, "\tOpened by PID %d\n", tp->tap_pid); splx(s); break; default: s = splimp(); dummy = ether_ioctl(ifp, cmd, data); splx(s); return (dummy); } return (0); } /* tapifioctl */ /* * tapifstart * * queue packets from higher level ready to put out */ static void tapifstart(ifp) struct ifnet *ifp; { struct tap_softc *tp = ifp->if_softc; int s; TAPDEBUG("%s starting\n", ifp->if_xname); /* * do not junk pending output if we are in VMnet mode. * XXX: can this do any harm because of queue overflow? */ if (((tp->tap_flags & TAP_VMNET) == 0) && ((tp->tap_flags & TAP_READY) != TAP_READY)) { struct mbuf *m = NULL; TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname, tp->tap_flags); s = splimp(); do { IF_DEQUEUE(&ifp->if_snd, m); if (m != NULL) m_freem(m); ifp->if_oerrors ++; } while (m != NULL); splx(s); return; } s = splimp(); ifp->if_flags |= IFF_OACTIVE; if (ifp->if_snd.ifq_len != 0) { if (tp->tap_flags & TAP_RWAIT) { tp->tap_flags &= ~TAP_RWAIT; wakeup(tp); } if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) pgsigio(&tp->tap_sigio, SIGIO, 0); selwakeuppri(&tp->tap_rsel, PZERO+1); ifp->if_opackets ++; /* obytes are counted in ether_output */ } ifp->if_flags &= ~IFF_OACTIVE; splx(s); } /* tapifstart */ /* * tapioctl * * the cdevsw interface is now pretty minimal */ static int tapioctl(dev, cmd, data, flag, td) dev_t dev; u_long cmd; caddr_t data; int flag; struct thread *td; { struct tap_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tap_if; struct tapinfo *tapp = NULL; int s; int f; switch (cmd) { case TAPSIFINFO: s = splimp(); tapp = (struct tapinfo *)data; ifp->if_mtu = tapp->mtu; ifp->if_type = tapp->type; ifp->if_baudrate = tapp->baudrate; splx(s); break; case TAPGIFINFO: tapp = (struct tapinfo *)data; tapp->mtu = ifp->if_mtu; tapp->type = ifp->if_type; tapp->baudrate = ifp->if_baudrate; break; case TAPSDEBUG: tapdebug = *(int *)data; break; case TAPGDEBUG: *(int *)data = tapdebug; break; case FIONBIO: break; case FIOASYNC: s = splimp(); if (*(int *)data) tp->tap_flags |= TAP_ASYNC; else tp->tap_flags &= ~TAP_ASYNC; splx(s); break; case FIONREAD: s = splimp(); if (ifp->if_snd.ifq_head) { struct mbuf *mb = ifp->if_snd.ifq_head; for(*(int *)data = 0;mb != NULL;mb = mb->m_next) *(int *)data += mb->m_len; } else *(int *)data = 0; splx(s); break; case FIOSETOWN: return (fsetown(*(int *)data, &tp->tap_sigio)); case FIOGETOWN: *(int *)data = fgetown(&tp->tap_sigio); return (0); /* this is deprecated, FIOSETOWN should be used instead */ case TIOCSPGRP: return (fsetown(-(*(int *)data), &tp->tap_sigio)); /* this is deprecated, FIOGETOWN should be used instead */ case TIOCGPGRP: *(int *)data = -fgetown(&tp->tap_sigio); return (0); /* VMware/VMnet port ioctl's */ case SIOCGIFFLAGS: /* get ifnet flags */ bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags)); break; case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */ f = *(int *)data; f &= 0x0fff; f &= ~IFF_CANTCHANGE; f |= IFF_UP; s = splimp(); ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE); splx(s); break; case OSIOCGIFADDR: /* get MAC address of the remote side */ case SIOCGIFADDR: bcopy(tp->ether_addr, data, sizeof(tp->ether_addr)); break; case SIOCSIFADDR: /* set MAC address of the remote side */ bcopy(data, tp->ether_addr, sizeof(tp->ether_addr)); break; default: return (ENOTTY); } return (0); } /* tapioctl */ /* * tapread * * the cdevsw read interface - reads a packet at a time, or at * least as much of a packet as can be read */ static int tapread(dev, uio, flag) dev_t dev; struct uio *uio; int flag; { struct tap_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tap_if; struct mbuf *m = NULL; int error = 0, len, s; TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, minor(dev)); if ((tp->tap_flags & TAP_READY) != TAP_READY) { TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n", ifp->if_xname, minor(dev), tp->tap_flags); return (EHOSTDOWN); } tp->tap_flags &= ~TAP_RWAIT; /* sleep until we get a packet */ do { s = splimp(); IF_DEQUEUE(&ifp->if_snd, m); splx(s); if (m == NULL) { if (flag & IO_NDELAY) return (EWOULDBLOCK); tp->tap_flags |= TAP_RWAIT; error = tsleep(tp,PCATCH|(PZERO+1),"taprd",0); if (error) return (error); } } while (m == NULL); /* feed packet to bpf */ BPF_MTAP(ifp, m); /* xfer packet to user space */ while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) { len = min(uio->uio_resid, m->m_len); if (len == 0) break; error = uiomove(mtod(m, void *), len, uio); m = m_free(m); } if (m != NULL) { TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname, minor(dev)); m_freem(m); } return (error); } /* tapread */ /* * tapwrite * * the cdevsw write interface - an atomic write is a packet - or else! */ static int tapwrite(dev, uio, flag) dev_t dev; struct uio *uio; int flag; { struct tap_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tap_if; struct mbuf *top = NULL, **mp = NULL, *m = NULL; int error = 0, tlen, mlen; TAPDEBUG("%s writting, minor = %#x\n", ifp->if_xname, minor(dev)); if (uio->uio_resid == 0) return (0); if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) { TAPDEBUG("%s invalid packet len = %d, minor = %#x\n", ifp->if_xname, uio->uio_resid, minor(dev)); return (EIO); } tlen = uio->uio_resid; /* get a header mbuf */ MGETHDR(m, M_DONTWAIT, MT_DATA); if (m == NULL) return (ENOBUFS); mlen = MHLEN; top = 0; mp = ⊤ while ((error == 0) && (uio->uio_resid > 0)) { m->m_len = min(mlen, uio->uio_resid); error = uiomove(mtod(m, void *), m->m_len, uio); *mp = m; mp = &m->m_next; if (uio->uio_resid > 0) { MGET(m, M_DONTWAIT, MT_DATA); if (m == NULL) { error = ENOBUFS; break; } mlen = MLEN; } } if (error) { ifp->if_ierrors ++; if (top) m_freem(top); return (error); } top->m_pkthdr.len = tlen; top->m_pkthdr.rcvif = ifp; /* Pass packet up to parent. */ (*ifp->if_input)(ifp, top); ifp->if_ipackets ++; /* ibytes are counted in parent */ return (0); } /* tapwrite */ /* * tappoll * * the poll interface, this is only useful on reads * really. the write detect always returns true, write never blocks * anyway, it either accepts the packet or drops it */ static int tappoll(dev, events, td) dev_t dev; int events; struct thread *td; { struct tap_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tap_if; int s, revents = 0; TAPDEBUG("%s polling, minor = %#x\n", ifp->if_xname, minor(dev)); s = splimp(); if (events & (POLLIN | POLLRDNORM)) { if (ifp->if_snd.ifq_len > 0) { TAPDEBUG("%s have data in queue. len = %d, " \ "minor = %#x\n", ifp->if_xname, ifp->if_snd.ifq_len, minor(dev)); revents |= (events & (POLLIN | POLLRDNORM)); } else { TAPDEBUG("%s waiting for data, minor = %#x\n", ifp->if_xname, minor(dev)); selrecord(td, &tp->tap_rsel); } } if (events & (POLLOUT | POLLWRNORM)) revents |= (events & (POLLOUT | POLLWRNORM)); splx(s); return (revents); } /* tappoll */ Index: head/sys/net/if_tun.c =================================================================== --- head/sys/net/if_tun.c (revision 126187) +++ head/sys/net/if_tun.c (revision 126188) @@ -1,771 +1,770 @@ /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */ /* * Copyright (c) 1988, Julian Onions * Nottingham University 1987. * * This source may be freely distributed, however I would be interested * in any changes that are made. * * This driver takes packets off the IP i/f and hands them up to a * user process to have its wicked way with. This driver has it's * roots in a similar driver written by Phil Cockcroft (formerly) at * UCL. This driver is based much more on read/write/poll mode of * operation though. * * $FreeBSD$ */ #include "opt_atalk.h" #include "opt_inet.h" #include "opt_inet6.h" #include "opt_ipx.h" #include "opt_mac.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef INET #include #endif #include #include #include struct tun_softc { TAILQ_ENTRY(tun_softc) tun_list; int tun_unit; dev_t tun_dev; u_short tun_flags; /* misc flags */ #define TUN_OPEN 0x0001 #define TUN_INITED 0x0002 #define TUN_RCOLL 0x0004 #define TUN_IASET 0x0008 #define TUN_DSTADDR 0x0010 #define TUN_LMODE 0x0020 #define TUN_RWAIT 0x0040 #define TUN_ASYNC 0x0080 #define TUN_IFHEAD 0x0100 #define TUN_READY (TUN_OPEN | TUN_INITED) struct proc *tun_proc; /* Owning process */ struct ifnet tun_if; /* the interface */ struct sigio *tun_sigio; /* information for async I/O */ struct selinfo tun_rsel; /* read select */ }; #define TUNDEBUG if (tundebug) if_printf #define TUNNAME "tun" static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface"); static int tundebug = 0; static struct clonedevs *tunclones; static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead); SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, ""); static void tunclone(void *arg, char *name, int namelen, dev_t *dev); static void tuncreate(dev_t dev); static int tunifioctl(struct ifnet *, u_long, caddr_t); static int tuninit(struct ifnet *); static int tunmodevent(module_t, int, void *); static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *, struct rtentry *rt); static void tunstart(struct ifnet *); static d_open_t tunopen; static d_close_t tunclose; static d_read_t tunread; static d_write_t tunwrite; static d_ioctl_t tunioctl; static d_poll_t tunpoll; static struct cdevsw tun_cdevsw = { .d_version = D_VERSION, - .d_flags = D_NEEDGIANT, + .d_flags = D_PSEUDO | D_NEEDGIANT, .d_open = tunopen, .d_close = tunclose, .d_read = tunread, .d_write = tunwrite, .d_ioctl = tunioctl, .d_poll = tunpoll, .d_name = TUNNAME, - .d_flags = D_PSEUDO, }; static void tunclone(void *arg, char *name, int namelen, dev_t *dev) { int u, i; if (*dev != NODEV) return; if (strcmp(name, TUNNAME) == 0) { u = -1; } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1) return; /* Don't recognise the name */ if (u != -1 && u > IF_MAXUNIT) return; /* Unit number too high */ /* find any existing device, or allocate new unit number */ i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0); if (i) { /* No preexisting dev_t, create one */ *dev = make_dev(&tun_cdevsw, unit2minor(u), UID_UUCP, GID_DIALER, 0600, "tun%d", u); if (*dev != NULL) (*dev)->si_flags |= SI_CHEAPCLONE; } } static int tunmodevent(module_t mod, int type, void *data) { static eventhandler_tag tag; struct tun_softc *tp; dev_t dev; switch (type) { case MOD_LOAD: tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000); if (tag == NULL) return (ENOMEM); break; case MOD_UNLOAD: EVENTHANDLER_DEREGISTER(dev_clone, tag); while (!TAILQ_EMPTY(&tunhead)) { tp = TAILQ_FIRST(&tunhead); KASSERT((tp->tun_flags & TUN_OPEN) == 0, ("tununits is out of sync - unit %d", tp->tun_if.if_dunit)); TAILQ_REMOVE(&tunhead, tp, tun_list); dev = tp->tun_dev; bpfdetach(&tp->tun_if); if_detach(&tp->tun_if); destroy_dev(dev); free(tp, M_TUN); } clone_cleanup(&tunclones); break; } return 0; } static moduledata_t tun_mod = { "if_tun", tunmodevent, 0 }; DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); static void tunstart(struct ifnet *ifp) { struct tun_softc *tp = ifp->if_softc; if (tp->tun_flags & TUN_RWAIT) { tp->tun_flags &= ~TUN_RWAIT; wakeup(tp); } if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) pgsigio(&tp->tun_sigio, SIGIO, 0); selwakeuppri(&tp->tun_rsel, PZERO + 1); } static void tuncreate(dev_t dev) { struct tun_softc *sc; struct ifnet *ifp; dev->si_flags &= ~SI_CHEAPCLONE; MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO); sc->tun_flags = TUN_INITED; sc->tun_dev = dev; TAILQ_INSERT_TAIL(&tunhead, sc, tun_list); ifp = &sc->tun_if; if_initname(ifp, TUNNAME, dev2unit(dev)); ifp->if_mtu = TUNMTU; ifp->if_ioctl = tunifioctl; ifp->if_output = tunoutput; ifp->if_start = tunstart; ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; ifp->if_type = IFT_PPP; ifp->if_snd.ifq_maxlen = ifqmaxlen; ifp->if_softc = sc; if_attach(ifp); bpfattach(ifp, DLT_NULL, sizeof(u_int)); dev->si_drv1 = sc; } static int tunopen(dev_t dev, int flag, int mode, struct thread *td) { struct ifnet *ifp; struct tun_softc *tp; tp = dev->si_drv1; if (!tp) { tuncreate(dev); tp = dev->si_drv1; } if (tp->tun_proc != NULL && tp->tun_proc != td->td_proc) return (EBUSY); tp->tun_proc = td->td_proc; tp->tun_flags |= TUN_OPEN; ifp = &tp->tun_if; TUNDEBUG(ifp, "open\n"); return (0); } /* * tunclose - close the device - mark i/f down & delete * routing info */ static int tunclose(dev_t dev, int foo, int bar, struct thread *td) { struct tun_softc *tp; struct ifnet *ifp; int s; tp = dev->si_drv1; ifp = &tp->tun_if; tp->tun_flags &= ~TUN_OPEN; tp->tun_proc = NULL; /* * junk all pending output */ IF_DRAIN(&ifp->if_snd); if (ifp->if_flags & IFF_UP) { s = splimp(); if_down(ifp); splx(s); } if (ifp->if_flags & IFF_RUNNING) { struct ifaddr *ifa; s = splimp(); /* find internet addresses and delete routes */ TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) if (ifa->ifa_addr->sa_family == AF_INET) rtinit(ifa, (int)RTM_DELETE, tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); ifp->if_flags &= ~IFF_RUNNING; splx(s); } funsetown(&tp->tun_sigio); selwakeuppri(&tp->tun_rsel, PZERO + 1); TUNDEBUG (ifp, "closed\n"); return (0); } static int tuninit(struct ifnet *ifp) { struct tun_softc *tp = ifp->if_softc; struct ifaddr *ifa; int error = 0; TUNDEBUG(ifp, "tuninit\n"); ifp->if_flags |= IFF_UP | IFF_RUNNING; getmicrotime(&ifp->if_lastchange); for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; ifa = TAILQ_NEXT(ifa, ifa_link)) { if (ifa->ifa_addr == NULL) error = EFAULT; /* XXX: Should maybe return straight off? */ else { #ifdef INET if (ifa->ifa_addr->sa_family == AF_INET) { struct sockaddr_in *si; si = (struct sockaddr_in *)ifa->ifa_addr; if (si->sin_addr.s_addr) tp->tun_flags |= TUN_IASET; si = (struct sockaddr_in *)ifa->ifa_dstaddr; if (si && si->sin_addr.s_addr) tp->tun_flags |= TUN_DSTADDR; } #endif } } return (error); } /* * Process an ioctl request. */ static int tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data) { struct ifreq *ifr = (struct ifreq *)data; struct tun_softc *tp = ifp->if_softc; struct ifstat *ifs; int error = 0, s; s = splimp(); switch(cmd) { case SIOCGIFSTATUS: ifs = (struct ifstat *)data; if (tp->tun_proc) sprintf(ifs->ascii + strlen(ifs->ascii), "\tOpened by PID %d\n", tp->tun_proc->p_pid); break; case SIOCSIFADDR: error = tuninit(ifp); TUNDEBUG(ifp, "address set, error=%d\n", error); break; case SIOCSIFDSTADDR: error = tuninit(ifp); TUNDEBUG(ifp, "destination address set, error=%d\n", error); break; case SIOCSIFMTU: ifp->if_mtu = ifr->ifr_mtu; TUNDEBUG(ifp, "mtu set\n"); break; case SIOCSIFFLAGS: case SIOCADDMULTI: case SIOCDELMULTI: break; default: error = EINVAL; } splx(s); return (error); } /* * tunoutput - queue packets from higher level ready to put out. */ static int tunoutput( struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst, struct rtentry *rt) { struct tun_softc *tp = ifp->if_softc; #ifdef MAC int error; #endif TUNDEBUG (ifp, "tunoutput\n"); #ifdef MAC error = mac_check_ifnet_transmit(ifp, m0); if (error) { m_freem(m0); return (error); } #endif if ((tp->tun_flags & TUN_READY) != TUN_READY) { TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); m_freem (m0); return (EHOSTDOWN); } if ((ifp->if_flags & IFF_UP) != IFF_UP) { m_freem (m0); return (EHOSTDOWN); } /* BPF write needs to be handled specially */ if (dst->sa_family == AF_UNSPEC) { dst->sa_family = *(mtod(m0, int *)); m0->m_len -= sizeof(int); m0->m_pkthdr.len -= sizeof(int); m0->m_data += sizeof(int); } if (ifp->if_bpf) { uint32_t af = dst->sa_family; bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0); } /* prepend sockaddr? this may abort if the mbuf allocation fails */ if (tp->tun_flags & TUN_LMODE) { /* allocate space for sockaddr */ M_PREPEND(m0, dst->sa_len, M_DONTWAIT); /* if allocation failed drop packet */ if (m0 == NULL) { ifp->if_iqdrops++; ifp->if_oerrors++; return (ENOBUFS); } else { bcopy(dst, m0->m_data, dst->sa_len); } } if (tp->tun_flags & TUN_IFHEAD) { /* Prepend the address family */ M_PREPEND(m0, 4, M_DONTWAIT); /* if allocation failed drop packet */ if (m0 == NULL) { ifp->if_iqdrops++; ifp->if_oerrors++; return (ENOBUFS); } else *(u_int32_t *)m0->m_data = htonl(dst->sa_family); } else { #ifdef INET if (dst->sa_family != AF_INET) #endif { m_freem(m0); return (EAFNOSUPPORT); } } if (! IF_HANDOFF(&ifp->if_snd, m0, ifp)) { ifp->if_collisions++; return (ENOBUFS); } ifp->if_opackets++; return (0); } /* * the cdevsw interface is now pretty minimal. */ static int tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) { int s; int error; struct tun_softc *tp = dev->si_drv1; struct tuninfo *tunp; switch (cmd) { case TUNSIFINFO: tunp = (struct tuninfo *)data; if (tunp->mtu < IF_MINMTU) return (EINVAL); if (tp->tun_if.if_mtu != tunp->mtu && (error = suser(td)) != 0) return (error); tp->tun_if.if_mtu = tunp->mtu; tp->tun_if.if_type = tunp->type; tp->tun_if.if_baudrate = tunp->baudrate; break; case TUNGIFINFO: tunp = (struct tuninfo *)data; tunp->mtu = tp->tun_if.if_mtu; tunp->type = tp->tun_if.if_type; tunp->baudrate = tp->tun_if.if_baudrate; break; case TUNSDEBUG: tundebug = *(int *)data; break; case TUNGDEBUG: *(int *)data = tundebug; break; case TUNSLMODE: if (*(int *)data) { tp->tun_flags |= TUN_LMODE; tp->tun_flags &= ~TUN_IFHEAD; } else tp->tun_flags &= ~TUN_LMODE; break; case TUNSIFHEAD: if (*(int *)data) { tp->tun_flags |= TUN_IFHEAD; tp->tun_flags &= ~TUN_LMODE; } else tp->tun_flags &= ~TUN_IFHEAD; break; case TUNGIFHEAD: *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0; break; case TUNSIFMODE: /* deny this if UP */ if (tp->tun_if.if_flags & IFF_UP) return(EBUSY); switch (*(int *)data & ~IFF_MULTICAST) { case IFF_POINTOPOINT: case IFF_BROADCAST: tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); tp->tun_if.if_flags |= *(int *)data; break; default: return(EINVAL); } break; case TUNSIFPID: tp->tun_proc = curthread->td_proc; break; case FIONBIO: break; case FIOASYNC: if (*(int *)data) tp->tun_flags |= TUN_ASYNC; else tp->tun_flags &= ~TUN_ASYNC; break; case FIONREAD: s = splimp(); if (tp->tun_if.if_snd.ifq_head) { struct mbuf *mb = tp->tun_if.if_snd.ifq_head; for( *(int *)data = 0; mb != 0; mb = mb->m_next) *(int *)data += mb->m_len; } else *(int *)data = 0; splx(s); break; case FIOSETOWN: return (fsetown(*(int *)data, &tp->tun_sigio)); case FIOGETOWN: *(int *)data = fgetown(&tp->tun_sigio); return (0); /* This is deprecated, FIOSETOWN should be used instead. */ case TIOCSPGRP: return (fsetown(-(*(int *)data), &tp->tun_sigio)); /* This is deprecated, FIOGETOWN should be used instead. */ case TIOCGPGRP: *(int *)data = -fgetown(&tp->tun_sigio); return (0); default: return (ENOTTY); } return (0); } /* * The cdevsw read interface - reads a packet at a time, or at * least as much of a packet as can be read. */ static int tunread(dev_t dev, struct uio *uio, int flag) { struct tun_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tun_if; struct mbuf *m; int error=0, len, s; TUNDEBUG (ifp, "read\n"); if ((tp->tun_flags & TUN_READY) != TUN_READY) { TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); return (EHOSTDOWN); } tp->tun_flags &= ~TUN_RWAIT; s = splimp(); do { IF_DEQUEUE(&ifp->if_snd, m); if (m == NULL) { if (flag & IO_NDELAY) { splx(s); return (EWOULDBLOCK); } tp->tun_flags |= TUN_RWAIT; if((error = tsleep(tp, PCATCH | (PZERO + 1), "tunread", 0)) != 0) { splx(s); return (error); } } } while (m == NULL); splx(s); while (m && uio->uio_resid > 0 && error == 0) { len = min(uio->uio_resid, m->m_len); if (len != 0) error = uiomove(mtod(m, void *), len, uio); m = m_free(m); } if (m) { TUNDEBUG(ifp, "Dropping mbuf\n"); m_freem(m); } return (error); } /* * the cdevsw write interface - an atomic write is a packet - or else! */ static int tunwrite(dev_t dev, struct uio *uio, int flag) { struct tun_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tun_if; struct mbuf *top, **mp, *m; int error=0, tlen, mlen; uint32_t family; int isr; TUNDEBUG(ifp, "tunwrite\n"); if ((ifp->if_flags & IFF_UP) != IFF_UP) /* ignore silently */ return (0); if (uio->uio_resid == 0) return (0); if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) { TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid); return (EIO); } tlen = uio->uio_resid; /* get a header mbuf */ MGETHDR(m, M_DONTWAIT, MT_DATA); if (m == NULL) return (ENOBUFS); mlen = MHLEN; top = 0; mp = ⊤ while (error == 0 && uio->uio_resid > 0) { m->m_len = min(mlen, uio->uio_resid); error = uiomove(mtod(m, void *), m->m_len, uio); *mp = m; mp = &m->m_next; if (uio->uio_resid > 0) { MGET (m, M_DONTWAIT, MT_DATA); if (m == 0) { error = ENOBUFS; break; } mlen = MLEN; } } if (error) { if (top) m_freem (top); ifp->if_ierrors++; return (error); } top->m_pkthdr.len = tlen; top->m_pkthdr.rcvif = ifp; #ifdef MAC mac_create_mbuf_from_ifnet(ifp, top); #endif if (tp->tun_flags & TUN_IFHEAD) { if (top->m_len < sizeof(family) && (top = m_pullup(top, sizeof(family))) == NULL) return (ENOBUFS); family = ntohl(*mtod(top, u_int32_t *)); m_adj(top, sizeof(family)); } else family = AF_INET; BPF_MTAP2(ifp, &family, sizeof(family), top); switch (family) { #ifdef INET case AF_INET: isr = NETISR_IP; break; #endif #ifdef INET6 case AF_INET6: isr = NETISR_IPV6; break; #endif #ifdef IPX case AF_IPX: isr = NETISR_IPX; break; #endif #ifdef NETATALK case AF_APPLETALK: isr = NETISR_ATALK2; break; #endif default: m_freem(m); return (EAFNOSUPPORT); } /* First chunk of an mbuf contains good junk */ if (harvest.point_to_point) random_harvest(m, 16, 3, 0, RANDOM_NET); ifp->if_ibytes += top->m_pkthdr.len; ifp->if_ipackets++; netisr_dispatch(isr, top); return (0); } /* * tunpoll - the poll interface, this is only useful on reads * really. The write detect always returns true, write never blocks * anyway, it either accepts the packet or drops it. */ static int tunpoll(dev_t dev, int events, struct thread *td) { int s; struct tun_softc *tp = dev->si_drv1; struct ifnet *ifp = &tp->tun_if; int revents = 0; s = splimp(); TUNDEBUG(ifp, "tunpoll\n"); if (events & (POLLIN | POLLRDNORM)) { if (ifp->if_snd.ifq_len > 0) { TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len); revents |= events & (POLLIN | POLLRDNORM); } else { TUNDEBUG(ifp, "tunpoll waiting\n"); selrecord(td, &tp->tun_rsel); } } if (events & (POLLOUT | POLLWRNORM)) revents |= events & (POLLOUT | POLLWRNORM); splx(s); return (revents); }