xref: /freebsd/sys/net/rtsock.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
1 /*
2  * Copyright (c) 1988, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
34  * $FreeBSD$
35  */
36 
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/jail.h>
50 
51 #include <net/if.h>
52 #include <net/route.h>
53 #include <net/raw_cb.h>
54 
55 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
56 
57 static struct	sockaddr route_dst = { 2, PF_ROUTE, };
58 static struct	sockaddr route_src = { 2, PF_ROUTE, };
59 static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
60 static struct	sockproto route_proto = { PF_ROUTE, };
61 
62 struct walkarg {
63 	int	w_tmemsize;
64 	int	w_op, w_arg;
65 	caddr_t	w_tmem;
66 	struct sysctl_req *w_req;
67 };
68 
69 static struct mbuf *
70 		rt_msg1 __P((int, struct rt_addrinfo *));
71 static int	rt_msg2 __P((int,
72 		    struct rt_addrinfo *, caddr_t, struct walkarg *));
73 static int	rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
74 static int	sysctl_dumpentry __P((struct radix_node *rn, void *vw));
75 static int	sysctl_iflist __P((int af, struct walkarg *w));
76 static int	 route_output __P((struct mbuf *, struct socket *));
77 static void	 rt_setmetrics __P((u_long, struct rt_metrics *, struct rt_metrics *));
78 
79 /* Sleazy use of local variables throughout file, warning!!!! */
80 #define dst	info.rti_info[RTAX_DST]
81 #define gate	info.rti_info[RTAX_GATEWAY]
82 #define netmask	info.rti_info[RTAX_NETMASK]
83 #define genmask	info.rti_info[RTAX_GENMASK]
84 #define ifpaddr	info.rti_info[RTAX_IFP]
85 #define ifaaddr	info.rti_info[RTAX_IFA]
86 #define brdaddr	info.rti_info[RTAX_BRD]
87 
88 /*
89  * It really doesn't make any sense at all for this code to share much
90  * with raw_usrreq.c, since its functionality is so restricted.  XXX
91  */
92 static int
93 rts_abort(struct socket *so)
94 {
95 	int s, error;
96 	s = splnet();
97 	error = raw_usrreqs.pru_abort(so);
98 	splx(s);
99 	return error;
100 }
101 
102 /* pru_accept is EOPNOTSUPP */
103 
104 static int
105 rts_attach(struct socket *so, int proto, struct thread *td)
106 {
107 	struct rawcb *rp;
108 	int s, error;
109 
110 	if (sotorawcb(so) != 0)
111 		return EISCONN;	/* XXX panic? */
112 	/* XXX */
113 	MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
114 	if (rp == 0)
115 		return ENOBUFS;
116 
117 	/*
118 	 * The splnet() is necessary to block protocols from sending
119 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
120 	 * this PCB is extant but incompletely initialized.
121 	 * Probably we should try to do more of this work beforehand and
122 	 * eliminate the spl.
123 	 */
124 	s = splnet();
125 	so->so_pcb = (caddr_t)rp;
126 	error = raw_attach(so, proto);
127 	rp = sotorawcb(so);
128 	if (error) {
129 		splx(s);
130 		so->so_pcb = NULL;
131 		free(rp, M_PCB);
132 		return error;
133 	}
134 	switch(rp->rcb_proto.sp_protocol) {
135 	case AF_INET:
136 		route_cb.ip_count++;
137 		break;
138 	case AF_INET6:
139 		route_cb.ip6_count++;
140 		break;
141 	case AF_IPX:
142 		route_cb.ipx_count++;
143 		break;
144 	case AF_NS:
145 		route_cb.ns_count++;
146 		break;
147 	}
148 	rp->rcb_faddr = &route_src;
149 	route_cb.any_count++;
150 	soisconnected(so);
151 	so->so_options |= SO_USELOOPBACK;
152 	splx(s);
153 	return 0;
154 }
155 
156 static int
157 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
158 {
159 	int s, error;
160 	s = splnet();
161 	error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
162 	splx(s);
163 	return error;
164 }
165 
166 static int
167 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
168 {
169 	int s, error;
170 	s = splnet();
171 	error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
172 	splx(s);
173 	return error;
174 }
175 
176 /* pru_connect2 is EOPNOTSUPP */
177 /* pru_control is EOPNOTSUPP */
178 
179 static int
180 rts_detach(struct socket *so)
181 {
182 	struct rawcb *rp = sotorawcb(so);
183 	int s, error;
184 
185 	s = splnet();
186 	if (rp != 0) {
187 		switch(rp->rcb_proto.sp_protocol) {
188 		case AF_INET:
189 			route_cb.ip_count--;
190 			break;
191 		case AF_INET6:
192 			route_cb.ip6_count--;
193 			break;
194 		case AF_IPX:
195 			route_cb.ipx_count--;
196 			break;
197 		case AF_NS:
198 			route_cb.ns_count--;
199 			break;
200 		}
201 		route_cb.any_count--;
202 	}
203 	error = raw_usrreqs.pru_detach(so);
204 	splx(s);
205 	return error;
206 }
207 
208 static int
209 rts_disconnect(struct socket *so)
210 {
211 	int s, error;
212 	s = splnet();
213 	error = raw_usrreqs.pru_disconnect(so);
214 	splx(s);
215 	return error;
216 }
217 
218 /* pru_listen is EOPNOTSUPP */
219 
220 static int
221 rts_peeraddr(struct socket *so, struct sockaddr **nam)
222 {
223 	int s, error;
224 	s = splnet();
225 	error = raw_usrreqs.pru_peeraddr(so, nam);
226 	splx(s);
227 	return error;
228 }
229 
230 /* pru_rcvd is EOPNOTSUPP */
231 /* pru_rcvoob is EOPNOTSUPP */
232 
233 static int
234 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
235 	 struct mbuf *control, struct thread *td)
236 {
237 	int s, error;
238 	s = splnet();
239 	error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
240 	splx(s);
241 	return error;
242 }
243 
244 /* pru_sense is null */
245 
246 static int
247 rts_shutdown(struct socket *so)
248 {
249 	int s, error;
250 	s = splnet();
251 	error = raw_usrreqs.pru_shutdown(so);
252 	splx(s);
253 	return error;
254 }
255 
256 static int
257 rts_sockaddr(struct socket *so, struct sockaddr **nam)
258 {
259 	int s, error;
260 	s = splnet();
261 	error = raw_usrreqs.pru_sockaddr(so, nam);
262 	splx(s);
263 	return error;
264 }
265 
266 static struct pr_usrreqs route_usrreqs = {
267 	rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
268 	pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
269 	pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
270 	rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
271 	sosend, soreceive, sopoll
272 };
273 
274 /*ARGSUSED*/
275 static int
276 route_output(m, so)
277 	register struct mbuf *m;
278 	struct socket *so;
279 {
280 	register struct rt_msghdr *rtm = 0;
281 	register struct rtentry *rt = 0;
282 	struct rtentry *saved_nrt = 0;
283 	struct radix_node_head *rnh;
284 	struct rt_addrinfo info;
285 	int len, error = 0;
286 	struct ifnet *ifp = 0;
287 	struct ifaddr *ifa = 0;
288 
289 #define senderr(e) { error = e; goto flush;}
290 	if (m == 0 || ((m->m_len < sizeof(long)) &&
291 		       (m = m_pullup(m, sizeof(long))) == 0))
292 		return (ENOBUFS);
293 	if ((m->m_flags & M_PKTHDR) == 0)
294 		panic("route_output");
295 	len = m->m_pkthdr.len;
296 	if (len < sizeof(*rtm) ||
297 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
298 		dst = 0;
299 		senderr(EINVAL);
300 	}
301 	R_Malloc(rtm, struct rt_msghdr *, len);
302 	if (rtm == 0) {
303 		dst = 0;
304 		senderr(ENOBUFS);
305 	}
306 	m_copydata(m, 0, len, (caddr_t)rtm);
307 	if (rtm->rtm_version != RTM_VERSION) {
308 		dst = 0;
309 		senderr(EPROTONOSUPPORT);
310 	}
311 	rtm->rtm_pid = curproc->p_pid;
312 	bzero(&info, sizeof(info));
313 	info.rti_addrs = rtm->rtm_addrs;
314 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
315 		dst = 0;
316 		senderr(EINVAL);
317 	}
318 	info.rti_flags = rtm->rtm_flags;
319 	if (dst == 0 || (dst->sa_family >= AF_MAX)
320 	    || (gate != 0 && (gate->sa_family >= AF_MAX)))
321 		senderr(EINVAL);
322 	if (genmask) {
323 		struct radix_node *t;
324 		t = rn_addmask((caddr_t)genmask, 0, 1);
325 		if (t && Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1,
326 			      *(u_char *)t->rn_key - 1) == 0)
327 			genmask = (struct sockaddr *)(t->rn_key);
328 		else
329 			senderr(ENOBUFS);
330 	}
331 
332 	/*
333 	 * Verify that the caller has the appropriate privilege; RTM_GET
334 	 * is the only operation the non-superuser is allowed.
335 	 */
336 	if (rtm->rtm_type != RTM_GET && (error = suser(curproc)) != 0)
337 		senderr(error);
338 
339 	switch (rtm->rtm_type) {
340 
341 	case RTM_ADD:
342 		if (gate == 0)
343 			senderr(EINVAL);
344 		error = rtrequest1(RTM_ADD, &info, &saved_nrt);
345 		if (error == 0 && saved_nrt) {
346 			rt_setmetrics(rtm->rtm_inits,
347 				&rtm->rtm_rmx, &saved_nrt->rt_rmx);
348 			saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
349 			saved_nrt->rt_rmx.rmx_locks |=
350 				(rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
351 			saved_nrt->rt_refcnt--;
352 			saved_nrt->rt_genmask = genmask;
353 		}
354 		break;
355 
356 	case RTM_DELETE:
357 		error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
358 		if (error == 0) {
359 			if ((rt = saved_nrt))
360 				rt->rt_refcnt++;
361 			goto report;
362 		}
363 		break;
364 
365 	case RTM_GET:
366 	case RTM_CHANGE:
367 	case RTM_LOCK:
368 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
369 			senderr(EAFNOSUPPORT);
370 		} else if ((rt = (struct rtentry *)
371 				rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
372 			rt->rt_refcnt++;
373 		else
374 			senderr(ESRCH);
375 		switch(rtm->rtm_type) {
376 
377 		case RTM_GET:
378 		report:
379 			dst = rt_key(rt);
380 			gate = rt->rt_gateway;
381 			netmask = rt_mask(rt);
382 			genmask = rt->rt_genmask;
383 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
384 				ifp = rt->rt_ifp;
385 				if (ifp) {
386 					ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
387 					ifaaddr = rt->rt_ifa->ifa_addr;
388 					if (ifp->if_flags & IFF_POINTOPOINT)
389 						brdaddr = rt->rt_ifa->ifa_dstaddr;
390 					rtm->rtm_index = ifp->if_index;
391 				} else {
392 					ifpaddr = 0;
393 					ifaaddr = 0;
394 			    }
395 			}
396 			len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
397 				(struct walkarg *)0);
398 			if (len > rtm->rtm_msglen) {
399 				struct rt_msghdr *new_rtm;
400 				R_Malloc(new_rtm, struct rt_msghdr *, len);
401 				if (new_rtm == 0)
402 					senderr(ENOBUFS);
403 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
404 				Free(rtm); rtm = new_rtm;
405 			}
406 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
407 				(struct walkarg *)0);
408 			rtm->rtm_flags = rt->rt_flags;
409 			rtm->rtm_rmx = rt->rt_rmx;
410 			rtm->rtm_addrs = info.rti_addrs;
411 			break;
412 
413 		case RTM_CHANGE:
414 			/*
415 			 * If they tried to change things but didn't specify
416 			 * the required gateway, then just use the old one.
417 			 * This can happen if the user tries to change the
418 			 * flags on the default route without changing the
419 			 * default gateway.  Changing flags still doesn't work.
420 			 */
421 			if ((rt->rt_flags & RTF_GATEWAY) && !gate)
422 				gate = rt->rt_gateway;
423 
424 			/* new gateway could require new ifaddr, ifp;
425 			   flags may also be different; ifp may be specified
426 			   by ll sockaddr when protocol address is ambiguous */
427 			if ((error = rt_getifa(&info)) != 0)
428 				senderr(error);
429 			if (gate != NULL &&
430 			    (error = rt_setgate(rt, rt_key(rt), gate)) != 0)
431 				senderr(error);
432 			if ((ifa = info.rti_ifa) != NULL) {
433 				register struct ifaddr *oifa = rt->rt_ifa;
434 				if (oifa != ifa) {
435 				    if (oifa && oifa->ifa_rtrequest)
436 					oifa->ifa_rtrequest(RTM_DELETE, rt,
437 					    &info);
438 				    IFAFREE(rt->rt_ifa);
439 				    rt->rt_ifa = ifa;
440 				    ifa->ifa_refcnt++;
441 				    rt->rt_ifp = info.rti_ifp;
442 				}
443 			}
444 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
445 					&rt->rt_rmx);
446 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
447 			       rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
448 			if (genmask)
449 				rt->rt_genmask = genmask;
450 			/*
451 			 * Fall into
452 			 */
453 		case RTM_LOCK:
454 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
455 			rt->rt_rmx.rmx_locks |=
456 				(rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
457 			break;
458 		}
459 		break;
460 
461 	default:
462 		senderr(EOPNOTSUPP);
463 	}
464 
465 flush:
466 	if (rtm) {
467 		if (error)
468 			rtm->rtm_errno = error;
469 		else
470 			rtm->rtm_flags |= RTF_DONE;
471 	}
472 	if (rt)
473 		rtfree(rt);
474     {
475 	register struct rawcb *rp = 0;
476 	/*
477 	 * Check to see if we don't want our own messages.
478 	 */
479 	if ((so->so_options & SO_USELOOPBACK) == 0) {
480 		if (route_cb.any_count <= 1) {
481 			if (rtm)
482 				Free(rtm);
483 			m_freem(m);
484 			return (error);
485 		}
486 		/* There is another listener, so construct message */
487 		rp = sotorawcb(so);
488 	}
489 	if (rtm) {
490 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
491 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
492 			m_freem(m);
493 			m = NULL;
494 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
495 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
496 		Free(rtm);
497 	}
498 	if (rp)
499 		rp->rcb_proto.sp_family = 0; /* Avoid us */
500 	if (dst)
501 		route_proto.sp_protocol = dst->sa_family;
502 	if (m)
503 		raw_input(m, &route_proto, &route_src, &route_dst);
504 	if (rp)
505 		rp->rcb_proto.sp_family = PF_ROUTE;
506     }
507 	return (error);
508 }
509 
510 static void
511 rt_setmetrics(which, in, out)
512 	u_long which;
513 	register struct rt_metrics *in, *out;
514 {
515 #define metric(f, e) if (which & (f)) out->e = in->e;
516 	metric(RTV_RPIPE, rmx_recvpipe);
517 	metric(RTV_SPIPE, rmx_sendpipe);
518 	metric(RTV_SSTHRESH, rmx_ssthresh);
519 	metric(RTV_RTT, rmx_rtt);
520 	metric(RTV_RTTVAR, rmx_rttvar);
521 	metric(RTV_HOPCOUNT, rmx_hopcount);
522 	metric(RTV_MTU, rmx_mtu);
523 	metric(RTV_EXPIRE, rmx_expire);
524 #undef metric
525 }
526 
527 #define ROUNDUP(a) \
528 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
529 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
530 
531 
532 /*
533  * Extract the addresses of the passed sockaddrs.
534  * Do a little sanity checking so as to avoid bad memory references.
535  * This data is derived straight from userland.
536  */
537 static int
538 rt_xaddrs(cp, cplim, rtinfo)
539 	register caddr_t cp, cplim;
540 	register struct rt_addrinfo *rtinfo;
541 {
542 	register struct sockaddr *sa;
543 	register int i;
544 
545 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
546 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
547 			continue;
548 		sa = (struct sockaddr *)cp;
549 		/*
550 		 * It won't fit.
551 		 */
552 		if ( (cp + sa->sa_len) > cplim ) {
553 			return (EINVAL);
554 		}
555 
556 		/*
557 		 * there are no more.. quit now
558 		 * If there are more bits, they are in error.
559 		 * I've seen this. route(1) can evidently generate these.
560 		 * This causes kernel to core dump.
561 		 * for compatibility, If we see this, point to a safe address.
562 		 */
563 		if (sa->sa_len == 0) {
564 			rtinfo->rti_info[i] = &sa_zero;
565 			return (0); /* should be EINVAL but for compat */
566 		}
567 
568 		/* accept it */
569 		rtinfo->rti_info[i] = sa;
570 		ADVANCE(cp, sa);
571 	}
572 	return (0);
573 }
574 
575 static struct mbuf *
576 rt_msg1(type, rtinfo)
577 	int type;
578 	register struct rt_addrinfo *rtinfo;
579 {
580 	register struct rt_msghdr *rtm;
581 	register struct mbuf *m;
582 	register int i;
583 	register struct sockaddr *sa;
584 	int len, dlen;
585 
586 	switch (type) {
587 
588 	case RTM_DELADDR:
589 	case RTM_NEWADDR:
590 		len = sizeof(struct ifa_msghdr);
591 		break;
592 
593 	case RTM_DELMADDR:
594 	case RTM_NEWMADDR:
595 		len = sizeof(struct ifma_msghdr);
596 		break;
597 
598 	case RTM_IFINFO:
599 		len = sizeof(struct if_msghdr);
600 		break;
601 
602 	default:
603 		len = sizeof(struct rt_msghdr);
604 	}
605 	if (len > MCLBYTES)
606 		panic("rt_msg1");
607 	m = m_gethdr(M_DONTWAIT, MT_DATA);
608 	if (m && len > MHLEN) {
609 		MCLGET(m, M_DONTWAIT);
610 		if ((m->m_flags & M_EXT) == 0) {
611 			m_free(m);
612 			m = NULL;
613 		}
614 	}
615 	if (m == 0)
616 		return (m);
617 	m->m_pkthdr.len = m->m_len = len;
618 	m->m_pkthdr.rcvif = 0;
619 	rtm = mtod(m, struct rt_msghdr *);
620 	bzero((caddr_t)rtm, len);
621 	for (i = 0; i < RTAX_MAX; i++) {
622 		if ((sa = rtinfo->rti_info[i]) == NULL)
623 			continue;
624 		rtinfo->rti_addrs |= (1 << i);
625 		dlen = ROUNDUP(sa->sa_len);
626 		m_copyback(m, len, dlen, (caddr_t)sa);
627 		len += dlen;
628 	}
629 	if (m->m_pkthdr.len != len) {
630 		m_freem(m);
631 		return (NULL);
632 	}
633 	rtm->rtm_msglen = len;
634 	rtm->rtm_version = RTM_VERSION;
635 	rtm->rtm_type = type;
636 	return (m);
637 }
638 
639 static int
640 rt_msg2(type, rtinfo, cp, w)
641 	int type;
642 	register struct rt_addrinfo *rtinfo;
643 	caddr_t cp;
644 	struct walkarg *w;
645 {
646 	register int i;
647 	int len, dlen, second_time = 0;
648 	caddr_t cp0;
649 
650 	rtinfo->rti_addrs = 0;
651 again:
652 	switch (type) {
653 
654 	case RTM_DELADDR:
655 	case RTM_NEWADDR:
656 		len = sizeof(struct ifa_msghdr);
657 		break;
658 
659 	case RTM_IFINFO:
660 		len = sizeof(struct if_msghdr);
661 		break;
662 
663 	default:
664 		len = sizeof(struct rt_msghdr);
665 	}
666 	cp0 = cp;
667 	if (cp0)
668 		cp += len;
669 	for (i = 0; i < RTAX_MAX; i++) {
670 		register struct sockaddr *sa;
671 
672 		if ((sa = rtinfo->rti_info[i]) == 0)
673 			continue;
674 		rtinfo->rti_addrs |= (1 << i);
675 		dlen = ROUNDUP(sa->sa_len);
676 		if (cp) {
677 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
678 			cp += dlen;
679 		}
680 		len += dlen;
681 	}
682 	if (cp == 0 && w != NULL && !second_time) {
683 		register struct walkarg *rw = w;
684 
685 		if (rw->w_req) {
686 			if (rw->w_tmemsize < len) {
687 				if (rw->w_tmem)
688 					free(rw->w_tmem, M_RTABLE);
689 				rw->w_tmem = (caddr_t)
690 					malloc(len, M_RTABLE, M_NOWAIT);
691 				if (rw->w_tmem)
692 					rw->w_tmemsize = len;
693 			}
694 			if (rw->w_tmem) {
695 				cp = rw->w_tmem;
696 				second_time = 1;
697 				goto again;
698 			}
699 		}
700 	}
701 	if (cp) {
702 		register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
703 
704 		rtm->rtm_version = RTM_VERSION;
705 		rtm->rtm_type = type;
706 		rtm->rtm_msglen = len;
707 	}
708 	return (len);
709 }
710 
711 /*
712  * This routine is called to generate a message from the routing
713  * socket indicating that a redirect has occured, a routing lookup
714  * has failed, or that a protocol has detected timeouts to a particular
715  * destination.
716  */
717 void
718 rt_missmsg(type, rtinfo, flags, error)
719 	int type, flags, error;
720 	register struct rt_addrinfo *rtinfo;
721 {
722 	register struct rt_msghdr *rtm;
723 	register struct mbuf *m;
724 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
725 
726 	if (route_cb.any_count == 0)
727 		return;
728 	m = rt_msg1(type, rtinfo);
729 	if (m == 0)
730 		return;
731 	rtm = mtod(m, struct rt_msghdr *);
732 	rtm->rtm_flags = RTF_DONE | flags;
733 	rtm->rtm_errno = error;
734 	rtm->rtm_addrs = rtinfo->rti_addrs;
735 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
736 	raw_input(m, &route_proto, &route_src, &route_dst);
737 }
738 
739 /*
740  * This routine is called to generate a message from the routing
741  * socket indicating that the status of a network interface has changed.
742  */
743 void
744 rt_ifmsg(ifp)
745 	register struct ifnet *ifp;
746 {
747 	register struct if_msghdr *ifm;
748 	struct mbuf *m;
749 	struct rt_addrinfo info;
750 
751 	if (route_cb.any_count == 0)
752 		return;
753 	bzero((caddr_t)&info, sizeof(info));
754 	m = rt_msg1(RTM_IFINFO, &info);
755 	if (m == 0)
756 		return;
757 	ifm = mtod(m, struct if_msghdr *);
758 	ifm->ifm_index = ifp->if_index;
759 	ifm->ifm_flags = (u_short)ifp->if_flags;
760 	ifm->ifm_data = ifp->if_data;
761 	ifm->ifm_addrs = 0;
762 	route_proto.sp_protocol = 0;
763 	raw_input(m, &route_proto, &route_src, &route_dst);
764 }
765 
766 /*
767  * This is called to generate messages from the routing socket
768  * indicating a network interface has had addresses associated with it.
769  * if we ever reverse the logic and replace messages TO the routing
770  * socket indicate a request to configure interfaces, then it will
771  * be unnecessary as the routing socket will automatically generate
772  * copies of it.
773  */
774 void
775 rt_newaddrmsg(cmd, ifa, error, rt)
776 	int cmd, error;
777 	register struct ifaddr *ifa;
778 	register struct rtentry *rt;
779 {
780 	struct rt_addrinfo info;
781 	struct sockaddr *sa = 0;
782 	int pass;
783 	struct mbuf *m = 0;
784 	struct ifnet *ifp = ifa->ifa_ifp;
785 
786 	if (route_cb.any_count == 0)
787 		return;
788 	for (pass = 1; pass < 3; pass++) {
789 		bzero((caddr_t)&info, sizeof(info));
790 		if ((cmd == RTM_ADD && pass == 1) ||
791 		    (cmd == RTM_DELETE && pass == 2)) {
792 			register struct ifa_msghdr *ifam;
793 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
794 
795 			ifaaddr = sa = ifa->ifa_addr;
796 			ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
797 			netmask = ifa->ifa_netmask;
798 			brdaddr = ifa->ifa_dstaddr;
799 			if ((m = rt_msg1(ncmd, &info)) == NULL)
800 				continue;
801 			ifam = mtod(m, struct ifa_msghdr *);
802 			ifam->ifam_index = ifp->if_index;
803 			ifam->ifam_metric = ifa->ifa_metric;
804 			ifam->ifam_flags = ifa->ifa_flags;
805 			ifam->ifam_addrs = info.rti_addrs;
806 		}
807 		if ((cmd == RTM_ADD && pass == 2) ||
808 		    (cmd == RTM_DELETE && pass == 1)) {
809 			register struct rt_msghdr *rtm;
810 
811 			if (rt == 0)
812 				continue;
813 			netmask = rt_mask(rt);
814 			dst = sa = rt_key(rt);
815 			gate = rt->rt_gateway;
816 			if ((m = rt_msg1(cmd, &info)) == NULL)
817 				continue;
818 			rtm = mtod(m, struct rt_msghdr *);
819 			rtm->rtm_index = ifp->if_index;
820 			rtm->rtm_flags |= rt->rt_flags;
821 			rtm->rtm_errno = error;
822 			rtm->rtm_addrs = info.rti_addrs;
823 		}
824 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
825 		raw_input(m, &route_proto, &route_src, &route_dst);
826 	}
827 }
828 
829 /*
830  * This is the analogue to the rt_newaddrmsg which performs the same
831  * function but for multicast group memberhips.  This is easier since
832  * there is no route state to worry about.
833  */
834 void
835 rt_newmaddrmsg(cmd, ifma)
836 	int cmd;
837 	struct ifmultiaddr *ifma;
838 {
839 	struct rt_addrinfo info;
840 	struct mbuf *m = 0;
841 	struct ifnet *ifp = ifma->ifma_ifp;
842 	struct ifma_msghdr *ifmam;
843 
844 	if (route_cb.any_count == 0)
845 		return;
846 
847 	bzero((caddr_t)&info, sizeof(info));
848 	ifaaddr = ifma->ifma_addr;
849 	if (ifp && TAILQ_FIRST(&ifp->if_addrhead))
850 		ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
851 	else
852 		ifpaddr = NULL;
853 	/*
854 	 * If a link-layer address is present, present it as a ``gateway''
855 	 * (similarly to how ARP entries, e.g., are presented).
856 	 */
857 	gate = ifma->ifma_lladdr;
858 	if ((m = rt_msg1(cmd, &info)) == NULL)
859 		return;
860 	ifmam = mtod(m, struct ifma_msghdr *);
861 	ifmam->ifmam_index = ifp->if_index;
862 	ifmam->ifmam_addrs = info.rti_addrs;
863 	route_proto.sp_protocol = ifma->ifma_addr->sa_family;
864 	raw_input(m, &route_proto, &route_src, &route_dst);
865 }
866 
867 /*
868  * This is used in dumping the kernel table via sysctl().
869  */
870 int
871 sysctl_dumpentry(rn, vw)
872 	struct radix_node *rn;
873 	void *vw;
874 {
875 	register struct walkarg *w = vw;
876 	register struct rtentry *rt = (struct rtentry *)rn;
877 	int error = 0, size;
878 	struct rt_addrinfo info;
879 
880 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
881 		return 0;
882 	bzero((caddr_t)&info, sizeof(info));
883 	dst = rt_key(rt);
884 	gate = rt->rt_gateway;
885 	netmask = rt_mask(rt);
886 	genmask = rt->rt_genmask;
887 	if (rt->rt_ifp) {
888 		ifpaddr = TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
889 		ifaaddr = rt->rt_ifa->ifa_addr;
890 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
891 			brdaddr = rt->rt_ifa->ifa_dstaddr;
892 	}
893 	size = rt_msg2(RTM_GET, &info, 0, w);
894 	if (w->w_req && w->w_tmem) {
895 		register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
896 
897 		rtm->rtm_flags = rt->rt_flags;
898 		rtm->rtm_use = rt->rt_use;
899 		rtm->rtm_rmx = rt->rt_rmx;
900 		rtm->rtm_index = rt->rt_ifp->if_index;
901 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
902 		rtm->rtm_addrs = info.rti_addrs;
903 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
904 		return (error);
905 	}
906 	return (error);
907 }
908 
909 int
910 sysctl_iflist(af, w)
911 	int	af;
912 	register struct	walkarg *w;
913 {
914 	register struct ifnet *ifp;
915 	register struct ifaddr *ifa;
916 	struct	rt_addrinfo info;
917 	int	len, error = 0;
918 
919 	bzero((caddr_t)&info, sizeof(info));
920 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
921 		if (w->w_arg && w->w_arg != ifp->if_index)
922 			continue;
923 		ifa = TAILQ_FIRST(&ifp->if_addrhead);
924 		ifpaddr = ifa->ifa_addr;
925 		len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
926 		ifpaddr = 0;
927 		if (w->w_req && w->w_tmem) {
928 			register struct if_msghdr *ifm;
929 
930 			ifm = (struct if_msghdr *)w->w_tmem;
931 			ifm->ifm_index = ifp->if_index;
932 			ifm->ifm_flags = (u_short)ifp->if_flags;
933 			ifm->ifm_data = ifp->if_data;
934 			ifm->ifm_addrs = info.rti_addrs;
935 			error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
936 			if (error)
937 				goto done;
938 		}
939 		while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) {
940 			if (af && af != ifa->ifa_addr->sa_family)
941 				continue;
942 			if (jailed(curproc->p_ucred) &&
943 			    prison_if(curproc->p_ucred, ifa->ifa_addr))
944 				continue;
945 			ifaaddr = ifa->ifa_addr;
946 			netmask = ifa->ifa_netmask;
947 			brdaddr = ifa->ifa_dstaddr;
948 			len = rt_msg2(RTM_NEWADDR, &info, 0, w);
949 			if (w->w_req && w->w_tmem) {
950 				register struct ifa_msghdr *ifam;
951 
952 				ifam = (struct ifa_msghdr *)w->w_tmem;
953 				ifam->ifam_index = ifa->ifa_ifp->if_index;
954 				ifam->ifam_flags = ifa->ifa_flags;
955 				ifam->ifam_metric = ifa->ifa_metric;
956 				ifam->ifam_addrs = info.rti_addrs;
957 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
958 				if (error)
959 					goto done;
960 			}
961 		}
962 		ifaaddr = netmask = brdaddr = 0;
963 	}
964 done:
965 	return (error);
966 }
967 
968 static int
969 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
970 {
971 	int	*name = (int *)arg1;
972 	u_int	namelen = arg2;
973 	register struct radix_node_head *rnh;
974 	int	i, s, error = EINVAL;
975 	u_char  af;
976 	struct	walkarg w;
977 
978 	name ++;
979 	namelen--;
980 	if (req->newptr)
981 		return (EPERM);
982 	if (namelen != 3)
983 		return (EINVAL);
984 	af = name[0];
985 	Bzero(&w, sizeof(w));
986 	w.w_op = name[1];
987 	w.w_arg = name[2];
988 	w.w_req = req;
989 
990 	s = splnet();
991 	switch (w.w_op) {
992 
993 	case NET_RT_DUMP:
994 	case NET_RT_FLAGS:
995 		for (i = 1; i <= AF_MAX; i++)
996 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
997 			    (error = rnh->rnh_walktree(rnh,
998 							sysctl_dumpentry, &w)))
999 				break;
1000 		break;
1001 
1002 	case NET_RT_IFLIST:
1003 		error = sysctl_iflist(af, &w);
1004 	}
1005 	splx(s);
1006 	if (w.w_tmem)
1007 		free(w.w_tmem, M_RTABLE);
1008 	return (error);
1009 }
1010 
1011 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1012 
1013 /*
1014  * Definitions of protocols supported in the ROUTE domain.
1015  */
1016 
1017 extern struct domain routedomain;		/* or at least forward */
1018 
1019 static struct protosw routesw[] = {
1020 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
1021   0,		route_output,	raw_ctlinput,	0,
1022   0,
1023   raw_init,	0,		0,		0,
1024   &route_usrreqs
1025 }
1026 };
1027 
1028 static struct domain routedomain =
1029     { PF_ROUTE, "route", 0, 0, 0,
1030       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
1031 
1032 DOMAIN_SET(route);
1033