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/in6_var.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/scope6_var.h>
77 #include <net/route/nhop.h>
79 #ifdef COMPAT_FREEBSD32
80 #include <sys/mount.h>
81 #include <compat/freebsd32/freebsd32.h>
91 struct if_data ifm_data;
101 uint16_t _ifm_spare1;
103 uint16_t ifm_data_off;
104 uint32_t _ifm_spare2;
105 struct if_data ifm_data;
108 struct ifa_msghdrl32 {
109 uint16_t ifam_msglen;
110 uint8_t ifam_version;
115 uint16_t _ifam_spare1;
117 uint16_t ifam_data_off;
119 struct if_data ifam_data;
122 #define SA_SIZE32(sa) \
123 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
125 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
127 #endif /* COMPAT_FREEBSD32 */
129 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
131 /* NB: these are not modified */
132 static struct sockaddr route_src = { 2, PF_ROUTE, };
133 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
135 /* These are external hooks for CARP. */
136 int (*carp_get_vhid_p)(struct ifaddr *);
139 * Used by rtsock/raw_input callback code to decide whether to filter the update
140 * notification to a socket bound to a particular FIB.
142 #define RTS_FILTER_FIB M_PROTO8
145 int ip_count; /* attached w/ AF_INET */
146 int ip6_count; /* attached w/ AF_INET6 */
147 int any_count; /* total attached */
149 VNET_DEFINE_STATIC(route_cb_t, route_cb);
150 #define V_route_cb VNET(route_cb)
152 struct mtx rtsock_mtx;
153 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
155 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
156 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
157 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
159 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
166 struct sysctl_req *w_req;
167 struct sockaddr *dst;
168 struct sockaddr *mask;
171 static void rts_input(struct mbuf *m);
172 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
173 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
174 struct walkarg *w, int *plen);
175 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
176 struct rt_addrinfo *rtinfo);
177 static int cleanup_xaddrs(struct rt_addrinfo *info);
178 static int sysctl_dumpentry(struct rtentry *rt, void *vw);
179 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
180 uint32_t weight, struct walkarg *w);
181 static int sysctl_iflist(int af, struct walkarg *w);
182 static int sysctl_ifmalist(int af, struct walkarg *w);
183 static int route_output(struct mbuf *m, struct socket *so, ...);
184 static void rt_getmetrics(const struct rtentry *rt,
185 const struct nhop_object *nh, struct rt_metrics *out);
186 static void rt_dispatch(struct mbuf *, sa_family_t);
187 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
188 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
189 static int update_rtm_from_rc(struct rt_addrinfo *info,
190 struct rt_msghdr **prtm, int alloc_len,
191 struct rib_cmd_info *rc, struct nhop_object *nh);
192 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
193 struct mbuf *m, sa_family_t saf, u_int fibnum,
195 static bool can_export_rte(struct ucred *td_ucred, bool rt_is_host,
196 const struct sockaddr *rt_dst);
198 static struct netisr_handler rtsock_nh = {
200 .nh_handler = rts_input,
201 .nh_proto = NETISR_ROUTE,
202 .nh_policy = NETISR_POLICY_SOURCE,
206 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
210 netisr_getqlimit(&rtsock_nh, &qlimit);
211 error = sysctl_handle_int(oidp, &qlimit, 0, req);
212 if (error || !req->newptr)
216 return (netisr_setqlimit(&rtsock_nh, qlimit));
218 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
219 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
220 0, 0, sysctl_route_netisr_maxqlen, "I",
221 "maximum routing socket dispatch queue length");
228 if (IS_DEFAULT_VNET(curvnet)) {
229 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
230 rtsock_nh.nh_qlimit = tmp;
231 netisr_register(&rtsock_nh);
235 netisr_register_vnet(&rtsock_nh);
238 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
243 vnet_rts_uninit(void)
246 netisr_unregister_vnet(&rtsock_nh);
248 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
253 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
258 KASSERT(m != NULL, ("%s: m is NULL", __func__));
259 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
260 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
262 /* No filtering requested. */
263 if ((m->m_flags & RTS_FILTER_FIB) == 0)
266 /* Check if it is a rts and the fib matches the one of the socket. */
267 fibnum = M_GETFIB(m);
268 if (proto->sp_family != PF_ROUTE ||
269 rp->rcb_socket == NULL ||
270 rp->rcb_socket->so_fibnum == fibnum)
273 /* Filtering requested and no match, the socket shall be skipped. */
278 rts_input(struct mbuf *m)
280 struct sockproto route_proto;
281 unsigned short *family;
284 route_proto.sp_family = PF_ROUTE;
285 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
287 family = (unsigned short *)(tag + 1);
288 route_proto.sp_protocol = *family;
289 m_tag_delete(m, tag);
291 route_proto.sp_protocol = 0;
293 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
297 * It really doesn't make any sense at all for this code to share much
298 * with raw_usrreq.c, since its functionality is so restricted. XXX
301 rts_abort(struct socket *so)
304 raw_usrreqs.pru_abort(so);
308 rts_close(struct socket *so)
311 raw_usrreqs.pru_close(so);
314 /* pru_accept is EOPNOTSUPP */
317 rts_attach(struct socket *so, int proto, struct thread *td)
322 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
325 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
327 so->so_pcb = (caddr_t)rp;
328 so->so_fibnum = td->td_proc->p_fibnum;
329 error = raw_attach(so, proto);
337 switch(rp->rcb_proto.sp_protocol) {
339 V_route_cb.ip_count++;
342 V_route_cb.ip6_count++;
345 V_route_cb.any_count++;
348 so->so_options |= SO_USELOOPBACK;
353 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
356 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
360 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
363 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
366 /* pru_connect2 is EOPNOTSUPP */
367 /* pru_control is EOPNOTSUPP */
370 rts_detach(struct socket *so)
372 struct rawcb *rp = sotorawcb(so);
374 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
377 switch(rp->rcb_proto.sp_protocol) {
379 V_route_cb.ip_count--;
382 V_route_cb.ip6_count--;
385 V_route_cb.any_count--;
387 raw_usrreqs.pru_detach(so);
391 rts_disconnect(struct socket *so)
394 return (raw_usrreqs.pru_disconnect(so));
397 /* pru_listen is EOPNOTSUPP */
400 rts_peeraddr(struct socket *so, struct sockaddr **nam)
403 return (raw_usrreqs.pru_peeraddr(so, nam));
406 /* pru_rcvd is EOPNOTSUPP */
407 /* pru_rcvoob is EOPNOTSUPP */
410 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
411 struct mbuf *control, struct thread *td)
414 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
417 /* pru_sense is null */
420 rts_shutdown(struct socket *so)
423 return (raw_usrreqs.pru_shutdown(so));
427 rts_sockaddr(struct socket *so, struct sockaddr **nam)
430 return (raw_usrreqs.pru_sockaddr(so, nam));
433 static struct pr_usrreqs route_usrreqs = {
434 .pru_abort = rts_abort,
435 .pru_attach = rts_attach,
436 .pru_bind = rts_bind,
437 .pru_connect = rts_connect,
438 .pru_detach = rts_detach,
439 .pru_disconnect = rts_disconnect,
440 .pru_peeraddr = rts_peeraddr,
441 .pru_send = rts_send,
442 .pru_shutdown = rts_shutdown,
443 .pru_sockaddr = rts_sockaddr,
444 .pru_close = rts_close,
447 #ifndef _SOCKADDR_UNION_DEFINED
448 #define _SOCKADDR_UNION_DEFINED
450 * The union of all possible address formats we handle.
452 union sockaddr_union {
454 struct sockaddr_in sin;
455 struct sockaddr_in6 sin6;
457 #endif /* _SOCKADDR_UNION_DEFINED */
460 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
461 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
463 #if defined(INET) || defined(INET6)
464 struct epoch_tracker et;
467 /* First, see if the returned address is part of the jail. */
468 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
469 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
473 switch (info->rti_info[RTAX_DST]->sa_family) {
483 * Try to find an address on the given outgoing interface
484 * that belongs to the jail.
487 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
490 if (sa->sa_family != AF_INET)
492 ia = ((struct sockaddr_in *)sa)->sin_addr;
493 if (prison_check_ip4(cred, &ia) == 0) {
501 * As a last resort return the 'default' jail address.
503 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
505 if (prison_get_ip4(cred, &ia) != 0)
508 bzero(&saun->sin, sizeof(struct sockaddr_in));
509 saun->sin.sin_len = sizeof(struct sockaddr_in);
510 saun->sin.sin_family = AF_INET;
511 saun->sin.sin_addr.s_addr = ia.s_addr;
512 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
525 * Try to find an address on the given outgoing interface
526 * that belongs to the jail.
529 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
532 if (sa->sa_family != AF_INET6)
534 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
535 &ia6, sizeof(struct in6_addr));
536 if (prison_check_ip6(cred, &ia6) == 0) {
544 * As a last resort return the 'default' jail address.
546 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
548 if (prison_get_ip6(cred, &ia6) != 0)
551 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
552 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
553 saun->sin6.sin6_family = AF_INET6;
554 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
555 if (sa6_recoverscope(&saun->sin6) != 0)
557 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
568 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
573 if (V_loif == NULL) {
574 printf("Unable to add blackhole/reject nhop without loopback");
577 info->rti_ifp = V_loif;
579 saf = info->rti_info[RTAX_DST]->sa_family;
581 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
582 if (ifa->ifa_addr->sa_family == saf) {
587 if (info->rti_ifa == NULL)
590 bzero(saun, sizeof(union sockaddr_union));
594 saun->sin.sin_family = AF_INET;
595 saun->sin.sin_len = sizeof(struct sockaddr_in);
596 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
601 saun->sin6.sin6_family = AF_INET6;
602 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
603 saun->sin6.sin6_addr = in6addr_loopback;
609 info->rti_info[RTAX_GATEWAY] = &saun->sa;
610 info->rti_flags |= RTF_GATEWAY;
616 * Fills in @info based on userland-provided @rtm message.
618 * Returns 0 on success.
621 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info)
626 rtm->rtm_pid = curproc->p_pid;
627 info->rti_addrs = rtm->rtm_addrs;
629 info->rti_mflags = rtm->rtm_inits;
630 info->rti_rmx = &rtm->rtm_rmx;
633 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
634 * link-local address because rtrequest requires addresses with
637 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
640 info->rti_flags = rtm->rtm_flags;
641 error = cleanup_xaddrs(info);
644 saf = info->rti_info[RTAX_DST]->sa_family;
646 * Verify that the caller has the appropriate privilege; RTM_GET
647 * is the only operation the non-superuser is allowed.
649 if (rtm->rtm_type != RTM_GET) {
650 error = priv_check(curthread, PRIV_NET_ROUTE);
656 * The given gateway address may be an interface address.
657 * For example, issuing a "route change" command on a route
658 * entry that was created from a tunnel, and the gateway
659 * address given is the local end point. In this case the
660 * RTF_GATEWAY flag must be cleared or the destination will
661 * not be reachable even though there is no error message.
663 if (info->rti_info[RTAX_GATEWAY] != NULL &&
664 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
665 struct rt_addrinfo ginfo;
666 struct sockaddr *gdst;
667 struct sockaddr_storage ss;
669 bzero(&ginfo, sizeof(ginfo));
670 bzero(&ss, sizeof(ss));
671 ss.ss_len = sizeof(ss);
673 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
674 gdst = info->rti_info[RTAX_GATEWAY];
677 * A host route through the loopback interface is
678 * installed for each interface adddress. In pre 8.0
679 * releases the interface address of a PPP link type
680 * is not reachable locally. This behavior is fixed as
681 * part of the new L2/L3 redesign and rewrite work. The
682 * signature of this interface address route is the
683 * AF_LINK sa_family type of the gateway, and the
684 * rt_ifp has the IFF_LOOPBACK flag set.
686 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
687 if (ss.ss_family == AF_LINK &&
688 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
689 info->rti_flags &= ~RTF_GATEWAY;
690 info->rti_flags |= RTF_GWFLAG_COMPAT;
692 rib_free_info(&ginfo);
699 static struct nhop_object *
700 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
702 if (!NH_IS_NHGRP(nh))
705 struct weightened_nhop *wn;
707 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
710 for (int i = 0; i < num_nhops; i++) {
711 if (match_nhop_gw(wn[i].nh, gw))
719 * Handles RTM_GET message from routing socket, returning matching rt.
722 * 0 on success, with locked and referenced matching rt in @rt_nrt
726 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
727 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
730 struct rib_head *rnh;
731 struct nhop_object *nh;
734 saf = info->rti_info[RTAX_DST]->sa_family;
736 rnh = rt_tables_get_rnh(fibnum, saf);
738 return (EAFNOSUPPORT);
743 * By (implicit) convention host route (one without netmask)
744 * means longest-prefix-match request and the route with netmask
745 * means exact-match lookup.
746 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
747 * prefixes, use original data to check for the netmask presence.
749 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
751 * Provide longest prefix match for
752 * address lookup (no mask).
753 * 'route -n get addr'
755 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
756 info->rti_info[RTAX_DST], &rnh->head);
758 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
759 info->rti_info[RTAX_DST],
760 info->rti_info[RTAX_NETMASK], &rnh->head);
762 if (rc->rc_rt == NULL) {
767 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
773 * If performing proxied L2 entry insertion, and
774 * the actual PPP host entry is found, perform
775 * another search to retrieve the prefix route of
776 * the local end point of the PPP link.
777 * TODO: move this logic to userland.
779 if (rtm->rtm_flags & RTF_ANNOUNCE) {
780 struct sockaddr laddr;
782 if (nh->nh_ifp != NULL &&
783 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
786 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
789 rt_maskedcopy(ifa->ifa_addr,
793 rt_maskedcopy(nh->nh_ifa->ifa_addr,
795 nh->nh_ifa->ifa_netmask);
797 * refactor rt and no lock operation necessary
799 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
801 if (rc->rc_rt == NULL) {
805 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
812 rc->rc_nh_weight = rc->rc_rt->rt_weight;
819 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
822 if (family == AF_INET) {
823 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
824 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
826 bzero(dst4, sizeof(struct sockaddr_in));
827 bzero(mask4, sizeof(struct sockaddr_in));
829 dst4->sin_family = AF_INET;
830 dst4->sin_len = sizeof(struct sockaddr_in);
831 mask4->sin_family = AF_INET;
832 mask4->sin_len = sizeof(struct sockaddr_in);
836 if (family == AF_INET6) {
837 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
838 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
840 bzero(dst6, sizeof(struct sockaddr_in6));
841 bzero(mask6, sizeof(struct sockaddr_in6));
843 dst6->sin6_family = AF_INET6;
844 dst6->sin6_len = sizeof(struct sockaddr_in6);
845 mask6->sin6_family = AF_INET6;
846 mask6->sin6_len = sizeof(struct sockaddr_in6);
852 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
853 struct sockaddr *mask)
856 if (dst->sa_family == AF_INET) {
857 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
858 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
859 uint32_t scopeid = 0;
860 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
866 if (dst->sa_family == AF_INET6) {
867 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
868 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
869 uint32_t scopeid = 0;
870 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
871 &mask6->sin6_addr, &scopeid);
872 dst6->sin6_scope_id = scopeid;
880 * Update sockaddrs, flags, etc in @prtm based on @rc data.
881 * rtm can be reallocated.
883 * Returns 0 on success, along with pointer to (potentially reallocated)
888 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
889 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
892 union sockaddr_union saun;
893 struct rt_msghdr *rtm, *orig_rtm = NULL;
898 union sockaddr_union sa_dst, sa_mask;
899 int family = info->rti_info[RTAX_DST]->sa_family;
900 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
901 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
903 info->rti_info[RTAX_DST] = &sa_dst.sa;
904 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
905 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
906 info->rti_info[RTAX_GENMASK] = 0;
908 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
910 info->rti_info[RTAX_IFP] =
911 ifp->if_addr->ifa_addr;
912 error = rtm_get_jailed(info, ifp, nh,
913 &saun, curthread->td_ucred);
916 if (ifp->if_flags & IFF_POINTOPOINT)
917 info->rti_info[RTAX_BRD] =
918 nh->nh_ifa->ifa_dstaddr;
919 rtm->rtm_index = ifp->if_index;
921 info->rti_info[RTAX_IFP] = NULL;
922 info->rti_info[RTAX_IFA] = NULL;
924 } else if (ifp != NULL)
925 rtm->rtm_index = ifp->if_index;
927 /* Check if we need to realloc storage */
928 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
929 if (len > alloc_len) {
930 struct rt_msghdr *tmp_rtm;
932 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
935 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
941 * Delay freeing original rtm as info contains
942 * data referencing it.
946 w.w_tmem = (caddr_t)rtm;
947 w.w_tmemsize = alloc_len;
948 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
950 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
951 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
952 rtm->rtm_flags = RTF_GATEWAY |
953 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
954 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
955 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
956 rtm->rtm_addrs = info->rti_addrs;
958 if (orig_rtm != NULL)
959 free(orig_rtm, M_TEMP);
967 save_del_notification(struct rib_cmd_info *rc, void *_cbdata)
969 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
971 if (rc->rc_cmd == RTM_DELETE)
976 save_add_notification(struct rib_cmd_info *rc, void *_cbdata)
978 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
980 if (rc->rc_cmd == RTM_ADD)
987 route_output(struct mbuf *m, struct socket *so, ...)
989 struct rt_msghdr *rtm = NULL;
990 struct rtentry *rt = NULL;
991 struct rt_addrinfo info;
992 struct epoch_tracker et;
994 struct sockaddr_storage ss;
995 struct sockaddr_in6 *sin6;
996 int i, rti_need_deembed = 0;
998 int alloc_len = 0, len, error = 0, fibnum;
999 sa_family_t saf = AF_UNSPEC;
1000 struct rib_cmd_info rc;
1001 struct nhop_object *nh;
1003 fibnum = so->so_fibnum;
1004 #define senderr(e) { error = e; goto flush;}
1005 if (m == NULL || ((m->m_len < sizeof(long)) &&
1006 (m = m_pullup(m, sizeof(long))) == NULL))
1008 if ((m->m_flags & M_PKTHDR) == 0)
1009 panic("route_output");
1010 NET_EPOCH_ENTER(et);
1011 len = m->m_pkthdr.len;
1012 if (len < sizeof(*rtm) ||
1013 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1017 * Most of current messages are in range 200-240 bytes,
1018 * minimize possible re-allocation on reply using larger size
1019 * buffer aligned on 1k boundaty.
1021 alloc_len = roundup2(len, 1024);
1022 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
1025 m_copydata(m, 0, len, (caddr_t)rtm);
1026 bzero(&info, sizeof(info));
1029 if (rtm->rtm_version != RTM_VERSION) {
1030 /* Do not touch message since format is unknown */
1033 senderr(EPROTONOSUPPORT);
1037 * Starting from here, it is possible
1038 * to alter original message and insert
1039 * caller PID and error value.
1042 if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) {
1046 saf = info.rti_info[RTAX_DST]->sa_family;
1048 /* support for new ARP code */
1049 if (rtm->rtm_flags & RTF_LLDATA) {
1050 error = lla_rt_output(rtm, &info);
1053 rti_need_deembed = 1;
1058 union sockaddr_union gw_saun;
1059 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1060 if (blackhole_flags != 0) {
1061 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1062 error = fill_blackholeinfo(&info, &gw_saun);
1067 /* TODO: rebuild rtm from scratch */
1070 switch (rtm->rtm_type) {
1073 if (rtm->rtm_type == RTM_ADD) {
1074 if (info.rti_info[RTAX_GATEWAY] == NULL)
1077 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1080 rti_need_deembed = 1;
1083 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1084 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1085 struct rib_cmd_info rc_simple = {};
1086 rib_decompose_notification(&rc,
1087 save_add_notification, (void *)&rc_simple);
1092 rtm->rtm_index = nh->nh_ifp->if_index;
1093 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1098 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1101 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1102 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1103 struct rib_cmd_info rc_simple = {};
1104 rib_decompose_notification(&rc,
1105 save_del_notification, (void *)&rc_simple);
1113 /* rt_msg2() will not be used when RTM_DELETE fails. */
1114 rti_need_deembed = 1;
1119 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1125 if (!can_export_rte(curthread->td_ucred,
1126 info.rti_info[RTAX_NETMASK] == NULL,
1127 info.rti_info[RTAX_DST])) {
1131 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1133 * Note that some sockaddr pointers may have changed to
1134 * point to memory outsize @rtm. Some may be pointing
1135 * to the on-stack variables.
1136 * Given that, any pointer in @info CANNOT BE USED.
1140 * scopeid deembedding has been performed while
1141 * writing updated rtm in rtsock_msg_buffer().
1142 * With that in mind, skip deembedding procedure below.
1145 rti_need_deembed = 0;
1152 senderr(EOPNOTSUPP);
1161 if (rti_need_deembed) {
1162 /* sin6_scope_id is recovered before sending rtm. */
1163 sin6 = (struct sockaddr_in6 *)&ss;
1164 for (i = 0; i < RTAX_MAX; i++) {
1165 if (info.rti_info[i] == NULL)
1167 if (info.rti_info[i]->sa_family != AF_INET6)
1169 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1170 if (sa6_recoverscope(sin6) == 0)
1171 bcopy(sin6, info.rti_info[i],
1177 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1183 * Sends the prepared reply message in @rtm to all rtsock clients.
1184 * Frees @m and @rtm.
1188 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1189 sa_family_t saf, u_int fibnum, int rtm_errno)
1191 struct rawcb *rp = NULL;
1194 * Check to see if we don't want our own messages.
1196 if ((so->so_options & SO_USELOOPBACK) == 0) {
1197 if (V_route_cb.any_count <= 1) {
1203 /* There is another listener, so construct message */
1209 rtm->rtm_errno = rtm_errno;
1211 rtm->rtm_flags |= RTF_DONE;
1213 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1214 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1217 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1218 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1223 M_SETFIB(m, fibnum);
1224 m->m_flags |= RTS_FILTER_FIB;
1227 * XXX insure we don't get a copy by
1228 * invalidating our protocol
1230 unsigned short family = rp->rcb_proto.sp_family;
1231 rp->rcb_proto.sp_family = 0;
1232 rt_dispatch(m, saf);
1233 rp->rcb_proto.sp_family = family;
1235 rt_dispatch(m, saf);
1240 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1241 struct rt_metrics *out)
1244 bzero(out, sizeof(*out));
1245 out->rmx_mtu = nh->nh_mtu;
1246 out->rmx_weight = rt->rt_weight;
1247 out->rmx_nhidx = nhop_get_idx(nh);
1248 /* Kernel -> userland timebase conversion. */
1249 out->rmx_expire = rt->rt_expire ?
1250 rt->rt_expire - time_uptime + time_second : 0;
1254 * Extract the addresses of the passed sockaddrs.
1255 * Do a little sanity checking so as to avoid bad memory references.
1256 * This data is derived straight from userland.
1259 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1261 struct sockaddr *sa;
1264 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1265 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1267 sa = (struct sockaddr *)cp;
1271 if (cp + sa->sa_len > cplim)
1274 * there are no more.. quit now
1275 * If there are more bits, they are in error.
1276 * I've seen this. route(1) can evidently generate these.
1277 * This causes kernel to core dump.
1278 * for compatibility, If we see this, point to a safe address.
1280 if (sa->sa_len == 0) {
1281 rtinfo->rti_info[i] = &sa_zero;
1282 return (0); /* should be EINVAL but for compat */
1286 if (sa->sa_family == AF_INET6)
1287 sa6_embedscope((struct sockaddr_in6 *)sa,
1290 rtinfo->rti_info[i] = sa;
1298 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1301 const struct sockaddr_in nsin = {
1302 .sin_family = AF_INET,
1303 .sin_len = sizeof(struct sockaddr_in),
1312 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1316 const struct sockaddr_in6 nsin6 = {
1317 .sin6_family = AF_INET6,
1318 .sin6_len = sizeof(struct sockaddr_in6),
1319 .sin6_addr = *addr6,
1320 .sin6_scope_id = scopeid,
1327 * Checks if gateway is suitable for lltable operations.
1328 * Lltable code requires AF_LINK gateway with ifindex
1329 * and mac address specified.
1330 * Returns 0 on success.
1333 cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1335 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1337 if (sdl->sdl_family != AF_LINK)
1340 if (sdl->sdl_index == 0)
1343 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len)
1350 cleanup_xaddrs_gateway(struct rt_addrinfo *info)
1352 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1354 if (info->rti_flags & RTF_LLDATA)
1355 return (cleanup_xaddrs_lladdr(info));
1357 switch (gw->sa_family) {
1361 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1362 if (gw_sin->sin_len < sizeof(struct sockaddr_in)) {
1363 printf("gw sin_len too small\n");
1366 fill_sockaddr_inet(gw_sin, gw_sin->sin_addr);
1373 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1374 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1375 printf("gw sin6_len too small\n");
1378 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1384 struct sockaddr_dl_short *gw_sdl;
1386 gw_sdl = (struct sockaddr_dl_short *)gw;
1387 if (gw_sdl->sdl_len < sizeof(struct sockaddr_dl_short)) {
1388 printf("gw sdl_len too small\n");
1392 const struct sockaddr_dl_short sdl = {
1393 .sdl_family = AF_LINK,
1394 .sdl_len = sizeof(struct sockaddr_dl_short),
1395 .sdl_index = gw_sdl->sdl_index,
1406 remove_netmask(struct rt_addrinfo *info)
1408 info->rti_info[RTAX_NETMASK] = NULL;
1409 info->rti_flags |= RTF_HOST;
1410 info->rti_addrs &= ~RTA_NETMASK;
1415 cleanup_xaddrs_inet(struct rt_addrinfo *info)
1417 struct sockaddr_in *dst_sa, *mask_sa;
1419 /* Check & fixup dst/netmask combination first */
1420 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1421 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1423 struct in_addr mask = {
1424 .s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST,
1426 struct in_addr dst = {
1427 .s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr))
1430 if (dst_sa->sin_len < sizeof(struct sockaddr_in)) {
1431 printf("dst sin_len too small\n");
1434 if (mask_sa && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1435 printf("mask sin_len too small\n");
1438 fill_sockaddr_inet(dst_sa, dst);
1440 if (mask.s_addr != INADDR_BROADCAST)
1441 fill_sockaddr_inet(mask_sa, mask);
1443 remove_netmask(info);
1446 if (info->rti_info[RTAX_GATEWAY] != NULL)
1447 return (cleanup_xaddrs_gateway(info));
1455 cleanup_xaddrs_inet6(struct rt_addrinfo *info)
1457 struct sockaddr_in6 *dst_sa, *mask_sa;
1458 struct in6_addr mask;
1460 /* Check & fixup dst/netmask combination first */
1461 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1462 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1464 mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1465 IN6_MASK_ADDR(&dst_sa->sin6_addr, &mask);
1467 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1468 printf("dst sin6_len too small\n");
1471 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1472 printf("mask sin6_len too small\n");
1475 fill_sockaddr_inet6(dst_sa, &dst_sa->sin6_addr, 0);
1477 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128))
1478 fill_sockaddr_inet6(mask_sa, &mask, 0);
1480 remove_netmask(info);
1483 if (info->rti_info[RTAX_GATEWAY] != NULL)
1484 return (cleanup_xaddrs_gateway(info));
1491 cleanup_xaddrs(struct rt_addrinfo *info)
1493 int error = EAFNOSUPPORT;
1495 if (info->rti_info[RTAX_DST] == NULL)
1498 if (info->rti_flags & RTF_LLDATA) {
1500 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1501 * prefix along with the actual address in RTA_DST.
1502 * Remove netmask to avoid unnecessary address masking.
1504 remove_netmask(info);
1507 switch (info->rti_info[RTAX_DST]->sa_family) {
1510 error = cleanup_xaddrs_inet(info);
1515 error = cleanup_xaddrs_inet6(info);
1524 * Fill in @dmask with valid netmask leaving original @smask
1525 * intact. Mostly used with radix netmasks.
1528 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1529 struct sockaddr_storage *dmask)
1531 if (dst == NULL || smask == NULL)
1534 memset(dmask, 0, dst->sa_len);
1535 memcpy(dmask, smask, smask->sa_len);
1536 dmask->ss_len = dst->sa_len;
1537 dmask->ss_family = dst->sa_family;
1539 return ((struct sockaddr *)dmask);
1543 * Writes information related to @rtinfo object to newly-allocated mbuf.
1544 * Assumes MCLBYTES is enough to construct any message.
1545 * Used for OS notifications of vaious events (if/ifa announces,etc)
1547 * Returns allocated mbuf or NULL on failure.
1549 static struct mbuf *
1550 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1552 struct sockaddr_storage ss;
1553 struct rt_msghdr *rtm;
1556 struct sockaddr *sa;
1558 struct sockaddr_in6 *sin6;
1565 len = sizeof(struct ifa_msghdr);
1570 len = sizeof(struct ifma_msghdr);
1574 len = sizeof(struct if_msghdr);
1577 case RTM_IFANNOUNCE:
1579 len = sizeof(struct if_announcemsghdr);
1583 len = sizeof(struct rt_msghdr);
1586 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1587 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1589 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1591 m = m_gethdr(M_NOWAIT, MT_DATA);
1595 m->m_pkthdr.len = m->m_len = len;
1596 rtm = mtod(m, struct rt_msghdr *);
1597 bzero((caddr_t)rtm, len);
1598 for (i = 0; i < RTAX_MAX; i++) {
1599 if ((sa = rtinfo->rti_info[i]) == NULL)
1601 rtinfo->rti_addrs |= (1 << i);
1604 KASSERT(dlen <= sizeof(ss),
1605 ("%s: sockaddr size overflow", __func__));
1606 bzero(&ss, sizeof(ss));
1607 bcopy(sa, &ss, sa->sa_len);
1608 sa = (struct sockaddr *)&ss;
1610 if (sa->sa_family == AF_INET6) {
1611 sin6 = (struct sockaddr_in6 *)sa;
1612 (void)sa6_recoverscope(sin6);
1615 m_copyback(m, len, dlen, (caddr_t)sa);
1618 if (m->m_pkthdr.len != len) {
1622 rtm->rtm_msglen = len;
1623 rtm->rtm_version = RTM_VERSION;
1624 rtm->rtm_type = type;
1629 * Writes information related to @rtinfo object to preallocated buffer.
1630 * Stores needed size in @plen. If @w is NULL, calculates size without
1632 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1634 * Returns 0 on success.
1638 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1640 struct sockaddr_storage ss;
1641 int len, buflen = 0, dlen, i;
1643 struct rt_msghdr *rtm = NULL;
1645 struct sockaddr_in6 *sin6;
1647 #ifdef COMPAT_FREEBSD32
1648 bool compat32 = false;
1654 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1655 #ifdef COMPAT_FREEBSD32
1656 if (w->w_req->flags & SCTL_MASK32) {
1657 len = sizeof(struct ifa_msghdrl32);
1661 len = sizeof(struct ifa_msghdrl);
1663 len = sizeof(struct ifa_msghdr);
1667 #ifdef COMPAT_FREEBSD32
1668 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1669 if (w->w_op == NET_RT_IFLISTL)
1670 len = sizeof(struct if_msghdrl32);
1672 len = sizeof(struct if_msghdr32);
1677 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1678 len = sizeof(struct if_msghdrl);
1680 len = sizeof(struct if_msghdr);
1684 len = sizeof(struct ifma_msghdr);
1688 len = sizeof(struct rt_msghdr);
1692 rtm = (struct rt_msghdr *)w->w_tmem;
1693 buflen = w->w_tmemsize - len;
1694 cp = (caddr_t)w->w_tmem + len;
1697 rtinfo->rti_addrs = 0;
1698 for (i = 0; i < RTAX_MAX; i++) {
1699 struct sockaddr *sa;
1701 if ((sa = rtinfo->rti_info[i]) == NULL)
1703 rtinfo->rti_addrs |= (1 << i);
1704 #ifdef COMPAT_FREEBSD32
1706 dlen = SA_SIZE32(sa);
1710 if (cp != NULL && buflen >= dlen) {
1711 KASSERT(dlen <= sizeof(ss),
1712 ("%s: sockaddr size overflow", __func__));
1713 bzero(&ss, sizeof(ss));
1714 bcopy(sa, &ss, sa->sa_len);
1715 sa = (struct sockaddr *)&ss;
1717 if (sa->sa_family == AF_INET6) {
1718 sin6 = (struct sockaddr_in6 *)sa;
1719 (void)sa6_recoverscope(sin6);
1722 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1725 } else if (cp != NULL) {
1727 * Buffer too small. Count needed size
1728 * and return with error.
1737 dlen = ALIGN(len) - len;
1749 /* fill header iff buffer is large enough */
1750 rtm->rtm_version = RTM_VERSION;
1751 rtm->rtm_type = type;
1752 rtm->rtm_msglen = len;
1757 if (w != NULL && cp == NULL)
1764 * This routine is called to generate a message from the routing
1765 * socket indicating that a redirect has occurred, a routing lookup
1766 * has failed, or that a protocol has detected timeouts to a particular
1770 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1773 struct rt_msghdr *rtm;
1775 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1777 if (V_route_cb.any_count == 0)
1779 m = rtsock_msg_mbuf(type, rtinfo);
1783 if (fibnum != RT_ALL_FIBS) {
1784 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1785 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1786 M_SETFIB(m, fibnum);
1787 m->m_flags |= RTS_FILTER_FIB;
1790 rtm = mtod(m, struct rt_msghdr *);
1791 rtm->rtm_flags = RTF_DONE | flags;
1792 rtm->rtm_errno = error;
1793 rtm->rtm_addrs = rtinfo->rti_addrs;
1794 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1798 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1801 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1805 * This routine is called to generate a message from the routing
1806 * socket indicating that the status of a network interface has changed.
1809 rt_ifmsg(struct ifnet *ifp)
1811 struct if_msghdr *ifm;
1813 struct rt_addrinfo info;
1815 if (V_route_cb.any_count == 0)
1817 bzero((caddr_t)&info, sizeof(info));
1818 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1821 ifm = mtod(m, struct if_msghdr *);
1822 ifm->ifm_index = ifp->if_index;
1823 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1824 if_data_copy(ifp, &ifm->ifm_data);
1826 rt_dispatch(m, AF_UNSPEC);
1830 * Announce interface address arrival/withdraw.
1831 * Please do not call directly, use rt_addrmsg().
1832 * Assume input data to be valid.
1833 * Returns 0 on success.
1836 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1838 struct rt_addrinfo info;
1839 struct sockaddr *sa;
1842 struct ifa_msghdr *ifam;
1843 struct ifnet *ifp = ifa->ifa_ifp;
1844 struct sockaddr_storage ss;
1846 if (V_route_cb.any_count == 0)
1849 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1851 bzero((caddr_t)&info, sizeof(info));
1852 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1853 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1854 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1855 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1856 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1857 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1859 ifam = mtod(m, struct ifa_msghdr *);
1860 ifam->ifam_index = ifp->if_index;
1861 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1862 ifam->ifam_flags = ifa->ifa_flags;
1863 ifam->ifam_addrs = info.rti_addrs;
1865 if (fibnum != RT_ALL_FIBS) {
1866 M_SETFIB(m, fibnum);
1867 m->m_flags |= RTS_FILTER_FIB;
1870 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1876 * Announce route addition/removal to rtsock based on @rt data.
1877 * Callers are advives to use rt_routemsg() instead of using this
1878 * function directly.
1879 * Assume @rt data is consistent.
1881 * Returns 0 on success.
1884 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
1887 union sockaddr_union dst, mask;
1888 struct rt_addrinfo info;
1890 if (V_route_cb.any_count == 0)
1893 int family = rt_get_family(rt);
1894 init_sockaddrs_family(family, &dst.sa, &mask.sa);
1895 export_rtaddrs(rt, &dst.sa, &mask.sa);
1897 bzero((caddr_t)&info, sizeof(info));
1898 info.rti_info[RTAX_DST] = &dst.sa;
1899 info.rti_info[RTAX_NETMASK] = &mask.sa;
1900 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1901 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
1902 info.rti_ifp = nh->nh_ifp;
1904 return (rtsock_routemsg_info(cmd, &info, fibnum));
1908 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
1910 struct rt_msghdr *rtm;
1911 struct sockaddr *sa;
1914 if (V_route_cb.any_count == 0)
1917 if (info->rti_flags & RTF_HOST)
1918 info->rti_info[RTAX_NETMASK] = NULL;
1920 m = rtsock_msg_mbuf(cmd, info);
1924 if (fibnum != RT_ALL_FIBS) {
1925 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1926 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1927 M_SETFIB(m, fibnum);
1928 m->m_flags |= RTS_FILTER_FIB;
1931 rtm = mtod(m, struct rt_msghdr *);
1932 rtm->rtm_addrs = info->rti_addrs;
1933 if (info->rti_ifp != NULL)
1934 rtm->rtm_index = info->rti_ifp->if_index;
1935 /* Add RTF_DONE to indicate command 'completion' required by API */
1936 info->rti_flags |= RTF_DONE;
1937 /* Reported routes has to be up */
1938 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
1939 info->rti_flags |= RTF_UP;
1940 rtm->rtm_flags = info->rti_flags;
1942 sa = info->rti_info[RTAX_DST];
1943 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1949 * This is the analogue to the rt_newaddrmsg which performs the same
1950 * function but for multicast group memberhips. This is easier since
1951 * there is no route state to worry about.
1954 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1956 struct rt_addrinfo info;
1957 struct mbuf *m = NULL;
1958 struct ifnet *ifp = ifma->ifma_ifp;
1959 struct ifma_msghdr *ifmam;
1961 if (V_route_cb.any_count == 0)
1964 bzero((caddr_t)&info, sizeof(info));
1965 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1966 if (ifp && ifp->if_addr)
1967 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1969 info.rti_info[RTAX_IFP] = NULL;
1971 * If a link-layer address is present, present it as a ``gateway''
1972 * (similarly to how ARP entries, e.g., are presented).
1974 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1975 m = rtsock_msg_mbuf(cmd, &info);
1978 ifmam = mtod(m, struct ifma_msghdr *);
1979 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1981 ifmam->ifmam_index = ifp->if_index;
1982 ifmam->ifmam_addrs = info.rti_addrs;
1983 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1986 static struct mbuf *
1987 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1988 struct rt_addrinfo *info)
1990 struct if_announcemsghdr *ifan;
1993 if (V_route_cb.any_count == 0)
1995 bzero((caddr_t)info, sizeof(*info));
1996 m = rtsock_msg_mbuf(type, info);
1998 ifan = mtod(m, struct if_announcemsghdr *);
1999 ifan->ifan_index = ifp->if_index;
2000 strlcpy(ifan->ifan_name, ifp->if_xname,
2001 sizeof(ifan->ifan_name));
2002 ifan->ifan_what = what;
2008 * This is called to generate routing socket messages indicating
2009 * IEEE80211 wireless events.
2010 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2013 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2016 struct rt_addrinfo info;
2018 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2021 * Append the ieee80211 data. Try to stick it in the
2022 * mbuf containing the ifannounce msg; otherwise allocate
2023 * a new mbuf and append.
2025 * NB: we assume m is a single mbuf.
2027 if (data_len > M_TRAILINGSPACE(m)) {
2028 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2033 bcopy(data, mtod(n, void *), data_len);
2034 n->m_len = data_len;
2036 } else if (data_len > 0) {
2037 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2038 m->m_len += data_len;
2040 if (m->m_flags & M_PKTHDR)
2041 m->m_pkthdr.len += data_len;
2042 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2043 rt_dispatch(m, AF_UNSPEC);
2048 * This is called to generate routing socket messages indicating
2049 * network interface arrival and departure.
2052 rt_ifannouncemsg(struct ifnet *ifp, int what)
2055 struct rt_addrinfo info;
2057 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2059 rt_dispatch(m, AF_UNSPEC);
2063 rt_dispatch(struct mbuf *m, sa_family_t saf)
2068 * Preserve the family from the sockaddr, if any, in an m_tag for
2069 * use when injecting the mbuf into the routing socket buffer from
2072 if (saf != AF_UNSPEC) {
2073 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
2079 *(unsigned short *)(tag + 1) = saf;
2080 m_tag_prepend(m, tag);
2084 m->m_pkthdr.rcvif = V_loif;
2090 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
2094 * Checks if rte can be exported v.r.t jails/vnets.
2096 * Returns 1 if it can, 0 otherwise.
2099 can_export_rte(struct ucred *td_ucred, bool rt_is_host,
2100 const struct sockaddr *rt_dst)
2103 if ((!rt_is_host) ? jailed_without_vnet(td_ucred)
2104 : prison_if(td_ucred, rt_dst) != 0)
2111 * This is used in dumping the kernel table via sysctl().
2114 sysctl_dumpentry(struct rtentry *rt, void *vw)
2116 struct walkarg *w = vw;
2117 struct nhop_object *nh;
2122 export_rtaddrs(rt, w->dst, w->mask);
2123 if (!can_export_rte(w->w_req->td->td_ucred, rt_is_host(rt), w->dst))
2125 nh = rt_get_raw_nhop(rt);
2127 if (NH_IS_NHGRP(nh)) {
2128 struct weightened_nhop *wn;
2130 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2131 for (int i = 0; i < num_nhops; i++) {
2132 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2138 error = sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2145 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2148 struct rt_addrinfo info;
2149 int error = 0, size;
2152 rtflags = nhop_get_rtflags(nh);
2154 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2157 bzero((caddr_t)&info, sizeof(info));
2158 info.rti_info[RTAX_DST] = w->dst;
2159 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2160 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2161 info.rti_info[RTAX_GENMASK] = 0;
2162 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2163 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2164 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2165 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2166 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2168 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2170 if (w->w_req && w->w_tmem) {
2171 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2173 bzero(&rtm->rtm_index,
2174 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2177 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2178 * and RTF_UP (if entry is linked, which is always true here).
2179 * Given that, use nhop rtflags & add RTF_UP.
2181 rtm->rtm_flags = rtflags | RTF_UP;
2182 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2183 rtm->rtm_flags = RTF_GATEWAY |
2184 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2185 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2186 rtm->rtm_rmx.rmx_weight = weight;
2187 rtm->rtm_index = nh->nh_ifp->if_index;
2188 rtm->rtm_addrs = info.rti_addrs;
2189 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2196 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2197 struct rt_addrinfo *info, struct walkarg *w, int len)
2199 struct if_msghdrl *ifm;
2200 struct if_data *ifd;
2202 ifm = (struct if_msghdrl *)w->w_tmem;
2204 #ifdef COMPAT_FREEBSD32
2205 if (w->w_req->flags & SCTL_MASK32) {
2206 struct if_msghdrl32 *ifm32;
2208 ifm32 = (struct if_msghdrl32 *)ifm;
2209 ifm32->ifm_addrs = info->rti_addrs;
2210 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2211 ifm32->ifm_index = ifp->if_index;
2212 ifm32->_ifm_spare1 = 0;
2213 ifm32->ifm_len = sizeof(*ifm32);
2214 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2215 ifm32->_ifm_spare2 = 0;
2216 ifd = &ifm32->ifm_data;
2220 ifm->ifm_addrs = info->rti_addrs;
2221 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2222 ifm->ifm_index = ifp->if_index;
2223 ifm->_ifm_spare1 = 0;
2224 ifm->ifm_len = sizeof(*ifm);
2225 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2226 ifm->_ifm_spare2 = 0;
2227 ifd = &ifm->ifm_data;
2230 memcpy(ifd, src_ifd, sizeof(*ifd));
2232 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2236 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2237 struct rt_addrinfo *info, struct walkarg *w, int len)
2239 struct if_msghdr *ifm;
2240 struct if_data *ifd;
2242 ifm = (struct if_msghdr *)w->w_tmem;
2244 #ifdef COMPAT_FREEBSD32
2245 if (w->w_req->flags & SCTL_MASK32) {
2246 struct if_msghdr32 *ifm32;
2248 ifm32 = (struct if_msghdr32 *)ifm;
2249 ifm32->ifm_addrs = info->rti_addrs;
2250 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2251 ifm32->ifm_index = ifp->if_index;
2252 ifm32->_ifm_spare1 = 0;
2253 ifd = &ifm32->ifm_data;
2257 ifm->ifm_addrs = info->rti_addrs;
2258 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2259 ifm->ifm_index = ifp->if_index;
2260 ifm->_ifm_spare1 = 0;
2261 ifd = &ifm->ifm_data;
2264 memcpy(ifd, src_ifd, sizeof(*ifd));
2266 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2270 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2271 struct walkarg *w, int len)
2273 struct ifa_msghdrl *ifam;
2274 struct if_data *ifd;
2276 ifam = (struct ifa_msghdrl *)w->w_tmem;
2278 #ifdef COMPAT_FREEBSD32
2279 if (w->w_req->flags & SCTL_MASK32) {
2280 struct ifa_msghdrl32 *ifam32;
2282 ifam32 = (struct ifa_msghdrl32 *)ifam;
2283 ifam32->ifam_addrs = info->rti_addrs;
2284 ifam32->ifam_flags = ifa->ifa_flags;
2285 ifam32->ifam_index = ifa->ifa_ifp->if_index;
2286 ifam32->_ifam_spare1 = 0;
2287 ifam32->ifam_len = sizeof(*ifam32);
2288 ifam32->ifam_data_off =
2289 offsetof(struct ifa_msghdrl32, ifam_data);
2290 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2291 ifd = &ifam32->ifam_data;
2295 ifam->ifam_addrs = info->rti_addrs;
2296 ifam->ifam_flags = ifa->ifa_flags;
2297 ifam->ifam_index = ifa->ifa_ifp->if_index;
2298 ifam->_ifam_spare1 = 0;
2299 ifam->ifam_len = sizeof(*ifam);
2300 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2301 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2302 ifd = &ifam->ifam_data;
2305 bzero(ifd, sizeof(*ifd));
2306 ifd->ifi_datalen = sizeof(struct if_data);
2307 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2308 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2309 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2310 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2312 /* Fixup if_data carp(4) vhid. */
2313 if (carp_get_vhid_p != NULL)
2314 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2316 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2320 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2321 struct walkarg *w, int len)
2323 struct ifa_msghdr *ifam;
2325 ifam = (struct ifa_msghdr *)w->w_tmem;
2326 ifam->ifam_addrs = info->rti_addrs;
2327 ifam->ifam_flags = ifa->ifa_flags;
2328 ifam->ifam_index = ifa->ifa_ifp->if_index;
2329 ifam->_ifam_spare1 = 0;
2330 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2332 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2336 sysctl_iflist(int af, struct walkarg *w)
2341 struct rt_addrinfo info;
2343 struct sockaddr_storage ss;
2345 bzero((caddr_t)&info, sizeof(info));
2346 bzero(&ifd, sizeof(ifd));
2347 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2348 if (w->w_arg && w->w_arg != ifp->if_index)
2350 if_data_copy(ifp, &ifd);
2352 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2353 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2356 info.rti_info[RTAX_IFP] = NULL;
2357 if (w->w_req && w->w_tmem) {
2358 if (w->w_op == NET_RT_IFLISTL)
2359 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2362 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2367 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2368 if (af && af != ifa->ifa_addr->sa_family)
2370 if (prison_if(w->w_req->td->td_ucred,
2371 ifa->ifa_addr) != 0)
2373 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2374 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2375 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2376 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2377 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2380 if (w->w_req && w->w_tmem) {
2381 if (w->w_op == NET_RT_IFLISTL)
2382 error = sysctl_iflist_ifaml(ifa, &info,
2385 error = sysctl_iflist_ifam(ifa, &info,
2391 info.rti_info[RTAX_IFA] = NULL;
2392 info.rti_info[RTAX_NETMASK] = NULL;
2393 info.rti_info[RTAX_BRD] = NULL;
2400 sysctl_ifmalist(int af, struct walkarg *w)
2402 struct rt_addrinfo info;
2404 struct ifmultiaddr *ifma;
2411 bzero((caddr_t)&info, sizeof(info));
2413 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2414 if (w->w_arg && w->w_arg != ifp->if_index)
2417 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2418 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2419 if (af && af != ifma->ifma_addr->sa_family)
2421 if (prison_if(w->w_req->td->td_ucred,
2422 ifma->ifma_addr) != 0)
2424 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2425 info.rti_info[RTAX_GATEWAY] =
2426 (ifma->ifma_addr->sa_family != AF_LINK) ?
2427 ifma->ifma_lladdr : NULL;
2428 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2431 if (w->w_req && w->w_tmem) {
2432 struct ifma_msghdr *ifmam;
2434 ifmam = (struct ifma_msghdr *)w->w_tmem;
2435 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2436 ifmam->ifmam_flags = 0;
2437 ifmam->ifmam_addrs = info.rti_addrs;
2438 ifmam->_ifmam_spare1 = 0;
2439 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2451 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2453 union sockaddr_union sa_dst, sa_mask;
2456 w->dst = (struct sockaddr *)&sa_dst;
2457 w->mask = (struct sockaddr *)&sa_mask;
2459 init_sockaddrs_family(family, w->dst, w->mask);
2461 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2465 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2467 struct epoch_tracker et;
2468 int *name = (int *)arg1;
2469 u_int namelen = arg2;
2470 struct rib_head *rnh = NULL; /* silence compiler. */
2471 int i, lim, error = EINVAL;
2480 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2482 fib = req->td->td_proc->p_fibnum;
2483 else if (namelen == 4)
2484 fib = (name[3] == RT_ALL_FIBS) ?
2485 req->td->td_proc->p_fibnum : name[3];
2487 return ((namelen < 3) ? EISDIR : ENOTDIR);
2488 if (fib < 0 || fib >= rt_numfibs)
2490 } else if (namelen != 3)
2491 return ((namelen < 3) ? EISDIR : ENOTDIR);
2495 bzero(&w, sizeof(w));
2500 error = sysctl_wire_old_buffer(req, 0);
2505 * Allocate reply buffer in advance.
2506 * All rtsock messages has maximum length of u_short.
2508 w.w_tmemsize = 65536;
2509 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2511 NET_EPOCH_ENTER(et);
2515 if (af == 0) { /* dump all tables */
2518 } else /* dump only one table */
2522 * take care of llinfo entries, the caller must
2525 if (w.w_op == NET_RT_FLAGS &&
2526 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2528 error = lltable_sysctl_dumparp(af, w.w_req);
2534 * take care of routing entries
2536 for (error = 0; error == 0 && i <= lim; i++) {
2537 rnh = rt_tables_get_rnh(fib, i);
2539 rtable_sysctl_dump(fib, i, &w);
2541 error = EAFNOSUPPORT;
2546 /* Allow dumping one specific af/fib at a time */
2552 if (fib < 0 || fib > rt_numfibs) {
2556 rnh = rt_tables_get_rnh(fib, af);
2558 error = EAFNOSUPPORT;
2561 if (w.w_op == NET_RT_NHOP)
2562 error = nhops_dump_sysctl(rnh, w.w_req);
2565 error = nhgrp_dump_sysctl(rnh, w.w_req);
2571 case NET_RT_IFLISTL:
2572 error = sysctl_iflist(af, &w);
2575 case NET_RT_IFMALIST:
2576 error = sysctl_ifmalist(af, &w);
2581 free(w.w_tmem, M_TEMP);
2585 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2586 sysctl_rtsock, "Return route tables and interface/address lists");
2589 * Definitions of protocols supported in the ROUTE domain.
2592 static struct domain routedomain; /* or at least forward */
2594 static struct protosw routesw[] = {
2596 .pr_type = SOCK_RAW,
2597 .pr_domain = &routedomain,
2598 .pr_flags = PR_ATOMIC|PR_ADDR,
2599 .pr_output = route_output,
2600 .pr_ctlinput = raw_ctlinput,
2601 .pr_init = raw_init,
2602 .pr_usrreqs = &route_usrreqs
2606 static struct domain routedomain = {
2607 .dom_family = PF_ROUTE,
2608 .dom_name = "route",
2609 .dom_protosw = routesw,
2610 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2613 VNET_DOMAIN_SET(route);