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