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