Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.26
      1 /*	$NetBSD: rtsock.c,v 1.26 1998/03/01 02:25:05 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1991, 1993
      5  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/proc.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/socket.h>
     43 #include <sys/socketvar.h>
     44 #include <sys/domain.h>
     45 #include <sys/protosw.h>
     46 
     47 #include <vm/vm.h>
     48 #include <sys/sysctl.h>
     49 
     50 #include <net/if.h>
     51 #include <net/route.h>
     52 #include <net/raw_cb.h>
     53 
     54 #include <machine/stdarg.h>
     55 
     56 struct	sockaddr route_dst = { 2, PF_ROUTE, };
     57 struct	sockaddr route_src = { 2, PF_ROUTE, };
     58 struct	sockproto route_proto = { PF_ROUTE, };
     59 
     60 struct walkarg {
     61 	int	w_op, w_arg, w_given, w_needed, w_tmemsize;
     62 	caddr_t	w_where, w_tmem;
     63 };
     64 
     65 static struct mbuf *rt_msg1 __P((int, struct rt_addrinfo *));
     66 static int rt_msg2 __P((int, struct rt_addrinfo *, caddr_t, struct walkarg *));
     67 static void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
     68 
     69 /* Sleazy use of local variables throughout file, warning!!!! */
     70 #define dst	info.rti_info[RTAX_DST]
     71 #define gate	info.rti_info[RTAX_GATEWAY]
     72 #define netmask	info.rti_info[RTAX_NETMASK]
     73 #define genmask	info.rti_info[RTAX_GENMASK]
     74 #define ifpaddr	info.rti_info[RTAX_IFP]
     75 #define ifaaddr	info.rti_info[RTAX_IFA]
     76 #define brdaddr	info.rti_info[RTAX_BRD]
     77 
     78 /*ARGSUSED*/
     79 int
     80 route_usrreq(so, req, m, nam, control, p)
     81 	register struct socket *so;
     82 	int req;
     83 	struct mbuf *m, *nam, *control;
     84 	struct proc *p;
     85 {
     86 	register int error = 0;
     87 	register struct rawcb *rp = sotorawcb(so);
     88 	int s;
     89 
     90 	if (req == PRU_ATTACH) {
     91 		MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
     92 		if ((so->so_pcb = rp) != NULL)
     93 			bzero(so->so_pcb, sizeof(*rp));
     94 
     95 	}
     96 	if (req == PRU_DETACH && rp) {
     97 		int af = rp->rcb_proto.sp_protocol;
     98 		if (af == AF_INET)
     99 			route_cb.ip_count--;
    100 		else if (af == AF_NS)
    101 			route_cb.ns_count--;
    102 		else if (af == AF_ISO)
    103 			route_cb.iso_count--;
    104 		route_cb.any_count--;
    105 	}
    106 
    107 	s = splsoftnet();
    108 
    109 	/*
    110 	 * Don't call raw_usrreq() in the attach case, because
    111 	 * we want to allow non-privileged processes to listen on
    112 	 * and send "safe" commands to the routing socket.
    113 	 */
    114 	if (req == PRU_ATTACH) {
    115 		if (p == 0)
    116 			error = EACCES;
    117 		else
    118 			error = raw_attach(so, (int)(long)nam);
    119 	} else
    120 		error = raw_usrreq(so, req, m, nam, control, p);
    121 
    122 	rp = sotorawcb(so);
    123 	if (req == PRU_ATTACH && rp) {
    124 		int af = rp->rcb_proto.sp_protocol;
    125 		if (error) {
    126 			free((caddr_t)rp, M_PCB);
    127 			splx(s);
    128 			return (error);
    129 		}
    130 		if (af == AF_INET)
    131 			route_cb.ip_count++;
    132 		else if (af == AF_NS)
    133 			route_cb.ns_count++;
    134 		else if (af == AF_ISO)
    135 			route_cb.iso_count++;
    136 		route_cb.any_count++;
    137 		rp->rcb_laddr = &route_src;
    138 		rp->rcb_faddr = &route_dst;
    139 		soisconnected(so);
    140 		so->so_options |= SO_USELOOPBACK;
    141 	}
    142 	splx(s);
    143 	return (error);
    144 }
    145 
    146 /*ARGSUSED*/
    147 int
    148 #if __STDC__
    149 route_output(struct mbuf *m, ...)
    150 #else
    151 route_output(m, va_alist)
    152 	struct mbuf *m;
    153 	va_dcl
    154 #endif
    155 {
    156 	register struct rt_msghdr *rtm = 0;
    157 	register struct rtentry *rt = 0;
    158 	struct rtentry *saved_nrt = 0;
    159 	struct radix_node_head *rnh;
    160 	struct rt_addrinfo info;
    161 	int len, error = 0;
    162 	struct ifnet *ifp = 0;
    163 	struct ifaddr *ifa = 0;
    164 	struct socket *so;
    165 	va_list ap;
    166 
    167 	va_start(ap, m);
    168 	so = va_arg(ap, struct socket *);
    169 	va_end(ap);
    170 
    171 
    172 #define senderr(e) { error = e; goto flush;}
    173 	if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
    174 	   (m = m_pullup(m, sizeof(int32_t))) == 0))
    175 		return (ENOBUFS);
    176 	if ((m->m_flags & M_PKTHDR) == 0)
    177 		panic("route_output");
    178 	len = m->m_pkthdr.len;
    179 	if (len < sizeof(*rtm) ||
    180 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
    181 		dst = 0;
    182 		senderr(EINVAL);
    183 	}
    184 	R_Malloc(rtm, struct rt_msghdr *, len);
    185 	if (rtm == 0) {
    186 		dst = 0;
    187 		senderr(ENOBUFS);
    188 	}
    189 	m_copydata(m, 0, len, (caddr_t)rtm);
    190 	if (rtm->rtm_version != RTM_VERSION) {
    191 		dst = 0;
    192 		senderr(EPROTONOSUPPORT);
    193 	}
    194 	rtm->rtm_pid = curproc->p_pid;
    195 	info.rti_addrs = rtm->rtm_addrs;
    196 	rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info);
    197 	if (dst == 0 || (dst->sa_family >= AF_MAX))
    198 		senderr(EINVAL);
    199 	if (gate != 0 && (gate->sa_family >= AF_MAX))
    200 		senderr(EINVAL);
    201 	if (genmask) {
    202 		struct radix_node *t;
    203 		t = rn_addmask((caddr_t)genmask, 0, 1);
    204 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
    205 			genmask = (struct sockaddr *)(t->rn_key);
    206 		else
    207 			senderr(ENOBUFS);
    208 	}
    209 
    210 	/*
    211 	 * Verify that the caller has the appropriate privilege; RTM_GET
    212 	 * is the only operation the non-superuser is allowed.
    213 	 */
    214 	if (rtm->rtm_type != RTM_GET &&
    215 	    suser(curproc->p_ucred, &curproc->p_acflag) != 0)
    216 		senderr(EACCES);
    217 
    218 	switch (rtm->rtm_type) {
    219 
    220 	case RTM_ADD:
    221 		if (gate == 0)
    222 			senderr(EINVAL);
    223 		error = rtrequest(RTM_ADD, dst, gate, netmask,
    224 		    rtm->rtm_flags, &saved_nrt);
    225 		if (error == 0 && saved_nrt) {
    226 			rt_setmetrics(rtm->rtm_inits,
    227 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
    228 			saved_nrt->rt_refcnt--;
    229 			saved_nrt->rt_genmask = genmask;
    230 		}
    231 		break;
    232 
    233 	case RTM_DELETE:
    234 		error = rtrequest(RTM_DELETE, dst, gate, netmask,
    235 		    rtm->rtm_flags, &saved_nrt);
    236 		if (error == 0) {
    237 			(rt = saved_nrt)->rt_refcnt++;
    238 			goto report;
    239 		}
    240 		break;
    241 
    242 	case RTM_GET:
    243 	case RTM_CHANGE:
    244 	case RTM_LOCK:
    245 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
    246 			senderr(EAFNOSUPPORT);
    247 		} else if ((rt = (struct rtentry *)
    248 		    rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
    249 			rt->rt_refcnt++;
    250 		else
    251 			senderr(ESRCH);
    252 		switch(rtm->rtm_type) {
    253 
    254 		case RTM_GET:
    255 		report:
    256 			dst = rt_key(rt);
    257 			gate = rt->rt_gateway;
    258 			netmask = rt_mask(rt);
    259 			genmask = rt->rt_genmask;
    260 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
    261 				if ((ifp = rt->rt_ifp) != NULL) {
    262 					ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    263 					ifaaddr = rt->rt_ifa->ifa_addr;
    264 					if (ifp->if_flags & IFF_POINTOPOINT)
    265 						brdaddr = rt->rt_ifa->ifa_dstaddr;
    266 					else
    267 						brdaddr = 0;
    268 					rtm->rtm_index = ifp->if_index;
    269 				} else {
    270 					ifpaddr = 0;
    271 					ifaaddr = 0;
    272 			    }
    273 			}
    274 			len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
    275 			    (struct walkarg *)0);
    276 			if (len > rtm->rtm_msglen) {
    277 				struct rt_msghdr *new_rtm;
    278 				R_Malloc(new_rtm, struct rt_msghdr *, len);
    279 				if (new_rtm == 0)
    280 					senderr(ENOBUFS);
    281 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
    282 				Free(rtm); rtm = new_rtm;
    283 			}
    284 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
    285 			    (struct walkarg *)0);
    286 			rtm->rtm_flags = rt->rt_flags;
    287 			rtm->rtm_rmx = rt->rt_rmx;
    288 			rtm->rtm_addrs = info.rti_addrs;
    289 			break;
    290 
    291 		case RTM_CHANGE:
    292 			if (gate && rt_setgate(rt, rt_key(rt), gate))
    293 				senderr(EDQUOT);
    294 			/* new gateway could require new ifaddr, ifp;
    295 			   flags may also be different; ifp may be specified
    296 			   by ll sockaddr when protocol address is ambiguous */
    297 			if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
    298 			    (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
    299 				ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
    300 				    ifp);
    301 			else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
    302 			    (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
    303 			    rt_key(rt), gate))))
    304 				ifp = ifa->ifa_ifp;
    305 			if (ifa) {
    306 				register struct ifaddr *oifa = rt->rt_ifa;
    307 				if (oifa != ifa) {
    308 				    if (oifa && oifa->ifa_rtrequest)
    309 					oifa->ifa_rtrequest(RTM_DELETE,
    310 					rt, gate);
    311 				    IFAFREE(rt->rt_ifa);
    312 				    rt->rt_ifa = ifa;
    313 				    ifa->ifa_refcnt++;
    314 				    rt->rt_ifp = ifp;
    315 				}
    316 			}
    317 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
    318 			    &rt->rt_rmx);
    319 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    320 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
    321 			if (genmask)
    322 				rt->rt_genmask = genmask;
    323 			/*
    324 			 * Fall into
    325 			 */
    326 		case RTM_LOCK:
    327 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    328 			rt->rt_rmx.rmx_locks |=
    329 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    330 			break;
    331 		}
    332 		break;
    333 
    334 	default:
    335 		senderr(EOPNOTSUPP);
    336 	}
    337 
    338 flush:
    339 	if (rtm) {
    340 		if (error)
    341 			rtm->rtm_errno = error;
    342 		else
    343 			rtm->rtm_flags |= RTF_DONE;
    344 	}
    345 	if (rt)
    346 		rtfree(rt);
    347     {
    348 	register struct rawcb *rp = 0;
    349 	/*
    350 	 * Check to see if we don't want our own messages.
    351 	 */
    352 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    353 		if (route_cb.any_count <= 1) {
    354 			if (rtm)
    355 				Free(rtm);
    356 			m_freem(m);
    357 			return (error);
    358 		}
    359 		/* There is another listener, so construct message */
    360 		rp = sotorawcb(so);
    361 	}
    362 	if (rtm) {
    363 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    364 		Free(rtm);
    365 	}
    366 	if (rp)
    367 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    368 	if (dst)
    369 		route_proto.sp_protocol = dst->sa_family;
    370 	raw_input(m, &route_proto, &route_src, &route_dst);
    371 	if (rp)
    372 		rp->rcb_proto.sp_family = PF_ROUTE;
    373     }
    374 	return (error);
    375 }
    376 
    377 void
    378 rt_setmetrics(which, in, out)
    379 	u_long which;
    380 	register struct rt_metrics *in, *out;
    381 {
    382 #define metric(f, e) if (which & (f)) out->e = in->e;
    383 	metric(RTV_RPIPE, rmx_recvpipe);
    384 	metric(RTV_SPIPE, rmx_sendpipe);
    385 	metric(RTV_SSTHRESH, rmx_ssthresh);
    386 	metric(RTV_RTT, rmx_rtt);
    387 	metric(RTV_RTTVAR, rmx_rttvar);
    388 	metric(RTV_HOPCOUNT, rmx_hopcount);
    389 	metric(RTV_MTU, rmx_mtu);
    390 	metric(RTV_EXPIRE, rmx_expire);
    391 #undef metric
    392 }
    393 
    394 #define ROUNDUP(a) \
    395 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    396 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    397 
    398 static void
    399 rt_xaddrs(cp, cplim, rtinfo)
    400 	register caddr_t cp, cplim;
    401 	register struct rt_addrinfo *rtinfo;
    402 {
    403 	register struct sockaddr *sa;
    404 	register int i;
    405 
    406 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
    407 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
    408 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    409 			continue;
    410 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
    411 		ADVANCE(cp, sa);
    412 	}
    413 }
    414 
    415 static struct mbuf *
    416 rt_msg1(type, rtinfo)
    417 	int type;
    418 	register struct rt_addrinfo *rtinfo;
    419 {
    420 	register struct rt_msghdr *rtm;
    421 	register struct mbuf *m;
    422 	register int i;
    423 	register struct sockaddr *sa;
    424 	int len, dlen;
    425 
    426 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    427 	if (m == 0)
    428 		return (m);
    429 	switch (type) {
    430 
    431 	case RTM_DELADDR:
    432 	case RTM_NEWADDR:
    433 		len = sizeof(struct ifa_msghdr);
    434 		break;
    435 
    436 	case RTM_IFINFO:
    437 		len = sizeof(struct if_msghdr);
    438 		break;
    439 
    440 	default:
    441 		len = sizeof(struct rt_msghdr);
    442 	}
    443 	if (len > MHLEN)
    444 		panic("rt_msg1");
    445 	m->m_pkthdr.len = m->m_len = len;
    446 	m->m_pkthdr.rcvif = 0;
    447 	rtm = mtod(m, struct rt_msghdr *);
    448 	bzero(rtm, len);
    449 	for (i = 0; i < RTAX_MAX; i++) {
    450 		if ((sa = rtinfo->rti_info[i]) == NULL)
    451 			continue;
    452 		rtinfo->rti_addrs |= (1 << i);
    453 		dlen = ROUNDUP(sa->sa_len);
    454 		m_copyback(m, len, dlen, (caddr_t)sa);
    455 		len += dlen;
    456 	}
    457 	if (m->m_pkthdr.len != len) {
    458 		m_freem(m);
    459 		return (NULL);
    460 	}
    461 	rtm->rtm_msglen = len;
    462 	rtm->rtm_version = RTM_VERSION;
    463 	rtm->rtm_type = type;
    464 	return (m);
    465 }
    466 
    467 static int
    468 rt_msg2(type, rtinfo, cp, w)
    469 	int type;
    470 	register struct rt_addrinfo *rtinfo;
    471 	caddr_t cp;
    472 	struct walkarg *w;
    473 {
    474 	register int i;
    475 	int len, dlen, second_time = 0;
    476 	caddr_t cp0;
    477 
    478 	rtinfo->rti_addrs = 0;
    479 again:
    480 	switch (type) {
    481 
    482 	case RTM_DELADDR:
    483 	case RTM_NEWADDR:
    484 		len = sizeof(struct ifa_msghdr);
    485 		break;
    486 
    487 	case RTM_IFINFO:
    488 		len = sizeof(struct if_msghdr);
    489 		break;
    490 
    491 	default:
    492 		len = sizeof(struct rt_msghdr);
    493 	}
    494 	if ((cp0 = cp) != NULL)
    495 		cp += len;
    496 	for (i = 0; i < RTAX_MAX; i++) {
    497 		register struct sockaddr *sa;
    498 
    499 		if ((sa = rtinfo->rti_info[i]) == 0)
    500 			continue;
    501 		rtinfo->rti_addrs |= (1 << i);
    502 		dlen = ROUNDUP(sa->sa_len);
    503 		if (cp) {
    504 			bcopy(sa, cp, (unsigned)dlen);
    505 			cp += dlen;
    506 		}
    507 		len += dlen;
    508 	}
    509 	if (cp == 0 && w != NULL && !second_time) {
    510 		register struct walkarg *rw = w;
    511 
    512 		rw->w_needed += len;
    513 		if (rw->w_needed <= 0 && rw->w_where) {
    514 			if (rw->w_tmemsize < len) {
    515 				if (rw->w_tmem)
    516 					free(rw->w_tmem, M_RTABLE);
    517 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
    518 				    M_NOWAIT);
    519 				if (rw->w_tmem)
    520 					rw->w_tmemsize = len;
    521 			}
    522 			if (rw->w_tmem) {
    523 				cp = rw->w_tmem;
    524 				second_time = 1;
    525 				goto again;
    526 			} else
    527 				rw->w_where = 0;
    528 		}
    529 	}
    530 	if (cp) {
    531 		register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    532 
    533 		rtm->rtm_version = RTM_VERSION;
    534 		rtm->rtm_type = type;
    535 		rtm->rtm_msglen = len;
    536 	}
    537 	return (len);
    538 }
    539 
    540 /*
    541  * This routine is called to generate a message from the routing
    542  * socket indicating that a redirect has occured, a routing lookup
    543  * has failed, or that a protocol has detected timeouts to a particular
    544  * destination.
    545  */
    546 void
    547 rt_missmsg(type, rtinfo, flags, error)
    548 	int type, flags, error;
    549 	register struct rt_addrinfo *rtinfo;
    550 {
    551 	register struct rt_msghdr *rtm;
    552 	register struct mbuf *m;
    553 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    554 
    555 	if (route_cb.any_count == 0)
    556 		return;
    557 	m = rt_msg1(type, rtinfo);
    558 	if (m == 0)
    559 		return;
    560 	rtm = mtod(m, struct rt_msghdr *);
    561 	rtm->rtm_flags = RTF_DONE | flags;
    562 	rtm->rtm_errno = error;
    563 	rtm->rtm_addrs = rtinfo->rti_addrs;
    564 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
    565 	raw_input(m, &route_proto, &route_src, &route_dst);
    566 }
    567 
    568 /*
    569  * This routine is called to generate a message from the routing
    570  * socket indicating that the status of a network interface has changed.
    571  */
    572 void
    573 rt_ifmsg(ifp)
    574 	register struct ifnet *ifp;
    575 {
    576 	register struct if_msghdr *ifm;
    577 	struct mbuf *m;
    578 	struct rt_addrinfo info;
    579 
    580 	if (route_cb.any_count == 0)
    581 		return;
    582 	bzero(&info, sizeof(info));
    583 	m = rt_msg1(RTM_IFINFO, &info);
    584 	if (m == 0)
    585 		return;
    586 	ifm = mtod(m, struct if_msghdr *);
    587 	ifm->ifm_index = ifp->if_index;
    588 	ifm->ifm_flags = ifp->if_flags;
    589 	ifm->ifm_data = ifp->if_data;
    590 	ifm->ifm_addrs = 0;
    591 	route_proto.sp_protocol = 0;
    592 	raw_input(m, &route_proto, &route_src, &route_dst);
    593 }
    594 
    595 /*
    596  * This is called to generate messages from the routing socket
    597  * indicating a network interface has had addresses associated with it.
    598  * if we ever reverse the logic and replace messages TO the routing
    599  * socket indicate a request to configure interfaces, then it will
    600  * be unnecessary as the routing socket will automatically generate
    601  * copies of it.
    602  */
    603 void
    604 rt_newaddrmsg(cmd, ifa, error, rt)
    605 	int cmd, error;
    606 	register struct ifaddr *ifa;
    607 	register struct rtentry *rt;
    608 {
    609 	struct rt_addrinfo info;
    610 	struct sockaddr *sa = NULL;
    611 	int pass;
    612 	struct mbuf *m = NULL;
    613 	struct ifnet *ifp = ifa->ifa_ifp;
    614 
    615 	if (route_cb.any_count == 0)
    616 		return;
    617 	for (pass = 1; pass < 3; pass++) {
    618 		bzero(&info, sizeof(info));
    619 		if ((cmd == RTM_ADD && pass == 1) ||
    620 		    (cmd == RTM_DELETE && pass == 2)) {
    621 			register struct ifa_msghdr *ifam;
    622 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
    623 
    624 			ifaaddr = sa = ifa->ifa_addr;
    625 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    626 			netmask = ifa->ifa_netmask;
    627 			brdaddr = ifa->ifa_dstaddr;
    628 			if ((m = rt_msg1(ncmd, &info)) == NULL)
    629 				continue;
    630 			ifam = mtod(m, struct ifa_msghdr *);
    631 			ifam->ifam_index = ifp->if_index;
    632 			ifam->ifam_metric = ifa->ifa_metric;
    633 			ifam->ifam_flags = ifa->ifa_flags;
    634 			ifam->ifam_addrs = info.rti_addrs;
    635 		}
    636 		if ((cmd == RTM_ADD && pass == 2) ||
    637 		    (cmd == RTM_DELETE && pass == 1)) {
    638 			register struct rt_msghdr *rtm;
    639 
    640 			if (rt == 0)
    641 				continue;
    642 			netmask = rt_mask(rt);
    643 			dst = sa = rt_key(rt);
    644 			gate = rt->rt_gateway;
    645 			if ((m = rt_msg1(cmd, &info)) == NULL)
    646 				continue;
    647 			rtm = mtod(m, struct rt_msghdr *);
    648 			rtm->rtm_index = ifp->if_index;
    649 			rtm->rtm_flags |= rt->rt_flags;
    650 			rtm->rtm_errno = error;
    651 			rtm->rtm_addrs = info.rti_addrs;
    652 		}
    653 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
    654 		raw_input(m, &route_proto, &route_src, &route_dst);
    655 	}
    656 }
    657 
    658 /*
    659  * This is used in dumping the kernel table via sysctl().
    660  */
    661 int
    662 sysctl_dumpentry(rn, v)
    663 	struct radix_node *rn;
    664 	register void *v;
    665 {
    666 	register struct walkarg *w = v;
    667 	register struct rtentry *rt = (struct rtentry *)rn;
    668 	int error = 0, size;
    669 	struct rt_addrinfo info;
    670 
    671 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    672 		return 0;
    673 	bzero(&info, sizeof(info));
    674 	dst = rt_key(rt);
    675 	gate = rt->rt_gateway;
    676 	netmask = rt_mask(rt);
    677 	genmask = rt->rt_genmask;
    678 	if (rt->rt_ifp) {
    679 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
    680 		ifaaddr = rt->rt_ifa->ifa_addr;
    681 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    682 			brdaddr = rt->rt_ifa->ifa_dstaddr;
    683 	}
    684 	size = rt_msg2(RTM_GET, &info, 0, w);
    685 	if (w->w_where && w->w_tmem) {
    686 		register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
    687 
    688 		rtm->rtm_flags = rt->rt_flags;
    689 		rtm->rtm_use = rt->rt_use;
    690 		rtm->rtm_rmx = rt->rt_rmx;
    691 		rtm->rtm_index = rt->rt_ifp->if_index;
    692 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    693 		rtm->rtm_addrs = info.rti_addrs;
    694 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    695 			w->w_where = NULL;
    696 		else
    697 			w->w_where += size;
    698 	}
    699 	return (error);
    700 }
    701 
    702 int
    703 sysctl_iflist(af, w)
    704 	int	af;
    705 	register struct	walkarg *w;
    706 {
    707 	register struct ifnet *ifp;
    708 	register struct ifaddr *ifa;
    709 	struct	rt_addrinfo info;
    710 	int	len, error = 0;
    711 
    712 	bzero(&info, sizeof(info));
    713 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
    714 		if (w->w_arg && w->w_arg != ifp->if_index)
    715 			continue;
    716 		ifa = ifp->if_addrlist.tqh_first;
    717 		ifpaddr = ifa->ifa_addr;
    718 		len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
    719 		ifpaddr = 0;
    720 		if (w->w_where && w->w_tmem) {
    721 			register struct if_msghdr *ifm;
    722 
    723 			ifm = (struct if_msghdr *)w->w_tmem;
    724 			ifm->ifm_index = ifp->if_index;
    725 			ifm->ifm_flags = ifp->if_flags;
    726 			ifm->ifm_data = ifp->if_data;
    727 			ifm->ifm_addrs = info.rti_addrs;
    728 			error = copyout(ifm, w->w_where, len);
    729 			if (error)
    730 				return (error);
    731 			w->w_where += len;
    732 		}
    733 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
    734 			if (af && af != ifa->ifa_addr->sa_family)
    735 				continue;
    736 			ifaaddr = ifa->ifa_addr;
    737 			netmask = ifa->ifa_netmask;
    738 			brdaddr = ifa->ifa_dstaddr;
    739 			len = rt_msg2(RTM_NEWADDR, &info, 0, w);
    740 			if (w->w_where && w->w_tmem) {
    741 				register struct ifa_msghdr *ifam;
    742 
    743 				ifam = (struct ifa_msghdr *)w->w_tmem;
    744 				ifam->ifam_index = ifa->ifa_ifp->if_index;
    745 				ifam->ifam_flags = ifa->ifa_flags;
    746 				ifam->ifam_metric = ifa->ifa_metric;
    747 				ifam->ifam_addrs = info.rti_addrs;
    748 				error = copyout(w->w_tmem, w->w_where, len);
    749 				if (error)
    750 					return (error);
    751 				w->w_where += len;
    752 			}
    753 		}
    754 		ifaaddr = netmask = brdaddr = 0;
    755 	}
    756 	return (0);
    757 }
    758 
    759 int
    760 sysctl_rtable(name, namelen, where, given, new, newlen)
    761 	int	*name;
    762 	u_int	namelen;
    763 	void 	*where;
    764 	size_t	*given;
    765 	void	*new;
    766 	size_t	newlen;
    767 {
    768 	register struct radix_node_head *rnh;
    769 	int	i, s, error = EINVAL;
    770 	u_char  af;
    771 	struct	walkarg w;
    772 
    773 	if (new)
    774 		return (EPERM);
    775 	if (namelen != 3)
    776 		return (EINVAL);
    777 	af = name[0];
    778 	Bzero(&w, sizeof(w));
    779 	w.w_where = where;
    780 	w.w_given = *given;
    781 	w.w_needed = 0 - w.w_given;
    782 	w.w_op = name[1];
    783 	w.w_arg = name[2];
    784 
    785 	s = splsoftnet();
    786 	switch (w.w_op) {
    787 
    788 	case NET_RT_DUMP:
    789 	case NET_RT_FLAGS:
    790 		for (i = 1; i <= AF_MAX; i++)
    791 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
    792 			    (error = (*rnh->rnh_walktree)(rnh,
    793 			    sysctl_dumpentry, &w)))
    794 				break;
    795 		break;
    796 
    797 	case NET_RT_IFLIST:
    798 		error = sysctl_iflist(af, &w);
    799 	}
    800 	splx(s);
    801 	if (w.w_tmem)
    802 		free(w.w_tmem, M_RTABLE);
    803 	w.w_needed += w.w_given;
    804 	if (where) {
    805 		*given = w.w_where - (caddr_t) where;
    806 		if (*given < w.w_needed)
    807 			return (ENOMEM);
    808 	} else {
    809 		*given = (11 * w.w_needed) / 10;
    810 	}
    811 	return (error);
    812 }
    813 
    814 /*
    815  * Definitions of protocols supported in the ROUTE domain.
    816  */
    817 
    818 extern	struct domain routedomain;		/* or at least forward */
    819 
    820 struct protosw routesw[] = {
    821 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
    822   raw_input,	route_output,	raw_ctlinput,	0,
    823   route_usrreq,
    824   raw_init,	0,		0,		0,
    825   sysctl_rtable,
    826 }
    827 };
    828 
    829 struct domain routedomain =
    830     { PF_ROUTE, "route", route_init, 0, 0,
    831       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
    832