2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1988, 1991, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)rtsock.c 8.5 (Berkeley) 11/2/94
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/syslog.h>
75 #include <net/route.h>
76 #include <net/raw_cb.h>
77 #include <netinet/in.h>
79 MALLOC_DEFINE(M_RTABLE
, "routetbl", "routing tables");
81 static struct sockaddr route_dst
= { 2, PF_ROUTE
, };
82 static struct sockaddr route_src
= { 2, PF_ROUTE
, };
83 static struct sockaddr sa_zero
= { sizeof(sa_zero
), AF_INET
, };
84 static struct sockproto route_proto
= { PF_ROUTE
, };
90 struct sysctl_req
*w_req
;
94 rt_msg1
__P((int, struct rt_addrinfo
*));
95 static int rt_msg2
__P((int,
96 struct rt_addrinfo
*, caddr_t
, struct walkarg
*));
97 static int rt_xaddrs
__P((caddr_t
, caddr_t
, struct rt_addrinfo
*));
98 static int sysctl_dumpentry
__P((struct radix_node
*rn
, void *vw
));
99 static int sysctl_iflist
__P((int af
, struct walkarg
*w
));
100 static int route_output
__P((struct mbuf
*, struct socket
*));
101 static void rt_setmetrics
__P((u_long
, struct rt_metrics
*, struct rt_metrics
*));
102 static void rt_setif
__P((struct rtentry
*, struct sockaddr
*, struct sockaddr
*,
105 /* Sleazy use of local variables throughout file, warning!!!! */
106 #define dst info.rti_info[RTAX_DST]
107 #define gate info.rti_info[RTAX_GATEWAY]
108 #define netmask info.rti_info[RTAX_NETMASK]
109 #define genmask info.rti_info[RTAX_GENMASK]
110 #define ifpaddr info.rti_info[RTAX_IFP]
111 #define ifaaddr info.rti_info[RTAX_IFA]
112 #define brdaddr info.rti_info[RTAX_BRD]
115 * It really doesn't make any sense at all for this code to share much
116 * with raw_usrreq.c, since its functionality is so restricted. XXX
119 rts_abort(struct socket
*so
)
123 error
= raw_usrreqs
.pru_abort(so
);
128 /* pru_accept is EOPNOTSUPP */
131 rts_attach(struct socket
*so
, int proto
, struct proc
*p
)
136 if (sotorawcb(so
) != 0)
137 return EISCONN
; /* XXX panic? */
138 MALLOC(rp
, struct rawcb
*, sizeof *rp
, M_PCB
, M_WAITOK
); /* XXX */
141 bzero(rp
, sizeof *rp
);
144 * The splnet() is necessary to block protocols from sending
145 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
146 * this PCB is extant but incompletely initialized.
147 * Probably we should try to do more of this work beforehand and
151 so
->so_pcb
= (caddr_t
)rp
;
152 error
= raw_attach(so
, proto
); /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
160 switch(rp
->rcb_proto
.sp_protocol
) {
165 route_cb
.ip6_count
++;
168 route_cb
.ipx_count
++;
174 rp
->rcb_faddr
= &route_src
;
175 route_cb
.any_count
++;
177 so
->so_options
|= SO_USELOOPBACK
;
183 rts_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
187 error
= raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
193 rts_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
197 error
= raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
202 /* pru_connect2 is EOPNOTSUPP */
203 /* pru_control is EOPNOTSUPP */
206 rts_detach(struct socket
*so
)
208 struct rawcb
*rp
= sotorawcb(so
);
213 switch(rp
->rcb_proto
.sp_protocol
) {
218 route_cb
.ip6_count
--;
221 route_cb
.ipx_count
--;
227 route_cb
.any_count
--;
229 error
= raw_usrreqs
.pru_detach(so
);
235 rts_disconnect(struct socket
*so
)
239 error
= raw_usrreqs
.pru_disconnect(so
);
244 /* pru_listen is EOPNOTSUPP */
247 rts_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
251 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
256 /* pru_rcvd is EOPNOTSUPP */
257 /* pru_rcvoob is EOPNOTSUPP */
260 rts_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
261 struct mbuf
*control
, struct proc
*p
)
265 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
270 /* pru_sense is null */
273 rts_shutdown(struct socket
*so
)
277 error
= raw_usrreqs
.pru_shutdown(so
);
283 rts_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
287 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
292 static struct pr_usrreqs route_usrreqs
= {
293 rts_abort
, pru_accept_notsupp
, rts_attach
, rts_bind
, rts_connect
,
294 pru_connect2_notsupp
, pru_control_notsupp
, rts_detach
, rts_disconnect
,
295 pru_listen_notsupp
, rts_peeraddr
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
296 rts_send
, pru_sense_null
, rts_shutdown
, rts_sockaddr
,
297 sosend
, soreceive
, sopoll
303 register struct mbuf
*m
;
306 register struct rt_msghdr
*rtm
= 0;
307 register struct rtentry
*rt
= 0;
308 struct rtentry
*saved_nrt
= 0;
309 struct radix_node_head
*rnh
;
310 struct rt_addrinfo info
;
312 struct ifnet
*ifp
= 0;
313 struct ifaddr
*ifa
= 0;
314 struct proc
*curproc
= current_proc();
315 int sendonlytoself
= 0;
317 #define senderr(e) { error = e; goto flush;}
318 if (m
== 0 || ((m
->m_len
< sizeof(long)) &&
319 (m
= m_pullup(m
, sizeof(long))) == 0))
321 if ((m
->m_flags
& M_PKTHDR
) == 0)
322 panic("route_output");
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) {
359 rtm
->rtm_pid
= curproc
->p_pid
;
360 info
.rti_addrs
= rtm
->rtm_addrs
;
361 if (rt_xaddrs((caddr_t
)(rtm
+ 1), len
+ (caddr_t
)rtm
, &info
)) {
365 if (dst
== 0 || (dst
->sa_family
>= AF_MAX
)
366 || (gate
!= 0 && (gate
->sa_family
>= AF_MAX
)))
369 struct radix_node
*t
;
370 t
= rn_addmask((caddr_t
)genmask
, 0, 1);
371 if (t
&& Bcmp(genmask
, t
->rn_key
, *(u_char
*)genmask
) == 0)
372 genmask
= (struct sockaddr
*)(t
->rn_key
);
376 switch (rtm
->rtm_type
) {
383 /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
384 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
385 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
386 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
387 * That circular route was ignored on previous version of MacOS X because of a routing bug
388 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
389 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
390 * made in order to provide binary compatibility with AOL.
391 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
392 * confusing the routing table with a wrong route to the previous default gateway
395 extern int check_routeselfref
;
396 #define satosinaddr(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr)
398 if (check_routeselfref
&& (dst
&& dst
->sa_family
== AF_INET
) &&
399 (netmask
&& satosinaddr(netmask
) == INADDR_BROADCAST
) &&
400 (gate
&& satosinaddr(dst
) == satosinaddr(gate
))) {
401 log(LOG_WARNING
, "route_output: circular route %ld.%ld.%ld.%ld/32 ignored\n",
402 (ntohl(satosinaddr(gate
)>>24))&0xff,
403 (ntohl(satosinaddr(gate
)>>16))&0xff,
404 (ntohl(satosinaddr(gate
)>>8))&0xff,
405 (ntohl(satosinaddr(gate
)))&0xff);
411 error
= rtrequest(RTM_ADD
, dst
, gate
, netmask
,
412 rtm
->rtm_flags
, &saved_nrt
);
413 if (error
== 0 && saved_nrt
) {
416 * If the route request specified an interface with
417 * IFA and/or IFP, we set the requested interface on
418 * the route with rt_setif. It would be much better
419 * to do this inside rtrequest, but that would
420 * require passing the desired interface, in some
421 * form, to rtrequest. Since rtrequest is called in
422 * so many places (roughly 40 in our source), adding
423 * a parameter is to much for us to swallow; this is
424 * something for the FreeBSD developers to tackle.
425 * Instead, we let rtrequest compute whatever
426 * interface it wants, then come in behind it and
427 * stick in the interface that we really want. This
428 * works reasonably well except when rtrequest can't
429 * figure out what interface to use (with
430 * ifa_withroute) and returns ENETUNREACH. Ideally
431 * it shouldn't matter if rtrequest can't figure out
432 * the interface if we're going to explicitly set it
433 * ourselves anyway. But practically we can't
434 * recover here because rtrequest will not do any of
435 * the work necessary to add the route if it can't
436 * find an interface. As long as there is a default
437 * route that leads to some interface, rtrequest will
438 * find an interface, so this problem should be
439 * rarely encountered.
443 rt_setif(saved_nrt
, ifpaddr
, ifaaddr
, gate
);
445 rt_setmetrics(rtm
->rtm_inits
,
446 &rtm
->rtm_rmx
, &saved_nrt
->rt_rmx
);
447 saved_nrt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
448 saved_nrt
->rt_rmx
.rmx_locks
|=
449 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
451 saved_nrt
->rt_genmask
= genmask
;
456 error
= rtrequest(RTM_DELETE
, dst
, gate
, netmask
,
457 rtm
->rtm_flags
, &saved_nrt
);
459 if ((rt
= 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
) {
480 gate
= rt
->rt_gateway
;
481 netmask
= rt_mask(rt
);
482 genmask
= rt
->rt_genmask
;
483 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
486 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
487 ifaaddr
= rt
->rt_ifa
->ifa_addr
;
488 rtm
->rtm_index
= ifp
->if_index
;
494 len
= rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)0,
495 (struct walkarg
*)0);
496 if (len
> rtm
->rtm_msglen
) {
497 struct rt_msghdr
*new_rtm
;
498 R_Malloc(new_rtm
, struct rt_msghdr
*, len
);
501 Bcopy(rtm
, new_rtm
, rtm
->rtm_msglen
);
502 Free(rtm
); rtm
= new_rtm
;
504 (void)rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)rtm
,
505 (struct walkarg
*)0);
506 rtm
->rtm_flags
= rt
->rt_flags
;
507 rtm
->rtm_rmx
= rt
->rt_rmx
;
508 rtm
->rtm_addrs
= info
.rti_addrs
;
512 if (gate
&& (error
= rt_setgate(rt
, rt_key(rt
), gate
)))
516 * If they tried to change things but didn't specify
517 * the required gateway, then just use the old one.
518 * This can happen if the user tries to change the
519 * flags on the default route without changing the
520 * default gateway. Changing flags still doesn't work.
522 if ((rt
->rt_flags
& RTF_GATEWAY
) && !gate
)
523 gate
= rt
->rt_gateway
;
527 * On Darwin, we call rt_setif which contains the
528 * equivalent to the code found at this very spot
531 rt_setif(rt
, ifpaddr
, ifaaddr
, gate
);
534 rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
,
537 /* rt_setif, called above does this for us on darwin */
538 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
539 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, gate
);
542 rt
->rt_genmask
= genmask
;
547 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
548 rt
->rt_rmx
.rmx_locks
|=
549 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
561 rtm
->rtm_errno
= error
;
563 rtm
->rtm_flags
|= RTF_DONE
;
568 register struct rawcb
*rp
= 0;
570 * Check to see if we don't want our own messages.
572 if ((so
->so_options
& SO_USELOOPBACK
) == 0) {
573 if (route_cb
.any_count
<= 1) {
579 /* There is another listener, so construct message */
583 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
584 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
587 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
)
588 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
591 if (sendonlytoself
&& m
) {
592 if (sbappendaddr(&so
->so_rcv
, &route_src
, m
, (struct mbuf
*)0) == 0) {
600 rp
->rcb_proto
.sp_family
= 0; /* Avoid us */
602 route_proto
.sp_protocol
= dst
->sa_family
;
604 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
606 rp
->rcb_proto
.sp_family
= PF_ROUTE
;
613 rt_setmetrics(which
, in
, out
)
615 register struct rt_metrics
*in
, *out
;
617 #define metric(f, e) if (which & (f)) out->e = in->e;
618 metric(RTV_RPIPE
, rmx_recvpipe
);
619 metric(RTV_SPIPE
, rmx_sendpipe
);
620 metric(RTV_SSTHRESH
, rmx_ssthresh
);
621 metric(RTV_RTT
, rmx_rtt
);
622 metric(RTV_RTTVAR
, rmx_rttvar
);
623 metric(RTV_HOPCOUNT
, rmx_hopcount
);
624 metric(RTV_MTU
, rmx_mtu
);
625 metric(RTV_EXPIRE
, rmx_expire
);
630 * Set route's interface given ifpaddr, ifaaddr, and gateway.
633 rt_setif(rt
, Ifpaddr
, Ifaaddr
, Gate
)
635 struct sockaddr
*Ifpaddr
, *Ifaaddr
, *Gate
;
637 struct ifaddr
*ifa
= 0;
638 struct ifnet
*ifp
= 0;
640 /* new gateway could require new ifaddr, ifp;
641 flags may also be different; ifp may be specified
642 by ll sockaddr when protocol address is ambiguous */
643 if (Ifpaddr
&& (ifa
= ifa_ifwithnet(Ifpaddr
)) &&
644 (ifp
= ifa
->ifa_ifp
) && (Ifaaddr
|| Gate
))
645 ifa
= ifaof_ifpforaddr(Ifaaddr
? Ifaaddr
: Gate
,
647 else if (Ifpaddr
&& (ifp
= if_withname(Ifpaddr
)) ) {
648 ifa
= Gate
? ifaof_ifpforaddr(Gate
, ifp
) :
649 TAILQ_FIRST(&ifp
->if_addrhead
);
651 else if ((Ifaaddr
&& (ifa
= ifa_ifwithaddr(Ifaaddr
))) ||
652 (Gate
&& (ifa
= ifa_ifwithroute(rt
->rt_flags
,
656 register struct ifaddr
*oifa
= rt
->rt_ifa
;
658 if (oifa
&& oifa
->ifa_rtrequest
)
659 oifa
->ifa_rtrequest(RTM_DELETE
,
663 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
664 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
665 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
671 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
672 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
673 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
678 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
679 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
683 * Extract the addresses of the passed sockaddrs.
684 * Do a little sanity checking so as to avoid bad memory references.
685 * This data is derived straight from userland.
688 rt_xaddrs(cp
, cplim
, rtinfo
)
689 register caddr_t cp
, cplim
;
690 register struct rt_addrinfo
*rtinfo
;
692 register struct sockaddr
*sa
;
695 bzero(rtinfo
->rti_info
, sizeof(rtinfo
->rti_info
));
696 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
697 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
699 sa
= (struct sockaddr
*)cp
;
703 if ( (cp
+ sa
->sa_len
) > cplim
) {
708 * there are no more.. quit now
709 * If there are more bits, they are in error.
710 * I've seen this. route(1) can evidently generate these.
711 * This causes kernel to core dump.
712 * for compatibility, If we see this, point to a safe address.
714 if (sa
->sa_len
== 0) {
715 rtinfo
->rti_info
[i
] = &sa_zero
;
716 return (0); /* should be EINVAL but for compat */
720 rtinfo
->rti_info
[i
] = sa
;
727 rt_msg1(type
, rtinfo
)
729 register struct rt_addrinfo
*rtinfo
;
731 register struct rt_msghdr
*rtm
;
732 register struct mbuf
*m
;
734 register struct sockaddr
*sa
;
741 len
= sizeof(struct ifa_msghdr
);
746 len
= sizeof(struct ifma_msghdr
);
750 len
= sizeof(struct if_msghdr
);
754 len
= sizeof(struct rt_msghdr
);
758 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
759 if (m
&& len
> MHLEN
) {
760 MCLGET(m
, M_DONTWAIT
);
761 if ((m
->m_flags
& M_EXT
) == 0) {
768 m
->m_pkthdr
.len
= m
->m_len
= len
;
769 m
->m_pkthdr
.rcvif
= 0;
770 rtm
= mtod(m
, struct rt_msghdr
*);
771 bzero((caddr_t
)rtm
, len
);
772 for (i
= 0; i
< RTAX_MAX
; i
++) {
773 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
775 rtinfo
->rti_addrs
|= (1 << i
);
776 dlen
= ROUNDUP(sa
->sa_len
);
777 m_copyback(m
, len
, dlen
, (caddr_t
)sa
);
780 if (m
->m_pkthdr
.len
!= len
) {
784 rtm
->rtm_msglen
= len
;
785 rtm
->rtm_version
= RTM_VERSION
;
786 rtm
->rtm_type
= type
;
791 rt_msg2(type
, rtinfo
, cp
, w
)
793 register struct rt_addrinfo
*rtinfo
;
798 int len
, dlen
, second_time
= 0;
801 rtinfo
->rti_addrs
= 0;
807 len
= sizeof(struct ifa_msghdr
);
811 len
= sizeof(struct if_msghdr
);
815 len
= sizeof(struct rt_msghdr
);
820 for (i
= 0; i
< RTAX_MAX
; i
++) {
821 register struct sockaddr
*sa
;
823 if ((sa
= rtinfo
->rti_info
[i
]) == 0)
825 rtinfo
->rti_addrs
|= (1 << i
);
826 dlen
= ROUNDUP(sa
->sa_len
);
828 bcopy((caddr_t
)sa
, cp
, (unsigned)dlen
);
833 if (cp
== 0 && w
!= NULL
&& !second_time
) {
834 register struct walkarg
*rw
= w
;
837 if (rw
->w_tmemsize
< len
) {
839 FREE(rw
->w_tmem
, M_RTABLE
);
840 rw
->w_tmem
= (caddr_t
)
841 _MALLOC(len
, M_RTABLE
, M_WAITOK
); /*###LD0412 was NOWAIT */
843 rw
->w_tmemsize
= len
;
853 register struct rt_msghdr
*rtm
= (struct rt_msghdr
*)cp0
;
855 rtm
->rtm_version
= RTM_VERSION
;
856 rtm
->rtm_type
= type
;
857 rtm
->rtm_msglen
= len
;
863 * This routine is called to generate a message from the routing
864 * socket indicating that a redirect has occured, a routing lookup
865 * has failed, or that a protocol has detected timeouts to a particular
869 rt_missmsg(type
, rtinfo
, flags
, error
)
870 int type
, flags
, error
;
871 register struct rt_addrinfo
*rtinfo
;
873 register struct rt_msghdr
*rtm
;
874 register struct mbuf
*m
;
875 struct sockaddr
*sa
= rtinfo
->rti_info
[RTAX_DST
];
877 if (route_cb
.any_count
== 0)
879 m
= rt_msg1(type
, rtinfo
);
882 rtm
= mtod(m
, struct rt_msghdr
*);
883 rtm
->rtm_flags
= RTF_DONE
| flags
;
884 rtm
->rtm_errno
= error
;
885 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
886 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
887 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
891 * This routine is called to generate a message from the routing
892 * socket indicating that the status of a network interface has changed.
896 register struct ifnet
*ifp
;
898 register struct if_msghdr
*ifm
;
900 struct rt_addrinfo info
;
902 if (route_cb
.any_count
== 0)
904 bzero((caddr_t
)&info
, sizeof(info
));
905 m
= rt_msg1(RTM_IFINFO
, &info
);
908 ifm
= mtod(m
, struct if_msghdr
*);
909 ifm
->ifm_index
= ifp
->if_index
;
910 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
911 ifm
->ifm_data
= ifp
->if_data
;
913 route_proto
.sp_protocol
= 0;
914 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
918 * This is called to generate messages from the routing socket
919 * indicating a network interface has had addresses associated with it.
920 * if we ever reverse the logic and replace messages TO the routing
921 * socket indicate a request to configure interfaces, then it will
922 * be unnecessary as the routing socket will automatically generate
926 rt_newaddrmsg(cmd
, ifa
, error
, rt
)
928 register struct ifaddr
*ifa
;
929 register struct rtentry
*rt
;
931 struct rt_addrinfo info
;
932 struct sockaddr
*sa
= 0;
935 struct ifnet
*ifp
= ifa
->ifa_ifp
;
937 if (route_cb
.any_count
== 0)
939 for (pass
= 1; pass
< 3; pass
++) {
940 bzero((caddr_t
)&info
, sizeof(info
));
941 if ((cmd
== RTM_ADD
&& pass
== 1) ||
942 (cmd
== RTM_DELETE
&& pass
== 2)) {
943 register struct ifa_msghdr
*ifam
;
944 int ncmd
= cmd
== RTM_ADD
? RTM_NEWADDR
: RTM_DELADDR
;
946 ifaaddr
= sa
= ifa
->ifa_addr
;
947 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
948 netmask
= ifa
->ifa_netmask
;
949 brdaddr
= ifa
->ifa_dstaddr
;
950 if ((m
= rt_msg1(ncmd
, &info
)) == NULL
)
952 ifam
= mtod(m
, struct ifa_msghdr
*);
953 ifam
->ifam_index
= ifp
->if_index
;
954 ifam
->ifam_metric
= ifa
->ifa_metric
;
955 ifam
->ifam_flags
= ifa
->ifa_flags
;
956 ifam
->ifam_addrs
= info
.rti_addrs
;
958 if ((cmd
== RTM_ADD
&& pass
== 2) ||
959 (cmd
== RTM_DELETE
&& pass
== 1)) {
960 register struct rt_msghdr
*rtm
;
964 netmask
= rt_mask(rt
);
965 dst
= sa
= rt_key(rt
);
966 gate
= rt
->rt_gateway
;
967 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
969 rtm
= mtod(m
, struct rt_msghdr
*);
970 rtm
->rtm_index
= ifp
->if_index
;
971 rtm
->rtm_flags
|= rt
->rt_flags
;
972 rtm
->rtm_errno
= error
;
973 rtm
->rtm_addrs
= info
.rti_addrs
;
975 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
976 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
981 * This is the analogue to the rt_newaddrmsg which performs the same
982 * function but for multicast group memberhips. This is easier since
983 * there is no route state to worry about.
986 rt_newmaddrmsg(cmd
, ifma
)
988 struct ifmultiaddr
*ifma
;
990 struct rt_addrinfo info
;
992 struct ifnet
*ifp
= ifma
->ifma_ifp
;
993 struct ifma_msghdr
*ifmam
;
995 if (route_cb
.any_count
== 0)
998 bzero((caddr_t
)&info
, sizeof(info
));
999 ifaaddr
= ifma
->ifma_addr
;
1000 if (ifp
&& ifp
->if_addrhead
.tqh_first
)
1001 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
1005 * If a link-layer address is present, present it as a ``gateway''
1006 * (similarly to how ARP entries, e.g., are presented).
1008 gate
= ifma
->ifma_lladdr
;
1009 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1011 ifmam
= mtod(m
, struct ifma_msghdr
*);
1012 ifmam
->ifmam_index
= ifp
->if_index
;
1013 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1014 route_proto
.sp_protocol
= ifma
->ifma_addr
->sa_family
;
1015 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1019 * This is used in dumping the kernel table via sysctl().
1022 sysctl_dumpentry(rn
, vw
)
1023 struct radix_node
*rn
;
1026 register struct walkarg
*w
= vw
;
1027 register struct rtentry
*rt
= (struct rtentry
*)rn
;
1028 int error
= 0, size
;
1029 struct rt_addrinfo info
;
1031 if (w
->w_op
== NET_RT_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
1033 bzero((caddr_t
)&info
, sizeof(info
));
1035 gate
= rt
->rt_gateway
;
1036 netmask
= rt_mask(rt
);
1037 genmask
= rt
->rt_genmask
;
1038 size
= rt_msg2(RTM_GET
, &info
, 0, w
);
1039 if (w
->w_req
&& w
->w_tmem
) {
1040 register struct rt_msghdr
*rtm
= (struct rt_msghdr
*)w
->w_tmem
;
1042 rtm
->rtm_flags
= rt
->rt_flags
;
1043 rtm
->rtm_use
= rt
->rt_use
;
1044 rtm
->rtm_rmx
= rt
->rt_rmx
;
1045 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1046 rtm
->rtm_errno
= rtm
->rtm_pid
= rtm
->rtm_seq
= 0;
1047 rtm
->rtm_addrs
= info
.rti_addrs
;
1048 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1055 sysctl_iflist(af
, w
)
1057 register struct walkarg
*w
;
1059 register struct ifnet
*ifp
;
1060 register struct ifaddr
*ifa
;
1061 struct rt_addrinfo info
;
1064 bzero((caddr_t
)&info
, sizeof(info
));
1065 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1066 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1068 ifa
= ifp
->if_addrhead
.tqh_first
;
1069 ifpaddr
= ifa
->ifa_addr
;
1070 len
= rt_msg2(RTM_IFINFO
, &info
, (caddr_t
)0, w
);
1072 if (w
->w_req
&& w
->w_tmem
) {
1073 register struct if_msghdr
*ifm
;
1075 ifm
= (struct if_msghdr
*)w
->w_tmem
;
1076 ifm
->ifm_index
= ifp
->if_index
;
1077 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1078 ifm
->ifm_data
= ifp
->if_data
;
1079 ifm
->ifm_addrs
= info
.rti_addrs
;
1080 error
= SYSCTL_OUT(w
->w_req
,(caddr_t
)ifm
, len
);
1084 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1085 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1088 if (curproc
->p_prison
&& prison_if(curproc
, ifa
->ifa_addr
))
1091 ifaaddr
= ifa
->ifa_addr
;
1092 netmask
= ifa
->ifa_netmask
;
1093 brdaddr
= ifa
->ifa_dstaddr
;
1094 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1095 if (w
->w_req
&& w
->w_tmem
) {
1096 register struct ifa_msghdr
*ifam
;
1098 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1099 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1100 ifam
->ifam_flags
= ifa
->ifa_flags
;
1101 ifam
->ifam_metric
= ifa
->ifa_metric
;
1102 ifam
->ifam_addrs
= info
.rti_addrs
;
1103 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1108 ifaaddr
= netmask
= brdaddr
= 0;
1114 sysctl_rtsock SYSCTL_HANDLER_ARGS
1116 int *name
= (int *)arg1
;
1117 u_int namelen
= arg2
;
1118 register struct radix_node_head
*rnh
;
1119 int i
, s
, error
= EINVAL
;
1130 Bzero(&w
, sizeof(w
));
1140 for (i
= 1; i
<= AF_MAX
; i
++)
1141 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
1142 (error
= rnh
->rnh_walktree(rnh
,
1143 sysctl_dumpentry
, &w
)))
1148 error
= sysctl_iflist(af
, &w
);
1152 FREE(w
.w_tmem
, M_RTABLE
);
1156 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
, sysctl_rtsock
, "");
1159 * Definitions of protocols supported in the ROUTE domain.
1162 struct domain routedomain
; /* or at least forward */
1164 static struct protosw routesw
[] = {
1165 { SOCK_RAW
, &routedomain
, 0, PR_ATOMIC
|PR_ADDR
,
1166 0, route_output
, raw_ctlinput
, 0,
1169 0, &route_usrreqs
, 0, 0
1173 struct domain routedomain
=
1174 { PF_ROUTE
, "route", route_init
, 0, 0,