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