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>
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip_carp.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
76 #include <net/route/nhop.h>
78 #ifdef COMPAT_FREEBSD32
79 #include <sys/mount.h>
80 #include <compat/freebsd32/freebsd32.h>
90 struct if_data ifm_data;
100 uint16_t _ifm_spare1;
102 uint16_t ifm_data_off;
103 uint32_t _ifm_spare2;
104 struct if_data ifm_data;
107 struct ifa_msghdrl32 {
108 uint16_t ifam_msglen;
109 uint8_t ifam_version;
114 uint16_t _ifam_spare1;
116 uint16_t ifam_data_off;
118 struct if_data ifam_data;
121 #define SA_SIZE32(sa) \
122 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
124 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
126 #endif /* COMPAT_FREEBSD32 */
128 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
130 /* NB: these are not modified */
131 static struct sockaddr route_src = { 2, PF_ROUTE, };
132 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
134 /* These are external hooks for CARP. */
135 int (*carp_get_vhid_p)(struct ifaddr *);
138 * Used by rtsock/raw_input callback code to decide whether to filter the update
139 * notification to a socket bound to a particular FIB.
141 #define RTS_FILTER_FIB M_PROTO8
144 int ip_count; /* attached w/ AF_INET */
145 int ip6_count; /* attached w/ AF_INET6 */
146 int any_count; /* total attached */
148 VNET_DEFINE_STATIC(route_cb_t, route_cb);
149 #define V_route_cb VNET(route_cb)
151 struct mtx rtsock_mtx;
152 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
154 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
155 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
156 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
158 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
165 struct sysctl_req *w_req;
166 struct sockaddr *dst;
167 struct sockaddr *mask;
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 rtentry *rt, 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 bool can_export_rte(struct ucred *td_ucred, bool rt_is_host,
194 const struct sockaddr *rt_dst);
196 static struct netisr_handler rtsock_nh = {
198 .nh_handler = rts_input,
199 .nh_proto = NETISR_ROUTE,
200 .nh_policy = NETISR_POLICY_SOURCE,
204 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
208 netisr_getqlimit(&rtsock_nh, &qlimit);
209 error = sysctl_handle_int(oidp, &qlimit, 0, req);
210 if (error || !req->newptr)
214 return (netisr_setqlimit(&rtsock_nh, qlimit));
216 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
217 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
218 0, 0, sysctl_route_netisr_maxqlen, "I",
219 "maximum routing socket dispatch queue length");
226 if (IS_DEFAULT_VNET(curvnet)) {
227 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
228 rtsock_nh.nh_qlimit = tmp;
229 netisr_register(&rtsock_nh);
233 netisr_register_vnet(&rtsock_nh);
236 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
241 vnet_rts_uninit(void)
244 netisr_unregister_vnet(&rtsock_nh);
246 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
251 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
256 KASSERT(m != NULL, ("%s: m is NULL", __func__));
257 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
258 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
260 /* No filtering requested. */
261 if ((m->m_flags & RTS_FILTER_FIB) == 0)
264 /* Check if it is a rts and the fib matches the one of the socket. */
265 fibnum = M_GETFIB(m);
266 if (proto->sp_family != PF_ROUTE ||
267 rp->rcb_socket == NULL ||
268 rp->rcb_socket->so_fibnum == fibnum)
271 /* Filtering requested and no match, the socket shall be skipped. */
276 rts_input(struct mbuf *m)
278 struct sockproto route_proto;
279 unsigned short *family;
282 route_proto.sp_family = PF_ROUTE;
283 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
285 family = (unsigned short *)(tag + 1);
286 route_proto.sp_protocol = *family;
287 m_tag_delete(m, tag);
289 route_proto.sp_protocol = 0;
291 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
295 * It really doesn't make any sense at all for this code to share much
296 * with raw_usrreq.c, since its functionality is so restricted. XXX
299 rts_abort(struct socket *so)
302 raw_usrreqs.pru_abort(so);
306 rts_close(struct socket *so)
309 raw_usrreqs.pru_close(so);
312 /* pru_accept is EOPNOTSUPP */
315 rts_attach(struct socket *so, int proto, struct thread *td)
320 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
323 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
325 so->so_pcb = (caddr_t)rp;
326 so->so_fibnum = td->td_proc->p_fibnum;
327 error = raw_attach(so, proto);
335 switch(rp->rcb_proto.sp_protocol) {
337 V_route_cb.ip_count++;
340 V_route_cb.ip6_count++;
343 V_route_cb.any_count++;
346 so->so_options |= SO_USELOOPBACK;
351 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
354 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
358 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
361 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
364 /* pru_connect2 is EOPNOTSUPP */
365 /* pru_control is EOPNOTSUPP */
368 rts_detach(struct socket *so)
370 struct rawcb *rp = sotorawcb(so);
372 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
375 switch(rp->rcb_proto.sp_protocol) {
377 V_route_cb.ip_count--;
380 V_route_cb.ip6_count--;
383 V_route_cb.any_count--;
385 raw_usrreqs.pru_detach(so);
389 rts_disconnect(struct socket *so)
392 return (raw_usrreqs.pru_disconnect(so));
395 /* pru_listen is EOPNOTSUPP */
398 rts_peeraddr(struct socket *so, struct sockaddr **nam)
401 return (raw_usrreqs.pru_peeraddr(so, nam));
404 /* pru_rcvd is EOPNOTSUPP */
405 /* pru_rcvoob is EOPNOTSUPP */
408 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
409 struct mbuf *control, struct thread *td)
412 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
415 /* pru_sense is null */
418 rts_shutdown(struct socket *so)
421 return (raw_usrreqs.pru_shutdown(so));
425 rts_sockaddr(struct socket *so, struct sockaddr **nam)
428 return (raw_usrreqs.pru_sockaddr(so, nam));
431 static struct pr_usrreqs route_usrreqs = {
432 .pru_abort = rts_abort,
433 .pru_attach = rts_attach,
434 .pru_bind = rts_bind,
435 .pru_connect = rts_connect,
436 .pru_detach = rts_detach,
437 .pru_disconnect = rts_disconnect,
438 .pru_peeraddr = rts_peeraddr,
439 .pru_send = rts_send,
440 .pru_shutdown = rts_shutdown,
441 .pru_sockaddr = rts_sockaddr,
442 .pru_close = rts_close,
445 #ifndef _SOCKADDR_UNION_DEFINED
446 #define _SOCKADDR_UNION_DEFINED
448 * The union of all possible address formats we handle.
450 union sockaddr_union {
452 struct sockaddr_in sin;
453 struct sockaddr_in6 sin6;
455 #endif /* _SOCKADDR_UNION_DEFINED */
458 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
459 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
461 #if defined(INET) || defined(INET6)
462 struct epoch_tracker et;
465 /* First, see if the returned address is part of the jail. */
466 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
467 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
471 switch (info->rti_info[RTAX_DST]->sa_family) {
481 * Try to find an address on the given outgoing interface
482 * that belongs to the jail.
485 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
488 if (sa->sa_family != AF_INET)
490 ia = ((struct sockaddr_in *)sa)->sin_addr;
491 if (prison_check_ip4(cred, &ia) == 0) {
499 * As a last resort return the 'default' jail address.
501 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
503 if (prison_get_ip4(cred, &ia) != 0)
506 bzero(&saun->sin, sizeof(struct sockaddr_in));
507 saun->sin.sin_len = sizeof(struct sockaddr_in);
508 saun->sin.sin_family = AF_INET;
509 saun->sin.sin_addr.s_addr = ia.s_addr;
510 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
523 * Try to find an address on the given outgoing interface
524 * that belongs to the jail.
527 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
530 if (sa->sa_family != AF_INET6)
532 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
533 &ia6, sizeof(struct in6_addr));
534 if (prison_check_ip6(cred, &ia6) == 0) {
542 * As a last resort return the 'default' jail address.
544 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
546 if (prison_get_ip6(cred, &ia6) != 0)
549 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
550 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
551 saun->sin6.sin6_family = AF_INET6;
552 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
553 if (sa6_recoverscope(&saun->sin6) != 0)
555 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
566 * Fills in @info based on userland-provided @rtm message.
568 * Returns 0 on success.
571 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info)
576 rtm->rtm_pid = curproc->p_pid;
577 info->rti_addrs = rtm->rtm_addrs;
579 info->rti_mflags = rtm->rtm_inits;
580 info->rti_rmx = &rtm->rtm_rmx;
583 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
584 * link-local address because rtrequest requires addresses with
587 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
590 if (rtm->rtm_flags & RTF_RNH_LOCKED)
592 info->rti_flags = rtm->rtm_flags;
593 if (info->rti_info[RTAX_DST] == NULL ||
594 info->rti_info[RTAX_DST]->sa_family >= AF_MAX ||
595 (info->rti_info[RTAX_GATEWAY] != NULL &&
596 info->rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
598 saf = info->rti_info[RTAX_DST]->sa_family;
600 * Verify that the caller has the appropriate privilege; RTM_GET
601 * is the only operation the non-superuser is allowed.
603 if (rtm->rtm_type != RTM_GET) {
604 error = priv_check(curthread, PRIV_NET_ROUTE);
610 * The given gateway address may be an interface address.
611 * For example, issuing a "route change" command on a route
612 * entry that was created from a tunnel, and the gateway
613 * address given is the local end point. In this case the
614 * RTF_GATEWAY flag must be cleared or the destination will
615 * not be reachable even though there is no error message.
617 if (info->rti_info[RTAX_GATEWAY] != NULL &&
618 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
619 struct rt_addrinfo ginfo;
620 struct sockaddr *gdst;
621 struct sockaddr_storage ss;
623 bzero(&ginfo, sizeof(ginfo));
624 bzero(&ss, sizeof(ss));
625 ss.ss_len = sizeof(ss);
627 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
628 gdst = info->rti_info[RTAX_GATEWAY];
631 * A host route through the loopback interface is
632 * installed for each interface adddress. In pre 8.0
633 * releases the interface address of a PPP link type
634 * is not reachable locally. This behavior is fixed as
635 * part of the new L2/L3 redesign and rewrite work. The
636 * signature of this interface address route is the
637 * AF_LINK sa_family type of the gateway, and the
638 * rt_ifp has the IFF_LOOPBACK flag set.
640 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
641 if (ss.ss_family == AF_LINK &&
642 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
643 info->rti_flags &= ~RTF_GATEWAY;
644 info->rti_flags |= RTF_GWFLAG_COMPAT;
646 rib_free_info(&ginfo);
653 static struct nhop_object *
654 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
656 if (!NH_IS_NHGRP(nh))
659 struct weightened_nhop *wn;
661 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
664 for (int i = 0; i < num_nhops; i++) {
665 if (match_nhop_gw(wn[i].nh, gw))
673 * Handles RTM_GET message from routing socket, returning matching rt.
676 * 0 on success, with locked and referenced matching rt in @rt_nrt
680 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
681 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
684 struct rib_head *rnh;
685 struct nhop_object *nh;
688 saf = info->rti_info[RTAX_DST]->sa_family;
690 rnh = rt_tables_get_rnh(fibnum, saf);
692 return (EAFNOSUPPORT);
696 if (info->rti_info[RTAX_NETMASK] == NULL) {
698 * Provide longest prefix match for
699 * address lookup (no mask).
700 * 'route -n get addr'
702 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
703 info->rti_info[RTAX_DST], &rnh->head);
705 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
706 info->rti_info[RTAX_DST],
707 info->rti_info[RTAX_NETMASK], &rnh->head);
709 if (rc->rc_rt == NULL) {
714 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
720 * If performing proxied L2 entry insertion, and
721 * the actual PPP host entry is found, perform
722 * another search to retrieve the prefix route of
723 * the local end point of the PPP link.
724 * TODO: move this logic to userland.
726 if (rtm->rtm_flags & RTF_ANNOUNCE) {
727 struct sockaddr laddr;
729 if (nh->nh_ifp != NULL &&
730 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
733 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
736 rt_maskedcopy(ifa->ifa_addr,
740 rt_maskedcopy(nh->nh_ifa->ifa_addr,
742 nh->nh_ifa->ifa_netmask);
744 * refactor rt and no lock operation necessary
746 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
748 if (rc->rc_rt == NULL) {
752 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
759 rc->rc_nh_weight = rc->rc_rt->rt_weight;
766 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
769 if (family == AF_INET) {
770 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
771 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
773 bzero(dst4, sizeof(struct sockaddr_in));
774 bzero(mask4, sizeof(struct sockaddr_in));
776 dst4->sin_family = AF_INET;
777 dst4->sin_len = sizeof(struct sockaddr_in);
778 mask4->sin_family = AF_INET;
779 mask4->sin_len = sizeof(struct sockaddr_in);
783 if (family == AF_INET6) {
784 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
785 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
787 bzero(dst6, sizeof(struct sockaddr_in6));
788 bzero(mask6, sizeof(struct sockaddr_in6));
790 dst6->sin6_family = AF_INET6;
791 dst6->sin6_len = sizeof(struct sockaddr_in6);
792 mask6->sin6_family = AF_INET6;
793 mask6->sin6_len = sizeof(struct sockaddr_in6);
799 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
800 struct sockaddr *mask)
803 if (dst->sa_family == AF_INET) {
804 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
805 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
806 uint32_t scopeid = 0;
807 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
813 if (dst->sa_family == AF_INET6) {
814 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
815 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
816 uint32_t scopeid = 0;
817 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
818 &mask6->sin6_addr, &scopeid);
819 dst6->sin6_scope_id = scopeid;
827 * Update sockaddrs, flags, etc in @prtm based on @rc data.
828 * rtm can be reallocated.
830 * Returns 0 on success, along with pointer to (potentially reallocated)
835 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
836 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
839 union sockaddr_union saun;
840 struct rt_msghdr *rtm, *orig_rtm = NULL;
845 union sockaddr_union sa_dst, sa_mask;
846 int family = info->rti_info[RTAX_DST]->sa_family;
847 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
848 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
850 info->rti_info[RTAX_DST] = &sa_dst.sa;
851 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
852 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
853 info->rti_info[RTAX_GENMASK] = 0;
855 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
857 info->rti_info[RTAX_IFP] =
858 ifp->if_addr->ifa_addr;
859 error = rtm_get_jailed(info, ifp, nh,
860 &saun, curthread->td_ucred);
863 if (ifp->if_flags & IFF_POINTOPOINT)
864 info->rti_info[RTAX_BRD] =
865 nh->nh_ifa->ifa_dstaddr;
866 rtm->rtm_index = ifp->if_index;
868 info->rti_info[RTAX_IFP] = NULL;
869 info->rti_info[RTAX_IFA] = NULL;
871 } else if (ifp != NULL)
872 rtm->rtm_index = ifp->if_index;
874 /* Check if we need to realloc storage */
875 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
876 if (len > alloc_len) {
877 struct rt_msghdr *tmp_rtm;
879 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
882 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
888 * Delay freeing original rtm as info contains
889 * data referencing it.
893 w.w_tmem = (caddr_t)rtm;
894 w.w_tmemsize = alloc_len;
895 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
897 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
898 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
899 rtm->rtm_flags = RTF_GATEWAY |
900 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
901 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
902 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
903 rtm->rtm_addrs = info->rti_addrs;
905 if (orig_rtm != NULL)
906 free(orig_rtm, M_TEMP);
914 save_del_notification(struct rib_cmd_info *rc, void *_cbdata)
916 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
918 if (rc->rc_cmd == RTM_DELETE)
923 save_add_notification(struct rib_cmd_info *rc, void *_cbdata)
925 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
927 if (rc->rc_cmd == RTM_ADD)
934 route_output(struct mbuf *m, struct socket *so, ...)
936 struct rt_msghdr *rtm = NULL;
937 struct rtentry *rt = NULL;
938 struct rt_addrinfo info;
939 struct epoch_tracker et;
941 struct sockaddr_storage ss;
942 struct sockaddr_in6 *sin6;
943 int i, rti_need_deembed = 0;
945 int alloc_len = 0, len, error = 0, fibnum;
946 sa_family_t saf = AF_UNSPEC;
948 struct rib_cmd_info rc;
949 struct nhop_object *nh;
951 fibnum = so->so_fibnum;
952 #define senderr(e) { error = e; goto flush;}
953 if (m == NULL || ((m->m_len < sizeof(long)) &&
954 (m = m_pullup(m, sizeof(long))) == NULL))
956 if ((m->m_flags & M_PKTHDR) == 0)
957 panic("route_output");
959 len = m->m_pkthdr.len;
960 if (len < sizeof(*rtm) ||
961 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
965 * Most of current messages are in range 200-240 bytes,
966 * minimize possible re-allocation on reply using larger size
967 * buffer aligned on 1k boundaty.
969 alloc_len = roundup2(len, 1024);
970 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
973 m_copydata(m, 0, len, (caddr_t)rtm);
974 bzero(&info, sizeof(info));
975 bzero(&w, sizeof(w));
978 if (rtm->rtm_version != RTM_VERSION) {
979 /* Do not touch message since format is unknown */
982 senderr(EPROTONOSUPPORT);
986 * Starting from here, it is possible
987 * to alter original message and insert
988 * caller PID and error value.
991 if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) {
995 saf = info.rti_info[RTAX_DST]->sa_family;
997 /* support for new ARP code */
998 if (rtm->rtm_flags & RTF_LLDATA) {
999 error = lla_rt_output(rtm, &info);
1002 rti_need_deembed = 1;
1007 switch (rtm->rtm_type) {
1010 if (rtm->rtm_type == RTM_ADD) {
1011 if (info.rti_info[RTAX_GATEWAY] == NULL)
1014 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1017 rti_need_deembed = 1;
1020 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1021 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1022 struct rib_cmd_info rc_simple = {};
1023 rib_decompose_notification(&rc,
1024 save_add_notification, (void *)&rc_simple);
1029 rtm->rtm_index = nh->nh_ifp->if_index;
1030 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1035 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1038 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1039 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1040 struct rib_cmd_info rc_simple = {};
1041 rib_decompose_notification(&rc,
1042 save_del_notification, (void *)&rc_simple);
1050 /* rt_msg2() will not be used when RTM_DELETE fails. */
1051 rti_need_deembed = 1;
1056 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1062 if (!can_export_rte(curthread->td_ucred,
1063 info.rti_info[RTAX_NETMASK] == NULL,
1064 info.rti_info[RTAX_DST])) {
1068 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1070 * Note that some sockaddr pointers may have changed to
1071 * point to memory outsize @rtm. Some may be pointing
1072 * to the on-stack variables.
1073 * Given that, any pointer in @info CANNOT BE USED.
1077 * scopeid deembedding has been performed while
1078 * writing updated rtm in rtsock_msg_buffer().
1079 * With that in mind, skip deembedding procedure below.
1082 rti_need_deembed = 0;
1089 senderr(EOPNOTSUPP);
1098 if (rti_need_deembed) {
1099 /* sin6_scope_id is recovered before sending rtm. */
1100 sin6 = (struct sockaddr_in6 *)&ss;
1101 for (i = 0; i < RTAX_MAX; i++) {
1102 if (info.rti_info[i] == NULL)
1104 if (info.rti_info[i]->sa_family != AF_INET6)
1106 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1107 if (sa6_recoverscope(sin6) == 0)
1108 bcopy(sin6, info.rti_info[i],
1114 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1120 * Sends the prepared reply message in @rtm to all rtsock clients.
1121 * Frees @m and @rtm.
1125 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1126 sa_family_t saf, u_int fibnum, int rtm_errno)
1128 struct rawcb *rp = NULL;
1131 * Check to see if we don't want our own messages.
1133 if ((so->so_options & SO_USELOOPBACK) == 0) {
1134 if (V_route_cb.any_count <= 1) {
1140 /* There is another listener, so construct message */
1146 rtm->rtm_errno = rtm_errno;
1148 rtm->rtm_flags |= RTF_DONE;
1150 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1151 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1154 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1155 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1160 M_SETFIB(m, fibnum);
1161 m->m_flags |= RTS_FILTER_FIB;
1164 * XXX insure we don't get a copy by
1165 * invalidating our protocol
1167 unsigned short family = rp->rcb_proto.sp_family;
1168 rp->rcb_proto.sp_family = 0;
1169 rt_dispatch(m, saf);
1170 rp->rcb_proto.sp_family = family;
1172 rt_dispatch(m, saf);
1177 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1178 struct rt_metrics *out)
1181 bzero(out, sizeof(*out));
1182 out->rmx_mtu = nh->nh_mtu;
1183 out->rmx_weight = rt->rt_weight;
1184 out->rmx_nhidx = nhop_get_idx(nh);
1185 /* Kernel -> userland timebase conversion. */
1186 out->rmx_expire = rt->rt_expire ?
1187 rt->rt_expire - time_uptime + time_second : 0;
1191 * Extract the addresses of the passed sockaddrs.
1192 * Do a little sanity checking so as to avoid bad memory references.
1193 * This data is derived straight from userland.
1196 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1198 struct sockaddr *sa;
1201 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1202 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1204 sa = (struct sockaddr *)cp;
1208 if (cp + sa->sa_len > cplim)
1211 * there are no more.. quit now
1212 * If there are more bits, they are in error.
1213 * I've seen this. route(1) can evidently generate these.
1214 * This causes kernel to core dump.
1215 * for compatibility, If we see this, point to a safe address.
1217 if (sa->sa_len == 0) {
1218 rtinfo->rti_info[i] = &sa_zero;
1219 return (0); /* should be EINVAL but for compat */
1223 if (sa->sa_family == AF_INET6)
1224 sa6_embedscope((struct sockaddr_in6 *)sa,
1227 rtinfo->rti_info[i] = sa;
1234 * Fill in @dmask with valid netmask leaving original @smask
1235 * intact. Mostly used with radix netmasks.
1238 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1239 struct sockaddr_storage *dmask)
1241 if (dst == NULL || smask == NULL)
1244 memset(dmask, 0, dst->sa_len);
1245 memcpy(dmask, smask, smask->sa_len);
1246 dmask->ss_len = dst->sa_len;
1247 dmask->ss_family = dst->sa_family;
1249 return ((struct sockaddr *)dmask);
1253 * Writes information related to @rtinfo object to newly-allocated mbuf.
1254 * Assumes MCLBYTES is enough to construct any message.
1255 * Used for OS notifications of vaious events (if/ifa announces,etc)
1257 * Returns allocated mbuf or NULL on failure.
1259 static struct mbuf *
1260 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1262 struct sockaddr_storage ss;
1263 struct rt_msghdr *rtm;
1266 struct sockaddr *sa;
1268 struct sockaddr_in6 *sin6;
1275 len = sizeof(struct ifa_msghdr);
1280 len = sizeof(struct ifma_msghdr);
1284 len = sizeof(struct if_msghdr);
1287 case RTM_IFANNOUNCE:
1289 len = sizeof(struct if_announcemsghdr);
1293 len = sizeof(struct rt_msghdr);
1296 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1297 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1299 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1301 m = m_gethdr(M_NOWAIT, MT_DATA);
1305 m->m_pkthdr.len = m->m_len = len;
1306 rtm = mtod(m, struct rt_msghdr *);
1307 bzero((caddr_t)rtm, len);
1308 for (i = 0; i < RTAX_MAX; i++) {
1309 if ((sa = rtinfo->rti_info[i]) == NULL)
1311 rtinfo->rti_addrs |= (1 << i);
1314 KASSERT(dlen <= sizeof(ss),
1315 ("%s: sockaddr size overflow", __func__));
1316 bzero(&ss, sizeof(ss));
1317 bcopy(sa, &ss, sa->sa_len);
1318 sa = (struct sockaddr *)&ss;
1320 if (sa->sa_family == AF_INET6) {
1321 sin6 = (struct sockaddr_in6 *)sa;
1322 (void)sa6_recoverscope(sin6);
1325 m_copyback(m, len, dlen, (caddr_t)sa);
1328 if (m->m_pkthdr.len != len) {
1332 rtm->rtm_msglen = len;
1333 rtm->rtm_version = RTM_VERSION;
1334 rtm->rtm_type = type;
1339 * Writes information related to @rtinfo object to preallocated buffer.
1340 * Stores needed size in @plen. If @w is NULL, calculates size without
1342 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1344 * Returns 0 on success.
1348 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1350 struct sockaddr_storage ss;
1351 int len, buflen = 0, dlen, i;
1353 struct rt_msghdr *rtm = NULL;
1355 struct sockaddr_in6 *sin6;
1357 #ifdef COMPAT_FREEBSD32
1358 bool compat32 = false;
1364 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1365 #ifdef COMPAT_FREEBSD32
1366 if (w->w_req->flags & SCTL_MASK32) {
1367 len = sizeof(struct ifa_msghdrl32);
1371 len = sizeof(struct ifa_msghdrl);
1373 len = sizeof(struct ifa_msghdr);
1377 #ifdef COMPAT_FREEBSD32
1378 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1379 if (w->w_op == NET_RT_IFLISTL)
1380 len = sizeof(struct if_msghdrl32);
1382 len = sizeof(struct if_msghdr32);
1387 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1388 len = sizeof(struct if_msghdrl);
1390 len = sizeof(struct if_msghdr);
1394 len = sizeof(struct ifma_msghdr);
1398 len = sizeof(struct rt_msghdr);
1402 rtm = (struct rt_msghdr *)w->w_tmem;
1403 buflen = w->w_tmemsize - len;
1404 cp = (caddr_t)w->w_tmem + len;
1407 rtinfo->rti_addrs = 0;
1408 for (i = 0; i < RTAX_MAX; i++) {
1409 struct sockaddr *sa;
1411 if ((sa = rtinfo->rti_info[i]) == NULL)
1413 rtinfo->rti_addrs |= (1 << i);
1414 #ifdef COMPAT_FREEBSD32
1416 dlen = SA_SIZE32(sa);
1420 if (cp != NULL && buflen >= dlen) {
1421 KASSERT(dlen <= sizeof(ss),
1422 ("%s: sockaddr size overflow", __func__));
1423 bzero(&ss, sizeof(ss));
1424 bcopy(sa, &ss, sa->sa_len);
1425 sa = (struct sockaddr *)&ss;
1427 if (sa->sa_family == AF_INET6) {
1428 sin6 = (struct sockaddr_in6 *)sa;
1429 (void)sa6_recoverscope(sin6);
1432 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1435 } else if (cp != NULL) {
1437 * Buffer too small. Count needed size
1438 * and return with error.
1447 dlen = ALIGN(len) - len;
1459 /* fill header iff buffer is large enough */
1460 rtm->rtm_version = RTM_VERSION;
1461 rtm->rtm_type = type;
1462 rtm->rtm_msglen = len;
1467 if (w != NULL && cp == NULL)
1474 * This routine is called to generate a message from the routing
1475 * socket indicating that a redirect has occurred, a routing lookup
1476 * has failed, or that a protocol has detected timeouts to a particular
1480 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1483 struct rt_msghdr *rtm;
1485 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1487 if (V_route_cb.any_count == 0)
1489 m = rtsock_msg_mbuf(type, rtinfo);
1493 if (fibnum != RT_ALL_FIBS) {
1494 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1495 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1496 M_SETFIB(m, fibnum);
1497 m->m_flags |= RTS_FILTER_FIB;
1500 rtm = mtod(m, struct rt_msghdr *);
1501 rtm->rtm_flags = RTF_DONE | flags;
1502 rtm->rtm_errno = error;
1503 rtm->rtm_addrs = rtinfo->rti_addrs;
1504 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1508 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1511 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1515 * This routine is called to generate a message from the routing
1516 * socket indicating that the status of a network interface has changed.
1519 rt_ifmsg(struct ifnet *ifp)
1521 struct if_msghdr *ifm;
1523 struct rt_addrinfo info;
1525 if (V_route_cb.any_count == 0)
1527 bzero((caddr_t)&info, sizeof(info));
1528 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1531 ifm = mtod(m, struct if_msghdr *);
1532 ifm->ifm_index = ifp->if_index;
1533 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1534 if_data_copy(ifp, &ifm->ifm_data);
1536 rt_dispatch(m, AF_UNSPEC);
1540 * Announce interface address arrival/withdraw.
1541 * Please do not call directly, use rt_addrmsg().
1542 * Assume input data to be valid.
1543 * Returns 0 on success.
1546 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1548 struct rt_addrinfo info;
1549 struct sockaddr *sa;
1552 struct ifa_msghdr *ifam;
1553 struct ifnet *ifp = ifa->ifa_ifp;
1554 struct sockaddr_storage ss;
1556 if (V_route_cb.any_count == 0)
1559 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1561 bzero((caddr_t)&info, sizeof(info));
1562 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1563 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1564 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1565 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1566 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1567 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1569 ifam = mtod(m, struct ifa_msghdr *);
1570 ifam->ifam_index = ifp->if_index;
1571 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1572 ifam->ifam_flags = ifa->ifa_flags;
1573 ifam->ifam_addrs = info.rti_addrs;
1575 if (fibnum != RT_ALL_FIBS) {
1576 M_SETFIB(m, fibnum);
1577 m->m_flags |= RTS_FILTER_FIB;
1580 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1586 * Announce route addition/removal to rtsock based on @rt data.
1587 * Callers are advives to use rt_routemsg() instead of using this
1588 * function directly.
1589 * Assume @rt data is consistent.
1591 * Returns 0 on success.
1594 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
1597 union sockaddr_union dst, mask;
1598 struct rt_addrinfo info;
1600 if (V_route_cb.any_count == 0)
1603 int family = rt_get_family(rt);
1604 init_sockaddrs_family(family, &dst.sa, &mask.sa);
1605 export_rtaddrs(rt, &dst.sa, &mask.sa);
1607 bzero((caddr_t)&info, sizeof(info));
1608 info.rti_info[RTAX_DST] = &dst.sa;
1609 info.rti_info[RTAX_NETMASK] = &mask.sa;
1610 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1611 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
1612 info.rti_ifp = nh->nh_ifp;
1614 return (rtsock_routemsg_info(cmd, &info, fibnum));
1618 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
1620 struct rt_msghdr *rtm;
1621 struct sockaddr *sa;
1624 if (V_route_cb.any_count == 0)
1627 if (info->rti_flags & RTF_HOST)
1628 info->rti_info[RTAX_NETMASK] = NULL;
1630 m = rtsock_msg_mbuf(cmd, info);
1634 if (fibnum != RT_ALL_FIBS) {
1635 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1636 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1637 M_SETFIB(m, fibnum);
1638 m->m_flags |= RTS_FILTER_FIB;
1641 rtm = mtod(m, struct rt_msghdr *);
1642 rtm->rtm_addrs = info->rti_addrs;
1643 if (info->rti_ifp != NULL)
1644 rtm->rtm_index = info->rti_ifp->if_index;
1645 /* Add RTF_DONE to indicate command 'completion' required by API */
1646 info->rti_flags |= RTF_DONE;
1647 /* Reported routes has to be up */
1648 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
1649 info->rti_flags |= RTF_UP;
1650 rtm->rtm_flags = info->rti_flags;
1652 sa = info->rti_info[RTAX_DST];
1653 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1659 * This is the analogue to the rt_newaddrmsg which performs the same
1660 * function but for multicast group memberhips. This is easier since
1661 * there is no route state to worry about.
1664 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1666 struct rt_addrinfo info;
1667 struct mbuf *m = NULL;
1668 struct ifnet *ifp = ifma->ifma_ifp;
1669 struct ifma_msghdr *ifmam;
1671 if (V_route_cb.any_count == 0)
1674 bzero((caddr_t)&info, sizeof(info));
1675 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1676 if (ifp && ifp->if_addr)
1677 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1679 info.rti_info[RTAX_IFP] = NULL;
1681 * If a link-layer address is present, present it as a ``gateway''
1682 * (similarly to how ARP entries, e.g., are presented).
1684 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1685 m = rtsock_msg_mbuf(cmd, &info);
1688 ifmam = mtod(m, struct ifma_msghdr *);
1689 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1691 ifmam->ifmam_index = ifp->if_index;
1692 ifmam->ifmam_addrs = info.rti_addrs;
1693 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1696 static struct mbuf *
1697 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1698 struct rt_addrinfo *info)
1700 struct if_announcemsghdr *ifan;
1703 if (V_route_cb.any_count == 0)
1705 bzero((caddr_t)info, sizeof(*info));
1706 m = rtsock_msg_mbuf(type, info);
1708 ifan = mtod(m, struct if_announcemsghdr *);
1709 ifan->ifan_index = ifp->if_index;
1710 strlcpy(ifan->ifan_name, ifp->if_xname,
1711 sizeof(ifan->ifan_name));
1712 ifan->ifan_what = what;
1718 * This is called to generate routing socket messages indicating
1719 * IEEE80211 wireless events.
1720 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1723 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1726 struct rt_addrinfo info;
1728 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1731 * Append the ieee80211 data. Try to stick it in the
1732 * mbuf containing the ifannounce msg; otherwise allocate
1733 * a new mbuf and append.
1735 * NB: we assume m is a single mbuf.
1737 if (data_len > M_TRAILINGSPACE(m)) {
1738 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1743 bcopy(data, mtod(n, void *), data_len);
1744 n->m_len = data_len;
1746 } else if (data_len > 0) {
1747 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1748 m->m_len += data_len;
1750 if (m->m_flags & M_PKTHDR)
1751 m->m_pkthdr.len += data_len;
1752 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1753 rt_dispatch(m, AF_UNSPEC);
1758 * This is called to generate routing socket messages indicating
1759 * network interface arrival and departure.
1762 rt_ifannouncemsg(struct ifnet *ifp, int what)
1765 struct rt_addrinfo info;
1767 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1769 rt_dispatch(m, AF_UNSPEC);
1773 rt_dispatch(struct mbuf *m, sa_family_t saf)
1778 * Preserve the family from the sockaddr, if any, in an m_tag for
1779 * use when injecting the mbuf into the routing socket buffer from
1782 if (saf != AF_UNSPEC) {
1783 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1789 *(unsigned short *)(tag + 1) = saf;
1790 m_tag_prepend(m, tag);
1794 m->m_pkthdr.rcvif = V_loif;
1800 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1804 * Checks if rte can be exported v.r.t jails/vnets.
1806 * Returns 1 if it can, 0 otherwise.
1809 can_export_rte(struct ucred *td_ucred, bool rt_is_host,
1810 const struct sockaddr *rt_dst)
1813 if ((!rt_is_host) ? jailed_without_vnet(td_ucred)
1814 : prison_if(td_ucred, rt_dst) != 0)
1821 * This is used in dumping the kernel table via sysctl().
1824 sysctl_dumpentry(struct rtentry *rt, void *vw)
1826 struct walkarg *w = vw;
1827 struct nhop_object *nh;
1832 export_rtaddrs(rt, w->dst, w->mask);
1833 if (!can_export_rte(w->w_req->td->td_ucred, rt_is_host(rt), w->dst))
1835 nh = rt_get_raw_nhop(rt);
1837 if (NH_IS_NHGRP(nh)) {
1838 struct weightened_nhop *wn;
1840 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
1841 for (int i = 0; i < num_nhops; i++) {
1842 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
1848 error = sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
1855 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
1858 struct rt_addrinfo info;
1859 int error = 0, size;
1862 rtflags = nhop_get_rtflags(nh);
1864 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
1867 bzero((caddr_t)&info, sizeof(info));
1868 info.rti_info[RTAX_DST] = w->dst;
1869 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1870 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
1871 info.rti_info[RTAX_GENMASK] = 0;
1872 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
1873 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
1874 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
1875 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
1876 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
1878 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1880 if (w->w_req && w->w_tmem) {
1881 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1883 bzero(&rtm->rtm_index,
1884 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1887 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
1888 * and RTF_UP (if entry is linked, which is always true here).
1889 * Given that, use nhop rtflags & add RTF_UP.
1891 rtm->rtm_flags = rtflags | RTF_UP;
1892 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
1893 rtm->rtm_flags = RTF_GATEWAY |
1894 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
1895 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
1896 rtm->rtm_rmx.rmx_weight = weight;
1897 rtm->rtm_index = nh->nh_ifp->if_index;
1898 rtm->rtm_addrs = info.rti_addrs;
1899 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1906 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1907 struct rt_addrinfo *info, struct walkarg *w, int len)
1909 struct if_msghdrl *ifm;
1910 struct if_data *ifd;
1912 ifm = (struct if_msghdrl *)w->w_tmem;
1914 #ifdef COMPAT_FREEBSD32
1915 if (w->w_req->flags & SCTL_MASK32) {
1916 struct if_msghdrl32 *ifm32;
1918 ifm32 = (struct if_msghdrl32 *)ifm;
1919 ifm32->ifm_addrs = info->rti_addrs;
1920 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1921 ifm32->ifm_index = ifp->if_index;
1922 ifm32->_ifm_spare1 = 0;
1923 ifm32->ifm_len = sizeof(*ifm32);
1924 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1925 ifm32->_ifm_spare2 = 0;
1926 ifd = &ifm32->ifm_data;
1930 ifm->ifm_addrs = info->rti_addrs;
1931 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1932 ifm->ifm_index = ifp->if_index;
1933 ifm->_ifm_spare1 = 0;
1934 ifm->ifm_len = sizeof(*ifm);
1935 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1936 ifm->_ifm_spare2 = 0;
1937 ifd = &ifm->ifm_data;
1940 memcpy(ifd, src_ifd, sizeof(*ifd));
1942 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1946 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1947 struct rt_addrinfo *info, struct walkarg *w, int len)
1949 struct if_msghdr *ifm;
1950 struct if_data *ifd;
1952 ifm = (struct if_msghdr *)w->w_tmem;
1954 #ifdef COMPAT_FREEBSD32
1955 if (w->w_req->flags & SCTL_MASK32) {
1956 struct if_msghdr32 *ifm32;
1958 ifm32 = (struct if_msghdr32 *)ifm;
1959 ifm32->ifm_addrs = info->rti_addrs;
1960 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1961 ifm32->ifm_index = ifp->if_index;
1962 ifm32->_ifm_spare1 = 0;
1963 ifd = &ifm32->ifm_data;
1967 ifm->ifm_addrs = info->rti_addrs;
1968 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1969 ifm->ifm_index = ifp->if_index;
1970 ifm->_ifm_spare1 = 0;
1971 ifd = &ifm->ifm_data;
1974 memcpy(ifd, src_ifd, sizeof(*ifd));
1976 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1980 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1981 struct walkarg *w, int len)
1983 struct ifa_msghdrl *ifam;
1984 struct if_data *ifd;
1986 ifam = (struct ifa_msghdrl *)w->w_tmem;
1988 #ifdef COMPAT_FREEBSD32
1989 if (w->w_req->flags & SCTL_MASK32) {
1990 struct ifa_msghdrl32 *ifam32;
1992 ifam32 = (struct ifa_msghdrl32 *)ifam;
1993 ifam32->ifam_addrs = info->rti_addrs;
1994 ifam32->ifam_flags = ifa->ifa_flags;
1995 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1996 ifam32->_ifam_spare1 = 0;
1997 ifam32->ifam_len = sizeof(*ifam32);
1998 ifam32->ifam_data_off =
1999 offsetof(struct ifa_msghdrl32, ifam_data);
2000 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2001 ifd = &ifam32->ifam_data;
2005 ifam->ifam_addrs = info->rti_addrs;
2006 ifam->ifam_flags = ifa->ifa_flags;
2007 ifam->ifam_index = ifa->ifa_ifp->if_index;
2008 ifam->_ifam_spare1 = 0;
2009 ifam->ifam_len = sizeof(*ifam);
2010 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2011 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2012 ifd = &ifam->ifam_data;
2015 bzero(ifd, sizeof(*ifd));
2016 ifd->ifi_datalen = sizeof(struct if_data);
2017 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2018 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2019 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2020 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2022 /* Fixup if_data carp(4) vhid. */
2023 if (carp_get_vhid_p != NULL)
2024 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2026 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2030 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2031 struct walkarg *w, int len)
2033 struct ifa_msghdr *ifam;
2035 ifam = (struct ifa_msghdr *)w->w_tmem;
2036 ifam->ifam_addrs = info->rti_addrs;
2037 ifam->ifam_flags = ifa->ifa_flags;
2038 ifam->ifam_index = ifa->ifa_ifp->if_index;
2039 ifam->_ifam_spare1 = 0;
2040 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2042 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2046 sysctl_iflist(int af, struct walkarg *w)
2051 struct rt_addrinfo info;
2053 struct sockaddr_storage ss;
2055 bzero((caddr_t)&info, sizeof(info));
2056 bzero(&ifd, sizeof(ifd));
2057 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2058 if (w->w_arg && w->w_arg != ifp->if_index)
2060 if_data_copy(ifp, &ifd);
2062 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2063 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2066 info.rti_info[RTAX_IFP] = NULL;
2067 if (w->w_req && w->w_tmem) {
2068 if (w->w_op == NET_RT_IFLISTL)
2069 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2072 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2077 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2078 if (af && af != ifa->ifa_addr->sa_family)
2080 if (prison_if(w->w_req->td->td_ucred,
2081 ifa->ifa_addr) != 0)
2083 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2084 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2085 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2086 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2087 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2090 if (w->w_req && w->w_tmem) {
2091 if (w->w_op == NET_RT_IFLISTL)
2092 error = sysctl_iflist_ifaml(ifa, &info,
2095 error = sysctl_iflist_ifam(ifa, &info,
2101 info.rti_info[RTAX_IFA] = NULL;
2102 info.rti_info[RTAX_NETMASK] = NULL;
2103 info.rti_info[RTAX_BRD] = NULL;
2110 sysctl_ifmalist(int af, struct walkarg *w)
2112 struct rt_addrinfo info;
2114 struct ifmultiaddr *ifma;
2121 bzero((caddr_t)&info, sizeof(info));
2123 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2124 if (w->w_arg && w->w_arg != ifp->if_index)
2127 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2128 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2129 if (af && af != ifma->ifma_addr->sa_family)
2131 if (prison_if(w->w_req->td->td_ucred,
2132 ifma->ifma_addr) != 0)
2134 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2135 info.rti_info[RTAX_GATEWAY] =
2136 (ifma->ifma_addr->sa_family != AF_LINK) ?
2137 ifma->ifma_lladdr : NULL;
2138 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2141 if (w->w_req && w->w_tmem) {
2142 struct ifma_msghdr *ifmam;
2144 ifmam = (struct ifma_msghdr *)w->w_tmem;
2145 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2146 ifmam->ifmam_flags = 0;
2147 ifmam->ifmam_addrs = info.rti_addrs;
2148 ifmam->_ifmam_spare1 = 0;
2149 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2161 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2163 union sockaddr_union sa_dst, sa_mask;
2166 w->dst = (struct sockaddr *)&sa_dst;
2167 w->mask = (struct sockaddr *)&sa_mask;
2169 init_sockaddrs_family(family, w->dst, w->mask);
2171 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2175 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2177 struct epoch_tracker et;
2178 int *name = (int *)arg1;
2179 u_int namelen = arg2;
2180 struct rib_head *rnh = NULL; /* silence compiler. */
2181 int i, lim, error = EINVAL;
2190 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2192 fib = req->td->td_proc->p_fibnum;
2193 else if (namelen == 4)
2194 fib = (name[3] == RT_ALL_FIBS) ?
2195 req->td->td_proc->p_fibnum : name[3];
2197 return ((namelen < 3) ? EISDIR : ENOTDIR);
2198 if (fib < 0 || fib >= rt_numfibs)
2200 } else if (namelen != 3)
2201 return ((namelen < 3) ? EISDIR : ENOTDIR);
2205 bzero(&w, sizeof(w));
2210 error = sysctl_wire_old_buffer(req, 0);
2215 * Allocate reply buffer in advance.
2216 * All rtsock messages has maximum length of u_short.
2218 w.w_tmemsize = 65536;
2219 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2221 NET_EPOCH_ENTER(et);
2225 if (af == 0) { /* dump all tables */
2228 } else /* dump only one table */
2232 * take care of llinfo entries, the caller must
2235 if (w.w_op == NET_RT_FLAGS &&
2236 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2238 error = lltable_sysctl_dumparp(af, w.w_req);
2244 * take care of routing entries
2246 for (error = 0; error == 0 && i <= lim; i++) {
2247 rnh = rt_tables_get_rnh(fib, i);
2249 rtable_sysctl_dump(fib, i, &w);
2251 error = EAFNOSUPPORT;
2256 /* Allow dumping one specific af/fib at a time */
2262 if (fib < 0 || fib > rt_numfibs) {
2266 rnh = rt_tables_get_rnh(fib, af);
2268 error = EAFNOSUPPORT;
2271 if (w.w_op == NET_RT_NHOP)
2272 error = nhops_dump_sysctl(rnh, w.w_req);
2275 error = nhgrp_dump_sysctl(rnh, w.w_req);
2281 case NET_RT_IFLISTL:
2282 error = sysctl_iflist(af, &w);
2285 case NET_RT_IFMALIST:
2286 error = sysctl_ifmalist(af, &w);
2291 free(w.w_tmem, M_TEMP);
2295 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2296 sysctl_rtsock, "Return route tables and interface/address lists");
2299 * Definitions of protocols supported in the ROUTE domain.
2302 static struct domain routedomain; /* or at least forward */
2304 static struct protosw routesw[] = {
2306 .pr_type = SOCK_RAW,
2307 .pr_domain = &routedomain,
2308 .pr_flags = PR_ATOMIC|PR_ADDR,
2309 .pr_output = route_output,
2310 .pr_ctlinput = raw_ctlinput,
2311 .pr_init = raw_init,
2312 .pr_usrreqs = &route_usrreqs
2316 static struct domain routedomain = {
2317 .dom_family = PF_ROUTE,
2318 .dom_name = "route",
2319 .dom_protosw = routesw,
2320 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2323 VNET_DOMAIN_SET(route);