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