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