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