rtsock.c revision 1.208 1 /* $NetBSD: rtsock.c,v 1.208 2017/03/14 09:39:28 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.208 2017/03/14 09:39:28 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 /* new gateway could require new ifaddr, ifp;
631 flags may also be different; ifp may be specified
632 by ll sockaddr when protocol address is ambiguous */
633 new_ifa = route_output_get_ifa(*info, rt, &new_ifp, &psref_new_ifa);
634 if (new_ifa != NULL) {
635 ifa_release(ifa, &psref_ifa);
636 ifa = new_ifa;
637 }
638 if (ifa) {
639 struct ifaddr *oifa = rt->rt_ifa;
640 if (oifa != ifa &&
641 !ifa_is_destroying(ifa) && !if_is_deactivated(new_ifp)) {
642 if (oifa && oifa->ifa_rtrequest)
643 oifa->ifa_rtrequest(RTM_DELETE, rt, info);
644 rt_replace_ifa(rt, ifa);
645 rt->rt_ifp = new_ifp;
646 }
647 if (new_ifa == NULL)
648 ifa_release(ifa, &psref_ifa);
649 }
650 ifa_release(new_ifa, &psref_new_ifa);
651 if (new_ifp && rt->rt_ifp != new_ifp
652 && !if_is_deactivated(new_ifp))
653 rt->rt_ifp = new_ifp;
654 rt_setmetrics(rtm->rtm_inits, rtm, rt);
655 if (rt->rt_flags != info->rti_flags)
656 rt->rt_flags = (info->rti_flags & ~PRESERVED_RTF)
657 | (rt->rt_flags & PRESERVED_RTF);
658 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
659 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
660 out:
661 if_put(ifp, &psref_ifp);
662
663 return error;
664 }
665
666 /*ARGSUSED*/
667 int
668 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
669 {
670 struct sockproto proto = { .sp_family = PF_XROUTE, };
671 struct rt_xmsghdr *rtm = NULL;
672 struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
673 struct rtentry *rt = NULL;
674 struct rtentry *saved_nrt = NULL;
675 struct rt_addrinfo info;
676 int len, error = 0;
677 sa_family_t family;
678 struct sockaddr_dl sdl;
679 int bound = curlwp_bind();
680 bool do_rt_free = false;
681
682 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
683 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
684 (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
685 error = ENOBUFS;
686 goto out;
687 }
688 if ((m->m_flags & M_PKTHDR) == 0)
689 panic("%s", __func__);
690 len = m->m_pkthdr.len;
691 if (len < sizeof(*rtm) ||
692 len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
693 info.rti_info[RTAX_DST] = NULL;
694 senderr(EINVAL);
695 }
696 R_Malloc(rtm, struct rt_xmsghdr *, len);
697 if (rtm == NULL) {
698 info.rti_info[RTAX_DST] = NULL;
699 senderr(ENOBUFS);
700 }
701 m_copydata(m, 0, len, rtm);
702 if (rtm->rtm_version != RTM_XVERSION) {
703 info.rti_info[RTAX_DST] = NULL;
704 senderr(EPROTONOSUPPORT);
705 }
706 rtm->rtm_pid = curproc->p_pid;
707 memset(&info, 0, sizeof(info));
708 info.rti_addrs = rtm->rtm_addrs;
709 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
710 &info)) {
711 senderr(EINVAL);
712 }
713 info.rti_flags = rtm->rtm_flags;
714 #ifdef RTSOCK_DEBUG
715 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
716 char abuf[INET_ADDRSTRLEN];
717 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
718 RT_IN_PRINT(&info, abuf, RTAX_DST));
719 }
720 #endif /* RTSOCK_DEBUG */
721 if (info.rti_info[RTAX_DST] == NULL ||
722 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
723 senderr(EINVAL);
724 }
725 if (info.rti_info[RTAX_GATEWAY] != NULL &&
726 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
727 senderr(EINVAL);
728 }
729
730 /*
731 * Verify that the caller has the appropriate privilege; RTM_GET
732 * is the only operation the non-superuser is allowed.
733 */
734 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
735 0, rtm, NULL, NULL) != 0)
736 senderr(EACCES);
737
738 switch (rtm->rtm_type) {
739
740 case RTM_ADD:
741 if (info.rti_info[RTAX_GATEWAY] == NULL) {
742 senderr(EINVAL);
743 }
744 #ifdef INET
745 /* support for new ARP code with keeping backcompat */
746 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
747 const struct sockaddr_dl *sdlp =
748 satocsdl(info.rti_info[RTAX_GATEWAY]);
749
750 /* Allow routing requests by interface index */
751 if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
752 && sdlp->sdl_slen == 0)
753 goto fallback;
754 /*
755 * Old arp binaries don't set the sdl_index
756 * so we have to complement it.
757 */
758 int sdl_index = sdlp->sdl_index;
759 if (sdl_index == 0) {
760 error = route_get_sdl_index(&info, &sdl_index);
761 if (error != 0)
762 goto fallback;
763 } else if (
764 info.rti_info[RTAX_DST]->sa_family == AF_INET) {
765 /*
766 * XXX workaround for SIN_PROXY case; proxy arp
767 * entry should be in an interface that has
768 * a network route including the destination,
769 * not a local (link) route that may not be a
770 * desired place, for example a tap.
771 */
772 const struct sockaddr_inarp *sina =
773 (const struct sockaddr_inarp *)
774 info.rti_info[RTAX_DST];
775 if (sina->sin_other & SIN_PROXY) {
776 error = route_get_sdl_index(&info,
777 &sdl_index);
778 if (error != 0)
779 goto fallback;
780 }
781 }
782 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
783 rtm->rtm_rmx.rmx_expire, &info, sdl_index);
784 break;
785 }
786 fallback:
787 #endif /* INET */
788 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
789 if (error == 0) {
790 rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
791 rt_unref(saved_nrt);
792 }
793 break;
794
795 case RTM_DELETE:
796 #ifdef INET
797 /* support for new ARP code */
798 if (info.rti_info[RTAX_GATEWAY] &&
799 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
800 (rtm->rtm_flags & RTF_LLDATA) != 0) {
801 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
802 rtm->rtm_rmx.rmx_expire, &info, 0);
803 break;
804 }
805 #endif /* INET */
806 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
807 if (error != 0)
808 break;
809
810 rt = saved_nrt;
811 do_rt_free = true;
812 info.rti_info[RTAX_DST] = rt_getkey(rt);
813 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
814 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
815 info.rti_info[RTAX_TAG] = rt_gettag(rt);
816 error = route_output_report(rt, &info, rtm, &new_rtm);
817 if (error)
818 senderr(error);
819 if (new_rtm != NULL) {
820 old_rtm = rtm;
821 rtm = new_rtm;
822 }
823 break;
824
825 case RTM_GET:
826 case RTM_CHANGE:
827 case RTM_LOCK:
828 /* XXX This will mask info.rti_info[RTAX_DST] with
829 * info.rti_info[RTAX_NETMASK] before
830 * searching. It did not used to do that. --dyoung
831 */
832 rt = NULL;
833 error = rtrequest1(RTM_GET, &info, &rt);
834 if (error != 0)
835 senderr(error);
836 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
837 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
838 info.rti_info[RTAX_DST]->sa_len) != 0)
839 senderr(ESRCH);
840 if (info.rti_info[RTAX_NETMASK] == NULL &&
841 rt_mask(rt) != NULL)
842 senderr(ETOOMANYREFS);
843 }
844
845 /*
846 * XXX if arp/ndp requests an L2 entry, we have to obtain
847 * it from lltable while for the route command we have to
848 * return a route as it is. How to distinguish them?
849 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
850 * indicates an L2 entry is requested. For old arp/ndp
851 * binaries, we check RTF_UP flag is NOT set; it works
852 * by the fact that arp/ndp don't set it while the route
853 * command sets it.
854 */
855 if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
856 (rtm->rtm_flags & RTF_UP) == 0) &&
857 rtm->rtm_type == RTM_GET &&
858 sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
859 int ll_flags = 0;
860 route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
861 &ll_flags);
862 info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
863 error = route_output_report(rt, &info, rtm, &new_rtm);
864 if (error)
865 senderr(error);
866 if (new_rtm != NULL) {
867 old_rtm = rtm;
868 rtm = new_rtm;
869 }
870 rtm->rtm_flags |= RTF_LLDATA;
871 rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
872 break;
873 }
874
875 switch (rtm->rtm_type) {
876 case RTM_GET:
877 info.rti_info[RTAX_DST] = rt_getkey(rt);
878 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
879 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
880 info.rti_info[RTAX_TAG] = rt_gettag(rt);
881 error = route_output_report(rt, &info, rtm, &new_rtm);
882 if (error)
883 senderr(error);
884 if (new_rtm != NULL) {
885 old_rtm = rtm;
886 rtm = new_rtm;
887 }
888 break;
889
890 case RTM_CHANGE:
891 #ifdef NET_MPSAFE
892 error = rt_update_prepare(rt);
893 if (error == 0) {
894 error = route_output_change(rt, &info, rtm);
895 rt_update_finish(rt);
896 }
897 #else
898 error = route_output_change(rt, &info, rtm);
899 #endif
900 if (error != 0)
901 goto flush;
902 /*FALLTHROUGH*/
903 case RTM_LOCK:
904 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
905 rt->rt_rmx.rmx_locks |=
906 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
907 break;
908 }
909 break;
910
911 default:
912 senderr(EOPNOTSUPP);
913 }
914
915 flush:
916 if (rtm) {
917 if (error)
918 rtm->rtm_errno = error;
919 else
920 rtm->rtm_flags |= RTF_DONE;
921 }
922 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
923 0;
924 /* We cannot free old_rtm until we have stopped using the
925 * pointers in info, some of which may point to sockaddrs
926 * in old_rtm.
927 */
928 if (old_rtm != NULL)
929 Free(old_rtm);
930 if (rt) {
931 if (do_rt_free)
932 rt_free(rt);
933 else
934 rt_unref(rt);
935 }
936 {
937 struct rawcb *rp = NULL;
938 /*
939 * Check to see if we don't want our own messages.
940 */
941 if ((so->so_options & SO_USELOOPBACK) == 0) {
942 if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
943 if (rtm)
944 Free(rtm);
945 m_freem(m);
946 goto out;
947 }
948 /* There is another listener, so construct message */
949 rp = sotorawcb(so);
950 }
951 if (rtm) {
952 m_copyback(m, 0, rtm->rtm_msglen, rtm);
953 if (m->m_pkthdr.len < rtm->rtm_msglen) {
954 m_freem(m);
955 m = NULL;
956 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
957 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
958 Free(rtm);
959 }
960 if (rp)
961 rp->rcb_proto.sp_family = 0; /* Avoid us */
962 if (family)
963 proto.sp_protocol = family;
964 if (m)
965 raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
966 &COMPATNAME(route_info).ri_dst);
967 if (rp)
968 rp->rcb_proto.sp_family = PF_XROUTE;
969 }
970 out:
971 curlwp_bindx(bound);
972 return error;
973 }
974
975 static void
976 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
977 {
978 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
979 metric(RTV_RPIPE, rmx_recvpipe);
980 metric(RTV_SPIPE, rmx_sendpipe);
981 metric(RTV_SSTHRESH, rmx_ssthresh);
982 metric(RTV_RTT, rmx_rtt);
983 metric(RTV_RTTVAR, rmx_rttvar);
984 metric(RTV_HOPCOUNT, rmx_hopcount);
985 metric(RTV_MTU, rmx_mtu);
986 #undef metric
987 if (which & RTV_EXPIRE) {
988 out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
989 time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
990 }
991 }
992
993 static void
994 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
995 {
996 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
997 metric(rmx_recvpipe);
998 metric(rmx_sendpipe);
999 metric(rmx_ssthresh);
1000 metric(rmx_rtt);
1001 metric(rmx_rttvar);
1002 metric(rmx_hopcount);
1003 metric(rmx_mtu);
1004 metric(rmx_locks);
1005 #undef metric
1006 out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
1007 time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
1008 }
1009
1010 static int
1011 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
1012 struct rt_addrinfo *rtinfo)
1013 {
1014 const struct sockaddr *sa = NULL; /* Quell compiler warning */
1015 int i;
1016
1017 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1018 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1019 continue;
1020 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
1021 RT_XADVANCE(cp, sa);
1022 }
1023
1024 /*
1025 * Check for extra addresses specified, except RTM_GET asking
1026 * for interface info.
1027 */
1028 if (rtmtype == RTM_GET) {
1029 if (((rtinfo->rti_addrs &
1030 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
1031 return 1;
1032 } else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
1033 return 1;
1034 /* Check for bad data length. */
1035 if (cp != cplim) {
1036 if (i == RTAX_NETMASK + 1 && sa != NULL &&
1037 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
1038 /*
1039 * The last sockaddr was info.rti_info[RTAX_NETMASK].
1040 * We accept this for now for the sake of old
1041 * binaries or third party softwares.
1042 */
1043 ;
1044 else
1045 return 1;
1046 }
1047 return 0;
1048 }
1049
1050 static int
1051 rt_getlen(int type)
1052 {
1053 #ifndef COMPAT_RTSOCK
1054 CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
1055 CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
1056 CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
1057 CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
1058 #endif
1059
1060 switch (type) {
1061 case RTM_ODELADDR:
1062 case RTM_ONEWADDR:
1063 case RTM_OCHGADDR:
1064 #ifdef COMPAT_70
1065 return sizeof(struct ifa_msghdr70);
1066 #else
1067 #ifdef RTSOCK_DEBUG
1068 printf("%s: unsupported RTM type %d\n", __func__, type);
1069 #endif
1070 return -1;
1071 #endif
1072 case RTM_DELADDR:
1073 case RTM_NEWADDR:
1074 case RTM_CHGADDR:
1075 return sizeof(struct ifa_xmsghdr);
1076
1077 case RTM_OOIFINFO:
1078 #ifdef COMPAT_14
1079 return sizeof(struct if_msghdr14);
1080 #else
1081 #ifdef RTSOCK_DEBUG
1082 printf("%s: unsupported RTM type RTM_OOIFINFO\n", __func__);
1083 #endif
1084 return -1;
1085 #endif
1086 case RTM_OIFINFO:
1087 #ifdef COMPAT_50
1088 return sizeof(struct if_msghdr50);
1089 #else
1090 #ifdef RTSOCK_DEBUG
1091 printf("%s: unsupported RTM type RTM_OIFINFO\n", __func__);
1092 #endif
1093 return -1;
1094 #endif
1095
1096 case RTM_IFINFO:
1097 return sizeof(struct if_xmsghdr);
1098
1099 case RTM_IFANNOUNCE:
1100 case RTM_IEEE80211:
1101 return sizeof(struct if_xannouncemsghdr);
1102
1103 default:
1104 return sizeof(struct rt_xmsghdr);
1105 }
1106 }
1107
1108
1109 struct mbuf *
1110 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
1111 {
1112 struct rt_xmsghdr *rtm;
1113 struct mbuf *m;
1114 int i;
1115 const struct sockaddr *sa;
1116 int len, dlen;
1117
1118 m = m_gethdr(M_DONTWAIT, MT_DATA);
1119 if (m == NULL)
1120 return m;
1121 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
1122
1123 if ((len = rt_getlen(type)) == -1)
1124 goto out;
1125 if (len > MHLEN + MLEN)
1126 panic("%s: message too long", __func__);
1127 else if (len > MHLEN) {
1128 m->m_next = m_get(M_DONTWAIT, MT_DATA);
1129 if (m->m_next == NULL)
1130 goto out;
1131 MCLAIM(m->m_next, m->m_owner);
1132 m->m_pkthdr.len = len;
1133 m->m_len = MHLEN;
1134 m->m_next->m_len = len - MHLEN;
1135 } else {
1136 m->m_pkthdr.len = m->m_len = len;
1137 }
1138 m_reset_rcvif(m);
1139 m_copyback(m, 0, datalen, data);
1140 if (len > datalen)
1141 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1142 rtm = mtod(m, struct rt_xmsghdr *);
1143 for (i = 0; i < RTAX_MAX; i++) {
1144 if ((sa = rtinfo->rti_info[i]) == NULL)
1145 continue;
1146 rtinfo->rti_addrs |= (1 << i);
1147 dlen = RT_XROUNDUP(sa->sa_len);
1148 m_copyback(m, len, sa->sa_len, sa);
1149 if (dlen != sa->sa_len) {
1150 /*
1151 * Up to 6 + 1 nul's since roundup is to
1152 * sizeof(uint64_t) (8 bytes)
1153 */
1154 m_copyback(m, len + sa->sa_len,
1155 dlen - sa->sa_len, "\0\0\0\0\0\0");
1156 }
1157 len += dlen;
1158 }
1159 if (m->m_pkthdr.len != len)
1160 goto out;
1161 rtm->rtm_msglen = len;
1162 rtm->rtm_version = RTM_XVERSION;
1163 rtm->rtm_type = type;
1164 return m;
1165 out:
1166 m_freem(m);
1167 return NULL;
1168 }
1169
1170 /*
1171 * rt_msg2
1172 *
1173 * fills 'cp' or 'w'.w_tmem with the routing socket message and
1174 * returns the length of the message in 'lenp'.
1175 *
1176 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1177 * the message
1178 * otherwise walkarg's w_needed is updated and if the user buffer is
1179 * specified and w_needed indicates space exists the information is copied
1180 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1181 * if the allocation fails ENOBUFS is returned.
1182 */
1183 static int
1184 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1185 int *lenp)
1186 {
1187 int i;
1188 int len, dlen, second_time = 0;
1189 char *cp0, *cp = cpv;
1190
1191 rtinfo->rti_addrs = 0;
1192 again:
1193 if ((len = rt_getlen(type)) == -1)
1194 return EINVAL;
1195
1196 if ((cp0 = cp) != NULL)
1197 cp += len;
1198 for (i = 0; i < RTAX_MAX; i++) {
1199 const struct sockaddr *sa;
1200
1201 if ((sa = rtinfo->rti_info[i]) == NULL)
1202 continue;
1203 rtinfo->rti_addrs |= (1 << i);
1204 dlen = RT_XROUNDUP(sa->sa_len);
1205 if (cp) {
1206 int diff = dlen - sa->sa_len;
1207 (void)memcpy(cp, sa, (size_t)sa->sa_len);
1208 cp += sa->sa_len;
1209 if (diff > 0) {
1210 (void)memset(cp, 0, (size_t)diff);
1211 cp += diff;
1212 }
1213 }
1214 len += dlen;
1215 }
1216 if (cp == NULL && w != NULL && !second_time) {
1217 struct rt_walkarg *rw = w;
1218
1219 rw->w_needed += len;
1220 if (rw->w_needed <= 0 && rw->w_where) {
1221 if (rw->w_tmemsize < len) {
1222 if (rw->w_tmem)
1223 kmem_free(rw->w_tmem, rw->w_tmemsize);
1224 rw->w_tmem = kmem_alloc(len, KM_SLEEP);
1225 if (rw->w_tmem)
1226 rw->w_tmemsize = len;
1227 else
1228 rw->w_tmemsize = 0;
1229 }
1230 if (rw->w_tmem) {
1231 cp = rw->w_tmem;
1232 second_time = 1;
1233 goto again;
1234 } else {
1235 rw->w_tmemneeded = len;
1236 return ENOBUFS;
1237 }
1238 }
1239 }
1240 if (cp) {
1241 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1242
1243 rtm->rtm_version = RTM_XVERSION;
1244 rtm->rtm_type = type;
1245 rtm->rtm_msglen = len;
1246 }
1247 if (lenp)
1248 *lenp = len;
1249 return 0;
1250 }
1251
1252 #ifndef COMPAT_RTSOCK
1253 int
1254 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1255 int *lenp)
1256 {
1257 return rt_msg2(type, rtinfo, cpv, w, lenp);
1258 }
1259 #endif
1260
1261 /*
1262 * This routine is called to generate a message from the routing
1263 * socket indicating that a redirect has occurred, a routing lookup
1264 * has failed, or that a protocol has detected timeouts to a particular
1265 * destination.
1266 */
1267 void
1268 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1269 int error)
1270 {
1271 struct rt_xmsghdr rtm;
1272 struct mbuf *m;
1273 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1274 struct rt_addrinfo info = *rtinfo;
1275
1276 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1277 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1278 return;
1279 memset(&rtm, 0, sizeof(rtm));
1280 rtm.rtm_pid = curproc->p_pid;
1281 rtm.rtm_flags = RTF_DONE | flags;
1282 rtm.rtm_errno = error;
1283 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1284 if (m == NULL)
1285 return;
1286 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1287 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1288 }
1289
1290 /*
1291 * This routine is called to generate a message from the routing
1292 * socket indicating that the status of a network interface has changed.
1293 */
1294 void
1295 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1296 {
1297 struct if_xmsghdr ifm;
1298 struct mbuf *m;
1299 struct rt_addrinfo info;
1300
1301 COMPATCALL(rt_ifmsg, (ifp));
1302 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1303 return;
1304 (void)memset(&info, 0, sizeof(info));
1305 (void)memset(&ifm, 0, sizeof(ifm));
1306 ifm.ifm_index = ifp->if_index;
1307 ifm.ifm_flags = ifp->if_flags;
1308 ifm.ifm_data = ifp->if_data;
1309 ifm.ifm_addrs = 0;
1310 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1311 if (m == NULL)
1312 return;
1313 COMPATNAME(route_enqueue)(m, 0);
1314 #ifdef COMPAT_14
1315 compat_14_rt_oifmsg(ifp);
1316 #endif
1317 #ifdef COMPAT_50
1318 compat_50_rt_oifmsg(ifp);
1319 #endif
1320 }
1321
1322 #ifndef COMPAT_RTSOCK
1323 static int
1324 if_addrflags(struct ifaddr *ifa)
1325 {
1326
1327 switch (ifa->ifa_addr->sa_family) {
1328 #ifdef INET
1329 case AF_INET:
1330 return ((struct in_ifaddr *)ifa)->ia4_flags;
1331 #endif
1332 #ifdef INET6
1333 case AF_INET6:
1334 return ((struct in6_ifaddr *)ifa)->ia6_flags;
1335 #endif
1336 default:
1337 return 0;
1338 }
1339 }
1340 #endif
1341
1342 /*
1343 * This is called to generate messages from the routing socket
1344 * indicating a network interface has had addresses associated with it.
1345 * if we ever reverse the logic and replace messages TO the routing
1346 * socket indicate a request to configure interfaces, then it will
1347 * be unnecessary as the routing socket will automatically generate
1348 * copies of it.
1349 */
1350 void
1351 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1352 struct rtentry *rt)
1353 {
1354 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
1355 struct rt_addrinfo info;
1356 const struct sockaddr *sa;
1357 int pass;
1358 struct mbuf *m;
1359 struct ifnet *ifp;
1360 struct rt_xmsghdr rtm;
1361 struct ifa_xmsghdr ifam;
1362 int ncmd;
1363
1364 KASSERT(ifa != NULL);
1365 KASSERT(ifa->ifa_addr != NULL);
1366 ifp = ifa->ifa_ifp;
1367 #ifdef SCTP
1368 if (cmd == RTM_ADD) {
1369 sctp_add_ip_address(ifa);
1370 } else if (cmd == RTM_DELETE) {
1371 sctp_delete_ip_address(ifa);
1372 }
1373 #endif
1374
1375 COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1376 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1377 return;
1378 for (pass = 1; pass < 3; pass++) {
1379 memset(&info, 0, sizeof(info));
1380 switch (cmdpass(cmd, pass)) {
1381 case cmdpass(RTM_ADD, 1):
1382 case cmdpass(RTM_CHANGE, 1):
1383 case cmdpass(RTM_DELETE, 2):
1384 case cmdpass(RTM_NEWADDR, 1):
1385 case cmdpass(RTM_DELADDR, 1):
1386 case cmdpass(RTM_CHGADDR, 1):
1387 switch (cmd) {
1388 case RTM_ADD:
1389 ncmd = RTM_XNEWADDR;
1390 break;
1391 case RTM_DELETE:
1392 ncmd = RTM_XDELADDR;
1393 break;
1394 case RTM_CHANGE:
1395 ncmd = RTM_XCHGADDR;
1396 break;
1397 case RTM_NEWADDR:
1398 ncmd = RTM_XNEWADDR;
1399 break;
1400 case RTM_DELADDR:
1401 ncmd = RTM_XDELADDR;
1402 break;
1403 case RTM_CHGADDR:
1404 ncmd = RTM_XCHGADDR;
1405 break;
1406 default:
1407 panic("%s: unknown command %d", __func__, cmd);
1408 }
1409 #ifdef COMPAT_70
1410 compat_70_rt_newaddrmsg1(ncmd, ifa);
1411 #endif
1412 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1413 KASSERT(ifp->if_dl != NULL);
1414 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1415 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1416 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1417 memset(&ifam, 0, sizeof(ifam));
1418 ifam.ifam_index = ifp->if_index;
1419 ifam.ifam_metric = ifa->ifa_metric;
1420 ifam.ifam_flags = ifa->ifa_flags;
1421 #ifndef COMPAT_RTSOCK
1422 ifam.ifam_pid = curproc->p_pid;
1423 ifam.ifam_addrflags = if_addrflags(ifa);
1424 #endif
1425 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1426 if (m == NULL)
1427 continue;
1428 mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1429 info.rti_addrs;
1430 break;
1431 case cmdpass(RTM_ADD, 2):
1432 case cmdpass(RTM_CHANGE, 2):
1433 case cmdpass(RTM_DELETE, 1):
1434 if (rt == NULL)
1435 continue;
1436 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1437 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1438 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1439 memset(&rtm, 0, sizeof(rtm));
1440 rtm.rtm_pid = curproc->p_pid;
1441 rtm.rtm_index = ifp->if_index;
1442 rtm.rtm_flags |= rt->rt_flags;
1443 rtm.rtm_errno = error;
1444 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1445 if (m == NULL)
1446 continue;
1447 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1448 break;
1449 default:
1450 continue;
1451 }
1452 KASSERTMSG(m != NULL, "called with wrong command");
1453 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1454 }
1455 #undef cmdpass
1456
1457 }
1458
1459 static struct mbuf *
1460 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1461 struct rt_addrinfo *info)
1462 {
1463 struct if_xannouncemsghdr ifan;
1464
1465 memset(info, 0, sizeof(*info));
1466 memset(&ifan, 0, sizeof(ifan));
1467 ifan.ifan_index = ifp->if_index;
1468 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1469 ifan.ifan_what = what;
1470 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1471 }
1472
1473 /*
1474 * This is called to generate routing socket messages indicating
1475 * network interface arrival and departure.
1476 */
1477 void
1478 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1479 {
1480 struct mbuf *m;
1481 struct rt_addrinfo info;
1482
1483 COMPATCALL(rt_ifannouncemsg, (ifp, what));
1484 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1485 return;
1486 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1487 if (m == NULL)
1488 return;
1489 COMPATNAME(route_enqueue)(m, 0);
1490 }
1491
1492 /*
1493 * This is called to generate routing socket messages indicating
1494 * IEEE80211 wireless events.
1495 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1496 */
1497 void
1498 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1499 size_t data_len)
1500 {
1501 struct mbuf *m;
1502 struct rt_addrinfo info;
1503
1504 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1505 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1506 return;
1507 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1508 if (m == NULL)
1509 return;
1510 /*
1511 * Append the ieee80211 data. Try to stick it in the
1512 * mbuf containing the ifannounce msg; otherwise allocate
1513 * a new mbuf and append.
1514 *
1515 * NB: we assume m is a single mbuf.
1516 */
1517 if (data_len > M_TRAILINGSPACE(m)) {
1518 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1519 if (n == NULL) {
1520 m_freem(m);
1521 return;
1522 }
1523 (void)memcpy(mtod(n, void *), data, data_len);
1524 n->m_len = data_len;
1525 m->m_next = n;
1526 } else if (data_len > 0) {
1527 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1528 m->m_len += data_len;
1529 }
1530 if (m->m_flags & M_PKTHDR)
1531 m->m_pkthdr.len += data_len;
1532 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1533 COMPATNAME(route_enqueue)(m, 0);
1534 }
1535
1536 /*
1537 * This is used in dumping the kernel table via sysctl().
1538 */
1539 static int
1540 sysctl_dumpentry(struct rtentry *rt, void *v)
1541 {
1542 struct rt_walkarg *w = v;
1543 int error = 0, size;
1544 struct rt_addrinfo info;
1545
1546 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1547 return 0;
1548 memset(&info, 0, sizeof(info));
1549 info.rti_info[RTAX_DST] = rt_getkey(rt);
1550 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1551 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1552 info.rti_info[RTAX_TAG] = rt_gettag(rt);
1553 if (rt->rt_ifp) {
1554 const struct ifaddr *rtifa;
1555 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1556 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
1557 * then rt_get_ifa() != NULL. So this ought to still be safe.
1558 * --dyoung
1559 */
1560 rtifa = rt_get_ifa(rt);
1561 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1562 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1563 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1564 }
1565 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1566 return error;
1567 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1568 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1569
1570 rtm->rtm_flags = rt->rt_flags;
1571 rtm->rtm_use = rt->rt_use;
1572 rtm_setmetrics(rt, rtm);
1573 KASSERT(rt->rt_ifp != NULL);
1574 rtm->rtm_index = rt->rt_ifp->if_index;
1575 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1576 rtm->rtm_addrs = info.rti_addrs;
1577 if ((error = copyout(rtm, w->w_where, size)) != 0)
1578 w->w_where = NULL;
1579 else
1580 w->w_where = (char *)w->w_where + size;
1581 }
1582 return error;
1583 }
1584
1585 static int
1586 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
1587 struct rt_addrinfo *info, size_t len)
1588 {
1589 struct if_xmsghdr *ifm;
1590 int error;
1591
1592 ifm = (struct if_xmsghdr *)w->w_tmem;
1593 ifm->ifm_index = ifp->if_index;
1594 ifm->ifm_flags = ifp->if_flags;
1595 ifm->ifm_data = ifp->if_data;
1596 ifm->ifm_addrs = info->rti_addrs;
1597 if ((error = copyout(ifm, w->w_where, len)) == 0)
1598 w->w_where = (char *)w->w_where + len;
1599 return error;
1600 }
1601
1602 static int
1603 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
1604 struct rt_addrinfo *info)
1605 {
1606 int len, error;
1607
1608 if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
1609 return error;
1610 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1611 struct ifa_xmsghdr *ifam;
1612
1613 ifam = (struct ifa_xmsghdr *)w->w_tmem;
1614 ifam->ifam_index = ifa->ifa_ifp->if_index;
1615 ifam->ifam_flags = ifa->ifa_flags;
1616 ifam->ifam_metric = ifa->ifa_metric;
1617 ifam->ifam_addrs = info->rti_addrs;
1618 #ifndef COMPAT_RTSOCK
1619 ifam->ifam_pid = 0;
1620 ifam->ifam_addrflags = if_addrflags(ifa);
1621 #endif
1622 if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
1623 w->w_where = (char *)w->w_where + len;
1624 }
1625 return error;
1626 }
1627
1628 static int
1629 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1630 {
1631 struct ifnet *ifp;
1632 struct ifaddr *ifa;
1633 struct rt_addrinfo info;
1634 int cmd, len, error = 0;
1635 int (*iflist_if)(struct ifnet *, struct rt_walkarg *,
1636 struct rt_addrinfo *, size_t);
1637 int (*iflist_addr)(struct rt_walkarg *, struct ifaddr *,
1638 struct rt_addrinfo *);
1639 int s;
1640 struct psref psref;
1641 int bound;
1642
1643 switch (type) {
1644 case NET_RT_IFLIST:
1645 cmd = RTM_IFINFO;
1646 iflist_if = sysctl_iflist_if;
1647 iflist_addr = sysctl_iflist_addr;
1648 break;
1649 #ifdef COMPAT_14
1650 case NET_RT_OOOIFLIST:
1651 cmd = RTM_OOIFINFO;
1652 iflist_if = compat_14_iflist;
1653 iflist_addr = compat_70_iflist_addr;
1654 break;
1655 #endif
1656 #ifdef COMPAT_50
1657 case NET_RT_OOIFLIST:
1658 cmd = RTM_OIFINFO;
1659 iflist_if = compat_50_iflist;
1660 iflist_addr = compat_70_iflist_addr;
1661 break;
1662 #endif
1663 #ifdef COMPAT_70
1664 case NET_RT_OIFLIST:
1665 cmd = RTM_IFINFO;
1666 iflist_if = sysctl_iflist_if;
1667 iflist_addr = compat_70_iflist_addr;
1668 break;
1669 #endif
1670 default:
1671 #ifdef RTSOCK_DEBUG
1672 printf("%s: unsupported IFLIST type %d\n", __func__, type);
1673 #endif
1674 return EINVAL;
1675 }
1676
1677 memset(&info, 0, sizeof(info));
1678
1679 bound = curlwp_bind();
1680 s = pserialize_read_enter();
1681 IFNET_READER_FOREACH(ifp) {
1682 int _s;
1683 if (w->w_arg && w->w_arg != ifp->if_index)
1684 continue;
1685 if (IFADDR_READER_EMPTY(ifp))
1686 continue;
1687
1688 if_acquire(ifp, &psref);
1689 pserialize_read_exit(s);
1690
1691 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1692 if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
1693 goto release_exit;
1694 info.rti_info[RTAX_IFP] = NULL;
1695 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1696 if ((error = iflist_if(ifp, w, &info, len)) != 0)
1697 goto release_exit;
1698 }
1699 _s = pserialize_read_enter();
1700 IFADDR_READER_FOREACH(ifa, ifp) {
1701 struct psref _psref;
1702 if (af && af != ifa->ifa_addr->sa_family)
1703 continue;
1704 ifa_acquire(ifa, &_psref);
1705 pserialize_read_exit(_s);
1706
1707 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1708 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1709 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1710 error = iflist_addr(w, ifa, &info);
1711
1712 _s = pserialize_read_enter();
1713 ifa_release(ifa, &_psref);
1714 if (error != 0) {
1715 pserialize_read_exit(_s);
1716 goto release_exit;
1717 }
1718 }
1719 pserialize_read_exit(_s);
1720 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1721 info.rti_info[RTAX_BRD] = NULL;
1722
1723 s = pserialize_read_enter();
1724 if_release(ifp, &psref);
1725 }
1726 pserialize_read_exit(s);
1727 curlwp_bindx(bound);
1728
1729 return 0;
1730
1731 release_exit:
1732 if_release(ifp, &psref);
1733 curlwp_bindx(bound);
1734 return error;
1735 }
1736
1737 static int
1738 sysctl_rtable(SYSCTLFN_ARGS)
1739 {
1740 void *where = oldp;
1741 size_t *given = oldlenp;
1742 int i, s, error = EINVAL;
1743 u_char af;
1744 struct rt_walkarg w;
1745
1746 if (namelen == 1 && name[0] == CTL_QUERY)
1747 return sysctl_query(SYSCTLFN_CALL(rnode));
1748
1749 if (newp)
1750 return EPERM;
1751 if (namelen != 3)
1752 return EINVAL;
1753 af = name[0];
1754 w.w_tmemneeded = 0;
1755 w.w_tmemsize = 0;
1756 w.w_tmem = NULL;
1757 again:
1758 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1759 if (w.w_tmemneeded) {
1760 w.w_tmem = kmem_alloc(w.w_tmemneeded, KM_SLEEP);
1761 w.w_tmemsize = w.w_tmemneeded;
1762 w.w_tmemneeded = 0;
1763 }
1764 w.w_op = name[1];
1765 w.w_arg = name[2];
1766 w.w_given = *given;
1767 w.w_needed = 0 - w.w_given;
1768 w.w_where = where;
1769
1770 s = splsoftnet();
1771 switch (w.w_op) {
1772
1773 case NET_RT_DUMP:
1774 case NET_RT_FLAGS:
1775 #ifdef INET
1776 /*
1777 * take care of llinfo entries, the caller must
1778 * specify an AF
1779 */
1780 if (w.w_op == NET_RT_FLAGS &&
1781 (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
1782 if (af != 0)
1783 error = lltable_sysctl_dumparp(af, &w);
1784 else
1785 error = EINVAL;
1786 break;
1787 }
1788 #endif /* INET */
1789
1790 for (i = 1; i <= AF_MAX; i++)
1791 if ((af == 0 || af == i) &&
1792 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1793 break;
1794 break;
1795
1796 #ifdef COMPAT_14
1797 case NET_RT_OOOIFLIST:
1798 error = sysctl_iflist(af, &w, w.w_op);
1799 break;
1800 #endif
1801 #ifdef COMPAT_50
1802 case NET_RT_OOIFLIST:
1803 error = sysctl_iflist(af, &w, w.w_op);
1804 break;
1805 #endif
1806 #ifdef COMPAT_70
1807 case NET_RT_OIFLIST:
1808 error = sysctl_iflist(af, &w, w.w_op);
1809 break;
1810 #endif
1811 case NET_RT_IFLIST:
1812 error = sysctl_iflist(af, &w, w.w_op);
1813 break;
1814 }
1815 splx(s);
1816
1817 /* check to see if we couldn't allocate memory with NOWAIT */
1818 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1819 goto again;
1820
1821 if (w.w_tmem)
1822 kmem_free(w.w_tmem, w.w_tmemsize);
1823 w.w_needed += w.w_given;
1824 if (where) {
1825 *given = (char *)w.w_where - (char *)where;
1826 if (*given < w.w_needed)
1827 return ENOMEM;
1828 } else {
1829 *given = (11 * w.w_needed) / 10;
1830 }
1831 return error;
1832 }
1833
1834 /*
1835 * Routing message software interrupt routine
1836 */
1837 static void
1838 COMPATNAME(route_intr)(void *cookie)
1839 {
1840 struct sockproto proto = { .sp_family = PF_XROUTE, };
1841 struct route_info * const ri = &COMPATNAME(route_info);
1842 struct mbuf *m;
1843
1844 mutex_enter(softnet_lock);
1845 KERNEL_LOCK(1, NULL);
1846 for (;;) {
1847 IFQ_LOCK(&ri->ri_intrq);
1848 IF_DEQUEUE(&ri->ri_intrq, m);
1849 IFQ_UNLOCK(&ri->ri_intrq);
1850 if (m == NULL)
1851 break;
1852 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1853 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
1854 }
1855 KERNEL_UNLOCK_ONE(NULL);
1856 mutex_exit(softnet_lock);
1857 }
1858
1859 /*
1860 * Enqueue a message to the software interrupt routine.
1861 */
1862 void
1863 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1864 {
1865 struct route_info * const ri = &COMPATNAME(route_info);
1866 int wasempty;
1867
1868 IFQ_LOCK(&ri->ri_intrq);
1869 if (IF_QFULL(&ri->ri_intrq)) {
1870 IF_DROP(&ri->ri_intrq);
1871 IFQ_UNLOCK(&ri->ri_intrq);
1872 m_freem(m);
1873 } else {
1874 wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1875 M_SETCTX(m, (uintptr_t)family);
1876 IF_ENQUEUE(&ri->ri_intrq, m);
1877 IFQ_UNLOCK(&ri->ri_intrq);
1878 if (wasempty) {
1879 kpreempt_disable();
1880 softint_schedule(ri->ri_sih);
1881 kpreempt_enable();
1882 }
1883 }
1884 }
1885
1886 static void
1887 COMPATNAME(route_init)(void)
1888 {
1889 struct route_info * const ri = &COMPATNAME(route_info);
1890
1891 #ifndef COMPAT_RTSOCK
1892 rt_init();
1893 #endif
1894
1895 sysctl_net_route_setup(NULL);
1896 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1897 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1898 COMPATNAME(route_intr), NULL);
1899 IFQ_LOCK_INIT(&ri->ri_intrq);
1900 }
1901
1902 /*
1903 * Definitions of protocols supported in the ROUTE domain.
1904 */
1905 #ifndef COMPAT_RTSOCK
1906 PR_WRAP_USRREQS(route);
1907 #else
1908 PR_WRAP_USRREQS(compat_50_route);
1909 #endif
1910
1911 static const struct pr_usrreqs route_usrreqs = {
1912 .pr_attach = COMPATNAME(route_attach_wrapper),
1913 .pr_detach = COMPATNAME(route_detach_wrapper),
1914 .pr_accept = COMPATNAME(route_accept_wrapper),
1915 .pr_bind = COMPATNAME(route_bind_wrapper),
1916 .pr_listen = COMPATNAME(route_listen_wrapper),
1917 .pr_connect = COMPATNAME(route_connect_wrapper),
1918 .pr_connect2 = COMPATNAME(route_connect2_wrapper),
1919 .pr_disconnect = COMPATNAME(route_disconnect_wrapper),
1920 .pr_shutdown = COMPATNAME(route_shutdown_wrapper),
1921 .pr_abort = COMPATNAME(route_abort_wrapper),
1922 .pr_ioctl = COMPATNAME(route_ioctl_wrapper),
1923 .pr_stat = COMPATNAME(route_stat_wrapper),
1924 .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper),
1925 .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper),
1926 .pr_rcvd = COMPATNAME(route_rcvd_wrapper),
1927 .pr_recvoob = COMPATNAME(route_recvoob_wrapper),
1928 .pr_send = COMPATNAME(route_send_wrapper),
1929 .pr_sendoob = COMPATNAME(route_sendoob_wrapper),
1930 .pr_purgeif = COMPATNAME(route_purgeif_wrapper),
1931 };
1932
1933 static const struct protosw COMPATNAME(route_protosw)[] = {
1934 {
1935 .pr_type = SOCK_RAW,
1936 .pr_domain = &COMPATNAME(routedomain),
1937 .pr_flags = PR_ATOMIC|PR_ADDR,
1938 .pr_input = raw_input,
1939 .pr_ctlinput = raw_ctlinput,
1940 .pr_usrreqs = &route_usrreqs,
1941 .pr_init = raw_init,
1942 },
1943 };
1944
1945 struct domain COMPATNAME(routedomain) = {
1946 .dom_family = PF_XROUTE,
1947 .dom_name = DOMAINNAME,
1948 .dom_init = COMPATNAME(route_init),
1949 .dom_protosw = COMPATNAME(route_protosw),
1950 .dom_protoswNPROTOSW =
1951 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1952 };
1953
1954 static void
1955 sysctl_net_route_setup(struct sysctllog **clog)
1956 {
1957 const struct sysctlnode *rnode = NULL;
1958
1959 sysctl_createv(clog, 0, NULL, &rnode,
1960 CTLFLAG_PERMANENT,
1961 CTLTYPE_NODE, DOMAINNAME,
1962 SYSCTL_DESCR("PF_ROUTE information"),
1963 NULL, 0, NULL, 0,
1964 CTL_NET, PF_XROUTE, CTL_EOL);
1965
1966 sysctl_createv(clog, 0, NULL, NULL,
1967 CTLFLAG_PERMANENT,
1968 CTLTYPE_NODE, "rtable",
1969 SYSCTL_DESCR("Routing table information"),
1970 sysctl_rtable, 0, NULL, 0,
1971 CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
1972
1973 sysctl_createv(clog, 0, &rnode, NULL,
1974 CTLFLAG_PERMANENT,
1975 CTLTYPE_STRUCT, "stats",
1976 SYSCTL_DESCR("Routing statistics"),
1977 NULL, 0, &rtstat, sizeof(rtstat),
1978 CTL_CREATE, CTL_EOL);
1979 }
1980