Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.22
      1  1.22  christos /*	$NetBSD: if_tun.c,v 1.22 1996/02/13 22:00:26 christos 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.9   deraadt 
     34   1.9   deraadt #include <machine/cpu.h>
     35   1.6   deraadt 
     36   1.6   deraadt #include <net/if.h>
     37   1.6   deraadt #include <net/netisr.h>
     38   1.6   deraadt #include <net/route.h>
     39   1.1       cgd 
     40   1.1       cgd #ifdef INET
     41   1.6   deraadt #include <netinet/in.h>
     42   1.6   deraadt #include <netinet/in_systm.h>
     43   1.6   deraadt #include <netinet/in_var.h>
     44   1.6   deraadt #include <netinet/ip.h>
     45   1.6   deraadt #include <netinet/if_ether.h>
     46   1.1       cgd #endif
     47   1.1       cgd 
     48   1.1       cgd #ifdef NS
     49   1.6   deraadt #include <netns/ns.h>
     50   1.6   deraadt #include <netns/ns_if.h>
     51   1.1       cgd #endif
     52   1.1       cgd 
     53   1.8   deraadt #include "bpfilter.h"
     54   1.8   deraadt #if NBPFILTER > 0
     55   1.8   deraadt #include <sys/time.h>
     56   1.8   deraadt #include <net/bpf.h>
     57   1.8   deraadt #endif
     58   1.8   deraadt 
     59   1.8   deraadt #include <net/if_tun.h>
     60  1.22  christos #include <net/net_conf.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.6   deraadt static int tuninit __P((int));
     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.8   deraadt 		ifp->if_unit = i;
     87   1.8   deraadt 		ifp->if_name = "tun";
     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.6   deraadt 	TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
    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.6   deraadt 	TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
    182   1.6   deraadt 	return (0);
    183   1.1       cgd }
    184   1.1       cgd 
    185   1.6   deraadt static int
    186   1.6   deraadt tuninit(unit)
    187   1.6   deraadt 	int	unit;
    188   1.6   deraadt {
    189   1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    190   1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    191   1.8   deraadt 	register struct ifaddr *ifa;
    192   1.8   deraadt 
    193   1.8   deraadt 	TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
    194   1.6   deraadt 
    195   1.6   deraadt 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    196   1.8   deraadt 
    197  1.17   mycroft 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    198  1.18   mycroft 	    ifa = ifa->ifa_list.tqe_next) {
    199  1.11   deraadt 		if (ifa->ifa_addr->sa_family == AF_INET) {
    200  1.17   mycroft 			struct sockaddr_in *sin;
    201  1.11   deraadt 
    202  1.17   mycroft 			sin = satosin(ifa->ifa_addr);
    203  1.17   mycroft 			if (sin && sin->sin_addr.s_addr)
    204  1.17   mycroft 				tp->tun_flags |= TUN_IASET;
    205  1.17   mycroft 
    206  1.17   mycroft 			sin = satosin(ifa->ifa_dstaddr);
    207  1.17   mycroft 			if (sin && sin->sin_addr.s_addr)
    208  1.17   mycroft 				tp->tun_flags |= TUN_DSTADDR;
    209  1.11   deraadt 		}
    210  1.18   mycroft 	}
    211   1.8   deraadt 
    212   1.6   deraadt 	return 0;
    213   1.1       cgd }
    214   1.1       cgd 
    215   1.1       cgd /*
    216   1.1       cgd  * Process an ioctl request.
    217   1.1       cgd  */
    218   1.1       cgd int
    219  1.22  christos tun_ioctl(ifp, cmd, data)
    220   1.6   deraadt 	struct ifnet *ifp;
    221  1.15       cgd 	u_long	cmd;
    222   1.6   deraadt 	caddr_t	data;
    223   1.6   deraadt {
    224   1.6   deraadt 	int		error = 0, s;
    225   1.6   deraadt 
    226   1.6   deraadt 	s = splimp();
    227   1.6   deraadt 	switch(cmd) {
    228   1.6   deraadt 	case SIOCSIFADDR:
    229   1.6   deraadt 		tuninit(ifp->if_unit);
    230  1.11   deraadt 		TUNDEBUG("%s%d: address set\n",
    231  1.11   deraadt 			 ifp->if_name, ifp->if_unit);
    232   1.6   deraadt 		break;
    233   1.6   deraadt 	case SIOCSIFDSTADDR:
    234  1.11   deraadt 		tuninit(ifp->if_unit);
    235  1.11   deraadt 		TUNDEBUG("%s%d: destination address set\n",
    236  1.11   deraadt 			 ifp->if_name, ifp->if_unit);
    237   1.6   deraadt 		break;
    238   1.6   deraadt 	default:
    239   1.6   deraadt 		error = EINVAL;
    240   1.6   deraadt 	}
    241   1.6   deraadt 	splx(s);
    242   1.6   deraadt 	return (error);
    243   1.1       cgd }
    244   1.1       cgd 
    245   1.1       cgd /*
    246  1.22  christos  * tun_output - queue packets from higher level ready to put out.
    247   1.1       cgd  */
    248   1.6   deraadt int
    249  1.22  christos tun_output(ifp, m0, dst, rt)
    250   1.6   deraadt 	struct ifnet   *ifp;
    251   1.6   deraadt 	struct mbuf    *m0;
    252   1.6   deraadt 	struct sockaddr *dst;
    253  1.12   deraadt 	struct rtentry *rt;
    254   1.6   deraadt {
    255   1.8   deraadt 	struct tun_softc *tp = &tunctl[ifp->if_unit];
    256   1.6   deraadt 	struct proc	*p;
    257   1.6   deraadt 	int		s;
    258   1.6   deraadt 
    259  1.22  christos 	TUNDEBUG ("%s%d: tun_output\n", ifp->if_name, ifp->if_unit);
    260   1.1       cgd 
    261   1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    262   1.8   deraadt 		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
    263   1.8   deraadt 			  ifp->if_unit, tp->tun_flags);
    264   1.8   deraadt 		m_freem (m0);
    265   1.8   deraadt 		return EHOSTDOWN;
    266   1.8   deraadt 	}
    267   1.8   deraadt 
    268   1.8   deraadt #if NBPFILTER > 0
    269   1.8   deraadt 	if (tp->tun_bpf) {
    270   1.8   deraadt 		/*
    271   1.8   deraadt 		 * We need to prepend the address family as
    272   1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    273   1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    274   1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    275   1.8   deraadt 		 * try to free it or keep a pointer to it).
    276   1.8   deraadt 		 */
    277   1.8   deraadt 		struct mbuf m;
    278  1.16       cgd 		u_int32_t af = dst->sa_family;
    279   1.8   deraadt 
    280   1.8   deraadt 		m.m_next = m0;
    281  1.16       cgd 		m.m_len = sizeof(af);
    282   1.8   deraadt 		m.m_data = (char *)&af;
    283   1.8   deraadt 
    284   1.8   deraadt 		bpf_mtap(tp->tun_bpf, &m);
    285   1.8   deraadt 	}
    286   1.8   deraadt #endif
    287   1.8   deraadt 
    288   1.6   deraadt 	switch(dst->sa_family) {
    289   1.1       cgd #ifdef INET
    290   1.6   deraadt 	case AF_INET:
    291   1.6   deraadt 		s = splimp();
    292   1.6   deraadt 		if (IF_QFULL(&ifp->if_snd)) {
    293   1.6   deraadt 			IF_DROP(&ifp->if_snd);
    294   1.6   deraadt 			m_freem(m0);
    295   1.6   deraadt 			splx(s);
    296   1.6   deraadt 			ifp->if_collisions++;
    297   1.6   deraadt 			return (ENOBUFS);
    298   1.6   deraadt 		}
    299   1.6   deraadt 		IF_ENQUEUE(&ifp->if_snd, m0);
    300   1.6   deraadt 		splx(s);
    301   1.6   deraadt 		ifp->if_opackets++;
    302   1.6   deraadt 		break;
    303   1.1       cgd #endif
    304   1.6   deraadt 	default:
    305   1.6   deraadt 		m_freem(m0);
    306   1.6   deraadt 		return EAFNOSUPPORT;
    307   1.6   deraadt 	}
    308   1.6   deraadt 
    309   1.6   deraadt 	if (tp->tun_flags & TUN_RWAIT) {
    310   1.6   deraadt 		tp->tun_flags &= ~TUN_RWAIT;
    311   1.6   deraadt 		wakeup((caddr_t)tp);
    312   1.6   deraadt 	}
    313   1.6   deraadt 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    314   1.6   deraadt 		if (tp->tun_pgrp > 0)
    315   1.6   deraadt 			gsignal(tp->tun_pgrp, SIGIO);
    316  1.22  christos 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    317   1.6   deraadt 			psignal(p, SIGIO);
    318   1.6   deraadt 	}
    319   1.6   deraadt 	selwakeup(&tp->tun_rsel);
    320   1.6   deraadt 	return 0;
    321   1.1       cgd }
    322   1.1       cgd 
    323   1.1       cgd /*
    324   1.1       cgd  * the cdevsw interface is now pretty minimal.
    325   1.1       cgd  */
    326   1.6   deraadt int
    327  1.20   mycroft tunioctl(dev, cmd, data, flag, p)
    328   1.6   deraadt 	dev_t		dev;
    329  1.15       cgd 	u_long		cmd;
    330   1.6   deraadt 	caddr_t		data;
    331   1.6   deraadt 	int		flag;
    332  1.12   deraadt 	struct proc	*p;
    333   1.6   deraadt {
    334   1.6   deraadt 	int		unit = minor(dev), s;
    335   1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    336   1.6   deraadt 
    337   1.6   deraadt 	switch (cmd) {
    338   1.6   deraadt 	case TUNSDEBUG:
    339   1.6   deraadt 		tundebug = *(int *)data;
    340   1.6   deraadt 		break;
    341   1.6   deraadt 	case TUNGDEBUG:
    342   1.6   deraadt 		*(int *)data = tundebug;
    343   1.6   deraadt 		break;
    344   1.6   deraadt 	case FIONBIO:
    345   1.6   deraadt 		if (*(int *)data)
    346   1.6   deraadt 			tp->tun_flags |= TUN_NBIO;
    347   1.6   deraadt 		else
    348   1.6   deraadt 			tp->tun_flags &= ~TUN_NBIO;
    349   1.6   deraadt 		break;
    350   1.6   deraadt 	case FIOASYNC:
    351   1.6   deraadt 		if (*(int *)data)
    352   1.6   deraadt 			tp->tun_flags |= TUN_ASYNC;
    353   1.6   deraadt 		else
    354   1.6   deraadt 			tp->tun_flags &= ~TUN_ASYNC;
    355   1.6   deraadt 		break;
    356   1.6   deraadt 	case FIONREAD:
    357   1.6   deraadt 		s = splimp();
    358   1.6   deraadt 		if (tp->tun_if.if_snd.ifq_head)
    359  1.19        pk 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    360   1.6   deraadt 		else
    361   1.6   deraadt 			*(int *)data = 0;
    362   1.6   deraadt 		splx(s);
    363   1.6   deraadt 		break;
    364   1.6   deraadt 	case TIOCSPGRP:
    365   1.6   deraadt 		tp->tun_pgrp = *(int *)data;
    366   1.6   deraadt 		break;
    367   1.6   deraadt 	case TIOCGPGRP:
    368   1.6   deraadt 		*(int *)data = tp->tun_pgrp;
    369   1.6   deraadt 		break;
    370   1.6   deraadt 	default:
    371   1.6   deraadt 		return (ENOTTY);
    372   1.6   deraadt 	}
    373   1.6   deraadt 	return (0);
    374   1.1       cgd }
    375   1.1       cgd 
    376   1.1       cgd /*
    377   1.6   deraadt  * The cdevsw read interface - reads a packet at a time, or at
    378   1.6   deraadt  * least as much of a packet as can be read.
    379   1.1       cgd  */
    380   1.6   deraadt int
    381  1.22  christos tunread(dev, uio, ioflag)
    382   1.6   deraadt 	dev_t		dev;
    383   1.6   deraadt 	struct uio	*uio;
    384  1.22  christos 	int		ioflag;
    385   1.6   deraadt {
    386   1.8   deraadt 	int		unit = minor(dev);
    387   1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    388   1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    389   1.6   deraadt 	struct mbuf	*m, *m0;
    390   1.6   deraadt 	int		error=0, len, s;
    391   1.6   deraadt 
    392   1.6   deraadt 	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
    393   1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    394   1.8   deraadt 		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
    395   1.8   deraadt 			  ifp->if_unit, tp->tun_flags);
    396   1.8   deraadt 		return EHOSTDOWN;
    397   1.8   deraadt 	}
    398   1.8   deraadt 
    399   1.6   deraadt 	tp->tun_flags &= ~TUN_RWAIT;
    400   1.6   deraadt 
    401   1.6   deraadt 	s = splimp();
    402   1.6   deraadt 	do {
    403   1.6   deraadt 		IF_DEQUEUE(&ifp->if_snd, m0);
    404   1.6   deraadt 		if (m0 == 0) {
    405   1.6   deraadt 			if (tp->tun_flags & TUN_NBIO) {
    406   1.6   deraadt 				splx(s);
    407   1.6   deraadt 				return EWOULDBLOCK;
    408   1.6   deraadt 			}
    409   1.6   deraadt 			tp->tun_flags |= TUN_RWAIT;
    410   1.6   deraadt 			tsleep((caddr_t)tp, PZERO + 1, "tunread", 0);
    411   1.6   deraadt 		}
    412   1.6   deraadt 	} while (m0 == 0);
    413   1.6   deraadt 	splx(s);
    414   1.6   deraadt 
    415   1.6   deraadt 	while (m0 && uio->uio_resid > 0 && error == 0) {
    416  1.13   deraadt 		len = min(uio->uio_resid, m0->m_len);
    417   1.6   deraadt 		if (len == 0)
    418   1.6   deraadt 			break;
    419   1.8   deraadt 		error = uiomove(mtod(m0, caddr_t), len, uio);
    420   1.6   deraadt 		MFREE(m0, m);
    421   1.6   deraadt 		m0 = m;
    422   1.6   deraadt 	}
    423   1.6   deraadt 
    424   1.6   deraadt 	if (m0) {
    425   1.6   deraadt 		TUNDEBUG("Dropping mbuf\n");
    426   1.6   deraadt 		m_freem(m0);
    427   1.6   deraadt 	}
    428  1.19        pk 	if (error)
    429  1.19        pk 		ifp->if_ierrors++;
    430   1.6   deraadt 	return error;
    431   1.1       cgd }
    432   1.1       cgd 
    433   1.1       cgd /*
    434   1.1       cgd  * the cdevsw write interface - an atomic write is a packet - or else!
    435   1.1       cgd  */
    436   1.6   deraadt int
    437  1.22  christos tunwrite(dev, uio, ioflag)
    438   1.8   deraadt 	dev_t		dev;
    439   1.6   deraadt 	struct uio	*uio;
    440  1.22  christos 	int		ioflag;
    441   1.6   deraadt {
    442   1.6   deraadt 	int		unit = minor (dev);
    443   1.8   deraadt 	struct ifnet	*ifp = &tunctl[unit].tun_if;
    444   1.6   deraadt 	struct mbuf	*top, **mp, *m;
    445   1.8   deraadt 	int		error=0, s, tlen, mlen;
    446   1.6   deraadt 
    447   1.6   deraadt 	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
    448   1.6   deraadt 
    449   1.6   deraadt 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
    450   1.6   deraadt 		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
    451   1.6   deraadt 		    uio->uio_resid);
    452   1.6   deraadt 		return EIO;
    453   1.6   deraadt 	}
    454   1.8   deraadt 	tlen = uio->uio_resid;
    455   1.8   deraadt 
    456   1.8   deraadt 	/* get a header mbuf */
    457   1.8   deraadt 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    458   1.8   deraadt 	if (m == NULL)
    459   1.8   deraadt 		return ENOBUFS;
    460   1.8   deraadt 	mlen = MHLEN;
    461   1.8   deraadt 
    462   1.6   deraadt 	top = 0;
    463   1.6   deraadt 	mp = &top;
    464   1.6   deraadt 	while (error == 0 && uio->uio_resid > 0) {
    465  1.13   deraadt 		m->m_len = min(mlen, uio->uio_resid);
    466   1.8   deraadt 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    467   1.6   deraadt 		*mp = m;
    468   1.6   deraadt 		mp = &m->m_next;
    469   1.8   deraadt 		if (uio->uio_resid > 0) {
    470   1.8   deraadt 			MGET (m, M_DONTWAIT, MT_DATA);
    471   1.8   deraadt 			if (m == 0) {
    472   1.8   deraadt 				error = ENOBUFS;
    473   1.8   deraadt 				break;
    474   1.8   deraadt 			}
    475   1.8   deraadt 			mlen = MLEN;
    476   1.8   deraadt 		}
    477   1.6   deraadt 	}
    478   1.6   deraadt 	if (error) {
    479   1.6   deraadt 		if (top)
    480   1.8   deraadt 			m_freem (top);
    481  1.19        pk 		ifp->if_ierrors++;
    482   1.6   deraadt 		return error;
    483   1.6   deraadt 	}
    484   1.6   deraadt 
    485   1.8   deraadt 	top->m_pkthdr.len = tlen;
    486   1.8   deraadt 	top->m_pkthdr.rcvif = ifp;
    487   1.8   deraadt 
    488   1.8   deraadt #if NBPFILTER > 0
    489   1.8   deraadt 	if (tunctl[unit].tun_bpf) {
    490   1.8   deraadt 		/*
    491   1.8   deraadt 		 * We need to prepend the address family as
    492   1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    493   1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    494   1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    495   1.8   deraadt 		 * try to free it or keep a pointer to it).
    496   1.8   deraadt 		 */
    497   1.8   deraadt 		struct mbuf m;
    498  1.16       cgd 		u_int32_t af = AF_INET;
    499   1.8   deraadt 
    500   1.8   deraadt 		m.m_next = top;
    501  1.16       cgd 		m.m_len = sizeof(af);
    502   1.8   deraadt 		m.m_data = (char *)&af;
    503   1.8   deraadt 
    504   1.8   deraadt 		bpf_mtap(tunctl[unit].tun_bpf, &m);
    505   1.6   deraadt 	}
    506   1.8   deraadt #endif
    507   1.6   deraadt 
    508   1.6   deraadt 	s = splimp();
    509   1.6   deraadt 	if (IF_QFULL (&ipintrq)) {
    510   1.6   deraadt 		IF_DROP(&ipintrq);
    511   1.6   deraadt 		splx(s);
    512   1.6   deraadt 		ifp->if_collisions++;
    513   1.6   deraadt 		m_freem(top);
    514   1.6   deraadt 		return ENOBUFS;
    515   1.6   deraadt 	}
    516   1.6   deraadt 	IF_ENQUEUE(&ipintrq, top);
    517   1.6   deraadt 	splx(s);
    518   1.6   deraadt 	ifp->if_ipackets++;
    519   1.6   deraadt 	schednetisr(NETISR_IP);
    520   1.6   deraadt 	return error;
    521   1.1       cgd }
    522   1.1       cgd 
    523   1.1       cgd /*
    524   1.6   deraadt  * tunselect - the select interface, this is only useful on reads
    525   1.6   deraadt  * really. The write detect always returns true, write never blocks
    526   1.6   deraadt  * anyway, it either accepts the packet or drops it.
    527   1.6   deraadt  */
    528   1.6   deraadt int
    529  1.22  christos tunselect(dev, rw, p)
    530   1.6   deraadt 	dev_t		dev;
    531   1.6   deraadt 	int		rw;
    532  1.22  christos 	struct proc	*p;
    533   1.6   deraadt {
    534   1.6   deraadt 	int		unit = minor(dev), s;
    535   1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    536   1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    537   1.6   deraadt 
    538   1.6   deraadt 	s = splimp();
    539   1.6   deraadt 	TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit);
    540   1.6   deraadt 
    541   1.6   deraadt 	switch (rw) {
    542   1.6   deraadt 	case FREAD:
    543   1.6   deraadt 		if (ifp->if_snd.ifq_len > 0) {
    544   1.6   deraadt 			splx(s);
    545   1.6   deraadt 			TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name,
    546   1.6   deraadt 			    ifp->if_unit, ifp->if_snd.ifq_len);
    547   1.6   deraadt 			return 1;
    548   1.6   deraadt 		}
    549   1.8   deraadt 		selrecord(curproc, &tp->tun_rsel);
    550   1.6   deraadt 		break;
    551   1.6   deraadt 	case FWRITE:
    552   1.6   deraadt 		splx(s);
    553   1.6   deraadt 		return 1;
    554   1.6   deraadt 	}
    555   1.6   deraadt 	splx(s);
    556   1.6   deraadt 	TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit);
    557   1.6   deraadt 	return 0;
    558   1.1       cgd }
    559   1.8   deraadt 
    560   1.8   deraadt #endif  /* NTUN */
    561