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