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