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"
33 #include "opt_mpath.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/domain.h>
42 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/rwlock.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
54 #define _IN_NET_RTSOCK_C
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/ip6_var.h>
69 #include <netinet6/scope6_var.h>
72 #ifdef COMPAT_FREEBSD32
73 #include <sys/mount.h>
74 #include <compat/freebsd32/freebsd32.h>
81 uint8_t ifi_link_state;
83 uint8_t ifi_baudrate_pf;
87 uint32_t ifi_baudrate;
88 uint32_t ifi_ipackets;
90 uint32_t ifi_opackets;
92 uint32_t ifi_collisions;
99 uint32_t ifi_hwassist;
101 struct timeval32 ifi_lastchange;
102 uint32_t ifi_oqdrops;
112 struct if_data32 ifm_data;
115 struct if_msghdrl32 {
122 uint16_t _ifm_spare1;
124 uint16_t ifm_data_off;
125 struct if_data32 ifm_data;
128 struct ifa_msghdrl32 {
129 uint16_t ifam_msglen;
130 uint8_t ifam_version;
135 uint16_t _ifam_spare1;
137 uint16_t ifam_data_off;
139 struct if_data32 ifam_data;
141 #endif /* COMPAT_FREEBSD32 */
143 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
145 /* NB: these are not modified */
146 static struct sockaddr route_src = { 2, PF_ROUTE, };
147 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
149 /* These are external hooks for CARP. */
150 int (*carp_get_vhid_p)(struct ifaddr *);
153 * Used by rtsock/raw_input callback code to decide whether to filter the update
154 * notification to a socket bound to a particular FIB.
156 #define RTS_FILTER_FIB M_PROTO8
159 int ip_count; /* attached w/ AF_INET */
160 int ip6_count; /* attached w/ AF_INET6 */
161 int ipx_count; /* attached w/ AF_IPX */
162 int any_count; /* total attached */
165 struct mtx rtsock_mtx;
166 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
168 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
169 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
170 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
172 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
178 struct sysctl_req *w_req;
181 static void rts_input(struct mbuf *m);
182 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
183 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
184 caddr_t cp, struct walkarg *w);
185 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
186 struct rt_addrinfo *rtinfo);
187 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
188 static int sysctl_iflist(int af, struct walkarg *w);
189 static int sysctl_ifmalist(int af, struct walkarg *w);
190 static int route_output(struct mbuf *m, struct socket *so);
191 static void rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt);
192 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
193 static void rt_dispatch(struct mbuf *, sa_family_t);
195 static struct netisr_handler rtsock_nh = {
197 .nh_handler = rts_input,
198 .nh_proto = NETISR_ROUTE,
199 .nh_policy = NETISR_POLICY_SOURCE,
203 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
207 netisr_getqlimit(&rtsock_nh, &qlimit);
208 error = sysctl_handle_int(oidp, &qlimit, 0, req);
209 if (error || !req->newptr)
213 return (netisr_setqlimit(&rtsock_nh, qlimit));
215 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
216 0, 0, sysctl_route_netisr_maxqlen, "I",
217 "maximum routing socket dispatch queue length");
224 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
225 rtsock_nh.nh_qlimit = tmp;
226 netisr_register(&rtsock_nh);
228 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
231 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
236 KASSERT(m != NULL, ("%s: m is NULL", __func__));
237 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
238 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
240 /* No filtering requested. */
241 if ((m->m_flags & RTS_FILTER_FIB) == 0)
244 /* Check if it is a rts and the fib matches the one of the socket. */
245 fibnum = M_GETFIB(m);
246 if (proto->sp_family != PF_ROUTE ||
247 rp->rcb_socket == NULL ||
248 rp->rcb_socket->so_fibnum == fibnum)
251 /* Filtering requested and no match, the socket shall be skipped. */
256 rts_input(struct mbuf *m)
258 struct sockproto route_proto;
259 unsigned short *family;
262 route_proto.sp_family = PF_ROUTE;
263 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
265 family = (unsigned short *)(tag + 1);
266 route_proto.sp_protocol = *family;
267 m_tag_delete(m, tag);
269 route_proto.sp_protocol = 0;
271 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
275 * It really doesn't make any sense at all for this code to share much
276 * with raw_usrreq.c, since its functionality is so restricted. XXX
279 rts_abort(struct socket *so)
282 raw_usrreqs.pru_abort(so);
286 rts_close(struct socket *so)
289 raw_usrreqs.pru_close(so);
292 /* pru_accept is EOPNOTSUPP */
295 rts_attach(struct socket *so, int proto, struct thread *td)
300 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
303 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
307 so->so_pcb = (caddr_t)rp;
308 so->so_fibnum = td->td_proc->p_fibnum;
309 error = raw_attach(so, proto);
317 switch(rp->rcb_proto.sp_protocol) {
322 route_cb.ip6_count++;
325 route_cb.ipx_count++;
328 route_cb.any_count++;
331 so->so_options |= SO_USELOOPBACK;
336 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
339 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
343 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
346 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
349 /* pru_connect2 is EOPNOTSUPP */
350 /* pru_control is EOPNOTSUPP */
353 rts_detach(struct socket *so)
355 struct rawcb *rp = sotorawcb(so);
357 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
360 switch(rp->rcb_proto.sp_protocol) {
365 route_cb.ip6_count--;
368 route_cb.ipx_count--;
371 route_cb.any_count--;
373 raw_usrreqs.pru_detach(so);
377 rts_disconnect(struct socket *so)
380 return (raw_usrreqs.pru_disconnect(so));
383 /* pru_listen is EOPNOTSUPP */
386 rts_peeraddr(struct socket *so, struct sockaddr **nam)
389 return (raw_usrreqs.pru_peeraddr(so, nam));
392 /* pru_rcvd is EOPNOTSUPP */
393 /* pru_rcvoob is EOPNOTSUPP */
396 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
397 struct mbuf *control, struct thread *td)
400 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
403 /* pru_sense is null */
406 rts_shutdown(struct socket *so)
409 return (raw_usrreqs.pru_shutdown(so));
413 rts_sockaddr(struct socket *so, struct sockaddr **nam)
416 return (raw_usrreqs.pru_sockaddr(so, nam));
419 static struct pr_usrreqs route_usrreqs = {
420 .pru_abort = rts_abort,
421 .pru_attach = rts_attach,
422 .pru_bind = rts_bind,
423 .pru_connect = rts_connect,
424 .pru_detach = rts_detach,
425 .pru_disconnect = rts_disconnect,
426 .pru_peeraddr = rts_peeraddr,
427 .pru_send = rts_send,
428 .pru_shutdown = rts_shutdown,
429 .pru_sockaddr = rts_sockaddr,
430 .pru_close = rts_close,
433 #ifndef _SOCKADDR_UNION_DEFINED
434 #define _SOCKADDR_UNION_DEFINED
436 * The union of all possible address formats we handle.
438 union sockaddr_union {
440 struct sockaddr_in sin;
441 struct sockaddr_in6 sin6;
443 #endif /* _SOCKADDR_UNION_DEFINED */
446 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
447 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
450 /* First, see if the returned address is part of the jail. */
451 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
452 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
456 switch (info->rti_info[RTAX_DST]->sa_family) {
466 * Try to find an address on the given outgoing interface
467 * that belongs to the jail.
470 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
473 if (sa->sa_family != AF_INET)
475 ia = ((struct sockaddr_in *)sa)->sin_addr;
476 if (prison_check_ip4(cred, &ia) == 0) {
481 IF_ADDR_RUNLOCK(ifp);
484 * As a last resort return the 'default' jail address.
486 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
488 if (prison_get_ip4(cred, &ia) != 0)
491 bzero(&saun->sin, sizeof(struct sockaddr_in));
492 saun->sin.sin_len = sizeof(struct sockaddr_in);
493 saun->sin.sin_family = AF_INET;
494 saun->sin.sin_addr.s_addr = ia.s_addr;
495 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
508 * Try to find an address on the given outgoing interface
509 * that belongs to the jail.
512 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
515 if (sa->sa_family != AF_INET6)
517 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
518 &ia6, sizeof(struct in6_addr));
519 if (prison_check_ip6(cred, &ia6) == 0) {
524 IF_ADDR_RUNLOCK(ifp);
527 * As a last resort return the 'default' jail address.
529 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
531 if (prison_get_ip6(cred, &ia6) != 0)
534 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
535 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
536 saun->sin6.sin6_family = AF_INET6;
537 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
538 if (sa6_recoverscope(&saun->sin6) != 0)
540 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
552 route_output(struct mbuf *m, struct socket *so)
554 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
555 struct rt_msghdr *rtm = NULL;
556 struct rtentry *rt = NULL;
557 struct radix_node_head *rnh;
558 struct rt_addrinfo info;
560 struct sockaddr_storage ss;
561 struct sockaddr_in6 *sin6;
562 int i, rti_need_deembed = 0;
565 struct ifnet *ifp = NULL;
566 union sockaddr_union saun;
567 sa_family_t saf = AF_UNSPEC;
569 #define senderr(e) { error = e; goto flush;}
570 if (m == NULL || ((m->m_len < sizeof(long)) &&
571 (m = m_pullup(m, sizeof(long))) == NULL))
573 if ((m->m_flags & M_PKTHDR) == 0)
574 panic("route_output");
575 len = m->m_pkthdr.len;
576 if (len < sizeof(*rtm) ||
577 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
578 info.rti_info[RTAX_DST] = NULL;
581 R_Malloc(rtm, struct rt_msghdr *, len);
583 info.rti_info[RTAX_DST] = NULL;
586 m_copydata(m, 0, len, (caddr_t)rtm);
587 if (rtm->rtm_version != RTM_VERSION) {
588 info.rti_info[RTAX_DST] = NULL;
589 senderr(EPROTONOSUPPORT);
591 rtm->rtm_pid = curproc->p_pid;
592 bzero(&info, sizeof(info));
593 info.rti_addrs = rtm->rtm_addrs;
595 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
596 * link-local address because rtrequest requires addresses with
599 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
600 info.rti_info[RTAX_DST] = NULL;
603 info.rti_flags = rtm->rtm_flags;
604 if (info.rti_info[RTAX_DST] == NULL ||
605 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
606 (info.rti_info[RTAX_GATEWAY] != NULL &&
607 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
609 saf = info.rti_info[RTAX_DST]->sa_family;
611 * Verify that the caller has the appropriate privilege; RTM_GET
612 * is the only operation the non-superuser is allowed.
614 if (rtm->rtm_type != RTM_GET) {
615 error = priv_check(curthread, PRIV_NET_ROUTE);
621 * The given gateway address may be an interface address.
622 * For example, issuing a "route change" command on a route
623 * entry that was created from a tunnel, and the gateway
624 * address given is the local end point. In this case the
625 * RTF_GATEWAY flag must be cleared or the destination will
626 * not be reachable even though there is no error message.
628 if (info.rti_info[RTAX_GATEWAY] != NULL &&
629 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
632 bzero(&gw_ro, sizeof(gw_ro));
633 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
634 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
636 * A host route through the loopback interface is
637 * installed for each interface adddress. In pre 8.0
638 * releases the interface address of a PPP link type
639 * is not reachable locally. This behavior is fixed as
640 * part of the new L2/L3 redesign and rewrite work. The
641 * signature of this interface address route is the
642 * AF_LINK sa_family type of the rt_gateway, and the
643 * rt_ifp has the IFF_LOOPBACK flag set.
645 if (gw_ro.ro_rt != NULL &&
646 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
647 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
648 info.rti_flags &= ~RTF_GATEWAY;
649 info.rti_flags |= RTF_GWFLAG_COMPAT;
651 if (gw_ro.ro_rt != NULL)
655 switch (rtm->rtm_type) {
656 struct rtentry *saved_nrt;
659 if (info.rti_info[RTAX_GATEWAY] == NULL)
663 /* support for new ARP code */
664 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
665 (rtm->rtm_flags & RTF_LLDATA) != 0) {
666 error = lla_rt_output(rtm, &info);
669 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
673 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
675 if (error == 0 && saved_nrt) {
677 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
680 rt_setmetrics(rtm, saved_nrt);
681 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
682 RT_REMREF(saved_nrt);
683 RT_UNLOCK(saved_nrt);
689 /* support for new ARP code */
690 if (info.rti_info[RTAX_GATEWAY] &&
691 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
692 (rtm->rtm_flags & RTF_LLDATA) != 0) {
693 error = lla_rt_output(rtm, &info);
696 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
700 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
708 /* rt_msg2() will not be used when RTM_DELETE fails. */
709 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
716 rnh = rt_tables_get_rnh(so->so_fibnum,
717 info.rti_info[RTAX_DST]->sa_family);
719 senderr(EAFNOSUPPORT);
721 RADIX_NODE_HEAD_RLOCK(rnh);
723 if (info.rti_info[RTAX_NETMASK] == NULL &&
724 rtm->rtm_type == RTM_GET) {
726 * Provide logest prefix match for
727 * address lookup (no mask).
728 * 'route -n get addr'
730 rt = (struct rtentry *) rnh->rnh_matchaddr(
731 info.rti_info[RTAX_DST], rnh);
733 rt = (struct rtentry *) rnh->rnh_lookup(
734 info.rti_info[RTAX_DST],
735 info.rti_info[RTAX_NETMASK], rnh);
738 RADIX_NODE_HEAD_RUNLOCK(rnh);
743 * for RTM_CHANGE/LOCK, if we got multipath routes,
744 * we require users to specify a matching RTAX_GATEWAY.
746 * for RTM_GET, gate is optional even with multipath.
747 * if gate == NULL the first match is returned.
748 * (no need to call rt_mpath_matchgate if gate == NULL)
750 if (rn_mpath_capable(rnh) &&
751 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
752 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
754 RADIX_NODE_HEAD_RUNLOCK(rnh);
760 * If performing proxied L2 entry insertion, and
761 * the actual PPP host entry is found, perform
762 * another search to retrieve the prefix route of
763 * the local end point of the PPP link.
765 if (rtm->rtm_flags & RTF_ANNOUNCE) {
766 struct sockaddr laddr;
768 if (rt->rt_ifp != NULL &&
769 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
772 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
774 rt_maskedcopy(ifa->ifa_addr,
778 rt_maskedcopy(rt->rt_ifa->ifa_addr,
780 rt->rt_ifa->ifa_netmask);
782 * refactor rt and no lock operation necessary
784 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
786 RADIX_NODE_HEAD_RUNLOCK(rnh);
792 RADIX_NODE_HEAD_RUNLOCK(rnh);
794 switch(rtm->rtm_type) {
799 if ((rt->rt_flags & RTF_HOST) == 0
800 ? jailed_without_vnet(curthread->td_ucred)
801 : prison_if(curthread->td_ucred,
806 info.rti_info[RTAX_DST] = rt_key(rt);
807 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
808 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
809 info.rti_info[RTAX_GENMASK] = 0;
810 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
813 info.rti_info[RTAX_IFP] =
814 ifp->if_addr->ifa_addr;
815 error = rtm_get_jailed(&info, ifp, rt,
816 &saun, curthread->td_ucred);
821 if (ifp->if_flags & IFF_POINTOPOINT)
822 info.rti_info[RTAX_BRD] =
823 rt->rt_ifa->ifa_dstaddr;
824 rtm->rtm_index = ifp->if_index;
826 info.rti_info[RTAX_IFP] = NULL;
827 info.rti_info[RTAX_IFA] = NULL;
829 } else if ((ifp = rt->rt_ifp) != NULL) {
830 rtm->rtm_index = ifp->if_index;
832 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
833 if (len > rtm->rtm_msglen) {
834 struct rt_msghdr *new_rtm;
835 R_Malloc(new_rtm, struct rt_msghdr *, len);
836 if (new_rtm == NULL) {
840 bcopy(rtm, new_rtm, rtm->rtm_msglen);
841 Free(rtm); rtm = new_rtm;
843 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
844 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
845 rtm->rtm_flags = RTF_GATEWAY |
846 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
848 rtm->rtm_flags = rt->rt_flags;
849 rt_getmetrics(rt, &rtm->rtm_rmx);
850 rtm->rtm_addrs = info.rti_addrs;
855 * New gateway could require new ifaddr, ifp;
856 * flags may also be different; ifp may be specified
857 * by ll sockaddr when protocol address is ambiguous
859 if (((rt->rt_flags & RTF_GATEWAY) &&
860 info.rti_info[RTAX_GATEWAY] != NULL) ||
861 info.rti_info[RTAX_IFP] != NULL ||
862 (info.rti_info[RTAX_IFA] != NULL &&
863 !sa_equal(info.rti_info[RTAX_IFA],
864 rt->rt_ifa->ifa_addr))) {
866 RADIX_NODE_HEAD_LOCK(rnh);
867 error = rt_getifa_fib(&info, rt->rt_fibnum);
869 * XXXRW: Really we should release this
870 * reference later, but this maintains
871 * historical behavior.
873 if (info.rti_ifa != NULL)
874 ifa_free(info.rti_ifa);
875 RADIX_NODE_HEAD_UNLOCK(rnh);
880 if (info.rti_ifa != NULL &&
881 info.rti_ifa != rt->rt_ifa &&
882 rt->rt_ifa != NULL &&
883 rt->rt_ifa->ifa_rtrequest != NULL) {
884 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
886 ifa_free(rt->rt_ifa);
888 if (info.rti_info[RTAX_GATEWAY] != NULL) {
890 RADIX_NODE_HEAD_LOCK(rnh);
893 error = rt_setgate(rt, rt_key(rt),
894 info.rti_info[RTAX_GATEWAY]);
895 RADIX_NODE_HEAD_UNLOCK(rnh);
900 rt->rt_flags &= ~RTF_GATEWAY;
901 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
903 if (info.rti_ifa != NULL &&
904 info.rti_ifa != rt->rt_ifa) {
905 ifa_ref(info.rti_ifa);
906 rt->rt_ifa = info.rti_ifa;
907 rt->rt_ifp = info.rti_ifp;
909 /* Allow some flags to be toggled on change. */
910 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
911 (rtm->rtm_flags & RTF_FMASK);
912 rt_setmetrics(rtm, rt);
913 rtm->rtm_index = rt->rt_ifp->if_index;
914 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
915 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
918 /* We don't support locks anymore */
931 rtm->rtm_errno = error;
933 rtm->rtm_flags |= RTF_DONE;
935 if (rt) /* XXX can this be true? */
938 struct rawcb *rp = NULL;
940 * Check to see if we don't want our own messages.
942 if ((so->so_options & SO_USELOOPBACK) == 0) {
943 if (route_cb.any_count <= 1) {
949 /* There is another listener, so construct message */
954 if (rti_need_deembed) {
955 /* sin6_scope_id is recovered before sending rtm. */
956 sin6 = (struct sockaddr_in6 *)&ss;
957 for (i = 0; i < RTAX_MAX; i++) {
958 if (info.rti_info[i] == NULL)
960 if (info.rti_info[i]->sa_family != AF_INET6)
962 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
963 if (sa6_recoverscope(sin6) == 0)
964 bcopy(sin6, info.rti_info[i],
969 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
970 if (m->m_pkthdr.len < rtm->rtm_msglen) {
973 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
974 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
977 M_SETFIB(m, so->so_fibnum);
978 m->m_flags |= RTS_FILTER_FIB;
981 * XXX insure we don't get a copy by
982 * invalidating our protocol
984 unsigned short family = rp->rcb_proto.sp_family;
985 rp->rcb_proto.sp_family = 0;
987 rp->rcb_proto.sp_family = family;
991 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
1000 rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt)
1003 if (rtm->rtm_inits & RTV_MTU)
1004 rt->rt_mtu = rtm->rtm_rmx.rmx_mtu;
1005 if (rtm->rtm_inits & RTV_WEIGHT)
1006 rt->rt_weight = rtm->rtm_rmx.rmx_weight;
1007 /* Kernel -> userland timebase conversion. */
1008 if (rtm->rtm_inits & RTV_EXPIRE)
1009 rt->rt_expire = rtm->rtm_rmx.rmx_expire ?
1010 rtm->rtm_rmx.rmx_expire - time_second + time_uptime : 0;
1014 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
1017 bzero(out, sizeof(*out));
1018 out->rmx_mtu = rt->rt_mtu;
1019 out->rmx_weight = rt->rt_weight;
1020 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
1021 /* Kernel -> userland timebase conversion. */
1022 out->rmx_expire = rt->rt_expire ?
1023 rt->rt_expire - time_uptime + time_second : 0;
1027 * Extract the addresses of the passed sockaddrs.
1028 * Do a little sanity checking so as to avoid bad memory references.
1029 * This data is derived straight from userland.
1032 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1034 struct sockaddr *sa;
1037 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1038 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1040 sa = (struct sockaddr *)cp;
1044 if (cp + sa->sa_len > cplim)
1047 * there are no more.. quit now
1048 * If there are more bits, they are in error.
1049 * I've seen this. route(1) can evidently generate these.
1050 * This causes kernel to core dump.
1051 * for compatibility, If we see this, point to a safe address.
1053 if (sa->sa_len == 0) {
1054 rtinfo->rti_info[i] = &sa_zero;
1055 return (0); /* should be EINVAL but for compat */
1059 if (sa->sa_family == AF_INET6)
1060 sa6_embedscope((struct sockaddr_in6 *)sa,
1063 rtinfo->rti_info[i] = sa;
1070 * Used by the routing socket.
1072 static struct mbuf *
1073 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1075 struct rt_msghdr *rtm;
1078 struct sockaddr *sa;
1080 struct sockaddr_storage ss;
1081 struct sockaddr_in6 *sin6;
1089 len = sizeof(struct ifa_msghdr);
1094 len = sizeof(struct ifma_msghdr);
1098 len = sizeof(struct if_msghdr);
1101 case RTM_IFANNOUNCE:
1103 len = sizeof(struct if_announcemsghdr);
1107 len = sizeof(struct rt_msghdr);
1110 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1111 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1113 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1115 m = m_gethdr(M_NOWAIT, MT_DATA);
1119 m->m_pkthdr.len = m->m_len = len;
1120 rtm = mtod(m, struct rt_msghdr *);
1121 bzero((caddr_t)rtm, len);
1122 for (i = 0; i < RTAX_MAX; i++) {
1123 if ((sa = rtinfo->rti_info[i]) == NULL)
1125 rtinfo->rti_addrs |= (1 << i);
1128 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1129 sin6 = (struct sockaddr_in6 *)&ss;
1130 bcopy(sa, sin6, sizeof(*sin6));
1131 if (sa6_recoverscope(sin6) == 0)
1132 sa = (struct sockaddr *)sin6;
1135 m_copyback(m, len, dlen, (caddr_t)sa);
1138 if (m->m_pkthdr.len != len) {
1142 rtm->rtm_msglen = len;
1143 rtm->rtm_version = RTM_VERSION;
1144 rtm->rtm_type = type;
1149 * Used by the sysctl code and routing socket.
1152 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1155 int len, dlen, second_time = 0;
1158 struct sockaddr_storage ss;
1159 struct sockaddr_in6 *sin6;
1162 rtinfo->rti_addrs = 0;
1168 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1169 #ifdef COMPAT_FREEBSD32
1170 if (w->w_req->flags & SCTL_MASK32)
1171 len = sizeof(struct ifa_msghdrl32);
1174 len = sizeof(struct ifa_msghdrl);
1176 len = sizeof(struct ifa_msghdr);
1180 #ifdef COMPAT_FREEBSD32
1181 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1182 if (w->w_op == NET_RT_IFLISTL)
1183 len = sizeof(struct if_msghdrl32);
1185 len = sizeof(struct if_msghdr32);
1189 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1190 len = sizeof(struct if_msghdrl);
1192 len = sizeof(struct if_msghdr);
1196 len = sizeof(struct ifma_msghdr);
1200 len = sizeof(struct rt_msghdr);
1205 for (i = 0; i < RTAX_MAX; i++) {
1206 struct sockaddr *sa;
1208 if ((sa = rtinfo->rti_info[i]) == NULL)
1210 rtinfo->rti_addrs |= (1 << i);
1214 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1215 sin6 = (struct sockaddr_in6 *)&ss;
1216 bcopy(sa, sin6, sizeof(*sin6));
1217 if (sa6_recoverscope(sin6) == 0)
1218 sa = (struct sockaddr *)sin6;
1221 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1227 if (cp == NULL && w != NULL && !second_time) {
1228 struct walkarg *rw = w;
1231 if (rw->w_tmemsize < len) {
1233 free(rw->w_tmem, M_RTABLE);
1234 rw->w_tmem = (caddr_t)
1235 malloc(len, M_RTABLE, M_NOWAIT);
1237 rw->w_tmemsize = len;
1247 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1249 rtm->rtm_version = RTM_VERSION;
1250 rtm->rtm_type = type;
1251 rtm->rtm_msglen = len;
1257 * This routine is called to generate a message from the routing
1258 * socket indicating that a redirect has occured, a routing lookup
1259 * has failed, or that a protocol has detected timeouts to a particular
1263 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1266 struct rt_msghdr *rtm;
1268 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1270 if (route_cb.any_count == 0)
1272 m = rt_msg1(type, rtinfo);
1276 if (fibnum != RT_ALL_FIBS) {
1277 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1278 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1279 M_SETFIB(m, fibnum);
1280 m->m_flags |= RTS_FILTER_FIB;
1283 rtm = mtod(m, struct rt_msghdr *);
1284 rtm->rtm_flags = RTF_DONE | flags;
1285 rtm->rtm_errno = error;
1286 rtm->rtm_addrs = rtinfo->rti_addrs;
1287 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1291 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1294 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1298 * This routine is called to generate a message from the routing
1299 * socket indicating that the status of a network interface has changed.
1302 rt_ifmsg(struct ifnet *ifp)
1304 struct if_msghdr *ifm;
1306 struct rt_addrinfo info;
1308 if (route_cb.any_count == 0)
1310 bzero((caddr_t)&info, sizeof(info));
1311 m = rt_msg1(RTM_IFINFO, &info);
1314 ifm = mtod(m, struct if_msghdr *);
1315 ifm->ifm_index = ifp->if_index;
1316 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1317 ifm->ifm_data = ifp->if_data;
1319 rt_dispatch(m, AF_UNSPEC);
1323 * Announce interface address arrival/withdraw.
1324 * Please do not call directly, use rt_addrmsg().
1325 * Assume input data to be valid.
1326 * Returns 0 on success.
1329 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1331 struct rt_addrinfo info;
1332 struct sockaddr *sa;
1335 struct ifa_msghdr *ifam;
1336 struct ifnet *ifp = ifa->ifa_ifp;
1338 if (route_cb.any_count == 0)
1341 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1343 bzero((caddr_t)&info, sizeof(info));
1344 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1345 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1346 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1347 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1348 if ((m = rt_msg1(ncmd, &info)) == NULL)
1350 ifam = mtod(m, struct ifa_msghdr *);
1351 ifam->ifam_index = ifp->if_index;
1352 ifam->ifam_metric = ifa->ifa_metric;
1353 ifam->ifam_flags = ifa->ifa_flags;
1354 ifam->ifam_addrs = info.rti_addrs;
1356 if (fibnum != RT_ALL_FIBS) {
1357 M_SETFIB(m, fibnum);
1358 m->m_flags |= RTS_FILTER_FIB;
1361 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1367 * Announce route addition/removal.
1368 * Please do not call directly, use rt_routemsg().
1369 * Note that @rt data MAY be inconsistent/invalid:
1370 * if some userland app sends us "invalid" route message (invalid mask,
1371 * no dst, wrong address families, etc...) we need to pass it back
1372 * to app (and any other rtsock consumers) with rtm_errno field set to
1375 * Returns 0 on success.
1378 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1381 struct rt_addrinfo info;
1382 struct sockaddr *sa;
1384 struct rt_msghdr *rtm;
1386 if (route_cb.any_count == 0)
1389 bzero((caddr_t)&info, sizeof(info));
1390 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1391 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1392 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1393 if ((m = rt_msg1(cmd, &info)) == NULL)
1395 rtm = mtod(m, struct rt_msghdr *);
1396 rtm->rtm_index = ifp->if_index;
1397 rtm->rtm_flags |= rt->rt_flags;
1398 rtm->rtm_errno = error;
1399 rtm->rtm_addrs = info.rti_addrs;
1401 if (fibnum != RT_ALL_FIBS) {
1402 M_SETFIB(m, fibnum);
1403 m->m_flags |= RTS_FILTER_FIB;
1406 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1412 * This is the analogue to the rt_newaddrmsg which performs the same
1413 * function but for multicast group memberhips. This is easier since
1414 * there is no route state to worry about.
1417 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1419 struct rt_addrinfo info;
1420 struct mbuf *m = NULL;
1421 struct ifnet *ifp = ifma->ifma_ifp;
1422 struct ifma_msghdr *ifmam;
1424 if (route_cb.any_count == 0)
1427 bzero((caddr_t)&info, sizeof(info));
1428 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1429 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1431 * If a link-layer address is present, present it as a ``gateway''
1432 * (similarly to how ARP entries, e.g., are presented).
1434 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1435 m = rt_msg1(cmd, &info);
1438 ifmam = mtod(m, struct ifma_msghdr *);
1439 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1441 ifmam->ifmam_index = ifp->if_index;
1442 ifmam->ifmam_addrs = info.rti_addrs;
1443 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1446 static struct mbuf *
1447 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1448 struct rt_addrinfo *info)
1450 struct if_announcemsghdr *ifan;
1453 if (route_cb.any_count == 0)
1455 bzero((caddr_t)info, sizeof(*info));
1456 m = rt_msg1(type, info);
1458 ifan = mtod(m, struct if_announcemsghdr *);
1459 ifan->ifan_index = ifp->if_index;
1460 strlcpy(ifan->ifan_name, ifp->if_xname,
1461 sizeof(ifan->ifan_name));
1462 ifan->ifan_what = what;
1468 * This is called to generate routing socket messages indicating
1469 * IEEE80211 wireless events.
1470 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1473 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1476 struct rt_addrinfo info;
1478 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1481 * Append the ieee80211 data. Try to stick it in the
1482 * mbuf containing the ifannounce msg; otherwise allocate
1483 * a new mbuf and append.
1485 * NB: we assume m is a single mbuf.
1487 if (data_len > M_TRAILINGSPACE(m)) {
1488 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1493 bcopy(data, mtod(n, void *), data_len);
1494 n->m_len = data_len;
1496 } else if (data_len > 0) {
1497 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1498 m->m_len += data_len;
1500 if (m->m_flags & M_PKTHDR)
1501 m->m_pkthdr.len += data_len;
1502 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1503 rt_dispatch(m, AF_UNSPEC);
1508 * This is called to generate routing socket messages indicating
1509 * network interface arrival and departure.
1512 rt_ifannouncemsg(struct ifnet *ifp, int what)
1515 struct rt_addrinfo info;
1517 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1519 rt_dispatch(m, AF_UNSPEC);
1523 rt_dispatch(struct mbuf *m, sa_family_t saf)
1528 * Preserve the family from the sockaddr, if any, in an m_tag for
1529 * use when injecting the mbuf into the routing socket buffer from
1532 if (saf != AF_UNSPEC) {
1533 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1539 *(unsigned short *)(tag + 1) = saf;
1540 m_tag_prepend(m, tag);
1544 m->m_pkthdr.rcvif = V_loif;
1550 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1554 * This is used in dumping the kernel table via sysctl().
1557 sysctl_dumpentry(struct radix_node *rn, void *vw)
1559 struct walkarg *w = vw;
1560 struct rtentry *rt = (struct rtentry *)rn;
1561 int error = 0, size;
1562 struct rt_addrinfo info;
1564 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1566 if ((rt->rt_flags & RTF_HOST) == 0
1567 ? jailed_without_vnet(w->w_req->td->td_ucred)
1568 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1570 bzero((caddr_t)&info, sizeof(info));
1571 info.rti_info[RTAX_DST] = rt_key(rt);
1572 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1573 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1574 info.rti_info[RTAX_GENMASK] = 0;
1576 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1577 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1578 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1579 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1581 size = rt_msg2(RTM_GET, &info, NULL, w);
1582 if (w->w_req && w->w_tmem) {
1583 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1585 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1586 rtm->rtm_flags = RTF_GATEWAY |
1587 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1589 rtm->rtm_flags = rt->rt_flags;
1590 rt_getmetrics(rt, &rtm->rtm_rmx);
1591 rtm->rtm_index = rt->rt_ifp->if_index;
1592 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1593 rtm->rtm_addrs = info.rti_addrs;
1594 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1600 #ifdef COMPAT_FREEBSD32
1602 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1605 bzero(dst, sizeof(*dst));
1606 CP(*src, *dst, ifi_type);
1607 CP(*src, *dst, ifi_physical);
1608 CP(*src, *dst, ifi_addrlen);
1609 CP(*src, *dst, ifi_hdrlen);
1610 CP(*src, *dst, ifi_link_state);
1611 CP(*src, *dst, ifi_vhid);
1612 CP(*src, *dst, ifi_baudrate_pf);
1613 dst->ifi_datalen = sizeof(struct if_data32);
1614 CP(*src, *dst, ifi_mtu);
1615 CP(*src, *dst, ifi_metric);
1616 CP(*src, *dst, ifi_baudrate);
1617 CP(*src, *dst, ifi_ipackets);
1618 CP(*src, *dst, ifi_ierrors);
1619 CP(*src, *dst, ifi_opackets);
1620 CP(*src, *dst, ifi_oerrors);
1621 CP(*src, *dst, ifi_collisions);
1622 CP(*src, *dst, ifi_ibytes);
1623 CP(*src, *dst, ifi_obytes);
1624 CP(*src, *dst, ifi_imcasts);
1625 CP(*src, *dst, ifi_omcasts);
1626 CP(*src, *dst, ifi_iqdrops);
1627 CP(*src, *dst, ifi_noproto);
1628 CP(*src, *dst, ifi_hwassist);
1629 CP(*src, *dst, ifi_epoch);
1630 TV_CP(*src, *dst, ifi_lastchange);
1635 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1636 struct walkarg *w, int len)
1638 struct if_msghdrl *ifm;
1640 #ifdef COMPAT_FREEBSD32
1641 if (w->w_req->flags & SCTL_MASK32) {
1642 struct if_msghdrl32 *ifm32;
1644 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1645 ifm32->ifm_addrs = info->rti_addrs;
1646 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1647 ifm32->ifm_index = ifp->if_index;
1648 ifm32->_ifm_spare1 = 0;
1649 ifm32->ifm_len = sizeof(*ifm32);
1650 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1652 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1653 /* Fixup if_data carp(4) vhid. */
1654 if (carp_get_vhid_p != NULL)
1655 ifm32->ifm_data.ifi_vhid =
1656 (*carp_get_vhid_p)(ifp->if_addr);
1657 ifm32->ifm_data.ifi_oqdrops = ifp->if_snd.ifq_drops;
1659 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1662 ifm = (struct if_msghdrl *)w->w_tmem;
1663 ifm->ifm_addrs = info->rti_addrs;
1664 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1665 ifm->ifm_index = ifp->if_index;
1666 ifm->_ifm_spare1 = 0;
1667 ifm->ifm_len = sizeof(*ifm);
1668 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1670 ifm->ifm_data = ifp->if_data;
1671 /* Fixup if_data carp(4) vhid. */
1672 if (carp_get_vhid_p != NULL)
1673 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1675 ifm->ifm_data.ifi_datalen += sizeof(u_long);
1676 ifm->ifi_oqdrops = ifp->if_snd.ifq_drops;
1678 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1682 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1683 struct walkarg *w, int len)
1685 struct if_msghdr *ifm;
1687 #ifdef COMPAT_FREEBSD32
1688 if (w->w_req->flags & SCTL_MASK32) {
1689 struct if_msghdr32 *ifm32;
1691 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1692 ifm32->ifm_addrs = info->rti_addrs;
1693 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1694 ifm32->ifm_index = ifp->if_index;
1696 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1697 /* Fixup if_data carp(4) vhid. */
1698 if (carp_get_vhid_p != NULL)
1699 ifm32->ifm_data.ifi_vhid =
1700 (*carp_get_vhid_p)(ifp->if_addr);
1702 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1705 ifm = (struct if_msghdr *)w->w_tmem;
1706 ifm->ifm_addrs = info->rti_addrs;
1707 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1708 ifm->ifm_index = ifp->if_index;
1710 ifm->ifm_data = ifp->if_data;
1711 /* Fixup if_data carp(4) vhid. */
1712 if (carp_get_vhid_p != NULL)
1713 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1715 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1719 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1720 struct walkarg *w, int len)
1722 struct ifa_msghdrl *ifam;
1724 #ifdef COMPAT_FREEBSD32
1725 if (w->w_req->flags & SCTL_MASK32) {
1726 struct ifa_msghdrl32 *ifam32;
1728 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1729 ifam32->ifam_addrs = info->rti_addrs;
1730 ifam32->ifam_flags = ifa->ifa_flags;
1731 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1732 ifam32->_ifam_spare1 = 0;
1733 ifam32->ifam_len = sizeof(*ifam32);
1734 ifam32->ifam_data_off =
1735 offsetof(struct ifa_msghdrl32, ifam_data);
1736 ifam32->ifam_metric = ifa->ifa_metric;
1738 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1739 /* Fixup if_data carp(4) vhid. */
1740 if (carp_get_vhid_p != NULL)
1741 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1743 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1747 ifam = (struct ifa_msghdrl *)w->w_tmem;
1748 ifam->ifam_addrs = info->rti_addrs;
1749 ifam->ifam_flags = ifa->ifa_flags;
1750 ifam->ifam_index = ifa->ifa_ifp->if_index;
1751 ifam->_ifam_spare1 = 0;
1752 ifam->ifam_len = sizeof(*ifam);
1753 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1754 ifam->ifam_metric = ifa->ifa_metric;
1756 ifam->ifam_data = ifa->if_data;
1757 /* Fixup if_data carp(4) vhid. */
1758 if (carp_get_vhid_p != NULL)
1759 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1761 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1765 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1766 struct walkarg *w, int len)
1768 struct ifa_msghdr *ifam;
1770 ifam = (struct ifa_msghdr *)w->w_tmem;
1771 ifam->ifam_addrs = info->rti_addrs;
1772 ifam->ifam_flags = ifa->ifa_flags;
1773 ifam->ifam_index = ifa->ifa_ifp->if_index;
1774 ifam->ifam_metric = ifa->ifa_metric;
1776 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1780 sysctl_iflist(int af, struct walkarg *w)
1784 struct rt_addrinfo info;
1787 bzero((caddr_t)&info, sizeof(info));
1788 IFNET_RLOCK_NOSLEEP();
1789 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1790 if (w->w_arg && w->w_arg != ifp->if_index)
1794 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1795 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1796 info.rti_info[RTAX_IFP] = NULL;
1797 if (w->w_req && w->w_tmem) {
1798 if (w->w_op == NET_RT_IFLISTL)
1799 error = sysctl_iflist_ifml(ifp, &info, w, len);
1801 error = sysctl_iflist_ifm(ifp, &info, w, len);
1805 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1806 if (af && af != ifa->ifa_addr->sa_family)
1808 if (prison_if(w->w_req->td->td_ucred,
1809 ifa->ifa_addr) != 0)
1811 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1812 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1813 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1814 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1815 if (w->w_req && w->w_tmem) {
1816 if (w->w_op == NET_RT_IFLISTL)
1817 error = sysctl_iflist_ifaml(ifa, &info,
1820 error = sysctl_iflist_ifam(ifa, &info,
1826 IF_ADDR_RUNLOCK(ifp);
1827 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1828 info.rti_info[RTAX_BRD] = NULL;
1832 IF_ADDR_RUNLOCK(ifp);
1833 IFNET_RUNLOCK_NOSLEEP();
1838 sysctl_ifmalist(int af, struct walkarg *w)
1841 struct ifmultiaddr *ifma;
1842 struct rt_addrinfo info;
1846 bzero((caddr_t)&info, sizeof(info));
1847 IFNET_RLOCK_NOSLEEP();
1848 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1849 if (w->w_arg && w->w_arg != ifp->if_index)
1852 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1854 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1855 if (af && af != ifma->ifma_addr->sa_family)
1857 if (prison_if(w->w_req->td->td_ucred,
1858 ifma->ifma_addr) != 0)
1860 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1861 info.rti_info[RTAX_GATEWAY] =
1862 (ifma->ifma_addr->sa_family != AF_LINK) ?
1863 ifma->ifma_lladdr : NULL;
1864 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1865 if (w->w_req && w->w_tmem) {
1866 struct ifma_msghdr *ifmam;
1868 ifmam = (struct ifma_msghdr *)w->w_tmem;
1869 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1870 ifmam->ifmam_flags = 0;
1871 ifmam->ifmam_addrs = info.rti_addrs;
1872 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1874 IF_ADDR_RUNLOCK(ifp);
1879 IF_ADDR_RUNLOCK(ifp);
1882 IFNET_RUNLOCK_NOSLEEP();
1887 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1889 int *name = (int *)arg1;
1890 u_int namelen = arg2;
1891 struct radix_node_head *rnh = NULL; /* silence compiler. */
1892 int i, lim, error = EINVAL;
1901 if (name[1] == NET_RT_DUMP) {
1903 fib = req->td->td_proc->p_fibnum;
1904 else if (namelen == 4)
1905 fib = (name[3] == RT_ALL_FIBS) ?
1906 req->td->td_proc->p_fibnum : name[3];
1908 return ((namelen < 3) ? EISDIR : ENOTDIR);
1909 if (fib < 0 || fib >= rt_numfibs)
1911 } else if (namelen != 3)
1912 return ((namelen < 3) ? EISDIR : ENOTDIR);
1916 bzero(&w, sizeof(w));
1921 error = sysctl_wire_old_buffer(req, 0);
1928 if (af == 0) { /* dump all tables */
1931 } else /* dump only one table */
1935 * take care of llinfo entries, the caller must
1938 if (w.w_op == NET_RT_FLAGS &&
1939 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1941 error = lltable_sysctl_dumparp(af, w.w_req);
1947 * take care of routing entries
1949 for (error = 0; error == 0 && i <= lim; i++) {
1950 rnh = rt_tables_get_rnh(fib, i);
1952 RADIX_NODE_HEAD_RLOCK(rnh);
1953 error = rnh->rnh_walktree(rnh,
1954 sysctl_dumpentry, &w);
1955 RADIX_NODE_HEAD_RUNLOCK(rnh);
1957 error = EAFNOSUPPORT;
1962 case NET_RT_IFLISTL:
1963 error = sysctl_iflist(af, &w);
1966 case NET_RT_IFMALIST:
1967 error = sysctl_ifmalist(af, &w);
1971 free(w.w_tmem, M_RTABLE);
1975 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1978 * Definitions of protocols supported in the ROUTE domain.
1981 static struct domain routedomain; /* or at least forward */
1983 static struct protosw routesw[] = {
1985 .pr_type = SOCK_RAW,
1986 .pr_domain = &routedomain,
1987 .pr_flags = PR_ATOMIC|PR_ADDR,
1988 .pr_output = route_output,
1989 .pr_ctlinput = raw_ctlinput,
1990 .pr_init = raw_init,
1991 .pr_usrreqs = &route_usrreqs
1995 static struct domain routedomain = {
1996 .dom_family = PF_ROUTE,
1997 .dom_name = "route",
1998 .dom_protosw = routesw,
1999 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
2002 VNET_DOMAIN_SET(route);