xref: /netbsd-src/sys/net/rtsock.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: rtsock.c,v 1.25 1997/12/10 00:47:57 christos 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.6 (Berkeley) 2/11/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)
198 		senderr(EINVAL);
199 	if (genmask) {
200 		struct radix_node *t;
201 		t = rn_addmask((caddr_t)genmask, 0, 1);
202 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
203 			genmask = (struct sockaddr *)(t->rn_key);
204 		else
205 			senderr(ENOBUFS);
206 	}
207 
208 	/*
209 	 * Verify that the caller has the appropriate privilege; RTM_GET
210 	 * is the only operation the non-superuser is allowed.
211 	 */
212 	if (rtm->rtm_type != RTM_GET &&
213 	    suser(curproc->p_ucred, &curproc->p_acflag) != 0)
214 		senderr(EACCES);
215 
216 	switch (rtm->rtm_type) {
217 
218 	case RTM_ADD:
219 		if (gate == 0)
220 			senderr(EINVAL);
221 		error = rtrequest(RTM_ADD, dst, gate, netmask,
222 		    rtm->rtm_flags, &saved_nrt);
223 		if (error == 0 && saved_nrt) {
224 			rt_setmetrics(rtm->rtm_inits,
225 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
226 			saved_nrt->rt_refcnt--;
227 			saved_nrt->rt_genmask = genmask;
228 		}
229 		break;
230 
231 	case RTM_DELETE:
232 		error = rtrequest(RTM_DELETE, dst, gate, netmask,
233 		    rtm->rtm_flags, &saved_nrt);
234 		if (error == 0) {
235 			(rt = saved_nrt)->rt_refcnt++;
236 			goto report;
237 		}
238 		break;
239 
240 	case RTM_GET:
241 	case RTM_CHANGE:
242 	case RTM_LOCK:
243 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
244 			senderr(EAFNOSUPPORT);
245 		} else if ((rt = (struct rtentry *)
246 		    rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
247 			rt->rt_refcnt++;
248 		else
249 			senderr(ESRCH);
250 		switch(rtm->rtm_type) {
251 
252 		case RTM_GET:
253 		report:
254 			dst = rt_key(rt);
255 			gate = rt->rt_gateway;
256 			netmask = rt_mask(rt);
257 			genmask = rt->rt_genmask;
258 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
259 				if ((ifp = rt->rt_ifp) != NULL) {
260 					ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
261 					ifaaddr = rt->rt_ifa->ifa_addr;
262 					if (ifp->if_flags & IFF_POINTOPOINT)
263 						brdaddr = rt->rt_ifa->ifa_dstaddr;
264 					else
265 						brdaddr = 0;
266 					rtm->rtm_index = ifp->if_index;
267 				} else {
268 					ifpaddr = 0;
269 					ifaaddr = 0;
270 			    }
271 			}
272 			len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
273 			    (struct walkarg *)0);
274 			if (len > rtm->rtm_msglen) {
275 				struct rt_msghdr *new_rtm;
276 				R_Malloc(new_rtm, struct rt_msghdr *, len);
277 				if (new_rtm == 0)
278 					senderr(ENOBUFS);
279 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
280 				Free(rtm); rtm = new_rtm;
281 			}
282 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
283 			    (struct walkarg *)0);
284 			rtm->rtm_flags = rt->rt_flags;
285 			rtm->rtm_rmx = rt->rt_rmx;
286 			rtm->rtm_addrs = info.rti_addrs;
287 			break;
288 
289 		case RTM_CHANGE:
290 			if (gate && rt_setgate(rt, rt_key(rt), gate))
291 				senderr(EDQUOT);
292 			/* new gateway could require new ifaddr, ifp;
293 			   flags may also be different; ifp may be specified
294 			   by ll sockaddr when protocol address is ambiguous */
295 			if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
296 			    (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
297 				ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
298 				    ifp);
299 			else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
300 			    (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
301 			    rt_key(rt), gate))))
302 				ifp = ifa->ifa_ifp;
303 			if (ifa) {
304 				register struct ifaddr *oifa = rt->rt_ifa;
305 				if (oifa != ifa) {
306 				    if (oifa && oifa->ifa_rtrequest)
307 					oifa->ifa_rtrequest(RTM_DELETE,
308 					rt, gate);
309 				    IFAFREE(rt->rt_ifa);
310 				    rt->rt_ifa = ifa;
311 				    ifa->ifa_refcnt++;
312 				    rt->rt_ifp = ifp;
313 				}
314 			}
315 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
316 			    &rt->rt_rmx);
317 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
318 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
319 			if (genmask)
320 				rt->rt_genmask = genmask;
321 			/*
322 			 * Fall into
323 			 */
324 		case RTM_LOCK:
325 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
326 			rt->rt_rmx.rmx_locks |=
327 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
328 			break;
329 		}
330 		break;
331 
332 	default:
333 		senderr(EOPNOTSUPP);
334 	}
335 
336 flush:
337 	if (rtm) {
338 		if (error)
339 			rtm->rtm_errno = error;
340 		else
341 			rtm->rtm_flags |= RTF_DONE;
342 	}
343 	if (rt)
344 		rtfree(rt);
345     {
346 	register struct rawcb *rp = 0;
347 	/*
348 	 * Check to see if we don't want our own messages.
349 	 */
350 	if ((so->so_options & SO_USELOOPBACK) == 0) {
351 		if (route_cb.any_count <= 1) {
352 			if (rtm)
353 				Free(rtm);
354 			m_freem(m);
355 			return (error);
356 		}
357 		/* There is another listener, so construct message */
358 		rp = sotorawcb(so);
359 	}
360 	if (rtm) {
361 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
362 		Free(rtm);
363 	}
364 	if (rp)
365 		rp->rcb_proto.sp_family = 0; /* Avoid us */
366 	if (dst)
367 		route_proto.sp_protocol = dst->sa_family;
368 	raw_input(m, &route_proto, &route_src, &route_dst);
369 	if (rp)
370 		rp->rcb_proto.sp_family = PF_ROUTE;
371     }
372 	return (error);
373 }
374 
375 void
376 rt_setmetrics(which, in, out)
377 	u_long which;
378 	register struct rt_metrics *in, *out;
379 {
380 #define metric(f, e) if (which & (f)) out->e = in->e;
381 	metric(RTV_RPIPE, rmx_recvpipe);
382 	metric(RTV_SPIPE, rmx_sendpipe);
383 	metric(RTV_SSTHRESH, rmx_ssthresh);
384 	metric(RTV_RTT, rmx_rtt);
385 	metric(RTV_RTTVAR, rmx_rttvar);
386 	metric(RTV_HOPCOUNT, rmx_hopcount);
387 	metric(RTV_MTU, rmx_mtu);
388 	metric(RTV_EXPIRE, rmx_expire);
389 #undef metric
390 }
391 
392 #define ROUNDUP(a) \
393 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
394 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
395 
396 static void
397 rt_xaddrs(cp, cplim, rtinfo)
398 	register caddr_t cp, cplim;
399 	register struct rt_addrinfo *rtinfo;
400 {
401 	register struct sockaddr *sa;
402 	register int i;
403 
404 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
405 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
406 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
407 			continue;
408 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
409 		ADVANCE(cp, sa);
410 	}
411 }
412 
413 static struct mbuf *
414 rt_msg1(type, rtinfo)
415 	int type;
416 	register struct rt_addrinfo *rtinfo;
417 {
418 	register struct rt_msghdr *rtm;
419 	register struct mbuf *m;
420 	register int i;
421 	register struct sockaddr *sa;
422 	int len, dlen;
423 
424 	m = m_gethdr(M_DONTWAIT, MT_DATA);
425 	if (m == 0)
426 		return (m);
427 	switch (type) {
428 
429 	case RTM_DELADDR:
430 	case RTM_NEWADDR:
431 		len = sizeof(struct ifa_msghdr);
432 		break;
433 
434 	case RTM_IFINFO:
435 		len = sizeof(struct if_msghdr);
436 		break;
437 
438 	default:
439 		len = sizeof(struct rt_msghdr);
440 	}
441 	if (len > MHLEN)
442 		panic("rt_msg1");
443 	m->m_pkthdr.len = m->m_len = len;
444 	m->m_pkthdr.rcvif = 0;
445 	rtm = mtod(m, struct rt_msghdr *);
446 	bzero(rtm, len);
447 	for (i = 0; i < RTAX_MAX; i++) {
448 		if ((sa = rtinfo->rti_info[i]) == NULL)
449 			continue;
450 		rtinfo->rti_addrs |= (1 << i);
451 		dlen = ROUNDUP(sa->sa_len);
452 		m_copyback(m, len, dlen, (caddr_t)sa);
453 		len += dlen;
454 	}
455 	if (m->m_pkthdr.len != len) {
456 		m_freem(m);
457 		return (NULL);
458 	}
459 	rtm->rtm_msglen = len;
460 	rtm->rtm_version = RTM_VERSION;
461 	rtm->rtm_type = type;
462 	return (m);
463 }
464 
465 static int
466 rt_msg2(type, rtinfo, cp, w)
467 	int type;
468 	register struct rt_addrinfo *rtinfo;
469 	caddr_t cp;
470 	struct walkarg *w;
471 {
472 	register int i;
473 	int len, dlen, second_time = 0;
474 	caddr_t cp0;
475 
476 	rtinfo->rti_addrs = 0;
477 again:
478 	switch (type) {
479 
480 	case RTM_DELADDR:
481 	case RTM_NEWADDR:
482 		len = sizeof(struct ifa_msghdr);
483 		break;
484 
485 	case RTM_IFINFO:
486 		len = sizeof(struct if_msghdr);
487 		break;
488 
489 	default:
490 		len = sizeof(struct rt_msghdr);
491 	}
492 	if ((cp0 = cp) != NULL)
493 		cp += len;
494 	for (i = 0; i < RTAX_MAX; i++) {
495 		register struct sockaddr *sa;
496 
497 		if ((sa = rtinfo->rti_info[i]) == 0)
498 			continue;
499 		rtinfo->rti_addrs |= (1 << i);
500 		dlen = ROUNDUP(sa->sa_len);
501 		if (cp) {
502 			bcopy(sa, cp, (unsigned)dlen);
503 			cp += dlen;
504 		}
505 		len += dlen;
506 	}
507 	if (cp == 0 && w != NULL && !second_time) {
508 		register struct walkarg *rw = w;
509 
510 		rw->w_needed += len;
511 		if (rw->w_needed <= 0 && rw->w_where) {
512 			if (rw->w_tmemsize < len) {
513 				if (rw->w_tmem)
514 					free(rw->w_tmem, M_RTABLE);
515 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
516 				    M_NOWAIT);
517 				if (rw->w_tmem)
518 					rw->w_tmemsize = len;
519 			}
520 			if (rw->w_tmem) {
521 				cp = rw->w_tmem;
522 				second_time = 1;
523 				goto again;
524 			} else
525 				rw->w_where = 0;
526 		}
527 	}
528 	if (cp) {
529 		register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
530 
531 		rtm->rtm_version = RTM_VERSION;
532 		rtm->rtm_type = type;
533 		rtm->rtm_msglen = len;
534 	}
535 	return (len);
536 }
537 
538 /*
539  * This routine is called to generate a message from the routing
540  * socket indicating that a redirect has occured, a routing lookup
541  * has failed, or that a protocol has detected timeouts to a particular
542  * destination.
543  */
544 void
545 rt_missmsg(type, rtinfo, flags, error)
546 	int type, flags, error;
547 	register struct rt_addrinfo *rtinfo;
548 {
549 	register struct rt_msghdr *rtm;
550 	register struct mbuf *m;
551 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
552 
553 	if (route_cb.any_count == 0)
554 		return;
555 	m = rt_msg1(type, rtinfo);
556 	if (m == 0)
557 		return;
558 	rtm = mtod(m, struct rt_msghdr *);
559 	rtm->rtm_flags = RTF_DONE | flags;
560 	rtm->rtm_errno = error;
561 	rtm->rtm_addrs = rtinfo->rti_addrs;
562 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
563 	raw_input(m, &route_proto, &route_src, &route_dst);
564 }
565 
566 /*
567  * This routine is called to generate a message from the routing
568  * socket indicating that the status of a network interface has changed.
569  */
570 void
571 rt_ifmsg(ifp)
572 	register struct ifnet *ifp;
573 {
574 	register struct if_msghdr *ifm;
575 	struct mbuf *m;
576 	struct rt_addrinfo info;
577 
578 	if (route_cb.any_count == 0)
579 		return;
580 	bzero(&info, sizeof(info));
581 	m = rt_msg1(RTM_IFINFO, &info);
582 	if (m == 0)
583 		return;
584 	ifm = mtod(m, struct if_msghdr *);
585 	ifm->ifm_index = ifp->if_index;
586 	ifm->ifm_flags = ifp->if_flags;
587 	ifm->ifm_data = ifp->if_data;
588 	ifm->ifm_addrs = 0;
589 	route_proto.sp_protocol = 0;
590 	raw_input(m, &route_proto, &route_src, &route_dst);
591 }
592 
593 /*
594  * This is called to generate messages from the routing socket
595  * indicating a network interface has had addresses associated with it.
596  * if we ever reverse the logic and replace messages TO the routing
597  * socket indicate a request to configure interfaces, then it will
598  * be unnecessary as the routing socket will automatically generate
599  * copies of it.
600  */
601 void
602 rt_newaddrmsg(cmd, ifa, error, rt)
603 	int cmd, error;
604 	register struct ifaddr *ifa;
605 	register struct rtentry *rt;
606 {
607 	struct rt_addrinfo info;
608 	struct sockaddr *sa = NULL;
609 	int pass;
610 	struct mbuf *m = NULL;
611 	struct ifnet *ifp = ifa->ifa_ifp;
612 
613 	if (route_cb.any_count == 0)
614 		return;
615 	for (pass = 1; pass < 3; pass++) {
616 		bzero(&info, sizeof(info));
617 		if ((cmd == RTM_ADD && pass == 1) ||
618 		    (cmd == RTM_DELETE && pass == 2)) {
619 			register struct ifa_msghdr *ifam;
620 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
621 
622 			ifaaddr = sa = ifa->ifa_addr;
623 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
624 			netmask = ifa->ifa_netmask;
625 			brdaddr = ifa->ifa_dstaddr;
626 			if ((m = rt_msg1(ncmd, &info)) == NULL)
627 				continue;
628 			ifam = mtod(m, struct ifa_msghdr *);
629 			ifam->ifam_index = ifp->if_index;
630 			ifam->ifam_metric = ifa->ifa_metric;
631 			ifam->ifam_flags = ifa->ifa_flags;
632 			ifam->ifam_addrs = info.rti_addrs;
633 		}
634 		if ((cmd == RTM_ADD && pass == 2) ||
635 		    (cmd == RTM_DELETE && pass == 1)) {
636 			register struct rt_msghdr *rtm;
637 
638 			if (rt == 0)
639 				continue;
640 			netmask = rt_mask(rt);
641 			dst = sa = rt_key(rt);
642 			gate = rt->rt_gateway;
643 			if ((m = rt_msg1(cmd, &info)) == NULL)
644 				continue;
645 			rtm = mtod(m, struct rt_msghdr *);
646 			rtm->rtm_index = ifp->if_index;
647 			rtm->rtm_flags |= rt->rt_flags;
648 			rtm->rtm_errno = error;
649 			rtm->rtm_addrs = info.rti_addrs;
650 		}
651 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
652 		raw_input(m, &route_proto, &route_src, &route_dst);
653 	}
654 }
655 
656 /*
657  * This is used in dumping the kernel table via sysctl().
658  */
659 int
660 sysctl_dumpentry(rn, v)
661 	struct radix_node *rn;
662 	register void *v;
663 {
664 	register struct walkarg *w = v;
665 	register struct rtentry *rt = (struct rtentry *)rn;
666 	int error = 0, size;
667 	struct rt_addrinfo info;
668 
669 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
670 		return 0;
671 	bzero(&info, sizeof(info));
672 	dst = rt_key(rt);
673 	gate = rt->rt_gateway;
674 	netmask = rt_mask(rt);
675 	genmask = rt->rt_genmask;
676 	if (rt->rt_ifp) {
677 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
678 		ifaaddr = rt->rt_ifa->ifa_addr;
679 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
680 			brdaddr = rt->rt_ifa->ifa_dstaddr;
681 	}
682 	size = rt_msg2(RTM_GET, &info, 0, w);
683 	if (w->w_where && w->w_tmem) {
684 		register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
685 
686 		rtm->rtm_flags = rt->rt_flags;
687 		rtm->rtm_use = rt->rt_use;
688 		rtm->rtm_rmx = rt->rt_rmx;
689 		rtm->rtm_index = rt->rt_ifp->if_index;
690 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
691 		rtm->rtm_addrs = info.rti_addrs;
692 		if ((error = copyout(rtm, w->w_where, size)) != 0)
693 			w->w_where = NULL;
694 		else
695 			w->w_where += size;
696 	}
697 	return (error);
698 }
699 
700 int
701 sysctl_iflist(af, w)
702 	int	af;
703 	register struct	walkarg *w;
704 {
705 	register struct ifnet *ifp;
706 	register struct ifaddr *ifa;
707 	struct	rt_addrinfo info;
708 	int	len, error = 0;
709 
710 	bzero(&info, sizeof(info));
711 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
712 		if (w->w_arg && w->w_arg != ifp->if_index)
713 			continue;
714 		ifa = ifp->if_addrlist.tqh_first;
715 		ifpaddr = ifa->ifa_addr;
716 		len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
717 		ifpaddr = 0;
718 		if (w->w_where && w->w_tmem) {
719 			register struct if_msghdr *ifm;
720 
721 			ifm = (struct if_msghdr *)w->w_tmem;
722 			ifm->ifm_index = ifp->if_index;
723 			ifm->ifm_flags = ifp->if_flags;
724 			ifm->ifm_data = ifp->if_data;
725 			ifm->ifm_addrs = info.rti_addrs;
726 			error = copyout(ifm, w->w_where, len);
727 			if (error)
728 				return (error);
729 			w->w_where += len;
730 		}
731 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
732 			if (af && af != ifa->ifa_addr->sa_family)
733 				continue;
734 			ifaaddr = ifa->ifa_addr;
735 			netmask = ifa->ifa_netmask;
736 			brdaddr = ifa->ifa_dstaddr;
737 			len = rt_msg2(RTM_NEWADDR, &info, 0, w);
738 			if (w->w_where && w->w_tmem) {
739 				register struct ifa_msghdr *ifam;
740 
741 				ifam = (struct ifa_msghdr *)w->w_tmem;
742 				ifam->ifam_index = ifa->ifa_ifp->if_index;
743 				ifam->ifam_flags = ifa->ifa_flags;
744 				ifam->ifam_metric = ifa->ifa_metric;
745 				ifam->ifam_addrs = info.rti_addrs;
746 				error = copyout(w->w_tmem, w->w_where, len);
747 				if (error)
748 					return (error);
749 				w->w_where += len;
750 			}
751 		}
752 		ifaaddr = netmask = brdaddr = 0;
753 	}
754 	return (0);
755 }
756 
757 int
758 sysctl_rtable(name, namelen, where, given, new, newlen)
759 	int	*name;
760 	u_int	namelen;
761 	void 	*where;
762 	size_t	*given;
763 	void	*new;
764 	size_t	newlen;
765 {
766 	register struct radix_node_head *rnh;
767 	int	i, s, error = EINVAL;
768 	u_char  af;
769 	struct	walkarg w;
770 
771 	if (new)
772 		return (EPERM);
773 	if (namelen != 3)
774 		return (EINVAL);
775 	af = name[0];
776 	Bzero(&w, sizeof(w));
777 	w.w_where = where;
778 	w.w_given = *given;
779 	w.w_needed = 0 - w.w_given;
780 	w.w_op = name[1];
781 	w.w_arg = name[2];
782 
783 	s = splsoftnet();
784 	switch (w.w_op) {
785 
786 	case NET_RT_DUMP:
787 	case NET_RT_FLAGS:
788 		for (i = 1; i <= AF_MAX; i++)
789 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
790 			    (error = (*rnh->rnh_walktree)(rnh,
791 			    sysctl_dumpentry, &w)))
792 				break;
793 		break;
794 
795 	case NET_RT_IFLIST:
796 		error = sysctl_iflist(af, &w);
797 	}
798 	splx(s);
799 	if (w.w_tmem)
800 		free(w.w_tmem, M_RTABLE);
801 	w.w_needed += w.w_given;
802 	if (where) {
803 		*given = w.w_where - (caddr_t) where;
804 		if (*given < w.w_needed)
805 			return (ENOMEM);
806 	} else {
807 		*given = (11 * w.w_needed) / 10;
808 	}
809 	return (error);
810 }
811 
812 /*
813  * Definitions of protocols supported in the ROUTE domain.
814  */
815 
816 extern	struct domain routedomain;		/* or at least forward */
817 
818 struct protosw routesw[] = {
819 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
820   raw_input,	route_output,	raw_ctlinput,	0,
821   route_usrreq,
822   raw_init,	0,		0,		0,
823   sysctl_rtable,
824 }
825 };
826 
827 struct domain routedomain =
828     { PF_ROUTE, "route", route_init, 0, 0,
829       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
830