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