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