xref: /freebsd/sys/net/rtsock.c (revision d184218c)
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_compat.h"
33 #include "opt_sctp.h"
34 #include "opt_mpath.h"
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 
38 #include <sys/param.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_llatbl.h>
58 #include <net/if_types.h>
59 #include <net/netisr.h>
60 #include <net/raw_cb.h>
61 #include <net/route.h>
62 #include <net/vnet.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/ip_carp.h>
67 #ifdef INET6
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/scope6_var.h>
70 #endif
71 
72 #if defined(INET) || defined(INET6)
73 #ifdef SCTP
74 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
75 #endif /* SCTP */
76 #endif
77 
78 #ifdef COMPAT_FREEBSD32
79 #include <sys/mount.h>
80 #include <compat/freebsd32/freebsd32.h>
81 
82 struct if_data32 {
83 	uint8_t	ifi_type;
84 	uint8_t	ifi_physical;
85 	uint8_t	ifi_addrlen;
86 	uint8_t	ifi_hdrlen;
87 	uint8_t	ifi_link_state;
88 	uint8_t	ifi_vhid;
89 	uint8_t	ifi_spare_char2;
90 	uint8_t	ifi_datalen;
91 	uint32_t ifi_mtu;
92 	uint32_t ifi_metric;
93 	uint32_t ifi_baudrate;
94 	uint32_t ifi_ipackets;
95 	uint32_t ifi_ierrors;
96 	uint32_t ifi_opackets;
97 	uint32_t ifi_oerrors;
98 	uint32_t ifi_collisions;
99 	uint32_t ifi_ibytes;
100 	uint32_t ifi_obytes;
101 	uint32_t ifi_imcasts;
102 	uint32_t ifi_omcasts;
103 	uint32_t ifi_iqdrops;
104 	uint32_t ifi_noproto;
105 	uint32_t ifi_hwassist;
106 	int32_t	ifi_epoch;
107 	struct	timeval32 ifi_lastchange;
108 };
109 
110 struct if_msghdr32 {
111 	uint16_t ifm_msglen;
112 	uint8_t	ifm_version;
113 	uint8_t	ifm_type;
114 	int32_t	ifm_addrs;
115 	int32_t	ifm_flags;
116 	uint16_t ifm_index;
117 	struct	if_data32 ifm_data;
118 };
119 
120 struct if_msghdrl32 {
121 	uint16_t ifm_msglen;
122 	uint8_t	ifm_version;
123 	uint8_t	ifm_type;
124 	int32_t	ifm_addrs;
125 	int32_t	ifm_flags;
126 	uint16_t ifm_index;
127 	uint16_t _ifm_spare1;
128 	uint16_t ifm_len;
129 	uint16_t ifm_data_off;
130 	struct	if_data32 ifm_data;
131 };
132 
133 struct ifa_msghdrl32 {
134 	uint16_t ifam_msglen;
135 	uint8_t	ifam_version;
136 	uint8_t	ifam_type;
137 	int32_t	ifam_addrs;
138 	int32_t	ifam_flags;
139 	uint16_t ifam_index;
140 	uint16_t _ifam_spare1;
141 	uint16_t ifam_len;
142 	uint16_t ifam_data_off;
143 	int32_t	ifam_metric;
144 	struct	if_data32 ifam_data;
145 };
146 #endif /* COMPAT_FREEBSD32 */
147 
148 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
149 
150 /* NB: these are not modified */
151 static struct	sockaddr route_src = { 2, PF_ROUTE, };
152 static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
153 
154 /* These are external hooks for CARP. */
155 int	(*carp_get_vhid_p)(struct ifaddr *);
156 
157 /*
158  * Used by rtsock/raw_input callback code to decide whether to filter the update
159  * notification to a socket bound to a particular FIB.
160  */
161 #define	RTS_FILTER_FIB	M_PROTO8
162 #define	RTS_ALLFIBS	-1
163 
164 static struct {
165 	int	ip_count;	/* attached w/ AF_INET */
166 	int	ip6_count;	/* attached w/ AF_INET6 */
167 	int	ipx_count;	/* attached w/ AF_IPX */
168 	int	any_count;	/* total attached */
169 } route_cb;
170 
171 struct mtx rtsock_mtx;
172 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
173 
174 #define	RTSOCK_LOCK()	mtx_lock(&rtsock_mtx)
175 #define	RTSOCK_UNLOCK()	mtx_unlock(&rtsock_mtx)
176 #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
177 
178 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
179 
180 struct walkarg {
181 	int	w_tmemsize;
182 	int	w_op, w_arg;
183 	caddr_t	w_tmem;
184 	struct sysctl_req *w_req;
185 };
186 
187 static void	rts_input(struct mbuf *m);
188 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
189 static int	rt_msg2(int type, struct rt_addrinfo *rtinfo,
190 			caddr_t cp, struct walkarg *w);
191 static int	rt_xaddrs(caddr_t cp, caddr_t cplim,
192 			struct rt_addrinfo *rtinfo);
193 static int	sysctl_dumpentry(struct radix_node *rn, void *vw);
194 static int	sysctl_iflist(int af, struct walkarg *w);
195 static int	sysctl_ifmalist(int af, struct walkarg *w);
196 static int	route_output(struct mbuf *m, struct socket *so);
197 static void	rt_setmetrics(u_long which, const struct rt_metrics *in,
198 			struct rt_metrics_lite *out);
199 static void	rt_getmetrics(const struct rt_metrics_lite *in,
200 			struct rt_metrics *out);
201 static void	rt_dispatch(struct mbuf *, sa_family_t);
202 
203 static struct netisr_handler rtsock_nh = {
204 	.nh_name = "rtsock",
205 	.nh_handler = rts_input,
206 	.nh_proto = NETISR_ROUTE,
207 	.nh_policy = NETISR_POLICY_SOURCE,
208 };
209 
210 static int
211 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
212 {
213 	int error, qlimit;
214 
215 	netisr_getqlimit(&rtsock_nh, &qlimit);
216 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
217         if (error || !req->newptr)
218                 return (error);
219 	if (qlimit < 1)
220 		return (EINVAL);
221 	return (netisr_setqlimit(&rtsock_nh, qlimit));
222 }
223 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
224     0, 0, sysctl_route_netisr_maxqlen, "I",
225     "maximum routing socket dispatch queue length");
226 
227 static void
228 rts_init(void)
229 {
230 	int tmp;
231 
232 	if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
233 		rtsock_nh.nh_qlimit = tmp;
234 	netisr_register(&rtsock_nh);
235 }
236 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
237 
238 static int
239 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
240     struct rawcb *rp)
241 {
242 	int fibnum;
243 
244 	KASSERT(m != NULL, ("%s: m is NULL", __func__));
245 	KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
246 	KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
247 
248 	/* No filtering requested. */
249 	if ((m->m_flags & RTS_FILTER_FIB) == 0)
250 		return (0);
251 
252 	/* Check if it is a rts and the fib matches the one of the socket. */
253 	fibnum = M_GETFIB(m);
254 	if (proto->sp_family != PF_ROUTE ||
255 	    rp->rcb_socket == NULL ||
256 	    rp->rcb_socket->so_fibnum == fibnum)
257 		return (0);
258 
259 	/* Filtering requested and no match, the socket shall be skipped. */
260 	return (1);
261 }
262 
263 static void
264 rts_input(struct mbuf *m)
265 {
266 	struct sockproto route_proto;
267 	unsigned short *family;
268 	struct m_tag *tag;
269 
270 	route_proto.sp_family = PF_ROUTE;
271 	tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
272 	if (tag != NULL) {
273 		family = (unsigned short *)(tag + 1);
274 		route_proto.sp_protocol = *family;
275 		m_tag_delete(m, tag);
276 	} else
277 		route_proto.sp_protocol = 0;
278 
279 	raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
280 }
281 
282 /*
283  * It really doesn't make any sense at all for this code to share much
284  * with raw_usrreq.c, since its functionality is so restricted.  XXX
285  */
286 static void
287 rts_abort(struct socket *so)
288 {
289 
290 	raw_usrreqs.pru_abort(so);
291 }
292 
293 static void
294 rts_close(struct socket *so)
295 {
296 
297 	raw_usrreqs.pru_close(so);
298 }
299 
300 /* pru_accept is EOPNOTSUPP */
301 
302 static int
303 rts_attach(struct socket *so, int proto, struct thread *td)
304 {
305 	struct rawcb *rp;
306 	int error;
307 
308 	KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
309 
310 	/* XXX */
311 	rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
312 	if (rp == NULL)
313 		return ENOBUFS;
314 
315 	so->so_pcb = (caddr_t)rp;
316 	so->so_fibnum = td->td_proc->p_fibnum;
317 	error = raw_attach(so, proto);
318 	rp = sotorawcb(so);
319 	if (error) {
320 		so->so_pcb = NULL;
321 		free(rp, M_PCB);
322 		return error;
323 	}
324 	RTSOCK_LOCK();
325 	switch(rp->rcb_proto.sp_protocol) {
326 	case AF_INET:
327 		route_cb.ip_count++;
328 		break;
329 	case AF_INET6:
330 		route_cb.ip6_count++;
331 		break;
332 	case AF_IPX:
333 		route_cb.ipx_count++;
334 		break;
335 	}
336 	route_cb.any_count++;
337 	RTSOCK_UNLOCK();
338 	soisconnected(so);
339 	so->so_options |= SO_USELOOPBACK;
340 	return 0;
341 }
342 
343 static int
344 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
345 {
346 
347 	return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
348 }
349 
350 static int
351 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
352 {
353 
354 	return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
355 }
356 
357 /* pru_connect2 is EOPNOTSUPP */
358 /* pru_control is EOPNOTSUPP */
359 
360 static void
361 rts_detach(struct socket *so)
362 {
363 	struct rawcb *rp = sotorawcb(so);
364 
365 	KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
366 
367 	RTSOCK_LOCK();
368 	switch(rp->rcb_proto.sp_protocol) {
369 	case AF_INET:
370 		route_cb.ip_count--;
371 		break;
372 	case AF_INET6:
373 		route_cb.ip6_count--;
374 		break;
375 	case AF_IPX:
376 		route_cb.ipx_count--;
377 		break;
378 	}
379 	route_cb.any_count--;
380 	RTSOCK_UNLOCK();
381 	raw_usrreqs.pru_detach(so);
382 }
383 
384 static int
385 rts_disconnect(struct socket *so)
386 {
387 
388 	return (raw_usrreqs.pru_disconnect(so));
389 }
390 
391 /* pru_listen is EOPNOTSUPP */
392 
393 static int
394 rts_peeraddr(struct socket *so, struct sockaddr **nam)
395 {
396 
397 	return (raw_usrreqs.pru_peeraddr(so, nam));
398 }
399 
400 /* pru_rcvd is EOPNOTSUPP */
401 /* pru_rcvoob is EOPNOTSUPP */
402 
403 static int
404 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
405 	 struct mbuf *control, struct thread *td)
406 {
407 
408 	return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
409 }
410 
411 /* pru_sense is null */
412 
413 static int
414 rts_shutdown(struct socket *so)
415 {
416 
417 	return (raw_usrreqs.pru_shutdown(so));
418 }
419 
420 static int
421 rts_sockaddr(struct socket *so, struct sockaddr **nam)
422 {
423 
424 	return (raw_usrreqs.pru_sockaddr(so, nam));
425 }
426 
427 static struct pr_usrreqs route_usrreqs = {
428 	.pru_abort =		rts_abort,
429 	.pru_attach =		rts_attach,
430 	.pru_bind =		rts_bind,
431 	.pru_connect =		rts_connect,
432 	.pru_detach =		rts_detach,
433 	.pru_disconnect =	rts_disconnect,
434 	.pru_peeraddr =		rts_peeraddr,
435 	.pru_send =		rts_send,
436 	.pru_shutdown =		rts_shutdown,
437 	.pru_sockaddr =		rts_sockaddr,
438 	.pru_close =		rts_close,
439 };
440 
441 #ifndef _SOCKADDR_UNION_DEFINED
442 #define	_SOCKADDR_UNION_DEFINED
443 /*
444  * The union of all possible address formats we handle.
445  */
446 union sockaddr_union {
447 	struct sockaddr		sa;
448 	struct sockaddr_in	sin;
449 	struct sockaddr_in6	sin6;
450 };
451 #endif /* _SOCKADDR_UNION_DEFINED */
452 
453 static int
454 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
455     struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
456 {
457 
458 	/* First, see if the returned address is part of the jail. */
459 	if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
460 		info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
461 		return (0);
462 	}
463 
464 	switch (info->rti_info[RTAX_DST]->sa_family) {
465 #ifdef INET
466 	case AF_INET:
467 	{
468 		struct in_addr ia;
469 		struct ifaddr *ifa;
470 		int found;
471 
472 		found = 0;
473 		/*
474 		 * Try to find an address on the given outgoing interface
475 		 * that belongs to the jail.
476 		 */
477 		IF_ADDR_RLOCK(ifp);
478 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
479 			struct sockaddr *sa;
480 			sa = ifa->ifa_addr;
481 			if (sa->sa_family != AF_INET)
482 				continue;
483 			ia = ((struct sockaddr_in *)sa)->sin_addr;
484 			if (prison_check_ip4(cred, &ia) == 0) {
485 				found = 1;
486 				break;
487 			}
488 		}
489 		IF_ADDR_RUNLOCK(ifp);
490 		if (!found) {
491 			/*
492 			 * As a last resort return the 'default' jail address.
493 			 */
494 			ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
495 			    sin_addr;
496 			if (prison_get_ip4(cred, &ia) != 0)
497 				return (ESRCH);
498 		}
499 		bzero(&saun->sin, sizeof(struct sockaddr_in));
500 		saun->sin.sin_len = sizeof(struct sockaddr_in);
501 		saun->sin.sin_family = AF_INET;
502 		saun->sin.sin_addr.s_addr = ia.s_addr;
503 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
504 		break;
505 	}
506 #endif
507 #ifdef INET6
508 	case AF_INET6:
509 	{
510 		struct in6_addr ia6;
511 		struct ifaddr *ifa;
512 		int found;
513 
514 		found = 0;
515 		/*
516 		 * Try to find an address on the given outgoing interface
517 		 * that belongs to the jail.
518 		 */
519 		IF_ADDR_RLOCK(ifp);
520 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
521 			struct sockaddr *sa;
522 			sa = ifa->ifa_addr;
523 			if (sa->sa_family != AF_INET6)
524 				continue;
525 			bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
526 			    &ia6, sizeof(struct in6_addr));
527 			if (prison_check_ip6(cred, &ia6) == 0) {
528 				found = 1;
529 				break;
530 			}
531 		}
532 		IF_ADDR_RUNLOCK(ifp);
533 		if (!found) {
534 			/*
535 			 * As a last resort return the 'default' jail address.
536 			 */
537 			ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
538 			    sin6_addr;
539 			if (prison_get_ip6(cred, &ia6) != 0)
540 				return (ESRCH);
541 		}
542 		bzero(&saun->sin6, sizeof(struct sockaddr_in6));
543 		saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
544 		saun->sin6.sin6_family = AF_INET6;
545 		bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
546 		if (sa6_recoverscope(&saun->sin6) != 0)
547 			return (ESRCH);
548 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
549 		break;
550 	}
551 #endif
552 	default:
553 		return (ESRCH);
554 	}
555 	return (0);
556 }
557 
558 /*ARGSUSED*/
559 static int
560 route_output(struct mbuf *m, struct socket *so)
561 {
562 #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
563 	struct rt_msghdr *rtm = NULL;
564 	struct rtentry *rt = NULL;
565 	struct radix_node_head *rnh;
566 	struct rt_addrinfo info;
567 #ifdef INET6
568 	struct sockaddr_storage ss;
569 	struct sockaddr_in6 *sin6;
570 	int i, rti_need_deembed = 0;
571 #endif
572 	int len, error = 0;
573 	struct ifnet *ifp = NULL;
574 	union sockaddr_union saun;
575 	sa_family_t saf = AF_UNSPEC;
576 
577 #define senderr(e) { error = e; goto flush;}
578 	if (m == NULL || ((m->m_len < sizeof(long)) &&
579 		       (m = m_pullup(m, sizeof(long))) == NULL))
580 		return (ENOBUFS);
581 	if ((m->m_flags & M_PKTHDR) == 0)
582 		panic("route_output");
583 	len = m->m_pkthdr.len;
584 	if (len < sizeof(*rtm) ||
585 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
586 		info.rti_info[RTAX_DST] = NULL;
587 		senderr(EINVAL);
588 	}
589 	R_Malloc(rtm, struct rt_msghdr *, len);
590 	if (rtm == NULL) {
591 		info.rti_info[RTAX_DST] = NULL;
592 		senderr(ENOBUFS);
593 	}
594 	m_copydata(m, 0, len, (caddr_t)rtm);
595 	if (rtm->rtm_version != RTM_VERSION) {
596 		info.rti_info[RTAX_DST] = NULL;
597 		senderr(EPROTONOSUPPORT);
598 	}
599 	rtm->rtm_pid = curproc->p_pid;
600 	bzero(&info, sizeof(info));
601 	info.rti_addrs = rtm->rtm_addrs;
602 	/*
603 	 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
604 	 * link-local address because rtrequest requires addresses with
605 	 * embedded scope id.
606 	 */
607 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
608 		info.rti_info[RTAX_DST] = NULL;
609 		senderr(EINVAL);
610 	}
611 	info.rti_flags = rtm->rtm_flags;
612 	if (info.rti_info[RTAX_DST] == NULL ||
613 	    info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
614 	    (info.rti_info[RTAX_GATEWAY] != NULL &&
615 	     info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
616 		senderr(EINVAL);
617 	saf = info.rti_info[RTAX_DST]->sa_family;
618 	/*
619 	 * Verify that the caller has the appropriate privilege; RTM_GET
620 	 * is the only operation the non-superuser is allowed.
621 	 */
622 	if (rtm->rtm_type != RTM_GET) {
623 		error = priv_check(curthread, PRIV_NET_ROUTE);
624 		if (error)
625 			senderr(error);
626 	}
627 
628 	/*
629 	 * The given gateway address may be an interface address.
630 	 * For example, issuing a "route change" command on a route
631 	 * entry that was created from a tunnel, and the gateway
632 	 * address given is the local end point. In this case the
633 	 * RTF_GATEWAY flag must be cleared or the destination will
634 	 * not be reachable even though there is no error message.
635 	 */
636 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
637 	    info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
638 		struct route gw_ro;
639 
640 		bzero(&gw_ro, sizeof(gw_ro));
641 		gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
642 		rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
643 		/*
644 		 * A host route through the loopback interface is
645 		 * installed for each interface adddress. In pre 8.0
646 		 * releases the interface address of a PPP link type
647 		 * is not reachable locally. This behavior is fixed as
648 		 * part of the new L2/L3 redesign and rewrite work. The
649 		 * signature of this interface address route is the
650 		 * AF_LINK sa_family type of the rt_gateway, and the
651 		 * rt_ifp has the IFF_LOOPBACK flag set.
652 		 */
653 		if (gw_ro.ro_rt != NULL &&
654 		    gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
655 		    gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
656 			info.rti_flags &= ~RTF_GATEWAY;
657 		if (gw_ro.ro_rt != NULL)
658 			RTFREE(gw_ro.ro_rt);
659 	}
660 
661 	switch (rtm->rtm_type) {
662 		struct rtentry *saved_nrt;
663 
664 	case RTM_ADD:
665 		if (info.rti_info[RTAX_GATEWAY] == NULL)
666 			senderr(EINVAL);
667 		saved_nrt = NULL;
668 
669 		/* support for new ARP code */
670 		if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
671 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
672 			error = lla_rt_output(rtm, &info);
673 #ifdef INET6
674 			if (error == 0)
675 				rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
676 #endif
677 			break;
678 		}
679 		error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
680 		    so->so_fibnum);
681 		if (error == 0 && saved_nrt) {
682 #ifdef INET6
683 			rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
684 #endif
685 			RT_LOCK(saved_nrt);
686 			rt_setmetrics(rtm->rtm_inits,
687 				&rtm->rtm_rmx, &saved_nrt->rt_rmx);
688 			rtm->rtm_index = saved_nrt->rt_ifp->if_index;
689 			RT_REMREF(saved_nrt);
690 			RT_UNLOCK(saved_nrt);
691 		}
692 		break;
693 
694 	case RTM_DELETE:
695 		saved_nrt = NULL;
696 		/* support for new ARP code */
697 		if (info.rti_info[RTAX_GATEWAY] &&
698 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
699 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
700 			error = lla_rt_output(rtm, &info);
701 #ifdef INET6
702 			if (error == 0)
703 				rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
704 #endif
705 			break;
706 		}
707 		error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
708 		    so->so_fibnum);
709 		if (error == 0) {
710 			RT_LOCK(saved_nrt);
711 			rt = saved_nrt;
712 			goto report;
713 		}
714 #ifdef INET6
715 		/* rt_msg2() will not be used when RTM_DELETE fails. */
716 		rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
717 #endif
718 		break;
719 
720 	case RTM_GET:
721 	case RTM_CHANGE:
722 	case RTM_LOCK:
723 		rnh = rt_tables_get_rnh(so->so_fibnum,
724 		    info.rti_info[RTAX_DST]->sa_family);
725 		if (rnh == NULL)
726 			senderr(EAFNOSUPPORT);
727 		RADIX_NODE_HEAD_RLOCK(rnh);
728 		rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
729 			info.rti_info[RTAX_NETMASK], rnh);
730 		if (rt == NULL) {	/* XXX looks bogus */
731 			RADIX_NODE_HEAD_RUNLOCK(rnh);
732 			senderr(ESRCH);
733 		}
734 #ifdef RADIX_MPATH
735 		/*
736 		 * for RTM_CHANGE/LOCK, if we got multipath routes,
737 		 * we require users to specify a matching RTAX_GATEWAY.
738 		 *
739 		 * for RTM_GET, gate is optional even with multipath.
740 		 * if gate == NULL the first match is returned.
741 		 * (no need to call rt_mpath_matchgate if gate == NULL)
742 		 */
743 		if (rn_mpath_capable(rnh) &&
744 		    (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
745 			rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
746 			if (!rt) {
747 				RADIX_NODE_HEAD_RUNLOCK(rnh);
748 				senderr(ESRCH);
749 			}
750 		}
751 #endif
752 		/*
753 		 * If performing proxied L2 entry insertion, and
754 		 * the actual PPP host entry is found, perform
755 		 * another search to retrieve the prefix route of
756 		 * the local end point of the PPP link.
757 		 */
758 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
759 			struct sockaddr laddr;
760 
761 			if (rt->rt_ifp != NULL &&
762 			    rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
763 				struct ifaddr *ifa;
764 
765 				ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
766 				if (ifa != NULL)
767 					rt_maskedcopy(ifa->ifa_addr,
768 						      &laddr,
769 						      ifa->ifa_netmask);
770 			} else
771 				rt_maskedcopy(rt->rt_ifa->ifa_addr,
772 					      &laddr,
773 					      rt->rt_ifa->ifa_netmask);
774 			/*
775 			 * refactor rt and no lock operation necessary
776 			 */
777 			rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
778 			if (rt == NULL) {
779 				RADIX_NODE_HEAD_RUNLOCK(rnh);
780 				senderr(ESRCH);
781 			}
782 		}
783 		RT_LOCK(rt);
784 		RT_ADDREF(rt);
785 		RADIX_NODE_HEAD_RUNLOCK(rnh);
786 
787 		/*
788 		 * Fix for PR: 82974
789 		 *
790 		 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
791 		 * returns a perfect match in case a netmask is
792 		 * specified.  For host routes only a longest prefix
793 		 * match is returned so it is necessary to compare the
794 		 * existence of the netmask.  If both have a netmask
795 		 * rnh_lookup() did a perfect match and if none of them
796 		 * have a netmask both are host routes which is also a
797 		 * perfect match.
798 		 */
799 
800 		if (rtm->rtm_type != RTM_GET &&
801 		    (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
802 			RT_UNLOCK(rt);
803 			senderr(ESRCH);
804 		}
805 
806 		switch(rtm->rtm_type) {
807 
808 		case RTM_GET:
809 		report:
810 			RT_LOCK_ASSERT(rt);
811 			if ((rt->rt_flags & RTF_HOST) == 0
812 			    ? jailed_without_vnet(curthread->td_ucred)
813 			    : prison_if(curthread->td_ucred,
814 			    rt_key(rt)) != 0) {
815 				RT_UNLOCK(rt);
816 				senderr(ESRCH);
817 			}
818 			info.rti_info[RTAX_DST] = rt_key(rt);
819 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
820 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
821 			info.rti_info[RTAX_GENMASK] = 0;
822 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
823 				ifp = rt->rt_ifp;
824 				if (ifp) {
825 					info.rti_info[RTAX_IFP] =
826 					    ifp->if_addr->ifa_addr;
827 					error = rtm_get_jailed(&info, ifp, rt,
828 					    &saun, curthread->td_ucred);
829 					if (error != 0) {
830 						RT_UNLOCK(rt);
831 						senderr(error);
832 					}
833 					if (ifp->if_flags & IFF_POINTOPOINT)
834 						info.rti_info[RTAX_BRD] =
835 						    rt->rt_ifa->ifa_dstaddr;
836 					rtm->rtm_index = ifp->if_index;
837 				} else {
838 					info.rti_info[RTAX_IFP] = NULL;
839 					info.rti_info[RTAX_IFA] = NULL;
840 				}
841 			} else if ((ifp = rt->rt_ifp) != NULL) {
842 				rtm->rtm_index = ifp->if_index;
843 			}
844 			len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
845 			if (len > rtm->rtm_msglen) {
846 				struct rt_msghdr *new_rtm;
847 				R_Malloc(new_rtm, struct rt_msghdr *, len);
848 				if (new_rtm == NULL) {
849 					RT_UNLOCK(rt);
850 					senderr(ENOBUFS);
851 				}
852 				bcopy(rtm, new_rtm, rtm->rtm_msglen);
853 				Free(rtm); rtm = new_rtm;
854 			}
855 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
856 			rtm->rtm_flags = rt->rt_flags;
857 			rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
858 			rtm->rtm_addrs = info.rti_addrs;
859 			break;
860 
861 		case RTM_CHANGE:
862 			/*
863 			 * New gateway could require new ifaddr, ifp;
864 			 * flags may also be different; ifp may be specified
865 			 * by ll sockaddr when protocol address is ambiguous
866 			 */
867 			if (((rt->rt_flags & RTF_GATEWAY) &&
868 			     info.rti_info[RTAX_GATEWAY] != NULL) ||
869 			    info.rti_info[RTAX_IFP] != NULL ||
870 			    (info.rti_info[RTAX_IFA] != NULL &&
871 			     !sa_equal(info.rti_info[RTAX_IFA],
872 				       rt->rt_ifa->ifa_addr))) {
873 				RT_UNLOCK(rt);
874 				RADIX_NODE_HEAD_LOCK(rnh);
875 				error = rt_getifa_fib(&info, rt->rt_fibnum);
876 				/*
877 				 * XXXRW: Really we should release this
878 				 * reference later, but this maintains
879 				 * historical behavior.
880 				 */
881 				if (info.rti_ifa != NULL)
882 					ifa_free(info.rti_ifa);
883 				RADIX_NODE_HEAD_UNLOCK(rnh);
884 				if (error != 0)
885 					senderr(error);
886 				RT_LOCK(rt);
887 			}
888 			if (info.rti_ifa != NULL &&
889 			    info.rti_ifa != rt->rt_ifa &&
890 			    rt->rt_ifa != NULL &&
891 			    rt->rt_ifa->ifa_rtrequest != NULL) {
892 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
893 				    &info);
894 				ifa_free(rt->rt_ifa);
895 			}
896 			if (info.rti_info[RTAX_GATEWAY] != NULL) {
897 				RT_UNLOCK(rt);
898 				RADIX_NODE_HEAD_LOCK(rnh);
899 				RT_LOCK(rt);
900 
901 				error = rt_setgate(rt, rt_key(rt),
902 				    info.rti_info[RTAX_GATEWAY]);
903 				RADIX_NODE_HEAD_UNLOCK(rnh);
904 				if (error != 0) {
905 					RT_UNLOCK(rt);
906 					senderr(error);
907 				}
908 				rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
909 			}
910 			if (info.rti_ifa != NULL &&
911 			    info.rti_ifa != rt->rt_ifa) {
912 				ifa_ref(info.rti_ifa);
913 				rt->rt_ifa = info.rti_ifa;
914 				rt->rt_ifp = info.rti_ifp;
915 			}
916 			/* Allow some flags to be toggled on change. */
917 			rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
918 				    (rtm->rtm_flags & RTF_FMASK);
919 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
920 					&rt->rt_rmx);
921 			rtm->rtm_index = rt->rt_ifp->if_index;
922 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
923 			       rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
924 			/* FALLTHROUGH */
925 		case RTM_LOCK:
926 			/* We don't support locks anymore */
927 			break;
928 		}
929 		RT_UNLOCK(rt);
930 		break;
931 
932 	default:
933 		senderr(EOPNOTSUPP);
934 	}
935 
936 flush:
937 	if (rtm) {
938 		if (error)
939 			rtm->rtm_errno = error;
940 		else
941 			rtm->rtm_flags |= RTF_DONE;
942 	}
943 	if (rt)		/* XXX can this be true? */
944 		RTFREE(rt);
945     {
946 	struct rawcb *rp = NULL;
947 	/*
948 	 * Check to see if we don't want our own messages.
949 	 */
950 	if ((so->so_options & SO_USELOOPBACK) == 0) {
951 		if (route_cb.any_count <= 1) {
952 			if (rtm)
953 				Free(rtm);
954 			m_freem(m);
955 			return (error);
956 		}
957 		/* There is another listener, so construct message */
958 		rp = sotorawcb(so);
959 	}
960 	if (rtm) {
961 #ifdef INET6
962 		if (rti_need_deembed) {
963 			/* sin6_scope_id is recovered before sending rtm. */
964 			for (i = 0; i < RTAX_MAX; i++) {
965 				sin6 = (struct sockaddr_in6 *)&ss;
966 				if (info.rti_info[i] == NULL)
967 					continue;
968 				if (info.rti_info[i]->sa_family != AF_INET6)
969 					continue;
970 				bcopy(info.rti_info[i], sin6, sizeof(*sin6));
971 				if (sa6_recoverscope(sin6) == 0)
972 					bcopy(sin6, info.rti_info[i],
973 						    sizeof(*sin6));
974 			}
975 		}
976 #endif
977 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
978 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
979 			m_freem(m);
980 			m = NULL;
981 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
982 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
983 	}
984 	if (m) {
985 		M_SETFIB(m, so->so_fibnum);
986 		m->m_flags |= RTS_FILTER_FIB;
987 		if (rp) {
988 			/*
989 			 * XXX insure we don't get a copy by
990 			 * invalidating our protocol
991 			 */
992 			unsigned short family = rp->rcb_proto.sp_family;
993 			rp->rcb_proto.sp_family = 0;
994 			rt_dispatch(m, saf);
995 			rp->rcb_proto.sp_family = family;
996 		} else
997 			rt_dispatch(m, saf);
998 	}
999 	/* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
1000 	if (rtm)
1001 		Free(rtm);
1002     }
1003 	return (error);
1004 #undef	sa_equal
1005 }
1006 
1007 static void
1008 rt_setmetrics(u_long which, const struct rt_metrics *in,
1009 	struct rt_metrics_lite *out)
1010 {
1011 #define metric(f, e) if (which & (f)) out->e = in->e;
1012 	/*
1013 	 * Only these are stored in the routing entry since introduction
1014 	 * of tcp hostcache. The rest is ignored.
1015 	 */
1016 	metric(RTV_MTU, rmx_mtu);
1017 	metric(RTV_WEIGHT, rmx_weight);
1018 	/* Userland -> kernel timebase conversion. */
1019 	if (which & RTV_EXPIRE)
1020 		out->rmx_expire = in->rmx_expire ?
1021 		    in->rmx_expire - time_second + time_uptime : 0;
1022 #undef metric
1023 }
1024 
1025 static void
1026 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
1027 {
1028 #define metric(e) out->e = in->e;
1029 	bzero(out, sizeof(*out));
1030 	metric(rmx_mtu);
1031 	metric(rmx_weight);
1032 	/* Kernel -> userland timebase conversion. */
1033 	out->rmx_expire = in->rmx_expire ?
1034 	    in->rmx_expire - time_uptime + time_second : 0;
1035 #undef metric
1036 }
1037 
1038 /*
1039  * Extract the addresses of the passed sockaddrs.
1040  * Do a little sanity checking so as to avoid bad memory references.
1041  * This data is derived straight from userland.
1042  */
1043 static int
1044 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1045 {
1046 	struct sockaddr *sa;
1047 	int i;
1048 
1049 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1050 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
1051 			continue;
1052 		sa = (struct sockaddr *)cp;
1053 		/*
1054 		 * It won't fit.
1055 		 */
1056 		if (cp + sa->sa_len > cplim)
1057 			return (EINVAL);
1058 		/*
1059 		 * there are no more.. quit now
1060 		 * If there are more bits, they are in error.
1061 		 * I've seen this. route(1) can evidently generate these.
1062 		 * This causes kernel to core dump.
1063 		 * for compatibility, If we see this, point to a safe address.
1064 		 */
1065 		if (sa->sa_len == 0) {
1066 			rtinfo->rti_info[i] = &sa_zero;
1067 			return (0); /* should be EINVAL but for compat */
1068 		}
1069 		/* accept it */
1070 #ifdef INET6
1071 		if (sa->sa_family == AF_INET6)
1072 			sa6_embedscope((struct sockaddr_in6 *)sa,
1073 			    V_ip6_use_defzone);
1074 #endif
1075 		rtinfo->rti_info[i] = sa;
1076 		cp += SA_SIZE(sa);
1077 	}
1078 	return (0);
1079 }
1080 
1081 /*
1082  * Used by the routing socket.
1083  */
1084 static struct mbuf *
1085 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1086 {
1087 	struct rt_msghdr *rtm;
1088 	struct mbuf *m;
1089 	int i;
1090 	struct sockaddr *sa;
1091 #ifdef INET6
1092 	struct sockaddr_storage ss;
1093 	struct sockaddr_in6 *sin6;
1094 #endif
1095 	int len, dlen;
1096 
1097 	switch (type) {
1098 
1099 	case RTM_DELADDR:
1100 	case RTM_NEWADDR:
1101 		len = sizeof(struct ifa_msghdr);
1102 		break;
1103 
1104 	case RTM_DELMADDR:
1105 	case RTM_NEWMADDR:
1106 		len = sizeof(struct ifma_msghdr);
1107 		break;
1108 
1109 	case RTM_IFINFO:
1110 		len = sizeof(struct if_msghdr);
1111 		break;
1112 
1113 	case RTM_IFANNOUNCE:
1114 	case RTM_IEEE80211:
1115 		len = sizeof(struct if_announcemsghdr);
1116 		break;
1117 
1118 	default:
1119 		len = sizeof(struct rt_msghdr);
1120 	}
1121 
1122 	/* XXXGL: can we use MJUMPAGESIZE cluster here? */
1123 	KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1124 	if (len > MHLEN)
1125 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1126 	else
1127 		m = m_gethdr(M_NOWAIT, MT_DATA);
1128 	if (m == NULL)
1129 		return (m);
1130 
1131 	m->m_pkthdr.len = m->m_len = len;
1132 	rtm = mtod(m, struct rt_msghdr *);
1133 	bzero((caddr_t)rtm, len);
1134 	for (i = 0; i < RTAX_MAX; i++) {
1135 		if ((sa = rtinfo->rti_info[i]) == NULL)
1136 			continue;
1137 		rtinfo->rti_addrs |= (1 << i);
1138 		dlen = SA_SIZE(sa);
1139 #ifdef INET6
1140 		if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1141 			sin6 = (struct sockaddr_in6 *)&ss;
1142 			bcopy(sa, sin6, sizeof(*sin6));
1143 			if (sa6_recoverscope(sin6) == 0)
1144 				sa = (struct sockaddr *)sin6;
1145 		}
1146 #endif
1147 		m_copyback(m, len, dlen, (caddr_t)sa);
1148 		len += dlen;
1149 	}
1150 	if (m->m_pkthdr.len != len) {
1151 		m_freem(m);
1152 		return (NULL);
1153 	}
1154 	rtm->rtm_msglen = len;
1155 	rtm->rtm_version = RTM_VERSION;
1156 	rtm->rtm_type = type;
1157 	return (m);
1158 }
1159 
1160 /*
1161  * Used by the sysctl code and routing socket.
1162  */
1163 static int
1164 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1165 {
1166 	int i;
1167 	int len, dlen, second_time = 0;
1168 	caddr_t cp0;
1169 #ifdef INET6
1170 	struct sockaddr_storage ss;
1171 	struct sockaddr_in6 *sin6;
1172 #endif
1173 
1174 	rtinfo->rti_addrs = 0;
1175 again:
1176 	switch (type) {
1177 
1178 	case RTM_DELADDR:
1179 	case RTM_NEWADDR:
1180 		if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1181 #ifdef COMPAT_FREEBSD32
1182 			if (w->w_req->flags & SCTL_MASK32)
1183 				len = sizeof(struct ifa_msghdrl32);
1184 			else
1185 #endif
1186 				len = sizeof(struct ifa_msghdrl);
1187 		} else
1188 			len = sizeof(struct ifa_msghdr);
1189 		break;
1190 
1191 	case RTM_IFINFO:
1192 #ifdef COMPAT_FREEBSD32
1193 		if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1194 			if (w->w_op == NET_RT_IFLISTL)
1195 				len = sizeof(struct if_msghdrl32);
1196 			else
1197 				len = sizeof(struct if_msghdr32);
1198 			break;
1199 		}
1200 #endif
1201 		if (w != NULL && w->w_op == NET_RT_IFLISTL)
1202 			len = sizeof(struct if_msghdrl);
1203 		else
1204 			len = sizeof(struct if_msghdr);
1205 		break;
1206 
1207 	case RTM_NEWMADDR:
1208 		len = sizeof(struct ifma_msghdr);
1209 		break;
1210 
1211 	default:
1212 		len = sizeof(struct rt_msghdr);
1213 	}
1214 	cp0 = cp;
1215 	if (cp0)
1216 		cp += len;
1217 	for (i = 0; i < RTAX_MAX; i++) {
1218 		struct sockaddr *sa;
1219 
1220 		if ((sa = rtinfo->rti_info[i]) == NULL)
1221 			continue;
1222 		rtinfo->rti_addrs |= (1 << i);
1223 		dlen = SA_SIZE(sa);
1224 		if (cp) {
1225 #ifdef INET6
1226 			if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1227 				sin6 = (struct sockaddr_in6 *)&ss;
1228 				bcopy(sa, sin6, sizeof(*sin6));
1229 				if (sa6_recoverscope(sin6) == 0)
1230 					sa = (struct sockaddr *)sin6;
1231 			}
1232 #endif
1233 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
1234 			cp += dlen;
1235 		}
1236 		len += dlen;
1237 	}
1238 	len = ALIGN(len);
1239 	if (cp == NULL && w != NULL && !second_time) {
1240 		struct walkarg *rw = w;
1241 
1242 		if (rw->w_req) {
1243 			if (rw->w_tmemsize < len) {
1244 				if (rw->w_tmem)
1245 					free(rw->w_tmem, M_RTABLE);
1246 				rw->w_tmem = (caddr_t)
1247 					malloc(len, M_RTABLE, M_NOWAIT);
1248 				if (rw->w_tmem)
1249 					rw->w_tmemsize = len;
1250 			}
1251 			if (rw->w_tmem) {
1252 				cp = rw->w_tmem;
1253 				second_time = 1;
1254 				goto again;
1255 			}
1256 		}
1257 	}
1258 	if (cp) {
1259 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1260 
1261 		rtm->rtm_version = RTM_VERSION;
1262 		rtm->rtm_type = type;
1263 		rtm->rtm_msglen = len;
1264 	}
1265 	return (len);
1266 }
1267 
1268 /*
1269  * This routine is called to generate a message from the routing
1270  * socket indicating that a redirect has occured, a routing lookup
1271  * has failed, or that a protocol has detected timeouts to a particular
1272  * destination.
1273  */
1274 void
1275 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1276     int fibnum)
1277 {
1278 	struct rt_msghdr *rtm;
1279 	struct mbuf *m;
1280 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1281 
1282 	if (route_cb.any_count == 0)
1283 		return;
1284 	m = rt_msg1(type, rtinfo);
1285 	if (m == NULL)
1286 		return;
1287 
1288 	if (fibnum != RTS_ALLFIBS) {
1289 		KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1290 		    "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1291 		M_SETFIB(m, fibnum);
1292 		m->m_flags |= RTS_FILTER_FIB;
1293 	}
1294 
1295 	rtm = mtod(m, struct rt_msghdr *);
1296 	rtm->rtm_flags = RTF_DONE | flags;
1297 	rtm->rtm_errno = error;
1298 	rtm->rtm_addrs = rtinfo->rti_addrs;
1299 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1300 }
1301 
1302 void
1303 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1304 {
1305 
1306 	rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1307 }
1308 
1309 /*
1310  * This routine is called to generate a message from the routing
1311  * socket indicating that the status of a network interface has changed.
1312  */
1313 void
1314 rt_ifmsg(struct ifnet *ifp)
1315 {
1316 	struct if_msghdr *ifm;
1317 	struct mbuf *m;
1318 	struct rt_addrinfo info;
1319 
1320 	if (route_cb.any_count == 0)
1321 		return;
1322 	bzero((caddr_t)&info, sizeof(info));
1323 	m = rt_msg1(RTM_IFINFO, &info);
1324 	if (m == NULL)
1325 		return;
1326 	ifm = mtod(m, struct if_msghdr *);
1327 	ifm->ifm_index = ifp->if_index;
1328 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1329 	ifm->ifm_data = ifp->if_data;
1330 	ifm->ifm_addrs = 0;
1331 	rt_dispatch(m, AF_UNSPEC);
1332 }
1333 
1334 /*
1335  * This is called to generate messages from the routing socket
1336  * indicating a network interface has had addresses associated with it.
1337  * if we ever reverse the logic and replace messages TO the routing
1338  * socket indicate a request to configure interfaces, then it will
1339  * be unnecessary as the routing socket will automatically generate
1340  * copies of it.
1341  */
1342 void
1343 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1344     int fibnum)
1345 {
1346 	struct rt_addrinfo info;
1347 	struct sockaddr *sa = NULL;
1348 	int pass;
1349 	struct mbuf *m = NULL;
1350 	struct ifnet *ifp = ifa->ifa_ifp;
1351 
1352 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1353 		("unexpected cmd %u", cmd));
1354 #if defined(INET) || defined(INET6)
1355 #ifdef SCTP
1356 	/*
1357 	 * notify the SCTP stack
1358 	 * this will only get called when an address is added/deleted
1359 	 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1360 	 */
1361 	sctp_addr_change(ifa, cmd);
1362 #endif /* SCTP */
1363 #endif
1364 	if (route_cb.any_count == 0)
1365 		return;
1366 	for (pass = 1; pass < 3; pass++) {
1367 		bzero((caddr_t)&info, sizeof(info));
1368 		if ((cmd == RTM_ADD && pass == 1) ||
1369 		    (cmd == RTM_DELETE && pass == 2)) {
1370 			struct ifa_msghdr *ifam;
1371 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1372 
1373 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1374 			info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1375 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1376 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1377 			if ((m = rt_msg1(ncmd, &info)) == NULL)
1378 				continue;
1379 			ifam = mtod(m, struct ifa_msghdr *);
1380 			ifam->ifam_index = ifp->if_index;
1381 			ifam->ifam_metric = ifa->ifa_metric;
1382 			ifam->ifam_flags = ifa->ifa_flags;
1383 			ifam->ifam_addrs = info.rti_addrs;
1384 		}
1385 		if ((cmd == RTM_ADD && pass == 2) ||
1386 		    (cmd == RTM_DELETE && pass == 1)) {
1387 			struct rt_msghdr *rtm;
1388 
1389 			if (rt == NULL)
1390 				continue;
1391 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1392 			info.rti_info[RTAX_DST] = sa = rt_key(rt);
1393 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1394 			if ((m = rt_msg1(cmd, &info)) == NULL)
1395 				continue;
1396 			rtm = mtod(m, struct rt_msghdr *);
1397 			rtm->rtm_index = ifp->if_index;
1398 			rtm->rtm_flags |= rt->rt_flags;
1399 			rtm->rtm_errno = error;
1400 			rtm->rtm_addrs = info.rti_addrs;
1401 		}
1402 		if (fibnum != RTS_ALLFIBS) {
1403 			KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1404 			    "fibnum out of range 0 <= %d < %d", __func__,
1405 			     fibnum, rt_numfibs));
1406 			M_SETFIB(m, fibnum);
1407 			m->m_flags |= RTS_FILTER_FIB;
1408 		}
1409 		rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1410 	}
1411 }
1412 
1413 void
1414 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1415 {
1416 
1417 	rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1418 }
1419 
1420 /*
1421  * This is the analogue to the rt_newaddrmsg which performs the same
1422  * function but for multicast group memberhips.  This is easier since
1423  * there is no route state to worry about.
1424  */
1425 void
1426 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1427 {
1428 	struct rt_addrinfo info;
1429 	struct mbuf *m = NULL;
1430 	struct ifnet *ifp = ifma->ifma_ifp;
1431 	struct ifma_msghdr *ifmam;
1432 
1433 	if (route_cb.any_count == 0)
1434 		return;
1435 
1436 	bzero((caddr_t)&info, sizeof(info));
1437 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1438 	info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1439 	/*
1440 	 * If a link-layer address is present, present it as a ``gateway''
1441 	 * (similarly to how ARP entries, e.g., are presented).
1442 	 */
1443 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1444 	m = rt_msg1(cmd, &info);
1445 	if (m == NULL)
1446 		return;
1447 	ifmam = mtod(m, struct ifma_msghdr *);
1448 	KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1449 	    __func__));
1450 	ifmam->ifmam_index = ifp->if_index;
1451 	ifmam->ifmam_addrs = info.rti_addrs;
1452 	rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1453 }
1454 
1455 static struct mbuf *
1456 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1457 	struct rt_addrinfo *info)
1458 {
1459 	struct if_announcemsghdr *ifan;
1460 	struct mbuf *m;
1461 
1462 	if (route_cb.any_count == 0)
1463 		return NULL;
1464 	bzero((caddr_t)info, sizeof(*info));
1465 	m = rt_msg1(type, info);
1466 	if (m != NULL) {
1467 		ifan = mtod(m, struct if_announcemsghdr *);
1468 		ifan->ifan_index = ifp->if_index;
1469 		strlcpy(ifan->ifan_name, ifp->if_xname,
1470 			sizeof(ifan->ifan_name));
1471 		ifan->ifan_what = what;
1472 	}
1473 	return m;
1474 }
1475 
1476 /*
1477  * This is called to generate routing socket messages indicating
1478  * IEEE80211 wireless events.
1479  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1480  */
1481 void
1482 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1483 {
1484 	struct mbuf *m;
1485 	struct rt_addrinfo info;
1486 
1487 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1488 	if (m != NULL) {
1489 		/*
1490 		 * Append the ieee80211 data.  Try to stick it in the
1491 		 * mbuf containing the ifannounce msg; otherwise allocate
1492 		 * a new mbuf and append.
1493 		 *
1494 		 * NB: we assume m is a single mbuf.
1495 		 */
1496 		if (data_len > M_TRAILINGSPACE(m)) {
1497 			struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1498 			if (n == NULL) {
1499 				m_freem(m);
1500 				return;
1501 			}
1502 			bcopy(data, mtod(n, void *), data_len);
1503 			n->m_len = data_len;
1504 			m->m_next = n;
1505 		} else if (data_len > 0) {
1506 			bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1507 			m->m_len += data_len;
1508 		}
1509 		if (m->m_flags & M_PKTHDR)
1510 			m->m_pkthdr.len += data_len;
1511 		mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1512 		rt_dispatch(m, AF_UNSPEC);
1513 	}
1514 }
1515 
1516 /*
1517  * This is called to generate routing socket messages indicating
1518  * network interface arrival and departure.
1519  */
1520 void
1521 rt_ifannouncemsg(struct ifnet *ifp, int what)
1522 {
1523 	struct mbuf *m;
1524 	struct rt_addrinfo info;
1525 
1526 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1527 	if (m != NULL)
1528 		rt_dispatch(m, AF_UNSPEC);
1529 }
1530 
1531 static void
1532 rt_dispatch(struct mbuf *m, sa_family_t saf)
1533 {
1534 	struct m_tag *tag;
1535 
1536 	/*
1537 	 * Preserve the family from the sockaddr, if any, in an m_tag for
1538 	 * use when injecting the mbuf into the routing socket buffer from
1539 	 * the netisr.
1540 	 */
1541 	if (saf != AF_UNSPEC) {
1542 		tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1543 		    M_NOWAIT);
1544 		if (tag == NULL) {
1545 			m_freem(m);
1546 			return;
1547 		}
1548 		*(unsigned short *)(tag + 1) = saf;
1549 		m_tag_prepend(m, tag);
1550 	}
1551 #ifdef VIMAGE
1552 	if (V_loif)
1553 		m->m_pkthdr.rcvif = V_loif;
1554 	else {
1555 		m_freem(m);
1556 		return;
1557 	}
1558 #endif
1559 	netisr_queue(NETISR_ROUTE, m);	/* mbuf is free'd on failure. */
1560 }
1561 
1562 /*
1563  * This is used in dumping the kernel table via sysctl().
1564  */
1565 static int
1566 sysctl_dumpentry(struct radix_node *rn, void *vw)
1567 {
1568 	struct walkarg *w = vw;
1569 	struct rtentry *rt = (struct rtentry *)rn;
1570 	int error = 0, size;
1571 	struct rt_addrinfo info;
1572 
1573 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1574 		return 0;
1575 	if ((rt->rt_flags & RTF_HOST) == 0
1576 	    ? jailed_without_vnet(w->w_req->td->td_ucred)
1577 	    : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1578 		return (0);
1579 	bzero((caddr_t)&info, sizeof(info));
1580 	info.rti_info[RTAX_DST] = rt_key(rt);
1581 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1582 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1583 	info.rti_info[RTAX_GENMASK] = 0;
1584 	if (rt->rt_ifp) {
1585 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1586 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1587 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1588 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1589 	}
1590 	size = rt_msg2(RTM_GET, &info, NULL, w);
1591 	if (w->w_req && w->w_tmem) {
1592 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1593 
1594 		rtm->rtm_flags = rt->rt_flags;
1595 		/*
1596 		 * let's be honest about this being a retarded hack
1597 		 */
1598 		rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1599 		rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1600 		rtm->rtm_index = rt->rt_ifp->if_index;
1601 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1602 		rtm->rtm_addrs = info.rti_addrs;
1603 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1604 		return (error);
1605 	}
1606 	return (error);
1607 }
1608 
1609 #ifdef COMPAT_FREEBSD32
1610 static void
1611 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1612 {
1613 
1614 	bzero(dst, sizeof(*dst));
1615 	CP(*src, *dst, ifi_type);
1616 	CP(*src, *dst, ifi_physical);
1617 	CP(*src, *dst, ifi_addrlen);
1618 	CP(*src, *dst, ifi_hdrlen);
1619 	CP(*src, *dst, ifi_link_state);
1620 	CP(*src, *dst, ifi_vhid);
1621 	dst->ifi_datalen = sizeof(struct if_data32);
1622 	CP(*src, *dst, ifi_mtu);
1623 	CP(*src, *dst, ifi_metric);
1624 	CP(*src, *dst, ifi_baudrate);
1625 	CP(*src, *dst, ifi_ipackets);
1626 	CP(*src, *dst, ifi_ierrors);
1627 	CP(*src, *dst, ifi_opackets);
1628 	CP(*src, *dst, ifi_oerrors);
1629 	CP(*src, *dst, ifi_collisions);
1630 	CP(*src, *dst, ifi_ibytes);
1631 	CP(*src, *dst, ifi_obytes);
1632 	CP(*src, *dst, ifi_imcasts);
1633 	CP(*src, *dst, ifi_omcasts);
1634 	CP(*src, *dst, ifi_iqdrops);
1635 	CP(*src, *dst, ifi_noproto);
1636 	CP(*src, *dst, ifi_hwassist);
1637 	CP(*src, *dst, ifi_epoch);
1638 	TV_CP(*src, *dst, ifi_lastchange);
1639 }
1640 #endif
1641 
1642 static int
1643 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1644     struct walkarg *w, int len)
1645 {
1646 	struct if_msghdrl *ifm;
1647 
1648 #ifdef COMPAT_FREEBSD32
1649 	if (w->w_req->flags & SCTL_MASK32) {
1650 		struct if_msghdrl32 *ifm32;
1651 
1652 		ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1653 		ifm32->ifm_addrs = info->rti_addrs;
1654 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1655 		ifm32->ifm_index = ifp->if_index;
1656 		ifm32->_ifm_spare1 = 0;
1657 		ifm32->ifm_len = sizeof(*ifm32);
1658 		ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1659 
1660 		copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1661 		/* Fixup if_data carp(4) vhid. */
1662 		if (carp_get_vhid_p != NULL)
1663 			ifm32->ifm_data.ifi_vhid =
1664 			    (*carp_get_vhid_p)(ifp->if_addr);
1665 
1666 		return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1667 	}
1668 #endif
1669 	ifm = (struct if_msghdrl *)w->w_tmem;
1670 	ifm->ifm_addrs = info->rti_addrs;
1671 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1672 	ifm->ifm_index = ifp->if_index;
1673 	ifm->_ifm_spare1 = 0;
1674 	ifm->ifm_len = sizeof(*ifm);
1675 	ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1676 
1677 	ifm->ifm_data = ifp->if_data;
1678 	/* Fixup if_data carp(4) vhid. */
1679 	if (carp_get_vhid_p != NULL)
1680 		ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1681 
1682 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1683 }
1684 
1685 static int
1686 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1687     struct walkarg *w, int len)
1688 {
1689 	struct if_msghdr *ifm;
1690 
1691 #ifdef COMPAT_FREEBSD32
1692 	if (w->w_req->flags & SCTL_MASK32) {
1693 		struct if_msghdr32 *ifm32;
1694 
1695 		ifm32 = (struct if_msghdr32 *)w->w_tmem;
1696 		ifm32->ifm_addrs = info->rti_addrs;
1697 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1698 		ifm32->ifm_index = ifp->if_index;
1699 
1700 		copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1701 		/* Fixup if_data carp(4) vhid. */
1702 		if (carp_get_vhid_p != NULL)
1703 			ifm32->ifm_data.ifi_vhid =
1704 			    (*carp_get_vhid_p)(ifp->if_addr);
1705 
1706 		return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1707 	}
1708 #endif
1709 	ifm = (struct if_msghdr *)w->w_tmem;
1710 	ifm->ifm_addrs = info->rti_addrs;
1711 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1712 	ifm->ifm_index = ifp->if_index;
1713 
1714 	ifm->ifm_data = ifp->if_data;
1715 	/* Fixup if_data carp(4) vhid. */
1716 	if (carp_get_vhid_p != NULL)
1717 		ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1718 
1719 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1720 }
1721 
1722 static int
1723 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1724     struct walkarg *w, int len)
1725 {
1726 	struct ifa_msghdrl *ifam;
1727 
1728 #ifdef COMPAT_FREEBSD32
1729 	if (w->w_req->flags & SCTL_MASK32) {
1730 		struct ifa_msghdrl32 *ifam32;
1731 
1732 		ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1733 		ifam32->ifam_addrs = info->rti_addrs;
1734 		ifam32->ifam_flags = ifa->ifa_flags;
1735 		ifam32->ifam_index = ifa->ifa_ifp->if_index;
1736 		ifam32->_ifam_spare1 = 0;
1737 		ifam32->ifam_len = sizeof(*ifam32);
1738 		ifam32->ifam_data_off =
1739 		    offsetof(struct ifa_msghdrl32, ifam_data);
1740 		ifam32->ifam_metric = ifa->ifa_metric;
1741 
1742 		copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1743 		/* Fixup if_data carp(4) vhid. */
1744 		if (carp_get_vhid_p != NULL)
1745 			ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1746 
1747 		return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1748 	}
1749 #endif
1750 
1751 	ifam = (struct ifa_msghdrl *)w->w_tmem;
1752 	ifam->ifam_addrs = info->rti_addrs;
1753 	ifam->ifam_flags = ifa->ifa_flags;
1754 	ifam->ifam_index = ifa->ifa_ifp->if_index;
1755 	ifam->_ifam_spare1 = 0;
1756 	ifam->ifam_len = sizeof(*ifam);
1757 	ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1758 	ifam->ifam_metric = ifa->ifa_metric;
1759 
1760 	ifam->ifam_data = ifa->if_data;
1761 	/* Fixup if_data carp(4) vhid. */
1762 	if (carp_get_vhid_p != NULL)
1763 		ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1764 
1765 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1766 }
1767 
1768 static int
1769 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1770     struct walkarg *w, int len)
1771 {
1772 	struct ifa_msghdr *ifam;
1773 
1774 	ifam = (struct ifa_msghdr *)w->w_tmem;
1775 	ifam->ifam_addrs = info->rti_addrs;
1776 	ifam->ifam_flags = ifa->ifa_flags;
1777 	ifam->ifam_index = ifa->ifa_ifp->if_index;
1778 	ifam->ifam_metric = ifa->ifa_metric;
1779 
1780 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1781 }
1782 
1783 static int
1784 sysctl_iflist(int af, struct walkarg *w)
1785 {
1786 	struct ifnet *ifp;
1787 	struct ifaddr *ifa;
1788 	struct rt_addrinfo info;
1789 	int len, error = 0;
1790 
1791 	bzero((caddr_t)&info, sizeof(info));
1792 	IFNET_RLOCK_NOSLEEP();
1793 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1794 		if (w->w_arg && w->w_arg != ifp->if_index)
1795 			continue;
1796 		IF_ADDR_RLOCK(ifp);
1797 		ifa = ifp->if_addr;
1798 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1799 		len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1800 		info.rti_info[RTAX_IFP] = NULL;
1801 		if (w->w_req && w->w_tmem) {
1802 			if (w->w_op == NET_RT_IFLISTL)
1803 				error = sysctl_iflist_ifml(ifp, &info, w, len);
1804 			else
1805 				error = sysctl_iflist_ifm(ifp, &info, w, len);
1806 			if (error)
1807 				goto done;
1808 		}
1809 		while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1810 			if (af && af != ifa->ifa_addr->sa_family)
1811 				continue;
1812 			if (prison_if(w->w_req->td->td_ucred,
1813 			    ifa->ifa_addr) != 0)
1814 				continue;
1815 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1816 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1817 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1818 			len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1819 			if (w->w_req && w->w_tmem) {
1820 				if (w->w_op == NET_RT_IFLISTL)
1821 					error = sysctl_iflist_ifaml(ifa, &info,
1822 					    w, len);
1823 				else
1824 					error = sysctl_iflist_ifam(ifa, &info,
1825 					    w, len);
1826 				if (error)
1827 					goto done;
1828 			}
1829 		}
1830 		IF_ADDR_RUNLOCK(ifp);
1831 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1832 			info.rti_info[RTAX_BRD] = NULL;
1833 	}
1834 done:
1835 	if (ifp != NULL)
1836 		IF_ADDR_RUNLOCK(ifp);
1837 	IFNET_RUNLOCK_NOSLEEP();
1838 	return (error);
1839 }
1840 
1841 static int
1842 sysctl_ifmalist(int af, struct walkarg *w)
1843 {
1844 	struct ifnet *ifp;
1845 	struct ifmultiaddr *ifma;
1846 	struct	rt_addrinfo info;
1847 	int	len, error = 0;
1848 	struct ifaddr *ifa;
1849 
1850 	bzero((caddr_t)&info, sizeof(info));
1851 	IFNET_RLOCK_NOSLEEP();
1852 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1853 		if (w->w_arg && w->w_arg != ifp->if_index)
1854 			continue;
1855 		ifa = ifp->if_addr;
1856 		info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1857 		IF_ADDR_RLOCK(ifp);
1858 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1859 			if (af && af != ifma->ifma_addr->sa_family)
1860 				continue;
1861 			if (prison_if(w->w_req->td->td_ucred,
1862 			    ifma->ifma_addr) != 0)
1863 				continue;
1864 			info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1865 			info.rti_info[RTAX_GATEWAY] =
1866 			    (ifma->ifma_addr->sa_family != AF_LINK) ?
1867 			    ifma->ifma_lladdr : NULL;
1868 			len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1869 			if (w->w_req && w->w_tmem) {
1870 				struct ifma_msghdr *ifmam;
1871 
1872 				ifmam = (struct ifma_msghdr *)w->w_tmem;
1873 				ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1874 				ifmam->ifmam_flags = 0;
1875 				ifmam->ifmam_addrs = info.rti_addrs;
1876 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1877 				if (error) {
1878 					IF_ADDR_RUNLOCK(ifp);
1879 					goto done;
1880 				}
1881 			}
1882 		}
1883 		IF_ADDR_RUNLOCK(ifp);
1884 	}
1885 done:
1886 	IFNET_RUNLOCK_NOSLEEP();
1887 	return (error);
1888 }
1889 
1890 static int
1891 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1892 {
1893 	int	*name = (int *)arg1;
1894 	u_int	namelen = arg2;
1895 	struct radix_node_head *rnh = NULL; /* silence compiler. */
1896 	int	i, lim, error = EINVAL;
1897 	u_char	af;
1898 	struct	walkarg w;
1899 
1900 	name ++;
1901 	namelen--;
1902 	if (req->newptr)
1903 		return (EPERM);
1904 	if (namelen != 3)
1905 		return ((namelen < 3) ? EISDIR : ENOTDIR);
1906 	af = name[0];
1907 	if (af > AF_MAX)
1908 		return (EINVAL);
1909 	bzero(&w, sizeof(w));
1910 	w.w_op = name[1];
1911 	w.w_arg = name[2];
1912 	w.w_req = req;
1913 
1914 	error = sysctl_wire_old_buffer(req, 0);
1915 	if (error)
1916 		return (error);
1917 	switch (w.w_op) {
1918 
1919 	case NET_RT_DUMP:
1920 	case NET_RT_FLAGS:
1921 		if (af == 0) {			/* dump all tables */
1922 			i = 1;
1923 			lim = AF_MAX;
1924 		} else				/* dump only one table */
1925 			i = lim = af;
1926 
1927 		/*
1928 		 * take care of llinfo entries, the caller must
1929 		 * specify an AF
1930 		 */
1931 		if (w.w_op == NET_RT_FLAGS &&
1932 		    (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1933 			if (af != 0)
1934 				error = lltable_sysctl_dumparp(af, w.w_req);
1935 			else
1936 				error = EINVAL;
1937 			break;
1938 		}
1939 		/*
1940 		 * take care of routing entries
1941 		 */
1942 		for (error = 0; error == 0 && i <= lim; i++) {
1943 			rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1944 			if (rnh != NULL) {
1945 				RADIX_NODE_HEAD_RLOCK(rnh);
1946 			    	error = rnh->rnh_walktree(rnh,
1947 				    sysctl_dumpentry, &w);
1948 				RADIX_NODE_HEAD_RUNLOCK(rnh);
1949 			} else if (af != 0)
1950 				error = EAFNOSUPPORT;
1951 		}
1952 		break;
1953 
1954 	case NET_RT_IFLIST:
1955 	case NET_RT_IFLISTL:
1956 		error = sysctl_iflist(af, &w);
1957 		break;
1958 
1959 	case NET_RT_IFMALIST:
1960 		error = sysctl_ifmalist(af, &w);
1961 		break;
1962 	}
1963 	if (w.w_tmem)
1964 		free(w.w_tmem, M_RTABLE);
1965 	return (error);
1966 }
1967 
1968 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1969 
1970 /*
1971  * Definitions of protocols supported in the ROUTE domain.
1972  */
1973 
1974 static struct domain routedomain;		/* or at least forward */
1975 
1976 static struct protosw routesw[] = {
1977 {
1978 	.pr_type =		SOCK_RAW,
1979 	.pr_domain =		&routedomain,
1980 	.pr_flags =		PR_ATOMIC|PR_ADDR,
1981 	.pr_output =		route_output,
1982 	.pr_ctlinput =		raw_ctlinput,
1983 	.pr_init =		raw_init,
1984 	.pr_usrreqs =		&route_usrreqs
1985 }
1986 };
1987 
1988 static struct domain routedomain = {
1989 	.dom_family =		PF_ROUTE,
1990 	.dom_name =		 "route",
1991 	.dom_protosw =		routesw,
1992 	.dom_protoswNPROTOSW =	&routesw[sizeof(routesw)/sizeof(routesw[0])]
1993 };
1994 
1995 VNET_DOMAIN_SET(route);
1996