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>
72 #include <net/route.h>
73 #include <net/raw_cb.h>
74 #include <netinet/in.h>
76 MALLOC_DEFINE(M_RTABLE
, "routetbl", "routing tables");
78 static struct sockaddr route_dst
= { 2, PF_ROUTE
, };
79 static struct sockaddr route_src
= { 2, PF_ROUTE
, };
80 static struct sockaddr sa_zero
= { sizeof(sa_zero
), AF_INET
, };
81 static struct sockproto route_proto
= { PF_ROUTE
, };
87 struct sysctl_req
*w_req
;
91 rt_msg1
__P((int, struct rt_addrinfo
*));
92 static int rt_msg2
__P((int,
93 struct rt_addrinfo
*, caddr_t
, struct walkarg
*));
94 static int rt_xaddrs
__P((caddr_t
, caddr_t
, struct rt_addrinfo
*));
95 static int sysctl_dumpentry
__P((struct radix_node
*rn
, void *vw
));
96 static int sysctl_iflist
__P((int af
, struct walkarg
*w
));
97 static int route_output
__P((struct mbuf
*, struct socket
*));
98 static void rt_setmetrics
__P((u_long
, struct rt_metrics
*, struct rt_metrics
*));
99 static void rt_setif
__P((struct rtentry
*, struct sockaddr
*, struct sockaddr
*,
102 /* Sleazy use of local variables throughout file, warning!!!! */
103 #define dst info.rti_info[RTAX_DST]
104 #define gate info.rti_info[RTAX_GATEWAY]
105 #define netmask info.rti_info[RTAX_NETMASK]
106 #define genmask info.rti_info[RTAX_GENMASK]
107 #define ifpaddr info.rti_info[RTAX_IFP]
108 #define ifaaddr info.rti_info[RTAX_IFA]
109 #define brdaddr info.rti_info[RTAX_BRD]
112 * It really doesn't make any sense at all for this code to share much
113 * with raw_usrreq.c, since its functionality is so restricted. XXX
116 rts_abort(struct socket
*so
)
120 error
= raw_usrreqs
.pru_abort(so
);
125 /* pru_accept is EOPNOTSUPP */
128 rts_attach(struct socket
*so
, int proto
, struct proc
*p
)
133 if (sotorawcb(so
) != 0)
134 return EISCONN
; /* XXX panic? */
135 MALLOC(rp
, struct rawcb
*, sizeof *rp
, M_PCB
, M_WAITOK
); /* XXX */
138 bzero(rp
, sizeof *rp
);
141 * The splnet() is necessary to block protocols from sending
142 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
143 * this PCB is extant but incompletely initialized.
144 * Probably we should try to do more of this work beforehand and
148 so
->so_pcb
= (caddr_t
)rp
;
149 error
= raw_usrreqs
.pru_attach(so
, proto
, p
);
156 switch(rp
->rcb_proto
.sp_protocol
) {
161 route_cb
.ip6_count
++;
164 route_cb
.ipx_count
++;
170 rp
->rcb_faddr
= &route_src
;
171 route_cb
.any_count
++;
173 so
->so_options
|= SO_USELOOPBACK
;
179 rts_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
183 error
= raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
189 rts_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
193 error
= raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
198 /* pru_connect2 is EOPNOTSUPP */
199 /* pru_control is EOPNOTSUPP */
202 rts_detach(struct socket
*so
)
204 struct rawcb
*rp
= sotorawcb(so
);
209 switch(rp
->rcb_proto
.sp_protocol
) {
214 route_cb
.ip6_count
--;
217 route_cb
.ipx_count
--;
223 route_cb
.any_count
--;
225 error
= raw_usrreqs
.pru_detach(so
);
231 rts_disconnect(struct socket
*so
)
235 error
= raw_usrreqs
.pru_disconnect(so
);
240 /* pru_listen is EOPNOTSUPP */
243 rts_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
247 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
252 /* pru_rcvd is EOPNOTSUPP */
253 /* pru_rcvoob is EOPNOTSUPP */
256 rts_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
257 struct mbuf
*control
, struct proc
*p
)
261 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
266 /* pru_sense is null */
269 rts_shutdown(struct socket
*so
)
273 error
= raw_usrreqs
.pru_shutdown(so
);
279 rts_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
283 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
288 static struct pr_usrreqs route_usrreqs
= {
289 rts_abort
, pru_accept_notsupp
, rts_attach
, rts_bind
, rts_connect
,
290 pru_connect2_notsupp
, pru_control_notsupp
, rts_detach
, rts_disconnect
,
291 pru_listen_notsupp
, rts_peeraddr
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
292 rts_send
, pru_sense_null
, rts_shutdown
, rts_sockaddr
,
293 sosend
, soreceive
, sopoll
299 register struct mbuf
*m
;
302 register struct rt_msghdr
*rtm
= 0;
303 register struct rtentry
*rt
= 0;
304 struct rtentry
*saved_nrt
= 0;
305 struct radix_node_head
*rnh
;
306 struct rt_addrinfo info
;
308 struct ifnet
*ifp
= 0;
309 struct ifaddr
*ifa
= 0;
310 struct proc
*curproc
= current_proc();
312 #define senderr(e) { error = e; goto flush;}
313 if (m
== 0 || ((m
->m_len
< sizeof(long)) &&
314 (m
= m_pullup(m
, sizeof(long))) == 0))
316 if ((m
->m_flags
& M_PKTHDR
) == 0)
317 panic("route_output");
318 len
= m
->m_pkthdr
.len
;
319 if (len
< sizeof(*rtm
) ||
320 len
!= mtod(m
, struct rt_msghdr
*)->rtm_msglen
) {
324 R_Malloc(rtm
, struct rt_msghdr
*, len
);
329 m_copydata(m
, 0, len
, (caddr_t
)rtm
);
330 if (rtm
->rtm_version
!= RTM_VERSION
) {
332 senderr(EPROTONOSUPPORT
);
334 rtm
->rtm_pid
= curproc
->p_pid
;
335 info
.rti_addrs
= rtm
->rtm_addrs
;
336 if (rt_xaddrs((caddr_t
)(rtm
+ 1), len
+ (caddr_t
)rtm
, &info
)) {
340 if (dst
== 0 || (dst
->sa_family
>= AF_MAX
)
341 || (gate
!= 0 && (gate
->sa_family
>= AF_MAX
)))
344 struct radix_node
*t
;
345 t
= rn_addmask((caddr_t
)genmask
, 0, 1);
346 if (t
&& Bcmp(genmask
, t
->rn_key
, *(u_char
*)genmask
) == 0)
347 genmask
= (struct sockaddr
*)(t
->rn_key
);
351 switch (rtm
->rtm_type
) {
358 /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
359 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
360 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
361 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
362 * That circular route was ignored on previous version of MacOS X because of a routing bug
363 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
364 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
365 * made in order to provide binary compatibility with AOL.
366 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
367 * confusing the routing table with a wrong route to the previous default gateway
370 extern int check_routeselfref
;
371 #define satosinaddr(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr)
373 if (check_routeselfref
&& (dst
&& dst
->sa_family
== AF_INET
) &&
374 (netmask
&& satosinaddr(netmask
) == INADDR_BROADCAST
) &&
375 (gate
&& satosinaddr(dst
) == satosinaddr(gate
))) {
376 log(LOG_WARNING
, "route_output: circular route %ld.%ld.%ld.%ld/32 ignored\n",
377 (ntohl(satosinaddr(gate
)>>24))&0xff,
378 (ntohl(satosinaddr(gate
)>>16))&0xff,
379 (ntohl(satosinaddr(gate
)>>8))&0xff,
380 (ntohl(satosinaddr(gate
)))&0xff);
386 error
= rtrequest(RTM_ADD
, dst
, gate
, netmask
,
387 rtm
->rtm_flags
, &saved_nrt
);
388 if (error
== 0 && saved_nrt
) {
391 * If the route request specified an interface with
392 * IFA and/or IFP, we set the requested interface on
393 * the route with rt_setif. It would be much better
394 * to do this inside rtrequest, but that would
395 * require passing the desired interface, in some
396 * form, to rtrequest. Since rtrequest is called in
397 * so many places (roughly 40 in our source), adding
398 * a parameter is to much for us to swallow; this is
399 * something for the FreeBSD developers to tackle.
400 * Instead, we let rtrequest compute whatever
401 * interface it wants, then come in behind it and
402 * stick in the interface that we really want. This
403 * works reasonably well except when rtrequest can't
404 * figure out what interface to use (with
405 * ifa_withroute) and returns ENETUNREACH. Ideally
406 * it shouldn't matter if rtrequest can't figure out
407 * the interface if we're going to explicitly set it
408 * ourselves anyway. But practically we can't
409 * recover here because rtrequest will not do any of
410 * the work necessary to add the route if it can't
411 * find an interface. As long as there is a default
412 * route that leads to some interface, rtrequest will
413 * find an interface, so this problem should be
414 * rarely encountered.
418 rt_setif(saved_nrt
, ifpaddr
, ifaaddr
, gate
);
420 rt_setmetrics(rtm
->rtm_inits
,
421 &rtm
->rtm_rmx
, &saved_nrt
->rt_rmx
);
422 saved_nrt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
423 saved_nrt
->rt_rmx
.rmx_locks
|=
424 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
426 saved_nrt
->rt_genmask
= genmask
;
431 error
= rtrequest(RTM_DELETE
, dst
, gate
, netmask
,
432 rtm
->rtm_flags
, &saved_nrt
);
434 if ((rt
= saved_nrt
))
443 if ((rnh
= rt_tables
[dst
->sa_family
]) == 0) {
444 senderr(EAFNOSUPPORT
);
445 } else if ((rt
= (struct rtentry
*)
446 rnh
->rnh_lookup(dst
, netmask
, rnh
)) != NULL
)
450 switch(rtm
->rtm_type
) {
455 gate
= rt
->rt_gateway
;
456 netmask
= rt_mask(rt
);
457 genmask
= rt
->rt_genmask
;
458 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
461 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
462 ifaaddr
= rt
->rt_ifa
->ifa_addr
;
463 rtm
->rtm_index
= ifp
->if_index
;
469 len
= rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)0,
470 (struct walkarg
*)0);
471 if (len
> rtm
->rtm_msglen
) {
472 struct rt_msghdr
*new_rtm
;
473 R_Malloc(new_rtm
, struct rt_msghdr
*, len
);
476 Bcopy(rtm
, new_rtm
, rtm
->rtm_msglen
);
477 Free(rtm
); rtm
= new_rtm
;
479 (void)rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)rtm
,
480 (struct walkarg
*)0);
481 rtm
->rtm_flags
= rt
->rt_flags
;
482 rtm
->rtm_rmx
= rt
->rt_rmx
;
483 rtm
->rtm_addrs
= info
.rti_addrs
;
487 if (gate
&& (error
= rt_setgate(rt
, rt_key(rt
), gate
)))
491 * If they tried to change things but didn't specify
492 * the required gateway, then just use the old one.
493 * This can happen if the user tries to change the
494 * flags on the default route without changing the
495 * default gateway. Changing flags still doesn't work.
497 if ((rt
->rt_flags
& RTF_GATEWAY
) && !gate
)
498 gate
= rt
->rt_gateway
;
502 * On Darwin, we call rt_setif which contains the
503 * equivalent to the code found at this very spot
506 rt_setif(rt
, ifpaddr
, ifaaddr
, gate
);
509 rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
,
512 /* rt_setif, called above does this for us on darwin */
513 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
514 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, gate
);
517 rt
->rt_genmask
= genmask
;
522 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
523 rt
->rt_rmx
.rmx_locks
|=
524 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
536 rtm
->rtm_errno
= error
;
538 rtm
->rtm_flags
|= RTF_DONE
;
543 register struct rawcb
*rp
= 0;
545 * Check to see if we don't want our own messages.
547 if ((so
->so_options
& SO_USELOOPBACK
) == 0) {
548 if (route_cb
.any_count
<= 1) {
554 /* There is another listener, so construct message */
558 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
559 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
562 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
)
563 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
567 rp
->rcb_proto
.sp_family
= 0; /* Avoid us */
569 route_proto
.sp_protocol
= dst
->sa_family
;
571 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
573 rp
->rcb_proto
.sp_family
= PF_ROUTE
;
579 rt_setmetrics(which
, in
, out
)
581 register struct rt_metrics
*in
, *out
;
583 #define metric(f, e) if (which & (f)) out->e = in->e;
584 metric(RTV_RPIPE
, rmx_recvpipe
);
585 metric(RTV_SPIPE
, rmx_sendpipe
);
586 metric(RTV_SSTHRESH
, rmx_ssthresh
);
587 metric(RTV_RTT
, rmx_rtt
);
588 metric(RTV_RTTVAR
, rmx_rttvar
);
589 metric(RTV_HOPCOUNT
, rmx_hopcount
);
590 metric(RTV_MTU
, rmx_mtu
);
591 metric(RTV_EXPIRE
, rmx_expire
);
596 * Set route's interface given ifpaddr, ifaaddr, and gateway.
599 rt_setif(rt
, Ifpaddr
, Ifaaddr
, Gate
)
601 struct sockaddr
*Ifpaddr
, *Ifaaddr
, *Gate
;
603 struct ifaddr
*ifa
= 0;
604 struct ifnet
*ifp
= 0;
606 /* new gateway could require new ifaddr, ifp;
607 flags may also be different; ifp may be specified
608 by ll sockaddr when protocol address is ambiguous */
609 if (Ifpaddr
&& (ifa
= ifa_ifwithnet(Ifpaddr
)) &&
610 (ifp
= ifa
->ifa_ifp
) && (Ifaaddr
|| Gate
))
611 ifa
= ifaof_ifpforaddr(Ifaaddr
? Ifaaddr
: Gate
,
613 else if (Ifpaddr
&& (ifp
= if_withname(Ifpaddr
)) ) {
614 ifa
= Gate
? ifaof_ifpforaddr(Gate
, ifp
) :
615 TAILQ_FIRST(&ifp
->if_addrhead
);
617 else if ((Ifaaddr
&& (ifa
= ifa_ifwithaddr(Ifaaddr
))) ||
618 (Gate
&& (ifa
= ifa_ifwithroute(rt
->rt_flags
,
622 register struct ifaddr
*oifa
= rt
->rt_ifa
;
624 if (oifa
&& oifa
->ifa_rtrequest
)
625 oifa
->ifa_rtrequest(RTM_DELETE
,
629 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
630 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
631 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
637 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
638 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
639 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, Gate
);
644 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
645 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
649 * Extract the addresses of the passed sockaddrs.
650 * Do a little sanity checking so as to avoid bad memory references.
651 * This data is derived straight from userland.
654 rt_xaddrs(cp
, cplim
, rtinfo
)
655 register caddr_t cp
, cplim
;
656 register struct rt_addrinfo
*rtinfo
;
658 register struct sockaddr
*sa
;
661 bzero(rtinfo
->rti_info
, sizeof(rtinfo
->rti_info
));
662 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
663 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
665 sa
= (struct sockaddr
*)cp
;
669 if ( (cp
+ sa
->sa_len
) > cplim
) {
674 * there are no more.. quit now
675 * If there are more bits, they are in error.
676 * I've seen this. route(1) can evidently generate these.
677 * This causes kernel to core dump.
678 * for compatibility, If we see this, point to a safe address.
680 if (sa
->sa_len
== 0) {
681 rtinfo
->rti_info
[i
] = &sa_zero
;
682 return (0); /* should be EINVAL but for compat */
686 rtinfo
->rti_info
[i
] = sa
;
693 rt_msg1(type
, rtinfo
)
695 register struct rt_addrinfo
*rtinfo
;
697 register struct rt_msghdr
*rtm
;
698 register struct mbuf
*m
;
700 register struct sockaddr
*sa
;
707 len
= sizeof(struct ifa_msghdr
);
712 len
= sizeof(struct ifma_msghdr
);
716 len
= sizeof(struct if_msghdr
);
720 len
= sizeof(struct rt_msghdr
);
724 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
725 if (m
&& len
> MHLEN
) {
726 MCLGET(m
, M_DONTWAIT
);
727 if ((m
->m_flags
& M_EXT
) == 0) {
734 m
->m_pkthdr
.len
= m
->m_len
= len
;
735 m
->m_pkthdr
.rcvif
= 0;
736 rtm
= mtod(m
, struct rt_msghdr
*);
737 bzero((caddr_t
)rtm
, len
);
738 for (i
= 0; i
< RTAX_MAX
; i
++) {
739 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
741 rtinfo
->rti_addrs
|= (1 << i
);
742 dlen
= ROUNDUP(sa
->sa_len
);
743 m_copyback(m
, len
, dlen
, (caddr_t
)sa
);
746 if (m
->m_pkthdr
.len
!= len
) {
750 rtm
->rtm_msglen
= len
;
751 rtm
->rtm_version
= RTM_VERSION
;
752 rtm
->rtm_type
= type
;
757 rt_msg2(type
, rtinfo
, cp
, w
)
759 register struct rt_addrinfo
*rtinfo
;
764 int len
, dlen
, second_time
= 0;
767 rtinfo
->rti_addrs
= 0;
773 len
= sizeof(struct ifa_msghdr
);
777 len
= sizeof(struct if_msghdr
);
781 len
= sizeof(struct rt_msghdr
);
786 for (i
= 0; i
< RTAX_MAX
; i
++) {
787 register struct sockaddr
*sa
;
789 if ((sa
= rtinfo
->rti_info
[i
]) == 0)
791 rtinfo
->rti_addrs
|= (1 << i
);
792 dlen
= ROUNDUP(sa
->sa_len
);
794 bcopy((caddr_t
)sa
, cp
, (unsigned)dlen
);
799 if (cp
== 0 && w
!= NULL
&& !second_time
) {
800 register struct walkarg
*rw
= w
;
803 if (rw
->w_tmemsize
< len
) {
805 FREE(rw
->w_tmem
, M_RTABLE
);
806 rw
->w_tmem
= (caddr_t
)
807 _MALLOC(len
, M_RTABLE
, M_WAITOK
); /*###LD0412 was NOWAIT */
809 rw
->w_tmemsize
= len
;
819 register struct rt_msghdr
*rtm
= (struct rt_msghdr
*)cp0
;
821 rtm
->rtm_version
= RTM_VERSION
;
822 rtm
->rtm_type
= type
;
823 rtm
->rtm_msglen
= len
;
829 * This routine is called to generate a message from the routing
830 * socket indicating that a redirect has occured, a routing lookup
831 * has failed, or that a protocol has detected timeouts to a particular
835 rt_missmsg(type
, rtinfo
, flags
, error
)
836 int type
, flags
, error
;
837 register struct rt_addrinfo
*rtinfo
;
839 register struct rt_msghdr
*rtm
;
840 register struct mbuf
*m
;
841 struct sockaddr
*sa
= rtinfo
->rti_info
[RTAX_DST
];
843 if (route_cb
.any_count
== 0)
845 m
= rt_msg1(type
, rtinfo
);
848 rtm
= mtod(m
, struct rt_msghdr
*);
849 rtm
->rtm_flags
= RTF_DONE
| flags
;
850 rtm
->rtm_errno
= error
;
851 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
852 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
853 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
857 * This routine is called to generate a message from the routing
858 * socket indicating that the status of a network interface has changed.
862 register struct ifnet
*ifp
;
864 register struct if_msghdr
*ifm
;
866 struct rt_addrinfo info
;
868 if (route_cb
.any_count
== 0)
870 bzero((caddr_t
)&info
, sizeof(info
));
871 m
= rt_msg1(RTM_IFINFO
, &info
);
874 ifm
= mtod(m
, struct if_msghdr
*);
875 ifm
->ifm_index
= ifp
->if_index
;
876 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
877 ifm
->ifm_data
= ifp
->if_data
;
879 route_proto
.sp_protocol
= 0;
880 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
884 * This is called to generate messages from the routing socket
885 * indicating a network interface has had addresses associated with it.
886 * if we ever reverse the logic and replace messages TO the routing
887 * socket indicate a request to configure interfaces, then it will
888 * be unnecessary as the routing socket will automatically generate
892 rt_newaddrmsg(cmd
, ifa
, error
, rt
)
894 register struct ifaddr
*ifa
;
895 register struct rtentry
*rt
;
897 struct rt_addrinfo info
;
898 struct sockaddr
*sa
= 0;
901 struct ifnet
*ifp
= ifa
->ifa_ifp
;
903 if (route_cb
.any_count
== 0)
905 for (pass
= 1; pass
< 3; pass
++) {
906 bzero((caddr_t
)&info
, sizeof(info
));
907 if ((cmd
== RTM_ADD
&& pass
== 1) ||
908 (cmd
== RTM_DELETE
&& pass
== 2)) {
909 register struct ifa_msghdr
*ifam
;
910 int ncmd
= cmd
== RTM_ADD
? RTM_NEWADDR
: RTM_DELADDR
;
912 ifaaddr
= sa
= ifa
->ifa_addr
;
913 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
914 netmask
= ifa
->ifa_netmask
;
915 brdaddr
= ifa
->ifa_dstaddr
;
916 if ((m
= rt_msg1(ncmd
, &info
)) == NULL
)
918 ifam
= mtod(m
, struct ifa_msghdr
*);
919 ifam
->ifam_index
= ifp
->if_index
;
920 ifam
->ifam_metric
= ifa
->ifa_metric
;
921 ifam
->ifam_flags
= ifa
->ifa_flags
;
922 ifam
->ifam_addrs
= info
.rti_addrs
;
924 if ((cmd
== RTM_ADD
&& pass
== 2) ||
925 (cmd
== RTM_DELETE
&& pass
== 1)) {
926 register struct rt_msghdr
*rtm
;
930 netmask
= rt_mask(rt
);
931 dst
= sa
= rt_key(rt
);
932 gate
= rt
->rt_gateway
;
933 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
935 rtm
= mtod(m
, struct rt_msghdr
*);
936 rtm
->rtm_index
= ifp
->if_index
;
937 rtm
->rtm_flags
|= rt
->rt_flags
;
938 rtm
->rtm_errno
= error
;
939 rtm
->rtm_addrs
= info
.rti_addrs
;
941 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
942 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
947 * This is the analogue to the rt_newaddrmsg which performs the same
948 * function but for multicast group memberhips. This is easier since
949 * there is no route state to worry about.
952 rt_newmaddrmsg(cmd
, ifma
)
954 struct ifmultiaddr
*ifma
;
956 struct rt_addrinfo info
;
958 struct ifnet
*ifp
= ifma
->ifma_ifp
;
959 struct ifma_msghdr
*ifmam
;
961 if (route_cb
.any_count
== 0)
964 bzero((caddr_t
)&info
, sizeof(info
));
965 ifaaddr
= ifma
->ifma_addr
;
966 if (ifp
&& ifp
->if_addrhead
.tqh_first
)
967 ifpaddr
= ifp
->if_addrhead
.tqh_first
->ifa_addr
;
971 * If a link-layer address is present, present it as a ``gateway''
972 * (similarly to how ARP entries, e.g., are presented).
974 gate
= ifma
->ifma_lladdr
;
975 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
977 ifmam
= mtod(m
, struct ifma_msghdr
*);
978 ifmam
->ifmam_index
= ifp
->if_index
;
979 ifmam
->ifmam_addrs
= info
.rti_addrs
;
980 route_proto
.sp_protocol
= ifma
->ifma_addr
->sa_family
;
981 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
985 * This is used in dumping the kernel table via sysctl().
988 sysctl_dumpentry(rn
, vw
)
989 struct radix_node
*rn
;
992 register struct walkarg
*w
= vw
;
993 register struct rtentry
*rt
= (struct rtentry
*)rn
;
995 struct rt_addrinfo info
;
997 if (w
->w_op
== NET_RT_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
999 bzero((caddr_t
)&info
, sizeof(info
));
1001 gate
= rt
->rt_gateway
;
1002 netmask
= rt_mask(rt
);
1003 genmask
= rt
->rt_genmask
;
1004 size
= rt_msg2(RTM_GET
, &info
, 0, w
);
1005 if (w
->w_req
&& w
->w_tmem
) {
1006 register struct rt_msghdr
*rtm
= (struct rt_msghdr
*)w
->w_tmem
;
1008 rtm
->rtm_flags
= rt
->rt_flags
;
1009 rtm
->rtm_use
= rt
->rt_use
;
1010 rtm
->rtm_rmx
= rt
->rt_rmx
;
1011 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1012 rtm
->rtm_errno
= rtm
->rtm_pid
= rtm
->rtm_seq
= 0;
1013 rtm
->rtm_addrs
= info
.rti_addrs
;
1014 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1021 sysctl_iflist(af
, w
)
1023 register struct walkarg
*w
;
1025 register struct ifnet
*ifp
;
1026 register struct ifaddr
*ifa
;
1027 struct rt_addrinfo info
;
1030 bzero((caddr_t
)&info
, sizeof(info
));
1031 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1032 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1034 ifa
= ifp
->if_addrhead
.tqh_first
;
1035 ifpaddr
= ifa
->ifa_addr
;
1036 len
= rt_msg2(RTM_IFINFO
, &info
, (caddr_t
)0, w
);
1038 if (w
->w_req
&& w
->w_tmem
) {
1039 register struct if_msghdr
*ifm
;
1041 ifm
= (struct if_msghdr
*)w
->w_tmem
;
1042 ifm
->ifm_index
= ifp
->if_index
;
1043 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1044 ifm
->ifm_data
= ifp
->if_data
;
1045 ifm
->ifm_addrs
= info
.rti_addrs
;
1046 error
= SYSCTL_OUT(w
->w_req
,(caddr_t
)ifm
, len
);
1050 while ((ifa
= ifa
->ifa_link
.tqe_next
) != 0) {
1051 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1054 if (curproc
->p_prison
&& prison_if(curproc
, ifa
->ifa_addr
))
1057 ifaaddr
= ifa
->ifa_addr
;
1058 netmask
= ifa
->ifa_netmask
;
1059 brdaddr
= ifa
->ifa_dstaddr
;
1060 len
= rt_msg2(RTM_NEWADDR
, &info
, 0, w
);
1061 if (w
->w_req
&& w
->w_tmem
) {
1062 register struct ifa_msghdr
*ifam
;
1064 ifam
= (struct ifa_msghdr
*)w
->w_tmem
;
1065 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1066 ifam
->ifam_flags
= ifa
->ifa_flags
;
1067 ifam
->ifam_metric
= ifa
->ifa_metric
;
1068 ifam
->ifam_addrs
= info
.rti_addrs
;
1069 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, len
);
1074 ifaaddr
= netmask
= brdaddr
= 0;
1080 sysctl_rtsock SYSCTL_HANDLER_ARGS
1082 int *name
= (int *)arg1
;
1083 u_int namelen
= arg2
;
1084 register struct radix_node_head
*rnh
;
1085 int i
, s
, error
= EINVAL
;
1096 Bzero(&w
, sizeof(w
));
1106 for (i
= 1; i
<= AF_MAX
; i
++)
1107 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
1108 (error
= rnh
->rnh_walktree(rnh
,
1109 sysctl_dumpentry
, &w
)))
1114 error
= sysctl_iflist(af
, &w
);
1118 FREE(w
.w_tmem
, M_RTABLE
);
1122 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
, sysctl_rtsock
, "");
1125 * Definitions of protocols supported in the ROUTE domain.
1128 struct domain routedomain
; /* or at least forward */
1130 static struct protosw routesw
[] = {
1131 { SOCK_RAW
, &routedomain
, 0, PR_ATOMIC
|PR_ADDR
,
1132 0, route_output
, raw_ctlinput
, 0,
1135 0, &route_usrreqs
, 0, 0
1139 struct domain routedomain
=
1140 { PF_ROUTE
, "route", route_init
, 0, 0,