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