2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "opt_route.h"
34 #include "opt_inet6.h"
36 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/eventhandler.h>
40 #include <sys/domain.h>
42 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/rmlock.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_var.h>
57 #include <net/if_private.h>
58 #include <net/if_dl.h>
59 #include <net/if_llatbl.h>
60 #include <net/if_types.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
63 #include <net/route/route_ctl.h>
64 #include <net/route/route_var.h>
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip_carp.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/scope6_var.h>
75 #include <net/route/nhop.h>
77 #define DEBUG_MOD_NAME rtsock
78 #define DEBUG_MAX_LEVEL LOG_DEBUG
79 #include <net/route/route_debug.h>
80 _DECLARE_DEBUG(LOG_INFO);
82 #ifdef COMPAT_FREEBSD32
83 #include <sys/mount.h>
84 #include <compat/freebsd32/freebsd32.h>
94 struct if_data ifm_data;
104 uint16_t _ifm_spare1;
106 uint16_t ifm_data_off;
107 uint32_t _ifm_spare2;
108 struct if_data ifm_data;
111 struct ifa_msghdrl32 {
112 uint16_t ifam_msglen;
113 uint8_t ifam_version;
118 uint16_t _ifam_spare1;
120 uint16_t ifam_data_off;
122 struct if_data ifam_data;
125 #define SA_SIZE32(sa) \
126 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
128 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
130 #endif /* COMPAT_FREEBSD32 */
132 struct linear_buffer {
133 char *base; /* Base allocated memory pointer */
134 uint32_t offset; /* Currently used offset */
135 uint32_t size; /* Total buffer size */
137 #define SCRATCH_BUFFER_SIZE 1024
139 #define RTS_PID_LOG(_l, _fmt, ...) RT_LOG_##_l(_l, "PID %d: " _fmt, curproc ? curproc->p_pid : 0, ## __VA_ARGS__)
141 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
143 /* NB: these are not modified */
144 static struct sockaddr route_src = { 2, PF_ROUTE, };
145 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
147 /* These are external hooks for CARP. */
148 int (*carp_get_vhid_p)(struct ifaddr *);
151 * Used by rtsock callback code to decide whether to filter the update
152 * notification to a socket bound to a particular FIB.
154 #define RTS_FILTER_FIB M_PROTO8
156 * Used to store address family of the notification.
158 #define m_rtsock_family m_pkthdr.PH_loc.eight[0]
161 LIST_ENTRY(rcb) list;
162 struct socket *rcb_socket;
163 sa_family_t rcb_family;
167 LIST_HEAD(, rcb) cblist;
168 int ip_count; /* attached w/ AF_INET */
169 int ip6_count; /* attached w/ AF_INET6 */
170 int any_count; /* total attached */
172 VNET_DEFINE_STATIC(route_cb_t, route_cb);
173 #define V_route_cb VNET(route_cb)
175 struct mtx rtsock_mtx;
176 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
178 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
179 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
180 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
182 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
189 struct sysctl_req *w_req;
190 struct sockaddr *dst;
191 struct sockaddr *mask;
194 static void rts_input(struct mbuf *m);
195 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
196 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
197 struct walkarg *w, int *plen);
198 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
199 struct rt_addrinfo *rtinfo);
200 static int cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb);
201 static int sysctl_dumpentry(struct rtentry *rt, void *vw);
202 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
203 uint32_t weight, struct walkarg *w);
204 static int sysctl_iflist(int af, struct walkarg *w);
205 static int sysctl_ifmalist(int af, struct walkarg *w);
206 static void rt_getmetrics(const struct rtentry *rt,
207 const struct nhop_object *nh, struct rt_metrics *out);
208 static void rt_dispatch(struct mbuf *, sa_family_t);
209 static void rt_ifannouncemsg(struct ifnet *ifp, int what);
210 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
211 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
212 static int update_rtm_from_rc(struct rt_addrinfo *info,
213 struct rt_msghdr **prtm, int alloc_len,
214 struct rib_cmd_info *rc, struct nhop_object *nh);
215 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
216 struct mbuf *m, sa_family_t saf, u_int fibnum,
218 static void rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc);
219 static void rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask);
221 static struct netisr_handler rtsock_nh = {
223 .nh_handler = rts_input,
224 .nh_proto = NETISR_ROUTE,
225 .nh_policy = NETISR_POLICY_SOURCE,
229 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
233 netisr_getqlimit(&rtsock_nh, &qlimit);
234 error = sysctl_handle_int(oidp, &qlimit, 0, req);
235 if (error || !req->newptr)
239 return (netisr_setqlimit(&rtsock_nh, qlimit));
241 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
242 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
243 0, 0, sysctl_route_netisr_maxqlen, "I",
244 "maximum routing socket dispatch queue length");
251 if (IS_DEFAULT_VNET(curvnet)) {
252 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
253 rtsock_nh.nh_qlimit = tmp;
254 netisr_register(&rtsock_nh);
258 netisr_register_vnet(&rtsock_nh);
261 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
266 vnet_rts_uninit(void)
269 netisr_unregister_vnet(&rtsock_nh);
271 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
276 report_route_event(const struct rib_cmd_info *rc, void *_cbdata)
278 uint32_t fibnum = (uint32_t)(uintptr_t)_cbdata;
279 struct nhop_object *nh;
281 nh = rc->rc_cmd == RTM_DELETE ? rc->rc_nh_old : rc->rc_nh_new;
282 rt_routemsg(rc->rc_cmd, rc->rc_rt, nh, fibnum);
286 rts_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
289 if ((rc->rc_nh_new && NH_IS_NHGRP(rc->rc_nh_new)) ||
290 (rc->rc_nh_old && NH_IS_NHGRP(rc->rc_nh_old))) {
291 rib_decompose_notification(rc, report_route_event,
292 (void *)(uintptr_t)fibnum);
295 report_route_event(rc, (void *)(uintptr_t)fibnum);
297 static struct rtbridge rtsbridge = {
298 .route_f = rts_handle_route_event,
299 .ifmsg_f = rtsock_ifmsg,
301 static struct rtbridge *rtsbridge_orig_p;
304 rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc)
306 netlink_callback_p->route_f(fibnum, rc);
312 rtsbridge_orig_p = rtsock_callback_p;
313 rtsock_callback_p = &rtsbridge;
315 SYSINIT(rtsock_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtsock_init, NULL);
318 rts_handle_ifnet_arrival(void *arg __unused, struct ifnet *ifp)
320 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
322 EVENTHANDLER_DEFINE(ifnet_arrival_event, rts_handle_ifnet_arrival, NULL, 0);
325 rts_handle_ifnet_departure(void *arg __unused, struct ifnet *ifp)
327 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
329 EVENTHANDLER_DEFINE(ifnet_departure_event, rts_handle_ifnet_departure, NULL, 0);
332 rts_append_data(struct socket *so, struct mbuf *m)
335 if (sbappendaddr(&so->so_rcv, &route_src, m, NULL) == 0) {
343 rts_input(struct mbuf *m)
350 LIST_FOREACH(rcb, &V_route_cb.cblist, list) {
351 if (rcb->rcb_family != AF_UNSPEC &&
352 rcb->rcb_family != m->m_rtsock_family)
354 if ((m->m_flags & RTS_FILTER_FIB) &&
355 M_GETFIB(m) != rcb->rcb_socket->so_fibnum)
360 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
362 rts_append_data(last, n);
364 last = rcb->rcb_socket;
367 rts_append_data(last, m);
374 rts_close(struct socket *so)
377 soisdisconnected(so);
380 static SYSCTL_NODE(_net, OID_AUTO, rtsock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
381 "Routing socket infrastructure");
382 static u_long rts_sendspace = 8192;
383 SYSCTL_ULONG(_net_rtsock, OID_AUTO, sendspace, CTLFLAG_RW, &rts_sendspace, 0,
384 "Default routing socket send space");
385 static u_long rts_recvspace = 8192;
386 SYSCTL_ULONG(_net_rtsock, OID_AUTO, recvspace, CTLFLAG_RW, &rts_recvspace, 0,
387 "Default routing socket receive space");
390 rts_attach(struct socket *so, int proto, struct thread *td)
395 error = soreserve(so, rts_sendspace, rts_recvspace);
399 rcb = malloc(sizeof(*rcb), M_PCB, M_WAITOK);
400 rcb->rcb_socket = so;
401 rcb->rcb_family = proto;
404 so->so_fibnum = td->td_proc->p_fibnum;
405 so->so_options |= SO_USELOOPBACK;
408 LIST_INSERT_HEAD(&V_route_cb.cblist, rcb, list);
411 V_route_cb.ip_count++;
414 V_route_cb.ip6_count++;
417 V_route_cb.any_count++;
425 rts_detach(struct socket *so)
427 struct rcb *rcb = so->so_pcb;
430 LIST_REMOVE(rcb, list);
431 switch(rcb->rcb_family) {
433 V_route_cb.ip_count--;
436 V_route_cb.ip6_count--;
439 V_route_cb.any_count--;
446 rts_disconnect(struct socket *so)
453 rts_shutdown(struct socket *so, enum shutdown_how how)
456 * Note: route socket marks itself as connected through its lifetime.
472 #ifndef _SOCKADDR_UNION_DEFINED
473 #define _SOCKADDR_UNION_DEFINED
475 * The union of all possible address formats we handle.
477 union sockaddr_union {
479 struct sockaddr_in sin;
480 struct sockaddr_in6 sin6;
482 #endif /* _SOCKADDR_UNION_DEFINED */
485 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
486 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
488 #if defined(INET) || defined(INET6)
489 struct epoch_tracker et;
492 /* First, see if the returned address is part of the jail. */
493 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
494 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
498 switch (info->rti_info[RTAX_DST]->sa_family) {
508 * Try to find an address on the given outgoing interface
509 * that belongs to the jail.
512 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
515 if (sa->sa_family != AF_INET)
517 ia = ((struct sockaddr_in *)sa)->sin_addr;
518 if (prison_check_ip4(cred, &ia) == 0) {
526 * As a last resort return the 'default' jail address.
528 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
530 if (prison_get_ip4(cred, &ia) != 0)
533 bzero(&saun->sin, sizeof(struct sockaddr_in));
534 saun->sin.sin_len = sizeof(struct sockaddr_in);
535 saun->sin.sin_family = AF_INET;
536 saun->sin.sin_addr.s_addr = ia.s_addr;
537 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
550 * Try to find an address on the given outgoing interface
551 * that belongs to the jail.
554 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
557 if (sa->sa_family != AF_INET6)
559 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
560 &ia6, sizeof(struct in6_addr));
561 if (prison_check_ip6(cred, &ia6) == 0) {
569 * As a last resort return the 'default' jail address.
571 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
573 if (prison_get_ip6(cred, &ia6) != 0)
576 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
577 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
578 saun->sin6.sin6_family = AF_INET6;
579 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
580 if (sa6_recoverscope(&saun->sin6) != 0)
582 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
593 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
598 if (V_loif == NULL) {
599 RTS_PID_LOG(LOG_INFO, "Unable to add blackhole/reject nhop without loopback");
602 info->rti_ifp = V_loif;
604 saf = info->rti_info[RTAX_DST]->sa_family;
606 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
607 if (ifa->ifa_addr->sa_family == saf) {
612 if (info->rti_ifa == NULL) {
613 RTS_PID_LOG(LOG_INFO, "Unable to find ifa for blackhole/reject nhop");
617 bzero(saun, sizeof(union sockaddr_union));
621 saun->sin.sin_family = AF_INET;
622 saun->sin.sin_len = sizeof(struct sockaddr_in);
623 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
628 saun->sin6.sin6_family = AF_INET6;
629 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
630 saun->sin6.sin6_addr = in6addr_loopback;
634 RTS_PID_LOG(LOG_INFO, "unsupported family: %d", saf);
637 info->rti_info[RTAX_GATEWAY] = &saun->sa;
638 info->rti_flags |= RTF_GATEWAY;
644 * Fills in @info based on userland-provided @rtm message.
646 * Returns 0 on success.
649 fill_addrinfo(struct rt_msghdr *rtm, int len, struct linear_buffer *lb, u_int fibnum,
650 struct rt_addrinfo *info)
654 rtm->rtm_pid = curproc->p_pid;
655 info->rti_addrs = rtm->rtm_addrs;
657 info->rti_mflags = rtm->rtm_inits;
658 info->rti_rmx = &rtm->rtm_rmx;
661 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
662 * link-local address because rtrequest requires addresses with
665 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
668 info->rti_flags = rtm->rtm_flags;
669 error = cleanup_xaddrs(info, lb);
673 * Verify that the caller has the appropriate privilege; RTM_GET
674 * is the only operation the non-superuser is allowed.
676 if (rtm->rtm_type != RTM_GET) {
677 error = priv_check(curthread, PRIV_NET_ROUTE);
683 * The given gateway address may be an interface address.
684 * For example, issuing a "route change" command on a route
685 * entry that was created from a tunnel, and the gateway
686 * address given is the local end point. In this case the
687 * RTF_GATEWAY flag must be cleared or the destination will
688 * not be reachable even though there is no error message.
690 if (info->rti_info[RTAX_GATEWAY] != NULL &&
691 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
692 struct nhop_object *nh;
695 * A host route through the loopback interface is
696 * installed for each interface address. In pre 8.0
697 * releases the interface address of a PPP link type
698 * is not reachable locally. This behavior is fixed as
699 * part of the new L2/L3 redesign and rewrite work. The
700 * signature of this interface address route is the
701 * AF_LINK sa_family type of the gateway, and the
702 * rt_ifp has the IFF_LOOPBACK flag set.
704 nh = rib_lookup(fibnum, info->rti_info[RTAX_GATEWAY], NHR_NONE, 0);
705 if (nh != NULL && nh->gw_sa.sa_family == AF_LINK &&
706 nh->nh_ifp->if_flags & IFF_LOOPBACK) {
707 info->rti_flags &= ~RTF_GATEWAY;
708 info->rti_flags |= RTF_GWFLAG_COMPAT;
715 static struct nhop_object *
716 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
718 if (!NH_IS_NHGRP(nh))
721 const struct weightened_nhop *wn;
723 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
726 for (int i = 0; i < num_nhops; i++) {
727 if (match_nhop_gw(wn[i].nh, gw))
735 * Handles RTM_GET message from routing socket, returning matching rt.
738 * 0 on success, with locked and referenced matching rt in @rt_nrt
742 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
743 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
746 struct rib_head *rnh;
747 struct nhop_object *nh;
750 saf = info->rti_info[RTAX_DST]->sa_family;
752 rnh = rt_tables_get_rnh(fibnum, saf);
754 return (EAFNOSUPPORT);
759 * By (implicit) convention host route (one without netmask)
760 * means longest-prefix-match request and the route with netmask
761 * means exact-match lookup.
762 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
763 * prefixes, use original data to check for the netmask presence.
765 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
767 * Provide longest prefix match for
768 * address lookup (no mask).
769 * 'route -n get addr'
771 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
772 info->rti_info[RTAX_DST], &rnh->head);
774 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
775 info->rti_info[RTAX_DST],
776 info->rti_info[RTAX_NETMASK], &rnh->head);
778 if (rc->rc_rt == NULL) {
783 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
789 * If performing proxied L2 entry insertion, and
790 * the actual PPP host entry is found, perform
791 * another search to retrieve the prefix route of
792 * the local end point of the PPP link.
793 * TODO: move this logic to userland.
795 if (rtm->rtm_flags & RTF_ANNOUNCE) {
796 struct sockaddr_storage laddr;
798 if (nh->nh_ifp != NULL &&
799 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
802 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
805 rt_maskedcopy(ifa->ifa_addr,
806 (struct sockaddr *)&laddr,
809 rt_maskedcopy(nh->nh_ifa->ifa_addr,
810 (struct sockaddr *)&laddr,
811 nh->nh_ifa->ifa_netmask);
813 * refactor rt and no lock operation necessary
815 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(
816 (struct sockaddr *)&laddr, &rnh->head);
817 if (rc->rc_rt == NULL) {
821 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
828 rc->rc_nh_weight = rc->rc_rt->rt_weight;
835 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
838 if (family == AF_INET) {
839 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
840 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
842 bzero(dst4, sizeof(struct sockaddr_in));
843 bzero(mask4, sizeof(struct sockaddr_in));
845 dst4->sin_family = AF_INET;
846 dst4->sin_len = sizeof(struct sockaddr_in);
847 mask4->sin_family = AF_INET;
848 mask4->sin_len = sizeof(struct sockaddr_in);
852 if (family == AF_INET6) {
853 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
854 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
856 bzero(dst6, sizeof(struct sockaddr_in6));
857 bzero(mask6, sizeof(struct sockaddr_in6));
859 dst6->sin6_family = AF_INET6;
860 dst6->sin6_len = sizeof(struct sockaddr_in6);
861 mask6->sin6_family = AF_INET6;
862 mask6->sin6_len = sizeof(struct sockaddr_in6);
868 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
869 struct sockaddr *mask)
872 if (dst->sa_family == AF_INET) {
873 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
874 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
875 uint32_t scopeid = 0;
876 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
882 if (dst->sa_family == AF_INET6) {
883 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
884 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
885 uint32_t scopeid = 0;
886 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
887 &mask6->sin6_addr, &scopeid);
888 dst6->sin6_scope_id = scopeid;
895 update_rtm_from_info(struct rt_addrinfo *info, struct rt_msghdr **prtm,
898 struct rt_msghdr *rtm, *orig_rtm = NULL;
903 /* Check if we need to realloc storage */
904 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
905 if (len > alloc_len) {
906 struct rt_msghdr *tmp_rtm;
908 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
911 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
917 * Delay freeing original rtm as info contains
918 * data referencing it.
922 w.w_tmem = (caddr_t)rtm;
923 w.w_tmemsize = alloc_len;
924 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
925 rtm->rtm_addrs = info->rti_addrs;
927 if (orig_rtm != NULL)
928 free(orig_rtm, M_TEMP);
935 * Update sockaddrs, flags, etc in @prtm based on @rc data.
936 * rtm can be reallocated.
938 * Returns 0 on success, along with pointer to (potentially reallocated)
943 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
944 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
946 union sockaddr_union saun;
947 struct rt_msghdr *rtm;
952 union sockaddr_union sa_dst, sa_mask;
953 int family = info->rti_info[RTAX_DST]->sa_family;
954 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
955 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
957 info->rti_info[RTAX_DST] = &sa_dst.sa;
958 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
959 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
960 info->rti_info[RTAX_GENMASK] = 0;
962 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
964 info->rti_info[RTAX_IFP] =
965 ifp->if_addr->ifa_addr;
966 error = rtm_get_jailed(info, ifp, nh,
967 &saun, curthread->td_ucred);
970 if (ifp->if_flags & IFF_POINTOPOINT)
971 info->rti_info[RTAX_BRD] =
972 nh->nh_ifa->ifa_dstaddr;
973 rtm->rtm_index = ifp->if_index;
975 info->rti_info[RTAX_IFP] = NULL;
976 info->rti_info[RTAX_IFA] = NULL;
978 } else if (ifp != NULL)
979 rtm->rtm_index = ifp->if_index;
981 if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0)
985 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
986 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
987 rtm->rtm_flags = RTF_GATEWAY |
988 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
989 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
990 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
997 save_del_notification(const struct rib_cmd_info *rc, void *_cbdata)
999 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1001 if (rc->rc_cmd == RTM_DELETE)
1006 save_add_notification(const struct rib_cmd_info *rc, void *_cbdata)
1008 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1010 if (rc->rc_cmd == RTM_ADD)
1015 #if defined(INET6) || defined(INET)
1016 static struct sockaddr *
1017 alloc_sockaddr_aligned(struct linear_buffer *lb, int len)
1019 len = roundup2(len, sizeof(uint64_t));
1020 if (lb->offset + len > lb->size)
1022 struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset);
1029 rts_send(struct socket *so, int flags, struct mbuf *m,
1030 struct sockaddr *nam, struct mbuf *control, struct thread *td)
1032 struct rt_msghdr *rtm = NULL;
1033 struct rt_addrinfo info;
1034 struct epoch_tracker et;
1036 struct sockaddr_storage ss;
1037 struct sockaddr_in6 *sin6;
1038 int i, rti_need_deembed = 0;
1040 int alloc_len = 0, len, error = 0, fibnum;
1041 sa_family_t saf = AF_UNSPEC;
1042 struct rib_cmd_info rc;
1043 struct nhop_object *nh;
1045 if ((flags & PRUS_OOB) || control != NULL) {
1047 if (control != NULL)
1049 return (EOPNOTSUPP);
1052 fibnum = so->so_fibnum;
1053 #define senderr(e) { error = e; goto flush;}
1054 if (m == NULL || ((m->m_len < sizeof(long)) &&
1055 (m = m_pullup(m, sizeof(long))) == NULL))
1057 if ((m->m_flags & M_PKTHDR) == 0)
1058 panic("route_output");
1059 NET_EPOCH_ENTER(et);
1060 len = m->m_pkthdr.len;
1061 if (len < sizeof(*rtm) ||
1062 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1066 * Most of current messages are in range 200-240 bytes,
1067 * minimize possible re-allocation on reply using larger size
1068 * buffer aligned on 1k boundaty.
1070 alloc_len = roundup2(len, 1024);
1071 int total_len = alloc_len + SCRATCH_BUFFER_SIZE;
1072 if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL)
1075 m_copydata(m, 0, len, (caddr_t)rtm);
1076 bzero(&info, sizeof(info));
1078 struct linear_buffer lb = {
1079 .base = (char *)rtm + alloc_len,
1080 .size = SCRATCH_BUFFER_SIZE,
1083 if (rtm->rtm_version != RTM_VERSION) {
1084 /* Do not touch message since format is unknown */
1087 senderr(EPROTONOSUPPORT);
1091 * Starting from here, it is possible
1092 * to alter original message and insert
1093 * caller PID and error value.
1096 if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) {
1099 /* fill_addringo() embeds scope into IPv6 addresses */
1101 rti_need_deembed = 1;
1104 saf = info.rti_info[RTAX_DST]->sa_family;
1106 /* support for new ARP code */
1107 if (rtm->rtm_flags & RTF_LLDATA) {
1108 error = lla_rt_output(rtm, &info);
1112 union sockaddr_union gw_saun;
1113 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1114 if (blackhole_flags != 0) {
1115 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1116 error = fill_blackholeinfo(&info, &gw_saun);
1118 RTS_PID_LOG(LOG_DEBUG, "both BLACKHOLE and REJECT flags specifiied");
1125 switch (rtm->rtm_type) {
1128 if (rtm->rtm_type == RTM_ADD) {
1129 if (info.rti_info[RTAX_GATEWAY] == NULL) {
1130 RTS_PID_LOG(LOG_DEBUG, "RTM_ADD w/o gateway");
1134 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1136 rtsock_notify_event(fibnum, &rc);
1138 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1139 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1140 struct rib_cmd_info rc_simple = {};
1141 rib_decompose_notification(&rc,
1142 save_add_notification, (void *)&rc_simple);
1146 /* nh MAY be empty if RTM_CHANGE request is no-op */
1149 rtm->rtm_index = nh->nh_ifp->if_index;
1150 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1156 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1158 rtsock_notify_event(fibnum, &rc);
1160 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1161 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1162 struct rib_cmd_info rc_simple = {};
1163 rib_decompose_notification(&rc,
1164 save_del_notification, (void *)&rc_simple);
1173 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1178 if (!rt_is_exportable(rc.rc_rt, curthread->td_ucred))
1183 senderr(EOPNOTSUPP);
1186 if (error == 0 && nh != NULL) {
1187 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1189 * Note that some sockaddr pointers may have changed to
1190 * point to memory outsize @rtm. Some may be pointing
1191 * to the on-stack variables.
1192 * Given that, any pointer in @info CANNOT BE USED.
1196 * scopeid deembedding has been performed while
1197 * writing updated rtm in rtsock_msg_buffer().
1198 * With that in mind, skip deembedding procedure below.
1201 rti_need_deembed = 0;
1210 if (rti_need_deembed) {
1211 /* sin6_scope_id is recovered before sending rtm. */
1212 sin6 = (struct sockaddr_in6 *)&ss;
1213 for (i = 0; i < RTAX_MAX; i++) {
1214 if (info.rti_info[i] == NULL)
1216 if (info.rti_info[i]->sa_family != AF_INET6)
1218 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1219 if (sa6_recoverscope(sin6) == 0)
1220 bcopy(sin6, info.rti_info[i],
1223 if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) {
1230 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1236 * Sends the prepared reply message in @rtm to all rtsock clients.
1237 * Frees @m and @rtm.
1241 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1242 sa_family_t saf, u_int fibnum, int rtm_errno)
1244 struct rcb *rcb = NULL;
1247 * Check to see if we don't want our own messages.
1249 if ((so->so_options & SO_USELOOPBACK) == 0) {
1250 if (V_route_cb.any_count <= 1) {
1256 /* There is another listener, so construct message */
1262 rtm->rtm_errno = rtm_errno;
1264 rtm->rtm_flags |= RTF_DONE;
1266 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1267 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1270 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1271 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1276 M_SETFIB(m, fibnum);
1277 m->m_flags |= RTS_FILTER_FIB;
1280 * XXX insure we don't get a copy by
1281 * invalidating our protocol
1283 sa_family_t family = rcb->rcb_family;
1284 rcb->rcb_family = AF_UNSPEC;
1285 rt_dispatch(m, saf);
1286 rcb->rcb_family = family;
1288 rt_dispatch(m, saf);
1293 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1294 struct rt_metrics *out)
1297 bzero(out, sizeof(*out));
1298 out->rmx_mtu = nh->nh_mtu;
1299 out->rmx_weight = rt->rt_weight;
1300 out->rmx_nhidx = nhop_get_idx(nh);
1301 /* Kernel -> userland timebase conversion. */
1302 out->rmx_expire = nhop_get_expire(nh) ?
1303 nhop_get_expire(nh) - time_uptime + time_second : 0;
1307 * Extract the addresses of the passed sockaddrs.
1308 * Do a little sanity checking so as to avoid bad memory references.
1309 * This data is derived straight from userland.
1312 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1314 struct sockaddr *sa;
1317 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1318 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1320 sa = (struct sockaddr *)cp;
1324 if (cp + sa->sa_len > cplim) {
1325 RTS_PID_LOG(LOG_DEBUG, "sa_len too big for sa type %d", i);
1329 * there are no more.. quit now
1330 * If there are more bits, they are in error.
1331 * I've seen this. route(1) can evidently generate these.
1332 * This causes kernel to core dump.
1333 * for compatibility, If we see this, point to a safe address.
1335 if (sa->sa_len == 0) {
1336 rtinfo->rti_info[i] = &sa_zero;
1337 return (0); /* should be EINVAL but for compat */
1341 if (sa->sa_family == AF_INET6)
1342 sa6_embedscope((struct sockaddr_in6 *)sa,
1345 rtinfo->rti_info[i] = sa;
1353 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1356 const struct sockaddr_in nsin = {
1357 .sin_family = AF_INET,
1358 .sin_len = sizeof(struct sockaddr_in),
1367 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1371 const struct sockaddr_in6 nsin6 = {
1372 .sin6_family = AF_INET6,
1373 .sin6_len = sizeof(struct sockaddr_in6),
1374 .sin6_addr = *addr6,
1375 .sin6_scope_id = scopeid,
1381 #if defined(INET6) || defined(INET)
1383 * Checks if gateway is suitable for lltable operations.
1384 * Lltable code requires AF_LINK gateway with ifindex
1385 * and mac address specified.
1386 * Returns 0 on success.
1389 cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1391 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1393 if (sdl->sdl_family != AF_LINK)
1396 if (sdl->sdl_index == 0) {
1397 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gateway w/o ifindex");
1401 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len) {
1402 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gw: sdl_nlen/sdl_alen too large");
1410 cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb)
1412 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1413 struct sockaddr *sa;
1415 if (info->rti_flags & RTF_LLDATA)
1416 return (cleanup_xaddrs_lladdr(info));
1418 switch (gw->sa_family) {
1422 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1424 /* Ensure reads do not go beyoud SA boundary */
1425 if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) {
1426 RTS_PID_LOG(LOG_DEBUG, "gateway sin_len too small: %d",
1430 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in));
1433 fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr);
1434 info->rti_info[RTAX_GATEWAY] = sa;
1441 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1442 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1443 RTS_PID_LOG(LOG_DEBUG, "gateway sin6_len too small: %d",
1447 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1453 struct sockaddr_dl *gw_sdl;
1455 size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data);
1456 gw_sdl = (struct sockaddr_dl *)gw;
1457 if (gw_sdl->sdl_len < sdl_min_len) {
1458 RTS_PID_LOG(LOG_DEBUG, "gateway sdl_len too small: %d",
1462 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short));
1466 const struct sockaddr_dl_short sdl = {
1467 .sdl_family = AF_LINK,
1468 .sdl_len = sizeof(struct sockaddr_dl_short),
1469 .sdl_index = gw_sdl->sdl_index,
1471 *((struct sockaddr_dl_short *)sa) = sdl;
1472 info->rti_info[RTAX_GATEWAY] = sa;
1482 remove_netmask(struct rt_addrinfo *info)
1484 info->rti_info[RTAX_NETMASK] = NULL;
1485 info->rti_flags |= RTF_HOST;
1486 info->rti_addrs &= ~RTA_NETMASK;
1491 cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb)
1493 struct sockaddr_in *dst_sa, *mask_sa;
1494 const int sa_len = sizeof(struct sockaddr_in);
1495 struct in_addr dst, mask;
1497 /* Check & fixup dst/netmask combination first */
1498 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1499 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1501 /* Ensure reads do not go beyound the buffer size */
1502 if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero)) {
1503 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin_len too small: %d",
1508 if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1510 * Some older routing software encode mask length into the
1511 * sin_len, thus resulting in "truncated" sockaddr.
1513 int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr);
1516 if (len > sizeof(struct in_addr))
1517 len = sizeof(struct in_addr);
1518 memcpy(&mask, &mask_sa->sin_addr, len);
1520 RTS_PID_LOG(LOG_DEBUG, "prefix mask sin_len too small: %d",
1525 mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST;
1527 dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr));
1529 /* Construct new "clean" dst/mask sockaddresses */
1530 if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1532 fill_sockaddr_inet(dst_sa, dst);
1533 info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa;
1535 if (mask.s_addr != INADDR_BROADCAST) {
1536 if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1538 fill_sockaddr_inet(mask_sa, mask);
1539 info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa;
1540 info->rti_flags &= ~RTF_HOST;
1542 remove_netmask(info);
1545 if (info->rti_info[RTAX_GATEWAY] != NULL)
1546 return (cleanup_xaddrs_gateway(info, lb));
1554 cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb)
1556 struct sockaddr *sa;
1557 struct sockaddr_in6 *dst_sa, *mask_sa;
1558 struct in6_addr mask, *dst;
1559 const int sa_len = sizeof(struct sockaddr_in6);
1561 /* Check & fixup dst/netmask combination first */
1562 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1563 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1565 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1566 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin6_len too small: %d",
1571 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1573 * Some older routing software encode mask length into the
1574 * sin6_len, thus resulting in "truncated" sockaddr.
1576 int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1578 bzero(&mask, sizeof(mask));
1579 if (len > sizeof(struct in6_addr))
1580 len = sizeof(struct in6_addr);
1581 memcpy(&mask, &mask_sa->sin6_addr, len);
1583 RTS_PID_LOG(LOG_DEBUG, "rtsock: prefix mask sin6_len too small: %d",
1588 mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1590 dst = &dst_sa->sin6_addr;
1591 IN6_MASK_ADDR(dst, &mask);
1593 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1595 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0);
1596 info->rti_info[RTAX_DST] = sa;
1598 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) {
1599 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1601 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0);
1602 info->rti_info[RTAX_NETMASK] = sa;
1603 info->rti_flags &= ~RTF_HOST;
1605 remove_netmask(info);
1608 if (info->rti_info[RTAX_GATEWAY] != NULL)
1609 return (cleanup_xaddrs_gateway(info, lb));
1616 cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb)
1618 int error = EAFNOSUPPORT;
1620 if (info->rti_info[RTAX_DST] == NULL) {
1621 RTS_PID_LOG(LOG_DEBUG, "prefix dst is not set");
1625 if (info->rti_flags & RTF_LLDATA) {
1627 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1628 * prefix along with the actual address in RTA_DST.
1629 * Remove netmask to avoid unnecessary address masking.
1631 remove_netmask(info);
1634 switch (info->rti_info[RTAX_DST]->sa_family) {
1637 error = cleanup_xaddrs_inet(info, lb);
1642 error = cleanup_xaddrs_inet6(info, lb);
1651 * Fill in @dmask with valid netmask leaving original @smask
1652 * intact. Mostly used with radix netmasks.
1655 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1656 struct sockaddr_storage *dmask)
1658 if (dst == NULL || smask == NULL)
1661 memset(dmask, 0, dst->sa_len);
1662 memcpy(dmask, smask, smask->sa_len);
1663 dmask->ss_len = dst->sa_len;
1664 dmask->ss_family = dst->sa_family;
1666 return ((struct sockaddr *)dmask);
1670 * Writes information related to @rtinfo object to newly-allocated mbuf.
1671 * Assumes MCLBYTES is enough to construct any message.
1672 * Used for OS notifications of vaious events (if/ifa announces,etc)
1674 * Returns allocated mbuf or NULL on failure.
1676 static struct mbuf *
1677 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1679 struct sockaddr_storage ss;
1680 struct rt_msghdr *rtm;
1683 struct sockaddr *sa;
1685 struct sockaddr_in6 *sin6;
1692 len = sizeof(struct ifa_msghdr);
1697 len = sizeof(struct ifma_msghdr);
1701 len = sizeof(struct if_msghdr);
1704 case RTM_IFANNOUNCE:
1706 len = sizeof(struct if_announcemsghdr);
1710 len = sizeof(struct rt_msghdr);
1713 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1714 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1716 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1718 m = m_gethdr(M_NOWAIT, MT_DATA);
1722 m->m_pkthdr.len = m->m_len = len;
1723 rtm = mtod(m, struct rt_msghdr *);
1724 bzero((caddr_t)rtm, len);
1725 for (i = 0; i < RTAX_MAX; i++) {
1726 if ((sa = rtinfo->rti_info[i]) == NULL)
1728 rtinfo->rti_addrs |= (1 << i);
1731 KASSERT(dlen <= sizeof(ss),
1732 ("%s: sockaddr size overflow", __func__));
1733 bzero(&ss, sizeof(ss));
1734 bcopy(sa, &ss, sa->sa_len);
1735 sa = (struct sockaddr *)&ss;
1737 if (sa->sa_family == AF_INET6) {
1738 sin6 = (struct sockaddr_in6 *)sa;
1739 (void)sa6_recoverscope(sin6);
1742 m_copyback(m, len, dlen, (caddr_t)sa);
1745 if (m->m_pkthdr.len != len) {
1749 rtm->rtm_msglen = len;
1750 rtm->rtm_version = RTM_VERSION;
1751 rtm->rtm_type = type;
1756 * Writes information related to @rtinfo object to preallocated buffer.
1757 * Stores needed size in @plen. If @w is NULL, calculates size without
1759 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1761 * Returns 0 on success.
1765 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1767 struct sockaddr_storage ss;
1768 int len, buflen = 0, dlen, i;
1770 struct rt_msghdr *rtm = NULL;
1772 struct sockaddr_in6 *sin6;
1774 #ifdef COMPAT_FREEBSD32
1775 bool compat32 = false;
1781 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1782 #ifdef COMPAT_FREEBSD32
1783 if (w->w_req->flags & SCTL_MASK32) {
1784 len = sizeof(struct ifa_msghdrl32);
1788 len = sizeof(struct ifa_msghdrl);
1790 len = sizeof(struct ifa_msghdr);
1794 #ifdef COMPAT_FREEBSD32
1795 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1796 if (w->w_op == NET_RT_IFLISTL)
1797 len = sizeof(struct if_msghdrl32);
1799 len = sizeof(struct if_msghdr32);
1804 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1805 len = sizeof(struct if_msghdrl);
1807 len = sizeof(struct if_msghdr);
1811 len = sizeof(struct ifma_msghdr);
1815 len = sizeof(struct rt_msghdr);
1819 rtm = (struct rt_msghdr *)w->w_tmem;
1820 buflen = w->w_tmemsize - len;
1821 cp = (caddr_t)w->w_tmem + len;
1824 rtinfo->rti_addrs = 0;
1825 for (i = 0; i < RTAX_MAX; i++) {
1826 struct sockaddr *sa;
1828 if ((sa = rtinfo->rti_info[i]) == NULL)
1830 rtinfo->rti_addrs |= (1 << i);
1831 #ifdef COMPAT_FREEBSD32
1833 dlen = SA_SIZE32(sa);
1837 if (cp != NULL && buflen >= dlen) {
1838 KASSERT(dlen <= sizeof(ss),
1839 ("%s: sockaddr size overflow", __func__));
1840 bzero(&ss, sizeof(ss));
1841 bcopy(sa, &ss, sa->sa_len);
1842 sa = (struct sockaddr *)&ss;
1844 if (sa->sa_family == AF_INET6) {
1845 sin6 = (struct sockaddr_in6 *)sa;
1846 (void)sa6_recoverscope(sin6);
1849 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1852 } else if (cp != NULL) {
1854 * Buffer too small. Count needed size
1855 * and return with error.
1864 dlen = ALIGN(len) - len;
1876 /* fill header iff buffer is large enough */
1877 rtm->rtm_version = RTM_VERSION;
1878 rtm->rtm_type = type;
1879 rtm->rtm_msglen = len;
1884 if (w != NULL && cp == NULL)
1891 * This routine is called to generate a message from the routing
1892 * socket indicating that a redirect has occurred, a routing lookup
1893 * has failed, or that a protocol has detected timeouts to a particular
1897 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1900 struct rt_msghdr *rtm;
1902 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1904 if (V_route_cb.any_count == 0)
1906 m = rtsock_msg_mbuf(type, rtinfo);
1910 if (fibnum != RT_ALL_FIBS) {
1911 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1912 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1913 M_SETFIB(m, fibnum);
1914 m->m_flags |= RTS_FILTER_FIB;
1917 rtm = mtod(m, struct rt_msghdr *);
1918 rtm->rtm_flags = RTF_DONE | flags;
1919 rtm->rtm_errno = error;
1920 rtm->rtm_addrs = rtinfo->rti_addrs;
1921 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1925 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1928 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1932 * This routine is called to generate a message from the routing
1933 * socket indicating that the status of a network interface has changed.
1936 rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask __unused)
1938 struct if_msghdr *ifm;
1940 struct rt_addrinfo info;
1942 if (V_route_cb.any_count == 0)
1944 bzero((caddr_t)&info, sizeof(info));
1945 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1948 ifm = mtod(m, struct if_msghdr *);
1949 ifm->ifm_index = ifp->if_index;
1950 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1951 if_data_copy(ifp, &ifm->ifm_data);
1953 rt_dispatch(m, AF_UNSPEC);
1957 * Announce interface address arrival/withdraw.
1958 * Please do not call directly, use rt_addrmsg().
1959 * Assume input data to be valid.
1960 * Returns 0 on success.
1963 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1965 struct rt_addrinfo info;
1966 struct sockaddr *sa;
1969 struct ifa_msghdr *ifam;
1970 struct ifnet *ifp = ifa->ifa_ifp;
1971 struct sockaddr_storage ss;
1973 if (V_route_cb.any_count == 0)
1976 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1978 bzero((caddr_t)&info, sizeof(info));
1979 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1980 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1981 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1982 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1983 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1984 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1986 ifam = mtod(m, struct ifa_msghdr *);
1987 ifam->ifam_index = ifp->if_index;
1988 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1989 ifam->ifam_flags = ifa->ifa_flags;
1990 ifam->ifam_addrs = info.rti_addrs;
1992 if (fibnum != RT_ALL_FIBS) {
1993 M_SETFIB(m, fibnum);
1994 m->m_flags |= RTS_FILTER_FIB;
1997 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2003 * Announce route addition/removal to rtsock based on @rt data.
2004 * Callers are advives to use rt_routemsg() instead of using this
2005 * function directly.
2006 * Assume @rt data is consistent.
2008 * Returns 0 on success.
2011 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
2014 union sockaddr_union dst, mask;
2015 struct rt_addrinfo info;
2017 if (V_route_cb.any_count == 0)
2020 int family = rt_get_family(rt);
2021 init_sockaddrs_family(family, &dst.sa, &mask.sa);
2022 export_rtaddrs(rt, &dst.sa, &mask.sa);
2024 bzero((caddr_t)&info, sizeof(info));
2025 info.rti_info[RTAX_DST] = &dst.sa;
2026 info.rti_info[RTAX_NETMASK] = &mask.sa;
2027 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2028 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
2029 info.rti_ifp = nh->nh_ifp;
2031 return (rtsock_routemsg_info(cmd, &info, fibnum));
2035 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2037 struct rt_msghdr *rtm;
2038 struct sockaddr *sa;
2041 if (V_route_cb.any_count == 0)
2044 if (info->rti_flags & RTF_HOST)
2045 info->rti_info[RTAX_NETMASK] = NULL;
2047 m = rtsock_msg_mbuf(cmd, info);
2051 if (fibnum != RT_ALL_FIBS) {
2052 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
2053 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
2054 M_SETFIB(m, fibnum);
2055 m->m_flags |= RTS_FILTER_FIB;
2058 rtm = mtod(m, struct rt_msghdr *);
2059 rtm->rtm_addrs = info->rti_addrs;
2060 if (info->rti_ifp != NULL)
2061 rtm->rtm_index = info->rti_ifp->if_index;
2062 /* Add RTF_DONE to indicate command 'completion' required by API */
2063 info->rti_flags |= RTF_DONE;
2064 /* Reported routes has to be up */
2065 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
2066 info->rti_flags |= RTF_UP;
2067 rtm->rtm_flags = info->rti_flags;
2069 sa = info->rti_info[RTAX_DST];
2070 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2076 * This is the analogue to the rt_newaddrmsg which performs the same
2077 * function but for multicast group memberhips. This is easier since
2078 * there is no route state to worry about.
2081 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
2083 struct rt_addrinfo info;
2084 struct mbuf *m = NULL;
2085 struct ifnet *ifp = ifma->ifma_ifp;
2086 struct ifma_msghdr *ifmam;
2088 if (V_route_cb.any_count == 0)
2091 bzero((caddr_t)&info, sizeof(info));
2092 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2093 if (ifp && ifp->if_addr)
2094 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
2096 info.rti_info[RTAX_IFP] = NULL;
2098 * If a link-layer address is present, present it as a ``gateway''
2099 * (similarly to how ARP entries, e.g., are presented).
2101 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
2102 m = rtsock_msg_mbuf(cmd, &info);
2105 ifmam = mtod(m, struct ifma_msghdr *);
2106 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
2108 ifmam->ifmam_index = ifp->if_index;
2109 ifmam->ifmam_addrs = info.rti_addrs;
2110 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
2113 static struct mbuf *
2114 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
2115 struct rt_addrinfo *info)
2117 struct if_announcemsghdr *ifan;
2120 if (V_route_cb.any_count == 0)
2122 bzero((caddr_t)info, sizeof(*info));
2123 m = rtsock_msg_mbuf(type, info);
2125 ifan = mtod(m, struct if_announcemsghdr *);
2126 ifan->ifan_index = ifp->if_index;
2127 strlcpy(ifan->ifan_name, ifp->if_xname,
2128 sizeof(ifan->ifan_name));
2129 ifan->ifan_what = what;
2135 * This is called to generate routing socket messages indicating
2136 * IEEE80211 wireless events.
2137 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2140 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2143 struct rt_addrinfo info;
2145 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2148 * Append the ieee80211 data. Try to stick it in the
2149 * mbuf containing the ifannounce msg; otherwise allocate
2150 * a new mbuf and append.
2152 * NB: we assume m is a single mbuf.
2154 if (data_len > M_TRAILINGSPACE(m)) {
2155 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2160 bcopy(data, mtod(n, void *), data_len);
2161 n->m_len = data_len;
2163 } else if (data_len > 0) {
2164 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2165 m->m_len += data_len;
2167 if (m->m_flags & M_PKTHDR)
2168 m->m_pkthdr.len += data_len;
2169 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2170 rt_dispatch(m, AF_UNSPEC);
2175 * This is called to generate routing socket messages indicating
2176 * network interface arrival and departure.
2179 rt_ifannouncemsg(struct ifnet *ifp, int what)
2182 struct rt_addrinfo info;
2184 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2186 rt_dispatch(m, AF_UNSPEC);
2190 rt_dispatch(struct mbuf *m, sa_family_t saf)
2195 m->m_rtsock_family = saf;
2197 m->m_pkthdr.rcvif = V_loif;
2202 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
2206 * This is used in dumping the kernel table via sysctl().
2209 sysctl_dumpentry(struct rtentry *rt, void *vw)
2211 struct walkarg *w = vw;
2212 struct nhop_object *nh;
2216 if (!rt_is_exportable(rt, w->w_req->td->td_ucred))
2219 export_rtaddrs(rt, w->dst, w->mask);
2220 nh = rt_get_raw_nhop(rt);
2222 if (NH_IS_NHGRP(nh)) {
2223 const struct weightened_nhop *wn;
2226 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2227 for (int i = 0; i < num_nhops; i++) {
2228 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2234 sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2241 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2244 struct rt_addrinfo info;
2245 int error = 0, size;
2248 rtflags = nhop_get_rtflags(nh);
2250 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2253 bzero((caddr_t)&info, sizeof(info));
2254 info.rti_info[RTAX_DST] = w->dst;
2255 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2256 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2257 info.rti_info[RTAX_GENMASK] = 0;
2258 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2259 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2260 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2261 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2262 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2264 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2266 if (w->w_req && w->w_tmem) {
2267 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2269 bzero(&rtm->rtm_index,
2270 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2273 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2274 * and RTF_UP (if entry is linked, which is always true here).
2275 * Given that, use nhop rtflags & add RTF_UP.
2277 rtm->rtm_flags = rtflags | RTF_UP;
2278 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2279 rtm->rtm_flags = RTF_GATEWAY |
2280 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2281 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2282 rtm->rtm_rmx.rmx_weight = weight;
2283 rtm->rtm_index = nh->nh_ifp->if_index;
2284 rtm->rtm_addrs = info.rti_addrs;
2285 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2292 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2293 struct rt_addrinfo *info, struct walkarg *w, int len)
2295 struct if_msghdrl *ifm;
2296 struct if_data *ifd;
2298 ifm = (struct if_msghdrl *)w->w_tmem;
2300 #ifdef COMPAT_FREEBSD32
2301 if (w->w_req->flags & SCTL_MASK32) {
2302 struct if_msghdrl32 *ifm32;
2304 ifm32 = (struct if_msghdrl32 *)ifm;
2305 ifm32->ifm_addrs = info->rti_addrs;
2306 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2307 ifm32->ifm_index = ifp->if_index;
2308 ifm32->_ifm_spare1 = 0;
2309 ifm32->ifm_len = sizeof(*ifm32);
2310 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2311 ifm32->_ifm_spare2 = 0;
2312 ifd = &ifm32->ifm_data;
2316 ifm->ifm_addrs = info->rti_addrs;
2317 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2318 ifm->ifm_index = ifp->if_index;
2319 ifm->_ifm_spare1 = 0;
2320 ifm->ifm_len = sizeof(*ifm);
2321 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2322 ifm->_ifm_spare2 = 0;
2323 ifd = &ifm->ifm_data;
2326 memcpy(ifd, src_ifd, sizeof(*ifd));
2328 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2332 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2333 struct rt_addrinfo *info, struct walkarg *w, int len)
2335 struct if_msghdr *ifm;
2336 struct if_data *ifd;
2338 ifm = (struct if_msghdr *)w->w_tmem;
2340 #ifdef COMPAT_FREEBSD32
2341 if (w->w_req->flags & SCTL_MASK32) {
2342 struct if_msghdr32 *ifm32;
2344 ifm32 = (struct if_msghdr32 *)ifm;
2345 ifm32->ifm_addrs = info->rti_addrs;
2346 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2347 ifm32->ifm_index = ifp->if_index;
2348 ifm32->_ifm_spare1 = 0;
2349 ifd = &ifm32->ifm_data;
2353 ifm->ifm_addrs = info->rti_addrs;
2354 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2355 ifm->ifm_index = ifp->if_index;
2356 ifm->_ifm_spare1 = 0;
2357 ifd = &ifm->ifm_data;
2360 memcpy(ifd, src_ifd, sizeof(*ifd));
2362 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2366 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2367 struct walkarg *w, int len)
2369 struct ifa_msghdrl *ifam;
2370 struct if_data *ifd;
2372 ifam = (struct ifa_msghdrl *)w->w_tmem;
2374 #ifdef COMPAT_FREEBSD32
2375 if (w->w_req->flags & SCTL_MASK32) {
2376 struct ifa_msghdrl32 *ifam32;
2378 ifam32 = (struct ifa_msghdrl32 *)ifam;
2379 ifam32->ifam_addrs = info->rti_addrs;
2380 ifam32->ifam_flags = ifa->ifa_flags;
2381 ifam32->ifam_index = ifa->ifa_ifp->if_index;
2382 ifam32->_ifam_spare1 = 0;
2383 ifam32->ifam_len = sizeof(*ifam32);
2384 ifam32->ifam_data_off =
2385 offsetof(struct ifa_msghdrl32, ifam_data);
2386 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2387 ifd = &ifam32->ifam_data;
2391 ifam->ifam_addrs = info->rti_addrs;
2392 ifam->ifam_flags = ifa->ifa_flags;
2393 ifam->ifam_index = ifa->ifa_ifp->if_index;
2394 ifam->_ifam_spare1 = 0;
2395 ifam->ifam_len = sizeof(*ifam);
2396 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2397 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2398 ifd = &ifam->ifam_data;
2401 bzero(ifd, sizeof(*ifd));
2402 ifd->ifi_datalen = sizeof(struct if_data);
2403 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2404 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2405 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2406 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2408 /* Fixup if_data carp(4) vhid. */
2409 if (carp_get_vhid_p != NULL)
2410 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2412 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2416 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2417 struct walkarg *w, int len)
2419 struct ifa_msghdr *ifam;
2421 ifam = (struct ifa_msghdr *)w->w_tmem;
2422 ifam->ifam_addrs = info->rti_addrs;
2423 ifam->ifam_flags = ifa->ifa_flags;
2424 ifam->ifam_index = ifa->ifa_ifp->if_index;
2425 ifam->_ifam_spare1 = 0;
2426 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2428 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2432 sysctl_iflist(int af, struct walkarg *w)
2437 struct rt_addrinfo info;
2439 struct sockaddr_storage ss;
2441 bzero((caddr_t)&info, sizeof(info));
2442 bzero(&ifd, sizeof(ifd));
2443 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2444 if (w->w_arg && w->w_arg != ifp->if_index)
2446 if_data_copy(ifp, &ifd);
2448 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2449 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2452 info.rti_info[RTAX_IFP] = NULL;
2453 if (w->w_req && w->w_tmem) {
2454 if (w->w_op == NET_RT_IFLISTL)
2455 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2458 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2463 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2464 if (af && af != ifa->ifa_addr->sa_family)
2466 if (prison_if(w->w_req->td->td_ucred,
2467 ifa->ifa_addr) != 0)
2469 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2470 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2471 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2472 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2473 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2476 if (w->w_req && w->w_tmem) {
2477 if (w->w_op == NET_RT_IFLISTL)
2478 error = sysctl_iflist_ifaml(ifa, &info,
2481 error = sysctl_iflist_ifam(ifa, &info,
2487 info.rti_info[RTAX_IFA] = NULL;
2488 info.rti_info[RTAX_NETMASK] = NULL;
2489 info.rti_info[RTAX_BRD] = NULL;
2496 sysctl_ifmalist(int af, struct walkarg *w)
2498 struct rt_addrinfo info;
2500 struct ifmultiaddr *ifma;
2507 bzero((caddr_t)&info, sizeof(info));
2509 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2510 if (w->w_arg && w->w_arg != ifp->if_index)
2513 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2514 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2515 if (af && af != ifma->ifma_addr->sa_family)
2517 if (prison_if(w->w_req->td->td_ucred,
2518 ifma->ifma_addr) != 0)
2520 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2521 info.rti_info[RTAX_GATEWAY] =
2522 (ifma->ifma_addr->sa_family != AF_LINK) ?
2523 ifma->ifma_lladdr : NULL;
2524 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2527 if (w->w_req && w->w_tmem) {
2528 struct ifma_msghdr *ifmam;
2530 ifmam = (struct ifma_msghdr *)w->w_tmem;
2531 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2532 ifmam->ifmam_flags = 0;
2533 ifmam->ifmam_addrs = info.rti_addrs;
2534 ifmam->_ifmam_spare1 = 0;
2535 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2547 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2549 union sockaddr_union sa_dst, sa_mask;
2552 w->dst = (struct sockaddr *)&sa_dst;
2553 w->mask = (struct sockaddr *)&sa_mask;
2555 init_sockaddrs_family(family, w->dst, w->mask);
2557 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2561 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2563 struct epoch_tracker et;
2564 int *name = (int *)arg1;
2565 u_int namelen = arg2;
2566 struct rib_head *rnh = NULL; /* silence compiler. */
2567 int i, lim, error = EINVAL;
2579 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2581 fib = req->td->td_proc->p_fibnum;
2582 else if (namelen == 4)
2583 fib = (name[3] == RT_ALL_FIBS) ?
2584 req->td->td_proc->p_fibnum : name[3];
2586 return ((namelen < 3) ? EISDIR : ENOTDIR);
2587 if (fib < 0 || fib >= rt_numfibs)
2589 } else if (namelen != 3)
2590 return ((namelen < 3) ? EISDIR : ENOTDIR);
2594 bzero(&w, sizeof(w));
2599 error = sysctl_wire_old_buffer(req, 0);
2604 * Allocate reply buffer in advance.
2605 * All rtsock messages has maximum length of u_short.
2607 w.w_tmemsize = 65536;
2608 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2610 NET_EPOCH_ENTER(et);
2614 if (af == 0) { /* dump all tables */
2617 } else /* dump only one table */
2621 * take care of llinfo entries, the caller must
2624 if (w.w_op == NET_RT_FLAGS &&
2625 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2627 error = lltable_sysctl_dumparp(af, w.w_req);
2633 * take care of routing entries
2635 for (error = 0; error == 0 && i <= lim; i++) {
2636 rnh = rt_tables_get_rnh(fib, i);
2638 rtable_sysctl_dump(fib, i, &w);
2640 error = EAFNOSUPPORT;
2645 /* Allow dumping one specific af/fib at a time */
2651 if (fib < 0 || fib > rt_numfibs) {
2655 rnh = rt_tables_get_rnh(fib, af);
2657 error = EAFNOSUPPORT;
2660 if (w.w_op == NET_RT_NHOP)
2661 error = nhops_dump_sysctl(rnh, w.w_req);
2664 error = nhgrp_dump_sysctl(rnh, w.w_req);
2670 case NET_RT_IFLISTL:
2671 error = sysctl_iflist(af, &w);
2674 case NET_RT_IFMALIST:
2675 error = sysctl_ifmalist(af, &w);
2680 free(w.w_tmem, M_TEMP);
2684 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2685 sysctl_rtsock, "Return route tables and interface/address lists");
2688 * Definitions of protocols supported in the ROUTE domain.
2691 static struct domain routedomain; /* or at least forward */
2693 static struct protosw routesw = {
2694 .pr_type = SOCK_RAW,
2695 .pr_flags = PR_ATOMIC|PR_ADDR,
2696 .pr_abort = rts_close,
2697 .pr_attach = rts_attach,
2698 .pr_detach = rts_detach,
2699 .pr_send = rts_send,
2700 .pr_shutdown = rts_shutdown,
2701 .pr_disconnect = rts_disconnect,
2702 .pr_close = rts_close,
2705 static struct domain routedomain = {
2706 .dom_family = PF_ROUTE,
2707 .dom_name = "route",
2709 .dom_protosw = { &routesw },