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