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