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