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
31 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
35 #include "opt_route.h"
37 #include "opt_inet6.h"
39 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/eventhandler.h>
43 #include <sys/domain.h>
45 #include <sys/malloc.h>
49 #include <sys/protosw.h>
50 #include <sys/rmlock.h>
51 #include <sys/rwlock.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
59 #include <net/if_var.h>
60 #include <net/if_private.h>
61 #include <net/if_dl.h>
62 #include <net/if_llatbl.h>
63 #include <net/if_types.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
66 #include <net/route/route_ctl.h>
67 #include <net/route/route_var.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #include <netinet/ip_carp.h>
74 #include <netinet6/in6_var.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/scope6_var.h>
78 #include <net/route/nhop.h>
80 #define DEBUG_MOD_NAME rtsock
81 #define DEBUG_MAX_LEVEL LOG_DEBUG
82 #include <net/route/route_debug.h>
83 _DECLARE_DEBUG(LOG_INFO);
85 #ifdef COMPAT_FREEBSD32
86 #include <sys/mount.h>
87 #include <compat/freebsd32/freebsd32.h>
97 struct if_data ifm_data;
100 struct if_msghdrl32 {
107 uint16_t _ifm_spare1;
109 uint16_t ifm_data_off;
110 uint32_t _ifm_spare2;
111 struct if_data ifm_data;
114 struct ifa_msghdrl32 {
115 uint16_t ifam_msglen;
116 uint8_t ifam_version;
121 uint16_t _ifam_spare1;
123 uint16_t ifam_data_off;
125 struct if_data ifam_data;
128 #define SA_SIZE32(sa) \
129 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
131 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
133 #endif /* COMPAT_FREEBSD32 */
135 struct linear_buffer {
136 char *base; /* Base allocated memory pointer */
137 uint32_t offset; /* Currently used offset */
138 uint32_t size; /* Total buffer size */
140 #define SCRATCH_BUFFER_SIZE 1024
142 #define RTS_PID_LOG(_l, _fmt, ...) RT_LOG_##_l(_l, "PID %d: " _fmt, curproc ? curproc->p_pid : 0, ## __VA_ARGS__)
144 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
146 /* NB: these are not modified */
147 static struct sockaddr route_src = { 2, PF_ROUTE, };
148 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
150 /* These are external hooks for CARP. */
151 int (*carp_get_vhid_p)(struct ifaddr *);
154 * Used by rtsock callback code to decide whether to filter the update
155 * notification to a socket bound to a particular FIB.
157 #define RTS_FILTER_FIB M_PROTO8
159 * Used to store address family of the notification.
161 #define m_rtsock_family m_pkthdr.PH_loc.eight[0]
164 LIST_ENTRY(rcb) list;
165 struct socket *rcb_socket;
166 sa_family_t rcb_family;
170 LIST_HEAD(, rcb) cblist;
171 int ip_count; /* attached w/ AF_INET */
172 int ip6_count; /* attached w/ AF_INET6 */
173 int any_count; /* total attached */
175 VNET_DEFINE_STATIC(route_cb_t, route_cb);
176 #define V_route_cb VNET(route_cb)
178 struct mtx rtsock_mtx;
179 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
181 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
182 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
183 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
185 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
192 struct sysctl_req *w_req;
193 struct sockaddr *dst;
194 struct sockaddr *mask;
197 static void rts_input(struct mbuf *m);
198 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
199 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
200 struct walkarg *w, int *plen);
201 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
202 struct rt_addrinfo *rtinfo);
203 static int cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb);
204 static int sysctl_dumpentry(struct rtentry *rt, void *vw);
205 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
206 uint32_t weight, struct walkarg *w);
207 static int sysctl_iflist(int af, struct walkarg *w);
208 static int sysctl_ifmalist(int af, struct walkarg *w);
209 static void rt_getmetrics(const struct rtentry *rt,
210 const struct nhop_object *nh, struct rt_metrics *out);
211 static void rt_dispatch(struct mbuf *, sa_family_t);
212 static void rt_ifannouncemsg(struct ifnet *ifp, int what);
213 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
214 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
215 static int update_rtm_from_rc(struct rt_addrinfo *info,
216 struct rt_msghdr **prtm, int alloc_len,
217 struct rib_cmd_info *rc, struct nhop_object *nh);
218 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
219 struct mbuf *m, sa_family_t saf, u_int fibnum,
221 static void rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc);
222 static void rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask);
224 static struct netisr_handler rtsock_nh = {
226 .nh_handler = rts_input,
227 .nh_proto = NETISR_ROUTE,
228 .nh_policy = NETISR_POLICY_SOURCE,
232 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
236 netisr_getqlimit(&rtsock_nh, &qlimit);
237 error = sysctl_handle_int(oidp, &qlimit, 0, req);
238 if (error || !req->newptr)
242 return (netisr_setqlimit(&rtsock_nh, qlimit));
244 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
245 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
246 0, 0, sysctl_route_netisr_maxqlen, "I",
247 "maximum routing socket dispatch queue length");
254 if (IS_DEFAULT_VNET(curvnet)) {
255 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
256 rtsock_nh.nh_qlimit = tmp;
257 netisr_register(&rtsock_nh);
261 netisr_register_vnet(&rtsock_nh);
264 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
269 vnet_rts_uninit(void)
272 netisr_unregister_vnet(&rtsock_nh);
274 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
279 report_route_event(const struct rib_cmd_info *rc, void *_cbdata)
281 uint32_t fibnum = (uint32_t)(uintptr_t)_cbdata;
282 struct nhop_object *nh;
284 nh = rc->rc_cmd == RTM_DELETE ? rc->rc_nh_old : rc->rc_nh_new;
285 rt_routemsg(rc->rc_cmd, rc->rc_rt, nh, fibnum);
289 rts_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
292 if ((rc->rc_nh_new && NH_IS_NHGRP(rc->rc_nh_new)) ||
293 (rc->rc_nh_old && NH_IS_NHGRP(rc->rc_nh_old))) {
294 rib_decompose_notification(rc, report_route_event,
295 (void *)(uintptr_t)fibnum);
298 report_route_event(rc, (void *)(uintptr_t)fibnum);
300 static struct rtbridge rtsbridge = {
301 .route_f = rts_handle_route_event,
302 .ifmsg_f = rtsock_ifmsg,
304 static struct rtbridge *rtsbridge_orig_p;
307 rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc)
309 netlink_callback_p->route_f(fibnum, rc);
315 rtsbridge_orig_p = rtsock_callback_p;
316 rtsock_callback_p = &rtsbridge;
318 SYSINIT(rtsock_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtsock_init, NULL);
321 rts_handle_ifnet_arrival(void *arg __unused, struct ifnet *ifp)
323 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
325 EVENTHANDLER_DEFINE(ifnet_arrival_event, rts_handle_ifnet_arrival, NULL, 0);
328 rts_handle_ifnet_departure(void *arg __unused, struct ifnet *ifp)
330 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
332 EVENTHANDLER_DEFINE(ifnet_departure_event, rts_handle_ifnet_departure, NULL, 0);
335 rts_append_data(struct socket *so, struct mbuf *m)
338 if (sbappendaddr(&so->so_rcv, &route_src, m, NULL) == 0) {
346 rts_input(struct mbuf *m)
353 LIST_FOREACH(rcb, &V_route_cb.cblist, list) {
354 if (rcb->rcb_family != AF_UNSPEC &&
355 rcb->rcb_family != m->m_rtsock_family)
357 if ((m->m_flags & RTS_FILTER_FIB) &&
358 M_GETFIB(m) != rcb->rcb_socket->so_fibnum)
363 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
365 rts_append_data(last, n);
367 last = rcb->rcb_socket;
370 rts_append_data(last, m);
377 rts_close(struct socket *so)
380 soisdisconnected(so);
383 static SYSCTL_NODE(_net, OID_AUTO, rtsock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
384 "Routing socket infrastructure");
385 static u_long rts_sendspace = 8192;
386 SYSCTL_ULONG(_net_rtsock, OID_AUTO, sendspace, CTLFLAG_RW, &rts_sendspace, 0,
387 "Default routing socket send space");
388 static u_long rts_recvspace = 8192;
389 SYSCTL_ULONG(_net_rtsock, OID_AUTO, recvspace, CTLFLAG_RW, &rts_recvspace, 0,
390 "Default routing socket receive space");
393 rts_attach(struct socket *so, int proto, struct thread *td)
398 error = soreserve(so, rts_sendspace, rts_recvspace);
402 rcb = malloc(sizeof(*rcb), M_PCB, M_WAITOK);
403 rcb->rcb_socket = so;
404 rcb->rcb_family = proto;
407 so->so_fibnum = td->td_proc->p_fibnum;
408 so->so_options |= SO_USELOOPBACK;
411 LIST_INSERT_HEAD(&V_route_cb.cblist, rcb, list);
414 V_route_cb.ip_count++;
417 V_route_cb.ip6_count++;
420 V_route_cb.any_count++;
428 rts_detach(struct socket *so)
430 struct rcb *rcb = so->so_pcb;
433 LIST_REMOVE(rcb, list);
434 switch(rcb->rcb_family) {
436 V_route_cb.ip_count--;
439 V_route_cb.ip6_count--;
442 V_route_cb.any_count--;
449 rts_disconnect(struct socket *so)
456 rts_shutdown(struct socket *so)
463 #ifndef _SOCKADDR_UNION_DEFINED
464 #define _SOCKADDR_UNION_DEFINED
466 * The union of all possible address formats we handle.
468 union sockaddr_union {
470 struct sockaddr_in sin;
471 struct sockaddr_in6 sin6;
473 #endif /* _SOCKADDR_UNION_DEFINED */
476 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
477 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
479 #if defined(INET) || defined(INET6)
480 struct epoch_tracker et;
483 /* First, see if the returned address is part of the jail. */
484 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
485 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
489 switch (info->rti_info[RTAX_DST]->sa_family) {
499 * Try to find an address on the given outgoing interface
500 * that belongs to the jail.
503 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
506 if (sa->sa_family != AF_INET)
508 ia = ((struct sockaddr_in *)sa)->sin_addr;
509 if (prison_check_ip4(cred, &ia) == 0) {
517 * As a last resort return the 'default' jail address.
519 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
521 if (prison_get_ip4(cred, &ia) != 0)
524 bzero(&saun->sin, sizeof(struct sockaddr_in));
525 saun->sin.sin_len = sizeof(struct sockaddr_in);
526 saun->sin.sin_family = AF_INET;
527 saun->sin.sin_addr.s_addr = ia.s_addr;
528 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
541 * Try to find an address on the given outgoing interface
542 * that belongs to the jail.
545 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
548 if (sa->sa_family != AF_INET6)
550 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
551 &ia6, sizeof(struct in6_addr));
552 if (prison_check_ip6(cred, &ia6) == 0) {
560 * As a last resort return the 'default' jail address.
562 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
564 if (prison_get_ip6(cred, &ia6) != 0)
567 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
568 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
569 saun->sin6.sin6_family = AF_INET6;
570 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
571 if (sa6_recoverscope(&saun->sin6) != 0)
573 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
584 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
589 if (V_loif == NULL) {
590 RTS_PID_LOG(LOG_INFO, "Unable to add blackhole/reject nhop without loopback");
593 info->rti_ifp = V_loif;
595 saf = info->rti_info[RTAX_DST]->sa_family;
597 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
598 if (ifa->ifa_addr->sa_family == saf) {
603 if (info->rti_ifa == NULL) {
604 RTS_PID_LOG(LOG_INFO, "Unable to find ifa for blackhole/reject nhop");
608 bzero(saun, sizeof(union sockaddr_union));
612 saun->sin.sin_family = AF_INET;
613 saun->sin.sin_len = sizeof(struct sockaddr_in);
614 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
619 saun->sin6.sin6_family = AF_INET6;
620 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
621 saun->sin6.sin6_addr = in6addr_loopback;
625 RTS_PID_LOG(LOG_INFO, "unsupported family: %d", saf);
628 info->rti_info[RTAX_GATEWAY] = &saun->sa;
629 info->rti_flags |= RTF_GATEWAY;
635 * Fills in @info based on userland-provided @rtm message.
637 * Returns 0 on success.
640 fill_addrinfo(struct rt_msghdr *rtm, int len, struct linear_buffer *lb, u_int fibnum,
641 struct rt_addrinfo *info)
645 rtm->rtm_pid = curproc->p_pid;
646 info->rti_addrs = rtm->rtm_addrs;
648 info->rti_mflags = rtm->rtm_inits;
649 info->rti_rmx = &rtm->rtm_rmx;
652 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
653 * link-local address because rtrequest requires addresses with
656 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
659 info->rti_flags = rtm->rtm_flags;
660 error = cleanup_xaddrs(info, lb);
664 * Verify that the caller has the appropriate privilege; RTM_GET
665 * is the only operation the non-superuser is allowed.
667 if (rtm->rtm_type != RTM_GET) {
668 error = priv_check(curthread, PRIV_NET_ROUTE);
674 * The given gateway address may be an interface address.
675 * For example, issuing a "route change" command on a route
676 * entry that was created from a tunnel, and the gateway
677 * address given is the local end point. In this case the
678 * RTF_GATEWAY flag must be cleared or the destination will
679 * not be reachable even though there is no error message.
681 if (info->rti_info[RTAX_GATEWAY] != NULL &&
682 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
683 struct nhop_object *nh;
686 * A host route through the loopback interface is
687 * installed for each interface adddress. In pre 8.0
688 * releases the interface address of a PPP link type
689 * is not reachable locally. This behavior is fixed as
690 * part of the new L2/L3 redesign and rewrite work. The
691 * signature of this interface address route is the
692 * AF_LINK sa_family type of the gateway, and the
693 * rt_ifp has the IFF_LOOPBACK flag set.
695 nh = rib_lookup(fibnum, info->rti_info[RTAX_GATEWAY], NHR_NONE, 0);
696 if (nh != NULL && nh->gw_sa.sa_family == AF_LINK &&
697 nh->nh_ifp->if_flags & IFF_LOOPBACK) {
698 info->rti_flags &= ~RTF_GATEWAY;
699 info->rti_flags |= RTF_GWFLAG_COMPAT;
706 static struct nhop_object *
707 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
709 if (!NH_IS_NHGRP(nh))
712 const struct weightened_nhop *wn;
714 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
717 for (int i = 0; i < num_nhops; i++) {
718 if (match_nhop_gw(wn[i].nh, gw))
726 * Handles RTM_GET message from routing socket, returning matching rt.
729 * 0 on success, with locked and referenced matching rt in @rt_nrt
733 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
734 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
737 struct rib_head *rnh;
738 struct nhop_object *nh;
741 saf = info->rti_info[RTAX_DST]->sa_family;
743 rnh = rt_tables_get_rnh(fibnum, saf);
745 return (EAFNOSUPPORT);
750 * By (implicit) convention host route (one without netmask)
751 * means longest-prefix-match request and the route with netmask
752 * means exact-match lookup.
753 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
754 * prefixes, use original data to check for the netmask presence.
756 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
758 * Provide longest prefix match for
759 * address lookup (no mask).
760 * 'route -n get addr'
762 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
763 info->rti_info[RTAX_DST], &rnh->head);
765 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
766 info->rti_info[RTAX_DST],
767 info->rti_info[RTAX_NETMASK], &rnh->head);
769 if (rc->rc_rt == NULL) {
774 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
780 * If performing proxied L2 entry insertion, and
781 * the actual PPP host entry is found, perform
782 * another search to retrieve the prefix route of
783 * the local end point of the PPP link.
784 * TODO: move this logic to userland.
786 if (rtm->rtm_flags & RTF_ANNOUNCE) {
787 struct sockaddr_storage laddr;
789 if (nh->nh_ifp != NULL &&
790 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
793 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
796 rt_maskedcopy(ifa->ifa_addr,
797 (struct sockaddr *)&laddr,
800 rt_maskedcopy(nh->nh_ifa->ifa_addr,
801 (struct sockaddr *)&laddr,
802 nh->nh_ifa->ifa_netmask);
804 * refactor rt and no lock operation necessary
806 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(
807 (struct sockaddr *)&laddr, &rnh->head);
808 if (rc->rc_rt == NULL) {
812 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
819 rc->rc_nh_weight = rc->rc_rt->rt_weight;
826 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
829 if (family == AF_INET) {
830 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
831 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
833 bzero(dst4, sizeof(struct sockaddr_in));
834 bzero(mask4, sizeof(struct sockaddr_in));
836 dst4->sin_family = AF_INET;
837 dst4->sin_len = sizeof(struct sockaddr_in);
838 mask4->sin_family = AF_INET;
839 mask4->sin_len = sizeof(struct sockaddr_in);
843 if (family == AF_INET6) {
844 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
845 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
847 bzero(dst6, sizeof(struct sockaddr_in6));
848 bzero(mask6, sizeof(struct sockaddr_in6));
850 dst6->sin6_family = AF_INET6;
851 dst6->sin6_len = sizeof(struct sockaddr_in6);
852 mask6->sin6_family = AF_INET6;
853 mask6->sin6_len = sizeof(struct sockaddr_in6);
859 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
860 struct sockaddr *mask)
863 if (dst->sa_family == AF_INET) {
864 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
865 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
866 uint32_t scopeid = 0;
867 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
873 if (dst->sa_family == AF_INET6) {
874 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
875 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
876 uint32_t scopeid = 0;
877 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
878 &mask6->sin6_addr, &scopeid);
879 dst6->sin6_scope_id = scopeid;
886 update_rtm_from_info(struct rt_addrinfo *info, struct rt_msghdr **prtm,
889 struct rt_msghdr *rtm, *orig_rtm = NULL;
894 /* Check if we need to realloc storage */
895 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
896 if (len > alloc_len) {
897 struct rt_msghdr *tmp_rtm;
899 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
902 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
908 * Delay freeing original rtm as info contains
909 * data referencing it.
913 w.w_tmem = (caddr_t)rtm;
914 w.w_tmemsize = alloc_len;
915 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
916 rtm->rtm_addrs = info->rti_addrs;
918 if (orig_rtm != NULL)
919 free(orig_rtm, M_TEMP);
926 * Update sockaddrs, flags, etc in @prtm based on @rc data.
927 * rtm can be reallocated.
929 * Returns 0 on success, along with pointer to (potentially reallocated)
934 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
935 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
937 union sockaddr_union saun;
938 struct rt_msghdr *rtm;
943 union sockaddr_union sa_dst, sa_mask;
944 int family = info->rti_info[RTAX_DST]->sa_family;
945 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
946 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
948 info->rti_info[RTAX_DST] = &sa_dst.sa;
949 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
950 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
951 info->rti_info[RTAX_GENMASK] = 0;
953 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
955 info->rti_info[RTAX_IFP] =
956 ifp->if_addr->ifa_addr;
957 error = rtm_get_jailed(info, ifp, nh,
958 &saun, curthread->td_ucred);
961 if (ifp->if_flags & IFF_POINTOPOINT)
962 info->rti_info[RTAX_BRD] =
963 nh->nh_ifa->ifa_dstaddr;
964 rtm->rtm_index = ifp->if_index;
966 info->rti_info[RTAX_IFP] = NULL;
967 info->rti_info[RTAX_IFA] = NULL;
969 } else if (ifp != NULL)
970 rtm->rtm_index = ifp->if_index;
972 if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0)
976 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
977 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
978 rtm->rtm_flags = RTF_GATEWAY |
979 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
980 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
981 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
988 save_del_notification(const struct rib_cmd_info *rc, void *_cbdata)
990 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
992 if (rc->rc_cmd == RTM_DELETE)
997 save_add_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_ADD)
1006 #if defined(INET6) || defined(INET)
1007 static struct sockaddr *
1008 alloc_sockaddr_aligned(struct linear_buffer *lb, int len)
1010 len = roundup2(len, sizeof(uint64_t));
1011 if (lb->offset + len > lb->size)
1013 struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset);
1020 rts_send(struct socket *so, int flags, struct mbuf *m,
1021 struct sockaddr *nam, struct mbuf *control, struct thread *td)
1023 struct rt_msghdr *rtm = NULL;
1024 struct rt_addrinfo info;
1025 struct epoch_tracker et;
1027 struct sockaddr_storage ss;
1028 struct sockaddr_in6 *sin6;
1029 int i, rti_need_deembed = 0;
1031 int alloc_len = 0, len, error = 0, fibnum;
1032 sa_family_t saf = AF_UNSPEC;
1033 struct rib_cmd_info rc;
1034 struct nhop_object *nh;
1036 if ((flags & PRUS_OOB) || control != NULL) {
1038 if (control != NULL)
1040 return (EOPNOTSUPP);
1043 fibnum = so->so_fibnum;
1044 #define senderr(e) { error = e; goto flush;}
1045 if (m == NULL || ((m->m_len < sizeof(long)) &&
1046 (m = m_pullup(m, sizeof(long))) == NULL))
1048 if ((m->m_flags & M_PKTHDR) == 0)
1049 panic("route_output");
1050 NET_EPOCH_ENTER(et);
1051 len = m->m_pkthdr.len;
1052 if (len < sizeof(*rtm) ||
1053 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1057 * Most of current messages are in range 200-240 bytes,
1058 * minimize possible re-allocation on reply using larger size
1059 * buffer aligned on 1k boundaty.
1061 alloc_len = roundup2(len, 1024);
1062 int total_len = alloc_len + SCRATCH_BUFFER_SIZE;
1063 if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL)
1066 m_copydata(m, 0, len, (caddr_t)rtm);
1067 bzero(&info, sizeof(info));
1069 struct linear_buffer lb = {
1070 .base = (char *)rtm + alloc_len,
1071 .size = SCRATCH_BUFFER_SIZE,
1074 if (rtm->rtm_version != RTM_VERSION) {
1075 /* Do not touch message since format is unknown */
1078 senderr(EPROTONOSUPPORT);
1082 * Starting from here, it is possible
1083 * to alter original message and insert
1084 * caller PID and error value.
1087 if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) {
1090 /* fill_addringo() embeds scope into IPv6 addresses */
1092 rti_need_deembed = 1;
1095 saf = info.rti_info[RTAX_DST]->sa_family;
1097 /* support for new ARP code */
1098 if (rtm->rtm_flags & RTF_LLDATA) {
1099 error = lla_rt_output(rtm, &info);
1103 union sockaddr_union gw_saun;
1104 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1105 if (blackhole_flags != 0) {
1106 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1107 error = fill_blackholeinfo(&info, &gw_saun);
1109 RTS_PID_LOG(LOG_DEBUG, "both BLACKHOLE and REJECT flags specifiied");
1116 switch (rtm->rtm_type) {
1119 if (rtm->rtm_type == RTM_ADD) {
1120 if (info.rti_info[RTAX_GATEWAY] == NULL) {
1121 RTS_PID_LOG(LOG_DEBUG, "RTM_ADD w/o gateway");
1125 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1127 rtsock_notify_event(fibnum, &rc);
1129 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1130 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1131 struct rib_cmd_info rc_simple = {};
1132 rib_decompose_notification(&rc,
1133 save_add_notification, (void *)&rc_simple);
1137 /* nh MAY be empty if RTM_CHANGE request is no-op */
1140 rtm->rtm_index = nh->nh_ifp->if_index;
1141 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1147 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1149 rtsock_notify_event(fibnum, &rc);
1151 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1152 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1153 struct rib_cmd_info rc_simple = {};
1154 rib_decompose_notification(&rc,
1155 save_del_notification, (void *)&rc_simple);
1164 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1169 if (!rt_is_exportable(rc.rc_rt, curthread->td_ucred))
1174 senderr(EOPNOTSUPP);
1177 if (error == 0 && nh != NULL) {
1178 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1180 * Note that some sockaddr pointers may have changed to
1181 * point to memory outsize @rtm. Some may be pointing
1182 * to the on-stack variables.
1183 * Given that, any pointer in @info CANNOT BE USED.
1187 * scopeid deembedding has been performed while
1188 * writing updated rtm in rtsock_msg_buffer().
1189 * With that in mind, skip deembedding procedure below.
1192 rti_need_deembed = 0;
1201 if (rti_need_deembed) {
1202 /* sin6_scope_id is recovered before sending rtm. */
1203 sin6 = (struct sockaddr_in6 *)&ss;
1204 for (i = 0; i < RTAX_MAX; i++) {
1205 if (info.rti_info[i] == NULL)
1207 if (info.rti_info[i]->sa_family != AF_INET6)
1209 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1210 if (sa6_recoverscope(sin6) == 0)
1211 bcopy(sin6, info.rti_info[i],
1214 if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) {
1221 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1227 * Sends the prepared reply message in @rtm to all rtsock clients.
1228 * Frees @m and @rtm.
1232 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1233 sa_family_t saf, u_int fibnum, int rtm_errno)
1235 struct rcb *rcb = NULL;
1238 * Check to see if we don't want our own messages.
1240 if ((so->so_options & SO_USELOOPBACK) == 0) {
1241 if (V_route_cb.any_count <= 1) {
1247 /* There is another listener, so construct message */
1253 rtm->rtm_errno = rtm_errno;
1255 rtm->rtm_flags |= RTF_DONE;
1257 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1258 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1261 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1262 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1267 M_SETFIB(m, fibnum);
1268 m->m_flags |= RTS_FILTER_FIB;
1271 * XXX insure we don't get a copy by
1272 * invalidating our protocol
1274 sa_family_t family = rcb->rcb_family;
1275 rcb->rcb_family = AF_UNSPEC;
1276 rt_dispatch(m, saf);
1277 rcb->rcb_family = family;
1279 rt_dispatch(m, saf);
1284 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1285 struct rt_metrics *out)
1288 bzero(out, sizeof(*out));
1289 out->rmx_mtu = nh->nh_mtu;
1290 out->rmx_weight = rt->rt_weight;
1291 out->rmx_nhidx = nhop_get_idx(nh);
1292 /* Kernel -> userland timebase conversion. */
1293 out->rmx_expire = nhop_get_expire(nh) ?
1294 nhop_get_expire(nh) - time_uptime + time_second : 0;
1298 * Extract the addresses of the passed sockaddrs.
1299 * Do a little sanity checking so as to avoid bad memory references.
1300 * This data is derived straight from userland.
1303 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1305 struct sockaddr *sa;
1308 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1309 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1311 sa = (struct sockaddr *)cp;
1315 if (cp + sa->sa_len > cplim) {
1316 RTS_PID_LOG(LOG_DEBUG, "sa_len too big for sa type %d", i);
1320 * there are no more.. quit now
1321 * If there are more bits, they are in error.
1322 * I've seen this. route(1) can evidently generate these.
1323 * This causes kernel to core dump.
1324 * for compatibility, If we see this, point to a safe address.
1326 if (sa->sa_len == 0) {
1327 rtinfo->rti_info[i] = &sa_zero;
1328 return (0); /* should be EINVAL but for compat */
1332 if (sa->sa_family == AF_INET6)
1333 sa6_embedscope((struct sockaddr_in6 *)sa,
1336 rtinfo->rti_info[i] = sa;
1344 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1347 const struct sockaddr_in nsin = {
1348 .sin_family = AF_INET,
1349 .sin_len = sizeof(struct sockaddr_in),
1358 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1362 const struct sockaddr_in6 nsin6 = {
1363 .sin6_family = AF_INET6,
1364 .sin6_len = sizeof(struct sockaddr_in6),
1365 .sin6_addr = *addr6,
1366 .sin6_scope_id = scopeid,
1372 #if defined(INET6) || defined(INET)
1374 * Checks if gateway is suitable for lltable operations.
1375 * Lltable code requires AF_LINK gateway with ifindex
1376 * and mac address specified.
1377 * Returns 0 on success.
1380 cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1382 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1384 if (sdl->sdl_family != AF_LINK)
1387 if (sdl->sdl_index == 0) {
1388 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gateway w/o ifindex");
1392 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len) {
1393 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gw: sdl_nlen/sdl_alen too large");
1401 cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb)
1403 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1404 struct sockaddr *sa;
1406 if (info->rti_flags & RTF_LLDATA)
1407 return (cleanup_xaddrs_lladdr(info));
1409 switch (gw->sa_family) {
1413 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1415 /* Ensure reads do not go beyoud SA boundary */
1416 if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) {
1417 RTS_PID_LOG(LOG_DEBUG, "gateway sin_len too small: %d",
1421 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in));
1424 fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr);
1425 info->rti_info[RTAX_GATEWAY] = sa;
1432 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1433 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1434 RTS_PID_LOG(LOG_DEBUG, "gateway sin6_len too small: %d",
1438 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1444 struct sockaddr_dl *gw_sdl;
1446 size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data);
1447 gw_sdl = (struct sockaddr_dl *)gw;
1448 if (gw_sdl->sdl_len < sdl_min_len) {
1449 RTS_PID_LOG(LOG_DEBUG, "gateway sdl_len too small: %d",
1453 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short));
1457 const struct sockaddr_dl_short sdl = {
1458 .sdl_family = AF_LINK,
1459 .sdl_len = sizeof(struct sockaddr_dl_short),
1460 .sdl_index = gw_sdl->sdl_index,
1462 *((struct sockaddr_dl_short *)sa) = sdl;
1463 info->rti_info[RTAX_GATEWAY] = sa;
1473 remove_netmask(struct rt_addrinfo *info)
1475 info->rti_info[RTAX_NETMASK] = NULL;
1476 info->rti_flags |= RTF_HOST;
1477 info->rti_addrs &= ~RTA_NETMASK;
1482 cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb)
1484 struct sockaddr_in *dst_sa, *mask_sa;
1485 const int sa_len = sizeof(struct sockaddr_in);
1486 struct in_addr dst, mask;
1488 /* Check & fixup dst/netmask combination first */
1489 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1490 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1492 /* Ensure reads do not go beyound the buffer size */
1493 if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero)) {
1494 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin_len too small: %d",
1499 if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1501 * Some older routing software encode mask length into the
1502 * sin_len, thus resulting in "truncated" sockaddr.
1504 int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr);
1507 if (len > sizeof(struct in_addr))
1508 len = sizeof(struct in_addr);
1509 memcpy(&mask, &mask_sa->sin_addr, len);
1511 RTS_PID_LOG(LOG_DEBUG, "prefix mask sin_len too small: %d",
1516 mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST;
1518 dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr));
1520 /* Construct new "clean" dst/mask sockaddresses */
1521 if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1523 fill_sockaddr_inet(dst_sa, dst);
1524 info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa;
1526 if (mask.s_addr != INADDR_BROADCAST) {
1527 if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1529 fill_sockaddr_inet(mask_sa, mask);
1530 info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa;
1531 info->rti_flags &= ~RTF_HOST;
1533 remove_netmask(info);
1536 if (info->rti_info[RTAX_GATEWAY] != NULL)
1537 return (cleanup_xaddrs_gateway(info, lb));
1545 cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb)
1547 struct sockaddr *sa;
1548 struct sockaddr_in6 *dst_sa, *mask_sa;
1549 struct in6_addr mask, *dst;
1550 const int sa_len = sizeof(struct sockaddr_in6);
1552 /* Check & fixup dst/netmask combination first */
1553 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1554 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1556 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1557 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin6_len too small: %d",
1562 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1564 * Some older routing software encode mask length into the
1565 * sin6_len, thus resulting in "truncated" sockaddr.
1567 int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1569 bzero(&mask, sizeof(mask));
1570 if (len > sizeof(struct in6_addr))
1571 len = sizeof(struct in6_addr);
1572 memcpy(&mask, &mask_sa->sin6_addr, len);
1574 RTS_PID_LOG(LOG_DEBUG, "rtsock: prefix mask sin6_len too small: %d",
1579 mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1581 dst = &dst_sa->sin6_addr;
1582 IN6_MASK_ADDR(dst, &mask);
1584 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1586 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0);
1587 info->rti_info[RTAX_DST] = sa;
1589 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) {
1590 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1592 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0);
1593 info->rti_info[RTAX_NETMASK] = sa;
1594 info->rti_flags &= ~RTF_HOST;
1596 remove_netmask(info);
1599 if (info->rti_info[RTAX_GATEWAY] != NULL)
1600 return (cleanup_xaddrs_gateway(info, lb));
1607 cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb)
1609 int error = EAFNOSUPPORT;
1611 if (info->rti_info[RTAX_DST] == NULL) {
1612 RTS_PID_LOG(LOG_DEBUG, "prefix dst is not set");
1616 if (info->rti_flags & RTF_LLDATA) {
1618 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1619 * prefix along with the actual address in RTA_DST.
1620 * Remove netmask to avoid unnecessary address masking.
1622 remove_netmask(info);
1625 switch (info->rti_info[RTAX_DST]->sa_family) {
1628 error = cleanup_xaddrs_inet(info, lb);
1633 error = cleanup_xaddrs_inet6(info, lb);
1642 * Fill in @dmask with valid netmask leaving original @smask
1643 * intact. Mostly used with radix netmasks.
1646 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1647 struct sockaddr_storage *dmask)
1649 if (dst == NULL || smask == NULL)
1652 memset(dmask, 0, dst->sa_len);
1653 memcpy(dmask, smask, smask->sa_len);
1654 dmask->ss_len = dst->sa_len;
1655 dmask->ss_family = dst->sa_family;
1657 return ((struct sockaddr *)dmask);
1661 * Writes information related to @rtinfo object to newly-allocated mbuf.
1662 * Assumes MCLBYTES is enough to construct any message.
1663 * Used for OS notifications of vaious events (if/ifa announces,etc)
1665 * Returns allocated mbuf or NULL on failure.
1667 static struct mbuf *
1668 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1670 struct sockaddr_storage ss;
1671 struct rt_msghdr *rtm;
1674 struct sockaddr *sa;
1676 struct sockaddr_in6 *sin6;
1683 len = sizeof(struct ifa_msghdr);
1688 len = sizeof(struct ifma_msghdr);
1692 len = sizeof(struct if_msghdr);
1695 case RTM_IFANNOUNCE:
1697 len = sizeof(struct if_announcemsghdr);
1701 len = sizeof(struct rt_msghdr);
1704 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1705 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1707 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1709 m = m_gethdr(M_NOWAIT, MT_DATA);
1713 m->m_pkthdr.len = m->m_len = len;
1714 rtm = mtod(m, struct rt_msghdr *);
1715 bzero((caddr_t)rtm, len);
1716 for (i = 0; i < RTAX_MAX; i++) {
1717 if ((sa = rtinfo->rti_info[i]) == NULL)
1719 rtinfo->rti_addrs |= (1 << i);
1722 KASSERT(dlen <= sizeof(ss),
1723 ("%s: sockaddr size overflow", __func__));
1724 bzero(&ss, sizeof(ss));
1725 bcopy(sa, &ss, sa->sa_len);
1726 sa = (struct sockaddr *)&ss;
1728 if (sa->sa_family == AF_INET6) {
1729 sin6 = (struct sockaddr_in6 *)sa;
1730 (void)sa6_recoverscope(sin6);
1733 m_copyback(m, len, dlen, (caddr_t)sa);
1736 if (m->m_pkthdr.len != len) {
1740 rtm->rtm_msglen = len;
1741 rtm->rtm_version = RTM_VERSION;
1742 rtm->rtm_type = type;
1747 * Writes information related to @rtinfo object to preallocated buffer.
1748 * Stores needed size in @plen. If @w is NULL, calculates size without
1750 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1752 * Returns 0 on success.
1756 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1758 struct sockaddr_storage ss;
1759 int len, buflen = 0, dlen, i;
1761 struct rt_msghdr *rtm = NULL;
1763 struct sockaddr_in6 *sin6;
1765 #ifdef COMPAT_FREEBSD32
1766 bool compat32 = false;
1772 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1773 #ifdef COMPAT_FREEBSD32
1774 if (w->w_req->flags & SCTL_MASK32) {
1775 len = sizeof(struct ifa_msghdrl32);
1779 len = sizeof(struct ifa_msghdrl);
1781 len = sizeof(struct ifa_msghdr);
1785 #ifdef COMPAT_FREEBSD32
1786 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1787 if (w->w_op == NET_RT_IFLISTL)
1788 len = sizeof(struct if_msghdrl32);
1790 len = sizeof(struct if_msghdr32);
1795 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1796 len = sizeof(struct if_msghdrl);
1798 len = sizeof(struct if_msghdr);
1802 len = sizeof(struct ifma_msghdr);
1806 len = sizeof(struct rt_msghdr);
1810 rtm = (struct rt_msghdr *)w->w_tmem;
1811 buflen = w->w_tmemsize - len;
1812 cp = (caddr_t)w->w_tmem + len;
1815 rtinfo->rti_addrs = 0;
1816 for (i = 0; i < RTAX_MAX; i++) {
1817 struct sockaddr *sa;
1819 if ((sa = rtinfo->rti_info[i]) == NULL)
1821 rtinfo->rti_addrs |= (1 << i);
1822 #ifdef COMPAT_FREEBSD32
1824 dlen = SA_SIZE32(sa);
1828 if (cp != NULL && buflen >= dlen) {
1829 KASSERT(dlen <= sizeof(ss),
1830 ("%s: sockaddr size overflow", __func__));
1831 bzero(&ss, sizeof(ss));
1832 bcopy(sa, &ss, sa->sa_len);
1833 sa = (struct sockaddr *)&ss;
1835 if (sa->sa_family == AF_INET6) {
1836 sin6 = (struct sockaddr_in6 *)sa;
1837 (void)sa6_recoverscope(sin6);
1840 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1843 } else if (cp != NULL) {
1845 * Buffer too small. Count needed size
1846 * and return with error.
1855 dlen = ALIGN(len) - len;
1867 /* fill header iff buffer is large enough */
1868 rtm->rtm_version = RTM_VERSION;
1869 rtm->rtm_type = type;
1870 rtm->rtm_msglen = len;
1875 if (w != NULL && cp == NULL)
1882 * This routine is called to generate a message from the routing
1883 * socket indicating that a redirect has occurred, a routing lookup
1884 * has failed, or that a protocol has detected timeouts to a particular
1888 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1891 struct rt_msghdr *rtm;
1893 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1895 if (V_route_cb.any_count == 0)
1897 m = rtsock_msg_mbuf(type, rtinfo);
1901 if (fibnum != RT_ALL_FIBS) {
1902 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1903 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1904 M_SETFIB(m, fibnum);
1905 m->m_flags |= RTS_FILTER_FIB;
1908 rtm = mtod(m, struct rt_msghdr *);
1909 rtm->rtm_flags = RTF_DONE | flags;
1910 rtm->rtm_errno = error;
1911 rtm->rtm_addrs = rtinfo->rti_addrs;
1912 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1916 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1919 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1923 * This routine is called to generate a message from the routing
1924 * socket indicating that the status of a network interface has changed.
1927 rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask __unused)
1929 struct if_msghdr *ifm;
1931 struct rt_addrinfo info;
1933 if (V_route_cb.any_count == 0)
1935 bzero((caddr_t)&info, sizeof(info));
1936 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1939 ifm = mtod(m, struct if_msghdr *);
1940 ifm->ifm_index = ifp->if_index;
1941 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1942 if_data_copy(ifp, &ifm->ifm_data);
1944 rt_dispatch(m, AF_UNSPEC);
1948 * Announce interface address arrival/withdraw.
1949 * Please do not call directly, use rt_addrmsg().
1950 * Assume input data to be valid.
1951 * Returns 0 on success.
1954 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1956 struct rt_addrinfo info;
1957 struct sockaddr *sa;
1960 struct ifa_msghdr *ifam;
1961 struct ifnet *ifp = ifa->ifa_ifp;
1962 struct sockaddr_storage ss;
1964 if (V_route_cb.any_count == 0)
1967 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1969 bzero((caddr_t)&info, sizeof(info));
1970 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1971 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1972 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1973 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1974 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1975 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1977 ifam = mtod(m, struct ifa_msghdr *);
1978 ifam->ifam_index = ifp->if_index;
1979 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1980 ifam->ifam_flags = ifa->ifa_flags;
1981 ifam->ifam_addrs = info.rti_addrs;
1983 if (fibnum != RT_ALL_FIBS) {
1984 M_SETFIB(m, fibnum);
1985 m->m_flags |= RTS_FILTER_FIB;
1988 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1994 * Announce route addition/removal to rtsock based on @rt data.
1995 * Callers are advives to use rt_routemsg() instead of using this
1996 * function directly.
1997 * Assume @rt data is consistent.
1999 * Returns 0 on success.
2002 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
2005 union sockaddr_union dst, mask;
2006 struct rt_addrinfo info;
2008 if (V_route_cb.any_count == 0)
2011 int family = rt_get_family(rt);
2012 init_sockaddrs_family(family, &dst.sa, &mask.sa);
2013 export_rtaddrs(rt, &dst.sa, &mask.sa);
2015 bzero((caddr_t)&info, sizeof(info));
2016 info.rti_info[RTAX_DST] = &dst.sa;
2017 info.rti_info[RTAX_NETMASK] = &mask.sa;
2018 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2019 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
2020 info.rti_ifp = nh->nh_ifp;
2022 return (rtsock_routemsg_info(cmd, &info, fibnum));
2026 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2028 struct rt_msghdr *rtm;
2029 struct sockaddr *sa;
2032 if (V_route_cb.any_count == 0)
2035 if (info->rti_flags & RTF_HOST)
2036 info->rti_info[RTAX_NETMASK] = NULL;
2038 m = rtsock_msg_mbuf(cmd, info);
2042 if (fibnum != RT_ALL_FIBS) {
2043 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
2044 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
2045 M_SETFIB(m, fibnum);
2046 m->m_flags |= RTS_FILTER_FIB;
2049 rtm = mtod(m, struct rt_msghdr *);
2050 rtm->rtm_addrs = info->rti_addrs;
2051 if (info->rti_ifp != NULL)
2052 rtm->rtm_index = info->rti_ifp->if_index;
2053 /* Add RTF_DONE to indicate command 'completion' required by API */
2054 info->rti_flags |= RTF_DONE;
2055 /* Reported routes has to be up */
2056 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
2057 info->rti_flags |= RTF_UP;
2058 rtm->rtm_flags = info->rti_flags;
2060 sa = info->rti_info[RTAX_DST];
2061 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2067 * This is the analogue to the rt_newaddrmsg which performs the same
2068 * function but for multicast group memberhips. This is easier since
2069 * there is no route state to worry about.
2072 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
2074 struct rt_addrinfo info;
2075 struct mbuf *m = NULL;
2076 struct ifnet *ifp = ifma->ifma_ifp;
2077 struct ifma_msghdr *ifmam;
2079 if (V_route_cb.any_count == 0)
2082 bzero((caddr_t)&info, sizeof(info));
2083 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2084 if (ifp && ifp->if_addr)
2085 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
2087 info.rti_info[RTAX_IFP] = NULL;
2089 * If a link-layer address is present, present it as a ``gateway''
2090 * (similarly to how ARP entries, e.g., are presented).
2092 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
2093 m = rtsock_msg_mbuf(cmd, &info);
2096 ifmam = mtod(m, struct ifma_msghdr *);
2097 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
2099 ifmam->ifmam_index = ifp->if_index;
2100 ifmam->ifmam_addrs = info.rti_addrs;
2101 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
2104 static struct mbuf *
2105 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
2106 struct rt_addrinfo *info)
2108 struct if_announcemsghdr *ifan;
2111 if (V_route_cb.any_count == 0)
2113 bzero((caddr_t)info, sizeof(*info));
2114 m = rtsock_msg_mbuf(type, info);
2116 ifan = mtod(m, struct if_announcemsghdr *);
2117 ifan->ifan_index = ifp->if_index;
2118 strlcpy(ifan->ifan_name, ifp->if_xname,
2119 sizeof(ifan->ifan_name));
2120 ifan->ifan_what = what;
2126 * This is called to generate routing socket messages indicating
2127 * IEEE80211 wireless events.
2128 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2131 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2134 struct rt_addrinfo info;
2136 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2139 * Append the ieee80211 data. Try to stick it in the
2140 * mbuf containing the ifannounce msg; otherwise allocate
2141 * a new mbuf and append.
2143 * NB: we assume m is a single mbuf.
2145 if (data_len > M_TRAILINGSPACE(m)) {
2146 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2151 bcopy(data, mtod(n, void *), data_len);
2152 n->m_len = data_len;
2154 } else if (data_len > 0) {
2155 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2156 m->m_len += data_len;
2158 if (m->m_flags & M_PKTHDR)
2159 m->m_pkthdr.len += data_len;
2160 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2161 rt_dispatch(m, AF_UNSPEC);
2166 * This is called to generate routing socket messages indicating
2167 * network interface arrival and departure.
2170 rt_ifannouncemsg(struct ifnet *ifp, int what)
2173 struct rt_addrinfo info;
2175 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2177 rt_dispatch(m, AF_UNSPEC);
2181 rt_dispatch(struct mbuf *m, sa_family_t saf)
2186 m->m_rtsock_family = saf;
2188 m->m_pkthdr.rcvif = V_loif;
2193 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
2197 * This is used in dumping the kernel table via sysctl().
2200 sysctl_dumpentry(struct rtentry *rt, void *vw)
2202 struct walkarg *w = vw;
2203 struct nhop_object *nh;
2207 if (!rt_is_exportable(rt, w->w_req->td->td_ucred))
2210 export_rtaddrs(rt, w->dst, w->mask);
2211 nh = rt_get_raw_nhop(rt);
2213 if (NH_IS_NHGRP(nh)) {
2214 const struct weightened_nhop *wn;
2217 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2218 for (int i = 0; i < num_nhops; i++) {
2219 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2225 sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2232 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2235 struct rt_addrinfo info;
2236 int error = 0, size;
2239 rtflags = nhop_get_rtflags(nh);
2241 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2244 bzero((caddr_t)&info, sizeof(info));
2245 info.rti_info[RTAX_DST] = w->dst;
2246 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2247 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2248 info.rti_info[RTAX_GENMASK] = 0;
2249 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2250 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2251 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2252 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2253 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2255 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2257 if (w->w_req && w->w_tmem) {
2258 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2260 bzero(&rtm->rtm_index,
2261 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2264 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2265 * and RTF_UP (if entry is linked, which is always true here).
2266 * Given that, use nhop rtflags & add RTF_UP.
2268 rtm->rtm_flags = rtflags | RTF_UP;
2269 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2270 rtm->rtm_flags = RTF_GATEWAY |
2271 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2272 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2273 rtm->rtm_rmx.rmx_weight = weight;
2274 rtm->rtm_index = nh->nh_ifp->if_index;
2275 rtm->rtm_addrs = info.rti_addrs;
2276 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2283 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2284 struct rt_addrinfo *info, struct walkarg *w, int len)
2286 struct if_msghdrl *ifm;
2287 struct if_data *ifd;
2289 ifm = (struct if_msghdrl *)w->w_tmem;
2291 #ifdef COMPAT_FREEBSD32
2292 if (w->w_req->flags & SCTL_MASK32) {
2293 struct if_msghdrl32 *ifm32;
2295 ifm32 = (struct if_msghdrl32 *)ifm;
2296 ifm32->ifm_addrs = info->rti_addrs;
2297 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2298 ifm32->ifm_index = ifp->if_index;
2299 ifm32->_ifm_spare1 = 0;
2300 ifm32->ifm_len = sizeof(*ifm32);
2301 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2302 ifm32->_ifm_spare2 = 0;
2303 ifd = &ifm32->ifm_data;
2307 ifm->ifm_addrs = info->rti_addrs;
2308 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2309 ifm->ifm_index = ifp->if_index;
2310 ifm->_ifm_spare1 = 0;
2311 ifm->ifm_len = sizeof(*ifm);
2312 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2313 ifm->_ifm_spare2 = 0;
2314 ifd = &ifm->ifm_data;
2317 memcpy(ifd, src_ifd, sizeof(*ifd));
2319 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2323 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2324 struct rt_addrinfo *info, struct walkarg *w, int len)
2326 struct if_msghdr *ifm;
2327 struct if_data *ifd;
2329 ifm = (struct if_msghdr *)w->w_tmem;
2331 #ifdef COMPAT_FREEBSD32
2332 if (w->w_req->flags & SCTL_MASK32) {
2333 struct if_msghdr32 *ifm32;
2335 ifm32 = (struct if_msghdr32 *)ifm;
2336 ifm32->ifm_addrs = info->rti_addrs;
2337 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2338 ifm32->ifm_index = ifp->if_index;
2339 ifm32->_ifm_spare1 = 0;
2340 ifd = &ifm32->ifm_data;
2344 ifm->ifm_addrs = info->rti_addrs;
2345 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2346 ifm->ifm_index = ifp->if_index;
2347 ifm->_ifm_spare1 = 0;
2348 ifd = &ifm->ifm_data;
2351 memcpy(ifd, src_ifd, sizeof(*ifd));
2353 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2357 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2358 struct walkarg *w, int len)
2360 struct ifa_msghdrl *ifam;
2361 struct if_data *ifd;
2363 ifam = (struct ifa_msghdrl *)w->w_tmem;
2365 #ifdef COMPAT_FREEBSD32
2366 if (w->w_req->flags & SCTL_MASK32) {
2367 struct ifa_msghdrl32 *ifam32;
2369 ifam32 = (struct ifa_msghdrl32 *)ifam;
2370 ifam32->ifam_addrs = info->rti_addrs;
2371 ifam32->ifam_flags = ifa->ifa_flags;
2372 ifam32->ifam_index = ifa->ifa_ifp->if_index;
2373 ifam32->_ifam_spare1 = 0;
2374 ifam32->ifam_len = sizeof(*ifam32);
2375 ifam32->ifam_data_off =
2376 offsetof(struct ifa_msghdrl32, ifam_data);
2377 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2378 ifd = &ifam32->ifam_data;
2382 ifam->ifam_addrs = info->rti_addrs;
2383 ifam->ifam_flags = ifa->ifa_flags;
2384 ifam->ifam_index = ifa->ifa_ifp->if_index;
2385 ifam->_ifam_spare1 = 0;
2386 ifam->ifam_len = sizeof(*ifam);
2387 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2388 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2389 ifd = &ifam->ifam_data;
2392 bzero(ifd, sizeof(*ifd));
2393 ifd->ifi_datalen = sizeof(struct if_data);
2394 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2395 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2396 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2397 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2399 /* Fixup if_data carp(4) vhid. */
2400 if (carp_get_vhid_p != NULL)
2401 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2403 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2407 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2408 struct walkarg *w, int len)
2410 struct ifa_msghdr *ifam;
2412 ifam = (struct ifa_msghdr *)w->w_tmem;
2413 ifam->ifam_addrs = info->rti_addrs;
2414 ifam->ifam_flags = ifa->ifa_flags;
2415 ifam->ifam_index = ifa->ifa_ifp->if_index;
2416 ifam->_ifam_spare1 = 0;
2417 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2419 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2423 sysctl_iflist(int af, struct walkarg *w)
2428 struct rt_addrinfo info;
2430 struct sockaddr_storage ss;
2432 bzero((caddr_t)&info, sizeof(info));
2433 bzero(&ifd, sizeof(ifd));
2434 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2435 if (w->w_arg && w->w_arg != ifp->if_index)
2437 if_data_copy(ifp, &ifd);
2439 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2440 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2443 info.rti_info[RTAX_IFP] = NULL;
2444 if (w->w_req && w->w_tmem) {
2445 if (w->w_op == NET_RT_IFLISTL)
2446 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2449 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2454 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2455 if (af && af != ifa->ifa_addr->sa_family)
2457 if (prison_if(w->w_req->td->td_ucred,
2458 ifa->ifa_addr) != 0)
2460 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2461 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2462 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2463 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2464 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2467 if (w->w_req && w->w_tmem) {
2468 if (w->w_op == NET_RT_IFLISTL)
2469 error = sysctl_iflist_ifaml(ifa, &info,
2472 error = sysctl_iflist_ifam(ifa, &info,
2478 info.rti_info[RTAX_IFA] = NULL;
2479 info.rti_info[RTAX_NETMASK] = NULL;
2480 info.rti_info[RTAX_BRD] = NULL;
2487 sysctl_ifmalist(int af, struct walkarg *w)
2489 struct rt_addrinfo info;
2491 struct ifmultiaddr *ifma;
2498 bzero((caddr_t)&info, sizeof(info));
2500 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2501 if (w->w_arg && w->w_arg != ifp->if_index)
2504 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2505 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2506 if (af && af != ifma->ifma_addr->sa_family)
2508 if (prison_if(w->w_req->td->td_ucred,
2509 ifma->ifma_addr) != 0)
2511 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2512 info.rti_info[RTAX_GATEWAY] =
2513 (ifma->ifma_addr->sa_family != AF_LINK) ?
2514 ifma->ifma_lladdr : NULL;
2515 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2518 if (w->w_req && w->w_tmem) {
2519 struct ifma_msghdr *ifmam;
2521 ifmam = (struct ifma_msghdr *)w->w_tmem;
2522 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2523 ifmam->ifmam_flags = 0;
2524 ifmam->ifmam_addrs = info.rti_addrs;
2525 ifmam->_ifmam_spare1 = 0;
2526 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2538 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2540 union sockaddr_union sa_dst, sa_mask;
2543 w->dst = (struct sockaddr *)&sa_dst;
2544 w->mask = (struct sockaddr *)&sa_mask;
2546 init_sockaddrs_family(family, w->dst, w->mask);
2548 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2552 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2554 struct epoch_tracker et;
2555 int *name = (int *)arg1;
2556 u_int namelen = arg2;
2557 struct rib_head *rnh = NULL; /* silence compiler. */
2558 int i, lim, error = EINVAL;
2570 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2572 fib = req->td->td_proc->p_fibnum;
2573 else if (namelen == 4)
2574 fib = (name[3] == RT_ALL_FIBS) ?
2575 req->td->td_proc->p_fibnum : name[3];
2577 return ((namelen < 3) ? EISDIR : ENOTDIR);
2578 if (fib < 0 || fib >= rt_numfibs)
2580 } else if (namelen != 3)
2581 return ((namelen < 3) ? EISDIR : ENOTDIR);
2585 bzero(&w, sizeof(w));
2590 error = sysctl_wire_old_buffer(req, 0);
2595 * Allocate reply buffer in advance.
2596 * All rtsock messages has maximum length of u_short.
2598 w.w_tmemsize = 65536;
2599 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2601 NET_EPOCH_ENTER(et);
2605 if (af == 0) { /* dump all tables */
2608 } else /* dump only one table */
2612 * take care of llinfo entries, the caller must
2615 if (w.w_op == NET_RT_FLAGS &&
2616 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2618 error = lltable_sysctl_dumparp(af, w.w_req);
2624 * take care of routing entries
2626 for (error = 0; error == 0 && i <= lim; i++) {
2627 rnh = rt_tables_get_rnh(fib, i);
2629 rtable_sysctl_dump(fib, i, &w);
2631 error = EAFNOSUPPORT;
2636 /* Allow dumping one specific af/fib at a time */
2642 if (fib < 0 || fib > rt_numfibs) {
2646 rnh = rt_tables_get_rnh(fib, af);
2648 error = EAFNOSUPPORT;
2651 if (w.w_op == NET_RT_NHOP)
2652 error = nhops_dump_sysctl(rnh, w.w_req);
2655 error = nhgrp_dump_sysctl(rnh, w.w_req);
2661 case NET_RT_IFLISTL:
2662 error = sysctl_iflist(af, &w);
2665 case NET_RT_IFMALIST:
2666 error = sysctl_ifmalist(af, &w);
2671 free(w.w_tmem, M_TEMP);
2675 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2676 sysctl_rtsock, "Return route tables and interface/address lists");
2679 * Definitions of protocols supported in the ROUTE domain.
2682 static struct domain routedomain; /* or at least forward */
2684 static struct protosw routesw = {
2685 .pr_type = SOCK_RAW,
2686 .pr_flags = PR_ATOMIC|PR_ADDR,
2687 .pr_abort = rts_close,
2688 .pr_attach = rts_attach,
2689 .pr_detach = rts_detach,
2690 .pr_send = rts_send,
2691 .pr_shutdown = rts_shutdown,
2692 .pr_disconnect = rts_disconnect,
2693 .pr_close = rts_close,
2696 static struct domain routedomain = {
2697 .dom_family = PF_ROUTE,
2698 .dom_name = "route",
2700 .dom_protosw = { &routesw },