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