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>
59 #include <ddb/db_lex.h>
63 #include <net/if_var.h>
64 #include <net/if_dl.h>
65 #include <net/if_llatbl.h>
66 #include <net/if_types.h>
67 #include <net/netisr.h>
68 #include <net/raw_cb.h>
69 #include <net/route.h>
70 #include <net/route_var.h>
73 #include <netinet/in.h>
74 #include <netinet/if_ether.h>
75 #include <netinet/ip_carp.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.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 struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
184 struct sockaddr *smask, struct sockaddr_storage *dmask);
185 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
186 struct rt_msghdr *rtm, struct rtentry **ret_nrt);
187 static int update_rtm_from_rte(struct rt_addrinfo *info,
188 struct rt_msghdr **prtm, int alloc_len,
190 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
191 struct mbuf *m, sa_family_t saf, u_int fibnum,
193 static int can_export_rte(struct ucred *td_ucred, const struct rtentry *rt);
195 static struct netisr_handler rtsock_nh = {
197 .nh_handler = rts_input,
198 .nh_proto = NETISR_ROUTE,
199 .nh_policy = NETISR_POLICY_SOURCE,
203 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
207 netisr_getqlimit(&rtsock_nh, &qlimit);
208 error = sysctl_handle_int(oidp, &qlimit, 0, req);
209 if (error || !req->newptr)
213 return (netisr_setqlimit(&rtsock_nh, qlimit));
215 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
216 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
217 0, 0, sysctl_route_netisr_maxqlen, "I",
218 "maximum routing socket dispatch queue length");
225 if (IS_DEFAULT_VNET(curvnet)) {
226 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
227 rtsock_nh.nh_qlimit = tmp;
228 netisr_register(&rtsock_nh);
232 netisr_register_vnet(&rtsock_nh);
235 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
240 vnet_rts_uninit(void)
243 netisr_unregister_vnet(&rtsock_nh);
245 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
250 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
255 KASSERT(m != NULL, ("%s: m is NULL", __func__));
256 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
257 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
259 /* No filtering requested. */
260 if ((m->m_flags & RTS_FILTER_FIB) == 0)
263 /* Check if it is a rts and the fib matches the one of the socket. */
264 fibnum = M_GETFIB(m);
265 if (proto->sp_family != PF_ROUTE ||
266 rp->rcb_socket == NULL ||
267 rp->rcb_socket->so_fibnum == fibnum)
270 /* Filtering requested and no match, the socket shall be skipped. */
275 rts_input(struct mbuf *m)
277 struct sockproto route_proto;
278 unsigned short *family;
281 route_proto.sp_family = PF_ROUTE;
282 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
284 family = (unsigned short *)(tag + 1);
285 route_proto.sp_protocol = *family;
286 m_tag_delete(m, tag);
288 route_proto.sp_protocol = 0;
290 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
294 * It really doesn't make any sense at all for this code to share much
295 * with raw_usrreq.c, since its functionality is so restricted. XXX
298 rts_abort(struct socket *so)
301 raw_usrreqs.pru_abort(so);
305 rts_close(struct socket *so)
308 raw_usrreqs.pru_close(so);
311 /* pru_accept is EOPNOTSUPP */
314 rts_attach(struct socket *so, int proto, struct thread *td)
319 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
322 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
324 so->so_pcb = (caddr_t)rp;
325 so->so_fibnum = td->td_proc->p_fibnum;
326 error = raw_attach(so, proto);
334 switch(rp->rcb_proto.sp_protocol) {
336 V_route_cb.ip_count++;
339 V_route_cb.ip6_count++;
342 V_route_cb.any_count++;
345 so->so_options |= SO_USELOOPBACK;
350 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
353 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
357 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
360 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
363 /* pru_connect2 is EOPNOTSUPP */
364 /* pru_control is EOPNOTSUPP */
367 rts_detach(struct socket *so)
369 struct rawcb *rp = sotorawcb(so);
371 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
374 switch(rp->rcb_proto.sp_protocol) {
376 V_route_cb.ip_count--;
379 V_route_cb.ip6_count--;
382 V_route_cb.any_count--;
384 raw_usrreqs.pru_detach(so);
388 rts_disconnect(struct socket *so)
391 return (raw_usrreqs.pru_disconnect(so));
394 /* pru_listen is EOPNOTSUPP */
397 rts_peeraddr(struct socket *so, struct sockaddr **nam)
400 return (raw_usrreqs.pru_peeraddr(so, nam));
403 /* pru_rcvd is EOPNOTSUPP */
404 /* pru_rcvoob is EOPNOTSUPP */
407 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
408 struct mbuf *control, struct thread *td)
411 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
414 /* pru_sense is null */
417 rts_shutdown(struct socket *so)
420 return (raw_usrreqs.pru_shutdown(so));
424 rts_sockaddr(struct socket *so, struct sockaddr **nam)
427 return (raw_usrreqs.pru_sockaddr(so, nam));
430 static struct pr_usrreqs route_usrreqs = {
431 .pru_abort = rts_abort,
432 .pru_attach = rts_attach,
433 .pru_bind = rts_bind,
434 .pru_connect = rts_connect,
435 .pru_detach = rts_detach,
436 .pru_disconnect = rts_disconnect,
437 .pru_peeraddr = rts_peeraddr,
438 .pru_send = rts_send,
439 .pru_shutdown = rts_shutdown,
440 .pru_sockaddr = rts_sockaddr,
441 .pru_close = rts_close,
444 #ifndef _SOCKADDR_UNION_DEFINED
445 #define _SOCKADDR_UNION_DEFINED
447 * The union of all possible address formats we handle.
449 union sockaddr_union {
451 struct sockaddr_in sin;
452 struct sockaddr_in6 sin6;
454 #endif /* _SOCKADDR_UNION_DEFINED */
457 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
458 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
460 #if defined(INET) || defined(INET6)
461 struct epoch_tracker et;
464 /* First, see if the returned address is part of the jail. */
465 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
466 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
470 switch (info->rti_info[RTAX_DST]->sa_family) {
480 * Try to find an address on the given outgoing interface
481 * that belongs to the jail.
484 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
487 if (sa->sa_family != AF_INET)
489 ia = ((struct sockaddr_in *)sa)->sin_addr;
490 if (prison_check_ip4(cred, &ia) == 0) {
498 * As a last resort return the 'default' jail address.
500 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
502 if (prison_get_ip4(cred, &ia) != 0)
505 bzero(&saun->sin, sizeof(struct sockaddr_in));
506 saun->sin.sin_len = sizeof(struct sockaddr_in);
507 saun->sin.sin_family = AF_INET;
508 saun->sin.sin_addr.s_addr = ia.s_addr;
509 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
522 * Try to find an address on the given outgoing interface
523 * that belongs to the jail.
526 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
529 if (sa->sa_family != AF_INET6)
531 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
532 &ia6, sizeof(struct in6_addr));
533 if (prison_check_ip6(cred, &ia6) == 0) {
541 * As a last resort return the 'default' jail address.
543 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
545 if (prison_get_ip6(cred, &ia6) != 0)
548 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
549 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
550 saun->sin6.sin6_family = AF_INET6;
551 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
552 if (sa6_recoverscope(&saun->sin6) != 0)
554 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
565 * Fills in @info based on userland-provided @rtm message.
567 * Returns 0 on success.
570 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info)
575 rtm->rtm_pid = curproc->p_pid;
576 info->rti_addrs = rtm->rtm_addrs;
578 info->rti_mflags = rtm->rtm_inits;
579 info->rti_rmx = &rtm->rtm_rmx;
582 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
583 * link-local address because rtrequest requires addresses with
586 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
589 if (rtm->rtm_flags & RTF_RNH_LOCKED)
591 info->rti_flags = rtm->rtm_flags;
592 if (info->rti_info[RTAX_DST] == NULL ||
593 info->rti_info[RTAX_DST]->sa_family >= AF_MAX ||
594 (info->rti_info[RTAX_GATEWAY] != NULL &&
595 info->rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
597 saf = info->rti_info[RTAX_DST]->sa_family;
599 * Verify that the caller has the appropriate privilege; RTM_GET
600 * is the only operation the non-superuser is allowed.
602 if (rtm->rtm_type != RTM_GET) {
603 error = priv_check(curthread, PRIV_NET_ROUTE);
609 * The given gateway address may be an interface address.
610 * For example, issuing a "route change" command on a route
611 * entry that was created from a tunnel, and the gateway
612 * address given is the local end point. In this case the
613 * RTF_GATEWAY flag must be cleared or the destination will
614 * not be reachable even though there is no error message.
616 if (info->rti_info[RTAX_GATEWAY] != NULL &&
617 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
618 struct rt_addrinfo ginfo;
619 struct sockaddr *gdst;
620 struct sockaddr_storage ss;
622 bzero(&ginfo, sizeof(ginfo));
623 bzero(&ss, sizeof(ss));
624 ss.ss_len = sizeof(ss);
626 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
627 gdst = info->rti_info[RTAX_GATEWAY];
630 * A host route through the loopback interface is
631 * installed for each interface adddress. In pre 8.0
632 * releases the interface address of a PPP link type
633 * is not reachable locally. This behavior is fixed as
634 * part of the new L2/L3 redesign and rewrite work. The
635 * signature of this interface address route is the
636 * AF_LINK sa_family type of the rt_gateway, and the
637 * rt_ifp has the IFF_LOOPBACK flag set.
639 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
640 if (ss.ss_family == AF_LINK &&
641 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
642 info->rti_flags &= ~RTF_GATEWAY;
643 info->rti_flags |= RTF_GWFLAG_COMPAT;
645 rib_free_info(&ginfo);
653 * Handles RTM_GET message from routing socket, returning matching rt.
656 * 0 on success, with locked and referenced matching rt in @rt_nrt
660 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
661 struct rt_msghdr *rtm, struct rtentry **ret_nrt)
665 struct rib_head *rnh;
668 saf = info->rti_info[RTAX_DST]->sa_family;
670 rnh = rt_tables_get_rnh(fibnum, saf);
672 return (EAFNOSUPPORT);
676 if (info->rti_info[RTAX_NETMASK] == NULL) {
678 * Provide longest prefix match for
679 * address lookup (no mask).
680 * 'route -n get addr'
682 rt = (struct rtentry *) rnh->rnh_matchaddr(
683 info->rti_info[RTAX_DST], &rnh->head);
685 rt = (struct rtentry *) rnh->rnh_lookup(
686 info->rti_info[RTAX_DST],
687 info->rti_info[RTAX_NETMASK], &rnh->head);
695 * for RTM_GET, gate is optional even with multipath.
696 * if gate == NULL the first match is returned.
697 * (no need to call rt_mpath_matchgate if gate == NULL)
699 if (rt_mpath_capable(rnh) && info->rti_info[RTAX_GATEWAY]) {
700 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
708 * If performing proxied L2 entry insertion, and
709 * the actual PPP host entry is found, perform
710 * another search to retrieve the prefix route of
711 * the local end point of the PPP link.
713 if (rtm->rtm_flags & RTF_ANNOUNCE) {
714 struct sockaddr laddr;
716 if (rt->rt_ifp != NULL &&
717 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
718 struct epoch_tracker et;
722 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
726 rt_maskedcopy(ifa->ifa_addr,
730 rt_maskedcopy(rt->rt_ifa->ifa_addr,
732 rt->rt_ifa->ifa_netmask);
734 * refactor rt and no lock operation necessary
736 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;
776 info->rti_info[RTAX_DST] = rt_key(rt);
777 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
778 info->rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
779 rt_mask(rt), &netmask_ss);
780 info->rti_info[RTAX_GENMASK] = 0;
782 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
784 info->rti_info[RTAX_IFP] =
785 ifp->if_addr->ifa_addr;
786 error = rtm_get_jailed(info, ifp, rt,
787 &saun, curthread->td_ucred);
790 if (ifp->if_flags & IFF_POINTOPOINT)
791 info->rti_info[RTAX_BRD] =
792 rt->rt_ifa->ifa_dstaddr;
793 rtm->rtm_index = ifp->if_index;
795 info->rti_info[RTAX_IFP] = NULL;
796 info->rti_info[RTAX_IFA] = NULL;
798 } else if (ifp != NULL)
799 rtm->rtm_index = ifp->if_index;
801 /* Check if we need to realloc storage */
802 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
803 if (len > alloc_len) {
804 struct rt_msghdr *tmp_rtm;
806 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
809 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
815 * Delay freeing original rtm as info contains
816 * data referencing it.
820 w.w_tmem = (caddr_t)rtm;
821 w.w_tmemsize = alloc_len;
822 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
824 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
825 rtm->rtm_flags = RTF_GATEWAY |
826 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
828 rtm->rtm_flags = rt->rt_flags;
829 rt_getmetrics(rt, &rtm->rtm_rmx);
830 rtm->rtm_addrs = info->rti_addrs;
832 if (orig_rtm != NULL)
833 free(orig_rtm, M_TEMP);
841 route_output(struct mbuf *m, struct socket *so, ...)
843 struct rt_msghdr *rtm = NULL;
844 struct rtentry *rt = NULL;
845 struct rt_addrinfo info;
846 struct epoch_tracker et;
848 struct sockaddr_storage ss;
849 struct sockaddr_in6 *sin6;
850 int i, rti_need_deembed = 0;
852 int alloc_len = 0, len, error = 0, fibnum;
853 sa_family_t saf = AF_UNSPEC;
856 fibnum = so->so_fibnum;
858 #define senderr(e) { error = e; goto flush;}
859 if (m == NULL || ((m->m_len < sizeof(long)) &&
860 (m = m_pullup(m, sizeof(long))) == NULL))
862 if ((m->m_flags & M_PKTHDR) == 0)
863 panic("route_output");
865 len = m->m_pkthdr.len;
866 if (len < sizeof(*rtm) ||
867 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
871 * Most of current messages are in range 200-240 bytes,
872 * minimize possible re-allocation on reply using larger size
873 * buffer aligned on 1k boundaty.
875 alloc_len = roundup2(len, 1024);
876 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
879 m_copydata(m, 0, len, (caddr_t)rtm);
880 bzero(&info, sizeof(info));
881 bzero(&w, sizeof(w));
883 if (rtm->rtm_version != RTM_VERSION) {
884 /* Do not touch message since format is unknown */
887 senderr(EPROTONOSUPPORT);
891 * Starting from here, it is possible
892 * to alter original message and insert
893 * caller PID and error value.
896 if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) {
900 saf = info.rti_info[RTAX_DST]->sa_family;
902 /* support for new ARP code */
903 if (rtm->rtm_flags & RTF_LLDATA) {
904 error = lla_rt_output(rtm, &info);
907 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
912 switch (rtm->rtm_type) {
913 struct rtentry *saved_nrt;
917 if (rtm->rtm_type == RTM_ADD) {
918 if (info.rti_info[RTAX_GATEWAY] == NULL)
922 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
924 if (error == 0 && saved_nrt != NULL) {
926 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
929 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
930 RT_REMREF(saved_nrt);
931 RT_UNLOCK(saved_nrt);
937 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
944 /* rt_msg2() will not be used when RTM_DELETE fails. */
945 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
950 error = handle_rtm_get(&info, fibnum, rtm, &rt);
956 if (!can_export_rte(curthread->td_ucred, rt)) {
960 error = update_rtm_from_rte(&info, &rtm, alloc_len, rt);
962 * Note that some sockaddr pointers may have changed to
963 * point to memory outsize @rtm. Some may be pointing
964 * to the on-stack variables.
965 * Given that, any pointer in @info CANNOT BE USED.
969 * scopeid deembedding has been performed while
970 * writing updated rtm in rtsock_msg_buffer().
971 * With that in mind, skip deembedding procedure below.
974 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_mtu;
1077 out->rmx_weight = rt->rt_weight;
1078 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
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.
1131 static struct sockaddr *
1132 rtsock_fix_netmask(struct sockaddr *dst, 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_gateway;
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 int error = 0, size;
1714 struct rt_addrinfo info;
1715 struct sockaddr_storage ss;
1719 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1721 if (!can_export_rte(w->w_req->td->td_ucred, rt))
1723 bzero((caddr_t)&info, sizeof(info));
1724 info.rti_info[RTAX_DST] = rt_key(rt);
1725 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1726 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1728 info.rti_info[RTAX_GENMASK] = 0;
1729 if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1730 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1731 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1732 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1733 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1735 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1737 if (w->w_req && w->w_tmem) {
1738 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1740 bzero(&rtm->rtm_index,
1741 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1742 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1743 rtm->rtm_flags = RTF_GATEWAY |
1744 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1746 rtm->rtm_flags = rt->rt_flags;
1747 rt_getmetrics(rt, &rtm->rtm_rmx);
1748 rtm->rtm_index = rt->rt_ifp->if_index;
1749 rtm->rtm_addrs = info.rti_addrs;
1750 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1757 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1758 struct rt_addrinfo *info, struct walkarg *w, int len)
1760 struct if_msghdrl *ifm;
1761 struct if_data *ifd;
1763 ifm = (struct if_msghdrl *)w->w_tmem;
1765 #ifdef COMPAT_FREEBSD32
1766 if (w->w_req->flags & SCTL_MASK32) {
1767 struct if_msghdrl32 *ifm32;
1769 ifm32 = (struct if_msghdrl32 *)ifm;
1770 ifm32->ifm_addrs = info->rti_addrs;
1771 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1772 ifm32->ifm_index = ifp->if_index;
1773 ifm32->_ifm_spare1 = 0;
1774 ifm32->ifm_len = sizeof(*ifm32);
1775 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1776 ifm32->_ifm_spare2 = 0;
1777 ifd = &ifm32->ifm_data;
1781 ifm->ifm_addrs = info->rti_addrs;
1782 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1783 ifm->ifm_index = ifp->if_index;
1784 ifm->_ifm_spare1 = 0;
1785 ifm->ifm_len = sizeof(*ifm);
1786 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1787 ifm->_ifm_spare2 = 0;
1788 ifd = &ifm->ifm_data;
1791 memcpy(ifd, src_ifd, sizeof(*ifd));
1793 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1797 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1798 struct rt_addrinfo *info, struct walkarg *w, int len)
1800 struct if_msghdr *ifm;
1801 struct if_data *ifd;
1803 ifm = (struct if_msghdr *)w->w_tmem;
1805 #ifdef COMPAT_FREEBSD32
1806 if (w->w_req->flags & SCTL_MASK32) {
1807 struct if_msghdr32 *ifm32;
1809 ifm32 = (struct if_msghdr32 *)ifm;
1810 ifm32->ifm_addrs = info->rti_addrs;
1811 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1812 ifm32->ifm_index = ifp->if_index;
1813 ifm32->_ifm_spare1 = 0;
1814 ifd = &ifm32->ifm_data;
1818 ifm->ifm_addrs = info->rti_addrs;
1819 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1820 ifm->ifm_index = ifp->if_index;
1821 ifm->_ifm_spare1 = 0;
1822 ifd = &ifm->ifm_data;
1825 memcpy(ifd, src_ifd, sizeof(*ifd));
1827 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1831 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1832 struct walkarg *w, int len)
1834 struct ifa_msghdrl *ifam;
1835 struct if_data *ifd;
1837 ifam = (struct ifa_msghdrl *)w->w_tmem;
1839 #ifdef COMPAT_FREEBSD32
1840 if (w->w_req->flags & SCTL_MASK32) {
1841 struct ifa_msghdrl32 *ifam32;
1843 ifam32 = (struct ifa_msghdrl32 *)ifam;
1844 ifam32->ifam_addrs = info->rti_addrs;
1845 ifam32->ifam_flags = ifa->ifa_flags;
1846 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1847 ifam32->_ifam_spare1 = 0;
1848 ifam32->ifam_len = sizeof(*ifam32);
1849 ifam32->ifam_data_off =
1850 offsetof(struct ifa_msghdrl32, ifam_data);
1851 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1852 ifd = &ifam32->ifam_data;
1856 ifam->ifam_addrs = info->rti_addrs;
1857 ifam->ifam_flags = ifa->ifa_flags;
1858 ifam->ifam_index = ifa->ifa_ifp->if_index;
1859 ifam->_ifam_spare1 = 0;
1860 ifam->ifam_len = sizeof(*ifam);
1861 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1862 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1863 ifd = &ifam->ifam_data;
1866 bzero(ifd, sizeof(*ifd));
1867 ifd->ifi_datalen = sizeof(struct if_data);
1868 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1869 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1870 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1871 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1873 /* Fixup if_data carp(4) vhid. */
1874 if (carp_get_vhid_p != NULL)
1875 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1877 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1881 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1882 struct walkarg *w, int len)
1884 struct ifa_msghdr *ifam;
1886 ifam = (struct ifa_msghdr *)w->w_tmem;
1887 ifam->ifam_addrs = info->rti_addrs;
1888 ifam->ifam_flags = ifa->ifa_flags;
1889 ifam->ifam_index = ifa->ifa_ifp->if_index;
1890 ifam->_ifam_spare1 = 0;
1891 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1893 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1897 sysctl_iflist(int af, struct walkarg *w)
1902 struct rt_addrinfo info;
1904 struct sockaddr_storage ss;
1906 bzero((caddr_t)&info, sizeof(info));
1907 bzero(&ifd, sizeof(ifd));
1908 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1909 if (w->w_arg && w->w_arg != ifp->if_index)
1911 if_data_copy(ifp, &ifd);
1913 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1914 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1917 info.rti_info[RTAX_IFP] = NULL;
1918 if (w->w_req && w->w_tmem) {
1919 if (w->w_op == NET_RT_IFLISTL)
1920 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1923 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1928 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1929 if (af && af != ifa->ifa_addr->sa_family)
1931 if (prison_if(w->w_req->td->td_ucred,
1932 ifa->ifa_addr) != 0)
1934 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1935 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1936 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1937 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1938 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1941 if (w->w_req && w->w_tmem) {
1942 if (w->w_op == NET_RT_IFLISTL)
1943 error = sysctl_iflist_ifaml(ifa, &info,
1946 error = sysctl_iflist_ifam(ifa, &info,
1952 info.rti_info[RTAX_IFA] = NULL;
1953 info.rti_info[RTAX_NETMASK] = NULL;
1954 info.rti_info[RTAX_BRD] = NULL;
1961 sysctl_ifmalist(int af, struct walkarg *w)
1963 struct rt_addrinfo info;
1965 struct ifmultiaddr *ifma;
1972 bzero((caddr_t)&info, sizeof(info));
1974 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1975 if (w->w_arg && w->w_arg != ifp->if_index)
1978 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1979 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1980 if (af && af != ifma->ifma_addr->sa_family)
1982 if (prison_if(w->w_req->td->td_ucred,
1983 ifma->ifma_addr) != 0)
1985 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1986 info.rti_info[RTAX_GATEWAY] =
1987 (ifma->ifma_addr->sa_family != AF_LINK) ?
1988 ifma->ifma_lladdr : NULL;
1989 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1992 if (w->w_req && w->w_tmem) {
1993 struct ifma_msghdr *ifmam;
1995 ifmam = (struct ifma_msghdr *)w->w_tmem;
1996 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1997 ifmam->ifmam_flags = 0;
1998 ifmam->ifmam_addrs = info.rti_addrs;
1999 ifmam->_ifmam_spare1 = 0;
2000 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2012 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2015 struct epoch_tracker et;
2016 int *name = (int *)arg1;
2017 u_int namelen = arg2;
2018 struct rib_head *rnh = NULL; /* silence compiler. */
2019 int i, lim, error = EINVAL;
2028 if (name[1] == NET_RT_DUMP) {
2030 fib = req->td->td_proc->p_fibnum;
2031 else if (namelen == 4)
2032 fib = (name[3] == RT_ALL_FIBS) ?
2033 req->td->td_proc->p_fibnum : name[3];
2035 return ((namelen < 3) ? EISDIR : ENOTDIR);
2036 if (fib < 0 || fib >= rt_numfibs)
2038 } else if (namelen != 3)
2039 return ((namelen < 3) ? EISDIR : ENOTDIR);
2043 bzero(&w, sizeof(w));
2048 error = sysctl_wire_old_buffer(req, 0);
2053 * Allocate reply buffer in advance.
2054 * All rtsock messages has maximum length of u_short.
2056 w.w_tmemsize = 65536;
2057 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2059 NET_EPOCH_ENTER(et);
2063 if (af == 0) { /* dump all tables */
2066 } else /* dump only one table */
2070 * take care of llinfo entries, the caller must
2073 if (w.w_op == NET_RT_FLAGS &&
2074 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2076 error = lltable_sysctl_dumparp(af, w.w_req);
2082 * take care of routing entries
2084 for (error = 0; error == 0 && i <= lim; i++) {
2085 rnh = rt_tables_get_rnh(fib, i);
2088 error = rnh->rnh_walktree(&rnh->head,
2089 sysctl_dumpentry, &w);
2092 error = EAFNOSUPPORT;
2097 case NET_RT_IFLISTL:
2098 error = sysctl_iflist(af, &w);
2101 case NET_RT_IFMALIST:
2102 error = sysctl_ifmalist(af, &w);
2107 free(w.w_tmem, M_TEMP);
2111 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2112 sysctl_rtsock, "Return route tables and interface/address lists");
2115 * Definitions of protocols supported in the ROUTE domain.
2118 static struct domain routedomain; /* or at least forward */
2120 static struct protosw routesw[] = {
2122 .pr_type = SOCK_RAW,
2123 .pr_domain = &routedomain,
2124 .pr_flags = PR_ATOMIC|PR_ADDR,
2125 .pr_output = route_output,
2126 .pr_ctlinput = raw_ctlinput,
2127 .pr_init = raw_init,
2128 .pr_usrreqs = &route_usrreqs
2132 static struct domain routedomain = {
2133 .dom_family = PF_ROUTE,
2134 .dom_name = "route",
2135 .dom_protosw = routesw,
2136 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2139 VNET_DOMAIN_SET(route);
2143 * Unfortunately, RTF_ values are expressed as raw masks rather than powers of
2144 * 2, so we cannot use them as nice C99 initializer indices below.
2146 static const char * const rtf_flag_strings[] = {
2173 [30] = "RNH_LOCKED",
2174 [31] = "GWFLAG_COMPAT",
2177 static const char * __pure
2178 rt_flag_name(unsigned idx)
2180 if (idx >= nitems(rtf_flag_strings))
2181 return ("INVALID_FLAG");
2182 if (rtf_flag_strings[idx] == NULL)
2184 return (rtf_flag_strings[idx]);
2188 rt_dumpaddr_ddb(const char *name, const struct sockaddr *sa)
2190 char buf[INET6_ADDRSTRLEN], *res;
2195 else if (sa->sa_family == AF_INET) {
2196 res = inet_ntop(AF_INET,
2197 &((const struct sockaddr_in *)sa)->sin_addr,
2199 } else if (sa->sa_family == AF_INET6) {
2200 res = inet_ntop(AF_INET6,
2201 &((const struct sockaddr_in6 *)sa)->sin6_addr,
2203 } else if (sa->sa_family == AF_LINK) {
2208 db_printf("%s <%s> ", name, res);
2212 db_printf("%s <af:%d> ", name, sa->sa_family);
2216 rt_dumpentry_ddb(struct radix_node *rn, void *arg __unused)
2218 struct sockaddr_storage ss;
2222 /* If RNTORT is important, put it in a header. */
2225 rt_dumpaddr_ddb("dst", rt_key(rt));
2226 rt_dumpaddr_ddb("gateway", rt->rt_gateway);
2227 rt_dumpaddr_ddb("netmask", rtsock_fix_netmask(rt_key(rt), rt_mask(rt),
2229 if (rt->rt_ifp != NULL && (rt->rt_ifp->if_flags & IFF_DYING) == 0) {
2230 rt_dumpaddr_ddb("ifp", rt->rt_ifp->if_addr->ifa_addr);
2231 rt_dumpaddr_ddb("ifa", rt->rt_ifa->ifa_addr);
2234 db_printf("flags ");
2235 flags = rt->rt_flags;
2239 while ((idx = ffs(flags)) > 0) {
2242 if (flags != rt->rt_flags)
2244 db_printf("%s", rt_flag_name(idx));
2246 flags &= ~(1ul << idx);
2253 DB_SHOW_COMMAND(routetable, db_show_routetable_cmd)
2255 struct rib_head *rnh;
2265 for (; i <= lim; i++) {
2266 rnh = rt_tables_get_rnh(0, i);
2269 db_printf("%s: AF %d not supported?\n",
2276 if (!have_addr && i > 1)
2279 db_printf("Route table for AF %d%s%s%s:\n", i,
2280 (i == AF_INET || i == AF_INET6) ? " (" : "",
2281 (i == AF_INET) ? "INET" : (i == AF_INET6) ? "INET6" : "",
2282 (i == AF_INET || i == AF_INET6) ? ")" : "");
2284 error = rnh->rnh_walktree(&rnh->head, rt_dumpentry_ddb, NULL);
2286 db_printf("%s: walktree(%d): %d\n", __func__, i,
2291 _DB_FUNC(_show, route, db_show_route_cmd, db_show_table, CS_OWN, NULL)
2293 char buf[INET6_ADDRSTRLEN], *bp;
2294 const void *dst_addrp;
2295 struct sockaddr *dstp;
2298 struct sockaddr_in dest_sin;
2299 struct sockaddr_in6 dest_sin6;
2301 uint16_t hextets[8];
2303 int t, af, exp, tokflags;
2306 * Undecoded address family. No double-colon expansion seen yet.
2310 /* Assume INET6 to start; we can work back if guess was wrong. */
2311 tokflags = DRT_WSPACE | DRT_HEX | DRT_HEXADECIMAL;
2314 * db_command has lexed 'show route' for us.
2316 t = db_read_token_flags(tokflags);
2318 t = db_read_token_flags(tokflags);
2321 * tEOL: Just 'show route' isn't a valid mode.
2322 * tMINUS: It's either '-h' or some invalid option. Regardless, usage.
2324 if (t == tEOL || t == tMINUS)
2329 tets = nitems(hextets);
2332 * Each loop iteration, we expect to read one octet (v4) or hextet
2333 * (v6), followed by an appropriate field separator ('.' or ':' or
2336 * At the start of each loop, we're looking for a number (octet or
2339 * INET6 addresses have a special case where they may begin with '::'.
2341 for (i = 0; i < tets; i++) {
2342 t = db_read_token_flags(tokflags);
2344 if (t == tCOLONCOLON) {
2345 /* INET6 with leading '::' or invalid. */
2347 db_printf("Parse error: unexpected extra "
2356 } else if (t == tNUMBER) {
2358 * Lexer separates out '-' as tMINUS, but make the
2359 * assumption explicit here.
2361 MPASS(db_tok_number >= 0);
2363 if (af == AF_INET && db_tok_number > UINT8_MAX) {
2364 db_printf("Not a valid v4 octet: %ld\n",
2365 (long)db_tok_number);
2368 hextets[i] = db_tok_number;
2369 } else if (t == tEOL) {
2371 * We can only detect the end of an IPv6 address in
2372 * compact representation with EOL.
2374 if (af != AF_INET6 || exp < 0) {
2375 db_printf("Parse failed. Got unexpected EOF "
2376 "when the address is not a compact-"
2377 "representation IPv6 address.\n");
2382 db_printf("Parse failed. Unexpected token %d.\n", t);
2386 /* Next, look for a separator, if appropriate. */
2390 t = db_read_token_flags(tokflags);
2396 if (t == tCOLONCOLON) {
2407 /* Need to fixup the first parsed number. */
2408 if (hextets[0] > 0x255 ||
2409 (hextets[0] & 0xf0) > 0x90 ||
2410 (hextets[0] & 0xf) > 9) {
2411 db_printf("Not a valid v4 octet: %x\n",
2417 dn = (hn >> 8) * 100 +
2418 ((hn >> 4) & 0xf) * 10 +
2423 /* Switch to decimal for remaining octets. */
2424 tokflags &= ~DRT_RADIX_MASK;
2425 tokflags |= DRT_DECIMAL;
2431 db_printf("Parse error. Unexpected token %d.\n", t);
2433 } else if (af == AF_INET) {
2436 db_printf("Expected '.' (%d) between octets but got "
2437 "(%d).\n", tDOT, t);
2440 } else if (af == AF_INET6) {
2443 if (t == tCOLONCOLON) {
2450 db_printf("Got bogus second '::' in v6 "
2456 * Handle in the earlier part of the loop
2457 * because we need to handle trailing :: too.
2463 db_printf("Expected ':' (%d) or '::' (%d) between "
2464 "hextets but got (%d).\n", tCOLON, tCOLONCOLON, t);
2469 /* Check for trailing garbage. */
2471 t = db_read_token_flags(tokflags);
2473 db_printf("Got unexpected garbage after address "
2480 * Need to expand compact INET6 addresses.
2482 * Technically '::' for a single ':0:' is MUST NOT but just in case,
2483 * don't bother expanding that form (exp >= 0 && i == tets case).
2485 if (af == AF_INET6 && exp >= 0 && i < tets) {
2487 memmove(&hextets[exp + 1 + (nitems(hextets) - i)],
2489 (i - (exp + 1)) * sizeof(hextets[0]));
2491 memset(&hextets[exp + 1], 0, (nitems(hextets) - i) *
2492 sizeof(hextets[0]));
2495 memset(&u, 0, sizeof(u));
2496 if (af == AF_INET) {
2497 u.dest_sin.sin_family = AF_INET;
2498 u.dest_sin.sin_len = sizeof(u.dest_sin);
2499 u.dest_sin.sin_addr.s_addr = htonl(
2500 ((uint32_t)hextets[0] << 24) |
2501 ((uint32_t)hextets[1] << 16) |
2502 ((uint32_t)hextets[2] << 8) |
2503 (uint32_t)hextets[3]);
2504 dstp = (void *)&u.dest_sin;
2505 dst_addrp = &u.dest_sin.sin_addr;
2506 } else if (af == AF_INET6) {
2507 u.dest_sin6.sin6_family = AF_INET6;
2508 u.dest_sin6.sin6_len = sizeof(u.dest_sin6);
2509 for (i = 0; i < nitems(hextets); i++)
2510 u.dest_sin6.sin6_addr.s6_addr16[i] = htons(hextets[i]);
2511 dstp = (void *)&u.dest_sin6;
2512 dst_addrp = &u.dest_sin6.sin6_addr;
2516 /* Appease Clang false positive: */
2520 bp = inet_ntop(af, dst_addrp, buf, sizeof(buf));
2522 db_printf("Looking up route to destination '%s'\n", bp);
2525 rt = rtalloc1(dstp, 0, RTF_RNH_LOCKED);
2529 db_printf("Could not get route for that server.\n");
2533 rt_dumpentry_ddb((void *)rt, NULL);
2538 db_printf("Usage: 'show route <address>'\n"
2539 " Currently accepts only dotted-decimal INET or colon-separated\n"
2540 " hextet INET6 addresses.\n");