Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.26
      1  1.26        pk /*	$NetBSD: if_tun.c,v 1.26 1996/06/25 22:15:13 pk 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.26        pk static void 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.26        pk 					       tp->tun_flags & TUN_DSTADDR
    173  1.26        pk 							? RTF_HOST
    174  1.26        pk 							: 0);
    175  1.18   mycroft 				}
    176  1.11   deraadt 			}
    177   1.8   deraadt 		}
    178   1.6   deraadt 		splx(s);
    179   1.6   deraadt 	}
    180   1.6   deraadt 	tp->tun_pgrp = 0;
    181   1.7   deraadt 	selwakeup(&tp->tun_rsel);
    182   1.6   deraadt 
    183  1.24   thorpej 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
    184   1.6   deraadt 	return (0);
    185   1.1       cgd }
    186   1.1       cgd 
    187  1.26        pk static void
    188  1.24   thorpej tuninit(tp)
    189  1.24   thorpej 	struct tun_softc *tp;
    190   1.6   deraadt {
    191   1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    192   1.8   deraadt 	register struct ifaddr *ifa;
    193   1.8   deraadt 
    194  1.24   thorpej 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
    195   1.6   deraadt 
    196   1.6   deraadt 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    197   1.8   deraadt 
    198  1.26        pk 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
    199  1.17   mycroft 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    200  1.26        pk 	     ifa = ifa->ifa_list.tqe_next) {
    201  1.11   deraadt 		if (ifa->ifa_addr->sa_family == AF_INET) {
    202  1.17   mycroft 			struct sockaddr_in *sin;
    203  1.11   deraadt 
    204  1.17   mycroft 			sin = satosin(ifa->ifa_addr);
    205  1.17   mycroft 			if (sin && sin->sin_addr.s_addr)
    206  1.17   mycroft 				tp->tun_flags |= TUN_IASET;
    207  1.17   mycroft 
    208  1.26        pk 			if (ifp->if_flags & IFF_POINTOPOINT) {
    209  1.26        pk 				sin = satosin(ifa->ifa_dstaddr);
    210  1.26        pk 				if (sin && sin->sin_addr.s_addr)
    211  1.26        pk 					tp->tun_flags |= TUN_DSTADDR;
    212  1.26        pk 			}
    213  1.11   deraadt 		}
    214  1.18   mycroft 	}
    215   1.8   deraadt 
    216  1.26        pk 	return;
    217   1.1       cgd }
    218   1.1       cgd 
    219   1.1       cgd /*
    220   1.1       cgd  * Process an ioctl request.
    221   1.1       cgd  */
    222   1.1       cgd int
    223  1.22  christos tun_ioctl(ifp, cmd, data)
    224   1.6   deraadt 	struct ifnet *ifp;
    225  1.25   mycroft 	u_long cmd;
    226   1.6   deraadt 	caddr_t	data;
    227   1.6   deraadt {
    228   1.6   deraadt 	int		error = 0, s;
    229   1.6   deraadt 
    230   1.6   deraadt 	s = splimp();
    231   1.6   deraadt 	switch(cmd) {
    232   1.6   deraadt 	case SIOCSIFADDR:
    233  1.24   thorpej 		tuninit((struct tun_softc *)(ifp->if_softc));
    234  1.24   thorpej 		TUNDEBUG("%s: address set\n", ifp->if_xname);
    235   1.6   deraadt 		break;
    236   1.6   deraadt 	case SIOCSIFDSTADDR:
    237  1.24   thorpej 		tuninit((struct tun_softc *)(ifp->if_softc));
    238  1.24   thorpej 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
    239   1.6   deraadt 		break;
    240  1.26        pk 	case SIOCSIFBRDADDR:
    241  1.26        pk 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
    242  1.26        pk 		break;
    243   1.6   deraadt 	default:
    244   1.6   deraadt 		error = EINVAL;
    245   1.6   deraadt 	}
    246   1.6   deraadt 	splx(s);
    247   1.6   deraadt 	return (error);
    248   1.1       cgd }
    249   1.1       cgd 
    250   1.1       cgd /*
    251  1.22  christos  * tun_output - queue packets from higher level ready to put out.
    252   1.1       cgd  */
    253   1.6   deraadt int
    254  1.22  christos tun_output(ifp, m0, dst, rt)
    255   1.6   deraadt 	struct ifnet   *ifp;
    256   1.6   deraadt 	struct mbuf    *m0;
    257   1.6   deraadt 	struct sockaddr *dst;
    258  1.12   deraadt 	struct rtentry *rt;
    259   1.6   deraadt {
    260  1.24   thorpej 	struct tun_softc *tp = ifp->if_softc;
    261   1.6   deraadt 	struct proc	*p;
    262   1.6   deraadt 	int		s;
    263   1.6   deraadt 
    264  1.24   thorpej 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
    265   1.1       cgd 
    266   1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    267  1.24   thorpej 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
    268  1.24   thorpej 			  tp->tun_flags);
    269   1.8   deraadt 		m_freem (m0);
    270  1.26        pk 		return (EHOSTDOWN);
    271   1.8   deraadt 	}
    272   1.8   deraadt 
    273   1.8   deraadt #if NBPFILTER > 0
    274   1.8   deraadt 	if (tp->tun_bpf) {
    275   1.8   deraadt 		/*
    276   1.8   deraadt 		 * We need to prepend the address family as
    277   1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    278   1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    279   1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    280   1.8   deraadt 		 * try to free it or keep a pointer to it).
    281   1.8   deraadt 		 */
    282   1.8   deraadt 		struct mbuf m;
    283  1.16       cgd 		u_int32_t af = dst->sa_family;
    284   1.8   deraadt 
    285   1.8   deraadt 		m.m_next = m0;
    286  1.16       cgd 		m.m_len = sizeof(af);
    287   1.8   deraadt 		m.m_data = (char *)&af;
    288   1.8   deraadt 
    289   1.8   deraadt 		bpf_mtap(tp->tun_bpf, &m);
    290   1.8   deraadt 	}
    291   1.8   deraadt #endif
    292   1.8   deraadt 
    293   1.6   deraadt 	switch(dst->sa_family) {
    294   1.1       cgd #ifdef INET
    295   1.6   deraadt 	case AF_INET:
    296  1.26        pk 		if (tp->tun_flags & TUN_PREPADDR) {
    297  1.26        pk 			/* Simple link-layer header */
    298  1.26        pk 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
    299  1.26        pk 			if (m0 == NULL) {
    300  1.26        pk 				IF_DROP(&ifp->if_snd);
    301  1.26        pk 				return (ENOBUFS);
    302  1.26        pk 			}
    303  1.26        pk 			bcopy(dst, mtod(m0, char *), dst->sa_len);
    304  1.26        pk 		}
    305  1.26        pk 
    306   1.6   deraadt 		s = splimp();
    307   1.6   deraadt 		if (IF_QFULL(&ifp->if_snd)) {
    308   1.6   deraadt 			IF_DROP(&ifp->if_snd);
    309   1.6   deraadt 			m_freem(m0);
    310   1.6   deraadt 			splx(s);
    311   1.6   deraadt 			ifp->if_collisions++;
    312   1.6   deraadt 			return (ENOBUFS);
    313   1.6   deraadt 		}
    314   1.6   deraadt 		IF_ENQUEUE(&ifp->if_snd, m0);
    315   1.6   deraadt 		splx(s);
    316   1.6   deraadt 		ifp->if_opackets++;
    317   1.6   deraadt 		break;
    318   1.1       cgd #endif
    319   1.6   deraadt 	default:
    320   1.6   deraadt 		m_freem(m0);
    321  1.26        pk 		return (EAFNOSUPPORT);
    322   1.6   deraadt 	}
    323   1.6   deraadt 
    324   1.6   deraadt 	if (tp->tun_flags & TUN_RWAIT) {
    325   1.6   deraadt 		tp->tun_flags &= ~TUN_RWAIT;
    326   1.6   deraadt 		wakeup((caddr_t)tp);
    327   1.6   deraadt 	}
    328   1.6   deraadt 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    329   1.6   deraadt 		if (tp->tun_pgrp > 0)
    330   1.6   deraadt 			gsignal(tp->tun_pgrp, SIGIO);
    331  1.22  christos 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    332   1.6   deraadt 			psignal(p, SIGIO);
    333   1.6   deraadt 	}
    334   1.6   deraadt 	selwakeup(&tp->tun_rsel);
    335  1.26        pk 	return (0);
    336   1.1       cgd }
    337   1.1       cgd 
    338   1.1       cgd /*
    339   1.1       cgd  * the cdevsw interface is now pretty minimal.
    340   1.1       cgd  */
    341   1.6   deraadt int
    342  1.20   mycroft tunioctl(dev, cmd, data, flag, p)
    343   1.6   deraadt 	dev_t		dev;
    344  1.15       cgd 	u_long		cmd;
    345   1.6   deraadt 	caddr_t		data;
    346   1.6   deraadt 	int		flag;
    347  1.12   deraadt 	struct proc	*p;
    348   1.6   deraadt {
    349   1.6   deraadt 	int		unit = minor(dev), s;
    350   1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    351   1.6   deraadt 
    352   1.6   deraadt 	switch (cmd) {
    353   1.6   deraadt 	case TUNSDEBUG:
    354   1.6   deraadt 		tundebug = *(int *)data;
    355   1.6   deraadt 		break;
    356  1.26        pk 
    357   1.6   deraadt 	case TUNGDEBUG:
    358   1.6   deraadt 		*(int *)data = tundebug;
    359   1.6   deraadt 		break;
    360  1.26        pk 
    361  1.26        pk 	case TUNSIFMODE:
    362  1.26        pk 		switch (*(int *)data) {
    363  1.26        pk 		case IFF_POINTOPOINT:
    364  1.26        pk 		case IFF_BROADCAST:
    365  1.26        pk 			s = splimp();
    366  1.26        pk 			if (tp->tun_if.if_flags & IFF_UP) {
    367  1.26        pk 				splx(s);
    368  1.26        pk 				return (EBUSY);
    369  1.26        pk 			}
    370  1.26        pk 			tp->tun_if.if_flags &=
    371  1.26        pk 				~(IFF_BROADCAST|IFF_POINTOPOINT);
    372  1.26        pk 			tp->tun_if.if_flags |= *(int *)data;
    373  1.26        pk 			splx(s);
    374  1.26        pk 			break;
    375  1.26        pk 		default:
    376  1.26        pk 			return (EINVAL);
    377  1.26        pk 			break;
    378  1.26        pk 		}
    379  1.26        pk 		break;
    380  1.26        pk 
    381  1.26        pk 	case TUNSLMODE:
    382  1.26        pk 		if (*(int *)data)
    383  1.26        pk 			tp->tun_flags |= TUN_PREPADDR;
    384  1.26        pk 		else
    385  1.26        pk 			tp->tun_flags &= ~TUN_PREPADDR;
    386  1.26        pk 		break;
    387  1.26        pk 
    388   1.6   deraadt 	case FIONBIO:
    389   1.6   deraadt 		if (*(int *)data)
    390   1.6   deraadt 			tp->tun_flags |= TUN_NBIO;
    391   1.6   deraadt 		else
    392   1.6   deraadt 			tp->tun_flags &= ~TUN_NBIO;
    393   1.6   deraadt 		break;
    394  1.26        pk 
    395   1.6   deraadt 	case FIOASYNC:
    396   1.6   deraadt 		if (*(int *)data)
    397   1.6   deraadt 			tp->tun_flags |= TUN_ASYNC;
    398   1.6   deraadt 		else
    399   1.6   deraadt 			tp->tun_flags &= ~TUN_ASYNC;
    400   1.6   deraadt 		break;
    401  1.26        pk 
    402   1.6   deraadt 	case FIONREAD:
    403   1.6   deraadt 		s = splimp();
    404   1.6   deraadt 		if (tp->tun_if.if_snd.ifq_head)
    405  1.19        pk 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    406   1.6   deraadt 		else
    407   1.6   deraadt 			*(int *)data = 0;
    408   1.6   deraadt 		splx(s);
    409   1.6   deraadt 		break;
    410  1.26        pk 
    411   1.6   deraadt 	case TIOCSPGRP:
    412   1.6   deraadt 		tp->tun_pgrp = *(int *)data;
    413   1.6   deraadt 		break;
    414  1.26        pk 
    415   1.6   deraadt 	case TIOCGPGRP:
    416   1.6   deraadt 		*(int *)data = tp->tun_pgrp;
    417   1.6   deraadt 		break;
    418  1.26        pk 
    419   1.6   deraadt 	default:
    420   1.6   deraadt 		return (ENOTTY);
    421   1.6   deraadt 	}
    422   1.6   deraadt 	return (0);
    423   1.1       cgd }
    424   1.1       cgd 
    425   1.1       cgd /*
    426   1.6   deraadt  * The cdevsw read interface - reads a packet at a time, or at
    427   1.6   deraadt  * least as much of a packet as can be read.
    428   1.1       cgd  */
    429   1.6   deraadt int
    430  1.22  christos tunread(dev, uio, ioflag)
    431   1.6   deraadt 	dev_t		dev;
    432   1.6   deraadt 	struct uio	*uio;
    433  1.22  christos 	int		ioflag;
    434   1.6   deraadt {
    435   1.8   deraadt 	int		unit = minor(dev);
    436   1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    437   1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    438   1.6   deraadt 	struct mbuf	*m, *m0;
    439   1.6   deraadt 	int		error=0, len, s;
    440   1.6   deraadt 
    441  1.24   thorpej 	TUNDEBUG ("%s: read\n", ifp->if_xname);
    442   1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    443  1.26        pk 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
    444   1.8   deraadt 		return EHOSTDOWN;
    445   1.8   deraadt 	}
    446   1.8   deraadt 
    447   1.6   deraadt 	tp->tun_flags &= ~TUN_RWAIT;
    448   1.6   deraadt 
    449   1.6   deraadt 	s = splimp();
    450   1.6   deraadt 	do {
    451   1.6   deraadt 		IF_DEQUEUE(&ifp->if_snd, m0);
    452   1.6   deraadt 		if (m0 == 0) {
    453   1.6   deraadt 			if (tp->tun_flags & TUN_NBIO) {
    454   1.6   deraadt 				splx(s);
    455  1.26        pk 				return (EWOULDBLOCK);
    456   1.6   deraadt 			}
    457   1.6   deraadt 			tp->tun_flags |= TUN_RWAIT;
    458  1.26        pk 			if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
    459  1.26        pk 				splx(s);
    460  1.26        pk 				return (EINTR);
    461  1.26        pk 			}
    462   1.6   deraadt 		}
    463   1.6   deraadt 	} while (m0 == 0);
    464   1.6   deraadt 	splx(s);
    465   1.6   deraadt 
    466   1.6   deraadt 	while (m0 && uio->uio_resid > 0 && error == 0) {
    467  1.13   deraadt 		len = min(uio->uio_resid, m0->m_len);
    468   1.6   deraadt 		if (len == 0)
    469   1.6   deraadt 			break;
    470   1.8   deraadt 		error = uiomove(mtod(m0, caddr_t), len, uio);
    471   1.6   deraadt 		MFREE(m0, m);
    472   1.6   deraadt 		m0 = m;
    473   1.6   deraadt 	}
    474   1.6   deraadt 
    475   1.6   deraadt 	if (m0) {
    476   1.6   deraadt 		TUNDEBUG("Dropping mbuf\n");
    477   1.6   deraadt 		m_freem(m0);
    478   1.6   deraadt 	}
    479  1.19        pk 	if (error)
    480  1.19        pk 		ifp->if_ierrors++;
    481  1.26        pk 	return (error);
    482   1.1       cgd }
    483   1.1       cgd 
    484   1.1       cgd /*
    485   1.1       cgd  * the cdevsw write interface - an atomic write is a packet - or else!
    486   1.1       cgd  */
    487   1.6   deraadt int
    488  1.22  christos tunwrite(dev, uio, ioflag)
    489   1.8   deraadt 	dev_t		dev;
    490   1.6   deraadt 	struct uio	*uio;
    491  1.22  christos 	int		ioflag;
    492   1.6   deraadt {
    493   1.6   deraadt 	int		unit = minor (dev);
    494  1.26        pk 	struct tun_softc *tp = &tunctl[unit];
    495  1.26        pk 	struct ifnet	*ifp = &tp->tun_if;
    496   1.6   deraadt 	struct mbuf	*top, **mp, *m;
    497  1.26        pk 	struct ifqueue	*ifq;
    498  1.26        pk 	struct sockaddr	dst;
    499  1.26        pk 	int		isr, error=0, s, tlen, mlen;
    500   1.6   deraadt 
    501  1.24   thorpej 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
    502   1.6   deraadt 
    503  1.26        pk 	if (tp->tun_flags & TUN_PREPADDR) {
    504  1.26        pk 		if (uio->uio_resid < sizeof(dst))
    505  1.26        pk 			return (EIO);
    506  1.26        pk 		error = uiomove((caddr_t)&dst, sizeof(dst), uio);
    507  1.26        pk 		if (dst.sa_len > sizeof(dst)) {
    508  1.26        pk 			/* Duh.. */
    509  1.26        pk 			char discard;
    510  1.26        pk 			int n = dst.sa_len - sizeof(dst);
    511  1.26        pk 			while (n--)
    512  1.26        pk 				if ((error = uiomove(&discard, 1, uio)) != 0)
    513  1.26        pk 					return (error);
    514  1.26        pk 		}
    515  1.26        pk 	} else {
    516  1.26        pk #ifdef INET
    517  1.26        pk 		dst.sa_family = AF_INET;
    518  1.26        pk #endif
    519  1.26        pk 	}
    520  1.26        pk 
    521   1.6   deraadt 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
    522  1.24   thorpej 		TUNDEBUG("%s: len=%d!\n", ifp->if_xname, uio->uio_resid);
    523  1.26        pk 		return (EIO);
    524   1.6   deraadt 	}
    525  1.26        pk 
    526  1.26        pk 	switch (dst.sa_family) {
    527  1.26        pk #ifdef INET
    528  1.26        pk 	case AF_INET:
    529  1.26        pk 		ifq = &ipintrq;
    530  1.26        pk 		isr = NETISR_IP;
    531  1.26        pk 		break;
    532  1.26        pk #endif
    533  1.26        pk 	default:
    534  1.26        pk 		return (EAFNOSUPPORT);
    535  1.26        pk 	}
    536  1.26        pk 
    537   1.8   deraadt 	tlen = uio->uio_resid;
    538   1.8   deraadt 
    539   1.8   deraadt 	/* get a header mbuf */
    540   1.8   deraadt 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    541   1.8   deraadt 	if (m == NULL)
    542  1.26        pk 		return (ENOBUFS);
    543   1.8   deraadt 	mlen = MHLEN;
    544   1.8   deraadt 
    545   1.6   deraadt 	top = 0;
    546   1.6   deraadt 	mp = &top;
    547   1.6   deraadt 	while (error == 0 && uio->uio_resid > 0) {
    548  1.13   deraadt 		m->m_len = min(mlen, uio->uio_resid);
    549   1.8   deraadt 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    550   1.6   deraadt 		*mp = m;
    551   1.6   deraadt 		mp = &m->m_next;
    552   1.8   deraadt 		if (uio->uio_resid > 0) {
    553   1.8   deraadt 			MGET (m, M_DONTWAIT, MT_DATA);
    554   1.8   deraadt 			if (m == 0) {
    555   1.8   deraadt 				error = ENOBUFS;
    556   1.8   deraadt 				break;
    557   1.8   deraadt 			}
    558   1.8   deraadt 			mlen = MLEN;
    559   1.8   deraadt 		}
    560   1.6   deraadt 	}
    561   1.6   deraadt 	if (error) {
    562   1.6   deraadt 		if (top)
    563   1.8   deraadt 			m_freem (top);
    564  1.19        pk 		ifp->if_ierrors++;
    565  1.26        pk 		return (error);
    566   1.6   deraadt 	}
    567   1.6   deraadt 
    568   1.8   deraadt 	top->m_pkthdr.len = tlen;
    569   1.8   deraadt 	top->m_pkthdr.rcvif = ifp;
    570   1.8   deraadt 
    571   1.8   deraadt #if NBPFILTER > 0
    572  1.26        pk 	if (tp->tun_bpf) {
    573   1.8   deraadt 		/*
    574   1.8   deraadt 		 * We need to prepend the address family as
    575   1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    576   1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    577   1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    578   1.8   deraadt 		 * try to free it or keep a pointer to it).
    579   1.8   deraadt 		 */
    580   1.8   deraadt 		struct mbuf m;
    581  1.16       cgd 		u_int32_t af = AF_INET;
    582   1.8   deraadt 
    583   1.8   deraadt 		m.m_next = top;
    584  1.16       cgd 		m.m_len = sizeof(af);
    585   1.8   deraadt 		m.m_data = (char *)&af;
    586   1.8   deraadt 
    587  1.26        pk 		bpf_mtap(tp->tun_bpf, &m);
    588   1.6   deraadt 	}
    589   1.8   deraadt #endif
    590   1.6   deraadt 
    591   1.6   deraadt 	s = splimp();
    592  1.26        pk 	if (IF_QFULL(ifq)) {
    593  1.26        pk 		IF_DROP(ifq);
    594   1.6   deraadt 		splx(s);
    595   1.6   deraadt 		ifp->if_collisions++;
    596   1.6   deraadt 		m_freem(top);
    597  1.26        pk 		return (ENOBUFS);
    598   1.6   deraadt 	}
    599  1.26        pk 	IF_ENQUEUE(ifq, top);
    600   1.6   deraadt 	splx(s);
    601   1.6   deraadt 	ifp->if_ipackets++;
    602  1.26        pk 	schednetisr(isr);
    603  1.26        pk 	return (error);
    604   1.1       cgd }
    605   1.1       cgd 
    606   1.1       cgd /*
    607   1.6   deraadt  * tunselect - the select interface, this is only useful on reads
    608   1.6   deraadt  * really. The write detect always returns true, write never blocks
    609   1.6   deraadt  * anyway, it either accepts the packet or drops it.
    610   1.6   deraadt  */
    611   1.6   deraadt int
    612  1.22  christos tunselect(dev, rw, p)
    613   1.6   deraadt 	dev_t		dev;
    614   1.6   deraadt 	int		rw;
    615  1.22  christos 	struct proc	*p;
    616   1.6   deraadt {
    617   1.6   deraadt 	int		unit = minor(dev), s;
    618   1.8   deraadt 	struct tun_softc *tp = &tunctl[unit];
    619   1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    620   1.6   deraadt 
    621   1.6   deraadt 	s = splimp();
    622  1.24   thorpej 	TUNDEBUG("%s: tunselect\n", ifp->if_xname);
    623   1.6   deraadt 
    624   1.6   deraadt 	switch (rw) {
    625   1.6   deraadt 	case FREAD:
    626   1.6   deraadt 		if (ifp->if_snd.ifq_len > 0) {
    627   1.6   deraadt 			splx(s);
    628  1.24   thorpej 			TUNDEBUG("%s: tunselect q=%d\n", ifp->if_xname,
    629  1.24   thorpej 			    ifp->if_snd.ifq_len);
    630  1.26        pk 			return (1);
    631   1.6   deraadt 		}
    632  1.25   mycroft 		selrecord(p, &tp->tun_rsel);
    633   1.6   deraadt 		break;
    634   1.6   deraadt 	case FWRITE:
    635   1.6   deraadt 		splx(s);
    636  1.26        pk 		return (1);
    637   1.6   deraadt 	}
    638   1.6   deraadt 	splx(s);
    639  1.24   thorpej 	TUNDEBUG("%s: tunselect waiting\n", ifp->if_xname);
    640  1.26        pk 	return (0);
    641   1.1       cgd }
    642   1.8   deraadt 
    643   1.8   deraadt #endif  /* NTUN */
    644