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