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