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