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)
460 #ifndef _SOCKADDR_UNION_DEFINED
461 #define _SOCKADDR_UNION_DEFINED
463 * The union of all possible address formats we handle.
465 union sockaddr_union {
467 struct sockaddr_in sin;
468 struct sockaddr_in6 sin6;
470 #endif /* _SOCKADDR_UNION_DEFINED */
473 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
474 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
476 #if defined(INET) || defined(INET6)
477 struct epoch_tracker et;
480 /* First, see if the returned address is part of the jail. */
481 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
482 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
486 switch (info->rti_info[RTAX_DST]->sa_family) {
496 * Try to find an address on the given outgoing interface
497 * that belongs to the jail.
500 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
503 if (sa->sa_family != AF_INET)
505 ia = ((struct sockaddr_in *)sa)->sin_addr;
506 if (prison_check_ip4(cred, &ia) == 0) {
514 * As a last resort return the 'default' jail address.
516 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
518 if (prison_get_ip4(cred, &ia) != 0)
521 bzero(&saun->sin, sizeof(struct sockaddr_in));
522 saun->sin.sin_len = sizeof(struct sockaddr_in);
523 saun->sin.sin_family = AF_INET;
524 saun->sin.sin_addr.s_addr = ia.s_addr;
525 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
538 * Try to find an address on the given outgoing interface
539 * that belongs to the jail.
542 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
545 if (sa->sa_family != AF_INET6)
547 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
548 &ia6, sizeof(struct in6_addr));
549 if (prison_check_ip6(cred, &ia6) == 0) {
557 * As a last resort return the 'default' jail address.
559 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
561 if (prison_get_ip6(cred, &ia6) != 0)
564 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
565 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
566 saun->sin6.sin6_family = AF_INET6;
567 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
568 if (sa6_recoverscope(&saun->sin6) != 0)
570 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
581 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
586 if (V_loif == NULL) {
587 RTS_PID_LOG(LOG_INFO, "Unable to add blackhole/reject nhop without loopback");
590 info->rti_ifp = V_loif;
592 saf = info->rti_info[RTAX_DST]->sa_family;
594 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
595 if (ifa->ifa_addr->sa_family == saf) {
600 if (info->rti_ifa == NULL) {
601 RTS_PID_LOG(LOG_INFO, "Unable to find ifa for blackhole/reject nhop");
605 bzero(saun, sizeof(union sockaddr_union));
609 saun->sin.sin_family = AF_INET;
610 saun->sin.sin_len = sizeof(struct sockaddr_in);
611 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
616 saun->sin6.sin6_family = AF_INET6;
617 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
618 saun->sin6.sin6_addr = in6addr_loopback;
622 RTS_PID_LOG(LOG_INFO, "unsupported family: %d", saf);
625 info->rti_info[RTAX_GATEWAY] = &saun->sa;
626 info->rti_flags |= RTF_GATEWAY;
632 * Fills in @info based on userland-provided @rtm message.
634 * Returns 0 on success.
637 fill_addrinfo(struct rt_msghdr *rtm, int len, struct linear_buffer *lb, u_int fibnum,
638 struct rt_addrinfo *info)
642 rtm->rtm_pid = curproc->p_pid;
643 info->rti_addrs = rtm->rtm_addrs;
645 info->rti_mflags = rtm->rtm_inits;
646 info->rti_rmx = &rtm->rtm_rmx;
649 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
650 * link-local address because rtrequest requires addresses with
653 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
656 info->rti_flags = rtm->rtm_flags;
657 error = cleanup_xaddrs(info, lb);
661 * Verify that the caller has the appropriate privilege; RTM_GET
662 * is the only operation the non-superuser is allowed.
664 if (rtm->rtm_type != RTM_GET) {
665 error = priv_check(curthread, PRIV_NET_ROUTE);
671 * The given gateway address may be an interface address.
672 * For example, issuing a "route change" command on a route
673 * entry that was created from a tunnel, and the gateway
674 * address given is the local end point. In this case the
675 * RTF_GATEWAY flag must be cleared or the destination will
676 * not be reachable even though there is no error message.
678 if (info->rti_info[RTAX_GATEWAY] != NULL &&
679 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
680 struct nhop_object *nh;
683 * A host route through the loopback interface is
684 * installed for each interface adddress. In pre 8.0
685 * releases the interface address of a PPP link type
686 * is not reachable locally. This behavior is fixed as
687 * part of the new L2/L3 redesign and rewrite work. The
688 * signature of this interface address route is the
689 * AF_LINK sa_family type of the gateway, and the
690 * rt_ifp has the IFF_LOOPBACK flag set.
692 nh = rib_lookup(fibnum, info->rti_info[RTAX_GATEWAY], NHR_NONE, 0);
693 if (nh != NULL && nh->gw_sa.sa_family == AF_LINK &&
694 nh->nh_ifp->if_flags & IFF_LOOPBACK) {
695 info->rti_flags &= ~RTF_GATEWAY;
696 info->rti_flags |= RTF_GWFLAG_COMPAT;
703 static struct nhop_object *
704 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
706 if (!NH_IS_NHGRP(nh))
709 const struct weightened_nhop *wn;
711 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
714 for (int i = 0; i < num_nhops; i++) {
715 if (match_nhop_gw(wn[i].nh, gw))
723 * Handles RTM_GET message from routing socket, returning matching rt.
726 * 0 on success, with locked and referenced matching rt in @rt_nrt
730 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
731 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
734 struct rib_head *rnh;
735 struct nhop_object *nh;
738 saf = info->rti_info[RTAX_DST]->sa_family;
740 rnh = rt_tables_get_rnh(fibnum, saf);
742 return (EAFNOSUPPORT);
747 * By (implicit) convention host route (one without netmask)
748 * means longest-prefix-match request and the route with netmask
749 * means exact-match lookup.
750 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
751 * prefixes, use original data to check for the netmask presence.
753 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
755 * Provide longest prefix match for
756 * address lookup (no mask).
757 * 'route -n get addr'
759 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
760 info->rti_info[RTAX_DST], &rnh->head);
762 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
763 info->rti_info[RTAX_DST],
764 info->rti_info[RTAX_NETMASK], &rnh->head);
766 if (rc->rc_rt == NULL) {
771 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
777 * If performing proxied L2 entry insertion, and
778 * the actual PPP host entry is found, perform
779 * another search to retrieve the prefix route of
780 * the local end point of the PPP link.
781 * TODO: move this logic to userland.
783 if (rtm->rtm_flags & RTF_ANNOUNCE) {
784 struct sockaddr_storage laddr;
786 if (nh->nh_ifp != NULL &&
787 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
790 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
793 rt_maskedcopy(ifa->ifa_addr,
794 (struct sockaddr *)&laddr,
797 rt_maskedcopy(nh->nh_ifa->ifa_addr,
798 (struct sockaddr *)&laddr,
799 nh->nh_ifa->ifa_netmask);
801 * refactor rt and no lock operation necessary
803 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(
804 (struct sockaddr *)&laddr, &rnh->head);
805 if (rc->rc_rt == NULL) {
809 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
816 rc->rc_nh_weight = rc->rc_rt->rt_weight;
823 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
826 if (family == AF_INET) {
827 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
828 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
830 bzero(dst4, sizeof(struct sockaddr_in));
831 bzero(mask4, sizeof(struct sockaddr_in));
833 dst4->sin_family = AF_INET;
834 dst4->sin_len = sizeof(struct sockaddr_in);
835 mask4->sin_family = AF_INET;
836 mask4->sin_len = sizeof(struct sockaddr_in);
840 if (family == AF_INET6) {
841 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
842 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
844 bzero(dst6, sizeof(struct sockaddr_in6));
845 bzero(mask6, sizeof(struct sockaddr_in6));
847 dst6->sin6_family = AF_INET6;
848 dst6->sin6_len = sizeof(struct sockaddr_in6);
849 mask6->sin6_family = AF_INET6;
850 mask6->sin6_len = sizeof(struct sockaddr_in6);
856 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
857 struct sockaddr *mask)
860 if (dst->sa_family == AF_INET) {
861 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
862 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
863 uint32_t scopeid = 0;
864 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
870 if (dst->sa_family == AF_INET6) {
871 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
872 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
873 uint32_t scopeid = 0;
874 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
875 &mask6->sin6_addr, &scopeid);
876 dst6->sin6_scope_id = scopeid;
883 update_rtm_from_info(struct rt_addrinfo *info, struct rt_msghdr **prtm,
886 struct rt_msghdr *rtm, *orig_rtm = NULL;
891 /* Check if we need to realloc storage */
892 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
893 if (len > alloc_len) {
894 struct rt_msghdr *tmp_rtm;
896 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
899 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
905 * Delay freeing original rtm as info contains
906 * data referencing it.
910 w.w_tmem = (caddr_t)rtm;
911 w.w_tmemsize = alloc_len;
912 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
913 rtm->rtm_addrs = info->rti_addrs;
915 if (orig_rtm != NULL)
916 free(orig_rtm, M_TEMP);
923 * Update sockaddrs, flags, etc in @prtm based on @rc data.
924 * rtm can be reallocated.
926 * Returns 0 on success, along with pointer to (potentially reallocated)
931 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
932 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
934 union sockaddr_union saun;
935 struct rt_msghdr *rtm;
940 union sockaddr_union sa_dst, sa_mask;
941 int family = info->rti_info[RTAX_DST]->sa_family;
942 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
943 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
945 info->rti_info[RTAX_DST] = &sa_dst.sa;
946 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
947 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
948 info->rti_info[RTAX_GENMASK] = 0;
950 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
952 info->rti_info[RTAX_IFP] =
953 ifp->if_addr->ifa_addr;
954 error = rtm_get_jailed(info, ifp, nh,
955 &saun, curthread->td_ucred);
958 if (ifp->if_flags & IFF_POINTOPOINT)
959 info->rti_info[RTAX_BRD] =
960 nh->nh_ifa->ifa_dstaddr;
961 rtm->rtm_index = ifp->if_index;
963 info->rti_info[RTAX_IFP] = NULL;
964 info->rti_info[RTAX_IFA] = NULL;
966 } else if (ifp != NULL)
967 rtm->rtm_index = ifp->if_index;
969 if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0)
973 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
974 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
975 rtm->rtm_flags = RTF_GATEWAY |
976 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
977 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
978 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
985 save_del_notification(const struct rib_cmd_info *rc, void *_cbdata)
987 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
989 if (rc->rc_cmd == RTM_DELETE)
994 save_add_notification(const struct rib_cmd_info *rc, void *_cbdata)
996 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
998 if (rc->rc_cmd == RTM_ADD)
1003 #if defined(INET6) || defined(INET)
1004 static struct sockaddr *
1005 alloc_sockaddr_aligned(struct linear_buffer *lb, int len)
1007 len = roundup2(len, sizeof(uint64_t));
1008 if (lb->offset + len > lb->size)
1010 struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset);
1017 rts_send(struct socket *so, int flags, struct mbuf *m,
1018 struct sockaddr *nam, struct mbuf *control, struct thread *td)
1020 struct rt_msghdr *rtm = NULL;
1021 struct rt_addrinfo info;
1022 struct epoch_tracker et;
1024 struct sockaddr_storage ss;
1025 struct sockaddr_in6 *sin6;
1026 int i, rti_need_deembed = 0;
1028 int alloc_len = 0, len, error = 0, fibnum;
1029 sa_family_t saf = AF_UNSPEC;
1030 struct rib_cmd_info rc;
1031 struct nhop_object *nh;
1033 if ((flags & PRUS_OOB) || control != NULL) {
1035 if (control != NULL)
1037 return (EOPNOTSUPP);
1040 fibnum = so->so_fibnum;
1041 #define senderr(e) { error = e; goto flush;}
1042 if (m == NULL || ((m->m_len < sizeof(long)) &&
1043 (m = m_pullup(m, sizeof(long))) == NULL))
1045 if ((m->m_flags & M_PKTHDR) == 0)
1046 panic("route_output");
1047 NET_EPOCH_ENTER(et);
1048 len = m->m_pkthdr.len;
1049 if (len < sizeof(*rtm) ||
1050 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1054 * Most of current messages are in range 200-240 bytes,
1055 * minimize possible re-allocation on reply using larger size
1056 * buffer aligned on 1k boundaty.
1058 alloc_len = roundup2(len, 1024);
1059 int total_len = alloc_len + SCRATCH_BUFFER_SIZE;
1060 if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL)
1063 m_copydata(m, 0, len, (caddr_t)rtm);
1064 bzero(&info, sizeof(info));
1066 struct linear_buffer lb = {
1067 .base = (char *)rtm + alloc_len,
1068 .size = SCRATCH_BUFFER_SIZE,
1071 if (rtm->rtm_version != RTM_VERSION) {
1072 /* Do not touch message since format is unknown */
1075 senderr(EPROTONOSUPPORT);
1079 * Starting from here, it is possible
1080 * to alter original message and insert
1081 * caller PID and error value.
1084 if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) {
1087 /* fill_addringo() embeds scope into IPv6 addresses */
1089 rti_need_deembed = 1;
1092 saf = info.rti_info[RTAX_DST]->sa_family;
1094 /* support for new ARP code */
1095 if (rtm->rtm_flags & RTF_LLDATA) {
1096 error = lla_rt_output(rtm, &info);
1100 union sockaddr_union gw_saun;
1101 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1102 if (blackhole_flags != 0) {
1103 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1104 error = fill_blackholeinfo(&info, &gw_saun);
1106 RTS_PID_LOG(LOG_DEBUG, "both BLACKHOLE and REJECT flags specifiied");
1113 switch (rtm->rtm_type) {
1116 if (rtm->rtm_type == RTM_ADD) {
1117 if (info.rti_info[RTAX_GATEWAY] == NULL) {
1118 RTS_PID_LOG(LOG_DEBUG, "RTM_ADD w/o gateway");
1122 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1124 rtsock_notify_event(fibnum, &rc);
1126 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1127 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1128 struct rib_cmd_info rc_simple = {};
1129 rib_decompose_notification(&rc,
1130 save_add_notification, (void *)&rc_simple);
1134 /* nh MAY be empty if RTM_CHANGE request is no-op */
1137 rtm->rtm_index = nh->nh_ifp->if_index;
1138 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1144 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1146 rtsock_notify_event(fibnum, &rc);
1148 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1149 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1150 struct rib_cmd_info rc_simple = {};
1151 rib_decompose_notification(&rc,
1152 save_del_notification, (void *)&rc_simple);
1161 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1166 if (!rt_is_exportable(rc.rc_rt, curthread->td_ucred))
1171 senderr(EOPNOTSUPP);
1174 if (error == 0 && nh != NULL) {
1175 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1177 * Note that some sockaddr pointers may have changed to
1178 * point to memory outsize @rtm. Some may be pointing
1179 * to the on-stack variables.
1180 * Given that, any pointer in @info CANNOT BE USED.
1184 * scopeid deembedding has been performed while
1185 * writing updated rtm in rtsock_msg_buffer().
1186 * With that in mind, skip deembedding procedure below.
1189 rti_need_deembed = 0;
1198 if (rti_need_deembed) {
1199 /* sin6_scope_id is recovered before sending rtm. */
1200 sin6 = (struct sockaddr_in6 *)&ss;
1201 for (i = 0; i < RTAX_MAX; i++) {
1202 if (info.rti_info[i] == NULL)
1204 if (info.rti_info[i]->sa_family != AF_INET6)
1206 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1207 if (sa6_recoverscope(sin6) == 0)
1208 bcopy(sin6, info.rti_info[i],
1211 if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) {
1218 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1224 * Sends the prepared reply message in @rtm to all rtsock clients.
1225 * Frees @m and @rtm.
1229 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1230 sa_family_t saf, u_int fibnum, int rtm_errno)
1232 struct rcb *rcb = NULL;
1235 * Check to see if we don't want our own messages.
1237 if ((so->so_options & SO_USELOOPBACK) == 0) {
1238 if (V_route_cb.any_count <= 1) {
1244 /* There is another listener, so construct message */
1250 rtm->rtm_errno = rtm_errno;
1252 rtm->rtm_flags |= RTF_DONE;
1254 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1255 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1258 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1259 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1264 M_SETFIB(m, fibnum);
1265 m->m_flags |= RTS_FILTER_FIB;
1268 * XXX insure we don't get a copy by
1269 * invalidating our protocol
1271 sa_family_t family = rcb->rcb_family;
1272 rcb->rcb_family = AF_UNSPEC;
1273 rt_dispatch(m, saf);
1274 rcb->rcb_family = family;
1276 rt_dispatch(m, saf);
1281 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1282 struct rt_metrics *out)
1285 bzero(out, sizeof(*out));
1286 out->rmx_mtu = nh->nh_mtu;
1287 out->rmx_weight = rt->rt_weight;
1288 out->rmx_nhidx = nhop_get_idx(nh);
1289 /* Kernel -> userland timebase conversion. */
1290 out->rmx_expire = nhop_get_expire(nh) ?
1291 nhop_get_expire(nh) - time_uptime + time_second : 0;
1295 * Extract the addresses of the passed sockaddrs.
1296 * Do a little sanity checking so as to avoid bad memory references.
1297 * This data is derived straight from userland.
1300 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1302 struct sockaddr *sa;
1305 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1306 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1308 sa = (struct sockaddr *)cp;
1312 if (cp + sa->sa_len > cplim) {
1313 RTS_PID_LOG(LOG_DEBUG, "sa_len too big for sa type %d", i);
1317 * there are no more.. quit now
1318 * If there are more bits, they are in error.
1319 * I've seen this. route(1) can evidently generate these.
1320 * This causes kernel to core dump.
1321 * for compatibility, If we see this, point to a safe address.
1323 if (sa->sa_len == 0) {
1324 rtinfo->rti_info[i] = &sa_zero;
1325 return (0); /* should be EINVAL but for compat */
1329 if (sa->sa_family == AF_INET6)
1330 sa6_embedscope((struct sockaddr_in6 *)sa,
1333 rtinfo->rti_info[i] = sa;
1341 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1344 const struct sockaddr_in nsin = {
1345 .sin_family = AF_INET,
1346 .sin_len = sizeof(struct sockaddr_in),
1355 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1359 const struct sockaddr_in6 nsin6 = {
1360 .sin6_family = AF_INET6,
1361 .sin6_len = sizeof(struct sockaddr_in6),
1362 .sin6_addr = *addr6,
1363 .sin6_scope_id = scopeid,
1369 #if defined(INET6) || defined(INET)
1371 * Checks if gateway is suitable for lltable operations.
1372 * Lltable code requires AF_LINK gateway with ifindex
1373 * and mac address specified.
1374 * Returns 0 on success.
1377 cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1379 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1381 if (sdl->sdl_family != AF_LINK)
1384 if (sdl->sdl_index == 0) {
1385 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gateway w/o ifindex");
1389 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len) {
1390 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gw: sdl_nlen/sdl_alen too large");
1398 cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb)
1400 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1401 struct sockaddr *sa;
1403 if (info->rti_flags & RTF_LLDATA)
1404 return (cleanup_xaddrs_lladdr(info));
1406 switch (gw->sa_family) {
1410 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1412 /* Ensure reads do not go beyoud SA boundary */
1413 if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) {
1414 RTS_PID_LOG(LOG_DEBUG, "gateway sin_len too small: %d",
1418 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in));
1421 fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr);
1422 info->rti_info[RTAX_GATEWAY] = sa;
1429 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1430 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1431 RTS_PID_LOG(LOG_DEBUG, "gateway sin6_len too small: %d",
1435 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1441 struct sockaddr_dl *gw_sdl;
1443 size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data);
1444 gw_sdl = (struct sockaddr_dl *)gw;
1445 if (gw_sdl->sdl_len < sdl_min_len) {
1446 RTS_PID_LOG(LOG_DEBUG, "gateway sdl_len too small: %d",
1450 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short));
1454 const struct sockaddr_dl_short sdl = {
1455 .sdl_family = AF_LINK,
1456 .sdl_len = sizeof(struct sockaddr_dl_short),
1457 .sdl_index = gw_sdl->sdl_index,
1459 *((struct sockaddr_dl_short *)sa) = sdl;
1460 info->rti_info[RTAX_GATEWAY] = sa;
1470 remove_netmask(struct rt_addrinfo *info)
1472 info->rti_info[RTAX_NETMASK] = NULL;
1473 info->rti_flags |= RTF_HOST;
1474 info->rti_addrs &= ~RTA_NETMASK;
1479 cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb)
1481 struct sockaddr_in *dst_sa, *mask_sa;
1482 const int sa_len = sizeof(struct sockaddr_in);
1483 struct in_addr dst, mask;
1485 /* Check & fixup dst/netmask combination first */
1486 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1487 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1489 /* Ensure reads do not go beyound the buffer size */
1490 if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero)) {
1491 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin_len too small: %d",
1496 if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1498 * Some older routing software encode mask length into the
1499 * sin_len, thus resulting in "truncated" sockaddr.
1501 int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr);
1504 if (len > sizeof(struct in_addr))
1505 len = sizeof(struct in_addr);
1506 memcpy(&mask, &mask_sa->sin_addr, len);
1508 RTS_PID_LOG(LOG_DEBUG, "prefix mask sin_len too small: %d",
1513 mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST;
1515 dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr));
1517 /* Construct new "clean" dst/mask sockaddresses */
1518 if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1520 fill_sockaddr_inet(dst_sa, dst);
1521 info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa;
1523 if (mask.s_addr != INADDR_BROADCAST) {
1524 if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1526 fill_sockaddr_inet(mask_sa, mask);
1527 info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa;
1528 info->rti_flags &= ~RTF_HOST;
1530 remove_netmask(info);
1533 if (info->rti_info[RTAX_GATEWAY] != NULL)
1534 return (cleanup_xaddrs_gateway(info, lb));
1542 cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb)
1544 struct sockaddr *sa;
1545 struct sockaddr_in6 *dst_sa, *mask_sa;
1546 struct in6_addr mask, *dst;
1547 const int sa_len = sizeof(struct sockaddr_in6);
1549 /* Check & fixup dst/netmask combination first */
1550 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1551 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1553 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1554 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin6_len too small: %d",
1559 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1561 * Some older routing software encode mask length into the
1562 * sin6_len, thus resulting in "truncated" sockaddr.
1564 int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1566 bzero(&mask, sizeof(mask));
1567 if (len > sizeof(struct in6_addr))
1568 len = sizeof(struct in6_addr);
1569 memcpy(&mask, &mask_sa->sin6_addr, len);
1571 RTS_PID_LOG(LOG_DEBUG, "rtsock: prefix mask sin6_len too small: %d",
1576 mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1578 dst = &dst_sa->sin6_addr;
1579 IN6_MASK_ADDR(dst, &mask);
1581 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1583 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0);
1584 info->rti_info[RTAX_DST] = sa;
1586 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) {
1587 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1589 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0);
1590 info->rti_info[RTAX_NETMASK] = sa;
1591 info->rti_flags &= ~RTF_HOST;
1593 remove_netmask(info);
1596 if (info->rti_info[RTAX_GATEWAY] != NULL)
1597 return (cleanup_xaddrs_gateway(info, lb));
1604 cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb)
1606 int error = EAFNOSUPPORT;
1608 if (info->rti_info[RTAX_DST] == NULL) {
1609 RTS_PID_LOG(LOG_DEBUG, "prefix dst is not set");
1613 if (info->rti_flags & RTF_LLDATA) {
1615 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1616 * prefix along with the actual address in RTA_DST.
1617 * Remove netmask to avoid unnecessary address masking.
1619 remove_netmask(info);
1622 switch (info->rti_info[RTAX_DST]->sa_family) {
1625 error = cleanup_xaddrs_inet(info, lb);
1630 error = cleanup_xaddrs_inet6(info, lb);
1639 * Fill in @dmask with valid netmask leaving original @smask
1640 * intact. Mostly used with radix netmasks.
1643 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1644 struct sockaddr_storage *dmask)
1646 if (dst == NULL || smask == NULL)
1649 memset(dmask, 0, dst->sa_len);
1650 memcpy(dmask, smask, smask->sa_len);
1651 dmask->ss_len = dst->sa_len;
1652 dmask->ss_family = dst->sa_family;
1654 return ((struct sockaddr *)dmask);
1658 * Writes information related to @rtinfo object to newly-allocated mbuf.
1659 * Assumes MCLBYTES is enough to construct any message.
1660 * Used for OS notifications of vaious events (if/ifa announces,etc)
1662 * Returns allocated mbuf or NULL on failure.
1664 static struct mbuf *
1665 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1667 struct sockaddr_storage ss;
1668 struct rt_msghdr *rtm;
1671 struct sockaddr *sa;
1673 struct sockaddr_in6 *sin6;
1680 len = sizeof(struct ifa_msghdr);
1685 len = sizeof(struct ifma_msghdr);
1689 len = sizeof(struct if_msghdr);
1692 case RTM_IFANNOUNCE:
1694 len = sizeof(struct if_announcemsghdr);
1698 len = sizeof(struct rt_msghdr);
1701 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1702 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1704 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1706 m = m_gethdr(M_NOWAIT, MT_DATA);
1710 m->m_pkthdr.len = m->m_len = len;
1711 rtm = mtod(m, struct rt_msghdr *);
1712 bzero((caddr_t)rtm, len);
1713 for (i = 0; i < RTAX_MAX; i++) {
1714 if ((sa = rtinfo->rti_info[i]) == NULL)
1716 rtinfo->rti_addrs |= (1 << i);
1719 KASSERT(dlen <= sizeof(ss),
1720 ("%s: sockaddr size overflow", __func__));
1721 bzero(&ss, sizeof(ss));
1722 bcopy(sa, &ss, sa->sa_len);
1723 sa = (struct sockaddr *)&ss;
1725 if (sa->sa_family == AF_INET6) {
1726 sin6 = (struct sockaddr_in6 *)sa;
1727 (void)sa6_recoverscope(sin6);
1730 m_copyback(m, len, dlen, (caddr_t)sa);
1733 if (m->m_pkthdr.len != len) {
1737 rtm->rtm_msglen = len;
1738 rtm->rtm_version = RTM_VERSION;
1739 rtm->rtm_type = type;
1744 * Writes information related to @rtinfo object to preallocated buffer.
1745 * Stores needed size in @plen. If @w is NULL, calculates size without
1747 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1749 * Returns 0 on success.
1753 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1755 struct sockaddr_storage ss;
1756 int len, buflen = 0, dlen, i;
1758 struct rt_msghdr *rtm = NULL;
1760 struct sockaddr_in6 *sin6;
1762 #ifdef COMPAT_FREEBSD32
1763 bool compat32 = false;
1769 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1770 #ifdef COMPAT_FREEBSD32
1771 if (w->w_req->flags & SCTL_MASK32) {
1772 len = sizeof(struct ifa_msghdrl32);
1776 len = sizeof(struct ifa_msghdrl);
1778 len = sizeof(struct ifa_msghdr);
1782 #ifdef COMPAT_FREEBSD32
1783 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1784 if (w->w_op == NET_RT_IFLISTL)
1785 len = sizeof(struct if_msghdrl32);
1787 len = sizeof(struct if_msghdr32);
1792 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1793 len = sizeof(struct if_msghdrl);
1795 len = sizeof(struct if_msghdr);
1799 len = sizeof(struct ifma_msghdr);
1803 len = sizeof(struct rt_msghdr);
1807 rtm = (struct rt_msghdr *)w->w_tmem;
1808 buflen = w->w_tmemsize - len;
1809 cp = (caddr_t)w->w_tmem + len;
1812 rtinfo->rti_addrs = 0;
1813 for (i = 0; i < RTAX_MAX; i++) {
1814 struct sockaddr *sa;
1816 if ((sa = rtinfo->rti_info[i]) == NULL)
1818 rtinfo->rti_addrs |= (1 << i);
1819 #ifdef COMPAT_FREEBSD32
1821 dlen = SA_SIZE32(sa);
1825 if (cp != NULL && buflen >= dlen) {
1826 KASSERT(dlen <= sizeof(ss),
1827 ("%s: sockaddr size overflow", __func__));
1828 bzero(&ss, sizeof(ss));
1829 bcopy(sa, &ss, sa->sa_len);
1830 sa = (struct sockaddr *)&ss;
1832 if (sa->sa_family == AF_INET6) {
1833 sin6 = (struct sockaddr_in6 *)sa;
1834 (void)sa6_recoverscope(sin6);
1837 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1840 } else if (cp != NULL) {
1842 * Buffer too small. Count needed size
1843 * and return with error.
1852 dlen = ALIGN(len) - len;
1864 /* fill header iff buffer is large enough */
1865 rtm->rtm_version = RTM_VERSION;
1866 rtm->rtm_type = type;
1867 rtm->rtm_msglen = len;
1872 if (w != NULL && cp == NULL)
1879 * This routine is called to generate a message from the routing
1880 * socket indicating that a redirect has occurred, a routing lookup
1881 * has failed, or that a protocol has detected timeouts to a particular
1885 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1888 struct rt_msghdr *rtm;
1890 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1892 if (V_route_cb.any_count == 0)
1894 m = rtsock_msg_mbuf(type, rtinfo);
1898 if (fibnum != RT_ALL_FIBS) {
1899 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1900 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1901 M_SETFIB(m, fibnum);
1902 m->m_flags |= RTS_FILTER_FIB;
1905 rtm = mtod(m, struct rt_msghdr *);
1906 rtm->rtm_flags = RTF_DONE | flags;
1907 rtm->rtm_errno = error;
1908 rtm->rtm_addrs = rtinfo->rti_addrs;
1909 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1913 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1916 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1920 * This routine is called to generate a message from the routing
1921 * socket indicating that the status of a network interface has changed.
1924 rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask __unused)
1926 struct if_msghdr *ifm;
1928 struct rt_addrinfo info;
1930 if (V_route_cb.any_count == 0)
1932 bzero((caddr_t)&info, sizeof(info));
1933 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1936 ifm = mtod(m, struct if_msghdr *);
1937 ifm->ifm_index = ifp->if_index;
1938 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1939 if_data_copy(ifp, &ifm->ifm_data);
1941 rt_dispatch(m, AF_UNSPEC);
1945 * Announce interface address arrival/withdraw.
1946 * Please do not call directly, use rt_addrmsg().
1947 * Assume input data to be valid.
1948 * Returns 0 on success.
1951 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1953 struct rt_addrinfo info;
1954 struct sockaddr *sa;
1957 struct ifa_msghdr *ifam;
1958 struct ifnet *ifp = ifa->ifa_ifp;
1959 struct sockaddr_storage ss;
1961 if (V_route_cb.any_count == 0)
1964 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1966 bzero((caddr_t)&info, sizeof(info));
1967 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1968 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1969 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1970 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1971 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1972 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1974 ifam = mtod(m, struct ifa_msghdr *);
1975 ifam->ifam_index = ifp->if_index;
1976 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1977 ifam->ifam_flags = ifa->ifa_flags;
1978 ifam->ifam_addrs = info.rti_addrs;
1980 if (fibnum != RT_ALL_FIBS) {
1981 M_SETFIB(m, fibnum);
1982 m->m_flags |= RTS_FILTER_FIB;
1985 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1991 * Announce route addition/removal to rtsock based on @rt data.
1992 * Callers are advives to use rt_routemsg() instead of using this
1993 * function directly.
1994 * Assume @rt data is consistent.
1996 * Returns 0 on success.
1999 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
2002 union sockaddr_union dst, mask;
2003 struct rt_addrinfo info;
2005 if (V_route_cb.any_count == 0)
2008 int family = rt_get_family(rt);
2009 init_sockaddrs_family(family, &dst.sa, &mask.sa);
2010 export_rtaddrs(rt, &dst.sa, &mask.sa);
2012 bzero((caddr_t)&info, sizeof(info));
2013 info.rti_info[RTAX_DST] = &dst.sa;
2014 info.rti_info[RTAX_NETMASK] = &mask.sa;
2015 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2016 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
2017 info.rti_ifp = nh->nh_ifp;
2019 return (rtsock_routemsg_info(cmd, &info, fibnum));
2023 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2025 struct rt_msghdr *rtm;
2026 struct sockaddr *sa;
2029 if (V_route_cb.any_count == 0)
2032 if (info->rti_flags & RTF_HOST)
2033 info->rti_info[RTAX_NETMASK] = NULL;
2035 m = rtsock_msg_mbuf(cmd, info);
2039 if (fibnum != RT_ALL_FIBS) {
2040 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
2041 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
2042 M_SETFIB(m, fibnum);
2043 m->m_flags |= RTS_FILTER_FIB;
2046 rtm = mtod(m, struct rt_msghdr *);
2047 rtm->rtm_addrs = info->rti_addrs;
2048 if (info->rti_ifp != NULL)
2049 rtm->rtm_index = info->rti_ifp->if_index;
2050 /* Add RTF_DONE to indicate command 'completion' required by API */
2051 info->rti_flags |= RTF_DONE;
2052 /* Reported routes has to be up */
2053 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
2054 info->rti_flags |= RTF_UP;
2055 rtm->rtm_flags = info->rti_flags;
2057 sa = info->rti_info[RTAX_DST];
2058 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2064 * This is the analogue to the rt_newaddrmsg which performs the same
2065 * function but for multicast group memberhips. This is easier since
2066 * there is no route state to worry about.
2069 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
2071 struct rt_addrinfo info;
2072 struct mbuf *m = NULL;
2073 struct ifnet *ifp = ifma->ifma_ifp;
2074 struct ifma_msghdr *ifmam;
2076 if (V_route_cb.any_count == 0)
2079 bzero((caddr_t)&info, sizeof(info));
2080 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2081 if (ifp && ifp->if_addr)
2082 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
2084 info.rti_info[RTAX_IFP] = NULL;
2086 * If a link-layer address is present, present it as a ``gateway''
2087 * (similarly to how ARP entries, e.g., are presented).
2089 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
2090 m = rtsock_msg_mbuf(cmd, &info);
2093 ifmam = mtod(m, struct ifma_msghdr *);
2094 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
2096 ifmam->ifmam_index = ifp->if_index;
2097 ifmam->ifmam_addrs = info.rti_addrs;
2098 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
2101 static struct mbuf *
2102 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
2103 struct rt_addrinfo *info)
2105 struct if_announcemsghdr *ifan;
2108 if (V_route_cb.any_count == 0)
2110 bzero((caddr_t)info, sizeof(*info));
2111 m = rtsock_msg_mbuf(type, info);
2113 ifan = mtod(m, struct if_announcemsghdr *);
2114 ifan->ifan_index = ifp->if_index;
2115 strlcpy(ifan->ifan_name, ifp->if_xname,
2116 sizeof(ifan->ifan_name));
2117 ifan->ifan_what = what;
2123 * This is called to generate routing socket messages indicating
2124 * IEEE80211 wireless events.
2125 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2128 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2131 struct rt_addrinfo info;
2133 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2136 * Append the ieee80211 data. Try to stick it in the
2137 * mbuf containing the ifannounce msg; otherwise allocate
2138 * a new mbuf and append.
2140 * NB: we assume m is a single mbuf.
2142 if (data_len > M_TRAILINGSPACE(m)) {
2143 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2148 bcopy(data, mtod(n, void *), data_len);
2149 n->m_len = data_len;
2151 } else if (data_len > 0) {
2152 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2153 m->m_len += data_len;
2155 if (m->m_flags & M_PKTHDR)
2156 m->m_pkthdr.len += data_len;
2157 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2158 rt_dispatch(m, AF_UNSPEC);
2163 * This is called to generate routing socket messages indicating
2164 * network interface arrival and departure.
2167 rt_ifannouncemsg(struct ifnet *ifp, int what)
2170 struct rt_addrinfo info;
2172 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2174 rt_dispatch(m, AF_UNSPEC);
2178 rt_dispatch(struct mbuf *m, sa_family_t saf)
2183 m->m_rtsock_family = saf;
2185 m->m_pkthdr.rcvif = V_loif;
2190 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
2194 * This is used in dumping the kernel table via sysctl().
2197 sysctl_dumpentry(struct rtentry *rt, void *vw)
2199 struct walkarg *w = vw;
2200 struct nhop_object *nh;
2204 if (!rt_is_exportable(rt, w->w_req->td->td_ucred))
2207 export_rtaddrs(rt, w->dst, w->mask);
2208 nh = rt_get_raw_nhop(rt);
2210 if (NH_IS_NHGRP(nh)) {
2211 const struct weightened_nhop *wn;
2214 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2215 for (int i = 0; i < num_nhops; i++) {
2216 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2222 sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2229 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2232 struct rt_addrinfo info;
2233 int error = 0, size;
2236 rtflags = nhop_get_rtflags(nh);
2238 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2241 bzero((caddr_t)&info, sizeof(info));
2242 info.rti_info[RTAX_DST] = w->dst;
2243 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2244 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2245 info.rti_info[RTAX_GENMASK] = 0;
2246 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2247 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2248 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2249 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2250 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2252 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2254 if (w->w_req && w->w_tmem) {
2255 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2257 bzero(&rtm->rtm_index,
2258 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2261 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2262 * and RTF_UP (if entry is linked, which is always true here).
2263 * Given that, use nhop rtflags & add RTF_UP.
2265 rtm->rtm_flags = rtflags | RTF_UP;
2266 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2267 rtm->rtm_flags = RTF_GATEWAY |
2268 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2269 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2270 rtm->rtm_rmx.rmx_weight = weight;
2271 rtm->rtm_index = nh->nh_ifp->if_index;
2272 rtm->rtm_addrs = info.rti_addrs;
2273 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2280 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2281 struct rt_addrinfo *info, struct walkarg *w, int len)
2283 struct if_msghdrl *ifm;
2284 struct if_data *ifd;
2286 ifm = (struct if_msghdrl *)w->w_tmem;
2288 #ifdef COMPAT_FREEBSD32
2289 if (w->w_req->flags & SCTL_MASK32) {
2290 struct if_msghdrl32 *ifm32;
2292 ifm32 = (struct if_msghdrl32 *)ifm;
2293 ifm32->ifm_addrs = info->rti_addrs;
2294 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2295 ifm32->ifm_index = ifp->if_index;
2296 ifm32->_ifm_spare1 = 0;
2297 ifm32->ifm_len = sizeof(*ifm32);
2298 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2299 ifm32->_ifm_spare2 = 0;
2300 ifd = &ifm32->ifm_data;
2304 ifm->ifm_addrs = info->rti_addrs;
2305 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2306 ifm->ifm_index = ifp->if_index;
2307 ifm->_ifm_spare1 = 0;
2308 ifm->ifm_len = sizeof(*ifm);
2309 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2310 ifm->_ifm_spare2 = 0;
2311 ifd = &ifm->ifm_data;
2314 memcpy(ifd, src_ifd, sizeof(*ifd));
2316 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2320 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2321 struct rt_addrinfo *info, struct walkarg *w, int len)
2323 struct if_msghdr *ifm;
2324 struct if_data *ifd;
2326 ifm = (struct if_msghdr *)w->w_tmem;
2328 #ifdef COMPAT_FREEBSD32
2329 if (w->w_req->flags & SCTL_MASK32) {
2330 struct if_msghdr32 *ifm32;
2332 ifm32 = (struct if_msghdr32 *)ifm;
2333 ifm32->ifm_addrs = info->rti_addrs;
2334 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2335 ifm32->ifm_index = ifp->if_index;
2336 ifm32->_ifm_spare1 = 0;
2337 ifd = &ifm32->ifm_data;
2341 ifm->ifm_addrs = info->rti_addrs;
2342 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2343 ifm->ifm_index = ifp->if_index;
2344 ifm->_ifm_spare1 = 0;
2345 ifd = &ifm->ifm_data;
2348 memcpy(ifd, src_ifd, sizeof(*ifd));
2350 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2354 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2355 struct walkarg *w, int len)
2357 struct ifa_msghdrl *ifam;
2358 struct if_data *ifd;
2360 ifam = (struct ifa_msghdrl *)w->w_tmem;
2362 #ifdef COMPAT_FREEBSD32
2363 if (w->w_req->flags & SCTL_MASK32) {
2364 struct ifa_msghdrl32 *ifam32;
2366 ifam32 = (struct ifa_msghdrl32 *)ifam;
2367 ifam32->ifam_addrs = info->rti_addrs;
2368 ifam32->ifam_flags = ifa->ifa_flags;
2369 ifam32->ifam_index = ifa->ifa_ifp->if_index;
2370 ifam32->_ifam_spare1 = 0;
2371 ifam32->ifam_len = sizeof(*ifam32);
2372 ifam32->ifam_data_off =
2373 offsetof(struct ifa_msghdrl32, ifam_data);
2374 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2375 ifd = &ifam32->ifam_data;
2379 ifam->ifam_addrs = info->rti_addrs;
2380 ifam->ifam_flags = ifa->ifa_flags;
2381 ifam->ifam_index = ifa->ifa_ifp->if_index;
2382 ifam->_ifam_spare1 = 0;
2383 ifam->ifam_len = sizeof(*ifam);
2384 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2385 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2386 ifd = &ifam->ifam_data;
2389 bzero(ifd, sizeof(*ifd));
2390 ifd->ifi_datalen = sizeof(struct if_data);
2391 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2392 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2393 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2394 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2396 /* Fixup if_data carp(4) vhid. */
2397 if (carp_get_vhid_p != NULL)
2398 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2400 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2404 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2405 struct walkarg *w, int len)
2407 struct ifa_msghdr *ifam;
2409 ifam = (struct ifa_msghdr *)w->w_tmem;
2410 ifam->ifam_addrs = info->rti_addrs;
2411 ifam->ifam_flags = ifa->ifa_flags;
2412 ifam->ifam_index = ifa->ifa_ifp->if_index;
2413 ifam->_ifam_spare1 = 0;
2414 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2416 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2420 sysctl_iflist(int af, struct walkarg *w)
2425 struct rt_addrinfo info;
2427 struct sockaddr_storage ss;
2429 bzero((caddr_t)&info, sizeof(info));
2430 bzero(&ifd, sizeof(ifd));
2431 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2432 if (w->w_arg && w->w_arg != ifp->if_index)
2434 if_data_copy(ifp, &ifd);
2436 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2437 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2440 info.rti_info[RTAX_IFP] = NULL;
2441 if (w->w_req && w->w_tmem) {
2442 if (w->w_op == NET_RT_IFLISTL)
2443 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2446 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2451 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2452 if (af && af != ifa->ifa_addr->sa_family)
2454 if (prison_if(w->w_req->td->td_ucred,
2455 ifa->ifa_addr) != 0)
2457 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2458 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2459 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2460 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2461 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2464 if (w->w_req && w->w_tmem) {
2465 if (w->w_op == NET_RT_IFLISTL)
2466 error = sysctl_iflist_ifaml(ifa, &info,
2469 error = sysctl_iflist_ifam(ifa, &info,
2475 info.rti_info[RTAX_IFA] = NULL;
2476 info.rti_info[RTAX_NETMASK] = NULL;
2477 info.rti_info[RTAX_BRD] = NULL;
2484 sysctl_ifmalist(int af, struct walkarg *w)
2486 struct rt_addrinfo info;
2488 struct ifmultiaddr *ifma;
2495 bzero((caddr_t)&info, sizeof(info));
2497 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2498 if (w->w_arg && w->w_arg != ifp->if_index)
2501 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2502 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2503 if (af && af != ifma->ifma_addr->sa_family)
2505 if (prison_if(w->w_req->td->td_ucred,
2506 ifma->ifma_addr) != 0)
2508 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2509 info.rti_info[RTAX_GATEWAY] =
2510 (ifma->ifma_addr->sa_family != AF_LINK) ?
2511 ifma->ifma_lladdr : NULL;
2512 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2515 if (w->w_req && w->w_tmem) {
2516 struct ifma_msghdr *ifmam;
2518 ifmam = (struct ifma_msghdr *)w->w_tmem;
2519 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2520 ifmam->ifmam_flags = 0;
2521 ifmam->ifmam_addrs = info.rti_addrs;
2522 ifmam->_ifmam_spare1 = 0;
2523 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2535 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2537 union sockaddr_union sa_dst, sa_mask;
2540 w->dst = (struct sockaddr *)&sa_dst;
2541 w->mask = (struct sockaddr *)&sa_mask;
2543 init_sockaddrs_family(family, w->dst, w->mask);
2545 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2549 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2551 struct epoch_tracker et;
2552 int *name = (int *)arg1;
2553 u_int namelen = arg2;
2554 struct rib_head *rnh = NULL; /* silence compiler. */
2555 int i, lim, error = EINVAL;
2567 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2569 fib = req->td->td_proc->p_fibnum;
2570 else if (namelen == 4)
2571 fib = (name[3] == RT_ALL_FIBS) ?
2572 req->td->td_proc->p_fibnum : name[3];
2574 return ((namelen < 3) ? EISDIR : ENOTDIR);
2575 if (fib < 0 || fib >= rt_numfibs)
2577 } else if (namelen != 3)
2578 return ((namelen < 3) ? EISDIR : ENOTDIR);
2582 bzero(&w, sizeof(w));
2587 error = sysctl_wire_old_buffer(req, 0);
2592 * Allocate reply buffer in advance.
2593 * All rtsock messages has maximum length of u_short.
2595 w.w_tmemsize = 65536;
2596 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2598 NET_EPOCH_ENTER(et);
2602 if (af == 0) { /* dump all tables */
2605 } else /* dump only one table */
2609 * take care of llinfo entries, the caller must
2612 if (w.w_op == NET_RT_FLAGS &&
2613 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2615 error = lltable_sysctl_dumparp(af, w.w_req);
2621 * take care of routing entries
2623 for (error = 0; error == 0 && i <= lim; i++) {
2624 rnh = rt_tables_get_rnh(fib, i);
2626 rtable_sysctl_dump(fib, i, &w);
2628 error = EAFNOSUPPORT;
2633 /* Allow dumping one specific af/fib at a time */
2639 if (fib < 0 || fib > rt_numfibs) {
2643 rnh = rt_tables_get_rnh(fib, af);
2645 error = EAFNOSUPPORT;
2648 if (w.w_op == NET_RT_NHOP)
2649 error = nhops_dump_sysctl(rnh, w.w_req);
2652 error = nhgrp_dump_sysctl(rnh, w.w_req);
2658 case NET_RT_IFLISTL:
2659 error = sysctl_iflist(af, &w);
2662 case NET_RT_IFMALIST:
2663 error = sysctl_ifmalist(af, &w);
2668 free(w.w_tmem, M_TEMP);
2672 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2673 sysctl_rtsock, "Return route tables and interface/address lists");
2676 * Definitions of protocols supported in the ROUTE domain.
2679 static struct domain routedomain; /* or at least forward */
2681 static struct protosw routesw = {
2682 .pr_type = SOCK_RAW,
2683 .pr_flags = PR_ATOMIC|PR_ADDR,
2684 .pr_abort = rts_close,
2685 .pr_attach = rts_attach,
2686 .pr_detach = rts_detach,
2687 .pr_send = rts_send,
2688 .pr_shutdown = rts_shutdown,
2689 .pr_disconnect = rts_disconnect,
2690 .pr_close = rts_close,
2693 static struct domain routedomain = {
2694 .dom_family = PF_ROUTE,
2695 .dom_name = "route",
2697 .dom_protosw = { &routesw },