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