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