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