2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1988, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. 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.
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
60 * @(#)rtsock.c 8.5 (Berkeley) 11/2/94
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/sysctl.h>
69 #include <sys/malloc.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/syslog.h>
76 #include <kern/lock.h>
79 #include <net/route.h>
80 #include <net/raw_cb.h>
81 #include <netinet/in.h>
83 #include <machine/spl.h>
85 extern struct rtstat rtstat
;
87 extern u_long route_generation
;
88 extern int use_routegenid
;
89 extern int check_routeselfref
;
91 MALLOC_DEFINE(M_RTABLE
, "routetbl", "routing tables");
93 extern lck_mtx_t
*rt_mtx
;
94 static struct sockaddr route_dst
= { 2, PF_ROUTE
, { 0, } };
95 static struct sockaddr route_src
= { 2, PF_ROUTE
, { 0, } };
96 static struct sockaddr sa_zero
= { sizeof(sa_zero
), AF_INET
, { 0, } };
97 static struct sockproto route_proto
= { PF_ROUTE
, 0 };
103 struct sysctl_req
*w_req
;
107 rt_msg1(int, struct rt_addrinfo
*);
108 static int rt_msg2(int, struct rt_addrinfo
*, caddr_t
, struct walkarg
*);
109 static int rt_xaddrs(caddr_t
, caddr_t
, struct rt_addrinfo
*);
110 static int sysctl_dumpentry(struct radix_node
*rn
, void *vw
);
111 static int sysctl_iflist(int af
, struct walkarg
*w
);
112 static int sysctl_iflist2(int af
, struct walkarg
*w
);
113 static int route_output(struct mbuf
*, struct socket
*);
114 static void rt_setmetrics(u_long
, struct rt_metrics
*, struct rt_metrics
*);
115 static void rt_setif(struct rtentry
*, struct sockaddr
*, struct sockaddr
*,
118 /* Sleazy use of local variables throughout file, warning!!!! */
119 #define dst info.rti_info[RTAX_DST]
120 #define gate info.rti_info[RTAX_GATEWAY]
121 #define netmask info.rti_info[RTAX_NETMASK]
122 #define genmask info.rti_info[RTAX_GENMASK]
123 #define ifpaddr info.rti_info[RTAX_IFP]
124 #define ifaaddr info.rti_info[RTAX_IFA]
125 #define brdaddr info.rti_info[RTAX_BRD]
128 * It really doesn't make any sense at all for this code to share much
129 * with raw_usrreq.c, since its functionality is so restricted. XXX
132 rts_abort(struct socket
*so
)
136 error
= raw_usrreqs
.pru_abort(so
);
140 /* pru_accept is EOPNOTSUPP */
143 rts_attach(struct socket
*so
, int proto
, __unused
struct proc
*p
)
148 if (sotorawcb(so
) != 0)
149 return EISCONN
; /* XXX panic? */
150 MALLOC(rp
, struct rawcb
*, sizeof *rp
, M_PCB
, M_WAITOK
); /* XXX */
153 bzero(rp
, sizeof *rp
);
156 * The splnet() is necessary to block protocols from sending
157 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
158 * this PCB is extant but incompletely initialized.
159 * Probably we should try to do more of this work beforehand and
162 so
->so_pcb
= (caddr_t
)rp
;
163 error
= raw_attach(so
, proto
); /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
168 so
->so_flags
|= SOF_PCBCLEARING
;
172 switch(rp
->rcb_proto
.sp_protocol
) {
173 //####LD route_cb needs looking
178 route_cb
.ip6_count
++;
181 route_cb
.ipx_count
++;
187 rp
->rcb_faddr
= &route_src
;
188 route_cb
.any_count
++;
189 /* the socket is already locked when we enter rts_attach */
191 so
->so_options
|= SO_USELOOPBACK
;
196 rts_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
199 error
= raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
204 rts_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
207 error
= raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
211 /* pru_connect2 is EOPNOTSUPP */
212 /* pru_control is EOPNOTSUPP */
215 rts_detach(struct socket
*so
)
217 struct rawcb
*rp
= sotorawcb(so
);
221 switch(rp
->rcb_proto
.sp_protocol
) {
226 route_cb
.ip6_count
--;
229 route_cb
.ipx_count
--;
235 route_cb
.any_count
--;
237 error
= raw_usrreqs
.pru_detach(so
);
242 rts_disconnect(struct socket
*so
)
245 error
= raw_usrreqs
.pru_disconnect(so
);
249 /* pru_listen is EOPNOTSUPP */
252 rts_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
255 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
259 /* pru_rcvd is EOPNOTSUPP */
260 /* pru_rcvoob is EOPNOTSUPP */
263 rts_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
264 struct mbuf
*control
, struct proc
*p
)
267 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
271 /* pru_sense is null */
274 rts_shutdown(struct socket
*so
)
277 error
= raw_usrreqs
.pru_shutdown(so
);
282 rts_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
285 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
289 static struct pr_usrreqs route_usrreqs
= {
290 rts_abort
, pru_accept_notsupp
, rts_attach
, rts_bind
,
291 rts_connect
, pru_connect2_notsupp
, pru_control_notsupp
,
292 rts_detach
, rts_disconnect
, pru_listen_notsupp
, rts_peeraddr
,
293 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, rts_send
, pru_sense_null
,
294 rts_shutdown
, rts_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
299 route_output(struct mbuf
*m
, struct socket
*so
)
301 struct rt_msghdr
*rtm
= NULL
;
302 struct rtentry
*rt
= NULL
;
303 struct rtentry
*saved_nrt
= NULL
;
304 struct radix_node_head
*rnh
;
305 struct rt_addrinfo info
;
307 struct ifnet
*ifp
= NULL
;
309 struct proc
*curproc
= current_proc();
311 int sendonlytoself
= 0;
313 #define senderr(e) { error = e; goto flush;}
314 if (m
== 0 || ((m
->m_len
< sizeof(long)) && (m
= m_pullup(m
, sizeof(long))) == 0))
316 if ((m
->m_flags
& M_PKTHDR
) == 0)
317 panic("route_output");
319 /* unlock the socket (but keep a reference) it won't be accessed until raw_input appends to it. */
320 socket_unlock(so
, 0);
321 lck_mtx_lock(rt_mtx
);
323 len
= m
->m_pkthdr
.len
;
324 if (len
< sizeof(*rtm
) ||
325 len
!= mtod(m
, struct rt_msghdr
*)->rtm_msglen
) {
329 R_Malloc(rtm
, struct rt_msghdr
*, len
);
334 m_copydata(m
, 0, len
, (caddr_t
)rtm
);
335 if (rtm
->rtm_version
!= RTM_VERSION
) {
337 senderr(EPROTONOSUPPORT
);
341 * Silent version of RTM_GET for Reachabiltiy APIs. We may change
342 * all RTM_GETs to be silent in the future, so this is private for now.
344 if (rtm
->rtm_type
== RTM_GET_SILENT
) {
345 if ((so
->so_options
& SO_USELOOPBACK
) == 0)
348 rtm
->rtm_type
= RTM_GET
;
352 * Perform permission checking, only privileged sockets
353 * may perform operations other than RTM_GET
355 if (rtm
->rtm_type
!= RTM_GET
&& (so
->so_state
& SS_PRIV
) == 0) {
360 rtm
->rtm_pid
= proc_selfpid();
361 info
.rti_addrs
= rtm
->rtm_addrs
;
362 if (rt_xaddrs((caddr_t
)(rtm
+ 1), len
+ (caddr_t
)rtm
, &info
)) {
366 if (dst
== 0 || (dst
->sa_family
>= AF_MAX
)
367 || (gate
!= 0 && (gate
->sa_family
>= AF_MAX
))) {
371 struct radix_node
*t
;
372 t
= rn_addmask((caddr_t
)genmask
, 0, 1);
373 if (t
&& Bcmp(genmask
, t
->rn_key
, *(u_char
*)genmask
) == 0)
374 genmask
= (struct sockaddr
*)(t
->rn_key
);
378 switch (rtm
->rtm_type
) {
385 /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
386 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
387 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
388 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
389 * That circular route was ignored on previous version of MacOS X because of a routing bug
390 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
391 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
392 * made in order to provide binary compatibility with AOL.
393 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
394 * confusing the routing table with a wrong route to the previous default gateway
397 #define satosinaddr(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr)
399 if (check_routeselfref
&& (dst
&& dst
->sa_family
== AF_INET
) &&
400 (netmask
&& satosinaddr(netmask
) == INADDR_BROADCAST
) &&
401 (gate
&& satosinaddr(dst
) == satosinaddr(gate
))) {
402 log(LOG_WARNING
, "route_output: circular route %ld.%ld.%ld.%ld/32 ignored\n",
403 (ntohl(satosinaddr(gate
)>>24))&0xff,
404 (ntohl(satosinaddr(gate
)>>16))&0xff,
405 (ntohl(satosinaddr(gate
)>>8))&0xff,
406 (ntohl(satosinaddr(gate
)))&0xff);
412 error
= rtrequest_locked(RTM_ADD
, dst
, gate
, netmask
,
413 rtm
->rtm_flags
, &saved_nrt
);
414 if (error
== 0 && saved_nrt
) {
417 * If the route request specified an interface with
418 * IFA and/or IFP, we set the requested interface on
419 * the route with rt_setif. It would be much better
420 * to do this inside rtrequest, but that would
421 * require passing the desired interface, in some
422 * form, to rtrequest. Since rtrequest is called in
423 * so many places (roughly 40 in our source), adding
424 * a parameter is to much for us to swallow; this is
425 * something for the FreeBSD developers to tackle.
426 * Instead, we let rtrequest compute whatever
427 * interface it wants, then come in behind it and
428 * stick in the interface that we really want. This
429 * works reasonably well except when rtrequest can't
430 * figure out what interface to use (with
431 * ifa_withroute) and returns ENETUNREACH. Ideally
432 * it shouldn't matter if rtrequest can't figure out
433 * the interface if we're going to explicitly set it
434 * ourselves anyway. But practically we can't
435 * recover here because rtrequest will not do any of
436 * the work necessary to add the route if it can't
437 * find an interface. As long as there is a default
438 * route that leads to some interface, rtrequest will
439 * find an interface, so this problem should be
440 * rarely encountered.
444 rt_setif(saved_nrt
, ifpaddr
, ifaaddr
, gate
);
446 rt_setmetrics(rtm
->rtm_inits
,
447 &rtm
->rtm_rmx
, &saved_nrt
->rt_rmx
);
448 saved_nrt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
449 saved_nrt
->rt_rmx
.rmx_locks
|=
450 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
452 saved_nrt
->rt_genmask
= genmask
;
457 error
= rtrequest_locked(RTM_DELETE
, dst
, gate
, netmask
,
458 rtm
->rtm_flags
, &saved_nrt
);
468 if ((rnh
= rt_tables
[dst
->sa_family
]) == 0) {
469 senderr(EAFNOSUPPORT
);
470 } else if ((rt
= (struct rtentry
*)
471 rnh
->rnh_lookup(dst
, netmask
, rnh
)) != NULL
)
475 switch(rtm
->rtm_type
) {
481 gate
= rt
->rt_gateway
;
482 netmask
= rt_mask(rt
);
483 genmask
= rt
->rt_genmask
;
484 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
487 ifnet_lock_shared(ifp
);
488 ifa2
= ifp
->if_addrhead
.tqh_first
;
489 ifpaddr
= ifa2
->ifa_addr
;
490 ifnet_lock_done(ifp
);
491 ifaaddr
= rt
->rt_ifa
->ifa_addr
;
492 rtm
->rtm_index
= ifp
->if_index
;
498 len
= rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)0,
499 (struct walkarg
*)0);
500 if (len
> rtm
->rtm_msglen
) {
501 struct rt_msghdr
*new_rtm
;
502 R_Malloc(new_rtm
, struct rt_msghdr
*, len
);
506 Bcopy(rtm
, new_rtm
, rtm
->rtm_msglen
);
507 R_Free(rtm
); rtm
= new_rtm
;
509 (void)rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)rtm
,
510 (struct walkarg
*)0);
511 rtm
->rtm_flags
= rt
->rt_flags
;
512 rtm
->rtm_rmx
= rt
->rt_rmx
;
513 rtm
->rtm_addrs
= info
.rti_addrs
;
518 if (gate
&& (error
= rt_setgate(rt
, rt_key(rt
), gate
)))
522 * If they tried to change things but didn't specify
523 * the required gateway, then just use the old one.
524 * This can happen if the user tries to change the
525 * flags on the default route without changing the
526 * default gateway. Changing flags still doesn't work.
528 if ((rt
->rt_flags
& RTF_GATEWAY
) && !gate
)
529 gate
= rt
->rt_gateway
;
533 * On Darwin, we call rt_setif which contains the
534 * equivalent to the code found at this very spot
537 rt_setif(rt
, ifpaddr
, ifaaddr
, gate
);
540 rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
,
543 /* rt_setif, called above does this for us on darwin */
544 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
545 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, gate
);
548 rt
->rt_genmask
= genmask
;
553 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
554 rt
->rt_rmx
.rmx_locks
|=
555 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
566 rtm
->rtm_errno
= error
;
568 rtm
->rtm_flags
|= RTF_DONE
;
572 lck_mtx_unlock(rt_mtx
);
573 socket_lock(so
, 0); /* relock the socket now */
575 struct rawcb
*rp
= 0;
577 * Check to see if we don't want our own messages.
579 if ((so
->so_options
& SO_USELOOPBACK
) == 0) {
580 if (route_cb
.any_count
<= 1) {
586 /* There is another listener, so construct message */
590 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
591 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
594 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
)
595 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
598 if (sendonlytoself
&& m
) {
600 if (sbappendaddr(&so
->so_rcv
, &route_src
, m
, (struct mbuf
*)0, &error
) != 0) {
607 rp
->rcb_proto
.sp_family
= 0; /* Avoid us */
609 route_proto
.sp_protocol
= dst
->sa_family
;
611 socket_unlock(so
, 0);
612 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
616 rp
->rcb_proto
.sp_family
= PF_ROUTE
;
623 rt_setmetrics(u_long which
, struct rt_metrics
*in
, struct rt_metrics
*out
)
625 #define metric(f, e) if (which & (f)) out->e = in->e;
626 metric(RTV_RPIPE
, rmx_recvpipe
);
627 metric(RTV_SPIPE
, rmx_sendpipe
);
628 metric(RTV_SSTHRESH
, rmx_ssthresh
);
629 metric(RTV_RTT
, rmx_rtt
);
630 metric(RTV_RTTVAR
, rmx_rttvar
);
631 metric(RTV_HOPCOUNT
, rmx_hopcount
);
632 metric(RTV_MTU
, rmx_mtu
);
633 metric(RTV_EXPIRE
, rmx_expire
);
638 * Set route's interface given ifpaddr, ifaaddr, and gateway.
643 struct sockaddr
*Ifpaddr
,
644 struct sockaddr
*Ifaaddr
,
645 struct sockaddr
*Gate
)
647 struct ifaddr
*ifa
= 0;
648 struct ifnet
*ifp
= 0;
650 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
652 /* trigger route cache reevaluation */
656 /* new gateway could require new ifaddr, ifp;
657 flags may also be different; ifp may be specified
658 by ll sockaddr when protocol address is ambiguous */
659 if (Ifpaddr
&& (ifa
= ifa_ifwithnet(Ifpaddr
)) &&
660 (ifp
= ifa
->ifa_ifp
) && (Ifaaddr
|| Gate
)) {
662 ifa
= ifaof_ifpforaddr(Ifaaddr
? Ifaaddr
: Gate
,
671 if (Ifpaddr
&& (ifp
= if_withname(Ifpaddr
)) ) {
673 ifa
= ifaof_ifpforaddr(Gate
, ifp
);
676 ifnet_lock_shared(ifp
);
677 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
679 ifnet_lock_done(ifp
);
682 else if (Ifaaddr
&& (ifa
= ifa_ifwithaddr(Ifaaddr
))) {
685 else if (Gate
&& (ifa
= ifa_ifwithroute_locked(rt
->rt_flags
,
686 rt_key(rt
), Gate
))) {
691 struct ifaddr
*oifa
= rt
->rt_ifa
;
693 if (oifa
&& oifa
->ifa_rtrequest
)
694 oifa
->ifa_rtrequest(RTM_DELETE
,
698 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
699 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
700 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
709 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
710 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
711 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
716 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
717 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
721 * Extract the addresses of the passed sockaddrs.
722 * Do a little sanity checking so as to avoid bad memory references.
723 * This data is derived straight from userland.
726 rt_xaddrs(caddr_t cp
, caddr_t cplim
, struct rt_addrinfo
*rtinfo
)
731 bzero(rtinfo
->rti_info
, sizeof(rtinfo
->rti_info
));
732 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
733 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
735 sa
= (struct sockaddr
*)cp
;
739 if ( (cp
+ sa
->sa_len
) > cplim
) {
744 * there are no more.. quit now
745 * If there are more bits, they are in error.
746 * I've seen this. route(1) can evidently generate these.
747 * This causes kernel to core dump.
748 * for compatibility, If we see this, point to a safe address.
750 if (sa
->sa_len
== 0) {
751 rtinfo
->rti_info
[i
] = &sa_zero
;
752 return (0); /* should be EINVAL but for compat */
756 rtinfo
->rti_info
[i
] = sa
;
765 struct rt_addrinfo
*rtinfo
)
767 struct rt_msghdr
*rtm
;
777 len
= sizeof(struct ifa_msghdr
);
782 len
= sizeof(struct ifma_msghdr
);
786 len
= sizeof(struct if_msghdr
);
790 len
= sizeof(struct rt_msghdr
);
794 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
795 if (m
&& len
> MHLEN
) {
796 MCLGET(m
, M_DONTWAIT
);
797 if ((m
->m_flags
& M_EXT
) == 0) {
804 m
->m_pkthdr
.len
= m
->m_len
= len
;
805 m
->m_pkthdr
.rcvif
= 0;
806 rtm
= mtod(m
, struct rt_msghdr
*);
807 bzero((caddr_t
)rtm
, len
);
808 for (i
= 0; i
< RTAX_MAX
; i
++) {
809 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
811 rtinfo
->rti_addrs
|= (1 << i
);
812 dlen
= ROUNDUP(sa
->sa_len
);
813 m_copyback(m
, len
, dlen
, (caddr_t
)sa
);
816 if (m
->m_pkthdr
.len
!= len
) {
820 rtm
->rtm_msglen
= len
;
821 rtm
->rtm_version
= RTM_VERSION
;
822 rtm
->rtm_type
= type
;
827 rt_msg2(int type
, struct rt_addrinfo
*rtinfo
, caddr_t cp
, struct walkarg
*w
)
830 int len
, dlen
, second_time
= 0;
833 rtinfo
->rti_addrs
= 0;
839 len
= sizeof(struct ifa_msghdr
);
844 len
= sizeof(struct ifma_msghdr
);
848 len
= sizeof(struct if_msghdr
);
852 len
= sizeof(struct if_msghdr2
);
856 len
= sizeof(struct ifma_msghdr2
);
860 len
= sizeof(struct rt_msghdr2
);
864 len
= sizeof(struct rt_msghdr
);
869 for (i
= 0; i
< RTAX_MAX
; i
++) {
872 if ((sa
= rtinfo
->rti_info
[i
]) == 0)
874 rtinfo
->rti_addrs
|= (1 << i
);
875 dlen
= ROUNDUP(sa
->sa_len
);
877 bcopy((caddr_t
)sa
, cp
, (unsigned)dlen
);
882 if (cp
== 0 && w
!= NULL
&& !second_time
) {
883 struct walkarg
*rw
= w
;
886 if (rw
->w_tmemsize
< len
) {
888 FREE(rw
->w_tmem
, M_RTABLE
);
889 rw
->w_tmem
= (caddr_t
)
890 _MALLOC(len
, M_RTABLE
, M_WAITOK
); /*###LD0412 was NOWAIT */
892 rw
->w_tmemsize
= len
;
902 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)cp0
;
904 rtm
->rtm_version
= RTM_VERSION
;
905 rtm
->rtm_type
= type
;
906 rtm
->rtm_msglen
= len
;
912 * This routine is called to generate a message from the routing
913 * socket indicating that a redirect has occurred, a routing lookup
914 * has failed, or that a protocol has detected timeouts to a particular
918 rt_missmsg(int type
, struct rt_addrinfo
*rtinfo
, int flags
, int error
)
920 struct rt_msghdr
*rtm
;
922 struct sockaddr
*sa
= rtinfo
->rti_info
[RTAX_DST
];
924 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
926 if (route_cb
.any_count
== 0)
928 m
= rt_msg1(type
, rtinfo
);
931 rtm
= mtod(m
, struct rt_msghdr
*);
932 rtm
->rtm_flags
= RTF_DONE
| flags
;
933 rtm
->rtm_errno
= error
;
934 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
935 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
936 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
940 * This routine is called to generate a message from the routing
941 * socket indicating that the status of a network interface has changed.
947 struct if_msghdr
*ifm
;
949 struct rt_addrinfo info
;
951 if (route_cb
.any_count
== 0)
953 bzero((caddr_t
)&info
, sizeof(info
));
954 m
= rt_msg1(RTM_IFINFO
, &info
);
957 ifm
= mtod(m
, struct if_msghdr
*);
958 ifm
->ifm_index
= ifp
->if_index
;
959 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
960 if_data_internal_to_if_data(ifp
, &ifp
->if_data
, &ifm
->ifm_data
);
962 route_proto
.sp_protocol
= 0;
963 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
967 * This is called to generate messages from the routing socket
968 * indicating a network interface has had addresses associated with it.
969 * if we ever reverse the logic and replace messages TO the routing
970 * socket indicate a request to configure interfaces, then it will
971 * be unnecessary as the routing socket will automatically generate
974 * Since this is coming from the interface, it is expected that the
975 * interface will be locked.
978 rt_newaddrmsg(int cmd
, struct ifaddr
*ifa
, int error
, struct rtentry
*rt
)
980 struct rt_addrinfo info
;
981 struct sockaddr
*sa
= 0;
984 struct ifnet
*ifp
= ifa
->ifa_ifp
;
986 if (route_cb
.any_count
== 0)
988 for (pass
= 1; pass
< 3; pass
++) {
989 bzero((caddr_t
)&info
, sizeof(info
));
990 if ((cmd
== RTM_ADD
&& pass
== 1) ||
991 (cmd
== RTM_DELETE
&& pass
== 2)) {
992 struct ifa_msghdr
*ifam
;
993 int ncmd
= cmd
== RTM_ADD
? RTM_NEWADDR
: RTM_DELADDR
;
995 ifaaddr
= sa
= ifa
->ifa_addr
;
996 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
997 netmask
= ifa
->ifa_netmask
;
998 brdaddr
= ifa
->ifa_dstaddr
;
999 if ((m
= rt_msg1(ncmd
, &info
)) == NULL
)
1001 ifam
= mtod(m
, struct ifa_msghdr
*);
1002 ifam
->ifam_index
= ifp
->if_index
;
1003 ifam
->ifam_metric
= ifa
->ifa_metric
;
1004 ifam
->ifam_flags
= ifa
->ifa_flags
;
1005 ifam
->ifam_addrs
= info
.rti_addrs
;
1007 if ((cmd
== RTM_ADD
&& pass
== 2) ||
1008 (cmd
== RTM_DELETE
&& pass
== 1)) {
1009 struct rt_msghdr
*rtm
;
1013 netmask
= rt_mask(rt
);
1014 dst
= sa
= rt_key(rt
);
1015 gate
= rt
->rt_gateway
;
1016 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1018 rtm
= mtod(m
, struct rt_msghdr
*);
1019 rtm
->rtm_index
= ifp
->if_index
;
1020 rtm
->rtm_flags
|= rt
->rt_flags
;
1021 rtm
->rtm_errno
= error
;
1022 rtm
->rtm_addrs
= info
.rti_addrs
;
1024 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
1025 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1030 * This is the analogue to the rt_newaddrmsg which performs the same
1031 * function but for multicast group memberhips. This is easier since
1032 * there is no route state to worry about.
1035 rt_newmaddrmsg(int cmd
, struct ifmultiaddr
*ifma
)
1037 struct rt_addrinfo info
;
1039 struct ifnet
*ifp
= ifma
->ifma_ifp
;
1040 struct ifma_msghdr
*ifmam
;
1042 if (route_cb
.any_count
== 0)
1045 bzero((caddr_t
)&info
, sizeof(info
));
1046 ifaaddr
= ifma
->ifma_addr
;
1047 if (ifp
&& ifp
->if_addrhead
.tqh_first
)
1048 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1052 * If a link-layer address is present, present it as a ``gateway''
1053 * (similarly to how ARP entries, e.g., are presented).
1055 gate
= ifma
->ifma_ll
->ifma_addr
;
1056 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1058 ifmam
= mtod(m
, struct ifma_msghdr
*);
1059 ifmam
->ifmam_index
= ifp
? ifp
->if_index
: 0;
1060 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1061 route_proto
.sp_protocol
= ifma
->ifma_addr
->sa_family
;
1062 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1066 * This is used in dumping the kernel table via sysctl().
1069 sysctl_dumpentry(struct radix_node
*rn
, void *vw
)
1071 struct walkarg
*w
= vw
;
1072 struct rtentry
*rt
= (struct rtentry
*)rn
;
1073 int error
= 0, size
;
1074 struct rt_addrinfo info
;
1076 if (w
->w_op
== NET_RT_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
1078 bzero((caddr_t
)&info
, sizeof(info
));
1080 gate
= rt
->rt_gateway
;
1081 netmask
= rt_mask(rt
);
1082 genmask
= rt
->rt_genmask
;
1083 if (w
->w_op
!= NET_RT_DUMP2
) {
1084 size
= rt_msg2(RTM_GET
, &info
, 0, w
);
1085 if (w
->w_req
&& w
->w_tmem
) {
1086 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)w
->w_tmem
;
1088 rtm
->rtm_flags
= rt
->rt_flags
;
1089 rtm
->rtm_use
= rt
->rt_use
;
1090 rtm
->rtm_rmx
= rt
->rt_rmx
;
1091 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1095 rtm
->rtm_addrs
= info
.rti_addrs
;
1096 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1100 size
= rt_msg2(RTM_GET2
, &info
, 0, w
);
1101 if (w
->w_req
&& w
->w_tmem
) {
1102 struct rt_msghdr2
*rtm
= (struct rt_msghdr2
*)w
->w_tmem
;
1104 rtm
->rtm_flags
= rt
->rt_flags
;
1105 rtm
->rtm_use
= rt
->rt_use
;
1106 rtm
->rtm_rmx
= rt
->rt_rmx
;
1107 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1108 rtm
->rtm_refcnt
= rt
->rt_refcnt
;
1110 rtm
->rtm_parentflags
= rt
->rt_parent
->rt_flags
;
1112 rtm
->rtm_parentflags
= 0;
1113 rtm
->rtm_reserved
= 0;
1114 rtm
->rtm_addrs
= info
.rti_addrs
;
1115 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1130 struct rt_addrinfo info
;
1133 bzero((caddr_t
)&info
, sizeof(info
));
1134 ifnet_head_lock_shared();
1135 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1138 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1140 ifnet_lock_shared(ifp
);
1141 ifa
= ifp
->if_addrhead
.tqh_first
;
1142 ifpaddr
= ifa
->ifa_addr
;
1143 len
= rt_msg2(RTM_IFINFO
, &info
, (caddr_t
)0, w
);
1145 if (w
->w_req
&& w
->w_tmem
) {
1146 struct if_msghdr
*ifm
;
1148 ifm
= (struct if_msghdr
*)w
->w_tmem
;
1149 ifm
->ifm_index
= ifp
->if_index
;
1150 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1151 if_data_internal_to_if_data(ifp
, &ifp
->if_data
, &ifm
->ifm_data
);
1152 ifm
->ifm_addrs
= info
.rti_addrs
;
1153 error
= SYSCTL_OUT(w
->w_req
,(caddr_t
)ifm
, len
);
1155 ifnet_lock_done(ifp
);
1159 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1160 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1162 ifaaddr
= ifa
->ifa_addr
;
1163 netmask
= ifa
->ifa_netmask
;
1164 brdaddr
= ifa
->ifa_dstaddr
;
1165 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1166 if (w
->w_req
&& w
->w_tmem
) {
1167 struct ifa_msghdr
*ifam
;
1169 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1170 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1171 ifam
->ifam_flags
= ifa
->ifa_flags
;
1172 ifam
->ifam_metric
= ifa
->ifa_metric
;
1173 ifam
->ifam_addrs
= info
.rti_addrs
;
1174 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1179 ifnet_lock_done(ifp
);
1180 ifaaddr
= netmask
= brdaddr
= 0;
1193 struct rt_addrinfo info
;
1196 bzero((caddr_t
)&info
, sizeof(info
));
1197 ifnet_head_lock_shared();
1198 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1201 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1203 ifnet_lock_shared(ifp
);
1204 ifa
= ifp
->if_addrhead
.tqh_first
;
1205 ifpaddr
= ifa
->ifa_addr
;
1206 len
= rt_msg2(RTM_IFINFO2
, &info
, (caddr_t
)0, w
);
1208 if (w
->w_req
&& w
->w_tmem
) {
1209 struct if_msghdr2
*ifm
;
1211 ifm
= (struct if_msghdr2
*)w
->w_tmem
;
1212 ifm
->ifm_addrs
= info
.rti_addrs
;
1213 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1214 ifm
->ifm_index
= ifp
->if_index
;
1215 ifm
->ifm_snd_len
= ifp
->if_snd
.ifq_len
;
1216 ifm
->ifm_snd_maxlen
= ifp
->if_snd
.ifq_maxlen
;
1217 ifm
->ifm_snd_drops
= ifp
->if_snd
.ifq_drops
;
1218 ifm
->ifm_timer
= ifp
->if_timer
;
1219 if_data_internal_to_if_data64(ifp
, &ifp
->if_data
, &ifm
->ifm_data
);
1220 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1222 ifnet_lock_done(ifp
);
1226 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1227 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1229 ifaaddr
= ifa
->ifa_addr
;
1230 netmask
= ifa
->ifa_netmask
;
1231 brdaddr
= ifa
->ifa_dstaddr
;
1232 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1233 if (w
->w_req
&& w
->w_tmem
) {
1234 struct ifa_msghdr
*ifam
;
1236 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1237 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1238 ifam
->ifam_flags
= ifa
->ifa_flags
;
1239 ifam
->ifam_metric
= ifa
->ifa_metric
;
1240 ifam
->ifam_addrs
= info
.rti_addrs
;
1241 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1247 ifnet_lock_done(ifp
);
1251 struct ifmultiaddr
*ifma
;
1253 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1254 ifma
= ifma
->ifma_link
.le_next
) {
1255 if (af
&& af
!= ifma
->ifma_addr
->sa_family
)
1257 bzero((caddr_t
)&info
, sizeof(info
));
1258 ifaaddr
= ifma
->ifma_addr
;
1259 if (ifp
->if_addrhead
.tqh_first
)
1260 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1262 gate
= ifma
->ifma_ll
->ifma_addr
;
1263 len
= rt_msg2(RTM_NEWMADDR2
, &info
, 0, w
);
1264 if (w
->w_req
&& w
->w_tmem
) {
1265 struct ifma_msghdr2
*ifmam
;
1267 ifmam
= (struct ifma_msghdr2
*)w
->w_tmem
;
1268 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1269 ifmam
->ifmam_flags
= 0;
1270 ifmam
->ifmam_index
= ifma
->ifma_ifp
->if_index
;
1271 ifmam
->ifmam_refcount
= ifma
->ifma_refcount
;
1272 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1278 ifnet_lock_done(ifp
);
1279 ifaaddr
= netmask
= brdaddr
= 0;
1287 sysctl_rtstat(struct sysctl_req
*req
)
1291 error
= SYSCTL_OUT(req
, &rtstat
, sizeof(struct rtstat
));
1299 sysctl_rttrash(struct sysctl_req
*req
)
1303 error
= SYSCTL_OUT(req
, &rttrash
, sizeof(rttrash
));
1312 sysctl_rtsock SYSCTL_HANDLER_ARGS
1314 int *name
= (int *)arg1
;
1315 u_int namelen
= arg2
;
1316 struct radix_node_head
*rnh
;
1317 int i
, error
= EINVAL
;
1328 Bzero(&w
, sizeof(w
));
1338 lck_mtx_lock(rt_mtx
);
1339 for (i
= 1; i
<= AF_MAX
; i
++)
1340 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
1341 (error
= rnh
->rnh_walktree(rnh
,
1342 sysctl_dumpentry
, &w
)))
1344 lck_mtx_unlock(rt_mtx
);
1347 error
= sysctl_iflist(af
, &w
);
1349 case NET_RT_IFLIST2
:
1350 error
= sysctl_iflist2(af
, &w
);
1353 error
= sysctl_rtstat(req
);
1356 error
= sysctl_rttrash(req
);
1360 FREE(w
.w_tmem
, M_RTABLE
);
1364 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
, sysctl_rtsock
, "");
1367 * Definitions of protocols supported in the ROUTE domain.
1370 struct domain routedomain
; /* or at least forward */
1372 static struct protosw routesw
[] = {
1373 { SOCK_RAW
, &routedomain
, 0, PR_ATOMIC
|PR_ADDR
,
1374 0, route_output
, raw_ctlinput
, 0,
1384 struct domain routedomain
=
1385 { PF_ROUTE
, "route", route_init
, 0, 0,
1387 NULL
, NULL
, 0, 0, 0, 0, NULL
, 0,