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