xref: /netbsd-src/sys/net/rtsock.c (revision 7863ba460b0a05b553c754e5dbc29247dddec322)
1 /*	$NetBSD: rtsock.c,v 1.239 2018/03/19 16:34:48 roy Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1988, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.239 2018/03/19 16:34:48 roy Exp $");
65 
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_mpls.h"
69 #include "opt_compat_netbsd.h"
70 #include "opt_sctp.h"
71 #include "opt_net_mpsafe.h"
72 #endif
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/sysctl.h>
82 #include <sys/kauth.h>
83 #include <sys/kmem.h>
84 #include <sys/intr.h>
85 #include <sys/condvar.h>
86 
87 #include <net/if.h>
88 #include <net/if_llatbl.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <net/raw_cb.h>
92 
93 #include <netinet/in_var.h>
94 #include <netinet/if_inarp.h>
95 
96 #include <netmpls/mpls.h>
97 
98 #ifdef SCTP
99 extern void sctp_add_ip_address(struct ifaddr *);
100 extern void sctp_delete_ip_address(struct ifaddr *);
101 #endif
102 
103 #if defined(COMPAT_14) || defined(COMPAT_50) || defined(COMPAT_70)
104 #include <compat/net/if.h>
105 #include <compat/net/route.h>
106 #endif
107 #ifdef COMPAT_RTSOCK
108 #define	RTM_XVERSION	RTM_OVERSION
109 #define	RTM_XNEWADDR	RTM_ONEWADDR
110 #define	RTM_XDELADDR	RTM_ODELADDR
111 #define	RTM_XCHGADDR	RTM_OCHGADDR
112 #define	RT_XADVANCE(a,b) RT_OADVANCE(a,b)
113 #define	RT_XROUNDUP(n)	RT_OROUNDUP(n)
114 #define	PF_XROUTE	PF_OROUTE
115 #define	rt_xmsghdr	rt_msghdr50
116 #define	if_xmsghdr	if_msghdr	/* if_msghdr50 is for RTM_OIFINFO */
117 #define	ifa_xmsghdr	ifa_msghdr50
118 #define	if_xannouncemsghdr	if_announcemsghdr50
119 #define	COMPATNAME(x)	compat_50_ ## x
120 #define	DOMAINNAME	"oroute"
121 CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
122 DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
123 #undef COMPAT_70
124 #else /* COMPAT_RTSOCK */
125 #define	RTM_XVERSION	RTM_VERSION
126 #define	RTM_XNEWADDR	RTM_NEWADDR
127 #define	RTM_XDELADDR	RTM_DELADDR
128 #define	RTM_XCHGADDR	RTM_CHGADDR
129 #define	RT_XADVANCE(a,b) RT_ADVANCE(a,b)
130 #define	RT_XROUNDUP(n)	RT_ROUNDUP(n)
131 #define	PF_XROUTE	PF_ROUTE
132 #define	rt_xmsghdr	rt_msghdr
133 #define	if_xmsghdr	if_msghdr
134 #define	ifa_xmsghdr	ifa_msghdr
135 #define	if_xannouncemsghdr	if_announcemsghdr
136 #define	COMPATNAME(x)	x
137 #define	DOMAINNAME	"route"
138 CTASSERT(sizeof(struct ifa_xmsghdr) == 32);
139 #ifdef COMPAT_50
140 #define	COMPATCALL(name, args)	compat_50_ ## name args
141 #endif
142 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
143 #undef COMPAT_50
144 #undef COMPAT_14
145 #endif /* COMPAT_RTSOCK */
146 
147 #ifndef COMPATCALL
148 #define	COMPATCALL(name, args)	do { } while (/*CONSTCOND*/ 0)
149 #endif
150 
151 #ifdef RTSOCK_DEBUG
152 #define RT_IN_PRINT(info, b, a) (in_print((b), sizeof(b), \
153     &((const struct sockaddr_in *)(info)->rti_info[(a)])->sin_addr), (b))
154 #endif /* RTSOCK_DEBUG */
155 
156 struct route_info COMPATNAME(route_info) = {
157 	.ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
158 	.ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
159 	.ri_maxqlen = IFQ_MAXLEN,
160 };
161 
162 #define	PRESERVED_RTF	(RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
163 
164 static void COMPATNAME(route_init)(void);
165 static int COMPATNAME(route_output)(struct mbuf *, struct socket *);
166 
167 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
168 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
169     struct rt_addrinfo *);
170 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
171 static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
172 static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
173 static void sysctl_net_route_setup(struct sysctllog **);
174 static int sysctl_dumpentry(struct rtentry *, void *);
175 static int sysctl_iflist(int, struct rt_walkarg *, int);
176 static int sysctl_rtable(SYSCTLFN_PROTO);
177 static void rt_adjustcount(int, int);
178 
179 static const struct protosw COMPATNAME(route_protosw)[];
180 
181 struct routecb {
182 	struct rawcb	rocb_rcb;
183 	unsigned int	rocb_msgfilter;
184 #define	RTMSGFILTER(m)	(1U << (m))
185 };
186 #define sotoroutecb(so)	((struct routecb *)(so)->so_pcb)
187 
188 static struct rawcbhead rt_rawcb;
189 #ifdef NET_MPSAFE
190 static kmutex_t *rt_so_mtx;
191 
192 static bool rt_updating = false;
193 static kcondvar_t rt_update_cv;
194 #endif
195 
196 static void
197 rt_adjustcount(int af, int cnt)
198 {
199 	struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
200 
201 	cb->any_count += cnt;
202 
203 	switch (af) {
204 	case AF_INET:
205 		cb->ip_count += cnt;
206 		return;
207 #ifdef INET6
208 	case AF_INET6:
209 		cb->ip6_count += cnt;
210 		return;
211 #endif
212 	case AF_MPLS:
213 		cb->mpls_count += cnt;
214 		return;
215 	}
216 }
217 
218 static int
219 COMPATNAME(route_filter)(struct mbuf *m, struct sockproto *proto,
220     struct rawcb *rp)
221 {
222 	struct routecb *rop = (struct routecb *)rp;
223 	struct rt_xmsghdr *rtm;
224 
225 	KASSERT(m != NULL);
226 	KASSERT(proto != NULL);
227 	KASSERT(rp != NULL);
228 
229 	/* Wrong family for this socket. */
230 	if (proto->sp_family != PF_ROUTE)
231 		return ENOPROTOOPT;
232 
233 	/* If no filter set, just return. */
234 	if (rop->rocb_msgfilter == 0)
235 		return 0;
236 
237 	/* Ensure we can access rtm_type */
238 	if (m->m_len <
239 	    offsetof(struct rt_xmsghdr, rtm_type) + sizeof(rtm->rtm_type))
240 		return EINVAL;
241 
242 	rtm = mtod(m, struct rt_xmsghdr *);
243 	/* If the rtm type is filtered out, return a positive. */
244 	if (!(rop->rocb_msgfilter & RTMSGFILTER(rtm->rtm_type)))
245 		return EEXIST;
246 
247 	/* Passed the filter. */
248 	return 0;
249 }
250 
251 static void
252 rt_pr_init(void)
253 {
254 
255 	LIST_INIT(&rt_rawcb);
256 }
257 
258 static int
259 COMPATNAME(route_attach)(struct socket *so, int proto)
260 {
261 	struct rawcb *rp;
262 	struct routecb *rop;
263 	int s, error;
264 
265 	KASSERT(sotorawcb(so) == NULL);
266 	rop = kmem_zalloc(sizeof(*rop), KM_SLEEP);
267 	rp = &rop->rocb_rcb;
268 	rp->rcb_len = sizeof(*rop);
269 	so->so_pcb = rp;
270 
271 	s = splsoftnet();
272 
273 #ifdef NET_MPSAFE
274 	KASSERT(so->so_lock == NULL);
275 	mutex_obj_hold(rt_so_mtx);
276 	so->so_lock = rt_so_mtx;
277 	solock(so);
278 #endif
279 
280 	if ((error = raw_attach(so, proto, &rt_rawcb)) == 0) {
281 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
282 		rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
283 		rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
284 		rp->rcb_filter = COMPATNAME(route_filter);
285 	}
286 	splx(s);
287 
288 	if (error) {
289 		kmem_free(rop, sizeof(*rop));
290 		so->so_pcb = NULL;
291 		return error;
292 	}
293 
294 	soisconnected(so);
295 	so->so_options |= SO_USELOOPBACK;
296 	KASSERT(solocked(so));
297 
298 	return error;
299 }
300 
301 static void
302 COMPATNAME(route_detach)(struct socket *so)
303 {
304 	struct rawcb *rp = sotorawcb(so);
305 	int s;
306 
307 	KASSERT(rp != NULL);
308 	KASSERT(solocked(so));
309 
310 	s = splsoftnet();
311 	rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
312 	raw_detach(so);
313 	splx(s);
314 }
315 
316 static int
317 COMPATNAME(route_accept)(struct socket *so, struct sockaddr *nam)
318 {
319 	KASSERT(solocked(so));
320 
321 	panic("route_accept");
322 
323 	return EOPNOTSUPP;
324 }
325 
326 static int
327 COMPATNAME(route_bind)(struct socket *so, struct sockaddr *nam, struct lwp *l)
328 {
329 	KASSERT(solocked(so));
330 
331 	return EOPNOTSUPP;
332 }
333 
334 static int
335 COMPATNAME(route_listen)(struct socket *so, struct lwp *l)
336 {
337 	KASSERT(solocked(so));
338 
339 	return EOPNOTSUPP;
340 }
341 
342 static int
343 COMPATNAME(route_connect)(struct socket *so, struct sockaddr *nam, struct lwp *l)
344 {
345 	KASSERT(solocked(so));
346 
347 	return EOPNOTSUPP;
348 }
349 
350 static int
351 COMPATNAME(route_connect2)(struct socket *so, struct socket *so2)
352 {
353 	KASSERT(solocked(so));
354 
355 	return EOPNOTSUPP;
356 }
357 
358 static int
359 COMPATNAME(route_disconnect)(struct socket *so)
360 {
361 	struct rawcb *rp = sotorawcb(so);
362 	int s;
363 
364 	KASSERT(solocked(so));
365 	KASSERT(rp != NULL);
366 
367 	s = splsoftnet();
368 	soisdisconnected(so);
369 	raw_disconnect(rp);
370 	splx(s);
371 
372 	return 0;
373 }
374 
375 static int
376 COMPATNAME(route_shutdown)(struct socket *so)
377 {
378 	int s;
379 
380 	KASSERT(solocked(so));
381 
382 	/*
383 	 * Mark the connection as being incapable of further input.
384 	 */
385 	s = splsoftnet();
386 	socantsendmore(so);
387 	splx(s);
388 	return 0;
389 }
390 
391 static int
392 COMPATNAME(route_abort)(struct socket *so)
393 {
394 	KASSERT(solocked(so));
395 
396 	panic("route_abort");
397 
398 	return EOPNOTSUPP;
399 }
400 
401 static int
402 COMPATNAME(route_ioctl)(struct socket *so, u_long cmd, void *nam,
403     struct ifnet * ifp)
404 {
405 	return EOPNOTSUPP;
406 }
407 
408 static int
409 COMPATNAME(route_stat)(struct socket *so, struct stat *ub)
410 {
411 	KASSERT(solocked(so));
412 
413 	return 0;
414 }
415 
416 static int
417 COMPATNAME(route_peeraddr)(struct socket *so, struct sockaddr *nam)
418 {
419 	struct rawcb *rp = sotorawcb(so);
420 
421 	KASSERT(solocked(so));
422 	KASSERT(rp != NULL);
423 	KASSERT(nam != NULL);
424 
425 	if (rp->rcb_faddr == NULL)
426 		return ENOTCONN;
427 
428 	raw_setpeeraddr(rp, nam);
429 	return 0;
430 }
431 
432 static int
433 COMPATNAME(route_sockaddr)(struct socket *so, struct sockaddr *nam)
434 {
435 	struct rawcb *rp = sotorawcb(so);
436 
437 	KASSERT(solocked(so));
438 	KASSERT(rp != NULL);
439 	KASSERT(nam != NULL);
440 
441 	if (rp->rcb_faddr == NULL)
442 		return ENOTCONN;
443 
444 	raw_setsockaddr(rp, nam);
445 	return 0;
446 }
447 
448 static int
449 COMPATNAME(route_rcvd)(struct socket *so, int flags, struct lwp *l)
450 {
451 	KASSERT(solocked(so));
452 
453 	return EOPNOTSUPP;
454 }
455 
456 static int
457 COMPATNAME(route_recvoob)(struct socket *so, struct mbuf *m, int flags)
458 {
459 	KASSERT(solocked(so));
460 
461 	return EOPNOTSUPP;
462 }
463 
464 static int
465 COMPATNAME(route_send)(struct socket *so, struct mbuf *m,
466     struct sockaddr *nam, struct mbuf *control, struct lwp *l)
467 {
468 	int error = 0;
469 	int s;
470 
471 	KASSERT(solocked(so));
472 	KASSERT(so->so_proto == &COMPATNAME(route_protosw)[0]);
473 
474 	s = splsoftnet();
475 	error = raw_send(so, m, nam, control, l, &COMPATNAME(route_output));
476 	splx(s);
477 
478 	return error;
479 }
480 
481 static int
482 COMPATNAME(route_sendoob)(struct socket *so, struct mbuf *m,
483     struct mbuf *control)
484 {
485 	KASSERT(solocked(so));
486 
487 	m_freem(m);
488 	m_freem(control);
489 
490 	return EOPNOTSUPP;
491 }
492 static int
493 COMPATNAME(route_purgeif)(struct socket *so, struct ifnet *ifp)
494 {
495 
496 	panic("route_purgeif");
497 
498 	return EOPNOTSUPP;
499 }
500 
501 #if defined(INET) || defined(INET6)
502 static int
503 route_get_sdl_index(struct rt_addrinfo *info, int *sdl_index)
504 {
505 	struct rtentry *nrt;
506 	int error;
507 
508 	error = rtrequest1(RTM_GET, info, &nrt);
509 	if (error != 0)
510 		return error;
511 	/*
512 	 * nrt->rt_ifp->if_index may not be correct
513 	 * due to changing to ifplo0.
514 	 */
515 	*sdl_index = satosdl(nrt->rt_gateway)->sdl_index;
516 	rt_unref(nrt);
517 
518 	return 0;
519 }
520 #endif
521 
522 static void
523 route_get_sdl(const struct ifnet *ifp, const struct sockaddr *dst,
524     struct sockaddr_dl *sdl, int *flags)
525 {
526 	struct llentry *la;
527 
528 	KASSERT(ifp != NULL);
529 
530 	IF_AFDATA_RLOCK(ifp);
531 	switch (dst->sa_family) {
532 	case AF_INET:
533 		la = lla_lookup(LLTABLE(ifp), 0, dst);
534 		break;
535 	case AF_INET6:
536 		la = lla_lookup(LLTABLE6(ifp), 0, dst);
537 		break;
538 	default:
539 		la = NULL;
540 		KASSERTMSG(0, "Invalid AF=%d\n", dst->sa_family);
541 		break;
542 	}
543 	IF_AFDATA_RUNLOCK(ifp);
544 
545 	void *a = (LLE_IS_VALID(la) && (la->la_flags & LLE_VALID) == LLE_VALID)
546 	    ? &la->ll_addr : NULL;
547 
548 	a = sockaddr_dl_init(sdl, sizeof(*sdl), ifp->if_index, ifp->if_type,
549 	    NULL, 0, a, ifp->if_addrlen);
550 	KASSERT(a != NULL);
551 
552 	if (la != NULL) {
553 		*flags = la->la_flags;
554 		LLE_RUNLOCK(la);
555 	}
556 }
557 
558 static int
559 route_output_report(struct rtentry *rt, struct rt_addrinfo *info,
560     struct rt_xmsghdr *rtm, struct rt_xmsghdr **new_rtm)
561 {
562 	int len;
563 
564 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
565 		const struct ifaddr *rtifa;
566 		const struct ifnet *ifp = rt->rt_ifp;
567 
568 		info->rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
569 		/* rtifa used to be simply rt->rt_ifa.
570 		 * If rt->rt_ifa != NULL, then
571 		 * rt_get_ifa() != NULL.  So this
572 		 * ought to still be safe. --dyoung
573 		 */
574 		rtifa = rt_get_ifa(rt);
575 		info->rti_info[RTAX_IFA] = rtifa->ifa_addr;
576 #ifdef RTSOCK_DEBUG
577 		if (info->rti_info[RTAX_IFA]->sa_family == AF_INET) {
578 			char ibuf[INET_ADDRSTRLEN];
579 			char abuf[INET_ADDRSTRLEN];
580 			printf("%s: copying out RTAX_IFA %s "
581 			    "for info->rti_info[RTAX_DST] %s "
582 			    "ifa_getifa %p ifa_seqno %p\n",
583 			    __func__,
584 			    RT_IN_PRINT(info, ibuf, RTAX_IFA),
585 			    RT_IN_PRINT(info, abuf, RTAX_DST),
586 			    (void *)rtifa->ifa_getifa,
587 			    rtifa->ifa_seqno);
588 		}
589 #endif /* RTSOCK_DEBUG */
590 		if (ifp->if_flags & IFF_POINTOPOINT)
591 			info->rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
592 		else
593 			info->rti_info[RTAX_BRD] = NULL;
594 		rtm->rtm_index = ifp->if_index;
595 	}
596 	(void)rt_msg2(rtm->rtm_type, info, NULL, NULL, &len);
597 	if (len > rtm->rtm_msglen) {
598 		struct rt_xmsghdr *old_rtm = rtm;
599 		R_Malloc(*new_rtm, struct rt_xmsghdr *, len);
600 		if (*new_rtm == NULL)
601 			return ENOBUFS;
602 		(void)memcpy(*new_rtm, old_rtm, old_rtm->rtm_msglen);
603 		rtm = *new_rtm;
604 	}
605 	(void)rt_msg2(rtm->rtm_type, info, rtm, NULL, 0);
606 	rtm->rtm_flags = rt->rt_flags;
607 	rtm_setmetrics(rt, rtm);
608 	rtm->rtm_addrs = info->rti_addrs;
609 
610 	return 0;
611 }
612 
613 static struct ifaddr *
614 route_output_get_ifa(const struct rt_addrinfo info, const struct rtentry *rt,
615     struct ifnet **ifp, struct psref *psref_ifp, struct psref *psref)
616 {
617 	struct ifaddr *ifa = NULL;
618 
619 	*ifp = NULL;
620 	if (info.rti_info[RTAX_IFP] != NULL) {
621 		ifa = ifa_ifwithnet_psref(info.rti_info[RTAX_IFP], psref);
622 		if (ifa == NULL)
623 			goto next;
624 		*ifp = ifa->ifa_ifp;
625 		if_acquire(*ifp, psref_ifp);
626 		if (info.rti_info[RTAX_IFA] == NULL &&
627 		    info.rti_info[RTAX_GATEWAY] == NULL)
628 			goto next;
629 		ifa_release(ifa, psref);
630 		if (info.rti_info[RTAX_IFA] == NULL) {
631 			/* route change <dst> <gw> -ifp <if> */
632 			ifa = ifaof_ifpforaddr_psref(info.rti_info[RTAX_GATEWAY],
633 			    *ifp, psref);
634 		} else {
635 			/* route change <dst> -ifp <if> -ifa <addr> */
636 			ifa = ifa_ifwithaddr_psref(info.rti_info[RTAX_IFA], psref);
637 			if (ifa != NULL)
638 				goto out;
639 			ifa = ifaof_ifpforaddr_psref(info.rti_info[RTAX_IFA],
640 			    *ifp, psref);
641 		}
642 		goto out;
643 	}
644 next:
645 	if (info.rti_info[RTAX_IFA] != NULL) {
646 		/* route change <dst> <gw> -ifa <addr> */
647 		ifa = ifa_ifwithaddr_psref(info.rti_info[RTAX_IFA], psref);
648 		if (ifa != NULL)
649 			goto out;
650 	}
651 	if (info.rti_info[RTAX_GATEWAY] != NULL) {
652 		/* route change <dst> <gw> */
653 		ifa = ifa_ifwithroute_psref(rt->rt_flags, rt_getkey(rt),
654 		    info.rti_info[RTAX_GATEWAY], psref);
655 	}
656 out:
657 	if (ifa != NULL && *ifp == NULL) {
658 		*ifp = ifa->ifa_ifp;
659 		if_acquire(*ifp, psref_ifp);
660 	}
661 	if (ifa == NULL && *ifp != NULL) {
662 		if_put(*ifp, psref_ifp);
663 		*ifp = NULL;
664 	}
665 	return ifa;
666 }
667 
668 static int
669 route_output_change(struct rtentry *rt, struct rt_addrinfo *info,
670     struct rt_xmsghdr *rtm)
671 {
672 	int error = 0;
673 	struct ifnet *ifp = NULL, *new_ifp = NULL;
674 	struct ifaddr *ifa = NULL, *new_ifa;
675 	struct psref psref_ifa, psref_new_ifa, psref_ifp, psref_new_ifp;
676 	bool newgw, ifp_changed = false;
677 
678 	/*
679 	 * New gateway could require new ifaddr, ifp;
680 	 * flags may also be different; ifp may be specified
681 	 * by ll sockaddr when protocol address is ambiguous
682 	 */
683 	newgw = info->rti_info[RTAX_GATEWAY] != NULL &&
684 	    sockaddr_cmp(info->rti_info[RTAX_GATEWAY], rt->rt_gateway) != 0;
685 
686 	if (newgw || info->rti_info[RTAX_IFP] != NULL ||
687 	    info->rti_info[RTAX_IFA] != NULL) {
688 		ifp = rt_getifp(info, &psref_ifp);
689 		/* info refers ifp so we need to keep a reference */
690 		ifa = rt_getifa(info, &psref_ifa);
691 		if (ifa == NULL) {
692 			error = ENETUNREACH;
693 			goto out;
694 		}
695 	}
696 	if (newgw) {
697 		error = rt_setgate(rt, info->rti_info[RTAX_GATEWAY]);
698 		if (error != 0)
699 			goto out;
700 	}
701 	if (info->rti_info[RTAX_TAG]) {
702 		const struct sockaddr *tag;
703 		tag = rt_settag(rt, info->rti_info[RTAX_TAG]);
704 		if (tag == NULL) {
705 			error = ENOBUFS;
706 			goto out;
707 		}
708 	}
709 	/*
710 	 * New gateway could require new ifaddr, ifp;
711 	 * flags may also be different; ifp may be specified
712 	 * by ll sockaddr when protocol address is ambiguous
713 	 */
714 	new_ifa = route_output_get_ifa(*info, rt, &new_ifp, &psref_new_ifp,
715 	    &psref_new_ifa);
716 	if (new_ifa != NULL) {
717 		ifa_release(ifa, &psref_ifa);
718 		ifa = new_ifa;
719 	}
720 	if (ifa) {
721 		struct ifaddr *oifa = rt->rt_ifa;
722 		if (oifa != ifa && !ifa_is_destroying(ifa) &&
723 		    new_ifp != NULL && !if_is_deactivated(new_ifp)) {
724 			if (oifa && oifa->ifa_rtrequest)
725 				oifa->ifa_rtrequest(RTM_DELETE, rt, info);
726 			rt_replace_ifa(rt, ifa);
727 			rt->rt_ifp = new_ifp;
728 			ifp_changed = true;
729 		}
730 		if (new_ifa == NULL)
731 			ifa_release(ifa, &psref_ifa);
732 	}
733 	ifa_release(new_ifa, &psref_new_ifa);
734 	if (new_ifp && rt->rt_ifp != new_ifp && !if_is_deactivated(new_ifp)) {
735 		rt->rt_ifp = new_ifp;
736 		ifp_changed = true;
737 	}
738 	rt_setmetrics(rtm->rtm_inits, rtm, rt);
739 	if (rt->rt_flags != info->rti_flags) {
740 		rt->rt_flags = (info->rti_flags & ~PRESERVED_RTF) |
741 		    (rt->rt_flags & PRESERVED_RTF);
742 	}
743 	if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
744 		rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
745 #if defined(INET) || defined(INET6)
746 	if (ifp_changed && rt_mask(rt) != NULL)
747 		lltable_prefix_free(rt_getkey(rt)->sa_family, rt_getkey(rt),
748 		    rt_mask(rt), 0);
749 #else
750 	(void)ifp_changed; /* XXX gcc */
751 #endif
752 out:
753 	if_put(new_ifp, &psref_new_ifp);
754 	if_put(ifp, &psref_ifp);
755 
756 	return error;
757 }
758 
759 /*ARGSUSED*/
760 int
761 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
762 {
763 	struct sockproto proto = { .sp_family = PF_XROUTE, };
764 	struct rt_xmsghdr *rtm = NULL;
765 	struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
766 	struct rtentry *rt = NULL;
767 	struct rtentry *saved_nrt = NULL;
768 	struct rt_addrinfo info;
769 	int len, error = 0;
770 	sa_family_t family;
771 	struct sockaddr_dl sdl;
772 	int bound = curlwp_bind();
773 	bool do_rt_free = false;
774 	struct sockaddr_storage netmask;
775 
776 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
777 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
778 	   (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
779 		error = ENOBUFS;
780 		goto out;
781 	}
782 	if ((m->m_flags & M_PKTHDR) == 0)
783 		panic("%s", __func__);
784 	len = m->m_pkthdr.len;
785 	if (len < sizeof(*rtm) ||
786 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
787 		info.rti_info[RTAX_DST] = NULL;
788 		senderr(EINVAL);
789 	}
790 	R_Malloc(rtm, struct rt_xmsghdr *, len);
791 	if (rtm == NULL) {
792 		info.rti_info[RTAX_DST] = NULL;
793 		senderr(ENOBUFS);
794 	}
795 	m_copydata(m, 0, len, rtm);
796 	if (rtm->rtm_version != RTM_XVERSION) {
797 		info.rti_info[RTAX_DST] = NULL;
798 		senderr(EPROTONOSUPPORT);
799 	}
800 	rtm->rtm_pid = curproc->p_pid;
801 	memset(&info, 0, sizeof(info));
802 	info.rti_addrs = rtm->rtm_addrs;
803 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
804 	    &info)) {
805 		senderr(EINVAL);
806 	}
807 	info.rti_flags = rtm->rtm_flags;
808 #ifdef RTSOCK_DEBUG
809 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
810 		char abuf[INET_ADDRSTRLEN];
811 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
812 		    RT_IN_PRINT(&info, abuf, RTAX_DST));
813 	}
814 #endif /* RTSOCK_DEBUG */
815 	if (info.rti_info[RTAX_DST] == NULL ||
816 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
817 		senderr(EINVAL);
818 	}
819 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
820 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
821 		senderr(EINVAL);
822 	}
823 
824 	/*
825 	 * Verify that the caller has the appropriate privilege; RTM_GET
826 	 * is the only operation the non-superuser is allowed.
827 	 */
828 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
829 	    0, rtm, NULL, NULL) != 0)
830 		senderr(EACCES);
831 
832 	/*
833 	 * route(8) passes a sockaddr truncated with prefixlen.
834 	 * The kernel doesn't expect such sockaddr and need to
835 	 * use a buffer that is big enough for the sockaddr expected
836 	 * (padded with 0's). We keep the original length of the sockaddr.
837 	 */
838 	if (info.rti_info[RTAX_NETMASK]) {
839 		/*
840 		 * Use the family of RTAX_DST, because RTAX_NETMASK
841 		 * can have a zero family if it comes from the radix
842 		 * tree via rt_mask().
843 		 */
844 		socklen_t sa_len = sockaddr_getsize_by_family(
845 		    info.rti_info[RTAX_DST]->sa_family);
846 		socklen_t masklen = sockaddr_getlen(
847 		    info.rti_info[RTAX_NETMASK]);
848 		if (sa_len != 0 && sa_len > masklen) {
849 			KASSERT(sa_len <= sizeof(netmask));
850 			memcpy(&netmask, info.rti_info[RTAX_NETMASK], masklen);
851 			memset((char *)&netmask + masklen, 0, sa_len - masklen);
852 			info.rti_info[RTAX_NETMASK] = sstocsa(&netmask);
853 		}
854 	}
855 
856 	switch (rtm->rtm_type) {
857 
858 	case RTM_ADD:
859 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
860 			senderr(EINVAL);
861 		}
862 #if defined(INET) || defined(INET6)
863 		/* support for new ARP/NDP code with keeping backcompat */
864 		if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
865 			const struct sockaddr_dl *sdlp =
866 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
867 
868 			/* Allow routing requests by interface index */
869 			if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
870 			    && sdlp->sdl_slen == 0)
871 				goto fallback;
872 			/*
873 			 * Old arp binaries don't set the sdl_index
874 			 * so we have to complement it.
875 			 */
876 			int sdl_index = sdlp->sdl_index;
877 			if (sdl_index == 0) {
878 				error = route_get_sdl_index(&info, &sdl_index);
879 				if (error != 0)
880 					goto fallback;
881 			} else if (
882 			    info.rti_info[RTAX_DST]->sa_family == AF_INET) {
883 				/*
884 				 * XXX workaround for SIN_PROXY case; proxy arp
885 				 * entry should be in an interface that has
886 				 * a network route including the destination,
887 				 * not a local (link) route that may not be a
888 				 * desired place, for example a tap.
889 				 */
890 				const struct sockaddr_inarp *sina =
891 				    (const struct sockaddr_inarp *)
892 				    info.rti_info[RTAX_DST];
893 				if (sina->sin_other & SIN_PROXY) {
894 					error = route_get_sdl_index(&info,
895 					    &sdl_index);
896 					if (error != 0)
897 						goto fallback;
898 				}
899 			}
900 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
901 			    rtm->rtm_rmx.rmx_expire, &info, sdl_index);
902 			break;
903 		}
904 	fallback:
905 #endif /* defined(INET) || defined(INET6) */
906 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
907 		if (error == 0) {
908 			rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
909 			rt_unref(saved_nrt);
910 		}
911 		break;
912 
913 	case RTM_DELETE:
914 #if defined(INET) || defined(INET6)
915 		/* support for new ARP/NDP code */
916 		if (info.rti_info[RTAX_GATEWAY] &&
917 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
918 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
919 			const struct sockaddr_dl *sdlp =
920 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
921 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
922 			    rtm->rtm_rmx.rmx_expire, &info, sdlp->sdl_index);
923 			rtm->rtm_flags &= ~RTF_UP;
924 			break;
925 		}
926 #endif
927 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
928 		if (error != 0)
929 			break;
930 
931 		rt = saved_nrt;
932 		do_rt_free = true;
933 		info.rti_info[RTAX_DST] = rt_getkey(rt);
934 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
935 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
936 		info.rti_info[RTAX_TAG] = rt_gettag(rt);
937 		error = route_output_report(rt, &info, rtm, &new_rtm);
938 		if (error)
939 			senderr(error);
940 		if (new_rtm != NULL) {
941 			old_rtm = rtm;
942 			rtm = new_rtm;
943 		}
944 		break;
945 
946 	case RTM_GET:
947 	case RTM_CHANGE:
948 	case RTM_LOCK:
949                 /* XXX This will mask info.rti_info[RTAX_DST] with
950 		 * info.rti_info[RTAX_NETMASK] before
951                  * searching.  It did not used to do that.  --dyoung
952 		 */
953 		rt = NULL;
954 		error = rtrequest1(RTM_GET, &info, &rt);
955 		if (error != 0)
956 			senderr(error);
957 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
958 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
959 			    info.rti_info[RTAX_DST]->sa_len) != 0)
960 				senderr(ESRCH);
961 			if (info.rti_info[RTAX_NETMASK] == NULL &&
962 			    rt_mask(rt) != NULL)
963 				senderr(ETOOMANYREFS);
964 		}
965 
966 		/*
967 		 * XXX if arp/ndp requests an L2 entry, we have to obtain
968 		 * it from lltable while for the route command we have to
969 		 * return a route as it is. How to distinguish them?
970 		 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
971 		 * indicates an L2 entry is requested. For old arp/ndp
972 		 * binaries, we check RTF_UP flag is NOT set; it works
973 		 * by the fact that arp/ndp don't set it while the route
974 		 * command sets it.
975 		 */
976 		if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
977 		     (rtm->rtm_flags & RTF_UP) == 0) &&
978 		    rtm->rtm_type == RTM_GET &&
979 		    sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
980 			int ll_flags = 0;
981 			route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
982 			    &ll_flags);
983 			info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
984 			error = route_output_report(rt, &info, rtm, &new_rtm);
985 			if (error)
986 				senderr(error);
987 			if (new_rtm != NULL) {
988 				old_rtm = rtm;
989 				rtm = new_rtm;
990 			}
991 			rtm->rtm_flags |= RTF_LLDATA;
992 			rtm->rtm_flags &= ~RTF_CONNECTED;
993 			rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
994 			break;
995 		}
996 
997 		switch (rtm->rtm_type) {
998 		case RTM_GET:
999 			info.rti_info[RTAX_DST] = rt_getkey(rt);
1000 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1001 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1002 			info.rti_info[RTAX_TAG] = rt_gettag(rt);
1003 			error = route_output_report(rt, &info, rtm, &new_rtm);
1004 			if (error)
1005 				senderr(error);
1006 			if (new_rtm != NULL) {
1007 				old_rtm = rtm;
1008 				rtm = new_rtm;
1009 			}
1010 			break;
1011 
1012 		case RTM_CHANGE:
1013 #ifdef NET_MPSAFE
1014 			/*
1015 			 * Release rt_so_mtx to avoid a deadlock with route_intr
1016 			 * and also serialize updating routes to avoid another.
1017 			 */
1018 			if (rt_updating) {
1019 				/* Release to allow the updater to proceed */
1020 				rt_unref(rt);
1021 				rt = NULL;
1022 			}
1023 			while (rt_updating) {
1024 				error = cv_wait_sig(&rt_update_cv, rt_so_mtx);
1025 				if (error != 0)
1026 					goto flush;
1027 			}
1028 			if (rt == NULL) {
1029 				error = rtrequest1(RTM_GET, &info, &rt);
1030 				if (error != 0)
1031 					goto flush;
1032 			}
1033 			rt_updating = true;
1034 			mutex_exit(rt_so_mtx);
1035 
1036 			error = rt_update_prepare(rt);
1037 			if (error == 0) {
1038 				error = route_output_change(rt, &info, rtm);
1039 				rt_update_finish(rt);
1040 			}
1041 
1042 			mutex_enter(rt_so_mtx);
1043 			rt_updating = false;
1044 			cv_broadcast(&rt_update_cv);
1045 #else
1046 			error = route_output_change(rt, &info, rtm);
1047 #endif
1048 			if (error != 0)
1049 				goto flush;
1050 			/*FALLTHROUGH*/
1051 		case RTM_LOCK:
1052 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
1053 			rt->rt_rmx.rmx_locks |=
1054 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
1055 			break;
1056 		}
1057 		break;
1058 
1059 	default:
1060 		senderr(EOPNOTSUPP);
1061 	}
1062 
1063 flush:
1064 	if (rtm) {
1065 		if (error)
1066 			rtm->rtm_errno = error;
1067 		else
1068 			rtm->rtm_flags |= RTF_DONE;
1069 	}
1070 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
1071 	    0;
1072 	/* We cannot free old_rtm until we have stopped using the
1073 	 * pointers in info, some of which may point to sockaddrs
1074 	 * in old_rtm.
1075 	 */
1076 	if (old_rtm != NULL)
1077 		Free(old_rtm);
1078 	if (rt) {
1079 		if (do_rt_free)
1080 			rt_free(rt);
1081 		else
1082 			rt_unref(rt);
1083 	}
1084     {
1085 	struct rawcb *rp = NULL;
1086 	/*
1087 	 * Check to see if we don't want our own messages.
1088 	 */
1089 	if ((so->so_options & SO_USELOOPBACK) == 0) {
1090 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
1091 			if (rtm)
1092 				Free(rtm);
1093 			m_freem(m);
1094 			goto out;
1095 		}
1096 		/* There is another listener, so construct message */
1097 		rp = sotorawcb(so);
1098 	}
1099 	if (rtm) {
1100 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
1101 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
1102 			m_freem(m);
1103 			m = NULL;
1104 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
1105 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1106 		Free(rtm);
1107 	}
1108 	if (rp)
1109 		rp->rcb_proto.sp_family = 0; /* Avoid us */
1110 	if (family)
1111 		proto.sp_protocol = family;
1112 	if (m)
1113 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
1114 		    &COMPATNAME(route_info).ri_dst, &rt_rawcb);
1115 	if (rp)
1116 		rp->rcb_proto.sp_family = PF_XROUTE;
1117     }
1118 out:
1119 	curlwp_bindx(bound);
1120 	return error;
1121 }
1122 
1123 static int
1124 route_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1125 {
1126 	struct routecb *rop = sotoroutecb(so);
1127 	int error = 0;
1128 	unsigned char *rtm_type;
1129 	size_t len;
1130 	unsigned int msgfilter;
1131 
1132 	KASSERT(solocked(so));
1133 
1134 	if (sopt->sopt_level != AF_ROUTE) {
1135 		error = ENOPROTOOPT;
1136 	} else switch (op) {
1137 	case PRCO_SETOPT:
1138 		switch (sopt->sopt_name) {
1139 		case RO_MSGFILTER:
1140 			msgfilter = 0;
1141 			for (rtm_type = sopt->sopt_data, len = sopt->sopt_size;
1142 			     len != 0;
1143 			     rtm_type++, len -= sizeof(*rtm_type))
1144 			{
1145 				/* Guard against overflowing our storage. */
1146 				if (*rtm_type >= sizeof(msgfilter) * CHAR_BIT) {
1147 					error = EOVERFLOW;
1148 					break;
1149 				}
1150 				msgfilter |= RTMSGFILTER(*rtm_type);
1151 			}
1152 			if (error == 0)
1153 				rop->rocb_msgfilter = msgfilter;
1154 			break;
1155 		default:
1156 			error = ENOPROTOOPT;
1157 			break;
1158 		}
1159 		break;
1160 	case PRCO_GETOPT:
1161 		switch (sopt->sopt_name) {
1162 		case RO_MSGFILTER:
1163 			error = ENOTSUP;
1164 			break;
1165 		default:
1166 			error = ENOPROTOOPT;
1167 			break;
1168 		}
1169 	}
1170 	return error;
1171 }
1172 
1173 static void
1174 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
1175 {
1176 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
1177 	metric(RTV_RPIPE, rmx_recvpipe);
1178 	metric(RTV_SPIPE, rmx_sendpipe);
1179 	metric(RTV_SSTHRESH, rmx_ssthresh);
1180 	metric(RTV_RTT, rmx_rtt);
1181 	metric(RTV_RTTVAR, rmx_rttvar);
1182 	metric(RTV_HOPCOUNT, rmx_hopcount);
1183 	metric(RTV_MTU, rmx_mtu);
1184 #undef metric
1185 	if (which & RTV_EXPIRE) {
1186 		out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
1187 		    time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
1188 	}
1189 }
1190 
1191 static void
1192 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
1193 {
1194 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
1195 	metric(rmx_recvpipe);
1196 	metric(rmx_sendpipe);
1197 	metric(rmx_ssthresh);
1198 	metric(rmx_rtt);
1199 	metric(rmx_rttvar);
1200 	metric(rmx_hopcount);
1201 	metric(rmx_mtu);
1202 	metric(rmx_locks);
1203 #undef metric
1204 	out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
1205 	    time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
1206 }
1207 
1208 static int
1209 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
1210     struct rt_addrinfo *rtinfo)
1211 {
1212 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
1213 	int i;
1214 
1215 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1216 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
1217 			continue;
1218 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
1219 		RT_XADVANCE(cp, sa);
1220 	}
1221 
1222 	/*
1223 	 * Check for extra addresses specified, except RTM_GET asking
1224 	 * for interface info.
1225 	 */
1226 	if (rtmtype == RTM_GET) {
1227 		if (((rtinfo->rti_addrs &
1228 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
1229 			return 1;
1230 	} else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
1231 		return 1;
1232 	/* Check for bad data length.  */
1233 	if (cp != cplim) {
1234 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
1235 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
1236 			/*
1237 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
1238 			 * We accept this for now for the sake of old
1239 			 * binaries or third party softwares.
1240 			 */
1241 			;
1242 		else
1243 			return 1;
1244 	}
1245 	return 0;
1246 }
1247 
1248 static int
1249 rt_getlen(int type)
1250 {
1251 #ifndef COMPAT_RTSOCK
1252 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
1253 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
1254 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
1255 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
1256 #endif
1257 
1258 	switch (type) {
1259 	case RTM_ODELADDR:
1260 	case RTM_ONEWADDR:
1261 	case RTM_OCHGADDR:
1262 #ifdef COMPAT_70
1263 		return sizeof(struct ifa_msghdr70);
1264 #else
1265 #ifdef RTSOCK_DEBUG
1266 		printf("%s: unsupported RTM type %d\n", __func__, type);
1267 #endif
1268 		return -1;
1269 #endif
1270 	case RTM_DELADDR:
1271 	case RTM_NEWADDR:
1272 	case RTM_CHGADDR:
1273 		return sizeof(struct ifa_xmsghdr);
1274 
1275 	case RTM_OOIFINFO:
1276 #ifdef COMPAT_14
1277 		return sizeof(struct if_msghdr14);
1278 #else
1279 #ifdef RTSOCK_DEBUG
1280 		printf("%s: unsupported RTM type RTM_OOIFINFO\n", __func__);
1281 #endif
1282 		return -1;
1283 #endif
1284 	case RTM_OIFINFO:
1285 #ifdef COMPAT_50
1286 		return sizeof(struct if_msghdr50);
1287 #else
1288 #ifdef RTSOCK_DEBUG
1289 		printf("%s: unsupported RTM type RTM_OIFINFO\n", __func__);
1290 #endif
1291 		return -1;
1292 #endif
1293 
1294 	case RTM_IFINFO:
1295 		return sizeof(struct if_xmsghdr);
1296 
1297 	case RTM_IFANNOUNCE:
1298 	case RTM_IEEE80211:
1299 		return sizeof(struct if_xannouncemsghdr);
1300 
1301 	default:
1302 		return sizeof(struct rt_xmsghdr);
1303 	}
1304 }
1305 
1306 
1307 struct mbuf *
1308 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
1309 {
1310 	struct rt_xmsghdr *rtm;
1311 	struct mbuf *m;
1312 	int i;
1313 	const struct sockaddr *sa;
1314 	int len, dlen;
1315 
1316 	m = m_gethdr(M_DONTWAIT, MT_DATA);
1317 	if (m == NULL)
1318 		return m;
1319 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
1320 
1321 	if ((len = rt_getlen(type)) == -1)
1322 		goto out;
1323 	if (len > MHLEN + MLEN)
1324 		panic("%s: message too long", __func__);
1325 	else if (len > MHLEN) {
1326 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
1327 		if (m->m_next == NULL)
1328 			goto out;
1329 		MCLAIM(m->m_next, m->m_owner);
1330 		m->m_pkthdr.len = len;
1331 		m->m_len = MHLEN;
1332 		m->m_next->m_len = len - MHLEN;
1333 	} else {
1334 		m->m_pkthdr.len = m->m_len = len;
1335 	}
1336 	m_reset_rcvif(m);
1337 	m_copyback(m, 0, datalen, data);
1338 	if (len > datalen)
1339 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1340 	rtm = mtod(m, struct rt_xmsghdr *);
1341 	for (i = 0; i < RTAX_MAX; i++) {
1342 		if ((sa = rtinfo->rti_info[i]) == NULL)
1343 			continue;
1344 		rtinfo->rti_addrs |= (1 << i);
1345 		dlen = RT_XROUNDUP(sa->sa_len);
1346 		m_copyback(m, len, sa->sa_len, sa);
1347 		if (dlen != sa->sa_len) {
1348 			/*
1349 			 * Up to 6 + 1 nul's since roundup is to
1350 			 * sizeof(uint64_t) (8 bytes)
1351 			 */
1352 			m_copyback(m, len + sa->sa_len,
1353 			    dlen - sa->sa_len, "\0\0\0\0\0\0");
1354 		}
1355 		len += dlen;
1356 	}
1357 	if (m->m_pkthdr.len != len)
1358 		goto out;
1359 	rtm->rtm_msglen = len;
1360 	rtm->rtm_version = RTM_XVERSION;
1361 	rtm->rtm_type = type;
1362 	return m;
1363 out:
1364 	m_freem(m);
1365 	return NULL;
1366 }
1367 
1368 /*
1369  * rt_msg2
1370  *
1371  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
1372  *		returns the length of the message in 'lenp'.
1373  *
1374  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1375  *	the message
1376  * otherwise walkarg's w_needed is updated and if the user buffer is
1377  *	specified and w_needed indicates space exists the information is copied
1378  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1379  *	if the allocation fails ENOBUFS is returned.
1380  */
1381 static int
1382 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1383 	int *lenp)
1384 {
1385 	int i;
1386 	int len, dlen, second_time = 0;
1387 	char *cp0, *cp = cpv;
1388 
1389 	rtinfo->rti_addrs = 0;
1390 again:
1391 	if ((len = rt_getlen(type)) == -1)
1392 		return EINVAL;
1393 
1394 	if ((cp0 = cp) != NULL)
1395 		cp += len;
1396 	for (i = 0; i < RTAX_MAX; i++) {
1397 		const struct sockaddr *sa;
1398 
1399 		if ((sa = rtinfo->rti_info[i]) == NULL)
1400 			continue;
1401 		rtinfo->rti_addrs |= (1 << i);
1402 		dlen = RT_XROUNDUP(sa->sa_len);
1403 		if (cp) {
1404 			int diff = dlen - sa->sa_len;
1405 			(void)memcpy(cp, sa, (size_t)sa->sa_len);
1406 			cp += sa->sa_len;
1407 			if (diff > 0) {
1408 				(void)memset(cp, 0, (size_t)diff);
1409 				cp += diff;
1410 			}
1411 		}
1412 		len += dlen;
1413 	}
1414 	if (cp == NULL && w != NULL && !second_time) {
1415 		struct rt_walkarg *rw = w;
1416 
1417 		rw->w_needed += len;
1418 		if (rw->w_needed <= 0 && rw->w_where) {
1419 			if (rw->w_tmemsize < len) {
1420 				if (rw->w_tmem)
1421 					kmem_free(rw->w_tmem, rw->w_tmemsize);
1422 				rw->w_tmem = kmem_alloc(len, KM_SLEEP);
1423 				rw->w_tmemsize = len;
1424 			}
1425 			if (rw->w_tmem) {
1426 				cp = rw->w_tmem;
1427 				second_time = 1;
1428 				goto again;
1429 			} else {
1430 				rw->w_tmemneeded = len;
1431 				return ENOBUFS;
1432 			}
1433 		}
1434 	}
1435 	if (cp) {
1436 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1437 
1438 		rtm->rtm_version = RTM_XVERSION;
1439 		rtm->rtm_type = type;
1440 		rtm->rtm_msglen = len;
1441 	}
1442 	if (lenp)
1443 		*lenp = len;
1444 	return 0;
1445 }
1446 
1447 #ifndef COMPAT_RTSOCK
1448 int
1449 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1450 	int *lenp)
1451 {
1452 	return rt_msg2(type, rtinfo, cpv, w, lenp);
1453 }
1454 #endif
1455 
1456 /*
1457  * This routine is called to generate a message from the routing
1458  * socket indicating that a redirect has occurred, a routing lookup
1459  * has failed, or that a protocol has detected timeouts to a particular
1460  * destination.
1461  */
1462 void
1463 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1464     int error)
1465 {
1466 	struct rt_xmsghdr rtm;
1467 	struct mbuf *m;
1468 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1469 	struct rt_addrinfo info = *rtinfo;
1470 
1471 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1472 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1473 		return;
1474 	memset(&rtm, 0, sizeof(rtm));
1475 	rtm.rtm_pid = curproc->p_pid;
1476 	rtm.rtm_flags = RTF_DONE | flags;
1477 	rtm.rtm_errno = error;
1478 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1479 	if (m == NULL)
1480 		return;
1481 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1482 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1483 }
1484 
1485 /*
1486  * This routine is called to generate a message from the routing
1487  * socket indicating that the status of a network interface has changed.
1488  */
1489 void
1490 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1491 {
1492 	struct if_xmsghdr ifm;
1493 	struct mbuf *m;
1494 	struct rt_addrinfo info;
1495 
1496 	COMPATCALL(rt_ifmsg, (ifp));
1497 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1498 		return;
1499 	(void)memset(&info, 0, sizeof(info));
1500 	(void)memset(&ifm, 0, sizeof(ifm));
1501 	ifm.ifm_index = ifp->if_index;
1502 	ifm.ifm_flags = ifp->if_flags;
1503 	ifm.ifm_data = ifp->if_data;
1504 	ifm.ifm_addrs = 0;
1505 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1506 	if (m == NULL)
1507 		return;
1508 	COMPATNAME(route_enqueue)(m, 0);
1509 #ifdef COMPAT_14
1510 	compat_14_rt_oifmsg(ifp);
1511 #endif
1512 #ifdef COMPAT_50
1513 	compat_50_rt_oifmsg(ifp);
1514 #endif
1515 }
1516 
1517 #ifndef COMPAT_RTSOCK
1518 static int
1519 if_addrflags(struct ifaddr *ifa)
1520 {
1521 
1522 	switch (ifa->ifa_addr->sa_family) {
1523 #ifdef INET
1524 	case AF_INET:
1525 		return ((struct in_ifaddr *)ifa)->ia4_flags;
1526 #endif
1527 #ifdef INET6
1528 	case AF_INET6:
1529 		return ((struct in6_ifaddr *)ifa)->ia6_flags;
1530 #endif
1531 	default:
1532 		return 0;
1533 	}
1534 }
1535 #endif
1536 
1537 /*
1538  * This is called to generate messages from the routing socket
1539  * indicating a network interface has had addresses associated with it.
1540  * if we ever reverse the logic and replace messages TO the routing
1541  * socket indicate a request to configure interfaces, then it will
1542  * be unnecessary as the routing socket will automatically generate
1543  * copies of it.
1544  */
1545 void
1546 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1547     struct rtentry *rt)
1548 {
1549 #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
1550 	struct rt_addrinfo info;
1551 	const struct sockaddr *sa;
1552 	int pass;
1553 	struct mbuf *m;
1554 	struct ifnet *ifp;
1555 	struct rt_xmsghdr rtm;
1556 	struct ifa_xmsghdr ifam;
1557 	int ncmd;
1558 
1559 	KASSERT(ifa != NULL);
1560 	KASSERT(ifa->ifa_addr != NULL);
1561 	ifp = ifa->ifa_ifp;
1562 #ifdef SCTP
1563 	if (cmd == RTM_ADD) {
1564 		sctp_add_ip_address(ifa);
1565 	} else if (cmd == RTM_DELETE) {
1566 		sctp_delete_ip_address(ifa);
1567 	}
1568 #endif
1569 
1570 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1571 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1572 		return;
1573 	for (pass = 1; pass < 3; pass++) {
1574 		memset(&info, 0, sizeof(info));
1575 		switch (cmdpass(cmd, pass)) {
1576 		case cmdpass(RTM_ADD, 1):
1577 		case cmdpass(RTM_CHANGE, 1):
1578 		case cmdpass(RTM_DELETE, 2):
1579 		case cmdpass(RTM_NEWADDR, 1):
1580 		case cmdpass(RTM_DELADDR, 1):
1581 		case cmdpass(RTM_CHGADDR, 1):
1582 			switch (cmd) {
1583 			case RTM_ADD:
1584 				ncmd = RTM_XNEWADDR;
1585 				break;
1586 			case RTM_DELETE:
1587 				ncmd = RTM_XDELADDR;
1588 				break;
1589 			case RTM_CHANGE:
1590 				ncmd = RTM_XCHGADDR;
1591 				break;
1592 			case RTM_NEWADDR:
1593 				ncmd = RTM_XNEWADDR;
1594 				break;
1595 			case RTM_DELADDR:
1596 				ncmd = RTM_XDELADDR;
1597 				break;
1598 			case RTM_CHGADDR:
1599 				ncmd = RTM_XCHGADDR;
1600 				break;
1601 			default:
1602 				panic("%s: unknown command %d", __func__, cmd);
1603 			}
1604 #ifdef COMPAT_70
1605 			compat_70_rt_newaddrmsg1(ncmd, ifa);
1606 #endif
1607 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1608 			KASSERT(ifp->if_dl != NULL);
1609 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1610 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1611 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1612 			memset(&ifam, 0, sizeof(ifam));
1613 			ifam.ifam_index = ifp->if_index;
1614 			ifam.ifam_metric = ifa->ifa_metric;
1615 			ifam.ifam_flags = ifa->ifa_flags;
1616 #ifndef COMPAT_RTSOCK
1617 			ifam.ifam_pid = curproc->p_pid;
1618 			ifam.ifam_addrflags = if_addrflags(ifa);
1619 #endif
1620 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1621 			if (m == NULL)
1622 				continue;
1623 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1624 			    info.rti_addrs;
1625 			break;
1626 		case cmdpass(RTM_ADD, 2):
1627 		case cmdpass(RTM_CHANGE, 2):
1628 		case cmdpass(RTM_DELETE, 1):
1629 			if (rt == NULL)
1630 				continue;
1631 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1632 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1633 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1634 			memset(&rtm, 0, sizeof(rtm));
1635 			rtm.rtm_pid = curproc->p_pid;
1636 			rtm.rtm_index = ifp->if_index;
1637 			rtm.rtm_flags |= rt->rt_flags;
1638 			rtm.rtm_errno = error;
1639 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1640 			if (m == NULL)
1641 				continue;
1642 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1643 			break;
1644 		default:
1645 			continue;
1646 		}
1647 		KASSERTMSG(m != NULL, "called with wrong command");
1648 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1649 	}
1650 #undef cmdpass
1651 
1652 }
1653 
1654 static struct mbuf *
1655 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1656     struct rt_addrinfo *info)
1657 {
1658 	struct if_xannouncemsghdr ifan;
1659 
1660 	memset(info, 0, sizeof(*info));
1661 	memset(&ifan, 0, sizeof(ifan));
1662 	ifan.ifan_index = ifp->if_index;
1663 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1664 	ifan.ifan_what = what;
1665 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1666 }
1667 
1668 /*
1669  * This is called to generate routing socket messages indicating
1670  * network interface arrival and departure.
1671  */
1672 void
1673 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1674 {
1675 	struct mbuf *m;
1676 	struct rt_addrinfo info;
1677 
1678 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
1679 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1680 		return;
1681 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1682 	if (m == NULL)
1683 		return;
1684 	COMPATNAME(route_enqueue)(m, 0);
1685 }
1686 
1687 /*
1688  * This is called to generate routing socket messages indicating
1689  * IEEE80211 wireless events.
1690  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1691  */
1692 void
1693 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1694 	size_t data_len)
1695 {
1696 	struct mbuf *m;
1697 	struct rt_addrinfo info;
1698 
1699 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1700 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1701 		return;
1702 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1703 	if (m == NULL)
1704 		return;
1705 	/*
1706 	 * Append the ieee80211 data.  Try to stick it in the
1707 	 * mbuf containing the ifannounce msg; otherwise allocate
1708 	 * a new mbuf and append.
1709 	 *
1710 	 * NB: we assume m is a single mbuf.
1711 	 */
1712 	if (data_len > M_TRAILINGSPACE(m)) {
1713 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1714 		if (n == NULL) {
1715 			m_freem(m);
1716 			return;
1717 		}
1718 		(void)memcpy(mtod(n, void *), data, data_len);
1719 		n->m_len = data_len;
1720 		m->m_next = n;
1721 	} else if (data_len > 0) {
1722 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1723 		m->m_len += data_len;
1724 	}
1725 	if (m->m_flags & M_PKTHDR)
1726 		m->m_pkthdr.len += data_len;
1727 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1728 	COMPATNAME(route_enqueue)(m, 0);
1729 }
1730 
1731 #ifndef COMPAT_RTSOCK
1732 /*
1733  * Send a routing message as mimicing that a cloned route is added.
1734  */
1735 void
1736 rt_clonedmsg(const struct sockaddr *dst, const struct ifnet *ifp,
1737     const struct rtentry *rt)
1738 {
1739 	struct rt_addrinfo info;
1740 	/* Mimic flags exactly */
1741 #define RTF_LLINFO	0x400
1742 #define RTF_CLONED	0x2000
1743 	int flags = RTF_UP | RTF_HOST | RTF_DONE | RTF_LLINFO | RTF_CLONED;
1744 	union {
1745 		struct sockaddr sa;
1746 		struct sockaddr_storage ss;
1747 		struct sockaddr_dl sdl;
1748 	} u;
1749 	uint8_t namelen = strlen(ifp->if_xname);
1750 	uint8_t addrlen = ifp->if_addrlen;
1751 
1752 	if (rt == NULL)
1753 		return; /* XXX */
1754 
1755 	memset(&info, 0, sizeof(info));
1756 	info.rti_info[RTAX_DST] = dst;
1757 	sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
1758 	    NULL, namelen, NULL, addrlen);
1759 	info.rti_info[RTAX_GATEWAY] = &u.sa;
1760 
1761 	rt_missmsg(RTM_ADD, &info, flags, 0);
1762 #undef RTF_LLINFO
1763 #undef RTF_CLONED
1764 }
1765 #endif /* COMPAT_RTSOCK */
1766 
1767 /*
1768  * This is used in dumping the kernel table via sysctl().
1769  */
1770 static int
1771 sysctl_dumpentry(struct rtentry *rt, void *v)
1772 {
1773 	struct rt_walkarg *w = v;
1774 	int error = 0, size;
1775 	struct rt_addrinfo info;
1776 
1777 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1778 		return 0;
1779 	memset(&info, 0, sizeof(info));
1780 	info.rti_info[RTAX_DST] = rt_getkey(rt);
1781 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1782 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1783 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
1784 	if (rt->rt_ifp) {
1785 		const struct ifaddr *rtifa;
1786 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1787 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
1788 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
1789 		 * --dyoung
1790 		 */
1791 		rtifa = rt_get_ifa(rt);
1792 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1793 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1794 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1795 	}
1796 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1797 		return error;
1798 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1799 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1800 
1801 		rtm->rtm_flags = rt->rt_flags;
1802 		rtm->rtm_use = rt->rt_use;
1803 		rtm_setmetrics(rt, rtm);
1804 		KASSERT(rt->rt_ifp != NULL);
1805 		rtm->rtm_index = rt->rt_ifp->if_index;
1806 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1807 		rtm->rtm_addrs = info.rti_addrs;
1808 		if ((error = copyout(rtm, w->w_where, size)) != 0)
1809 			w->w_where = NULL;
1810 		else
1811 			w->w_where = (char *)w->w_where + size;
1812 	}
1813 	return error;
1814 }
1815 
1816 static int
1817 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
1818     struct rt_addrinfo *info, size_t len)
1819 {
1820 	struct if_xmsghdr *ifm;
1821 	int error;
1822 
1823 	ifm = (struct if_xmsghdr *)w->w_tmem;
1824 	ifm->ifm_index = ifp->if_index;
1825 	ifm->ifm_flags = ifp->if_flags;
1826 	ifm->ifm_data = ifp->if_data;
1827 	ifm->ifm_addrs = info->rti_addrs;
1828 	if ((error = copyout(ifm, w->w_where, len)) == 0)
1829 		w->w_where = (char *)w->w_where + len;
1830 	return error;
1831 }
1832 
1833 static int
1834 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
1835      struct rt_addrinfo *info)
1836 {
1837 	int len, error;
1838 
1839 	if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
1840 		return error;
1841 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1842 		struct ifa_xmsghdr *ifam;
1843 
1844 		ifam = (struct ifa_xmsghdr *)w->w_tmem;
1845 		ifam->ifam_index = ifa->ifa_ifp->if_index;
1846 		ifam->ifam_flags = ifa->ifa_flags;
1847 		ifam->ifam_metric = ifa->ifa_metric;
1848 		ifam->ifam_addrs = info->rti_addrs;
1849 #ifndef COMPAT_RTSOCK
1850 		ifam->ifam_pid = 0;
1851 		ifam->ifam_addrflags = if_addrflags(ifa);
1852 #endif
1853 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
1854 			w->w_where = (char *)w->w_where + len;
1855 	}
1856 	return error;
1857 }
1858 
1859 static int
1860 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1861 {
1862 	struct ifnet *ifp;
1863 	struct ifaddr *ifa;
1864 	struct	rt_addrinfo info;
1865 	int	cmd, len, error = 0;
1866 	int	(*iflist_if)(struct ifnet *, struct rt_walkarg *,
1867 			     struct rt_addrinfo *, size_t);
1868 	int	(*iflist_addr)(struct rt_walkarg *, struct ifaddr *,
1869 			       struct rt_addrinfo *);
1870 	int s;
1871 	struct psref psref;
1872 	int bound;
1873 
1874 	switch (type) {
1875 	case NET_RT_IFLIST:
1876 		cmd = RTM_IFINFO;
1877 		iflist_if = sysctl_iflist_if;
1878 		iflist_addr = sysctl_iflist_addr;
1879 		break;
1880 #ifdef COMPAT_14
1881 	case NET_RT_OOOIFLIST:
1882 		cmd = RTM_OOIFINFO;
1883 		iflist_if = compat_14_iflist;
1884 		iflist_addr = compat_70_iflist_addr;
1885 		break;
1886 #endif
1887 #ifdef COMPAT_50
1888 	case NET_RT_OOIFLIST:
1889 		cmd = RTM_OIFINFO;
1890 		iflist_if = compat_50_iflist;
1891 		iflist_addr = compat_70_iflist_addr;
1892 		break;
1893 #endif
1894 #ifdef COMPAT_70
1895 	case NET_RT_OIFLIST:
1896 		cmd = RTM_IFINFO;
1897 		iflist_if = sysctl_iflist_if;
1898 		iflist_addr = compat_70_iflist_addr;
1899 		break;
1900 #endif
1901 	default:
1902 #ifdef RTSOCK_DEBUG
1903 		printf("%s: unsupported IFLIST type %d\n", __func__, type);
1904 #endif
1905 		return EINVAL;
1906 	}
1907 
1908 	memset(&info, 0, sizeof(info));
1909 
1910 	bound = curlwp_bind();
1911 	s = pserialize_read_enter();
1912 	IFNET_READER_FOREACH(ifp) {
1913 		int _s;
1914 		if (w->w_arg && w->w_arg != ifp->if_index)
1915 			continue;
1916 		if (IFADDR_READER_EMPTY(ifp))
1917 			continue;
1918 
1919 		if_acquire(ifp, &psref);
1920 		pserialize_read_exit(s);
1921 
1922 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1923 		if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
1924 			goto release_exit;
1925 		info.rti_info[RTAX_IFP] = NULL;
1926 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1927 			if ((error = iflist_if(ifp, w, &info, len)) != 0)
1928 				goto release_exit;
1929 		}
1930 		_s = pserialize_read_enter();
1931 		IFADDR_READER_FOREACH(ifa, ifp) {
1932 			struct psref _psref;
1933 			if (af && af != ifa->ifa_addr->sa_family)
1934 				continue;
1935 			ifa_acquire(ifa, &_psref);
1936 			pserialize_read_exit(_s);
1937 
1938 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1939 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1940 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1941 			error = iflist_addr(w, ifa, &info);
1942 
1943 			_s = pserialize_read_enter();
1944 			ifa_release(ifa, &_psref);
1945 			if (error != 0) {
1946 				pserialize_read_exit(_s);
1947 				goto release_exit;
1948 			}
1949 		}
1950 		pserialize_read_exit(_s);
1951 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1952 		    info.rti_info[RTAX_BRD] = NULL;
1953 
1954 		s = pserialize_read_enter();
1955 		if_release(ifp, &psref);
1956 	}
1957 	pserialize_read_exit(s);
1958 	curlwp_bindx(bound);
1959 
1960 	return 0;
1961 
1962 release_exit:
1963 	if_release(ifp, &psref);
1964 	curlwp_bindx(bound);
1965 	return error;
1966 }
1967 
1968 static int
1969 sysctl_rtable(SYSCTLFN_ARGS)
1970 {
1971 	void 	*where = oldp;
1972 	size_t	*given = oldlenp;
1973 	int	i, s, error = EINVAL;
1974 	u_char  af;
1975 	struct	rt_walkarg w;
1976 
1977 	if (namelen == 1 && name[0] == CTL_QUERY)
1978 		return sysctl_query(SYSCTLFN_CALL(rnode));
1979 
1980 	if (newp)
1981 		return EPERM;
1982 	if (namelen != 3)
1983 		return EINVAL;
1984 	af = name[0];
1985 	w.w_tmemneeded = 0;
1986 	w.w_tmemsize = 0;
1987 	w.w_tmem = NULL;
1988 again:
1989 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
1990 	if (w.w_tmemneeded) {
1991 		w.w_tmem = kmem_alloc(w.w_tmemneeded, KM_SLEEP);
1992 		w.w_tmemsize = w.w_tmemneeded;
1993 		w.w_tmemneeded = 0;
1994 	}
1995 	w.w_op = name[1];
1996 	w.w_arg = name[2];
1997 	w.w_given = *given;
1998 	w.w_needed = 0 - w.w_given;
1999 	w.w_where = where;
2000 
2001 	s = splsoftnet();
2002 	switch (w.w_op) {
2003 
2004 	case NET_RT_DUMP:
2005 	case NET_RT_FLAGS:
2006 #if defined(INET) || defined(INET6)
2007 		/*
2008 		 * take care of llinfo entries, the caller must
2009 		 * specify an AF
2010 		 */
2011 		if (w.w_op == NET_RT_FLAGS &&
2012 		    (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
2013 			if (af != 0)
2014 				error = lltable_sysctl_dump(af, &w);
2015 			else
2016 				error = EINVAL;
2017 			break;
2018 		}
2019 #endif
2020 
2021 		for (i = 1; i <= AF_MAX; i++) {
2022 			if (af == 0 || af == i) {
2023 				error = rt_walktree(i, sysctl_dumpentry, &w);
2024 				if (error != 0)
2025 					break;
2026 #if defined(INET) || defined(INET6)
2027 				/*
2028 				 * Return ARP/NDP entries too for
2029 				 * backward compatibility.
2030 				 */
2031 				error = lltable_sysctl_dump(i, &w);
2032 				if (error != 0)
2033 					break;
2034 #endif
2035 			}
2036 		}
2037 		break;
2038 
2039 #ifdef COMPAT_14
2040 	case NET_RT_OOOIFLIST:
2041 		error = sysctl_iflist(af, &w, w.w_op);
2042 		break;
2043 #endif
2044 #ifdef COMPAT_50
2045 	case NET_RT_OOIFLIST:
2046 		error = sysctl_iflist(af, &w, w.w_op);
2047 		break;
2048 #endif
2049 #ifdef COMPAT_70
2050 	case NET_RT_OIFLIST:
2051 		error = sysctl_iflist(af, &w, w.w_op);
2052 		break;
2053 #endif
2054 	case NET_RT_IFLIST:
2055 		error = sysctl_iflist(af, &w, w.w_op);
2056 		break;
2057 	}
2058 	splx(s);
2059 
2060 	/* check to see if we couldn't allocate memory with NOWAIT */
2061 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
2062 		goto again;
2063 
2064 	if (w.w_tmem)
2065 		kmem_free(w.w_tmem, w.w_tmemsize);
2066 	w.w_needed += w.w_given;
2067 	if (where) {
2068 		*given = (char *)w.w_where - (char *)where;
2069 		if (*given < w.w_needed)
2070 			return ENOMEM;
2071 	} else {
2072 		*given = (11 * w.w_needed) / 10;
2073 	}
2074 	return error;
2075 }
2076 
2077 /*
2078  * Routing message software interrupt routine
2079  */
2080 static void
2081 COMPATNAME(route_intr)(void *cookie)
2082 {
2083 	struct sockproto proto = { .sp_family = PF_XROUTE, };
2084 	struct route_info * const ri = &COMPATNAME(route_info);
2085 	struct mbuf *m;
2086 
2087 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
2088 	for (;;) {
2089 		IFQ_LOCK(&ri->ri_intrq);
2090 		IF_DEQUEUE(&ri->ri_intrq, m);
2091 		IFQ_UNLOCK(&ri->ri_intrq);
2092 		if (m == NULL)
2093 			break;
2094 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
2095 #ifdef NET_MPSAFE
2096 		mutex_enter(rt_so_mtx);
2097 #endif
2098 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst, &rt_rawcb);
2099 #ifdef NET_MPSAFE
2100 		mutex_exit(rt_so_mtx);
2101 #endif
2102 	}
2103 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2104 }
2105 
2106 /*
2107  * Enqueue a message to the software interrupt routine.
2108  */
2109 void
2110 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
2111 {
2112 	struct route_info * const ri = &COMPATNAME(route_info);
2113 	int wasempty;
2114 
2115 	IFQ_LOCK(&ri->ri_intrq);
2116 	if (IF_QFULL(&ri->ri_intrq)) {
2117 		printf("%s: queue full, dropped message\n", __func__);
2118 		IF_DROP(&ri->ri_intrq);
2119 		IFQ_UNLOCK(&ri->ri_intrq);
2120 		m_freem(m);
2121 	} else {
2122 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
2123 		M_SETCTX(m, (uintptr_t)family);
2124 		IF_ENQUEUE(&ri->ri_intrq, m);
2125 		IFQ_UNLOCK(&ri->ri_intrq);
2126 		if (wasempty) {
2127 			kpreempt_disable();
2128 			softint_schedule(ri->ri_sih);
2129 			kpreempt_enable();
2130 		}
2131 	}
2132 }
2133 
2134 static void
2135 COMPATNAME(route_init)(void)
2136 {
2137 	struct route_info * const ri = &COMPATNAME(route_info);
2138 
2139 #ifndef COMPAT_RTSOCK
2140 	rt_init();
2141 #endif
2142 #ifdef NET_MPSAFE
2143 	rt_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
2144 
2145 	cv_init(&rt_update_cv, "rtsock_cv");
2146 #endif
2147 
2148 	sysctl_net_route_setup(NULL);
2149 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
2150 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
2151 	    COMPATNAME(route_intr), NULL);
2152 	IFQ_LOCK_INIT(&ri->ri_intrq);
2153 }
2154 
2155 /*
2156  * Definitions of protocols supported in the ROUTE domain.
2157  */
2158 #ifndef COMPAT_RTSOCK
2159 PR_WRAP_USRREQS(route);
2160 #else
2161 PR_WRAP_USRREQS(compat_50_route);
2162 #endif
2163 
2164 static const struct pr_usrreqs route_usrreqs = {
2165 	.pr_attach	= COMPATNAME(route_attach_wrapper),
2166 	.pr_detach	= COMPATNAME(route_detach_wrapper),
2167 	.pr_accept	= COMPATNAME(route_accept_wrapper),
2168 	.pr_bind	= COMPATNAME(route_bind_wrapper),
2169 	.pr_listen	= COMPATNAME(route_listen_wrapper),
2170 	.pr_connect	= COMPATNAME(route_connect_wrapper),
2171 	.pr_connect2	= COMPATNAME(route_connect2_wrapper),
2172 	.pr_disconnect	= COMPATNAME(route_disconnect_wrapper),
2173 	.pr_shutdown	= COMPATNAME(route_shutdown_wrapper),
2174 	.pr_abort	= COMPATNAME(route_abort_wrapper),
2175 	.pr_ioctl	= COMPATNAME(route_ioctl_wrapper),
2176 	.pr_stat	= COMPATNAME(route_stat_wrapper),
2177 	.pr_peeraddr	= COMPATNAME(route_peeraddr_wrapper),
2178 	.pr_sockaddr	= COMPATNAME(route_sockaddr_wrapper),
2179 	.pr_rcvd	= COMPATNAME(route_rcvd_wrapper),
2180 	.pr_recvoob	= COMPATNAME(route_recvoob_wrapper),
2181 	.pr_send	= COMPATNAME(route_send_wrapper),
2182 	.pr_sendoob	= COMPATNAME(route_sendoob_wrapper),
2183 	.pr_purgeif	= COMPATNAME(route_purgeif_wrapper),
2184 };
2185 
2186 static const struct protosw COMPATNAME(route_protosw)[] = {
2187 	{
2188 		.pr_type = SOCK_RAW,
2189 		.pr_domain = &COMPATNAME(routedomain),
2190 		.pr_flags = PR_ATOMIC|PR_ADDR,
2191 		.pr_input = raw_input,
2192 		.pr_ctlinput = raw_ctlinput,
2193 		.pr_ctloutput = route_ctloutput,
2194 		.pr_usrreqs = &route_usrreqs,
2195 		.pr_init = rt_pr_init,
2196 	},
2197 };
2198 
2199 struct domain COMPATNAME(routedomain) = {
2200 	.dom_family = PF_XROUTE,
2201 	.dom_name = DOMAINNAME,
2202 	.dom_init = COMPATNAME(route_init),
2203 	.dom_protosw = COMPATNAME(route_protosw),
2204 	.dom_protoswNPROTOSW =
2205 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
2206 };
2207 
2208 static void
2209 sysctl_net_route_setup(struct sysctllog **clog)
2210 {
2211 	const struct sysctlnode *rnode = NULL;
2212 
2213 	sysctl_createv(clog, 0, NULL, &rnode,
2214 		       CTLFLAG_PERMANENT,
2215 		       CTLTYPE_NODE, DOMAINNAME,
2216 		       SYSCTL_DESCR("PF_ROUTE information"),
2217 		       NULL, 0, NULL, 0,
2218 		       CTL_NET, PF_XROUTE, CTL_EOL);
2219 
2220 	sysctl_createv(clog, 0, NULL, NULL,
2221 		       CTLFLAG_PERMANENT,
2222 		       CTLTYPE_NODE, "rtable",
2223 		       SYSCTL_DESCR("Routing table information"),
2224 		       sysctl_rtable, 0, NULL, 0,
2225 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
2226 
2227 	sysctl_createv(clog, 0, &rnode, NULL,
2228 		       CTLFLAG_PERMANENT,
2229 		       CTLTYPE_STRUCT, "stats",
2230 		       SYSCTL_DESCR("Routing statistics"),
2231 		       NULL, 0, &rtstat, sizeof(rtstat),
2232 		       CTL_CREATE, CTL_EOL);
2233 }
2234