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_mpath.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_var.h>
67 #include <net/radix_mpath.h>
71 #include <netinet/in.h>
72 #include <netinet/if_ether.h>
73 #include <netinet/ip_carp.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/scope6_var.h>
78 #include <net/route/nhop.h>
79 #include <net/route/shared.h>
81 #ifdef COMPAT_FREEBSD32
82 #include <sys/mount.h>
83 #include <compat/freebsd32/freebsd32.h>
93 struct if_data ifm_data;
103 uint16_t _ifm_spare1;
105 uint16_t ifm_data_off;
106 uint32_t _ifm_spare2;
107 struct if_data ifm_data;
110 struct ifa_msghdrl32 {
111 uint16_t ifam_msglen;
112 uint8_t ifam_version;
117 uint16_t _ifam_spare1;
119 uint16_t ifam_data_off;
121 struct if_data ifam_data;
124 #define SA_SIZE32(sa) \
125 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
127 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
129 #endif /* COMPAT_FREEBSD32 */
131 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
133 /* NB: these are not modified */
134 static struct sockaddr route_src = { 2, PF_ROUTE, };
135 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
137 /* These are external hooks for CARP. */
138 int (*carp_get_vhid_p)(struct ifaddr *);
141 * Used by rtsock/raw_input callback code to decide whether to filter the update
142 * notification to a socket bound to a particular FIB.
144 #define RTS_FILTER_FIB M_PROTO8
147 int ip_count; /* attached w/ AF_INET */
148 int ip6_count; /* attached w/ AF_INET6 */
149 int any_count; /* total attached */
151 VNET_DEFINE_STATIC(route_cb_t, route_cb);
152 #define V_route_cb VNET(route_cb)
154 struct mtx rtsock_mtx;
155 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
157 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
158 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
159 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
161 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
168 struct sysctl_req *w_req;
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 sysctl_dumpentry(struct radix_node *rn, void *vw);
178 static int sysctl_iflist(int af, struct walkarg *w);
179 static int sysctl_ifmalist(int af, struct walkarg *w);
180 static int route_output(struct mbuf *m, struct socket *so, ...);
181 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
182 static void rt_dispatch(struct mbuf *, sa_family_t);
183 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
184 struct rt_msghdr *rtm, struct rtentry **ret_nrt);
185 static int update_rtm_from_rte(struct rt_addrinfo *info,
186 struct rt_msghdr **prtm, int alloc_len,
188 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
189 struct mbuf *m, sa_family_t saf, u_int fibnum,
191 static int can_export_rte(struct ucred *td_ucred, const struct rtentry *rt);
193 static struct netisr_handler rtsock_nh = {
195 .nh_handler = rts_input,
196 .nh_proto = NETISR_ROUTE,
197 .nh_policy = NETISR_POLICY_SOURCE,
201 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
205 netisr_getqlimit(&rtsock_nh, &qlimit);
206 error = sysctl_handle_int(oidp, &qlimit, 0, req);
207 if (error || !req->newptr)
211 return (netisr_setqlimit(&rtsock_nh, qlimit));
213 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
214 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
215 0, 0, sysctl_route_netisr_maxqlen, "I",
216 "maximum routing socket dispatch queue length");
223 if (IS_DEFAULT_VNET(curvnet)) {
224 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
225 rtsock_nh.nh_qlimit = tmp;
226 netisr_register(&rtsock_nh);
230 netisr_register_vnet(&rtsock_nh);
233 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
238 vnet_rts_uninit(void)
241 netisr_unregister_vnet(&rtsock_nh);
243 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
248 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
253 KASSERT(m != NULL, ("%s: m is NULL", __func__));
254 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
255 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
257 /* No filtering requested. */
258 if ((m->m_flags & RTS_FILTER_FIB) == 0)
261 /* Check if it is a rts and the fib matches the one of the socket. */
262 fibnum = M_GETFIB(m);
263 if (proto->sp_family != PF_ROUTE ||
264 rp->rcb_socket == NULL ||
265 rp->rcb_socket->so_fibnum == fibnum)
268 /* Filtering requested and no match, the socket shall be skipped. */
273 rts_input(struct mbuf *m)
275 struct sockproto route_proto;
276 unsigned short *family;
279 route_proto.sp_family = PF_ROUTE;
280 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
282 family = (unsigned short *)(tag + 1);
283 route_proto.sp_protocol = *family;
284 m_tag_delete(m, tag);
286 route_proto.sp_protocol = 0;
288 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
292 * It really doesn't make any sense at all for this code to share much
293 * with raw_usrreq.c, since its functionality is so restricted. XXX
296 rts_abort(struct socket *so)
299 raw_usrreqs.pru_abort(so);
303 rts_close(struct socket *so)
306 raw_usrreqs.pru_close(so);
309 /* pru_accept is EOPNOTSUPP */
312 rts_attach(struct socket *so, int proto, struct thread *td)
317 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
320 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
322 so->so_pcb = (caddr_t)rp;
323 so->so_fibnum = td->td_proc->p_fibnum;
324 error = raw_attach(so, proto);
332 switch(rp->rcb_proto.sp_protocol) {
334 V_route_cb.ip_count++;
337 V_route_cb.ip6_count++;
340 V_route_cb.any_count++;
343 so->so_options |= SO_USELOOPBACK;
348 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
351 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
355 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
358 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
361 /* pru_connect2 is EOPNOTSUPP */
362 /* pru_control is EOPNOTSUPP */
365 rts_detach(struct socket *so)
367 struct rawcb *rp = sotorawcb(so);
369 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
372 switch(rp->rcb_proto.sp_protocol) {
374 V_route_cb.ip_count--;
377 V_route_cb.ip6_count--;
380 V_route_cb.any_count--;
382 raw_usrreqs.pru_detach(so);
386 rts_disconnect(struct socket *so)
389 return (raw_usrreqs.pru_disconnect(so));
392 /* pru_listen is EOPNOTSUPP */
395 rts_peeraddr(struct socket *so, struct sockaddr **nam)
398 return (raw_usrreqs.pru_peeraddr(so, nam));
401 /* pru_rcvd is EOPNOTSUPP */
402 /* pru_rcvoob is EOPNOTSUPP */
405 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
406 struct mbuf *control, struct thread *td)
409 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
412 /* pru_sense is null */
415 rts_shutdown(struct socket *so)
418 return (raw_usrreqs.pru_shutdown(so));
422 rts_sockaddr(struct socket *so, struct sockaddr **nam)
425 return (raw_usrreqs.pru_sockaddr(so, nam));
428 static struct pr_usrreqs route_usrreqs = {
429 .pru_abort = rts_abort,
430 .pru_attach = rts_attach,
431 .pru_bind = rts_bind,
432 .pru_connect = rts_connect,
433 .pru_detach = rts_detach,
434 .pru_disconnect = rts_disconnect,
435 .pru_peeraddr = rts_peeraddr,
436 .pru_send = rts_send,
437 .pru_shutdown = rts_shutdown,
438 .pru_sockaddr = rts_sockaddr,
439 .pru_close = rts_close,
442 #ifndef _SOCKADDR_UNION_DEFINED
443 #define _SOCKADDR_UNION_DEFINED
445 * The union of all possible address formats we handle.
447 union sockaddr_union {
449 struct sockaddr_in sin;
450 struct sockaddr_in6 sin6;
452 #endif /* _SOCKADDR_UNION_DEFINED */
455 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
456 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
458 #if defined(INET) || defined(INET6)
459 struct epoch_tracker et;
462 /* First, see if the returned address is part of the jail. */
463 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
464 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
468 switch (info->rti_info[RTAX_DST]->sa_family) {
478 * Try to find an address on the given outgoing interface
479 * that belongs to the jail.
482 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
485 if (sa->sa_family != AF_INET)
487 ia = ((struct sockaddr_in *)sa)->sin_addr;
488 if (prison_check_ip4(cred, &ia) == 0) {
496 * As a last resort return the 'default' jail address.
498 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
500 if (prison_get_ip4(cred, &ia) != 0)
503 bzero(&saun->sin, sizeof(struct sockaddr_in));
504 saun->sin.sin_len = sizeof(struct sockaddr_in);
505 saun->sin.sin_family = AF_INET;
506 saun->sin.sin_addr.s_addr = ia.s_addr;
507 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
520 * Try to find an address on the given outgoing interface
521 * that belongs to the jail.
524 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
527 if (sa->sa_family != AF_INET6)
529 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
530 &ia6, sizeof(struct in6_addr));
531 if (prison_check_ip6(cred, &ia6) == 0) {
539 * As a last resort return the 'default' jail address.
541 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
543 if (prison_get_ip6(cred, &ia6) != 0)
546 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
547 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
548 saun->sin6.sin6_family = AF_INET6;
549 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
550 if (sa6_recoverscope(&saun->sin6) != 0)
552 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
563 * Fills in @info based on userland-provided @rtm message.
565 * Returns 0 on success.
568 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info)
573 rtm->rtm_pid = curproc->p_pid;
574 info->rti_addrs = rtm->rtm_addrs;
576 info->rti_mflags = rtm->rtm_inits;
577 info->rti_rmx = &rtm->rtm_rmx;
580 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
581 * link-local address because rtrequest requires addresses with
584 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
587 if (rtm->rtm_flags & RTF_RNH_LOCKED)
589 info->rti_flags = rtm->rtm_flags;
590 if (info->rti_info[RTAX_DST] == NULL ||
591 info->rti_info[RTAX_DST]->sa_family >= AF_MAX ||
592 (info->rti_info[RTAX_GATEWAY] != NULL &&
593 info->rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
595 saf = info->rti_info[RTAX_DST]->sa_family;
597 * Verify that the caller has the appropriate privilege; RTM_GET
598 * is the only operation the non-superuser is allowed.
600 if (rtm->rtm_type != RTM_GET) {
601 error = priv_check(curthread, PRIV_NET_ROUTE);
607 * The given gateway address may be an interface address.
608 * For example, issuing a "route change" command on a route
609 * entry that was created from a tunnel, and the gateway
610 * address given is the local end point. In this case the
611 * RTF_GATEWAY flag must be cleared or the destination will
612 * not be reachable even though there is no error message.
614 if (info->rti_info[RTAX_GATEWAY] != NULL &&
615 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
616 struct rt_addrinfo ginfo;
617 struct sockaddr *gdst;
618 struct sockaddr_storage ss;
620 bzero(&ginfo, sizeof(ginfo));
621 bzero(&ss, sizeof(ss));
622 ss.ss_len = sizeof(ss);
624 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
625 gdst = info->rti_info[RTAX_GATEWAY];
628 * A host route through the loopback interface is
629 * installed for each interface adddress. In pre 8.0
630 * releases the interface address of a PPP link type
631 * is not reachable locally. This behavior is fixed as
632 * part of the new L2/L3 redesign and rewrite work. The
633 * signature of this interface address route is the
634 * AF_LINK sa_family type of the gateway, and the
635 * rt_ifp has the IFF_LOOPBACK flag set.
637 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
638 if (ss.ss_family == AF_LINK &&
639 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
640 info->rti_flags &= ~RTF_GATEWAY;
641 info->rti_flags |= RTF_GWFLAG_COMPAT;
643 rib_free_info(&ginfo);
651 * Handles RTM_GET message from routing socket, returning matching rt.
654 * 0 on success, with locked and referenced matching rt in @rt_nrt
658 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
659 struct rt_msghdr *rtm, struct rtentry **ret_nrt)
663 struct rib_head *rnh;
666 saf = info->rti_info[RTAX_DST]->sa_family;
668 rnh = rt_tables_get_rnh(fibnum, saf);
670 return (EAFNOSUPPORT);
674 if (info->rti_info[RTAX_NETMASK] == NULL) {
676 * Provide longest prefix match for
677 * address lookup (no mask).
678 * 'route -n get addr'
680 rt = (struct rtentry *) rnh->rnh_matchaddr(
681 info->rti_info[RTAX_DST], &rnh->head);
683 rt = (struct rtentry *) rnh->rnh_lookup(
684 info->rti_info[RTAX_DST],
685 info->rti_info[RTAX_NETMASK], &rnh->head);
693 * for RTM_GET, gate is optional even with multipath.
694 * if gate == NULL the first match is returned.
695 * (no need to call rt_mpath_matchgate if gate == NULL)
697 if (rt_mpath_capable(rnh) && info->rti_info[RTAX_GATEWAY]) {
698 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
706 * If performing proxied L2 entry insertion, and
707 * the actual PPP host entry is found, perform
708 * another search to retrieve the prefix route of
709 * the local end point of the PPP link.
710 * TODO: move this logic to userland.
712 if (rtm->rtm_flags & RTF_ANNOUNCE) {
713 struct sockaddr laddr;
714 struct nhop_object *nh;
717 if (nh->nh_ifp != NULL &&
718 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
719 struct epoch_tracker et;
723 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
727 rt_maskedcopy(ifa->ifa_addr,
731 rt_maskedcopy(nh->nh_ifa->ifa_addr,
733 nh->nh_ifa->ifa_netmask);
735 * refactor rt and no lock operation necessary
737 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
753 * Update sockaddrs, flags, etc in @prtm based on @rt data.
754 * Assumes @rt is locked.
755 * rtm can be reallocated.
757 * Returns 0 on success, along with pointer to (potentially reallocated)
762 update_rtm_from_rte(struct rt_addrinfo *info, struct rt_msghdr **prtm,
763 int alloc_len, struct rtentry *rt)
765 struct sockaddr_storage netmask_ss;
767 union sockaddr_union saun;
768 struct rt_msghdr *rtm, *orig_rtm = NULL;
769 struct nhop_object *nh;
778 info->rti_info[RTAX_DST] = rt_key(rt);
779 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
780 info->rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
781 rt_mask(rt), &netmask_ss);
782 info->rti_info[RTAX_GENMASK] = 0;
784 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
786 info->rti_info[RTAX_IFP] =
787 ifp->if_addr->ifa_addr;
788 error = rtm_get_jailed(info, ifp, nh,
789 &saun, curthread->td_ucred);
792 if (ifp->if_flags & IFF_POINTOPOINT)
793 info->rti_info[RTAX_BRD] =
794 nh->nh_ifa->ifa_dstaddr;
795 rtm->rtm_index = ifp->if_index;
797 info->rti_info[RTAX_IFP] = NULL;
798 info->rti_info[RTAX_IFA] = NULL;
800 } else if (ifp != NULL)
801 rtm->rtm_index = ifp->if_index;
803 /* Check if we need to realloc storage */
804 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
805 if (len > alloc_len) {
806 struct rt_msghdr *tmp_rtm;
808 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
811 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
817 * Delay freeing original rtm as info contains
818 * data referencing it.
822 w.w_tmem = (caddr_t)rtm;
823 w.w_tmemsize = alloc_len;
824 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
826 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
827 rtm->rtm_flags = RTF_GATEWAY |
828 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
830 rtm->rtm_flags = rt->rt_flags;
831 rt_getmetrics(rt, &rtm->rtm_rmx);
832 rtm->rtm_addrs = info->rti_addrs;
834 if (orig_rtm != NULL)
835 free(orig_rtm, M_TEMP);
843 route_output(struct mbuf *m, struct socket *so, ...)
845 struct rt_msghdr *rtm = NULL;
846 struct rtentry *rt = NULL;
847 struct rt_addrinfo info;
848 struct epoch_tracker et;
850 struct sockaddr_storage ss;
851 struct sockaddr_in6 *sin6;
852 int i, rti_need_deembed = 0;
854 int alloc_len = 0, len, error = 0, fibnum;
855 sa_family_t saf = AF_UNSPEC;
858 fibnum = so->so_fibnum;
860 #define senderr(e) { error = e; goto flush;}
861 if (m == NULL || ((m->m_len < sizeof(long)) &&
862 (m = m_pullup(m, sizeof(long))) == NULL))
864 if ((m->m_flags & M_PKTHDR) == 0)
865 panic("route_output");
867 len = m->m_pkthdr.len;
868 if (len < sizeof(*rtm) ||
869 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
873 * Most of current messages are in range 200-240 bytes,
874 * minimize possible re-allocation on reply using larger size
875 * buffer aligned on 1k boundaty.
877 alloc_len = roundup2(len, 1024);
878 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
881 m_copydata(m, 0, len, (caddr_t)rtm);
882 bzero(&info, sizeof(info));
883 bzero(&w, sizeof(w));
885 if (rtm->rtm_version != RTM_VERSION) {
886 /* Do not touch message since format is unknown */
889 senderr(EPROTONOSUPPORT);
893 * Starting from here, it is possible
894 * to alter original message and insert
895 * caller PID and error value.
898 if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) {
902 saf = info.rti_info[RTAX_DST]->sa_family;
904 /* support for new ARP code */
905 if (rtm->rtm_flags & RTF_LLDATA) {
906 error = lla_rt_output(rtm, &info);
909 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
914 switch (rtm->rtm_type) {
915 struct rtentry *saved_nrt;
919 if (rtm->rtm_type == RTM_ADD) {
920 if (info.rti_info[RTAX_GATEWAY] == NULL)
924 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
926 if (error == 0 && saved_nrt != NULL) {
928 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
931 rtm->rtm_index = saved_nrt->rt_nhop->nh_ifp->if_index;
932 RT_UNLOCK(saved_nrt);
938 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
945 /* rt_msg2() will not be used when RTM_DELETE fails. */
946 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
951 error = handle_rtm_get(&info, fibnum, rtm, &rt);
957 if (!can_export_rte(curthread->td_ucred, rt)) {
961 error = update_rtm_from_rte(&info, &rtm, alloc_len, rt);
963 * Note that some sockaddr pointers may have changed to
964 * point to memory outsize @rtm. Some may be pointing
965 * to the on-stack variables.
966 * Given that, any pointer in @info CANNOT BE USED.
970 * scopeid deembedding has been performed while
971 * writing updated rtm in rtsock_msg_buffer().
972 * With that in mind, skip deembedding procedure below.
975 rti_need_deembed = 0;
992 if (rti_need_deembed) {
993 /* sin6_scope_id is recovered before sending rtm. */
994 sin6 = (struct sockaddr_in6 *)&ss;
995 for (i = 0; i < RTAX_MAX; i++) {
996 if (info.rti_info[i] == NULL)
998 if (info.rti_info[i]->sa_family != AF_INET6)
1000 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1001 if (sa6_recoverscope(sin6) == 0)
1002 bcopy(sin6, info.rti_info[i],
1008 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1014 * Sends the prepared reply message in @rtm to all rtsock clients.
1015 * Frees @m and @rtm.
1019 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1020 sa_family_t saf, u_int fibnum, int rtm_errno)
1022 struct rawcb *rp = NULL;
1025 * Check to see if we don't want our own messages.
1027 if ((so->so_options & SO_USELOOPBACK) == 0) {
1028 if (V_route_cb.any_count <= 1) {
1034 /* There is another listener, so construct message */
1040 rtm->rtm_errno = rtm_errno;
1042 rtm->rtm_flags |= RTF_DONE;
1044 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1045 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1048 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1049 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1054 M_SETFIB(m, fibnum);
1055 m->m_flags |= RTS_FILTER_FIB;
1058 * XXX insure we don't get a copy by
1059 * invalidating our protocol
1061 unsigned short family = rp->rcb_proto.sp_family;
1062 rp->rcb_proto.sp_family = 0;
1063 rt_dispatch(m, saf);
1064 rp->rcb_proto.sp_family = family;
1066 rt_dispatch(m, saf);
1072 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
1075 bzero(out, sizeof(*out));
1076 out->rmx_mtu = rt->rt_nhop->nh_mtu;
1077 out->rmx_weight = rt->rt_weight;
1078 out->rmx_nhidx = nhop_get_idx(rt->rt_nhop);
1079 /* Kernel -> userland timebase conversion. */
1080 out->rmx_expire = rt->rt_expire ?
1081 rt->rt_expire - time_uptime + time_second : 0;
1085 * Extract the addresses of the passed sockaddrs.
1086 * Do a little sanity checking so as to avoid bad memory references.
1087 * This data is derived straight from userland.
1090 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1092 struct sockaddr *sa;
1095 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1096 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1098 sa = (struct sockaddr *)cp;
1102 if (cp + sa->sa_len > cplim)
1105 * there are no more.. quit now
1106 * If there are more bits, they are in error.
1107 * I've seen this. route(1) can evidently generate these.
1108 * This causes kernel to core dump.
1109 * for compatibility, If we see this, point to a safe address.
1111 if (sa->sa_len == 0) {
1112 rtinfo->rti_info[i] = &sa_zero;
1113 return (0); /* should be EINVAL but for compat */
1117 if (sa->sa_family == AF_INET6)
1118 sa6_embedscope((struct sockaddr_in6 *)sa,
1121 rtinfo->rti_info[i] = sa;
1128 * Fill in @dmask with valid netmask leaving original @smask
1129 * intact. Mostly used with radix netmasks.
1132 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1133 struct sockaddr_storage *dmask)
1135 if (dst == NULL || smask == NULL)
1138 memset(dmask, 0, dst->sa_len);
1139 memcpy(dmask, smask, smask->sa_len);
1140 dmask->ss_len = dst->sa_len;
1141 dmask->ss_family = dst->sa_family;
1143 return ((struct sockaddr *)dmask);
1147 * Writes information related to @rtinfo object to newly-allocated mbuf.
1148 * Assumes MCLBYTES is enough to construct any message.
1149 * Used for OS notifications of vaious events (if/ifa announces,etc)
1151 * Returns allocated mbuf or NULL on failure.
1153 static struct mbuf *
1154 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1156 struct rt_msghdr *rtm;
1159 struct sockaddr *sa;
1161 struct sockaddr_storage ss;
1162 struct sockaddr_in6 *sin6;
1170 len = sizeof(struct ifa_msghdr);
1175 len = sizeof(struct ifma_msghdr);
1179 len = sizeof(struct if_msghdr);
1182 case RTM_IFANNOUNCE:
1184 len = sizeof(struct if_announcemsghdr);
1188 len = sizeof(struct rt_msghdr);
1191 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1192 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1194 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1196 m = m_gethdr(M_NOWAIT, MT_DATA);
1200 m->m_pkthdr.len = m->m_len = len;
1201 rtm = mtod(m, struct rt_msghdr *);
1202 bzero((caddr_t)rtm, len);
1203 for (i = 0; i < RTAX_MAX; i++) {
1204 if ((sa = rtinfo->rti_info[i]) == NULL)
1206 rtinfo->rti_addrs |= (1 << i);
1209 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1210 sin6 = (struct sockaddr_in6 *)&ss;
1211 bcopy(sa, sin6, sizeof(*sin6));
1212 if (sa6_recoverscope(sin6) == 0)
1213 sa = (struct sockaddr *)sin6;
1216 m_copyback(m, len, dlen, (caddr_t)sa);
1219 if (m->m_pkthdr.len != len) {
1223 rtm->rtm_msglen = len;
1224 rtm->rtm_version = RTM_VERSION;
1225 rtm->rtm_type = type;
1230 * Writes information related to @rtinfo object to preallocated buffer.
1231 * Stores needed size in @plen. If @w is NULL, calculates size without
1233 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1235 * Returns 0 on success.
1239 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1242 int len, buflen = 0, dlen;
1244 struct rt_msghdr *rtm = NULL;
1246 struct sockaddr_storage ss;
1247 struct sockaddr_in6 *sin6;
1249 #ifdef COMPAT_FREEBSD32
1250 bool compat32 = false;
1257 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1258 #ifdef COMPAT_FREEBSD32
1259 if (w->w_req->flags & SCTL_MASK32) {
1260 len = sizeof(struct ifa_msghdrl32);
1264 len = sizeof(struct ifa_msghdrl);
1266 len = sizeof(struct ifa_msghdr);
1270 #ifdef COMPAT_FREEBSD32
1271 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1272 if (w->w_op == NET_RT_IFLISTL)
1273 len = sizeof(struct if_msghdrl32);
1275 len = sizeof(struct if_msghdr32);
1280 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1281 len = sizeof(struct if_msghdrl);
1283 len = sizeof(struct if_msghdr);
1287 len = sizeof(struct ifma_msghdr);
1291 len = sizeof(struct rt_msghdr);
1295 rtm = (struct rt_msghdr *)w->w_tmem;
1296 buflen = w->w_tmemsize - len;
1297 cp = (caddr_t)w->w_tmem + len;
1300 rtinfo->rti_addrs = 0;
1301 for (i = 0; i < RTAX_MAX; i++) {
1302 struct sockaddr *sa;
1304 if ((sa = rtinfo->rti_info[i]) == NULL)
1306 rtinfo->rti_addrs |= (1 << i);
1307 #ifdef COMPAT_FREEBSD32
1309 dlen = SA_SIZE32(sa);
1313 if (cp != NULL && buflen >= dlen) {
1315 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1316 sin6 = (struct sockaddr_in6 *)&ss;
1317 bcopy(sa, sin6, sizeof(*sin6));
1318 if (sa6_recoverscope(sin6) == 0)
1319 sa = (struct sockaddr *)sin6;
1322 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1325 } else if (cp != NULL) {
1327 * Buffer too small. Count needed size
1328 * and return with error.
1337 dlen = ALIGN(len) - len;
1349 /* fill header iff buffer is large enough */
1350 rtm->rtm_version = RTM_VERSION;
1351 rtm->rtm_type = type;
1352 rtm->rtm_msglen = len;
1357 if (w != NULL && cp == NULL)
1364 * This routine is called to generate a message from the routing
1365 * socket indicating that a redirect has occurred, a routing lookup
1366 * has failed, or that a protocol has detected timeouts to a particular
1370 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1373 struct rt_msghdr *rtm;
1375 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1377 if (V_route_cb.any_count == 0)
1379 m = rtsock_msg_mbuf(type, rtinfo);
1383 if (fibnum != RT_ALL_FIBS) {
1384 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1385 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1386 M_SETFIB(m, fibnum);
1387 m->m_flags |= RTS_FILTER_FIB;
1390 rtm = mtod(m, struct rt_msghdr *);
1391 rtm->rtm_flags = RTF_DONE | flags;
1392 rtm->rtm_errno = error;
1393 rtm->rtm_addrs = rtinfo->rti_addrs;
1394 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1398 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1401 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1405 * This routine is called to generate a message from the routing
1406 * socket indicating that the status of a network interface has changed.
1409 rt_ifmsg(struct ifnet *ifp)
1411 struct if_msghdr *ifm;
1413 struct rt_addrinfo info;
1415 if (V_route_cb.any_count == 0)
1417 bzero((caddr_t)&info, sizeof(info));
1418 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1421 ifm = mtod(m, struct if_msghdr *);
1422 ifm->ifm_index = ifp->if_index;
1423 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1424 if_data_copy(ifp, &ifm->ifm_data);
1426 rt_dispatch(m, AF_UNSPEC);
1430 * Announce interface address arrival/withdraw.
1431 * Please do not call directly, use rt_addrmsg().
1432 * Assume input data to be valid.
1433 * Returns 0 on success.
1436 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1438 struct rt_addrinfo info;
1439 struct sockaddr *sa;
1442 struct ifa_msghdr *ifam;
1443 struct ifnet *ifp = ifa->ifa_ifp;
1444 struct sockaddr_storage ss;
1446 if (V_route_cb.any_count == 0)
1449 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1451 bzero((caddr_t)&info, sizeof(info));
1452 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1453 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1454 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1455 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1456 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1457 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1459 ifam = mtod(m, struct ifa_msghdr *);
1460 ifam->ifam_index = ifp->if_index;
1461 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1462 ifam->ifam_flags = ifa->ifa_flags;
1463 ifam->ifam_addrs = info.rti_addrs;
1465 if (fibnum != RT_ALL_FIBS) {
1466 M_SETFIB(m, fibnum);
1467 m->m_flags |= RTS_FILTER_FIB;
1470 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1476 * Announce route addition/removal to rtsock based on @rt data.
1477 * Callers are advives to use rt_routemsg() instead of using this
1478 * function directly.
1479 * Assume @rt data is consistent.
1481 * Returns 0 on success.
1484 rtsock_routemsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int rti_addrs,
1487 struct sockaddr_storage ss;
1488 struct rt_addrinfo info;
1490 if (V_route_cb.any_count == 0)
1493 bzero((caddr_t)&info, sizeof(info));
1494 info.rti_info[RTAX_DST] = rt_key(rt);
1495 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt), rt_mask(rt), &ss);
1496 info.rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
1497 info.rti_flags = rt->rt_flags;
1500 return (rtsock_routemsg_info(cmd, &info, fibnum));
1504 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
1506 struct rt_msghdr *rtm;
1507 struct sockaddr *sa;
1510 if (V_route_cb.any_count == 0)
1513 if (info->rti_flags & RTF_HOST)
1514 info->rti_info[RTAX_NETMASK] = NULL;
1516 m = rtsock_msg_mbuf(cmd, info);
1520 if (fibnum != RT_ALL_FIBS) {
1521 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1522 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1523 M_SETFIB(m, fibnum);
1524 m->m_flags |= RTS_FILTER_FIB;
1527 rtm = mtod(m, struct rt_msghdr *);
1528 rtm->rtm_addrs = info->rti_addrs;
1529 if (info->rti_ifp != NULL)
1530 rtm->rtm_index = info->rti_ifp->if_index;
1531 /* Add RTF_DONE to indicate command 'completion' required by API */
1532 info->rti_flags |= RTF_DONE;
1533 /* Reported routes has to be up */
1534 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
1535 info->rti_flags |= RTF_UP;
1536 rtm->rtm_flags = info->rti_flags;
1538 sa = info->rti_info[RTAX_DST];
1539 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1545 * This is the analogue to the rt_newaddrmsg which performs the same
1546 * function but for multicast group memberhips. This is easier since
1547 * there is no route state to worry about.
1550 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1552 struct rt_addrinfo info;
1553 struct mbuf *m = NULL;
1554 struct ifnet *ifp = ifma->ifma_ifp;
1555 struct ifma_msghdr *ifmam;
1557 if (V_route_cb.any_count == 0)
1560 bzero((caddr_t)&info, sizeof(info));
1561 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1562 if (ifp && ifp->if_addr)
1563 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1565 info.rti_info[RTAX_IFP] = NULL;
1567 * If a link-layer address is present, present it as a ``gateway''
1568 * (similarly to how ARP entries, e.g., are presented).
1570 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1571 m = rtsock_msg_mbuf(cmd, &info);
1574 ifmam = mtod(m, struct ifma_msghdr *);
1575 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1577 ifmam->ifmam_index = ifp->if_index;
1578 ifmam->ifmam_addrs = info.rti_addrs;
1579 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1582 static struct mbuf *
1583 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1584 struct rt_addrinfo *info)
1586 struct if_announcemsghdr *ifan;
1589 if (V_route_cb.any_count == 0)
1591 bzero((caddr_t)info, sizeof(*info));
1592 m = rtsock_msg_mbuf(type, info);
1594 ifan = mtod(m, struct if_announcemsghdr *);
1595 ifan->ifan_index = ifp->if_index;
1596 strlcpy(ifan->ifan_name, ifp->if_xname,
1597 sizeof(ifan->ifan_name));
1598 ifan->ifan_what = what;
1604 * This is called to generate routing socket messages indicating
1605 * IEEE80211 wireless events.
1606 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1609 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1612 struct rt_addrinfo info;
1614 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1617 * Append the ieee80211 data. Try to stick it in the
1618 * mbuf containing the ifannounce msg; otherwise allocate
1619 * a new mbuf and append.
1621 * NB: we assume m is a single mbuf.
1623 if (data_len > M_TRAILINGSPACE(m)) {
1624 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1629 bcopy(data, mtod(n, void *), data_len);
1630 n->m_len = data_len;
1632 } else if (data_len > 0) {
1633 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1634 m->m_len += data_len;
1636 if (m->m_flags & M_PKTHDR)
1637 m->m_pkthdr.len += data_len;
1638 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1639 rt_dispatch(m, AF_UNSPEC);
1644 * This is called to generate routing socket messages indicating
1645 * network interface arrival and departure.
1648 rt_ifannouncemsg(struct ifnet *ifp, int what)
1651 struct rt_addrinfo info;
1653 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1655 rt_dispatch(m, AF_UNSPEC);
1659 rt_dispatch(struct mbuf *m, sa_family_t saf)
1664 * Preserve the family from the sockaddr, if any, in an m_tag for
1665 * use when injecting the mbuf into the routing socket buffer from
1668 if (saf != AF_UNSPEC) {
1669 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1675 *(unsigned short *)(tag + 1) = saf;
1676 m_tag_prepend(m, tag);
1680 m->m_pkthdr.rcvif = V_loif;
1686 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1690 * Checks if rte can be exported v.r.t jails/vnets.
1692 * Returns 1 if it can, 0 otherwise.
1695 can_export_rte(struct ucred *td_ucred, const struct rtentry *rt)
1698 if ((rt->rt_flags & RTF_HOST) == 0
1699 ? jailed_without_vnet(td_ucred)
1700 : prison_if(td_ucred, rt_key_const(rt)) != 0)
1706 * This is used in dumping the kernel table via sysctl().
1709 sysctl_dumpentry(struct radix_node *rn, void *vw)
1711 struct walkarg *w = vw;
1712 struct rtentry *rt = (struct rtentry *)rn;
1713 struct nhop_object *nh;
1714 int error = 0, size;
1715 struct rt_addrinfo info;
1716 struct sockaddr_storage ss;
1720 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1722 if (!can_export_rte(w->w_req->td->td_ucred, rt))
1724 bzero((caddr_t)&info, sizeof(info));
1725 info.rti_info[RTAX_DST] = rt_key(rt);
1726 info.rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
1727 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1729 info.rti_info[RTAX_GENMASK] = 0;
1731 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
1732 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
1733 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
1734 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
1735 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
1737 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1739 if (w->w_req && w->w_tmem) {
1740 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1742 bzero(&rtm->rtm_index,
1743 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1744 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1745 rtm->rtm_flags = RTF_GATEWAY |
1746 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1748 rtm->rtm_flags = rt->rt_flags;
1749 rtm->rtm_flags |= nhop_get_rtflags(nh);
1750 rt_getmetrics(rt, &rtm->rtm_rmx);
1751 rtm->rtm_index = nh->nh_ifp->if_index;
1752 rtm->rtm_addrs = info.rti_addrs;
1753 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1760 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1761 struct rt_addrinfo *info, struct walkarg *w, int len)
1763 struct if_msghdrl *ifm;
1764 struct if_data *ifd;
1766 ifm = (struct if_msghdrl *)w->w_tmem;
1768 #ifdef COMPAT_FREEBSD32
1769 if (w->w_req->flags & SCTL_MASK32) {
1770 struct if_msghdrl32 *ifm32;
1772 ifm32 = (struct if_msghdrl32 *)ifm;
1773 ifm32->ifm_addrs = info->rti_addrs;
1774 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1775 ifm32->ifm_index = ifp->if_index;
1776 ifm32->_ifm_spare1 = 0;
1777 ifm32->ifm_len = sizeof(*ifm32);
1778 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1779 ifm32->_ifm_spare2 = 0;
1780 ifd = &ifm32->ifm_data;
1784 ifm->ifm_addrs = info->rti_addrs;
1785 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1786 ifm->ifm_index = ifp->if_index;
1787 ifm->_ifm_spare1 = 0;
1788 ifm->ifm_len = sizeof(*ifm);
1789 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1790 ifm->_ifm_spare2 = 0;
1791 ifd = &ifm->ifm_data;
1794 memcpy(ifd, src_ifd, sizeof(*ifd));
1796 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1800 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1801 struct rt_addrinfo *info, struct walkarg *w, int len)
1803 struct if_msghdr *ifm;
1804 struct if_data *ifd;
1806 ifm = (struct if_msghdr *)w->w_tmem;
1808 #ifdef COMPAT_FREEBSD32
1809 if (w->w_req->flags & SCTL_MASK32) {
1810 struct if_msghdr32 *ifm32;
1812 ifm32 = (struct if_msghdr32 *)ifm;
1813 ifm32->ifm_addrs = info->rti_addrs;
1814 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1815 ifm32->ifm_index = ifp->if_index;
1816 ifm32->_ifm_spare1 = 0;
1817 ifd = &ifm32->ifm_data;
1821 ifm->ifm_addrs = info->rti_addrs;
1822 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1823 ifm->ifm_index = ifp->if_index;
1824 ifm->_ifm_spare1 = 0;
1825 ifd = &ifm->ifm_data;
1828 memcpy(ifd, src_ifd, sizeof(*ifd));
1830 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1834 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1835 struct walkarg *w, int len)
1837 struct ifa_msghdrl *ifam;
1838 struct if_data *ifd;
1840 ifam = (struct ifa_msghdrl *)w->w_tmem;
1842 #ifdef COMPAT_FREEBSD32
1843 if (w->w_req->flags & SCTL_MASK32) {
1844 struct ifa_msghdrl32 *ifam32;
1846 ifam32 = (struct ifa_msghdrl32 *)ifam;
1847 ifam32->ifam_addrs = info->rti_addrs;
1848 ifam32->ifam_flags = ifa->ifa_flags;
1849 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1850 ifam32->_ifam_spare1 = 0;
1851 ifam32->ifam_len = sizeof(*ifam32);
1852 ifam32->ifam_data_off =
1853 offsetof(struct ifa_msghdrl32, ifam_data);
1854 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1855 ifd = &ifam32->ifam_data;
1859 ifam->ifam_addrs = info->rti_addrs;
1860 ifam->ifam_flags = ifa->ifa_flags;
1861 ifam->ifam_index = ifa->ifa_ifp->if_index;
1862 ifam->_ifam_spare1 = 0;
1863 ifam->ifam_len = sizeof(*ifam);
1864 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1865 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1866 ifd = &ifam->ifam_data;
1869 bzero(ifd, sizeof(*ifd));
1870 ifd->ifi_datalen = sizeof(struct if_data);
1871 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1872 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1873 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1874 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1876 /* Fixup if_data carp(4) vhid. */
1877 if (carp_get_vhid_p != NULL)
1878 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1880 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1884 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1885 struct walkarg *w, int len)
1887 struct ifa_msghdr *ifam;
1889 ifam = (struct ifa_msghdr *)w->w_tmem;
1890 ifam->ifam_addrs = info->rti_addrs;
1891 ifam->ifam_flags = ifa->ifa_flags;
1892 ifam->ifam_index = ifa->ifa_ifp->if_index;
1893 ifam->_ifam_spare1 = 0;
1894 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1896 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1900 sysctl_iflist(int af, struct walkarg *w)
1905 struct rt_addrinfo info;
1907 struct sockaddr_storage ss;
1909 bzero((caddr_t)&info, sizeof(info));
1910 bzero(&ifd, sizeof(ifd));
1911 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1912 if (w->w_arg && w->w_arg != ifp->if_index)
1914 if_data_copy(ifp, &ifd);
1916 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1917 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1920 info.rti_info[RTAX_IFP] = NULL;
1921 if (w->w_req && w->w_tmem) {
1922 if (w->w_op == NET_RT_IFLISTL)
1923 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1926 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1931 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1932 if (af && af != ifa->ifa_addr->sa_family)
1934 if (prison_if(w->w_req->td->td_ucred,
1935 ifa->ifa_addr) != 0)
1937 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1938 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1939 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1940 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1941 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1944 if (w->w_req && w->w_tmem) {
1945 if (w->w_op == NET_RT_IFLISTL)
1946 error = sysctl_iflist_ifaml(ifa, &info,
1949 error = sysctl_iflist_ifam(ifa, &info,
1955 info.rti_info[RTAX_IFA] = NULL;
1956 info.rti_info[RTAX_NETMASK] = NULL;
1957 info.rti_info[RTAX_BRD] = NULL;
1964 sysctl_ifmalist(int af, struct walkarg *w)
1966 struct rt_addrinfo info;
1968 struct ifmultiaddr *ifma;
1975 bzero((caddr_t)&info, sizeof(info));
1977 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1978 if (w->w_arg && w->w_arg != ifp->if_index)
1981 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1982 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1983 if (af && af != ifma->ifma_addr->sa_family)
1985 if (prison_if(w->w_req->td->td_ucred,
1986 ifma->ifma_addr) != 0)
1988 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1989 info.rti_info[RTAX_GATEWAY] =
1990 (ifma->ifma_addr->sa_family != AF_LINK) ?
1991 ifma->ifma_lladdr : NULL;
1992 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1995 if (w->w_req && w->w_tmem) {
1996 struct ifma_msghdr *ifmam;
1998 ifmam = (struct ifma_msghdr *)w->w_tmem;
1999 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2000 ifmam->ifmam_flags = 0;
2001 ifmam->ifmam_addrs = info.rti_addrs;
2002 ifmam->_ifmam_spare1 = 0;
2003 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2015 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2018 struct epoch_tracker et;
2019 int *name = (int *)arg1;
2020 u_int namelen = arg2;
2021 struct rib_head *rnh = NULL; /* silence compiler. */
2022 int i, lim, error = EINVAL;
2031 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP) {
2033 fib = req->td->td_proc->p_fibnum;
2034 else if (namelen == 4)
2035 fib = (name[3] == RT_ALL_FIBS) ?
2036 req->td->td_proc->p_fibnum : name[3];
2038 return ((namelen < 3) ? EISDIR : ENOTDIR);
2039 if (fib < 0 || fib >= rt_numfibs)
2041 } else if (namelen != 3)
2042 return ((namelen < 3) ? EISDIR : ENOTDIR);
2046 bzero(&w, sizeof(w));
2051 error = sysctl_wire_old_buffer(req, 0);
2056 * Allocate reply buffer in advance.
2057 * All rtsock messages has maximum length of u_short.
2059 w.w_tmemsize = 65536;
2060 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2062 NET_EPOCH_ENTER(et);
2066 if (af == 0) { /* dump all tables */
2069 } else /* dump only one table */
2073 * take care of llinfo entries, the caller must
2076 if (w.w_op == NET_RT_FLAGS &&
2077 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2079 error = lltable_sysctl_dumparp(af, w.w_req);
2085 * take care of routing entries
2087 for (error = 0; error == 0 && i <= lim; i++) {
2088 rnh = rt_tables_get_rnh(fib, i);
2091 error = rnh->rnh_walktree(&rnh->head,
2092 sysctl_dumpentry, &w);
2095 error = EAFNOSUPPORT;
2099 /* Allow dumping one specific af/fib at a time */
2105 if (fib < 0 || fib > rt_numfibs) {
2109 rnh = rt_tables_get_rnh(fib, af);
2111 error = EAFNOSUPPORT;
2114 if (w.w_op == NET_RT_NHOP)
2115 error = nhops_dump_sysctl(rnh, w.w_req);
2118 case NET_RT_IFLISTL:
2119 error = sysctl_iflist(af, &w);
2122 case NET_RT_IFMALIST:
2123 error = sysctl_ifmalist(af, &w);
2128 free(w.w_tmem, M_TEMP);
2132 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2133 sysctl_rtsock, "Return route tables and interface/address lists");
2136 * Definitions of protocols supported in the ROUTE domain.
2139 static struct domain routedomain; /* or at least forward */
2141 static struct protosw routesw[] = {
2143 .pr_type = SOCK_RAW,
2144 .pr_domain = &routedomain,
2145 .pr_flags = PR_ATOMIC|PR_ADDR,
2146 .pr_output = route_output,
2147 .pr_ctlinput = raw_ctlinput,
2148 .pr_init = raw_init,
2149 .pr_usrreqs = &route_usrreqs
2153 static struct domain routedomain = {
2154 .dom_family = PF_ROUTE,
2155 .dom_name = "route",
2156 .dom_protosw = routesw,
2157 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2160 VNET_DOMAIN_SET(route);