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