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/domain.h>
44 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/rmlock.h>
50 #include <sys/rwlock.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 #include <net/raw_cb.h>
64 #include <net/route.h>
65 #include <net/route/route_ctl.h>
66 #include <net/route/route_var.h>
68 #include <net/radix_mpath.h>
72 #include <netinet/in.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/ip_carp.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/scope6_var.h>
79 #include <net/route/nhop.h>
81 #ifdef COMPAT_FREEBSD32
82 #include <sys/mount.h>
83 #include <compat/freebsd32/freebsd32.h>
93 struct if_data ifm_data;
103 uint16_t _ifm_spare1;
105 uint16_t ifm_data_off;
106 uint32_t _ifm_spare2;
107 struct if_data ifm_data;
110 struct ifa_msghdrl32 {
111 uint16_t ifam_msglen;
112 uint8_t ifam_version;
117 uint16_t _ifam_spare1;
119 uint16_t ifam_data_off;
121 struct if_data ifam_data;
124 #define SA_SIZE32(sa) \
125 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
127 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
129 #endif /* COMPAT_FREEBSD32 */
131 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
133 /* NB: these are not modified */
134 static struct sockaddr route_src = { 2, PF_ROUTE, };
135 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
137 /* These are external hooks for CARP. */
138 int (*carp_get_vhid_p)(struct ifaddr *);
141 * Used by rtsock/raw_input callback code to decide whether to filter the update
142 * notification to a socket bound to a particular FIB.
144 #define RTS_FILTER_FIB M_PROTO8
147 int ip_count; /* attached w/ AF_INET */
148 int ip6_count; /* attached w/ AF_INET6 */
149 int any_count; /* total attached */
151 VNET_DEFINE_STATIC(route_cb_t, route_cb);
152 #define V_route_cb VNET(route_cb)
154 struct mtx rtsock_mtx;
155 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
157 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
158 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
159 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
161 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
167 struct sysctl_req *w_req;
170 static void rts_input(struct mbuf *m);
171 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
172 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
173 struct walkarg *w, int *plen);
174 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
175 struct rt_addrinfo *rtinfo);
176 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
177 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
178 uint32_t weight, struct walkarg *w);
179 static int sysctl_iflist(int af, struct walkarg *w);
180 static int sysctl_ifmalist(int af, struct walkarg *w);
181 static int route_output(struct mbuf *m, struct socket *so, ...);
182 static void rt_getmetrics(const struct rtentry *rt,
183 const struct nhop_object *nh, struct rt_metrics *out);
184 static void rt_dispatch(struct mbuf *, sa_family_t);
185 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
186 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
187 static int update_rtm_from_rc(struct rt_addrinfo *info,
188 struct rt_msghdr **prtm, int alloc_len,
189 struct rib_cmd_info *rc, struct nhop_object *nh);
190 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
191 struct mbuf *m, sa_family_t saf, u_int fibnum,
193 static int can_export_rte(struct ucred *td_ucred, const struct rtentry *rt);
195 static struct netisr_handler rtsock_nh = {
197 .nh_handler = rts_input,
198 .nh_proto = NETISR_ROUTE,
199 .nh_policy = NETISR_POLICY_SOURCE,
203 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
207 netisr_getqlimit(&rtsock_nh, &qlimit);
208 error = sysctl_handle_int(oidp, &qlimit, 0, req);
209 if (error || !req->newptr)
213 return (netisr_setqlimit(&rtsock_nh, qlimit));
215 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
216 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
217 0, 0, sysctl_route_netisr_maxqlen, "I",
218 "maximum routing socket dispatch queue length");
225 if (IS_DEFAULT_VNET(curvnet)) {
226 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
227 rtsock_nh.nh_qlimit = tmp;
228 netisr_register(&rtsock_nh);
232 netisr_register_vnet(&rtsock_nh);
235 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
240 vnet_rts_uninit(void)
243 netisr_unregister_vnet(&rtsock_nh);
245 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
250 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
255 KASSERT(m != NULL, ("%s: m is NULL", __func__));
256 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
257 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
259 /* No filtering requested. */
260 if ((m->m_flags & RTS_FILTER_FIB) == 0)
263 /* Check if it is a rts and the fib matches the one of the socket. */
264 fibnum = M_GETFIB(m);
265 if (proto->sp_family != PF_ROUTE ||
266 rp->rcb_socket == NULL ||
267 rp->rcb_socket->so_fibnum == fibnum)
270 /* Filtering requested and no match, the socket shall be skipped. */
275 rts_input(struct mbuf *m)
277 struct sockproto route_proto;
278 unsigned short *family;
281 route_proto.sp_family = PF_ROUTE;
282 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
284 family = (unsigned short *)(tag + 1);
285 route_proto.sp_protocol = *family;
286 m_tag_delete(m, tag);
288 route_proto.sp_protocol = 0;
290 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
294 * It really doesn't make any sense at all for this code to share much
295 * with raw_usrreq.c, since its functionality is so restricted. XXX
298 rts_abort(struct socket *so)
301 raw_usrreqs.pru_abort(so);
305 rts_close(struct socket *so)
308 raw_usrreqs.pru_close(so);
311 /* pru_accept is EOPNOTSUPP */
314 rts_attach(struct socket *so, int proto, struct thread *td)
319 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
322 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
324 so->so_pcb = (caddr_t)rp;
325 so->so_fibnum = td->td_proc->p_fibnum;
326 error = raw_attach(so, proto);
334 switch(rp->rcb_proto.sp_protocol) {
336 V_route_cb.ip_count++;
339 V_route_cb.ip6_count++;
342 V_route_cb.any_count++;
345 so->so_options |= SO_USELOOPBACK;
350 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
353 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
357 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
360 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
363 /* pru_connect2 is EOPNOTSUPP */
364 /* pru_control is EOPNOTSUPP */
367 rts_detach(struct socket *so)
369 struct rawcb *rp = sotorawcb(so);
371 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
374 switch(rp->rcb_proto.sp_protocol) {
376 V_route_cb.ip_count--;
379 V_route_cb.ip6_count--;
382 V_route_cb.any_count--;
384 raw_usrreqs.pru_detach(so);
388 rts_disconnect(struct socket *so)
391 return (raw_usrreqs.pru_disconnect(so));
394 /* pru_listen is EOPNOTSUPP */
397 rts_peeraddr(struct socket *so, struct sockaddr **nam)
400 return (raw_usrreqs.pru_peeraddr(so, nam));
403 /* pru_rcvd is EOPNOTSUPP */
404 /* pru_rcvoob is EOPNOTSUPP */
407 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
408 struct mbuf *control, struct thread *td)
411 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
414 /* pru_sense is null */
417 rts_shutdown(struct socket *so)
420 return (raw_usrreqs.pru_shutdown(so));
424 rts_sockaddr(struct socket *so, struct sockaddr **nam)
427 return (raw_usrreqs.pru_sockaddr(so, nam));
430 static struct pr_usrreqs route_usrreqs = {
431 .pru_abort = rts_abort,
432 .pru_attach = rts_attach,
433 .pru_bind = rts_bind,
434 .pru_connect = rts_connect,
435 .pru_detach = rts_detach,
436 .pru_disconnect = rts_disconnect,
437 .pru_peeraddr = rts_peeraddr,
438 .pru_send = rts_send,
439 .pru_shutdown = rts_shutdown,
440 .pru_sockaddr = rts_sockaddr,
441 .pru_close = rts_close,
444 #ifndef _SOCKADDR_UNION_DEFINED
445 #define _SOCKADDR_UNION_DEFINED
447 * The union of all possible address formats we handle.
449 union sockaddr_union {
451 struct sockaddr_in sin;
452 struct sockaddr_in6 sin6;
454 #endif /* _SOCKADDR_UNION_DEFINED */
457 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
458 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
460 #if defined(INET) || defined(INET6)
461 struct epoch_tracker et;
464 /* First, see if the returned address is part of the jail. */
465 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
466 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
470 switch (info->rti_info[RTAX_DST]->sa_family) {
480 * Try to find an address on the given outgoing interface
481 * that belongs to the jail.
484 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
487 if (sa->sa_family != AF_INET)
489 ia = ((struct sockaddr_in *)sa)->sin_addr;
490 if (prison_check_ip4(cred, &ia) == 0) {
498 * As a last resort return the 'default' jail address.
500 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
502 if (prison_get_ip4(cred, &ia) != 0)
505 bzero(&saun->sin, sizeof(struct sockaddr_in));
506 saun->sin.sin_len = sizeof(struct sockaddr_in);
507 saun->sin.sin_family = AF_INET;
508 saun->sin.sin_addr.s_addr = ia.s_addr;
509 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
522 * Try to find an address on the given outgoing interface
523 * that belongs to the jail.
526 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
529 if (sa->sa_family != AF_INET6)
531 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
532 &ia6, sizeof(struct in6_addr));
533 if (prison_check_ip6(cred, &ia6) == 0) {
541 * As a last resort return the 'default' jail address.
543 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
545 if (prison_get_ip6(cred, &ia6) != 0)
548 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
549 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
550 saun->sin6.sin6_family = AF_INET6;
551 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
552 if (sa6_recoverscope(&saun->sin6) != 0)
554 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
565 * Fills in @info based on userland-provided @rtm message.
567 * Returns 0 on success.
570 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info)
575 rtm->rtm_pid = curproc->p_pid;
576 info->rti_addrs = rtm->rtm_addrs;
578 info->rti_mflags = rtm->rtm_inits;
579 info->rti_rmx = &rtm->rtm_rmx;
582 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
583 * link-local address because rtrequest requires addresses with
586 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
589 if (rtm->rtm_flags & RTF_RNH_LOCKED)
591 info->rti_flags = rtm->rtm_flags;
592 if (info->rti_info[RTAX_DST] == NULL ||
593 info->rti_info[RTAX_DST]->sa_family >= AF_MAX ||
594 (info->rti_info[RTAX_GATEWAY] != NULL &&
595 info->rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
597 saf = info->rti_info[RTAX_DST]->sa_family;
599 * Verify that the caller has the appropriate privilege; RTM_GET
600 * is the only operation the non-superuser is allowed.
602 if (rtm->rtm_type != RTM_GET) {
603 error = priv_check(curthread, PRIV_NET_ROUTE);
609 * The given gateway address may be an interface address.
610 * For example, issuing a "route change" command on a route
611 * entry that was created from a tunnel, and the gateway
612 * address given is the local end point. In this case the
613 * RTF_GATEWAY flag must be cleared or the destination will
614 * not be reachable even though there is no error message.
616 if (info->rti_info[RTAX_GATEWAY] != NULL &&
617 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
618 struct rt_addrinfo ginfo;
619 struct sockaddr *gdst;
620 struct sockaddr_storage ss;
622 bzero(&ginfo, sizeof(ginfo));
623 bzero(&ss, sizeof(ss));
624 ss.ss_len = sizeof(ss);
626 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
627 gdst = info->rti_info[RTAX_GATEWAY];
630 * A host route through the loopback interface is
631 * installed for each interface adddress. In pre 8.0
632 * releases the interface address of a PPP link type
633 * is not reachable locally. This behavior is fixed as
634 * part of the new L2/L3 redesign and rewrite work. The
635 * signature of this interface address route is the
636 * AF_LINK sa_family type of the gateway, and the
637 * rt_ifp has the IFF_LOOPBACK flag set.
639 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
640 if (ss.ss_family == AF_LINK &&
641 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
642 info->rti_flags &= ~RTF_GATEWAY;
643 info->rti_flags |= RTF_GWFLAG_COMPAT;
645 rib_free_info(&ginfo);
652 static struct nhop_object *
653 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
655 if (!NH_IS_NHGRP(nh))
658 struct weightened_nhop *wn;
660 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
663 for (int i = 0; i < num_nhops; i++) {
664 if (match_nhop_gw(wn[i].nh, gw))
672 * Handles RTM_GET message from routing socket, returning matching rt.
675 * 0 on success, with locked and referenced matching rt in @rt_nrt
679 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
680 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
683 struct rib_head *rnh;
684 struct nhop_object *nh;
687 saf = info->rti_info[RTAX_DST]->sa_family;
689 rnh = rt_tables_get_rnh(fibnum, saf);
691 return (EAFNOSUPPORT);
695 if (info->rti_info[RTAX_NETMASK] == NULL) {
697 * Provide longest prefix match for
698 * address lookup (no mask).
699 * 'route -n get addr'
701 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
702 info->rti_info[RTAX_DST], &rnh->head);
704 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
705 info->rti_info[RTAX_DST],
706 info->rti_info[RTAX_NETMASK], &rnh->head);
708 if (rc->rc_rt == NULL) {
713 nh = select_nhop(rc->rc_rt->rt_nhop, info->rti_info[RTAX_GATEWAY]);
719 * If performing proxied L2 entry insertion, and
720 * the actual PPP host entry is found, perform
721 * another search to retrieve the prefix route of
722 * the local end point of the PPP link.
723 * TODO: move this logic to userland.
725 if (rtm->rtm_flags & RTF_ANNOUNCE) {
726 struct sockaddr laddr;
727 struct nhop_object *nh;
729 nh = rc->rc_rt->rt_nhop;
730 if (nh->nh_ifp != NULL &&
731 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
734 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
737 rt_maskedcopy(ifa->ifa_addr,
741 rt_maskedcopy(nh->nh_ifa->ifa_addr,
743 nh->nh_ifa->ifa_netmask);
745 * refactor rt and no lock operation necessary
747 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
749 if (rc->rc_rt == NULL) {
753 nh = select_nhop(rc->rc_rt->rt_nhop, info->rti_info[RTAX_GATEWAY]);
760 rc->rc_nh_weight = rc->rc_rt->rt_weight;
767 * Update sockaddrs, flags, etc in @prtm based on @rc data.
768 * rtm can be reallocated.
770 * Returns 0 on success, along with pointer to (potentially reallocated)
775 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
776 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
778 struct sockaddr_storage netmask_ss;
780 union sockaddr_union saun;
781 struct rt_msghdr *rtm, *orig_rtm = NULL;
787 info->rti_info[RTAX_DST] = rt_key(rc->rc_rt);
788 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
789 info->rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rc->rc_rt),
790 rt_mask(rc->rc_rt), &netmask_ss);
791 info->rti_info[RTAX_GENMASK] = 0;
793 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
795 info->rti_info[RTAX_IFP] =
796 ifp->if_addr->ifa_addr;
797 error = rtm_get_jailed(info, ifp, nh,
798 &saun, curthread->td_ucred);
801 if (ifp->if_flags & IFF_POINTOPOINT)
802 info->rti_info[RTAX_BRD] =
803 nh->nh_ifa->ifa_dstaddr;
804 rtm->rtm_index = ifp->if_index;
806 info->rti_info[RTAX_IFP] = NULL;
807 info->rti_info[RTAX_IFA] = NULL;
809 } else if (ifp != NULL)
810 rtm->rtm_index = ifp->if_index;
812 /* Check if we need to realloc storage */
813 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
814 if (len > alloc_len) {
815 struct rt_msghdr *tmp_rtm;
817 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
820 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
826 * Delay freeing original rtm as info contains
827 * data referencing it.
831 w.w_tmem = (caddr_t)rtm;
832 w.w_tmemsize = alloc_len;
833 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
835 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
836 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
837 rtm->rtm_flags = RTF_GATEWAY |
838 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
839 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
840 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
841 rtm->rtm_addrs = info->rti_addrs;
843 if (orig_rtm != NULL)
844 free(orig_rtm, M_TEMP);
852 save_del_notification(struct rib_cmd_info *rc, void *_cbdata)
854 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
856 if (rc->rc_cmd == RTM_DELETE)
861 save_add_notification(struct rib_cmd_info *rc, void *_cbdata)
863 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
865 if (rc->rc_cmd == RTM_ADD)
872 route_output(struct mbuf *m, struct socket *so, ...)
874 struct rt_msghdr *rtm = NULL;
875 struct rtentry *rt = NULL;
876 struct rt_addrinfo info;
877 struct epoch_tracker et;
879 struct sockaddr_storage ss;
880 struct sockaddr_in6 *sin6;
881 int i, rti_need_deembed = 0;
883 int alloc_len = 0, len, error = 0, fibnum;
884 sa_family_t saf = AF_UNSPEC;
886 struct rib_cmd_info rc;
887 struct nhop_object *nh;
889 fibnum = so->so_fibnum;
890 #define senderr(e) { error = e; goto flush;}
891 if (m == NULL || ((m->m_len < sizeof(long)) &&
892 (m = m_pullup(m, sizeof(long))) == NULL))
894 if ((m->m_flags & M_PKTHDR) == 0)
895 panic("route_output");
897 len = m->m_pkthdr.len;
898 if (len < sizeof(*rtm) ||
899 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
903 * Most of current messages are in range 200-240 bytes,
904 * minimize possible re-allocation on reply using larger size
905 * buffer aligned on 1k boundaty.
907 alloc_len = roundup2(len, 1024);
908 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
911 m_copydata(m, 0, len, (caddr_t)rtm);
912 bzero(&info, sizeof(info));
913 bzero(&w, sizeof(w));
916 if (rtm->rtm_version != RTM_VERSION) {
917 /* Do not touch message since format is unknown */
920 senderr(EPROTONOSUPPORT);
924 * Starting from here, it is possible
925 * to alter original message and insert
926 * caller PID and error value.
929 if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) {
933 saf = info.rti_info[RTAX_DST]->sa_family;
935 /* support for new ARP code */
936 if (rtm->rtm_flags & RTF_LLDATA) {
937 error = lla_rt_output(rtm, &info);
940 rti_need_deembed = 1;
945 switch (rtm->rtm_type) {
948 if (rtm->rtm_type == RTM_ADD) {
949 if (info.rti_info[RTAX_GATEWAY] == NULL)
952 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
955 rti_need_deembed = 1;
958 if (NH_IS_NHGRP(rc.rc_nh_new) ||
959 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
960 struct rib_cmd_info rc_simple = {};
961 rib_decompose_notification(&rc,
962 save_add_notification, (void *)&rc_simple);
967 rtm->rtm_index = nh->nh_ifp->if_index;
972 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
975 if (NH_IS_NHGRP(rc.rc_nh_old) ||
976 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
977 struct rib_cmd_info rc_simple = {};
978 rib_decompose_notification(&rc,
979 save_del_notification, (void *)&rc_simple);
987 /* rt_msg2() will not be used when RTM_DELETE fails. */
988 rti_need_deembed = 1;
993 error = handle_rtm_get(&info, fibnum, rtm, &rc);
999 if (!can_export_rte(curthread->td_ucred, rc.rc_rt)) {
1003 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1005 * Note that some sockaddr pointers may have changed to
1006 * point to memory outsize @rtm. Some may be pointing
1007 * to the on-stack variables.
1008 * Given that, any pointer in @info CANNOT BE USED.
1012 * scopeid deembedding has been performed while
1013 * writing updated rtm in rtsock_msg_buffer().
1014 * With that in mind, skip deembedding procedure below.
1017 rti_need_deembed = 0;
1024 senderr(EOPNOTSUPP);
1033 if (rti_need_deembed) {
1034 /* sin6_scope_id is recovered before sending rtm. */
1035 sin6 = (struct sockaddr_in6 *)&ss;
1036 for (i = 0; i < RTAX_MAX; i++) {
1037 if (info.rti_info[i] == NULL)
1039 if (info.rti_info[i]->sa_family != AF_INET6)
1041 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1042 if (sa6_recoverscope(sin6) == 0)
1043 bcopy(sin6, info.rti_info[i],
1049 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1055 * Sends the prepared reply message in @rtm to all rtsock clients.
1056 * Frees @m and @rtm.
1060 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1061 sa_family_t saf, u_int fibnum, int rtm_errno)
1063 struct rawcb *rp = NULL;
1066 * Check to see if we don't want our own messages.
1068 if ((so->so_options & SO_USELOOPBACK) == 0) {
1069 if (V_route_cb.any_count <= 1) {
1075 /* There is another listener, so construct message */
1081 rtm->rtm_errno = rtm_errno;
1083 rtm->rtm_flags |= RTF_DONE;
1085 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1086 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1089 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1090 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1095 M_SETFIB(m, fibnum);
1096 m->m_flags |= RTS_FILTER_FIB;
1099 * XXX insure we don't get a copy by
1100 * invalidating our protocol
1102 unsigned short family = rp->rcb_proto.sp_family;
1103 rp->rcb_proto.sp_family = 0;
1104 rt_dispatch(m, saf);
1105 rp->rcb_proto.sp_family = family;
1107 rt_dispatch(m, saf);
1112 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1113 struct rt_metrics *out)
1116 bzero(out, sizeof(*out));
1117 out->rmx_mtu = nh->nh_mtu;
1118 out->rmx_weight = rt->rt_weight;
1119 out->rmx_nhidx = nhop_get_idx(nh);
1120 /* Kernel -> userland timebase conversion. */
1121 out->rmx_expire = rt->rt_expire ?
1122 rt->rt_expire - time_uptime + time_second : 0;
1126 * Extract the addresses of the passed sockaddrs.
1127 * Do a little sanity checking so as to avoid bad memory references.
1128 * This data is derived straight from userland.
1131 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1133 struct sockaddr *sa;
1136 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1137 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1139 sa = (struct sockaddr *)cp;
1143 if (cp + sa->sa_len > cplim)
1146 * there are no more.. quit now
1147 * If there are more bits, they are in error.
1148 * I've seen this. route(1) can evidently generate these.
1149 * This causes kernel to core dump.
1150 * for compatibility, If we see this, point to a safe address.
1152 if (sa->sa_len == 0) {
1153 rtinfo->rti_info[i] = &sa_zero;
1154 return (0); /* should be EINVAL but for compat */
1158 if (sa->sa_family == AF_INET6)
1159 sa6_embedscope((struct sockaddr_in6 *)sa,
1162 rtinfo->rti_info[i] = sa;
1169 * Fill in @dmask with valid netmask leaving original @smask
1170 * intact. Mostly used with radix netmasks.
1173 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1174 struct sockaddr_storage *dmask)
1176 if (dst == NULL || smask == NULL)
1179 memset(dmask, 0, dst->sa_len);
1180 memcpy(dmask, smask, smask->sa_len);
1181 dmask->ss_len = dst->sa_len;
1182 dmask->ss_family = dst->sa_family;
1184 return ((struct sockaddr *)dmask);
1188 * Writes information related to @rtinfo object to newly-allocated mbuf.
1189 * Assumes MCLBYTES is enough to construct any message.
1190 * Used for OS notifications of vaious events (if/ifa announces,etc)
1192 * Returns allocated mbuf or NULL on failure.
1194 static struct mbuf *
1195 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1197 struct rt_msghdr *rtm;
1200 struct sockaddr *sa;
1202 struct sockaddr_storage ss;
1203 struct sockaddr_in6 *sin6;
1210 len = sizeof(struct ifa_msghdr);
1215 len = sizeof(struct ifma_msghdr);
1219 len = sizeof(struct if_msghdr);
1222 case RTM_IFANNOUNCE:
1224 len = sizeof(struct if_announcemsghdr);
1228 len = sizeof(struct rt_msghdr);
1231 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1232 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1234 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1236 m = m_gethdr(M_NOWAIT, MT_DATA);
1240 m->m_pkthdr.len = m->m_len = len;
1241 rtm = mtod(m, struct rt_msghdr *);
1242 bzero((caddr_t)rtm, len);
1243 for (i = 0; i < RTAX_MAX; i++) {
1244 if ((sa = rtinfo->rti_info[i]) == NULL)
1246 rtinfo->rti_addrs |= (1 << i);
1249 if (sa->sa_family == AF_INET6) {
1250 sin6 = (struct sockaddr_in6 *)&ss;
1251 bcopy(sa, sin6, sizeof(*sin6));
1252 if (sa6_recoverscope(sin6) == 0)
1253 sa = (struct sockaddr *)sin6;
1256 m_copyback(m, len, dlen, (caddr_t)sa);
1259 if (m->m_pkthdr.len != len) {
1263 rtm->rtm_msglen = len;
1264 rtm->rtm_version = RTM_VERSION;
1265 rtm->rtm_type = type;
1270 * Writes information related to @rtinfo object to preallocated buffer.
1271 * Stores needed size in @plen. If @w is NULL, calculates size without
1273 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1275 * Returns 0 on success.
1279 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1282 int len, buflen = 0, dlen;
1284 struct rt_msghdr *rtm = NULL;
1286 struct sockaddr_storage ss;
1287 struct sockaddr_in6 *sin6;
1289 #ifdef COMPAT_FREEBSD32
1290 bool compat32 = false;
1296 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1297 #ifdef COMPAT_FREEBSD32
1298 if (w->w_req->flags & SCTL_MASK32) {
1299 len = sizeof(struct ifa_msghdrl32);
1303 len = sizeof(struct ifa_msghdrl);
1305 len = sizeof(struct ifa_msghdr);
1309 #ifdef COMPAT_FREEBSD32
1310 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1311 if (w->w_op == NET_RT_IFLISTL)
1312 len = sizeof(struct if_msghdrl32);
1314 len = sizeof(struct if_msghdr32);
1319 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1320 len = sizeof(struct if_msghdrl);
1322 len = sizeof(struct if_msghdr);
1326 len = sizeof(struct ifma_msghdr);
1330 len = sizeof(struct rt_msghdr);
1334 rtm = (struct rt_msghdr *)w->w_tmem;
1335 buflen = w->w_tmemsize - len;
1336 cp = (caddr_t)w->w_tmem + len;
1339 rtinfo->rti_addrs = 0;
1340 for (i = 0; i < RTAX_MAX; i++) {
1341 struct sockaddr *sa;
1343 if ((sa = rtinfo->rti_info[i]) == NULL)
1345 rtinfo->rti_addrs |= (1 << i);
1346 #ifdef COMPAT_FREEBSD32
1348 dlen = SA_SIZE32(sa);
1352 if (cp != NULL && buflen >= dlen) {
1354 if (sa->sa_family == AF_INET6) {
1355 sin6 = (struct sockaddr_in6 *)&ss;
1356 bcopy(sa, sin6, sizeof(*sin6));
1357 if (sa6_recoverscope(sin6) == 0)
1358 sa = (struct sockaddr *)sin6;
1361 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1364 } else if (cp != NULL) {
1366 * Buffer too small. Count needed size
1367 * and return with error.
1376 dlen = ALIGN(len) - len;
1388 /* fill header iff buffer is large enough */
1389 rtm->rtm_version = RTM_VERSION;
1390 rtm->rtm_type = type;
1391 rtm->rtm_msglen = len;
1396 if (w != NULL && cp == NULL)
1403 * This routine is called to generate a message from the routing
1404 * socket indicating that a redirect has occurred, a routing lookup
1405 * has failed, or that a protocol has detected timeouts to a particular
1409 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1412 struct rt_msghdr *rtm;
1414 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1416 if (V_route_cb.any_count == 0)
1418 m = rtsock_msg_mbuf(type, rtinfo);
1422 if (fibnum != RT_ALL_FIBS) {
1423 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1424 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1425 M_SETFIB(m, fibnum);
1426 m->m_flags |= RTS_FILTER_FIB;
1429 rtm = mtod(m, struct rt_msghdr *);
1430 rtm->rtm_flags = RTF_DONE | flags;
1431 rtm->rtm_errno = error;
1432 rtm->rtm_addrs = rtinfo->rti_addrs;
1433 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1437 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1440 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1444 * This routine is called to generate a message from the routing
1445 * socket indicating that the status of a network interface has changed.
1448 rt_ifmsg(struct ifnet *ifp)
1450 struct if_msghdr *ifm;
1452 struct rt_addrinfo info;
1454 if (V_route_cb.any_count == 0)
1456 bzero((caddr_t)&info, sizeof(info));
1457 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1460 ifm = mtod(m, struct if_msghdr *);
1461 ifm->ifm_index = ifp->if_index;
1462 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1463 if_data_copy(ifp, &ifm->ifm_data);
1465 rt_dispatch(m, AF_UNSPEC);
1469 * Announce interface address arrival/withdraw.
1470 * Please do not call directly, use rt_addrmsg().
1471 * Assume input data to be valid.
1472 * Returns 0 on success.
1475 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1477 struct rt_addrinfo info;
1478 struct sockaddr *sa;
1481 struct ifa_msghdr *ifam;
1482 struct ifnet *ifp = ifa->ifa_ifp;
1483 struct sockaddr_storage ss;
1485 if (V_route_cb.any_count == 0)
1488 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1490 bzero((caddr_t)&info, sizeof(info));
1491 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1492 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1493 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1494 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1495 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1496 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1498 ifam = mtod(m, struct ifa_msghdr *);
1499 ifam->ifam_index = ifp->if_index;
1500 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1501 ifam->ifam_flags = ifa->ifa_flags;
1502 ifam->ifam_addrs = info.rti_addrs;
1504 if (fibnum != RT_ALL_FIBS) {
1505 M_SETFIB(m, fibnum);
1506 m->m_flags |= RTS_FILTER_FIB;
1509 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1515 * Announce route addition/removal to rtsock based on @rt data.
1516 * Callers are advives to use rt_routemsg() instead of using this
1517 * function directly.
1518 * Assume @rt data is consistent.
1520 * Returns 0 on success.
1523 rtsock_routemsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int rti_addrs,
1526 struct sockaddr_storage ss;
1527 struct rt_addrinfo info;
1528 struct nhop_object *nh;
1530 if (V_route_cb.any_count == 0)
1534 bzero((caddr_t)&info, sizeof(info));
1535 info.rti_info[RTAX_DST] = rt_key(rt);
1536 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt), rt_mask(rt), &ss);
1537 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1538 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
1541 return (rtsock_routemsg_info(cmd, &info, fibnum));
1545 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
1547 struct rt_msghdr *rtm;
1548 struct sockaddr *sa;
1551 if (V_route_cb.any_count == 0)
1554 if (info->rti_flags & RTF_HOST)
1555 info->rti_info[RTAX_NETMASK] = NULL;
1557 m = rtsock_msg_mbuf(cmd, info);
1561 if (fibnum != RT_ALL_FIBS) {
1562 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1563 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1564 M_SETFIB(m, fibnum);
1565 m->m_flags |= RTS_FILTER_FIB;
1568 rtm = mtod(m, struct rt_msghdr *);
1569 rtm->rtm_addrs = info->rti_addrs;
1570 if (info->rti_ifp != NULL)
1571 rtm->rtm_index = info->rti_ifp->if_index;
1572 /* Add RTF_DONE to indicate command 'completion' required by API */
1573 info->rti_flags |= RTF_DONE;
1574 /* Reported routes has to be up */
1575 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
1576 info->rti_flags |= RTF_UP;
1577 rtm->rtm_flags = info->rti_flags;
1579 sa = info->rti_info[RTAX_DST];
1580 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1586 * This is the analogue to the rt_newaddrmsg which performs the same
1587 * function but for multicast group memberhips. This is easier since
1588 * there is no route state to worry about.
1591 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1593 struct rt_addrinfo info;
1594 struct mbuf *m = NULL;
1595 struct ifnet *ifp = ifma->ifma_ifp;
1596 struct ifma_msghdr *ifmam;
1598 if (V_route_cb.any_count == 0)
1601 bzero((caddr_t)&info, sizeof(info));
1602 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1603 if (ifp && ifp->if_addr)
1604 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1606 info.rti_info[RTAX_IFP] = NULL;
1608 * If a link-layer address is present, present it as a ``gateway''
1609 * (similarly to how ARP entries, e.g., are presented).
1611 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1612 m = rtsock_msg_mbuf(cmd, &info);
1615 ifmam = mtod(m, struct ifma_msghdr *);
1616 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1618 ifmam->ifmam_index = ifp->if_index;
1619 ifmam->ifmam_addrs = info.rti_addrs;
1620 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1623 static struct mbuf *
1624 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1625 struct rt_addrinfo *info)
1627 struct if_announcemsghdr *ifan;
1630 if (V_route_cb.any_count == 0)
1632 bzero((caddr_t)info, sizeof(*info));
1633 m = rtsock_msg_mbuf(type, info);
1635 ifan = mtod(m, struct if_announcemsghdr *);
1636 ifan->ifan_index = ifp->if_index;
1637 strlcpy(ifan->ifan_name, ifp->if_xname,
1638 sizeof(ifan->ifan_name));
1639 ifan->ifan_what = what;
1645 * This is called to generate routing socket messages indicating
1646 * IEEE80211 wireless events.
1647 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1650 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1653 struct rt_addrinfo info;
1655 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1658 * Append the ieee80211 data. Try to stick it in the
1659 * mbuf containing the ifannounce msg; otherwise allocate
1660 * a new mbuf and append.
1662 * NB: we assume m is a single mbuf.
1664 if (data_len > M_TRAILINGSPACE(m)) {
1665 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1670 bcopy(data, mtod(n, void *), data_len);
1671 n->m_len = data_len;
1673 } else if (data_len > 0) {
1674 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1675 m->m_len += data_len;
1677 if (m->m_flags & M_PKTHDR)
1678 m->m_pkthdr.len += data_len;
1679 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1680 rt_dispatch(m, AF_UNSPEC);
1685 * This is called to generate routing socket messages indicating
1686 * network interface arrival and departure.
1689 rt_ifannouncemsg(struct ifnet *ifp, int what)
1692 struct rt_addrinfo info;
1694 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1696 rt_dispatch(m, AF_UNSPEC);
1700 rt_dispatch(struct mbuf *m, sa_family_t saf)
1705 * Preserve the family from the sockaddr, if any, in an m_tag for
1706 * use when injecting the mbuf into the routing socket buffer from
1709 if (saf != AF_UNSPEC) {
1710 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1716 *(unsigned short *)(tag + 1) = saf;
1717 m_tag_prepend(m, tag);
1721 m->m_pkthdr.rcvif = V_loif;
1727 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1731 * Checks if rte can be exported v.r.t jails/vnets.
1733 * Returns 1 if it can, 0 otherwise.
1736 can_export_rte(struct ucred *td_ucred, const struct rtentry *rt)
1739 if ((rt->rte_flags & RTF_HOST) == 0
1740 ? jailed_without_vnet(td_ucred)
1741 : prison_if(td_ucred, rt_key_const(rt)) != 0)
1747 * This is used in dumping the kernel table via sysctl().
1750 sysctl_dumpentry(struct radix_node *rn, void *vw)
1752 struct walkarg *w = vw;
1753 struct rtentry *rt = (struct rtentry *)rn;
1754 struct nhop_object *nh;
1759 if (w->w_op == NET_RT_FLAGS && !(rt->rte_flags & w->w_arg))
1761 if (!can_export_rte(w->w_req->td->td_ucred, rt))
1765 if (NH_IS_NHGRP(nh)) {
1766 struct weightened_nhop *wn;
1768 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
1769 for (int i = 0; i < num_nhops; i++) {
1770 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
1776 error = sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
1783 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
1786 struct rt_addrinfo info;
1787 int error = 0, size;
1788 struct sockaddr_storage ss;
1790 bzero((caddr_t)&info, sizeof(info));
1791 info.rti_info[RTAX_DST] = rt_key(rt);
1792 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1793 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1795 info.rti_info[RTAX_GENMASK] = 0;
1796 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
1797 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
1798 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
1799 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
1800 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
1802 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1804 if (w->w_req && w->w_tmem) {
1805 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1807 bzero(&rtm->rtm_index,
1808 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1809 if (rt->rte_flags & RTF_GWFLAG_COMPAT)
1810 rtm->rtm_flags = RTF_GATEWAY |
1811 (rt->rte_flags & ~RTF_GWFLAG_COMPAT);
1813 rtm->rtm_flags = rt->rte_flags;
1814 rtm->rtm_flags |= nhop_get_rtflags(nh);
1815 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
1816 rtm->rtm_rmx.rmx_weight = weight;
1817 rtm->rtm_index = nh->nh_ifp->if_index;
1818 rtm->rtm_addrs = info.rti_addrs;
1819 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1826 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1827 struct rt_addrinfo *info, struct walkarg *w, int len)
1829 struct if_msghdrl *ifm;
1830 struct if_data *ifd;
1832 ifm = (struct if_msghdrl *)w->w_tmem;
1834 #ifdef COMPAT_FREEBSD32
1835 if (w->w_req->flags & SCTL_MASK32) {
1836 struct if_msghdrl32 *ifm32;
1838 ifm32 = (struct if_msghdrl32 *)ifm;
1839 ifm32->ifm_addrs = info->rti_addrs;
1840 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1841 ifm32->ifm_index = ifp->if_index;
1842 ifm32->_ifm_spare1 = 0;
1843 ifm32->ifm_len = sizeof(*ifm32);
1844 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1845 ifm32->_ifm_spare2 = 0;
1846 ifd = &ifm32->ifm_data;
1850 ifm->ifm_addrs = info->rti_addrs;
1851 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1852 ifm->ifm_index = ifp->if_index;
1853 ifm->_ifm_spare1 = 0;
1854 ifm->ifm_len = sizeof(*ifm);
1855 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1856 ifm->_ifm_spare2 = 0;
1857 ifd = &ifm->ifm_data;
1860 memcpy(ifd, src_ifd, sizeof(*ifd));
1862 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1866 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1867 struct rt_addrinfo *info, struct walkarg *w, int len)
1869 struct if_msghdr *ifm;
1870 struct if_data *ifd;
1872 ifm = (struct if_msghdr *)w->w_tmem;
1874 #ifdef COMPAT_FREEBSD32
1875 if (w->w_req->flags & SCTL_MASK32) {
1876 struct if_msghdr32 *ifm32;
1878 ifm32 = (struct if_msghdr32 *)ifm;
1879 ifm32->ifm_addrs = info->rti_addrs;
1880 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1881 ifm32->ifm_index = ifp->if_index;
1882 ifm32->_ifm_spare1 = 0;
1883 ifd = &ifm32->ifm_data;
1887 ifm->ifm_addrs = info->rti_addrs;
1888 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1889 ifm->ifm_index = ifp->if_index;
1890 ifm->_ifm_spare1 = 0;
1891 ifd = &ifm->ifm_data;
1894 memcpy(ifd, src_ifd, sizeof(*ifd));
1896 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1900 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1901 struct walkarg *w, int len)
1903 struct ifa_msghdrl *ifam;
1904 struct if_data *ifd;
1906 ifam = (struct ifa_msghdrl *)w->w_tmem;
1908 #ifdef COMPAT_FREEBSD32
1909 if (w->w_req->flags & SCTL_MASK32) {
1910 struct ifa_msghdrl32 *ifam32;
1912 ifam32 = (struct ifa_msghdrl32 *)ifam;
1913 ifam32->ifam_addrs = info->rti_addrs;
1914 ifam32->ifam_flags = ifa->ifa_flags;
1915 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1916 ifam32->_ifam_spare1 = 0;
1917 ifam32->ifam_len = sizeof(*ifam32);
1918 ifam32->ifam_data_off =
1919 offsetof(struct ifa_msghdrl32, ifam_data);
1920 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1921 ifd = &ifam32->ifam_data;
1925 ifam->ifam_addrs = info->rti_addrs;
1926 ifam->ifam_flags = ifa->ifa_flags;
1927 ifam->ifam_index = ifa->ifa_ifp->if_index;
1928 ifam->_ifam_spare1 = 0;
1929 ifam->ifam_len = sizeof(*ifam);
1930 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1931 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1932 ifd = &ifam->ifam_data;
1935 bzero(ifd, sizeof(*ifd));
1936 ifd->ifi_datalen = sizeof(struct if_data);
1937 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1938 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1939 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1940 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1942 /* Fixup if_data carp(4) vhid. */
1943 if (carp_get_vhid_p != NULL)
1944 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1946 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1950 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1951 struct walkarg *w, int len)
1953 struct ifa_msghdr *ifam;
1955 ifam = (struct ifa_msghdr *)w->w_tmem;
1956 ifam->ifam_addrs = info->rti_addrs;
1957 ifam->ifam_flags = ifa->ifa_flags;
1958 ifam->ifam_index = ifa->ifa_ifp->if_index;
1959 ifam->_ifam_spare1 = 0;
1960 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1962 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1966 sysctl_iflist(int af, struct walkarg *w)
1971 struct rt_addrinfo info;
1973 struct sockaddr_storage ss;
1975 bzero((caddr_t)&info, sizeof(info));
1976 bzero(&ifd, sizeof(ifd));
1977 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1978 if (w->w_arg && w->w_arg != ifp->if_index)
1980 if_data_copy(ifp, &ifd);
1982 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1983 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1986 info.rti_info[RTAX_IFP] = NULL;
1987 if (w->w_req && w->w_tmem) {
1988 if (w->w_op == NET_RT_IFLISTL)
1989 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1992 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1997 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1998 if (af && af != ifa->ifa_addr->sa_family)
2000 if (prison_if(w->w_req->td->td_ucred,
2001 ifa->ifa_addr) != 0)
2003 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2004 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2005 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2006 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2007 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2010 if (w->w_req && w->w_tmem) {
2011 if (w->w_op == NET_RT_IFLISTL)
2012 error = sysctl_iflist_ifaml(ifa, &info,
2015 error = sysctl_iflist_ifam(ifa, &info,
2021 info.rti_info[RTAX_IFA] = NULL;
2022 info.rti_info[RTAX_NETMASK] = NULL;
2023 info.rti_info[RTAX_BRD] = NULL;
2030 sysctl_ifmalist(int af, struct walkarg *w)
2032 struct rt_addrinfo info;
2034 struct ifmultiaddr *ifma;
2041 bzero((caddr_t)&info, sizeof(info));
2043 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2044 if (w->w_arg && w->w_arg != ifp->if_index)
2047 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2048 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2049 if (af && af != ifma->ifma_addr->sa_family)
2051 if (prison_if(w->w_req->td->td_ucred,
2052 ifma->ifma_addr) != 0)
2054 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2055 info.rti_info[RTAX_GATEWAY] =
2056 (ifma->ifma_addr->sa_family != AF_LINK) ?
2057 ifma->ifma_lladdr : NULL;
2058 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2061 if (w->w_req && w->w_tmem) {
2062 struct ifma_msghdr *ifmam;
2064 ifmam = (struct ifma_msghdr *)w->w_tmem;
2065 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2066 ifmam->ifmam_flags = 0;
2067 ifmam->ifmam_addrs = info.rti_addrs;
2068 ifmam->_ifmam_spare1 = 0;
2069 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2081 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2084 struct epoch_tracker et;
2085 int *name = (int *)arg1;
2086 u_int namelen = arg2;
2087 struct rib_head *rnh = NULL; /* silence compiler. */
2088 int i, lim, error = EINVAL;
2097 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2099 fib = req->td->td_proc->p_fibnum;
2100 else if (namelen == 4)
2101 fib = (name[3] == RT_ALL_FIBS) ?
2102 req->td->td_proc->p_fibnum : name[3];
2104 return ((namelen < 3) ? EISDIR : ENOTDIR);
2105 if (fib < 0 || fib >= rt_numfibs)
2107 } else if (namelen != 3)
2108 return ((namelen < 3) ? EISDIR : ENOTDIR);
2112 bzero(&w, sizeof(w));
2117 error = sysctl_wire_old_buffer(req, 0);
2122 * Allocate reply buffer in advance.
2123 * All rtsock messages has maximum length of u_short.
2125 w.w_tmemsize = 65536;
2126 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2128 NET_EPOCH_ENTER(et);
2132 if (af == 0) { /* dump all tables */
2135 } else /* dump only one table */
2139 * take care of llinfo entries, the caller must
2142 if (w.w_op == NET_RT_FLAGS &&
2143 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2145 error = lltable_sysctl_dumparp(af, w.w_req);
2151 * take care of routing entries
2153 for (error = 0; error == 0 && i <= lim; i++) {
2154 rnh = rt_tables_get_rnh(fib, i);
2157 error = rnh->rnh_walktree(&rnh->head,
2158 sysctl_dumpentry, &w);
2161 error = EAFNOSUPPORT;
2166 /* Allow dumping one specific af/fib at a time */
2172 if (fib < 0 || fib > rt_numfibs) {
2176 rnh = rt_tables_get_rnh(fib, af);
2178 error = EAFNOSUPPORT;
2181 if (w.w_op == NET_RT_NHOP)
2182 error = nhops_dump_sysctl(rnh, w.w_req);
2185 error = nhgrp_dump_sysctl(rnh, w.w_req);
2191 case NET_RT_IFLISTL:
2192 error = sysctl_iflist(af, &w);
2195 case NET_RT_IFMALIST:
2196 error = sysctl_ifmalist(af, &w);
2201 free(w.w_tmem, M_TEMP);
2205 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2206 sysctl_rtsock, "Return route tables and interface/address lists");
2209 * Definitions of protocols supported in the ROUTE domain.
2212 static struct domain routedomain; /* or at least forward */
2214 static struct protosw routesw[] = {
2216 .pr_type = SOCK_RAW,
2217 .pr_domain = &routedomain,
2218 .pr_flags = PR_ATOMIC|PR_ADDR,
2219 .pr_output = route_output,
2220 .pr_ctlinput = raw_ctlinput,
2221 .pr_init = raw_init,
2222 .pr_usrreqs = &route_usrreqs
2226 static struct domain routedomain = {
2227 .dom_family = PF_ROUTE,
2228 .dom_name = "route",
2229 .dom_protosw = routesw,
2230 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2233 VNET_DOMAIN_SET(route);