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