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