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