Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.37.4.1
      1  1.37.4.1   thorpej /*	$NetBSD: if_tun.c,v 1.37.4.1 1999/07/01 23:45:20 thorpej Exp $	*/
      2      1.14       cgd 
      3       1.1       cgd /*
      4       1.8   deraadt  * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
      5       1.8   deraadt  * Nottingham University 1987.
      6       1.1       cgd  *
      7       1.1       cgd  * This source may be freely distributed, however I would be interested
      8       1.1       cgd  * in any changes that are made.
      9       1.6   deraadt  *
     10       1.1       cgd  * This driver takes packets off the IP i/f and hands them up to a
     11      1.21    scottr  * user process to have its wicked way with. This driver has its
     12       1.1       cgd  * roots in a similar driver written by Phil Cockcroft (formerly) at
     13      1.27   mycroft  * UCL. This driver is based much more on read/write/poll mode of
     14       1.1       cgd  * operation though.
     15       1.1       cgd  */
     16       1.1       cgd 
     17       1.8   deraadt #include "tun.h"
     18       1.8   deraadt #if NTUN > 0
     19      1.33  jonathan 
     20      1.33  jonathan #include "opt_inet.h"
     21      1.34  jonathan #include "opt_ns.h"
     22       1.8   deraadt 
     23       1.6   deraadt #include <sys/param.h>
     24       1.8   deraadt #include <sys/proc.h>
     25       1.6   deraadt #include <sys/systm.h>
     26       1.6   deraadt #include <sys/mbuf.h>
     27       1.6   deraadt #include <sys/buf.h>
     28       1.6   deraadt #include <sys/protosw.h>
     29       1.6   deraadt #include <sys/socket.h>
     30       1.6   deraadt #include <sys/ioctl.h>
     31       1.6   deraadt #include <sys/errno.h>
     32       1.6   deraadt #include <sys/syslog.h>
     33       1.6   deraadt #include <sys/select.h>
     34      1.27   mycroft #include <sys/poll.h>
     35       1.8   deraadt #include <sys/file.h>
     36      1.22  christos #include <sys/signalvar.h>
     37      1.23  christos #include <sys/conf.h>
     38       1.9   deraadt 
     39       1.9   deraadt #include <machine/cpu.h>
     40       1.6   deraadt 
     41       1.6   deraadt #include <net/if.h>
     42      1.30        is #include <net/if_ether.h>
     43       1.6   deraadt #include <net/netisr.h>
     44       1.6   deraadt #include <net/route.h>
     45       1.1       cgd 
     46      1.30        is 
     47       1.1       cgd #ifdef INET
     48       1.6   deraadt #include <netinet/in.h>
     49       1.6   deraadt #include <netinet/in_systm.h>
     50       1.6   deraadt #include <netinet/in_var.h>
     51       1.6   deraadt #include <netinet/ip.h>
     52      1.30        is #include <netinet/if_inarp.h>
     53       1.1       cgd #endif
     54       1.1       cgd 
     55       1.1       cgd #ifdef NS
     56       1.6   deraadt #include <netns/ns.h>
     57       1.6   deraadt #include <netns/ns_if.h>
     58       1.1       cgd #endif
     59       1.1       cgd 
     60       1.8   deraadt #include "bpfilter.h"
     61       1.8   deraadt #if NBPFILTER > 0
     62       1.8   deraadt #include <sys/time.h>
     63       1.8   deraadt #include <net/bpf.h>
     64       1.8   deraadt #endif
     65       1.8   deraadt 
     66       1.8   deraadt #include <net/if_tun.h>
     67       1.8   deraadt 
     68      1.29  christos #define TUNDEBUG	if (tundebug) printf
     69       1.6   deraadt int	tundebug = 0;
     70       1.1       cgd 
     71       1.6   deraadt struct tun_softc tunctl[NTUN];
     72       1.6   deraadt extern int ifqmaxlen;
     73      1.22  christos void	tunattach __P((int));
     74       1.6   deraadt 
     75      1.22  christos int	tun_ioctl __P((struct ifnet *, u_long, caddr_t));
     76      1.22  christos int	tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     77      1.22  christos 		       struct rtentry *rt));
     78       1.6   deraadt 
     79      1.26        pk static void tuninit __P((struct tun_softc *));
     80       1.6   deraadt 
     81       1.8   deraadt void
     82       1.8   deraadt tunattach(unused)
     83       1.8   deraadt 	int unused;
     84       1.8   deraadt {
     85       1.8   deraadt 	register int i;
     86       1.8   deraadt 	struct ifnet *ifp;
     87       1.8   deraadt 
     88       1.8   deraadt 	for (i = 0; i < NTUN; i++) {
     89       1.8   deraadt 		tunctl[i].tun_flags = TUN_INITED;
     90       1.8   deraadt 
     91       1.8   deraadt 		ifp = &tunctl[i].tun_if;
     92      1.29  christos 		sprintf(ifp->if_xname, "tun%d", i);
     93      1.24   thorpej 		ifp->if_softc = &tunctl[i];
     94       1.8   deraadt 		ifp->if_mtu = TUNMTU;
     95      1.22  christos 		ifp->if_ioctl = tun_ioctl;
     96      1.22  christos 		ifp->if_output = tun_output;
     97       1.8   deraadt 		ifp->if_flags = IFF_POINTOPOINT;
     98       1.8   deraadt 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
     99       1.8   deraadt 		ifp->if_collisions = 0;
    100       1.8   deraadt 		ifp->if_ierrors = 0;
    101       1.8   deraadt 		ifp->if_oerrors = 0;
    102       1.8   deraadt 		ifp->if_ipackets = 0;
    103       1.8   deraadt 		ifp->if_opackets = 0;
    104       1.8   deraadt 		if_attach(ifp);
    105       1.8   deraadt #if NBPFILTER > 0
    106      1.16       cgd 		bpfattach(&tunctl[i].tun_bpf, ifp, DLT_NULL, sizeof(u_int32_t));
    107       1.8   deraadt #endif
    108       1.8   deraadt 	}
    109       1.8   deraadt }
    110       1.8   deraadt 
    111       1.6   deraadt /*
    112       1.6   deraadt  * tunnel open - must be superuser & the device must be
    113       1.6   deraadt  * configured in
    114       1.6   deraadt  */
    115       1.6   deraadt int
    116       1.6   deraadt tunopen(dev, flag, mode, p)
    117       1.6   deraadt 	dev_t	dev;
    118       1.6   deraadt 	int	flag, mode;
    119       1.6   deraadt 	struct proc *p;
    120       1.6   deraadt {
    121       1.6   deraadt 	struct ifnet	*ifp;
    122       1.8   deraadt 	struct tun_softc *tp;
    123       1.6   deraadt 	register int	unit, error;
    124       1.1       cgd 
    125      1.22  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    126       1.5   deraadt 		return (error);
    127       1.5   deraadt 
    128       1.6   deraadt 	if ((unit = minor(dev)) >= NTUN)
    129       1.6   deraadt 		return (ENXIO);
    130       1.6   deraadt 	tp = &tunctl[unit];
    131       1.6   deraadt 	if (tp->tun_flags & TUN_OPEN)
    132       1.6   deraadt 		return ENXIO;
    133       1.6   deraadt 	ifp = &tp->tun_if;
    134       1.6   deraadt 	tp->tun_flags |= TUN_OPEN;
    135      1.24   thorpej 	TUNDEBUG("%s: open\n", ifp->if_xname);
    136       1.6   deraadt 	return (0);
    137       1.1       cgd }
    138       1.1       cgd 
    139       1.6   deraadt /*
    140       1.6   deraadt  * tunclose - close the device - mark i/f down & delete
    141       1.6   deraadt  * routing info
    142       1.6   deraadt  */
    143       1.6   deraadt int
    144      1.22  christos tunclose(dev, flag, mode, p)
    145       1.6   deraadt 	dev_t	dev;
    146       1.6   deraadt 	int	flag;
    147      1.22  christos 	int	mode;
    148      1.22  christos 	struct proc *p;
    149       1.6   deraadt {
    150       1.8   deraadt 	register int	unit = minor(dev), s;
    151       1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    152       1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    153       1.6   deraadt 	struct mbuf	*m;
    154       1.6   deraadt 
    155      1.10    andrew 	tp->tun_flags &= ~TUN_OPEN;
    156       1.6   deraadt 
    157       1.6   deraadt 	/*
    158       1.6   deraadt 	 * junk all pending output
    159       1.6   deraadt 	 */
    160       1.6   deraadt 	do {
    161       1.6   deraadt 		s = splimp();
    162       1.6   deraadt 		IF_DEQUEUE(&ifp->if_snd, m);
    163       1.6   deraadt 		splx(s);
    164       1.6   deraadt 		if (m)
    165       1.6   deraadt 			m_freem(m);
    166       1.6   deraadt 	} while (m);
    167       1.6   deraadt 
    168       1.6   deraadt 	if (ifp->if_flags & IFF_UP) {
    169       1.6   deraadt 		s = splimp();
    170       1.6   deraadt 		if_down(ifp);
    171       1.8   deraadt 		if (ifp->if_flags & IFF_RUNNING) {
    172      1.17   mycroft 			/* find internet addresses and delete routes */
    173      1.17   mycroft 			register struct ifaddr *ifa;
    174      1.17   mycroft 			for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    175      1.18   mycroft 			    ifa = ifa->ifa_list.tqe_next) {
    176  1.37.4.1   thorpej #ifdef INET
    177      1.18   mycroft 				if (ifa->ifa_addr->sa_family == AF_INET) {
    178      1.18   mycroft 					rtinit(ifa, (int)RTM_DELETE,
    179      1.26        pk 					       tp->tun_flags & TUN_DSTADDR
    180      1.26        pk 							? RTF_HOST
    181      1.26        pk 							: 0);
    182      1.18   mycroft 				}
    183  1.37.4.1   thorpej #endif
    184      1.11   deraadt 			}
    185       1.8   deraadt 		}
    186       1.6   deraadt 		splx(s);
    187       1.6   deraadt 	}
    188       1.6   deraadt 	tp->tun_pgrp = 0;
    189       1.7   deraadt 	selwakeup(&tp->tun_rsel);
    190       1.6   deraadt 
    191      1.24   thorpej 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
    192       1.6   deraadt 	return (0);
    193       1.1       cgd }
    194       1.1       cgd 
    195      1.26        pk static void
    196      1.24   thorpej tuninit(tp)
    197      1.24   thorpej 	struct tun_softc *tp;
    198       1.6   deraadt {
    199       1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    200       1.8   deraadt 	register struct ifaddr *ifa;
    201       1.8   deraadt 
    202      1.24   thorpej 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
    203       1.6   deraadt 
    204       1.6   deraadt 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    205       1.8   deraadt 
    206      1.26        pk 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
    207      1.17   mycroft 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    208      1.26        pk 	     ifa = ifa->ifa_list.tqe_next) {
    209  1.37.4.1   thorpej #ifdef INET
    210      1.11   deraadt 		if (ifa->ifa_addr->sa_family == AF_INET) {
    211      1.17   mycroft 			struct sockaddr_in *sin;
    212      1.11   deraadt 
    213      1.17   mycroft 			sin = satosin(ifa->ifa_addr);
    214      1.17   mycroft 			if (sin && sin->sin_addr.s_addr)
    215      1.17   mycroft 				tp->tun_flags |= TUN_IASET;
    216      1.17   mycroft 
    217      1.26        pk 			if (ifp->if_flags & IFF_POINTOPOINT) {
    218      1.26        pk 				sin = satosin(ifa->ifa_dstaddr);
    219      1.26        pk 				if (sin && sin->sin_addr.s_addr)
    220      1.26        pk 					tp->tun_flags |= TUN_DSTADDR;
    221      1.26        pk 			}
    222      1.11   deraadt 		}
    223  1.37.4.1   thorpej #endif
    224      1.18   mycroft 	}
    225       1.8   deraadt 
    226      1.26        pk 	return;
    227       1.1       cgd }
    228       1.1       cgd 
    229       1.1       cgd /*
    230       1.1       cgd  * Process an ioctl request.
    231       1.1       cgd  */
    232       1.1       cgd int
    233      1.22  christos tun_ioctl(ifp, cmd, data)
    234       1.6   deraadt 	struct ifnet *ifp;
    235      1.25   mycroft 	u_long cmd;
    236       1.6   deraadt 	caddr_t	data;
    237       1.6   deraadt {
    238       1.6   deraadt 	int		error = 0, s;
    239       1.6   deraadt 
    240       1.6   deraadt 	s = splimp();
    241       1.6   deraadt 	switch(cmd) {
    242       1.6   deraadt 	case SIOCSIFADDR:
    243      1.24   thorpej 		tuninit((struct tun_softc *)(ifp->if_softc));
    244      1.24   thorpej 		TUNDEBUG("%s: address set\n", ifp->if_xname);
    245       1.6   deraadt 		break;
    246       1.6   deraadt 	case SIOCSIFDSTADDR:
    247      1.24   thorpej 		tuninit((struct tun_softc *)(ifp->if_softc));
    248      1.24   thorpej 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
    249       1.6   deraadt 		break;
    250      1.26        pk 	case SIOCSIFBRDADDR:
    251      1.26        pk 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
    252      1.26        pk 		break;
    253      1.31      matt 	case SIOCSIFMTU: {
    254      1.31      matt 		struct ifreq *ifr = (struct ifreq *) data;
    255      1.31      matt 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
    256      1.31      matt 		    error = EINVAL;
    257      1.31      matt 		    break;
    258      1.31      matt 		}
    259      1.31      matt 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
    260      1.31      matt 		ifp->if_mtu = ifr->ifr_mtu;
    261      1.32      matt 		break;
    262      1.32      matt 	}
    263      1.32      matt 	case SIOCADDMULTI:
    264      1.32      matt 	case SIOCDELMULTI: {
    265      1.32      matt 		struct ifreq *ifr = (struct ifreq *) data;
    266      1.32      matt 		if (ifr == 0) {
    267      1.32      matt 	        	error = EAFNOSUPPORT;           /* XXX */
    268      1.32      matt 			break;
    269      1.32      matt 		}
    270      1.32      matt 		switch (ifr->ifr_addr.sa_family) {
    271      1.32      matt 
    272      1.32      matt #ifdef INET
    273      1.32      matt 		case AF_INET:
    274      1.32      matt 			break;
    275      1.32      matt #endif
    276      1.32      matt 
    277      1.32      matt 		default:
    278      1.32      matt 			error = EAFNOSUPPORT;
    279      1.32      matt 			break;
    280      1.32      matt 		}
    281      1.31      matt 		break;
    282      1.31      matt 	}
    283  1.37.4.1   thorpej 	case SIOCSIFFLAGS:
    284  1.37.4.1   thorpej 		break;
    285       1.6   deraadt 	default:
    286       1.6   deraadt 		error = EINVAL;
    287       1.6   deraadt 	}
    288       1.6   deraadt 	splx(s);
    289       1.6   deraadt 	return (error);
    290       1.1       cgd }
    291       1.1       cgd 
    292       1.1       cgd /*
    293      1.22  christos  * tun_output - queue packets from higher level ready to put out.
    294       1.1       cgd  */
    295       1.6   deraadt int
    296      1.22  christos tun_output(ifp, m0, dst, rt)
    297       1.6   deraadt 	struct ifnet   *ifp;
    298       1.6   deraadt 	struct mbuf    *m0;
    299       1.6   deraadt 	struct sockaddr *dst;
    300      1.12   deraadt 	struct rtentry *rt;
    301       1.6   deraadt {
    302      1.24   thorpej 	struct tun_softc *tp = ifp->if_softc;
    303       1.6   deraadt 	struct proc	*p;
    304  1.37.4.1   thorpej #ifdef INET
    305       1.6   deraadt 	int		s;
    306  1.37.4.1   thorpej #endif
    307       1.6   deraadt 
    308      1.24   thorpej 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
    309       1.1       cgd 
    310       1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    311      1.24   thorpej 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
    312      1.24   thorpej 			  tp->tun_flags);
    313       1.8   deraadt 		m_freem (m0);
    314      1.26        pk 		return (EHOSTDOWN);
    315       1.8   deraadt 	}
    316       1.8   deraadt 
    317       1.8   deraadt #if NBPFILTER > 0
    318       1.8   deraadt 	if (tp->tun_bpf) {
    319       1.8   deraadt 		/*
    320       1.8   deraadt 		 * We need to prepend the address family as
    321       1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    322       1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    323       1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    324       1.8   deraadt 		 * try to free it or keep a pointer to it).
    325       1.8   deraadt 		 */
    326       1.8   deraadt 		struct mbuf m;
    327      1.16       cgd 		u_int32_t af = dst->sa_family;
    328       1.8   deraadt 
    329       1.8   deraadt 		m.m_next = m0;
    330      1.16       cgd 		m.m_len = sizeof(af);
    331       1.8   deraadt 		m.m_data = (char *)&af;
    332       1.8   deraadt 
    333       1.8   deraadt 		bpf_mtap(tp->tun_bpf, &m);
    334       1.8   deraadt 	}
    335       1.8   deraadt #endif
    336       1.8   deraadt 
    337       1.6   deraadt 	switch(dst->sa_family) {
    338       1.1       cgd #ifdef INET
    339       1.6   deraadt 	case AF_INET:
    340      1.26        pk 		if (tp->tun_flags & TUN_PREPADDR) {
    341      1.26        pk 			/* Simple link-layer header */
    342      1.26        pk 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
    343      1.26        pk 			if (m0 == NULL) {
    344      1.26        pk 				IF_DROP(&ifp->if_snd);
    345      1.26        pk 				return (ENOBUFS);
    346      1.26        pk 			}
    347      1.26        pk 			bcopy(dst, mtod(m0, char *), dst->sa_len);
    348      1.26        pk 		}
    349      1.36  sommerfe 		/* FALLTHROUGH */
    350      1.36  sommerfe 	case AF_UNSPEC:
    351       1.6   deraadt 		s = splimp();
    352       1.6   deraadt 		if (IF_QFULL(&ifp->if_snd)) {
    353       1.6   deraadt 			IF_DROP(&ifp->if_snd);
    354       1.6   deraadt 			m_freem(m0);
    355       1.6   deraadt 			splx(s);
    356       1.6   deraadt 			ifp->if_collisions++;
    357       1.6   deraadt 			return (ENOBUFS);
    358       1.6   deraadt 		}
    359       1.6   deraadt 		IF_ENQUEUE(&ifp->if_snd, m0);
    360       1.6   deraadt 		splx(s);
    361       1.6   deraadt 		ifp->if_opackets++;
    362       1.6   deraadt 		break;
    363       1.1       cgd #endif
    364       1.6   deraadt 	default:
    365       1.6   deraadt 		m_freem(m0);
    366      1.26        pk 		return (EAFNOSUPPORT);
    367       1.6   deraadt 	}
    368       1.6   deraadt 
    369       1.6   deraadt 	if (tp->tun_flags & TUN_RWAIT) {
    370       1.6   deraadt 		tp->tun_flags &= ~TUN_RWAIT;
    371       1.6   deraadt 		wakeup((caddr_t)tp);
    372       1.6   deraadt 	}
    373       1.6   deraadt 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    374       1.6   deraadt 		if (tp->tun_pgrp > 0)
    375       1.6   deraadt 			gsignal(tp->tun_pgrp, SIGIO);
    376      1.22  christos 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    377       1.6   deraadt 			psignal(p, SIGIO);
    378       1.6   deraadt 	}
    379       1.6   deraadt 	selwakeup(&tp->tun_rsel);
    380      1.26        pk 	return (0);
    381       1.1       cgd }
    382       1.1       cgd 
    383       1.1       cgd /*
    384       1.1       cgd  * the cdevsw interface is now pretty minimal.
    385       1.1       cgd  */
    386       1.6   deraadt int
    387      1.20   mycroft tunioctl(dev, cmd, data, flag, p)
    388       1.6   deraadt 	dev_t		dev;
    389      1.15       cgd 	u_long		cmd;
    390       1.6   deraadt 	caddr_t		data;
    391       1.6   deraadt 	int		flag;
    392      1.12   deraadt 	struct proc	*p;
    393       1.6   deraadt {
    394       1.6   deraadt 	int		unit = minor(dev), s;
    395       1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    396       1.6   deraadt 
    397       1.6   deraadt 	switch (cmd) {
    398       1.6   deraadt 	case TUNSDEBUG:
    399       1.6   deraadt 		tundebug = *(int *)data;
    400       1.6   deraadt 		break;
    401      1.26        pk 
    402       1.6   deraadt 	case TUNGDEBUG:
    403       1.6   deraadt 		*(int *)data = tundebug;
    404       1.6   deraadt 		break;
    405      1.26        pk 
    406      1.26        pk 	case TUNSIFMODE:
    407      1.31      matt 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
    408      1.26        pk 		case IFF_POINTOPOINT:
    409      1.26        pk 		case IFF_BROADCAST:
    410      1.26        pk 			s = splimp();
    411      1.26        pk 			if (tp->tun_if.if_flags & IFF_UP) {
    412      1.26        pk 				splx(s);
    413      1.26        pk 				return (EBUSY);
    414      1.26        pk 			}
    415      1.26        pk 			tp->tun_if.if_flags &=
    416      1.31      matt 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
    417      1.26        pk 			tp->tun_if.if_flags |= *(int *)data;
    418      1.26        pk 			splx(s);
    419      1.26        pk 			break;
    420      1.26        pk 		default:
    421      1.26        pk 			return (EINVAL);
    422      1.26        pk 			break;
    423      1.26        pk 		}
    424      1.26        pk 		break;
    425      1.26        pk 
    426      1.26        pk 	case TUNSLMODE:
    427      1.26        pk 		if (*(int *)data)
    428      1.26        pk 			tp->tun_flags |= TUN_PREPADDR;
    429      1.26        pk 		else
    430      1.26        pk 			tp->tun_flags &= ~TUN_PREPADDR;
    431      1.26        pk 		break;
    432      1.26        pk 
    433       1.6   deraadt 	case FIONBIO:
    434       1.6   deraadt 		if (*(int *)data)
    435       1.6   deraadt 			tp->tun_flags |= TUN_NBIO;
    436       1.6   deraadt 		else
    437       1.6   deraadt 			tp->tun_flags &= ~TUN_NBIO;
    438       1.6   deraadt 		break;
    439      1.26        pk 
    440       1.6   deraadt 	case FIOASYNC:
    441       1.6   deraadt 		if (*(int *)data)
    442       1.6   deraadt 			tp->tun_flags |= TUN_ASYNC;
    443       1.6   deraadt 		else
    444       1.6   deraadt 			tp->tun_flags &= ~TUN_ASYNC;
    445       1.6   deraadt 		break;
    446      1.26        pk 
    447       1.6   deraadt 	case FIONREAD:
    448       1.6   deraadt 		s = splimp();
    449       1.6   deraadt 		if (tp->tun_if.if_snd.ifq_head)
    450      1.19        pk 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    451       1.6   deraadt 		else
    452       1.6   deraadt 			*(int *)data = 0;
    453       1.6   deraadt 		splx(s);
    454       1.6   deraadt 		break;
    455      1.26        pk 
    456       1.6   deraadt 	case TIOCSPGRP:
    457       1.6   deraadt 		tp->tun_pgrp = *(int *)data;
    458       1.6   deraadt 		break;
    459      1.26        pk 
    460       1.6   deraadt 	case TIOCGPGRP:
    461       1.6   deraadt 		*(int *)data = tp->tun_pgrp;
    462       1.6   deraadt 		break;
    463      1.26        pk 
    464       1.6   deraadt 	default:
    465       1.6   deraadt 		return (ENOTTY);
    466       1.6   deraadt 	}
    467       1.6   deraadt 	return (0);
    468       1.1       cgd }
    469       1.1       cgd 
    470       1.1       cgd /*
    471       1.6   deraadt  * The cdevsw read interface - reads a packet at a time, or at
    472       1.6   deraadt  * least as much of a packet as can be read.
    473       1.1       cgd  */
    474       1.6   deraadt int
    475      1.22  christos tunread(dev, uio, ioflag)
    476       1.6   deraadt 	dev_t		dev;
    477       1.6   deraadt 	struct uio	*uio;
    478      1.22  christos 	int		ioflag;
    479       1.6   deraadt {
    480       1.8   deraadt 	int		unit = minor(dev);
    481       1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    482       1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    483       1.6   deraadt 	struct mbuf	*m, *m0;
    484       1.6   deraadt 	int		error=0, len, s;
    485       1.6   deraadt 
    486      1.24   thorpej 	TUNDEBUG ("%s: read\n", ifp->if_xname);
    487       1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    488      1.26        pk 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
    489       1.8   deraadt 		return EHOSTDOWN;
    490       1.8   deraadt 	}
    491       1.8   deraadt 
    492       1.6   deraadt 	tp->tun_flags &= ~TUN_RWAIT;
    493       1.6   deraadt 
    494       1.6   deraadt 	s = splimp();
    495       1.6   deraadt 	do {
    496       1.6   deraadt 		IF_DEQUEUE(&ifp->if_snd, m0);
    497       1.6   deraadt 		if (m0 == 0) {
    498       1.6   deraadt 			if (tp->tun_flags & TUN_NBIO) {
    499       1.6   deraadt 				splx(s);
    500      1.26        pk 				return (EWOULDBLOCK);
    501       1.6   deraadt 			}
    502       1.6   deraadt 			tp->tun_flags |= TUN_RWAIT;
    503      1.26        pk 			if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
    504      1.26        pk 				splx(s);
    505      1.26        pk 				return (EINTR);
    506      1.26        pk 			}
    507       1.6   deraadt 		}
    508       1.6   deraadt 	} while (m0 == 0);
    509       1.6   deraadt 	splx(s);
    510       1.6   deraadt 
    511       1.6   deraadt 	while (m0 && uio->uio_resid > 0 && error == 0) {
    512      1.13   deraadt 		len = min(uio->uio_resid, m0->m_len);
    513       1.6   deraadt 		if (len == 0)
    514       1.6   deraadt 			break;
    515       1.8   deraadt 		error = uiomove(mtod(m0, caddr_t), len, uio);
    516       1.6   deraadt 		MFREE(m0, m);
    517       1.6   deraadt 		m0 = m;
    518       1.6   deraadt 	}
    519       1.6   deraadt 
    520       1.6   deraadt 	if (m0) {
    521       1.6   deraadt 		TUNDEBUG("Dropping mbuf\n");
    522       1.6   deraadt 		m_freem(m0);
    523       1.6   deraadt 	}
    524      1.19        pk 	if (error)
    525      1.19        pk 		ifp->if_ierrors++;
    526      1.26        pk 	return (error);
    527       1.1       cgd }
    528       1.1       cgd 
    529       1.1       cgd /*
    530       1.1       cgd  * the cdevsw write interface - an atomic write is a packet - or else!
    531       1.1       cgd  */
    532       1.6   deraadt int
    533      1.22  christos tunwrite(dev, uio, ioflag)
    534       1.8   deraadt 	dev_t		dev;
    535       1.6   deraadt 	struct uio	*uio;
    536      1.22  christos 	int		ioflag;
    537       1.6   deraadt {
    538       1.6   deraadt 	int		unit = minor (dev);
    539      1.26        pk 	struct tun_softc *tp = &tunctl[unit];
    540      1.26        pk 	struct ifnet	*ifp = &tp->tun_if;
    541       1.6   deraadt 	struct mbuf	*top, **mp, *m;
    542      1.26        pk 	struct ifqueue	*ifq;
    543      1.26        pk 	struct sockaddr	dst;
    544      1.26        pk 	int		isr, error=0, s, tlen, mlen;
    545       1.6   deraadt 
    546      1.24   thorpej 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
    547       1.6   deraadt 
    548      1.26        pk 	if (tp->tun_flags & TUN_PREPADDR) {
    549      1.26        pk 		if (uio->uio_resid < sizeof(dst))
    550      1.26        pk 			return (EIO);
    551      1.26        pk 		error = uiomove((caddr_t)&dst, sizeof(dst), uio);
    552      1.26        pk 		if (dst.sa_len > sizeof(dst)) {
    553      1.26        pk 			/* Duh.. */
    554      1.26        pk 			char discard;
    555      1.26        pk 			int n = dst.sa_len - sizeof(dst);
    556      1.26        pk 			while (n--)
    557      1.26        pk 				if ((error = uiomove(&discard, 1, uio)) != 0)
    558      1.26        pk 					return (error);
    559      1.26        pk 		}
    560      1.26        pk 	} else {
    561      1.26        pk #ifdef INET
    562      1.26        pk 		dst.sa_family = AF_INET;
    563      1.26        pk #endif
    564      1.26        pk 	}
    565      1.26        pk 
    566       1.6   deraadt 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
    567      1.37    mjacob 		TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
    568      1.37    mjacob 		    (unsigned long)uio->uio_resid);
    569      1.26        pk 		return (EIO);
    570       1.6   deraadt 	}
    571      1.26        pk 
    572      1.26        pk 	switch (dst.sa_family) {
    573      1.26        pk #ifdef INET
    574      1.26        pk 	case AF_INET:
    575      1.26        pk 		ifq = &ipintrq;
    576      1.26        pk 		isr = NETISR_IP;
    577      1.26        pk 		break;
    578      1.26        pk #endif
    579      1.26        pk 	default:
    580      1.26        pk 		return (EAFNOSUPPORT);
    581      1.26        pk 	}
    582      1.26        pk 
    583       1.8   deraadt 	tlen = uio->uio_resid;
    584       1.8   deraadt 
    585       1.8   deraadt 	/* get a header mbuf */
    586       1.8   deraadt 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    587       1.8   deraadt 	if (m == NULL)
    588      1.26        pk 		return (ENOBUFS);
    589       1.8   deraadt 	mlen = MHLEN;
    590       1.8   deraadt 
    591       1.6   deraadt 	top = 0;
    592       1.6   deraadt 	mp = &top;
    593       1.6   deraadt 	while (error == 0 && uio->uio_resid > 0) {
    594      1.13   deraadt 		m->m_len = min(mlen, uio->uio_resid);
    595       1.8   deraadt 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    596       1.6   deraadt 		*mp = m;
    597       1.6   deraadt 		mp = &m->m_next;
    598       1.8   deraadt 		if (uio->uio_resid > 0) {
    599       1.8   deraadt 			MGET (m, M_DONTWAIT, MT_DATA);
    600       1.8   deraadt 			if (m == 0) {
    601       1.8   deraadt 				error = ENOBUFS;
    602       1.8   deraadt 				break;
    603       1.8   deraadt 			}
    604       1.8   deraadt 			mlen = MLEN;
    605       1.8   deraadt 		}
    606       1.6   deraadt 	}
    607       1.6   deraadt 	if (error) {
    608       1.6   deraadt 		if (top)
    609       1.8   deraadt 			m_freem (top);
    610      1.19        pk 		ifp->if_ierrors++;
    611      1.26        pk 		return (error);
    612       1.6   deraadt 	}
    613       1.6   deraadt 
    614       1.8   deraadt 	top->m_pkthdr.len = tlen;
    615       1.8   deraadt 	top->m_pkthdr.rcvif = ifp;
    616       1.8   deraadt 
    617       1.8   deraadt #if NBPFILTER > 0
    618      1.26        pk 	if (tp->tun_bpf) {
    619       1.8   deraadt 		/*
    620       1.8   deraadt 		 * We need to prepend the address family as
    621       1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    622       1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    623       1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    624       1.8   deraadt 		 * try to free it or keep a pointer to it).
    625       1.8   deraadt 		 */
    626       1.8   deraadt 		struct mbuf m;
    627      1.16       cgd 		u_int32_t af = AF_INET;
    628       1.8   deraadt 
    629       1.8   deraadt 		m.m_next = top;
    630      1.16       cgd 		m.m_len = sizeof(af);
    631       1.8   deraadt 		m.m_data = (char *)&af;
    632       1.8   deraadt 
    633      1.26        pk 		bpf_mtap(tp->tun_bpf, &m);
    634       1.6   deraadt 	}
    635       1.8   deraadt #endif
    636       1.6   deraadt 
    637       1.6   deraadt 	s = splimp();
    638      1.26        pk 	if (IF_QFULL(ifq)) {
    639      1.26        pk 		IF_DROP(ifq);
    640       1.6   deraadt 		splx(s);
    641       1.6   deraadt 		ifp->if_collisions++;
    642       1.6   deraadt 		m_freem(top);
    643      1.26        pk 		return (ENOBUFS);
    644       1.6   deraadt 	}
    645      1.26        pk 	IF_ENQUEUE(ifq, top);
    646       1.6   deraadt 	splx(s);
    647       1.6   deraadt 	ifp->if_ipackets++;
    648      1.26        pk 	schednetisr(isr);
    649      1.26        pk 	return (error);
    650       1.1       cgd }
    651       1.1       cgd 
    652       1.1       cgd /*
    653      1.27   mycroft  * tunpoll - the poll interface, this is only useful on reads
    654       1.6   deraadt  * really. The write detect always returns true, write never blocks
    655       1.6   deraadt  * anyway, it either accepts the packet or drops it.
    656       1.6   deraadt  */
    657       1.6   deraadt int
    658      1.27   mycroft tunpoll(dev, events, p)
    659       1.6   deraadt 	dev_t		dev;
    660      1.27   mycroft 	int		events;
    661      1.22  christos 	struct proc	*p;
    662       1.6   deraadt {
    663       1.6   deraadt 	int		unit = minor(dev), s;
    664       1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    665       1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    666      1.27   mycroft 	int		revents = 0;
    667       1.6   deraadt 
    668       1.6   deraadt 	s = splimp();
    669      1.27   mycroft 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
    670       1.6   deraadt 
    671      1.35     veego 	if (events & (POLLIN | POLLRDNORM)) {
    672       1.6   deraadt 		if (ifp->if_snd.ifq_len > 0) {
    673      1.27   mycroft 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
    674      1.24   thorpej 			    ifp->if_snd.ifq_len);
    675      1.27   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    676      1.27   mycroft 		} else {
    677      1.27   mycroft 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
    678      1.27   mycroft 			selrecord(p, &tp->tun_rsel);
    679       1.6   deraadt 		}
    680      1.35     veego 	}
    681      1.27   mycroft 
    682      1.27   mycroft 	if (events & (POLLOUT | POLLWRNORM))
    683      1.27   mycroft 		revents |= events & (POLLOUT | POLLWRNORM);
    684      1.27   mycroft 
    685       1.6   deraadt 	splx(s);
    686      1.27   mycroft 	return (revents);
    687       1.1       cgd }
    688       1.8   deraadt 
    689       1.8   deraadt #endif  /* NTUN */
    690