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