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