Home | History | Annotate | Line # | Download | only in net
if_srt.c revision 1.30.4.1
      1  1.30.4.1        ad /* $NetBSD: if_srt.c,v 1.30.4.1 2020/02/29 20:21:06 ad Exp $ */
      2       1.1     mouse /* This file is in the public domain. */
      3       1.1     mouse 
      4       1.6     lukem #include <sys/cdefs.h>
      5  1.30.4.1        ad __KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1.30.4.1 2020/02/29 20:21:06 ad Exp $");
      6       1.6     lukem 
      7      1.20     pooka #ifdef _KERNEL_OPT
      8       1.1     mouse #include "opt_inet.h"
      9      1.20     pooka #endif
     10       1.1     mouse 
     11       1.1     mouse #if !defined(INET) && !defined(INET6)
     12       1.1     mouse #error "srt without INET/INET6?"
     13       1.1     mouse #endif
     14       1.1     mouse 
     15       1.1     mouse #ifndef SRT_MAXUNIT
     16       1.1     mouse #define SRT_MAXUNIT 255
     17       1.1     mouse #endif
     18       1.1     mouse 
     19       1.3     mouse /* include-file bug workarounds */
     20      1.12    dyoung #include <sys/types.h>		/* sys/conf.h */
     21      1.12    dyoung #include <sys/resource.h>	/* sys/resourcevar.h
     22      1.12    dyoung 				 * (uvm/uvm_param.h, sys/mbuf.h)
     23      1.12    dyoung 				 */
     24      1.12    dyoung #include <netinet/in.h>		/* netinet/ip.h */
     25      1.12    dyoung #include <sys/param.h>		/* sys/mbuf.h */
     26      1.12    dyoung #include <netinet/in_systm.h>	/* netinet/ip.h */
     27       1.3     mouse 
     28       1.1     mouse #include <sys/conf.h>
     29       1.1     mouse #include <sys/mbuf.h>
     30       1.1     mouse #include <sys/errno.h>
     31       1.1     mouse #include <sys/fcntl.h>
     32       1.1     mouse #include <sys/param.h>
     33       1.1     mouse #include <sys/ioctl.h>
     34      1.23  christos #include <sys/module.h>
     35      1.23  christos #include <sys/device.h>
     36       1.1     mouse #include <netinet/ip.h>
     37       1.1     mouse #include <netinet/ip6.h>
     38      1.25       kre #include <netinet6/in6_var.h>
     39      1.26     ozaki #include <netinet6/ip6_var.h>
     40      1.25       kre #include <netinet6/nd6.h>
     41      1.25       kre #include <netinet6/scope6_var.h>
     42       1.1     mouse #include <net/if_types.h>
     43       1.1     mouse 
     44       1.1     mouse #include "if_srt.h"
     45       1.1     mouse 
     46       1.1     mouse /* until we know what to pass to bpfattach.... */
     47      1.13     pooka /* #define BPFILTER_NOW_AVAILABLE */
     48       1.1     mouse 
     49      1.12    dyoung struct srt_softc {
     50      1.12    dyoung 	struct ifnet intf;	/* XXX interface botch */
     51      1.12    dyoung 	int unit;
     52      1.12    dyoung 	int nrt;
     53      1.12    dyoung 	struct srt_rt **rts;
     54      1.12    dyoung 	unsigned int flags;	/* SSF_* values from if_srt.h */
     55       1.1     mouse #define SSF_UCHG (SSF_MTULOCK) /* userland-changeable bits */
     56      1.12    dyoung 	unsigned int kflags;	/* bits private to this file */
     57       1.1     mouse #define SKF_CDEVOPEN 0x00000001
     58      1.12    dyoung };
     59      1.12    dyoung 
     60      1.23  christos #include "ioconf.h"
     61       1.1     mouse 
     62      1.12    dyoung static struct srt_softc *softcv[SRT_MAXUNIT+1];
     63       1.1     mouse static unsigned int global_flags;
     64       1.1     mouse 
     65      1.23  christos static u_int srt_count;
     66      1.23  christos 
     67      1.28  pgoyette #ifdef _MODULE
     68      1.28  pgoyette devmajor_t srt_bmajor = -1, srt_cmajor = -1;
     69      1.28  pgoyette #endif
     70      1.28  pgoyette 
     71      1.28  pgoyette static int srt_open(dev_t, int, int, struct lwp *);
     72      1.28  pgoyette static int srt_close(dev_t, int, int, struct lwp *);
     73      1.28  pgoyette static int srt_ioctl(dev_t, u_long, void *, int, struct lwp *);
     74      1.28  pgoyette 
     75      1.28  pgoyette const struct cdevsw srt_cdevsw = {
     76      1.28  pgoyette 	.d_open = srt_open,
     77      1.28  pgoyette 	.d_close = srt_close,
     78      1.28  pgoyette 	.d_read = nullread,
     79      1.28  pgoyette 	.d_write = nullwrite,
     80      1.28  pgoyette 	.d_ioctl = srt_ioctl,
     81      1.28  pgoyette 	.d_stop = nullstop,
     82      1.28  pgoyette 	.d_tty = notty,
     83      1.28  pgoyette 	.d_poll = nullpoll,
     84      1.28  pgoyette 	.d_mmap = nommap,
     85      1.28  pgoyette 	.d_kqfilter = nullkqfilter,
     86      1.28  pgoyette 	.d_discard = nodiscard,
     87      1.28  pgoyette 	.d_flag = D_OTHER
     88      1.28  pgoyette };
     89      1.28  pgoyette 
     90       1.1     mouse /* Internal routines. */
     91       1.1     mouse 
     92      1.12    dyoung static unsigned int ipv4_masks[33] = {
     93      1.12    dyoung 	0x00000000, /* /0 */
     94      1.12    dyoung 	0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, /* /1 - /4 */
     95      1.12    dyoung 	0xf8000000, 0xfc000000, 0xfe000000, 0xff000000, /* /5 - /8 */
     96      1.12    dyoung 	0xff800000, 0xffc00000, 0xffe00000, 0xfff00000, /* /9 - /12 */
     97      1.12    dyoung 	0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, /* /13 - /16 */
     98      1.12    dyoung 	0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000, /* /17 - /20 */
     99      1.12    dyoung 	0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00, /* /21 - /24 */
    100      1.12    dyoung 	0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, /* /25 - /28 */
    101      1.12    dyoung 	0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff  /* /29 - /32 */
    102      1.12    dyoung };
    103       1.1     mouse 
    104      1.12    dyoung static void
    105      1.12    dyoung update_mtu(struct srt_softc *sc)
    106       1.1     mouse {
    107      1.12    dyoung 	int mtu;
    108      1.12    dyoung 	int i;
    109      1.12    dyoung 	struct srt_rt *r;
    110      1.12    dyoung 
    111      1.12    dyoung 	if (sc->flags & SSF_MTULOCK)
    112      1.12    dyoung 		return;
    113      1.12    dyoung 	mtu = 65535;
    114      1.27   msaitoh 	for (i = sc->nrt-1; i>=0; i--) {
    115      1.12    dyoung 		r = sc->rts[i];
    116      1.12    dyoung 		if (r->u.dstifp->if_mtu < mtu)
    117      1.12    dyoung 			mtu = r->u.dstifp->if_mtu;
    118      1.12    dyoung 	}
    119      1.12    dyoung 	sc->intf.if_mtu = mtu;
    120       1.1     mouse }
    121       1.1     mouse 
    122      1.12    dyoung static struct srt_rt *
    123      1.12    dyoung find_rt(struct srt_softc *sc, int af, ...)
    124       1.1     mouse {
    125      1.12    dyoung 	int i;
    126      1.12    dyoung 	struct srt_rt *r;
    127      1.12    dyoung 	struct in_addr ia;
    128      1.12    dyoung 	struct in6_addr ia6;
    129      1.12    dyoung 	va_list ap;
    130      1.12    dyoung 
    131      1.12    dyoung 	ia.s_addr = 0; ia6.s6_addr[0] = 0; /* shut up incorrect -Wuninitialized */
    132      1.12    dyoung 	va_start(ap,af);
    133      1.12    dyoung 	switch (af) {
    134      1.12    dyoung 	case AF_INET:
    135      1.12    dyoung 		ia = va_arg(ap,struct in_addr);
    136      1.12    dyoung 		break;
    137      1.12    dyoung 	case AF_INET6:
    138      1.12    dyoung 		ia6 = va_arg(ap,struct in6_addr);
    139      1.12    dyoung 		break;
    140      1.12    dyoung 	default:
    141      1.12    dyoung 		panic("if_srt find_rt: impossible address family");
    142      1.12    dyoung 		break;
    143      1.12    dyoung 	}
    144      1.12    dyoung 	va_end(ap);
    145      1.15       tls 	for (i=0; i < sc->nrt; i++) {
    146      1.12    dyoung 		r = sc->rts[i];
    147      1.12    dyoung 		if (r->af != af)
    148      1.12    dyoung 			continue;
    149      1.12    dyoung 		switch (af) {
    150      1.12    dyoung 		case AF_INET:
    151      1.15       tls 			if ((ia.s_addr & htonl(ipv4_masks[r->srcmask])) ==
    152      1.12    dyoung 			    r->srcmatch.v4.s_addr)
    153      1.12    dyoung 				return r;
    154      1.12    dyoung 			break;
    155      1.12    dyoung 		case AF_INET6:
    156      1.12    dyoung 			if ((r->srcmask >= 8) &&
    157      1.12    dyoung 			    memcmp(&ia6,&r->srcmatch.v6,r->srcmask / 8) != 0)
    158      1.12    dyoung 				continue;
    159      1.12    dyoung 			if ((r->srcmask % 8) &&
    160      1.12    dyoung 			    ((ia6.s6_addr[r->srcmask / 8] ^
    161      1.12    dyoung 			     r->srcmatch.v6.s6_addr[r->srcmask / 8]) &
    162      1.12    dyoung 			     0xff & (0xff00 >> (r->srcmask % 8))))
    163      1.12    dyoung 				continue;
    164      1.12    dyoung 			return r;
    165      1.12    dyoung 		default:
    166      1.12    dyoung 			panic("if_srt find_rt: impossible address family 2");
    167      1.12    dyoung 			break;
    168      1.12    dyoung 		}
    169      1.12    dyoung 	}
    170      1.12    dyoung 	return 0;
    171       1.1     mouse }
    172       1.1     mouse 
    173       1.1     mouse /* Network device interface. */
    174       1.1     mouse 
    175      1.12    dyoung static int
    176      1.12    dyoung srt_if_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    177       1.1     mouse {
    178      1.12    dyoung 	struct ifaddr *ifa;
    179      1.12    dyoung 	int s;
    180      1.12    dyoung 	int err;
    181      1.12    dyoung 
    182      1.12    dyoung 	err = 0;
    183      1.12    dyoung 	s = splnet();
    184      1.12    dyoung 	switch (cmd) {
    185      1.12    dyoung 	case SIOCINITIFADDR:
    186      1.12    dyoung 		ifa = (void *) data;
    187      1.12    dyoung 		switch (ifa->ifa_addr->sa_family) {
    188       1.1     mouse #ifdef INET
    189      1.12    dyoung 		case AF_INET:
    190      1.12    dyoung 			break;
    191       1.1     mouse #endif
    192       1.1     mouse #ifdef INET6
    193      1.12    dyoung 		case AF_INET6:
    194      1.12    dyoung 			break;
    195       1.1     mouse #endif
    196      1.12    dyoung 		default:
    197      1.12    dyoung 			err = EAFNOSUPPORT;
    198      1.12    dyoung 			break;
    199      1.12    dyoung 		}
    200      1.12    dyoung 		break;
    201      1.12    dyoung 	default:
    202      1.12    dyoung 		if ((err = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    203      1.12    dyoung 			err = 0;
    204      1.12    dyoung 		break;
    205      1.12    dyoung 	}
    206      1.12    dyoung 	splx(s);
    207      1.12    dyoung 	return err;
    208       1.1     mouse }
    209       1.1     mouse 
    210      1.12    dyoung static int
    211      1.12    dyoung srt_if_output(
    212      1.12    dyoung 	struct ifnet *ifp,
    213       1.1     mouse 	struct mbuf *m,
    214       1.4    dyoung 	const struct sockaddr *to,
    215      1.21     ozaki 	const struct rtentry *rtp)
    216       1.1     mouse {
    217      1.12    dyoung 	struct srt_softc *sc;
    218      1.12    dyoung 	struct srt_rt *r;
    219       1.1     mouse 
    220      1.12    dyoung 	sc = ifp->if_softc;
    221      1.12    dyoung 	if (! (ifp->if_flags & IFF_UP)) {
    222      1.12    dyoung 		m_freem(m);
    223      1.12    dyoung 		return ENETDOWN;
    224      1.12    dyoung 	}
    225      1.12    dyoung 	switch (to->sa_family) {
    226       1.1     mouse #ifdef INET
    227      1.12    dyoung 	case AF_INET: {
    228      1.12    dyoung 		struct ip *ip;
    229      1.12    dyoung 		ip = mtod(m,struct ip *);
    230      1.12    dyoung 		r = find_rt(sc,AF_INET,ip->ip_src);
    231      1.12    dyoung 		break;
    232      1.12    dyoung 	}
    233       1.1     mouse #endif
    234      1.12    dyoung #ifdef INET6
    235      1.12    dyoung 	case AF_INET6: {
    236      1.12    dyoung 		struct ip6_hdr *ip;
    237      1.12    dyoung 		ip = mtod(m,struct ip6_hdr *);
    238      1.12    dyoung 		r = find_rt(sc,AF_INET6,ip->ip6_src);
    239      1.12    dyoung 		break;
    240       1.1     mouse 	}
    241       1.1     mouse #endif
    242      1.12    dyoung 	default:
    243      1.12    dyoung 		IF_DROP(&ifp->if_snd);
    244      1.12    dyoung 		m_freem(m);
    245      1.12    dyoung 		return EAFNOSUPPORT;
    246      1.12    dyoung 	}
    247      1.12    dyoung 	/* XXX Do we need to bpf_tap?  Or do higher layers now handle that? */
    248      1.12    dyoung 	/* if_gif.c seems to imply the latter. */
    249  1.30.4.1        ad 	if_statinc(ifp, if_opackets);
    250      1.12    dyoung 	if (! r) {
    251  1.30.4.1        ad 		if_statinc(ifp, if_oerrors);
    252      1.12    dyoung 		m_freem(m);
    253      1.12    dyoung 		return 0;
    254      1.12    dyoung 	}
    255      1.12    dyoung 	if (! (m->m_flags & M_PKTHDR)) {
    256      1.12    dyoung 		printf("srt_if_output no PKTHDR\n");
    257      1.12    dyoung 		m_freem(m);
    258      1.12    dyoung 		return 0;
    259      1.12    dyoung 	}
    260  1.30.4.1        ad 	if_statadd(ifp, if_obytes, m->m_pkthdr.len);
    261      1.12    dyoung 	if (! (r->u.dstifp->if_flags & IFF_UP)) {
    262      1.12    dyoung 		m_freem(m);
    263      1.12    dyoung 		return 0; /* XXX ENETDOWN? */
    264      1.12    dyoung 	}
    265      1.12    dyoung 	/* XXX is 0 the right last arg here? */
    266      1.25       kre 	if (to->sa_family == AF_INET6)
    267      1.26     ozaki 		return ip6_if_output(r->u.dstifp, r->u.dstifp, m, &r->dst.sin6, 0);
    268      1.22  knakahar 	return if_output_lock(r->u.dstifp, r->u.dstifp, m, &r->dst.sa, 0);
    269       1.1     mouse }
    270       1.1     mouse 
    271      1.12    dyoung static int
    272      1.12    dyoung srt_clone_create(struct if_clone *cl, int unit)
    273       1.1     mouse {
    274      1.12    dyoung 	struct srt_softc *sc;
    275      1.27   msaitoh 	int rv;
    276       1.1     mouse 
    277      1.12    dyoung 	if (unit < 0 || unit > SRT_MAXUNIT)
    278      1.12    dyoung 		return ENXIO;
    279      1.12    dyoung 	if (softcv[unit])
    280      1.12    dyoung 		return EBUSY;
    281      1.27   msaitoh 	sc = malloc(sizeof(struct srt_softc), M_DEVBUF, M_WAITOK | M_ZERO);
    282      1.12    dyoung 	sc->unit = unit;
    283      1.12    dyoung 	sc->nrt = 0;
    284      1.12    dyoung 	sc->rts = 0;
    285      1.12    dyoung 	sc->flags = 0;
    286      1.12    dyoung 	sc->kflags = 0;
    287      1.12    dyoung 	if_initname(&sc->intf,cl->ifc_name,unit);
    288      1.12    dyoung 	sc->intf.if_softc = sc;
    289      1.12    dyoung 	sc->intf.if_mtu = 65535;
    290      1.12    dyoung 	sc->intf.if_flags = IFF_POINTOPOINT;
    291      1.12    dyoung 	sc->intf.if_type = IFT_OTHER;
    292      1.12    dyoung 	sc->intf.if_ioctl = &srt_if_ioctl;
    293      1.12    dyoung 	sc->intf.if_output = &srt_if_output;
    294      1.12    dyoung 	sc->intf.if_dlt = DLT_RAW;
    295      1.27   msaitoh 	rv = if_attach(&sc->intf);
    296      1.27   msaitoh 	if (rv != 0) {
    297      1.27   msaitoh 		aprint_error("%s: if_initialize failed(%d)\n",
    298      1.27   msaitoh 		    sc->intf.if_xname, rv);
    299      1.27   msaitoh 		free(sc, M_DEVBUF);
    300      1.27   msaitoh 		return rv;
    301      1.27   msaitoh 	}
    302      1.12    dyoung 	if_alloc_sadl(&sc->intf);
    303      1.13     pooka #ifdef BPFILTER_NOW_AVAILABLE
    304      1.14     joerg 	bpf_attach(&sc->intf, 0, 0);
    305       1.1     mouse #endif
    306      1.12    dyoung 	softcv[unit] = sc;
    307      1.23  christos 	atomic_inc_uint(&srt_count);
    308      1.12    dyoung 	return 0;
    309       1.1     mouse }
    310       1.1     mouse 
    311      1.12    dyoung static int
    312      1.12    dyoung srt_clone_destroy(struct ifnet *ifp)
    313       1.1     mouse {
    314      1.12    dyoung 	struct srt_softc *sc;
    315       1.1     mouse 
    316      1.12    dyoung 	sc = ifp->if_softc;
    317      1.12    dyoung 	if ((ifp->if_flags & IFF_UP) || (sc->kflags & SKF_CDEVOPEN))
    318      1.12    dyoung 		return EBUSY;
    319      1.13     pooka #ifdef BPFILTER_NOW_AVAILABLE
    320      1.14     joerg 	bpf_detach(ifp);
    321       1.1     mouse #endif
    322      1.12    dyoung 	if_detach(ifp);
    323      1.12    dyoung 	if (sc->unit < 0 || sc->unit > SRT_MAXUNIT) {
    324      1.12    dyoung 		panic("srt_clone_destroy: impossible unit %d\n",sc->unit);
    325      1.12    dyoung 	}
    326      1.12    dyoung 	if (softcv[sc->unit] != sc) {
    327      1.12    dyoung 		panic("srt_clone_destroy: bad backpointer ([%d]=%p not %p)\n",
    328      1.27   msaitoh 		sc->unit, (void *)softcv[sc->unit], (void *)sc);
    329      1.12    dyoung 	}
    330      1.12    dyoung 	softcv[sc->unit] = 0;
    331      1.27   msaitoh 	free(sc, M_DEVBUF);
    332      1.23  christos 	atomic_inc_uint(&srt_count);
    333      1.12    dyoung 	return 0;
    334       1.1     mouse }
    335       1.1     mouse 
    336       1.1     mouse struct if_clone srt_clone =
    337      1.27   msaitoh     IF_CLONE_INITIALIZER("srt", &srt_clone_create, &srt_clone_destroy);
    338       1.1     mouse 
    339      1.12    dyoung void
    340      1.23  christos srtattach(int n)
    341      1.23  christos {
    342      1.23  christos 
    343      1.23  christos 	/*
    344      1.23  christos 	 * Nothing to do here, initialization is handled by the
    345      1.23  christos 	 * module initialization code in srtinit() below).
    346      1.23  christos 	 */
    347      1.23  christos }
    348      1.23  christos 
    349      1.23  christos static void
    350      1.23  christos srtinit(void)
    351       1.1     mouse {
    352      1.12    dyoung 	int i;
    353       1.1     mouse 
    354      1.23  christos 	for (i = SRT_MAXUNIT; i >= 0; i--)
    355      1.12    dyoung 		softcv[i] = 0;
    356      1.12    dyoung 	global_flags = 0;
    357      1.12    dyoung 	if_clone_attach(&srt_clone);
    358      1.28  pgoyette #ifdef _MODULE
    359      1.28  pgoyette 	devsw_attach("srt", NULL, &srt_bmajor, &srt_cdevsw, &srt_cmajor);
    360      1.28  pgoyette #endif
    361       1.1     mouse }
    362       1.1     mouse 
    363      1.23  christos static int
    364      1.23  christos srtdetach(void)
    365      1.23  christos {
    366      1.23  christos 	int error = 0;
    367      1.23  christos 	int i;
    368      1.23  christos 
    369      1.28  pgoyette 	if_clone_detach(&srt_clone);
    370      1.28  pgoyette #ifdef _MODULE
    371      1.28  pgoyette 	devsw_detach(NULL, &srt_cdevsw);
    372      1.28  pgoyette 	if (error != 0) {
    373      1.28  pgoyette 		if_clone_attach(&srt_clone);
    374      1.28  pgoyette 		return error;
    375      1.28  pgoyette 	}
    376      1.28  pgoyette #endif
    377      1.28  pgoyette 
    378      1.23  christos 	for (i = SRT_MAXUNIT; i >= 0; i--)
    379      1.23  christos 		if(softcv[i]) {
    380      1.23  christos 			error = EBUSY;
    381      1.28  pgoyette #ifdef _MODULE
    382      1.28  pgoyette 			devsw_attach("srt", NULL, &srt_bmajor,
    383      1.28  pgoyette 			    &srt_cdevsw, &srt_cmajor);
    384      1.28  pgoyette #endif
    385      1.28  pgoyette 			if_clone_attach(&srt_clone);
    386      1.23  christos 			break;
    387      1.23  christos 		}
    388      1.23  christos 
    389      1.23  christos 	return error;
    390      1.23  christos }
    391      1.23  christos 
    392       1.1     mouse /* Special-device interface. */
    393       1.1     mouse 
    394      1.12    dyoung static int
    395      1.12    dyoung srt_open(dev_t dev, int flag, int mode, struct lwp *l)
    396       1.1     mouse {
    397      1.12    dyoung 	int unit;
    398      1.12    dyoung 	struct srt_softc *sc;
    399       1.1     mouse 
    400      1.12    dyoung 	unit = minor(dev);
    401      1.12    dyoung 	if (unit < 0 || unit > SRT_MAXUNIT)
    402      1.12    dyoung 		return ENXIO;
    403      1.12    dyoung 	sc = softcv[unit];
    404      1.29  pgoyette 	if (sc == NULL) {
    405      1.29  pgoyette 		(void)srt_clone_create(&srt_clone, minor(dev));
    406      1.29  pgoyette 		sc = softcv[unit];
    407      1.29  pgoyette 	}
    408      1.12    dyoung 	if (! sc)
    409      1.12    dyoung 		return ENXIO;
    410      1.12    dyoung 	sc->kflags |= SKF_CDEVOPEN;
    411      1.12    dyoung 	return 0;
    412       1.1     mouse }
    413       1.1     mouse 
    414      1.12    dyoung static int
    415      1.12    dyoung srt_close(dev_t dev, int flag, int mode, struct lwp *l)
    416       1.1     mouse {
    417      1.12    dyoung 	int unit;
    418      1.12    dyoung 	struct srt_softc *sc;
    419       1.1     mouse 
    420      1.12    dyoung 	unit = minor(dev);
    421      1.12    dyoung 	if (unit < 0 || unit > SRT_MAXUNIT)
    422      1.12    dyoung 		return ENXIO;
    423      1.12    dyoung 	sc = softcv[unit];
    424      1.12    dyoung 	if (! sc)
    425      1.12    dyoung 		return ENXIO;
    426      1.12    dyoung 	sc->kflags &= ~SKF_CDEVOPEN;
    427      1.12    dyoung 	return 0;
    428       1.1     mouse }
    429       1.1     mouse 
    430      1.12    dyoung static int
    431      1.12    dyoung srt_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    432       1.1     mouse {
    433      1.12    dyoung 	unsigned int f, i, n, o;
    434      1.12    dyoung 	struct srt_softc *sc;
    435      1.12    dyoung 	struct srt_rt *dr;
    436      1.12    dyoung 	struct srt_rt *scr;
    437      1.12    dyoung 	struct ifnet *ifp;
    438      1.12    dyoung 	char nbuf[IFNAMSIZ];
    439      1.12    dyoung 
    440      1.12    dyoung 	sc = softcv[minor(dev)];
    441      1.12    dyoung 	if (! sc)
    442      1.12    dyoung 		panic("srt_ioctl: softc disappeared");
    443      1.12    dyoung 	switch (cmd) {
    444      1.12    dyoung 	case SRT_GETNRT:
    445      1.12    dyoung 		if (! (flag & FREAD))
    446      1.12    dyoung 			return EBADF;
    447      1.12    dyoung 		*(unsigned int *)data = sc->nrt;
    448      1.12    dyoung 		return 0;
    449      1.12    dyoung 	case SRT_GETRT:
    450      1.12    dyoung 		if (! (flag & FREAD))
    451      1.12    dyoung 			return EBADF;
    452      1.12    dyoung 		dr = (struct srt_rt *) data;
    453      1.12    dyoung 		if (dr->inx >= sc->nrt)
    454      1.12    dyoung 			return EDOM;
    455      1.12    dyoung 		scr = sc->rts[dr->inx];
    456      1.12    dyoung 		dr->af = scr->af;
    457      1.12    dyoung 		dr->srcmatch = scr->srcmatch;
    458      1.12    dyoung 		dr->srcmask = scr->srcmask;
    459      1.27   msaitoh 		strlcpy(&dr->u.dstifn[0], &scr->u.dstifp->if_xname[0],
    460      1.27   msaitoh 		    IFNAMSIZ);
    461      1.27   msaitoh 		memcpy(&dr->dst, &scr->dst, scr->dst.sa.sa_len);
    462      1.12    dyoung 		return 0;
    463      1.12    dyoung 	case SRT_SETRT:
    464      1.12    dyoung 		if (! (flag & FWRITE))
    465      1.12    dyoung 			return EBADF;
    466      1.12    dyoung 		dr = (struct srt_rt *) data;
    467      1.12    dyoung 		if (dr->inx > sc->nrt)
    468      1.12    dyoung 			return EDOM;
    469      1.27   msaitoh 		strlcpy(&nbuf[0], &dr->u.dstifn[0], IFNAMSIZ);
    470      1.12    dyoung 		nbuf[IFNAMSIZ-1] = '\0';
    471      1.12    dyoung 		if (dr->dst.sa.sa_family != dr->af)
    472      1.12    dyoung 			return EIO;
    473      1.12    dyoung 		switch (dr->af) {
    474       1.1     mouse #ifdef INET
    475      1.12    dyoung 		case AF_INET:
    476      1.12    dyoung 			if (dr->dst.sa.sa_len != sizeof(dr->dst.sin))
    477      1.12    dyoung 				return EIO;
    478      1.12    dyoung 			if (dr->srcmask > 32)
    479      1.12    dyoung 				return EIO;
    480      1.12    dyoung 		break;
    481       1.1     mouse #endif
    482       1.1     mouse #ifdef INET6
    483      1.12    dyoung 		case AF_INET6:
    484      1.12    dyoung 			if (dr->dst.sa.sa_len != sizeof(dr->dst.sin6))
    485      1.12    dyoung 				return EIO;
    486      1.12    dyoung 			if (dr->srcmask > 128)
    487      1.12    dyoung 				return EIO;
    488      1.12    dyoung 		break;
    489       1.1     mouse #endif
    490      1.12    dyoung 		default:
    491      1.12    dyoung 			return EAFNOSUPPORT;
    492      1.12    dyoung 		}
    493      1.12    dyoung 		ifp = ifunit(&nbuf[0]);
    494      1.12    dyoung 		if (ifp == 0)
    495      1.12    dyoung 			return ENXIO; /* needs translation */
    496      1.12    dyoung 		if (dr->inx == sc->nrt) {
    497      1.12    dyoung 			struct srt_rt **tmp;
    498      1.12    dyoung 			tmp = malloc((sc->nrt+1)*sizeof(*tmp), M_DEVBUF,
    499      1.12    dyoung 			    M_WAITOK);
    500      1.12    dyoung 			if (tmp == 0)
    501      1.12    dyoung 				return ENOBUFS;
    502      1.12    dyoung 			tmp[sc->nrt] = 0;
    503      1.12    dyoung 			if (sc->nrt > 0) {
    504      1.12    dyoung 				memcpy(tmp, sc->rts, sc->nrt*sizeof(*tmp));
    505      1.12    dyoung 				free(sc->rts, M_DEVBUF);
    506      1.12    dyoung 			}
    507      1.12    dyoung 			sc->rts = tmp;
    508      1.12    dyoung 			sc->nrt ++;
    509      1.12    dyoung 		}
    510      1.12    dyoung 		scr = sc->rts[dr->inx];
    511      1.12    dyoung 		if (scr == 0) {
    512      1.27   msaitoh 			scr = malloc(sizeof(struct srt_rt), M_DEVBUF,M_WAITOK);
    513      1.12    dyoung 			if (scr == 0)
    514      1.12    dyoung 				return ENOBUFS;
    515      1.12    dyoung 			scr->inx = dr->inx;
    516      1.12    dyoung 			scr->af = AF_UNSPEC;
    517      1.12    dyoung 			sc->rts[dr->inx] = scr;
    518      1.12    dyoung 		}
    519      1.12    dyoung 		scr->af = dr->af;
    520      1.12    dyoung 		scr->srcmatch = dr->srcmatch;
    521      1.12    dyoung 		scr->srcmask = dr->srcmask;
    522      1.12    dyoung 		scr->u.dstifp = ifp;
    523      1.12    dyoung 		memcpy(&scr->dst,&dr->dst,dr->dst.sa.sa_len);
    524      1.25       kre 		if (dr->af == AF_INET6)
    525      1.25       kre 			in6_setzoneid(&scr->dst.sin6.sin6_addr, ifp->if_index);
    526      1.12    dyoung 		update_mtu(sc);
    527      1.12    dyoung 		return 0;
    528      1.12    dyoung 	case SRT_DELRT:
    529      1.12    dyoung 		if (! (flag & FWRITE))
    530      1.12    dyoung 			return EBADF;
    531      1.12    dyoung 		i = *(unsigned int *)data;
    532      1.12    dyoung 		if (i >= sc->nrt)
    533      1.12    dyoung 			return EDOM;
    534      1.12    dyoung 		scr = sc->rts[i];
    535      1.12    dyoung 		sc->rts[i] = 0;
    536      1.12    dyoung 		free(scr, M_DEVBUF);
    537      1.12    dyoung 		sc->nrt--;
    538      1.12    dyoung 		if (i < sc->nrt) {
    539      1.12    dyoung 			memcpy(sc->rts+i, sc->rts+i+1,
    540      1.27   msaitoh 			    (sc->nrt-i) * sizeof(*sc->rts));
    541      1.12    dyoung 		}
    542      1.12    dyoung 		if (sc->nrt == 0) {
    543      1.12    dyoung 			free(sc->rts, M_DEVBUF);
    544      1.12    dyoung 			sc->rts = 0;
    545      1.12    dyoung 			sc->intf.if_flags &= ~IFF_UP;
    546      1.12    dyoung 		}
    547      1.12    dyoung 		update_mtu(sc);
    548      1.12    dyoung 		return 0;
    549      1.12    dyoung 	case SRT_SFLAGS:
    550      1.12    dyoung 		if (! (flag & FWRITE))
    551      1.12    dyoung 			return EBADF;
    552      1.12    dyoung 		f = *(unsigned int *)data & SSF_UCHG;
    553      1.12    dyoung 		global_flags = (global_flags & ~SSF_UCHG) | (f & SSF_GLOBAL);
    554      1.12    dyoung 		sc->flags = (sc->flags & ~SSF_UCHG) | (f & ~SSF_GLOBAL);
    555      1.12    dyoung 		return 0;
    556      1.12    dyoung 	case SRT_GFLAGS:
    557      1.12    dyoung 		if (! (flag & FREAD))
    558      1.12    dyoung 			return EBADF;
    559      1.12    dyoung 		*(unsigned int *)data = sc->flags | global_flags;
    560      1.12    dyoung 		return 0;
    561      1.12    dyoung 	case SRT_SGFLAGS:
    562      1.27   msaitoh 		if ((flag & (FWRITE | FREAD)) != (FWRITE | FREAD))
    563      1.12    dyoung 			return EBADF;
    564      1.12    dyoung 		o = sc->flags | global_flags;
    565      1.12    dyoung 		n = *(unsigned int *)data & SSF_UCHG;
    566      1.12    dyoung 		global_flags = (global_flags & ~SSF_UCHG) | (n & SSF_GLOBAL);
    567      1.12    dyoung 		sc->flags = (sc->flags & ~SSF_UCHG) | (n & ~SSF_GLOBAL);
    568      1.12    dyoung 		*(unsigned int *)data = o;
    569      1.12    dyoung 		return 0;
    570      1.12    dyoung 	case SRT_DEBUG:
    571      1.12    dyoung 		return 0;
    572      1.12    dyoung 		break;
    573      1.12    dyoung 	}
    574      1.12    dyoung 	return ENOTTY;
    575       1.1     mouse }
    576       1.1     mouse 
    577      1.23  christos /*
    578      1.23  christos  * Module infrastructure
    579      1.23  christos  */
    580      1.23  christos #include "if_module.h"
    581      1.23  christos 
    582      1.30  pgoyette IF_MODULE(MODULE_CLASS_DRIVER, srt, NULL)
    583