Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.30
      1 /*	$NetBSD: rtsock.c,v 1.30 1999/07/01 08:12:49 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 <sys/param.h>
     68 #include <sys/systm.h>
     69 #include <sys/proc.h>
     70 #include <sys/mbuf.h>
     71 #include <sys/socket.h>
     72 #include <sys/socketvar.h>
     73 #include <sys/domain.h>
     74 #include <sys/protosw.h>
     75 
     76 #include <vm/vm.h>
     77 #include <sys/sysctl.h>
     78 
     79 #include <net/if.h>
     80 #include <net/route.h>
     81 #include <net/raw_cb.h>
     82 
     83 #include <machine/stdarg.h>
     84 
     85 struct	sockaddr route_dst = { 2, PF_ROUTE, };
     86 struct	sockaddr route_src = { 2, PF_ROUTE, };
     87 struct	sockproto route_proto = { PF_ROUTE, };
     88 
     89 struct walkarg {
     90 	int	w_op;
     91 	int	w_arg;
     92 	int	w_given;
     93 	int	w_needed;
     94 	caddr_t	w_where;
     95 	int	w_tmemsize;
     96 	int	w_tmemneeded;
     97 	caddr_t	w_tmem;
     98 };
     99 
    100 static struct mbuf *rt_msg1 __P((int, struct rt_addrinfo *));
    101 static int rt_msg2 __P((int, struct rt_addrinfo *, caddr_t, struct walkarg *,
    102     int *));
    103 static void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
    104 static __inline void rt_adjustcount __P((int, int));
    105 static void rt_setif __P((struct rtentry *, struct sockaddr *,
    106 	struct sockaddr *, struct sockaddr *));
    107 
    108 /* Sleazy use of local variables throughout file, warning!!!! */
    109 #define dst	info.rti_info[RTAX_DST]
    110 #define gate	info.rti_info[RTAX_GATEWAY]
    111 #define netmask	info.rti_info[RTAX_NETMASK]
    112 #define genmask	info.rti_info[RTAX_GENMASK]
    113 #define ifpaddr	info.rti_info[RTAX_IFP]
    114 #define ifaaddr	info.rti_info[RTAX_IFA]
    115 #define brdaddr	info.rti_info[RTAX_BRD]
    116 
    117 static __inline void
    118 rt_adjustcount(af, cnt)
    119 	int af, cnt;
    120 {
    121 	route_cb.any_count += cnt;
    122 	switch (af) {
    123 	case AF_INET:
    124 		route_cb.ip_count += cnt;
    125 		return;
    126 #ifdef INET6
    127 	case AF_INET6:
    128 		route_cb.ip6_count += cnt;
    129 		return;
    130 #endif
    131 	case AF_IPX:
    132 		route_cb.ipx_count += cnt;
    133 		return;
    134 	case AF_NS:
    135 		route_cb.ns_count += cnt;
    136 		return;
    137 	case AF_ISO:
    138 		route_cb.iso_count += cnt;
    139 		return;
    140 	}
    141 }
    142 
    143 /*ARGSUSED*/
    144 int
    145 route_usrreq(so, req, m, nam, control, p)
    146 	register struct socket *so;
    147 	int req;
    148 	struct mbuf *m, *nam, *control;
    149 	struct proc *p;
    150 {
    151 	register int error = 0;
    152 	register struct rawcb *rp = sotorawcb(so);
    153 	int s;
    154 
    155 	if (req == PRU_ATTACH) {
    156 		MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
    157 		if ((so->so_pcb = rp) != NULL)
    158 			bzero(so->so_pcb, sizeof(*rp));
    159 
    160 	}
    161 	if (req == PRU_DETACH && rp)
    162 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    163 	s = splsoftnet();
    164 
    165 	/*
    166 	 * Don't call raw_usrreq() in the attach case, because
    167 	 * we want to allow non-privileged processes to listen on
    168 	 * and send "safe" commands to the routing socket.
    169 	 */
    170 	if (req == PRU_ATTACH) {
    171 		if (p == 0)
    172 			error = EACCES;
    173 		else
    174 			error = raw_attach(so, (int)(long)nam);
    175 	} else
    176 		error = raw_usrreq(so, req, m, nam, control, p);
    177 
    178 	rp = sotorawcb(so);
    179 	if (req == PRU_ATTACH && rp) {
    180 		if (error) {
    181 			free((caddr_t)rp, M_PCB);
    182 			splx(s);
    183 			return (error);
    184 		}
    185 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    186 		rp->rcb_laddr = &route_src;
    187 		rp->rcb_faddr = &route_dst;
    188 		soisconnected(so);
    189 		so->so_options |= SO_USELOOPBACK;
    190 	}
    191 	splx(s);
    192 	return (error);
    193 }
    194 
    195 /*ARGSUSED*/
    196 int
    197 #if __STDC__
    198 route_output(struct mbuf *m, ...)
    199 #else
    200 route_output(m, va_alist)
    201 	struct mbuf *m;
    202 	va_dcl
    203 #endif
    204 {
    205 	register struct rt_msghdr *rtm = 0;
    206 	register struct rtentry *rt = 0;
    207 	struct rtentry *saved_nrt = 0;
    208 	struct radix_node_head *rnh;
    209 	struct rt_addrinfo info;
    210 	int len, error = 0;
    211 	struct ifnet *ifp = 0;
    212 	struct socket *so;
    213 	va_list ap;
    214 
    215 	va_start(ap, m);
    216 	so = va_arg(ap, struct socket *);
    217 	va_end(ap);
    218 
    219 	bzero(&info, sizeof(info));
    220 #define senderr(e) { error = e; goto flush;}
    221 	if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
    222 	   (m = m_pullup(m, sizeof(int32_t))) == 0))
    223 		return (ENOBUFS);
    224 	if ((m->m_flags & M_PKTHDR) == 0)
    225 		panic("route_output");
    226 	len = m->m_pkthdr.len;
    227 	if (len < sizeof(*rtm) ||
    228 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
    229 		dst = 0;
    230 		senderr(EINVAL);
    231 	}
    232 	R_Malloc(rtm, struct rt_msghdr *, len);
    233 	if (rtm == 0) {
    234 		dst = 0;
    235 		senderr(ENOBUFS);
    236 	}
    237 	m_copydata(m, 0, len, (caddr_t)rtm);
    238 	if (rtm->rtm_version != RTM_VERSION) {
    239 		dst = 0;
    240 		senderr(EPROTONOSUPPORT);
    241 	}
    242 	rtm->rtm_pid = curproc->p_pid;
    243 	info.rti_addrs = rtm->rtm_addrs;
    244 	rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info);
    245 	if (dst == 0 || (dst->sa_family >= AF_MAX))
    246 		senderr(EINVAL);
    247 	if (gate != 0 && (gate->sa_family >= AF_MAX))
    248 		senderr(EINVAL);
    249 	if (genmask) {
    250 		struct radix_node *t;
    251 		t = rn_addmask((caddr_t)genmask, 0, 1);
    252 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
    253 			genmask = (struct sockaddr *)(t->rn_key);
    254 		else
    255 			senderr(ENOBUFS);
    256 	}
    257 
    258 	/*
    259 	 * Verify that the caller has the appropriate privilege; RTM_GET
    260 	 * is the only operation the non-superuser is allowed.
    261 	 */
    262 	if (rtm->rtm_type != RTM_GET &&
    263 	    suser(curproc->p_ucred, &curproc->p_acflag) != 0)
    264 		senderr(EACCES);
    265 
    266 	switch (rtm->rtm_type) {
    267 
    268 	case RTM_ADD:
    269 		if (gate == 0)
    270 			senderr(EINVAL);
    271 		error = rtrequest(RTM_ADD, dst, gate, netmask,
    272 		    rtm->rtm_flags, &saved_nrt);
    273 		if (error == 0 && saved_nrt) {
    274 			/*
    275 			 * If the route request specified an interface with
    276 			 * IFA and/or IFP, we set the requested interface on
    277 			 * the route with rt_setif.  It would be much better
    278 			 * to do this inside rtrequest, but that would
    279 			 * require passing the desired interface, in some
    280 			 * form, to rtrequest.  Since rtrequest is called in
    281 			 * so many places (roughly 40 in our source), adding
    282 			 * a parameter is to much for us to swallow; this is
    283 			 * something for the FreeBSD developers to tackle.
    284 			 * Instead, we let rtrequest compute whatever
    285 			 * interface it wants, then come in behind it and
    286 			 * stick in the interface that we really want.  This
    287 			 * works reasonably well except when rtrequest can't
    288 			 * figure out what interface to use (with
    289 			 * ifa_withroute) and returns ENETUNREACH.  Ideally
    290 			 * it shouldn't matter if rtrequest can't figure out
    291 			 * the interface if we're going to explicitly set it
    292 			 * ourselves anyway.  But practically we can't
    293 			 * recover here because rtrequest will not do any of
    294 			 * the work necessary to add the route if it can't
    295 			 * find an interface.  As long as there is a default
    296 			 * route that leads to some interface, rtrequest will
    297 			 * find an interface, so this problem should be
    298 			 * rarely encountered.
    299 			 * dwiggins (at) bbn.com
    300 			 */
    301 
    302 			rt_setif(saved_nrt, ifpaddr, ifaaddr, gate);
    303 			rt_setmetrics(rtm->rtm_inits,
    304 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
    305 			saved_nrt->rt_refcnt--;
    306 			saved_nrt->rt_genmask = genmask;
    307 		}
    308 		break;
    309 
    310 	case RTM_DELETE:
    311 		error = rtrequest(RTM_DELETE, dst, gate, netmask,
    312 		    rtm->rtm_flags, &saved_nrt);
    313 		if (error == 0) {
    314 			(rt = saved_nrt)->rt_refcnt++;
    315 			goto report;
    316 		}
    317 		break;
    318 
    319 	case RTM_GET:
    320 	case RTM_CHANGE:
    321 	case RTM_LOCK:
    322 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
    323 			senderr(EAFNOSUPPORT);
    324 		} else if ((rt = (struct rtentry *)
    325 		    rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
    326 			rt->rt_refcnt++;
    327 		else
    328 			senderr(ESRCH);
    329 		switch(rtm->rtm_type) {
    330 
    331 		case RTM_GET:
    332 		report:
    333 			dst = rt_key(rt);
    334 			gate = rt->rt_gateway;
    335 			netmask = rt_mask(rt);
    336 			genmask = rt->rt_genmask;
    337 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
    338 				if ((ifp = rt->rt_ifp) != NULL) {
    339 					ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    340 					ifaaddr = rt->rt_ifa->ifa_addr;
    341 					if (ifp->if_flags & IFF_POINTOPOINT)
    342 						brdaddr = rt->rt_ifa->ifa_dstaddr;
    343 					else
    344 						brdaddr = 0;
    345 					rtm->rtm_index = ifp->if_index;
    346 				} else {
    347 					ifpaddr = 0;
    348 					ifaaddr = 0;
    349 			    }
    350 			}
    351 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
    352 			    (struct walkarg *)0, &len);
    353 			if (len > rtm->rtm_msglen) {
    354 				struct rt_msghdr *new_rtm;
    355 				R_Malloc(new_rtm, struct rt_msghdr *, len);
    356 				if (new_rtm == 0)
    357 					senderr(ENOBUFS);
    358 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
    359 				Free(rtm); rtm = new_rtm;
    360 			}
    361 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
    362 			    (struct walkarg *)0, 0);
    363 			rtm->rtm_flags = rt->rt_flags;
    364 			rtm->rtm_rmx = rt->rt_rmx;
    365 			rtm->rtm_addrs = info.rti_addrs;
    366 			break;
    367 
    368 		case RTM_CHANGE:
    369 			if (gate && rt_setgate(rt, rt_key(rt), gate))
    370 				senderr(EDQUOT);
    371 
    372 			rt_setif(rt, ifpaddr, ifaaddr, gate);
    373 
    374 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
    375 			    &rt->rt_rmx);
    376 			if (genmask)
    377 				rt->rt_genmask = genmask;
    378 			/*
    379 			 * Fall into
    380 			 */
    381 		case RTM_LOCK:
    382 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    383 			rt->rt_rmx.rmx_locks |=
    384 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    385 			break;
    386 		}
    387 		break;
    388 
    389 	default:
    390 		senderr(EOPNOTSUPP);
    391 	}
    392 
    393 flush:
    394 	if (rtm) {
    395 		if (error)
    396 			rtm->rtm_errno = error;
    397 		else
    398 			rtm->rtm_flags |= RTF_DONE;
    399 	}
    400 	if (rt)
    401 		rtfree(rt);
    402     {
    403 	register struct rawcb *rp = 0;
    404 	/*
    405 	 * Check to see if we don't want our own messages.
    406 	 */
    407 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    408 		if (route_cb.any_count <= 1) {
    409 			if (rtm)
    410 				Free(rtm);
    411 			m_freem(m);
    412 			return (error);
    413 		}
    414 		/* There is another listener, so construct message */
    415 		rp = sotorawcb(so);
    416 	}
    417 	if (rtm) {
    418 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    419 		Free(rtm);
    420 	}
    421 	if (rp)
    422 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    423 	if (dst)
    424 		route_proto.sp_protocol = dst->sa_family;
    425 	raw_input(m, &route_proto, &route_src, &route_dst);
    426 	if (rp)
    427 		rp->rcb_proto.sp_family = PF_ROUTE;
    428     }
    429 	return (error);
    430 }
    431 
    432 void
    433 rt_setmetrics(which, in, out)
    434 	u_long which;
    435 	register struct rt_metrics *in, *out;
    436 {
    437 #define metric(f, e) if (which & (f)) out->e = in->e;
    438 	metric(RTV_RPIPE, rmx_recvpipe);
    439 	metric(RTV_SPIPE, rmx_sendpipe);
    440 	metric(RTV_SSTHRESH, rmx_ssthresh);
    441 	metric(RTV_RTT, rmx_rtt);
    442 	metric(RTV_RTTVAR, rmx_rttvar);
    443 	metric(RTV_HOPCOUNT, rmx_hopcount);
    444 	metric(RTV_MTU, rmx_mtu);
    445 	metric(RTV_EXPIRE, rmx_expire);
    446 #undef metric
    447 }
    448 
    449 /*
    450  * Set route's interface given ifpaddr, ifaaddr, and gateway.
    451  */
    452 static void
    453 rt_setif(rt, Ifpaddr, Ifaaddr, Gate)
    454 	struct rtentry *rt;
    455 	struct sockaddr *Ifpaddr, *Ifaaddr, *Gate;
    456 {
    457 	struct ifaddr *ifa = 0;
    458 	struct ifnet  *ifp = 0;
    459 
    460 	/* new gateway could require new ifaddr, ifp;
    461 	   flags may also be different; ifp may be specified
    462 	   by ll sockaddr when protocol address is ambiguous */
    463 	if (Ifpaddr && (ifa = ifa_ifwithnet(Ifpaddr)) &&
    464 	    (ifp = ifa->ifa_ifp) && (Ifaaddr || Gate))
    465 		ifa = ifaof_ifpforaddr(Ifaaddr ? Ifaaddr : Gate,
    466 					ifp);
    467 	else if (Ifpaddr && (ifp = if_withname(Ifpaddr)) ) {
    468 		ifa = Gate ? ifaof_ifpforaddr(Gate, ifp) :
    469 				TAILQ_FIRST(&ifp->if_addrlist);
    470 	}
    471 	else if ((Ifaaddr && (ifa = ifa_ifwithaddr(Ifaaddr))) ||
    472 		 (Gate && (ifa = ifa_ifwithroute(rt->rt_flags,
    473 					rt_key(rt), Gate))))
    474 		ifp = ifa->ifa_ifp;
    475 	if (ifa) {
    476 		register struct ifaddr *oifa = rt->rt_ifa;
    477 		if (oifa != ifa) {
    478 		    if (oifa && oifa->ifa_rtrequest)
    479 			oifa->ifa_rtrequest(RTM_DELETE,
    480 						rt, Gate);
    481 		    IFAFREE(rt->rt_ifa);
    482 		    rt->rt_ifa = ifa;
    483 		    ifa->ifa_refcnt++;
    484 		    rt->rt_ifp = ifp;
    485 		    rt->rt_rmx.rmx_mtu = ifp->if_mtu;
    486 		    if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    487 			rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, Gate);
    488 		} else
    489 			goto call_ifareq;
    490 		return;
    491 	}
    492       call_ifareq:
    493 	/* XXX: to reset gateway to correct value, at RTM_CHANGE */
    494 	if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    495 		rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, Gate);
    496 }
    497 
    498 
    499 #define ROUNDUP(a) \
    500 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    501 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    502 
    503 static void
    504 rt_xaddrs(cp, cplim, rtinfo)
    505 	register caddr_t cp, cplim;
    506 	register struct rt_addrinfo *rtinfo;
    507 {
    508 	register struct sockaddr *sa;
    509 	register int i;
    510 
    511 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
    512 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
    513 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    514 			continue;
    515 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
    516 		ADVANCE(cp, sa);
    517 	}
    518 }
    519 
    520 static struct mbuf *
    521 rt_msg1(type, rtinfo)
    522 	int type;
    523 	register struct rt_addrinfo *rtinfo;
    524 {
    525 	register struct rt_msghdr *rtm;
    526 	register struct mbuf *m;
    527 	register int i;
    528 	register struct sockaddr *sa;
    529 	int len, dlen;
    530 
    531 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    532 	if (m == 0)
    533 		return (m);
    534 	switch (type) {
    535 
    536 	case RTM_DELADDR:
    537 	case RTM_NEWADDR:
    538 		len = sizeof(struct ifa_msghdr);
    539 		break;
    540 
    541 	case RTM_IFINFO:
    542 		len = sizeof(struct if_msghdr);
    543 		break;
    544 
    545 	default:
    546 		len = sizeof(struct rt_msghdr);
    547 	}
    548 	if (len > MHLEN)
    549 		panic("rt_msg1");
    550 	m->m_pkthdr.len = m->m_len = len;
    551 	m->m_pkthdr.rcvif = 0;
    552 	rtm = mtod(m, struct rt_msghdr *);
    553 	bzero(rtm, len);
    554 	for (i = 0; i < RTAX_MAX; i++) {
    555 		if ((sa = rtinfo->rti_info[i]) == NULL)
    556 			continue;
    557 		rtinfo->rti_addrs |= (1 << i);
    558 		dlen = ROUNDUP(sa->sa_len);
    559 		m_copyback(m, len, dlen, (caddr_t)sa);
    560 		len += dlen;
    561 	}
    562 	if (m->m_pkthdr.len != len) {
    563 		m_freem(m);
    564 		return (NULL);
    565 	}
    566 	rtm->rtm_msglen = len;
    567 	rtm->rtm_version = RTM_VERSION;
    568 	rtm->rtm_type = type;
    569 	return (m);
    570 }
    571 
    572 /*
    573  * rt_msg2
    574  *
    575  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    576  *		returns the length of the message in 'lenp'.
    577  *
    578  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    579  *	the message
    580  * otherwise walkarg's w_needed is updated and if the user buffer is
    581  *	specified and w_needed indicates space exists the information is copied
    582  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    583  *	if the allocation fails ENOBUFS is returned.
    584  */
    585 static int
    586 rt_msg2(type, rtinfo, cp, w, lenp)
    587 	int type;
    588 	register struct rt_addrinfo *rtinfo;
    589 	caddr_t cp;
    590 	struct walkarg *w;
    591 	int *lenp;
    592 {
    593 	register int i;
    594 	int len, dlen, second_time = 0;
    595 	caddr_t cp0;
    596 
    597 	rtinfo->rti_addrs = 0;
    598 again:
    599 	switch (type) {
    600 
    601 	case RTM_DELADDR:
    602 	case RTM_NEWADDR:
    603 		len = sizeof(struct ifa_msghdr);
    604 		break;
    605 
    606 	case RTM_IFINFO:
    607 		len = sizeof(struct if_msghdr);
    608 		break;
    609 
    610 	default:
    611 		len = sizeof(struct rt_msghdr);
    612 	}
    613 	if ((cp0 = cp) != NULL)
    614 		cp += len;
    615 	for (i = 0; i < RTAX_MAX; i++) {
    616 		register struct sockaddr *sa;
    617 
    618 		if ((sa = rtinfo->rti_info[i]) == 0)
    619 			continue;
    620 		rtinfo->rti_addrs |= (1 << i);
    621 		dlen = ROUNDUP(sa->sa_len);
    622 		if (cp) {
    623 			bcopy(sa, cp, (unsigned)dlen);
    624 			cp += dlen;
    625 		}
    626 		len += dlen;
    627 	}
    628 	if (cp == 0 && w != NULL && !second_time) {
    629 		register struct walkarg *rw = w;
    630 
    631 		rw->w_needed += len;
    632 		if (rw->w_needed <= 0 && rw->w_where) {
    633 			if (rw->w_tmemsize < len) {
    634 				if (rw->w_tmem)
    635 					free(rw->w_tmem, M_RTABLE);
    636 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
    637 				    M_NOWAIT);
    638 				if (rw->w_tmem)
    639 					rw->w_tmemsize = len;
    640 			}
    641 			if (rw->w_tmem) {
    642 				cp = rw->w_tmem;
    643 				second_time = 1;
    644 				goto again;
    645 			} else {
    646 				rw->w_tmemneeded = len;
    647 				return (ENOBUFS);
    648 			}
    649 		}
    650 	}
    651 	if (cp) {
    652 		register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    653 
    654 		rtm->rtm_version = RTM_VERSION;
    655 		rtm->rtm_type = type;
    656 		rtm->rtm_msglen = len;
    657 	}
    658 	if (lenp)
    659 		*lenp = len;
    660 	return (0);
    661 }
    662 
    663 /*
    664  * This routine is called to generate a message from the routing
    665  * socket indicating that a redirect has occured, a routing lookup
    666  * has failed, or that a protocol has detected timeouts to a particular
    667  * destination.
    668  */
    669 void
    670 rt_missmsg(type, rtinfo, flags, error)
    671 	int type, flags, error;
    672 	register struct rt_addrinfo *rtinfo;
    673 {
    674 	register struct rt_msghdr *rtm;
    675 	register struct mbuf *m;
    676 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    677 
    678 	if (route_cb.any_count == 0)
    679 		return;
    680 	m = rt_msg1(type, rtinfo);
    681 	if (m == 0)
    682 		return;
    683 	rtm = mtod(m, struct rt_msghdr *);
    684 	rtm->rtm_flags = RTF_DONE | flags;
    685 	rtm->rtm_errno = error;
    686 	rtm->rtm_addrs = rtinfo->rti_addrs;
    687 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
    688 	raw_input(m, &route_proto, &route_src, &route_dst);
    689 }
    690 
    691 /*
    692  * This routine is called to generate a message from the routing
    693  * socket indicating that the status of a network interface has changed.
    694  */
    695 void
    696 rt_ifmsg(ifp)
    697 	register struct ifnet *ifp;
    698 {
    699 	register struct if_msghdr *ifm;
    700 	struct mbuf *m;
    701 	struct rt_addrinfo info;
    702 
    703 	if (route_cb.any_count == 0)
    704 		return;
    705 	bzero(&info, sizeof(info));
    706 	m = rt_msg1(RTM_IFINFO, &info);
    707 	if (m == 0)
    708 		return;
    709 	ifm = mtod(m, struct if_msghdr *);
    710 	ifm->ifm_index = ifp->if_index;
    711 	ifm->ifm_flags = ifp->if_flags;
    712 	ifm->ifm_data = ifp->if_data;
    713 	ifm->ifm_addrs = 0;
    714 	route_proto.sp_protocol = 0;
    715 	raw_input(m, &route_proto, &route_src, &route_dst);
    716 }
    717 
    718 /*
    719  * This is called to generate messages from the routing socket
    720  * indicating a network interface has had addresses associated with it.
    721  * if we ever reverse the logic and replace messages TO the routing
    722  * socket indicate a request to configure interfaces, then it will
    723  * be unnecessary as the routing socket will automatically generate
    724  * copies of it.
    725  */
    726 void
    727 rt_newaddrmsg(cmd, ifa, error, rt)
    728 	int cmd, error;
    729 	register struct ifaddr *ifa;
    730 	register struct rtentry *rt;
    731 {
    732 	struct rt_addrinfo info;
    733 	struct sockaddr *sa = NULL;
    734 	int pass;
    735 	struct mbuf *m = NULL;
    736 	struct ifnet *ifp = ifa->ifa_ifp;
    737 
    738 	if (route_cb.any_count == 0)
    739 		return;
    740 	for (pass = 1; pass < 3; pass++) {
    741 		bzero(&info, sizeof(info));
    742 		if ((cmd == RTM_ADD && pass == 1) ||
    743 		    (cmd == RTM_DELETE && pass == 2)) {
    744 			register struct ifa_msghdr *ifam;
    745 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
    746 
    747 			ifaaddr = sa = ifa->ifa_addr;
    748 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    749 			netmask = ifa->ifa_netmask;
    750 			brdaddr = ifa->ifa_dstaddr;
    751 			if ((m = rt_msg1(ncmd, &info)) == NULL)
    752 				continue;
    753 			ifam = mtod(m, struct ifa_msghdr *);
    754 			ifam->ifam_index = ifp->if_index;
    755 			ifam->ifam_metric = ifa->ifa_metric;
    756 			ifam->ifam_flags = ifa->ifa_flags;
    757 			ifam->ifam_addrs = info.rti_addrs;
    758 		}
    759 		if ((cmd == RTM_ADD && pass == 2) ||
    760 		    (cmd == RTM_DELETE && pass == 1)) {
    761 			register struct rt_msghdr *rtm;
    762 
    763 			if (rt == 0)
    764 				continue;
    765 			netmask = rt_mask(rt);
    766 			dst = sa = rt_key(rt);
    767 			gate = rt->rt_gateway;
    768 			if ((m = rt_msg1(cmd, &info)) == NULL)
    769 				continue;
    770 			rtm = mtod(m, struct rt_msghdr *);
    771 			rtm->rtm_index = ifp->if_index;
    772 			rtm->rtm_flags |= rt->rt_flags;
    773 			rtm->rtm_errno = error;
    774 			rtm->rtm_addrs = info.rti_addrs;
    775 		}
    776 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
    777 		raw_input(m, &route_proto, &route_src, &route_dst);
    778 	}
    779 }
    780 
    781 /*
    782  * This is used in dumping the kernel table via sysctl().
    783  */
    784 int
    785 sysctl_dumpentry(rn, v)
    786 	struct radix_node *rn;
    787 	register void *v;
    788 {
    789 	register struct walkarg *w = v;
    790 	register struct rtentry *rt = (struct rtentry *)rn;
    791 	int error = 0, size;
    792 	struct rt_addrinfo info;
    793 
    794 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    795 		return 0;
    796 	bzero(&info, sizeof(info));
    797 	dst = rt_key(rt);
    798 	gate = rt->rt_gateway;
    799 	netmask = rt_mask(rt);
    800 	genmask = rt->rt_genmask;
    801 	if (rt->rt_ifp) {
    802 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
    803 		ifaaddr = rt->rt_ifa->ifa_addr;
    804 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    805 			brdaddr = rt->rt_ifa->ifa_dstaddr;
    806 	}
    807 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    808 		return (error);
    809 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    810 		register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
    811 
    812 		rtm->rtm_flags = rt->rt_flags;
    813 		rtm->rtm_use = rt->rt_use;
    814 		rtm->rtm_rmx = rt->rt_rmx;
    815 		rtm->rtm_index = rt->rt_ifp->if_index;
    816 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    817 		rtm->rtm_addrs = info.rti_addrs;
    818 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    819 			w->w_where = NULL;
    820 		else
    821 			w->w_where += size;
    822 	}
    823 	return (error);
    824 }
    825 
    826 int
    827 sysctl_iflist(af, w)
    828 	int	af;
    829 	register struct	walkarg *w;
    830 {
    831 	register struct ifnet *ifp;
    832 	register struct ifaddr *ifa;
    833 	struct	rt_addrinfo info;
    834 	int	len, error = 0;
    835 
    836 	bzero(&info, sizeof(info));
    837 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
    838 		if (w->w_arg && w->w_arg != ifp->if_index)
    839 			continue;
    840 		ifa = ifp->if_addrlist.tqh_first;
    841 		ifpaddr = ifa->ifa_addr;
    842 		if ((error = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len)))
    843 			return (error);
    844 		ifpaddr = 0;
    845 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    846 			register struct if_msghdr *ifm;
    847 
    848 			ifm = (struct if_msghdr *)w->w_tmem;
    849 			ifm->ifm_index = ifp->if_index;
    850 			ifm->ifm_flags = ifp->if_flags;
    851 			ifm->ifm_data = ifp->if_data;
    852 			ifm->ifm_addrs = info.rti_addrs;
    853 			error = copyout(ifm, w->w_where, len);
    854 			if (error)
    855 				return (error);
    856 			w->w_where += len;
    857 		}
    858 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
    859 			if (af && af != ifa->ifa_addr->sa_family)
    860 				continue;
    861 			ifaaddr = ifa->ifa_addr;
    862 			netmask = ifa->ifa_netmask;
    863 			brdaddr = ifa->ifa_dstaddr;
    864 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
    865 				return (error);
    866 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    867 				register struct ifa_msghdr *ifam;
    868 
    869 				ifam = (struct ifa_msghdr *)w->w_tmem;
    870 				ifam->ifam_index = ifa->ifa_ifp->if_index;
    871 				ifam->ifam_flags = ifa->ifa_flags;
    872 				ifam->ifam_metric = ifa->ifa_metric;
    873 				ifam->ifam_addrs = info.rti_addrs;
    874 				error = copyout(w->w_tmem, w->w_where, len);
    875 				if (error)
    876 					return (error);
    877 				w->w_where += len;
    878 			}
    879 		}
    880 		ifaaddr = netmask = brdaddr = 0;
    881 	}
    882 	return (0);
    883 }
    884 
    885 int
    886 sysctl_rtable(name, namelen, where, given, new, newlen)
    887 	int	*name;
    888 	u_int	namelen;
    889 	void 	*where;
    890 	size_t	*given;
    891 	void	*new;
    892 	size_t	newlen;
    893 {
    894 	register struct radix_node_head *rnh;
    895 	int	i, s, error = EINVAL;
    896 	u_char  af;
    897 	struct	walkarg w;
    898 
    899 	if (new)
    900 		return (EPERM);
    901 	if (namelen != 3)
    902 		return (EINVAL);
    903 	af = name[0];
    904 	w.w_tmemneeded = 0;
    905 	w.w_tmemsize = 0;
    906 	w.w_tmem = NULL;
    907 again:
    908 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
    909 	if (w.w_tmemneeded) {
    910 		w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
    911 		w.w_tmemsize = w.w_tmemneeded;
    912 		w.w_tmemneeded = 0;
    913 	}
    914 	w.w_op = name[1];
    915 	w.w_arg = name[2];
    916 	w.w_given = *given;
    917 	w.w_needed = 0 - w.w_given;
    918 	w.w_where = where;
    919 
    920 	s = splsoftnet();
    921 	switch (w.w_op) {
    922 
    923 	case NET_RT_DUMP:
    924 	case NET_RT_FLAGS:
    925 		for (i = 1; i <= AF_MAX; i++)
    926 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
    927 			    (error = (*rnh->rnh_walktree)(rnh,
    928 			    sysctl_dumpentry, &w)))
    929 				break;
    930 		break;
    931 
    932 	case NET_RT_IFLIST:
    933 		error = sysctl_iflist(af, &w);
    934 	}
    935 	splx(s);
    936 
    937 	/* check to see if we couldn't allocate memory with NOWAIT */
    938 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
    939 		goto again;
    940 
    941 	if (w.w_tmem)
    942 		free(w.w_tmem, M_RTABLE);
    943 	w.w_needed += w.w_given;
    944 	if (where) {
    945 		*given = w.w_where - (caddr_t) where;
    946 		if (*given < w.w_needed)
    947 			return (ENOMEM);
    948 	} else {
    949 		*given = (11 * w.w_needed) / 10;
    950 	}
    951 	return (error);
    952 }
    953 
    954 /*
    955  * Definitions of protocols supported in the ROUTE domain.
    956  */
    957 
    958 extern	struct domain routedomain;		/* or at least forward */
    959 
    960 struct protosw routesw[] = {
    961 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
    962   raw_input,	route_output,	raw_ctlinput,	0,
    963   route_usrreq,
    964   raw_init,	0,		0,		0,
    965   sysctl_rtable,
    966 }
    967 };
    968 
    969 struct domain routedomain =
    970     { PF_ROUTE, "route", route_init, 0, 0,
    971       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
    972