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