xref: /freebsd/sys/net/rtsock.c (revision 9162f64b58d01ec01481d60b6cdc06ffd8e8c7fc)
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 #include "opt_sctp.h"
33 #include "opt_mpath.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 
37 #include <sys/param.h>
38 #include <sys/domain.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/priv.h>
45 #include <sys/proc.h>
46 #include <sys/protosw.h>
47 #include <sys/rwlock.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #include <sys/vimage.h>
54 
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_llatbl.h>
58 #include <net/netisr.h>
59 #include <net/raw_cb.h>
60 #include <net/route.h>
61 #include <net/vnet.h>
62 
63 #include <netinet/in.h>
64 #ifdef INET6
65 #include <netinet6/scope6_var.h>
66 #endif
67 
68 #ifdef SCTP
69 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
70 #endif /* SCTP */
71 
72 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
73 
74 /* NB: these are not modified */
75 static struct	sockaddr route_src = { 2, PF_ROUTE, };
76 static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
77 
78 static struct {
79 	int	ip_count;	/* attached w/ AF_INET */
80 	int	ip6_count;	/* attached w/ AF_INET6 */
81 	int	ipx_count;	/* attached w/ AF_IPX */
82 	int	any_count;	/* total attached */
83 } route_cb;
84 
85 struct mtx rtsock_mtx;
86 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
87 
88 #define	RTSOCK_LOCK()	mtx_lock(&rtsock_mtx)
89 #define	RTSOCK_UNLOCK()	mtx_unlock(&rtsock_mtx)
90 #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
91 
92 static struct	ifqueue rtsintrq;
93 
94 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
95 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
96     &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
97 
98 struct walkarg {
99 	int	w_tmemsize;
100 	int	w_op, w_arg;
101 	caddr_t	w_tmem;
102 	struct sysctl_req *w_req;
103 };
104 
105 static void	rts_input(struct mbuf *m);
106 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
107 static int	rt_msg2(int type, struct rt_addrinfo *rtinfo,
108 			caddr_t cp, struct walkarg *w);
109 static int	rt_xaddrs(caddr_t cp, caddr_t cplim,
110 			struct rt_addrinfo *rtinfo);
111 static int	sysctl_dumpentry(struct radix_node *rn, void *vw);
112 static int	sysctl_iflist(int af, struct walkarg *w);
113 static int	sysctl_ifmalist(int af, struct walkarg *w);
114 static int	route_output(struct mbuf *m, struct socket *so);
115 static void	rt_setmetrics(u_long which, const struct rt_metrics *in,
116 			struct rt_metrics_lite *out);
117 static void	rt_getmetrics(const struct rt_metrics_lite *in,
118 			struct rt_metrics *out);
119 static void	rt_dispatch(struct mbuf *, const struct sockaddr *);
120 
121 static void
122 rts_init(void)
123 {
124 	int tmp;
125 
126 	rtsintrq.ifq_maxlen = 256;
127 	if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
128 		rtsintrq.ifq_maxlen = tmp;
129 	mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
130 	netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, 0);
131 }
132 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
133 
134 static void
135 rts_input(struct mbuf *m)
136 {
137 	struct sockproto route_proto;
138 	unsigned short *family;
139 	struct m_tag *tag;
140 
141 	route_proto.sp_family = PF_ROUTE;
142 	tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
143 	if (tag != NULL) {
144 		family = (unsigned short *)(tag + 1);
145 		route_proto.sp_protocol = *family;
146 		m_tag_delete(m, tag);
147 	} else
148 		route_proto.sp_protocol = 0;
149 
150 	raw_input(m, &route_proto, &route_src);
151 }
152 
153 /*
154  * It really doesn't make any sense at all for this code to share much
155  * with raw_usrreq.c, since its functionality is so restricted.  XXX
156  */
157 static void
158 rts_abort(struct socket *so)
159 {
160 
161 	raw_usrreqs.pru_abort(so);
162 }
163 
164 static void
165 rts_close(struct socket *so)
166 {
167 
168 	raw_usrreqs.pru_close(so);
169 }
170 
171 /* pru_accept is EOPNOTSUPP */
172 
173 static int
174 rts_attach(struct socket *so, int proto, struct thread *td)
175 {
176 	struct rawcb *rp;
177 	int s, error;
178 
179 	KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
180 
181 	/* XXX */
182 	rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
183 	if (rp == NULL)
184 		return ENOBUFS;
185 
186 	/*
187 	 * The splnet() is necessary to block protocols from sending
188 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
189 	 * this PCB is extant but incompletely initialized.
190 	 * Probably we should try to do more of this work beforehand and
191 	 * eliminate the spl.
192 	 */
193 	s = splnet();
194 	so->so_pcb = (caddr_t)rp;
195 	so->so_fibnum = td->td_proc->p_fibnum;
196 	error = raw_attach(so, proto);
197 	rp = sotorawcb(so);
198 	if (error) {
199 		splx(s);
200 		so->so_pcb = NULL;
201 		free(rp, M_PCB);
202 		return error;
203 	}
204 	RTSOCK_LOCK();
205 	switch(rp->rcb_proto.sp_protocol) {
206 	case AF_INET:
207 		route_cb.ip_count++;
208 		break;
209 	case AF_INET6:
210 		route_cb.ip6_count++;
211 		break;
212 	case AF_IPX:
213 		route_cb.ipx_count++;
214 		break;
215 	}
216 	route_cb.any_count++;
217 	RTSOCK_UNLOCK();
218 	soisconnected(so);
219 	so->so_options |= SO_USELOOPBACK;
220 	splx(s);
221 	return 0;
222 }
223 
224 static int
225 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
226 {
227 
228 	return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
229 }
230 
231 static int
232 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
233 {
234 
235 	return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
236 }
237 
238 /* pru_connect2 is EOPNOTSUPP */
239 /* pru_control is EOPNOTSUPP */
240 
241 static void
242 rts_detach(struct socket *so)
243 {
244 	struct rawcb *rp = sotorawcb(so);
245 
246 	KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
247 
248 	RTSOCK_LOCK();
249 	switch(rp->rcb_proto.sp_protocol) {
250 	case AF_INET:
251 		route_cb.ip_count--;
252 		break;
253 	case AF_INET6:
254 		route_cb.ip6_count--;
255 		break;
256 	case AF_IPX:
257 		route_cb.ipx_count--;
258 		break;
259 	}
260 	route_cb.any_count--;
261 	RTSOCK_UNLOCK();
262 	raw_usrreqs.pru_detach(so);
263 }
264 
265 static int
266 rts_disconnect(struct socket *so)
267 {
268 
269 	return (raw_usrreqs.pru_disconnect(so));
270 }
271 
272 /* pru_listen is EOPNOTSUPP */
273 
274 static int
275 rts_peeraddr(struct socket *so, struct sockaddr **nam)
276 {
277 
278 	return (raw_usrreqs.pru_peeraddr(so, nam));
279 }
280 
281 /* pru_rcvd is EOPNOTSUPP */
282 /* pru_rcvoob is EOPNOTSUPP */
283 
284 static int
285 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
286 	 struct mbuf *control, struct thread *td)
287 {
288 
289 	return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
290 }
291 
292 /* pru_sense is null */
293 
294 static int
295 rts_shutdown(struct socket *so)
296 {
297 
298 	return (raw_usrreqs.pru_shutdown(so));
299 }
300 
301 static int
302 rts_sockaddr(struct socket *so, struct sockaddr **nam)
303 {
304 
305 	return (raw_usrreqs.pru_sockaddr(so, nam));
306 }
307 
308 static struct pr_usrreqs route_usrreqs = {
309 	.pru_abort =		rts_abort,
310 	.pru_attach =		rts_attach,
311 	.pru_bind =		rts_bind,
312 	.pru_connect =		rts_connect,
313 	.pru_detach =		rts_detach,
314 	.pru_disconnect =	rts_disconnect,
315 	.pru_peeraddr =		rts_peeraddr,
316 	.pru_send =		rts_send,
317 	.pru_shutdown =		rts_shutdown,
318 	.pru_sockaddr =		rts_sockaddr,
319 	.pru_close =		rts_close,
320 };
321 
322 #ifndef _SOCKADDR_UNION_DEFINED
323 #define	_SOCKADDR_UNION_DEFINED
324 /*
325  * The union of all possible address formats we handle.
326  */
327 union sockaddr_union {
328 	struct sockaddr		sa;
329 	struct sockaddr_in	sin;
330 	struct sockaddr_in6	sin6;
331 };
332 #endif /* _SOCKADDR_UNION_DEFINED */
333 
334 static int
335 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
336     struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
337 {
338 
339 	switch (info->rti_info[RTAX_DST]->sa_family) {
340 #ifdef INET
341 	case AF_INET:
342 	{
343 		struct in_addr ia;
344 
345 		/*
346 		 * 1. Check if the returned address is part of the jail.
347 		 */
348 		ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->sin_addr;
349 		if (prison_check_ip4(cred, &ia) != 0) {
350 			info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
351 
352 		} else {
353 			struct ifaddr *ifa;
354 			int found;
355 
356 			found = 0;
357 
358 			/*
359 			 * 2. Try to find an address on the given outgoing
360 			 *    interface that belongs to the jail.
361 			 */
362 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
363 				struct sockaddr *sa;
364 				sa = ifa->ifa_addr;
365 				if (sa->sa_family != AF_INET)
366 					continue;
367 				ia = ((struct sockaddr_in *)sa)->sin_addr;
368 				if (prison_check_ip4(cred, &ia) != 0) {
369 					found = 1;
370 					break;
371 				}
372 			}
373 			if (!found) {
374 				/*
375 				 * 3. As a last resort return the 'default'
376 				 * jail address.
377 				 */
378 				if (prison_getip4(cred, &ia) != 0)
379 					return (ESRCH);
380 			}
381 			bzero(&saun->sin, sizeof(struct sockaddr_in));
382 			saun->sin.sin_len = sizeof(struct sockaddr_in);
383 			saun->sin.sin_family = AF_INET;
384 			saun->sin.sin_addr.s_addr = ia.s_addr;
385 			info->rti_info[RTAX_IFA] =
386 			    (struct sockaddr *)&saun->sin;
387 		}
388 		break;
389 	}
390 #endif
391 #ifdef INET6
392 	case AF_INET6:
393 	{
394 		struct in6_addr ia6;
395 
396 		/*
397 		 * 1. Check if the returned address is part of the jail.
398 		 */
399 		bcopy(&((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->sin6_addr,
400 		    &ia6, sizeof(struct in6_addr));
401 		if (prison_check_ip6(cred, &ia6) != 0) {
402 			info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
403 		} else {
404 			struct ifaddr *ifa;
405 			int found;
406 
407 			found = 0;
408 
409 			/*
410 			 * 2. Try to find an address on the given outgoing
411 			 *    interface that belongs to the jail.
412 			 */
413 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
414 				struct sockaddr *sa;
415 				sa = ifa->ifa_addr;
416 				if (sa->sa_family != AF_INET6)
417 					continue;
418 				bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
419 				    &ia6, sizeof(struct in6_addr));
420 				if (prison_check_ip6(cred, &ia6) != 0) {
421 					found = 1;
422 					break;
423 				}
424 			}
425 			if (!found) {
426 				/*
427 				 * 3. As a last resort return the 'default'
428 				 * jail address.
429 				 */
430 				if (prison_getip6(cred, &ia6) != 0)
431 					return (ESRCH);
432 			}
433 			bzero(&saun->sin6, sizeof(struct sockaddr_in6));
434 			saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
435 			saun->sin6.sin6_family = AF_INET6;
436 			bcopy(&ia6, &saun->sin6.sin6_addr,
437 			    sizeof(struct in6_addr));
438 			if (sa6_recoverscope(&saun->sin6) != 0)
439 				return (ESRCH);
440 			info->rti_info[RTAX_IFA] =
441 			    (struct sockaddr *)&saun->sin6;
442 		}
443 		break;
444 	}
445 #endif
446 	default:
447 		return (ESRCH);
448 	}
449 	return (0);
450 }
451 
452 /*ARGSUSED*/
453 static int
454 route_output(struct mbuf *m, struct socket *so)
455 {
456 #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
457 	INIT_VNET_NET(so->so_vnet);
458 	struct rt_msghdr *rtm = NULL;
459 	struct rtentry *rt = NULL;
460 	struct radix_node_head *rnh;
461 	struct rt_addrinfo info;
462 	int len, error = 0;
463 	struct ifnet *ifp = NULL;
464 	union sockaddr_union saun;
465 
466 #define senderr(e) { error = e; goto flush;}
467 	if (m == NULL || ((m->m_len < sizeof(long)) &&
468 		       (m = m_pullup(m, sizeof(long))) == NULL))
469 		return (ENOBUFS);
470 	if ((m->m_flags & M_PKTHDR) == 0)
471 		panic("route_output");
472 	len = m->m_pkthdr.len;
473 	if (len < sizeof(*rtm) ||
474 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
475 		info.rti_info[RTAX_DST] = NULL;
476 		senderr(EINVAL);
477 	}
478 	R_Malloc(rtm, struct rt_msghdr *, len);
479 	if (rtm == NULL) {
480 		info.rti_info[RTAX_DST] = NULL;
481 		senderr(ENOBUFS);
482 	}
483 	m_copydata(m, 0, len, (caddr_t)rtm);
484 	if (rtm->rtm_version != RTM_VERSION) {
485 		info.rti_info[RTAX_DST] = NULL;
486 		senderr(EPROTONOSUPPORT);
487 	}
488 	rtm->rtm_pid = curproc->p_pid;
489 	bzero(&info, sizeof(info));
490 	info.rti_addrs = rtm->rtm_addrs;
491 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
492 		info.rti_info[RTAX_DST] = NULL;
493 		senderr(EINVAL);
494 	}
495 	info.rti_flags = rtm->rtm_flags;
496 	if (info.rti_info[RTAX_DST] == NULL ||
497 	    info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
498 	    (info.rti_info[RTAX_GATEWAY] != NULL &&
499 	     info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
500 		senderr(EINVAL);
501 	/*
502 	 * Verify that the caller has the appropriate privilege; RTM_GET
503 	 * is the only operation the non-superuser is allowed.
504 	 */
505 	if (rtm->rtm_type != RTM_GET) {
506 		error = priv_check(curthread, PRIV_NET_ROUTE);
507 		if (error)
508 			senderr(error);
509 	}
510 
511 	switch (rtm->rtm_type) {
512 		struct rtentry *saved_nrt;
513 
514 	case RTM_ADD:
515 		if (info.rti_info[RTAX_GATEWAY] == NULL)
516 			senderr(EINVAL);
517 		saved_nrt = NULL;
518 
519 		/* support for new ARP code */
520 		if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
521 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
522 			error = lla_rt_output(rtm, &info);
523 			break;
524 		}
525 		error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
526 		    so->so_fibnum);
527 		if (error == 0 && saved_nrt) {
528 			RT_LOCK(saved_nrt);
529 			rt_setmetrics(rtm->rtm_inits,
530 				&rtm->rtm_rmx, &saved_nrt->rt_rmx);
531 			rtm->rtm_index = saved_nrt->rt_ifp->if_index;
532 			RT_REMREF(saved_nrt);
533 			RT_UNLOCK(saved_nrt);
534 		}
535 		break;
536 
537 	case RTM_DELETE:
538 		saved_nrt = NULL;
539 		/* support for new ARP code */
540 		if (info.rti_info[RTAX_GATEWAY] &&
541 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
542 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
543 			error = lla_rt_output(rtm, &info);
544 			break;
545 		}
546 		error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
547 		    so->so_fibnum);
548 		if (error == 0) {
549 			RT_LOCK(saved_nrt);
550 			rt = saved_nrt;
551 			goto report;
552 		}
553 		break;
554 
555 	case RTM_GET:
556 	case RTM_CHANGE:
557 	case RTM_LOCK:
558 		rnh = V_rt_tables[so->so_fibnum][info.rti_info[RTAX_DST]->sa_family];
559 		if (rnh == NULL)
560 			senderr(EAFNOSUPPORT);
561 		RADIX_NODE_HEAD_RLOCK(rnh);
562 		rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
563 			info.rti_info[RTAX_NETMASK], rnh);
564 		if (rt == NULL) {	/* XXX looks bogus */
565 			RADIX_NODE_HEAD_RUNLOCK(rnh);
566 			senderr(ESRCH);
567 		}
568 #ifdef RADIX_MPATH
569 		/*
570 		 * for RTM_CHANGE/LOCK, if we got multipath routes,
571 		 * we require users to specify a matching RTAX_GATEWAY.
572 		 *
573 		 * for RTM_GET, gate is optional even with multipath.
574 		 * if gate == NULL the first match is returned.
575 		 * (no need to call rt_mpath_matchgate if gate == NULL)
576 		 */
577 		if (rn_mpath_capable(rnh) &&
578 		    (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
579 			rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
580 			if (!rt) {
581 				RADIX_NODE_HEAD_RUNLOCK(rnh);
582 				senderr(ESRCH);
583 			}
584 		}
585 #endif
586 		RT_LOCK(rt);
587 		RT_ADDREF(rt);
588 		RADIX_NODE_HEAD_RUNLOCK(rnh);
589 
590 		/*
591 		 * Fix for PR: 82974
592 		 *
593 		 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
594 		 * returns a perfect match in case a netmask is
595 		 * specified.  For host routes only a longest prefix
596 		 * match is returned so it is necessary to compare the
597 		 * existence of the netmask.  If both have a netmask
598 		 * rnh_lookup() did a perfect match and if none of them
599 		 * have a netmask both are host routes which is also a
600 		 * perfect match.
601 		 */
602 
603 		if (rtm->rtm_type != RTM_GET &&
604 		    (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
605 			RT_UNLOCK(rt);
606 			senderr(ESRCH);
607 		}
608 
609 		switch(rtm->rtm_type) {
610 
611 		case RTM_GET:
612 		report:
613 			RT_LOCK_ASSERT(rt);
614 			if (jailed(curthread->td_ucred) &&
615 			    ((rt->rt_flags & RTF_HOST) == 0 ||
616 			    !prison_if(curthread->td_ucred, rt_key(rt)))) {
617 				RT_UNLOCK(rt);
618 				senderr(ESRCH);
619 			}
620 			info.rti_info[RTAX_DST] = rt_key(rt);
621 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
622 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
623 			info.rti_info[RTAX_GENMASK] = 0;
624 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
625 				ifp = rt->rt_ifp;
626 				if (ifp) {
627 					info.rti_info[RTAX_IFP] =
628 					    ifp->if_addr->ifa_addr;
629 					if (jailed(curthread->td_ucred)) {
630 						error = rtm_get_jailed(
631 						    &info, ifp, rt, &saun,
632 						    curthread->td_ucred);
633 						if (error != 0) {
634 							RT_UNLOCK(rt);
635 							senderr(ESRCH);
636 						}
637 					} else {
638 						info.rti_info[RTAX_IFA] =
639 						    rt->rt_ifa->ifa_addr;
640 					}
641 					if (ifp->if_flags & IFF_POINTOPOINT)
642 						info.rti_info[RTAX_BRD] =
643 						    rt->rt_ifa->ifa_dstaddr;
644 					rtm->rtm_index = ifp->if_index;
645 				} else {
646 					info.rti_info[RTAX_IFP] = NULL;
647 					info.rti_info[RTAX_IFA] = NULL;
648 				}
649 			} else if ((ifp = rt->rt_ifp) != NULL) {
650 				rtm->rtm_index = ifp->if_index;
651 			}
652 			len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
653 			if (len > rtm->rtm_msglen) {
654 				struct rt_msghdr *new_rtm;
655 				R_Malloc(new_rtm, struct rt_msghdr *, len);
656 				if (new_rtm == NULL) {
657 					RT_UNLOCK(rt);
658 					senderr(ENOBUFS);
659 				}
660 				bcopy(rtm, new_rtm, rtm->rtm_msglen);
661 				Free(rtm); rtm = new_rtm;
662 			}
663 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
664 			rtm->rtm_flags = rt->rt_flags;
665 			rtm->rtm_use = 0;
666 			rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
667 			rtm->rtm_addrs = info.rti_addrs;
668 			break;
669 
670 		case RTM_CHANGE:
671 			/*
672 			 * New gateway could require new ifaddr, ifp;
673 			 * flags may also be different; ifp may be specified
674 			 * by ll sockaddr when protocol address is ambiguous
675 			 */
676 			if (((rt->rt_flags & RTF_GATEWAY) &&
677 			     info.rti_info[RTAX_GATEWAY] != NULL) ||
678 			    info.rti_info[RTAX_IFP] != NULL ||
679 			    (info.rti_info[RTAX_IFA] != NULL &&
680 			     !sa_equal(info.rti_info[RTAX_IFA],
681 				       rt->rt_ifa->ifa_addr))) {
682 				RT_UNLOCK(rt);
683 				RADIX_NODE_HEAD_LOCK(rnh);
684 				error = rt_getifa_fib(&info, rt->rt_fibnum);
685 				RADIX_NODE_HEAD_UNLOCK(rnh);
686 				if (error != 0)
687 					senderr(error);
688 				RT_LOCK(rt);
689 			}
690 			if (info.rti_ifa != NULL &&
691 			    info.rti_ifa != rt->rt_ifa &&
692 			    rt->rt_ifa != NULL &&
693 			    rt->rt_ifa->ifa_rtrequest != NULL) {
694 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
695 				    &info);
696 				IFAFREE(rt->rt_ifa);
697 			}
698 			if (info.rti_info[RTAX_GATEWAY] != NULL) {
699 				RT_UNLOCK(rt);
700 				RADIX_NODE_HEAD_LOCK(rnh);
701 				RT_LOCK(rt);
702 
703 				error = rt_setgate(rt, rt_key(rt),
704 				    info.rti_info[RTAX_GATEWAY]);
705 				RADIX_NODE_HEAD_UNLOCK(rnh);
706 				if (error != 0) {
707 					RT_UNLOCK(rt);
708 					senderr(error);
709 				}
710 				rt->rt_flags |= RTF_GATEWAY;
711 			}
712 			if (info.rti_ifa != NULL &&
713 			    info.rti_ifa != rt->rt_ifa) {
714 				IFAREF(info.rti_ifa);
715 				rt->rt_ifa = info.rti_ifa;
716 				rt->rt_ifp = info.rti_ifp;
717 			}
718 			/* Allow some flags to be toggled on change. */
719 			if (rtm->rtm_fmask & RTF_FMASK)
720 				rt->rt_flags = (rt->rt_flags &
721 				    ~rtm->rtm_fmask) |
722 				    (rtm->rtm_flags & rtm->rtm_fmask);
723 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
724 					&rt->rt_rmx);
725 			rtm->rtm_index = rt->rt_ifp->if_index;
726 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
727 			       rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
728 			/* FALLTHROUGH */
729 		case RTM_LOCK:
730 			/* We don't support locks anymore */
731 			break;
732 		}
733 		RT_UNLOCK(rt);
734 		break;
735 
736 	default:
737 		senderr(EOPNOTSUPP);
738 	}
739 
740 flush:
741 	if (rtm) {
742 		if (error)
743 			rtm->rtm_errno = error;
744 		else
745 			rtm->rtm_flags |= RTF_DONE;
746 	}
747 	if (rt)		/* XXX can this be true? */
748 		RTFREE(rt);
749     {
750 	struct rawcb *rp = NULL;
751 	/*
752 	 * Check to see if we don't want our own messages.
753 	 */
754 	if ((so->so_options & SO_USELOOPBACK) == 0) {
755 		if (route_cb.any_count <= 1) {
756 			if (rtm)
757 				Free(rtm);
758 			m_freem(m);
759 			return (error);
760 		}
761 		/* There is another listener, so construct message */
762 		rp = sotorawcb(so);
763 	}
764 	if (rtm) {
765 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
766 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
767 			m_freem(m);
768 			m = NULL;
769 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
770 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
771 		Free(rtm);
772 	}
773 	if (m) {
774 		if (rp) {
775 			/*
776 			 * XXX insure we don't get a copy by
777 			 * invalidating our protocol
778 			 */
779 			unsigned short family = rp->rcb_proto.sp_family;
780 			rp->rcb_proto.sp_family = 0;
781 			rt_dispatch(m, info.rti_info[RTAX_DST]);
782 			rp->rcb_proto.sp_family = family;
783 		} else
784 			rt_dispatch(m, info.rti_info[RTAX_DST]);
785 	}
786     }
787 	return (error);
788 #undef	sa_equal
789 }
790 
791 static void
792 rt_setmetrics(u_long which, const struct rt_metrics *in,
793 	struct rt_metrics_lite *out)
794 {
795 #define metric(f, e) if (which & (f)) out->e = in->e;
796 	/*
797 	 * Only these are stored in the routing entry since introduction
798 	 * of tcp hostcache. The rest is ignored.
799 	 */
800 	metric(RTV_MTU, rmx_mtu);
801 	/* Userland -> kernel timebase conversion. */
802 	if (which & RTV_EXPIRE)
803 		out->rmx_expire = in->rmx_expire ?
804 		    in->rmx_expire - time_second + time_uptime : 0;
805 #undef metric
806 }
807 
808 static void
809 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
810 {
811 #define metric(e) out->e = in->e;
812 	bzero(out, sizeof(*out));
813 	metric(rmx_mtu);
814 	/* Kernel -> userland timebase conversion. */
815 	out->rmx_expire = in->rmx_expire ?
816 	    in->rmx_expire - time_uptime + time_second : 0;
817 #undef metric
818 }
819 
820 /*
821  * Extract the addresses of the passed sockaddrs.
822  * Do a little sanity checking so as to avoid bad memory references.
823  * This data is derived straight from userland.
824  */
825 static int
826 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
827 {
828 	struct sockaddr *sa;
829 	int i;
830 
831 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
832 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
833 			continue;
834 		sa = (struct sockaddr *)cp;
835 		/*
836 		 * It won't fit.
837 		 */
838 		if (cp + sa->sa_len > cplim)
839 			return (EINVAL);
840 		/*
841 		 * there are no more.. quit now
842 		 * If there are more bits, they are in error.
843 		 * I've seen this. route(1) can evidently generate these.
844 		 * This causes kernel to core dump.
845 		 * for compatibility, If we see this, point to a safe address.
846 		 */
847 		if (sa->sa_len == 0) {
848 			rtinfo->rti_info[i] = &sa_zero;
849 			return (0); /* should be EINVAL but for compat */
850 		}
851 		/* accept it */
852 		rtinfo->rti_info[i] = sa;
853 		cp += SA_SIZE(sa);
854 	}
855 	return (0);
856 }
857 
858 static struct mbuf *
859 rt_msg1(int type, struct rt_addrinfo *rtinfo)
860 {
861 	struct rt_msghdr *rtm;
862 	struct mbuf *m;
863 	int i;
864 	struct sockaddr *sa;
865 	int len, dlen;
866 
867 	switch (type) {
868 
869 	case RTM_DELADDR:
870 	case RTM_NEWADDR:
871 		len = sizeof(struct ifa_msghdr);
872 		break;
873 
874 	case RTM_DELMADDR:
875 	case RTM_NEWMADDR:
876 		len = sizeof(struct ifma_msghdr);
877 		break;
878 
879 	case RTM_IFINFO:
880 		len = sizeof(struct if_msghdr);
881 		break;
882 
883 	case RTM_IFANNOUNCE:
884 	case RTM_IEEE80211:
885 		len = sizeof(struct if_announcemsghdr);
886 		break;
887 
888 	default:
889 		len = sizeof(struct rt_msghdr);
890 	}
891 	if (len > MCLBYTES)
892 		panic("rt_msg1");
893 	m = m_gethdr(M_DONTWAIT, MT_DATA);
894 	if (m && len > MHLEN) {
895 		MCLGET(m, M_DONTWAIT);
896 		if ((m->m_flags & M_EXT) == 0) {
897 			m_free(m);
898 			m = NULL;
899 		}
900 	}
901 	if (m == NULL)
902 		return (m);
903 	m->m_pkthdr.len = m->m_len = len;
904 	m->m_pkthdr.rcvif = NULL;
905 	rtm = mtod(m, struct rt_msghdr *);
906 	bzero((caddr_t)rtm, len);
907 	for (i = 0; i < RTAX_MAX; i++) {
908 		if ((sa = rtinfo->rti_info[i]) == NULL)
909 			continue;
910 		rtinfo->rti_addrs |= (1 << i);
911 		dlen = SA_SIZE(sa);
912 		m_copyback(m, len, dlen, (caddr_t)sa);
913 		len += dlen;
914 	}
915 	if (m->m_pkthdr.len != len) {
916 		m_freem(m);
917 		return (NULL);
918 	}
919 	rtm->rtm_msglen = len;
920 	rtm->rtm_version = RTM_VERSION;
921 	rtm->rtm_type = type;
922 	return (m);
923 }
924 
925 static int
926 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
927 {
928 	int i;
929 	int len, dlen, second_time = 0;
930 	caddr_t cp0;
931 
932 	rtinfo->rti_addrs = 0;
933 again:
934 	switch (type) {
935 
936 	case RTM_DELADDR:
937 	case RTM_NEWADDR:
938 		len = sizeof(struct ifa_msghdr);
939 		break;
940 
941 	case RTM_IFINFO:
942 		len = sizeof(struct if_msghdr);
943 		break;
944 
945 	case RTM_NEWMADDR:
946 		len = sizeof(struct ifma_msghdr);
947 		break;
948 
949 	default:
950 		len = sizeof(struct rt_msghdr);
951 	}
952 	cp0 = cp;
953 	if (cp0)
954 		cp += len;
955 	for (i = 0; i < RTAX_MAX; i++) {
956 		struct sockaddr *sa;
957 
958 		if ((sa = rtinfo->rti_info[i]) == NULL)
959 			continue;
960 		rtinfo->rti_addrs |= (1 << i);
961 		dlen = SA_SIZE(sa);
962 		if (cp) {
963 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
964 			cp += dlen;
965 		}
966 		len += dlen;
967 	}
968 	len = ALIGN(len);
969 	if (cp == NULL && w != NULL && !second_time) {
970 		struct walkarg *rw = w;
971 
972 		if (rw->w_req) {
973 			if (rw->w_tmemsize < len) {
974 				if (rw->w_tmem)
975 					free(rw->w_tmem, M_RTABLE);
976 				rw->w_tmem = (caddr_t)
977 					malloc(len, M_RTABLE, M_NOWAIT);
978 				if (rw->w_tmem)
979 					rw->w_tmemsize = len;
980 			}
981 			if (rw->w_tmem) {
982 				cp = rw->w_tmem;
983 				second_time = 1;
984 				goto again;
985 			}
986 		}
987 	}
988 	if (cp) {
989 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
990 
991 		rtm->rtm_version = RTM_VERSION;
992 		rtm->rtm_type = type;
993 		rtm->rtm_msglen = len;
994 	}
995 	return (len);
996 }
997 
998 /*
999  * This routine is called to generate a message from the routing
1000  * socket indicating that a redirect has occured, a routing lookup
1001  * has failed, or that a protocol has detected timeouts to a particular
1002  * destination.
1003  */
1004 void
1005 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1006 {
1007 	struct rt_msghdr *rtm;
1008 	struct mbuf *m;
1009 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1010 
1011 	if (route_cb.any_count == 0)
1012 		return;
1013 	m = rt_msg1(type, rtinfo);
1014 	if (m == NULL)
1015 		return;
1016 	rtm = mtod(m, struct rt_msghdr *);
1017 	rtm->rtm_flags = RTF_DONE | flags;
1018 	rtm->rtm_errno = error;
1019 	rtm->rtm_addrs = rtinfo->rti_addrs;
1020 	rt_dispatch(m, sa);
1021 }
1022 
1023 /*
1024  * This routine is called to generate a message from the routing
1025  * socket indicating that the status of a network interface has changed.
1026  */
1027 void
1028 rt_ifmsg(struct ifnet *ifp)
1029 {
1030 	struct if_msghdr *ifm;
1031 	struct mbuf *m;
1032 	struct rt_addrinfo info;
1033 
1034 	if (route_cb.any_count == 0)
1035 		return;
1036 	bzero((caddr_t)&info, sizeof(info));
1037 	m = rt_msg1(RTM_IFINFO, &info);
1038 	if (m == NULL)
1039 		return;
1040 	ifm = mtod(m, struct if_msghdr *);
1041 	ifm->ifm_index = ifp->if_index;
1042 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1043 	ifm->ifm_data = ifp->if_data;
1044 	ifm->ifm_addrs = 0;
1045 	rt_dispatch(m, NULL);
1046 }
1047 
1048 /*
1049  * This is called to generate messages from the routing socket
1050  * indicating a network interface has had addresses associated with it.
1051  * if we ever reverse the logic and replace messages TO the routing
1052  * socket indicate a request to configure interfaces, then it will
1053  * be unnecessary as the routing socket will automatically generate
1054  * copies of it.
1055  */
1056 void
1057 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1058 {
1059 	struct rt_addrinfo info;
1060 	struct sockaddr *sa = NULL;
1061 	int pass;
1062 	struct mbuf *m = NULL;
1063 	struct ifnet *ifp = ifa->ifa_ifp;
1064 
1065 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1066 		("unexpected cmd %u", cmd));
1067 #ifdef SCTP
1068 	/*
1069 	 * notify the SCTP stack
1070 	 * this will only get called when an address is added/deleted
1071 	 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1072 	 */
1073 	sctp_addr_change(ifa, cmd);
1074 #endif /* SCTP */
1075 	if (route_cb.any_count == 0)
1076 		return;
1077 	for (pass = 1; pass < 3; pass++) {
1078 		bzero((caddr_t)&info, sizeof(info));
1079 		if ((cmd == RTM_ADD && pass == 1) ||
1080 		    (cmd == RTM_DELETE && pass == 2)) {
1081 			struct ifa_msghdr *ifam;
1082 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1083 
1084 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1085 			info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1086 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1087 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1088 			if ((m = rt_msg1(ncmd, &info)) == NULL)
1089 				continue;
1090 			ifam = mtod(m, struct ifa_msghdr *);
1091 			ifam->ifam_index = ifp->if_index;
1092 			ifam->ifam_metric = ifa->ifa_metric;
1093 			ifam->ifam_flags = ifa->ifa_flags;
1094 			ifam->ifam_addrs = info.rti_addrs;
1095 		}
1096 		if ((cmd == RTM_ADD && pass == 2) ||
1097 		    (cmd == RTM_DELETE && pass == 1)) {
1098 			struct rt_msghdr *rtm;
1099 
1100 			if (rt == NULL)
1101 				continue;
1102 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1103 			info.rti_info[RTAX_DST] = sa = rt_key(rt);
1104 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1105 			if ((m = rt_msg1(cmd, &info)) == NULL)
1106 				continue;
1107 			rtm = mtod(m, struct rt_msghdr *);
1108 			rtm->rtm_index = ifp->if_index;
1109 			rtm->rtm_flags |= rt->rt_flags;
1110 			rtm->rtm_errno = error;
1111 			rtm->rtm_addrs = info.rti_addrs;
1112 		}
1113 		rt_dispatch(m, sa);
1114 	}
1115 }
1116 
1117 /*
1118  * This is the analogue to the rt_newaddrmsg which performs the same
1119  * function but for multicast group memberhips.  This is easier since
1120  * there is no route state to worry about.
1121  */
1122 void
1123 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1124 {
1125 	struct rt_addrinfo info;
1126 	struct mbuf *m = NULL;
1127 	struct ifnet *ifp = ifma->ifma_ifp;
1128 	struct ifma_msghdr *ifmam;
1129 
1130 	if (route_cb.any_count == 0)
1131 		return;
1132 
1133 	bzero((caddr_t)&info, sizeof(info));
1134 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1135 	info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1136 	/*
1137 	 * If a link-layer address is present, present it as a ``gateway''
1138 	 * (similarly to how ARP entries, e.g., are presented).
1139 	 */
1140 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1141 	m = rt_msg1(cmd, &info);
1142 	if (m == NULL)
1143 		return;
1144 	ifmam = mtod(m, struct ifma_msghdr *);
1145 	KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1146 	    __func__));
1147 	ifmam->ifmam_index = ifp->if_index;
1148 	ifmam->ifmam_addrs = info.rti_addrs;
1149 	rt_dispatch(m, ifma->ifma_addr);
1150 }
1151 
1152 static struct mbuf *
1153 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1154 	struct rt_addrinfo *info)
1155 {
1156 	struct if_announcemsghdr *ifan;
1157 	struct mbuf *m;
1158 
1159 	if (route_cb.any_count == 0)
1160 		return NULL;
1161 	bzero((caddr_t)info, sizeof(*info));
1162 	m = rt_msg1(type, info);
1163 	if (m != NULL) {
1164 		ifan = mtod(m, struct if_announcemsghdr *);
1165 		ifan->ifan_index = ifp->if_index;
1166 		strlcpy(ifan->ifan_name, ifp->if_xname,
1167 			sizeof(ifan->ifan_name));
1168 		ifan->ifan_what = what;
1169 	}
1170 	return m;
1171 }
1172 
1173 /*
1174  * This is called to generate routing socket messages indicating
1175  * IEEE80211 wireless events.
1176  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1177  */
1178 void
1179 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1180 {
1181 	struct mbuf *m;
1182 	struct rt_addrinfo info;
1183 
1184 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1185 	if (m != NULL) {
1186 		/*
1187 		 * Append the ieee80211 data.  Try to stick it in the
1188 		 * mbuf containing the ifannounce msg; otherwise allocate
1189 		 * a new mbuf and append.
1190 		 *
1191 		 * NB: we assume m is a single mbuf.
1192 		 */
1193 		if (data_len > M_TRAILINGSPACE(m)) {
1194 			struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1195 			if (n == NULL) {
1196 				m_freem(m);
1197 				return;
1198 			}
1199 			bcopy(data, mtod(n, void *), data_len);
1200 			n->m_len = data_len;
1201 			m->m_next = n;
1202 		} else if (data_len > 0) {
1203 			bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1204 			m->m_len += data_len;
1205 		}
1206 		if (m->m_flags & M_PKTHDR)
1207 			m->m_pkthdr.len += data_len;
1208 		mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1209 		rt_dispatch(m, NULL);
1210 	}
1211 }
1212 
1213 /*
1214  * This is called to generate routing socket messages indicating
1215  * network interface arrival and departure.
1216  */
1217 void
1218 rt_ifannouncemsg(struct ifnet *ifp, int what)
1219 {
1220 	struct mbuf *m;
1221 	struct rt_addrinfo info;
1222 
1223 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1224 	if (m != NULL)
1225 		rt_dispatch(m, NULL);
1226 }
1227 
1228 static void
1229 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1230 {
1231 	INIT_VNET_NET(curvnet);
1232 	struct m_tag *tag;
1233 
1234 	/*
1235 	 * Preserve the family from the sockaddr, if any, in an m_tag for
1236 	 * use when injecting the mbuf into the routing socket buffer from
1237 	 * the netisr.
1238 	 */
1239 	if (sa != NULL) {
1240 		tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1241 		    M_NOWAIT);
1242 		if (tag == NULL) {
1243 			m_freem(m);
1244 			return;
1245 		}
1246 		*(unsigned short *)(tag + 1) = sa->sa_family;
1247 		m_tag_prepend(m, tag);
1248 	}
1249 	netisr_queue(NETISR_ROUTE, m);	/* mbuf is free'd on failure. */
1250 }
1251 
1252 /*
1253  * This is used in dumping the kernel table via sysctl().
1254  */
1255 static int
1256 sysctl_dumpentry(struct radix_node *rn, void *vw)
1257 {
1258 	struct walkarg *w = vw;
1259 	struct rtentry *rt = (struct rtentry *)rn;
1260 	int error = 0, size;
1261 	struct rt_addrinfo info;
1262 
1263 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1264 		return 0;
1265 	if (jailed(w->w_req->td->td_ucred) &&
1266 	    ((rt->rt_flags & RTF_HOST) == 0 ||
1267 	    !prison_if(w->w_req->td->td_ucred, rt_key(rt))))
1268 		return (0);
1269 	bzero((caddr_t)&info, sizeof(info));
1270 	info.rti_info[RTAX_DST] = rt_key(rt);
1271 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1272 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1273 	info.rti_info[RTAX_GENMASK] = 0;
1274 	if (rt->rt_ifp) {
1275 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1276 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1277 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1278 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1279 	}
1280 	size = rt_msg2(RTM_GET, &info, NULL, w);
1281 	if (w->w_req && w->w_tmem) {
1282 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1283 
1284 		rtm->rtm_flags = rt->rt_flags;
1285 		rtm->rtm_use = rt->rt_rmx.rmx_pksent;
1286 		rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1287 		rtm->rtm_index = rt->rt_ifp->if_index;
1288 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1289 		rtm->rtm_addrs = info.rti_addrs;
1290 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1291 		return (error);
1292 	}
1293 	return (error);
1294 }
1295 
1296 static int
1297 sysctl_iflist(int af, struct walkarg *w)
1298 {
1299 	INIT_VNET_NET(curvnet);
1300 	struct ifnet *ifp;
1301 	struct ifaddr *ifa;
1302 	struct rt_addrinfo info;
1303 	int len, error = 0;
1304 
1305 	bzero((caddr_t)&info, sizeof(info));
1306 	IFNET_RLOCK();
1307 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1308 		if (w->w_arg && w->w_arg != ifp->if_index)
1309 			continue;
1310 		ifa = ifp->if_addr;
1311 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1312 		len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1313 		info.rti_info[RTAX_IFP] = NULL;
1314 		if (w->w_req && w->w_tmem) {
1315 			struct if_msghdr *ifm;
1316 
1317 			ifm = (struct if_msghdr *)w->w_tmem;
1318 			ifm->ifm_index = ifp->if_index;
1319 			ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1320 			ifm->ifm_data = ifp->if_data;
1321 			ifm->ifm_addrs = info.rti_addrs;
1322 			error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1323 			if (error)
1324 				goto done;
1325 		}
1326 		while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1327 			if (af && af != ifa->ifa_addr->sa_family)
1328 				continue;
1329 			if (jailed(w->w_req->td->td_ucred) &&
1330 			    !prison_if(w->w_req->td->td_ucred, ifa->ifa_addr))
1331 				continue;
1332 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1333 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1334 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1335 			len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1336 			if (w->w_req && w->w_tmem) {
1337 				struct ifa_msghdr *ifam;
1338 
1339 				ifam = (struct ifa_msghdr *)w->w_tmem;
1340 				ifam->ifam_index = ifa->ifa_ifp->if_index;
1341 				ifam->ifam_flags = ifa->ifa_flags;
1342 				ifam->ifam_metric = ifa->ifa_metric;
1343 				ifam->ifam_addrs = info.rti_addrs;
1344 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1345 				if (error)
1346 					goto done;
1347 			}
1348 		}
1349 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1350 			info.rti_info[RTAX_BRD] = NULL;
1351 	}
1352 done:
1353 	IFNET_RUNLOCK();
1354 	return (error);
1355 }
1356 
1357 static int
1358 sysctl_ifmalist(int af, struct walkarg *w)
1359 {
1360 	INIT_VNET_NET(curvnet);
1361 	struct ifnet *ifp;
1362 	struct ifmultiaddr *ifma;
1363 	struct	rt_addrinfo info;
1364 	int	len, error = 0;
1365 	struct ifaddr *ifa;
1366 
1367 	bzero((caddr_t)&info, sizeof(info));
1368 	IFNET_RLOCK();
1369 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1370 		if (w->w_arg && w->w_arg != ifp->if_index)
1371 			continue;
1372 		ifa = ifp->if_addr;
1373 		info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1374 		IF_ADDR_LOCK(ifp);
1375 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1376 			if (af && af != ifma->ifma_addr->sa_family)
1377 				continue;
1378 			if (jailed(w->w_req->td->td_ucred) &&
1379 			    !prison_if(w->w_req->td->td_ucred, ifma->ifma_addr))
1380 				continue;
1381 			info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1382 			info.rti_info[RTAX_GATEWAY] =
1383 			    (ifma->ifma_addr->sa_family != AF_LINK) ?
1384 			    ifma->ifma_lladdr : NULL;
1385 			len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1386 			if (w->w_req && w->w_tmem) {
1387 				struct ifma_msghdr *ifmam;
1388 
1389 				ifmam = (struct ifma_msghdr *)w->w_tmem;
1390 				ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1391 				ifmam->ifmam_flags = 0;
1392 				ifmam->ifmam_addrs = info.rti_addrs;
1393 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1394 				if (error) {
1395 					IF_ADDR_UNLOCK(ifp);
1396 					goto done;
1397 				}
1398 			}
1399 		}
1400 		IF_ADDR_UNLOCK(ifp);
1401 	}
1402 done:
1403 	IFNET_RUNLOCK();
1404 	return (error);
1405 }
1406 
1407 static int
1408 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1409 {
1410 	INIT_VNET_NET(curvnet);
1411 	int	*name = (int *)arg1;
1412 	u_int	namelen = arg2;
1413 	struct radix_node_head *rnh;
1414 	int	i, lim, error = EINVAL;
1415 	u_char	af;
1416 	struct	walkarg w;
1417 
1418 	name ++;
1419 	namelen--;
1420 	if (req->newptr)
1421 		return (EPERM);
1422 	if (namelen != 3)
1423 		return ((namelen < 3) ? EISDIR : ENOTDIR);
1424 	af = name[0];
1425 	if (af > AF_MAX)
1426 		return (EINVAL);
1427 	bzero(&w, sizeof(w));
1428 	w.w_op = name[1];
1429 	w.w_arg = name[2];
1430 	w.w_req = req;
1431 
1432 	error = sysctl_wire_old_buffer(req, 0);
1433 	if (error)
1434 		return (error);
1435 	switch (w.w_op) {
1436 
1437 	case NET_RT_DUMP:
1438 	case NET_RT_FLAGS:
1439 		if (af == 0) {			/* dump all tables */
1440 			i = 1;
1441 			lim = AF_MAX;
1442 		} else				/* dump only one table */
1443 			i = lim = af;
1444 
1445 		/*
1446 		 * take care of llinfo entries, the caller must
1447 		 * specify an AF
1448 		 */
1449 		if (w.w_op == NET_RT_FLAGS && w.w_arg == 0) {
1450 			if (af != 0)
1451 				error = lltable_sysctl_dumparp(af, w.w_req);
1452 			else
1453 				error = EINVAL;
1454 			break;
1455 		}
1456 		/*
1457 		 * take care of routing entries
1458 		 */
1459 		for (error = 0; error == 0 && i <= lim; i++)
1460 			if ((rnh = V_rt_tables[req->td->td_proc->p_fibnum][i]) != NULL) {
1461 				RADIX_NODE_HEAD_LOCK(rnh);
1462 			    	error = rnh->rnh_walktree(rnh,
1463 				    sysctl_dumpentry, &w);
1464 				RADIX_NODE_HEAD_UNLOCK(rnh);
1465 			} else if (af != 0)
1466 				error = EAFNOSUPPORT;
1467 		break;
1468 
1469 	case NET_RT_IFLIST:
1470 		error = sysctl_iflist(af, &w);
1471 		break;
1472 
1473 	case NET_RT_IFMALIST:
1474 		error = sysctl_ifmalist(af, &w);
1475 		break;
1476 	}
1477 	if (w.w_tmem)
1478 		free(w.w_tmem, M_RTABLE);
1479 	return (error);
1480 }
1481 
1482 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1483 
1484 /*
1485  * Definitions of protocols supported in the ROUTE domain.
1486  */
1487 
1488 static struct domain routedomain;		/* or at least forward */
1489 
1490 static struct protosw routesw[] = {
1491 {
1492 	.pr_type =		SOCK_RAW,
1493 	.pr_domain =		&routedomain,
1494 	.pr_flags =		PR_ATOMIC|PR_ADDR,
1495 	.pr_output =		route_output,
1496 	.pr_ctlinput =		raw_ctlinput,
1497 	.pr_init =		raw_init,
1498 	.pr_usrreqs =		&route_usrreqs
1499 }
1500 };
1501 
1502 static struct domain routedomain = {
1503 	.dom_family =		PF_ROUTE,
1504 	.dom_name =		 "route",
1505 	.dom_protosw =		routesw,
1506 	.dom_protoswNPROTOSW =	&routesw[sizeof(routesw)/sizeof(routesw[0])]
1507 };
1508 
1509 DOMAIN_SET(route);
1510