2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1988, 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)rtsock.c 8.5 (Berkeley) 11/2/94
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
63 #include <sys/malloc.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/domain.h>
68 #include <sys/protosw.h>
69 #include <sys/syslog.h>
70 #include <kern/lock.h>
73 #include <net/route.h>
74 #include <net/raw_cb.h>
75 #include <netinet/in.h>
77 #include <machine/spl.h>
79 extern void m_copydata(struct mbuf
*, int, int, caddr_t
);
80 extern void m_copyback(struct mbuf
*, int, int, caddr_t
);
82 extern struct rtstat rtstat
;
85 MALLOC_DEFINE(M_RTABLE
, "routetbl", "routing tables");
87 extern lck_mtx_t
*rt_mtx
;
88 static struct sockaddr route_dst
= { 2, PF_ROUTE
, { 0, } };
89 static struct sockaddr route_src
= { 2, PF_ROUTE
, { 0, } };
90 static struct sockaddr sa_zero
= { sizeof(sa_zero
), AF_INET
, { 0, } };
91 static struct sockproto route_proto
= { PF_ROUTE
, 0 };
97 struct sysctl_req
*w_req
;
101 rt_msg1(int, struct rt_addrinfo
*);
102 static int rt_msg2(int, struct rt_addrinfo
*, caddr_t
, struct walkarg
*);
103 static int rt_xaddrs(caddr_t
, caddr_t
, struct rt_addrinfo
*);
104 static int sysctl_dumpentry(struct radix_node
*rn
, void *vw
);
105 static int sysctl_iflist(int af
, struct walkarg
*w
);
106 static int sysctl_iflist2(int af
, struct walkarg
*w
);
107 static int route_output(struct mbuf
*, struct socket
*);
108 static void rt_setmetrics(u_long
, struct rt_metrics
*, struct rt_metrics
*);
109 static void rt_setif(struct rtentry
*, struct sockaddr
*, struct sockaddr
*,
112 /* Sleazy use of local variables throughout file, warning!!!! */
113 #define dst info.rti_info[RTAX_DST]
114 #define gate info.rti_info[RTAX_GATEWAY]
115 #define netmask info.rti_info[RTAX_NETMASK]
116 #define genmask info.rti_info[RTAX_GENMASK]
117 #define ifpaddr info.rti_info[RTAX_IFP]
118 #define ifaaddr info.rti_info[RTAX_IFA]
119 #define brdaddr info.rti_info[RTAX_BRD]
122 * It really doesn't make any sense at all for this code to share much
123 * with raw_usrreq.c, since its functionality is so restricted. XXX
126 rts_abort(struct socket
*so
)
130 error
= raw_usrreqs
.pru_abort(so
);
134 /* pru_accept is EOPNOTSUPP */
137 rts_attach(struct socket
*so
, int proto
, __unused
struct proc
*p
)
142 if (sotorawcb(so
) != 0)
143 return EISCONN
; /* XXX panic? */
144 MALLOC(rp
, struct rawcb
*, sizeof *rp
, M_PCB
, M_WAITOK
); /* XXX */
147 bzero(rp
, sizeof *rp
);
150 * The splnet() is necessary to block protocols from sending
151 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
152 * this PCB is extant but incompletely initialized.
153 * Probably we should try to do more of this work beforehand and
156 so
->so_pcb
= (caddr_t
)rp
;
157 error
= raw_attach(so
, proto
); /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
162 so
->so_flags
|= SOF_PCBCLEARING
;
166 switch(rp
->rcb_proto
.sp_protocol
) {
167 //####LD route_cb needs looking
172 route_cb
.ip6_count
++;
175 route_cb
.ipx_count
++;
181 rp
->rcb_faddr
= &route_src
;
182 route_cb
.any_count
++;
184 so
->so_options
|= SO_USELOOPBACK
;
185 socket_unlock(so
, 1);
190 rts_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
194 error
= raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
200 rts_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
204 error
= raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
209 /* pru_connect2 is EOPNOTSUPP */
210 /* pru_control is EOPNOTSUPP */
213 rts_detach(struct socket
*so
)
215 struct rawcb
*rp
= sotorawcb(so
);
220 switch(rp
->rcb_proto
.sp_protocol
) {
225 route_cb
.ip6_count
--;
228 route_cb
.ipx_count
--;
234 route_cb
.any_count
--;
236 error
= raw_usrreqs
.pru_detach(so
);
242 rts_disconnect(struct socket
*so
)
246 error
= raw_usrreqs
.pru_disconnect(so
);
251 /* pru_listen is EOPNOTSUPP */
254 rts_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
258 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
263 /* pru_rcvd is EOPNOTSUPP */
264 /* pru_rcvoob is EOPNOTSUPP */
267 rts_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
268 struct mbuf
*control
, struct proc
*p
)
272 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
277 /* pru_sense is null */
280 rts_shutdown(struct socket
*so
)
284 error
= raw_usrreqs
.pru_shutdown(so
);
290 rts_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
294 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
299 static struct pr_usrreqs route_usrreqs
= {
300 rts_abort
, pru_accept_notsupp
, rts_attach
, rts_bind
,
301 rts_connect
, pru_connect2_notsupp
, pru_control_notsupp
,
302 rts_detach
, rts_disconnect
, pru_listen_notsupp
, rts_peeraddr
,
303 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, rts_send
, pru_sense_null
,
304 rts_shutdown
, rts_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
313 struct rt_msghdr
*rtm
= 0;
314 struct rtentry
*rt
= 0;
315 struct rtentry
*saved_nrt
= 0;
316 struct radix_node_head
*rnh
;
317 struct rt_addrinfo info
;
319 struct ifnet
*ifp
= 0;
321 struct proc
*curproc
= current_proc();
323 int sendonlytoself
= 0;
325 #define senderr(e) { error = e; goto flush;}
326 if (m
== 0 || ((m
->m_len
< sizeof(long)) && (m
= m_pullup(m
, sizeof(long))) == 0))
328 if ((m
->m_flags
& M_PKTHDR
) == 0)
329 panic("route_output");
331 /* unlock the socket (but keep a reference) it won't be accessed until raw_input appends to it. */
332 socket_unlock(so
, 0);
333 lck_mtx_lock(rt_mtx
);
335 len
= m
->m_pkthdr
.len
;
336 if (len
< sizeof(*rtm
) ||
337 len
!= mtod(m
, struct rt_msghdr
*)->rtm_msglen
) {
341 R_Malloc(rtm
, struct rt_msghdr
*, len
);
346 m_copydata(m
, 0, len
, (caddr_t
)rtm
);
347 if (rtm
->rtm_version
!= RTM_VERSION
) {
349 senderr(EPROTONOSUPPORT
);
353 * Silent version of RTM_GET for Reachabiltiy APIs. We may change
354 * all RTM_GETs to be silent in the future, so this is private for now.
356 if (rtm
->rtm_type
== RTM_GET_SILENT
) {
357 if ((so
->so_options
& SO_USELOOPBACK
) == 0)
360 rtm
->rtm_type
= RTM_GET
;
364 * Perform permission checking, only privileged sockets
365 * may perform operations other than RTM_GET
367 if (rtm
->rtm_type
!= RTM_GET
&& (so
->so_state
& SS_PRIV
) == 0) {
372 rtm
->rtm_pid
= proc_selfpid();
373 info
.rti_addrs
= rtm
->rtm_addrs
;
374 if (rt_xaddrs((caddr_t
)(rtm
+ 1), len
+ (caddr_t
)rtm
, &info
)) {
378 if (dst
== 0 || (dst
->sa_family
>= AF_MAX
)
379 || (gate
!= 0 && (gate
->sa_family
>= AF_MAX
))) {
383 struct radix_node
*t
;
384 t
= rn_addmask((caddr_t
)genmask
, 0, 1);
385 if (t
&& Bcmp(genmask
, t
->rn_key
, *(u_char
*)genmask
) == 0)
386 genmask
= (struct sockaddr
*)(t
->rn_key
);
390 switch (rtm
->rtm_type
) {
397 /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
398 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
399 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
400 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
401 * That circular route was ignored on previous version of MacOS X because of a routing bug
402 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
403 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
404 * made in order to provide binary compatibility with AOL.
405 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
406 * confusing the routing table with a wrong route to the previous default gateway
409 extern int check_routeselfref
;
410 #define satosinaddr(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr)
412 if (check_routeselfref
&& (dst
&& dst
->sa_family
== AF_INET
) &&
413 (netmask
&& satosinaddr(netmask
) == INADDR_BROADCAST
) &&
414 (gate
&& satosinaddr(dst
) == satosinaddr(gate
))) {
415 log(LOG_WARNING
, "route_output: circular route %ld.%ld.%ld.%ld/32 ignored\n",
416 (ntohl(satosinaddr(gate
)>>24))&0xff,
417 (ntohl(satosinaddr(gate
)>>16))&0xff,
418 (ntohl(satosinaddr(gate
)>>8))&0xff,
419 (ntohl(satosinaddr(gate
)))&0xff);
425 error
= rtrequest_locked(RTM_ADD
, dst
, gate
, netmask
,
426 rtm
->rtm_flags
, &saved_nrt
);
427 if (error
== 0 && saved_nrt
) {
430 * If the route request specified an interface with
431 * IFA and/or IFP, we set the requested interface on
432 * the route with rt_setif. It would be much better
433 * to do this inside rtrequest, but that would
434 * require passing the desired interface, in some
435 * form, to rtrequest. Since rtrequest is called in
436 * so many places (roughly 40 in our source), adding
437 * a parameter is to much for us to swallow; this is
438 * something for the FreeBSD developers to tackle.
439 * Instead, we let rtrequest compute whatever
440 * interface it wants, then come in behind it and
441 * stick in the interface that we really want. This
442 * works reasonably well except when rtrequest can't
443 * figure out what interface to use (with
444 * ifa_withroute) and returns ENETUNREACH. Ideally
445 * it shouldn't matter if rtrequest can't figure out
446 * the interface if we're going to explicitly set it
447 * ourselves anyway. But practically we can't
448 * recover here because rtrequest will not do any of
449 * the work necessary to add the route if it can't
450 * find an interface. As long as there is a default
451 * route that leads to some interface, rtrequest will
452 * find an interface, so this problem should be
453 * rarely encountered.
457 rt_setif(saved_nrt
, ifpaddr
, ifaaddr
, gate
);
459 rt_setmetrics(rtm
->rtm_inits
,
460 &rtm
->rtm_rmx
, &saved_nrt
->rt_rmx
);
461 saved_nrt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
462 saved_nrt
->rt_rmx
.rmx_locks
|=
463 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
465 saved_nrt
->rt_genmask
= genmask
;
470 error
= rtrequest_locked(RTM_DELETE
, dst
, gate
, netmask
,
471 rtm
->rtm_flags
, &saved_nrt
);
473 if ((rt
= saved_nrt
))
482 if ((rnh
= rt_tables
[dst
->sa_family
]) == 0) {
483 senderr(EAFNOSUPPORT
);
484 } else if ((rt
= (struct rtentry
*)
485 rnh
->rnh_lookup(dst
, netmask
, rnh
)) != NULL
)
489 switch(rtm
->rtm_type
) {
495 gate
= rt
->rt_gateway
;
496 netmask
= rt_mask(rt
);
497 genmask
= rt
->rt_genmask
;
498 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
501 ifnet_lock_shared(ifp
);
502 ifa2
= ifp
->if_addrhead
.tqh_first
;
503 ifpaddr
= ifa2
->ifa_addr
;
504 ifnet_lock_done(ifp
);
505 ifaaddr
= rt
->rt_ifa
->ifa_addr
;
506 rtm
->rtm_index
= ifp
->if_index
;
512 len
= rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)0,
513 (struct walkarg
*)0);
514 if (len
> rtm
->rtm_msglen
) {
515 struct rt_msghdr
*new_rtm
;
516 R_Malloc(new_rtm
, struct rt_msghdr
*, len
);
520 Bcopy(rtm
, new_rtm
, rtm
->rtm_msglen
);
521 R_Free(rtm
); rtm
= new_rtm
;
523 (void)rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)rtm
,
524 (struct walkarg
*)0);
525 rtm
->rtm_flags
= rt
->rt_flags
;
526 rtm
->rtm_rmx
= rt
->rt_rmx
;
527 rtm
->rtm_addrs
= info
.rti_addrs
;
532 if (gate
&& (error
= rt_setgate(rt
, rt_key(rt
), gate
)))
536 * If they tried to change things but didn't specify
537 * the required gateway, then just use the old one.
538 * This can happen if the user tries to change the
539 * flags on the default route without changing the
540 * default gateway. Changing flags still doesn't work.
542 if ((rt
->rt_flags
& RTF_GATEWAY
) && !gate
)
543 gate
= rt
->rt_gateway
;
547 * On Darwin, we call rt_setif which contains the
548 * equivalent to the code found at this very spot
551 rt_setif(rt
, ifpaddr
, ifaaddr
, gate
);
554 rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
,
557 /* rt_setif, called above does this for us on darwin */
558 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
559 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, gate
);
562 rt
->rt_genmask
= genmask
;
567 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
568 rt
->rt_rmx
.rmx_locks
|=
569 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
580 rtm
->rtm_errno
= error
;
582 rtm
->rtm_flags
|= RTF_DONE
;
586 lck_mtx_unlock(rt_mtx
);
587 socket_lock(so
, 0); /* relock the socket now */
589 struct rawcb
*rp
= 0;
591 * Check to see if we don't want our own messages.
593 if ((so
->so_options
& SO_USELOOPBACK
) == 0) {
594 if (route_cb
.any_count
<= 1) {
600 /* There is another listener, so construct message */
604 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
605 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
608 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
)
609 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
612 if (sendonlytoself
&& m
) {
614 if (sbappendaddr(&so
->so_rcv
, &route_src
, m
, (struct mbuf
*)0, &error
) != 0) {
621 rp
->rcb_proto
.sp_family
= 0; /* Avoid us */
623 route_proto
.sp_protocol
= dst
->sa_family
;
625 socket_unlock(so
, 0);
626 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
630 rp
->rcb_proto
.sp_family
= PF_ROUTE
;
637 rt_setmetrics(which
, in
, out
)
639 struct rt_metrics
*in
, *out
;
641 #define metric(f, e) if (which & (f)) out->e = in->e;
642 metric(RTV_RPIPE
, rmx_recvpipe
);
643 metric(RTV_SPIPE
, rmx_sendpipe
);
644 metric(RTV_SSTHRESH
, rmx_ssthresh
);
645 metric(RTV_RTT
, rmx_rtt
);
646 metric(RTV_RTTVAR
, rmx_rttvar
);
647 metric(RTV_HOPCOUNT
, rmx_hopcount
);
648 metric(RTV_MTU
, rmx_mtu
);
649 metric(RTV_EXPIRE
, rmx_expire
);
654 * Set route's interface given ifpaddr, ifaaddr, and gateway.
659 struct sockaddr
*Ifpaddr
,
660 struct sockaddr
*Ifaaddr
,
661 struct sockaddr
*Gate
)
663 struct ifaddr
*ifa
= 0;
664 struct ifnet
*ifp
= 0;
666 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
668 /* new gateway could require new ifaddr, ifp;
669 flags may also be different; ifp may be specified
670 by ll sockaddr when protocol address is ambiguous */
671 if (Ifpaddr
&& (ifa
= ifa_ifwithnet(Ifpaddr
)) &&
672 (ifp
= ifa
->ifa_ifp
) && (Ifaaddr
|| Gate
)) {
674 ifa
= ifaof_ifpforaddr(Ifaaddr
? Ifaaddr
: Gate
,
683 if (Ifpaddr
&& (ifp
= if_withname(Ifpaddr
)) ) {
685 ifa
= ifaof_ifpforaddr(Gate
, ifp
);
688 ifnet_lock_shared(ifp
);
689 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
691 ifnet_lock_done(ifp
);
694 else if (Ifaaddr
&& (ifa
= ifa_ifwithaddr(Ifaaddr
))) {
697 else if (Gate
&& (ifa
= ifa_ifwithroute(rt
->rt_flags
,
698 rt_key(rt
), Gate
))) {
703 struct ifaddr
*oifa
= rt
->rt_ifa
;
705 if (oifa
&& oifa
->ifa_rtrequest
)
706 oifa
->ifa_rtrequest(RTM_DELETE
,
710 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
711 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
712 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
721 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
722 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
723 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
728 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
729 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
733 * Extract the addresses of the passed sockaddrs.
734 * Do a little sanity checking so as to avoid bad memory references.
735 * This data is derived straight from userland.
738 rt_xaddrs(cp
, cplim
, rtinfo
)
740 struct rt_addrinfo
*rtinfo
;
745 bzero(rtinfo
->rti_info
, sizeof(rtinfo
->rti_info
));
746 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
747 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
749 sa
= (struct sockaddr
*)cp
;
753 if ( (cp
+ sa
->sa_len
) > cplim
) {
758 * there are no more.. quit now
759 * If there are more bits, they are in error.
760 * I've seen this. route(1) can evidently generate these.
761 * This causes kernel to core dump.
762 * for compatibility, If we see this, point to a safe address.
764 if (sa
->sa_len
== 0) {
765 rtinfo
->rti_info
[i
] = &sa_zero
;
766 return (0); /* should be EINVAL but for compat */
770 rtinfo
->rti_info
[i
] = sa
;
779 struct rt_addrinfo
*rtinfo
)
781 struct rt_msghdr
*rtm
;
791 len
= sizeof(struct ifa_msghdr
);
796 len
= sizeof(struct ifma_msghdr
);
800 len
= sizeof(struct if_msghdr
);
804 len
= sizeof(struct rt_msghdr
);
808 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
809 if (m
&& len
> MHLEN
) {
810 MCLGET(m
, M_DONTWAIT
);
811 if ((m
->m_flags
& M_EXT
) == 0) {
818 m
->m_pkthdr
.len
= m
->m_len
= len
;
819 m
->m_pkthdr
.rcvif
= 0;
820 rtm
= mtod(m
, struct rt_msghdr
*);
821 bzero((caddr_t
)rtm
, len
);
822 for (i
= 0; i
< RTAX_MAX
; i
++) {
823 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
825 rtinfo
->rti_addrs
|= (1 << i
);
826 dlen
= ROUNDUP(sa
->sa_len
);
827 m_copyback(m
, len
, dlen
, (caddr_t
)sa
);
830 if (m
->m_pkthdr
.len
!= len
) {
834 rtm
->rtm_msglen
= len
;
835 rtm
->rtm_version
= RTM_VERSION
;
836 rtm
->rtm_type
= type
;
841 rt_msg2(type
, rtinfo
, cp
, w
)
843 struct rt_addrinfo
*rtinfo
;
848 int len
, dlen
, second_time
= 0;
851 rtinfo
->rti_addrs
= 0;
857 len
= sizeof(struct ifa_msghdr
);
862 len
= sizeof(struct ifma_msghdr
);
866 len
= sizeof(struct if_msghdr
);
870 len
= sizeof(struct if_msghdr2
);
874 len
= sizeof(struct ifma_msghdr2
);
878 len
= sizeof(struct rt_msghdr2
);
882 len
= sizeof(struct rt_msghdr
);
887 for (i
= 0; i
< RTAX_MAX
; i
++) {
890 if ((sa
= rtinfo
->rti_info
[i
]) == 0)
892 rtinfo
->rti_addrs
|= (1 << i
);
893 dlen
= ROUNDUP(sa
->sa_len
);
895 bcopy((caddr_t
)sa
, cp
, (unsigned)dlen
);
900 if (cp
== 0 && w
!= NULL
&& !second_time
) {
901 struct walkarg
*rw
= w
;
904 if (rw
->w_tmemsize
< len
) {
906 FREE(rw
->w_tmem
, M_RTABLE
);
907 rw
->w_tmem
= (caddr_t
)
908 _MALLOC(len
, M_RTABLE
, M_WAITOK
); /*###LD0412 was NOWAIT */
910 rw
->w_tmemsize
= len
;
920 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)cp0
;
922 rtm
->rtm_version
= RTM_VERSION
;
923 rtm
->rtm_type
= type
;
924 rtm
->rtm_msglen
= len
;
930 * This routine is called to generate a message from the routing
931 * socket indicating that a redirect has occurred, a routing lookup
932 * has failed, or that a protocol has detected timeouts to a particular
936 rt_missmsg(type
, rtinfo
, flags
, error
)
937 int type
, flags
, error
;
938 struct rt_addrinfo
*rtinfo
;
940 struct rt_msghdr
*rtm
;
942 struct sockaddr
*sa
= rtinfo
->rti_info
[RTAX_DST
];
944 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
946 if (route_cb
.any_count
== 0)
948 m
= rt_msg1(type
, rtinfo
);
951 rtm
= mtod(m
, struct rt_msghdr
*);
952 rtm
->rtm_flags
= RTF_DONE
| flags
;
953 rtm
->rtm_errno
= error
;
954 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
955 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
956 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
960 * This routine is called to generate a message from the routing
961 * socket indicating that the status of a network interface has changed.
967 struct if_msghdr
*ifm
;
969 struct rt_addrinfo info
;
971 if (route_cb
.any_count
== 0)
973 bzero((caddr_t
)&info
, sizeof(info
));
974 m
= rt_msg1(RTM_IFINFO
, &info
);
977 ifm
= mtod(m
, struct if_msghdr
*);
978 ifm
->ifm_index
= ifp
->if_index
;
979 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
980 if_data_internal_to_if_data(&ifp
->if_data
, &ifm
->ifm_data
);
982 route_proto
.sp_protocol
= 0;
983 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
987 * This is called to generate messages from the routing socket
988 * indicating a network interface has had addresses associated with it.
989 * if we ever reverse the logic and replace messages TO the routing
990 * socket indicate a request to configure interfaces, then it will
991 * be unnecessary as the routing socket will automatically generate
994 * Since this is coming from the interface, it is expected that the
995 * interface will be locked.
998 rt_newaddrmsg(cmd
, ifa
, error
, rt
)
1003 struct rt_addrinfo info
;
1004 struct sockaddr
*sa
= 0;
1007 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1009 if (route_cb
.any_count
== 0)
1011 for (pass
= 1; pass
< 3; pass
++) {
1012 bzero((caddr_t
)&info
, sizeof(info
));
1013 if ((cmd
== RTM_ADD
&& pass
== 1) ||
1014 (cmd
== RTM_DELETE
&& pass
== 2)) {
1015 struct ifa_msghdr
*ifam
;
1016 int ncmd
= cmd
== RTM_ADD
? RTM_NEWADDR
: RTM_DELADDR
;
1018 ifaaddr
= sa
= ifa
->ifa_addr
;
1019 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1020 netmask
= ifa
->ifa_netmask
;
1021 brdaddr
= ifa
->ifa_dstaddr
;
1022 if ((m
= rt_msg1(ncmd
, &info
)) == NULL
)
1024 ifam
= mtod(m
, struct ifa_msghdr
*);
1025 ifam
->ifam_index
= ifp
->if_index
;
1026 ifam
->ifam_metric
= ifa
->ifa_metric
;
1027 ifam
->ifam_flags
= ifa
->ifa_flags
;
1028 ifam
->ifam_addrs
= info
.rti_addrs
;
1030 if ((cmd
== RTM_ADD
&& pass
== 2) ||
1031 (cmd
== RTM_DELETE
&& pass
== 1)) {
1032 struct rt_msghdr
*rtm
;
1036 netmask
= rt_mask(rt
);
1037 dst
= sa
= rt_key(rt
);
1038 gate
= rt
->rt_gateway
;
1039 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1041 rtm
= mtod(m
, struct rt_msghdr
*);
1042 rtm
->rtm_index
= ifp
->if_index
;
1043 rtm
->rtm_flags
|= rt
->rt_flags
;
1044 rtm
->rtm_errno
= error
;
1045 rtm
->rtm_addrs
= info
.rti_addrs
;
1047 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
1048 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1053 * This is the analogue to the rt_newaddrmsg which performs the same
1054 * function but for multicast group memberhips. This is easier since
1055 * there is no route state to worry about.
1058 rt_newmaddrmsg(cmd
, ifma
)
1060 struct ifmultiaddr
*ifma
;
1062 struct rt_addrinfo info
;
1064 struct ifnet
*ifp
= ifma
->ifma_ifp
;
1065 struct ifma_msghdr
*ifmam
;
1067 if (route_cb
.any_count
== 0)
1070 bzero((caddr_t
)&info
, sizeof(info
));
1071 ifaaddr
= ifma
->ifma_addr
;
1072 if (ifp
&& ifp
->if_addrhead
.tqh_first
)
1073 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1077 * If a link-layer address is present, present it as a ``gateway''
1078 * (similarly to how ARP entries, e.g., are presented).
1080 gate
= ifma
->ifma_ll
->ifma_addr
;
1081 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1083 ifmam
= mtod(m
, struct ifma_msghdr
*);
1084 ifmam
->ifmam_index
= ifp
? ifp
->if_index
: 0;
1085 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1086 route_proto
.sp_protocol
= ifma
->ifma_addr
->sa_family
;
1087 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1091 * This is used in dumping the kernel table via sysctl().
1094 sysctl_dumpentry(rn
, vw
)
1095 struct radix_node
*rn
;
1098 struct walkarg
*w
= vw
;
1099 struct rtentry
*rt
= (struct rtentry
*)rn
;
1100 int error
= 0, size
;
1101 struct rt_addrinfo info
;
1103 if (w
->w_op
== NET_RT_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
1105 bzero((caddr_t
)&info
, sizeof(info
));
1107 gate
= rt
->rt_gateway
;
1108 netmask
= rt_mask(rt
);
1109 genmask
= rt
->rt_genmask
;
1110 if (w
->w_op
!= NET_RT_DUMP2
) {
1111 size
= rt_msg2(RTM_GET
, &info
, 0, w
);
1112 if (w
->w_req
&& w
->w_tmem
) {
1113 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)w
->w_tmem
;
1115 rtm
->rtm_flags
= rt
->rt_flags
;
1116 rtm
->rtm_use
= rt
->rt_use
;
1117 rtm
->rtm_rmx
= rt
->rt_rmx
;
1118 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1122 rtm
->rtm_addrs
= info
.rti_addrs
;
1123 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1127 size
= rt_msg2(RTM_GET2
, &info
, 0, w
);
1128 if (w
->w_req
&& w
->w_tmem
) {
1129 struct rt_msghdr2
*rtm
= (struct rt_msghdr2
*)w
->w_tmem
;
1131 rtm
->rtm_flags
= rt
->rt_flags
;
1132 rtm
->rtm_use
= rt
->rt_use
;
1133 rtm
->rtm_rmx
= rt
->rt_rmx
;
1134 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1135 rtm
->rtm_refcnt
= rt
->rt_refcnt
;
1137 rtm
->rtm_parentflags
= rt
->rt_parent
->rt_flags
;
1139 rtm
->rtm_parentflags
= 0;
1140 rtm
->rtm_reserved
= 0;
1141 rtm
->rtm_addrs
= info
.rti_addrs
;
1142 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1157 struct rt_addrinfo info
;
1160 bzero((caddr_t
)&info
, sizeof(info
));
1161 ifnet_head_lock_shared();
1162 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1165 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1167 ifnet_lock_shared(ifp
);
1168 ifa
= ifp
->if_addrhead
.tqh_first
;
1169 ifpaddr
= ifa
->ifa_addr
;
1170 len
= rt_msg2(RTM_IFINFO
, &info
, (caddr_t
)0, w
);
1172 if (w
->w_req
&& w
->w_tmem
) {
1173 struct if_msghdr
*ifm
;
1175 ifm
= (struct if_msghdr
*)w
->w_tmem
;
1176 ifm
->ifm_index
= ifp
->if_index
;
1177 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1178 if_data_internal_to_if_data(&ifp
->if_data
, &ifm
->ifm_data
);
1179 ifm
->ifm_addrs
= info
.rti_addrs
;
1180 error
= SYSCTL_OUT(w
->w_req
,(caddr_t
)ifm
, len
);
1182 ifnet_lock_done(ifp
);
1186 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1187 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1190 if (curproc
->p_prison
&& prison_if(curproc
, ifa
->ifa_addr
))
1193 ifaaddr
= ifa
->ifa_addr
;
1194 netmask
= ifa
->ifa_netmask
;
1195 brdaddr
= ifa
->ifa_dstaddr
;
1196 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1197 if (w
->w_req
&& w
->w_tmem
) {
1198 struct ifa_msghdr
*ifam
;
1200 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1201 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1202 ifam
->ifam_flags
= ifa
->ifa_flags
;
1203 ifam
->ifam_metric
= ifa
->ifa_metric
;
1204 ifam
->ifam_addrs
= info
.rti_addrs
;
1205 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1210 ifnet_lock_done(ifp
);
1211 ifaaddr
= netmask
= brdaddr
= 0;
1224 struct rt_addrinfo info
;
1227 bzero((caddr_t
)&info
, sizeof(info
));
1228 ifnet_head_lock_shared();
1229 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1232 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1234 ifnet_lock_shared(ifp
);
1235 ifa
= ifp
->if_addrhead
.tqh_first
;
1236 ifpaddr
= ifa
->ifa_addr
;
1237 len
= rt_msg2(RTM_IFINFO2
, &info
, (caddr_t
)0, w
);
1239 if (w
->w_req
&& w
->w_tmem
) {
1240 struct if_msghdr2
*ifm
;
1242 ifm
= (struct if_msghdr2
*)w
->w_tmem
;
1243 ifm
->ifm_addrs
= info
.rti_addrs
;
1244 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1245 ifm
->ifm_index
= ifp
->if_index
;
1246 ifm
->ifm_snd_len
= ifp
->if_snd
.ifq_len
;
1247 ifm
->ifm_snd_maxlen
= ifp
->if_snd
.ifq_maxlen
;
1248 ifm
->ifm_snd_drops
= ifp
->if_snd
.ifq_drops
;
1249 ifm
->ifm_timer
= ifp
->if_timer
;
1250 if_data_internal_to_if_data64(&ifp
->if_data
, &ifm
->ifm_data
);
1251 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1253 ifnet_lock_done(ifp
);
1257 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1258 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1260 ifaaddr
= ifa
->ifa_addr
;
1261 netmask
= ifa
->ifa_netmask
;
1262 brdaddr
= ifa
->ifa_dstaddr
;
1263 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1264 if (w
->w_req
&& w
->w_tmem
) {
1265 struct ifa_msghdr
*ifam
;
1267 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1268 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1269 ifam
->ifam_flags
= ifa
->ifa_flags
;
1270 ifam
->ifam_metric
= ifa
->ifa_metric
;
1271 ifam
->ifam_addrs
= info
.rti_addrs
;
1272 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1278 ifnet_lock_done(ifp
);
1282 struct ifmultiaddr
*ifma
;
1284 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1285 ifma
= ifma
->ifma_link
.le_next
) {
1286 if (af
&& af
!= ifma
->ifma_addr
->sa_family
)
1288 bzero((caddr_t
)&info
, sizeof(info
));
1289 ifaaddr
= ifma
->ifma_addr
;
1290 if (ifp
->if_addrhead
.tqh_first
)
1291 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1293 gate
= ifma
->ifma_ll
->ifma_addr
;
1294 len
= rt_msg2(RTM_NEWMADDR2
, &info
, 0, w
);
1295 if (w
->w_req
&& w
->w_tmem
) {
1296 struct ifma_msghdr2
*ifmam
;
1298 ifmam
= (struct ifma_msghdr2
*)w
->w_tmem
;
1299 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1300 ifmam
->ifmam_flags
= 0;
1301 ifmam
->ifmam_index
= ifma
->ifma_ifp
->if_index
;
1302 ifmam
->ifmam_refcount
= ifma
->ifma_refcount
;
1303 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1309 ifnet_lock_done(ifp
);
1310 ifaaddr
= netmask
= brdaddr
= 0;
1318 sysctl_rtstat(struct sysctl_req
*req
)
1322 error
= SYSCTL_OUT(req
, &rtstat
, sizeof(struct rtstat
));
1330 sysctl_rttrash(struct sysctl_req
*req
)
1334 error
= SYSCTL_OUT(req
, &rttrash
, sizeof(rttrash
));
1343 sysctl_rtsock SYSCTL_HANDLER_ARGS
1345 int *name
= (int *)arg1
;
1346 u_int namelen
= arg2
;
1347 struct radix_node_head
*rnh
;
1348 int i
, error
= EINVAL
;
1359 Bzero(&w
, sizeof(w
));
1364 lck_mtx_lock(rt_mtx
);
1370 for (i
= 1; i
<= AF_MAX
; i
++)
1371 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
1372 (error
= rnh
->rnh_walktree(rnh
,
1373 sysctl_dumpentry
, &w
)))
1377 error
= sysctl_iflist(af
, &w
);
1379 case NET_RT_IFLIST2
:
1380 error
= sysctl_iflist2(af
, &w
);
1383 error
= sysctl_rtstat(req
);
1386 error
= sysctl_rttrash(req
);
1389 lck_mtx_unlock(rt_mtx
);
1391 FREE(w
.w_tmem
, M_RTABLE
);
1395 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
, sysctl_rtsock
, "");
1398 * Definitions of protocols supported in the ROUTE domain.
1401 struct domain routedomain
; /* or at least forward */
1403 static struct protosw routesw
[] = {
1404 { SOCK_RAW
, &routedomain
, 0, PR_ATOMIC
|PR_ADDR
,
1405 0, route_output
, raw_ctlinput
, 0,
1415 struct domain routedomain
=
1416 { PF_ROUTE
, "route", route_init
, 0, 0,
1418 0, 0, 0, 0, 0, 0, 0, 0,