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, 0, "");
167 struct sysctl_req *w_req;
170 static void rts_input(struct mbuf *m);
171 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
172 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
173 struct walkarg *w, int *plen);
174 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
175 struct rt_addrinfo *rtinfo);
176 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
177 static int sysctl_iflist(int af, struct walkarg *w);
178 static int sysctl_ifmalist(int af, struct walkarg *w);
179 static int route_output(struct mbuf *m, struct socket *so, ...);
180 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
181 static void rt_dispatch(struct mbuf *, sa_family_t);
182 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
183 struct sockaddr *smask, struct sockaddr_storage *dmask);
185 static struct netisr_handler rtsock_nh = {
187 .nh_handler = rts_input,
188 .nh_proto = NETISR_ROUTE,
189 .nh_policy = NETISR_POLICY_SOURCE,
193 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
197 netisr_getqlimit(&rtsock_nh, &qlimit);
198 error = sysctl_handle_int(oidp, &qlimit, 0, req);
199 if (error || !req->newptr)
203 return (netisr_setqlimit(&rtsock_nh, qlimit));
205 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
206 0, 0, sysctl_route_netisr_maxqlen, "I",
207 "maximum routing socket dispatch queue length");
214 if (IS_DEFAULT_VNET(curvnet)) {
215 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
216 rtsock_nh.nh_qlimit = tmp;
217 netisr_register(&rtsock_nh);
221 netisr_register_vnet(&rtsock_nh);
224 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
229 vnet_rts_uninit(void)
232 netisr_unregister_vnet(&rtsock_nh);
234 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
239 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
244 KASSERT(m != NULL, ("%s: m is NULL", __func__));
245 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
246 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
248 /* No filtering requested. */
249 if ((m->m_flags & RTS_FILTER_FIB) == 0)
252 /* Check if it is a rts and the fib matches the one of the socket. */
253 fibnum = M_GETFIB(m);
254 if (proto->sp_family != PF_ROUTE ||
255 rp->rcb_socket == NULL ||
256 rp->rcb_socket->so_fibnum == fibnum)
259 /* Filtering requested and no match, the socket shall be skipped. */
264 rts_input(struct mbuf *m)
266 struct sockproto route_proto;
267 unsigned short *family;
270 route_proto.sp_family = PF_ROUTE;
271 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
273 family = (unsigned short *)(tag + 1);
274 route_proto.sp_protocol = *family;
275 m_tag_delete(m, tag);
277 route_proto.sp_protocol = 0;
279 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
283 * It really doesn't make any sense at all for this code to share much
284 * with raw_usrreq.c, since its functionality is so restricted. XXX
287 rts_abort(struct socket *so)
290 raw_usrreqs.pru_abort(so);
294 rts_close(struct socket *so)
297 raw_usrreqs.pru_close(so);
300 /* pru_accept is EOPNOTSUPP */
303 rts_attach(struct socket *so, int proto, struct thread *td)
308 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
311 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
313 so->so_pcb = (caddr_t)rp;
314 so->so_fibnum = td->td_proc->p_fibnum;
315 error = raw_attach(so, proto);
323 switch(rp->rcb_proto.sp_protocol) {
325 V_route_cb.ip_count++;
328 V_route_cb.ip6_count++;
331 V_route_cb.any_count++;
334 so->so_options |= SO_USELOOPBACK;
339 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
342 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
346 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
349 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
352 /* pru_connect2 is EOPNOTSUPP */
353 /* pru_control is EOPNOTSUPP */
356 rts_detach(struct socket *so)
358 struct rawcb *rp = sotorawcb(so);
360 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
363 switch(rp->rcb_proto.sp_protocol) {
365 V_route_cb.ip_count--;
368 V_route_cb.ip6_count--;
371 V_route_cb.any_count--;
373 raw_usrreqs.pru_detach(so);
377 rts_disconnect(struct socket *so)
380 return (raw_usrreqs.pru_disconnect(so));
383 /* pru_listen is EOPNOTSUPP */
386 rts_peeraddr(struct socket *so, struct sockaddr **nam)
389 return (raw_usrreqs.pru_peeraddr(so, nam));
392 /* pru_rcvd is EOPNOTSUPP */
393 /* pru_rcvoob is EOPNOTSUPP */
396 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
397 struct mbuf *control, struct thread *td)
400 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
403 /* pru_sense is null */
406 rts_shutdown(struct socket *so)
409 return (raw_usrreqs.pru_shutdown(so));
413 rts_sockaddr(struct socket *so, struct sockaddr **nam)
416 return (raw_usrreqs.pru_sockaddr(so, nam));
419 static struct pr_usrreqs route_usrreqs = {
420 .pru_abort = rts_abort,
421 .pru_attach = rts_attach,
422 .pru_bind = rts_bind,
423 .pru_connect = rts_connect,
424 .pru_detach = rts_detach,
425 .pru_disconnect = rts_disconnect,
426 .pru_peeraddr = rts_peeraddr,
427 .pru_send = rts_send,
428 .pru_shutdown = rts_shutdown,
429 .pru_sockaddr = rts_sockaddr,
430 .pru_close = rts_close,
433 #ifndef _SOCKADDR_UNION_DEFINED
434 #define _SOCKADDR_UNION_DEFINED
436 * The union of all possible address formats we handle.
438 union sockaddr_union {
440 struct sockaddr_in sin;
441 struct sockaddr_in6 sin6;
443 #endif /* _SOCKADDR_UNION_DEFINED */
446 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
447 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
449 #if defined(INET) || defined(INET6)
450 struct epoch_tracker et;
453 /* First, see if the returned address is part of the jail. */
454 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
455 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
459 switch (info->rti_info[RTAX_DST]->sa_family) {
469 * Try to find an address on the given outgoing interface
470 * that belongs to the jail.
473 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
476 if (sa->sa_family != AF_INET)
478 ia = ((struct sockaddr_in *)sa)->sin_addr;
479 if (prison_check_ip4(cred, &ia) == 0) {
487 * As a last resort return the 'default' jail address.
489 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
491 if (prison_get_ip4(cred, &ia) != 0)
494 bzero(&saun->sin, sizeof(struct sockaddr_in));
495 saun->sin.sin_len = sizeof(struct sockaddr_in);
496 saun->sin.sin_family = AF_INET;
497 saun->sin.sin_addr.s_addr = ia.s_addr;
498 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
511 * Try to find an address on the given outgoing interface
512 * that belongs to the jail.
515 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
518 if (sa->sa_family != AF_INET6)
520 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
521 &ia6, sizeof(struct in6_addr));
522 if (prison_check_ip6(cred, &ia6) == 0) {
530 * As a last resort return the 'default' jail address.
532 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
534 if (prison_get_ip6(cred, &ia6) != 0)
537 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
538 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
539 saun->sin6.sin6_family = AF_INET6;
540 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
541 if (sa6_recoverscope(&saun->sin6) != 0)
543 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
555 route_output(struct mbuf *m, struct socket *so, ...)
558 struct rt_msghdr *rtm = NULL;
559 struct rtentry *rt = NULL;
560 struct rib_head *rnh;
561 struct rt_addrinfo info;
562 struct sockaddr_storage ss;
563 struct epoch_tracker et;
565 struct sockaddr_in6 *sin6;
566 int i, rti_need_deembed = 0;
568 int alloc_len = 0, len, error = 0, fibnum;
569 struct ifnet *ifp = NULL;
570 union sockaddr_union saun;
571 sa_family_t saf = AF_UNSPEC;
572 struct rawcb *rp = NULL;
575 fibnum = so->so_fibnum;
577 #define senderr(e) { error = e; goto flush;}
578 if (m == NULL || ((m->m_len < sizeof(long)) &&
579 (m = m_pullup(m, sizeof(long))) == NULL))
581 if ((m->m_flags & M_PKTHDR) == 0)
582 panic("route_output");
584 len = m->m_pkthdr.len;
585 if (len < sizeof(*rtm) ||
586 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
590 * Most of current messages are in range 200-240 bytes,
591 * minimize possible re-allocation on reply using larger size
592 * buffer aligned on 1k boundaty.
594 alloc_len = roundup2(len, 1024);
595 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
598 m_copydata(m, 0, len, (caddr_t)rtm);
599 bzero(&info, sizeof(info));
600 bzero(&w, sizeof(w));
602 if (rtm->rtm_version != RTM_VERSION) {
603 /* Do not touch message since format is unknown */
606 senderr(EPROTONOSUPPORT);
610 * Starting from here, it is possible
611 * to alter original message and insert
612 * caller PID and error value.
615 rtm->rtm_pid = curproc->p_pid;
616 info.rti_addrs = rtm->rtm_addrs;
618 info.rti_mflags = rtm->rtm_inits;
619 info.rti_rmx = &rtm->rtm_rmx;
622 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
623 * link-local address because rtrequest requires addresses with
626 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
629 if (rtm->rtm_flags & RTF_RNH_LOCKED)
631 info.rti_flags = rtm->rtm_flags;
632 if (info.rti_info[RTAX_DST] == NULL ||
633 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
634 (info.rti_info[RTAX_GATEWAY] != NULL &&
635 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
637 saf = info.rti_info[RTAX_DST]->sa_family;
639 * Verify that the caller has the appropriate privilege; RTM_GET
640 * is the only operation the non-superuser is allowed.
642 if (rtm->rtm_type != RTM_GET) {
643 error = priv_check(curthread, PRIV_NET_ROUTE);
649 * The given gateway address may be an interface address.
650 * For example, issuing a "route change" command on a route
651 * entry that was created from a tunnel, and the gateway
652 * address given is the local end point. In this case the
653 * RTF_GATEWAY flag must be cleared or the destination will
654 * not be reachable even though there is no error message.
656 if (info.rti_info[RTAX_GATEWAY] != NULL &&
657 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
658 struct rt_addrinfo ginfo;
659 struct sockaddr *gdst;
661 bzero(&ginfo, sizeof(ginfo));
662 bzero(&ss, sizeof(ss));
663 ss.ss_len = sizeof(ss);
665 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
666 gdst = info.rti_info[RTAX_GATEWAY];
669 * A host route through the loopback interface is
670 * installed for each interface adddress. In pre 8.0
671 * releases the interface address of a PPP link type
672 * is not reachable locally. This behavior is fixed as
673 * part of the new L2/L3 redesign and rewrite work. The
674 * signature of this interface address route is the
675 * AF_LINK sa_family type of the rt_gateway, and the
676 * rt_ifp has the IFF_LOOPBACK flag set.
678 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
679 if (ss.ss_family == AF_LINK &&
680 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
681 info.rti_flags &= ~RTF_GATEWAY;
682 info.rti_flags |= RTF_GWFLAG_COMPAT;
684 rib_free_info(&ginfo);
688 switch (rtm->rtm_type) {
689 struct rtentry *saved_nrt;
693 if (rtm->rtm_type == RTM_ADD) {
694 if (info.rti_info[RTAX_GATEWAY] == NULL)
699 /* support for new ARP code */
700 if (info.rti_info[RTAX_GATEWAY] != NULL &&
701 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
702 (rtm->rtm_flags & RTF_LLDATA) != 0) {
703 error = lla_rt_output(rtm, &info);
706 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
710 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
712 if (error == 0 && saved_nrt != NULL) {
714 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
717 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
718 RT_REMREF(saved_nrt);
719 RT_UNLOCK(saved_nrt);
725 /* support for new ARP code */
726 if (info.rti_info[RTAX_GATEWAY] &&
727 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
728 (rtm->rtm_flags & RTF_LLDATA) != 0) {
729 error = lla_rt_output(rtm, &info);
732 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
736 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
743 /* rt_msg2() will not be used when RTM_DELETE fails. */
744 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
749 rnh = rt_tables_get_rnh(fibnum, saf);
751 senderr(EAFNOSUPPORT);
755 if (info.rti_info[RTAX_NETMASK] == NULL &&
756 rtm->rtm_type == RTM_GET) {
758 * Provide longest prefix match for
759 * address lookup (no mask).
760 * 'route -n get addr'
762 rt = (struct rtentry *) rnh->rnh_matchaddr(
763 info.rti_info[RTAX_DST], &rnh->head);
765 rt = (struct rtentry *) rnh->rnh_lookup(
766 info.rti_info[RTAX_DST],
767 info.rti_info[RTAX_NETMASK], &rnh->head);
775 * for RTM_CHANGE/LOCK, if we got multipath routes,
776 * we require users to specify a matching RTAX_GATEWAY.
778 * for RTM_GET, gate is optional even with multipath.
779 * if gate == NULL the first match is returned.
780 * (no need to call rt_mpath_matchgate if gate == NULL)
782 if (rt_mpath_capable(rnh) &&
783 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
784 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
792 * If performing proxied L2 entry insertion, and
793 * the actual PPP host entry is found, perform
794 * another search to retrieve the prefix route of
795 * the local end point of the PPP link.
797 if (rtm->rtm_flags & RTF_ANNOUNCE) {
798 struct sockaddr laddr;
800 if (rt->rt_ifp != NULL &&
801 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
802 struct epoch_tracker et;
806 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
810 rt_maskedcopy(ifa->ifa_addr,
814 rt_maskedcopy(rt->rt_ifa->ifa_addr,
816 rt->rt_ifa->ifa_netmask);
818 * refactor rt and no lock operation necessary
820 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
833 if ((rt->rt_flags & RTF_HOST) == 0
834 ? jailed_without_vnet(curthread->td_ucred)
835 : prison_if(curthread->td_ucred,
840 info.rti_info[RTAX_DST] = rt_key(rt);
841 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
842 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
844 info.rti_info[RTAX_GENMASK] = 0;
845 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
848 info.rti_info[RTAX_IFP] =
849 ifp->if_addr->ifa_addr;
850 error = rtm_get_jailed(&info, ifp, rt,
851 &saun, curthread->td_ucred);
856 if (ifp->if_flags & IFF_POINTOPOINT)
857 info.rti_info[RTAX_BRD] =
858 rt->rt_ifa->ifa_dstaddr;
859 rtm->rtm_index = ifp->if_index;
861 info.rti_info[RTAX_IFP] = NULL;
862 info.rti_info[RTAX_IFA] = NULL;
864 } else if ((ifp = rt->rt_ifp) != NULL) {
865 rtm->rtm_index = ifp->if_index;
868 /* Check if we need to realloc storage */
869 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
870 if (len > alloc_len) {
871 struct rt_msghdr *new_rtm;
872 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
873 if (new_rtm == NULL) {
877 bcopy(rtm, new_rtm, rtm->rtm_msglen);
883 w.w_tmem = (caddr_t)rtm;
884 w.w_tmemsize = alloc_len;
885 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
887 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
888 rtm->rtm_flags = RTF_GATEWAY |
889 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
891 rtm->rtm_flags = rt->rt_flags;
892 rt_getmetrics(rt, &rtm->rtm_rmx);
893 rtm->rtm_addrs = info.rti_addrs;
907 * Check to see if we don't want our own messages.
909 if ((so->so_options & SO_USELOOPBACK) == 0) {
910 if (V_route_cb.any_count <= 1) {
916 /* There is another listener, so construct message */
922 if (rti_need_deembed) {
923 /* sin6_scope_id is recovered before sending rtm. */
924 sin6 = (struct sockaddr_in6 *)&ss;
925 for (i = 0; i < RTAX_MAX; i++) {
926 if (info.rti_info[i] == NULL)
928 if (info.rti_info[i]->sa_family != AF_INET6)
930 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
931 if (sa6_recoverscope(sin6) == 0)
932 bcopy(sin6, info.rti_info[i],
938 rtm->rtm_errno = error;
940 rtm->rtm_flags |= RTF_DONE;
942 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
943 if (m->m_pkthdr.len < rtm->rtm_msglen) {
946 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
947 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
953 m->m_flags |= RTS_FILTER_FIB;
956 * XXX insure we don't get a copy by
957 * invalidating our protocol
959 unsigned short family = rp->rcb_proto.sp_family;
960 rp->rcb_proto.sp_family = 0;
962 rp->rcb_proto.sp_family = family;
971 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
974 bzero(out, sizeof(*out));
975 out->rmx_mtu = rt->rt_mtu;
976 out->rmx_weight = rt->rt_weight;
977 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
978 /* Kernel -> userland timebase conversion. */
979 out->rmx_expire = rt->rt_expire ?
980 rt->rt_expire - time_uptime + time_second : 0;
984 * Extract the addresses of the passed sockaddrs.
985 * Do a little sanity checking so as to avoid bad memory references.
986 * This data is derived straight from userland.
989 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
994 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
995 if ((rtinfo->rti_addrs & (1 << i)) == 0)
997 sa = (struct sockaddr *)cp;
1001 if (cp + sa->sa_len > cplim)
1004 * there are no more.. quit now
1005 * If there are more bits, they are in error.
1006 * I've seen this. route(1) can evidently generate these.
1007 * This causes kernel to core dump.
1008 * for compatibility, If we see this, point to a safe address.
1010 if (sa->sa_len == 0) {
1011 rtinfo->rti_info[i] = &sa_zero;
1012 return (0); /* should be EINVAL but for compat */
1016 if (sa->sa_family == AF_INET6)
1017 sa6_embedscope((struct sockaddr_in6 *)sa,
1020 rtinfo->rti_info[i] = sa;
1027 * Fill in @dmask with valid netmask leaving original @smask
1028 * intact. Mostly used with radix netmasks.
1030 static struct sockaddr *
1031 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1032 struct sockaddr_storage *dmask)
1034 if (dst == NULL || smask == NULL)
1037 memset(dmask, 0, dst->sa_len);
1038 memcpy(dmask, smask, smask->sa_len);
1039 dmask->ss_len = dst->sa_len;
1040 dmask->ss_family = dst->sa_family;
1042 return ((struct sockaddr *)dmask);
1046 * Writes information related to @rtinfo object to newly-allocated mbuf.
1047 * Assumes MCLBYTES is enough to construct any message.
1048 * Used for OS notifications of vaious events (if/ifa announces,etc)
1050 * Returns allocated mbuf or NULL on failure.
1052 static struct mbuf *
1053 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1055 struct rt_msghdr *rtm;
1058 struct sockaddr *sa;
1060 struct sockaddr_storage ss;
1061 struct sockaddr_in6 *sin6;
1069 len = sizeof(struct ifa_msghdr);
1074 len = sizeof(struct ifma_msghdr);
1078 len = sizeof(struct if_msghdr);
1081 case RTM_IFANNOUNCE:
1083 len = sizeof(struct if_announcemsghdr);
1087 len = sizeof(struct rt_msghdr);
1090 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1091 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1093 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1095 m = m_gethdr(M_NOWAIT, MT_DATA);
1099 m->m_pkthdr.len = m->m_len = len;
1100 rtm = mtod(m, struct rt_msghdr *);
1101 bzero((caddr_t)rtm, len);
1102 for (i = 0; i < RTAX_MAX; i++) {
1103 if ((sa = rtinfo->rti_info[i]) == NULL)
1105 rtinfo->rti_addrs |= (1 << i);
1108 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1109 sin6 = (struct sockaddr_in6 *)&ss;
1110 bcopy(sa, sin6, sizeof(*sin6));
1111 if (sa6_recoverscope(sin6) == 0)
1112 sa = (struct sockaddr *)sin6;
1115 m_copyback(m, len, dlen, (caddr_t)sa);
1118 if (m->m_pkthdr.len != len) {
1122 rtm->rtm_msglen = len;
1123 rtm->rtm_version = RTM_VERSION;
1124 rtm->rtm_type = type;
1129 * Writes information related to @rtinfo object to preallocated buffer.
1130 * Stores needed size in @plen. If @w is NULL, calculates size without
1132 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1134 * Returns 0 on success.
1138 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1141 int len, buflen = 0, dlen;
1143 struct rt_msghdr *rtm = NULL;
1145 struct sockaddr_storage ss;
1146 struct sockaddr_in6 *sin6;
1148 #ifdef COMPAT_FREEBSD32
1149 bool compat32 = false;
1156 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1157 #ifdef COMPAT_FREEBSD32
1158 if (w->w_req->flags & SCTL_MASK32) {
1159 len = sizeof(struct ifa_msghdrl32);
1163 len = sizeof(struct ifa_msghdrl);
1165 len = sizeof(struct ifa_msghdr);
1169 #ifdef COMPAT_FREEBSD32
1170 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1171 if (w->w_op == NET_RT_IFLISTL)
1172 len = sizeof(struct if_msghdrl32);
1174 len = sizeof(struct if_msghdr32);
1179 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1180 len = sizeof(struct if_msghdrl);
1182 len = sizeof(struct if_msghdr);
1186 len = sizeof(struct ifma_msghdr);
1190 len = sizeof(struct rt_msghdr);
1194 rtm = (struct rt_msghdr *)w->w_tmem;
1195 buflen = w->w_tmemsize - len;
1196 cp = (caddr_t)w->w_tmem + len;
1199 rtinfo->rti_addrs = 0;
1200 for (i = 0; i < RTAX_MAX; i++) {
1201 struct sockaddr *sa;
1203 if ((sa = rtinfo->rti_info[i]) == NULL)
1205 rtinfo->rti_addrs |= (1 << i);
1206 #ifdef COMPAT_FREEBSD32
1208 dlen = SA_SIZE32(sa);
1212 if (cp != NULL && buflen >= dlen) {
1214 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1215 sin6 = (struct sockaddr_in6 *)&ss;
1216 bcopy(sa, sin6, sizeof(*sin6));
1217 if (sa6_recoverscope(sin6) == 0)
1218 sa = (struct sockaddr *)sin6;
1221 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1224 } else if (cp != NULL) {
1226 * Buffer too small. Count needed size
1227 * and return with error.
1236 dlen = ALIGN(len) - len;
1248 /* fill header iff buffer is large enough */
1249 rtm->rtm_version = RTM_VERSION;
1250 rtm->rtm_type = type;
1251 rtm->rtm_msglen = len;
1256 if (w != NULL && cp == NULL)
1263 * This routine is called to generate a message from the routing
1264 * socket indicating that a redirect has occurred, a routing lookup
1265 * has failed, or that a protocol has detected timeouts to a particular
1269 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1272 struct rt_msghdr *rtm;
1274 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1276 if (V_route_cb.any_count == 0)
1278 m = rtsock_msg_mbuf(type, rtinfo);
1282 if (fibnum != RT_ALL_FIBS) {
1283 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1284 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1285 M_SETFIB(m, fibnum);
1286 m->m_flags |= RTS_FILTER_FIB;
1289 rtm = mtod(m, struct rt_msghdr *);
1290 rtm->rtm_flags = RTF_DONE | flags;
1291 rtm->rtm_errno = error;
1292 rtm->rtm_addrs = rtinfo->rti_addrs;
1293 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1297 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1300 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1304 * This routine is called to generate a message from the routing
1305 * socket indicating that the status of a network interface has changed.
1308 rt_ifmsg(struct ifnet *ifp)
1310 struct if_msghdr *ifm;
1312 struct rt_addrinfo info;
1314 if (V_route_cb.any_count == 0)
1316 bzero((caddr_t)&info, sizeof(info));
1317 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1320 ifm = mtod(m, struct if_msghdr *);
1321 ifm->ifm_index = ifp->if_index;
1322 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1323 if_data_copy(ifp, &ifm->ifm_data);
1325 rt_dispatch(m, AF_UNSPEC);
1329 * Announce interface address arrival/withdraw.
1330 * Please do not call directly, use rt_addrmsg().
1331 * Assume input data to be valid.
1332 * Returns 0 on success.
1335 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1337 struct rt_addrinfo info;
1338 struct sockaddr *sa;
1341 struct ifa_msghdr *ifam;
1342 struct ifnet *ifp = ifa->ifa_ifp;
1343 struct sockaddr_storage ss;
1345 if (V_route_cb.any_count == 0)
1348 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1350 bzero((caddr_t)&info, sizeof(info));
1351 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1352 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1353 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1354 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1355 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1356 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1358 ifam = mtod(m, struct ifa_msghdr *);
1359 ifam->ifam_index = ifp->if_index;
1360 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1361 ifam->ifam_flags = ifa->ifa_flags;
1362 ifam->ifam_addrs = info.rti_addrs;
1364 if (fibnum != RT_ALL_FIBS) {
1365 M_SETFIB(m, fibnum);
1366 m->m_flags |= RTS_FILTER_FIB;
1369 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1375 * Announce route addition/removal.
1376 * Please do not call directly, use rt_routemsg().
1377 * Note that @rt data MAY be inconsistent/invalid:
1378 * if some userland app sends us "invalid" route message (invalid mask,
1379 * no dst, wrong address families, etc...) we need to pass it back
1380 * to app (and any other rtsock consumers) with rtm_errno field set to
1383 * Returns 0 on success.
1386 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1389 struct rt_addrinfo info;
1390 struct sockaddr *sa;
1392 struct rt_msghdr *rtm;
1393 struct sockaddr_storage ss;
1395 if (V_route_cb.any_count == 0)
1398 bzero((caddr_t)&info, sizeof(info));
1399 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1400 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1401 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1402 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1404 rtm = mtod(m, struct rt_msghdr *);
1405 rtm->rtm_index = ifp->if_index;
1406 rtm->rtm_flags |= rt->rt_flags;
1407 rtm->rtm_errno = error;
1408 rtm->rtm_addrs = info.rti_addrs;
1410 if (fibnum != RT_ALL_FIBS) {
1411 M_SETFIB(m, fibnum);
1412 m->m_flags |= RTS_FILTER_FIB;
1415 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1421 * This is the analogue to the rt_newaddrmsg which performs the same
1422 * function but for multicast group memberhips. This is easier since
1423 * there is no route state to worry about.
1426 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1428 struct rt_addrinfo info;
1429 struct mbuf *m = NULL;
1430 struct ifnet *ifp = ifma->ifma_ifp;
1431 struct ifma_msghdr *ifmam;
1433 if (V_route_cb.any_count == 0)
1436 bzero((caddr_t)&info, sizeof(info));
1437 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1438 if (ifp && ifp->if_addr)
1439 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1441 info.rti_info[RTAX_IFP] = NULL;
1443 * If a link-layer address is present, present it as a ``gateway''
1444 * (similarly to how ARP entries, e.g., are presented).
1446 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1447 m = rtsock_msg_mbuf(cmd, &info);
1450 ifmam = mtod(m, struct ifma_msghdr *);
1451 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1453 ifmam->ifmam_index = ifp->if_index;
1454 ifmam->ifmam_addrs = info.rti_addrs;
1455 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1458 static struct mbuf *
1459 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1460 struct rt_addrinfo *info)
1462 struct if_announcemsghdr *ifan;
1465 if (V_route_cb.any_count == 0)
1467 bzero((caddr_t)info, sizeof(*info));
1468 m = rtsock_msg_mbuf(type, info);
1470 ifan = mtod(m, struct if_announcemsghdr *);
1471 ifan->ifan_index = ifp->if_index;
1472 strlcpy(ifan->ifan_name, ifp->if_xname,
1473 sizeof(ifan->ifan_name));
1474 ifan->ifan_what = what;
1480 * This is called to generate routing socket messages indicating
1481 * IEEE80211 wireless events.
1482 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1485 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1488 struct rt_addrinfo info;
1490 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1493 * Append the ieee80211 data. Try to stick it in the
1494 * mbuf containing the ifannounce msg; otherwise allocate
1495 * a new mbuf and append.
1497 * NB: we assume m is a single mbuf.
1499 if (data_len > M_TRAILINGSPACE(m)) {
1500 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1505 bcopy(data, mtod(n, void *), data_len);
1506 n->m_len = data_len;
1508 } else if (data_len > 0) {
1509 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1510 m->m_len += data_len;
1512 if (m->m_flags & M_PKTHDR)
1513 m->m_pkthdr.len += data_len;
1514 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1515 rt_dispatch(m, AF_UNSPEC);
1520 * This is called to generate routing socket messages indicating
1521 * network interface arrival and departure.
1524 rt_ifannouncemsg(struct ifnet *ifp, int what)
1527 struct rt_addrinfo info;
1529 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1531 rt_dispatch(m, AF_UNSPEC);
1535 rt_dispatch(struct mbuf *m, sa_family_t saf)
1540 * Preserve the family from the sockaddr, if any, in an m_tag for
1541 * use when injecting the mbuf into the routing socket buffer from
1544 if (saf != AF_UNSPEC) {
1545 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1551 *(unsigned short *)(tag + 1) = saf;
1552 m_tag_prepend(m, tag);
1556 m->m_pkthdr.rcvif = V_loif;
1562 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1566 * This is used in dumping the kernel table via sysctl().
1569 sysctl_dumpentry(struct radix_node *rn, void *vw)
1571 struct walkarg *w = vw;
1572 struct rtentry *rt = (struct rtentry *)rn;
1573 int error = 0, size;
1574 struct rt_addrinfo info;
1575 struct sockaddr_storage ss;
1579 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1581 if ((rt->rt_flags & RTF_HOST) == 0
1582 ? jailed_without_vnet(w->w_req->td->td_ucred)
1583 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1585 bzero((caddr_t)&info, sizeof(info));
1586 info.rti_info[RTAX_DST] = rt_key(rt);
1587 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1588 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1590 info.rti_info[RTAX_GENMASK] = 0;
1591 if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1592 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1593 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1594 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1595 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1597 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1599 if (w->w_req && w->w_tmem) {
1600 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1602 bzero(&rtm->rtm_index,
1603 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1604 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1605 rtm->rtm_flags = RTF_GATEWAY |
1606 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1608 rtm->rtm_flags = rt->rt_flags;
1609 rt_getmetrics(rt, &rtm->rtm_rmx);
1610 rtm->rtm_index = rt->rt_ifp->if_index;
1611 rtm->rtm_addrs = info.rti_addrs;
1612 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1619 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1620 struct rt_addrinfo *info, struct walkarg *w, int len)
1622 struct if_msghdrl *ifm;
1623 struct if_data *ifd;
1625 ifm = (struct if_msghdrl *)w->w_tmem;
1627 #ifdef COMPAT_FREEBSD32
1628 if (w->w_req->flags & SCTL_MASK32) {
1629 struct if_msghdrl32 *ifm32;
1631 ifm32 = (struct if_msghdrl32 *)ifm;
1632 ifm32->ifm_addrs = info->rti_addrs;
1633 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1634 ifm32->ifm_index = ifp->if_index;
1635 ifm32->_ifm_spare1 = 0;
1636 ifm32->ifm_len = sizeof(*ifm32);
1637 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1638 ifm32->_ifm_spare2 = 0;
1639 ifd = &ifm32->ifm_data;
1643 ifm->ifm_addrs = info->rti_addrs;
1644 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1645 ifm->ifm_index = ifp->if_index;
1646 ifm->_ifm_spare1 = 0;
1647 ifm->ifm_len = sizeof(*ifm);
1648 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1649 ifm->_ifm_spare2 = 0;
1650 ifd = &ifm->ifm_data;
1653 memcpy(ifd, src_ifd, sizeof(*ifd));
1655 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1659 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1660 struct rt_addrinfo *info, struct walkarg *w, int len)
1662 struct if_msghdr *ifm;
1663 struct if_data *ifd;
1665 ifm = (struct if_msghdr *)w->w_tmem;
1667 #ifdef COMPAT_FREEBSD32
1668 if (w->w_req->flags & SCTL_MASK32) {
1669 struct if_msghdr32 *ifm32;
1671 ifm32 = (struct if_msghdr32 *)ifm;
1672 ifm32->ifm_addrs = info->rti_addrs;
1673 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1674 ifm32->ifm_index = ifp->if_index;
1675 ifm32->_ifm_spare1 = 0;
1676 ifd = &ifm32->ifm_data;
1680 ifm->ifm_addrs = info->rti_addrs;
1681 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1682 ifm->ifm_index = ifp->if_index;
1683 ifm->_ifm_spare1 = 0;
1684 ifd = &ifm->ifm_data;
1687 memcpy(ifd, src_ifd, sizeof(*ifd));
1689 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1693 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1694 struct walkarg *w, int len)
1696 struct ifa_msghdrl *ifam;
1697 struct if_data *ifd;
1699 ifam = (struct ifa_msghdrl *)w->w_tmem;
1701 #ifdef COMPAT_FREEBSD32
1702 if (w->w_req->flags & SCTL_MASK32) {
1703 struct ifa_msghdrl32 *ifam32;
1705 ifam32 = (struct ifa_msghdrl32 *)ifam;
1706 ifam32->ifam_addrs = info->rti_addrs;
1707 ifam32->ifam_flags = ifa->ifa_flags;
1708 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1709 ifam32->_ifam_spare1 = 0;
1710 ifam32->ifam_len = sizeof(*ifam32);
1711 ifam32->ifam_data_off =
1712 offsetof(struct ifa_msghdrl32, ifam_data);
1713 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1714 ifd = &ifam32->ifam_data;
1718 ifam->ifam_addrs = info->rti_addrs;
1719 ifam->ifam_flags = ifa->ifa_flags;
1720 ifam->ifam_index = ifa->ifa_ifp->if_index;
1721 ifam->_ifam_spare1 = 0;
1722 ifam->ifam_len = sizeof(*ifam);
1723 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1724 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1725 ifd = &ifam->ifam_data;
1728 bzero(ifd, sizeof(*ifd));
1729 ifd->ifi_datalen = sizeof(struct if_data);
1730 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1731 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1732 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1733 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1735 /* Fixup if_data carp(4) vhid. */
1736 if (carp_get_vhid_p != NULL)
1737 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1739 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1743 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1744 struct walkarg *w, int len)
1746 struct ifa_msghdr *ifam;
1748 ifam = (struct ifa_msghdr *)w->w_tmem;
1749 ifam->ifam_addrs = info->rti_addrs;
1750 ifam->ifam_flags = ifa->ifa_flags;
1751 ifam->ifam_index = ifa->ifa_ifp->if_index;
1752 ifam->_ifam_spare1 = 0;
1753 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1755 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1759 sysctl_iflist(int af, struct walkarg *w)
1764 struct rt_addrinfo info;
1766 struct sockaddr_storage ss;
1768 bzero((caddr_t)&info, sizeof(info));
1769 bzero(&ifd, sizeof(ifd));
1770 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1771 if (w->w_arg && w->w_arg != ifp->if_index)
1773 if_data_copy(ifp, &ifd);
1775 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1776 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1779 info.rti_info[RTAX_IFP] = NULL;
1780 if (w->w_req && w->w_tmem) {
1781 if (w->w_op == NET_RT_IFLISTL)
1782 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1785 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1790 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1791 if (af && af != ifa->ifa_addr->sa_family)
1793 if (prison_if(w->w_req->td->td_ucred,
1794 ifa->ifa_addr) != 0)
1796 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1797 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1798 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1799 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1800 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1803 if (w->w_req && w->w_tmem) {
1804 if (w->w_op == NET_RT_IFLISTL)
1805 error = sysctl_iflist_ifaml(ifa, &info,
1808 error = sysctl_iflist_ifam(ifa, &info,
1814 info.rti_info[RTAX_IFA] = NULL;
1815 info.rti_info[RTAX_NETMASK] = NULL;
1816 info.rti_info[RTAX_BRD] = NULL;
1823 sysctl_ifmalist(int af, struct walkarg *w)
1825 struct rt_addrinfo info;
1827 struct ifmultiaddr *ifma;
1834 bzero((caddr_t)&info, sizeof(info));
1836 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1837 if (w->w_arg && w->w_arg != ifp->if_index)
1840 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1841 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1842 if (af && af != ifma->ifma_addr->sa_family)
1844 if (prison_if(w->w_req->td->td_ucred,
1845 ifma->ifma_addr) != 0)
1847 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1848 info.rti_info[RTAX_GATEWAY] =
1849 (ifma->ifma_addr->sa_family != AF_LINK) ?
1850 ifma->ifma_lladdr : NULL;
1851 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1854 if (w->w_req && w->w_tmem) {
1855 struct ifma_msghdr *ifmam;
1857 ifmam = (struct ifma_msghdr *)w->w_tmem;
1858 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1859 ifmam->ifmam_flags = 0;
1860 ifmam->ifmam_addrs = info.rti_addrs;
1861 ifmam->_ifmam_spare1 = 0;
1862 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1874 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1877 struct epoch_tracker et;
1878 int *name = (int *)arg1;
1879 u_int namelen = arg2;
1880 struct rib_head *rnh = NULL; /* silence compiler. */
1881 int i, lim, error = EINVAL;
1890 if (name[1] == NET_RT_DUMP) {
1892 fib = req->td->td_proc->p_fibnum;
1893 else if (namelen == 4)
1894 fib = (name[3] == RT_ALL_FIBS) ?
1895 req->td->td_proc->p_fibnum : name[3];
1897 return ((namelen < 3) ? EISDIR : ENOTDIR);
1898 if (fib < 0 || fib >= rt_numfibs)
1900 } else if (namelen != 3)
1901 return ((namelen < 3) ? EISDIR : ENOTDIR);
1905 bzero(&w, sizeof(w));
1910 error = sysctl_wire_old_buffer(req, 0);
1915 * Allocate reply buffer in advance.
1916 * All rtsock messages has maximum length of u_short.
1918 w.w_tmemsize = 65536;
1919 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1921 NET_EPOCH_ENTER(et);
1925 if (af == 0) { /* dump all tables */
1928 } else /* dump only one table */
1932 * take care of llinfo entries, the caller must
1935 if (w.w_op == NET_RT_FLAGS &&
1936 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1938 error = lltable_sysctl_dumparp(af, w.w_req);
1944 * take care of routing entries
1946 for (error = 0; error == 0 && i <= lim; i++) {
1947 rnh = rt_tables_get_rnh(fib, i);
1950 error = rnh->rnh_walktree(&rnh->head,
1951 sysctl_dumpentry, &w);
1954 error = EAFNOSUPPORT;
1959 case NET_RT_IFLISTL:
1960 error = sysctl_iflist(af, &w);
1963 case NET_RT_IFMALIST:
1964 error = sysctl_ifmalist(af, &w);
1969 free(w.w_tmem, M_TEMP);
1973 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1976 * Definitions of protocols supported in the ROUTE domain.
1979 static struct domain routedomain; /* or at least forward */
1981 static struct protosw routesw[] = {
1983 .pr_type = SOCK_RAW,
1984 .pr_domain = &routedomain,
1985 .pr_flags = PR_ATOMIC|PR_ADDR,
1986 .pr_output = route_output,
1987 .pr_ctlinput = raw_ctlinput,
1988 .pr_init = raw_init,
1989 .pr_usrreqs = &route_usrreqs
1993 static struct domain routedomain = {
1994 .dom_family = PF_ROUTE,
1995 .dom_name = "route",
1996 .dom_protosw = routesw,
1997 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2000 VNET_DOMAIN_SET(route);
2004 * Unfortunately, RTF_ values are expressed as raw masks rather than powers of
2005 * 2, so we cannot use them as nice C99 initializer indices below.
2007 static const char * const rtf_flag_strings[] = {
2034 [30] = "RNH_LOCKED",
2035 [31] = "GWFLAG_COMPAT",
2038 static const char * __pure
2039 rt_flag_name(unsigned idx)
2041 if (idx >= nitems(rtf_flag_strings))
2042 return ("INVALID_FLAG");
2043 if (rtf_flag_strings[idx] == NULL)
2045 return (rtf_flag_strings[idx]);
2049 rt_dumpaddr_ddb(const char *name, const struct sockaddr *sa)
2051 char buf[INET6_ADDRSTRLEN], *res;
2056 else if (sa->sa_family == AF_INET) {
2057 res = inet_ntop(AF_INET,
2058 &((const struct sockaddr_in *)sa)->sin_addr,
2060 } else if (sa->sa_family == AF_INET6) {
2061 res = inet_ntop(AF_INET6,
2062 &((const struct sockaddr_in6 *)sa)->sin6_addr,
2064 } else if (sa->sa_family == AF_LINK) {
2069 db_printf("%s <%s> ", name, res);
2073 db_printf("%s <af:%d> ", name, sa->sa_family);
2077 rt_dumpentry_ddb(struct radix_node *rn, void *arg __unused)
2079 struct sockaddr_storage ss;
2083 /* If RNTORT is important, put it in a header. */
2086 rt_dumpaddr_ddb("dst", rt_key(rt));
2087 rt_dumpaddr_ddb("gateway", rt->rt_gateway);
2088 rt_dumpaddr_ddb("netmask", rtsock_fix_netmask(rt_key(rt), rt_mask(rt),
2090 if (rt->rt_ifp != NULL && (rt->rt_ifp->if_flags & IFF_DYING) == 0) {
2091 rt_dumpaddr_ddb("ifp", rt->rt_ifp->if_addr->ifa_addr);
2092 rt_dumpaddr_ddb("ifa", rt->rt_ifa->ifa_addr);
2095 db_printf("flags ");
2096 flags = rt->rt_flags;
2100 while ((idx = ffs(flags)) > 0) {
2103 if (flags != rt->rt_flags)
2105 db_printf("%s", rt_flag_name(idx));
2107 flags &= ~(1ul << idx);
2114 DB_SHOW_COMMAND(routetable, db_show_routetable_cmd)
2116 struct rib_head *rnh;
2126 for (; i <= lim; i++) {
2127 rnh = rt_tables_get_rnh(0, i);
2130 db_printf("%s: AF %d not supported?\n",
2137 if (!have_addr && i > 1)
2140 db_printf("Route table for AF %d%s%s%s:\n", i,
2141 (i == AF_INET || i == AF_INET6) ? " (" : "",
2142 (i == AF_INET) ? "INET" : (i == AF_INET6) ? "INET6" : "",
2143 (i == AF_INET || i == AF_INET6) ? ")" : "");
2145 error = rnh->rnh_walktree(&rnh->head, rt_dumpentry_ddb, NULL);
2147 db_printf("%s: walktree(%d): %d\n", __func__, i,
2152 _DB_FUNC(_show, route, db_show_route_cmd, db_show_table, CS_OWN, NULL)
2154 char buf[INET6_ADDRSTRLEN], *bp;
2155 const void *dst_addrp;
2156 struct sockaddr *dstp;
2159 struct sockaddr_in dest_sin;
2160 struct sockaddr_in6 dest_sin6;
2162 uint16_t hextets[8];
2164 int t, af, exp, tokflags;
2167 * Undecoded address family. No double-colon expansion seen yet.
2171 /* Assume INET6 to start; we can work back if guess was wrong. */
2172 tokflags = DRT_WSPACE | DRT_HEX | DRT_HEXADECIMAL;
2175 * db_command has lexed 'show route' for us.
2177 t = db_read_token_flags(tokflags);
2179 t = db_read_token_flags(tokflags);
2182 * tEOL: Just 'show route' isn't a valid mode.
2183 * tMINUS: It's either '-h' or some invalid option. Regardless, usage.
2185 if (t == tEOL || t == tMINUS)
2190 tets = nitems(hextets);
2193 * Each loop iteration, we expect to read one octet (v4) or hextet
2194 * (v6), followed by an appropriate field separator ('.' or ':' or
2197 * At the start of each loop, we're looking for a number (octet or
2200 * INET6 addresses have a special case where they may begin with '::'.
2202 for (i = 0; i < tets; i++) {
2203 t = db_read_token_flags(tokflags);
2205 if (t == tCOLONCOLON) {
2206 /* INET6 with leading '::' or invalid. */
2208 db_printf("Parse error: unexpected extra "
2217 } else if (t == tNUMBER) {
2219 * Lexer separates out '-' as tMINUS, but make the
2220 * assumption explicit here.
2222 MPASS(db_tok_number >= 0);
2224 if (af == AF_INET && db_tok_number > UINT8_MAX) {
2225 db_printf("Not a valid v4 octet: %ld\n",
2226 (long)db_tok_number);
2229 hextets[i] = db_tok_number;
2230 } else if (t == tEOL) {
2232 * We can only detect the end of an IPv6 address in
2233 * compact representation with EOL.
2235 if (af != AF_INET6 || exp < 0) {
2236 db_printf("Parse failed. Got unexpected EOF "
2237 "when the address is not a compact-"
2238 "representation IPv6 address.\n");
2243 db_printf("Parse failed. Unexpected token %d.\n", t);
2247 /* Next, look for a separator, if appropriate. */
2251 t = db_read_token_flags(tokflags);
2257 if (t == tCOLONCOLON) {
2268 /* Need to fixup the first parsed number. */
2269 if (hextets[0] > 0x255 ||
2270 (hextets[0] & 0xf0) > 0x90 ||
2271 (hextets[0] & 0xf) > 9) {
2272 db_printf("Not a valid v4 octet: %x\n",
2278 dn = (hn >> 8) * 100 +
2279 ((hn >> 4) & 0xf) * 10 +
2284 /* Switch to decimal for remaining octets. */
2285 tokflags &= ~DRT_RADIX_MASK;
2286 tokflags |= DRT_DECIMAL;
2292 db_printf("Parse error. Unexpected token %d.\n", t);
2294 } else if (af == AF_INET) {
2297 db_printf("Expected '.' (%d) between octets but got "
2298 "(%d).\n", tDOT, t);
2301 } else if (af == AF_INET6) {
2304 if (t == tCOLONCOLON) {
2311 db_printf("Got bogus second '::' in v6 "
2317 * Handle in the earlier part of the loop
2318 * because we need to handle trailing :: too.
2324 db_printf("Expected ':' (%d) or '::' (%d) between "
2325 "hextets but got (%d).\n", tCOLON, tCOLONCOLON, t);
2330 /* Check for trailing garbage. */
2332 t = db_read_token_flags(tokflags);
2334 db_printf("Got unexpected garbage after address "
2341 * Need to expand compact INET6 addresses.
2343 * Technically '::' for a single ':0:' is MUST NOT but just in case,
2344 * don't bother expanding that form (exp >= 0 && i == tets case).
2346 if (af == AF_INET6 && exp >= 0 && i < tets) {
2348 memmove(&hextets[exp + 1 + (nitems(hextets) - i)],
2350 (i - (exp + 1)) * sizeof(hextets[0]));
2352 memset(&hextets[exp + 1], 0, (nitems(hextets) - i) *
2353 sizeof(hextets[0]));
2356 memset(&u, 0, sizeof(u));
2357 if (af == AF_INET) {
2358 u.dest_sin.sin_family = AF_INET;
2359 u.dest_sin.sin_len = sizeof(u.dest_sin);
2360 u.dest_sin.sin_addr.s_addr = htonl(
2361 ((uint32_t)hextets[0] << 24) |
2362 ((uint32_t)hextets[1] << 16) |
2363 ((uint32_t)hextets[2] << 8) |
2364 (uint32_t)hextets[3]);
2365 dstp = (void *)&u.dest_sin;
2366 dst_addrp = &u.dest_sin.sin_addr;
2367 } else if (af == AF_INET6) {
2368 u.dest_sin6.sin6_family = AF_INET6;
2369 u.dest_sin6.sin6_len = sizeof(u.dest_sin6);
2370 for (i = 0; i < nitems(hextets); i++)
2371 u.dest_sin6.sin6_addr.s6_addr16[i] = htons(hextets[i]);
2372 dstp = (void *)&u.dest_sin6;
2373 dst_addrp = &u.dest_sin6.sin6_addr;
2377 /* Appease Clang false positive: */
2381 bp = inet_ntop(af, dst_addrp, buf, sizeof(buf));
2383 db_printf("Looking up route to destination '%s'\n", bp);
2386 rt = rtalloc1(dstp, 0, RTF_RNH_LOCKED);
2390 db_printf("Could not get route for that server.\n");
2394 rt_dumpentry_ddb((void *)rt, NULL);
2399 db_printf("Usage: 'show route <address>'\n"
2400 " Currently accepts only dotted-decimal INET or colon-separated\n"
2401 " hextet INET6 addresses.\n");