]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/rtsock.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / net / rtsock.c
CommitLineData
1c79356b 1/*
c910b4d9 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1988, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)rtsock.c 8.5 (Berkeley) 11/2/94
61 */
62
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/kernel.h>
67#include <sys/sysctl.h>
68#include <sys/proc.h>
69#include <sys/malloc.h>
70#include <sys/mbuf.h>
71#include <sys/socket.h>
72#include <sys/socketvar.h>
73#include <sys/domain.h>
74#include <sys/protosw.h>
9bccf70c 75#include <sys/syslog.h>
91447636 76#include <kern/lock.h>
1c79356b
A
77
78#include <net/if.h>
79#include <net/route.h>
80#include <net/raw_cb.h>
9bccf70c 81#include <netinet/in.h>
1c79356b 82
91447636
A
83#include <machine/spl.h>
84
91447636 85extern struct rtstat rtstat;
2d21ac55 86extern int check_routeselfref;
91447636 87
1c79356b
A
88MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
89
91447636
A
90static struct sockaddr route_dst = { 2, PF_ROUTE, { 0, } };
91static struct sockaddr route_src = { 2, PF_ROUTE, { 0, } };
92static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, { 0, } };
93static struct sockproto route_proto = { PF_ROUTE, 0 };
1c79356b 94
1c79356b
A
95struct walkarg {
96 int w_tmemsize;
97 int w_op, w_arg;
98 caddr_t w_tmem;
99 struct sysctl_req *w_req;
100};
101
b0d623f7 102static struct mbuf *rt_msg1(int, struct rt_addrinfo *);
91447636
A
103static int rt_msg2(int, struct rt_addrinfo *, caddr_t, struct walkarg *);
104static int rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
105static int sysctl_dumpentry(struct radix_node *rn, void *vw);
106static int sysctl_iflist(int af, struct walkarg *w);
107static int sysctl_iflist2(int af, struct walkarg *w);
108static int route_output(struct mbuf *, struct socket *);
b0d623f7 109static void rt_setmetrics(u_int32_t, struct rt_metrics *, struct rt_metrics *);
91447636 110static void rt_setif(struct rtentry *, struct sockaddr *, struct sockaddr *,
c910b4d9
A
111 struct sockaddr *, unsigned int);
112
113#define SIN(sa) ((struct sockaddr_in *)(size_t)(sa))
1c79356b
A
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 */
128static int
129rts_abort(struct socket *so)
130{
91447636
A
131 int error;
132
1c79356b 133 error = raw_usrreqs.pru_abort(so);
1c79356b
A
134 return error;
135}
136
137/* pru_accept is EOPNOTSUPP */
138
139static int
91447636 140rts_attach(struct socket *so, int proto, __unused struct proc *p)
1c79356b
A
141{
142 struct rawcb *rp;
91447636 143 int error;
1c79356b
A
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 */
1c79356b 159 so->so_pcb = (caddr_t)rp;
55e303ae 160 error = raw_attach(so, proto); /* don't use raw_usrreqs.pru_attach, it checks for SS_PRIV */
1c79356b
A
161 rp = sotorawcb(so);
162 if (error) {
1c79356b 163 FREE(rp, M_PCB);
2d21ac55 164 so->so_pcb = NULL;
91447636 165 so->so_flags |= SOF_PCBCLEARING;
1c79356b
A
166 return error;
167 }
37839358 168
1c79356b 169 switch(rp->rcb_proto.sp_protocol) {
91447636 170//####LD route_cb needs looking
1c79356b
A
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;
1c79356b
A
183 }
184 rp->rcb_faddr = &route_src;
185 route_cb.any_count++;
37839358 186 /* the socket is already locked when we enter rts_attach */
1c79356b
A
187 soisconnected(so);
188 so->so_options |= SO_USELOOPBACK;
1c79356b
A
189 return 0;
190}
191
192static int
193rts_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
194{
2d21ac55 195 int error;
1c79356b 196 error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
1c79356b
A
197 return error;
198}
199
200static int
201rts_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
202{
2d21ac55 203 int error;
1c79356b 204 error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
1c79356b
A
205 return error;
206}
207
208/* pru_connect2 is EOPNOTSUPP */
209/* pru_control is EOPNOTSUPP */
210
211static int
212rts_detach(struct socket *so)
213{
214 struct rawcb *rp = sotorawcb(so);
2d21ac55 215 int error;
1c79356b 216
1c79356b
A
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;
1c79356b
A
231 }
232 route_cb.any_count--;
233 }
234 error = raw_usrreqs.pru_detach(so);
1c79356b
A
235 return error;
236}
237
238static int
239rts_disconnect(struct socket *so)
240{
2d21ac55 241 int error;
1c79356b 242 error = raw_usrreqs.pru_disconnect(so);
1c79356b
A
243 return error;
244}
245
246/* pru_listen is EOPNOTSUPP */
247
248static int
249rts_peeraddr(struct socket *so, struct sockaddr **nam)
250{
2d21ac55 251 int error;
1c79356b 252 error = raw_usrreqs.pru_peeraddr(so, nam);
1c79356b
A
253 return error;
254}
255
256/* pru_rcvd is EOPNOTSUPP */
257/* pru_rcvoob is EOPNOTSUPP */
258
259static int
260rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
261 struct mbuf *control, struct proc *p)
262{
2d21ac55 263 int error;
1c79356b 264 error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
1c79356b
A
265 return error;
266}
267
268/* pru_sense is null */
269
270static int
271rts_shutdown(struct socket *so)
272{
2d21ac55 273 int error;
1c79356b 274 error = raw_usrreqs.pru_shutdown(so);
1c79356b
A
275 return error;
276}
277
278static int
279rts_sockaddr(struct socket *so, struct sockaddr **nam)
280{
2d21ac55 281 int error;
1c79356b 282 error = raw_usrreqs.pru_sockaddr(so, nam);
1c79356b
A
283 return error;
284}
285
286static struct pr_usrreqs route_usrreqs = {
91447636
A
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
1c79356b
A
292};
293
294/*ARGSUSED*/
295static int
2d21ac55 296route_output(struct mbuf *m, struct socket *so)
1c79356b 297{
2d21ac55
A
298 struct rt_msghdr *rtm = NULL;
299 struct rtentry *rt = NULL;
300 struct rtentry *saved_nrt = NULL;
1c79356b
A
301 struct radix_node_head *rnh;
302 struct rt_addrinfo info;
303 int len, error = 0;
2d21ac55 304 struct ifnet *ifp = NULL;
91447636 305#ifndef __APPLE__
1c79356b 306 struct proc *curproc = current_proc();
91447636 307#endif
c910b4d9 308 struct sockaddr_in dst_in, gate_in;
55e303ae 309 int sendonlytoself = 0;
c910b4d9 310 unsigned int ifscope = IFSCOPE_NONE;
1c79356b
A
311
312#define senderr(e) { error = e; goto flush;}
c910b4d9 313 if (m == NULL ||
b0d623f7 314 ((m->m_len < sizeof(intptr_t)) && (m = m_pullup(m, sizeof(intptr_t))) == 0))
1c79356b
A
315 return (ENOBUFS);
316 if ((m->m_flags & M_PKTHDR) == 0)
317 panic("route_output");
91447636
A
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);
b0d623f7 321 lck_mtx_lock(rnh_lock);
91447636 322
1c79356b
A
323 len = m->m_pkthdr.len;
324 if (len < sizeof(*rtm) ||
325 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
c910b4d9 326 dst = NULL;
1c79356b
A
327 senderr(EINVAL);
328 }
329 R_Malloc(rtm, struct rt_msghdr *, len);
c910b4d9
A
330 if (rtm == NULL) {
331 dst = NULL;
1c79356b
A
332 senderr(ENOBUFS);
333 }
334 m_copydata(m, 0, len, (caddr_t)rtm);
335 if (rtm->rtm_version != RTM_VERSION) {
c910b4d9 336 dst = NULL;
1c79356b
A
337 senderr(EPROTONOSUPPORT);
338 }
c910b4d9 339
55e303ae
A
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 }
c910b4d9 350
55e303ae
A
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) {
c910b4d9 356 dst = NULL;
55e303ae
A
357 senderr(EPERM);
358 }
91447636
A
359
360 rtm->rtm_pid = proc_selfpid();
1c79356b
A
361 info.rti_addrs = rtm->rtm_addrs;
362 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
c910b4d9 363 dst = NULL;
1c79356b
A
364 senderr(EINVAL);
365 }
c910b4d9
A
366 if (dst == NULL || (dst->sa_family >= AF_MAX) ||
367 (gate != NULL && (gate->sa_family >= AF_MAX))) {
1c79356b 368 senderr(EINVAL);
91447636 369 }
c910b4d9
A
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
1c79356b
A
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 }
c910b4d9
A
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
1c79356b 415 switch (rtm->rtm_type) {
c910b4d9 416
91447636 417 case RTM_ADD:
c910b4d9 418 if (gate == NULL)
91447636 419 senderr(EINVAL);
9bccf70c
A
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{
9bccf70c 434#define satosinaddr(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr)
91447636
A
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 }
9bccf70c
A
447}
448#endif
c910b4d9
A
449 error = rtrequest_scoped_locked(RTM_ADD, dst, gate,
450 netmask, rtm->rtm_flags, &saved_nrt, ifscope);
91447636 451 if (error == 0 && saved_nrt) {
b0d623f7 452 RT_LOCK(saved_nrt);
9bccf70c 453#ifdef __APPLE__
91447636
A
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
c910b4d9
A
482 rt_setif(saved_nrt, ifpaddr, ifaaddr, gate,
483 ifscope);
9bccf70c 484#endif
91447636
A
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);
91447636 490 saved_nrt->rt_genmask = genmask;
b0d623f7
A
491 RT_REMREF_LOCKED(saved_nrt);
492 RT_UNLOCK(saved_nrt);
1c79356b 493 }
91447636
A
494 break;
495
496 case RTM_DELETE:
c910b4d9
A
497 error = rtrequest_scoped_locked(RTM_DELETE, dst,
498 gate, netmask, rtm->rtm_flags, &saved_nrt, ifscope);
91447636 499 if (error == 0) {
2d21ac55 500 rt = saved_nrt;
b0d623f7 501 RT_LOCK(rt);
91447636 502 goto report;
1c79356b 503 }
1c79356b
A
504 break;
505
91447636 506 case RTM_GET:
1c79356b 507 case RTM_CHANGE:
91447636 508 case RTM_LOCK:
c910b4d9 509 if ((rnh = rt_tables[dst->sa_family]) == NULL)
91447636 510 senderr(EAFNOSUPPORT);
c910b4d9
A
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)
91447636 518 senderr(ESRCH);
b0d623f7 519 RT_LOCK(rt);
c910b4d9 520
91447636
A
521 switch(rtm->rtm_type) {
522
523 case RTM_GET: {
524 struct ifaddr *ifa2;
525 report:
b0d623f7
A
526 ifa2 = NULL;
527 RT_LOCK_ASSERT_HELD(rt);
91447636
A
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;
b0d623f7 538 ifaref(ifa2);
91447636
A
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) {
b0d623f7
A
553 RT_UNLOCK(rt);
554 if (ifa2 != NULL)
555 ifafree(ifa2);
91447636
A
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;
b0d623f7
A
566 if (ifa2 != NULL)
567 ifafree(ifa2);
91447636
A
568 }
569 break;
570
571 case RTM_CHANGE:
b0d623f7
A
572 if (gate && (error = rt_setgate(rt,
573 rt_key(rt), gate))) {
574 RT_UNLOCK(rt);
91447636 575 senderr(error);
b0d623f7 576 }
91447636
A
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
9bccf70c 587#ifdef __APPLE__
91447636
A
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 */
c910b4d9
A
593 rt_setif(rt, ifpaddr, ifaaddr, gate,
594 ifscope);
9bccf70c 595#endif
91447636
A
596
597 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
598 &rt->rt_rmx);
9bccf70c 599#ifndef __APPLE__
91447636
A
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);
9bccf70c 603#endif
91447636
A
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 }
b0d623f7 615 RT_UNLOCK(rt);
1c79356b 616 break;
91447636
A
617
618 default:
619 senderr(EOPNOTSUPP);
1c79356b 620 }
1c79356b
A
621flush:
622 if (rtm) {
623 if (error)
624 rtm->rtm_errno = error;
625 else
626 rtm->rtm_flags |= RTF_DONE;
627 }
b0d623f7
A
628 if (rt != NULL) {
629 RT_LOCK_ASSERT_NOTHELD(rt);
91447636 630 rtfree_locked(rt);
b0d623f7
A
631 }
632 lck_mtx_unlock(rnh_lock);
91447636 633 socket_lock(so, 0); /* relock the socket now */
1c79356b 634 {
91447636 635 struct rawcb *rp = 0;
1c79356b
A
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)
91447636 642 R_Free(rtm);
1c79356b
A
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);
9bccf70c
A
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);
91447636 656 R_Free(rtm);
1c79356b 657 }
55e303ae 658 if (sendonlytoself && m) {
91447636
A
659 error = 0;
660 if (sbappendaddr(&so->so_rcv, &route_src, m, (struct mbuf*)0, &error) != 0) {
55e303ae
A
661 sorwakeup(so);
662 }
91447636
A
663 if (error)
664 return error;
55e303ae
A
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;
91447636
A
670 if (m) {
671 socket_unlock(so, 0);
55e303ae 672 raw_input(m, &route_proto, &route_src, &route_dst);
91447636
A
673 socket_lock(so, 0);
674 }
55e303ae
A
675 if (rp)
676 rp->rcb_proto.sp_family = PF_ROUTE;
677 }
678 }
1c79356b
A
679 return (error);
680}
681
682static void
b0d623f7 683rt_setmetrics(u_int32_t which, struct rt_metrics *in, struct rt_metrics *out)
1c79356b
A
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 */
700static void
c910b4d9
A
701rt_setif(struct rtentry *rt, struct sockaddr *Ifpaddr, struct sockaddr *Ifaaddr,
702 struct sockaddr *Gate, unsigned int ifscope)
1c79356b
A
703{
704 struct ifaddr *ifa = 0;
705 struct ifnet *ifp = 0;
706
b0d623f7
A
707 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
708
709 RT_LOCK_ASSERT_HELD(rt);
91447636 710
2d21ac55
A
711 /* trigger route cache reevaluation */
712 if (use_routegenid)
b0d623f7
A
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);
2d21ac55 721
c910b4d9
A
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)) &&
91447636 728 (ifp = ifa->ifa_ifp) && (Ifaaddr || Gate)) {
c910b4d9
A
729 ifafree(ifa);
730 ifa = ifaof_ifpforaddr(Ifaaddr ? Ifaaddr : Gate, ifp);
731 } else {
91447636
A
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);
c910b4d9 739 } else {
91447636
A
740 ifnet_lock_shared(ifp);
741 ifa = TAILQ_FIRST(&ifp->if_addrhead);
b0d623f7
A
742 if (ifa != NULL)
743 ifaref(ifa);
91447636
A
744 ifnet_lock_done(ifp);
745 }
c910b4d9
A
746 } else if (Ifaaddr &&
747 (ifa = ifa_ifwithaddr_scoped(Ifaaddr, ifscope))) {
91447636 748 ifp = ifa->ifa_ifp;
b0d623f7
A
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 }
91447636
A
770 }
771 }
1c79356b 772 if (ifa) {
91447636 773 struct ifaddr *oifa = rt->rt_ifa;
1c79356b 774 if (oifa != ifa) {
c910b4d9
A
775 if (oifa && oifa->ifa_rtrequest)
776 oifa->ifa_rtrequest(RTM_DELETE, rt, Gate);
9bccf70c 777 rtsetifa(rt, ifa);
c910b4d9
A
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)
9bccf70c 787 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, Gate);
91447636 788 ifafree(ifa);
b0d623f7
A
789 /* Release extra ref */
790 RT_REMREF_LOCKED(rt);
791 return;
91447636
A
792 }
793 ifafree(ifa);
1c79356b 794 }
b0d623f7 795
1c79356b
A
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);
1c79356b 799
b0d623f7
A
800 /* Release extra ref */
801 RT_REMREF_LOCKED(rt);
802}
1c79356b 803
b0d623f7
A
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))
1c79356b
A
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 */
814static int
2d21ac55 815rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1c79356b 816{
91447636
A
817 struct sockaddr *sa;
818 int i;
1c79356b
A
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;
b0d623f7 846 ADVANCE32(cp, sa);
1c79356b
A
847 }
848 return (0);
849}
850
851static struct mbuf *
b0d623f7 852rt_msg1(int type, struct rt_addrinfo *rtinfo)
1c79356b 853{
91447636
A
854 struct rt_msghdr *rtm;
855 struct mbuf *m;
856 int i;
1c79356b
A
857 int len, dlen;
858
1c79356b
A
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 }
9bccf70c 878 if (len > MCLBYTES)
1c79356b 879 panic("rt_msg1");
9bccf70c
A
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);
1c79356b
A
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++) {
b0d623f7
A
895 struct sockaddr *sa, *hint;
896 struct sockaddr_storage ss;
897
1c79356b
A
898 if ((sa = rtinfo->rti_info[i]) == NULL)
899 continue;
b0d623f7
A
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
1c79356b 915 rtinfo->rti_addrs |= (1 << i);
b0d623f7 916 dlen = ROUNDUP32(sa->sa_len);
1c79356b
A
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
930static int
2d21ac55 931rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1c79356b 932{
91447636 933 int i;
1c79356b
A
934 int len, dlen, second_time = 0;
935 caddr_t cp0;
936
937 rtinfo->rti_addrs = 0;
938again:
939 switch (type) {
940
941 case RTM_DELADDR:
942 case RTM_NEWADDR:
943 len = sizeof(struct ifa_msghdr);
944 break;
945
91447636
A
946 case RTM_DELMADDR:
947 case RTM_NEWMADDR:
948 len = sizeof(struct ifma_msghdr);
949 break;
950
1c79356b
A
951 case RTM_IFINFO:
952 len = sizeof(struct if_msghdr);
953 break;
954
91447636
A
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
1c79356b
A
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++) {
b0d623f7
A
974 struct sockaddr *sa, *hint;
975 struct sockaddr_storage ss;
1c79356b
A
976
977 if ((sa = rtinfo->rti_info[i]) == 0)
978 continue;
b0d623f7
A
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
1c79356b 994 rtinfo->rti_addrs |= (1 << i);
b0d623f7 995 dlen = ROUNDUP32(sa->sa_len);
1c79356b
A
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) {
91447636 1003 struct walkarg *rw = w;
1c79356b
A
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)
0b4e3aa0 1010 _MALLOC(len, M_RTABLE, M_WAITOK); /*###LD0412 was NOWAIT */
1c79356b
A
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) {
91447636 1022 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1c79356b
A
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
91447636 1033 * socket indicating that a redirect has occurred, a routing lookup
1c79356b
A
1034 * has failed, or that a protocol has detected timeouts to a particular
1035 * destination.
1036 */
1037void
2d21ac55 1038rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1c79356b 1039{
91447636
A
1040 struct rt_msghdr *rtm;
1041 struct mbuf *m;
1c79356b
A
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 */
1061void
91447636
A
1062rt_ifmsg(
1063 struct ifnet *ifp)
1c79356b 1064{
91447636 1065 struct if_msghdr *ifm;
1c79356b
A
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;
2d21ac55 1078 if_data_internal_to_if_data(ifp, &ifp->if_data, &ifm->ifm_data);
1c79356b
A
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.
91447636
A
1091 *
1092 * Since this is coming from the interface, it is expected that the
b0d623f7 1093 * interface will be locked. Caller must hold rt_lock.
1c79356b
A
1094 */
1095void
2d21ac55 1096rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1c79356b
A
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
b0d623f7
A
1104 RT_LOCK_ASSERT_HELD(rt);
1105
1c79356b
A
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)) {
91447636 1112 struct ifa_msghdr *ifam;
1c79356b
A
1113 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1114
b0d623f7
A
1115 /* Lock ifp for if_addrhead */
1116 ifnet_lock_shared(ifp);
1c79356b
A
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;
b0d623f7
A
1121 if ((m = rt_msg1(ncmd, &info)) == NULL) {
1122 ifnet_lock_done(ifp);
1c79356b 1123 continue;
b0d623f7
A
1124 }
1125 ifnet_lock_done(ifp);
1c79356b
A
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)) {
91447636 1134 struct rt_msghdr *rtm;
1c79356b
A
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 */
1159void
2d21ac55 1160rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1c79356b
A
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;
b0d623f7
A
1172 /* Lock ifp for if_addrhead */
1173 if (ifp != NULL)
1174 ifnet_lock_shared(ifp);
9bccf70c
A
1175 if (ifp && ifp->if_addrhead.tqh_first)
1176 ifpaddr = ifp->if_addrhead.tqh_first->ifa_addr;
1177 else
1178 ifpaddr = NULL;
1c79356b
A
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 */
91447636 1183 gate = ifma->ifma_ll->ifma_addr;
b0d623f7
A
1184 if ((m = rt_msg1(cmd, &info)) == NULL) {
1185 if (ifp != NULL)
1186 ifnet_lock_done(ifp);
1c79356b 1187 return;
b0d623f7
A
1188 }
1189 if (ifp != NULL)
1190 ifnet_lock_done(ifp);
1c79356b 1191 ifmam = mtod(m, struct ifma_msghdr *);
91447636 1192 ifmam->ifmam_index = ifp ? ifp->if_index : 0;
1c79356b
A
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 */
1201int
2d21ac55 1202sysctl_dumpentry(struct radix_node *rn, void *vw)
1c79356b 1203{
91447636
A
1204 struct walkarg *w = vw;
1205 struct rtentry *rt = (struct rtentry *)rn;
1c79356b
A
1206 int error = 0, size;
1207 struct rt_addrinfo info;
1208
b0d623f7
A
1209 RT_LOCK(rt);
1210 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) {
1211 RT_UNLOCK(rt);
1c79356b 1212 return 0;
b0d623f7 1213 }
1c79356b
A
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;
91447636
A
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);
b0d623f7 1233 RT_UNLOCK(rt);
91447636
A
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);
b0d623f7 1253 RT_UNLOCK(rt);
91447636
A
1254 return (error);
1255
1256 }
1c79356b 1257 }
b0d623f7 1258 RT_UNLOCK(rt);
1c79356b
A
1259 return (error);
1260}
1261
1262int
91447636
A
1263sysctl_iflist(
1264 int af,
1265 struct walkarg *w)
1c79356b 1266{
91447636
A
1267 struct ifnet *ifp;
1268 struct ifaddr *ifa;
1c79356b
A
1269 struct rt_addrinfo info;
1270 int len, error = 0;
1271
1272 bzero((caddr_t)&info, sizeof(info));
91447636
A
1273 ifnet_head_lock_shared();
1274 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
1275 if (error)
1276 break;
1c79356b
A
1277 if (w->w_arg && w->w_arg != ifp->if_index)
1278 continue;
91447636 1279 ifnet_lock_shared(ifp);
1c79356b
A
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) {
91447636 1285 struct if_msghdr *ifm;
1c79356b
A
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;
2d21ac55 1290 if_data_internal_to_if_data(ifp, &ifp->if_data, &ifm->ifm_data);
1c79356b
A
1291 ifm->ifm_addrs = info.rti_addrs;
1292 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
91447636
A
1293 if (error) {
1294 ifnet_lock_done(ifp);
1295 break;
1296 }
1c79356b
A
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) {
91447636 1306 struct ifa_msghdr *ifam;
1c79356b
A
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)
91447636 1315 break;
1c79356b
A
1316 }
1317 }
91447636 1318 ifnet_lock_done(ifp);
1c79356b
A
1319 ifaaddr = netmask = brdaddr = 0;
1320 }
91447636
A
1321 ifnet_head_done();
1322 return error;
1c79356b
A
1323}
1324
91447636
A
1325int
1326sysctl_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;
2d21ac55 1358 if_data_internal_to_if_data64(ifp, &ifp->if_data, &ifm->ifm_data);
91447636
A
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
1425static int
1426sysctl_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
1437static int
1438sysctl_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
1c79356b
A
1450static int
1451sysctl_rtsock SYSCTL_HANDLER_ARGS
1452{
c910b4d9 1453#pragma unused(oidp)
1c79356b
A
1454 int *name = (int *)arg1;
1455 u_int namelen = arg2;
91447636
A
1456 struct radix_node_head *rnh;
1457 int i, error = EINVAL;
1c79356b
A
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
1c79356b
A
1473 switch (w.w_op) {
1474
1475 case NET_RT_DUMP:
91447636 1476 case NET_RT_DUMP2:
1c79356b 1477 case NET_RT_FLAGS:
b0d623f7 1478 lck_mtx_lock(rnh_lock);
1c79356b
A
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;
b0d623f7 1484 lck_mtx_unlock(rnh_lock);
1c79356b 1485 break;
1c79356b
A
1486 case NET_RT_IFLIST:
1487 error = sysctl_iflist(af, &w);
91447636
A
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;
1c79356b 1498 }
1c79356b
A
1499 if (w.w_tmem)
1500 FREE(w.w_tmem, M_RTABLE);
1501 return (error);
1502}
1503
1504SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1505
1c79356b
A
1506/*
1507 * Definitions of protocols supported in the ROUTE domain.
1508 */
1509
b0d623f7 1510extern struct domain routedomain; /* or at least forward */
1c79356b 1511
1c79356b
A
1512static struct protosw routesw[] = {
1513{ SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
9bccf70c 1514 0, route_output, raw_ctlinput, 0,
1c79356b
A
1515 0,
1516 raw_init, 0, 0, 0,
91447636
A
1517 0,
1518 &route_usrreqs,
1519 0, 0, 0,
1520 { 0, 0 }, 0, { 0 }
1c79356b
A
1521}
1522};
1523
1524struct domain routedomain =
1525 { PF_ROUTE, "route", route_init, 0, 0,
91447636 1526 routesw,
2d21ac55 1527 NULL, NULL, 0, 0, 0, 0, NULL, 0,
91447636 1528 { 0, 0 } };
1c79356b
A
1529
1530DOMAIN_SET(route);
1531