rtsock.c revision 1.220 1 /* $NetBSD: rtsock.c,v 1.220 2017/06/26 03:13:40 ozaki-r Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.220 2017/06/26 03:13:40 ozaki-r Exp $");
65
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_mpls.h"
69 #include "opt_compat_netbsd.h"
70 #include "opt_sctp.h"
71 #include "opt_net_mpsafe.h"
72 #endif
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/sysctl.h>
82 #include <sys/kauth.h>
83 #include <sys/kmem.h>
84 #include <sys/intr.h>
85
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 #ifdef INET
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 /* INET */
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
740 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
741 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
742 (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
743 error = ENOBUFS;
744 goto out;
745 }
746 if ((m->m_flags & M_PKTHDR) == 0)
747 panic("%s", __func__);
748 len = m->m_pkthdr.len;
749 if (len < sizeof(*rtm) ||
750 len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
751 info.rti_info[RTAX_DST] = NULL;
752 senderr(EINVAL);
753 }
754 R_Malloc(rtm, struct rt_xmsghdr *, len);
755 if (rtm == NULL) {
756 info.rti_info[RTAX_DST] = NULL;
757 senderr(ENOBUFS);
758 }
759 m_copydata(m, 0, len, rtm);
760 if (rtm->rtm_version != RTM_XVERSION) {
761 info.rti_info[RTAX_DST] = NULL;
762 senderr(EPROTONOSUPPORT);
763 }
764 rtm->rtm_pid = curproc->p_pid;
765 memset(&info, 0, sizeof(info));
766 info.rti_addrs = rtm->rtm_addrs;
767 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
768 &info)) {
769 senderr(EINVAL);
770 }
771 info.rti_flags = rtm->rtm_flags;
772 #ifdef RTSOCK_DEBUG
773 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
774 char abuf[INET_ADDRSTRLEN];
775 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
776 RT_IN_PRINT(&info, abuf, RTAX_DST));
777 }
778 #endif /* RTSOCK_DEBUG */
779 if (info.rti_info[RTAX_DST] == NULL ||
780 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
781 senderr(EINVAL);
782 }
783 if (info.rti_info[RTAX_GATEWAY] != NULL &&
784 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
785 senderr(EINVAL);
786 }
787
788 /*
789 * Verify that the caller has the appropriate privilege; RTM_GET
790 * is the only operation the non-superuser is allowed.
791 */
792 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
793 0, rtm, NULL, NULL) != 0)
794 senderr(EACCES);
795
796 switch (rtm->rtm_type) {
797
798 case RTM_ADD:
799 if (info.rti_info[RTAX_GATEWAY] == NULL) {
800 senderr(EINVAL);
801 }
802 #ifdef INET
803 /* support for new ARP code with keeping backcompat */
804 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
805 const struct sockaddr_dl *sdlp =
806 satocsdl(info.rti_info[RTAX_GATEWAY]);
807
808 /* Allow routing requests by interface index */
809 if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
810 && sdlp->sdl_slen == 0)
811 goto fallback;
812 /*
813 * Old arp binaries don't set the sdl_index
814 * so we have to complement it.
815 */
816 int sdl_index = sdlp->sdl_index;
817 if (sdl_index == 0) {
818 error = route_get_sdl_index(&info, &sdl_index);
819 if (error != 0)
820 goto fallback;
821 } else if (
822 info.rti_info[RTAX_DST]->sa_family == AF_INET) {
823 /*
824 * XXX workaround for SIN_PROXY case; proxy arp
825 * entry should be in an interface that has
826 * a network route including the destination,
827 * not a local (link) route that may not be a
828 * desired place, for example a tap.
829 */
830 const struct sockaddr_inarp *sina =
831 (const struct sockaddr_inarp *)
832 info.rti_info[RTAX_DST];
833 if (sina->sin_other & SIN_PROXY) {
834 error = route_get_sdl_index(&info,
835 &sdl_index);
836 if (error != 0)
837 goto fallback;
838 }
839 }
840 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
841 rtm->rtm_rmx.rmx_expire, &info, sdl_index);
842 break;
843 }
844 fallback:
845 #endif /* INET */
846 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
847 if (error == 0) {
848 rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
849 rt_unref(saved_nrt);
850 }
851 break;
852
853 case RTM_DELETE:
854 #ifdef INET
855 /* support for new ARP code */
856 if (info.rti_info[RTAX_GATEWAY] &&
857 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
858 (rtm->rtm_flags & RTF_LLDATA) != 0) {
859 const struct sockaddr_dl *sdlp =
860 satocsdl(info.rti_info[RTAX_GATEWAY]);
861 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
862 rtm->rtm_rmx.rmx_expire, &info, sdlp->sdl_index);
863 break;
864 }
865 #endif /* INET */
866 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
867 if (error != 0)
868 break;
869
870 rt = saved_nrt;
871 do_rt_free = true;
872 info.rti_info[RTAX_DST] = rt_getkey(rt);
873 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
874 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
875 info.rti_info[RTAX_TAG] = rt_gettag(rt);
876 error = route_output_report(rt, &info, rtm, &new_rtm);
877 if (error)
878 senderr(error);
879 if (new_rtm != NULL) {
880 old_rtm = rtm;
881 rtm = new_rtm;
882 }
883 break;
884
885 case RTM_GET:
886 case RTM_CHANGE:
887 case RTM_LOCK:
888 /* XXX This will mask info.rti_info[RTAX_DST] with
889 * info.rti_info[RTAX_NETMASK] before
890 * searching. It did not used to do that. --dyoung
891 */
892 rt = NULL;
893 error = rtrequest1(RTM_GET, &info, &rt);
894 if (error != 0)
895 senderr(error);
896 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
897 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
898 info.rti_info[RTAX_DST]->sa_len) != 0)
899 senderr(ESRCH);
900 if (info.rti_info[RTAX_NETMASK] == NULL &&
901 rt_mask(rt) != NULL)
902 senderr(ETOOMANYREFS);
903 }
904
905 /*
906 * XXX if arp/ndp requests an L2 entry, we have to obtain
907 * it from lltable while for the route command we have to
908 * return a route as it is. How to distinguish them?
909 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
910 * indicates an L2 entry is requested. For old arp/ndp
911 * binaries, we check RTF_UP flag is NOT set; it works
912 * by the fact that arp/ndp don't set it while the route
913 * command sets it.
914 */
915 if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
916 (rtm->rtm_flags & RTF_UP) == 0) &&
917 rtm->rtm_type == RTM_GET &&
918 sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
919 int ll_flags = 0;
920 route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
921 &ll_flags);
922 info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
923 error = route_output_report(rt, &info, rtm, &new_rtm);
924 if (error)
925 senderr(error);
926 if (new_rtm != NULL) {
927 old_rtm = rtm;
928 rtm = new_rtm;
929 }
930 rtm->rtm_flags |= RTF_LLDATA;
931 rtm->rtm_flags &= ~RTF_CONNECTED;
932 rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
933 break;
934 }
935
936 switch (rtm->rtm_type) {
937 case RTM_GET:
938 info.rti_info[RTAX_DST] = rt_getkey(rt);
939 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
940 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
941 info.rti_info[RTAX_TAG] = rt_gettag(rt);
942 error = route_output_report(rt, &info, rtm, &new_rtm);
943 if (error)
944 senderr(error);
945 if (new_rtm != NULL) {
946 old_rtm = rtm;
947 rtm = new_rtm;
948 }
949 break;
950
951 case RTM_CHANGE:
952 #ifdef NET_MPSAFE
953 error = rt_update_prepare(rt);
954 if (error == 0) {
955 error = route_output_change(rt, &info, rtm);
956 rt_update_finish(rt);
957 }
958 #else
959 error = route_output_change(rt, &info, rtm);
960 #endif
961 if (error != 0)
962 goto flush;
963 /*FALLTHROUGH*/
964 case RTM_LOCK:
965 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
966 rt->rt_rmx.rmx_locks |=
967 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
968 break;
969 }
970 break;
971
972 default:
973 senderr(EOPNOTSUPP);
974 }
975
976 flush:
977 if (rtm) {
978 if (error)
979 rtm->rtm_errno = error;
980 else
981 rtm->rtm_flags |= RTF_DONE;
982 }
983 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
984 0;
985 /* We cannot free old_rtm until we have stopped using the
986 * pointers in info, some of which may point to sockaddrs
987 * in old_rtm.
988 */
989 if (old_rtm != NULL)
990 Free(old_rtm);
991 if (rt) {
992 if (do_rt_free)
993 rt_free(rt);
994 else
995 rt_unref(rt);
996 }
997 {
998 struct rawcb *rp = NULL;
999 /*
1000 * Check to see if we don't want our own messages.
1001 */
1002 if ((so->so_options & SO_USELOOPBACK) == 0) {
1003 if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
1004 if (rtm)
1005 Free(rtm);
1006 m_freem(m);
1007 goto out;
1008 }
1009 /* There is another listener, so construct message */
1010 rp = sotorawcb(so);
1011 }
1012 if (rtm) {
1013 m_copyback(m, 0, rtm->rtm_msglen, rtm);
1014 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1015 m_freem(m);
1016 m = NULL;
1017 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1018 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1019 Free(rtm);
1020 }
1021 if (rp)
1022 rp->rcb_proto.sp_family = 0; /* Avoid us */
1023 if (family)
1024 proto.sp_protocol = family;
1025 if (m)
1026 raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
1027 &COMPATNAME(route_info).ri_dst);
1028 if (rp)
1029 rp->rcb_proto.sp_family = PF_XROUTE;
1030 }
1031 out:
1032 curlwp_bindx(bound);
1033 return error;
1034 }
1035
1036 static int
1037 route_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1038 {
1039 struct routecb *rop = sotoroutecb(so);
1040 int error = 0;
1041 unsigned char *rtm_type;
1042 size_t len;
1043 unsigned int msgfilter;
1044
1045 KASSERT(solocked(so));
1046
1047 if (sopt->sopt_level != AF_ROUTE) {
1048 error = ENOPROTOOPT;
1049 } else switch (op) {
1050 case PRCO_SETOPT:
1051 switch (sopt->sopt_name) {
1052 case RO_MSGFILTER:
1053 msgfilter = 0;
1054 for (rtm_type = sopt->sopt_data, len = sopt->sopt_size;
1055 len != 0;
1056 rtm_type++, len -= sizeof(*rtm_type))
1057 {
1058 /* Guard against overflowing our storage. */
1059 if (*rtm_type >= sizeof(msgfilter) * CHAR_BIT) {
1060 error = EOVERFLOW;
1061 break;
1062 }
1063 msgfilter |= RTMSGFILTER(*rtm_type);
1064 }
1065 if (error == 0)
1066 rop->rocb_msgfilter = msgfilter;
1067 break;
1068 default:
1069 error = ENOPROTOOPT;
1070 break;
1071 }
1072 break;
1073 case PRCO_GETOPT:
1074 switch (sopt->sopt_name) {
1075 case RO_MSGFILTER:
1076 error = ENOTSUP;
1077 break;
1078 default:
1079 error = ENOPROTOOPT;
1080 break;
1081 }
1082 }
1083 return error;
1084 }
1085
1086 static void
1087 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
1088 {
1089 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
1090 metric(RTV_RPIPE, rmx_recvpipe);
1091 metric(RTV_SPIPE, rmx_sendpipe);
1092 metric(RTV_SSTHRESH, rmx_ssthresh);
1093 metric(RTV_RTT, rmx_rtt);
1094 metric(RTV_RTTVAR, rmx_rttvar);
1095 metric(RTV_HOPCOUNT, rmx_hopcount);
1096 metric(RTV_MTU, rmx_mtu);
1097 #undef metric
1098 if (which & RTV_EXPIRE) {
1099 out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
1100 time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
1101 }
1102 }
1103
1104 static void
1105 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
1106 {
1107 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
1108 metric(rmx_recvpipe);
1109 metric(rmx_sendpipe);
1110 metric(rmx_ssthresh);
1111 metric(rmx_rtt);
1112 metric(rmx_rttvar);
1113 metric(rmx_hopcount);
1114 metric(rmx_mtu);
1115 metric(rmx_locks);
1116 #undef metric
1117 out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
1118 time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
1119 }
1120
1121 static int
1122 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
1123 struct rt_addrinfo *rtinfo)
1124 {
1125 const struct sockaddr *sa = NULL; /* Quell compiler warning */
1126 int i;
1127
1128 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1129 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1130 continue;
1131 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
1132 RT_XADVANCE(cp, sa);
1133 }
1134
1135 /*
1136 * Check for extra addresses specified, except RTM_GET asking
1137 * for interface info.
1138 */
1139 if (rtmtype == RTM_GET) {
1140 if (((rtinfo->rti_addrs &
1141 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
1142 return 1;
1143 } else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
1144 return 1;
1145 /* Check for bad data length. */
1146 if (cp != cplim) {
1147 if (i == RTAX_NETMASK + 1 && sa != NULL &&
1148 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
1149 /*
1150 * The last sockaddr was info.rti_info[RTAX_NETMASK].
1151 * We accept this for now for the sake of old
1152 * binaries or third party softwares.
1153 */
1154 ;
1155 else
1156 return 1;
1157 }
1158 return 0;
1159 }
1160
1161 static int
1162 rt_getlen(int type)
1163 {
1164 #ifndef COMPAT_RTSOCK
1165 CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
1166 CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
1167 CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
1168 CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
1169 #endif
1170
1171 switch (type) {
1172 case RTM_ODELADDR:
1173 case RTM_ONEWADDR:
1174 case RTM_OCHGADDR:
1175 #ifdef COMPAT_70
1176 return sizeof(struct ifa_msghdr70);
1177 #else
1178 #ifdef RTSOCK_DEBUG
1179 printf("%s: unsupported RTM type %d\n", __func__, type);
1180 #endif
1181 return -1;
1182 #endif
1183 case RTM_DELADDR:
1184 case RTM_NEWADDR:
1185 case RTM_CHGADDR:
1186 return sizeof(struct ifa_xmsghdr);
1187
1188 case RTM_OOIFINFO:
1189 #ifdef COMPAT_14
1190 return sizeof(struct if_msghdr14);
1191 #else
1192 #ifdef RTSOCK_DEBUG
1193 printf("%s: unsupported RTM type RTM_OOIFINFO\n", __func__);
1194 #endif
1195 return -1;
1196 #endif
1197 case RTM_OIFINFO:
1198 #ifdef COMPAT_50
1199 return sizeof(struct if_msghdr50);
1200 #else
1201 #ifdef RTSOCK_DEBUG
1202 printf("%s: unsupported RTM type RTM_OIFINFO\n", __func__);
1203 #endif
1204 return -1;
1205 #endif
1206
1207 case RTM_IFINFO:
1208 return sizeof(struct if_xmsghdr);
1209
1210 case RTM_IFANNOUNCE:
1211 case RTM_IEEE80211:
1212 return sizeof(struct if_xannouncemsghdr);
1213
1214 default:
1215 return sizeof(struct rt_xmsghdr);
1216 }
1217 }
1218
1219
1220 struct mbuf *
1221 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
1222 {
1223 struct rt_xmsghdr *rtm;
1224 struct mbuf *m;
1225 int i;
1226 const struct sockaddr *sa;
1227 int len, dlen;
1228
1229 m = m_gethdr(M_DONTWAIT, MT_DATA);
1230 if (m == NULL)
1231 return m;
1232 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
1233
1234 if ((len = rt_getlen(type)) == -1)
1235 goto out;
1236 if (len > MHLEN + MLEN)
1237 panic("%s: message too long", __func__);
1238 else if (len > MHLEN) {
1239 m->m_next = m_get(M_DONTWAIT, MT_DATA);
1240 if (m->m_next == NULL)
1241 goto out;
1242 MCLAIM(m->m_next, m->m_owner);
1243 m->m_pkthdr.len = len;
1244 m->m_len = MHLEN;
1245 m->m_next->m_len = len - MHLEN;
1246 } else {
1247 m->m_pkthdr.len = m->m_len = len;
1248 }
1249 m_reset_rcvif(m);
1250 m_copyback(m, 0, datalen, data);
1251 if (len > datalen)
1252 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1253 rtm = mtod(m, struct rt_xmsghdr *);
1254 for (i = 0; i < RTAX_MAX; i++) {
1255 if ((sa = rtinfo->rti_info[i]) == NULL)
1256 continue;
1257 rtinfo->rti_addrs |= (1 << i);
1258 dlen = RT_XROUNDUP(sa->sa_len);
1259 m_copyback(m, len, sa->sa_len, sa);
1260 if (dlen != sa->sa_len) {
1261 /*
1262 * Up to 6 + 1 nul's since roundup is to
1263 * sizeof(uint64_t) (8 bytes)
1264 */
1265 m_copyback(m, len + sa->sa_len,
1266 dlen - sa->sa_len, "\0\0\0\0\0\0");
1267 }
1268 len += dlen;
1269 }
1270 if (m->m_pkthdr.len != len)
1271 goto out;
1272 rtm->rtm_msglen = len;
1273 rtm->rtm_version = RTM_XVERSION;
1274 rtm->rtm_type = type;
1275 return m;
1276 out:
1277 m_freem(m);
1278 return NULL;
1279 }
1280
1281 /*
1282 * rt_msg2
1283 *
1284 * fills 'cp' or 'w'.w_tmem with the routing socket message and
1285 * returns the length of the message in 'lenp'.
1286 *
1287 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1288 * the message
1289 * otherwise walkarg's w_needed is updated and if the user buffer is
1290 * specified and w_needed indicates space exists the information is copied
1291 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1292 * if the allocation fails ENOBUFS is returned.
1293 */
1294 static int
1295 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1296 int *lenp)
1297 {
1298 int i;
1299 int len, dlen, second_time = 0;
1300 char *cp0, *cp = cpv;
1301
1302 rtinfo->rti_addrs = 0;
1303 again:
1304 if ((len = rt_getlen(type)) == -1)
1305 return EINVAL;
1306
1307 if ((cp0 = cp) != NULL)
1308 cp += len;
1309 for (i = 0; i < RTAX_MAX; i++) {
1310 const struct sockaddr *sa;
1311
1312 if ((sa = rtinfo->rti_info[i]) == NULL)
1313 continue;
1314 rtinfo->rti_addrs |= (1 << i);
1315 dlen = RT_XROUNDUP(sa->sa_len);
1316 if (cp) {
1317 int diff = dlen - sa->sa_len;
1318 (void)memcpy(cp, sa, (size_t)sa->sa_len);
1319 cp += sa->sa_len;
1320 if (diff > 0) {
1321 (void)memset(cp, 0, (size_t)diff);
1322 cp += diff;
1323 }
1324 }
1325 len += dlen;
1326 }
1327 if (cp == NULL && w != NULL && !second_time) {
1328 struct rt_walkarg *rw = w;
1329
1330 rw->w_needed += len;
1331 if (rw->w_needed <= 0 && rw->w_where) {
1332 if (rw->w_tmemsize < len) {
1333 if (rw->w_tmem)
1334 kmem_free(rw->w_tmem, rw->w_tmemsize);
1335 rw->w_tmem = kmem_alloc(len, KM_SLEEP);
1336 rw->w_tmemsize = len;
1337 }
1338 if (rw->w_tmem) {
1339 cp = rw->w_tmem;
1340 second_time = 1;
1341 goto again;
1342 } else {
1343 rw->w_tmemneeded = len;
1344 return ENOBUFS;
1345 }
1346 }
1347 }
1348 if (cp) {
1349 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1350
1351 rtm->rtm_version = RTM_XVERSION;
1352 rtm->rtm_type = type;
1353 rtm->rtm_msglen = len;
1354 }
1355 if (lenp)
1356 *lenp = len;
1357 return 0;
1358 }
1359
1360 #ifndef COMPAT_RTSOCK
1361 int
1362 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1363 int *lenp)
1364 {
1365 return rt_msg2(type, rtinfo, cpv, w, lenp);
1366 }
1367 #endif
1368
1369 /*
1370 * This routine is called to generate a message from the routing
1371 * socket indicating that a redirect has occurred, a routing lookup
1372 * has failed, or that a protocol has detected timeouts to a particular
1373 * destination.
1374 */
1375 void
1376 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1377 int error)
1378 {
1379 struct rt_xmsghdr rtm;
1380 struct mbuf *m;
1381 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1382 struct rt_addrinfo info = *rtinfo;
1383
1384 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1385 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1386 return;
1387 memset(&rtm, 0, sizeof(rtm));
1388 rtm.rtm_pid = curproc->p_pid;
1389 rtm.rtm_flags = RTF_DONE | flags;
1390 rtm.rtm_errno = error;
1391 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1392 if (m == NULL)
1393 return;
1394 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1395 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1396 }
1397
1398 /*
1399 * This routine is called to generate a message from the routing
1400 * socket indicating that the status of a network interface has changed.
1401 */
1402 void
1403 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1404 {
1405 struct if_xmsghdr ifm;
1406 struct mbuf *m;
1407 struct rt_addrinfo info;
1408
1409 COMPATCALL(rt_ifmsg, (ifp));
1410 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1411 return;
1412 (void)memset(&info, 0, sizeof(info));
1413 (void)memset(&ifm, 0, sizeof(ifm));
1414 ifm.ifm_index = ifp->if_index;
1415 ifm.ifm_flags = ifp->if_flags;
1416 ifm.ifm_data = ifp->if_data;
1417 ifm.ifm_addrs = 0;
1418 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1419 if (m == NULL)
1420 return;
1421 COMPATNAME(route_enqueue)(m, 0);
1422 #ifdef COMPAT_14
1423 compat_14_rt_oifmsg(ifp);
1424 #endif
1425 #ifdef COMPAT_50
1426 compat_50_rt_oifmsg(ifp);
1427 #endif
1428 }
1429
1430 #ifndef COMPAT_RTSOCK
1431 static int
1432 if_addrflags(struct ifaddr *ifa)
1433 {
1434
1435 switch (ifa->ifa_addr->sa_family) {
1436 #ifdef INET
1437 case AF_INET:
1438 return ((struct in_ifaddr *)ifa)->ia4_flags;
1439 #endif
1440 #ifdef INET6
1441 case AF_INET6:
1442 return ((struct in6_ifaddr *)ifa)->ia6_flags;
1443 #endif
1444 default:
1445 return 0;
1446 }
1447 }
1448 #endif
1449
1450 /*
1451 * This is called to generate messages from the routing socket
1452 * indicating a network interface has had addresses associated with it.
1453 * if we ever reverse the logic and replace messages TO the routing
1454 * socket indicate a request to configure interfaces, then it will
1455 * be unnecessary as the routing socket will automatically generate
1456 * copies of it.
1457 */
1458 void
1459 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1460 struct rtentry *rt)
1461 {
1462 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
1463 struct rt_addrinfo info;
1464 const struct sockaddr *sa;
1465 int pass;
1466 struct mbuf *m;
1467 struct ifnet *ifp;
1468 struct rt_xmsghdr rtm;
1469 struct ifa_xmsghdr ifam;
1470 int ncmd;
1471
1472 KASSERT(ifa != NULL);
1473 KASSERT(ifa->ifa_addr != NULL);
1474 ifp = ifa->ifa_ifp;
1475 #ifdef SCTP
1476 if (cmd == RTM_ADD) {
1477 sctp_add_ip_address(ifa);
1478 } else if (cmd == RTM_DELETE) {
1479 sctp_delete_ip_address(ifa);
1480 }
1481 #endif
1482
1483 COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1484 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1485 return;
1486 for (pass = 1; pass < 3; pass++) {
1487 memset(&info, 0, sizeof(info));
1488 switch (cmdpass(cmd, pass)) {
1489 case cmdpass(RTM_ADD, 1):
1490 case cmdpass(RTM_CHANGE, 1):
1491 case cmdpass(RTM_DELETE, 2):
1492 case cmdpass(RTM_NEWADDR, 1):
1493 case cmdpass(RTM_DELADDR, 1):
1494 case cmdpass(RTM_CHGADDR, 1):
1495 switch (cmd) {
1496 case RTM_ADD:
1497 ncmd = RTM_XNEWADDR;
1498 break;
1499 case RTM_DELETE:
1500 ncmd = RTM_XDELADDR;
1501 break;
1502 case RTM_CHANGE:
1503 ncmd = RTM_XCHGADDR;
1504 break;
1505 case RTM_NEWADDR:
1506 ncmd = RTM_XNEWADDR;
1507 break;
1508 case RTM_DELADDR:
1509 ncmd = RTM_XDELADDR;
1510 break;
1511 case RTM_CHGADDR:
1512 ncmd = RTM_XCHGADDR;
1513 break;
1514 default:
1515 panic("%s: unknown command %d", __func__, cmd);
1516 }
1517 #ifdef COMPAT_70
1518 compat_70_rt_newaddrmsg1(ncmd, ifa);
1519 #endif
1520 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1521 KASSERT(ifp->if_dl != NULL);
1522 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1523 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1524 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1525 memset(&ifam, 0, sizeof(ifam));
1526 ifam.ifam_index = ifp->if_index;
1527 ifam.ifam_metric = ifa->ifa_metric;
1528 ifam.ifam_flags = ifa->ifa_flags;
1529 #ifndef COMPAT_RTSOCK
1530 ifam.ifam_pid = curproc->p_pid;
1531 ifam.ifam_addrflags = if_addrflags(ifa);
1532 #endif
1533 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1534 if (m == NULL)
1535 continue;
1536 mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1537 info.rti_addrs;
1538 break;
1539 case cmdpass(RTM_ADD, 2):
1540 case cmdpass(RTM_CHANGE, 2):
1541 case cmdpass(RTM_DELETE, 1):
1542 if (rt == NULL)
1543 continue;
1544 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1545 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1546 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1547 memset(&rtm, 0, sizeof(rtm));
1548 rtm.rtm_pid = curproc->p_pid;
1549 rtm.rtm_index = ifp->if_index;
1550 rtm.rtm_flags |= rt->rt_flags;
1551 rtm.rtm_errno = error;
1552 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1553 if (m == NULL)
1554 continue;
1555 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1556 break;
1557 default:
1558 continue;
1559 }
1560 KASSERTMSG(m != NULL, "called with wrong command");
1561 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1562 }
1563 #undef cmdpass
1564
1565 }
1566
1567 static struct mbuf *
1568 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1569 struct rt_addrinfo *info)
1570 {
1571 struct if_xannouncemsghdr ifan;
1572
1573 memset(info, 0, sizeof(*info));
1574 memset(&ifan, 0, sizeof(ifan));
1575 ifan.ifan_index = ifp->if_index;
1576 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1577 ifan.ifan_what = what;
1578 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1579 }
1580
1581 /*
1582 * This is called to generate routing socket messages indicating
1583 * network interface arrival and departure.
1584 */
1585 void
1586 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1587 {
1588 struct mbuf *m;
1589 struct rt_addrinfo info;
1590
1591 COMPATCALL(rt_ifannouncemsg, (ifp, what));
1592 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1593 return;
1594 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1595 if (m == NULL)
1596 return;
1597 COMPATNAME(route_enqueue)(m, 0);
1598 }
1599
1600 /*
1601 * This is called to generate routing socket messages indicating
1602 * IEEE80211 wireless events.
1603 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1604 */
1605 void
1606 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1607 size_t data_len)
1608 {
1609 struct mbuf *m;
1610 struct rt_addrinfo info;
1611
1612 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1613 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1614 return;
1615 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1616 if (m == NULL)
1617 return;
1618 /*
1619 * Append the ieee80211 data. Try to stick it in the
1620 * mbuf containing the ifannounce msg; otherwise allocate
1621 * a new mbuf and append.
1622 *
1623 * NB: we assume m is a single mbuf.
1624 */
1625 if (data_len > M_TRAILINGSPACE(m)) {
1626 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1627 if (n == NULL) {
1628 m_freem(m);
1629 return;
1630 }
1631 (void)memcpy(mtod(n, void *), data, data_len);
1632 n->m_len = data_len;
1633 m->m_next = n;
1634 } else if (data_len > 0) {
1635 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1636 m->m_len += data_len;
1637 }
1638 if (m->m_flags & M_PKTHDR)
1639 m->m_pkthdr.len += data_len;
1640 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1641 COMPATNAME(route_enqueue)(m, 0);
1642 }
1643
1644 #ifndef COMPAT_RTSOCK
1645 /*
1646 * Send a routing message as mimicing that a cloned route is added.
1647 */
1648 void
1649 rt_clonedmsg(const struct sockaddr *dst, const struct ifnet *ifp,
1650 const struct rtentry *rt)
1651 {
1652 struct rt_addrinfo info;
1653 /* Mimic flags exactly */
1654 #define RTF_LLINFO 0x400
1655 #define RTF_CLONED 0x2000
1656 int flags = RTF_UP | RTF_HOST | RTF_DONE | RTF_LLINFO | RTF_CLONED;
1657 union {
1658 struct sockaddr sa;
1659 struct sockaddr_storage ss;
1660 struct sockaddr_dl sdl;
1661 } u;
1662 uint8_t namelen = strlen(ifp->if_xname);
1663 uint8_t addrlen = ifp->if_addrlen;
1664
1665 if (rt == NULL)
1666 return; /* XXX */
1667
1668 memset(&info, 0, sizeof(info));
1669 info.rti_info[RTAX_DST] = dst;
1670 sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
1671 NULL, namelen, NULL, addrlen);
1672 info.rti_info[RTAX_GATEWAY] = &u.sa;
1673 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1674 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1675
1676 rt_missmsg(RTM_ADD, &info, flags, 0);
1677 #undef RTF_LLINFO
1678 #undef RTF_CLONED
1679 }
1680 #endif /* COMPAT_RTSOCK */
1681
1682 /*
1683 * This is used in dumping the kernel table via sysctl().
1684 */
1685 static int
1686 sysctl_dumpentry(struct rtentry *rt, void *v)
1687 {
1688 struct rt_walkarg *w = v;
1689 int error = 0, size;
1690 struct rt_addrinfo info;
1691
1692 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1693 return 0;
1694 memset(&info, 0, sizeof(info));
1695 info.rti_info[RTAX_DST] = rt_getkey(rt);
1696 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1697 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1698 info.rti_info[RTAX_TAG] = rt_gettag(rt);
1699 if (rt->rt_ifp) {
1700 const struct ifaddr *rtifa;
1701 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1702 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
1703 * then rt_get_ifa() != NULL. So this ought to still be safe.
1704 * --dyoung
1705 */
1706 rtifa = rt_get_ifa(rt);
1707 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1708 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1709 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1710 }
1711 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1712 return error;
1713 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1714 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1715
1716 rtm->rtm_flags = rt->rt_flags;
1717 rtm->rtm_use = rt->rt_use;
1718 rtm_setmetrics(rt, rtm);
1719 KASSERT(rt->rt_ifp != NULL);
1720 rtm->rtm_index = rt->rt_ifp->if_index;
1721 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1722 rtm->rtm_addrs = info.rti_addrs;
1723 if ((error = copyout(rtm, w->w_where, size)) != 0)
1724 w->w_where = NULL;
1725 else
1726 w->w_where = (char *)w->w_where + size;
1727 }
1728 return error;
1729 }
1730
1731 static int
1732 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
1733 struct rt_addrinfo *info, size_t len)
1734 {
1735 struct if_xmsghdr *ifm;
1736 int error;
1737
1738 ifm = (struct if_xmsghdr *)w->w_tmem;
1739 ifm->ifm_index = ifp->if_index;
1740 ifm->ifm_flags = ifp->if_flags;
1741 ifm->ifm_data = ifp->if_data;
1742 ifm->ifm_addrs = info->rti_addrs;
1743 if ((error = copyout(ifm, w->w_where, len)) == 0)
1744 w->w_where = (char *)w->w_where + len;
1745 return error;
1746 }
1747
1748 static int
1749 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
1750 struct rt_addrinfo *info)
1751 {
1752 int len, error;
1753
1754 if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
1755 return error;
1756 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1757 struct ifa_xmsghdr *ifam;
1758
1759 ifam = (struct ifa_xmsghdr *)w->w_tmem;
1760 ifam->ifam_index = ifa->ifa_ifp->if_index;
1761 ifam->ifam_flags = ifa->ifa_flags;
1762 ifam->ifam_metric = ifa->ifa_metric;
1763 ifam->ifam_addrs = info->rti_addrs;
1764 #ifndef COMPAT_RTSOCK
1765 ifam->ifam_pid = 0;
1766 ifam->ifam_addrflags = if_addrflags(ifa);
1767 #endif
1768 if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
1769 w->w_where = (char *)w->w_where + len;
1770 }
1771 return error;
1772 }
1773
1774 static int
1775 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1776 {
1777 struct ifnet *ifp;
1778 struct ifaddr *ifa;
1779 struct rt_addrinfo info;
1780 int cmd, len, error = 0;
1781 int (*iflist_if)(struct ifnet *, struct rt_walkarg *,
1782 struct rt_addrinfo *, size_t);
1783 int (*iflist_addr)(struct rt_walkarg *, struct ifaddr *,
1784 struct rt_addrinfo *);
1785 int s;
1786 struct psref psref;
1787 int bound;
1788
1789 switch (type) {
1790 case NET_RT_IFLIST:
1791 cmd = RTM_IFINFO;
1792 iflist_if = sysctl_iflist_if;
1793 iflist_addr = sysctl_iflist_addr;
1794 break;
1795 #ifdef COMPAT_14
1796 case NET_RT_OOOIFLIST:
1797 cmd = RTM_OOIFINFO;
1798 iflist_if = compat_14_iflist;
1799 iflist_addr = compat_70_iflist_addr;
1800 break;
1801 #endif
1802 #ifdef COMPAT_50
1803 case NET_RT_OOIFLIST:
1804 cmd = RTM_OIFINFO;
1805 iflist_if = compat_50_iflist;
1806 iflist_addr = compat_70_iflist_addr;
1807 break;
1808 #endif
1809 #ifdef COMPAT_70
1810 case NET_RT_OIFLIST:
1811 cmd = RTM_IFINFO;
1812 iflist_if = sysctl_iflist_if;
1813 iflist_addr = compat_70_iflist_addr;
1814 break;
1815 #endif
1816 default:
1817 #ifdef RTSOCK_DEBUG
1818 printf("%s: unsupported IFLIST type %d\n", __func__, type);
1819 #endif
1820 return EINVAL;
1821 }
1822
1823 memset(&info, 0, sizeof(info));
1824
1825 bound = curlwp_bind();
1826 s = pserialize_read_enter();
1827 IFNET_READER_FOREACH(ifp) {
1828 int _s;
1829 if (w->w_arg && w->w_arg != ifp->if_index)
1830 continue;
1831 if (IFADDR_READER_EMPTY(ifp))
1832 continue;
1833
1834 if_acquire(ifp, &psref);
1835 pserialize_read_exit(s);
1836
1837 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1838 if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
1839 goto release_exit;
1840 info.rti_info[RTAX_IFP] = NULL;
1841 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1842 if ((error = iflist_if(ifp, w, &info, len)) != 0)
1843 goto release_exit;
1844 }
1845 _s = pserialize_read_enter();
1846 IFADDR_READER_FOREACH(ifa, ifp) {
1847 struct psref _psref;
1848 if (af && af != ifa->ifa_addr->sa_family)
1849 continue;
1850 ifa_acquire(ifa, &_psref);
1851 pserialize_read_exit(_s);
1852
1853 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1854 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1855 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1856 error = iflist_addr(w, ifa, &info);
1857
1858 _s = pserialize_read_enter();
1859 ifa_release(ifa, &_psref);
1860 if (error != 0) {
1861 pserialize_read_exit(_s);
1862 goto release_exit;
1863 }
1864 }
1865 pserialize_read_exit(_s);
1866 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1867 info.rti_info[RTAX_BRD] = NULL;
1868
1869 s = pserialize_read_enter();
1870 if_release(ifp, &psref);
1871 }
1872 pserialize_read_exit(s);
1873 curlwp_bindx(bound);
1874
1875 return 0;
1876
1877 release_exit:
1878 if_release(ifp, &psref);
1879 curlwp_bindx(bound);
1880 return error;
1881 }
1882
1883 static int
1884 sysctl_rtable(SYSCTLFN_ARGS)
1885 {
1886 void *where = oldp;
1887 size_t *given = oldlenp;
1888 int i, s, error = EINVAL;
1889 u_char af;
1890 struct rt_walkarg w;
1891
1892 if (namelen == 1 && name[0] == CTL_QUERY)
1893 return sysctl_query(SYSCTLFN_CALL(rnode));
1894
1895 if (newp)
1896 return EPERM;
1897 if (namelen != 3)
1898 return EINVAL;
1899 af = name[0];
1900 w.w_tmemneeded = 0;
1901 w.w_tmemsize = 0;
1902 w.w_tmem = NULL;
1903 again:
1904 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1905 if (w.w_tmemneeded) {
1906 w.w_tmem = kmem_alloc(w.w_tmemneeded, KM_SLEEP);
1907 w.w_tmemsize = w.w_tmemneeded;
1908 w.w_tmemneeded = 0;
1909 }
1910 w.w_op = name[1];
1911 w.w_arg = name[2];
1912 w.w_given = *given;
1913 w.w_needed = 0 - w.w_given;
1914 w.w_where = where;
1915
1916 s = splsoftnet();
1917 switch (w.w_op) {
1918
1919 case NET_RT_DUMP:
1920 case NET_RT_FLAGS:
1921 #if defined(INET) || defined(INET6)
1922 /*
1923 * take care of llinfo entries, the caller must
1924 * specify an AF
1925 */
1926 if (w.w_op == NET_RT_FLAGS &&
1927 (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
1928 if (af != 0)
1929 error = lltable_sysctl_dump(af, &w);
1930 else
1931 error = EINVAL;
1932 break;
1933 }
1934 #endif
1935
1936 for (i = 1; i <= AF_MAX; i++)
1937 if ((af == 0 || af == i) &&
1938 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1939 break;
1940 break;
1941
1942 #ifdef COMPAT_14
1943 case NET_RT_OOOIFLIST:
1944 error = sysctl_iflist(af, &w, w.w_op);
1945 break;
1946 #endif
1947 #ifdef COMPAT_50
1948 case NET_RT_OOIFLIST:
1949 error = sysctl_iflist(af, &w, w.w_op);
1950 break;
1951 #endif
1952 #ifdef COMPAT_70
1953 case NET_RT_OIFLIST:
1954 error = sysctl_iflist(af, &w, w.w_op);
1955 break;
1956 #endif
1957 case NET_RT_IFLIST:
1958 error = sysctl_iflist(af, &w, w.w_op);
1959 break;
1960 }
1961 splx(s);
1962
1963 /* check to see if we couldn't allocate memory with NOWAIT */
1964 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1965 goto again;
1966
1967 if (w.w_tmem)
1968 kmem_free(w.w_tmem, w.w_tmemsize);
1969 w.w_needed += w.w_given;
1970 if (where) {
1971 *given = (char *)w.w_where - (char *)where;
1972 if (*given < w.w_needed)
1973 return ENOMEM;
1974 } else {
1975 *given = (11 * w.w_needed) / 10;
1976 }
1977 return error;
1978 }
1979
1980 /*
1981 * Routing message software interrupt routine
1982 */
1983 static void
1984 COMPATNAME(route_intr)(void *cookie)
1985 {
1986 struct sockproto proto = { .sp_family = PF_XROUTE, };
1987 struct route_info * const ri = &COMPATNAME(route_info);
1988 struct mbuf *m;
1989
1990 mutex_enter(softnet_lock);
1991 KERNEL_LOCK(1, NULL);
1992 for (;;) {
1993 IFQ_LOCK(&ri->ri_intrq);
1994 IF_DEQUEUE(&ri->ri_intrq, m);
1995 IFQ_UNLOCK(&ri->ri_intrq);
1996 if (m == NULL)
1997 break;
1998 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1999 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
2000 }
2001 KERNEL_UNLOCK_ONE(NULL);
2002 mutex_exit(softnet_lock);
2003 }
2004
2005 /*
2006 * Enqueue a message to the software interrupt routine.
2007 */
2008 void
2009 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
2010 {
2011 struct route_info * const ri = &COMPATNAME(route_info);
2012 int wasempty;
2013
2014 IFQ_LOCK(&ri->ri_intrq);
2015 if (IF_QFULL(&ri->ri_intrq)) {
2016 IF_DROP(&ri->ri_intrq);
2017 IFQ_UNLOCK(&ri->ri_intrq);
2018 m_freem(m);
2019 } else {
2020 wasempty = IF_IS_EMPTY(&ri->ri_intrq);
2021 M_SETCTX(m, (uintptr_t)family);
2022 IF_ENQUEUE(&ri->ri_intrq, m);
2023 IFQ_UNLOCK(&ri->ri_intrq);
2024 if (wasempty) {
2025 kpreempt_disable();
2026 softint_schedule(ri->ri_sih);
2027 kpreempt_enable();
2028 }
2029 }
2030 }
2031
2032 static void
2033 COMPATNAME(route_init)(void)
2034 {
2035 struct route_info * const ri = &COMPATNAME(route_info);
2036
2037 #ifndef COMPAT_RTSOCK
2038 rt_init();
2039 #endif
2040
2041 sysctl_net_route_setup(NULL);
2042 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
2043 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
2044 COMPATNAME(route_intr), NULL);
2045 IFQ_LOCK_INIT(&ri->ri_intrq);
2046 }
2047
2048 /*
2049 * Definitions of protocols supported in the ROUTE domain.
2050 */
2051 #ifndef COMPAT_RTSOCK
2052 PR_WRAP_USRREQS(route);
2053 #else
2054 PR_WRAP_USRREQS(compat_50_route);
2055 #endif
2056
2057 static const struct pr_usrreqs route_usrreqs = {
2058 .pr_attach = COMPATNAME(route_attach_wrapper),
2059 .pr_detach = COMPATNAME(route_detach_wrapper),
2060 .pr_accept = COMPATNAME(route_accept_wrapper),
2061 .pr_bind = COMPATNAME(route_bind_wrapper),
2062 .pr_listen = COMPATNAME(route_listen_wrapper),
2063 .pr_connect = COMPATNAME(route_connect_wrapper),
2064 .pr_connect2 = COMPATNAME(route_connect2_wrapper),
2065 .pr_disconnect = COMPATNAME(route_disconnect_wrapper),
2066 .pr_shutdown = COMPATNAME(route_shutdown_wrapper),
2067 .pr_abort = COMPATNAME(route_abort_wrapper),
2068 .pr_ioctl = COMPATNAME(route_ioctl_wrapper),
2069 .pr_stat = COMPATNAME(route_stat_wrapper),
2070 .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper),
2071 .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper),
2072 .pr_rcvd = COMPATNAME(route_rcvd_wrapper),
2073 .pr_recvoob = COMPATNAME(route_recvoob_wrapper),
2074 .pr_send = COMPATNAME(route_send_wrapper),
2075 .pr_sendoob = COMPATNAME(route_sendoob_wrapper),
2076 .pr_purgeif = COMPATNAME(route_purgeif_wrapper),
2077 };
2078
2079 static const struct protosw COMPATNAME(route_protosw)[] = {
2080 {
2081 .pr_type = SOCK_RAW,
2082 .pr_domain = &COMPATNAME(routedomain),
2083 .pr_flags = PR_ATOMIC|PR_ADDR,
2084 .pr_input = raw_input,
2085 .pr_ctlinput = raw_ctlinput,
2086 .pr_ctloutput = route_ctloutput,
2087 .pr_usrreqs = &route_usrreqs,
2088 .pr_init = raw_init,
2089 },
2090 };
2091
2092 struct domain COMPATNAME(routedomain) = {
2093 .dom_family = PF_XROUTE,
2094 .dom_name = DOMAINNAME,
2095 .dom_init = COMPATNAME(route_init),
2096 .dom_protosw = COMPATNAME(route_protosw),
2097 .dom_protoswNPROTOSW =
2098 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
2099 };
2100
2101 static void
2102 sysctl_net_route_setup(struct sysctllog **clog)
2103 {
2104 const struct sysctlnode *rnode = NULL;
2105
2106 sysctl_createv(clog, 0, NULL, &rnode,
2107 CTLFLAG_PERMANENT,
2108 CTLTYPE_NODE, DOMAINNAME,
2109 SYSCTL_DESCR("PF_ROUTE information"),
2110 NULL, 0, NULL, 0,
2111 CTL_NET, PF_XROUTE, CTL_EOL);
2112
2113 sysctl_createv(clog, 0, NULL, NULL,
2114 CTLFLAG_PERMANENT,
2115 CTLTYPE_NODE, "rtable",
2116 SYSCTL_DESCR("Routing table information"),
2117 sysctl_rtable, 0, NULL, 0,
2118 CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
2119
2120 sysctl_createv(clog, 0, &rnode, NULL,
2121 CTLFLAG_PERMANENT,
2122 CTLTYPE_STRUCT, "stats",
2123 SYSCTL_DESCR("Routing statistics"),
2124 NULL, 0, &rtstat, sizeof(rtstat),
2125 CTL_CREATE, CTL_EOL);
2126 }
2127