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;
564 struct sockaddr_in6 *sin6;
565 int i, rti_need_deembed = 0;
567 int alloc_len = 0, len, error = 0, fibnum;
568 struct ifnet *ifp = NULL;
569 union sockaddr_union saun;
570 sa_family_t saf = AF_UNSPEC;
571 struct rawcb *rp = NULL;
574 fibnum = so->so_fibnum;
576 #define senderr(e) { error = e; goto flush;}
577 if (m == NULL || ((m->m_len < sizeof(long)) &&
578 (m = m_pullup(m, sizeof(long))) == NULL))
580 if ((m->m_flags & M_PKTHDR) == 0)
581 panic("route_output");
582 len = m->m_pkthdr.len;
583 if (len < sizeof(*rtm) ||
584 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
588 * Most of current messages are in range 200-240 bytes,
589 * minimize possible re-allocation on reply using larger size
590 * buffer aligned on 1k boundaty.
592 alloc_len = roundup2(len, 1024);
593 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
596 m_copydata(m, 0, len, (caddr_t)rtm);
597 bzero(&info, sizeof(info));
598 bzero(&w, sizeof(w));
600 if (rtm->rtm_version != RTM_VERSION) {
601 /* Do not touch message since format is unknown */
604 senderr(EPROTONOSUPPORT);
608 * Starting from here, it is possible
609 * to alter original message and insert
610 * caller PID and error value.
613 rtm->rtm_pid = curproc->p_pid;
614 info.rti_addrs = rtm->rtm_addrs;
616 info.rti_mflags = rtm->rtm_inits;
617 info.rti_rmx = &rtm->rtm_rmx;
620 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
621 * link-local address because rtrequest requires addresses with
624 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
627 if (rtm->rtm_flags & RTF_RNH_LOCKED)
629 info.rti_flags = rtm->rtm_flags;
630 if (info.rti_info[RTAX_DST] == NULL ||
631 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
632 (info.rti_info[RTAX_GATEWAY] != NULL &&
633 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
635 saf = info.rti_info[RTAX_DST]->sa_family;
637 * Verify that the caller has the appropriate privilege; RTM_GET
638 * is the only operation the non-superuser is allowed.
640 if (rtm->rtm_type != RTM_GET) {
641 error = priv_check(curthread, PRIV_NET_ROUTE);
647 * The given gateway address may be an interface address.
648 * For example, issuing a "route change" command on a route
649 * entry that was created from a tunnel, and the gateway
650 * address given is the local end point. In this case the
651 * RTF_GATEWAY flag must be cleared or the destination will
652 * not be reachable even though there is no error message.
654 if (info.rti_info[RTAX_GATEWAY] != NULL &&
655 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
656 struct rt_addrinfo ginfo;
657 struct sockaddr *gdst;
659 bzero(&ginfo, sizeof(ginfo));
660 bzero(&ss, sizeof(ss));
661 ss.ss_len = sizeof(ss);
663 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
664 gdst = info.rti_info[RTAX_GATEWAY];
667 * A host route through the loopback interface is
668 * installed for each interface adddress. In pre 8.0
669 * releases the interface address of a PPP link type
670 * is not reachable locally. This behavior is fixed as
671 * part of the new L2/L3 redesign and rewrite work. The
672 * signature of this interface address route is the
673 * AF_LINK sa_family type of the rt_gateway, and the
674 * rt_ifp has the IFF_LOOPBACK flag set.
676 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
677 if (ss.ss_family == AF_LINK &&
678 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
679 info.rti_flags &= ~RTF_GATEWAY;
680 info.rti_flags |= RTF_GWFLAG_COMPAT;
682 rib_free_info(&ginfo);
686 switch (rtm->rtm_type) {
687 struct rtentry *saved_nrt;
691 if (rtm->rtm_type == RTM_ADD) {
692 if (info.rti_info[RTAX_GATEWAY] == NULL)
697 /* support for new ARP code */
698 if (info.rti_info[RTAX_GATEWAY] != NULL &&
699 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
700 (rtm->rtm_flags & RTF_LLDATA) != 0) {
701 error = lla_rt_output(rtm, &info);
704 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
708 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
710 if (error == 0 && saved_nrt != NULL) {
712 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
715 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
716 RT_REMREF(saved_nrt);
717 RT_UNLOCK(saved_nrt);
723 /* support for new ARP code */
724 if (info.rti_info[RTAX_GATEWAY] &&
725 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
726 (rtm->rtm_flags & RTF_LLDATA) != 0) {
727 error = lla_rt_output(rtm, &info);
730 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
734 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
741 /* rt_msg2() will not be used when RTM_DELETE fails. */
742 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
747 rnh = rt_tables_get_rnh(fibnum, saf);
749 senderr(EAFNOSUPPORT);
753 if (info.rti_info[RTAX_NETMASK] == NULL &&
754 rtm->rtm_type == RTM_GET) {
756 * Provide longest prefix match for
757 * address lookup (no mask).
758 * 'route -n get addr'
760 rt = (struct rtentry *) rnh->rnh_matchaddr(
761 info.rti_info[RTAX_DST], &rnh->head);
763 rt = (struct rtentry *) rnh->rnh_lookup(
764 info.rti_info[RTAX_DST],
765 info.rti_info[RTAX_NETMASK], &rnh->head);
773 * for RTM_CHANGE/LOCK, if we got multipath routes,
774 * we require users to specify a matching RTAX_GATEWAY.
776 * for RTM_GET, gate is optional even with multipath.
777 * if gate == NULL the first match is returned.
778 * (no need to call rt_mpath_matchgate if gate == NULL)
780 if (rt_mpath_capable(rnh) &&
781 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
782 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
790 * If performing proxied L2 entry insertion, and
791 * the actual PPP host entry is found, perform
792 * another search to retrieve the prefix route of
793 * the local end point of the PPP link.
795 if (rtm->rtm_flags & RTF_ANNOUNCE) {
796 struct sockaddr laddr;
798 if (rt->rt_ifp != NULL &&
799 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
800 struct epoch_tracker et;
804 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
807 rt_maskedcopy(ifa->ifa_addr,
812 rt_maskedcopy(rt->rt_ifa->ifa_addr,
814 rt->rt_ifa->ifa_netmask);
816 * refactor rt and no lock operation necessary
818 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
831 if ((rt->rt_flags & RTF_HOST) == 0
832 ? jailed_without_vnet(curthread->td_ucred)
833 : prison_if(curthread->td_ucred,
838 info.rti_info[RTAX_DST] = rt_key(rt);
839 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
840 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
842 info.rti_info[RTAX_GENMASK] = 0;
843 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
846 info.rti_info[RTAX_IFP] =
847 ifp->if_addr->ifa_addr;
848 error = rtm_get_jailed(&info, ifp, rt,
849 &saun, curthread->td_ucred);
854 if (ifp->if_flags & IFF_POINTOPOINT)
855 info.rti_info[RTAX_BRD] =
856 rt->rt_ifa->ifa_dstaddr;
857 rtm->rtm_index = ifp->if_index;
859 info.rti_info[RTAX_IFP] = NULL;
860 info.rti_info[RTAX_IFA] = NULL;
862 } else if ((ifp = rt->rt_ifp) != NULL) {
863 rtm->rtm_index = ifp->if_index;
866 /* Check if we need to realloc storage */
867 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
868 if (len > alloc_len) {
869 struct rt_msghdr *new_rtm;
870 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
871 if (new_rtm == NULL) {
875 bcopy(rtm, new_rtm, rtm->rtm_msglen);
881 w.w_tmem = (caddr_t)rtm;
882 w.w_tmemsize = alloc_len;
883 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
885 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
886 rtm->rtm_flags = RTF_GATEWAY |
887 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
889 rtm->rtm_flags = rt->rt_flags;
890 rt_getmetrics(rt, &rtm->rtm_rmx);
891 rtm->rtm_addrs = info.rti_addrs;
904 * Check to see if we don't want our own messages.
906 if ((so->so_options & SO_USELOOPBACK) == 0) {
907 if (V_route_cb.any_count <= 1) {
913 /* There is another listener, so construct message */
919 if (rti_need_deembed) {
920 /* sin6_scope_id is recovered before sending rtm. */
921 sin6 = (struct sockaddr_in6 *)&ss;
922 for (i = 0; i < RTAX_MAX; i++) {
923 if (info.rti_info[i] == NULL)
925 if (info.rti_info[i]->sa_family != AF_INET6)
927 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
928 if (sa6_recoverscope(sin6) == 0)
929 bcopy(sin6, info.rti_info[i],
935 rtm->rtm_errno = error;
937 rtm->rtm_flags |= RTF_DONE;
939 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
940 if (m->m_pkthdr.len < rtm->rtm_msglen) {
943 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
944 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
950 m->m_flags |= RTS_FILTER_FIB;
953 * XXX insure we don't get a copy by
954 * invalidating our protocol
956 unsigned short family = rp->rcb_proto.sp_family;
957 rp->rcb_proto.sp_family = 0;
959 rp->rcb_proto.sp_family = family;
968 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
971 bzero(out, sizeof(*out));
972 out->rmx_mtu = rt->rt_mtu;
973 out->rmx_weight = rt->rt_weight;
974 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
975 /* Kernel -> userland timebase conversion. */
976 out->rmx_expire = rt->rt_expire ?
977 rt->rt_expire - time_uptime + time_second : 0;
981 * Extract the addresses of the passed sockaddrs.
982 * Do a little sanity checking so as to avoid bad memory references.
983 * This data is derived straight from userland.
986 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
991 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
992 if ((rtinfo->rti_addrs & (1 << i)) == 0)
994 sa = (struct sockaddr *)cp;
998 if (cp + sa->sa_len > cplim)
1001 * there are no more.. quit now
1002 * If there are more bits, they are in error.
1003 * I've seen this. route(1) can evidently generate these.
1004 * This causes kernel to core dump.
1005 * for compatibility, If we see this, point to a safe address.
1007 if (sa->sa_len == 0) {
1008 rtinfo->rti_info[i] = &sa_zero;
1009 return (0); /* should be EINVAL but for compat */
1013 if (sa->sa_family == AF_INET6)
1014 sa6_embedscope((struct sockaddr_in6 *)sa,
1017 rtinfo->rti_info[i] = sa;
1024 * Fill in @dmask with valid netmask leaving original @smask
1025 * intact. Mostly used with radix netmasks.
1027 static struct sockaddr *
1028 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1029 struct sockaddr_storage *dmask)
1031 if (dst == NULL || smask == NULL)
1034 memset(dmask, 0, dst->sa_len);
1035 memcpy(dmask, smask, smask->sa_len);
1036 dmask->ss_len = dst->sa_len;
1037 dmask->ss_family = dst->sa_family;
1039 return ((struct sockaddr *)dmask);
1043 * Writes information related to @rtinfo object to newly-allocated mbuf.
1044 * Assumes MCLBYTES is enough to construct any message.
1045 * Used for OS notifications of vaious events (if/ifa announces,etc)
1047 * Returns allocated mbuf or NULL on failure.
1049 static struct mbuf *
1050 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1052 struct rt_msghdr *rtm;
1055 struct sockaddr *sa;
1057 struct sockaddr_storage ss;
1058 struct sockaddr_in6 *sin6;
1066 len = sizeof(struct ifa_msghdr);
1071 len = sizeof(struct ifma_msghdr);
1075 len = sizeof(struct if_msghdr);
1078 case RTM_IFANNOUNCE:
1080 len = sizeof(struct if_announcemsghdr);
1084 len = sizeof(struct rt_msghdr);
1087 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1088 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1090 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1092 m = m_gethdr(M_NOWAIT, MT_DATA);
1096 m->m_pkthdr.len = m->m_len = len;
1097 rtm = mtod(m, struct rt_msghdr *);
1098 bzero((caddr_t)rtm, len);
1099 for (i = 0; i < RTAX_MAX; i++) {
1100 if ((sa = rtinfo->rti_info[i]) == NULL)
1102 rtinfo->rti_addrs |= (1 << i);
1105 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1106 sin6 = (struct sockaddr_in6 *)&ss;
1107 bcopy(sa, sin6, sizeof(*sin6));
1108 if (sa6_recoverscope(sin6) == 0)
1109 sa = (struct sockaddr *)sin6;
1112 m_copyback(m, len, dlen, (caddr_t)sa);
1115 if (m->m_pkthdr.len != len) {
1119 rtm->rtm_msglen = len;
1120 rtm->rtm_version = RTM_VERSION;
1121 rtm->rtm_type = type;
1126 * Writes information related to @rtinfo object to preallocated buffer.
1127 * Stores needed size in @plen. If @w is NULL, calculates size without
1129 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1131 * Returns 0 on success.
1135 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1138 int len, buflen = 0, dlen;
1140 struct rt_msghdr *rtm = NULL;
1142 struct sockaddr_storage ss;
1143 struct sockaddr_in6 *sin6;
1145 #ifdef COMPAT_FREEBSD32
1146 bool compat32 = false;
1153 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1154 #ifdef COMPAT_FREEBSD32
1155 if (w->w_req->flags & SCTL_MASK32) {
1156 len = sizeof(struct ifa_msghdrl32);
1160 len = sizeof(struct ifa_msghdrl);
1162 len = sizeof(struct ifa_msghdr);
1166 #ifdef COMPAT_FREEBSD32
1167 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1168 if (w->w_op == NET_RT_IFLISTL)
1169 len = sizeof(struct if_msghdrl32);
1171 len = sizeof(struct if_msghdr32);
1176 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1177 len = sizeof(struct if_msghdrl);
1179 len = sizeof(struct if_msghdr);
1183 len = sizeof(struct ifma_msghdr);
1187 len = sizeof(struct rt_msghdr);
1191 rtm = (struct rt_msghdr *)w->w_tmem;
1192 buflen = w->w_tmemsize - len;
1193 cp = (caddr_t)w->w_tmem + len;
1196 rtinfo->rti_addrs = 0;
1197 for (i = 0; i < RTAX_MAX; i++) {
1198 struct sockaddr *sa;
1200 if ((sa = rtinfo->rti_info[i]) == NULL)
1202 rtinfo->rti_addrs |= (1 << i);
1203 #ifdef COMPAT_FREEBSD32
1205 dlen = SA_SIZE32(sa);
1209 if (cp != NULL && buflen >= dlen) {
1211 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1212 sin6 = (struct sockaddr_in6 *)&ss;
1213 bcopy(sa, sin6, sizeof(*sin6));
1214 if (sa6_recoverscope(sin6) == 0)
1215 sa = (struct sockaddr *)sin6;
1218 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1221 } else if (cp != NULL) {
1223 * Buffer too small. Count needed size
1224 * and return with error.
1233 dlen = ALIGN(len) - len;
1245 /* fill header iff buffer is large enough */
1246 rtm->rtm_version = RTM_VERSION;
1247 rtm->rtm_type = type;
1248 rtm->rtm_msglen = len;
1253 if (w != NULL && cp == NULL)
1260 * This routine is called to generate a message from the routing
1261 * socket indicating that a redirect has occurred, a routing lookup
1262 * has failed, or that a protocol has detected timeouts to a particular
1266 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1269 struct rt_msghdr *rtm;
1271 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1273 if (V_route_cb.any_count == 0)
1275 m = rtsock_msg_mbuf(type, rtinfo);
1279 if (fibnum != RT_ALL_FIBS) {
1280 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1281 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1282 M_SETFIB(m, fibnum);
1283 m->m_flags |= RTS_FILTER_FIB;
1286 rtm = mtod(m, struct rt_msghdr *);
1287 rtm->rtm_flags = RTF_DONE | flags;
1288 rtm->rtm_errno = error;
1289 rtm->rtm_addrs = rtinfo->rti_addrs;
1290 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1294 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1297 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1301 * This routine is called to generate a message from the routing
1302 * socket indicating that the status of a network interface has changed.
1305 rt_ifmsg(struct ifnet *ifp)
1307 struct if_msghdr *ifm;
1309 struct rt_addrinfo info;
1311 if (V_route_cb.any_count == 0)
1313 bzero((caddr_t)&info, sizeof(info));
1314 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1317 ifm = mtod(m, struct if_msghdr *);
1318 ifm->ifm_index = ifp->if_index;
1319 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1320 if_data_copy(ifp, &ifm->ifm_data);
1322 rt_dispatch(m, AF_UNSPEC);
1326 * Announce interface address arrival/withdraw.
1327 * Please do not call directly, use rt_addrmsg().
1328 * Assume input data to be valid.
1329 * Returns 0 on success.
1332 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1334 struct rt_addrinfo info;
1335 struct sockaddr *sa;
1338 struct ifa_msghdr *ifam;
1339 struct ifnet *ifp = ifa->ifa_ifp;
1340 struct sockaddr_storage ss;
1342 if (V_route_cb.any_count == 0)
1345 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1347 bzero((caddr_t)&info, sizeof(info));
1348 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1349 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1350 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1351 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1352 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1353 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1355 ifam = mtod(m, struct ifa_msghdr *);
1356 ifam->ifam_index = ifp->if_index;
1357 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1358 ifam->ifam_flags = ifa->ifa_flags;
1359 ifam->ifam_addrs = info.rti_addrs;
1361 if (fibnum != RT_ALL_FIBS) {
1362 M_SETFIB(m, fibnum);
1363 m->m_flags |= RTS_FILTER_FIB;
1366 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1372 * Announce route addition/removal.
1373 * Please do not call directly, use rt_routemsg().
1374 * Note that @rt data MAY be inconsistent/invalid:
1375 * if some userland app sends us "invalid" route message (invalid mask,
1376 * no dst, wrong address families, etc...) we need to pass it back
1377 * to app (and any other rtsock consumers) with rtm_errno field set to
1380 * Returns 0 on success.
1383 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1386 struct rt_addrinfo info;
1387 struct sockaddr *sa;
1389 struct rt_msghdr *rtm;
1390 struct sockaddr_storage ss;
1392 if (V_route_cb.any_count == 0)
1395 bzero((caddr_t)&info, sizeof(info));
1396 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1397 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1398 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1399 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1401 rtm = mtod(m, struct rt_msghdr *);
1402 rtm->rtm_index = ifp->if_index;
1403 rtm->rtm_flags |= rt->rt_flags;
1404 rtm->rtm_errno = error;
1405 rtm->rtm_addrs = info.rti_addrs;
1407 if (fibnum != RT_ALL_FIBS) {
1408 M_SETFIB(m, fibnum);
1409 m->m_flags |= RTS_FILTER_FIB;
1412 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1418 * This is the analogue to the rt_newaddrmsg which performs the same
1419 * function but for multicast group memberhips. This is easier since
1420 * there is no route state to worry about.
1423 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1425 struct rt_addrinfo info;
1426 struct mbuf *m = NULL;
1427 struct ifnet *ifp = ifma->ifma_ifp;
1428 struct ifma_msghdr *ifmam;
1430 if (V_route_cb.any_count == 0)
1433 bzero((caddr_t)&info, sizeof(info));
1434 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1435 if (ifp && ifp->if_addr)
1436 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1438 info.rti_info[RTAX_IFP] = NULL;
1440 * If a link-layer address is present, present it as a ``gateway''
1441 * (similarly to how ARP entries, e.g., are presented).
1443 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1444 m = rtsock_msg_mbuf(cmd, &info);
1447 ifmam = mtod(m, struct ifma_msghdr *);
1448 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1450 ifmam->ifmam_index = ifp->if_index;
1451 ifmam->ifmam_addrs = info.rti_addrs;
1452 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1455 static struct mbuf *
1456 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1457 struct rt_addrinfo *info)
1459 struct if_announcemsghdr *ifan;
1462 if (V_route_cb.any_count == 0)
1464 bzero((caddr_t)info, sizeof(*info));
1465 m = rtsock_msg_mbuf(type, info);
1467 ifan = mtod(m, struct if_announcemsghdr *);
1468 ifan->ifan_index = ifp->if_index;
1469 strlcpy(ifan->ifan_name, ifp->if_xname,
1470 sizeof(ifan->ifan_name));
1471 ifan->ifan_what = what;
1477 * This is called to generate routing socket messages indicating
1478 * IEEE80211 wireless events.
1479 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1482 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1485 struct rt_addrinfo info;
1487 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1490 * Append the ieee80211 data. Try to stick it in the
1491 * mbuf containing the ifannounce msg; otherwise allocate
1492 * a new mbuf and append.
1494 * NB: we assume m is a single mbuf.
1496 if (data_len > M_TRAILINGSPACE(m)) {
1497 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1502 bcopy(data, mtod(n, void *), data_len);
1503 n->m_len = data_len;
1505 } else if (data_len > 0) {
1506 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1507 m->m_len += data_len;
1509 if (m->m_flags & M_PKTHDR)
1510 m->m_pkthdr.len += data_len;
1511 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1512 rt_dispatch(m, AF_UNSPEC);
1517 * This is called to generate routing socket messages indicating
1518 * network interface arrival and departure.
1521 rt_ifannouncemsg(struct ifnet *ifp, int what)
1524 struct rt_addrinfo info;
1526 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1528 rt_dispatch(m, AF_UNSPEC);
1532 rt_dispatch(struct mbuf *m, sa_family_t saf)
1537 * Preserve the family from the sockaddr, if any, in an m_tag for
1538 * use when injecting the mbuf into the routing socket buffer from
1541 if (saf != AF_UNSPEC) {
1542 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1548 *(unsigned short *)(tag + 1) = saf;
1549 m_tag_prepend(m, tag);
1553 m->m_pkthdr.rcvif = V_loif;
1559 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1563 * This is used in dumping the kernel table via sysctl().
1566 sysctl_dumpentry(struct radix_node *rn, void *vw)
1568 struct walkarg *w = vw;
1569 struct rtentry *rt = (struct rtentry *)rn;
1570 int error = 0, size;
1571 struct rt_addrinfo info;
1572 struct sockaddr_storage ss;
1576 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1578 if ((rt->rt_flags & RTF_HOST) == 0
1579 ? jailed_without_vnet(w->w_req->td->td_ucred)
1580 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1582 bzero((caddr_t)&info, sizeof(info));
1583 info.rti_info[RTAX_DST] = rt_key(rt);
1584 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1585 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1587 info.rti_info[RTAX_GENMASK] = 0;
1588 if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1589 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1590 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1591 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1592 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1594 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1596 if (w->w_req && w->w_tmem) {
1597 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1599 bzero(&rtm->rtm_index,
1600 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1601 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1602 rtm->rtm_flags = RTF_GATEWAY |
1603 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1605 rtm->rtm_flags = rt->rt_flags;
1606 rt_getmetrics(rt, &rtm->rtm_rmx);
1607 rtm->rtm_index = rt->rt_ifp->if_index;
1608 rtm->rtm_addrs = info.rti_addrs;
1609 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1616 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1617 struct rt_addrinfo *info, struct walkarg *w, int len)
1619 struct if_msghdrl *ifm;
1620 struct if_data *ifd;
1622 ifm = (struct if_msghdrl *)w->w_tmem;
1624 #ifdef COMPAT_FREEBSD32
1625 if (w->w_req->flags & SCTL_MASK32) {
1626 struct if_msghdrl32 *ifm32;
1628 ifm32 = (struct if_msghdrl32 *)ifm;
1629 ifm32->ifm_addrs = info->rti_addrs;
1630 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1631 ifm32->ifm_index = ifp->if_index;
1632 ifm32->_ifm_spare1 = 0;
1633 ifm32->ifm_len = sizeof(*ifm32);
1634 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1635 ifm32->_ifm_spare2 = 0;
1636 ifd = &ifm32->ifm_data;
1640 ifm->ifm_addrs = info->rti_addrs;
1641 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1642 ifm->ifm_index = ifp->if_index;
1643 ifm->_ifm_spare1 = 0;
1644 ifm->ifm_len = sizeof(*ifm);
1645 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1646 ifm->_ifm_spare2 = 0;
1647 ifd = &ifm->ifm_data;
1650 memcpy(ifd, src_ifd, sizeof(*ifd));
1652 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1656 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1657 struct rt_addrinfo *info, struct walkarg *w, int len)
1659 struct if_msghdr *ifm;
1660 struct if_data *ifd;
1662 ifm = (struct if_msghdr *)w->w_tmem;
1664 #ifdef COMPAT_FREEBSD32
1665 if (w->w_req->flags & SCTL_MASK32) {
1666 struct if_msghdr32 *ifm32;
1668 ifm32 = (struct if_msghdr32 *)ifm;
1669 ifm32->ifm_addrs = info->rti_addrs;
1670 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1671 ifm32->ifm_index = ifp->if_index;
1672 ifm32->_ifm_spare1 = 0;
1673 ifd = &ifm32->ifm_data;
1677 ifm->ifm_addrs = info->rti_addrs;
1678 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1679 ifm->ifm_index = ifp->if_index;
1680 ifm->_ifm_spare1 = 0;
1681 ifd = &ifm->ifm_data;
1684 memcpy(ifd, src_ifd, sizeof(*ifd));
1686 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1690 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1691 struct walkarg *w, int len)
1693 struct ifa_msghdrl *ifam;
1694 struct if_data *ifd;
1696 ifam = (struct ifa_msghdrl *)w->w_tmem;
1698 #ifdef COMPAT_FREEBSD32
1699 if (w->w_req->flags & SCTL_MASK32) {
1700 struct ifa_msghdrl32 *ifam32;
1702 ifam32 = (struct ifa_msghdrl32 *)ifam;
1703 ifam32->ifam_addrs = info->rti_addrs;
1704 ifam32->ifam_flags = ifa->ifa_flags;
1705 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1706 ifam32->_ifam_spare1 = 0;
1707 ifam32->ifam_len = sizeof(*ifam32);
1708 ifam32->ifam_data_off =
1709 offsetof(struct ifa_msghdrl32, ifam_data);
1710 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1711 ifd = &ifam32->ifam_data;
1715 ifam->ifam_addrs = info->rti_addrs;
1716 ifam->ifam_flags = ifa->ifa_flags;
1717 ifam->ifam_index = ifa->ifa_ifp->if_index;
1718 ifam->_ifam_spare1 = 0;
1719 ifam->ifam_len = sizeof(*ifam);
1720 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1721 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1722 ifd = &ifam->ifam_data;
1725 bzero(ifd, sizeof(*ifd));
1726 ifd->ifi_datalen = sizeof(struct if_data);
1727 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1728 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1729 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1730 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1732 /* Fixup if_data carp(4) vhid. */
1733 if (carp_get_vhid_p != NULL)
1734 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1736 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1740 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1741 struct walkarg *w, int len)
1743 struct ifa_msghdr *ifam;
1745 ifam = (struct ifa_msghdr *)w->w_tmem;
1746 ifam->ifam_addrs = info->rti_addrs;
1747 ifam->ifam_flags = ifa->ifa_flags;
1748 ifam->ifam_index = ifa->ifa_ifp->if_index;
1749 ifam->_ifam_spare1 = 0;
1750 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1752 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1756 sysctl_iflist(int af, struct walkarg *w)
1761 struct rt_addrinfo info;
1763 struct sockaddr_storage ss;
1764 struct epoch_tracker et;
1766 bzero((caddr_t)&info, sizeof(info));
1767 bzero(&ifd, sizeof(ifd));
1768 NET_EPOCH_ENTER(et);
1769 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1770 if (w->w_arg && w->w_arg != ifp->if_index)
1772 if_data_copy(ifp, &ifd);
1774 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1775 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1778 info.rti_info[RTAX_IFP] = NULL;
1779 if (w->w_req && w->w_tmem) {
1780 if (w->w_op == NET_RT_IFLISTL)
1781 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1784 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1789 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1790 if (af && af != ifa->ifa_addr->sa_family)
1792 if (prison_if(w->w_req->td->td_ucred,
1793 ifa->ifa_addr) != 0)
1795 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1796 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1797 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1798 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1799 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1802 if (w->w_req && w->w_tmem) {
1803 if (w->w_op == NET_RT_IFLISTL)
1804 error = sysctl_iflist_ifaml(ifa, &info,
1807 error = sysctl_iflist_ifam(ifa, &info,
1813 info.rti_info[RTAX_IFA] = NULL;
1814 info.rti_info[RTAX_NETMASK] = NULL;
1815 info.rti_info[RTAX_BRD] = NULL;
1823 sysctl_ifmalist(int af, struct walkarg *w)
1825 struct rt_addrinfo info;
1826 struct epoch_tracker et;
1828 struct ifmultiaddr *ifma;
1833 bzero((caddr_t)&info, sizeof(info));
1835 NET_EPOCH_ENTER(et);
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);
1875 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
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);
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);
1949 struct epoch_tracker et;
1952 NET_EPOCH_ENTER(et);
1953 error = rnh->rnh_walktree(&rnh->head,
1954 sysctl_dumpentry, &w);
1958 error = EAFNOSUPPORT;
1963 case NET_RT_IFLISTL:
1964 error = sysctl_iflist(af, &w);
1967 case NET_RT_IFMALIST:
1968 error = sysctl_ifmalist(af, &w);
1972 free(w.w_tmem, M_TEMP);
1976 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1979 * Definitions of protocols supported in the ROUTE domain.
1982 static struct domain routedomain; /* or at least forward */
1984 static struct protosw routesw[] = {
1986 .pr_type = SOCK_RAW,
1987 .pr_domain = &routedomain,
1988 .pr_flags = PR_ATOMIC|PR_ADDR,
1989 .pr_output = route_output,
1990 .pr_ctlinput = raw_ctlinput,
1991 .pr_init = raw_init,
1992 .pr_usrreqs = &route_usrreqs
1996 static struct domain routedomain = {
1997 .dom_family = PF_ROUTE,
1998 .dom_name = "route",
1999 .dom_protosw = routesw,
2000 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2003 VNET_DOMAIN_SET(route);
2007 * Unfortunately, RTF_ values are expressed as raw masks rather than powers of
2008 * 2, so we cannot use them as nice C99 initializer indices below.
2010 static const char * const rtf_flag_strings[] = {
2037 [30] = "RNH_LOCKED",
2038 [31] = "GWFLAG_COMPAT",
2041 static const char * __pure
2042 rt_flag_name(unsigned idx)
2044 if (idx >= nitems(rtf_flag_strings))
2045 return ("INVALID_FLAG");
2046 if (rtf_flag_strings[idx] == NULL)
2048 return (rtf_flag_strings[idx]);
2052 rt_dumpaddr_ddb(const char *name, const struct sockaddr *sa)
2054 char buf[INET6_ADDRSTRLEN], *res;
2059 else if (sa->sa_family == AF_INET) {
2060 res = inet_ntop(AF_INET,
2061 &((const struct sockaddr_in *)sa)->sin_addr,
2063 } else if (sa->sa_family == AF_INET6) {
2064 res = inet_ntop(AF_INET6,
2065 &((const struct sockaddr_in6 *)sa)->sin6_addr,
2067 } else if (sa->sa_family == AF_LINK) {
2072 db_printf("%s <%s> ", name, res);
2076 db_printf("%s <af:%d> ", name, sa->sa_family);
2080 rt_dumpentry_ddb(struct radix_node *rn, void *arg __unused)
2082 struct sockaddr_storage ss;
2086 /* If RNTORT is important, put it in a header. */
2089 rt_dumpaddr_ddb("dst", rt_key(rt));
2090 rt_dumpaddr_ddb("gateway", rt->rt_gateway);
2091 rt_dumpaddr_ddb("netmask", rtsock_fix_netmask(rt_key(rt), rt_mask(rt),
2093 if (rt->rt_ifp != NULL && (rt->rt_ifp->if_flags & IFF_DYING) == 0) {
2094 rt_dumpaddr_ddb("ifp", rt->rt_ifp->if_addr->ifa_addr);
2095 rt_dumpaddr_ddb("ifa", rt->rt_ifa->ifa_addr);
2098 db_printf("flags ");
2099 flags = rt->rt_flags;
2103 while ((idx = ffs(flags)) > 0) {
2106 if (flags != rt->rt_flags)
2108 db_printf("%s", rt_flag_name(idx));
2110 flags &= ~(1ul << idx);
2117 DB_SHOW_COMMAND(routetable, db_show_routetable_cmd)
2119 struct rib_head *rnh;
2129 for (; i <= lim; i++) {
2130 rnh = rt_tables_get_rnh(0, i);
2133 db_printf("%s: AF %d not supported?\n",
2140 if (!have_addr && i > 1)
2143 db_printf("Route table for AF %d%s%s%s:\n", i,
2144 (i == AF_INET || i == AF_INET6) ? " (" : "",
2145 (i == AF_INET) ? "INET" : (i == AF_INET6) ? "INET6" : "",
2146 (i == AF_INET || i == AF_INET6) ? ")" : "");
2148 error = rnh->rnh_walktree(&rnh->head, rt_dumpentry_ddb, NULL);
2150 db_printf("%s: walktree(%d): %d\n", __func__, i,
2155 _DB_FUNC(_show, route, db_show_route_cmd, db_show_table, CS_OWN, NULL)
2157 char buf[INET6_ADDRSTRLEN], *bp;
2158 const void *dst_addrp;
2159 struct sockaddr *dstp;
2162 struct sockaddr_in dest_sin;
2163 struct sockaddr_in6 dest_sin6;
2165 uint16_t hextets[8];
2167 int t, af, exp, tokflags;
2170 * Undecoded address family. No double-colon expansion seen yet.
2174 /* Assume INET6 to start; we can work back if guess was wrong. */
2175 tokflags = DRT_WSPACE | DRT_HEX | DRT_HEXADECIMAL;
2178 * db_command has lexed 'show route' for us.
2180 t = db_read_token_flags(tokflags);
2182 t = db_read_token_flags(tokflags);
2185 * tEOL: Just 'show route' isn't a valid mode.
2186 * tMINUS: It's either '-h' or some invalid option. Regardless, usage.
2188 if (t == tEOL || t == tMINUS)
2193 tets = nitems(hextets);
2196 * Each loop iteration, we expect to read one octet (v4) or hextet
2197 * (v6), followed by an appropriate field separator ('.' or ':' or
2200 * At the start of each loop, we're looking for a number (octet or
2203 * INET6 addresses have a special case where they may begin with '::'.
2205 for (i = 0; i < tets; i++) {
2206 t = db_read_token_flags(tokflags);
2208 if (t == tCOLONCOLON) {
2209 /* INET6 with leading '::' or invalid. */
2211 db_printf("Parse error: unexpected extra "
2220 } else if (t == tNUMBER) {
2222 * Lexer separates out '-' as tMINUS, but make the
2223 * assumption explicit here.
2225 MPASS(db_tok_number >= 0);
2227 if (af == AF_INET && db_tok_number > UINT8_MAX) {
2228 db_printf("Not a valid v4 octet: %ld\n",
2229 (long)db_tok_number);
2232 hextets[i] = db_tok_number;
2233 } else if (t == tEOL) {
2235 * We can only detect the end of an IPv6 address in
2236 * compact representation with EOL.
2238 if (af != AF_INET6 || exp < 0) {
2239 db_printf("Parse failed. Got unexpected EOF "
2240 "when the address is not a compact-"
2241 "representation IPv6 address.\n");
2246 db_printf("Parse failed. Unexpected token %d.\n", t);
2250 /* Next, look for a separator, if appropriate. */
2254 t = db_read_token_flags(tokflags);
2260 if (t == tCOLONCOLON) {
2271 /* Need to fixup the first parsed number. */
2272 if (hextets[0] > 0x255 ||
2273 (hextets[0] & 0xf0) > 0x90 ||
2274 (hextets[0] & 0xf) > 9) {
2275 db_printf("Not a valid v4 octet: %x\n",
2281 dn = (hn >> 8) * 100 +
2282 ((hn >> 4) & 0xf) * 10 +
2287 /* Switch to decimal for remaining octets. */
2288 tokflags &= ~DRT_RADIX_MASK;
2289 tokflags |= DRT_DECIMAL;
2295 db_printf("Parse error. Unexpected token %d.\n", t);
2297 } else if (af == AF_INET) {
2300 db_printf("Expected '.' (%d) between octets but got "
2301 "(%d).\n", tDOT, t);
2304 } else if (af == AF_INET6) {
2307 if (t == tCOLONCOLON) {
2314 db_printf("Got bogus second '::' in v6 "
2320 * Handle in the earlier part of the loop
2321 * because we need to handle trailing :: too.
2327 db_printf("Expected ':' (%d) or '::' (%d) between "
2328 "hextets but got (%d).\n", tCOLON, tCOLONCOLON, t);
2333 /* Check for trailing garbage. */
2335 t = db_read_token_flags(tokflags);
2337 db_printf("Got unexpected garbage after address "
2344 * Need to expand compact INET6 addresses.
2346 * Technically '::' for a single ':0:' is MUST NOT but just in case,
2347 * don't bother expanding that form (exp >= 0 && i == tets case).
2349 if (af == AF_INET6 && exp >= 0 && i < tets) {
2351 memmove(&hextets[exp + 1 + (nitems(hextets) - i)],
2353 (i - (exp + 1)) * sizeof(hextets[0]));
2355 memset(&hextets[exp + 1], 0, (nitems(hextets) - i) *
2356 sizeof(hextets[0]));
2359 memset(&u, 0, sizeof(u));
2360 if (af == AF_INET) {
2361 u.dest_sin.sin_family = AF_INET;
2362 u.dest_sin.sin_len = sizeof(u.dest_sin);
2363 u.dest_sin.sin_addr.s_addr = htonl(
2364 ((uint32_t)hextets[0] << 24) |
2365 ((uint32_t)hextets[1] << 16) |
2366 ((uint32_t)hextets[2] << 8) |
2367 (uint32_t)hextets[3]);
2368 dstp = (void *)&u.dest_sin;
2369 dst_addrp = &u.dest_sin.sin_addr;
2370 } else if (af == AF_INET6) {
2371 u.dest_sin6.sin6_family = AF_INET6;
2372 u.dest_sin6.sin6_len = sizeof(u.dest_sin6);
2373 for (i = 0; i < nitems(hextets); i++)
2374 u.dest_sin6.sin6_addr.s6_addr16[i] = htons(hextets[i]);
2375 dstp = (void *)&u.dest_sin6;
2376 dst_addrp = &u.dest_sin6.sin6_addr;
2380 /* Appease Clang false positive: */
2384 bp = inet_ntop(af, dst_addrp, buf, sizeof(buf));
2386 db_printf("Looking up route to destination '%s'\n", bp);
2389 rt = rtalloc1(dstp, 0, RTF_RNH_LOCKED);
2393 db_printf("Could not get route for that server.\n");
2397 rt_dumpentry_ddb((void *)rt, NULL);
2402 db_printf("Usage: 'show route <address>'\n"
2403 " Currently accepts only dotted-decimal INET or colon-separated\n"
2404 " hextet INET6 addresses.\n");