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