2 * Copyright (c) 2000-2017 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 <netinet6/nd6.h>
89 extern struct rtstat rtstat
;
90 extern struct domain routedomain_s
;
91 static struct domain
*routedomain
= NULL
;
93 MALLOC_DEFINE(M_RTABLE
, "routetbl", "routing tables");
95 static struct sockaddr route_dst
= { 2, PF_ROUTE
, { 0, } };
96 static struct sockaddr route_src
= { 2, PF_ROUTE
, { 0, } };
97 static struct sockaddr sa_zero
= { sizeof (sa_zero
), AF_INET
, { 0, } };
100 u_int32_t ip_count
; /* attached w/ AF_INET */
101 u_int32_t ip6_count
; /* attached w/ AF_INET6 */
102 u_int32_t any_count
; /* total attached */
105 static struct route_cb route_cb
;
111 struct sysctl_req
*w_req
;
114 static void route_dinit(struct domain
*);
115 static int rts_abort(struct socket
*);
116 static int rts_attach(struct socket
*, int, struct proc
*);
117 static int rts_bind(struct socket
*, struct sockaddr
*, struct proc
*);
118 static int rts_connect(struct socket
*, struct sockaddr
*, struct proc
*);
119 static int rts_detach(struct socket
*);
120 static int rts_disconnect(struct socket
*);
121 static int rts_peeraddr(struct socket
*, struct sockaddr
**);
122 static int rts_send(struct socket
*, int, struct mbuf
*, struct sockaddr
*,
123 struct mbuf
*, struct proc
*);
124 static int rts_shutdown(struct socket
*);
125 static int rts_sockaddr(struct socket
*, struct sockaddr
**);
127 static int route_output(struct mbuf
*, struct socket
*);
128 static int rt_setmetrics(u_int32_t
, struct rt_metrics
*, struct rtentry
*);
129 static void rt_getmetrics(struct rtentry
*, struct rt_metrics
*);
130 static void rt_setif(struct rtentry
*, struct sockaddr
*, struct sockaddr
*,
131 struct sockaddr
*, unsigned int);
132 static int rt_xaddrs(caddr_t
, caddr_t
, struct rt_addrinfo
*);
133 static struct mbuf
*rt_msg1(int, struct rt_addrinfo
*);
134 static int rt_msg2(int, struct rt_addrinfo
*, caddr_t
, struct walkarg
*,
136 static int sysctl_dumpentry(struct radix_node
*rn
, void *vw
);
137 static int sysctl_dumpentry_ext(struct radix_node
*rn
, void *vw
);
138 static int sysctl_iflist(int af
, struct walkarg
*w
);
139 static int sysctl_iflist2(int af
, struct walkarg
*w
);
140 static int sysctl_rtstat(struct sysctl_req
*);
141 static int sysctl_rttrash(struct sysctl_req
*);
142 static int sysctl_rtsock SYSCTL_HANDLER_ARGS
;
144 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
147 SYSCTL_NODE(_net
, OID_AUTO
, route
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "routing");
149 /* Align x to 1024 (only power of 2) assuming x is positive */
150 #define ALIGN_BYTES(x) do { \
151 x = P2ALIGN(x, 1024); \
154 #define ROUNDUP32(a) \
155 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (uint32_t) - 1))) : \
158 #define ADVANCE32(x, n) \
159 (x += ROUNDUP32((n)->sa_len))
162 * It really doesn't make any sense at all for this code to share much
163 * with raw_usrreq.c, since its functionality is so restricted. XXX
166 rts_abort(struct socket
*so
)
168 return (raw_usrreqs
.pru_abort(so
));
171 /* pru_accept is EOPNOTSUPP */
174 rts_attach(struct socket
*so
, int proto
, struct proc
*p
)
180 VERIFY(so
->so_pcb
== NULL
);
182 MALLOC(rp
, struct rawcb
*, sizeof (*rp
), M_PCB
, M_WAITOK
| M_ZERO
);
186 so
->so_pcb
= (caddr_t
)rp
;
187 /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
188 error
= raw_attach(so
, proto
);
193 so
->so_flags
|= SOF_PCBCLEARING
;
197 switch (rp
->rcb_proto
.sp_protocol
) {
199 atomic_add_32(&route_cb
.ip_count
, 1);
202 atomic_add_32(&route_cb
.ip6_count
, 1);
205 rp
->rcb_faddr
= &route_src
;
206 atomic_add_32(&route_cb
.any_count
, 1);
207 /* the socket is already locked when we enter rts_attach */
209 so
->so_options
|= SO_USELOOPBACK
;
214 rts_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
216 return (raw_usrreqs
.pru_bind(so
, nam
, p
)); /* xxx just EINVAL */
220 rts_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
222 return (raw_usrreqs
.pru_connect(so
, nam
, p
)); /* XXX just EINVAL */
225 /* pru_connect2 is EOPNOTSUPP */
226 /* pru_control is EOPNOTSUPP */
229 rts_detach(struct socket
*so
)
231 struct rawcb
*rp
= sotorawcb(so
);
235 switch (rp
->rcb_proto
.sp_protocol
) {
237 atomic_add_32(&route_cb
.ip_count
, -1);
240 atomic_add_32(&route_cb
.ip6_count
, -1);
243 atomic_add_32(&route_cb
.any_count
, -1);
244 return (raw_usrreqs
.pru_detach(so
));
248 rts_disconnect(struct socket
*so
)
250 return (raw_usrreqs
.pru_disconnect(so
));
253 /* pru_listen is EOPNOTSUPP */
256 rts_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
258 return (raw_usrreqs
.pru_peeraddr(so
, nam
));
261 /* pru_rcvd is EOPNOTSUPP */
262 /* pru_rcvoob is EOPNOTSUPP */
265 rts_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
266 struct mbuf
*control
, struct proc
*p
)
268 return (raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
));
271 /* pru_sense is null */
274 rts_shutdown(struct socket
*so
)
276 return (raw_usrreqs
.pru_shutdown(so
));
280 rts_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
282 return (raw_usrreqs
.pru_sockaddr(so
, nam
));
285 static struct pr_usrreqs route_usrreqs
= {
286 .pru_abort
= rts_abort
,
287 .pru_attach
= rts_attach
,
288 .pru_bind
= rts_bind
,
289 .pru_connect
= rts_connect
,
290 .pru_detach
= rts_detach
,
291 .pru_disconnect
= rts_disconnect
,
292 .pru_peeraddr
= rts_peeraddr
,
293 .pru_send
= rts_send
,
294 .pru_shutdown
= rts_shutdown
,
295 .pru_sockaddr
= rts_sockaddr
,
296 .pru_sosend
= sosend
,
297 .pru_soreceive
= soreceive
,
302 route_output(struct mbuf
*m
, struct socket
*so
)
304 struct rt_msghdr
*rtm
= NULL
;
305 struct rtentry
*rt
= NULL
;
306 struct rtentry
*saved_nrt
= NULL
;
307 struct radix_node_head
*rnh
;
308 struct rt_addrinfo info
;
310 sa_family_t dst_sa_family
= 0;
311 struct ifnet
*ifp
= NULL
;
312 struct sockaddr_in dst_in
, gate_in
;
313 int sendonlytoself
= 0;
314 unsigned int ifscope
= IFSCOPE_NONE
;
315 struct rawcb
*rp
= NULL
;
316 boolean_t is_router
= FALSE
;
317 #define senderr(e) { error = (e); goto flush; }
318 if (m
== NULL
|| ((m
->m_len
< sizeof (intptr_t)) &&
319 (m
= m_pullup(m
, sizeof (intptr_t))) == NULL
))
321 VERIFY(m
->m_flags
& M_PKTHDR
);
324 * Unlock the socket (but keep a reference) it won't be
325 * accessed until raw_input appends to it.
327 socket_unlock(so
, 0);
328 lck_mtx_lock(rnh_lock
);
330 len
= m
->m_pkthdr
.len
;
331 if (len
< sizeof (*rtm
) ||
332 len
!= mtod(m
, struct rt_msghdr
*)->rtm_msglen
) {
333 info
.rti_info
[RTAX_DST
] = NULL
;
336 R_Malloc(rtm
, struct rt_msghdr
*, len
);
338 info
.rti_info
[RTAX_DST
] = NULL
;
341 m_copydata(m
, 0, len
, (caddr_t
)rtm
);
342 if (rtm
->rtm_version
!= RTM_VERSION
) {
343 info
.rti_info
[RTAX_DST
] = NULL
;
344 senderr(EPROTONOSUPPORT
);
348 * Silent version of RTM_GET for Reachabiltiy APIs. We may change
349 * all RTM_GETs to be silent in the future, so this is private for now.
351 if (rtm
->rtm_type
== RTM_GET_SILENT
) {
352 if (!(so
->so_options
& SO_USELOOPBACK
))
355 rtm
->rtm_type
= RTM_GET
;
359 * Perform permission checking, only privileged sockets
360 * may perform operations other than RTM_GET
362 if (rtm
->rtm_type
!= RTM_GET
&& !(so
->so_state
& SS_PRIV
)) {
363 info
.rti_info
[RTAX_DST
] = NULL
;
367 rtm
->rtm_pid
= proc_selfpid();
368 info
.rti_addrs
= rtm
->rtm_addrs
;
369 if (rt_xaddrs((caddr_t
)(rtm
+ 1), len
+ (caddr_t
)rtm
, &info
)) {
370 info
.rti_info
[RTAX_DST
] = NULL
;
373 if (info
.rti_info
[RTAX_DST
] == NULL
||
374 info
.rti_info
[RTAX_DST
]->sa_family
>= AF_MAX
||
375 (info
.rti_info
[RTAX_GATEWAY
] != NULL
&&
376 info
.rti_info
[RTAX_GATEWAY
]->sa_family
>= AF_MAX
))
379 if (info
.rti_info
[RTAX_DST
]->sa_family
== AF_INET
&&
380 info
.rti_info
[RTAX_DST
]->sa_len
!= sizeof (dst_in
)) {
381 /* At minimum, we need up to sin_addr */
382 if (info
.rti_info
[RTAX_DST
]->sa_len
<
383 offsetof(struct sockaddr_in
, sin_zero
))
385 bzero(&dst_in
, sizeof (dst_in
));
386 dst_in
.sin_len
= sizeof (dst_in
);
387 dst_in
.sin_family
= AF_INET
;
388 dst_in
.sin_port
= SIN(info
.rti_info
[RTAX_DST
])->sin_port
;
389 dst_in
.sin_addr
= SIN(info
.rti_info
[RTAX_DST
])->sin_addr
;
390 info
.rti_info
[RTAX_DST
] = (struct sockaddr
*)&dst_in
;
391 dst_sa_family
= info
.rti_info
[RTAX_DST
]->sa_family
;
394 if (info
.rti_info
[RTAX_GATEWAY
] != NULL
&&
395 info
.rti_info
[RTAX_GATEWAY
]->sa_family
== AF_INET
&&
396 info
.rti_info
[RTAX_GATEWAY
]->sa_len
!= sizeof (gate_in
)) {
397 /* At minimum, we need up to sin_addr */
398 if (info
.rti_info
[RTAX_GATEWAY
]->sa_len
<
399 offsetof(struct sockaddr_in
, sin_zero
))
401 bzero(&gate_in
, sizeof (gate_in
));
402 gate_in
.sin_len
= sizeof (gate_in
);
403 gate_in
.sin_family
= AF_INET
;
404 gate_in
.sin_port
= SIN(info
.rti_info
[RTAX_GATEWAY
])->sin_port
;
405 gate_in
.sin_addr
= SIN(info
.rti_info
[RTAX_GATEWAY
])->sin_addr
;
406 info
.rti_info
[RTAX_GATEWAY
] = (struct sockaddr
*)&gate_in
;
409 if (info
.rti_info
[RTAX_GENMASK
]) {
410 struct radix_node
*t
;
411 t
= rn_addmask((caddr_t
)info
.rti_info
[RTAX_GENMASK
], 0, 1);
412 if (t
!= NULL
&& Bcmp(info
.rti_info
[RTAX_GENMASK
],
413 t
->rn_key
, *(u_char
*)info
.rti_info
[RTAX_GENMASK
]) == 0)
414 info
.rti_info
[RTAX_GENMASK
] =
415 (struct sockaddr
*)(t
->rn_key
);
421 * If RTF_IFSCOPE flag is set, then rtm_index specifies the scope.
423 if (rtm
->rtm_flags
& RTF_IFSCOPE
) {
424 if (info
.rti_info
[RTAX_DST
]->sa_family
!= AF_INET
&&
425 info
.rti_info
[RTAX_DST
]->sa_family
!= AF_INET6
)
427 ifscope
= rtm
->rtm_index
;
430 * Block changes on INTCOPROC interfaces.
433 unsigned int intcoproc_scope
= 0;
434 ifnet_head_lock_shared();
435 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
436 if (IFNET_IS_INTCOPROC(ifp
)) {
437 intcoproc_scope
= ifp
->if_index
;
442 if (intcoproc_scope
== ifscope
&& current_proc()->p_pid
!= 0)
447 * RTF_PROXY can only be set internally from within the kernel.
449 if (rtm
->rtm_flags
& RTF_PROXY
)
453 * For AF_INET, always zero out the embedded scope ID. If this is
454 * a scoped request, it must be done explicitly by setting RTF_IFSCOPE
455 * flag and the corresponding rtm_index value. This is to prevent
456 * false interpretation of the scope ID because it's using the sin_zero
457 * field, which might not be properly cleared by the requestor.
459 if (info
.rti_info
[RTAX_DST
]->sa_family
== AF_INET
)
460 sin_set_ifscope(info
.rti_info
[RTAX_DST
], IFSCOPE_NONE
);
461 if (info
.rti_info
[RTAX_GATEWAY
] != NULL
&&
462 info
.rti_info
[RTAX_GATEWAY
]->sa_family
== AF_INET
)
463 sin_set_ifscope(info
.rti_info
[RTAX_GATEWAY
], IFSCOPE_NONE
);
465 switch (rtm
->rtm_type
) {
467 if (info
.rti_info
[RTAX_GATEWAY
] == NULL
)
470 error
= rtrequest_scoped_locked(RTM_ADD
,
471 info
.rti_info
[RTAX_DST
], info
.rti_info
[RTAX_GATEWAY
],
472 info
.rti_info
[RTAX_NETMASK
], rtm
->rtm_flags
, &saved_nrt
,
474 if (error
== 0 && saved_nrt
!= NULL
) {
477 * If the route request specified an interface with
478 * IFA and/or IFP, we set the requested interface on
479 * the route with rt_setif. It would be much better
480 * to do this inside rtrequest, but that would
481 * require passing the desired interface, in some
482 * form, to rtrequest. Since rtrequest is called in
483 * so many places (roughly 40 in our source), adding
484 * a parameter is to much for us to swallow; this is
485 * something for the FreeBSD developers to tackle.
486 * Instead, we let rtrequest compute whatever
487 * interface it wants, then come in behind it and
488 * stick in the interface that we really want. This
489 * works reasonably well except when rtrequest can't
490 * figure out what interface to use (with
491 * ifa_withroute) and returns ENETUNREACH. Ideally
492 * it shouldn't matter if rtrequest can't figure out
493 * the interface if we're going to explicitly set it
494 * ourselves anyway. But practically we can't
495 * recover here because rtrequest will not do any of
496 * the work necessary to add the route if it can't
497 * find an interface. As long as there is a default
498 * route that leads to some interface, rtrequest will
499 * find an interface, so this problem should be
500 * rarely encountered.
504 info
.rti_info
[RTAX_IFP
], info
.rti_info
[RTAX_IFA
],
505 info
.rti_info
[RTAX_GATEWAY
], ifscope
);
506 (void)rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
, saved_nrt
);
507 saved_nrt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
508 saved_nrt
->rt_rmx
.rmx_locks
|=
509 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
510 saved_nrt
->rt_genmask
= info
.rti_info
[RTAX_GENMASK
];
511 RT_REMREF_LOCKED(saved_nrt
);
512 RT_UNLOCK(saved_nrt
);
517 error
= rtrequest_scoped_locked(RTM_DELETE
,
518 info
.rti_info
[RTAX_DST
], info
.rti_info
[RTAX_GATEWAY
],
519 info
.rti_info
[RTAX_NETMASK
], rtm
->rtm_flags
, &saved_nrt
,
531 rnh
= rt_tables
[info
.rti_info
[RTAX_DST
]->sa_family
];
533 senderr(EAFNOSUPPORT
);
535 * Lookup the best match based on the key-mask pair;
536 * callee adds a reference and checks for root node.
538 rt
= rt_lookup(TRUE
, info
.rti_info
[RTAX_DST
],
539 info
.rti_info
[RTAX_NETMASK
], rnh
, ifscope
);
545 * Holding rnh_lock here prevents the possibility of
546 * ifa from changing (e.g. in_ifinit), so it is safe
547 * to access its ifa_addr (down below) without locking.
549 switch (rtm
->rtm_type
) {
554 cred
= kauth_cred_proc_ref(current_proc());
556 RT_LOCK_ASSERT_HELD(rt
);
557 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
558 dst_sa_family
= info
.rti_info
[RTAX_DST
]->sa_family
;
559 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
560 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
561 info
.rti_info
[RTAX_GENMASK
] = rt
->rt_genmask
;
562 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
565 ifnet_lock_shared(ifp
);
566 ifa2
= ifp
->if_lladdr
;
567 info
.rti_info
[RTAX_IFP
] =
570 ifnet_lock_done(ifp
);
571 info
.rti_info
[RTAX_IFA
] =
572 rt
->rt_ifa
->ifa_addr
;
573 rtm
->rtm_index
= ifp
->if_index
;
575 info
.rti_info
[RTAX_IFP
] = NULL
;
576 info
.rti_info
[RTAX_IFA
] = NULL
;
578 } else if ((ifp
= rt
->rt_ifp
) != NULL
) {
579 rtm
->rtm_index
= ifp
->if_index
;
583 len
= rt_msg2(rtm
->rtm_type
, &info
, NULL
, NULL
, &cred
);
586 if (len
> rtm
->rtm_msglen
) {
587 struct rt_msghdr
*new_rtm
;
588 R_Malloc(new_rtm
, struct rt_msghdr
*, len
);
589 if (new_rtm
== NULL
) {
595 Bcopy(rtm
, new_rtm
, rtm
->rtm_msglen
);
596 R_Free(rtm
); rtm
= new_rtm
;
600 (void) rt_msg2(rtm
->rtm_type
, &info
, (caddr_t
)rtm
,
604 rtm
->rtm_flags
= rt
->rt_flags
;
605 rt_getmetrics(rt
, &rtm
->rtm_rmx
);
606 rtm
->rtm_addrs
= info
.rti_addrs
;
610 kauth_cred_unref(&cred
);
615 is_router
= (rt
->rt_flags
& RTF_ROUTER
) ? TRUE
: FALSE
;
617 if (info
.rti_info
[RTAX_GATEWAY
] != NULL
&&
618 (error
= rt_setgate(rt
, rt_key(rt
),
619 info
.rti_info
[RTAX_GATEWAY
]))) {
625 * If they tried to change things but didn't specify
626 * the required gateway, then just use the old one.
627 * This can happen if the user tries to change the
628 * flags on the default route without changing the
629 * default gateway. Changing flags still doesn't work.
631 if ((rt
->rt_flags
& RTF_GATEWAY
) &&
632 info
.rti_info
[RTAX_GATEWAY
] == NULL
)
633 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
636 * On Darwin, we call rt_setif which contains the
637 * equivalent to the code found at this very spot
641 info
.rti_info
[RTAX_IFP
], info
.rti_info
[RTAX_IFA
],
642 info
.rti_info
[RTAX_GATEWAY
], ifscope
);
644 if ((error
= rt_setmetrics(rtm
->rtm_inits
,
645 &rtm
->rtm_rmx
, rt
))) {
650 if (info
.rti_info
[RTAX_GENMASK
])
651 rt
->rt_genmask
= info
.rti_info
[RTAX_GENMASK
];
654 * Enqueue work item to invoke callback for this route entry
655 * This may not be needed always, but for now issue it anytime
656 * RTM_CHANGE gets called.
658 route_event_enqueue_nwk_wq_entry(rt
, NULL
, ROUTE_ENTRY_REFRESH
, NULL
, TRUE
);
660 * If the route is for a router, walk the tree to send refresh
661 * event to protocol cloned entries
664 struct route_event rt_ev
;
665 route_event_init(&rt_ev
, rt
, NULL
, ROUTE_ENTRY_REFRESH
);
667 (void) rnh
->rnh_walktree(rnh
, route_event_walktree
, (void *)&rt_ev
);
672 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
673 rt
->rt_rmx
.rmx_locks
|=
674 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
686 rtm
->rtm_errno
= error
;
688 rtm
->rtm_flags
|= RTF_DONE
;
691 RT_LOCK_ASSERT_NOTHELD(rt
);
694 lck_mtx_unlock(rnh_lock
);
696 /* relock the socket now */
699 * Check to see if we don't want our own messages.
701 if (!(so
->so_options
& SO_USELOOPBACK
)) {
702 if (route_cb
.any_count
<= 1) {
708 /* There is another listener, so construct message */
712 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
713 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
716 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
) {
717 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
721 if (sendonlytoself
&& m
!= NULL
) {
723 if (sbappendaddr(&so
->so_rcv
, &route_src
, m
,
724 NULL
, &error
) != 0) {
730 struct sockproto route_proto
= { PF_ROUTE
, 0 };
732 rp
->rcb_proto
.sp_family
= 0; /* Avoid us */
733 if (dst_sa_family
!= 0)
734 route_proto
.sp_protocol
= dst_sa_family
;
736 socket_unlock(so
, 0);
737 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
741 rp
->rcb_proto
.sp_family
= PF_ROUTE
;
747 rt_setexpire(struct rtentry
*rt
, uint64_t expiry
)
749 /* set both rt_expire and rmx_expire */
750 rt
->rt_expire
= expiry
;
752 rt
->rt_rmx
.rmx_expire
= expiry
+ rt
->base_calendartime
-
755 rt
->rt_rmx
.rmx_expire
= 0;
760 rt_setmetrics(u_int32_t which
, struct rt_metrics
*in
, struct rtentry
*out
)
762 if (!(which
& RTV_REFRESH_HOST
)) {
763 struct timeval caltime
;
764 getmicrotime(&caltime
);
765 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->e;
766 metric(RTV_RPIPE
, rmx_recvpipe
);
767 metric(RTV_SPIPE
, rmx_sendpipe
);
768 metric(RTV_SSTHRESH
, rmx_ssthresh
);
769 metric(RTV_RTT
, rmx_rtt
);
770 metric(RTV_RTTVAR
, rmx_rttvar
);
771 metric(RTV_HOPCOUNT
, rmx_hopcount
);
772 metric(RTV_MTU
, rmx_mtu
);
773 metric(RTV_EXPIRE
, rmx_expire
);
775 if (out
->rt_rmx
.rmx_expire
> 0) {
776 /* account for system time change */
777 getmicrotime(&caltime
);
778 out
->base_calendartime
+=
779 NET_CALCULATE_CLOCKSKEW(caltime
,
780 out
->base_calendartime
,
781 net_uptime(), out
->base_uptime
);
783 out
->rt_rmx
.rmx_expire
-
784 out
->base_calendartime
+
787 rt_setexpire(out
, 0);
790 VERIFY(out
->rt_expire
== 0 || out
->rt_rmx
.rmx_expire
!= 0);
791 VERIFY(out
->rt_expire
!= 0 || out
->rt_rmx
.rmx_expire
== 0);
793 /* Only RTV_REFRESH_HOST must be set */
794 if ((which
& ~RTV_REFRESH_HOST
) ||
795 (out
->rt_flags
& RTF_STATIC
) ||
796 !(out
->rt_flags
& RTF_LLINFO
)) {
800 if (out
->rt_llinfo_refresh
== NULL
) {
804 out
->rt_llinfo_refresh(out
);
810 rt_getmetrics(struct rtentry
*in
, struct rt_metrics
*out
)
812 struct timeval caltime
;
814 VERIFY(in
->rt_expire
== 0 || in
->rt_rmx
.rmx_expire
!= 0);
815 VERIFY(in
->rt_expire
!= 0 || in
->rt_rmx
.rmx_expire
== 0);
819 if (in
->rt_expire
!= 0) {
820 /* account for system time change */
821 getmicrotime(&caltime
);
823 in
->base_calendartime
+=
824 NET_CALCULATE_CLOCKSKEW(caltime
,
825 in
->base_calendartime
, net_uptime(), in
->base_uptime
);
827 out
->rmx_expire
= in
->base_calendartime
+
828 in
->rt_expire
- in
->base_uptime
;
835 * Set route's interface given info.rti_info[RTAX_IFP],
836 * info.rti_info[RTAX_IFA], and gateway.
839 rt_setif(struct rtentry
*rt
, struct sockaddr
*Ifpaddr
, struct sockaddr
*Ifaaddr
,
840 struct sockaddr
*Gate
, unsigned int ifscope
)
842 struct ifaddr
*ifa
= NULL
;
843 struct ifnet
*ifp
= NULL
;
844 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
846 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
848 RT_LOCK_ASSERT_HELD(rt
);
850 /* Don't update a defunct route */
851 if (rt
->rt_flags
& RTF_CONDEMNED
)
854 /* Add an extra ref for ourselves */
855 RT_ADDREF_LOCKED(rt
);
857 /* Become a regular mutex, just in case */
861 * New gateway could require new ifaddr, ifp; flags may also
862 * be different; ifp may be specified by ll sockaddr when
863 * protocol address is ambiguous.
865 if (Ifpaddr
&& (ifa
= ifa_ifwithnet_scoped(Ifpaddr
, ifscope
)) &&
866 (ifp
= ifa
->ifa_ifp
) && (Ifaaddr
|| Gate
)) {
868 ifa
= ifaof_ifpforaddr(Ifaaddr
? Ifaaddr
: Gate
, ifp
);
874 if (Ifpaddr
&& (ifp
= if_withname(Ifpaddr
))) {
876 ifa
= ifaof_ifpforaddr(Gate
, ifp
);
878 ifnet_lock_shared(ifp
);
879 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
882 ifnet_lock_done(ifp
);
884 } else if (Ifaaddr
&&
885 (ifa
= ifa_ifwithaddr_scoped(Ifaaddr
, ifscope
))) {
887 } else if (Gate
!= NULL
) {
889 * Safe to drop rt_lock and use rt_key, since holding
890 * rnh_lock here prevents another thread from calling
891 * rt_setgate() on this route. We cannot hold the
892 * lock across ifa_ifwithroute since the lookup done
893 * by that routine may point to the same route.
896 if ((ifa
= ifa_ifwithroute_scoped_locked(rt
->rt_flags
,
897 rt_key(rt
), Gate
, ifscope
)) != NULL
)
900 /* Don't update a defunct route */
901 if (rt
->rt_flags
& RTF_CONDEMNED
) {
904 /* Release extra ref */
905 RT_REMREF_LOCKED(rt
);
911 /* trigger route cache reevaluation */
912 if (rt_key(rt
)->sa_family
== AF_INET
)
913 routegenid_inet_update();
915 else if (rt_key(rt
)->sa_family
== AF_INET6
)
916 routegenid_inet6_update();
920 struct ifaddr
*oifa
= rt
->rt_ifa
;
924 ifa_rtrequest
= oifa
->ifa_rtrequest
;
926 if (ifa_rtrequest
!= NULL
)
927 ifa_rtrequest(RTM_DELETE
, rt
, Gate
);
931 if (rt
->rt_ifp
!= ifp
) {
933 * Purge any link-layer info caching.
935 if (rt
->rt_llinfo_purge
!= NULL
)
936 rt
->rt_llinfo_purge(rt
);
939 * Adjust route ref count for the interfaces.
941 if (rt
->rt_if_ref_fn
!= NULL
) {
942 rt
->rt_if_ref_fn(ifp
, 1);
943 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
948 * If this is the (non-scoped) default route, record
949 * the interface index used for the primary ifscope.
951 if (rt_primary_default(rt
, rt_key(rt
))) {
952 set_primary_ifscope(rt_key(rt
)->sa_family
,
953 rt
->rt_ifp
->if_index
);
956 * If rmx_mtu is not locked, update it
957 * to the MTU used by the new interface.
959 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
960 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
962 if (rt
->rt_ifa
!= NULL
) {
963 IFA_LOCK_SPIN(rt
->rt_ifa
);
964 ifa_rtrequest
= rt
->rt_ifa
->ifa_rtrequest
;
965 IFA_UNLOCK(rt
->rt_ifa
);
966 if (ifa_rtrequest
!= NULL
)
967 ifa_rtrequest(RTM_ADD
, rt
, Gate
);
970 /* Release extra ref */
971 RT_REMREF_LOCKED(rt
);
978 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
979 if (rt
->rt_ifa
!= NULL
) {
980 IFA_LOCK_SPIN(rt
->rt_ifa
);
981 ifa_rtrequest
= rt
->rt_ifa
->ifa_rtrequest
;
982 IFA_UNLOCK(rt
->rt_ifa
);
983 if (ifa_rtrequest
!= NULL
)
984 ifa_rtrequest(RTM_ADD
, rt
, Gate
);
988 * Workaround for local address routes pointing to the loopback
989 * interface added by configd, until <rdar://problem/12970142>.
991 if ((rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) &&
992 (rt
->rt_flags
& RTF_HOST
) && rt
->rt_ifa
->ifa_ifp
== rt
->rt_ifp
) {
993 ifa
= ifa_ifwithaddr(rt_key(rt
));
995 if (ifa
!= rt
->rt_ifa
)
1001 /* Release extra ref */
1002 RT_REMREF_LOCKED(rt
);
1006 * Extract the addresses of the passed sockaddrs.
1007 * Do a little sanity checking so as to avoid bad memory references.
1008 * This data is derived straight from userland.
1011 rt_xaddrs(caddr_t cp
, caddr_t cplim
, struct rt_addrinfo
*rtinfo
)
1013 struct sockaddr
*sa
;
1016 bzero(rtinfo
->rti_info
, sizeof (rtinfo
->rti_info
));
1017 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
1018 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
1020 sa
= (struct sockaddr
*)cp
;
1024 if ((cp
+ sa
->sa_len
) > cplim
)
1027 * there are no more.. quit now
1028 * If there are more bits, they are in error.
1029 * I've seen this. route(1) can evidently generate these.
1030 * This causes kernel to core dump.
1031 * for compatibility, If we see this, point to a safe address.
1033 if (sa
->sa_len
== 0) {
1034 rtinfo
->rti_info
[i
] = &sa_zero
;
1035 return (0); /* should be EINVAL but for compat */
1038 rtinfo
->rti_info
[i
] = sa
;
1044 static struct mbuf
*
1045 rt_msg1(int type
, struct rt_addrinfo
*rtinfo
)
1047 struct rt_msghdr
*rtm
;
1056 len
= sizeof (struct ifa_msghdr
);
1061 len
= sizeof (struct ifma_msghdr
);
1065 len
= sizeof (struct if_msghdr
);
1069 len
= sizeof (struct rt_msghdr
);
1071 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
1072 if (m
&& len
> MHLEN
) {
1073 MCLGET(m
, M_DONTWAIT
);
1074 if (!(m
->m_flags
& M_EXT
)) {
1081 m
->m_pkthdr
.len
= m
->m_len
= len
;
1082 m
->m_pkthdr
.rcvif
= NULL
;
1083 rtm
= mtod(m
, struct rt_msghdr
*);
1084 bzero((caddr_t
)rtm
, len
);
1086 for (i
= 0; i
< RTAX_MAX
; i
++) {
1087 struct sockaddr
*sa
, *hint
;
1088 uint8_t ssbuf
[SOCK_MAXADDRLEN
+ 1];
1091 * Make sure to accomodate the largest possible size of sa_len.
1093 _CASSERT(sizeof (ssbuf
) == (SOCK_MAXADDRLEN
+ 1));
1095 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
1101 if ((hint
= rtinfo
->rti_info
[RTAX_DST
]) == NULL
)
1102 hint
= rtinfo
->rti_info
[RTAX_IFA
];
1104 /* Scrub away any trace of embedded interface scope */
1105 sa
= rtm_scrub(type
, i
, hint
, sa
, &ssbuf
,
1106 sizeof (ssbuf
), NULL
);
1113 rtinfo
->rti_addrs
|= (1 << i
);
1115 m_copyback(m
, off
, dlen
, (caddr_t
)sa
);
1117 off
+= ROUNDUP32(dlen
);
1119 if (m
->m_pkthdr
.len
!= len
) {
1123 rtm
->rtm_msglen
= len
;
1124 rtm
->rtm_version
= RTM_VERSION
;
1125 rtm
->rtm_type
= type
;
1130 rt_msg2(int type
, struct rt_addrinfo
*rtinfo
, caddr_t cp
, struct walkarg
*w
,
1131 kauth_cred_t
* credp
)
1134 int len
, dlen
, rlen
, second_time
= 0;
1137 rtinfo
->rti_addrs
= 0;
1143 len
= sizeof (struct ifa_msghdr
);
1148 len
= sizeof (struct ifma_msghdr
);
1152 len
= sizeof (struct if_msghdr
);
1156 len
= sizeof (struct if_msghdr2
);
1160 len
= sizeof (struct ifma_msghdr2
);
1164 len
= sizeof (struct rt_msghdr_ext
);
1168 len
= sizeof (struct rt_msghdr2
);
1172 len
= sizeof (struct rt_msghdr
);
1177 for (i
= 0; i
< RTAX_MAX
; i
++) {
1178 struct sockaddr
*sa
, *hint
;
1179 uint8_t ssbuf
[SOCK_MAXADDRLEN
+ 1];
1182 * Make sure to accomodate the largest possible size of sa_len.
1184 _CASSERT(sizeof (ssbuf
) == (SOCK_MAXADDRLEN
+ 1));
1186 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
1192 if ((hint
= rtinfo
->rti_info
[RTAX_DST
]) == NULL
)
1193 hint
= rtinfo
->rti_info
[RTAX_IFA
];
1195 /* Scrub away any trace of embedded interface scope */
1196 sa
= rtm_scrub(type
, i
, hint
, sa
, &ssbuf
,
1197 sizeof (ssbuf
), NULL
);
1201 sa
= rtm_scrub(type
, i
, NULL
, sa
, &ssbuf
,
1202 sizeof (ssbuf
), credp
);
1209 rtinfo
->rti_addrs
|= (1 << i
);
1211 rlen
= ROUNDUP32(dlen
);
1213 bcopy((caddr_t
)sa
, cp
, (size_t)dlen
);
1215 bzero(cp
+ dlen
, rlen
- dlen
);
1220 if (cp
== NULL
&& w
!= NULL
&& !second_time
) {
1221 struct walkarg
*rw
= w
;
1223 if (rw
->w_req
!= NULL
) {
1224 if (rw
->w_tmemsize
< len
) {
1225 if (rw
->w_tmem
!= NULL
)
1226 FREE(rw
->w_tmem
, M_RTABLE
);
1227 rw
->w_tmem
= _MALLOC(len
, M_RTABLE
, M_WAITOK
);
1228 if (rw
->w_tmem
!= NULL
)
1229 rw
->w_tmemsize
= len
;
1231 if (rw
->w_tmem
!= NULL
) {
1239 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)(void *)cp0
;
1241 rtm
->rtm_version
= RTM_VERSION
;
1242 rtm
->rtm_type
= type
;
1243 rtm
->rtm_msglen
= len
;
1249 * This routine is called to generate a message from the routing
1250 * socket indicating that a redirect has occurred, a routing lookup
1251 * has failed, or that a protocol has detected timeouts to a particular
1255 rt_missmsg(int type
, struct rt_addrinfo
*rtinfo
, int flags
, int error
)
1257 struct rt_msghdr
*rtm
;
1259 struct sockaddr
*sa
= rtinfo
->rti_info
[RTAX_DST
];
1260 struct sockproto route_proto
= { PF_ROUTE
, 0 };
1262 if (route_cb
.any_count
== 0)
1264 m
= rt_msg1(type
, rtinfo
);
1267 rtm
= mtod(m
, struct rt_msghdr
*);
1268 rtm
->rtm_flags
= RTF_DONE
| flags
;
1269 rtm
->rtm_errno
= error
;
1270 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
1271 route_proto
.sp_family
= sa
? sa
->sa_family
: 0;
1272 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1276 * This routine is called to generate a message from the routing
1277 * socket indicating that the status of a network interface has changed.
1280 rt_ifmsg(struct ifnet
*ifp
)
1282 struct if_msghdr
*ifm
;
1284 struct rt_addrinfo info
;
1285 struct sockproto route_proto
= { PF_ROUTE
, 0 };
1287 if (route_cb
.any_count
== 0)
1289 bzero((caddr_t
)&info
, sizeof (info
));
1290 m
= rt_msg1(RTM_IFINFO
, &info
);
1293 ifm
= mtod(m
, struct if_msghdr
*);
1294 ifm
->ifm_index
= ifp
->if_index
;
1295 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1296 if_data_internal_to_if_data(ifp
, &ifp
->if_data
, &ifm
->ifm_data
);
1298 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1302 * This is called to generate messages from the routing socket
1303 * indicating a network interface has had addresses associated with it.
1304 * if we ever reverse the logic and replace messages TO the routing
1305 * socket indicate a request to configure interfaces, then it will
1306 * be unnecessary as the routing socket will automatically generate
1309 * Since this is coming from the interface, it is expected that the
1310 * interface will be locked. Caller must hold rnh_lock and rt_lock.
1313 rt_newaddrmsg(int cmd
, struct ifaddr
*ifa
, int error
, struct rtentry
*rt
)
1315 struct rt_addrinfo info
;
1316 struct sockaddr
*sa
= 0;
1319 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1320 struct sockproto route_proto
= { PF_ROUTE
, 0 };
1322 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1323 RT_LOCK_ASSERT_HELD(rt
);
1325 if (route_cb
.any_count
== 0)
1328 /* Become a regular mutex, just in case */
1329 RT_CONVERT_LOCK(rt
);
1330 for (pass
= 1; pass
< 3; pass
++) {
1331 bzero((caddr_t
)&info
, sizeof (info
));
1332 if ((cmd
== RTM_ADD
&& pass
== 1) ||
1333 (cmd
== RTM_DELETE
&& pass
== 2)) {
1334 struct ifa_msghdr
*ifam
;
1335 int ncmd
= cmd
== RTM_ADD
? RTM_NEWADDR
: RTM_DELADDR
;
1337 /* Lock ifp for if_lladdr */
1338 ifnet_lock_shared(ifp
);
1340 info
.rti_info
[RTAX_IFA
] = sa
= ifa
->ifa_addr
;
1342 * Holding ifnet lock here prevents the link address
1343 * from changing contents, so no need to hold its
1344 * lock. The link address is always present; it's
1347 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
1348 info
.rti_info
[RTAX_NETMASK
] = ifa
->ifa_netmask
;
1349 info
.rti_info
[RTAX_BRD
] = ifa
->ifa_dstaddr
;
1350 if ((m
= rt_msg1(ncmd
, &info
)) == NULL
) {
1352 ifnet_lock_done(ifp
);
1356 ifnet_lock_done(ifp
);
1357 ifam
= mtod(m
, struct ifa_msghdr
*);
1358 ifam
->ifam_index
= ifp
->if_index
;
1360 ifam
->ifam_metric
= ifa
->ifa_metric
;
1361 ifam
->ifam_flags
= ifa
->ifa_flags
;
1363 ifam
->ifam_addrs
= info
.rti_addrs
;
1365 if ((cmd
== RTM_ADD
&& pass
== 2) ||
1366 (cmd
== RTM_DELETE
&& pass
== 1)) {
1367 struct rt_msghdr
*rtm
;
1371 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1372 info
.rti_info
[RTAX_DST
] = sa
= rt_key(rt
);
1373 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1374 if ((m
= rt_msg1(cmd
, &info
)) == NULL
)
1376 rtm
= mtod(m
, struct rt_msghdr
*);
1377 rtm
->rtm_index
= ifp
->if_index
;
1378 rtm
->rtm_flags
|= rt
->rt_flags
;
1379 rtm
->rtm_errno
= error
;
1380 rtm
->rtm_addrs
= info
.rti_addrs
;
1382 route_proto
.sp_protocol
= sa
? sa
->sa_family
: 0;
1383 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1388 * This is the analogue to the rt_newaddrmsg which performs the same
1389 * function but for multicast group memberhips. This is easier since
1390 * there is no route state to worry about.
1393 rt_newmaddrmsg(int cmd
, struct ifmultiaddr
*ifma
)
1395 struct rt_addrinfo info
;
1397 struct ifnet
*ifp
= ifma
->ifma_ifp
;
1398 struct ifma_msghdr
*ifmam
;
1399 struct sockproto route_proto
= { PF_ROUTE
, 0 };
1401 if (route_cb
.any_count
== 0)
1404 /* Lock ifp for if_lladdr */
1405 ifnet_lock_shared(ifp
);
1406 bzero((caddr_t
)&info
, sizeof (info
));
1408 info
.rti_info
[RTAX_IFA
] = ifma
->ifma_addr
;
1409 /* lladdr doesn't need lock */
1410 info
.rti_info
[RTAX_IFP
] = ifp
->if_lladdr
->ifa_addr
;
1413 * If a link-layer address is present, present it as a ``gateway''
1414 * (similarly to how ARP entries, e.g., are presented).
1416 info
.rti_info
[RTAX_GATEWAY
] = (ifma
->ifma_ll
!= NULL
) ?
1417 ifma
->ifma_ll
->ifma_addr
: NULL
;
1418 if ((m
= rt_msg1(cmd
, &info
)) == NULL
) {
1420 ifnet_lock_done(ifp
);
1423 ifmam
= mtod(m
, struct ifma_msghdr
*);
1424 ifmam
->ifmam_index
= ifp
->if_index
;
1425 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1426 route_proto
.sp_protocol
= ifma
->ifma_addr
->sa_family
;
1428 ifnet_lock_done(ifp
);
1429 raw_input(m
, &route_proto
, &route_src
, &route_dst
);
1435 const char *c
= "RTM_?";
1486 case RTM_GET_SILENT
:
1487 c
= "RTM_GET_SILENT";
1493 c
= "RTM_NEWMADDR2";
1507 * This is used in dumping the kernel table via sysctl().
1510 sysctl_dumpentry(struct radix_node
*rn
, void *vw
)
1512 struct walkarg
*w
= vw
;
1513 struct rtentry
*rt
= (struct rtentry
*)rn
;
1514 int error
= 0, size
;
1515 struct rt_addrinfo info
;
1518 cred
= kauth_cred_proc_ref(current_proc());
1521 if (w
->w_op
== NET_RT_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
1523 bzero((caddr_t
)&info
, sizeof (info
));
1524 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
1525 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1526 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1527 info
.rti_info
[RTAX_GENMASK
] = rt
->rt_genmask
;
1529 if (w
->w_op
!= NET_RT_DUMP2
) {
1530 size
= rt_msg2(RTM_GET
, &info
, NULL
, w
, &cred
);
1531 if (w
->w_req
!= NULL
&& w
->w_tmem
!= NULL
) {
1532 struct rt_msghdr
*rtm
=
1533 (struct rt_msghdr
*)(void *)w
->w_tmem
;
1535 rtm
->rtm_flags
= rt
->rt_flags
;
1536 rtm
->rtm_use
= rt
->rt_use
;
1537 rt_getmetrics(rt
, &rtm
->rtm_rmx
);
1538 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1542 rtm
->rtm_addrs
= info
.rti_addrs
;
1543 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1546 size
= rt_msg2(RTM_GET2
, &info
, NULL
, w
, &cred
);
1547 if (w
->w_req
!= NULL
&& w
->w_tmem
!= NULL
) {
1548 struct rt_msghdr2
*rtm
=
1549 (struct rt_msghdr2
*)(void *)w
->w_tmem
;
1551 rtm
->rtm_flags
= rt
->rt_flags
;
1552 rtm
->rtm_use
= rt
->rt_use
;
1553 rt_getmetrics(rt
, &rtm
->rtm_rmx
);
1554 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1555 rtm
->rtm_refcnt
= rt
->rt_refcnt
;
1557 rtm
->rtm_parentflags
= rt
->rt_parent
->rt_flags
;
1559 rtm
->rtm_parentflags
= 0;
1560 rtm
->rtm_reserved
= 0;
1561 rtm
->rtm_addrs
= info
.rti_addrs
;
1562 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)rtm
, size
);
1568 kauth_cred_unref(&cred
);
1573 * This is used for dumping extended information from route entries.
1576 sysctl_dumpentry_ext(struct radix_node
*rn
, void *vw
)
1578 struct walkarg
*w
= vw
;
1579 struct rtentry
*rt
= (struct rtentry
*)rn
;
1580 int error
= 0, size
;
1581 struct rt_addrinfo info
;
1584 cred
= kauth_cred_proc_ref(current_proc());
1587 if (w
->w_op
== NET_RT_DUMPX_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
1589 bzero(&info
, sizeof (info
));
1590 info
.rti_info
[RTAX_DST
] = rt_key(rt
);
1591 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1592 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1593 info
.rti_info
[RTAX_GENMASK
] = rt
->rt_genmask
;
1595 size
= rt_msg2(RTM_GET_EXT
, &info
, NULL
, w
, &cred
);
1596 if (w
->w_req
!= NULL
&& w
->w_tmem
!= NULL
) {
1597 struct rt_msghdr_ext
*ertm
=
1598 (struct rt_msghdr_ext
*)(void *)w
->w_tmem
;
1600 ertm
->rtm_flags
= rt
->rt_flags
;
1601 ertm
->rtm_use
= rt
->rt_use
;
1602 rt_getmetrics(rt
, &ertm
->rtm_rmx
);
1603 ertm
->rtm_index
= rt
->rt_ifp
->if_index
;
1606 ertm
->rtm_errno
= 0;
1607 ertm
->rtm_addrs
= info
.rti_addrs
;
1608 if (rt
->rt_llinfo_get_ri
== NULL
) {
1609 bzero(&ertm
->rtm_ri
, sizeof (ertm
->rtm_ri
));
1610 ertm
->rtm_ri
.ri_rssi
= IFNET_RSSI_UNKNOWN
;
1611 ertm
->rtm_ri
.ri_lqm
= IFNET_LQM_THRESH_OFF
;
1612 ertm
->rtm_ri
.ri_npm
= IFNET_NPM_THRESH_UNKNOWN
;
1614 rt
->rt_llinfo_get_ri(rt
, &ertm
->rtm_ri
);
1616 error
= SYSCTL_OUT(w
->w_req
, (caddr_t
)ertm
, size
);
1621 kauth_cred_unref(&cred
);
1627 * To avoid to call copyout() while holding locks and to cause problems
1628 * in the paging path, sysctl_iflist() and sysctl_iflist2() contstruct
1629 * the list in two passes. In the first pass we compute the total
1630 * length of the data we are going to copyout, then we release
1631 * all locks to allocate a temporary buffer that gets filled
1632 * in the second pass.
1634 * Note that we are verifying the assumption that _MALLOC returns a buffer
1635 * that is at least 32 bits aligned and that the messages and addresses are
1639 sysctl_iflist(int af
, struct walkarg
*w
)
1643 struct rt_addrinfo info
;
1644 int len
= 0, error
= 0;
1646 int total_len
= 0, current_len
= 0;
1647 char *total_buffer
= NULL
, *cp
= NULL
;
1650 cred
= kauth_cred_proc_ref(current_proc());
1652 bzero((caddr_t
)&info
, sizeof (info
));
1654 for (pass
= 0; pass
< 2; pass
++) {
1655 ifnet_head_lock_shared();
1657 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1660 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1662 ifnet_lock_shared(ifp
);
1664 * Holding ifnet lock here prevents the link address
1665 * from changing contents, so no need to hold the ifa
1666 * lock. The link address is always present; it's
1669 ifa
= ifp
->if_lladdr
;
1670 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1671 len
= rt_msg2(RTM_IFINFO
, &info
, NULL
, NULL
, &cred
);
1675 struct if_msghdr
*ifm
;
1677 if (current_len
+ len
> total_len
) {
1678 ifnet_lock_done(ifp
);
1682 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1683 len
= rt_msg2(RTM_IFINFO
, &info
,
1684 (caddr_t
)cp
, NULL
, &cred
);
1685 info
.rti_info
[RTAX_IFP
] = NULL
;
1687 ifm
= (struct if_msghdr
*)(void *)cp
;
1688 ifm
->ifm_index
= ifp
->if_index
;
1689 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1690 if_data_internal_to_if_data(ifp
, &ifp
->if_data
,
1692 ifm
->ifm_addrs
= info
.rti_addrs
;
1694 * <rdar://problem/32940901>
1695 * Round bytes only for non-platform
1697 if (!csproc_get_platform_binary(w
->w_req
->p
)) {
1698 ALIGN_BYTES(ifm
->ifm_data
.ifi_ibytes
);
1699 ALIGN_BYTES(ifm
->ifm_data
.ifi_obytes
);
1703 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
1706 while ((ifa
= ifa
->ifa_link
.tqe_next
) != NULL
) {
1708 if (af
&& af
!= ifa
->ifa_addr
->sa_family
) {
1712 info
.rti_info
[RTAX_IFA
] = ifa
->ifa_addr
;
1713 info
.rti_info
[RTAX_NETMASK
] = ifa
->ifa_netmask
;
1714 info
.rti_info
[RTAX_BRD
] = ifa
->ifa_dstaddr
;
1715 len
= rt_msg2(RTM_NEWADDR
, &info
, NULL
, NULL
,
1720 struct ifa_msghdr
*ifam
;
1722 if (current_len
+ len
> total_len
) {
1727 len
= rt_msg2(RTM_NEWADDR
, &info
,
1728 (caddr_t
)cp
, NULL
, &cred
);
1730 ifam
= (struct ifa_msghdr
*)(void *)cp
;
1732 ifa
->ifa_ifp
->if_index
;
1733 ifam
->ifam_flags
= ifa
->ifa_flags
;
1734 ifam
->ifam_metric
= ifa
->ifa_metric
;
1735 ifam
->ifam_addrs
= info
.rti_addrs
;
1738 VERIFY(IS_P2ALIGNED(cp
,
1739 sizeof (u_int32_t
)));
1744 ifnet_lock_done(ifp
);
1745 info
.rti_info
[RTAX_IFA
] = info
.rti_info
[RTAX_NETMASK
] =
1746 info
.rti_info
[RTAX_BRD
] = NULL
;
1752 if (error
== ENOBUFS
)
1753 printf("%s: current_len (%d) + len (%d) > "
1754 "total_len (%d)\n", __func__
, current_len
,
1760 /* Better to return zero length buffer than ENOBUFS */
1763 total_len
+= total_len
>> 3;
1764 total_buffer
= _MALLOC(total_len
, M_RTABLE
,
1766 if (total_buffer
== NULL
) {
1767 printf("%s: _MALLOC(%d) failed\n", __func__
,
1773 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
1775 error
= SYSCTL_OUT(w
->w_req
, total_buffer
, current_len
);
1781 if (total_buffer
!= NULL
)
1782 _FREE(total_buffer
, M_RTABLE
);
1784 kauth_cred_unref(&cred
);
1789 sysctl_iflist2(int af
, struct walkarg
*w
)
1793 struct rt_addrinfo info
;
1794 int len
= 0, error
= 0;
1796 int total_len
= 0, current_len
= 0;
1797 char *total_buffer
= NULL
, *cp
= NULL
;
1800 cred
= kauth_cred_proc_ref(current_proc());
1802 bzero((caddr_t
)&info
, sizeof (info
));
1804 for (pass
= 0; pass
< 2; pass
++) {
1805 struct ifmultiaddr
*ifma
;
1807 ifnet_head_lock_shared();
1809 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1812 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1814 ifnet_lock_shared(ifp
);
1816 * Holding ifnet lock here prevents the link address
1817 * from changing contents, so no need to hold the ifa
1818 * lock. The link address is always present; it's
1821 ifa
= ifp
->if_lladdr
;
1822 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1823 len
= rt_msg2(RTM_IFINFO2
, &info
, NULL
, NULL
, &cred
);
1827 struct if_msghdr2
*ifm
;
1829 if (current_len
+ len
> total_len
) {
1830 ifnet_lock_done(ifp
);
1834 info
.rti_info
[RTAX_IFP
] = ifa
->ifa_addr
;
1835 len
= rt_msg2(RTM_IFINFO2
, &info
,
1836 (caddr_t
)cp
, NULL
, &cred
);
1837 info
.rti_info
[RTAX_IFP
] = NULL
;
1839 ifm
= (struct if_msghdr2
*)(void *)cp
;
1840 ifm
->ifm_addrs
= info
.rti_addrs
;
1841 ifm
->ifm_flags
= (u_short
)ifp
->if_flags
;
1842 ifm
->ifm_index
= ifp
->if_index
;
1843 ifm
->ifm_snd_len
= IFCQ_LEN(&ifp
->if_snd
);
1844 ifm
->ifm_snd_maxlen
= IFCQ_MAXLEN(&ifp
->if_snd
);
1845 ifm
->ifm_snd_drops
=
1846 ifp
->if_snd
.ifcq_dropcnt
.packets
;
1847 ifm
->ifm_timer
= ifp
->if_timer
;
1848 if_data_internal_to_if_data64(ifp
,
1849 &ifp
->if_data
, &ifm
->ifm_data
);
1851 * <rdar://problem/32940901>
1852 * Round bytes only for non-platform
1854 if (!csproc_get_platform_binary(w
->w_req
->p
)) {
1855 ALIGN_BYTES(ifm
->ifm_data
.ifi_ibytes
);
1856 ALIGN_BYTES(ifm
->ifm_data
.ifi_obytes
);
1860 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
1863 while ((ifa
= ifa
->ifa_link
.tqe_next
) != NULL
) {
1865 if (af
&& af
!= ifa
->ifa_addr
->sa_family
) {
1869 info
.rti_info
[RTAX_IFA
] = ifa
->ifa_addr
;
1870 info
.rti_info
[RTAX_NETMASK
] = ifa
->ifa_netmask
;
1871 info
.rti_info
[RTAX_BRD
] = ifa
->ifa_dstaddr
;
1872 len
= rt_msg2(RTM_NEWADDR
, &info
, NULL
, NULL
,
1877 struct ifa_msghdr
*ifam
;
1879 if (current_len
+ len
> total_len
) {
1884 len
= rt_msg2(RTM_NEWADDR
, &info
,
1885 (caddr_t
)cp
, NULL
, &cred
);
1887 ifam
= (struct ifa_msghdr
*)(void *)cp
;
1889 ifa
->ifa_ifp
->if_index
;
1890 ifam
->ifam_flags
= ifa
->ifa_flags
;
1891 ifam
->ifam_metric
= ifa
->ifa_metric
;
1892 ifam
->ifam_addrs
= info
.rti_addrs
;
1895 VERIFY(IS_P2ALIGNED(cp
,
1896 sizeof (u_int32_t
)));
1902 ifnet_lock_done(ifp
);
1906 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
);
1907 ifma
!= NULL
; ifma
= LIST_NEXT(ifma
, ifma_link
)) {
1908 struct ifaddr
*ifa0
;
1911 if (af
&& af
!= ifma
->ifma_addr
->sa_family
) {
1915 bzero((caddr_t
)&info
, sizeof (info
));
1916 info
.rti_info
[RTAX_IFA
] = ifma
->ifma_addr
;
1918 * Holding ifnet lock here prevents the link
1919 * address from changing contents, so no need
1920 * to hold the ifa0 lock. The link address is
1921 * always present; it's never freed.
1923 ifa0
= ifp
->if_lladdr
;
1924 info
.rti_info
[RTAX_IFP
] = ifa0
->ifa_addr
;
1925 if (ifma
->ifma_ll
!= NULL
)
1926 info
.rti_info
[RTAX_GATEWAY
] =
1927 ifma
->ifma_ll
->ifma_addr
;
1928 len
= rt_msg2(RTM_NEWMADDR2
, &info
, NULL
, NULL
,
1933 struct ifma_msghdr2
*ifmam
;
1935 if (current_len
+ len
> total_len
) {
1940 len
= rt_msg2(RTM_NEWMADDR2
, &info
,
1941 (caddr_t
)cp
, NULL
, &cred
);
1944 (struct ifma_msghdr2
*)(void *)cp
;
1945 ifmam
->ifmam_addrs
= info
.rti_addrs
;
1946 ifmam
->ifmam_flags
= 0;
1947 ifmam
->ifmam_index
=
1948 ifma
->ifma_ifp
->if_index
;
1949 ifmam
->ifmam_refcount
=
1953 VERIFY(IS_P2ALIGNED(cp
,
1954 sizeof (u_int32_t
)));
1959 ifnet_lock_done(ifp
);
1960 info
.rti_info
[RTAX_IFA
] = info
.rti_info
[RTAX_NETMASK
] =
1961 info
.rti_info
[RTAX_BRD
] = NULL
;
1966 if (error
== ENOBUFS
)
1967 printf("%s: current_len (%d) + len (%d) > "
1968 "total_len (%d)\n", __func__
, current_len
,
1974 /* Better to return zero length buffer than ENOBUFS */
1977 total_len
+= total_len
>> 3;
1978 total_buffer
= _MALLOC(total_len
, M_RTABLE
,
1980 if (total_buffer
== NULL
) {
1981 printf("%s: _MALLOC(%d) failed\n", __func__
,
1987 VERIFY(IS_P2ALIGNED(cp
, sizeof (u_int32_t
)));
1989 error
= SYSCTL_OUT(w
->w_req
, total_buffer
, current_len
);
1995 if (total_buffer
!= NULL
)
1996 _FREE(total_buffer
, M_RTABLE
);
1998 kauth_cred_unref(&cred
);
2004 sysctl_rtstat(struct sysctl_req
*req
)
2006 return (SYSCTL_OUT(req
, &rtstat
, sizeof (struct rtstat
)));
2010 sysctl_rttrash(struct sysctl_req
*req
)
2012 return (SYSCTL_OUT(req
, &rttrash
, sizeof (rttrash
)));
2016 sysctl_rtsock SYSCTL_HANDLER_ARGS
2018 #pragma unused(oidp)
2019 int *name
= (int *)arg1
;
2020 u_int namelen
= arg2
;
2021 struct radix_node_head
*rnh
;
2022 int i
, error
= EINVAL
;
2033 Bzero(&w
, sizeof (w
));
2043 lck_mtx_lock(rnh_lock
);
2044 for (i
= 1; i
<= AF_MAX
; i
++)
2045 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
2046 (error
= rnh
->rnh_walktree(rnh
,
2047 sysctl_dumpentry
, &w
)))
2049 lck_mtx_unlock(rnh_lock
);
2052 case NET_RT_DUMPX_FLAGS
:
2053 lck_mtx_lock(rnh_lock
);
2054 for (i
= 1; i
<= AF_MAX
; i
++)
2055 if ((rnh
= rt_tables
[i
]) && (af
== 0 || af
== i
) &&
2056 (error
= rnh
->rnh_walktree(rnh
,
2057 sysctl_dumpentry_ext
, &w
)))
2059 lck_mtx_unlock(rnh_lock
);
2062 error
= sysctl_iflist(af
, &w
);
2064 case NET_RT_IFLIST2
:
2065 error
= sysctl_iflist2(af
, &w
);
2068 error
= sysctl_rtstat(req
);
2071 error
= sysctl_rttrash(req
);
2074 if (w
.w_tmem
!= NULL
)
2075 FREE(w
.w_tmem
, M_RTABLE
);
2080 * Definitions of protocols supported in the ROUTE domain.
2082 static struct protosw routesw
[] = {
2084 .pr_type
= SOCK_RAW
,
2086 .pr_flags
= PR_ATOMIC
|PR_ADDR
,
2087 .pr_output
= route_output
,
2088 .pr_ctlinput
= raw_ctlinput
,
2089 .pr_init
= raw_init
,
2090 .pr_usrreqs
= &route_usrreqs
,
2094 static int route_proto_count
= (sizeof (routesw
) / sizeof (struct protosw
));
2096 struct domain routedomain_s
= {
2097 .dom_family
= PF_ROUTE
,
2098 .dom_name
= "route",
2099 .dom_init
= route_dinit
,
2103 route_dinit(struct domain
*dp
)
2108 VERIFY(!(dp
->dom_flags
& DOM_INITIALIZED
));
2109 VERIFY(routedomain
== NULL
);
2113 for (i
= 0, pr
= &routesw
[0]; i
< route_proto_count
; i
++, pr
++)
2114 net_add_proto(pr
, dp
, 1);