2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
32 #include "opt_compat.h"
34 #include "opt_mpath.h"
36 #include "opt_inet6.h"
38 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
43 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
56 #include <net/if_dl.h>
57 #include <net/if_llatbl.h>
58 #include <net/if_types.h>
59 #include <net/netisr.h>
60 #include <net/raw_cb.h>
61 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/ip_carp.h>
68 #include <netinet6/scope6_var.h>
71 #if defined(INET) || defined(INET6)
73 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
77 #ifdef COMPAT_FREEBSD32
78 #include <sys/mount.h>
79 #include <compat/freebsd32/freebsd32.h>
86 uint8_t ifi_link_state;
88 uint8_t ifi_spare_char2;
92 uint32_t ifi_baudrate;
93 uint32_t ifi_ipackets;
95 uint32_t ifi_opackets;
97 uint32_t ifi_collisions;
100 uint32_t ifi_imcasts;
101 uint32_t ifi_omcasts;
102 uint32_t ifi_iqdrops;
103 uint32_t ifi_noproto;
104 uint32_t ifi_hwassist;
106 struct timeval32 ifi_lastchange;
116 struct if_data32 ifm_data;
120 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
122 /* NB: these are not modified */
123 static struct sockaddr route_src = { 2, PF_ROUTE, };
124 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
126 /* These are external hooks for CARP. */
127 int (*carp_get_vhid_p)(struct ifaddr *);
130 * Used by rtsock/raw_input callback code to decide whether to filter the update
131 * notification to a socket bound to a particular FIB.
133 #define RTS_FILTER_FIB M_PROTO8
134 #define RTS_ALLFIBS -1
137 int ip_count; /* attached w/ AF_INET */
138 int ip6_count; /* attached w/ AF_INET6 */
139 int ipx_count; /* attached w/ AF_IPX */
140 int any_count; /* total attached */
143 struct mtx rtsock_mtx;
144 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
146 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
147 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
148 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
150 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
156 struct sysctl_req *w_req;
159 static void rts_input(struct mbuf *m);
160 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
161 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
162 caddr_t cp, struct walkarg *w);
163 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
164 struct rt_addrinfo *rtinfo);
165 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
166 static int sysctl_iflist(int af, struct walkarg *w);
167 static int sysctl_ifmalist(int af, struct walkarg *w);
168 static int route_output(struct mbuf *m, struct socket *so);
169 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
170 struct rt_metrics_lite *out);
171 static void rt_getmetrics(const struct rt_metrics_lite *in,
172 struct rt_metrics *out);
173 static void rt_dispatch(struct mbuf *, sa_family_t);
175 static struct netisr_handler rtsock_nh = {
177 .nh_handler = rts_input,
178 .nh_proto = NETISR_ROUTE,
179 .nh_policy = NETISR_POLICY_SOURCE,
183 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
187 netisr_getqlimit(&rtsock_nh, &qlimit);
188 error = sysctl_handle_int(oidp, &qlimit, 0, req);
189 if (error || !req->newptr)
193 return (netisr_setqlimit(&rtsock_nh, qlimit));
195 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
196 0, 0, sysctl_route_netisr_maxqlen, "I",
197 "maximum routing socket dispatch queue length");
204 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
205 rtsock_nh.nh_qlimit = tmp;
206 netisr_register(&rtsock_nh);
208 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
211 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
216 KASSERT(m != NULL, ("%s: m is NULL", __func__));
217 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
218 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
220 /* No filtering requested. */
221 if ((m->m_flags & RTS_FILTER_FIB) == 0)
224 /* Check if it is a rts and the fib matches the one of the socket. */
225 fibnum = M_GETFIB(m);
226 if (proto->sp_family != PF_ROUTE ||
227 rp->rcb_socket == NULL ||
228 rp->rcb_socket->so_fibnum == fibnum)
231 /* Filtering requested and no match, the socket shall be skipped. */
236 rts_input(struct mbuf *m)
238 struct sockproto route_proto;
239 unsigned short *family;
242 route_proto.sp_family = PF_ROUTE;
243 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
245 family = (unsigned short *)(tag + 1);
246 route_proto.sp_protocol = *family;
247 m_tag_delete(m, tag);
249 route_proto.sp_protocol = 0;
251 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
255 * It really doesn't make any sense at all for this code to share much
256 * with raw_usrreq.c, since its functionality is so restricted. XXX
259 rts_abort(struct socket *so)
262 raw_usrreqs.pru_abort(so);
266 rts_close(struct socket *so)
269 raw_usrreqs.pru_close(so);
272 /* pru_accept is EOPNOTSUPP */
275 rts_attach(struct socket *so, int proto, struct thread *td)
280 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
283 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
288 * The splnet() is necessary to block protocols from sending
289 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
290 * this PCB is extant but incompletely initialized.
291 * Probably we should try to do more of this work beforehand and
295 so->so_pcb = (caddr_t)rp;
296 so->so_fibnum = td->td_proc->p_fibnum;
297 error = raw_attach(so, proto);
306 switch(rp->rcb_proto.sp_protocol) {
311 route_cb.ip6_count++;
314 route_cb.ipx_count++;
317 route_cb.any_count++;
320 so->so_options |= SO_USELOOPBACK;
326 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
329 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
333 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
336 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
339 /* pru_connect2 is EOPNOTSUPP */
340 /* pru_control is EOPNOTSUPP */
343 rts_detach(struct socket *so)
345 struct rawcb *rp = sotorawcb(so);
347 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
350 switch(rp->rcb_proto.sp_protocol) {
355 route_cb.ip6_count--;
358 route_cb.ipx_count--;
361 route_cb.any_count--;
363 raw_usrreqs.pru_detach(so);
367 rts_disconnect(struct socket *so)
370 return (raw_usrreqs.pru_disconnect(so));
373 /* pru_listen is EOPNOTSUPP */
376 rts_peeraddr(struct socket *so, struct sockaddr **nam)
379 return (raw_usrreqs.pru_peeraddr(so, nam));
382 /* pru_rcvd is EOPNOTSUPP */
383 /* pru_rcvoob is EOPNOTSUPP */
386 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
387 struct mbuf *control, struct thread *td)
390 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
393 /* pru_sense is null */
396 rts_shutdown(struct socket *so)
399 return (raw_usrreqs.pru_shutdown(so));
403 rts_sockaddr(struct socket *so, struct sockaddr **nam)
406 return (raw_usrreqs.pru_sockaddr(so, nam));
409 static struct pr_usrreqs route_usrreqs = {
410 .pru_abort = rts_abort,
411 .pru_attach = rts_attach,
412 .pru_bind = rts_bind,
413 .pru_connect = rts_connect,
414 .pru_detach = rts_detach,
415 .pru_disconnect = rts_disconnect,
416 .pru_peeraddr = rts_peeraddr,
417 .pru_send = rts_send,
418 .pru_shutdown = rts_shutdown,
419 .pru_sockaddr = rts_sockaddr,
420 .pru_close = rts_close,
423 #ifndef _SOCKADDR_UNION_DEFINED
424 #define _SOCKADDR_UNION_DEFINED
426 * The union of all possible address formats we handle.
428 union sockaddr_union {
430 struct sockaddr_in sin;
431 struct sockaddr_in6 sin6;
433 #endif /* _SOCKADDR_UNION_DEFINED */
436 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
437 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
440 /* First, see if the returned address is part of the jail. */
441 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
442 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
446 switch (info->rti_info[RTAX_DST]->sa_family) {
456 * Try to find an address on the given outgoing interface
457 * that belongs to the jail.
460 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
463 if (sa->sa_family != AF_INET)
465 ia = ((struct sockaddr_in *)sa)->sin_addr;
466 if (prison_check_ip4(cred, &ia) == 0) {
474 * As a last resort return the 'default' jail address.
476 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
478 if (prison_get_ip4(cred, &ia) != 0)
481 bzero(&saun->sin, sizeof(struct sockaddr_in));
482 saun->sin.sin_len = sizeof(struct sockaddr_in);
483 saun->sin.sin_family = AF_INET;
484 saun->sin.sin_addr.s_addr = ia.s_addr;
485 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
498 * Try to find an address on the given outgoing interface
499 * that belongs to the jail.
502 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
505 if (sa->sa_family != AF_INET6)
507 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
508 &ia6, sizeof(struct in6_addr));
509 if (prison_check_ip6(cred, &ia6) == 0) {
517 * As a last resort return the 'default' jail address.
519 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
521 if (prison_get_ip6(cred, &ia6) != 0)
524 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
525 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
526 saun->sin6.sin6_family = AF_INET6;
527 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
528 if (sa6_recoverscope(&saun->sin6) != 0)
530 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
542 route_output(struct mbuf *m, struct socket *so)
544 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
545 struct rt_msghdr *rtm = NULL;
546 struct rtentry *rt = NULL;
547 struct radix_node_head *rnh;
548 struct rt_addrinfo info;
550 struct ifnet *ifp = NULL;
551 union sockaddr_union saun;
552 sa_family_t saf = AF_UNSPEC;
554 #define senderr(e) { error = e; goto flush;}
555 if (m == NULL || ((m->m_len < sizeof(long)) &&
556 (m = m_pullup(m, sizeof(long))) == NULL))
558 if ((m->m_flags & M_PKTHDR) == 0)
559 panic("route_output");
560 len = m->m_pkthdr.len;
561 if (len < sizeof(*rtm) ||
562 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
563 info.rti_info[RTAX_DST] = NULL;
566 R_Malloc(rtm, struct rt_msghdr *, len);
568 info.rti_info[RTAX_DST] = NULL;
571 m_copydata(m, 0, len, (caddr_t)rtm);
572 if (rtm->rtm_version != RTM_VERSION) {
573 info.rti_info[RTAX_DST] = NULL;
574 senderr(EPROTONOSUPPORT);
576 rtm->rtm_pid = curproc->p_pid;
577 bzero(&info, sizeof(info));
578 info.rti_addrs = rtm->rtm_addrs;
579 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
580 info.rti_info[RTAX_DST] = NULL;
583 info.rti_flags = rtm->rtm_flags;
584 if (info.rti_info[RTAX_DST] == NULL ||
585 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
586 (info.rti_info[RTAX_GATEWAY] != NULL &&
587 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
589 saf = info.rti_info[RTAX_DST]->sa_family;
591 * Verify that the caller has the appropriate privilege; RTM_GET
592 * is the only operation the non-superuser is allowed.
594 if (rtm->rtm_type != RTM_GET) {
595 error = priv_check(curthread, PRIV_NET_ROUTE);
601 * The given gateway address may be an interface address.
602 * For example, issuing a "route change" command on a route
603 * entry that was created from a tunnel, and the gateway
604 * address given is the local end point. In this case the
605 * RTF_GATEWAY flag must be cleared or the destination will
606 * not be reachable even though there is no error message.
608 if (info.rti_info[RTAX_GATEWAY] != NULL &&
609 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
612 bzero(&gw_ro, sizeof(gw_ro));
613 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
614 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
616 * A host route through the loopback interface is
617 * installed for each interface adddress. In pre 8.0
618 * releases the interface address of a PPP link type
619 * is not reachable locally. This behavior is fixed as
620 * part of the new L2/L3 redesign and rewrite work. The
621 * signature of this interface address route is the
622 * AF_LINK sa_family type of the rt_gateway, and the
623 * rt_ifp has the IFF_LOOPBACK flag set.
625 if (gw_ro.ro_rt != NULL &&
626 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
627 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
628 info.rti_flags &= ~RTF_GATEWAY;
629 if (gw_ro.ro_rt != NULL)
633 switch (rtm->rtm_type) {
634 struct rtentry *saved_nrt;
637 if (info.rti_info[RTAX_GATEWAY] == NULL)
641 /* support for new ARP code */
642 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
643 (rtm->rtm_flags & RTF_LLDATA) != 0) {
644 error = lla_rt_output(rtm, &info);
647 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
649 if (error == 0 && saved_nrt) {
651 rt_setmetrics(rtm->rtm_inits,
652 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
653 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
654 RT_REMREF(saved_nrt);
655 RT_UNLOCK(saved_nrt);
661 /* support for new ARP code */
662 if (info.rti_info[RTAX_GATEWAY] &&
663 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
664 (rtm->rtm_flags & RTF_LLDATA) != 0) {
665 error = lla_rt_output(rtm, &info);
668 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
680 rnh = rt_tables_get_rnh(so->so_fibnum,
681 info.rti_info[RTAX_DST]->sa_family);
683 senderr(EAFNOSUPPORT);
684 RADIX_NODE_HEAD_RLOCK(rnh);
685 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
686 info.rti_info[RTAX_NETMASK], rnh);
687 if (rt == NULL) { /* XXX looks bogus */
688 RADIX_NODE_HEAD_RUNLOCK(rnh);
693 * for RTM_CHANGE/LOCK, if we got multipath routes,
694 * we require users to specify a matching RTAX_GATEWAY.
696 * for RTM_GET, gate is optional even with multipath.
697 * if gate == NULL the first match is returned.
698 * (no need to call rt_mpath_matchgate if gate == NULL)
700 if (rn_mpath_capable(rnh) &&
701 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
702 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
704 RADIX_NODE_HEAD_RUNLOCK(rnh);
710 * If performing proxied L2 entry insertion, and
711 * the actual PPP host entry is found, perform
712 * another search to retrieve the prefix route of
713 * the local end point of the PPP link.
715 if (rtm->rtm_flags & RTF_ANNOUNCE) {
716 struct sockaddr laddr;
718 if (rt->rt_ifp != NULL &&
719 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
722 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
724 rt_maskedcopy(ifa->ifa_addr,
728 rt_maskedcopy(rt->rt_ifa->ifa_addr,
730 rt->rt_ifa->ifa_netmask);
732 * refactor rt and no lock operation necessary
734 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
736 RADIX_NODE_HEAD_RUNLOCK(rnh);
742 RADIX_NODE_HEAD_RUNLOCK(rnh);
747 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
748 * returns a perfect match in case a netmask is
749 * specified. For host routes only a longest prefix
750 * match is returned so it is necessary to compare the
751 * existence of the netmask. If both have a netmask
752 * rnh_lookup() did a perfect match and if none of them
753 * have a netmask both are host routes which is also a
757 if (rtm->rtm_type != RTM_GET &&
758 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
763 switch(rtm->rtm_type) {
768 if ((rt->rt_flags & RTF_HOST) == 0
769 ? jailed_without_vnet(curthread->td_ucred)
770 : prison_if(curthread->td_ucred,
775 info.rti_info[RTAX_DST] = rt_key(rt);
776 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
777 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
778 info.rti_info[RTAX_GENMASK] = 0;
779 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
782 info.rti_info[RTAX_IFP] =
783 ifp->if_addr->ifa_addr;
784 error = rtm_get_jailed(&info, ifp, rt,
785 &saun, curthread->td_ucred);
790 if (ifp->if_flags & IFF_POINTOPOINT)
791 info.rti_info[RTAX_BRD] =
792 rt->rt_ifa->ifa_dstaddr;
793 rtm->rtm_index = ifp->if_index;
795 info.rti_info[RTAX_IFP] = NULL;
796 info.rti_info[RTAX_IFA] = NULL;
798 } else if ((ifp = rt->rt_ifp) != NULL) {
799 rtm->rtm_index = ifp->if_index;
801 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
802 if (len > rtm->rtm_msglen) {
803 struct rt_msghdr *new_rtm;
804 R_Malloc(new_rtm, struct rt_msghdr *, len);
805 if (new_rtm == NULL) {
809 bcopy(rtm, new_rtm, rtm->rtm_msglen);
810 Free(rtm); rtm = new_rtm;
812 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
813 rtm->rtm_flags = rt->rt_flags;
814 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
815 rtm->rtm_addrs = info.rti_addrs;
820 * New gateway could require new ifaddr, ifp;
821 * flags may also be different; ifp may be specified
822 * by ll sockaddr when protocol address is ambiguous
824 if (((rt->rt_flags & RTF_GATEWAY) &&
825 info.rti_info[RTAX_GATEWAY] != NULL) ||
826 info.rti_info[RTAX_IFP] != NULL ||
827 (info.rti_info[RTAX_IFA] != NULL &&
828 !sa_equal(info.rti_info[RTAX_IFA],
829 rt->rt_ifa->ifa_addr))) {
831 RADIX_NODE_HEAD_LOCK(rnh);
832 error = rt_getifa_fib(&info, rt->rt_fibnum);
834 * XXXRW: Really we should release this
835 * reference later, but this maintains
836 * historical behavior.
838 if (info.rti_ifa != NULL)
839 ifa_free(info.rti_ifa);
840 RADIX_NODE_HEAD_UNLOCK(rnh);
845 if (info.rti_ifa != NULL &&
846 info.rti_ifa != rt->rt_ifa &&
847 rt->rt_ifa != NULL &&
848 rt->rt_ifa->ifa_rtrequest != NULL) {
849 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
851 ifa_free(rt->rt_ifa);
853 if (info.rti_info[RTAX_GATEWAY] != NULL) {
855 RADIX_NODE_HEAD_LOCK(rnh);
858 error = rt_setgate(rt, rt_key(rt),
859 info.rti_info[RTAX_GATEWAY]);
860 RADIX_NODE_HEAD_UNLOCK(rnh);
865 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
867 if (info.rti_ifa != NULL &&
868 info.rti_ifa != rt->rt_ifa) {
869 ifa_ref(info.rti_ifa);
870 rt->rt_ifa = info.rti_ifa;
871 rt->rt_ifp = info.rti_ifp;
873 /* Allow some flags to be toggled on change. */
874 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
875 (rtm->rtm_flags & RTF_FMASK);
876 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
878 rtm->rtm_index = rt->rt_ifp->if_index;
879 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
880 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
883 /* We don't support locks anymore */
896 rtm->rtm_errno = error;
898 rtm->rtm_flags |= RTF_DONE;
900 if (rt) /* XXX can this be true? */
903 struct rawcb *rp = NULL;
905 * Check to see if we don't want our own messages.
907 if ((so->so_options & SO_USELOOPBACK) == 0) {
908 if (route_cb.any_count <= 1) {
914 /* There is another listener, so construct message */
918 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
919 if (m->m_pkthdr.len < rtm->rtm_msglen) {
922 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
923 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
926 M_SETFIB(m, so->so_fibnum);
927 m->m_flags |= RTS_FILTER_FIB;
930 * XXX insure we don't get a copy by
931 * invalidating our protocol
933 unsigned short family = rp->rcb_proto.sp_family;
934 rp->rcb_proto.sp_family = 0;
936 rp->rcb_proto.sp_family = family;
940 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
949 rt_setmetrics(u_long which, const struct rt_metrics *in,
950 struct rt_metrics_lite *out)
952 #define metric(f, e) if (which & (f)) out->e = in->e;
954 * Only these are stored in the routing entry since introduction
955 * of tcp hostcache. The rest is ignored.
957 metric(RTV_MTU, rmx_mtu);
958 metric(RTV_WEIGHT, rmx_weight);
959 /* Userland -> kernel timebase conversion. */
960 if (which & RTV_EXPIRE)
961 out->rmx_expire = in->rmx_expire ?
962 in->rmx_expire - time_second + time_uptime : 0;
967 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
969 #define metric(e) out->e = in->e;
970 bzero(out, sizeof(*out));
973 /* Kernel -> userland timebase conversion. */
974 out->rmx_expire = in->rmx_expire ?
975 in->rmx_expire - time_uptime + time_second : 0;
980 * Extract the addresses of the passed sockaddrs.
981 * Do a little sanity checking so as to avoid bad memory references.
982 * This data is derived straight from userland.
985 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
990 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
991 if ((rtinfo->rti_addrs & (1 << i)) == 0)
993 sa = (struct sockaddr *)cp;
997 if (cp + sa->sa_len > cplim)
1000 * there are no more.. quit now
1001 * If there are more bits, they are in error.
1002 * I've seen this. route(1) can evidently generate these.
1003 * This causes kernel to core dump.
1004 * for compatibility, If we see this, point to a safe address.
1006 if (sa->sa_len == 0) {
1007 rtinfo->rti_info[i] = &sa_zero;
1008 return (0); /* should be EINVAL but for compat */
1011 rtinfo->rti_info[i] = sa;
1017 static struct mbuf *
1018 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1020 struct rt_msghdr *rtm;
1023 struct sockaddr *sa;
1030 len = sizeof(struct ifa_msghdr);
1035 len = sizeof(struct ifma_msghdr);
1039 len = sizeof(struct if_msghdr);
1042 case RTM_IFANNOUNCE:
1044 len = sizeof(struct if_announcemsghdr);
1048 len = sizeof(struct rt_msghdr);
1052 m = m_gethdr(M_DONTWAIT, MT_DATA);
1053 if (m && len > MHLEN) {
1054 MCLGET(m, M_DONTWAIT);
1055 if ((m->m_flags & M_EXT) == 0) {
1062 m->m_pkthdr.len = m->m_len = len;
1063 m->m_pkthdr.rcvif = NULL;
1064 rtm = mtod(m, struct rt_msghdr *);
1065 bzero((caddr_t)rtm, len);
1066 for (i = 0; i < RTAX_MAX; i++) {
1067 if ((sa = rtinfo->rti_info[i]) == NULL)
1069 rtinfo->rti_addrs |= (1 << i);
1071 m_copyback(m, len, dlen, (caddr_t)sa);
1074 if (m->m_pkthdr.len != len) {
1078 rtm->rtm_msglen = len;
1079 rtm->rtm_version = RTM_VERSION;
1080 rtm->rtm_type = type;
1085 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1088 int len, dlen, second_time = 0;
1091 rtinfo->rti_addrs = 0;
1097 len = sizeof(struct ifa_msghdr);
1101 #ifdef COMPAT_FREEBSD32
1102 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1103 len = sizeof(struct if_msghdr32);
1107 len = sizeof(struct if_msghdr);
1111 len = sizeof(struct ifma_msghdr);
1115 len = sizeof(struct rt_msghdr);
1120 for (i = 0; i < RTAX_MAX; i++) {
1121 struct sockaddr *sa;
1123 if ((sa = rtinfo->rti_info[i]) == NULL)
1125 rtinfo->rti_addrs |= (1 << i);
1128 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1134 if (cp == NULL && w != NULL && !second_time) {
1135 struct walkarg *rw = w;
1138 if (rw->w_tmemsize < len) {
1140 free(rw->w_tmem, M_RTABLE);
1141 rw->w_tmem = (caddr_t)
1142 malloc(len, M_RTABLE, M_NOWAIT);
1144 rw->w_tmemsize = len;
1154 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1156 rtm->rtm_version = RTM_VERSION;
1157 rtm->rtm_type = type;
1158 rtm->rtm_msglen = len;
1164 * This routine is called to generate a message from the routing
1165 * socket indicating that a redirect has occured, a routing lookup
1166 * has failed, or that a protocol has detected timeouts to a particular
1170 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1173 struct rt_msghdr *rtm;
1175 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1177 if (route_cb.any_count == 0)
1179 m = rt_msg1(type, rtinfo);
1183 if (fibnum != RTS_ALLFIBS) {
1184 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1185 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1186 M_SETFIB(m, fibnum);
1187 m->m_flags |= RTS_FILTER_FIB;
1190 rtm = mtod(m, struct rt_msghdr *);
1191 rtm->rtm_flags = RTF_DONE | flags;
1192 rtm->rtm_errno = error;
1193 rtm->rtm_addrs = rtinfo->rti_addrs;
1194 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1198 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1201 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1205 * This routine is called to generate a message from the routing
1206 * socket indicating that the status of a network interface has changed.
1209 rt_ifmsg(struct ifnet *ifp)
1211 struct if_msghdr *ifm;
1213 struct rt_addrinfo info;
1215 if (route_cb.any_count == 0)
1217 bzero((caddr_t)&info, sizeof(info));
1218 m = rt_msg1(RTM_IFINFO, &info);
1221 ifm = mtod(m, struct if_msghdr *);
1222 ifm->ifm_index = ifp->if_index;
1223 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1224 ifm->ifm_data = ifp->if_data;
1226 rt_dispatch(m, AF_UNSPEC);
1230 * This is called to generate messages from the routing socket
1231 * indicating a network interface has had addresses associated with it.
1232 * if we ever reverse the logic and replace messages TO the routing
1233 * socket indicate a request to configure interfaces, then it will
1234 * be unnecessary as the routing socket will automatically generate
1238 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1241 struct rt_addrinfo info;
1242 struct sockaddr *sa = NULL;
1244 struct mbuf *m = NULL;
1245 struct ifnet *ifp = ifa->ifa_ifp;
1247 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1248 ("unexpected cmd %u", cmd));
1249 #if defined(INET) || defined(INET6)
1252 * notify the SCTP stack
1253 * this will only get called when an address is added/deleted
1254 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1256 sctp_addr_change(ifa, cmd);
1259 if (route_cb.any_count == 0)
1261 for (pass = 1; pass < 3; pass++) {
1262 bzero((caddr_t)&info, sizeof(info));
1263 if ((cmd == RTM_ADD && pass == 1) ||
1264 (cmd == RTM_DELETE && pass == 2)) {
1265 struct ifa_msghdr *ifam;
1266 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1268 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1269 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1270 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1271 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1272 if ((m = rt_msg1(ncmd, &info)) == NULL)
1274 ifam = mtod(m, struct ifa_msghdr *);
1275 ifam->ifam_index = ifp->if_index;
1276 ifam->ifam_metric = ifa->ifa_metric;
1277 ifam->ifam_flags = ifa->ifa_flags;
1278 ifam->ifam_addrs = info.rti_addrs;
1280 if ((cmd == RTM_ADD && pass == 2) ||
1281 (cmd == RTM_DELETE && pass == 1)) {
1282 struct rt_msghdr *rtm;
1286 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1287 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1288 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1289 if ((m = rt_msg1(cmd, &info)) == NULL)
1291 rtm = mtod(m, struct rt_msghdr *);
1292 rtm->rtm_index = ifp->if_index;
1293 rtm->rtm_flags |= rt->rt_flags;
1294 rtm->rtm_errno = error;
1295 rtm->rtm_addrs = info.rti_addrs;
1297 if (fibnum != RTS_ALLFIBS) {
1298 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1299 "fibnum out of range 0 <= %d < %d", __func__,
1300 fibnum, rt_numfibs));
1301 M_SETFIB(m, fibnum);
1302 m->m_flags |= RTS_FILTER_FIB;
1304 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1309 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1312 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1316 * This is the analogue to the rt_newaddrmsg which performs the same
1317 * function but for multicast group memberhips. This is easier since
1318 * there is no route state to worry about.
1321 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1323 struct rt_addrinfo info;
1324 struct mbuf *m = NULL;
1325 struct ifnet *ifp = ifma->ifma_ifp;
1326 struct ifma_msghdr *ifmam;
1328 if (route_cb.any_count == 0)
1331 bzero((caddr_t)&info, sizeof(info));
1332 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1333 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1335 * If a link-layer address is present, present it as a ``gateway''
1336 * (similarly to how ARP entries, e.g., are presented).
1338 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1339 m = rt_msg1(cmd, &info);
1342 ifmam = mtod(m, struct ifma_msghdr *);
1343 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1345 ifmam->ifmam_index = ifp->if_index;
1346 ifmam->ifmam_addrs = info.rti_addrs;
1347 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1350 static struct mbuf *
1351 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1352 struct rt_addrinfo *info)
1354 struct if_announcemsghdr *ifan;
1357 if (route_cb.any_count == 0)
1359 bzero((caddr_t)info, sizeof(*info));
1360 m = rt_msg1(type, info);
1362 ifan = mtod(m, struct if_announcemsghdr *);
1363 ifan->ifan_index = ifp->if_index;
1364 strlcpy(ifan->ifan_name, ifp->if_xname,
1365 sizeof(ifan->ifan_name));
1366 ifan->ifan_what = what;
1372 * This is called to generate routing socket messages indicating
1373 * IEEE80211 wireless events.
1374 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1377 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1380 struct rt_addrinfo info;
1382 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1385 * Append the ieee80211 data. Try to stick it in the
1386 * mbuf containing the ifannounce msg; otherwise allocate
1387 * a new mbuf and append.
1389 * NB: we assume m is a single mbuf.
1391 if (data_len > M_TRAILINGSPACE(m)) {
1392 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1397 bcopy(data, mtod(n, void *), data_len);
1398 n->m_len = data_len;
1400 } else if (data_len > 0) {
1401 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1402 m->m_len += data_len;
1404 if (m->m_flags & M_PKTHDR)
1405 m->m_pkthdr.len += data_len;
1406 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1407 rt_dispatch(m, AF_UNSPEC);
1412 * This is called to generate routing socket messages indicating
1413 * network interface arrival and departure.
1416 rt_ifannouncemsg(struct ifnet *ifp, int what)
1419 struct rt_addrinfo info;
1421 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1423 rt_dispatch(m, AF_UNSPEC);
1427 rt_dispatch(struct mbuf *m, sa_family_t saf)
1432 * Preserve the family from the sockaddr, if any, in an m_tag for
1433 * use when injecting the mbuf into the routing socket buffer from
1436 if (saf != AF_UNSPEC) {
1437 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1443 *(unsigned short *)(tag + 1) = saf;
1444 m_tag_prepend(m, tag);
1448 m->m_pkthdr.rcvif = V_loif;
1454 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1458 * This is used in dumping the kernel table via sysctl().
1461 sysctl_dumpentry(struct radix_node *rn, void *vw)
1463 struct walkarg *w = vw;
1464 struct rtentry *rt = (struct rtentry *)rn;
1465 int error = 0, size;
1466 struct rt_addrinfo info;
1468 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1470 if ((rt->rt_flags & RTF_HOST) == 0
1471 ? jailed_without_vnet(w->w_req->td->td_ucred)
1472 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1474 bzero((caddr_t)&info, sizeof(info));
1475 info.rti_info[RTAX_DST] = rt_key(rt);
1476 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1477 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1478 info.rti_info[RTAX_GENMASK] = 0;
1480 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1481 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1482 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1483 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1485 size = rt_msg2(RTM_GET, &info, NULL, w);
1486 if (w->w_req && w->w_tmem) {
1487 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1489 rtm->rtm_flags = rt->rt_flags;
1491 * let's be honest about this being a retarded hack
1493 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1494 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1495 rtm->rtm_index = rt->rt_ifp->if_index;
1496 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1497 rtm->rtm_addrs = info.rti_addrs;
1498 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1504 #ifdef COMPAT_FREEBSD32
1506 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1509 bzero(dst, sizeof(*dst));
1510 CP(*src, *dst, ifi_type);
1511 CP(*src, *dst, ifi_physical);
1512 CP(*src, *dst, ifi_addrlen);
1513 CP(*src, *dst, ifi_hdrlen);
1514 CP(*src, *dst, ifi_link_state);
1515 CP(*src, *dst, ifi_vhid);
1516 dst->ifi_datalen = sizeof(struct if_data32);
1517 CP(*src, *dst, ifi_mtu);
1518 CP(*src, *dst, ifi_metric);
1519 CP(*src, *dst, ifi_baudrate);
1520 CP(*src, *dst, ifi_ipackets);
1521 CP(*src, *dst, ifi_ierrors);
1522 CP(*src, *dst, ifi_opackets);
1523 CP(*src, *dst, ifi_oerrors);
1524 CP(*src, *dst, ifi_collisions);
1525 CP(*src, *dst, ifi_ibytes);
1526 CP(*src, *dst, ifi_obytes);
1527 CP(*src, *dst, ifi_imcasts);
1528 CP(*src, *dst, ifi_omcasts);
1529 CP(*src, *dst, ifi_iqdrops);
1530 CP(*src, *dst, ifi_noproto);
1531 CP(*src, *dst, ifi_hwassist);
1532 CP(*src, *dst, ifi_epoch);
1533 TV_CP(*src, *dst, ifi_lastchange);
1538 sysctl_iflist(int af, struct walkarg *w)
1542 struct rt_addrinfo info;
1545 bzero((caddr_t)&info, sizeof(info));
1547 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1548 if (w->w_arg && w->w_arg != ifp->if_index)
1552 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1553 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1554 info.rti_info[RTAX_IFP] = NULL;
1555 if (w->w_req && w->w_tmem) {
1556 struct if_msghdr *ifm;
1558 #ifdef COMPAT_FREEBSD32
1559 if (w->w_req->flags & SCTL_MASK32) {
1560 struct if_msghdr32 *ifm32;
1562 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1563 ifm32->ifm_index = ifp->if_index;
1564 ifm32->ifm_flags = ifp->if_flags |
1566 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1567 if (carp_get_vhid_p != NULL)
1568 ifm32->ifm_data.ifi_vhid =
1569 (*carp_get_vhid_p)(ifa);
1570 ifm32->ifm_addrs = info.rti_addrs;
1571 error = SYSCTL_OUT(w->w_req, (caddr_t)ifm32,
1576 ifm = (struct if_msghdr *)w->w_tmem;
1577 ifm->ifm_index = ifp->if_index;
1578 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1579 ifm->ifm_data = ifp->if_data;
1580 if (carp_get_vhid_p != NULL)
1581 ifm->ifm_data.ifi_vhid =
1582 (*carp_get_vhid_p)(ifa);
1583 ifm->ifm_addrs = info.rti_addrs;
1584 error = SYSCTL_OUT(w->w_req, (caddr_t)ifm, len);
1585 #ifdef COMPAT_FREEBSD32
1591 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1592 if (af && af != ifa->ifa_addr->sa_family)
1594 if (prison_if(w->w_req->td->td_ucred,
1595 ifa->ifa_addr) != 0)
1597 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1598 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1599 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1600 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1601 if (w->w_req && w->w_tmem) {
1602 struct ifa_msghdr *ifam;
1604 ifam = (struct ifa_msghdr *)w->w_tmem;
1605 ifam->ifam_index = ifa->ifa_ifp->if_index;
1606 ifam->ifam_flags = ifa->ifa_flags;
1607 ifam->ifam_metric = ifa->ifa_metric;
1608 ifam->ifam_addrs = info.rti_addrs;
1609 if (carp_get_vhid_p != NULL)
1610 ifam->ifam_data.ifi_vhid =
1611 (*carp_get_vhid_p)(ifa);
1612 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1617 IF_ADDR_UNLOCK(ifp);
1618 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1619 info.rti_info[RTAX_BRD] = NULL;
1623 IF_ADDR_UNLOCK(ifp);
1629 sysctl_ifmalist(int af, struct walkarg *w)
1632 struct ifmultiaddr *ifma;
1633 struct rt_addrinfo info;
1637 bzero((caddr_t)&info, sizeof(info));
1639 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1640 if (w->w_arg && w->w_arg != ifp->if_index)
1643 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1645 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1646 if (af && af != ifma->ifma_addr->sa_family)
1648 if (prison_if(w->w_req->td->td_ucred,
1649 ifma->ifma_addr) != 0)
1651 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1652 info.rti_info[RTAX_GATEWAY] =
1653 (ifma->ifma_addr->sa_family != AF_LINK) ?
1654 ifma->ifma_lladdr : NULL;
1655 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1656 if (w->w_req && w->w_tmem) {
1657 struct ifma_msghdr *ifmam;
1659 ifmam = (struct ifma_msghdr *)w->w_tmem;
1660 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1661 ifmam->ifmam_flags = 0;
1662 ifmam->ifmam_addrs = info.rti_addrs;
1663 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1665 IF_ADDR_UNLOCK(ifp);
1670 IF_ADDR_UNLOCK(ifp);
1678 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1680 int *name = (int *)arg1;
1681 u_int namelen = arg2;
1682 struct radix_node_head *rnh = NULL; /* silence compiler. */
1683 int i, lim, error = EINVAL;
1692 return ((namelen < 3) ? EISDIR : ENOTDIR);
1696 bzero(&w, sizeof(w));
1701 error = sysctl_wire_old_buffer(req, 0);
1708 if (af == 0) { /* dump all tables */
1711 } else /* dump only one table */
1715 * take care of llinfo entries, the caller must
1718 if (w.w_op == NET_RT_FLAGS &&
1719 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1721 error = lltable_sysctl_dumparp(af, w.w_req);
1727 * take care of routing entries
1729 for (error = 0; error == 0 && i <= lim; i++) {
1730 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1732 RADIX_NODE_HEAD_LOCK(rnh);
1733 error = rnh->rnh_walktree(rnh,
1734 sysctl_dumpentry, &w);
1735 RADIX_NODE_HEAD_UNLOCK(rnh);
1737 error = EAFNOSUPPORT;
1742 error = sysctl_iflist(af, &w);
1745 case NET_RT_IFMALIST:
1746 error = sysctl_ifmalist(af, &w);
1750 free(w.w_tmem, M_RTABLE);
1754 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1757 * Definitions of protocols supported in the ROUTE domain.
1760 static struct domain routedomain; /* or at least forward */
1762 static struct protosw routesw[] = {
1764 .pr_type = SOCK_RAW,
1765 .pr_domain = &routedomain,
1766 .pr_flags = PR_ATOMIC|PR_ADDR,
1767 .pr_output = route_output,
1768 .pr_ctlinput = raw_ctlinput,
1769 .pr_init = raw_init,
1770 .pr_usrreqs = &route_usrreqs
1774 static struct domain routedomain = {
1775 .dom_family = PF_ROUTE,
1776 .dom_name = "route",
1777 .dom_protosw = routesw,
1778 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1781 VNET_DOMAIN_SET(route);