Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.46
      1 /*	$NetBSD: rtsock.c,v 1.46 2001/06/04 01:30:11 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1988, 1991, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
     65  */
     66 
     67 #include "opt_inet.h"
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/proc.h>
     72 #include <sys/mbuf.h>
     73 #include <sys/socket.h>
     74 #include <sys/socketvar.h>
     75 #include <sys/domain.h>
     76 #include <sys/protosw.h>
     77 
     78 #include <uvm/uvm_extern.h>
     79 
     80 #include <sys/sysctl.h>
     81 
     82 #include <net/if.h>
     83 #include <net/route.h>
     84 #include <net/raw_cb.h>
     85 
     86 #include <machine/stdarg.h>
     87 
     88 struct	sockaddr route_dst = { 2, PF_ROUTE, };
     89 struct	sockaddr route_src = { 2, PF_ROUTE, };
     90 struct	sockproto route_proto = { PF_ROUTE, };
     91 
     92 struct walkarg {
     93 	int	w_op;
     94 	int	w_arg;
     95 	int	w_given;
     96 	int	w_needed;
     97 	caddr_t	w_where;
     98 	int	w_tmemsize;
     99 	int	w_tmemneeded;
    100 	caddr_t	w_tmem;
    101 };
    102 
    103 static struct mbuf *rt_msg1 __P((int, struct rt_addrinfo *, caddr_t, int));
    104 static int rt_msg2 __P((int, struct rt_addrinfo *, caddr_t, struct walkarg *,
    105     int *));
    106 static int rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
    107 static int sysctl_dumpentry __P((struct radix_node *, void *));
    108 static int sysctl_iflist __P((int, struct walkarg *, int));
    109 static int sysctl_rtable __P((int *, u_int, void *, size_t *, void *, size_t));
    110 static __inline void rt_adjustcount __P((int, int));
    111 
    112 /* Sleazy use of local variables throughout file, warning!!!! */
    113 #define dst	info.rti_info[RTAX_DST]
    114 #define gate	info.rti_info[RTAX_GATEWAY]
    115 #define netmask	info.rti_info[RTAX_NETMASK]
    116 #define genmask	info.rti_info[RTAX_GENMASK]
    117 #define ifpaddr	info.rti_info[RTAX_IFP]
    118 #define ifaaddr	info.rti_info[RTAX_IFA]
    119 #define brdaddr	info.rti_info[RTAX_BRD]
    120 
    121 static __inline void
    122 rt_adjustcount(af, cnt)
    123 	int af, cnt;
    124 {
    125 	route_cb.any_count += cnt;
    126 	switch (af) {
    127 	case AF_INET:
    128 		route_cb.ip_count += cnt;
    129 		return;
    130 #ifdef INET6
    131 	case AF_INET6:
    132 		route_cb.ip6_count += cnt;
    133 		return;
    134 #endif
    135 	case AF_IPX:
    136 		route_cb.ipx_count += cnt;
    137 		return;
    138 	case AF_NS:
    139 		route_cb.ns_count += cnt;
    140 		return;
    141 	case AF_ISO:
    142 		route_cb.iso_count += cnt;
    143 		return;
    144 	}
    145 }
    146 
    147 /*ARGSUSED*/
    148 int
    149 route_usrreq(so, req, m, nam, control, p)
    150 	struct socket *so;
    151 	int req;
    152 	struct mbuf *m, *nam, *control;
    153 	struct proc *p;
    154 {
    155 	int error = 0;
    156 	struct rawcb *rp = sotorawcb(so);
    157 	int s;
    158 
    159 	if (req == PRU_ATTACH) {
    160 		MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
    161 		if ((so->so_pcb = rp) != NULL)
    162 			bzero(so->so_pcb, sizeof(*rp));
    163 
    164 	}
    165 	if (req == PRU_DETACH && rp)
    166 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    167 	s = splsoftnet();
    168 
    169 	/*
    170 	 * Don't call raw_usrreq() in the attach case, because
    171 	 * we want to allow non-privileged processes to listen on
    172 	 * and send "safe" commands to the routing socket.
    173 	 */
    174 	if (req == PRU_ATTACH) {
    175 		if (p == 0)
    176 			error = EACCES;
    177 		else
    178 			error = raw_attach(so, (int)(long)nam);
    179 	} else
    180 		error = raw_usrreq(so, req, m, nam, control, p);
    181 
    182 	rp = sotorawcb(so);
    183 	if (req == PRU_ATTACH && rp) {
    184 		if (error) {
    185 			free((caddr_t)rp, M_PCB);
    186 			splx(s);
    187 			return (error);
    188 		}
    189 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    190 		rp->rcb_laddr = &route_src;
    191 		rp->rcb_faddr = &route_dst;
    192 		soisconnected(so);
    193 		so->so_options |= SO_USELOOPBACK;
    194 	}
    195 	splx(s);
    196 	return (error);
    197 }
    198 
    199 /*ARGSUSED*/
    200 int
    201 #if __STDC__
    202 route_output(struct mbuf *m, ...)
    203 #else
    204 route_output(m, va_alist)
    205 	struct mbuf *m;
    206 	va_dcl
    207 #endif
    208 {
    209 	struct rt_msghdr *rtm = 0;
    210 	struct radix_node *rn = 0;
    211 	struct rtentry *rt = 0;
    212 	struct rtentry *saved_nrt = 0;
    213 	struct radix_node_head *rnh;
    214 	struct rt_addrinfo info;
    215 	int len, error = 0;
    216 	struct ifnet *ifp = 0;
    217 	struct ifaddr *ifa = 0;
    218 	struct socket *so;
    219 	va_list ap;
    220 
    221 	va_start(ap, m);
    222 	so = va_arg(ap, struct socket *);
    223 	va_end(ap);
    224 
    225 #define senderr(e) do { error = e; goto flush;} while (0)
    226 	if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
    227 	   (m = m_pullup(m, sizeof(int32_t))) == 0))
    228 		return (ENOBUFS);
    229 	if ((m->m_flags & M_PKTHDR) == 0)
    230 		panic("route_output");
    231 	len = m->m_pkthdr.len;
    232 	if (len < sizeof(*rtm) ||
    233 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
    234 		dst = 0;
    235 		senderr(EINVAL);
    236 	}
    237 	R_Malloc(rtm, struct rt_msghdr *, len);
    238 	if (rtm == 0) {
    239 		dst = 0;
    240 		senderr(ENOBUFS);
    241 	}
    242 	m_copydata(m, 0, len, (caddr_t)rtm);
    243 	if (rtm->rtm_version != RTM_VERSION) {
    244 		dst = 0;
    245 		senderr(EPROTONOSUPPORT);
    246 	}
    247 	rtm->rtm_pid = curproc->p_pid;
    248 	bzero(&info, sizeof(info));
    249 	info.rti_addrs = rtm->rtm_addrs;
    250 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
    251 		senderr(EINVAL);
    252 	info.rti_flags = rtm->rtm_flags;
    253 	if (dst == 0 || (dst->sa_family >= AF_MAX))
    254 		senderr(EINVAL);
    255 	if (gate != 0 && (gate->sa_family >= AF_MAX))
    256 		senderr(EINVAL);
    257 	if (genmask) {
    258 		struct radix_node *t;
    259 		t = rn_addmask((caddr_t)genmask, 0, 1);
    260 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
    261 			genmask = (struct sockaddr *)(t->rn_key);
    262 		else
    263 			senderr(ENOBUFS);
    264 	}
    265 
    266 	/*
    267 	 * Verify that the caller has the appropriate privilege; RTM_GET
    268 	 * is the only operation the non-superuser is allowed.
    269 	 */
    270 	if (rtm->rtm_type != RTM_GET &&
    271 	    suser(curproc->p_ucred, &curproc->p_acflag) != 0)
    272 		senderr(EACCES);
    273 
    274 	switch (rtm->rtm_type) {
    275 
    276 	case RTM_ADD:
    277 		if (gate == 0)
    278 			senderr(EINVAL);
    279 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    280 		if (error == 0 && saved_nrt) {
    281 			rt_setmetrics(rtm->rtm_inits,
    282 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
    283 			saved_nrt->rt_refcnt--;
    284 			saved_nrt->rt_genmask = genmask;
    285 		}
    286 		break;
    287 
    288 	case RTM_DELETE:
    289 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    290 		if (error == 0) {
    291 			(rt = saved_nrt)->rt_refcnt++;
    292 			goto report;
    293 		}
    294 		break;
    295 
    296 	case RTM_GET:
    297 	case RTM_CHANGE:
    298 	case RTM_LOCK:
    299 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
    300 			senderr(EAFNOSUPPORT);
    301 		}
    302 		rn = rnh->rnh_lookup(dst, netmask, rnh);
    303 		if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0) {
    304 			senderr(ESRCH);
    305 		}
    306 		rt = (struct rtentry *)rn;
    307 		rt->rt_refcnt++;
    308 
    309 		switch(rtm->rtm_type) {
    310 
    311 		case RTM_GET:
    312 		report:
    313 			dst = rt_key(rt);
    314 			gate = rt->rt_gateway;
    315 			netmask = rt_mask(rt);
    316 			genmask = rt->rt_genmask;
    317 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
    318 				if ((ifp = rt->rt_ifp) != NULL) {
    319 					ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    320 					ifaaddr = rt->rt_ifa->ifa_addr;
    321 					if (ifp->if_flags & IFF_POINTOPOINT)
    322 						brdaddr = rt->rt_ifa->ifa_dstaddr;
    323 					else
    324 						brdaddr = 0;
    325 					rtm->rtm_index = ifp->if_index;
    326 				} else {
    327 					ifpaddr = 0;
    328 					ifaaddr = 0;
    329 				}
    330 			}
    331 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
    332 			    (struct walkarg *)0, &len);
    333 			if (len > rtm->rtm_msglen) {
    334 				struct rt_msghdr *new_rtm;
    335 				R_Malloc(new_rtm, struct rt_msghdr *, len);
    336 				if (new_rtm == 0)
    337 					senderr(ENOBUFS);
    338 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
    339 				Free(rtm); rtm = new_rtm;
    340 			}
    341 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
    342 			    (struct walkarg *)0, 0);
    343 			rtm->rtm_flags = rt->rt_flags;
    344 			rtm->rtm_rmx = rt->rt_rmx;
    345 			rtm->rtm_addrs = info.rti_addrs;
    346 			break;
    347 
    348 		case RTM_CHANGE:
    349 			/*
    350 			 * new gateway could require new ifaddr, ifp;
    351 			 * flags may also be different; ifp may be specified
    352 			 * by ll sockaddr when protocol address is ambiguous
    353 			 */
    354 			if ((error = rt_getifa(&info)) != 0)
    355 				senderr(error);
    356 			if (gate && rt_setgate(rt, rt_key(rt), gate))
    357 				senderr(EDQUOT);
    358 			/* new gateway could require new ifaddr, ifp;
    359 			   flags may also be different; ifp may be specified
    360 			   by ll sockaddr when protocol address is ambiguous */
    361 			if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
    362 			    (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
    363 				ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
    364 				    ifp);
    365 			else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
    366 			    (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
    367 			    rt_key(rt), gate))))
    368 				ifp = ifa->ifa_ifp;
    369 			if (ifa) {
    370 				struct ifaddr *oifa = rt->rt_ifa;
    371 				if (oifa != ifa) {
    372 				    if (oifa && oifa->ifa_rtrequest)
    373 					oifa->ifa_rtrequest(RTM_DELETE, rt,
    374 					    &info);
    375 				    IFAFREE(rt->rt_ifa);
    376 				    rt->rt_ifa = ifa;
    377 				    IFAREF(rt->rt_ifa);
    378 				    rt->rt_ifp = ifp;
    379 				}
    380 			}
    381 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
    382 			    &rt->rt_rmx);
    383 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    384 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
    385 			if (genmask)
    386 				rt->rt_genmask = genmask;
    387 			/*
    388 			 * Fall into
    389 			 */
    390 		case RTM_LOCK:
    391 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    392 			rt->rt_rmx.rmx_locks |=
    393 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    394 			break;
    395 		}
    396 		break;
    397 
    398 	default:
    399 		senderr(EOPNOTSUPP);
    400 	}
    401 
    402 flush:
    403 	if (rtm) {
    404 		if (error)
    405 			rtm->rtm_errno = error;
    406 		else
    407 			rtm->rtm_flags |= RTF_DONE;
    408 	}
    409 	if (rt)
    410 		rtfree(rt);
    411     {
    412 	struct rawcb *rp = 0;
    413 	/*
    414 	 * Check to see if we don't want our own messages.
    415 	 */
    416 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    417 		if (route_cb.any_count <= 1) {
    418 			if (rtm)
    419 				Free(rtm);
    420 			m_freem(m);
    421 			return (error);
    422 		}
    423 		/* There is another listener, so construct message */
    424 		rp = sotorawcb(so);
    425 	}
    426 	/*
    427 	 * copy rtm into m.
    428 	 * XXX is it okay if we omit responses when we are unable to reply?
    429 	 */
    430 	if (rtm) {
    431 		if (rtm->rtm_msglen > MCLBYTES) {
    432 			m_freem(m);
    433 			m = NULL;
    434 		} else if (m->m_pkthdr.len > rtm->rtm_msglen) {
    435 			m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    436 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    437 		} else {
    438 			m_freem(m);
    439 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    440 			if (m && rtm->rtm_msglen > MHLEN) {
    441 				MCLGET(m, M_DONTWAIT);
    442 				if ((m->m_flags & M_EXT) == 0) {
    443 					m_free(m);
    444 					m = NULL;
    445 				}
    446 			}
    447 			if (m) {
    448 				m->m_pkthdr.len = m->m_len = rtm->rtm_msglen;
    449 				m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    450 			}
    451 		}
    452 		Free(rtm);
    453 	} else {
    454 		m_freem(m);
    455 		m = NULL;
    456 	}
    457 	if (rp)
    458 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    459 	if (dst)
    460 		route_proto.sp_protocol = dst->sa_family;
    461 	if (m)
    462 		raw_input(m, &route_proto, &route_src, &route_dst);
    463 	if (rp)
    464 		rp->rcb_proto.sp_family = PF_ROUTE;
    465     }
    466 	return (error);
    467 }
    468 
    469 void
    470 rt_setmetrics(which, in, out)
    471 	u_long which;
    472 	struct rt_metrics *in, *out;
    473 {
    474 #define metric(f, e) if (which & (f)) out->e = in->e;
    475 	metric(RTV_RPIPE, rmx_recvpipe);
    476 	metric(RTV_SPIPE, rmx_sendpipe);
    477 	metric(RTV_SSTHRESH, rmx_ssthresh);
    478 	metric(RTV_RTT, rmx_rtt);
    479 	metric(RTV_RTTVAR, rmx_rttvar);
    480 	metric(RTV_HOPCOUNT, rmx_hopcount);
    481 	metric(RTV_MTU, rmx_mtu);
    482 	metric(RTV_EXPIRE, rmx_expire);
    483 #undef metric
    484 }
    485 
    486 #define ROUNDUP(a) \
    487 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    488 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    489 
    490 static int
    491 rt_xaddrs(cp, cplim, rtinfo)
    492 	caddr_t cp, cplim;
    493 	struct rt_addrinfo *rtinfo;
    494 {
    495 	struct sockaddr *sa;
    496 	int i;
    497 
    498 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
    499 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
    500 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    501 			continue;
    502 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
    503 		ADVANCE(cp, sa);
    504 	}
    505 
    506 	/* Check for extra addresses specified.  */
    507 	if ((rtinfo->rti_addrs & (~0 << i)) != 0)
    508 		return (1);
    509 	/* Check for bad data length.  */
    510 	if (cp != cplim) {
    511 		if (i == RTAX_NETMASK + 1 &&
    512 		    cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
    513 			/*
    514 			 * The last sockaddr was netmask.
    515 			 * We accept this for now for the sake of old
    516 			 * binaries or third party softwares.
    517 			 */
    518 			;
    519 		else
    520 			return (1);
    521 	}
    522 	return (0);
    523 }
    524 
    525 static struct mbuf *
    526 rt_msg1(type, rtinfo, data, datalen)
    527 	int type;
    528 	struct rt_addrinfo *rtinfo;
    529 	caddr_t data;
    530 	int datalen;
    531 {
    532 	struct rt_msghdr *rtm;
    533 	struct mbuf *m;
    534 	int i;
    535 	struct sockaddr *sa;
    536 	int hlen, alen, len, dlen;
    537 
    538 	/* pre-compute length */
    539 	switch (type) {
    540 	case RTM_DELADDR:
    541 	case RTM_NEWADDR:
    542 		hlen = sizeof(struct ifa_msghdr);
    543 		break;
    544 
    545 #ifdef COMPAT_14
    546 	case RTM_OIFINFO:
    547 		hlen = sizeof(struct if_msghdr14);
    548 		break;
    549 #endif
    550 
    551 	case RTM_IFINFO:
    552 		hlen = sizeof(struct if_msghdr);
    553 		break;
    554 
    555 	case RTM_IFANNOUNCE:
    556 		hlen = sizeof(struct if_announcemsghdr);
    557 		break;
    558 
    559 	default:
    560 		hlen = sizeof(struct rt_msghdr);
    561 		break;
    562 	}
    563 	alen = 0;
    564 	for (i = 0; i < RTAX_MAX; i++) {
    565 		if ((sa = rtinfo->rti_info[i]) == NULL)
    566 			continue;
    567 		dlen = ROUNDUP(sa->sa_len);
    568 		alen += dlen;
    569 	}
    570 
    571 	if (hlen > MHLEN || alen > MCLBYTES)
    572 		panic("rt_msg1: message too long");
    573 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    574 	if (!m)
    575 		return (NULL);
    576 	m->m_pkthdr.len = hlen;
    577 	m->m_len = hlen;
    578 	if (hlen + alen > MHLEN) {
    579 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
    580 		if (m->m_next && alen > MLEN) {
    581 			MCLGET(m->m_next, M_DONTWAIT);
    582 			if ((m->m_next->m_flags & M_EXT) == 0) {
    583 				m_freem(m->m_next);
    584 				m->m_next = NULL;
    585 			}
    586 		}
    587 		if (!m->m_next) {
    588 			m_freem(m);
    589 			return (NULL);
    590 		}
    591 		m->m_pkthdr.len += alen;
    592 		m->m_next->m_len = alen;
    593 	} else  {
    594 		m->m_pkthdr.len += alen;
    595 		m->m_len += alen;
    596 	}
    597 	m->m_pkthdr.rcvif = NULL;
    598 	m_copyback(m, 0, datalen, data);
    599 	rtm = mtod(m, struct rt_msghdr *);
    600 	len = hlen;
    601 	for (i = 0; i < RTAX_MAX; i++) {
    602 		if ((sa = rtinfo->rti_info[i]) == NULL)
    603 			continue;
    604 		rtinfo->rti_addrs |= (1 << i);
    605 		dlen = ROUNDUP(sa->sa_len);
    606 		m_copyback(m, len, dlen, (caddr_t)sa);
    607 		len += dlen;
    608 	}
    609 	rtm->rtm_msglen = len;
    610 	rtm->rtm_version = RTM_VERSION;
    611 	rtm->rtm_type = type;
    612 	return (m);
    613 }
    614 
    615 /*
    616  * rt_msg2
    617  *
    618  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    619  *		returns the length of the message in 'lenp'.
    620  *
    621  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    622  *	the message
    623  * otherwise walkarg's w_needed is updated and if the user buffer is
    624  *	specified and w_needed indicates space exists the information is copied
    625  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    626  *	if the allocation fails ENOBUFS is returned.
    627  */
    628 static int
    629 rt_msg2(type, rtinfo, cp, w, lenp)
    630 	int type;
    631 	struct rt_addrinfo *rtinfo;
    632 	caddr_t cp;
    633 	struct walkarg *w;
    634 	int *lenp;
    635 {
    636 	int i;
    637 	int len, dlen, second_time = 0;
    638 	caddr_t cp0;
    639 
    640 	rtinfo->rti_addrs = 0;
    641 again:
    642 	switch (type) {
    643 
    644 	case RTM_DELADDR:
    645 	case RTM_NEWADDR:
    646 		len = sizeof(struct ifa_msghdr);
    647 		break;
    648 #ifdef COMPAT_14
    649 	case RTM_OIFINFO:
    650 		len = sizeof(struct if_msghdr14);
    651 		break;
    652 #endif
    653 
    654 	case RTM_IFINFO:
    655 		len = sizeof(struct if_msghdr);
    656 		break;
    657 
    658 	default:
    659 		len = sizeof(struct rt_msghdr);
    660 	}
    661 	if ((cp0 = cp) != NULL)
    662 		cp += len;
    663 	for (i = 0; i < RTAX_MAX; i++) {
    664 		struct sockaddr *sa;
    665 
    666 		if ((sa = rtinfo->rti_info[i]) == 0)
    667 			continue;
    668 		rtinfo->rti_addrs |= (1 << i);
    669 		dlen = ROUNDUP(sa->sa_len);
    670 		if (cp) {
    671 			bcopy(sa, cp, (unsigned)dlen);
    672 			cp += dlen;
    673 		}
    674 		len += dlen;
    675 	}
    676 	if (cp == 0 && w != NULL && !second_time) {
    677 		struct walkarg *rw = w;
    678 
    679 		rw->w_needed += len;
    680 		if (rw->w_needed <= 0 && rw->w_where) {
    681 			if (rw->w_tmemsize < len) {
    682 				if (rw->w_tmem)
    683 					free(rw->w_tmem, M_RTABLE);
    684 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
    685 				    M_NOWAIT);
    686 				if (rw->w_tmem)
    687 					rw->w_tmemsize = len;
    688 			}
    689 			if (rw->w_tmem) {
    690 				cp = rw->w_tmem;
    691 				second_time = 1;
    692 				goto again;
    693 			} else {
    694 				rw->w_tmemneeded = len;
    695 				return (ENOBUFS);
    696 			}
    697 		}
    698 	}
    699 	if (cp) {
    700 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    701 
    702 		rtm->rtm_version = RTM_VERSION;
    703 		rtm->rtm_type = type;
    704 		rtm->rtm_msglen = len;
    705 	}
    706 	if (lenp)
    707 		*lenp = len;
    708 	return (0);
    709 }
    710 
    711 /*
    712  * This routine is called to generate a message from the routing
    713  * socket indicating that a redirect has occured, a routing lookup
    714  * has failed, or that a protocol has detected timeouts to a particular
    715  * destination.
    716  */
    717 void
    718 rt_missmsg(type, rtinfo, flags, error)
    719 	int type, flags, error;
    720 	struct rt_addrinfo *rtinfo;
    721 {
    722 	struct rt_msghdr rtm;
    723 	struct mbuf *m;
    724 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    725 
    726 	if (route_cb.any_count == 0)
    727 		return;
    728 	bzero(&rtm, sizeof(rtm));
    729 	rtm.rtm_flags = RTF_DONE | flags;
    730 	rtm.rtm_errno = error;
    731 	m = rt_msg1(type, rtinfo, (caddr_t)&rtm, sizeof(rtm));
    732 	if (m == 0)
    733 		return;
    734 	mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
    735 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
    736 	raw_input(m, &route_proto, &route_src, &route_dst);
    737 }
    738 
    739 /*
    740  * This routine is called to generate a message from the routing
    741  * socket indicating that the status of a network interface has changed.
    742  */
    743 void
    744 rt_ifmsg(ifp)
    745 	struct ifnet *ifp;
    746 {
    747 	struct if_msghdr ifm;
    748 #ifdef COMPAT_14
    749 	struct if_msghdr14 oifm;
    750 #endif
    751 	struct mbuf *m;
    752 	struct rt_addrinfo info;
    753 
    754 	if (route_cb.any_count == 0)
    755 		return;
    756 	bzero(&info, sizeof(info));
    757 	bzero(&ifm, sizeof(ifm));
    758 	ifm.ifm_index = ifp->if_index;
    759 	ifm.ifm_flags = ifp->if_flags;
    760 	ifm.ifm_data = ifp->if_data;
    761 	ifm.ifm_addrs = 0;
    762 	m = rt_msg1(RTM_IFINFO, &info, (caddr_t)&ifm, sizeof(ifm));
    763 	if (m == 0)
    764 		return;
    765 	route_proto.sp_protocol = 0;
    766 	raw_input(m, &route_proto, &route_src, &route_dst);
    767 #ifdef COMPAT_14
    768 	bzero(&info, sizeof(info));
    769 	bzero(&oifm, sizeof(oifm));
    770 	oifm.ifm_index = ifp->if_index;
    771 	oifm.ifm_flags = ifp->if_flags;
    772 	oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
    773 	oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
    774 	oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
    775 	oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
    776 	oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
    777 	oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
    778 	oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
    779 	oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
    780 	oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
    781 	oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
    782 	oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
    783 	oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
    784 	oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
    785 	oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
    786 	oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
    787 	oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
    788 	oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
    789 	oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
    790 	oifm.ifm_addrs = 0;
    791 	m = rt_msg1(RTM_OIFINFO, &info, (caddr_t)&oifm, sizeof(oifm));
    792 	if (m == 0)
    793 		return;
    794 	route_proto.sp_protocol = 0;
    795 	raw_input(m, &route_proto, &route_src, &route_dst);
    796 #endif
    797 }
    798 
    799 /*
    800  * This is called to generate messages from the routing socket
    801  * indicating a network interface has had addresses associated with it.
    802  * if we ever reverse the logic and replace messages TO the routing
    803  * socket indicate a request to configure interfaces, then it will
    804  * be unnecessary as the routing socket will automatically generate
    805  * copies of it.
    806  */
    807 void
    808 rt_newaddrmsg(cmd, ifa, error, rt)
    809 	int cmd, error;
    810 	struct ifaddr *ifa;
    811 	struct rtentry *rt;
    812 {
    813 	struct rt_addrinfo info;
    814 	struct sockaddr *sa = NULL;
    815 	int pass;
    816 	struct mbuf *m = NULL;
    817 	struct ifnet *ifp = ifa->ifa_ifp;
    818 
    819 	if (route_cb.any_count == 0)
    820 		return;
    821 	for (pass = 1; pass < 3; pass++) {
    822 		bzero(&info, sizeof(info));
    823 		if ((cmd == RTM_ADD && pass == 1) ||
    824 		    (cmd == RTM_DELETE && pass == 2)) {
    825 			struct ifa_msghdr ifam;
    826 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
    827 
    828 			ifaaddr = sa = ifa->ifa_addr;
    829 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    830 			netmask = ifa->ifa_netmask;
    831 			brdaddr = ifa->ifa_dstaddr;
    832 			bzero(&ifam, sizeof(ifam));
    833 			ifam.ifam_index = ifp->if_index;
    834 			ifam.ifam_metric = ifa->ifa_metric;
    835 			ifam.ifam_flags = ifa->ifa_flags;
    836 			m = rt_msg1(ncmd, &info, (caddr_t)&ifam, sizeof(ifam));
    837 			if (m == NULL)
    838 				continue;
    839 			mtod(m, struct ifa_msghdr *)->ifam_addrs =
    840 			    info.rti_addrs;
    841 		}
    842 		if ((cmd == RTM_ADD && pass == 2) ||
    843 		    (cmd == RTM_DELETE && pass == 1)) {
    844 			struct rt_msghdr rtm;
    845 
    846 			if (rt == 0)
    847 				continue;
    848 			netmask = rt_mask(rt);
    849 			dst = sa = rt_key(rt);
    850 			gate = rt->rt_gateway;
    851 			bzero(&rtm, sizeof(rtm));
    852 			rtm.rtm_index = ifp->if_index;
    853 			rtm.rtm_flags |= rt->rt_flags;
    854 			rtm.rtm_errno = error;
    855 			m = rt_msg1(cmd, &info, (caddr_t)&rtm, sizeof(rtm));
    856 			if (m == NULL)
    857 				continue;
    858 			mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
    859 		}
    860 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
    861 		raw_input(m, &route_proto, &route_src, &route_dst);
    862 	}
    863 }
    864 
    865 /*
    866  * This is called to generate routing socket messages indicating
    867  * network interface arrival and departure.
    868  */
    869 void
    870 rt_ifannouncemsg(ifp, what)
    871 	struct ifnet *ifp;
    872 	int what;
    873 {
    874 	struct if_announcemsghdr ifan;
    875 	struct mbuf *m;
    876 	struct rt_addrinfo info;
    877 
    878 	if (route_cb.any_count == 0)
    879 		return;
    880 	bzero(&info, sizeof(info));
    881 	bzero(&ifan, sizeof(ifan));
    882 	ifan.ifan_index = ifp->if_index;
    883 	strcpy(ifan.ifan_name, ifp->if_xname);
    884 	ifan.ifan_what = what;
    885 	m = rt_msg1(RTM_IFANNOUNCE, &info, (caddr_t)&ifan, sizeof(ifan));
    886 	if (m == 0)
    887 		return;
    888 	route_proto.sp_protocol = 0;
    889 	raw_input(m, &route_proto, &route_src, &route_dst);
    890 }
    891 
    892 /*
    893  * This is used in dumping the kernel table via sysctl().
    894  */
    895 static int
    896 sysctl_dumpentry(rn, v)
    897 	struct radix_node *rn;
    898 	void *v;
    899 {
    900 	struct walkarg *w = v;
    901 	struct rtentry *rt = (struct rtentry *)rn;
    902 	int error = 0, size;
    903 	struct rt_addrinfo info;
    904 
    905 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    906 		return 0;
    907 	bzero(&info, sizeof(info));
    908 	dst = rt_key(rt);
    909 	gate = rt->rt_gateway;
    910 	netmask = rt_mask(rt);
    911 	genmask = rt->rt_genmask;
    912 	if (rt->rt_ifp) {
    913 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
    914 		ifaaddr = rt->rt_ifa->ifa_addr;
    915 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    916 			brdaddr = rt->rt_ifa->ifa_dstaddr;
    917 	}
    918 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    919 		return (error);
    920 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    921 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
    922 
    923 		rtm->rtm_flags = rt->rt_flags;
    924 		rtm->rtm_use = rt->rt_use;
    925 		rtm->rtm_rmx = rt->rt_rmx;
    926 		rtm->rtm_index = rt->rt_ifp->if_index;
    927 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    928 		rtm->rtm_addrs = info.rti_addrs;
    929 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    930 			w->w_where = NULL;
    931 		else
    932 			w->w_where += size;
    933 	}
    934 	return (error);
    935 }
    936 
    937 static int
    938 sysctl_iflist(af, w, type)
    939 	int	af;
    940 	struct	walkarg *w;
    941 	int type;
    942 {
    943 	struct ifnet *ifp;
    944 	struct ifaddr *ifa;
    945 	struct	rt_addrinfo info;
    946 	int	len, error = 0;
    947 
    948 	bzero(&info, sizeof(info));
    949 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
    950 		if (w->w_arg && w->w_arg != ifp->if_index)
    951 			continue;
    952 		ifa = ifp->if_addrlist.tqh_first;
    953 		ifpaddr = ifa->ifa_addr;
    954 		switch(type) {
    955 		case NET_RT_IFLIST:
    956 			error =
    957 			    rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len);
    958 			break;
    959 #ifdef COMPAT_14
    960 		case NET_RT_OIFLIST:
    961 			error =
    962 			    rt_msg2(RTM_OIFINFO, &info, (caddr_t)0, w, &len);
    963 			break;
    964 #endif
    965 		default:
    966 			panic("sysctl_iflist(1)");
    967 		}
    968 		if (error)
    969 			return (error);
    970 		ifpaddr = 0;
    971 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    972 			switch(type) {
    973 			case NET_RT_IFLIST: {
    974 				struct if_msghdr *ifm;
    975 
    976 				ifm = (struct if_msghdr *)w->w_tmem;
    977 				ifm->ifm_index = ifp->if_index;
    978 				ifm->ifm_flags = ifp->if_flags;
    979 				ifm->ifm_data = ifp->if_data;
    980 				ifm->ifm_addrs = info.rti_addrs;
    981 				error = copyout(ifm, w->w_where, len);
    982 				if (error)
    983 					return (error);
    984 				w->w_where += len;
    985 				break;
    986 			}
    987 
    988 #ifdef COMPAT_14
    989 			case NET_RT_OIFLIST: {
    990 				struct if_msghdr14 *ifm;
    991 
    992 				ifm = (struct if_msghdr14 *)w->w_tmem;
    993 				ifm->ifm_index = ifp->if_index;
    994 				ifm->ifm_flags = ifp->if_flags;
    995 				ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
    996 				ifm->ifm_data.ifi_addrlen =
    997 				    ifp->if_data.ifi_addrlen;
    998 				ifm->ifm_data.ifi_hdrlen =
    999 				    ifp->if_data.ifi_hdrlen;
   1000 				ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
   1001 				ifm->ifm_data.ifi_metric =
   1002 				    ifp->if_data.ifi_metric;
   1003 				ifm->ifm_data.ifi_baudrate =
   1004 				    ifp->if_data.ifi_baudrate;
   1005 				ifm->ifm_data.ifi_ipackets =
   1006 				    ifp->if_data.ifi_ipackets;
   1007 				ifm->ifm_data.ifi_ierrors =
   1008 				    ifp->if_data.ifi_ierrors;
   1009 				ifm->ifm_data.ifi_opackets =
   1010 				    ifp->if_data.ifi_opackets;
   1011 				ifm->ifm_data.ifi_oerrors =
   1012 				    ifp->if_data.ifi_oerrors;
   1013 				ifm->ifm_data.ifi_collisions =
   1014 				    ifp->if_data.ifi_collisions;
   1015 				ifm->ifm_data.ifi_ibytes =
   1016 				    ifp->if_data.ifi_ibytes;
   1017 				ifm->ifm_data.ifi_obytes =
   1018 				    ifp->if_data.ifi_obytes;
   1019 				ifm->ifm_data.ifi_imcasts =
   1020 				    ifp->if_data.ifi_imcasts;
   1021 				ifm->ifm_data.ifi_omcasts =
   1022 				    ifp->if_data.ifi_omcasts;
   1023 				ifm->ifm_data.ifi_iqdrops =
   1024 				    ifp->if_data.ifi_iqdrops;
   1025 				ifm->ifm_data.ifi_noproto =
   1026 				    ifp->if_data.ifi_noproto;
   1027 				ifm->ifm_data.ifi_lastchange =
   1028 				    ifp->if_data.ifi_lastchange;
   1029 				ifm->ifm_addrs = info.rti_addrs;
   1030 				error = copyout(ifm, w->w_where, len);
   1031 				if (error)
   1032 					return (error);
   1033 				w->w_where += len;
   1034 				break;
   1035 			}
   1036 #endif
   1037 			default:
   1038 				panic("sysctl_iflist(2)");
   1039 			}
   1040 		}
   1041 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
   1042 			if (af && af != ifa->ifa_addr->sa_family)
   1043 				continue;
   1044 			ifaaddr = ifa->ifa_addr;
   1045 			netmask = ifa->ifa_netmask;
   1046 			brdaddr = ifa->ifa_dstaddr;
   1047 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
   1048 				return (error);
   1049 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1050 				struct ifa_msghdr *ifam;
   1051 
   1052 				ifam = (struct ifa_msghdr *)w->w_tmem;
   1053 				ifam->ifam_index = ifa->ifa_ifp->if_index;
   1054 				ifam->ifam_flags = ifa->ifa_flags;
   1055 				ifam->ifam_metric = ifa->ifa_metric;
   1056 				ifam->ifam_addrs = info.rti_addrs;
   1057 				error = copyout(w->w_tmem, w->w_where, len);
   1058 				if (error)
   1059 					return (error);
   1060 				w->w_where += len;
   1061 			}
   1062 		}
   1063 		ifaaddr = netmask = brdaddr = 0;
   1064 	}
   1065 	return (0);
   1066 }
   1067 
   1068 static int
   1069 sysctl_rtable(name, namelen, where, given, new, newlen)
   1070 	int	*name;
   1071 	u_int	namelen;
   1072 	void 	*where;
   1073 	size_t	*given;
   1074 	void	*new;
   1075 	size_t	newlen;
   1076 {
   1077 	struct radix_node_head *rnh;
   1078 	int	i, s, error = EINVAL;
   1079 	u_char  af;
   1080 	struct	walkarg w;
   1081 
   1082 	if (new)
   1083 		return (EPERM);
   1084 	if (namelen != 3)
   1085 		return (EINVAL);
   1086 	af = name[0];
   1087 	w.w_tmemneeded = 0;
   1088 	w.w_tmemsize = 0;
   1089 	w.w_tmem = NULL;
   1090 again:
   1091 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1092 	if (w.w_tmemneeded) {
   1093 		w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
   1094 		w.w_tmemsize = w.w_tmemneeded;
   1095 		w.w_tmemneeded = 0;
   1096 	}
   1097 	w.w_op = name[1];
   1098 	w.w_arg = name[2];
   1099 	w.w_given = *given;
   1100 	w.w_needed = 0 - w.w_given;
   1101 	w.w_where = where;
   1102 
   1103 	s = splsoftnet();
   1104 	switch (w.w_op) {
   1105 
   1106 	case NET_RT_DUMP:
   1107 	case NET_RT_FLAGS:
   1108 		for (i = 1; i <= AF_MAX; i++)
   1109 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
   1110 			    (error = (*rnh->rnh_walktree)(rnh,
   1111 			    sysctl_dumpentry, &w)))
   1112 				break;
   1113 		break;
   1114 
   1115 #ifdef COMPAT_14
   1116 	case NET_RT_OIFLIST:
   1117 		error = sysctl_iflist(af, &w, w.w_op);
   1118 		break;
   1119 #endif
   1120 
   1121 	case NET_RT_IFLIST:
   1122 		error = sysctl_iflist(af, &w, w.w_op);
   1123 	}
   1124 	splx(s);
   1125 
   1126 	/* check to see if we couldn't allocate memory with NOWAIT */
   1127 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   1128 		goto again;
   1129 
   1130 	if (w.w_tmem)
   1131 		free(w.w_tmem, M_RTABLE);
   1132 	w.w_needed += w.w_given;
   1133 	if (where) {
   1134 		*given = w.w_where - (caddr_t) where;
   1135 		if (*given < w.w_needed)
   1136 			return (ENOMEM);
   1137 	} else {
   1138 		*given = (11 * w.w_needed) / 10;
   1139 	}
   1140 	return (error);
   1141 }
   1142 
   1143 /*
   1144  * Definitions of protocols supported in the ROUTE domain.
   1145  */
   1146 
   1147 extern	struct domain routedomain;		/* or at least forward */
   1148 
   1149 struct protosw routesw[] = {
   1150 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
   1151   raw_input,	route_output,	raw_ctlinput,	0,
   1152   route_usrreq,
   1153   raw_init,	0,		0,		0,
   1154   sysctl_rtable,
   1155 }
   1156 };
   1157 
   1158 struct domain routedomain =
   1159     { PF_ROUTE, "route", route_init, 0, 0,
   1160       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
   1161