]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
37839358 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
37839358 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
37839358 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1988, 1991, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | * @(#)rtsock.c 8.5 (Berkeley) 11/2/94 | |
55 | */ | |
56 | ||
57 | ||
58 | #include <sys/param.h> | |
59 | #include <sys/systm.h> | |
60 | #include <sys/kernel.h> | |
61 | #include <sys/sysctl.h> | |
62 | #include <sys/proc.h> | |
63 | #include <sys/malloc.h> | |
64 | #include <sys/mbuf.h> | |
65 | #include <sys/socket.h> | |
66 | #include <sys/socketvar.h> | |
67 | #include <sys/domain.h> | |
68 | #include <sys/protosw.h> | |
9bccf70c | 69 | #include <sys/syslog.h> |
91447636 | 70 | #include <kern/lock.h> |
1c79356b A |
71 | |
72 | #include <net/if.h> | |
73 | #include <net/route.h> | |
74 | #include <net/raw_cb.h> | |
9bccf70c | 75 | #include <netinet/in.h> |
1c79356b | 76 | |
91447636 A |
77 | #include <machine/spl.h> |
78 | ||
79 | extern void m_copydata(struct mbuf *, int, int, caddr_t); | |
80 | extern void m_copyback(struct mbuf *, int, int, caddr_t); | |
81 | ||
82 | extern struct rtstat rtstat; | |
83 | extern int rttrash; | |
84 | ||
1c79356b A |
85 | MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); |
86 | ||
91447636 A |
87 | extern lck_mtx_t *rt_mtx; |
88 | static struct sockaddr route_dst = { 2, PF_ROUTE, { 0, } }; | |
89 | static struct sockaddr route_src = { 2, PF_ROUTE, { 0, } }; | |
90 | static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, { 0, } }; | |
91 | static struct sockproto route_proto = { PF_ROUTE, 0 }; | |
1c79356b | 92 | |
1c79356b A |
93 | struct walkarg { |
94 | int w_tmemsize; | |
95 | int w_op, w_arg; | |
96 | caddr_t w_tmem; | |
97 | struct sysctl_req *w_req; | |
98 | }; | |
99 | ||
100 | static struct mbuf * | |
91447636 A |
101 | rt_msg1(int, struct rt_addrinfo *); |
102 | static int rt_msg2(int, struct rt_addrinfo *, caddr_t, struct walkarg *); | |
103 | static int rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); | |
104 | static int sysctl_dumpentry(struct radix_node *rn, void *vw); | |
105 | static int sysctl_iflist(int af, struct walkarg *w); | |
106 | static int sysctl_iflist2(int af, struct walkarg *w); | |
107 | static int route_output(struct mbuf *, struct socket *); | |
108 | static void rt_setmetrics(u_long, struct rt_metrics *, struct rt_metrics *); | |
109 | static void rt_setif(struct rtentry *, struct sockaddr *, struct sockaddr *, | |
110 | struct sockaddr *); | |
1c79356b A |
111 | |
112 | /* Sleazy use of local variables throughout file, warning!!!! */ | |
113 | #define dst info.rti_info[RTAX_DST] | |
114 | #define gate info.rti_info[RTAX_GATEWAY] | |
115 | #define netmask info.rti_info[RTAX_NETMASK] | |
116 | #define genmask info.rti_info[RTAX_GENMASK] | |
117 | #define ifpaddr info.rti_info[RTAX_IFP] | |
118 | #define ifaaddr info.rti_info[RTAX_IFA] | |
119 | #define brdaddr info.rti_info[RTAX_BRD] | |
120 | ||
121 | /* | |
122 | * It really doesn't make any sense at all for this code to share much | |
123 | * with raw_usrreq.c, since its functionality is so restricted. XXX | |
124 | */ | |
125 | static int | |
126 | rts_abort(struct socket *so) | |
127 | { | |
91447636 A |
128 | int error; |
129 | ||
1c79356b | 130 | error = raw_usrreqs.pru_abort(so); |
1c79356b A |
131 | return error; |
132 | } | |
133 | ||
134 | /* pru_accept is EOPNOTSUPP */ | |
135 | ||
136 | static int | |
91447636 | 137 | rts_attach(struct socket *so, int proto, __unused struct proc *p) |
1c79356b A |
138 | { |
139 | struct rawcb *rp; | |
91447636 | 140 | int error; |
1c79356b A |
141 | |
142 | if (sotorawcb(so) != 0) | |
143 | return EISCONN; /* XXX panic? */ | |
144 | MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK); /* XXX */ | |
145 | if (rp == 0) | |
146 | return ENOBUFS; | |
147 | bzero(rp, sizeof *rp); | |
148 | ||
149 | /* | |
150 | * The splnet() is necessary to block protocols from sending | |
151 | * error notifications (like RTM_REDIRECT or RTM_LOSING) while | |
152 | * this PCB is extant but incompletely initialized. | |
153 | * Probably we should try to do more of this work beforehand and | |
154 | * eliminate the spl. | |
155 | */ | |
1c79356b | 156 | so->so_pcb = (caddr_t)rp; |
55e303ae | 157 | error = raw_attach(so, proto); /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */ |
1c79356b A |
158 | rp = sotorawcb(so); |
159 | if (error) { | |
1c79356b | 160 | FREE(rp, M_PCB); |
55e303ae | 161 | so->so_pcb = 0; |
91447636 | 162 | so->so_flags |= SOF_PCBCLEARING; |
1c79356b A |
163 | return error; |
164 | } | |
37839358 | 165 | |
1c79356b | 166 | switch(rp->rcb_proto.sp_protocol) { |
91447636 | 167 | //####LD route_cb needs looking |
1c79356b A |
168 | case AF_INET: |
169 | route_cb.ip_count++; | |
170 | break; | |
171 | case AF_INET6: | |
172 | route_cb.ip6_count++; | |
173 | break; | |
174 | case AF_IPX: | |
175 | route_cb.ipx_count++; | |
176 | break; | |
177 | case AF_NS: | |
178 | route_cb.ns_count++; | |
179 | break; | |
1c79356b A |
180 | } |
181 | rp->rcb_faddr = &route_src; | |
182 | route_cb.any_count++; | |
37839358 | 183 | /* the socket is already locked when we enter rts_attach */ |
1c79356b A |
184 | soisconnected(so); |
185 | so->so_options |= SO_USELOOPBACK; | |
1c79356b A |
186 | return 0; |
187 | } | |
188 | ||
189 | static int | |
190 | rts_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
191 | { | |
192 | int s, error; | |
193 | s = splnet(); | |
194 | error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */ | |
195 | splx(s); | |
196 | return error; | |
197 | } | |
198 | ||
199 | static int | |
200 | rts_connect(struct socket *so, struct sockaddr *nam, struct proc *p) | |
201 | { | |
202 | int s, error; | |
203 | s = splnet(); | |
204 | error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */ | |
205 | splx(s); | |
206 | return error; | |
207 | } | |
208 | ||
209 | /* pru_connect2 is EOPNOTSUPP */ | |
210 | /* pru_control is EOPNOTSUPP */ | |
211 | ||
212 | static int | |
213 | rts_detach(struct socket *so) | |
214 | { | |
215 | struct rawcb *rp = sotorawcb(so); | |
216 | int s, error; | |
217 | ||
218 | s = splnet(); | |
219 | if (rp != 0) { | |
220 | switch(rp->rcb_proto.sp_protocol) { | |
221 | case AF_INET: | |
222 | route_cb.ip_count--; | |
223 | break; | |
224 | case AF_INET6: | |
225 | route_cb.ip6_count--; | |
226 | break; | |
227 | case AF_IPX: | |
228 | route_cb.ipx_count--; | |
229 | break; | |
230 | case AF_NS: | |
231 | route_cb.ns_count--; | |
232 | break; | |
1c79356b A |
233 | } |
234 | route_cb.any_count--; | |
235 | } | |
236 | error = raw_usrreqs.pru_detach(so); | |
237 | splx(s); | |
238 | return error; | |
239 | } | |
240 | ||
241 | static int | |
242 | rts_disconnect(struct socket *so) | |
243 | { | |
244 | int s, error; | |
245 | s = splnet(); | |
246 | error = raw_usrreqs.pru_disconnect(so); | |
247 | splx(s); | |
248 | return error; | |
249 | } | |
250 | ||
251 | /* pru_listen is EOPNOTSUPP */ | |
252 | ||
253 | static int | |
254 | rts_peeraddr(struct socket *so, struct sockaddr **nam) | |
255 | { | |
256 | int s, error; | |
257 | s = splnet(); | |
258 | error = raw_usrreqs.pru_peeraddr(so, nam); | |
259 | splx(s); | |
260 | return error; | |
261 | } | |
262 | ||
263 | /* pru_rcvd is EOPNOTSUPP */ | |
264 | /* pru_rcvoob is EOPNOTSUPP */ | |
265 | ||
266 | static int | |
267 | rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, | |
268 | struct mbuf *control, struct proc *p) | |
269 | { | |
270 | int s, error; | |
271 | s = splnet(); | |
272 | error = raw_usrreqs.pru_send(so, flags, m, nam, control, p); | |
273 | splx(s); | |
274 | return error; | |
275 | } | |
276 | ||
277 | /* pru_sense is null */ | |
278 | ||
279 | static int | |
280 | rts_shutdown(struct socket *so) | |
281 | { | |
282 | int s, error; | |
283 | s = splnet(); | |
284 | error = raw_usrreqs.pru_shutdown(so); | |
285 | splx(s); | |
286 | return error; | |
287 | } | |
288 | ||
289 | static int | |
290 | rts_sockaddr(struct socket *so, struct sockaddr **nam) | |
291 | { | |
292 | int s, error; | |
293 | s = splnet(); | |
294 | error = raw_usrreqs.pru_sockaddr(so, nam); | |
295 | splx(s); | |
296 | return error; | |
297 | } | |
298 | ||
299 | static struct pr_usrreqs route_usrreqs = { | |
91447636 A |
300 | rts_abort, pru_accept_notsupp, rts_attach, rts_bind, |
301 | rts_connect, pru_connect2_notsupp, pru_control_notsupp, | |
302 | rts_detach, rts_disconnect, pru_listen_notsupp, rts_peeraddr, | |
303 | pru_rcvd_notsupp, pru_rcvoob_notsupp, rts_send, pru_sense_null, | |
304 | rts_shutdown, rts_sockaddr, sosend, soreceive, pru_sopoll_notsupp | |
1c79356b A |
305 | }; |
306 | ||
307 | /*ARGSUSED*/ | |
308 | static int | |
309 | route_output(m, so) | |
91447636 | 310 | struct mbuf *m; |
1c79356b A |
311 | struct socket *so; |
312 | { | |
91447636 A |
313 | struct rt_msghdr *rtm = 0; |
314 | struct rtentry *rt = 0; | |
1c79356b A |
315 | struct rtentry *saved_nrt = 0; |
316 | struct radix_node_head *rnh; | |
317 | struct rt_addrinfo info; | |
318 | int len, error = 0; | |
319 | struct ifnet *ifp = 0; | |
91447636 | 320 | #ifndef __APPLE__ |
1c79356b | 321 | struct proc *curproc = current_proc(); |
91447636 | 322 | #endif |
55e303ae | 323 | int sendonlytoself = 0; |
1c79356b A |
324 | |
325 | #define senderr(e) { error = e; goto flush;} | |
91447636 | 326 | if (m == 0 || ((m->m_len < sizeof(long)) && (m = m_pullup(m, sizeof(long))) == 0)) |
1c79356b A |
327 | return (ENOBUFS); |
328 | if ((m->m_flags & M_PKTHDR) == 0) | |
329 | panic("route_output"); | |
91447636 A |
330 | |
331 | /* unlock the socket (but keep a reference) it won't be accessed until raw_input appends to it. */ | |
332 | socket_unlock(so, 0); | |
333 | lck_mtx_lock(rt_mtx); | |
334 | ||
1c79356b A |
335 | len = m->m_pkthdr.len; |
336 | if (len < sizeof(*rtm) || | |
337 | len != mtod(m, struct rt_msghdr *)->rtm_msglen) { | |
338 | dst = 0; | |
339 | senderr(EINVAL); | |
340 | } | |
341 | R_Malloc(rtm, struct rt_msghdr *, len); | |
342 | if (rtm == 0) { | |
343 | dst = 0; | |
344 | senderr(ENOBUFS); | |
345 | } | |
346 | m_copydata(m, 0, len, (caddr_t)rtm); | |
347 | if (rtm->rtm_version != RTM_VERSION) { | |
348 | dst = 0; | |
349 | senderr(EPROTONOSUPPORT); | |
350 | } | |
55e303ae A |
351 | |
352 | /* | |
353 | * Silent version of RTM_GET for Reachabiltiy APIs. We may change | |
354 | * all RTM_GETs to be silent in the future, so this is private for now. | |
355 | */ | |
356 | if (rtm->rtm_type == RTM_GET_SILENT) { | |
357 | if ((so->so_options & SO_USELOOPBACK) == 0) | |
358 | senderr(EINVAL); | |
359 | sendonlytoself = 1; | |
360 | rtm->rtm_type = RTM_GET; | |
361 | } | |
362 | ||
363 | /* | |
364 | * Perform permission checking, only privileged sockets | |
365 | * may perform operations other than RTM_GET | |
366 | */ | |
367 | if (rtm->rtm_type != RTM_GET && (so->so_state & SS_PRIV) == 0) { | |
368 | dst = 0; | |
369 | senderr(EPERM); | |
370 | } | |
91447636 A |
371 | |
372 | rtm->rtm_pid = proc_selfpid(); | |
1c79356b A |
373 | info.rti_addrs = rtm->rtm_addrs; |
374 | if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { | |
375 | dst = 0; | |
376 | senderr(EINVAL); | |
377 | } | |
378 | if (dst == 0 || (dst->sa_family >= AF_MAX) | |
91447636 | 379 | || (gate != 0 && (gate->sa_family >= AF_MAX))) { |
1c79356b | 380 | senderr(EINVAL); |
91447636 | 381 | } |
1c79356b A |
382 | if (genmask) { |
383 | struct radix_node *t; | |
384 | t = rn_addmask((caddr_t)genmask, 0, 1); | |
385 | if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0) | |
386 | genmask = (struct sockaddr *)(t->rn_key); | |
387 | else | |
388 | senderr(ENOBUFS); | |
389 | } | |
390 | switch (rtm->rtm_type) { | |
91447636 A |
391 | |
392 | case RTM_ADD: | |
393 | if (gate == 0) | |
394 | senderr(EINVAL); | |
9bccf70c A |
395 | |
396 | #ifdef __APPLE__ | |
397 | /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954) | |
398 | * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel | |
399 | * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point. | |
400 | * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS. | |
401 | * That circular route was ignored on previous version of MacOS X because of a routing bug | |
402 | * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED | |
403 | * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is | |
404 | * made in order to provide binary compatibility with AOL. | |
405 | * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of | |
406 | * confusing the routing table with a wrong route to the previous default gateway | |
407 | */ | |
408 | { | |
91447636 | 409 | extern int check_routeselfref; |
9bccf70c | 410 | #define satosinaddr(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr) |
91447636 A |
411 | |
412 | if (check_routeselfref && (dst && dst->sa_family == AF_INET) && | |
413 | (netmask && satosinaddr(netmask) == INADDR_BROADCAST) && | |
414 | (gate && satosinaddr(dst) == satosinaddr(gate))) { | |
415 | log(LOG_WARNING, "route_output: circular route %ld.%ld.%ld.%ld/32 ignored\n", | |
416 | (ntohl(satosinaddr(gate)>>24))&0xff, | |
417 | (ntohl(satosinaddr(gate)>>16))&0xff, | |
418 | (ntohl(satosinaddr(gate)>>8))&0xff, | |
419 | (ntohl(satosinaddr(gate)))&0xff); | |
420 | ||
421 | senderr(EINVAL); | |
422 | } | |
9bccf70c A |
423 | } |
424 | #endif | |
91447636 A |
425 | error = rtrequest_locked(RTM_ADD, dst, gate, netmask, |
426 | rtm->rtm_flags, &saved_nrt); | |
427 | if (error == 0 && saved_nrt) { | |
9bccf70c | 428 | #ifdef __APPLE__ |
91447636 A |
429 | /* |
430 | * If the route request specified an interface with | |
431 | * IFA and/or IFP, we set the requested interface on | |
432 | * the route with rt_setif. It would be much better | |
433 | * to do this inside rtrequest, but that would | |
434 | * require passing the desired interface, in some | |
435 | * form, to rtrequest. Since rtrequest is called in | |
436 | * so many places (roughly 40 in our source), adding | |
437 | * a parameter is to much for us to swallow; this is | |
438 | * something for the FreeBSD developers to tackle. | |
439 | * Instead, we let rtrequest compute whatever | |
440 | * interface it wants, then come in behind it and | |
441 | * stick in the interface that we really want. This | |
442 | * works reasonably well except when rtrequest can't | |
443 | * figure out what interface to use (with | |
444 | * ifa_withroute) and returns ENETUNREACH. Ideally | |
445 | * it shouldn't matter if rtrequest can't figure out | |
446 | * the interface if we're going to explicitly set it | |
447 | * ourselves anyway. But practically we can't | |
448 | * recover here because rtrequest will not do any of | |
449 | * the work necessary to add the route if it can't | |
450 | * find an interface. As long as there is a default | |
451 | * route that leads to some interface, rtrequest will | |
452 | * find an interface, so this problem should be | |
453 | * rarely encountered. | |
454 | * dwiggins@bbn.com | |
455 | */ | |
456 | ||
457 | rt_setif(saved_nrt, ifpaddr, ifaaddr, gate); | |
9bccf70c | 458 | #endif |
91447636 A |
459 | rt_setmetrics(rtm->rtm_inits, |
460 | &rtm->rtm_rmx, &saved_nrt->rt_rmx); | |
461 | saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); | |
462 | saved_nrt->rt_rmx.rmx_locks |= | |
463 | (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); | |
464 | rtunref(saved_nrt); | |
465 | saved_nrt->rt_genmask = genmask; | |
1c79356b | 466 | } |
91447636 A |
467 | break; |
468 | ||
469 | case RTM_DELETE: | |
470 | error = rtrequest_locked(RTM_DELETE, dst, gate, netmask, | |
471 | rtm->rtm_flags, &saved_nrt); | |
472 | if (error == 0) { | |
473 | if ((rt = saved_nrt)) | |
474 | rtref(rt); | |
475 | goto report; | |
1c79356b | 476 | } |
1c79356b A |
477 | break; |
478 | ||
91447636 | 479 | case RTM_GET: |
1c79356b | 480 | case RTM_CHANGE: |
91447636 A |
481 | case RTM_LOCK: |
482 | if ((rnh = rt_tables[dst->sa_family]) == 0) { | |
483 | senderr(EAFNOSUPPORT); | |
484 | } else if ((rt = (struct rtentry *) | |
485 | rnh->rnh_lookup(dst, netmask, rnh)) != NULL) | |
486 | rtref(rt); | |
487 | else | |
488 | senderr(ESRCH); | |
489 | switch(rtm->rtm_type) { | |
490 | ||
491 | case RTM_GET: { | |
492 | struct ifaddr *ifa2; | |
493 | report: | |
494 | dst = rt_key(rt); | |
495 | gate = rt->rt_gateway; | |
496 | netmask = rt_mask(rt); | |
497 | genmask = rt->rt_genmask; | |
498 | if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { | |
499 | ifp = rt->rt_ifp; | |
500 | if (ifp) { | |
501 | ifnet_lock_shared(ifp); | |
502 | ifa2 = ifp->if_addrhead.tqh_first; | |
503 | ifpaddr = ifa2->ifa_addr; | |
504 | ifnet_lock_done(ifp); | |
505 | ifaaddr = rt->rt_ifa->ifa_addr; | |
506 | rtm->rtm_index = ifp->if_index; | |
507 | } else { | |
508 | ifpaddr = 0; | |
509 | ifaaddr = 0; | |
510 | } | |
511 | } | |
512 | len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0, | |
513 | (struct walkarg *)0); | |
514 | if (len > rtm->rtm_msglen) { | |
515 | struct rt_msghdr *new_rtm; | |
516 | R_Malloc(new_rtm, struct rt_msghdr *, len); | |
517 | if (new_rtm == 0) { | |
518 | senderr(ENOBUFS); | |
519 | } | |
520 | Bcopy(rtm, new_rtm, rtm->rtm_msglen); | |
521 | R_Free(rtm); rtm = new_rtm; | |
522 | } | |
523 | (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, | |
524 | (struct walkarg *)0); | |
525 | rtm->rtm_flags = rt->rt_flags; | |
526 | rtm->rtm_rmx = rt->rt_rmx; | |
527 | rtm->rtm_addrs = info.rti_addrs; | |
528 | } | |
529 | break; | |
530 | ||
531 | case RTM_CHANGE: | |
532 | if (gate && (error = rt_setgate(rt, rt_key(rt), gate))) | |
533 | senderr(error); | |
534 | ||
535 | /* | |
536 | * If they tried to change things but didn't specify | |
537 | * the required gateway, then just use the old one. | |
538 | * This can happen if the user tries to change the | |
539 | * flags on the default route without changing the | |
540 | * default gateway. Changing flags still doesn't work. | |
541 | */ | |
542 | if ((rt->rt_flags & RTF_GATEWAY) && !gate) | |
543 | gate = rt->rt_gateway; | |
544 | ||
9bccf70c | 545 | #ifdef __APPLE__ |
91447636 A |
546 | /* |
547 | * On Darwin, we call rt_setif which contains the | |
548 | * equivalent to the code found at this very spot | |
549 | * in BSD. | |
550 | */ | |
551 | rt_setif(rt, ifpaddr, ifaaddr, gate); | |
9bccf70c | 552 | #endif |
91447636 A |
553 | |
554 | rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, | |
555 | &rt->rt_rmx); | |
9bccf70c | 556 | #ifndef __APPLE__ |
91447636 A |
557 | /* rt_setif, called above does this for us on darwin */ |
558 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) | |
559 | rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate); | |
9bccf70c | 560 | #endif |
91447636 A |
561 | if (genmask) |
562 | rt->rt_genmask = genmask; | |
563 | /* | |
564 | * Fall into | |
565 | */ | |
566 | case RTM_LOCK: | |
567 | rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); | |
568 | rt->rt_rmx.rmx_locks |= | |
569 | (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); | |
570 | break; | |
571 | } | |
1c79356b | 572 | break; |
91447636 A |
573 | |
574 | default: | |
575 | senderr(EOPNOTSUPP); | |
1c79356b | 576 | } |
1c79356b A |
577 | flush: |
578 | if (rtm) { | |
579 | if (error) | |
580 | rtm->rtm_errno = error; | |
581 | else | |
582 | rtm->rtm_flags |= RTF_DONE; | |
583 | } | |
584 | if (rt) | |
91447636 A |
585 | rtfree_locked(rt); |
586 | lck_mtx_unlock(rt_mtx); | |
587 | socket_lock(so, 0); /* relock the socket now */ | |
1c79356b | 588 | { |
91447636 | 589 | struct rawcb *rp = 0; |
1c79356b A |
590 | /* |
591 | * Check to see if we don't want our own messages. | |
592 | */ | |
593 | if ((so->so_options & SO_USELOOPBACK) == 0) { | |
594 | if (route_cb.any_count <= 1) { | |
595 | if (rtm) | |
91447636 | 596 | R_Free(rtm); |
1c79356b A |
597 | m_freem(m); |
598 | return (error); | |
599 | } | |
600 | /* There is another listener, so construct message */ | |
601 | rp = sotorawcb(so); | |
602 | } | |
603 | if (rtm) { | |
604 | m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); | |
9bccf70c A |
605 | if (m->m_pkthdr.len < rtm->rtm_msglen) { |
606 | m_freem(m); | |
607 | m = NULL; | |
608 | } else if (m->m_pkthdr.len > rtm->rtm_msglen) | |
609 | m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); | |
91447636 | 610 | R_Free(rtm); |
1c79356b | 611 | } |
55e303ae | 612 | if (sendonlytoself && m) { |
91447636 A |
613 | error = 0; |
614 | if (sbappendaddr(&so->so_rcv, &route_src, m, (struct mbuf*)0, &error) != 0) { | |
55e303ae A |
615 | sorwakeup(so); |
616 | } | |
91447636 A |
617 | if (error) |
618 | return error; | |
55e303ae A |
619 | } else { |
620 | if (rp) | |
621 | rp->rcb_proto.sp_family = 0; /* Avoid us */ | |
622 | if (dst) | |
623 | route_proto.sp_protocol = dst->sa_family; | |
91447636 A |
624 | if (m) { |
625 | socket_unlock(so, 0); | |
55e303ae | 626 | raw_input(m, &route_proto, &route_src, &route_dst); |
91447636 A |
627 | socket_lock(so, 0); |
628 | } | |
55e303ae A |
629 | if (rp) |
630 | rp->rcb_proto.sp_family = PF_ROUTE; | |
631 | } | |
632 | } | |
1c79356b A |
633 | return (error); |
634 | } | |
635 | ||
636 | static void | |
637 | rt_setmetrics(which, in, out) | |
638 | u_long which; | |
91447636 | 639 | struct rt_metrics *in, *out; |
1c79356b A |
640 | { |
641 | #define metric(f, e) if (which & (f)) out->e = in->e; | |
642 | metric(RTV_RPIPE, rmx_recvpipe); | |
643 | metric(RTV_SPIPE, rmx_sendpipe); | |
644 | metric(RTV_SSTHRESH, rmx_ssthresh); | |
645 | metric(RTV_RTT, rmx_rtt); | |
646 | metric(RTV_RTTVAR, rmx_rttvar); | |
647 | metric(RTV_HOPCOUNT, rmx_hopcount); | |
648 | metric(RTV_MTU, rmx_mtu); | |
649 | metric(RTV_EXPIRE, rmx_expire); | |
650 | #undef metric | |
651 | } | |
652 | ||
653 | /* | |
654 | * Set route's interface given ifpaddr, ifaaddr, and gateway. | |
655 | */ | |
656 | static void | |
91447636 A |
657 | rt_setif( |
658 | struct rtentry *rt, | |
659 | struct sockaddr *Ifpaddr, | |
660 | struct sockaddr *Ifaaddr, | |
661 | struct sockaddr *Gate) | |
1c79356b A |
662 | { |
663 | struct ifaddr *ifa = 0; | |
664 | struct ifnet *ifp = 0; | |
665 | ||
91447636 A |
666 | lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED); |
667 | ||
1c79356b A |
668 | /* new gateway could require new ifaddr, ifp; |
669 | flags may also be different; ifp may be specified | |
670 | by ll sockaddr when protocol address is ambiguous */ | |
671 | if (Ifpaddr && (ifa = ifa_ifwithnet(Ifpaddr)) && | |
91447636 A |
672 | (ifp = ifa->ifa_ifp) && (Ifaaddr || Gate)) { |
673 | ifafree(ifa); | |
1c79356b A |
674 | ifa = ifaof_ifpforaddr(Ifaaddr ? Ifaaddr : Gate, |
675 | ifp); | |
1c79356b | 676 | } |
91447636 A |
677 | else |
678 | { | |
679 | if (ifa) { | |
680 | ifafree(ifa); | |
681 | ifa = 0; | |
682 | } | |
683 | if (Ifpaddr && (ifp = if_withname(Ifpaddr)) ) { | |
684 | if (Gate) { | |
685 | ifa = ifaof_ifpforaddr(Gate, ifp); | |
686 | } | |
687 | else { | |
688 | ifnet_lock_shared(ifp); | |
689 | ifa = TAILQ_FIRST(&ifp->if_addrhead); | |
690 | ifaref(ifa); | |
691 | ifnet_lock_done(ifp); | |
692 | } | |
693 | } | |
694 | else if (Ifaaddr && (ifa = ifa_ifwithaddr(Ifaaddr))) { | |
695 | ifp = ifa->ifa_ifp; | |
696 | } | |
697 | else if (Gate && (ifa = ifa_ifwithroute(rt->rt_flags, | |
698 | rt_key(rt), Gate))) { | |
699 | ifp = ifa->ifa_ifp; | |
700 | } | |
701 | } | |
1c79356b | 702 | if (ifa) { |
91447636 | 703 | struct ifaddr *oifa = rt->rt_ifa; |
1c79356b A |
704 | if (oifa != ifa) { |
705 | if (oifa && oifa->ifa_rtrequest) | |
706 | oifa->ifa_rtrequest(RTM_DELETE, | |
707 | rt, Gate); | |
9bccf70c | 708 | rtsetifa(rt, ifa); |
1c79356b A |
709 | rt->rt_ifp = ifp; |
710 | rt->rt_rmx.rmx_mtu = ifp->if_mtu; | |
711 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) | |
9bccf70c | 712 | rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, Gate); |
91447636 A |
713 | } else { |
714 | ifafree(ifa); | |
1c79356b | 715 | goto call_ifareq; |
91447636 A |
716 | } |
717 | ifafree(ifa); | |
1c79356b A |
718 | return; |
719 | } | |
720 | call_ifareq: | |
721 | /* XXX: to reset gateway to correct value, at RTM_CHANGE */ | |
722 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) | |
723 | rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, Gate); | |
724 | } | |
725 | ||
726 | ||
727 | #define ROUNDUP(a) \ | |
728 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) | |
729 | #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) | |
730 | ||
731 | ||
732 | /* | |
733 | * Extract the addresses of the passed sockaddrs. | |
734 | * Do a little sanity checking so as to avoid bad memory references. | |
735 | * This data is derived straight from userland. | |
736 | */ | |
737 | static int | |
738 | rt_xaddrs(cp, cplim, rtinfo) | |
91447636 A |
739 | caddr_t cp, cplim; |
740 | struct rt_addrinfo *rtinfo; | |
1c79356b | 741 | { |
91447636 A |
742 | struct sockaddr *sa; |
743 | int i; | |
1c79356b A |
744 | |
745 | bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); | |
746 | for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { | |
747 | if ((rtinfo->rti_addrs & (1 << i)) == 0) | |
748 | continue; | |
749 | sa = (struct sockaddr *)cp; | |
750 | /* | |
751 | * It won't fit. | |
752 | */ | |
753 | if ( (cp + sa->sa_len) > cplim ) { | |
754 | return (EINVAL); | |
755 | } | |
756 | ||
757 | /* | |
758 | * there are no more.. quit now | |
759 | * If there are more bits, they are in error. | |
760 | * I've seen this. route(1) can evidently generate these. | |
761 | * This causes kernel to core dump. | |
762 | * for compatibility, If we see this, point to a safe address. | |
763 | */ | |
764 | if (sa->sa_len == 0) { | |
765 | rtinfo->rti_info[i] = &sa_zero; | |
766 | return (0); /* should be EINVAL but for compat */ | |
767 | } | |
768 | ||
769 | /* accept it */ | |
770 | rtinfo->rti_info[i] = sa; | |
771 | ADVANCE(cp, sa); | |
772 | } | |
773 | return (0); | |
774 | } | |
775 | ||
776 | static struct mbuf * | |
91447636 A |
777 | rt_msg1( |
778 | int type, | |
779 | struct rt_addrinfo *rtinfo) | |
1c79356b | 780 | { |
91447636 A |
781 | struct rt_msghdr *rtm; |
782 | struct mbuf *m; | |
783 | int i; | |
784 | struct sockaddr *sa; | |
1c79356b A |
785 | int len, dlen; |
786 | ||
1c79356b A |
787 | switch (type) { |
788 | ||
789 | case RTM_DELADDR: | |
790 | case RTM_NEWADDR: | |
791 | len = sizeof(struct ifa_msghdr); | |
792 | break; | |
793 | ||
794 | case RTM_DELMADDR: | |
795 | case RTM_NEWMADDR: | |
796 | len = sizeof(struct ifma_msghdr); | |
797 | break; | |
798 | ||
799 | case RTM_IFINFO: | |
800 | len = sizeof(struct if_msghdr); | |
801 | break; | |
802 | ||
803 | default: | |
804 | len = sizeof(struct rt_msghdr); | |
805 | } | |
9bccf70c | 806 | if (len > MCLBYTES) |
1c79356b | 807 | panic("rt_msg1"); |
9bccf70c A |
808 | m = m_gethdr(M_DONTWAIT, MT_DATA); |
809 | if (m && len > MHLEN) { | |
810 | MCLGET(m, M_DONTWAIT); | |
811 | if ((m->m_flags & M_EXT) == 0) { | |
812 | m_free(m); | |
813 | m = NULL; | |
814 | } | |
815 | } | |
816 | if (m == 0) | |
817 | return (m); | |
1c79356b A |
818 | m->m_pkthdr.len = m->m_len = len; |
819 | m->m_pkthdr.rcvif = 0; | |
820 | rtm = mtod(m, struct rt_msghdr *); | |
821 | bzero((caddr_t)rtm, len); | |
822 | for (i = 0; i < RTAX_MAX; i++) { | |
823 | if ((sa = rtinfo->rti_info[i]) == NULL) | |
824 | continue; | |
825 | rtinfo->rti_addrs |= (1 << i); | |
826 | dlen = ROUNDUP(sa->sa_len); | |
827 | m_copyback(m, len, dlen, (caddr_t)sa); | |
828 | len += dlen; | |
829 | } | |
830 | if (m->m_pkthdr.len != len) { | |
831 | m_freem(m); | |
832 | return (NULL); | |
833 | } | |
834 | rtm->rtm_msglen = len; | |
835 | rtm->rtm_version = RTM_VERSION; | |
836 | rtm->rtm_type = type; | |
837 | return (m); | |
838 | } | |
839 | ||
840 | static int | |
841 | rt_msg2(type, rtinfo, cp, w) | |
842 | int type; | |
91447636 | 843 | struct rt_addrinfo *rtinfo; |
1c79356b A |
844 | caddr_t cp; |
845 | struct walkarg *w; | |
846 | { | |
91447636 | 847 | int i; |
1c79356b A |
848 | int len, dlen, second_time = 0; |
849 | caddr_t cp0; | |
850 | ||
851 | rtinfo->rti_addrs = 0; | |
852 | again: | |
853 | switch (type) { | |
854 | ||
855 | case RTM_DELADDR: | |
856 | case RTM_NEWADDR: | |
857 | len = sizeof(struct ifa_msghdr); | |
858 | break; | |
859 | ||
91447636 A |
860 | case RTM_DELMADDR: |
861 | case RTM_NEWMADDR: | |
862 | len = sizeof(struct ifma_msghdr); | |
863 | break; | |
864 | ||
1c79356b A |
865 | case RTM_IFINFO: |
866 | len = sizeof(struct if_msghdr); | |
867 | break; | |
868 | ||
91447636 A |
869 | case RTM_IFINFO2: |
870 | len = sizeof(struct if_msghdr2); | |
871 | break; | |
872 | ||
873 | case RTM_NEWMADDR2: | |
874 | len = sizeof(struct ifma_msghdr2); | |
875 | break; | |
876 | ||
877 | case RTM_GET2: | |
878 | len = sizeof(struct rt_msghdr2); | |
879 | break; | |
880 | ||
1c79356b A |
881 | default: |
882 | len = sizeof(struct rt_msghdr); | |
883 | } | |
884 | cp0 = cp; | |
885 | if (cp0) | |
886 | cp += len; | |
887 | for (i = 0; i < RTAX_MAX; i++) { | |
91447636 | 888 | struct sockaddr *sa; |
1c79356b A |
889 | |
890 | if ((sa = rtinfo->rti_info[i]) == 0) | |
891 | continue; | |
892 | rtinfo->rti_addrs |= (1 << i); | |
893 | dlen = ROUNDUP(sa->sa_len); | |
894 | if (cp) { | |
895 | bcopy((caddr_t)sa, cp, (unsigned)dlen); | |
896 | cp += dlen; | |
897 | } | |
898 | len += dlen; | |
899 | } | |
900 | if (cp == 0 && w != NULL && !second_time) { | |
91447636 | 901 | struct walkarg *rw = w; |
1c79356b A |
902 | |
903 | if (rw->w_req) { | |
904 | if (rw->w_tmemsize < len) { | |
905 | if (rw->w_tmem) | |
906 | FREE(rw->w_tmem, M_RTABLE); | |
907 | rw->w_tmem = (caddr_t) | |
0b4e3aa0 | 908 | _MALLOC(len, M_RTABLE, M_WAITOK); /*###LD0412 was NOWAIT */ |
1c79356b A |
909 | if (rw->w_tmem) |
910 | rw->w_tmemsize = len; | |
911 | } | |
912 | if (rw->w_tmem) { | |
913 | cp = rw->w_tmem; | |
914 | second_time = 1; | |
915 | goto again; | |
916 | } | |
917 | } | |
918 | } | |
919 | if (cp) { | |
91447636 | 920 | struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; |
1c79356b A |
921 | |
922 | rtm->rtm_version = RTM_VERSION; | |
923 | rtm->rtm_type = type; | |
924 | rtm->rtm_msglen = len; | |
925 | } | |
926 | return (len); | |
927 | } | |
928 | ||
929 | /* | |
930 | * This routine is called to generate a message from the routing | |
91447636 | 931 | * socket indicating that a redirect has occurred, a routing lookup |
1c79356b A |
932 | * has failed, or that a protocol has detected timeouts to a particular |
933 | * destination. | |
934 | */ | |
935 | void | |
936 | rt_missmsg(type, rtinfo, flags, error) | |
937 | int type, flags, error; | |
91447636 | 938 | struct rt_addrinfo *rtinfo; |
1c79356b | 939 | { |
91447636 A |
940 | struct rt_msghdr *rtm; |
941 | struct mbuf *m; | |
1c79356b A |
942 | struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; |
943 | ||
91447636 A |
944 | lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED); |
945 | ||
1c79356b A |
946 | if (route_cb.any_count == 0) |
947 | return; | |
948 | m = rt_msg1(type, rtinfo); | |
949 | if (m == 0) | |
950 | return; | |
951 | rtm = mtod(m, struct rt_msghdr *); | |
952 | rtm->rtm_flags = RTF_DONE | flags; | |
953 | rtm->rtm_errno = error; | |
954 | rtm->rtm_addrs = rtinfo->rti_addrs; | |
955 | route_proto.sp_protocol = sa ? sa->sa_family : 0; | |
956 | raw_input(m, &route_proto, &route_src, &route_dst); | |
957 | } | |
958 | ||
959 | /* | |
960 | * This routine is called to generate a message from the routing | |
961 | * socket indicating that the status of a network interface has changed. | |
962 | */ | |
963 | void | |
91447636 A |
964 | rt_ifmsg( |
965 | struct ifnet *ifp) | |
1c79356b | 966 | { |
91447636 | 967 | struct if_msghdr *ifm; |
1c79356b A |
968 | struct mbuf *m; |
969 | struct rt_addrinfo info; | |
970 | ||
971 | if (route_cb.any_count == 0) | |
972 | return; | |
973 | bzero((caddr_t)&info, sizeof(info)); | |
974 | m = rt_msg1(RTM_IFINFO, &info); | |
975 | if (m == 0) | |
976 | return; | |
977 | ifm = mtod(m, struct if_msghdr *); | |
978 | ifm->ifm_index = ifp->if_index; | |
979 | ifm->ifm_flags = (u_short)ifp->if_flags; | |
91447636 | 980 | if_data_internal_to_if_data(&ifp->if_data, &ifm->ifm_data); |
1c79356b A |
981 | ifm->ifm_addrs = 0; |
982 | route_proto.sp_protocol = 0; | |
983 | raw_input(m, &route_proto, &route_src, &route_dst); | |
984 | } | |
985 | ||
986 | /* | |
987 | * This is called to generate messages from the routing socket | |
988 | * indicating a network interface has had addresses associated with it. | |
989 | * if we ever reverse the logic and replace messages TO the routing | |
990 | * socket indicate a request to configure interfaces, then it will | |
991 | * be unnecessary as the routing socket will automatically generate | |
992 | * copies of it. | |
91447636 A |
993 | * |
994 | * Since this is coming from the interface, it is expected that the | |
995 | * interface will be locked. | |
1c79356b A |
996 | */ |
997 | void | |
998 | rt_newaddrmsg(cmd, ifa, error, rt) | |
999 | int cmd, error; | |
91447636 A |
1000 | struct ifaddr *ifa; |
1001 | struct rtentry *rt; | |
1c79356b A |
1002 | { |
1003 | struct rt_addrinfo info; | |
1004 | struct sockaddr *sa = 0; | |
1005 | int pass; | |
1006 | struct mbuf *m = 0; | |
1007 | struct ifnet *ifp = ifa->ifa_ifp; | |
1008 | ||
1009 | if (route_cb.any_count == 0) | |
1010 | return; | |
1011 | for (pass = 1; pass < 3; pass++) { | |
1012 | bzero((caddr_t)&info, sizeof(info)); | |
1013 | if ((cmd == RTM_ADD && pass == 1) || | |
1014 | (cmd == RTM_DELETE && pass == 2)) { | |
91447636 | 1015 | struct ifa_msghdr *ifam; |
1c79356b A |
1016 | int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; |
1017 | ||
1018 | ifaaddr = sa = ifa->ifa_addr; | |
1019 | ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr; | |
1020 | netmask = ifa->ifa_netmask; | |
1021 | brdaddr = ifa->ifa_dstaddr; | |
1022 | if ((m = rt_msg1(ncmd, &info)) == NULL) | |
1023 | continue; | |
1024 | ifam = mtod(m, struct ifa_msghdr *); | |
1025 | ifam->ifam_index = ifp->if_index; | |
1026 | ifam->ifam_metric = ifa->ifa_metric; | |
1027 | ifam->ifam_flags = ifa->ifa_flags; | |
1028 | ifam->ifam_addrs = info.rti_addrs; | |
1029 | } | |
1030 | if ((cmd == RTM_ADD && pass == 2) || | |
1031 | (cmd == RTM_DELETE && pass == 1)) { | |
91447636 | 1032 | struct rt_msghdr *rtm; |
1c79356b A |
1033 | |
1034 | if (rt == 0) | |
1035 | continue; | |
1036 | netmask = rt_mask(rt); | |
1037 | dst = sa = rt_key(rt); | |
1038 | gate = rt->rt_gateway; | |
1039 | if ((m = rt_msg1(cmd, &info)) == NULL) | |
1040 | continue; | |
1041 | rtm = mtod(m, struct rt_msghdr *); | |
1042 | rtm->rtm_index = ifp->if_index; | |
1043 | rtm->rtm_flags |= rt->rt_flags; | |
1044 | rtm->rtm_errno = error; | |
1045 | rtm->rtm_addrs = info.rti_addrs; | |
1046 | } | |
1047 | route_proto.sp_protocol = sa ? sa->sa_family : 0; | |
1048 | raw_input(m, &route_proto, &route_src, &route_dst); | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | /* | |
1053 | * This is the analogue to the rt_newaddrmsg which performs the same | |
1054 | * function but for multicast group memberhips. This is easier since | |
1055 | * there is no route state to worry about. | |
1056 | */ | |
1057 | void | |
1058 | rt_newmaddrmsg(cmd, ifma) | |
1059 | int cmd; | |
1060 | struct ifmultiaddr *ifma; | |
1061 | { | |
1062 | struct rt_addrinfo info; | |
1063 | struct mbuf *m = 0; | |
1064 | struct ifnet *ifp = ifma->ifma_ifp; | |
1065 | struct ifma_msghdr *ifmam; | |
1066 | ||
1067 | if (route_cb.any_count == 0) | |
1068 | return; | |
1069 | ||
1070 | bzero((caddr_t)&info, sizeof(info)); | |
1071 | ifaaddr = ifma->ifma_addr; | |
9bccf70c A |
1072 | if (ifp && ifp->if_addrhead.tqh_first) |
1073 | ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr; | |
1074 | else | |
1075 | ifpaddr = NULL; | |
1c79356b A |
1076 | /* |
1077 | * If a link-layer address is present, present it as a ``gateway'' | |
1078 | * (similarly to how ARP entries, e.g., are presented). | |
1079 | */ | |
91447636 | 1080 | gate = ifma->ifma_ll->ifma_addr; |
1c79356b A |
1081 | if ((m = rt_msg1(cmd, &info)) == NULL) |
1082 | return; | |
1083 | ifmam = mtod(m, struct ifma_msghdr *); | |
91447636 | 1084 | ifmam->ifmam_index = ifp ? ifp->if_index : 0; |
1c79356b A |
1085 | ifmam->ifmam_addrs = info.rti_addrs; |
1086 | route_proto.sp_protocol = ifma->ifma_addr->sa_family; | |
1087 | raw_input(m, &route_proto, &route_src, &route_dst); | |
1088 | } | |
1089 | ||
1090 | /* | |
1091 | * This is used in dumping the kernel table via sysctl(). | |
1092 | */ | |
1093 | int | |
1094 | sysctl_dumpentry(rn, vw) | |
1095 | struct radix_node *rn; | |
1096 | void *vw; | |
1097 | { | |
91447636 A |
1098 | struct walkarg *w = vw; |
1099 | struct rtentry *rt = (struct rtentry *)rn; | |
1c79356b A |
1100 | int error = 0, size; |
1101 | struct rt_addrinfo info; | |
1102 | ||
1103 | if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) | |
1104 | return 0; | |
1105 | bzero((caddr_t)&info, sizeof(info)); | |
1106 | dst = rt_key(rt); | |
1107 | gate = rt->rt_gateway; | |
1108 | netmask = rt_mask(rt); | |
1109 | genmask = rt->rt_genmask; | |
91447636 A |
1110 | if (w->w_op != NET_RT_DUMP2) { |
1111 | size = rt_msg2(RTM_GET, &info, 0, w); | |
1112 | if (w->w_req && w->w_tmem) { | |
1113 | struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; | |
1114 | ||
1115 | rtm->rtm_flags = rt->rt_flags; | |
1116 | rtm->rtm_use = rt->rt_use; | |
1117 | rtm->rtm_rmx = rt->rt_rmx; | |
1118 | rtm->rtm_index = rt->rt_ifp->if_index; | |
1119 | rtm->rtm_pid = 0; | |
1120 | rtm->rtm_seq = 0; | |
1121 | rtm->rtm_errno = 0; | |
1122 | rtm->rtm_addrs = info.rti_addrs; | |
1123 | error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); | |
1124 | return (error); | |
1125 | } | |
1126 | } else { | |
1127 | size = rt_msg2(RTM_GET2, &info, 0, w); | |
1128 | if (w->w_req && w->w_tmem) { | |
1129 | struct rt_msghdr2 *rtm = (struct rt_msghdr2 *)w->w_tmem; | |
1130 | ||
1131 | rtm->rtm_flags = rt->rt_flags; | |
1132 | rtm->rtm_use = rt->rt_use; | |
1133 | rtm->rtm_rmx = rt->rt_rmx; | |
1134 | rtm->rtm_index = rt->rt_ifp->if_index; | |
1135 | rtm->rtm_refcnt = rt->rt_refcnt; | |
1136 | if (rt->rt_parent) | |
1137 | rtm->rtm_parentflags = rt->rt_parent->rt_flags; | |
1138 | else | |
1139 | rtm->rtm_parentflags = 0; | |
1140 | rtm->rtm_reserved = 0; | |
1141 | rtm->rtm_addrs = info.rti_addrs; | |
1142 | error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); | |
1143 | return (error); | |
1144 | ||
1145 | } | |
1c79356b A |
1146 | } |
1147 | return (error); | |
1148 | } | |
1149 | ||
1150 | int | |
91447636 A |
1151 | sysctl_iflist( |
1152 | int af, | |
1153 | struct walkarg *w) | |
1c79356b | 1154 | { |
91447636 A |
1155 | struct ifnet *ifp; |
1156 | struct ifaddr *ifa; | |
1c79356b A |
1157 | struct rt_addrinfo info; |
1158 | int len, error = 0; | |
1159 | ||
1160 | bzero((caddr_t)&info, sizeof(info)); | |
91447636 A |
1161 | ifnet_head_lock_shared(); |
1162 | TAILQ_FOREACH(ifp, &ifnet_head, if_link) { | |
1163 | if (error) | |
1164 | break; | |
1c79356b A |
1165 | if (w->w_arg && w->w_arg != ifp->if_index) |
1166 | continue; | |
91447636 | 1167 | ifnet_lock_shared(ifp); |
1c79356b A |
1168 | ifa = ifp->if_addrhead.tqh_first; |
1169 | ifpaddr = ifa->ifa_addr; | |
1170 | len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w); | |
1171 | ifpaddr = 0; | |
1172 | if (w->w_req && w->w_tmem) { | |
91447636 | 1173 | struct if_msghdr *ifm; |
1c79356b A |
1174 | |
1175 | ifm = (struct if_msghdr *)w->w_tmem; | |
1176 | ifm->ifm_index = ifp->if_index; | |
1177 | ifm->ifm_flags = (u_short)ifp->if_flags; | |
91447636 | 1178 | if_data_internal_to_if_data(&ifp->if_data, &ifm->ifm_data); |
1c79356b A |
1179 | ifm->ifm_addrs = info.rti_addrs; |
1180 | error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); | |
91447636 A |
1181 | if (error) { |
1182 | ifnet_lock_done(ifp); | |
1183 | break; | |
1184 | } | |
1c79356b A |
1185 | } |
1186 | while ((ifa = ifa->ifa_link.tqe_next) != 0) { | |
1187 | if (af && af != ifa->ifa_addr->sa_family) | |
1188 | continue; | |
9bccf70c A |
1189 | #ifndef __APPLE__ |
1190 | if (curproc->p_prison && prison_if(curproc, ifa->ifa_addr)) | |
1191 | continue; | |
1192 | #endif | |
1c79356b A |
1193 | ifaaddr = ifa->ifa_addr; |
1194 | netmask = ifa->ifa_netmask; | |
1195 | brdaddr = ifa->ifa_dstaddr; | |
1196 | len = rt_msg2(RTM_NEWADDR, &info, 0, w); | |
1197 | if (w->w_req && w->w_tmem) { | |
91447636 | 1198 | struct ifa_msghdr *ifam; |
1c79356b A |
1199 | |
1200 | ifam = (struct ifa_msghdr *)w->w_tmem; | |
1201 | ifam->ifam_index = ifa->ifa_ifp->if_index; | |
1202 | ifam->ifam_flags = ifa->ifa_flags; | |
1203 | ifam->ifam_metric = ifa->ifa_metric; | |
1204 | ifam->ifam_addrs = info.rti_addrs; | |
1205 | error = SYSCTL_OUT(w->w_req, w->w_tmem, len); | |
1206 | if (error) | |
91447636 | 1207 | break; |
1c79356b A |
1208 | } |
1209 | } | |
91447636 | 1210 | ifnet_lock_done(ifp); |
1c79356b A |
1211 | ifaaddr = netmask = brdaddr = 0; |
1212 | } | |
91447636 A |
1213 | ifnet_head_done(); |
1214 | return error; | |
1c79356b A |
1215 | } |
1216 | ||
91447636 A |
1217 | int |
1218 | sysctl_iflist2( | |
1219 | int af, | |
1220 | struct walkarg *w) | |
1221 | { | |
1222 | struct ifnet *ifp; | |
1223 | struct ifaddr *ifa; | |
1224 | struct rt_addrinfo info; | |
1225 | int len, error = 0; | |
1226 | ||
1227 | bzero((caddr_t)&info, sizeof(info)); | |
1228 | ifnet_head_lock_shared(); | |
1229 | TAILQ_FOREACH(ifp, &ifnet_head, if_link) { | |
1230 | if (error) | |
1231 | break; | |
1232 | if (w->w_arg && w->w_arg != ifp->if_index) | |
1233 | continue; | |
1234 | ifnet_lock_shared(ifp); | |
1235 | ifa = ifp->if_addrhead.tqh_first; | |
1236 | ifpaddr = ifa->ifa_addr; | |
1237 | len = rt_msg2(RTM_IFINFO2, &info, (caddr_t)0, w); | |
1238 | ifpaddr = 0; | |
1239 | if (w->w_req && w->w_tmem) { | |
1240 | struct if_msghdr2 *ifm; | |
1241 | ||
1242 | ifm = (struct if_msghdr2 *)w->w_tmem; | |
1243 | ifm->ifm_addrs = info.rti_addrs; | |
1244 | ifm->ifm_flags = (u_short)ifp->if_flags; | |
1245 | ifm->ifm_index = ifp->if_index; | |
1246 | ifm->ifm_snd_len = ifp->if_snd.ifq_len; | |
1247 | ifm->ifm_snd_maxlen = ifp->if_snd.ifq_maxlen; | |
1248 | ifm->ifm_snd_drops = ifp->if_snd.ifq_drops; | |
1249 | ifm->ifm_timer = ifp->if_timer; | |
1250 | if_data_internal_to_if_data64(&ifp->if_data, &ifm->ifm_data); | |
1251 | error = SYSCTL_OUT(w->w_req, w->w_tmem, len); | |
1252 | if (error) { | |
1253 | ifnet_lock_done(ifp); | |
1254 | break; | |
1255 | } | |
1256 | } | |
1257 | while ((ifa = ifa->ifa_link.tqe_next) != 0) { | |
1258 | if (af && af != ifa->ifa_addr->sa_family) | |
1259 | continue; | |
1260 | ifaaddr = ifa->ifa_addr; | |
1261 | netmask = ifa->ifa_netmask; | |
1262 | brdaddr = ifa->ifa_dstaddr; | |
1263 | len = rt_msg2(RTM_NEWADDR, &info, 0, w); | |
1264 | if (w->w_req && w->w_tmem) { | |
1265 | struct ifa_msghdr *ifam; | |
1266 | ||
1267 | ifam = (struct ifa_msghdr *)w->w_tmem; | |
1268 | ifam->ifam_index = ifa->ifa_ifp->if_index; | |
1269 | ifam->ifam_flags = ifa->ifa_flags; | |
1270 | ifam->ifam_metric = ifa->ifa_metric; | |
1271 | ifam->ifam_addrs = info.rti_addrs; | |
1272 | error = SYSCTL_OUT(w->w_req, w->w_tmem, len); | |
1273 | if (error) | |
1274 | break; | |
1275 | } | |
1276 | } | |
1277 | if (error) { | |
1278 | ifnet_lock_done(ifp); | |
1279 | break; | |
1280 | } | |
1281 | { | |
1282 | struct ifmultiaddr *ifma; | |
1283 | ||
1284 | for (ifma = ifp->if_multiaddrs.lh_first; ifma; | |
1285 | ifma = ifma->ifma_link.le_next) { | |
1286 | if (af && af != ifma->ifma_addr->sa_family) | |
1287 | continue; | |
1288 | bzero((caddr_t)&info, sizeof(info)); | |
1289 | ifaaddr = ifma->ifma_addr; | |
1290 | if (ifp->if_addrhead.tqh_first) | |
1291 | ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr; | |
1292 | if (ifma->ifma_ll) | |
1293 | gate = ifma->ifma_ll->ifma_addr; | |
1294 | len = rt_msg2(RTM_NEWMADDR2, &info, 0, w); | |
1295 | if (w->w_req && w->w_tmem) { | |
1296 | struct ifma_msghdr2 *ifmam; | |
1297 | ||
1298 | ifmam = (struct ifma_msghdr2 *)w->w_tmem; | |
1299 | ifmam->ifmam_addrs = info.rti_addrs; | |
1300 | ifmam->ifmam_flags = 0; | |
1301 | ifmam->ifmam_index = ifma->ifma_ifp->if_index; | |
1302 | ifmam->ifmam_refcount = ifma->ifma_refcount; | |
1303 | error = SYSCTL_OUT(w->w_req, w->w_tmem, len); | |
1304 | if (error) | |
1305 | break; | |
1306 | } | |
1307 | } | |
1308 | } | |
1309 | ifnet_lock_done(ifp); | |
1310 | ifaaddr = netmask = brdaddr = 0; | |
1311 | } | |
1312 | ifnet_head_done(); | |
1313 | return error; | |
1314 | } | |
1315 | ||
1316 | ||
1317 | static int | |
1318 | sysctl_rtstat(struct sysctl_req *req) | |
1319 | { | |
1320 | int error; | |
1321 | ||
1322 | error = SYSCTL_OUT(req, &rtstat, sizeof(struct rtstat)); | |
1323 | if (error) | |
1324 | return (error); | |
1325 | ||
1326 | return 0; | |
1327 | } | |
1328 | ||
1329 | static int | |
1330 | sysctl_rttrash(struct sysctl_req *req) | |
1331 | { | |
1332 | int error; | |
1333 | ||
1334 | error = SYSCTL_OUT(req, &rttrash, sizeof(rttrash)); | |
1335 | if (error) | |
1336 | return (error); | |
1337 | ||
1338 | return 0; | |
1339 | } | |
1340 | ||
1341 | ||
1c79356b A |
1342 | static int |
1343 | sysctl_rtsock SYSCTL_HANDLER_ARGS | |
1344 | { | |
1345 | int *name = (int *)arg1; | |
1346 | u_int namelen = arg2; | |
91447636 A |
1347 | struct radix_node_head *rnh; |
1348 | int i, error = EINVAL; | |
1c79356b A |
1349 | u_char af; |
1350 | struct walkarg w; | |
1351 | ||
1352 | name ++; | |
1353 | namelen--; | |
1354 | if (req->newptr) | |
1355 | return (EPERM); | |
1356 | if (namelen != 3) | |
1357 | return (EINVAL); | |
1358 | af = name[0]; | |
1359 | Bzero(&w, sizeof(w)); | |
1360 | w.w_op = name[1]; | |
1361 | w.w_arg = name[2]; | |
1362 | w.w_req = req; | |
1363 | ||
1c79356b A |
1364 | switch (w.w_op) { |
1365 | ||
1366 | case NET_RT_DUMP: | |
91447636 | 1367 | case NET_RT_DUMP2: |
1c79356b | 1368 | case NET_RT_FLAGS: |
cc9f6e38 | 1369 | lck_mtx_lock(rt_mtx); |
1c79356b A |
1370 | for (i = 1; i <= AF_MAX; i++) |
1371 | if ((rnh = rt_tables[i]) && (af == 0 || af == i) && | |
1372 | (error = rnh->rnh_walktree(rnh, | |
1373 | sysctl_dumpentry, &w))) | |
1374 | break; | |
cc9f6e38 | 1375 | lck_mtx_unlock(rt_mtx); |
1c79356b | 1376 | break; |
1c79356b A |
1377 | case NET_RT_IFLIST: |
1378 | error = sysctl_iflist(af, &w); | |
91447636 A |
1379 | break; |
1380 | case NET_RT_IFLIST2: | |
1381 | error = sysctl_iflist2(af, &w); | |
1382 | break; | |
1383 | case NET_RT_STAT: | |
1384 | error = sysctl_rtstat(req); | |
1385 | break; | |
1386 | case NET_RT_TRASH: | |
1387 | error = sysctl_rttrash(req); | |
1388 | break; | |
1c79356b | 1389 | } |
1c79356b A |
1390 | if (w.w_tmem) |
1391 | FREE(w.w_tmem, M_RTABLE); | |
1392 | return (error); | |
1393 | } | |
1394 | ||
1395 | SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); | |
1396 | ||
1c79356b A |
1397 | /* |
1398 | * Definitions of protocols supported in the ROUTE domain. | |
1399 | */ | |
1400 | ||
1401 | struct domain routedomain; /* or at least forward */ | |
1402 | ||
1c79356b A |
1403 | static struct protosw routesw[] = { |
1404 | { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, | |
9bccf70c | 1405 | 0, route_output, raw_ctlinput, 0, |
1c79356b A |
1406 | 0, |
1407 | raw_init, 0, 0, 0, | |
91447636 A |
1408 | 0, |
1409 | &route_usrreqs, | |
1410 | 0, 0, 0, | |
1411 | { 0, 0 }, 0, { 0 } | |
1c79356b A |
1412 | } |
1413 | }; | |
1414 | ||
1415 | struct domain routedomain = | |
1416 | { PF_ROUTE, "route", route_init, 0, 0, | |
91447636 A |
1417 | routesw, |
1418 | 0, 0, 0, 0, 0, 0, 0, 0, | |
1419 | { 0, 0 } }; | |
1c79356b A |
1420 | |
1421 | DOMAIN_SET(route); | |
1422 |