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