Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.49
      1 /*	$NetBSD: rtsock.c,v 1.49 2001/07/19 00:12:09 enami 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 			memset(so->so_pcb, 0, 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 	memset(&info, 0, 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 	if (rtm) {
    427 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    428 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
    429 			m_freem(m);
    430 			m = NULL;
    431 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
    432 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    433 		Free(rtm);
    434 	}
    435 	if (rp)
    436 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    437 	if (dst)
    438 		route_proto.sp_protocol = dst->sa_family;
    439 	if (m)
    440 		raw_input(m, &route_proto, &route_src, &route_dst);
    441 	if (rp)
    442 		rp->rcb_proto.sp_family = PF_ROUTE;
    443     }
    444 	return (error);
    445 }
    446 
    447 void
    448 rt_setmetrics(which, in, out)
    449 	u_long which;
    450 	struct rt_metrics *in, *out;
    451 {
    452 #define metric(f, e) if (which & (f)) out->e = in->e;
    453 	metric(RTV_RPIPE, rmx_recvpipe);
    454 	metric(RTV_SPIPE, rmx_sendpipe);
    455 	metric(RTV_SSTHRESH, rmx_ssthresh);
    456 	metric(RTV_RTT, rmx_rtt);
    457 	metric(RTV_RTTVAR, rmx_rttvar);
    458 	metric(RTV_HOPCOUNT, rmx_hopcount);
    459 	metric(RTV_MTU, rmx_mtu);
    460 	metric(RTV_EXPIRE, rmx_expire);
    461 #undef metric
    462 }
    463 
    464 #define ROUNDUP(a) \
    465 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    466 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    467 
    468 static int
    469 rt_xaddrs(cp, cplim, rtinfo)
    470 	caddr_t cp, cplim;
    471 	struct rt_addrinfo *rtinfo;
    472 {
    473 	struct sockaddr *sa;
    474 	int i;
    475 
    476 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
    477 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    478 			continue;
    479 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
    480 		ADVANCE(cp, sa);
    481 	}
    482 
    483 	/* Check for extra addresses specified.  */
    484 	if ((rtinfo->rti_addrs & (~0 << i)) != 0)
    485 		return (1);
    486 	/* Check for bad data length.  */
    487 	if (cp != cplim) {
    488 		if (i == RTAX_NETMASK + 1 &&
    489 		    cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
    490 			/*
    491 			 * The last sockaddr was netmask.
    492 			 * We accept this for now for the sake of old
    493 			 * binaries or third party softwares.
    494 			 */
    495 			;
    496 		else
    497 			return (1);
    498 	}
    499 	return (0);
    500 }
    501 
    502 static struct mbuf *
    503 rt_msg1(type, rtinfo, data, datalen)
    504 	int type;
    505 	struct rt_addrinfo *rtinfo;
    506 	caddr_t data;
    507 	int datalen;
    508 {
    509 	struct rt_msghdr *rtm;
    510 	struct mbuf *m;
    511 	int i;
    512 	struct sockaddr *sa;
    513 	int len, dlen;
    514 
    515 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    516 	if (m == 0)
    517 		return (m);
    518 	switch (type) {
    519 
    520 	case RTM_DELADDR:
    521 	case RTM_NEWADDR:
    522 		len = sizeof(struct ifa_msghdr);
    523 		break;
    524 
    525 #ifdef COMPAT_14
    526 	case RTM_OIFINFO:
    527 		len = sizeof(struct if_msghdr14);
    528 		break;
    529 #endif
    530 
    531 	case RTM_IFINFO:
    532 		len = sizeof(struct if_msghdr);
    533 		break;
    534 
    535 	case RTM_IFANNOUNCE:
    536 		len = sizeof(struct if_announcemsghdr);
    537 		break;
    538 
    539 	default:
    540 		len = sizeof(struct rt_msghdr);
    541 	}
    542 	if (len > MHLEN + MLEN)
    543 		panic("rt_msg1: message too long");
    544 	else if (len > MHLEN) {
    545 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
    546 		if (m->m_next == NULL) {
    547 			m_freem(m);
    548 			return (NULL);
    549 		}
    550 		m->m_pkthdr.len = len;
    551 		m->m_len = MHLEN;
    552 		m->m_next->m_len = len - MHLEN;
    553 	} else {
    554 		m->m_pkthdr.len = m->m_len = len;
    555 	}
    556 	m->m_pkthdr.rcvif = 0;
    557 	m_copyback(m, 0, datalen, data);
    558 	rtm = mtod(m, struct rt_msghdr *);
    559 	for (i = 0; i < RTAX_MAX; i++) {
    560 		if ((sa = rtinfo->rti_info[i]) == NULL)
    561 			continue;
    562 		rtinfo->rti_addrs |= (1 << i);
    563 		dlen = ROUNDUP(sa->sa_len);
    564 		m_copyback(m, len, dlen, (caddr_t)sa);
    565 		len += dlen;
    566 	}
    567 	if (m->m_pkthdr.len != len) {
    568 		m_freem(m);
    569 		return (NULL);
    570 	}
    571 	rtm->rtm_msglen = len;
    572 	rtm->rtm_version = RTM_VERSION;
    573 	rtm->rtm_type = type;
    574 	return (m);
    575 }
    576 
    577 /*
    578  * rt_msg2
    579  *
    580  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    581  *		returns the length of the message in 'lenp'.
    582  *
    583  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    584  *	the message
    585  * otherwise walkarg's w_needed is updated and if the user buffer is
    586  *	specified and w_needed indicates space exists the information is copied
    587  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    588  *	if the allocation fails ENOBUFS is returned.
    589  */
    590 static int
    591 rt_msg2(type, rtinfo, cp, w, lenp)
    592 	int type;
    593 	struct rt_addrinfo *rtinfo;
    594 	caddr_t cp;
    595 	struct walkarg *w;
    596 	int *lenp;
    597 {
    598 	int i;
    599 	int len, dlen, second_time = 0;
    600 	caddr_t cp0;
    601 
    602 	rtinfo->rti_addrs = 0;
    603 again:
    604 	switch (type) {
    605 
    606 	case RTM_DELADDR:
    607 	case RTM_NEWADDR:
    608 		len = sizeof(struct ifa_msghdr);
    609 		break;
    610 #ifdef COMPAT_14
    611 	case RTM_OIFINFO:
    612 		len = sizeof(struct if_msghdr14);
    613 		break;
    614 #endif
    615 
    616 	case RTM_IFINFO:
    617 		len = sizeof(struct if_msghdr);
    618 		break;
    619 
    620 	default:
    621 		len = sizeof(struct rt_msghdr);
    622 	}
    623 	if ((cp0 = cp) != NULL)
    624 		cp += len;
    625 	for (i = 0; i < RTAX_MAX; i++) {
    626 		struct sockaddr *sa;
    627 
    628 		if ((sa = rtinfo->rti_info[i]) == 0)
    629 			continue;
    630 		rtinfo->rti_addrs |= (1 << i);
    631 		dlen = ROUNDUP(sa->sa_len);
    632 		if (cp) {
    633 			bcopy(sa, cp, (unsigned)dlen);
    634 			cp += dlen;
    635 		}
    636 		len += dlen;
    637 	}
    638 	if (cp == 0 && w != NULL && !second_time) {
    639 		struct walkarg *rw = w;
    640 
    641 		rw->w_needed += len;
    642 		if (rw->w_needed <= 0 && rw->w_where) {
    643 			if (rw->w_tmemsize < len) {
    644 				if (rw->w_tmem)
    645 					free(rw->w_tmem, M_RTABLE);
    646 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
    647 				    M_NOWAIT);
    648 				if (rw->w_tmem)
    649 					rw->w_tmemsize = len;
    650 			}
    651 			if (rw->w_tmem) {
    652 				cp = rw->w_tmem;
    653 				second_time = 1;
    654 				goto again;
    655 			} else {
    656 				rw->w_tmemneeded = len;
    657 				return (ENOBUFS);
    658 			}
    659 		}
    660 	}
    661 	if (cp) {
    662 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    663 
    664 		rtm->rtm_version = RTM_VERSION;
    665 		rtm->rtm_type = type;
    666 		rtm->rtm_msglen = len;
    667 	}
    668 	if (lenp)
    669 		*lenp = len;
    670 	return (0);
    671 }
    672 
    673 /*
    674  * This routine is called to generate a message from the routing
    675  * socket indicating that a redirect has occured, a routing lookup
    676  * has failed, or that a protocol has detected timeouts to a particular
    677  * destination.
    678  */
    679 void
    680 rt_missmsg(type, rtinfo, flags, error)
    681 	int type, flags, error;
    682 	struct rt_addrinfo *rtinfo;
    683 {
    684 	struct rt_msghdr rtm;
    685 	struct mbuf *m;
    686 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    687 
    688 	if (route_cb.any_count == 0)
    689 		return;
    690 	memset(&rtm, 0, sizeof(rtm));
    691 	rtm.rtm_flags = RTF_DONE | flags;
    692 	rtm.rtm_errno = error;
    693 	m = rt_msg1(type, rtinfo, (caddr_t)&rtm, sizeof(rtm));
    694 	if (m == 0)
    695 		return;
    696 	mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
    697 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
    698 	raw_input(m, &route_proto, &route_src, &route_dst);
    699 }
    700 
    701 /*
    702  * This routine is called to generate a message from the routing
    703  * socket indicating that the status of a network interface has changed.
    704  */
    705 void
    706 rt_ifmsg(ifp)
    707 	struct ifnet *ifp;
    708 {
    709 	struct if_msghdr ifm;
    710 #ifdef COMPAT_14
    711 	struct if_msghdr14 oifm;
    712 #endif
    713 	struct mbuf *m;
    714 	struct rt_addrinfo info;
    715 
    716 	if (route_cb.any_count == 0)
    717 		return;
    718 	memset(&info, 0, sizeof(info));
    719 	memset(&ifm, 0, sizeof(ifm));
    720 	ifm.ifm_index = ifp->if_index;
    721 	ifm.ifm_flags = ifp->if_flags;
    722 	ifm.ifm_data = ifp->if_data;
    723 	ifm.ifm_addrs = 0;
    724 	m = rt_msg1(RTM_IFINFO, &info, (caddr_t)&ifm, sizeof(ifm));
    725 	if (m == 0)
    726 		return;
    727 	route_proto.sp_protocol = 0;
    728 	raw_input(m, &route_proto, &route_src, &route_dst);
    729 #ifdef COMPAT_14
    730 	memset(&info, 0, sizeof(info));
    731 	memset(&oifm, 0, sizeof(oifm));
    732 	oifm.ifm_index = ifp->if_index;
    733 	oifm.ifm_flags = ifp->if_flags;
    734 	oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
    735 	oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
    736 	oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
    737 	oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
    738 	oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
    739 	oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
    740 	oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
    741 	oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
    742 	oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
    743 	oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
    744 	oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
    745 	oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
    746 	oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
    747 	oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
    748 	oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
    749 	oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
    750 	oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
    751 	oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
    752 	oifm.ifm_addrs = 0;
    753 	m = rt_msg1(RTM_OIFINFO, &info, (caddr_t)&oifm, sizeof(oifm));
    754 	if (m == 0)
    755 		return;
    756 	route_proto.sp_protocol = 0;
    757 	raw_input(m, &route_proto, &route_src, &route_dst);
    758 #endif
    759 }
    760 
    761 /*
    762  * This is called to generate messages from the routing socket
    763  * indicating a network interface has had addresses associated with it.
    764  * if we ever reverse the logic and replace messages TO the routing
    765  * socket indicate a request to configure interfaces, then it will
    766  * be unnecessary as the routing socket will automatically generate
    767  * copies of it.
    768  */
    769 void
    770 rt_newaddrmsg(cmd, ifa, error, rt)
    771 	int cmd, error;
    772 	struct ifaddr *ifa;
    773 	struct rtentry *rt;
    774 {
    775 	struct rt_addrinfo info;
    776 	struct sockaddr *sa = NULL;
    777 	int pass;
    778 	struct mbuf *m = NULL;
    779 	struct ifnet *ifp = ifa->ifa_ifp;
    780 
    781 	if (route_cb.any_count == 0)
    782 		return;
    783 	for (pass = 1; pass < 3; pass++) {
    784 		memset(&info, 0, sizeof(info));
    785 		if ((cmd == RTM_ADD && pass == 1) ||
    786 		    (cmd == RTM_DELETE && pass == 2)) {
    787 			struct ifa_msghdr ifam;
    788 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
    789 
    790 			ifaaddr = sa = ifa->ifa_addr;
    791 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    792 			netmask = ifa->ifa_netmask;
    793 			brdaddr = ifa->ifa_dstaddr;
    794 			memset(&ifam, 0, sizeof(ifam));
    795 			ifam.ifam_index = ifp->if_index;
    796 			ifam.ifam_metric = ifa->ifa_metric;
    797 			ifam.ifam_flags = ifa->ifa_flags;
    798 			m = rt_msg1(ncmd, &info, (caddr_t)&ifam, sizeof(ifam));
    799 			if (m == NULL)
    800 				continue;
    801 			mtod(m, struct ifa_msghdr *)->ifam_addrs =
    802 			    info.rti_addrs;
    803 		}
    804 		if ((cmd == RTM_ADD && pass == 2) ||
    805 		    (cmd == RTM_DELETE && pass == 1)) {
    806 			struct rt_msghdr rtm;
    807 
    808 			if (rt == 0)
    809 				continue;
    810 			netmask = rt_mask(rt);
    811 			dst = sa = rt_key(rt);
    812 			gate = rt->rt_gateway;
    813 			memset(&rtm, 0, sizeof(rtm));
    814 			rtm.rtm_index = ifp->if_index;
    815 			rtm.rtm_flags |= rt->rt_flags;
    816 			rtm.rtm_errno = error;
    817 			m = rt_msg1(cmd, &info, (caddr_t)&rtm, sizeof(rtm));
    818 			if (m == NULL)
    819 				continue;
    820 			mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
    821 		}
    822 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
    823 		raw_input(m, &route_proto, &route_src, &route_dst);
    824 	}
    825 }
    826 
    827 /*
    828  * This is called to generate routing socket messages indicating
    829  * network interface arrival and departure.
    830  */
    831 void
    832 rt_ifannouncemsg(ifp, what)
    833 	struct ifnet *ifp;
    834 	int what;
    835 {
    836 	struct if_announcemsghdr ifan;
    837 	struct mbuf *m;
    838 	struct rt_addrinfo info;
    839 
    840 	if (route_cb.any_count == 0)
    841 		return;
    842 	memset(&info, 0, sizeof(info));
    843 	memset(&ifan, 0, sizeof(ifan));
    844 	ifan.ifan_index = ifp->if_index;
    845 	strcpy(ifan.ifan_name, ifp->if_xname);
    846 	ifan.ifan_what = what;
    847 	m = rt_msg1(RTM_IFANNOUNCE, &info, (caddr_t)&ifan, sizeof(ifan));
    848 	if (m == 0)
    849 		return;
    850 	route_proto.sp_protocol = 0;
    851 	raw_input(m, &route_proto, &route_src, &route_dst);
    852 }
    853 
    854 /*
    855  * This is used in dumping the kernel table via sysctl().
    856  */
    857 static int
    858 sysctl_dumpentry(rn, v)
    859 	struct radix_node *rn;
    860 	void *v;
    861 {
    862 	struct walkarg *w = v;
    863 	struct rtentry *rt = (struct rtentry *)rn;
    864 	int error = 0, size;
    865 	struct rt_addrinfo info;
    866 
    867 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    868 		return 0;
    869 	memset(&info, 0, sizeof(info));
    870 	dst = rt_key(rt);
    871 	gate = rt->rt_gateway;
    872 	netmask = rt_mask(rt);
    873 	genmask = rt->rt_genmask;
    874 	if (rt->rt_ifp) {
    875 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
    876 		ifaaddr = rt->rt_ifa->ifa_addr;
    877 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    878 			brdaddr = rt->rt_ifa->ifa_dstaddr;
    879 	}
    880 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    881 		return (error);
    882 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    883 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
    884 
    885 		rtm->rtm_flags = rt->rt_flags;
    886 		rtm->rtm_use = rt->rt_use;
    887 		rtm->rtm_rmx = rt->rt_rmx;
    888 		rtm->rtm_index = rt->rt_ifp->if_index;
    889 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    890 		rtm->rtm_addrs = info.rti_addrs;
    891 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    892 			w->w_where = NULL;
    893 		else
    894 			w->w_where += size;
    895 	}
    896 	return (error);
    897 }
    898 
    899 static int
    900 sysctl_iflist(af, w, type)
    901 	int	af;
    902 	struct	walkarg *w;
    903 	int type;
    904 {
    905 	struct ifnet *ifp;
    906 	struct ifaddr *ifa;
    907 	struct	rt_addrinfo info;
    908 	int	len, error = 0;
    909 
    910 	memset(&info, 0, sizeof(info));
    911 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
    912 		if (w->w_arg && w->w_arg != ifp->if_index)
    913 			continue;
    914 		ifa = ifp->if_addrlist.tqh_first;
    915 		ifpaddr = ifa->ifa_addr;
    916 		switch(type) {
    917 		case NET_RT_IFLIST:
    918 			error =
    919 			    rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len);
    920 			break;
    921 #ifdef COMPAT_14
    922 		case NET_RT_OIFLIST:
    923 			error =
    924 			    rt_msg2(RTM_OIFINFO, &info, (caddr_t)0, w, &len);
    925 			break;
    926 #endif
    927 		default:
    928 			panic("sysctl_iflist(1)");
    929 		}
    930 		if (error)
    931 			return (error);
    932 		ifpaddr = 0;
    933 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    934 			switch(type) {
    935 			case NET_RT_IFLIST: {
    936 				struct if_msghdr *ifm;
    937 
    938 				ifm = (struct if_msghdr *)w->w_tmem;
    939 				ifm->ifm_index = ifp->if_index;
    940 				ifm->ifm_flags = ifp->if_flags;
    941 				ifm->ifm_data = ifp->if_data;
    942 				ifm->ifm_addrs = info.rti_addrs;
    943 				error = copyout(ifm, w->w_where, len);
    944 				if (error)
    945 					return (error);
    946 				w->w_where += len;
    947 				break;
    948 			}
    949 
    950 #ifdef COMPAT_14
    951 			case NET_RT_OIFLIST: {
    952 				struct if_msghdr14 *ifm;
    953 
    954 				ifm = (struct if_msghdr14 *)w->w_tmem;
    955 				ifm->ifm_index = ifp->if_index;
    956 				ifm->ifm_flags = ifp->if_flags;
    957 				ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
    958 				ifm->ifm_data.ifi_addrlen =
    959 				    ifp->if_data.ifi_addrlen;
    960 				ifm->ifm_data.ifi_hdrlen =
    961 				    ifp->if_data.ifi_hdrlen;
    962 				ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
    963 				ifm->ifm_data.ifi_metric =
    964 				    ifp->if_data.ifi_metric;
    965 				ifm->ifm_data.ifi_baudrate =
    966 				    ifp->if_data.ifi_baudrate;
    967 				ifm->ifm_data.ifi_ipackets =
    968 				    ifp->if_data.ifi_ipackets;
    969 				ifm->ifm_data.ifi_ierrors =
    970 				    ifp->if_data.ifi_ierrors;
    971 				ifm->ifm_data.ifi_opackets =
    972 				    ifp->if_data.ifi_opackets;
    973 				ifm->ifm_data.ifi_oerrors =
    974 				    ifp->if_data.ifi_oerrors;
    975 				ifm->ifm_data.ifi_collisions =
    976 				    ifp->if_data.ifi_collisions;
    977 				ifm->ifm_data.ifi_ibytes =
    978 				    ifp->if_data.ifi_ibytes;
    979 				ifm->ifm_data.ifi_obytes =
    980 				    ifp->if_data.ifi_obytes;
    981 				ifm->ifm_data.ifi_imcasts =
    982 				    ifp->if_data.ifi_imcasts;
    983 				ifm->ifm_data.ifi_omcasts =
    984 				    ifp->if_data.ifi_omcasts;
    985 				ifm->ifm_data.ifi_iqdrops =
    986 				    ifp->if_data.ifi_iqdrops;
    987 				ifm->ifm_data.ifi_noproto =
    988 				    ifp->if_data.ifi_noproto;
    989 				ifm->ifm_data.ifi_lastchange =
    990 				    ifp->if_data.ifi_lastchange;
    991 				ifm->ifm_addrs = info.rti_addrs;
    992 				error = copyout(ifm, w->w_where, len);
    993 				if (error)
    994 					return (error);
    995 				w->w_where += len;
    996 				break;
    997 			}
    998 #endif
    999 			default:
   1000 				panic("sysctl_iflist(2)");
   1001 			}
   1002 		}
   1003 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
   1004 			if (af && af != ifa->ifa_addr->sa_family)
   1005 				continue;
   1006 			ifaaddr = ifa->ifa_addr;
   1007 			netmask = ifa->ifa_netmask;
   1008 			brdaddr = ifa->ifa_dstaddr;
   1009 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
   1010 				return (error);
   1011 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1012 				struct ifa_msghdr *ifam;
   1013 
   1014 				ifam = (struct ifa_msghdr *)w->w_tmem;
   1015 				ifam->ifam_index = ifa->ifa_ifp->if_index;
   1016 				ifam->ifam_flags = ifa->ifa_flags;
   1017 				ifam->ifam_metric = ifa->ifa_metric;
   1018 				ifam->ifam_addrs = info.rti_addrs;
   1019 				error = copyout(w->w_tmem, w->w_where, len);
   1020 				if (error)
   1021 					return (error);
   1022 				w->w_where += len;
   1023 			}
   1024 		}
   1025 		ifaaddr = netmask = brdaddr = 0;
   1026 	}
   1027 	return (0);
   1028 }
   1029 
   1030 static int
   1031 sysctl_rtable(name, namelen, where, given, new, newlen)
   1032 	int	*name;
   1033 	u_int	namelen;
   1034 	void 	*where;
   1035 	size_t	*given;
   1036 	void	*new;
   1037 	size_t	newlen;
   1038 {
   1039 	struct radix_node_head *rnh;
   1040 	int	i, s, error = EINVAL;
   1041 	u_char  af;
   1042 	struct	walkarg w;
   1043 
   1044 	if (new)
   1045 		return (EPERM);
   1046 	if (namelen != 3)
   1047 		return (EINVAL);
   1048 	af = name[0];
   1049 	w.w_tmemneeded = 0;
   1050 	w.w_tmemsize = 0;
   1051 	w.w_tmem = NULL;
   1052 again:
   1053 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1054 	if (w.w_tmemneeded) {
   1055 		w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
   1056 		w.w_tmemsize = w.w_tmemneeded;
   1057 		w.w_tmemneeded = 0;
   1058 	}
   1059 	w.w_op = name[1];
   1060 	w.w_arg = name[2];
   1061 	w.w_given = *given;
   1062 	w.w_needed = 0 - w.w_given;
   1063 	w.w_where = where;
   1064 
   1065 	s = splsoftnet();
   1066 	switch (w.w_op) {
   1067 
   1068 	case NET_RT_DUMP:
   1069 	case NET_RT_FLAGS:
   1070 		for (i = 1; i <= AF_MAX; i++)
   1071 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
   1072 			    (error = (*rnh->rnh_walktree)(rnh,
   1073 			    sysctl_dumpentry, &w)))
   1074 				break;
   1075 		break;
   1076 
   1077 #ifdef COMPAT_14
   1078 	case NET_RT_OIFLIST:
   1079 		error = sysctl_iflist(af, &w, w.w_op);
   1080 		break;
   1081 #endif
   1082 
   1083 	case NET_RT_IFLIST:
   1084 		error = sysctl_iflist(af, &w, w.w_op);
   1085 	}
   1086 	splx(s);
   1087 
   1088 	/* check to see if we couldn't allocate memory with NOWAIT */
   1089 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   1090 		goto again;
   1091 
   1092 	if (w.w_tmem)
   1093 		free(w.w_tmem, M_RTABLE);
   1094 	w.w_needed += w.w_given;
   1095 	if (where) {
   1096 		*given = w.w_where - (caddr_t) where;
   1097 		if (*given < w.w_needed)
   1098 			return (ENOMEM);
   1099 	} else {
   1100 		*given = (11 * w.w_needed) / 10;
   1101 	}
   1102 	return (error);
   1103 }
   1104 
   1105 /*
   1106  * Definitions of protocols supported in the ROUTE domain.
   1107  */
   1108 
   1109 extern	struct domain routedomain;		/* or at least forward */
   1110 
   1111 struct protosw routesw[] = {
   1112 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
   1113   raw_input,	route_output,	raw_ctlinput,	0,
   1114   route_usrreq,
   1115   raw_init,	0,		0,		0,
   1116   sysctl_rtable,
   1117 }
   1118 };
   1119 
   1120 struct domain routedomain =
   1121     { PF_ROUTE, "route", route_init, 0, 0,
   1122       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
   1123