]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/rtsock.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / net / rtsock.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1988, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)rtsock.c 8.5 (Berkeley) 11/2/94
61 */
62
1c79356b
A
63#include <sys/param.h>
64#include <sys/systm.h>
39236c6e 65#include <sys/kauth.h>
1c79356b
A
66#include <sys/kernel.h>
67#include <sys/sysctl.h>
68#include <sys/proc.h>
69#include <sys/malloc.h>
70#include <sys/mbuf.h>
71#include <sys/socket.h>
72#include <sys/socketvar.h>
73#include <sys/domain.h>
74#include <sys/protosw.h>
9bccf70c 75#include <sys/syslog.h>
6d2010ae 76#include <sys/mcache.h>
fe8ab488 77#include <kern/locks.h>
5ba3f43e 78#include <sys/codesign.h>
1c79356b
A
79
80#include <net/if.h>
81#include <net/route.h>
d1ecb069 82#include <net/dlil.h>
1c79356b 83#include <net/raw_cb.h>
9bccf70c 84#include <netinet/in.h>
d1ecb069
A
85#include <netinet/in_var.h>
86#include <netinet/in_arp.h>
d9a64523
A
87#include <netinet/ip.h>
88#include <netinet/ip6.h>
d1ecb069 89#include <netinet6/nd6.h>
1c79356b 90
91447636 91extern struct rtstat rtstat;
39236c6e
A
92extern struct domain routedomain_s;
93static struct domain *routedomain = NULL;
91447636 94
1c79356b
A
95MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
96
cb323159
A
97static struct sockaddr route_dst = { .sa_len = 2, .sa_family = PF_ROUTE, .sa_data = { 0, } };
98static struct sockaddr route_src = { .sa_len = 2, .sa_family = PF_ROUTE, .sa_data = { 0, } };
99static struct sockaddr sa_zero = { .sa_len = sizeof(sa_zero), .sa_family = AF_INET, .sa_data = { 0, } };
39236c6e
A
100
101struct route_cb {
0a7de745
A
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 */
39236c6e
A
105};
106
107static struct route_cb route_cb;
1c79356b 108
1c79356b 109struct walkarg {
0a7de745
A
110 int w_tmemsize;
111 int w_op, w_arg;
112 caddr_t w_tmem;
1c79356b
A
113 struct sysctl_req *w_req;
114};
115
39236c6e
A
116static void route_dinit(struct domain *);
117static int rts_abort(struct socket *);
118static int rts_attach(struct socket *, int, struct proc *);
119static int rts_bind(struct socket *, struct sockaddr *, struct proc *);
120static int rts_connect(struct socket *, struct sockaddr *, struct proc *);
121static int rts_detach(struct socket *);
122static int rts_disconnect(struct socket *);
123static int rts_peeraddr(struct socket *, struct sockaddr **);
124static int rts_send(struct socket *, int, struct mbuf *, struct sockaddr *,
125 struct mbuf *, struct proc *);
126static int rts_shutdown(struct socket *);
127static int rts_sockaddr(struct socket *, struct sockaddr **);
128
129static int route_output(struct mbuf *, struct socket *);
3e170ce0 130static int rt_setmetrics(u_int32_t, struct rt_metrics *, struct rtentry *);
39236c6e
A
131static void rt_getmetrics(struct rtentry *, struct rt_metrics *);
132static void rt_setif(struct rtentry *, struct sockaddr *, struct sockaddr *,
133 struct sockaddr *, unsigned int);
134static int rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
f427ee49
A
135static struct mbuf *rt_msg1(u_char, struct rt_addrinfo *);
136static int rt_msg2(u_char, struct rt_addrinfo *, caddr_t, struct walkarg *,
5ba3f43e 137 kauth_cred_t *);
39236c6e
A
138static int sysctl_dumpentry(struct radix_node *rn, void *vw);
139static int sysctl_dumpentry_ext(struct radix_node *rn, void *vw);
140static int sysctl_iflist(int af, struct walkarg *w);
141static int sysctl_iflist2(int af, struct walkarg *w);
142static int sysctl_rtstat(struct sysctl_req *);
143static int sysctl_rttrash(struct sysctl_req *);
144static int sysctl_rtsock SYSCTL_HANDLER_ARGS;
145
146SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_LOCKED,
0a7de745 147 sysctl_rtsock, "");
39236c6e 148
0a7de745 149SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "routing");
39236c6e 150
5ba3f43e 151/* Align x to 1024 (only power of 2) assuming x is positive */
0a7de745 152#define ALIGN_BYTES(x) do { \
f427ee49 153 x = (uint32_t)P2ALIGN(x, 1024); \
5ba3f43e
A
154} while(0)
155
0a7de745
A
156#define ROUNDUP32(a) \
157 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (uint32_t) - 1))) : \
39236c6e
A
158 sizeof (uint32_t))
159
0a7de745 160#define ADVANCE32(x, n) \
39236c6e 161 (x += ROUNDUP32((n)->sa_len))
d1ecb069 162
cb323159
A
163#define RT_HAS_IFADDR(rt) \
164 ((rt)->rt_ifa != NULL && (rt)->rt_ifa->ifa_addr != NULL)
165
1c79356b
A
166/*
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
169 */
170static int
171rts_abort(struct socket *so)
172{
0a7de745 173 return raw_usrreqs.pru_abort(so);
1c79356b
A
174}
175
176/* pru_accept is EOPNOTSUPP */
177
178static int
39236c6e 179rts_attach(struct socket *so, int proto, struct proc *p)
1c79356b 180{
39236c6e 181#pragma unused(p)
1c79356b 182 struct rawcb *rp;
91447636 183 int error;
1c79356b 184
39236c6e
A
185 VERIFY(so->so_pcb == NULL);
186
0a7de745
A
187 MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK | M_ZERO);
188 if (rp == NULL) {
189 return ENOBUFS;
190 }
1c79356b 191
1c79356b 192 so->so_pcb = (caddr_t)rp;
39236c6e
A
193 /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
194 error = raw_attach(so, proto);
1c79356b
A
195 rp = sotorawcb(so);
196 if (error) {
1c79356b 197 FREE(rp, M_PCB);
2d21ac55 198 so->so_pcb = NULL;
91447636 199 so->so_flags |= SOF_PCBCLEARING;
0a7de745 200 return error;
1c79356b 201 }
37839358 202
39236c6e 203 switch (rp->rcb_proto.sp_protocol) {
1c79356b 204 case AF_INET:
39236c6e 205 atomic_add_32(&route_cb.ip_count, 1);
1c79356b
A
206 break;
207 case AF_INET6:
39236c6e 208 atomic_add_32(&route_cb.ip6_count, 1);
1c79356b 209 break;
1c79356b
A
210 }
211 rp->rcb_faddr = &route_src;
39236c6e
A
212 atomic_add_32(&route_cb.any_count, 1);
213 /* the socket is already locked when we enter rts_attach */
1c79356b
A
214 soisconnected(so);
215 so->so_options |= SO_USELOOPBACK;
0a7de745 216 return 0;
1c79356b
A
217}
218
219static int
220rts_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
221{
0a7de745 222 return raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
1c79356b
A
223}
224
225static int
226rts_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
227{
0a7de745 228 return raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
1c79356b
A
229}
230
231/* pru_connect2 is EOPNOTSUPP */
232/* pru_control is EOPNOTSUPP */
233
234static int
235rts_detach(struct socket *so)
236{
237 struct rawcb *rp = sotorawcb(so);
1c79356b 238
39236c6e
A
239 VERIFY(rp != NULL);
240
241 switch (rp->rcb_proto.sp_protocol) {
242 case AF_INET:
243 atomic_add_32(&route_cb.ip_count, -1);
244 break;
245 case AF_INET6:
246 atomic_add_32(&route_cb.ip6_count, -1);
247 break;
1c79356b 248 }
39236c6e 249 atomic_add_32(&route_cb.any_count, -1);
0a7de745 250 return raw_usrreqs.pru_detach(so);
1c79356b
A
251}
252
253static int
254rts_disconnect(struct socket *so)
255{
0a7de745 256 return raw_usrreqs.pru_disconnect(so);
1c79356b
A
257}
258
259/* pru_listen is EOPNOTSUPP */
260
261static int
262rts_peeraddr(struct socket *so, struct sockaddr **nam)
263{
0a7de745 264 return raw_usrreqs.pru_peeraddr(so, nam);
1c79356b
A
265}
266
267/* pru_rcvd is EOPNOTSUPP */
268/* pru_rcvoob is EOPNOTSUPP */
269
270static int
271rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
39236c6e 272 struct mbuf *control, struct proc *p)
1c79356b 273{
0a7de745 274 return raw_usrreqs.pru_send(so, flags, m, nam, control, p);
1c79356b
A
275}
276
277/* pru_sense is null */
278
279static int
280rts_shutdown(struct socket *so)
281{
0a7de745 282 return raw_usrreqs.pru_shutdown(so);
1c79356b
A
283}
284
285static int
286rts_sockaddr(struct socket *so, struct sockaddr **nam)
287{
0a7de745 288 return raw_usrreqs.pru_sockaddr(so, nam);
1c79356b
A
289}
290
291static struct pr_usrreqs route_usrreqs = {
0a7de745
A
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,
1c79356b
A
304};
305
306/*ARGSUSED*/
307static int
2d21ac55 308route_output(struct mbuf *m, struct socket *so)
1c79356b 309{
2d21ac55
A
310 struct rt_msghdr *rtm = NULL;
311 struct rtentry *rt = NULL;
312 struct rtentry *saved_nrt = NULL;
1c79356b
A
313 struct radix_node_head *rnh;
314 struct rt_addrinfo info;
315 int len, error = 0;
6d2010ae 316 sa_family_t dst_sa_family = 0;
2d21ac55 317 struct ifnet *ifp = NULL;
c910b4d9 318 struct sockaddr_in dst_in, gate_in;
55e303ae 319 int sendonlytoself = 0;
c910b4d9 320 unsigned int ifscope = IFSCOPE_NONE;
39236c6e 321 struct rawcb *rp = NULL;
5ba3f43e 322 boolean_t is_router = FALSE;
0a7de745
A
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)) {
326 return ENOBUFS;
327 }
39236c6e 328 VERIFY(m->m_flags & M_PKTHDR);
91447636 329
39236c6e
A
330 /*
331 * Unlock the socket (but keep a reference) it won't be
332 * accessed until raw_input appends to it.
333 */
91447636 334 socket_unlock(so, 0);
b0d623f7 335 lck_mtx_lock(rnh_lock);
91447636 336
1c79356b 337 len = m->m_pkthdr.len;
0a7de745 338 if (len < sizeof(*rtm) ||
1c79356b 339 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
6d2010ae 340 info.rti_info[RTAX_DST] = NULL;
1c79356b
A
341 senderr(EINVAL);
342 }
343 R_Malloc(rtm, struct rt_msghdr *, len);
c910b4d9 344 if (rtm == NULL) {
6d2010ae 345 info.rti_info[RTAX_DST] = NULL;
1c79356b
A
346 senderr(ENOBUFS);
347 }
348 m_copydata(m, 0, len, (caddr_t)rtm);
349 if (rtm->rtm_version != RTM_VERSION) {
6d2010ae 350 info.rti_info[RTAX_DST] = NULL;
1c79356b
A
351 senderr(EPROTONOSUPPORT);
352 }
c910b4d9 353
55e303ae
A
354 /*
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.
357 */
358 if (rtm->rtm_type == RTM_GET_SILENT) {
0a7de745 359 if (!(so->so_options & SO_USELOOPBACK)) {
55e303ae 360 senderr(EINVAL);
0a7de745 361 }
55e303ae
A
362 sendonlytoself = 1;
363 rtm->rtm_type = RTM_GET;
364 }
c910b4d9 365
55e303ae
A
366 /*
367 * Perform permission checking, only privileged sockets
368 * may perform operations other than RTM_GET
369 */
39236c6e 370 if (rtm->rtm_type != RTM_GET && !(so->so_state & SS_PRIV)) {
6d2010ae 371 info.rti_info[RTAX_DST] = NULL;
55e303ae
A
372 senderr(EPERM);
373 }
91447636
A
374
375 rtm->rtm_pid = proc_selfpid();
1c79356b
A
376 info.rti_addrs = rtm->rtm_addrs;
377 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
6d2010ae 378 info.rti_info[RTAX_DST] = NULL;
1c79356b
A
379 senderr(EINVAL);
380 }
39236c6e
A
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 &&
0a7de745 384 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
1c79356b 385 senderr(EINVAL);
0a7de745 386 }
c910b4d9 387
39236c6e 388 if (info.rti_info[RTAX_DST]->sa_family == AF_INET &&
cb323159 389 info.rti_info[RTAX_DST]->sa_len != sizeof(struct sockaddr_in)) {
c910b4d9 390 /* At minimum, we need up to sin_addr */
39236c6e 391 if (info.rti_info[RTAX_DST]->sa_len <
0a7de745 392 offsetof(struct sockaddr_in, sin_zero)) {
c910b4d9 393 senderr(EINVAL);
0a7de745
A
394 }
395 bzero(&dst_in, sizeof(dst_in));
396 dst_in.sin_len = sizeof(dst_in);
c910b4d9 397 dst_in.sin_family = AF_INET;
6d2010ae
A
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;
cb323159
A
402 } else if (info.rti_info[RTAX_DST]->sa_family == AF_INET6 &&
403 info.rti_info[RTAX_DST]->sa_len < sizeof(struct sockaddr_in6)) {
404 senderr(EINVAL);
c910b4d9
A
405 }
406
cb323159
A
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)) {
413 senderr(EINVAL);
414 }
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)) {
c910b4d9 423 senderr(EINVAL);
0a7de745 424 }
c910b4d9
A
425 }
426
6d2010ae 427 if (info.rti_info[RTAX_GENMASK]) {
1c79356b 428 struct radix_node *t;
6d2010ae 429 t = rn_addmask((caddr_t)info.rti_info[RTAX_GENMASK], 0, 1);
39236c6e 430 if (t != NULL && Bcmp(info.rti_info[RTAX_GENMASK],
0a7de745 431 t->rn_key, *(u_char *)info.rti_info[RTAX_GENMASK]) == 0) {
39236c6e
A
432 info.rti_info[RTAX_GENMASK] =
433 (struct sockaddr *)(t->rn_key);
0a7de745 434 } else {
1c79356b 435 senderr(ENOBUFS);
0a7de745 436 }
1c79356b 437 }
c910b4d9
A
438
439 /*
440 * If RTF_IFSCOPE flag is set, then rtm_index specifies the scope.
441 */
442 if (rtm->rtm_flags & RTF_IFSCOPE) {
39236c6e 443 if (info.rti_info[RTAX_DST]->sa_family != AF_INET &&
0a7de745 444 info.rti_info[RTAX_DST]->sa_family != AF_INET6) {
c910b4d9 445 senderr(EINVAL);
0a7de745 446 }
c910b4d9
A
447 ifscope = rtm->rtm_index;
448 }
00867663
A
449 /*
450 * Block changes on INTCOPROC interfaces.
451 */
452 if (ifscope) {
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;
458 break;
459 }
460 }
461 ifnet_head_done();
0a7de745 462 if (intcoproc_scope == ifscope && current_proc()->p_pid != 0) {
00867663 463 senderr(EINVAL);
0a7de745 464 }
00867663 465 }
c910b4d9 466
316670eb
A
467 /*
468 * RTF_PROXY can only be set internally from within the kernel.
469 */
0a7de745 470 if (rtm->rtm_flags & RTF_PROXY) {
316670eb 471 senderr(EINVAL);
0a7de745 472 }
316670eb 473
6d2010ae
A
474 /*
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.
480 */
0a7de745 481 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
6d2010ae 482 sin_set_ifscope(info.rti_info[RTAX_DST], IFSCOPE_NONE);
0a7de745 483 }
39236c6e 484 if (info.rti_info[RTAX_GATEWAY] != NULL &&
0a7de745 485 info.rti_info[RTAX_GATEWAY]->sa_family == AF_INET) {
6d2010ae 486 sin_set_ifscope(info.rti_info[RTAX_GATEWAY], IFSCOPE_NONE);
0a7de745 487 }
1c79356b 488 switch (rtm->rtm_type) {
39236c6e 489 case RTM_ADD:
0a7de745 490 if (info.rti_info[RTAX_GATEWAY] == NULL) {
39236c6e 491 senderr(EINVAL);
0a7de745 492 }
c910b4d9 493
39236c6e
A
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,
497 ifscope);
498 if (error == 0 && saved_nrt != NULL) {
499 RT_LOCK(saved_nrt);
500 /*
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.
525 * dwiggins@bbn.com
526 */
527 rt_setif(saved_nrt,
528 info.rti_info[RTAX_IFP], info.rti_info[RTAX_IFA],
529 info.rti_info[RTAX_GATEWAY], ifscope);
3e170ce0 530 (void)rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, saved_nrt);
39236c6e
A
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);
537 }
538 break;
539
540 case RTM_DELETE:
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,
544 ifscope);
545 if (error == 0) {
546 rt = saved_nrt;
547 RT_LOCK(rt);
548 goto report;
549 }
550 break;
551
552 case RTM_GET:
553 case RTM_CHANGE:
554 case RTM_LOCK:
555 rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
0a7de745 556 if (rnh == NULL) {
39236c6e 557 senderr(EAFNOSUPPORT);
0a7de745 558 }
39236c6e
A
559 /*
560 * Lookup the best match based on the key-mask pair;
561 * callee adds a reference and checks for root node.
562 */
563 rt = rt_lookup(TRUE, info.rti_info[RTAX_DST],
564 info.rti_info[RTAX_NETMASK], rnh, ifscope);
0a7de745 565 if (rt == NULL) {
39236c6e 566 senderr(ESRCH);
0a7de745 567 }
39236c6e 568 RT_LOCK(rt);
91447636 569
39236c6e
A
570 /*
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.
574 */
575 switch (rtm->rtm_type) {
576 case RTM_GET: {
813fb2f6 577 kauth_cred_t cred;
a39ff7e2 578 kauth_cred_t* credp;
39236c6e
A
579 struct ifaddr *ifa2;
580report:
813fb2f6 581 cred = kauth_cred_proc_ref(current_proc());
d9a64523 582 credp = &cred;
a39ff7e2 583
39236c6e
A
584 ifa2 = NULL;
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)) {
592 ifp = rt->rt_ifp;
593 if (ifp != NULL) {
594 ifnet_lock_shared(ifp);
595 ifa2 = ifp->if_lladdr;
596 info.rti_info[RTAX_IFP] =
597 ifa2->ifa_addr;
598 IFA_ADDREF(ifa2);
599 ifnet_lock_done(ifp);
600 info.rti_info[RTAX_IFA] =
601 rt->rt_ifa->ifa_addr;
602 rtm->rtm_index = ifp->if_index;
603 } else {
604 info.rti_info[RTAX_IFP] = NULL;
605 info.rti_info[RTAX_IFA] = NULL;
606 }
607 } else if ((ifp = rt->rt_ifp) != NULL) {
608 rtm->rtm_index = ifp->if_index;
1c79356b 609 }
0a7de745 610 if (ifa2 != NULL) {
39236c6e 611 IFA_LOCK(ifa2);
0a7de745 612 }
a39ff7e2 613 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL, credp);
0a7de745 614 if (ifa2 != NULL) {
39236c6e 615 IFA_UNLOCK(ifa2);
0a7de745 616 }
527f9951
A
617 struct rt_msghdr *out_rtm;
618 R_Malloc(out_rtm, struct rt_msghdr *, len);
619 if (out_rtm == NULL) {
620 RT_UNLOCK(rt);
0a7de745 621 if (ifa2 != NULL) {
527f9951 622 IFA_REMREF(ifa2);
0a7de745 623 }
527f9951 624 senderr(ENOBUFS);
39236c6e 625 }
527f9951 626 Bcopy(rtm, out_rtm, sizeof(struct rt_msghdr));
0a7de745 627 if (ifa2 != NULL) {
39236c6e 628 IFA_LOCK(ifa2);
0a7de745 629 }
527f9951 630 (void) rt_msg2(out_rtm->rtm_type, &info, (caddr_t)out_rtm,
5ba3f43e 631 NULL, &cred);
0a7de745 632 if (ifa2 != NULL) {
39236c6e 633 IFA_UNLOCK(ifa2);
0a7de745 634 }
527f9951
A
635 R_Free(rtm);
636 rtm = out_rtm;
39236c6e
A
637 rtm->rtm_flags = rt->rt_flags;
638 rt_getmetrics(rt, &rtm->rtm_rmx);
639 rtm->rtm_addrs = info.rti_addrs;
0a7de745 640 if (ifa2 != NULL) {
39236c6e 641 IFA_REMREF(ifa2);
0a7de745 642 }
5ba3f43e
A
643
644 kauth_cred_unref(&cred);
1c79356b 645 break;
39236c6e 646 }
1c79356b
A
647
648 case RTM_CHANGE:
5ba3f43e
A
649 is_router = (rt->rt_flags & RTF_ROUTER) ? TRUE : FALSE;
650
39236c6e
A
651 if (info.rti_info[RTAX_GATEWAY] != NULL &&
652 (error = rt_setgate(rt, rt_key(rt),
653 info.rti_info[RTAX_GATEWAY]))) {
654 int tmp = error;
655 RT_UNLOCK(rt);
656 senderr(tmp);
657 }
c910b4d9 658 /*
39236c6e
A
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
5ba3f43e 663 * default gateway. Changing flags still doesn't work.
c910b4d9 664 */
39236c6e 665 if ((rt->rt_flags & RTF_GATEWAY) &&
0a7de745 666 info.rti_info[RTAX_GATEWAY] == NULL) {
39236c6e 667 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
0a7de745 668 }
c910b4d9 669
6d2010ae 670 /*
39236c6e
A
671 * On Darwin, we call rt_setif which contains the
672 * equivalent to the code found at this very spot
673 * in BSD.
6d2010ae 674 */
39236c6e
A
675 rt_setif(rt,
676 info.rti_info[RTAX_IFP], info.rti_info[RTAX_IFA],
677 info.rti_info[RTAX_GATEWAY], ifscope);
678
3e170ce0
A
679 if ((error = rt_setmetrics(rtm->rtm_inits,
680 &rtm->rtm_rmx, rt))) {
0a7de745
A
681 int tmp = error;
682 RT_UNLOCK(rt);
683 senderr(tmp);
3e170ce0 684 }
0a7de745 685 if (info.rti_info[RTAX_GENMASK]) {
39236c6e 686 rt->rt_genmask = info.rti_info[RTAX_GENMASK];
0a7de745 687 }
5ba3f43e
A
688
689 /*
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.
693 */
694 route_event_enqueue_nwk_wq_entry(rt, NULL, ROUTE_ENTRY_REFRESH, NULL, TRUE);
695 /*
696 * If the route is for a router, walk the tree to send refresh
697 * event to protocol cloned entries
698 */
699 if (is_router) {
700 struct route_event rt_ev;
701 route_event_init(&rt_ev, rt, NULL, ROUTE_ENTRY_REFRESH);
702 RT_UNLOCK(rt);
703 (void) rnh->rnh_walktree(rnh, route_event_walktree, (void *)&rt_ev);
704 RT_LOCK(rt);
705 }
f427ee49 706 OS_FALLTHROUGH;
39236c6e
A
707 case RTM_LOCK:
708 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
709 rt->rt_rmx.rmx_locks |=
710 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
1c79356b 711 break;
39236c6e
A
712 }
713 RT_UNLOCK(rt);
714 break;
39236c6e
A
715 default:
716 senderr(EOPNOTSUPP);
1c79356b 717 }
1c79356b 718flush:
39236c6e 719 if (rtm != NULL) {
0a7de745 720 if (error) {
1c79356b 721 rtm->rtm_errno = error;
0a7de745 722 } else {
1c79356b 723 rtm->rtm_flags |= RTF_DONE;
0a7de745 724 }
1c79356b 725 }
b0d623f7
A
726 if (rt != NULL) {
727 RT_LOCK_ASSERT_NOTHELD(rt);
91447636 728 rtfree_locked(rt);
b0d623f7
A
729 }
730 lck_mtx_unlock(rnh_lock);
39236c6e
A
731
732 /* relock the socket now */
733 socket_lock(so, 0);
1c79356b
A
734 /*
735 * Check to see if we don't want our own messages.
736 */
39236c6e 737 if (!(so->so_options & SO_USELOOPBACK)) {
1c79356b 738 if (route_cb.any_count <= 1) {
0a7de745 739 if (rtm != NULL) {
91447636 740 R_Free(rtm);
0a7de745 741 }
1c79356b 742 m_freem(m);
0a7de745 743 return error;
1c79356b
A
744 }
745 /* There is another listener, so construct message */
746 rp = sotorawcb(so);
747 }
39236c6e 748 if (rtm != NULL) {
1c79356b 749 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
9bccf70c
A
750 if (m->m_pkthdr.len < rtm->rtm_msglen) {
751 m_freem(m);
752 m = NULL;
39236c6e 753 } else if (m->m_pkthdr.len > rtm->rtm_msglen) {
9bccf70c 754 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
39236c6e 755 }
91447636 756 R_Free(rtm);
1c79356b 757 }
39236c6e 758 if (sendonlytoself && m != NULL) {
91447636 759 error = 0;
39236c6e
A
760 if (sbappendaddr(&so->so_rcv, &route_src, m,
761 NULL, &error) != 0) {
55e303ae
A
762 sorwakeup(so);
763 }
0a7de745
A
764 if (error) {
765 return error;
766 }
55e303ae 767 } else {
cb323159 768 struct sockproto route_proto = { .sp_family = PF_ROUTE, .sp_protocol = 0 };
0a7de745 769 if (rp != NULL) {
55e303ae 770 rp->rcb_proto.sp_family = 0; /* Avoid us */
0a7de745
A
771 }
772 if (dst_sa_family != 0) {
6d2010ae 773 route_proto.sp_protocol = dst_sa_family;
0a7de745 774 }
39236c6e 775 if (m != NULL) {
91447636 776 socket_unlock(so, 0);
55e303ae 777 raw_input(m, &route_proto, &route_src, &route_dst);
91447636
A
778 socket_lock(so, 0);
779 }
0a7de745 780 if (rp != NULL) {
55e303ae 781 rp->rcb_proto.sp_family = PF_ROUTE;
0a7de745 782 }
55e303ae 783 }
0a7de745 784 return error;
1c79356b
A
785}
786
6d2010ae
A
787void
788rt_setexpire(struct rtentry *rt, uint64_t expiry)
789{
790 /* set both rt_expire and rmx_expire */
791 rt->rt_expire = expiry;
792 if (expiry) {
f427ee49
A
793 rt->rt_rmx.rmx_expire =
794 (int32_t)(expiry + rt->base_calendartime -
795 rt->base_uptime);
39236c6e 796 } else {
6d2010ae 797 rt->rt_rmx.rmx_expire = 0;
39236c6e 798 }
6d2010ae
A
799}
800
3e170ce0 801static int
6d2010ae 802rt_setmetrics(u_int32_t which, struct rt_metrics *in, struct rtentry *out)
1c79356b 803{
3e170ce0
A
804 if (!(which & RTV_REFRESH_HOST)) {
805 struct timeval caltime;
806 getmicrotime(&caltime);
0a7de745 807#define metric(f, e) if (which & (f)) out->rt_rmx.e = in->e;
3e170ce0
A
808 metric(RTV_RPIPE, rmx_recvpipe);
809 metric(RTV_SPIPE, rmx_sendpipe);
810 metric(RTV_SSTHRESH, rmx_ssthresh);
811 metric(RTV_RTT, rmx_rtt);
812 metric(RTV_RTTVAR, rmx_rttvar);
813 metric(RTV_HOPCOUNT, rmx_hopcount);
814 metric(RTV_MTU, rmx_mtu);
815 metric(RTV_EXPIRE, rmx_expire);
1c79356b 816#undef metric
3e170ce0
A
817 if (out->rt_rmx.rmx_expire > 0) {
818 /* account for system time change */
819 getmicrotime(&caltime);
820 out->base_calendartime +=
0a7de745
A
821 NET_CALCULATE_CLOCKSKEW(caltime,
822 out->base_calendartime,
823 net_uptime(), out->base_uptime);
3e170ce0 824 rt_setexpire(out,
0a7de745
A
825 out->rt_rmx.rmx_expire -
826 out->base_calendartime +
827 out->base_uptime);
3e170ce0
A
828 } else {
829 rt_setexpire(out, 0);
830 }
39236c6e 831
3e170ce0
A
832 VERIFY(out->rt_expire == 0 || out->rt_rmx.rmx_expire != 0);
833 VERIFY(out->rt_expire != 0 || out->rt_rmx.rmx_expire == 0);
6d2010ae 834 } else {
3e170ce0
A
835 /* Only RTV_REFRESH_HOST must be set */
836 if ((which & ~RTV_REFRESH_HOST) ||
837 (out->rt_flags & RTF_STATIC) ||
838 !(out->rt_flags & RTF_LLINFO)) {
0a7de745 839 return EINVAL;
3e170ce0 840 }
39236c6e 841
3e170ce0 842 if (out->rt_llinfo_refresh == NULL) {
0a7de745 843 return ENOTSUP;
3e170ce0
A
844 }
845
846 out->rt_llinfo_refresh(out);
847 }
0a7de745 848 return 0;
6d2010ae
A
849}
850
851static void
852rt_getmetrics(struct rtentry *in, struct rt_metrics *out)
853{
39236c6e 854 struct timeval caltime;
6d2010ae
A
855
856 VERIFY(in->rt_expire == 0 || in->rt_rmx.rmx_expire != 0);
857 VERIFY(in->rt_expire != 0 || in->rt_rmx.rmx_expire == 0);
39236c6e
A
858
859 *out = in->rt_rmx;
860
861 if (in->rt_expire != 0) {
6d2010ae 862 /* account for system time change */
39236c6e 863 getmicrotime(&caltime);
6d2010ae
A
864
865 in->base_calendartime +=
39236c6e
A
866 NET_CALCULATE_CLOCKSKEW(caltime,
867 in->base_calendartime, net_uptime(), in->base_uptime);
868
f427ee49
A
869 out->rmx_expire = (int32_t)(in->base_calendartime +
870 in->rt_expire - in->base_uptime);
39236c6e 871 } else {
6d2010ae 872 out->rmx_expire = 0;
39236c6e 873 }
1c79356b
A
874}
875
876/*
39236c6e
A
877 * Set route's interface given info.rti_info[RTAX_IFP],
878 * info.rti_info[RTAX_IFA], and gateway.
1c79356b
A
879 */
880static void
c910b4d9
A
881rt_setif(struct rtentry *rt, struct sockaddr *Ifpaddr, struct sockaddr *Ifaaddr,
882 struct sockaddr *Gate, unsigned int ifscope)
1c79356b 883{
6d2010ae
A
884 struct ifaddr *ifa = NULL;
885 struct ifnet *ifp = NULL;
39236c6e 886 void (*ifa_rtrequest)(int, struct rtentry *, struct sockaddr *);
1c79356b 887
5ba3f43e 888 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
889
890 RT_LOCK_ASSERT_HELD(rt);
91447636 891
b0d623f7 892 /* Don't update a defunct route */
0a7de745 893 if (rt->rt_flags & RTF_CONDEMNED) {
b0d623f7 894 return;
0a7de745 895 }
b0d623f7
A
896
897 /* Add an extra ref for ourselves */
898 RT_ADDREF_LOCKED(rt);
2d21ac55 899
6d2010ae
A
900 /* Become a regular mutex, just in case */
901 RT_CONVERT_LOCK(rt);
902
c910b4d9
A
903 /*
904 * New gateway could require new ifaddr, ifp; flags may also
905 * be different; ifp may be specified by ll sockaddr when
906 * protocol address is ambiguous.
907 */
908 if (Ifpaddr && (ifa = ifa_ifwithnet_scoped(Ifpaddr, ifscope)) &&
91447636 909 (ifp = ifa->ifa_ifp) && (Ifaaddr || Gate)) {
6d2010ae 910 IFA_REMREF(ifa);
c910b4d9
A
911 ifa = ifaof_ifpforaddr(Ifaaddr ? Ifaaddr : Gate, ifp);
912 } else {
39236c6e 913 if (ifa != NULL) {
6d2010ae 914 IFA_REMREF(ifa);
39236c6e 915 ifa = NULL;
91447636 916 }
39236c6e 917 if (Ifpaddr && (ifp = if_withname(Ifpaddr))) {
91447636
A
918 if (Gate) {
919 ifa = ifaof_ifpforaddr(Gate, ifp);
c910b4d9 920 } else {
91447636
A
921 ifnet_lock_shared(ifp);
922 ifa = TAILQ_FIRST(&ifp->if_addrhead);
0a7de745 923 if (ifa != NULL) {
6d2010ae 924 IFA_ADDREF(ifa);
0a7de745 925 }
91447636
A
926 ifnet_lock_done(ifp);
927 }
c910b4d9
A
928 } else if (Ifaaddr &&
929 (ifa = ifa_ifwithaddr_scoped(Ifaaddr, ifscope))) {
91447636 930 ifp = ifa->ifa_ifp;
b0d623f7
A
931 } else if (Gate != NULL) {
932 /*
933 * Safe to drop rt_lock and use rt_key, since holding
934 * rnh_lock here prevents another thread from calling
935 * rt_setgate() on this route. We cannot hold the
936 * lock across ifa_ifwithroute since the lookup done
937 * by that routine may point to the same route.
938 */
939 RT_UNLOCK(rt);
940 if ((ifa = ifa_ifwithroute_scoped_locked(rt->rt_flags,
0a7de745 941 rt_key(rt), Gate, ifscope)) != NULL) {
b0d623f7 942 ifp = ifa->ifa_ifp;
0a7de745 943 }
b0d623f7
A
944 RT_LOCK(rt);
945 /* Don't update a defunct route */
946 if (rt->rt_flags & RTF_CONDEMNED) {
0a7de745 947 if (ifa != NULL) {
6d2010ae 948 IFA_REMREF(ifa);
0a7de745 949 }
b0d623f7
A
950 /* Release extra ref */
951 RT_REMREF_LOCKED(rt);
952 return;
953 }
91447636
A
954 }
955 }
39236c6e
A
956
957 /* trigger route cache reevaluation */
0a7de745 958 if (rt_key(rt)->sa_family == AF_INET) {
39236c6e 959 routegenid_inet_update();
f427ee49 960 } else if (rt_key(rt)->sa_family == AF_INET6) {
39236c6e 961 routegenid_inet6_update();
0a7de745 962 }
39236c6e
A
963
964 if (ifa != NULL) {
91447636 965 struct ifaddr *oifa = rt->rt_ifa;
1c79356b 966 if (oifa != ifa) {
6d2010ae
A
967 if (oifa != NULL) {
968 IFA_LOCK_SPIN(oifa);
969 ifa_rtrequest = oifa->ifa_rtrequest;
970 IFA_UNLOCK(oifa);
0a7de745 971 if (ifa_rtrequest != NULL) {
6d2010ae 972 ifa_rtrequest(RTM_DELETE, rt, Gate);
0a7de745 973 }
6d2010ae 974 }
9bccf70c 975 rtsetifa(rt, ifa);
6d2010ae
A
976
977 if (rt->rt_ifp != ifp) {
978 /*
979 * Purge any link-layer info caching.
980 */
0a7de745 981 if (rt->rt_llinfo_purge != NULL) {
6d2010ae 982 rt->rt_llinfo_purge(rt);
0a7de745 983 }
6d2010ae
A
984
985 /*
986 * Adjust route ref count for the interfaces.
987 */
988 if (rt->rt_if_ref_fn != NULL) {
989 rt->rt_if_ref_fn(ifp, 1);
990 rt->rt_if_ref_fn(rt->rt_ifp, -1);
991 }
d1ecb069 992 }
c910b4d9
A
993 rt->rt_ifp = ifp;
994 /*
995 * If this is the (non-scoped) default route, record
996 * the interface index used for the primary ifscope.
997 */
6d2010ae
A
998 if (rt_primary_default(rt, rt_key(rt))) {
999 set_primary_ifscope(rt_key(rt)->sa_family,
1000 rt->rt_ifp->if_index);
1001 }
39236c6e
A
1002 /*
1003 * If rmx_mtu is not locked, update it
1004 * to the MTU used by the new interface.
1005 */
d9a64523 1006 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) {
39236c6e 1007 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
d9a64523
A
1008 if (rt_key(rt)->sa_family == AF_INET &&
1009 INTF_ADJUST_MTU_FOR_CLAT46(ifp)) {
1010 rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
1011 /* Further adjust the size for CLAT46 expansion */
1012 rt->rt_rmx.rmx_mtu -= CLAT46_HDR_EXPANSION_OVERHD;
1013 }
1014 }
39236c6e 1015
6d2010ae
A
1016 if (rt->rt_ifa != NULL) {
1017 IFA_LOCK_SPIN(rt->rt_ifa);
1018 ifa_rtrequest = rt->rt_ifa->ifa_rtrequest;
1019 IFA_UNLOCK(rt->rt_ifa);
0a7de745 1020 if (ifa_rtrequest != NULL) {
6d2010ae 1021 ifa_rtrequest(RTM_ADD, rt, Gate);
0a7de745 1022 }
6d2010ae
A
1023 }
1024 IFA_REMREF(ifa);
b0d623f7
A
1025 /* Release extra ref */
1026 RT_REMREF_LOCKED(rt);
1027 return;
91447636 1028 }
6d2010ae 1029 IFA_REMREF(ifa);
39236c6e 1030 ifa = NULL;
1c79356b 1031 }
b0d623f7 1032
1c79356b 1033 /* XXX: to reset gateway to correct value, at RTM_CHANGE */
6d2010ae
A
1034 if (rt->rt_ifa != NULL) {
1035 IFA_LOCK_SPIN(rt->rt_ifa);
1036 ifa_rtrequest = rt->rt_ifa->ifa_rtrequest;
1037 IFA_UNLOCK(rt->rt_ifa);
0a7de745 1038 if (ifa_rtrequest != NULL) {
6d2010ae 1039 ifa_rtrequest(RTM_ADD, rt, Gate);
0a7de745 1040 }
6d2010ae 1041 }
1c79356b 1042
39236c6e
A
1043 /*
1044 * Workaround for local address routes pointing to the loopback
1045 * interface added by configd, until <rdar://problem/12970142>.
1046 */
1047 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) &&
1048 (rt->rt_flags & RTF_HOST) && rt->rt_ifa->ifa_ifp == rt->rt_ifp) {
1049 ifa = ifa_ifwithaddr(rt_key(rt));
1050 if (ifa != NULL) {
0a7de745 1051 if (ifa != rt->rt_ifa) {
39236c6e 1052 rtsetifa(rt, ifa);
0a7de745 1053 }
39236c6e
A
1054 IFA_REMREF(ifa);
1055 }
1056 }
1057
b0d623f7
A
1058 /* Release extra ref */
1059 RT_REMREF_LOCKED(rt);
1060}
1c79356b 1061
1c79356b
A
1062/*
1063 * Extract the addresses of the passed sockaddrs.
1064 * Do a little sanity checking so as to avoid bad memory references.
1065 * This data is derived straight from userland.
1066 */
1067static int
2d21ac55 1068rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1c79356b 1069{
91447636
A
1070 struct sockaddr *sa;
1071 int i;
1c79356b 1072
0a7de745 1073 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
1c79356b 1074 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
0a7de745 1075 if ((rtinfo->rti_addrs & (1 << i)) == 0) {
1c79356b 1076 continue;
0a7de745 1077 }
1c79356b
A
1078 sa = (struct sockaddr *)cp;
1079 /*
1080 * It won't fit.
1081 */
0a7de745
A
1082 if ((cp + sa->sa_len) > cplim) {
1083 return EINVAL;
1084 }
4ba76501
A
1085 if (sa->sa_len > sizeof(struct sockaddr_storage)) {
1086 return EINVAL;
1087 }
1c79356b
A
1088 /*
1089 * there are no more.. quit now
1090 * If there are more bits, they are in error.
39236c6e 1091 * I've seen this. route(1) can evidently generate these.
1c79356b
A
1092 * This causes kernel to core dump.
1093 * for compatibility, If we see this, point to a safe address.
1094 */
1095 if (sa->sa_len == 0) {
1096 rtinfo->rti_info[i] = &sa_zero;
0a7de745 1097 return 0; /* should be EINVAL but for compat */
1c79356b 1098 }
a991bd8d
A
1099 if (sa->sa_len < offsetof(struct sockaddr, sa_data)) {
1100 return EINVAL;
1101 }
1c79356b
A
1102 /* accept it */
1103 rtinfo->rti_info[i] = sa;
b0d623f7 1104 ADVANCE32(cp, sa);
1c79356b 1105 }
0a7de745 1106 return 0;
1c79356b
A
1107}
1108
1109static struct mbuf *
f427ee49 1110rt_msg1(u_char type, struct rt_addrinfo *rtinfo)
1c79356b 1111{
91447636
A
1112 struct rt_msghdr *rtm;
1113 struct mbuf *m;
1114 int i;
3e170ce0 1115 int len, dlen, off;
1c79356b 1116
1c79356b 1117 switch (type) {
1c79356b
A
1118 case RTM_DELADDR:
1119 case RTM_NEWADDR:
0a7de745 1120 len = sizeof(struct ifa_msghdr);
1c79356b
A
1121 break;
1122
1123 case RTM_DELMADDR:
1124 case RTM_NEWMADDR:
0a7de745 1125 len = sizeof(struct ifma_msghdr);
1c79356b
A
1126 break;
1127
1128 case RTM_IFINFO:
0a7de745 1129 len = sizeof(struct if_msghdr);
1c79356b
A
1130 break;
1131
1132 default:
0a7de745 1133 len = sizeof(struct rt_msghdr);
1c79356b 1134 }
9bccf70c
A
1135 m = m_gethdr(M_DONTWAIT, MT_DATA);
1136 if (m && len > MHLEN) {
1137 MCLGET(m, M_DONTWAIT);
39236c6e 1138 if (!(m->m_flags & M_EXT)) {
9bccf70c
A
1139 m_free(m);
1140 m = NULL;
1141 }
1142 }
0a7de745
A
1143 if (m == NULL) {
1144 return NULL;
1145 }
1c79356b 1146 m->m_pkthdr.len = m->m_len = len;
39236c6e 1147 m->m_pkthdr.rcvif = NULL;
1c79356b
A
1148 rtm = mtod(m, struct rt_msghdr *);
1149 bzero((caddr_t)rtm, len);
3e170ce0 1150 off = len;
1c79356b 1151 for (i = 0; i < RTAX_MAX; i++) {
b0d623f7 1152 struct sockaddr *sa, *hint;
39236c6e
A
1153 uint8_t ssbuf[SOCK_MAXADDRLEN + 1];
1154
1155 /*
1156 * Make sure to accomodate the largest possible size of sa_len.
1157 */
0a7de745 1158 _CASSERT(sizeof(ssbuf) == (SOCK_MAXADDRLEN + 1));
b0d623f7 1159
0a7de745 1160 if ((sa = rtinfo->rti_info[i]) == NULL) {
1c79356b 1161 continue;
0a7de745 1162 }
b0d623f7
A
1163
1164 switch (i) {
1165 case RTAX_DST:
1166 case RTAX_NETMASK:
0a7de745 1167 if ((hint = rtinfo->rti_info[RTAX_DST]) == NULL) {
b0d623f7 1168 hint = rtinfo->rti_info[RTAX_IFA];
0a7de745 1169 }
b0d623f7
A
1170
1171 /* Scrub away any trace of embedded interface scope */
39236c6e 1172 sa = rtm_scrub(type, i, hint, sa, &ssbuf,
0a7de745 1173 sizeof(ssbuf), NULL);
b0d623f7
A
1174 break;
1175
1176 default:
1177 break;
1178 }
1179
1c79356b 1180 rtinfo->rti_addrs |= (1 << i);
3e170ce0
A
1181 dlen = sa->sa_len;
1182 m_copyback(m, off, dlen, (caddr_t)sa);
1183 len = off + dlen;
1184 off += ROUNDUP32(dlen);
1c79356b
A
1185 }
1186 if (m->m_pkthdr.len != len) {
1187 m_freem(m);
0a7de745 1188 return NULL;
1c79356b 1189 }
f427ee49 1190 rtm->rtm_msglen = (u_short)len;
1c79356b
A
1191 rtm->rtm_version = RTM_VERSION;
1192 rtm->rtm_type = type;
0a7de745 1193 return m;
1c79356b
A
1194}
1195
1196static int
f427ee49 1197rt_msg2(u_char type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w,
0a7de745 1198 kauth_cred_t* credp)
1c79356b 1199{
91447636 1200 int i;
3e170ce0 1201 int len, dlen, rlen, second_time = 0;
1c79356b
A
1202 caddr_t cp0;
1203
1204 rtinfo->rti_addrs = 0;
1205again:
1206 switch (type) {
1c79356b
A
1207 case RTM_DELADDR:
1208 case RTM_NEWADDR:
0a7de745 1209 len = sizeof(struct ifa_msghdr);
1c79356b
A
1210 break;
1211
91447636
A
1212 case RTM_DELMADDR:
1213 case RTM_NEWMADDR:
0a7de745 1214 len = sizeof(struct ifma_msghdr);
91447636
A
1215 break;
1216
1c79356b 1217 case RTM_IFINFO:
0a7de745 1218 len = sizeof(struct if_msghdr);
1c79356b
A
1219 break;
1220
91447636 1221 case RTM_IFINFO2:
0a7de745 1222 len = sizeof(struct if_msghdr2);
91447636
A
1223 break;
1224
1225 case RTM_NEWMADDR2:
0a7de745 1226 len = sizeof(struct ifma_msghdr2);
91447636
A
1227 break;
1228
6d2010ae 1229 case RTM_GET_EXT:
0a7de745 1230 len = sizeof(struct rt_msghdr_ext);
6d2010ae
A
1231 break;
1232
91447636 1233 case RTM_GET2:
0a7de745 1234 len = sizeof(struct rt_msghdr2);
91447636
A
1235 break;
1236
1c79356b 1237 default:
0a7de745 1238 len = sizeof(struct rt_msghdr);
1c79356b
A
1239 }
1240 cp0 = cp;
0a7de745 1241 if (cp0) {
1c79356b 1242 cp += len;
0a7de745 1243 }
1c79356b 1244 for (i = 0; i < RTAX_MAX; i++) {
b0d623f7 1245 struct sockaddr *sa, *hint;
39236c6e 1246 uint8_t ssbuf[SOCK_MAXADDRLEN + 1];
1c79356b 1247
39236c6e
A
1248 /*
1249 * Make sure to accomodate the largest possible size of sa_len.
1250 */
0a7de745 1251 _CASSERT(sizeof(ssbuf) == (SOCK_MAXADDRLEN + 1));
39236c6e 1252
0a7de745 1253 if ((sa = rtinfo->rti_info[i]) == NULL) {
1c79356b 1254 continue;
0a7de745 1255 }
b0d623f7
A
1256
1257 switch (i) {
1258 case RTAX_DST:
1259 case RTAX_NETMASK:
0a7de745 1260 if ((hint = rtinfo->rti_info[RTAX_DST]) == NULL) {
b0d623f7 1261 hint = rtinfo->rti_info[RTAX_IFA];
0a7de745 1262 }
b0d623f7
A
1263
1264 /* Scrub away any trace of embedded interface scope */
39236c6e 1265 sa = rtm_scrub(type, i, hint, sa, &ssbuf,
0a7de745 1266 sizeof(ssbuf), NULL);
39236c6e 1267 break;
d190cdc3 1268 case RTAX_GATEWAY:
39236c6e
A
1269 case RTAX_IFP:
1270 sa = rtm_scrub(type, i, NULL, sa, &ssbuf,
0a7de745 1271 sizeof(ssbuf), credp);
b0d623f7
A
1272 break;
1273
1274 default:
1275 break;
1276 }
1277
1c79356b 1278 rtinfo->rti_addrs |= (1 << i);
3e170ce0
A
1279 dlen = sa->sa_len;
1280 rlen = ROUNDUP32(dlen);
1c79356b 1281 if (cp) {
3e170ce0 1282 bcopy((caddr_t)sa, cp, (size_t)dlen);
0a7de745 1283 if (dlen != rlen) {
3e170ce0 1284 bzero(cp + dlen, rlen - dlen);
0a7de745 1285 }
3e170ce0 1286 cp += rlen;
1c79356b 1287 }
3e170ce0 1288 len += rlen;
1c79356b 1289 }
39236c6e 1290 if (cp == NULL && w != NULL && !second_time) {
91447636 1291 struct walkarg *rw = w;
1c79356b 1292
39236c6e 1293 if (rw->w_req != NULL) {
1c79356b 1294 if (rw->w_tmemsize < len) {
0a7de745 1295 if (rw->w_tmem != NULL) {
1c79356b 1296 FREE(rw->w_tmem, M_RTABLE);
0a7de745
A
1297 }
1298 rw->w_tmem = _MALLOC(len, M_RTABLE, M_ZERO | M_WAITOK);
1299 if (rw->w_tmem != NULL) {
1c79356b 1300 rw->w_tmemsize = len;
0a7de745 1301 }
1c79356b 1302 }
39236c6e 1303 if (rw->w_tmem != NULL) {
1c79356b
A
1304 cp = rw->w_tmem;
1305 second_time = 1;
1306 goto again;
1307 }
1308 }
1309 }
1310 if (cp) {
316670eb 1311 struct rt_msghdr *rtm = (struct rt_msghdr *)(void *)cp0;
1c79356b
A
1312
1313 rtm->rtm_version = RTM_VERSION;
1314 rtm->rtm_type = type;
f427ee49 1315 rtm->rtm_msglen = (u_short)len;
1c79356b 1316 }
0a7de745 1317 return len;
1c79356b
A
1318}
1319
1320/*
1321 * This routine is called to generate a message from the routing
91447636 1322 * socket indicating that a redirect has occurred, a routing lookup
1c79356b
A
1323 * has failed, or that a protocol has detected timeouts to a particular
1324 * destination.
1325 */
1326void
f427ee49 1327rt_missmsg(u_char type, struct rt_addrinfo *rtinfo, int flags, int error)
1c79356b 1328{
91447636
A
1329 struct rt_msghdr *rtm;
1330 struct mbuf *m;
1c79356b 1331 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
cb323159 1332 struct sockproto route_proto = { .sp_family = PF_ROUTE, .sp_protocol = 0 };
1c79356b 1333
0a7de745 1334 if (route_cb.any_count == 0) {
1c79356b 1335 return;
0a7de745 1336 }
1c79356b 1337 m = rt_msg1(type, rtinfo);
0a7de745 1338 if (m == NULL) {
1c79356b 1339 return;
0a7de745 1340 }
1c79356b
A
1341 rtm = mtod(m, struct rt_msghdr *);
1342 rtm->rtm_flags = RTF_DONE | flags;
1343 rtm->rtm_errno = error;
1344 rtm->rtm_addrs = rtinfo->rti_addrs;
6d2010ae 1345 route_proto.sp_family = sa ? sa->sa_family : 0;
1c79356b
A
1346 raw_input(m, &route_proto, &route_src, &route_dst);
1347}
1348
1349/*
1350 * This routine is called to generate a message from the routing
1351 * socket indicating that the status of a network interface has changed.
1352 */
1353void
39236c6e 1354rt_ifmsg(struct ifnet *ifp)
1c79356b 1355{
91447636 1356 struct if_msghdr *ifm;
1c79356b
A
1357 struct mbuf *m;
1358 struct rt_addrinfo info;
cb323159 1359 struct sockproto route_proto = { .sp_family = PF_ROUTE, .sp_protocol = 0 };
1c79356b 1360
0a7de745 1361 if (route_cb.any_count == 0) {
1c79356b 1362 return;
0a7de745
A
1363 }
1364 bzero((caddr_t)&info, sizeof(info));
1c79356b 1365 m = rt_msg1(RTM_IFINFO, &info);
0a7de745 1366 if (m == NULL) {
1c79356b 1367 return;
0a7de745 1368 }
1c79356b
A
1369 ifm = mtod(m, struct if_msghdr *);
1370 ifm->ifm_index = ifp->if_index;
1371 ifm->ifm_flags = (u_short)ifp->if_flags;
2d21ac55 1372 if_data_internal_to_if_data(ifp, &ifp->if_data, &ifm->ifm_data);
1c79356b 1373 ifm->ifm_addrs = 0;
1c79356b
A
1374 raw_input(m, &route_proto, &route_src, &route_dst);
1375}
1376
1377/*
1378 * This is called to generate messages from the routing socket
1379 * indicating a network interface has had addresses associated with it.
1380 * if we ever reverse the logic and replace messages TO the routing
1381 * socket indicate a request to configure interfaces, then it will
1382 * be unnecessary as the routing socket will automatically generate
1383 * copies of it.
91447636
A
1384 *
1385 * Since this is coming from the interface, it is expected that the
6d2010ae 1386 * interface will be locked. Caller must hold rnh_lock and rt_lock.
1c79356b
A
1387 */
1388void
f427ee49 1389rt_newaddrmsg(u_char cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1c79356b
A
1390{
1391 struct rt_addrinfo info;
1392 struct sockaddr *sa = 0;
1393 int pass;
1394 struct mbuf *m = 0;
1395 struct ifnet *ifp = ifa->ifa_ifp;
cb323159 1396 struct sockproto route_proto = { .sp_family = PF_ROUTE, .sp_protocol = 0 };
1c79356b 1397
5ba3f43e 1398 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
1399 RT_LOCK_ASSERT_HELD(rt);
1400
0a7de745 1401 if (route_cb.any_count == 0) {
1c79356b 1402 return;
0a7de745 1403 }
6d2010ae
A
1404
1405 /* Become a regular mutex, just in case */
1406 RT_CONVERT_LOCK(rt);
1c79356b 1407 for (pass = 1; pass < 3; pass++) {
0a7de745 1408 bzero((caddr_t)&info, sizeof(info));
1c79356b
A
1409 if ((cmd == RTM_ADD && pass == 1) ||
1410 (cmd == RTM_DELETE && pass == 2)) {
91447636 1411 struct ifa_msghdr *ifam;
f427ee49 1412 u_char ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1c79356b 1413
6d2010ae 1414 /* Lock ifp for if_lladdr */
b0d623f7 1415 ifnet_lock_shared(ifp);
6d2010ae
A
1416 IFA_LOCK(ifa);
1417 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1418 /*
1419 * Holding ifnet lock here prevents the link address
1420 * from changing contents, so no need to hold its
1421 * lock. The link address is always present; it's
1422 * never freed.
1423 */
1424 info.rti_info[RTAX_IFP] = ifp->if_lladdr->ifa_addr;
1425 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1426 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
b0d623f7 1427 if ((m = rt_msg1(ncmd, &info)) == NULL) {
6d2010ae 1428 IFA_UNLOCK(ifa);
b0d623f7 1429 ifnet_lock_done(ifp);
1c79356b 1430 continue;
b0d623f7 1431 }
6d2010ae 1432 IFA_UNLOCK(ifa);
b0d623f7 1433 ifnet_lock_done(ifp);
1c79356b
A
1434 ifam = mtod(m, struct ifa_msghdr *);
1435 ifam->ifam_index = ifp->if_index;
6d2010ae 1436 IFA_LOCK_SPIN(ifa);
1c79356b
A
1437 ifam->ifam_metric = ifa->ifa_metric;
1438 ifam->ifam_flags = ifa->ifa_flags;
6d2010ae 1439 IFA_UNLOCK(ifa);
1c79356b
A
1440 ifam->ifam_addrs = info.rti_addrs;
1441 }
1442 if ((cmd == RTM_ADD && pass == 2) ||
1443 (cmd == RTM_DELETE && pass == 1)) {
91447636 1444 struct rt_msghdr *rtm;
1c79356b 1445
0a7de745 1446 if (rt == NULL) {
1c79356b 1447 continue;
0a7de745 1448 }
6d2010ae
A
1449 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1450 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1451 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
0a7de745 1452 if ((m = rt_msg1(cmd, &info)) == NULL) {
1c79356b 1453 continue;
0a7de745 1454 }
1c79356b
A
1455 rtm = mtod(m, struct rt_msghdr *);
1456 rtm->rtm_index = ifp->if_index;
1457 rtm->rtm_flags |= rt->rt_flags;
1458 rtm->rtm_errno = error;
1459 rtm->rtm_addrs = info.rti_addrs;
1460 }
1461 route_proto.sp_protocol = sa ? sa->sa_family : 0;
1462 raw_input(m, &route_proto, &route_src, &route_dst);
1463 }
1464}
1465
1466/*
1467 * This is the analogue to the rt_newaddrmsg which performs the same
1468 * function but for multicast group memberhips. This is easier since
1469 * there is no route state to worry about.
1470 */
1471void
f427ee49 1472rt_newmaddrmsg(u_char cmd, struct ifmultiaddr *ifma)
1c79356b
A
1473{
1474 struct rt_addrinfo info;
1475 struct mbuf *m = 0;
1476 struct ifnet *ifp = ifma->ifma_ifp;
1477 struct ifma_msghdr *ifmam;
cb323159 1478 struct sockproto route_proto = { .sp_family = PF_ROUTE, .sp_protocol = 0 };
1c79356b 1479
0a7de745 1480 if (route_cb.any_count == 0) {
1c79356b 1481 return;
0a7de745 1482 }
1c79356b 1483
6d2010ae
A
1484 /* Lock ifp for if_lladdr */
1485 ifnet_lock_shared(ifp);
0a7de745 1486 bzero((caddr_t)&info, sizeof(info));
6d2010ae
A
1487 IFMA_LOCK(ifma);
1488 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
39236c6e
A
1489 /* lladdr doesn't need lock */
1490 info.rti_info[RTAX_IFP] = ifp->if_lladdr->ifa_addr;
6d2010ae 1491
1c79356b
A
1492 /*
1493 * If a link-layer address is present, present it as a ``gateway''
1494 * (similarly to how ARP entries, e.g., are presented).
1495 */
39236c6e
A
1496 info.rti_info[RTAX_GATEWAY] = (ifma->ifma_ll != NULL) ?
1497 ifma->ifma_ll->ifma_addr : NULL;
b0d623f7 1498 if ((m = rt_msg1(cmd, &info)) == NULL) {
6d2010ae
A
1499 IFMA_UNLOCK(ifma);
1500 ifnet_lock_done(ifp);
1c79356b 1501 return;
b0d623f7 1502 }
1c79356b 1503 ifmam = mtod(m, struct ifma_msghdr *);
6d2010ae 1504 ifmam->ifmam_index = ifp->if_index;
1c79356b
A
1505 ifmam->ifmam_addrs = info.rti_addrs;
1506 route_proto.sp_protocol = ifma->ifma_addr->sa_family;
6d2010ae
A
1507 IFMA_UNLOCK(ifma);
1508 ifnet_lock_done(ifp);
1c79356b
A
1509 raw_input(m, &route_proto, &route_src, &route_dst);
1510}
1511
39236c6e
A
1512const char *
1513rtm2str(int cmd)
1514{
1515 const char *c = "RTM_?";
1516
1517 switch (cmd) {
1518 case RTM_ADD:
1519 c = "RTM_ADD";
1520 break;
1521 case RTM_DELETE:
1522 c = "RTM_DELETE";
1523 break;
1524 case RTM_CHANGE:
1525 c = "RTM_CHANGE";
1526 break;
1527 case RTM_GET:
1528 c = "RTM_GET";
1529 break;
1530 case RTM_LOSING:
1531 c = "RTM_LOSING";
1532 break;
1533 case RTM_REDIRECT:
1534 c = "RTM_REDIRECT";
1535 break;
1536 case RTM_MISS:
1537 c = "RTM_MISS";
1538 break;
1539 case RTM_LOCK:
1540 c = "RTM_LOCK";
1541 break;
1542 case RTM_OLDADD:
1543 c = "RTM_OLDADD";
1544 break;
1545 case RTM_OLDDEL:
1546 c = "RTM_OLDDEL";
1547 break;
1548 case RTM_RESOLVE:
1549 c = "RTM_RESOLVE";
1550 break;
1551 case RTM_NEWADDR:
1552 c = "RTM_NEWADDR";
1553 break;
1554 case RTM_DELADDR:
1555 c = "RTM_DELADDR";
1556 break;
1557 case RTM_IFINFO:
1558 c = "RTM_IFINFO";
1559 break;
1560 case RTM_NEWMADDR:
1561 c = "RTM_NEWMADDR";
1562 break;
1563 case RTM_DELMADDR:
1564 c = "RTM_DELMADDR";
1565 break;
1566 case RTM_GET_SILENT:
1567 c = "RTM_GET_SILENT";
1568 break;
1569 case RTM_IFINFO2:
1570 c = "RTM_IFINFO2";
1571 break;
1572 case RTM_NEWMADDR2:
1573 c = "RTM_NEWMADDR2";
1574 break;
1575 case RTM_GET2:
1576 c = "RTM_GET2";
1577 break;
1578 case RTM_GET_EXT:
1579 c = "RTM_GET_EXT";
1580 break;
1581 }
1582
0a7de745 1583 return c;
39236c6e
A
1584}
1585
1c79356b
A
1586/*
1587 * This is used in dumping the kernel table via sysctl().
1588 */
39236c6e 1589static int
2d21ac55 1590sysctl_dumpentry(struct radix_node *rn, void *vw)
1c79356b 1591{
91447636
A
1592 struct walkarg *w = vw;
1593 struct rtentry *rt = (struct rtentry *)rn;
1c79356b
A
1594 int error = 0, size;
1595 struct rt_addrinfo info;
39236c6e 1596 kauth_cred_t cred;
a39ff7e2 1597 kauth_cred_t *credp;
39236c6e
A
1598
1599 cred = kauth_cred_proc_ref(current_proc());
d9a64523 1600 credp = &cred;
1c79356b 1601
b0d623f7 1602 RT_LOCK(rt);
d9a64523 1603 if ((w->w_op == NET_RT_FLAGS || w->w_op == NET_RT_FLAGS_PRIV) &&
0a7de745 1604 !(rt->rt_flags & w->w_arg)) {
39236c6e 1605 goto done;
0a7de745 1606 }
d9a64523
A
1607
1608 /*
1609 * If the matching route has RTF_LLINFO set, then we can skip scrubbing the MAC
1610 * only if the outgoing interface is not loopback and the process has entitlement
1611 * for neighbor cache read.
1612 */
1613 if (w->w_op == NET_RT_FLAGS_PRIV && (rt->rt_flags & RTF_LLINFO)) {
1614 if (rt->rt_ifp != lo_ifp &&
1615 (route_op_entitlement_check(NULL, cred, ROUTE_OP_READ, TRUE) == 0)) {
1616 credp = NULL;
1617 }
1618 }
1619
0a7de745 1620 bzero((caddr_t)&info, sizeof(info));
6d2010ae
A
1621 info.rti_info[RTAX_DST] = rt_key(rt);
1622 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1623 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1624 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
cb323159
A
1625 if (RT_HAS_IFADDR(rt)) {
1626 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1627 }
6d2010ae 1628
91447636 1629 if (w->w_op != NET_RT_DUMP2) {
a39ff7e2 1630 size = rt_msg2(RTM_GET, &info, NULL, w, credp);
39236c6e 1631 if (w->w_req != NULL && w->w_tmem != NULL) {
316670eb
A
1632 struct rt_msghdr *rtm =
1633 (struct rt_msghdr *)(void *)w->w_tmem;
91447636
A
1634
1635 rtm->rtm_flags = rt->rt_flags;
1636 rtm->rtm_use = rt->rt_use;
6d2010ae 1637 rt_getmetrics(rt, &rtm->rtm_rmx);
91447636
A
1638 rtm->rtm_index = rt->rt_ifp->if_index;
1639 rtm->rtm_pid = 0;
6d2010ae
A
1640 rtm->rtm_seq = 0;
1641 rtm->rtm_errno = 0;
91447636
A
1642 rtm->rtm_addrs = info.rti_addrs;
1643 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
91447636
A
1644 }
1645 } else {
a39ff7e2 1646 size = rt_msg2(RTM_GET2, &info, NULL, w, credp);
39236c6e 1647 if (w->w_req != NULL && w->w_tmem != NULL) {
316670eb
A
1648 struct rt_msghdr2 *rtm =
1649 (struct rt_msghdr2 *)(void *)w->w_tmem;
6d2010ae
A
1650
1651 rtm->rtm_flags = rt->rt_flags;
1652 rtm->rtm_use = rt->rt_use;
1653 rt_getmetrics(rt, &rtm->rtm_rmx);
1654 rtm->rtm_index = rt->rt_ifp->if_index;
1655 rtm->rtm_refcnt = rt->rt_refcnt;
0a7de745 1656 if (rt->rt_parent) {
91447636 1657 rtm->rtm_parentflags = rt->rt_parent->rt_flags;
0a7de745 1658 } else {
91447636 1659 rtm->rtm_parentflags = 0;
0a7de745 1660 }
6d2010ae
A
1661 rtm->rtm_reserved = 0;
1662 rtm->rtm_addrs = info.rti_addrs;
1663 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
91447636 1664 }
1c79356b 1665 }
39236c6e
A
1666
1667done:
b0d623f7 1668 RT_UNLOCK(rt);
39236c6e 1669 kauth_cred_unref(&cred);
0a7de745 1670 return error;
1c79356b
A
1671}
1672
6d2010ae
A
1673/*
1674 * This is used for dumping extended information from route entries.
1675 */
39236c6e 1676static int
6d2010ae
A
1677sysctl_dumpentry_ext(struct radix_node *rn, void *vw)
1678{
1679 struct walkarg *w = vw;
1680 struct rtentry *rt = (struct rtentry *)rn;
1681 int error = 0, size;
1682 struct rt_addrinfo info;
39236c6e
A
1683 kauth_cred_t cred;
1684
1685 cred = kauth_cred_proc_ref(current_proc());
6d2010ae
A
1686
1687 RT_LOCK(rt);
0a7de745 1688 if (w->w_op == NET_RT_DUMPX_FLAGS && !(rt->rt_flags & w->w_arg)) {
39236c6e 1689 goto done;
0a7de745
A
1690 }
1691 bzero(&info, sizeof(info));
6d2010ae
A
1692 info.rti_info[RTAX_DST] = rt_key(rt);
1693 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1694 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1695 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
39236c6e 1696
5ba3f43e 1697 size = rt_msg2(RTM_GET_EXT, &info, NULL, w, &cred);
39236c6e 1698 if (w->w_req != NULL && w->w_tmem != NULL) {
316670eb
A
1699 struct rt_msghdr_ext *ertm =
1700 (struct rt_msghdr_ext *)(void *)w->w_tmem;
6d2010ae
A
1701
1702 ertm->rtm_flags = rt->rt_flags;
1703 ertm->rtm_use = rt->rt_use;
1704 rt_getmetrics(rt, &ertm->rtm_rmx);
1705 ertm->rtm_index = rt->rt_ifp->if_index;
1706 ertm->rtm_pid = 0;
1707 ertm->rtm_seq = 0;
1708 ertm->rtm_errno = 0;
1709 ertm->rtm_addrs = info.rti_addrs;
316670eb 1710 if (rt->rt_llinfo_get_ri == NULL) {
0a7de745 1711 bzero(&ertm->rtm_ri, sizeof(ertm->rtm_ri));
316670eb
A
1712 ertm->rtm_ri.ri_rssi = IFNET_RSSI_UNKNOWN;
1713 ertm->rtm_ri.ri_lqm = IFNET_LQM_THRESH_OFF;
1714 ertm->rtm_ri.ri_npm = IFNET_NPM_THRESH_UNKNOWN;
39236c6e 1715 } else {
6d2010ae 1716 rt->rt_llinfo_get_ri(rt, &ertm->rtm_ri);
39236c6e 1717 }
6d2010ae 1718 error = SYSCTL_OUT(w->w_req, (caddr_t)ertm, size);
6d2010ae 1719 }
39236c6e
A
1720
1721done:
6d2010ae 1722 RT_UNLOCK(rt);
39236c6e 1723 kauth_cred_unref(&cred);
0a7de745 1724 return error;
6d2010ae
A
1725}
1726
1727/*
1728 * rdar://9307819
39236c6e
A
1729 * To avoid to call copyout() while holding locks and to cause problems
1730 * in the paging path, sysctl_iflist() and sysctl_iflist2() contstruct
6d2010ae
A
1731 * the list in two passes. In the first pass we compute the total
1732 * length of the data we are going to copyout, then we release
39236c6e 1733 * all locks to allocate a temporary buffer that gets filled
6d2010ae
A
1734 * in the second pass.
1735 *
39236c6e
A
1736 * Note that we are verifying the assumption that _MALLOC returns a buffer
1737 * that is at least 32 bits aligned and that the messages and addresses are
6d2010ae
A
1738 * 32 bits aligned.
1739 */
39236c6e 1740static int
6d2010ae 1741sysctl_iflist(int af, struct walkarg *w)
1c79356b 1742{
91447636
A
1743 struct ifnet *ifp;
1744 struct ifaddr *ifa;
0a7de745
A
1745 struct rt_addrinfo info;
1746 int len = 0, error = 0;
1747 int pass = 0;
1748 int total_len = 0, current_len = 0;
1749 char *total_buffer = NULL, *cp = NULL;
39236c6e
A
1750 kauth_cred_t cred;
1751
1752 cred = kauth_cred_proc_ref(current_proc());
1753
0a7de745 1754 bzero((caddr_t)&info, sizeof(info));
1c79356b 1755
6d2010ae
A
1756 for (pass = 0; pass < 2; pass++) {
1757 ifnet_head_lock_shared();
39236c6e 1758
6d2010ae 1759 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
0a7de745 1760 if (error) {
91447636 1761 break;
0a7de745
A
1762 }
1763 if (w->w_arg && w->w_arg != ifp->if_index) {
1c79356b 1764 continue;
0a7de745 1765 }
6d2010ae
A
1766 ifnet_lock_shared(ifp);
1767 /*
39236c6e
A
1768 * Holding ifnet lock here prevents the link address
1769 * from changing contents, so no need to hold the ifa
1770 * lock. The link address is always present; it's
1771 * never freed.
6d2010ae
A
1772 */
1773 ifa = ifp->if_lladdr;
1774 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
5ba3f43e 1775 len = rt_msg2(RTM_IFINFO, &info, NULL, NULL, &cred);
6d2010ae
A
1776 if (pass == 0) {
1777 total_len += len;
1778 } else {
1779 struct if_msghdr *ifm;
1780
1781 if (current_len + len > total_len) {
1782 ifnet_lock_done(ifp);
6d2010ae 1783 error = ENOBUFS;
91447636 1784 break;
6d2010ae
A
1785 }
1786 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
39236c6e 1787 len = rt_msg2(RTM_IFINFO, &info,
5ba3f43e 1788 (caddr_t)cp, NULL, &cred);
6d2010ae 1789 info.rti_info[RTAX_IFP] = NULL;
39236c6e 1790
316670eb 1791 ifm = (struct if_msghdr *)(void *)cp;
6d2010ae
A
1792 ifm->ifm_index = ifp->if_index;
1793 ifm->ifm_flags = (u_short)ifp->if_flags;
1794 if_data_internal_to_if_data(ifp, &ifp->if_data,
39236c6e 1795 &ifm->ifm_data);
6d2010ae 1796 ifm->ifm_addrs = info.rti_addrs;
5ba3f43e
A
1797 /*
1798 * <rdar://problem/32940901>
1799 * Round bytes only for non-platform
0a7de745 1800 */
5ba3f43e
A
1801 if (!csproc_get_platform_binary(w->w_req->p)) {
1802 ALIGN_BYTES(ifm->ifm_data.ifi_ibytes);
1803 ALIGN_BYTES(ifm->ifm_data.ifi_obytes);
1804 }
6d2010ae
A
1805
1806 cp += len;
0a7de745 1807 VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
6d2010ae 1808 current_len += len;
1c79356b 1809 }
39236c6e 1810 while ((ifa = ifa->ifa_link.tqe_next) != NULL) {
6d2010ae
A
1811 IFA_LOCK(ifa);
1812 if (af && af != ifa->ifa_addr->sa_family) {
1813 IFA_UNLOCK(ifa);
1814 continue;
1815 }
d9a64523
A
1816 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1817 (((struct in6_ifaddr *)ifa)->ia6_flags &
0a7de745 1818 IN6_IFF_CLAT46) != 0) {
d9a64523
A
1819 IFA_UNLOCK(ifa);
1820 continue;
1821 }
6d2010ae
A
1822 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1823 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1824 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
39236c6e 1825 len = rt_msg2(RTM_NEWADDR, &info, NULL, NULL,
5ba3f43e 1826 &cred);
6d2010ae
A
1827 if (pass == 0) {
1828 total_len += len;
1829 } else {
1830 struct ifa_msghdr *ifam;
1831
1832 if (current_len + len > total_len) {
1833 IFA_UNLOCK(ifa);
6d2010ae
A
1834 error = ENOBUFS;
1835 break;
1836 }
39236c6e 1837 len = rt_msg2(RTM_NEWADDR, &info,
5ba3f43e 1838 (caddr_t)cp, NULL, &cred);
39236c6e 1839
316670eb 1840 ifam = (struct ifa_msghdr *)(void *)cp;
39236c6e
A
1841 ifam->ifam_index =
1842 ifa->ifa_ifp->if_index;
6d2010ae
A
1843 ifam->ifam_flags = ifa->ifa_flags;
1844 ifam->ifam_metric = ifa->ifa_metric;
1845 ifam->ifam_addrs = info.rti_addrs;
1846
1847 cp += len;
39236c6e 1848 VERIFY(IS_P2ALIGNED(cp,
0a7de745 1849 sizeof(u_int32_t)));
6d2010ae
A
1850 current_len += len;
1851 }
1852 IFA_UNLOCK(ifa);
1853 }
1854 ifnet_lock_done(ifp);
39236c6e
A
1855 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1856 info.rti_info[RTAX_BRD] = NULL;
6d2010ae 1857 }
39236c6e 1858
6d2010ae 1859 ifnet_head_done();
39236c6e
A
1860
1861 if (error != 0) {
0a7de745 1862 if (error == ENOBUFS) {
39236c6e
A
1863 printf("%s: current_len (%d) + len (%d) > "
1864 "total_len (%d)\n", __func__, current_len,
1865 len, total_len);
0a7de745 1866 }
6d2010ae 1867 break;
39236c6e
A
1868 }
1869
6d2010ae
A
1870 if (pass == 0) {
1871 /* Better to return zero length buffer than ENOBUFS */
0a7de745 1872 if (total_len == 0) {
6d2010ae 1873 total_len = 1;
0a7de745 1874 }
6d2010ae 1875 total_len += total_len >> 3;
39236c6e
A
1876 total_buffer = _MALLOC(total_len, M_RTABLE,
1877 M_ZERO | M_WAITOK);
6d2010ae 1878 if (total_buffer == NULL) {
39236c6e
A
1879 printf("%s: _MALLOC(%d) failed\n", __func__,
1880 total_len);
6d2010ae
A
1881 error = ENOBUFS;
1882 break;
1883 }
1884 cp = total_buffer;
0a7de745 1885 VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
6d2010ae
A
1886 } else {
1887 error = SYSCTL_OUT(w->w_req, total_buffer, current_len);
0a7de745 1888 if (error) {
6d2010ae 1889 break;
0a7de745 1890 }
1c79356b 1891 }
1c79356b 1892 }
39236c6e 1893
0a7de745 1894 if (total_buffer != NULL) {
6d2010ae 1895 _FREE(total_buffer, M_RTABLE);
0a7de745 1896 }
39236c6e
A
1897
1898 kauth_cred_unref(&cred);
0a7de745 1899 return error;
1c79356b
A
1900}
1901
39236c6e 1902static int
6d2010ae 1903sysctl_iflist2(int af, struct walkarg *w)
91447636
A
1904{
1905 struct ifnet *ifp;
1906 struct ifaddr *ifa;
0a7de745
A
1907 struct rt_addrinfo info;
1908 int len = 0, error = 0;
1909 int pass = 0;
1910 int total_len = 0, current_len = 0;
1911 char *total_buffer = NULL, *cp = NULL;
39236c6e 1912 kauth_cred_t cred;
6d2010ae 1913
39236c6e
A
1914 cred = kauth_cred_proc_ref(current_proc());
1915
0a7de745 1916 bzero((caddr_t)&info, sizeof(info));
6d2010ae
A
1917
1918 for (pass = 0; pass < 2; pass++) {
39236c6e
A
1919 struct ifmultiaddr *ifma;
1920
6d2010ae 1921 ifnet_head_lock_shared();
39236c6e 1922
6d2010ae 1923 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
0a7de745 1924 if (error) {
91447636 1925 break;
0a7de745
A
1926 }
1927 if (w->w_arg && w->w_arg != ifp->if_index) {
91447636 1928 continue;
0a7de745 1929 }
6d2010ae
A
1930 ifnet_lock_shared(ifp);
1931 /*
39236c6e
A
1932 * Holding ifnet lock here prevents the link address
1933 * from changing contents, so no need to hold the ifa
1934 * lock. The link address is always present; it's
1935 * never freed.
6d2010ae
A
1936 */
1937 ifa = ifp->if_lladdr;
1938 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
5ba3f43e 1939 len = rt_msg2(RTM_IFINFO2, &info, NULL, NULL, &cred);
6d2010ae
A
1940 if (pass == 0) {
1941 total_len += len;
1942 } else {
1943 struct if_msghdr2 *ifm;
1944
1945 if (current_len + len > total_len) {
1946 ifnet_lock_done(ifp);
6d2010ae 1947 error = ENOBUFS;
91447636 1948 break;
6d2010ae
A
1949 }
1950 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
39236c6e 1951 len = rt_msg2(RTM_IFINFO2, &info,
5ba3f43e 1952 (caddr_t)cp, NULL, &cred);
6d2010ae 1953 info.rti_info[RTAX_IFP] = NULL;
39236c6e 1954
316670eb 1955 ifm = (struct if_msghdr2 *)(void *)cp;
6d2010ae
A
1956 ifm->ifm_addrs = info.rti_addrs;
1957 ifm->ifm_flags = (u_short)ifp->if_flags;
1958 ifm->ifm_index = ifp->if_index;
316670eb
A
1959 ifm->ifm_snd_len = IFCQ_LEN(&ifp->if_snd);
1960 ifm->ifm_snd_maxlen = IFCQ_MAXLEN(&ifp->if_snd);
1961 ifm->ifm_snd_drops =
f427ee49 1962 (int)ifp->if_snd.ifcq_dropcnt.packets;
6d2010ae 1963 ifm->ifm_timer = ifp->if_timer;
39236c6e
A
1964 if_data_internal_to_if_data64(ifp,
1965 &ifp->if_data, &ifm->ifm_data);
5ba3f43e
A
1966 /*
1967 * <rdar://problem/32940901>
1968 * Round bytes only for non-platform
0a7de745 1969 */
5ba3f43e
A
1970 if (!csproc_get_platform_binary(w->w_req->p)) {
1971 ALIGN_BYTES(ifm->ifm_data.ifi_ibytes);
1972 ALIGN_BYTES(ifm->ifm_data.ifi_obytes);
1973 }
6d2010ae
A
1974
1975 cp += len;
0a7de745 1976 VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
6d2010ae 1977 current_len += len;
91447636 1978 }
39236c6e 1979 while ((ifa = ifa->ifa_link.tqe_next) != NULL) {
6d2010ae
A
1980 IFA_LOCK(ifa);
1981 if (af && af != ifa->ifa_addr->sa_family) {
1982 IFA_UNLOCK(ifa);
91447636 1983 continue;
6d2010ae 1984 }
d9a64523
A
1985 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1986 (((struct in6_ifaddr *)ifa)->ia6_flags &
0a7de745 1987 IN6_IFF_CLAT46) != 0) {
d9a64523
A
1988 IFA_UNLOCK(ifa);
1989 continue;
1990 }
1991
6d2010ae
A
1992 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1993 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1994 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
39236c6e 1995 len = rt_msg2(RTM_NEWADDR, &info, NULL, NULL,
5ba3f43e 1996 &cred);
6d2010ae
A
1997 if (pass == 0) {
1998 total_len += len;
1999 } else {
2000 struct ifa_msghdr *ifam;
39236c6e 2001
6d2010ae
A
2002 if (current_len + len > total_len) {
2003 IFA_UNLOCK(ifa);
6d2010ae 2004 error = ENOBUFS;
91447636 2005 break;
6d2010ae 2006 }
39236c6e 2007 len = rt_msg2(RTM_NEWADDR, &info,
5ba3f43e 2008 (caddr_t)cp, NULL, &cred);
6d2010ae 2009
316670eb 2010 ifam = (struct ifa_msghdr *)(void *)cp;
39236c6e
A
2011 ifam->ifam_index =
2012 ifa->ifa_ifp->if_index;
6d2010ae
A
2013 ifam->ifam_flags = ifa->ifa_flags;
2014 ifam->ifam_metric = ifa->ifa_metric;
2015 ifam->ifam_addrs = info.rti_addrs;
2016
2017 cp += len;
39236c6e 2018 VERIFY(IS_P2ALIGNED(cp,
0a7de745 2019 sizeof(u_int32_t)));
6d2010ae
A
2020 current_len += len;
2021 }
2022 IFA_UNLOCK(ifa);
2023 }
2024 if (error) {
2025 ifnet_lock_done(ifp);
2026 break;
2027 }
39236c6e
A
2028
2029 for (ifma = LIST_FIRST(&ifp->if_multiaddrs);
2030 ifma != NULL; ifma = LIST_NEXT(ifma, ifma_link)) {
2031 struct ifaddr *ifa0;
2032
2033 IFMA_LOCK(ifma);
2034 if (af && af != ifma->ifma_addr->sa_family) {
2035 IFMA_UNLOCK(ifma);
2036 continue;
2037 }
0a7de745 2038 bzero((caddr_t)&info, sizeof(info));
39236c6e
A
2039 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2040 /*
2041 * Holding ifnet lock here prevents the link
2042 * address from changing contents, so no need
2043 * to hold the ifa0 lock. The link address is
2044 * always present; it's never freed.
2045 */
2046 ifa0 = ifp->if_lladdr;
2047 info.rti_info[RTAX_IFP] = ifa0->ifa_addr;
0a7de745 2048 if (ifma->ifma_ll != NULL) {
39236c6e
A
2049 info.rti_info[RTAX_GATEWAY] =
2050 ifma->ifma_ll->ifma_addr;
0a7de745 2051 }
39236c6e 2052 len = rt_msg2(RTM_NEWMADDR2, &info, NULL, NULL,
5ba3f43e 2053 &cred);
39236c6e
A
2054 if (pass == 0) {
2055 total_len += len;
2056 } else {
2057 struct ifma_msghdr2 *ifmam;
2058
2059 if (current_len + len > total_len) {
6d2010ae 2060 IFMA_UNLOCK(ifma);
39236c6e
A
2061 error = ENOBUFS;
2062 break;
6d2010ae 2063 }
39236c6e 2064 len = rt_msg2(RTM_NEWMADDR2, &info,
5ba3f43e 2065 (caddr_t)cp, NULL, &cred);
39236c6e
A
2066
2067 ifmam =
2068 (struct ifma_msghdr2 *)(void *)cp;
2069 ifmam->ifmam_addrs = info.rti_addrs;
2070 ifmam->ifmam_flags = 0;
2071 ifmam->ifmam_index =
2072 ifma->ifma_ifp->if_index;
2073 ifmam->ifmam_refcount =
2074 ifma->ifma_reqcnt;
2075
2076 cp += len;
2077 VERIFY(IS_P2ALIGNED(cp,
0a7de745 2078 sizeof(u_int32_t)));
39236c6e 2079 current_len += len;
91447636 2080 }
39236c6e 2081 IFMA_UNLOCK(ifma);
91447636 2082 }
6d2010ae 2083 ifnet_lock_done(ifp);
39236c6e
A
2084 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
2085 info.rti_info[RTAX_BRD] = NULL;
6d2010ae
A
2086 }
2087 ifnet_head_done();
39236c6e
A
2088
2089 if (error) {
0a7de745 2090 if (error == ENOBUFS) {
39236c6e
A
2091 printf("%s: current_len (%d) + len (%d) > "
2092 "total_len (%d)\n", __func__, current_len,
2093 len, total_len);
0a7de745 2094 }
6d2010ae 2095 break;
39236c6e
A
2096 }
2097
6d2010ae
A
2098 if (pass == 0) {
2099 /* Better to return zero length buffer than ENOBUFS */
0a7de745 2100 if (total_len == 0) {
6d2010ae 2101 total_len = 1;
0a7de745 2102 }
6d2010ae 2103 total_len += total_len >> 3;
39236c6e
A
2104 total_buffer = _MALLOC(total_len, M_RTABLE,
2105 M_ZERO | M_WAITOK);
6d2010ae 2106 if (total_buffer == NULL) {
39236c6e
A
2107 printf("%s: _MALLOC(%d) failed\n", __func__,
2108 total_len);
6d2010ae
A
2109 error = ENOBUFS;
2110 break;
2111 }
2112 cp = total_buffer;
0a7de745 2113 VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
6d2010ae
A
2114 } else {
2115 error = SYSCTL_OUT(w->w_req, total_buffer, current_len);
0a7de745 2116 if (error) {
6d2010ae 2117 break;
0a7de745 2118 }
91447636 2119 }
91447636 2120 }
39236c6e 2121
0a7de745 2122 if (total_buffer != NULL) {
6d2010ae 2123 _FREE(total_buffer, M_RTABLE);
0a7de745 2124 }
39236c6e
A
2125
2126 kauth_cred_unref(&cred);
0a7de745 2127 return error;
91447636
A
2128}
2129
2130
2131static int
2132sysctl_rtstat(struct sysctl_req *req)
2133{
0a7de745 2134 return SYSCTL_OUT(req, &rtstat, sizeof(struct rtstat));
91447636
A
2135}
2136
2137static int
2138sysctl_rttrash(struct sysctl_req *req)
2139{
0a7de745 2140 return SYSCTL_OUT(req, &rttrash, sizeof(rttrash));
d1ecb069 2141}
91447636 2142
1c79356b
A
2143static int
2144sysctl_rtsock SYSCTL_HANDLER_ARGS
2145{
c910b4d9 2146#pragma unused(oidp)
0a7de745
A
2147 int *name = (int *)arg1;
2148 u_int namelen = arg2;
91447636 2149 struct radix_node_head *rnh;
0a7de745 2150 int i, error = EINVAL;
1c79356b 2151 u_char af;
0a7de745 2152 struct walkarg w;
1c79356b 2153
0a7de745 2154 name++;
1c79356b 2155 namelen--;
0a7de745
A
2156 if (req->newptr) {
2157 return EPERM;
2158 }
2159 if (namelen != 3) {
2160 return EINVAL;
2161 }
f427ee49 2162 af = (u_char)name[0];
0a7de745 2163 Bzero(&w, sizeof(w));
1c79356b
A
2164 w.w_op = name[1];
2165 w.w_arg = name[2];
2166 w.w_req = req;
2167
1c79356b 2168 switch (w.w_op) {
1c79356b 2169 case NET_RT_DUMP:
91447636 2170 case NET_RT_DUMP2:
1c79356b 2171 case NET_RT_FLAGS:
d9a64523 2172 case NET_RT_FLAGS_PRIV:
b0d623f7 2173 lck_mtx_lock(rnh_lock);
0a7de745 2174 for (i = 1; i <= AF_MAX; i++) {
1c79356b
A
2175 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
2176 (error = rnh->rnh_walktree(rnh,
0a7de745 2177 sysctl_dumpentry, &w))) {
6d2010ae 2178 break;
0a7de745
A
2179 }
2180 }
6d2010ae
A
2181 lck_mtx_unlock(rnh_lock);
2182 break;
2183 case NET_RT_DUMPX:
2184 case NET_RT_DUMPX_FLAGS:
2185 lck_mtx_lock(rnh_lock);
0a7de745 2186 for (i = 1; i <= AF_MAX; i++) {
6d2010ae
A
2187 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
2188 (error = rnh->rnh_walktree(rnh,
0a7de745 2189 sysctl_dumpentry_ext, &w))) {
1c79356b 2190 break;
0a7de745
A
2191 }
2192 }
b0d623f7 2193 lck_mtx_unlock(rnh_lock);
1c79356b 2194 break;
1c79356b
A
2195 case NET_RT_IFLIST:
2196 error = sysctl_iflist(af, &w);
91447636
A
2197 break;
2198 case NET_RT_IFLIST2:
2199 error = sysctl_iflist2(af, &w);
2200 break;
2201 case NET_RT_STAT:
2202 error = sysctl_rtstat(req);
2203 break;
2204 case NET_RT_TRASH:
2205 error = sysctl_rttrash(req);
2206 break;
1c79356b 2207 }
0a7de745 2208 if (w.w_tmem != NULL) {
1c79356b 2209 FREE(w.w_tmem, M_RTABLE);
0a7de745
A
2210 }
2211 return error;
1c79356b
A
2212}
2213
1c79356b
A
2214/*
2215 * Definitions of protocols supported in the ROUTE domain.
2216 */
1c79356b 2217static struct protosw routesw[] = {
0a7de745
A
2218 {
2219 .pr_type = SOCK_RAW,
2220 .pr_protocol = 0,
2221 .pr_flags = PR_ATOMIC | PR_ADDR,
2222 .pr_output = route_output,
2223 .pr_ctlinput = raw_ctlinput,
2224 .pr_init = raw_init,
2225 .pr_usrreqs = &route_usrreqs,
2226 }
1c79356b
A
2227};
2228
0a7de745 2229static int route_proto_count = (sizeof(routesw) / sizeof(struct protosw));
1c79356b 2230
39236c6e 2231struct domain routedomain_s = {
0a7de745
A
2232 .dom_family = PF_ROUTE,
2233 .dom_name = "route",
2234 .dom_init = route_dinit,
39236c6e
A
2235};
2236
2237static void
2238route_dinit(struct domain *dp)
2239{
2240 struct protosw *pr;
2241 int i;
1c79356b 2242
39236c6e
A
2243 VERIFY(!(dp->dom_flags & DOM_INITIALIZED));
2244 VERIFY(routedomain == NULL);
2245
2246 routedomain = dp;
2247
0a7de745 2248 for (i = 0, pr = &routesw[0]; i < route_proto_count; i++, pr++) {
39236c6e 2249 net_add_proto(pr, dp, 1);
0a7de745 2250 }
39236c6e
A
2251
2252 route_init();
2253}