2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1988, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)rtsock.c 8.5 (Berkeley) 11/2/94
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/sysctl.h>
71 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 #include <sys/syslog.h>
78 #include <kern/lock.h>
81 #include <net/route.h>
82 #include <net/raw_cb.h>
83 #include <netinet/in.h>
85 #include <machine/spl.h>
87 extern void m_copydata(struct mbuf
*, int, int, caddr_t
);
88 extern void m_copyback(struct mbuf
*, int, int, caddr_t
);
90 extern struct rtstat rtstat
;
93 MALLOC_DEFINE(M_RTABLE
, "routetbl", "routing tables");
95 extern lck_mtx_t
*rt_mtx
;
96 static struct sockaddr route_dst
= { 2, PF_ROUTE
, { 0, } };
97 static struct sockaddr route_src
= { 2, PF_ROUTE
, { 0, } };
98 static struct sockaddr sa_zero
= { sizeof(sa_zero
), AF_INET
, { 0, } };
99 static struct sockproto route_proto
= { PF_ROUTE
, 0 };
105 struct sysctl_req
*w_req
;
109 rt_msg1(int, struct rt_addrinfo
*);
110 static int rt_msg2(int, struct rt_addrinfo
*, caddr_t
, struct walkarg
*);
111 static int rt_xaddrs(caddr_t
, caddr_t
, struct rt_addrinfo
*);
112 static int sysctl_dumpentry(struct radix_node
*rn
, void *vw
);
113 static int sysctl_iflist(int af
, struct walkarg
*w
);
114 static int sysctl_iflist2(int af
, struct walkarg
*w
);
115 static int route_output(struct mbuf
*, struct socket
*);
116 static void rt_setmetrics(u_long
, struct rt_metrics
*, struct rt_metrics
*);
117 static void rt_setif(struct rtentry
*, struct sockaddr
*, struct sockaddr
*,
120 /* Sleazy use of local variables throughout file, warning!!!! */
121 #define dst info.rti_info[RTAX_DST]
122 #define gate info.rti_info[RTAX_GATEWAY]
123 #define netmask info.rti_info[RTAX_NETMASK]
124 #define genmask info.rti_info[RTAX_GENMASK]
125 #define ifpaddr info.rti_info[RTAX_IFP]
126 #define ifaaddr info.rti_info[RTAX_IFA]
127 #define brdaddr info.rti_info[RTAX_BRD]
130 * It really doesn't make any sense at all for this code to share much
131 * with raw_usrreq.c, since its functionality is so restricted. XXX
134 rts_abort(struct socket
*so
)
138 error
= raw_usrreqs
.pru_abort(so
);
142 /* pru_accept is EOPNOTSUPP */
145 rts_attach(struct socket
*so
, int proto
, __unused
struct proc
*p
)
150 if (sotorawcb(so
) != 0)
151 return EISCONN
; /* XXX panic? */
152 MALLOC(rp
, struct rawcb
*, sizeof *rp
, M_PCB
, M_WAITOK
); /* XXX */
155 bzero(rp
, sizeof *rp
);
158 * The splnet() is necessary to block protocols from sending
159 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
160 * this PCB is extant but incompletely initialized.
161 * Probably we should try to do more of this work beforehand and
164 so
->so_pcb
= (caddr_t
)rp
;
165 error
= raw_attach(so
, proto
); /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
170 so
->so_flags
|= SOF_PCBCLEARING
;
174 switch(rp
->rcb_proto
.sp_protocol
) {
175 //####LD route_cb needs looking
180 route_cb
.ip6_count
++;
183 route_cb
.ipx_count
++;
189 rp
->rcb_faddr
= &route_src
;
190 route_cb
.any_count
++;
191 /* the socket is already locked when we enter rts_attach */
193 so
->so_options
|= SO_USELOOPBACK
;
198 rts_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
202 error
= raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
208 rts_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
212 error
= raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
217 /* pru_connect2 is EOPNOTSUPP */
218 /* pru_control is EOPNOTSUPP */
221 rts_detach(struct socket
*so
)
223 struct rawcb
*rp
= sotorawcb(so
);
228 switch(rp
->rcb_proto
.sp_protocol
) {
233 route_cb
.ip6_count
--;
236 route_cb
.ipx_count
--;
242 route_cb
.any_count
--;
244 error
= raw_usrreqs
.pru_detach(so
);
250 rts_disconnect(struct socket
*so
)
254 error
= raw_usrreqs
.pru_disconnect(so
);
259 /* pru_listen is EOPNOTSUPP */
262 rts_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
266 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
271 /* pru_rcvd is EOPNOTSUPP */
272 /* pru_rcvoob is EOPNOTSUPP */
275 rts_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
276 struct mbuf
*control
, struct proc
*p
)
280 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
285 /* pru_sense is null */
288 rts_shutdown(struct socket
*so
)
292 error
= raw_usrreqs
.pru_shutdown(so
);
298 rts_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
302 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
307 static struct pr_usrreqs route_usrreqs
= {
308 rts_abort
, pru_accept_notsupp
, rts_attach
, rts_bind
,
309 rts_connect
, pru_connect2_notsupp
, pru_control_notsupp
,
310 rts_detach
, rts_disconnect
, pru_listen_notsupp
, rts_peeraddr
,
311 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, rts_send
, pru_sense_null
,
312 rts_shutdown
, rts_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
321 struct rt_msghdr
*rtm
= 0;
322 struct rtentry
*rt
= 0;
323 struct rtentry
*saved_nrt
= 0;
324 struct radix_node_head
*rnh
;
325 struct rt_addrinfo info
;
327 struct ifnet
*ifp
= 0;
329 struct proc
*curproc
= current_proc();
331 int sendonlytoself
= 0;
333 #define senderr(e) { error = e; goto flush;}
334 if (m
== 0 || ((m
->m_len
< sizeof(long)) && (m
= m_pullup(m
, sizeof(long))) == 0))
336 if ((m
->m_flags
& M_PKTHDR
) == 0)
337 panic("route_output");
339 /* unlock the socket (but keep a reference) it won't be accessed until raw_input appends to it. */
340 socket_unlock(so
, 0);
341 lck_mtx_lock(rt_mtx
);
343 len
= m
->m_pkthdr
.len
;
344 if (len
< sizeof(*rtm
) ||
345 len
!= mtod(m
, struct rt_msghdr
*)->rtm_msglen
) {
349 R_Malloc(rtm
, struct rt_msghdr
*, len
);
354 m_copydata(m
, 0, len
, (caddr_t
)rtm
);
355 if (rtm
->rtm_version
!= RTM_VERSION
) {
357 senderr(EPROTONOSUPPORT
);
361 * Silent version of RTM_GET for Reachabiltiy APIs. We may change
362 * all RTM_GETs to be silent in the future, so this is private for now.
364 if (rtm
->rtm_type
== RTM_GET_SILENT
) {
365 if ((so
->so_options
& SO_USELOOPBACK
) == 0)
368 rtm
->rtm_type
= RTM_GET
;
372 * Perform permission checking, only privileged sockets
373 * may perform operations other than RTM_GET
375 if (rtm
->rtm_type
!= RTM_GET
&& (so
->so_state
& SS_PRIV
) == 0) {
380 rtm
->rtm_pid
= proc_selfpid();
381 info
.rti_addrs
= rtm
->rtm_addrs
;
382 if (rt_xaddrs((caddr_t
)(rtm
+ 1), len
+ (caddr_t
)rtm
, &info
)) {
386 if (dst
== 0 || (dst
->sa_family
>= AF_MAX
)
387 || (gate
!= 0 && (gate
->sa_family
>= AF_MAX
))) {
391 struct radix_node
*t
;
392 t
= rn_addmask((caddr_t
)genmask
, 0, 1);
393 if (t
&& Bcmp(genmask
, t
->rn_key
, *(u_char
*)genmask
) == 0)
394 genmask
= (struct sockaddr
*)(t
->rn_key
);
398 switch (rtm
->rtm_type
) {
405 /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
406 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
407 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
408 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
409 * That circular route was ignored on previous version of MacOS X because of a routing bug
410 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
411 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
412 * made in order to provide binary compatibility with AOL.
413 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
414 * confusing the routing table with a wrong route to the previous default gateway
417 extern int check_routeselfref
;
418 #define satosinaddr(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr)
420 if (check_routeselfref
&& (dst
&& dst
->sa_family
== AF_INET
) &&
421 (netmask
&& satosinaddr(netmask
) == INADDR_BROADCAST
) &&
422 (gate
&& satosinaddr(dst
) == satosinaddr(gate
))) {
423 log(LOG_WARNING
, "route_output: circular route %ld.%ld.%ld.%ld/32 ignored\n",
424 (ntohl(satosinaddr(gate
)>>24))&0xff,
425 (ntohl(satosinaddr(gate
)>>16))&0xff,
426 (ntohl(satosinaddr(gate
)>>8))&0xff,
427 (ntohl(satosinaddr(gate
)))&0xff);
433 error
= rtrequest_locked(RTM_ADD
, dst
, gate
, netmask
,
434 rtm
->rtm_flags
, &saved_nrt
);
435 if (error
== 0 && saved_nrt
) {
438 * If the route request specified an interface with
439 * IFA and/or IFP, we set the requested interface on
440 * the route with rt_setif. It would be much better
441 * to do this inside rtrequest, but that would
442 * require passing the desired interface, in some
443 * form, to rtrequest. Since rtrequest is called in
444 * so many places (roughly 40 in our source), adding
445 * a parameter is to much for us to swallow; this is
446 * something for the FreeBSD developers to tackle.
447 * Instead, we let rtrequest compute whatever
448 * interface it wants, then come in behind it and
449 * stick in the interface that we really want. This
450 * works reasonably well except when rtrequest can't
451 * figure out what interface to use (with
452 * ifa_withroute) and returns ENETUNREACH. Ideally
453 * it shouldn't matter if rtrequest can't figure out
454 * the interface if we're going to explicitly set it
455 * ourselves anyway. But practically we can't
456 * recover here because rtrequest will not do any of
457 * the work necessary to add the route if it can't
458 * find an interface. As long as there is a default
459 * route that leads to some interface, rtrequest will
460 * find an interface, so this problem should be
461 * rarely encountered.
465 rt_setif(saved_nrt
, ifpaddr
, ifaaddr
, gate
);
467 rt_setmetrics(rtm
->rtm_inits
,
468 &rtm
->rtm_rmx
, &saved_nrt
->rt_rmx
);
469 saved_nrt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
470 saved_nrt
->rt_rmx
.rmx_locks
|=
471 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
473 saved_nrt
->rt_genmask
= genmask
;
478 error
= rtrequest_locked(RTM_DELETE
, dst
, gate
, netmask
,
479 rtm
->rtm_flags
, &saved_nrt
);
481 if ((rt
= saved_nrt
))
490 if ((rnh
= rt_tables
[dst
->sa_family
]) == 0) {
491 senderr(EAFNOSUPPORT
);
492 } else if ((rt
= (struct rtentry
*)
493 rnh
->rnh_lookup(dst
, netmask
, rnh
)) != NULL
)
497 switch(rtm
->rtm_type
) {
503 gate
= rt
->rt_gateway
;
504 netmask
= rt_mask(rt
);
505 genmask
= rt
->rt_genmask
;
506 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
509 ifnet_lock_shared(ifp
);
510 ifa2
= ifp
->if_addrhead
.tqh_first
;
511 ifpaddr
= ifa2
->ifa_addr
;
512 ifnet_lock_done(ifp
);
513 ifaaddr
= rt
->rt_ifa
->ifa_addr
;
514 rtm
->rtm_index
= ifp
->if_index
;
520 len
= rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)0,
521 (struct walkarg
*)0);
522 if (len
> rtm
->rtm_msglen
) {
523 struct rt_msghdr
*new_rtm
;
524 R_Malloc(new_rtm
, struct rt_msghdr
*, len
);
528 Bcopy(rtm
, new_rtm
, rtm
->rtm_msglen
);
529 R_Free(rtm
); rtm
= new_rtm
;
531 (void)rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)rtm
,
532 (struct walkarg
*)0);
533 rtm
->rtm_flags
= rt
->rt_flags
;
534 rtm
->rtm_rmx
= rt
->rt_rmx
;
535 rtm
->rtm_addrs
= info
.rti_addrs
;
540 if (gate
&& (error
= rt_setgate(rt
, rt_key(rt
), gate
)))
544 * If they tried to change things but didn't specify
545 * the required gateway, then just use the old one.
546 * This can happen if the user tries to change the
547 * flags on the default route without changing the
548 * default gateway. Changing flags still doesn't work.
550 if ((rt
->rt_flags
& RTF_GATEWAY
) && !gate
)
551 gate
= rt
->rt_gateway
;
555 * On Darwin, we call rt_setif which contains the
556 * equivalent to the code found at this very spot
559 rt_setif(rt
, ifpaddr
, ifaaddr
, gate
);
562 rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
,
565 /* rt_setif, called above does this for us on darwin */
566 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
567 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, gate
);
570 rt
->rt_genmask
= genmask
;
575 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
576 rt
->rt_rmx
.rmx_locks
|=
577 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
588 rtm
->rtm_errno
= error
;
590 rtm
->rtm_flags
|= RTF_DONE
;
594 lck_mtx_unlock(rt_mtx
);
595 socket_lock(so
, 0); /* relock the socket now */
597 struct rawcb
*rp
= 0;
599 * Check to see if we don't want our own messages.
601 if ((so
->so_options
& SO_USELOOPBACK
) == 0) {
602 if (route_cb
.any_count
<= 1) {
608 /* There is another listener, so construct message */
612 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
613 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
616 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
)
617 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
620 if (sendonlytoself
&& m
) {
622 if (sbappendaddr(&so
->so_rcv
, &route_src
, m
, (struct mbuf
*)0, &error
) != 0) {
629 rp
->rcb_proto
.sp_family
= 0; /* Avoid us */
631 route_proto
.sp_protocol
= dst
->sa_family
;
633 socket_unlock(so
, 0);
634 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
638 rp
->rcb_proto
.sp_family
= PF_ROUTE
;
645 rt_setmetrics(which
, in
, out
)
647 struct rt_metrics
*in
, *out
;
649 #define metric(f, e) if (which & (f)) out->e = in->e;
650 metric(RTV_RPIPE
, rmx_recvpipe
);
651 metric(RTV_SPIPE
, rmx_sendpipe
);
652 metric(RTV_SSTHRESH
, rmx_ssthresh
);
653 metric(RTV_RTT
, rmx_rtt
);
654 metric(RTV_RTTVAR
, rmx_rttvar
);
655 metric(RTV_HOPCOUNT
, rmx_hopcount
);
656 metric(RTV_MTU
, rmx_mtu
);
657 metric(RTV_EXPIRE
, rmx_expire
);
662 * Set route's interface given ifpaddr, ifaaddr, and gateway.
667 struct sockaddr
*Ifpaddr
,
668 struct sockaddr
*Ifaaddr
,
669 struct sockaddr
*Gate
)
671 struct ifaddr
*ifa
= 0;
672 struct ifnet
*ifp
= 0;
674 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
676 /* new gateway could require new ifaddr, ifp;
677 flags may also be different; ifp may be specified
678 by ll sockaddr when protocol address is ambiguous */
679 if (Ifpaddr
&& (ifa
= ifa_ifwithnet(Ifpaddr
)) &&
680 (ifp
= ifa
->ifa_ifp
) && (Ifaaddr
|| Gate
)) {
682 ifa
= ifaof_ifpforaddr(Ifaaddr
? Ifaaddr
: Gate
,
691 if (Ifpaddr
&& (ifp
= if_withname(Ifpaddr
)) ) {
693 ifa
= ifaof_ifpforaddr(Gate
, ifp
);
696 ifnet_lock_shared(ifp
);
697 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
699 ifnet_lock_done(ifp
);
702 else if (Ifaaddr
&& (ifa
= ifa_ifwithaddr(Ifaaddr
))) {
705 else if (Gate
&& (ifa
= ifa_ifwithroute(rt
->rt_flags
,
706 rt_key(rt
), Gate
))) {
711 struct ifaddr
*oifa
= rt
->rt_ifa
;
713 if (oifa
&& oifa
->ifa_rtrequest
)
714 oifa
->ifa_rtrequest(RTM_DELETE
,
718 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
719 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
720 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
729 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
730 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
731 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
736 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
737 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
741 * Extract the addresses of the passed sockaddrs.
742 * Do a little sanity checking so as to avoid bad memory references.
743 * This data is derived straight from userland.
746 rt_xaddrs(cp
, cplim
, rtinfo
)
748 struct rt_addrinfo
*rtinfo
;
753 bzero(rtinfo
->rti_info
, sizeof(rtinfo
->rti_info
));
754 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
755 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
757 sa
= (struct sockaddr
*)cp
;
761 if ( (cp
+ sa
->sa_len
) > cplim
) {
766 * there are no more.. quit now
767 * If there are more bits, they are in error.
768 * I've seen this. route(1) can evidently generate these.
769 * This causes kernel to core dump.
770 * for compatibility, If we see this, point to a safe address.
772 if (sa
->sa_len
== 0) {
773 rtinfo
->rti_info
[i
] = &sa_zero
;
774 return (0); /* should be EINVAL but for compat */
778 rtinfo
->rti_info
[i
] = sa
;
787 struct rt_addrinfo
*rtinfo
)
789 struct rt_msghdr
*rtm
;
799 len
= sizeof(struct ifa_msghdr
);
804 len
= sizeof(struct ifma_msghdr
);
808 len
= sizeof(struct if_msghdr
);
812 len
= sizeof(struct rt_msghdr
);
816 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
817 if (m
&& len
> MHLEN
) {
818 MCLGET(m
, M_DONTWAIT
);
819 if ((m
->m_flags
& M_EXT
) == 0) {
826 m
->m_pkthdr
.len
= m
->m_len
= len
;
827 m
->m_pkthdr
.rcvif
= 0;
828 rtm
= mtod(m
, struct rt_msghdr
*);
829 bzero((caddr_t
)rtm
, len
);
830 for (i
= 0; i
< RTAX_MAX
; i
++) {
831 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
833 rtinfo
->rti_addrs
|= (1 << i
);
834 dlen
= ROUNDUP(sa
->sa_len
);
835 m_copyback(m
, len
, dlen
, (caddr_t
)sa
);
838 if (m
->m_pkthdr
.len
!= len
) {
842 rtm
->rtm_msglen
= len
;
843 rtm
->rtm_version
= RTM_VERSION
;
844 rtm
->rtm_type
= type
;
849 rt_msg2(type
, rtinfo
, cp
, w
)
851 struct rt_addrinfo
*rtinfo
;
856 int len
, dlen
, second_time
= 0;
859 rtinfo
->rti_addrs
= 0;
865 len
= sizeof(struct ifa_msghdr
);
870 len
= sizeof(struct ifma_msghdr
);
874 len
= sizeof(struct if_msghdr
);
878 len
= sizeof(struct if_msghdr2
);
882 len
= sizeof(struct ifma_msghdr2
);
886 len
= sizeof(struct rt_msghdr2
);
890 len
= sizeof(struct rt_msghdr
);
895 for (i
= 0; i
< RTAX_MAX
; i
++) {
898 if ((sa
= rtinfo
->rti_info
[i
]) == 0)
900 rtinfo
->rti_addrs
|= (1 << i
);
901 dlen
= ROUNDUP(sa
->sa_len
);
903 bcopy((caddr_t
)sa
, cp
, (unsigned)dlen
);
908 if (cp
== 0 && w
!= NULL
&& !second_time
) {
909 struct walkarg
*rw
= w
;
912 if (rw
->w_tmemsize
< len
) {
914 FREE(rw
->w_tmem
, M_RTABLE
);
915 rw
->w_tmem
= (caddr_t
)
916 _MALLOC(len
, M_RTABLE
, M_WAITOK
); /*###LD0412 was NOWAIT */
918 rw
->w_tmemsize
= len
;
928 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)cp0
;
930 rtm
->rtm_version
= RTM_VERSION
;
931 rtm
->rtm_type
= type
;
932 rtm
->rtm_msglen
= len
;
938 * This routine is called to generate a message from the routing
939 * socket indicating that a redirect has occurred, a routing lookup
940 * has failed, or that a protocol has detected timeouts to a particular
944 rt_missmsg(type
, rtinfo
, flags
, error
)
945 int type
, flags
, error
;
946 struct rt_addrinfo
*rtinfo
;
948 struct rt_msghdr
*rtm
;
950 struct sockaddr
*sa
= rtinfo
->rti_info
[RTAX_DST
];
952 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
954 if (route_cb
.any_count
== 0)
956 m
= rt_msg1(type
, rtinfo
);
959 rtm
= mtod(m
, struct rt_msghdr
*);
960 rtm
->rtm_flags
= RTF_DONE
| flags
;
961 rtm
->rtm_errno
= error
;
962 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
963 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
964 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
968 * This routine is called to generate a message from the routing
969 * socket indicating that the status of a network interface has changed.
975 struct if_msghdr
*ifm
;
977 struct rt_addrinfo info
;
979 if (route_cb
.any_count
== 0)
981 bzero((caddr_t
)&info
, sizeof(info
));
982 m
= rt_msg1(RTM_IFINFO
, &info
);
985 ifm
= mtod(m
, struct if_msghdr
*);
986 ifm
->ifm_index
= ifp
->if_index
;
987 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
988 if_data_internal_to_if_data(&ifp
->if_data
, &ifm
->ifm_data
);
990 route_proto
.sp_protocol
= 0;
991 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
995 * This is called to generate messages from the routing socket
996 * indicating a network interface has had addresses associated with it.
997 * if we ever reverse the logic and replace messages TO the routing
998 * socket indicate a request to configure interfaces, then it will
999 * be unnecessary as the routing socket will automatically generate
1002 * Since this is coming from the interface, it is expected that the
1003 * interface will be locked.
1006 rt_newaddrmsg(cmd
, ifa
, error
, rt
)
1011 struct rt_addrinfo info
;
1012 struct sockaddr
*sa
= 0;
1015 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1017 if (route_cb
.any_count
== 0)
1019 for (pass
= 1; pass
< 3; pass
++) {
1020 bzero((caddr_t
)&info
, sizeof(info
));
1021 if ((cmd
== RTM_ADD
&& pass
== 1) ||
1022 (cmd
== RTM_DELETE
&& pass
== 2)) {
1023 struct ifa_msghdr
*ifam
;
1024 int ncmd
= cmd
== RTM_ADD
? RTM_NEWADDR
: RTM_DELADDR
;
1026 ifaaddr
= sa
= ifa
->ifa_addr
;
1027 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1028 netmask
= ifa
->ifa_netmask
;
1029 brdaddr
= ifa
->ifa_dstaddr
;
1030 if ((m
= rt_msg1(ncmd
, &info
)) == NULL
)
1032 ifam
= mtod(m
, struct ifa_msghdr
*);
1033 ifam
->ifam_index
= ifp
->if_index
;
1034 ifam
->ifam_metric
= ifa
->ifa_metric
;
1035 ifam
->ifam_flags
= ifa
->ifa_flags
;
1036 ifam
->ifam_addrs
= info
.rti_addrs
;
1038 if ((cmd
== RTM_ADD
&& pass
== 2) ||
1039 (cmd
== RTM_DELETE
&& pass
== 1)) {
1040 struct rt_msghdr
*rtm
;
1044 netmask
= rt_mask(rt
);
1045 dst
= sa
= rt_key(rt
);
1046 gate
= rt
->rt_gateway
;
1047 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1049 rtm
= mtod(m
, struct rt_msghdr
*);
1050 rtm
->rtm_index
= ifp
->if_index
;
1051 rtm
->rtm_flags
|= rt
->rt_flags
;
1052 rtm
->rtm_errno
= error
;
1053 rtm
->rtm_addrs
= info
.rti_addrs
;
1055 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
1056 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1061 * This is the analogue to the rt_newaddrmsg which performs the same
1062 * function but for multicast group memberhips. This is easier since
1063 * there is no route state to worry about.
1066 rt_newmaddrmsg(cmd
, ifma
)
1068 struct ifmultiaddr
*ifma
;
1070 struct rt_addrinfo info
;
1072 struct ifnet
*ifp
= ifma
->ifma_ifp
;
1073 struct ifma_msghdr
*ifmam
;
1075 if (route_cb
.any_count
== 0)
1078 bzero((caddr_t
)&info
, sizeof(info
));
1079 ifaaddr
= ifma
->ifma_addr
;
1080 if (ifp
&& ifp
->if_addrhead
.tqh_first
)
1081 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1085 * If a link-layer address is present, present it as a ``gateway''
1086 * (similarly to how ARP entries, e.g., are presented).
1088 gate
= ifma
->ifma_ll
->ifma_addr
;
1089 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1091 ifmam
= mtod(m
, struct ifma_msghdr
*);
1092 ifmam
->ifmam_index
= ifp
? ifp
->if_index
: 0;
1093 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1094 route_proto
.sp_protocol
= ifma
->ifma_addr
->sa_family
;
1095 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1099 * This is used in dumping the kernel table via sysctl().
1102 sysctl_dumpentry(rn
, vw
)
1103 struct radix_node
*rn
;
1106 struct walkarg
*w
= vw
;
1107 struct rtentry
*rt
= (struct rtentry
*)rn
;
1108 int error
= 0, size
;
1109 struct rt_addrinfo info
;
1111 if (w
->w_op
== NET_RT_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
1113 bzero((caddr_t
)&info
, sizeof(info
));
1115 gate
= rt
->rt_gateway
;
1116 netmask
= rt_mask(rt
);
1117 genmask
= rt
->rt_genmask
;
1118 if (w
->w_op
!= NET_RT_DUMP2
) {
1119 size
= rt_msg2(RTM_GET
, &info
, 0, w
);
1120 if (w
->w_req
&& w
->w_tmem
) {
1121 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)w
->w_tmem
;
1123 rtm
->rtm_flags
= rt
->rt_flags
;
1124 rtm
->rtm_use
= rt
->rt_use
;
1125 rtm
->rtm_rmx
= rt
->rt_rmx
;
1126 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1130 rtm
->rtm_addrs
= info
.rti_addrs
;
1131 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1135 size
= rt_msg2(RTM_GET2
, &info
, 0, w
);
1136 if (w
->w_req
&& w
->w_tmem
) {
1137 struct rt_msghdr2
*rtm
= (struct rt_msghdr2
*)w
->w_tmem
;
1139 rtm
->rtm_flags
= rt
->rt_flags
;
1140 rtm
->rtm_use
= rt
->rt_use
;
1141 rtm
->rtm_rmx
= rt
->rt_rmx
;
1142 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1143 rtm
->rtm_refcnt
= rt
->rt_refcnt
;
1145 rtm
->rtm_parentflags
= rt
->rt_parent
->rt_flags
;
1147 rtm
->rtm_parentflags
= 0;
1148 rtm
->rtm_reserved
= 0;
1149 rtm
->rtm_addrs
= info
.rti_addrs
;
1150 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1165 struct rt_addrinfo info
;
1168 bzero((caddr_t
)&info
, sizeof(info
));
1169 ifnet_head_lock_shared();
1170 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1173 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1175 ifnet_lock_shared(ifp
);
1176 ifa
= ifp
->if_addrhead
.tqh_first
;
1177 ifpaddr
= ifa
->ifa_addr
;
1178 len
= rt_msg2(RTM_IFINFO
, &info
, (caddr_t
)0, w
);
1180 if (w
->w_req
&& w
->w_tmem
) {
1181 struct if_msghdr
*ifm
;
1183 ifm
= (struct if_msghdr
*)w
->w_tmem
;
1184 ifm
->ifm_index
= ifp
->if_index
;
1185 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1186 if_data_internal_to_if_data(&ifp
->if_data
, &ifm
->ifm_data
);
1187 ifm
->ifm_addrs
= info
.rti_addrs
;
1188 error
= SYSCTL_OUT(w
->w_req
,(caddr_t
)ifm
, len
);
1190 ifnet_lock_done(ifp
);
1194 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1195 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1198 if (curproc
->p_prison
&& prison_if(curproc
, ifa
->ifa_addr
))
1201 ifaaddr
= ifa
->ifa_addr
;
1202 netmask
= ifa
->ifa_netmask
;
1203 brdaddr
= ifa
->ifa_dstaddr
;
1204 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1205 if (w
->w_req
&& w
->w_tmem
) {
1206 struct ifa_msghdr
*ifam
;
1208 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1209 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1210 ifam
->ifam_flags
= ifa
->ifa_flags
;
1211 ifam
->ifam_metric
= ifa
->ifa_metric
;
1212 ifam
->ifam_addrs
= info
.rti_addrs
;
1213 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1218 ifnet_lock_done(ifp
);
1219 ifaaddr
= netmask
= brdaddr
= 0;
1232 struct rt_addrinfo info
;
1235 bzero((caddr_t
)&info
, sizeof(info
));
1236 ifnet_head_lock_shared();
1237 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1240 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1242 ifnet_lock_shared(ifp
);
1243 ifa
= ifp
->if_addrhead
.tqh_first
;
1244 ifpaddr
= ifa
->ifa_addr
;
1245 len
= rt_msg2(RTM_IFINFO2
, &info
, (caddr_t
)0, w
);
1247 if (w
->w_req
&& w
->w_tmem
) {
1248 struct if_msghdr2
*ifm
;
1250 ifm
= (struct if_msghdr2
*)w
->w_tmem
;
1251 ifm
->ifm_addrs
= info
.rti_addrs
;
1252 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1253 ifm
->ifm_index
= ifp
->if_index
;
1254 ifm
->ifm_snd_len
= ifp
->if_snd
.ifq_len
;
1255 ifm
->ifm_snd_maxlen
= ifp
->if_snd
.ifq_maxlen
;
1256 ifm
->ifm_snd_drops
= ifp
->if_snd
.ifq_drops
;
1257 ifm
->ifm_timer
= ifp
->if_timer
;
1258 if_data_internal_to_if_data64(&ifp
->if_data
, &ifm
->ifm_data
);
1259 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1261 ifnet_lock_done(ifp
);
1265 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1266 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1268 ifaaddr
= ifa
->ifa_addr
;
1269 netmask
= ifa
->ifa_netmask
;
1270 brdaddr
= ifa
->ifa_dstaddr
;
1271 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1272 if (w
->w_req
&& w
->w_tmem
) {
1273 struct ifa_msghdr
*ifam
;
1275 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1276 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1277 ifam
->ifam_flags
= ifa
->ifa_flags
;
1278 ifam
->ifam_metric
= ifa
->ifa_metric
;
1279 ifam
->ifam_addrs
= info
.rti_addrs
;
1280 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1286 ifnet_lock_done(ifp
);
1290 struct ifmultiaddr
*ifma
;
1292 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1293 ifma
= ifma
->ifma_link
.le_next
) {
1294 if (af
&& af
!= ifma
->ifma_addr
->sa_family
)
1296 bzero((caddr_t
)&info
, sizeof(info
));
1297 ifaaddr
= ifma
->ifma_addr
;
1298 if (ifp
->if_addrhead
.tqh_first
)
1299 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1301 gate
= ifma
->ifma_ll
->ifma_addr
;
1302 len
= rt_msg2(RTM_NEWMADDR2
, &info
, 0, w
);
1303 if (w
->w_req
&& w
->w_tmem
) {
1304 struct ifma_msghdr2
*ifmam
;
1306 ifmam
= (struct ifma_msghdr2
*)w
->w_tmem
;
1307 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1308 ifmam
->ifmam_flags
= 0;
1309 ifmam
->ifmam_index
= ifma
->ifma_ifp
->if_index
;
1310 ifmam
->ifmam_refcount
= ifma
->ifma_refcount
;
1311 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1317 ifnet_lock_done(ifp
);
1318 ifaaddr
= netmask
= brdaddr
= 0;
1326 sysctl_rtstat(struct sysctl_req
*req
)
1330 error
= SYSCTL_OUT(req
, &rtstat
, sizeof(struct rtstat
));
1338 sysctl_rttrash(struct sysctl_req
*req
)
1342 error
= SYSCTL_OUT(req
, &rttrash
, sizeof(rttrash
));
1351 sysctl_rtsock SYSCTL_HANDLER_ARGS
1353 int *name
= (int *)arg1
;
1354 u_int namelen
= arg2
;
1355 struct radix_node_head
*rnh
;
1356 int i
, error
= EINVAL
;
1367 Bzero(&w
, sizeof(w
));
1377 lck_mtx_lock(rt_mtx
);
1378 for (i
= 1; i
<= AF_MAX
; i
++)
1379 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
1380 (error
= rnh
->rnh_walktree(rnh
,
1381 sysctl_dumpentry
, &w
)))
1383 lck_mtx_unlock(rt_mtx
);
1386 error
= sysctl_iflist(af
, &w
);
1388 case NET_RT_IFLIST2
:
1389 error
= sysctl_iflist2(af
, &w
);
1392 error
= sysctl_rtstat(req
);
1395 error
= sysctl_rttrash(req
);
1399 FREE(w
.w_tmem
, M_RTABLE
);
1403 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
, sysctl_rtsock
, "");
1406 * Definitions of protocols supported in the ROUTE domain.
1409 struct domain routedomain
; /* or at least forward */
1411 static struct protosw routesw
[] = {
1412 { SOCK_RAW
, &routedomain
, 0, PR_ATOMIC
|PR_ADDR
,
1413 0, route_output
, raw_ctlinput
, 0,
1423 struct domain routedomain
=
1424 { PF_ROUTE
, "route", route_init
, 0, 0,
1426 0, 0, 0, 0, 0, 0, 0, 0,