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