2 * Copyright (c) 2000-2019 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
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kauth.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 <sys/mcache.h>
77 #include <kern/locks.h>
78 #include <sys/codesign.h>
81 #include <net/route.h>
83 #include <net/raw_cb.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_arp.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/nd6.h>
91 extern struct rtstat rtstat
;
92 extern struct domain routedomain_s
;
93 static struct domain
*routedomain
= NULL
;
95 MALLOC_DEFINE(M_RTABLE
, "routetbl", "routing tables");
97 static struct sockaddr route_dst
= { .sa_len
= 2, .sa_family
= PF_ROUTE
, .sa_data
= { 0, } };
98 static struct sockaddr route_src
= { .sa_len
= 2, .sa_family
= PF_ROUTE
, .sa_data
= { 0, } };
99 static struct sockaddr sa_zero
= { .sa_len
= sizeof(sa_zero
), .sa_family
= AF_INET
, .sa_data
= { 0, } };
102 u_int32_t ip_count
; /* attached w/ AF_INET */
103 u_int32_t ip6_count
; /* attached w/ AF_INET6 */
104 u_int32_t any_count
; /* total attached */
107 static struct route_cb route_cb
;
113 struct sysctl_req
*w_req
;
116 static void route_dinit(struct domain
*);
117 static int rts_abort(struct socket
*);
118 static int rts_attach(struct socket
*, int, struct proc
*);
119 static int rts_bind(struct socket
*, struct sockaddr
*, struct proc
*);
120 static int rts_connect(struct socket
*, struct sockaddr
*, struct proc
*);
121 static int rts_detach(struct socket
*);
122 static int rts_disconnect(struct socket
*);
123 static int rts_peeraddr(struct socket
*, struct sockaddr
**);
124 static int rts_send(struct socket
*, int, struct mbuf
*, struct sockaddr
*,
125 struct mbuf
*, struct proc
*);
126 static int rts_shutdown(struct socket
*);
127 static int rts_sockaddr(struct socket
*, struct sockaddr
**);
129 static int route_output(struct mbuf
*, struct socket
*);
130 static int rt_setmetrics(u_int32_t
, struct rt_metrics
*, struct rtentry
*);
131 static void rt_getmetrics(struct rtentry
*, struct rt_metrics
*);
132 static void rt_setif(struct rtentry
*, struct sockaddr
*, struct sockaddr
*,
133 struct sockaddr
*, unsigned int);
134 static int rt_xaddrs(caddr_t
, caddr_t
, struct rt_addrinfo
*);
135 static struct mbuf
*rt_msg1(int, struct rt_addrinfo
*);
136 static int rt_msg2(int, struct rt_addrinfo
*, caddr_t
, struct walkarg
*,
138 static int sysctl_dumpentry(struct radix_node
*rn
, void *vw
);
139 static int sysctl_dumpentry_ext(struct radix_node
*rn
, void *vw
);
140 static int sysctl_iflist(int af
, struct walkarg
*w
);
141 static int sysctl_iflist2(int af
, struct walkarg
*w
);
142 static int sysctl_rtstat(struct sysctl_req
*);
143 static int sysctl_rttrash(struct sysctl_req
*);
144 static int sysctl_rtsock SYSCTL_HANDLER_ARGS
;
146 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
149 SYSCTL_NODE(_net
, OID_AUTO
, route
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "routing");
151 /* Align x to 1024 (only power of 2) assuming x is positive */
152 #define ALIGN_BYTES(x) do { \
153 x = P2ALIGN(x, 1024); \
156 #define ROUNDUP32(a) \
157 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (uint32_t) - 1))) : \
160 #define ADVANCE32(x, n) \
161 (x += ROUNDUP32((n)->sa_len))
163 #define RT_HAS_IFADDR(rt) \
164 ((rt)->rt_ifa != NULL && (rt)->rt_ifa->ifa_addr != NULL)
167 * It really doesn't make any sense at all for this code to share much
168 * with raw_usrreq.c, since its functionality is so restricted. XXX
171 rts_abort(struct socket
*so
)
173 return raw_usrreqs
.pru_abort(so
);
176 /* pru_accept is EOPNOTSUPP */
179 rts_attach(struct socket
*so
, int proto
, struct proc
*p
)
185 VERIFY(so
->so_pcb
== NULL
);
187 MALLOC(rp
, struct rawcb
*, sizeof(*rp
), M_PCB
, M_WAITOK
| M_ZERO
);
192 so
->so_pcb
= (caddr_t
)rp
;
193 /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
194 error
= raw_attach(so
, proto
);
199 so
->so_flags
|= SOF_PCBCLEARING
;
203 switch (rp
->rcb_proto
.sp_protocol
) {
205 atomic_add_32(&route_cb
.ip_count
, 1);
208 atomic_add_32(&route_cb
.ip6_count
, 1);
211 rp
->rcb_faddr
= &route_src
;
212 atomic_add_32(&route_cb
.any_count
, 1);
213 /* the socket is already locked when we enter rts_attach */
215 so
->so_options
|= SO_USELOOPBACK
;
220 rts_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
222 return raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
226 rts_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
228 return raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
231 /* pru_connect2 is EOPNOTSUPP */
232 /* pru_control is EOPNOTSUPP */
235 rts_detach(struct socket
*so
)
237 struct rawcb
*rp
= sotorawcb(so
);
241 switch (rp
->rcb_proto
.sp_protocol
) {
243 atomic_add_32(&route_cb
.ip_count
, -1);
246 atomic_add_32(&route_cb
.ip6_count
, -1);
249 atomic_add_32(&route_cb
.any_count
, -1);
250 return raw_usrreqs
.pru_detach(so
);
254 rts_disconnect(struct socket
*so
)
256 return raw_usrreqs
.pru_disconnect(so
);
259 /* pru_listen is EOPNOTSUPP */
262 rts_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
264 return raw_usrreqs
.pru_peeraddr(so
, nam
);
267 /* pru_rcvd is EOPNOTSUPP */
268 /* pru_rcvoob is EOPNOTSUPP */
271 rts_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
272 struct mbuf
*control
, struct proc
*p
)
274 return raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
277 /* pru_sense is null */
280 rts_shutdown(struct socket
*so
)
282 return raw_usrreqs
.pru_shutdown(so
);
286 rts_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
288 return raw_usrreqs
.pru_sockaddr(so
, nam
);
291 static struct pr_usrreqs route_usrreqs
= {
292 .pru_abort
= rts_abort
,
293 .pru_attach
= rts_attach
,
294 .pru_bind
= rts_bind
,
295 .pru_connect
= rts_connect
,
296 .pru_detach
= rts_detach
,
297 .pru_disconnect
= rts_disconnect
,
298 .pru_peeraddr
= rts_peeraddr
,
299 .pru_send
= rts_send
,
300 .pru_shutdown
= rts_shutdown
,
301 .pru_sockaddr
= rts_sockaddr
,
302 .pru_sosend
= sosend
,
303 .pru_soreceive
= soreceive
,
308 route_output(struct mbuf
*m
, struct socket
*so
)
310 struct rt_msghdr
*rtm
= NULL
;
311 struct rtentry
*rt
= NULL
;
312 struct rtentry
*saved_nrt
= NULL
;
313 struct radix_node_head
*rnh
;
314 struct rt_addrinfo info
;
316 sa_family_t dst_sa_family
= 0;
317 struct ifnet
*ifp
= NULL
;
318 struct sockaddr_in dst_in
, gate_in
;
319 int sendonlytoself
= 0;
320 unsigned int ifscope
= IFSCOPE_NONE
;
321 struct rawcb
*rp
= NULL
;
322 boolean_t is_router
= FALSE
;
323 #define senderr(e) { error = (e); goto flush; }
324 if (m
== NULL
|| ((m
->m_len
< sizeof(intptr_t)) &&
325 (m
= m_pullup(m
, sizeof(intptr_t))) == NULL
)) {
328 VERIFY(m
->m_flags
& M_PKTHDR
);
331 * Unlock the socket (but keep a reference) it won't be
332 * accessed until raw_input appends to it.
334 socket_unlock(so
, 0);
335 lck_mtx_lock(rnh_lock
);
337 len
= m
->m_pkthdr
.len
;
338 if (len
< sizeof(*rtm
) ||
339 len
!= mtod(m
, struct rt_msghdr
*)->rtm_msglen
) {
340 info
.rti_info
[RTAX_DST
] = NULL
;
343 R_Malloc(rtm
, struct rt_msghdr
*, len
);
345 info
.rti_info
[RTAX_DST
] = NULL
;
348 m_copydata(m
, 0, len
, (caddr_t
)rtm
);
349 if (rtm
->rtm_version
!= RTM_VERSION
) {
350 info
.rti_info
[RTAX_DST
] = NULL
;
351 senderr(EPROTONOSUPPORT
);
355 * Silent version of RTM_GET for Reachabiltiy APIs. We may change
356 * all RTM_GETs to be silent in the future, so this is private for now.
358 if (rtm
->rtm_type
== RTM_GET_SILENT
) {
359 if (!(so
->so_options
& SO_USELOOPBACK
)) {
363 rtm
->rtm_type
= RTM_GET
;
367 * Perform permission checking, only privileged sockets
368 * may perform operations other than RTM_GET
370 if (rtm
->rtm_type
!= RTM_GET
&& !(so
->so_state
& SS_PRIV
)) {
371 info
.rti_info
[RTAX_DST
] = NULL
;
375 rtm
->rtm_pid
= proc_selfpid();
376 info
.rti_addrs
= rtm
->rtm_addrs
;
377 if (rt_xaddrs((caddr_t
)(rtm
+ 1), len
+ (caddr_t
)rtm
, &info
)) {
378 info
.rti_info
[RTAX_DST
] = NULL
;
381 if (info
.rti_info
[RTAX_DST
] == NULL
||
382 info
.rti_info
[RTAX_DST
]->sa_family
>= AF_MAX
||
383 (info
.rti_info
[RTAX_GATEWAY
] != NULL
&&
384 info
.rti_info
[RTAX_GATEWAY
]->sa_family
>= AF_MAX
)) {
388 if (info
.rti_info
[RTAX_DST
]->sa_family
== AF_INET
&&
389 info
.rti_info
[RTAX_DST
]->sa_len
!= sizeof(struct sockaddr_in
)) {
390 /* At minimum, we need up to sin_addr */
391 if (info
.rti_info
[RTAX_DST
]->sa_len
<
392 offsetof(struct sockaddr_in
, sin_zero
)) {
395 bzero(&dst_in
, sizeof(dst_in
));
396 dst_in
.sin_len
= sizeof(dst_in
);
397 dst_in
.sin_family
= AF_INET
;
398 dst_in
.sin_port
= SIN(info
.rti_info
[RTAX_DST
])->sin_port
;
399 dst_in
.sin_addr
= SIN(info
.rti_info
[RTAX_DST
])->sin_addr
;
400 info
.rti_info
[RTAX_DST
] = (struct sockaddr
*)&dst_in
;
401 dst_sa_family
= info
.rti_info
[RTAX_DST
]->sa_family
;
402 } else if (info
.rti_info
[RTAX_DST
]->sa_family
== AF_INET6
&&
403 info
.rti_info
[RTAX_DST
]->sa_len
< sizeof(struct sockaddr_in6
)) {
407 if (info
.rti_info
[RTAX_GATEWAY
] != NULL
) {
408 if (info
.rti_info
[RTAX_GATEWAY
]->sa_family
== AF_INET
&&
409 info
.rti_info
[RTAX_GATEWAY
]->sa_len
!= sizeof(struct sockaddr_in
)) {
410 /* At minimum, we need up to sin_addr */
411 if (info
.rti_info
[RTAX_GATEWAY
]->sa_len
<
412 offsetof(struct sockaddr_in
, sin_zero
)) {
415 bzero(&gate_in
, sizeof(gate_in
));
416 gate_in
.sin_len
= sizeof(gate_in
);
417 gate_in
.sin_family
= AF_INET
;
418 gate_in
.sin_port
= SIN(info
.rti_info
[RTAX_GATEWAY
])->sin_port
;
419 gate_in
.sin_addr
= SIN(info
.rti_info
[RTAX_GATEWAY
])->sin_addr
;
420 info
.rti_info
[RTAX_GATEWAY
] = (struct sockaddr
*)&gate_in
;
421 } else if (info
.rti_info
[RTAX_GATEWAY
]->sa_family
== AF_INET6
&&
422 info
.rti_info
[RTAX_GATEWAY
]->sa_len
< sizeof(struct sockaddr_in6
)) {
427 if (info
.rti_info
[RTAX_GENMASK
]) {
428 struct radix_node
*t
;
429 t
= rn_addmask((caddr_t
)info
.rti_info
[RTAX_GENMASK
], 0, 1);
430 if (t
!= NULL
&& Bcmp(info
.rti_info
[RTAX_GENMASK
],
431 t
->rn_key
, *(u_char
*)info
.rti_info
[RTAX_GENMASK
]) == 0) {
432 info
.rti_info
[RTAX_GENMASK
] =
433 (struct sockaddr
*)(t
->rn_key
);
440 * If RTF_IFSCOPE flag is set, then rtm_index specifies the scope.
442 if (rtm
->rtm_flags
& RTF_IFSCOPE
) {
443 if (info
.rti_info
[RTAX_DST
]->sa_family
!= AF_INET
&&
444 info
.rti_info
[RTAX_DST
]->sa_family
!= AF_INET6
) {
447 ifscope
= rtm
->rtm_index
;
450 * Block changes on INTCOPROC interfaces.
453 unsigned int intcoproc_scope
= 0;
454 ifnet_head_lock_shared();
455 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
456 if (IFNET_IS_INTCOPROC(ifp
)) {
457 intcoproc_scope
= ifp
->if_index
;
462 if (intcoproc_scope
== ifscope
&& current_proc()->p_pid
!= 0) {
468 * RTF_PROXY can only be set internally from within the kernel.
470 if (rtm
->rtm_flags
& RTF_PROXY
) {
475 * For AF_INET, always zero out the embedded scope ID. If this is
476 * a scoped request, it must be done explicitly by setting RTF_IFSCOPE
477 * flag and the corresponding rtm_index value. This is to prevent
478 * false interpretation of the scope ID because it's using the sin_zero
479 * field, which might not be properly cleared by the requestor.
481 if (info
.rti_info
[RTAX_DST
]->sa_family
== AF_INET
) {
482 sin_set_ifscope(info
.rti_info
[RTAX_DST
], IFSCOPE_NONE
);
484 if (info
.rti_info
[RTAX_GATEWAY
] != NULL
&&
485 info
.rti_info
[RTAX_GATEWAY
]->sa_family
== AF_INET
) {
486 sin_set_ifscope(info
.rti_info
[RTAX_GATEWAY
], IFSCOPE_NONE
);
488 switch (rtm
->rtm_type
) {
490 if (info
.rti_info
[RTAX_GATEWAY
] == NULL
) {
494 error
= rtrequest_scoped_locked(RTM_ADD
,
495 info
.rti_info
[RTAX_DST
], info
.rti_info
[RTAX_GATEWAY
],
496 info
.rti_info
[RTAX_NETMASK
], rtm
->rtm_flags
, &saved_nrt
,
498 if (error
== 0 && saved_nrt
!= NULL
) {
501 * If the route request specified an interface with
502 * IFA and/or IFP, we set the requested interface on
503 * the route with rt_setif. It would be much better
504 * to do this inside rtrequest, but that would
505 * require passing the desired interface, in some
506 * form, to rtrequest. Since rtrequest is called in
507 * so many places (roughly 40 in our source), adding
508 * a parameter is to much for us to swallow; this is
509 * something for the FreeBSD developers to tackle.
510 * Instead, we let rtrequest compute whatever
511 * interface it wants, then come in behind it and
512 * stick in the interface that we really want. This
513 * works reasonably well except when rtrequest can't
514 * figure out what interface to use (with
515 * ifa_withroute) and returns ENETUNREACH. Ideally
516 * it shouldn't matter if rtrequest can't figure out
517 * the interface if we're going to explicitly set it
518 * ourselves anyway. But practically we can't
519 * recover here because rtrequest will not do any of
520 * the work necessary to add the route if it can't
521 * find an interface. As long as there is a default
522 * route that leads to some interface, rtrequest will
523 * find an interface, so this problem should be
524 * rarely encountered.
528 info
.rti_info
[RTAX_IFP
], info
.rti_info
[RTAX_IFA
],
529 info
.rti_info
[RTAX_GATEWAY
], ifscope
);
530 (void)rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
, saved_nrt
);
531 saved_nrt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
532 saved_nrt
->rt_rmx
.rmx_locks
|=
533 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
534 saved_nrt
->rt_genmask
= info
.rti_info
[RTAX_GENMASK
];
535 RT_REMREF_LOCKED(saved_nrt
);
536 RT_UNLOCK(saved_nrt
);
541 error
= rtrequest_scoped_locked(RTM_DELETE
,
542 info
.rti_info
[RTAX_DST
], info
.rti_info
[RTAX_GATEWAY
],
543 info
.rti_info
[RTAX_NETMASK
], rtm
->rtm_flags
, &saved_nrt
,
555 rnh
= rt_tables
[info
.rti_info
[RTAX_DST
]->sa_family
];
557 senderr(EAFNOSUPPORT
);
560 * Lookup the best match based on the key-mask pair;
561 * callee adds a reference and checks for root node.
563 rt
= rt_lookup(TRUE
, info
.rti_info
[RTAX_DST
],
564 info
.rti_info
[RTAX_NETMASK
], rnh
, ifscope
);
571 * Holding rnh_lock here prevents the possibility of
572 * ifa from changing (e.g. in_ifinit), so it is safe
573 * to access its ifa_addr (down below) without locking.
575 switch (rtm
->rtm_type
) {
581 cred
= kauth_cred_proc_ref(current_proc());
585 RT_LOCK_ASSERT_HELD(rt
);
586 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
587 dst_sa_family
= info
.rti_info
[RTAX_DST
]->sa_family
;
588 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
589 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
590 info
.rti_info
[RTAX_GENMASK
] = rt
->rt_genmask
;
591 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
594 ifnet_lock_shared(ifp
);
595 ifa2
= ifp
->if_lladdr
;
596 info
.rti_info
[RTAX_IFP
] =
599 ifnet_lock_done(ifp
);
600 info
.rti_info
[RTAX_IFA
] =
601 rt
->rt_ifa
->ifa_addr
;
602 rtm
->rtm_index
= ifp
->if_index
;
604 info
.rti_info
[RTAX_IFP
] = NULL
;
605 info
.rti_info
[RTAX_IFA
] = NULL
;
607 } else if ((ifp
= rt
->rt_ifp
) != NULL
) {
608 rtm
->rtm_index
= ifp
->if_index
;
613 len
= rt_msg2(rtm
->rtm_type
, &info
, NULL
, NULL
, credp
);
617 struct rt_msghdr
*out_rtm
;
618 R_Malloc(out_rtm
, struct rt_msghdr
*, len
);
619 if (out_rtm
== NULL
) {
626 Bcopy(rtm
, out_rtm
, sizeof(struct rt_msghdr
));
630 (void) rt_msg2(out_rtm
->rtm_type
, &info
, (caddr_t
)out_rtm
,
637 rtm
->rtm_flags
= rt
->rt_flags
;
638 rt_getmetrics(rt
, &rtm
->rtm_rmx
);
639 rtm
->rtm_addrs
= info
.rti_addrs
;
644 kauth_cred_unref(&cred
);
649 is_router
= (rt
->rt_flags
& RTF_ROUTER
) ? TRUE
: FALSE
;
651 if (info
.rti_info
[RTAX_GATEWAY
] != NULL
&&
652 (error
= rt_setgate(rt
, rt_key(rt
),
653 info
.rti_info
[RTAX_GATEWAY
]))) {
659 * If they tried to change things but didn't specify
660 * the required gateway, then just use the old one.
661 * This can happen if the user tries to change the
662 * flags on the default route without changing the
663 * default gateway. Changing flags still doesn't work.
665 if ((rt
->rt_flags
& RTF_GATEWAY
) &&
666 info
.rti_info
[RTAX_GATEWAY
] == NULL
) {
667 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
671 * On Darwin, we call rt_setif which contains the
672 * equivalent to the code found at this very spot
676 info
.rti_info
[RTAX_IFP
], info
.rti_info
[RTAX_IFA
],
677 info
.rti_info
[RTAX_GATEWAY
], ifscope
);
679 if ((error
= rt_setmetrics(rtm
->rtm_inits
,
680 &rtm
->rtm_rmx
, rt
))) {
685 if (info
.rti_info
[RTAX_GENMASK
]) {
686 rt
->rt_genmask
= info
.rti_info
[RTAX_GENMASK
];
690 * Enqueue work item to invoke callback for this route entry
691 * This may not be needed always, but for now issue it anytime
692 * RTM_CHANGE gets called.
694 route_event_enqueue_nwk_wq_entry(rt
, NULL
, ROUTE_ENTRY_REFRESH
, NULL
, TRUE
);
696 * If the route is for a router, walk the tree to send refresh
697 * event to protocol cloned entries
700 struct route_event rt_ev
;
701 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_ENTRY_REFRESH
);
703 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
, (void *)&rt_ev
);
708 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
709 rt
->rt_rmx
.rmx_locks
|=
710 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
721 rtm
->rtm_errno
= error
;
723 rtm
->rtm_flags
|= RTF_DONE
;
727 RT_LOCK_ASSERT_NOTHELD(rt
);
730 lck_mtx_unlock(rnh_lock
);
732 /* relock the socket now */
735 * Check to see if we don't want our own messages.
737 if (!(so
->so_options
& SO_USELOOPBACK
)) {
738 if (route_cb
.any_count
<= 1) {
745 /* There is another listener, so construct message */
749 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
750 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
753 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
) {
754 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
758 if (sendonlytoself
&& m
!= NULL
) {
760 if (sbappendaddr(&so
->so_rcv
, &route_src
, m
,
761 NULL
, &error
) != 0) {
768 struct sockproto route_proto
= { .sp_family
= PF_ROUTE
, .sp_protocol
= 0 };
770 rp
->rcb_proto
.sp_family
= 0; /* Avoid us */
772 if (dst_sa_family
!= 0) {
773 route_proto
.sp_protocol
= dst_sa_family
;
776 socket_unlock(so
, 0);
777 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
781 rp
->rcb_proto
.sp_family
= PF_ROUTE
;
788 rt_setexpire(struct rtentry
*rt
, uint64_t expiry
)
790 /* set both rt_expire and rmx_expire */
791 rt
->rt_expire
= expiry
;
793 rt
->rt_rmx
.rmx_expire
= expiry
+ rt
->base_calendartime
-
796 rt
->rt_rmx
.rmx_expire
= 0;
801 rt_setmetrics(u_int32_t which
, struct rt_metrics
*in
, struct rtentry
*out
)
803 if (!(which
& RTV_REFRESH_HOST
)) {
804 struct timeval caltime
;
805 getmicrotime(&caltime
);
806 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->e;
807 metric(RTV_RPIPE
, rmx_recvpipe
);
808 metric(RTV_SPIPE
, rmx_sendpipe
);
809 metric(RTV_SSTHRESH
, rmx_ssthresh
);
810 metric(RTV_RTT
, rmx_rtt
);
811 metric(RTV_RTTVAR
, rmx_rttvar
);
812 metric(RTV_HOPCOUNT
, rmx_hopcount
);
813 metric(RTV_MTU
, rmx_mtu
);
814 metric(RTV_EXPIRE
, rmx_expire
);
816 if (out
->rt_rmx
.rmx_expire
> 0) {
817 /* account for system time change */
818 getmicrotime(&caltime
);
819 out
->base_calendartime
+=
820 NET_CALCULATE_CLOCKSKEW(caltime
,
821 out
->base_calendartime
,
822 net_uptime(), out
->base_uptime
);
824 out
->rt_rmx
.rmx_expire
-
825 out
->base_calendartime
+
828 rt_setexpire(out
, 0);
831 VERIFY(out
->rt_expire
== 0 || out
->rt_rmx
.rmx_expire
!= 0);
832 VERIFY(out
->rt_expire
!= 0 || out
->rt_rmx
.rmx_expire
== 0);
834 /* Only RTV_REFRESH_HOST must be set */
835 if ((which
& ~RTV_REFRESH_HOST
) ||
836 (out
->rt_flags
& RTF_STATIC
) ||
837 !(out
->rt_flags
& RTF_LLINFO
)) {
841 if (out
->rt_llinfo_refresh
== NULL
) {
845 out
->rt_llinfo_refresh(out
);
851 rt_getmetrics(struct rtentry
*in
, struct rt_metrics
*out
)
853 struct timeval caltime
;
855 VERIFY(in
->rt_expire
== 0 || in
->rt_rmx
.rmx_expire
!= 0);
856 VERIFY(in
->rt_expire
!= 0 || in
->rt_rmx
.rmx_expire
== 0);
860 if (in
->rt_expire
!= 0) {
861 /* account for system time change */
862 getmicrotime(&caltime
);
864 in
->base_calendartime
+=
865 NET_CALCULATE_CLOCKSKEW(caltime
,
866 in
->base_calendartime
, net_uptime(), in
->base_uptime
);
868 out
->rmx_expire
= in
->base_calendartime
+
869 in
->rt_expire
- in
->base_uptime
;
876 * Set route's interface given info.rti_info[RTAX_IFP],
877 * info.rti_info[RTAX_IFA], and gateway.
880 rt_setif(struct rtentry
*rt
, struct sockaddr
*Ifpaddr
, struct sockaddr
*Ifaaddr
,
881 struct sockaddr
*Gate
, unsigned int ifscope
)
883 struct ifaddr
*ifa
= NULL
;
884 struct ifnet
*ifp
= NULL
;
885 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
887 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
889 RT_LOCK_ASSERT_HELD(rt
);
891 /* Don't update a defunct route */
892 if (rt
->rt_flags
& RTF_CONDEMNED
) {
896 /* Add an extra ref for ourselves */
897 RT_ADDREF_LOCKED(rt
);
899 /* Become a regular mutex, just in case */
903 * New gateway could require new ifaddr, ifp; flags may also
904 * be different; ifp may be specified by ll sockaddr when
905 * protocol address is ambiguous.
907 if (Ifpaddr
&& (ifa
= ifa_ifwithnet_scoped(Ifpaddr
, ifscope
)) &&
908 (ifp
= ifa
->ifa_ifp
) && (Ifaaddr
|| Gate
)) {
910 ifa
= ifaof_ifpforaddr(Ifaaddr
? Ifaaddr
: Gate
, ifp
);
916 if (Ifpaddr
&& (ifp
= if_withname(Ifpaddr
))) {
918 ifa
= ifaof_ifpforaddr(Gate
, ifp
);
920 ifnet_lock_shared(ifp
);
921 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
925 ifnet_lock_done(ifp
);
927 } else if (Ifaaddr
&&
928 (ifa
= ifa_ifwithaddr_scoped(Ifaaddr
, ifscope
))) {
930 } else if (Gate
!= NULL
) {
932 * Safe to drop rt_lock and use rt_key, since holding
933 * rnh_lock here prevents another thread from calling
934 * rt_setgate() on this route. We cannot hold the
935 * lock across ifa_ifwithroute since the lookup done
936 * by that routine may point to the same route.
939 if ((ifa
= ifa_ifwithroute_scoped_locked(rt
->rt_flags
,
940 rt_key(rt
), Gate
, ifscope
)) != NULL
) {
944 /* Don't update a defunct route */
945 if (rt
->rt_flags
& RTF_CONDEMNED
) {
949 /* Release extra ref */
950 RT_REMREF_LOCKED(rt
);
956 /* trigger route cache reevaluation */
957 if (rt_key(rt
)->sa_family
== AF_INET
) {
958 routegenid_inet_update();
961 else if (rt_key(rt
)->sa_family
== AF_INET6
) {
962 routegenid_inet6_update();
967 struct ifaddr
*oifa
= rt
->rt_ifa
;
971 ifa_rtrequest
= oifa
->ifa_rtrequest
;
973 if (ifa_rtrequest
!= NULL
) {
974 ifa_rtrequest(RTM_DELETE
, rt
, Gate
);
979 if (rt
->rt_ifp
!= ifp
) {
981 * Purge any link-layer info caching.
983 if (rt
->rt_llinfo_purge
!= NULL
) {
984 rt
->rt_llinfo_purge(rt
);
988 * Adjust route ref count for the interfaces.
990 if (rt
->rt_if_ref_fn
!= NULL
) {
991 rt
->rt_if_ref_fn(ifp
, 1);
992 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
997 * If this is the (non-scoped) default route, record
998 * the interface index used for the primary ifscope.
1000 if (rt_primary_default(rt
, rt_key(rt
))) {
1001 set_primary_ifscope(rt_key(rt
)->sa_family
,
1002 rt
->rt_ifp
->if_index
);
1005 * If rmx_mtu is not locked, update it
1006 * to the MTU used by the new interface.
1008 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1009 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
1010 if (rt_key(rt
)->sa_family
== AF_INET
&&
1011 INTF_ADJUST_MTU_FOR_CLAT46(ifp
)) {
1012 rt
->rt_rmx
.rmx_mtu
= IN6_LINKMTU(rt
->rt_ifp
);
1013 /* Further adjust the size for CLAT46 expansion */
1014 rt
->rt_rmx
.rmx_mtu
-= CLAT46_HDR_EXPANSION_OVERHD
;
1018 if (rt
->rt_ifa
!= NULL
) {
1019 IFA_LOCK_SPIN(rt
->rt_ifa
);
1020 ifa_rtrequest
= rt
->rt_ifa
->ifa_rtrequest
;
1021 IFA_UNLOCK(rt
->rt_ifa
);
1022 if (ifa_rtrequest
!= NULL
) {
1023 ifa_rtrequest(RTM_ADD
, rt
, Gate
);
1027 /* Release extra ref */
1028 RT_REMREF_LOCKED(rt
);
1035 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
1036 if (rt
->rt_ifa
!= NULL
) {
1037 IFA_LOCK_SPIN(rt
->rt_ifa
);
1038 ifa_rtrequest
= rt
->rt_ifa
->ifa_rtrequest
;
1039 IFA_UNLOCK(rt
->rt_ifa
);
1040 if (ifa_rtrequest
!= NULL
) {
1041 ifa_rtrequest(RTM_ADD
, rt
, Gate
);
1046 * Workaround for local address routes pointing to the loopback
1047 * interface added by configd, until <rdar://problem/12970142>.
1049 if ((rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) &&
1050 (rt
->rt_flags
& RTF_HOST
) && rt
->rt_ifa
->ifa_ifp
== rt
->rt_ifp
) {
1051 ifa
= ifa_ifwithaddr(rt_key(rt
));
1053 if (ifa
!= rt
->rt_ifa
) {
1060 /* Release extra ref */
1061 RT_REMREF_LOCKED(rt
);
1065 * Extract the addresses of the passed sockaddrs.
1066 * Do a little sanity checking so as to avoid bad memory references.
1067 * This data is derived straight from userland.
1070 rt_xaddrs(caddr_t cp
, caddr_t cplim
, struct rt_addrinfo
*rtinfo
)
1072 struct sockaddr
*sa
;
1075 bzero(rtinfo
->rti_info
, sizeof(rtinfo
->rti_info
));
1076 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
1077 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0) {
1080 sa
= (struct sockaddr
*)cp
;
1084 if ((cp
+ sa
->sa_len
) > cplim
) {
1088 * there are no more.. quit now
1089 * If there are more bits, they are in error.
1090 * I've seen this. route(1) can evidently generate these.
1091 * This causes kernel to core dump.
1092 * for compatibility, If we see this, point to a safe address.
1094 if (sa
->sa_len
== 0) {
1095 rtinfo
->rti_info
[i
] = &sa_zero
;
1096 return 0; /* should be EINVAL but for compat */
1099 rtinfo
->rti_info
[i
] = sa
;
1105 static struct mbuf
*
1106 rt_msg1(int type
, struct rt_addrinfo
*rtinfo
)
1108 struct rt_msghdr
*rtm
;
1116 len
= sizeof(struct ifa_msghdr
);
1121 len
= sizeof(struct ifma_msghdr
);
1125 len
= sizeof(struct if_msghdr
);
1129 len
= sizeof(struct rt_msghdr
);
1131 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
1132 if (m
&& len
> MHLEN
) {
1133 MCLGET(m
, M_DONTWAIT
);
1134 if (!(m
->m_flags
& M_EXT
)) {
1142 m
->m_pkthdr
.len
= m
->m_len
= len
;
1143 m
->m_pkthdr
.rcvif
= NULL
;
1144 rtm
= mtod(m
, struct rt_msghdr
*);
1145 bzero((caddr_t
)rtm
, len
);
1147 for (i
= 0; i
< RTAX_MAX
; i
++) {
1148 struct sockaddr
*sa
, *hint
;
1149 uint8_t ssbuf
[SOCK_MAXADDRLEN
+ 1];
1152 * Make sure to accomodate the largest possible size of sa_len.
1154 _CASSERT(sizeof(ssbuf
) == (SOCK_MAXADDRLEN
+ 1));
1156 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
) {
1163 if ((hint
= rtinfo
->rti_info
[RTAX_DST
]) == NULL
) {
1164 hint
= rtinfo
->rti_info
[RTAX_IFA
];
1167 /* Scrub away any trace of embedded interface scope */
1168 sa
= rtm_scrub(type
, i
, hint
, sa
, &ssbuf
,
1169 sizeof(ssbuf
), NULL
);
1176 rtinfo
->rti_addrs
|= (1 << i
);
1178 m_copyback(m
, off
, dlen
, (caddr_t
)sa
);
1180 off
+= ROUNDUP32(dlen
);
1182 if (m
->m_pkthdr
.len
!= len
) {
1186 rtm
->rtm_msglen
= len
;
1187 rtm
->rtm_version
= RTM_VERSION
;
1188 rtm
->rtm_type
= type
;
1193 rt_msg2(int type
, struct rt_addrinfo
*rtinfo
, caddr_t cp
, struct walkarg
*w
,
1194 kauth_cred_t
* credp
)
1197 int len
, dlen
, rlen
, second_time
= 0;
1200 rtinfo
->rti_addrs
= 0;
1205 len
= sizeof(struct ifa_msghdr
);
1210 len
= sizeof(struct ifma_msghdr
);
1214 len
= sizeof(struct if_msghdr
);
1218 len
= sizeof(struct if_msghdr2
);
1222 len
= sizeof(struct ifma_msghdr2
);
1226 len
= sizeof(struct rt_msghdr_ext
);
1230 len
= sizeof(struct rt_msghdr2
);
1234 len
= sizeof(struct rt_msghdr
);
1240 for (i
= 0; i
< RTAX_MAX
; i
++) {
1241 struct sockaddr
*sa
, *hint
;
1242 uint8_t ssbuf
[SOCK_MAXADDRLEN
+ 1];
1245 * Make sure to accomodate the largest possible size of sa_len.
1247 _CASSERT(sizeof(ssbuf
) == (SOCK_MAXADDRLEN
+ 1));
1249 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
) {
1256 if ((hint
= rtinfo
->rti_info
[RTAX_DST
]) == NULL
) {
1257 hint
= rtinfo
->rti_info
[RTAX_IFA
];
1260 /* Scrub away any trace of embedded interface scope */
1261 sa
= rtm_scrub(type
, i
, hint
, sa
, &ssbuf
,
1262 sizeof(ssbuf
), NULL
);
1266 sa
= rtm_scrub(type
, i
, NULL
, sa
, &ssbuf
,
1267 sizeof(ssbuf
), credp
);
1274 rtinfo
->rti_addrs
|= (1 << i
);
1276 rlen
= ROUNDUP32(dlen
);
1278 bcopy((caddr_t
)sa
, cp
, (size_t)dlen
);
1280 bzero(cp
+ dlen
, rlen
- dlen
);
1286 if (cp
== NULL
&& w
!= NULL
&& !second_time
) {
1287 struct walkarg
*rw
= w
;
1289 if (rw
->w_req
!= NULL
) {
1290 if (rw
->w_tmemsize
< len
) {
1291 if (rw
->w_tmem
!= NULL
) {
1292 FREE(rw
->w_tmem
, M_RTABLE
);
1294 rw
->w_tmem
= _MALLOC(len
, M_RTABLE
, M_ZERO
| M_WAITOK
);
1295 if (rw
->w_tmem
!= NULL
) {
1296 rw
->w_tmemsize
= len
;
1299 if (rw
->w_tmem
!= NULL
) {
1307 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)(void *)cp0
;
1309 rtm
->rtm_version
= RTM_VERSION
;
1310 rtm
->rtm_type
= type
;
1311 rtm
->rtm_msglen
= len
;
1317 * This routine is called to generate a message from the routing
1318 * socket indicating that a redirect has occurred, a routing lookup
1319 * has failed, or that a protocol has detected timeouts to a particular
1323 rt_missmsg(int type
, struct rt_addrinfo
*rtinfo
, int flags
, int error
)
1325 struct rt_msghdr
*rtm
;
1327 struct sockaddr
*sa
= rtinfo
->rti_info
[RTAX_DST
];
1328 struct sockproto route_proto
= { .sp_family
= PF_ROUTE
, .sp_protocol
= 0 };
1330 if (route_cb
.any_count
== 0) {
1333 m
= rt_msg1(type
, rtinfo
);
1337 rtm
= mtod(m
, struct rt_msghdr
*);
1338 rtm
->rtm_flags
= RTF_DONE
| flags
;
1339 rtm
->rtm_errno
= error
;
1340 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
1341 route_proto
.sp_family
= sa
? sa
->sa_family
: 0;
1342 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1346 * This routine is called to generate a message from the routing
1347 * socket indicating that the status of a network interface has changed.
1350 rt_ifmsg(struct ifnet
*ifp
)
1352 struct if_msghdr
*ifm
;
1354 struct rt_addrinfo info
;
1355 struct sockproto route_proto
= { .sp_family
= PF_ROUTE
, .sp_protocol
= 0 };
1357 if (route_cb
.any_count
== 0) {
1360 bzero((caddr_t
)&info
, sizeof(info
));
1361 m
= rt_msg1(RTM_IFINFO
, &info
);
1365 ifm
= mtod(m
, struct if_msghdr
*);
1366 ifm
->ifm_index
= ifp
->if_index
;
1367 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1368 if_data_internal_to_if_data(ifp
, &ifp
->if_data
, &ifm
->ifm_data
);
1370 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1374 * This is called to generate messages from the routing socket
1375 * indicating a network interface has had addresses associated with it.
1376 * if we ever reverse the logic and replace messages TO the routing
1377 * socket indicate a request to configure interfaces, then it will
1378 * be unnecessary as the routing socket will automatically generate
1381 * Since this is coming from the interface, it is expected that the
1382 * interface will be locked. Caller must hold rnh_lock and rt_lock.
1385 rt_newaddrmsg(int cmd
, struct ifaddr
*ifa
, int error
, struct rtentry
*rt
)
1387 struct rt_addrinfo info
;
1388 struct sockaddr
*sa
= 0;
1391 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1392 struct sockproto route_proto
= { .sp_family
= PF_ROUTE
, .sp_protocol
= 0 };
1394 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1395 RT_LOCK_ASSERT_HELD(rt
);
1397 if (route_cb
.any_count
== 0) {
1401 /* Become a regular mutex, just in case */
1402 RT_CONVERT_LOCK(rt
);
1403 for (pass
= 1; pass
< 3; pass
++) {
1404 bzero((caddr_t
)&info
, sizeof(info
));
1405 if ((cmd
== RTM_ADD
&& pass
== 1) ||
1406 (cmd
== RTM_DELETE
&& pass
== 2)) {
1407 struct ifa_msghdr
*ifam
;
1408 int ncmd
= cmd
== RTM_ADD
? RTM_NEWADDR
: RTM_DELADDR
;
1410 /* Lock ifp for if_lladdr */
1411 ifnet_lock_shared(ifp
);
1413 info
.rti_info
[RTAX_IFA
] = sa
= ifa
->ifa_addr
;
1415 * Holding ifnet lock here prevents the link address
1416 * from changing contents, so no need to hold its
1417 * lock. The link address is always present; it's
1420 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
1421 info
.rti_info
[RTAX_NETMASK
] = ifa
->ifa_netmask
;
1422 info
.rti_info
[RTAX_BRD
] = ifa
->ifa_dstaddr
;
1423 if ((m
= rt_msg1(ncmd
, &info
)) == NULL
) {
1425 ifnet_lock_done(ifp
);
1429 ifnet_lock_done(ifp
);
1430 ifam
= mtod(m
, struct ifa_msghdr
*);
1431 ifam
->ifam_index
= ifp
->if_index
;
1433 ifam
->ifam_metric
= ifa
->ifa_metric
;
1434 ifam
->ifam_flags
= ifa
->ifa_flags
;
1436 ifam
->ifam_addrs
= info
.rti_addrs
;
1438 if ((cmd
== RTM_ADD
&& pass
== 2) ||
1439 (cmd
== RTM_DELETE
&& pass
== 1)) {
1440 struct rt_msghdr
*rtm
;
1445 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1446 info
.rti_info
[RTAX_DST
] = sa
= rt_key(rt
);
1447 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1448 if ((m
= rt_msg1(cmd
, &info
)) == NULL
) {
1451 rtm
= mtod(m
, struct rt_msghdr
*);
1452 rtm
->rtm_index
= ifp
->if_index
;
1453 rtm
->rtm_flags
|= rt
->rt_flags
;
1454 rtm
->rtm_errno
= error
;
1455 rtm
->rtm_addrs
= info
.rti_addrs
;
1457 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
1458 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1463 * This is the analogue to the rt_newaddrmsg which performs the same
1464 * function but for multicast group memberhips. This is easier since
1465 * there is no route state to worry about.
1468 rt_newmaddrmsg(int cmd
, struct ifmultiaddr
*ifma
)
1470 struct rt_addrinfo info
;
1472 struct ifnet
*ifp
= ifma
->ifma_ifp
;
1473 struct ifma_msghdr
*ifmam
;
1474 struct sockproto route_proto
= { .sp_family
= PF_ROUTE
, .sp_protocol
= 0 };
1476 if (route_cb
.any_count
== 0) {
1480 /* Lock ifp for if_lladdr */
1481 ifnet_lock_shared(ifp
);
1482 bzero((caddr_t
)&info
, sizeof(info
));
1484 info
.rti_info
[RTAX_IFA
] = ifma
->ifma_addr
;
1485 /* lladdr doesn't need lock */
1486 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
1489 * If a link-layer address is present, present it as a ``gateway''
1490 * (similarly to how ARP entries, e.g., are presented).
1492 info
.rti_info
[RTAX_GATEWAY
] = (ifma
->ifma_ll
!= NULL
) ?
1493 ifma
->ifma_ll
->ifma_addr
: NULL
;
1494 if ((m
= rt_msg1(cmd
, &info
)) == NULL
) {
1496 ifnet_lock_done(ifp
);
1499 ifmam
= mtod(m
, struct ifma_msghdr
*);
1500 ifmam
->ifmam_index
= ifp
->if_index
;
1501 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1502 route_proto
.sp_protocol
= ifma
->ifma_addr
->sa_family
;
1504 ifnet_lock_done(ifp
);
1505 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1511 const char *c
= "RTM_?";
1562 case RTM_GET_SILENT
:
1563 c
= "RTM_GET_SILENT";
1569 c
= "RTM_NEWMADDR2";
1583 * This is used in dumping the kernel table via sysctl().
1586 sysctl_dumpentry(struct radix_node
*rn
, void *vw
)
1588 struct walkarg
*w
= vw
;
1589 struct rtentry
*rt
= (struct rtentry
*)rn
;
1590 int error
= 0, size
;
1591 struct rt_addrinfo info
;
1593 kauth_cred_t
*credp
;
1595 cred
= kauth_cred_proc_ref(current_proc());
1599 if ((w
->w_op
== NET_RT_FLAGS
|| w
->w_op
== NET_RT_FLAGS_PRIV
) &&
1600 !(rt
->rt_flags
& w
->w_arg
)) {
1605 * If the matching route has RTF_LLINFO set, then we can skip scrubbing the MAC
1606 * only if the outgoing interface is not loopback and the process has entitlement
1607 * for neighbor cache read.
1609 if (w
->w_op
== NET_RT_FLAGS_PRIV
&& (rt
->rt_flags
& RTF_LLINFO
)) {
1610 if (rt
->rt_ifp
!= lo_ifp
&&
1611 (route_op_entitlement_check(NULL
, cred
, ROUTE_OP_READ
, TRUE
) == 0)) {
1616 bzero((caddr_t
)&info
, sizeof(info
));
1617 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
1618 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1619 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1620 info
.rti_info
[RTAX_GENMASK
] = rt
->rt_genmask
;
1621 if (RT_HAS_IFADDR(rt
)) {
1622 info
.rti_info
[RTAX_IFA
] = rt
->rt_ifa
->ifa_addr
;
1625 if (w
->w_op
!= NET_RT_DUMP2
) {
1626 size
= rt_msg2(RTM_GET
, &info
, NULL
, w
, credp
);
1627 if (w
->w_req
!= NULL
&& w
->w_tmem
!= NULL
) {
1628 struct rt_msghdr
*rtm
=
1629 (struct rt_msghdr
*)(void *)w
->w_tmem
;
1631 rtm
->rtm_flags
= rt
->rt_flags
;
1632 rtm
->rtm_use
= rt
->rt_use
;
1633 rt_getmetrics(rt
, &rtm
->rtm_rmx
);
1634 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1638 rtm
->rtm_addrs
= info
.rti_addrs
;
1639 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1642 size
= rt_msg2(RTM_GET2
, &info
, NULL
, w
, credp
);
1643 if (w
->w_req
!= NULL
&& w
->w_tmem
!= NULL
) {
1644 struct rt_msghdr2
*rtm
=
1645 (struct rt_msghdr2
*)(void *)w
->w_tmem
;
1647 rtm
->rtm_flags
= rt
->rt_flags
;
1648 rtm
->rtm_use
= rt
->rt_use
;
1649 rt_getmetrics(rt
, &rtm
->rtm_rmx
);
1650 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1651 rtm
->rtm_refcnt
= rt
->rt_refcnt
;
1652 if (rt
->rt_parent
) {
1653 rtm
->rtm_parentflags
= rt
->rt_parent
->rt_flags
;
1655 rtm
->rtm_parentflags
= 0;
1657 rtm
->rtm_reserved
= 0;
1658 rtm
->rtm_addrs
= info
.rti_addrs
;
1659 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1665 kauth_cred_unref(&cred
);
1670 * This is used for dumping extended information from route entries.
1673 sysctl_dumpentry_ext(struct radix_node
*rn
, void *vw
)
1675 struct walkarg
*w
= vw
;
1676 struct rtentry
*rt
= (struct rtentry
*)rn
;
1677 int error
= 0, size
;
1678 struct rt_addrinfo info
;
1681 cred
= kauth_cred_proc_ref(current_proc());
1684 if (w
->w_op
== NET_RT_DUMPX_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
)) {
1687 bzero(&info
, sizeof(info
));
1688 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
1689 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1690 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1691 info
.rti_info
[RTAX_GENMASK
] = rt
->rt_genmask
;
1693 size
= rt_msg2(RTM_GET_EXT
, &info
, NULL
, w
, &cred
);
1694 if (w
->w_req
!= NULL
&& w
->w_tmem
!= NULL
) {
1695 struct rt_msghdr_ext
*ertm
=
1696 (struct rt_msghdr_ext
*)(void *)w
->w_tmem
;
1698 ertm
->rtm_flags
= rt
->rt_flags
;
1699 ertm
->rtm_use
= rt
->rt_use
;
1700 rt_getmetrics(rt
, &ertm
->rtm_rmx
);
1701 ertm
->rtm_index
= rt
->rt_ifp
->if_index
;
1704 ertm
->rtm_errno
= 0;
1705 ertm
->rtm_addrs
= info
.rti_addrs
;
1706 if (rt
->rt_llinfo_get_ri
== NULL
) {
1707 bzero(&ertm
->rtm_ri
, sizeof(ertm
->rtm_ri
));
1708 ertm
->rtm_ri
.ri_rssi
= IFNET_RSSI_UNKNOWN
;
1709 ertm
->rtm_ri
.ri_lqm
= IFNET_LQM_THRESH_OFF
;
1710 ertm
->rtm_ri
.ri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
1712 rt
->rt_llinfo_get_ri(rt
, &ertm
->rtm_ri
);
1714 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)ertm
, size
);
1719 kauth_cred_unref(&cred
);
1725 * To avoid to call copyout() while holding locks and to cause problems
1726 * in the paging path, sysctl_iflist() and sysctl_iflist2() contstruct
1727 * the list in two passes. In the first pass we compute the total
1728 * length of the data we are going to copyout, then we release
1729 * all locks to allocate a temporary buffer that gets filled
1730 * in the second pass.
1732 * Note that we are verifying the assumption that _MALLOC returns a buffer
1733 * that is at least 32 bits aligned and that the messages and addresses are
1737 sysctl_iflist(int af
, struct walkarg
*w
)
1741 struct rt_addrinfo info
;
1742 int len
= 0, error
= 0;
1744 int total_len
= 0, current_len
= 0;
1745 char *total_buffer
= NULL
, *cp
= NULL
;
1748 cred
= kauth_cred_proc_ref(current_proc());
1750 bzero((caddr_t
)&info
, sizeof(info
));
1752 for (pass
= 0; pass
< 2; pass
++) {
1753 ifnet_head_lock_shared();
1755 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1759 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
) {
1762 ifnet_lock_shared(ifp
);
1764 * Holding ifnet lock here prevents the link address
1765 * from changing contents, so no need to hold the ifa
1766 * lock. The link address is always present; it's
1769 ifa
= ifp
->if_lladdr
;
1770 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1771 len
= rt_msg2(RTM_IFINFO
, &info
, NULL
, NULL
, &cred
);
1775 struct if_msghdr
*ifm
;
1777 if (current_len
+ len
> total_len
) {
1778 ifnet_lock_done(ifp
);
1782 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1783 len
= rt_msg2(RTM_IFINFO
, &info
,
1784 (caddr_t
)cp
, NULL
, &cred
);
1785 info
.rti_info
[RTAX_IFP
] = NULL
;
1787 ifm
= (struct if_msghdr
*)(void *)cp
;
1788 ifm
->ifm_index
= ifp
->if_index
;
1789 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1790 if_data_internal_to_if_data(ifp
, &ifp
->if_data
,
1792 ifm
->ifm_addrs
= info
.rti_addrs
;
1794 * <rdar://problem/32940901>
1795 * Round bytes only for non-platform
1797 if (!csproc_get_platform_binary(w
->w_req
->p
)) {
1798 ALIGN_BYTES(ifm
->ifm_data
.ifi_ibytes
);
1799 ALIGN_BYTES(ifm
->ifm_data
.ifi_obytes
);
1803 VERIFY(IS_P2ALIGNED(cp
, sizeof(u_int32_t
)));
1806 while ((ifa
= ifa
->ifa_link
.tqe_next
) != NULL
) {
1808 if (af
&& af
!= ifa
->ifa_addr
->sa_family
) {
1812 if (ifa
->ifa_addr
->sa_family
== AF_INET6
&&
1813 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1814 IN6_IFF_CLAT46
) != 0) {
1818 info
.rti_info
[RTAX_IFA
] = ifa
->ifa_addr
;
1819 info
.rti_info
[RTAX_NETMASK
] = ifa
->ifa_netmask
;
1820 info
.rti_info
[RTAX_BRD
] = ifa
->ifa_dstaddr
;
1821 len
= rt_msg2(RTM_NEWADDR
, &info
, NULL
, NULL
,
1826 struct ifa_msghdr
*ifam
;
1828 if (current_len
+ len
> total_len
) {
1833 len
= rt_msg2(RTM_NEWADDR
, &info
,
1834 (caddr_t
)cp
, NULL
, &cred
);
1836 ifam
= (struct ifa_msghdr
*)(void *)cp
;
1838 ifa
->ifa_ifp
->if_index
;
1839 ifam
->ifam_flags
= ifa
->ifa_flags
;
1840 ifam
->ifam_metric
= ifa
->ifa_metric
;
1841 ifam
->ifam_addrs
= info
.rti_addrs
;
1844 VERIFY(IS_P2ALIGNED(cp
,
1845 sizeof(u_int32_t
)));
1850 ifnet_lock_done(ifp
);
1851 info
.rti_info
[RTAX_IFA
] = info
.rti_info
[RTAX_NETMASK
] =
1852 info
.rti_info
[RTAX_BRD
] = NULL
;
1858 if (error
== ENOBUFS
) {
1859 printf("%s: current_len (%d) + len (%d) > "
1860 "total_len (%d)\n", __func__
, current_len
,
1867 /* Better to return zero length buffer than ENOBUFS */
1868 if (total_len
== 0) {
1871 total_len
+= total_len
>> 3;
1872 total_buffer
= _MALLOC(total_len
, M_RTABLE
,
1874 if (total_buffer
== NULL
) {
1875 printf("%s: _MALLOC(%d) failed\n", __func__
,
1881 VERIFY(IS_P2ALIGNED(cp
, sizeof(u_int32_t
)));
1883 error
= SYSCTL_OUT(w
->w_req
, total_buffer
, current_len
);
1890 if (total_buffer
!= NULL
) {
1891 _FREE(total_buffer
, M_RTABLE
);
1894 kauth_cred_unref(&cred
);
1899 sysctl_iflist2(int af
, struct walkarg
*w
)
1903 struct rt_addrinfo info
;
1904 int len
= 0, error
= 0;
1906 int total_len
= 0, current_len
= 0;
1907 char *total_buffer
= NULL
, *cp
= NULL
;
1910 cred
= kauth_cred_proc_ref(current_proc());
1912 bzero((caddr_t
)&info
, sizeof(info
));
1914 for (pass
= 0; pass
< 2; pass
++) {
1915 struct ifmultiaddr
*ifma
;
1917 ifnet_head_lock_shared();
1919 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1923 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
) {
1926 ifnet_lock_shared(ifp
);
1928 * Holding ifnet lock here prevents the link address
1929 * from changing contents, so no need to hold the ifa
1930 * lock. The link address is always present; it's
1933 ifa
= ifp
->if_lladdr
;
1934 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1935 len
= rt_msg2(RTM_IFINFO2
, &info
, NULL
, NULL
, &cred
);
1939 struct if_msghdr2
*ifm
;
1941 if (current_len
+ len
> total_len
) {
1942 ifnet_lock_done(ifp
);
1946 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1947 len
= rt_msg2(RTM_IFINFO2
, &info
,
1948 (caddr_t
)cp
, NULL
, &cred
);
1949 info
.rti_info
[RTAX_IFP
] = NULL
;
1951 ifm
= (struct if_msghdr2
*)(void *)cp
;
1952 ifm
->ifm_addrs
= info
.rti_addrs
;
1953 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1954 ifm
->ifm_index
= ifp
->if_index
;
1955 ifm
->ifm_snd_len
= IFCQ_LEN(&ifp
->if_snd
);
1956 ifm
->ifm_snd_maxlen
= IFCQ_MAXLEN(&ifp
->if_snd
);
1957 ifm
->ifm_snd_drops
=
1958 ifp
->if_snd
.ifcq_dropcnt
.packets
;
1959 ifm
->ifm_timer
= ifp
->if_timer
;
1960 if_data_internal_to_if_data64(ifp
,
1961 &ifp
->if_data
, &ifm
->ifm_data
);
1963 * <rdar://problem/32940901>
1964 * Round bytes only for non-platform
1966 if (!csproc_get_platform_binary(w
->w_req
->p
)) {
1967 ALIGN_BYTES(ifm
->ifm_data
.ifi_ibytes
);
1968 ALIGN_BYTES(ifm
->ifm_data
.ifi_obytes
);
1972 VERIFY(IS_P2ALIGNED(cp
, sizeof(u_int32_t
)));
1975 while ((ifa
= ifa
->ifa_link
.tqe_next
) != NULL
) {
1977 if (af
&& af
!= ifa
->ifa_addr
->sa_family
) {
1981 if (ifa
->ifa_addr
->sa_family
== AF_INET6
&&
1982 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1983 IN6_IFF_CLAT46
) != 0) {
1988 info
.rti_info
[RTAX_IFA
] = ifa
->ifa_addr
;
1989 info
.rti_info
[RTAX_NETMASK
] = ifa
->ifa_netmask
;
1990 info
.rti_info
[RTAX_BRD
] = ifa
->ifa_dstaddr
;
1991 len
= rt_msg2(RTM_NEWADDR
, &info
, NULL
, NULL
,
1996 struct ifa_msghdr
*ifam
;
1998 if (current_len
+ len
> total_len
) {
2003 len
= rt_msg2(RTM_NEWADDR
, &info
,
2004 (caddr_t
)cp
, NULL
, &cred
);
2006 ifam
= (struct ifa_msghdr
*)(void *)cp
;
2008 ifa
->ifa_ifp
->if_index
;
2009 ifam
->ifam_flags
= ifa
->ifa_flags
;
2010 ifam
->ifam_metric
= ifa
->ifa_metric
;
2011 ifam
->ifam_addrs
= info
.rti_addrs
;
2014 VERIFY(IS_P2ALIGNED(cp
,
2015 sizeof(u_int32_t
)));
2021 ifnet_lock_done(ifp
);
2025 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
);
2026 ifma
!= NULL
; ifma
= LIST_NEXT(ifma
, ifma_link
)) {
2027 struct ifaddr
*ifa0
;
2030 if (af
&& af
!= ifma
->ifma_addr
->sa_family
) {
2034 bzero((caddr_t
)&info
, sizeof(info
));
2035 info
.rti_info
[RTAX_IFA
] = ifma
->ifma_addr
;
2037 * Holding ifnet lock here prevents the link
2038 * address from changing contents, so no need
2039 * to hold the ifa0 lock. The link address is
2040 * always present; it's never freed.
2042 ifa0
= ifp
->if_lladdr
;
2043 info
.rti_info
[RTAX_IFP
] = ifa0
->ifa_addr
;
2044 if (ifma
->ifma_ll
!= NULL
) {
2045 info
.rti_info
[RTAX_GATEWAY
] =
2046 ifma
->ifma_ll
->ifma_addr
;
2048 len
= rt_msg2(RTM_NEWMADDR2
, &info
, NULL
, NULL
,
2053 struct ifma_msghdr2
*ifmam
;
2055 if (current_len
+ len
> total_len
) {
2060 len
= rt_msg2(RTM_NEWMADDR2
, &info
,
2061 (caddr_t
)cp
, NULL
, &cred
);
2064 (struct ifma_msghdr2
*)(void *)cp
;
2065 ifmam
->ifmam_addrs
= info
.rti_addrs
;
2066 ifmam
->ifmam_flags
= 0;
2067 ifmam
->ifmam_index
=
2068 ifma
->ifma_ifp
->if_index
;
2069 ifmam
->ifmam_refcount
=
2073 VERIFY(IS_P2ALIGNED(cp
,
2074 sizeof(u_int32_t
)));
2079 ifnet_lock_done(ifp
);
2080 info
.rti_info
[RTAX_IFA
] = info
.rti_info
[RTAX_NETMASK
] =
2081 info
.rti_info
[RTAX_BRD
] = NULL
;
2086 if (error
== ENOBUFS
) {
2087 printf("%s: current_len (%d) + len (%d) > "
2088 "total_len (%d)\n", __func__
, current_len
,
2095 /* Better to return zero length buffer than ENOBUFS */
2096 if (total_len
== 0) {
2099 total_len
+= total_len
>> 3;
2100 total_buffer
= _MALLOC(total_len
, M_RTABLE
,
2102 if (total_buffer
== NULL
) {
2103 printf("%s: _MALLOC(%d) failed\n", __func__
,
2109 VERIFY(IS_P2ALIGNED(cp
, sizeof(u_int32_t
)));
2111 error
= SYSCTL_OUT(w
->w_req
, total_buffer
, current_len
);
2118 if (total_buffer
!= NULL
) {
2119 _FREE(total_buffer
, M_RTABLE
);
2122 kauth_cred_unref(&cred
);
2128 sysctl_rtstat(struct sysctl_req
*req
)
2130 return SYSCTL_OUT(req
, &rtstat
, sizeof(struct rtstat
));
2134 sysctl_rttrash(struct sysctl_req
*req
)
2136 return SYSCTL_OUT(req
, &rttrash
, sizeof(rttrash
));
2140 sysctl_rtsock SYSCTL_HANDLER_ARGS
2142 #pragma unused(oidp)
2143 int *name
= (int *)arg1
;
2144 u_int namelen
= arg2
;
2145 struct radix_node_head
*rnh
;
2146 int i
, error
= EINVAL
;
2159 Bzero(&w
, sizeof(w
));
2168 case NET_RT_FLAGS_PRIV
:
2169 lck_mtx_lock(rnh_lock
);
2170 for (i
= 1; i
<= AF_MAX
; i
++) {
2171 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
2172 (error
= rnh
->rnh_walktree(rnh
,
2173 sysctl_dumpentry
, &w
))) {
2177 lck_mtx_unlock(rnh_lock
);
2180 case NET_RT_DUMPX_FLAGS
:
2181 lck_mtx_lock(rnh_lock
);
2182 for (i
= 1; i
<= AF_MAX
; i
++) {
2183 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
2184 (error
= rnh
->rnh_walktree(rnh
,
2185 sysctl_dumpentry_ext
, &w
))) {
2189 lck_mtx_unlock(rnh_lock
);
2192 error
= sysctl_iflist(af
, &w
);
2194 case NET_RT_IFLIST2
:
2195 error
= sysctl_iflist2(af
, &w
);
2198 error
= sysctl_rtstat(req
);
2201 error
= sysctl_rttrash(req
);
2204 if (w
.w_tmem
!= NULL
) {
2205 FREE(w
.w_tmem
, M_RTABLE
);
2211 * Definitions of protocols supported in the ROUTE domain.
2213 static struct protosw routesw
[] = {
2215 .pr_type
= SOCK_RAW
,
2217 .pr_flags
= PR_ATOMIC
| PR_ADDR
,
2218 .pr_output
= route_output
,
2219 .pr_ctlinput
= raw_ctlinput
,
2220 .pr_init
= raw_init
,
2221 .pr_usrreqs
= &route_usrreqs
,
2225 static int route_proto_count
= (sizeof(routesw
) / sizeof(struct protosw
));
2227 struct domain routedomain_s
= {
2228 .dom_family
= PF_ROUTE
,
2229 .dom_name
= "route",
2230 .dom_init
= route_dinit
,
2234 route_dinit(struct domain
*dp
)
2239 VERIFY(!(dp
->dom_flags
& DOM_INITIALIZED
));
2240 VERIFY(routedomain
== NULL
);
2244 for (i
= 0, pr
= &routesw
[0]; i
< route_proto_count
; i
++, pr
++) {
2245 net_add_proto(pr
, dp
, 1);