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