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