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