2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
35 #include "opt_mpath.h"
37 #include "opt_inet6.h"
39 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/domain.h>
44 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/rmlock.h>
50 #include <sys/rwlock.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 #include <net/raw_cb.h>
64 #include <net/route.h>
65 #include <net/route/route_ctl.h>
66 #include <net/route/route_var.h>
68 #include <net/radix_mpath.h>
72 #include <netinet/in.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/ip_carp.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/scope6_var.h>
79 #include <net/route/nhop.h>
80 #include <net/route/shared.h>
82 #ifdef COMPAT_FREEBSD32
83 #include <sys/mount.h>
84 #include <compat/freebsd32/freebsd32.h>
94 struct if_data ifm_data;
104 uint16_t _ifm_spare1;
106 uint16_t ifm_data_off;
107 uint32_t _ifm_spare2;
108 struct if_data ifm_data;
111 struct ifa_msghdrl32 {
112 uint16_t ifam_msglen;
113 uint8_t ifam_version;
118 uint16_t _ifam_spare1;
120 uint16_t ifam_data_off;
122 struct if_data ifam_data;
125 #define SA_SIZE32(sa) \
126 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
128 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
130 #endif /* COMPAT_FREEBSD32 */
132 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
134 /* NB: these are not modified */
135 static struct sockaddr route_src = { 2, PF_ROUTE, };
136 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
138 /* These are external hooks for CARP. */
139 int (*carp_get_vhid_p)(struct ifaddr *);
142 * Used by rtsock/raw_input callback code to decide whether to filter the update
143 * notification to a socket bound to a particular FIB.
145 #define RTS_FILTER_FIB M_PROTO8
148 int ip_count; /* attached w/ AF_INET */
149 int ip6_count; /* attached w/ AF_INET6 */
150 int any_count; /* total attached */
152 VNET_DEFINE_STATIC(route_cb_t, route_cb);
153 #define V_route_cb VNET(route_cb)
155 struct mtx rtsock_mtx;
156 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
158 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
159 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
160 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
162 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
169 struct sysctl_req *w_req;
172 static void rts_input(struct mbuf *m);
173 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
174 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
175 struct walkarg *w, int *plen);
176 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
177 struct rt_addrinfo *rtinfo);
178 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
179 static int sysctl_iflist(int af, struct walkarg *w);
180 static int sysctl_ifmalist(int af, struct walkarg *w);
181 static int route_output(struct mbuf *m, struct socket *so, ...);
182 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
183 static void rt_dispatch(struct mbuf *, sa_family_t);
184 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
185 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
186 static int update_rtm_from_rte(struct rt_addrinfo *info,
187 struct rt_msghdr **prtm, int alloc_len,
188 struct rtentry *rt, struct nhop_object *nh);
189 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
190 struct mbuf *m, sa_family_t saf, u_int fibnum,
192 static int can_export_rte(struct ucred *td_ucred, const struct rtentry *rt);
194 static struct netisr_handler rtsock_nh = {
196 .nh_handler = rts_input,
197 .nh_proto = NETISR_ROUTE,
198 .nh_policy = NETISR_POLICY_SOURCE,
202 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
206 netisr_getqlimit(&rtsock_nh, &qlimit);
207 error = sysctl_handle_int(oidp, &qlimit, 0, req);
208 if (error || !req->newptr)
212 return (netisr_setqlimit(&rtsock_nh, qlimit));
214 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
215 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
216 0, 0, sysctl_route_netisr_maxqlen, "I",
217 "maximum routing socket dispatch queue length");
224 if (IS_DEFAULT_VNET(curvnet)) {
225 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
226 rtsock_nh.nh_qlimit = tmp;
227 netisr_register(&rtsock_nh);
231 netisr_register_vnet(&rtsock_nh);
234 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
239 vnet_rts_uninit(void)
242 netisr_unregister_vnet(&rtsock_nh);
244 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
249 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
254 KASSERT(m != NULL, ("%s: m is NULL", __func__));
255 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
256 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
258 /* No filtering requested. */
259 if ((m->m_flags & RTS_FILTER_FIB) == 0)
262 /* Check if it is a rts and the fib matches the one of the socket. */
263 fibnum = M_GETFIB(m);
264 if (proto->sp_family != PF_ROUTE ||
265 rp->rcb_socket == NULL ||
266 rp->rcb_socket->so_fibnum == fibnum)
269 /* Filtering requested and no match, the socket shall be skipped. */
274 rts_input(struct mbuf *m)
276 struct sockproto route_proto;
277 unsigned short *family;
280 route_proto.sp_family = PF_ROUTE;
281 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
283 family = (unsigned short *)(tag + 1);
284 route_proto.sp_protocol = *family;
285 m_tag_delete(m, tag);
287 route_proto.sp_protocol = 0;
289 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
293 * It really doesn't make any sense at all for this code to share much
294 * with raw_usrreq.c, since its functionality is so restricted. XXX
297 rts_abort(struct socket *so)
300 raw_usrreqs.pru_abort(so);
304 rts_close(struct socket *so)
307 raw_usrreqs.pru_close(so);
310 /* pru_accept is EOPNOTSUPP */
313 rts_attach(struct socket *so, int proto, struct thread *td)
318 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
321 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
323 so->so_pcb = (caddr_t)rp;
324 so->so_fibnum = td->td_proc->p_fibnum;
325 error = raw_attach(so, proto);
333 switch(rp->rcb_proto.sp_protocol) {
335 V_route_cb.ip_count++;
338 V_route_cb.ip6_count++;
341 V_route_cb.any_count++;
344 so->so_options |= SO_USELOOPBACK;
349 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
352 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
356 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
359 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
362 /* pru_connect2 is EOPNOTSUPP */
363 /* pru_control is EOPNOTSUPP */
366 rts_detach(struct socket *so)
368 struct rawcb *rp = sotorawcb(so);
370 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
373 switch(rp->rcb_proto.sp_protocol) {
375 V_route_cb.ip_count--;
378 V_route_cb.ip6_count--;
381 V_route_cb.any_count--;
383 raw_usrreqs.pru_detach(so);
387 rts_disconnect(struct socket *so)
390 return (raw_usrreqs.pru_disconnect(so));
393 /* pru_listen is EOPNOTSUPP */
396 rts_peeraddr(struct socket *so, struct sockaddr **nam)
399 return (raw_usrreqs.pru_peeraddr(so, nam));
402 /* pru_rcvd is EOPNOTSUPP */
403 /* pru_rcvoob is EOPNOTSUPP */
406 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
407 struct mbuf *control, struct thread *td)
410 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
413 /* pru_sense is null */
416 rts_shutdown(struct socket *so)
419 return (raw_usrreqs.pru_shutdown(so));
423 rts_sockaddr(struct socket *so, struct sockaddr **nam)
426 return (raw_usrreqs.pru_sockaddr(so, nam));
429 static struct pr_usrreqs route_usrreqs = {
430 .pru_abort = rts_abort,
431 .pru_attach = rts_attach,
432 .pru_bind = rts_bind,
433 .pru_connect = rts_connect,
434 .pru_detach = rts_detach,
435 .pru_disconnect = rts_disconnect,
436 .pru_peeraddr = rts_peeraddr,
437 .pru_send = rts_send,
438 .pru_shutdown = rts_shutdown,
439 .pru_sockaddr = rts_sockaddr,
440 .pru_close = rts_close,
443 #ifndef _SOCKADDR_UNION_DEFINED
444 #define _SOCKADDR_UNION_DEFINED
446 * The union of all possible address formats we handle.
448 union sockaddr_union {
450 struct sockaddr_in sin;
451 struct sockaddr_in6 sin6;
453 #endif /* _SOCKADDR_UNION_DEFINED */
456 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
457 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
459 #if defined(INET) || defined(INET6)
460 struct epoch_tracker et;
463 /* First, see if the returned address is part of the jail. */
464 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
465 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
469 switch (info->rti_info[RTAX_DST]->sa_family) {
479 * Try to find an address on the given outgoing interface
480 * that belongs to the jail.
483 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
486 if (sa->sa_family != AF_INET)
488 ia = ((struct sockaddr_in *)sa)->sin_addr;
489 if (prison_check_ip4(cred, &ia) == 0) {
497 * As a last resort return the 'default' jail address.
499 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
501 if (prison_get_ip4(cred, &ia) != 0)
504 bzero(&saun->sin, sizeof(struct sockaddr_in));
505 saun->sin.sin_len = sizeof(struct sockaddr_in);
506 saun->sin.sin_family = AF_INET;
507 saun->sin.sin_addr.s_addr = ia.s_addr;
508 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
521 * Try to find an address on the given outgoing interface
522 * that belongs to the jail.
525 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
528 if (sa->sa_family != AF_INET6)
530 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
531 &ia6, sizeof(struct in6_addr));
532 if (prison_check_ip6(cred, &ia6) == 0) {
540 * As a last resort return the 'default' jail address.
542 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
544 if (prison_get_ip6(cred, &ia6) != 0)
547 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
548 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
549 saun->sin6.sin6_family = AF_INET6;
550 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
551 if (sa6_recoverscope(&saun->sin6) != 0)
553 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
564 * Fills in @info based on userland-provided @rtm message.
566 * Returns 0 on success.
569 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info)
574 rtm->rtm_pid = curproc->p_pid;
575 info->rti_addrs = rtm->rtm_addrs;
577 info->rti_mflags = rtm->rtm_inits;
578 info->rti_rmx = &rtm->rtm_rmx;
581 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
582 * link-local address because rtrequest requires addresses with
585 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
588 if (rtm->rtm_flags & RTF_RNH_LOCKED)
590 info->rti_flags = rtm->rtm_flags;
591 if (info->rti_info[RTAX_DST] == NULL ||
592 info->rti_info[RTAX_DST]->sa_family >= AF_MAX ||
593 (info->rti_info[RTAX_GATEWAY] != NULL &&
594 info->rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
596 saf = info->rti_info[RTAX_DST]->sa_family;
598 * Verify that the caller has the appropriate privilege; RTM_GET
599 * is the only operation the non-superuser is allowed.
601 if (rtm->rtm_type != RTM_GET) {
602 error = priv_check(curthread, PRIV_NET_ROUTE);
608 * The given gateway address may be an interface address.
609 * For example, issuing a "route change" command on a route
610 * entry that was created from a tunnel, and the gateway
611 * address given is the local end point. In this case the
612 * RTF_GATEWAY flag must be cleared or the destination will
613 * not be reachable even though there is no error message.
615 if (info->rti_info[RTAX_GATEWAY] != NULL &&
616 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
617 struct rt_addrinfo ginfo;
618 struct sockaddr *gdst;
619 struct sockaddr_storage ss;
621 bzero(&ginfo, sizeof(ginfo));
622 bzero(&ss, sizeof(ss));
623 ss.ss_len = sizeof(ss);
625 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
626 gdst = info->rti_info[RTAX_GATEWAY];
629 * A host route through the loopback interface is
630 * installed for each interface adddress. In pre 8.0
631 * releases the interface address of a PPP link type
632 * is not reachable locally. This behavior is fixed as
633 * part of the new L2/L3 redesign and rewrite work. The
634 * signature of this interface address route is the
635 * AF_LINK sa_family type of the gateway, and the
636 * rt_ifp has the IFF_LOOPBACK flag set.
638 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
639 if (ss.ss_family == AF_LINK &&
640 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
641 info->rti_flags &= ~RTF_GATEWAY;
642 info->rti_flags |= RTF_GWFLAG_COMPAT;
644 rib_free_info(&ginfo);
652 * Handles RTM_GET message from routing socket, returning matching rt.
655 * 0 on success, with locked and referenced matching rt in @rt_nrt
659 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
660 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
663 struct rib_head *rnh;
666 saf = info->rti_info[RTAX_DST]->sa_family;
668 rnh = rt_tables_get_rnh(fibnum, saf);
670 return (EAFNOSUPPORT);
674 if (info->rti_info[RTAX_NETMASK] == NULL) {
676 * Provide longest prefix match for
677 * address lookup (no mask).
678 * 'route -n get addr'
680 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
681 info->rti_info[RTAX_DST], &rnh->head);
683 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
684 info->rti_info[RTAX_DST],
685 info->rti_info[RTAX_NETMASK], &rnh->head);
687 if (rc->rc_rt == NULL) {
693 * for RTM_GET, gate is optional even with multipath.
694 * if gate == NULL the first match is returned.
695 * (no need to call rt_mpath_matchgate if gate == NULL)
697 if (rt_mpath_capable(rnh) && info->rti_info[RTAX_GATEWAY]) {
698 rc->rc_rt = rt_mpath_matchgate(rc->rc_rt,
699 info->rti_info[RTAX_GATEWAY]);
700 if (rc->rc_rt == NULL) {
707 * If performing proxied L2 entry insertion, and
708 * the actual PPP host entry is found, perform
709 * another search to retrieve the prefix route of
710 * the local end point of the PPP link.
711 * TODO: move this logic to userland.
713 if (rtm->rtm_flags & RTF_ANNOUNCE) {
714 struct sockaddr laddr;
715 struct nhop_object *nh;
717 nh = rc->rc_rt->rt_nhop;
718 if (nh->nh_ifp != NULL &&
719 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
722 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
725 rt_maskedcopy(ifa->ifa_addr,
729 rt_maskedcopy(nh->nh_ifa->ifa_addr,
731 nh->nh_ifa->ifa_netmask);
733 * refactor rt and no lock operation necessary
735 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
737 if (rc->rc_rt == NULL) {
742 rc->rc_nh_new = rc->rc_rt->rt_nhop;
749 * Update sockaddrs, flags, etc in @prtm based on @rt data.
750 * rtm can be reallocated.
752 * Returns 0 on success, along with pointer to (potentially reallocated)
757 update_rtm_from_rte(struct rt_addrinfo *info, struct rt_msghdr **prtm,
758 int alloc_len, struct rtentry *rt, struct nhop_object *nh)
760 struct sockaddr_storage netmask_ss;
762 union sockaddr_union saun;
763 struct rt_msghdr *rtm, *orig_rtm = NULL;
769 info->rti_info[RTAX_DST] = rt_key(rt);
770 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
771 info->rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
772 rt_mask(rt), &netmask_ss);
773 info->rti_info[RTAX_GENMASK] = 0;
775 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
777 info->rti_info[RTAX_IFP] =
778 ifp->if_addr->ifa_addr;
779 error = rtm_get_jailed(info, ifp, nh,
780 &saun, curthread->td_ucred);
783 if (ifp->if_flags & IFF_POINTOPOINT)
784 info->rti_info[RTAX_BRD] =
785 nh->nh_ifa->ifa_dstaddr;
786 rtm->rtm_index = ifp->if_index;
788 info->rti_info[RTAX_IFP] = NULL;
789 info->rti_info[RTAX_IFA] = NULL;
791 } else if (ifp != NULL)
792 rtm->rtm_index = ifp->if_index;
794 /* Check if we need to realloc storage */
795 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
796 if (len > alloc_len) {
797 struct rt_msghdr *tmp_rtm;
799 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
802 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
808 * Delay freeing original rtm as info contains
809 * data referencing it.
813 w.w_tmem = (caddr_t)rtm;
814 w.w_tmemsize = alloc_len;
815 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
817 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
818 rtm->rtm_flags = RTF_GATEWAY |
819 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
821 rtm->rtm_flags = rt->rt_flags;
822 rt_getmetrics(rt, &rtm->rtm_rmx);
823 rtm->rtm_addrs = info->rti_addrs;
825 if (orig_rtm != NULL)
826 free(orig_rtm, M_TEMP);
834 route_output(struct mbuf *m, struct socket *so, ...)
836 struct rt_msghdr *rtm = NULL;
837 struct rtentry *rt = NULL;
838 struct rt_addrinfo info;
839 struct epoch_tracker et;
841 struct sockaddr_storage ss;
842 struct sockaddr_in6 *sin6;
843 int i, rti_need_deembed = 0;
845 int alloc_len = 0, len, error = 0, fibnum;
846 sa_family_t saf = AF_UNSPEC;
848 struct rib_cmd_info rc;
849 struct nhop_object *nh;
851 fibnum = so->so_fibnum;
853 #define senderr(e) { error = e; goto flush;}
854 if (m == NULL || ((m->m_len < sizeof(long)) &&
855 (m = m_pullup(m, sizeof(long))) == NULL))
857 if ((m->m_flags & M_PKTHDR) == 0)
858 panic("route_output");
860 len = m->m_pkthdr.len;
861 if (len < sizeof(*rtm) ||
862 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
866 * Most of current messages are in range 200-240 bytes,
867 * minimize possible re-allocation on reply using larger size
868 * buffer aligned on 1k boundaty.
870 alloc_len = roundup2(len, 1024);
871 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
874 m_copydata(m, 0, len, (caddr_t)rtm);
875 bzero(&info, sizeof(info));
876 bzero(&w, sizeof(w));
879 if (rtm->rtm_version != RTM_VERSION) {
880 /* Do not touch message since format is unknown */
883 senderr(EPROTONOSUPPORT);
887 * Starting from here, it is possible
888 * to alter original message and insert
889 * caller PID and error value.
892 if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) {
896 saf = info.rti_info[RTAX_DST]->sa_family;
898 /* support for new ARP code */
899 if (rtm->rtm_flags & RTF_LLDATA) {
900 error = lla_rt_output(rtm, &info);
903 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
908 switch (rtm->rtm_type) {
911 if (rtm->rtm_type == RTM_ADD) {
912 if (info.rti_info[RTAX_GATEWAY] == NULL)
915 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
918 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
920 rtm->rtm_index = rc.rc_nh_new->nh_ifp->if_index;
926 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
932 /* rt_msg2() will not be used when RTM_DELETE fails. */
933 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
938 error = handle_rtm_get(&info, fibnum, rtm, &rc);
944 if (!can_export_rte(curthread->td_ucred, rc.rc_rt)) {
948 error = update_rtm_from_rte(&info, &rtm, alloc_len, rc.rc_rt, nh);
950 * Note that some sockaddr pointers may have changed to
951 * point to memory outsize @rtm. Some may be pointing
952 * to the on-stack variables.
953 * Given that, any pointer in @info CANNOT BE USED.
957 * scopeid deembedding has been performed while
958 * writing updated rtm in rtsock_msg_buffer().
959 * With that in mind, skip deembedding procedure below.
962 rti_need_deembed = 0;
978 if (rti_need_deembed) {
979 /* sin6_scope_id is recovered before sending rtm. */
980 sin6 = (struct sockaddr_in6 *)&ss;
981 for (i = 0; i < RTAX_MAX; i++) {
982 if (info.rti_info[i] == NULL)
984 if (info.rti_info[i]->sa_family != AF_INET6)
986 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
987 if (sa6_recoverscope(sin6) == 0)
988 bcopy(sin6, info.rti_info[i],
994 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1000 * Sends the prepared reply message in @rtm to all rtsock clients.
1001 * Frees @m and @rtm.
1005 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1006 sa_family_t saf, u_int fibnum, int rtm_errno)
1008 struct rawcb *rp = NULL;
1011 * Check to see if we don't want our own messages.
1013 if ((so->so_options & SO_USELOOPBACK) == 0) {
1014 if (V_route_cb.any_count <= 1) {
1020 /* There is another listener, so construct message */
1026 rtm->rtm_errno = rtm_errno;
1028 rtm->rtm_flags |= RTF_DONE;
1030 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1031 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1034 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1035 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1040 M_SETFIB(m, fibnum);
1041 m->m_flags |= RTS_FILTER_FIB;
1044 * XXX insure we don't get a copy by
1045 * invalidating our protocol
1047 unsigned short family = rp->rcb_proto.sp_family;
1048 rp->rcb_proto.sp_family = 0;
1049 rt_dispatch(m, saf);
1050 rp->rcb_proto.sp_family = family;
1052 rt_dispatch(m, saf);
1058 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
1061 bzero(out, sizeof(*out));
1062 out->rmx_mtu = rt->rt_nhop->nh_mtu;
1063 out->rmx_weight = rt->rt_weight;
1064 out->rmx_nhidx = nhop_get_idx(rt->rt_nhop);
1065 /* Kernel -> userland timebase conversion. */
1066 out->rmx_expire = rt->rt_expire ?
1067 rt->rt_expire - time_uptime + time_second : 0;
1071 * Extract the addresses of the passed sockaddrs.
1072 * Do a little sanity checking so as to avoid bad memory references.
1073 * This data is derived straight from userland.
1076 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1078 struct sockaddr *sa;
1081 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1082 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1084 sa = (struct sockaddr *)cp;
1088 if (cp + sa->sa_len > cplim)
1091 * there are no more.. quit now
1092 * If there are more bits, they are in error.
1093 * I've seen this. route(1) can evidently generate these.
1094 * This causes kernel to core dump.
1095 * for compatibility, If we see this, point to a safe address.
1097 if (sa->sa_len == 0) {
1098 rtinfo->rti_info[i] = &sa_zero;
1099 return (0); /* should be EINVAL but for compat */
1103 if (sa->sa_family == AF_INET6)
1104 sa6_embedscope((struct sockaddr_in6 *)sa,
1107 rtinfo->rti_info[i] = sa;
1114 * Fill in @dmask with valid netmask leaving original @smask
1115 * intact. Mostly used with radix netmasks.
1118 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1119 struct sockaddr_storage *dmask)
1121 if (dst == NULL || smask == NULL)
1124 memset(dmask, 0, dst->sa_len);
1125 memcpy(dmask, smask, smask->sa_len);
1126 dmask->ss_len = dst->sa_len;
1127 dmask->ss_family = dst->sa_family;
1129 return ((struct sockaddr *)dmask);
1133 * Writes information related to @rtinfo object to newly-allocated mbuf.
1134 * Assumes MCLBYTES is enough to construct any message.
1135 * Used for OS notifications of vaious events (if/ifa announces,etc)
1137 * Returns allocated mbuf or NULL on failure.
1139 static struct mbuf *
1140 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1142 struct rt_msghdr *rtm;
1145 struct sockaddr *sa;
1147 struct sockaddr_storage ss;
1148 struct sockaddr_in6 *sin6;
1156 len = sizeof(struct ifa_msghdr);
1161 len = sizeof(struct ifma_msghdr);
1165 len = sizeof(struct if_msghdr);
1168 case RTM_IFANNOUNCE:
1170 len = sizeof(struct if_announcemsghdr);
1174 len = sizeof(struct rt_msghdr);
1177 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1178 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1180 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1182 m = m_gethdr(M_NOWAIT, MT_DATA);
1186 m->m_pkthdr.len = m->m_len = len;
1187 rtm = mtod(m, struct rt_msghdr *);
1188 bzero((caddr_t)rtm, len);
1189 for (i = 0; i < RTAX_MAX; i++) {
1190 if ((sa = rtinfo->rti_info[i]) == NULL)
1192 rtinfo->rti_addrs |= (1 << i);
1195 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1196 sin6 = (struct sockaddr_in6 *)&ss;
1197 bcopy(sa, sin6, sizeof(*sin6));
1198 if (sa6_recoverscope(sin6) == 0)
1199 sa = (struct sockaddr *)sin6;
1202 m_copyback(m, len, dlen, (caddr_t)sa);
1205 if (m->m_pkthdr.len != len) {
1209 rtm->rtm_msglen = len;
1210 rtm->rtm_version = RTM_VERSION;
1211 rtm->rtm_type = type;
1216 * Writes information related to @rtinfo object to preallocated buffer.
1217 * Stores needed size in @plen. If @w is NULL, calculates size without
1219 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1221 * Returns 0 on success.
1225 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1228 int len, buflen = 0, dlen;
1230 struct rt_msghdr *rtm = NULL;
1232 struct sockaddr_storage ss;
1233 struct sockaddr_in6 *sin6;
1235 #ifdef COMPAT_FREEBSD32
1236 bool compat32 = false;
1243 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1244 #ifdef COMPAT_FREEBSD32
1245 if (w->w_req->flags & SCTL_MASK32) {
1246 len = sizeof(struct ifa_msghdrl32);
1250 len = sizeof(struct ifa_msghdrl);
1252 len = sizeof(struct ifa_msghdr);
1256 #ifdef COMPAT_FREEBSD32
1257 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1258 if (w->w_op == NET_RT_IFLISTL)
1259 len = sizeof(struct if_msghdrl32);
1261 len = sizeof(struct if_msghdr32);
1266 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1267 len = sizeof(struct if_msghdrl);
1269 len = sizeof(struct if_msghdr);
1273 len = sizeof(struct ifma_msghdr);
1277 len = sizeof(struct rt_msghdr);
1281 rtm = (struct rt_msghdr *)w->w_tmem;
1282 buflen = w->w_tmemsize - len;
1283 cp = (caddr_t)w->w_tmem + len;
1286 rtinfo->rti_addrs = 0;
1287 for (i = 0; i < RTAX_MAX; i++) {
1288 struct sockaddr *sa;
1290 if ((sa = rtinfo->rti_info[i]) == NULL)
1292 rtinfo->rti_addrs |= (1 << i);
1293 #ifdef COMPAT_FREEBSD32
1295 dlen = SA_SIZE32(sa);
1299 if (cp != NULL && buflen >= dlen) {
1301 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1302 sin6 = (struct sockaddr_in6 *)&ss;
1303 bcopy(sa, sin6, sizeof(*sin6));
1304 if (sa6_recoverscope(sin6) == 0)
1305 sa = (struct sockaddr *)sin6;
1308 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1311 } else if (cp != NULL) {
1313 * Buffer too small. Count needed size
1314 * and return with error.
1323 dlen = ALIGN(len) - len;
1335 /* fill header iff buffer is large enough */
1336 rtm->rtm_version = RTM_VERSION;
1337 rtm->rtm_type = type;
1338 rtm->rtm_msglen = len;
1343 if (w != NULL && cp == NULL)
1350 * This routine is called to generate a message from the routing
1351 * socket indicating that a redirect has occurred, a routing lookup
1352 * has failed, or that a protocol has detected timeouts to a particular
1356 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1359 struct rt_msghdr *rtm;
1361 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1363 if (V_route_cb.any_count == 0)
1365 m = rtsock_msg_mbuf(type, rtinfo);
1369 if (fibnum != RT_ALL_FIBS) {
1370 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1371 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1372 M_SETFIB(m, fibnum);
1373 m->m_flags |= RTS_FILTER_FIB;
1376 rtm = mtod(m, struct rt_msghdr *);
1377 rtm->rtm_flags = RTF_DONE | flags;
1378 rtm->rtm_errno = error;
1379 rtm->rtm_addrs = rtinfo->rti_addrs;
1380 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1384 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1387 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1391 * This routine is called to generate a message from the routing
1392 * socket indicating that the status of a network interface has changed.
1395 rt_ifmsg(struct ifnet *ifp)
1397 struct if_msghdr *ifm;
1399 struct rt_addrinfo info;
1401 if (V_route_cb.any_count == 0)
1403 bzero((caddr_t)&info, sizeof(info));
1404 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1407 ifm = mtod(m, struct if_msghdr *);
1408 ifm->ifm_index = ifp->if_index;
1409 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1410 if_data_copy(ifp, &ifm->ifm_data);
1412 rt_dispatch(m, AF_UNSPEC);
1416 * Announce interface address arrival/withdraw.
1417 * Please do not call directly, use rt_addrmsg().
1418 * Assume input data to be valid.
1419 * Returns 0 on success.
1422 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1424 struct rt_addrinfo info;
1425 struct sockaddr *sa;
1428 struct ifa_msghdr *ifam;
1429 struct ifnet *ifp = ifa->ifa_ifp;
1430 struct sockaddr_storage ss;
1432 if (V_route_cb.any_count == 0)
1435 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1437 bzero((caddr_t)&info, sizeof(info));
1438 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1439 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1440 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1441 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1442 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1443 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1445 ifam = mtod(m, struct ifa_msghdr *);
1446 ifam->ifam_index = ifp->if_index;
1447 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1448 ifam->ifam_flags = ifa->ifa_flags;
1449 ifam->ifam_addrs = info.rti_addrs;
1451 if (fibnum != RT_ALL_FIBS) {
1452 M_SETFIB(m, fibnum);
1453 m->m_flags |= RTS_FILTER_FIB;
1456 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1462 * Announce route addition/removal to rtsock based on @rt data.
1463 * Callers are advives to use rt_routemsg() instead of using this
1464 * function directly.
1465 * Assume @rt data is consistent.
1467 * Returns 0 on success.
1470 rtsock_routemsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int rti_addrs,
1473 struct sockaddr_storage ss;
1474 struct rt_addrinfo info;
1476 if (V_route_cb.any_count == 0)
1479 bzero((caddr_t)&info, sizeof(info));
1480 info.rti_info[RTAX_DST] = rt_key(rt);
1481 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt), rt_mask(rt), &ss);
1482 info.rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
1483 info.rti_flags = rt->rt_flags;
1486 return (rtsock_routemsg_info(cmd, &info, fibnum));
1490 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
1492 struct rt_msghdr *rtm;
1493 struct sockaddr *sa;
1496 if (V_route_cb.any_count == 0)
1499 if (info->rti_flags & RTF_HOST)
1500 info->rti_info[RTAX_NETMASK] = NULL;
1502 m = rtsock_msg_mbuf(cmd, info);
1506 if (fibnum != RT_ALL_FIBS) {
1507 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1508 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1509 M_SETFIB(m, fibnum);
1510 m->m_flags |= RTS_FILTER_FIB;
1513 rtm = mtod(m, struct rt_msghdr *);
1514 rtm->rtm_addrs = info->rti_addrs;
1515 if (info->rti_ifp != NULL)
1516 rtm->rtm_index = info->rti_ifp->if_index;
1517 /* Add RTF_DONE to indicate command 'completion' required by API */
1518 info->rti_flags |= RTF_DONE;
1519 /* Reported routes has to be up */
1520 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
1521 info->rti_flags |= RTF_UP;
1522 rtm->rtm_flags = info->rti_flags;
1524 sa = info->rti_info[RTAX_DST];
1525 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1531 * This is the analogue to the rt_newaddrmsg which performs the same
1532 * function but for multicast group memberhips. This is easier since
1533 * there is no route state to worry about.
1536 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1538 struct rt_addrinfo info;
1539 struct mbuf *m = NULL;
1540 struct ifnet *ifp = ifma->ifma_ifp;
1541 struct ifma_msghdr *ifmam;
1543 if (V_route_cb.any_count == 0)
1546 bzero((caddr_t)&info, sizeof(info));
1547 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1548 if (ifp && ifp->if_addr)
1549 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1551 info.rti_info[RTAX_IFP] = NULL;
1553 * If a link-layer address is present, present it as a ``gateway''
1554 * (similarly to how ARP entries, e.g., are presented).
1556 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1557 m = rtsock_msg_mbuf(cmd, &info);
1560 ifmam = mtod(m, struct ifma_msghdr *);
1561 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1563 ifmam->ifmam_index = ifp->if_index;
1564 ifmam->ifmam_addrs = info.rti_addrs;
1565 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1568 static struct mbuf *
1569 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1570 struct rt_addrinfo *info)
1572 struct if_announcemsghdr *ifan;
1575 if (V_route_cb.any_count == 0)
1577 bzero((caddr_t)info, sizeof(*info));
1578 m = rtsock_msg_mbuf(type, info);
1580 ifan = mtod(m, struct if_announcemsghdr *);
1581 ifan->ifan_index = ifp->if_index;
1582 strlcpy(ifan->ifan_name, ifp->if_xname,
1583 sizeof(ifan->ifan_name));
1584 ifan->ifan_what = what;
1590 * This is called to generate routing socket messages indicating
1591 * IEEE80211 wireless events.
1592 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1595 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1598 struct rt_addrinfo info;
1600 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1603 * Append the ieee80211 data. Try to stick it in the
1604 * mbuf containing the ifannounce msg; otherwise allocate
1605 * a new mbuf and append.
1607 * NB: we assume m is a single mbuf.
1609 if (data_len > M_TRAILINGSPACE(m)) {
1610 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1615 bcopy(data, mtod(n, void *), data_len);
1616 n->m_len = data_len;
1618 } else if (data_len > 0) {
1619 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1620 m->m_len += data_len;
1622 if (m->m_flags & M_PKTHDR)
1623 m->m_pkthdr.len += data_len;
1624 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1625 rt_dispatch(m, AF_UNSPEC);
1630 * This is called to generate routing socket messages indicating
1631 * network interface arrival and departure.
1634 rt_ifannouncemsg(struct ifnet *ifp, int what)
1637 struct rt_addrinfo info;
1639 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1641 rt_dispatch(m, AF_UNSPEC);
1645 rt_dispatch(struct mbuf *m, sa_family_t saf)
1650 * Preserve the family from the sockaddr, if any, in an m_tag for
1651 * use when injecting the mbuf into the routing socket buffer from
1654 if (saf != AF_UNSPEC) {
1655 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1661 *(unsigned short *)(tag + 1) = saf;
1662 m_tag_prepend(m, tag);
1666 m->m_pkthdr.rcvif = V_loif;
1672 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1676 * Checks if rte can be exported v.r.t jails/vnets.
1678 * Returns 1 if it can, 0 otherwise.
1681 can_export_rte(struct ucred *td_ucred, const struct rtentry *rt)
1684 if ((rt->rt_flags & RTF_HOST) == 0
1685 ? jailed_without_vnet(td_ucred)
1686 : prison_if(td_ucred, rt_key_const(rt)) != 0)
1692 * This is used in dumping the kernel table via sysctl().
1695 sysctl_dumpentry(struct radix_node *rn, void *vw)
1697 struct walkarg *w = vw;
1698 struct rtentry *rt = (struct rtentry *)rn;
1699 struct nhop_object *nh;
1700 int error = 0, size;
1701 struct rt_addrinfo info;
1702 struct sockaddr_storage ss;
1706 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1708 if (!can_export_rte(w->w_req->td->td_ucred, rt))
1710 bzero((caddr_t)&info, sizeof(info));
1711 info.rti_info[RTAX_DST] = rt_key(rt);
1712 info.rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
1713 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1715 info.rti_info[RTAX_GENMASK] = 0;
1717 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
1718 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
1719 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
1720 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
1721 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
1723 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1725 if (w->w_req && w->w_tmem) {
1726 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1728 bzero(&rtm->rtm_index,
1729 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1730 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1731 rtm->rtm_flags = RTF_GATEWAY |
1732 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1734 rtm->rtm_flags = rt->rt_flags;
1735 rtm->rtm_flags |= nhop_get_rtflags(nh);
1736 rt_getmetrics(rt, &rtm->rtm_rmx);
1737 rtm->rtm_index = nh->nh_ifp->if_index;
1738 rtm->rtm_addrs = info.rti_addrs;
1739 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1746 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1747 struct rt_addrinfo *info, struct walkarg *w, int len)
1749 struct if_msghdrl *ifm;
1750 struct if_data *ifd;
1752 ifm = (struct if_msghdrl *)w->w_tmem;
1754 #ifdef COMPAT_FREEBSD32
1755 if (w->w_req->flags & SCTL_MASK32) {
1756 struct if_msghdrl32 *ifm32;
1758 ifm32 = (struct if_msghdrl32 *)ifm;
1759 ifm32->ifm_addrs = info->rti_addrs;
1760 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1761 ifm32->ifm_index = ifp->if_index;
1762 ifm32->_ifm_spare1 = 0;
1763 ifm32->ifm_len = sizeof(*ifm32);
1764 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1765 ifm32->_ifm_spare2 = 0;
1766 ifd = &ifm32->ifm_data;
1770 ifm->ifm_addrs = info->rti_addrs;
1771 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1772 ifm->ifm_index = ifp->if_index;
1773 ifm->_ifm_spare1 = 0;
1774 ifm->ifm_len = sizeof(*ifm);
1775 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1776 ifm->_ifm_spare2 = 0;
1777 ifd = &ifm->ifm_data;
1780 memcpy(ifd, src_ifd, sizeof(*ifd));
1782 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1786 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1787 struct rt_addrinfo *info, struct walkarg *w, int len)
1789 struct if_msghdr *ifm;
1790 struct if_data *ifd;
1792 ifm = (struct if_msghdr *)w->w_tmem;
1794 #ifdef COMPAT_FREEBSD32
1795 if (w->w_req->flags & SCTL_MASK32) {
1796 struct if_msghdr32 *ifm32;
1798 ifm32 = (struct if_msghdr32 *)ifm;
1799 ifm32->ifm_addrs = info->rti_addrs;
1800 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1801 ifm32->ifm_index = ifp->if_index;
1802 ifm32->_ifm_spare1 = 0;
1803 ifd = &ifm32->ifm_data;
1807 ifm->ifm_addrs = info->rti_addrs;
1808 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1809 ifm->ifm_index = ifp->if_index;
1810 ifm->_ifm_spare1 = 0;
1811 ifd = &ifm->ifm_data;
1814 memcpy(ifd, src_ifd, sizeof(*ifd));
1816 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1820 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1821 struct walkarg *w, int len)
1823 struct ifa_msghdrl *ifam;
1824 struct if_data *ifd;
1826 ifam = (struct ifa_msghdrl *)w->w_tmem;
1828 #ifdef COMPAT_FREEBSD32
1829 if (w->w_req->flags & SCTL_MASK32) {
1830 struct ifa_msghdrl32 *ifam32;
1832 ifam32 = (struct ifa_msghdrl32 *)ifam;
1833 ifam32->ifam_addrs = info->rti_addrs;
1834 ifam32->ifam_flags = ifa->ifa_flags;
1835 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1836 ifam32->_ifam_spare1 = 0;
1837 ifam32->ifam_len = sizeof(*ifam32);
1838 ifam32->ifam_data_off =
1839 offsetof(struct ifa_msghdrl32, ifam_data);
1840 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1841 ifd = &ifam32->ifam_data;
1845 ifam->ifam_addrs = info->rti_addrs;
1846 ifam->ifam_flags = ifa->ifa_flags;
1847 ifam->ifam_index = ifa->ifa_ifp->if_index;
1848 ifam->_ifam_spare1 = 0;
1849 ifam->ifam_len = sizeof(*ifam);
1850 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1851 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1852 ifd = &ifam->ifam_data;
1855 bzero(ifd, sizeof(*ifd));
1856 ifd->ifi_datalen = sizeof(struct if_data);
1857 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1858 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1859 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1860 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1862 /* Fixup if_data carp(4) vhid. */
1863 if (carp_get_vhid_p != NULL)
1864 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1866 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1870 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1871 struct walkarg *w, int len)
1873 struct ifa_msghdr *ifam;
1875 ifam = (struct ifa_msghdr *)w->w_tmem;
1876 ifam->ifam_addrs = info->rti_addrs;
1877 ifam->ifam_flags = ifa->ifa_flags;
1878 ifam->ifam_index = ifa->ifa_ifp->if_index;
1879 ifam->_ifam_spare1 = 0;
1880 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1882 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1886 sysctl_iflist(int af, struct walkarg *w)
1891 struct rt_addrinfo info;
1893 struct sockaddr_storage ss;
1895 bzero((caddr_t)&info, sizeof(info));
1896 bzero(&ifd, sizeof(ifd));
1897 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1898 if (w->w_arg && w->w_arg != ifp->if_index)
1900 if_data_copy(ifp, &ifd);
1902 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1903 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1906 info.rti_info[RTAX_IFP] = NULL;
1907 if (w->w_req && w->w_tmem) {
1908 if (w->w_op == NET_RT_IFLISTL)
1909 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1912 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1917 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1918 if (af && af != ifa->ifa_addr->sa_family)
1920 if (prison_if(w->w_req->td->td_ucred,
1921 ifa->ifa_addr) != 0)
1923 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1924 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1925 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1926 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1927 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1930 if (w->w_req && w->w_tmem) {
1931 if (w->w_op == NET_RT_IFLISTL)
1932 error = sysctl_iflist_ifaml(ifa, &info,
1935 error = sysctl_iflist_ifam(ifa, &info,
1941 info.rti_info[RTAX_IFA] = NULL;
1942 info.rti_info[RTAX_NETMASK] = NULL;
1943 info.rti_info[RTAX_BRD] = NULL;
1950 sysctl_ifmalist(int af, struct walkarg *w)
1952 struct rt_addrinfo info;
1954 struct ifmultiaddr *ifma;
1961 bzero((caddr_t)&info, sizeof(info));
1963 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1964 if (w->w_arg && w->w_arg != ifp->if_index)
1967 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1968 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1969 if (af && af != ifma->ifma_addr->sa_family)
1971 if (prison_if(w->w_req->td->td_ucred,
1972 ifma->ifma_addr) != 0)
1974 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1975 info.rti_info[RTAX_GATEWAY] =
1976 (ifma->ifma_addr->sa_family != AF_LINK) ?
1977 ifma->ifma_lladdr : NULL;
1978 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1981 if (w->w_req && w->w_tmem) {
1982 struct ifma_msghdr *ifmam;
1984 ifmam = (struct ifma_msghdr *)w->w_tmem;
1985 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1986 ifmam->ifmam_flags = 0;
1987 ifmam->ifmam_addrs = info.rti_addrs;
1988 ifmam->_ifmam_spare1 = 0;
1989 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2001 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2004 struct epoch_tracker et;
2005 int *name = (int *)arg1;
2006 u_int namelen = arg2;
2007 struct rib_head *rnh = NULL; /* silence compiler. */
2008 int i, lim, error = EINVAL;
2017 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP) {
2019 fib = req->td->td_proc->p_fibnum;
2020 else if (namelen == 4)
2021 fib = (name[3] == RT_ALL_FIBS) ?
2022 req->td->td_proc->p_fibnum : name[3];
2024 return ((namelen < 3) ? EISDIR : ENOTDIR);
2025 if (fib < 0 || fib >= rt_numfibs)
2027 } else if (namelen != 3)
2028 return ((namelen < 3) ? EISDIR : ENOTDIR);
2032 bzero(&w, sizeof(w));
2037 error = sysctl_wire_old_buffer(req, 0);
2042 * Allocate reply buffer in advance.
2043 * All rtsock messages has maximum length of u_short.
2045 w.w_tmemsize = 65536;
2046 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2048 NET_EPOCH_ENTER(et);
2052 if (af == 0) { /* dump all tables */
2055 } else /* dump only one table */
2059 * take care of llinfo entries, the caller must
2062 if (w.w_op == NET_RT_FLAGS &&
2063 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2065 error = lltable_sysctl_dumparp(af, w.w_req);
2071 * take care of routing entries
2073 for (error = 0; error == 0 && i <= lim; i++) {
2074 rnh = rt_tables_get_rnh(fib, i);
2077 error = rnh->rnh_walktree(&rnh->head,
2078 sysctl_dumpentry, &w);
2081 error = EAFNOSUPPORT;
2085 /* Allow dumping one specific af/fib at a time */
2091 if (fib < 0 || fib > rt_numfibs) {
2095 rnh = rt_tables_get_rnh(fib, af);
2097 error = EAFNOSUPPORT;
2100 if (w.w_op == NET_RT_NHOP)
2101 error = nhops_dump_sysctl(rnh, w.w_req);
2104 case NET_RT_IFLISTL:
2105 error = sysctl_iflist(af, &w);
2108 case NET_RT_IFMALIST:
2109 error = sysctl_ifmalist(af, &w);
2114 free(w.w_tmem, M_TEMP);
2118 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2119 sysctl_rtsock, "Return route tables and interface/address lists");
2122 * Definitions of protocols supported in the ROUTE domain.
2125 static struct domain routedomain; /* or at least forward */
2127 static struct protosw routesw[] = {
2129 .pr_type = SOCK_RAW,
2130 .pr_domain = &routedomain,
2131 .pr_flags = PR_ATOMIC|PR_ADDR,
2132 .pr_output = route_output,
2133 .pr_ctlinput = raw_ctlinput,
2134 .pr_init = raw_init,
2135 .pr_usrreqs = &route_usrreqs
2139 static struct domain routedomain = {
2140 .dom_family = PF_ROUTE,
2141 .dom_name = "route",
2142 .dom_protosw = routesw,
2143 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2146 VNET_DOMAIN_SET(route);