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_route.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>
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip_carp.h>
73 #include <netinet6/in6_var.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/scope6_var.h>
77 #include <net/route/nhop.h>
79 #ifdef COMPAT_FREEBSD32
80 #include <sys/mount.h>
81 #include <compat/freebsd32/freebsd32.h>
91 struct if_data ifm_data;
101 uint16_t _ifm_spare1;
103 uint16_t ifm_data_off;
104 uint32_t _ifm_spare2;
105 struct if_data ifm_data;
108 struct ifa_msghdrl32 {
109 uint16_t ifam_msglen;
110 uint8_t ifam_version;
115 uint16_t _ifam_spare1;
117 uint16_t ifam_data_off;
119 struct if_data ifam_data;
122 #define SA_SIZE32(sa) \
123 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
125 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
127 #endif /* COMPAT_FREEBSD32 */
129 struct linear_buffer {
130 char *base; /* Base allocated memory pointer */
131 uint32_t offset; /* Currently used offset */
132 uint32_t size; /* Total buffer size */
134 #define SCRATCH_BUFFER_SIZE 1024
136 #define RTS_PID_PRINTF(_fmt, ...) \
137 printf("rtsock:%s(): PID %d: " _fmt "\n", __func__, curproc->p_pid, ## __VA_ARGS__)
139 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
141 /* NB: these are not modified */
142 static struct sockaddr route_src = { 2, PF_ROUTE, };
143 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
145 /* These are external hooks for CARP. */
146 int (*carp_get_vhid_p)(struct ifaddr *);
149 * Used by rtsock/raw_input callback code to decide whether to filter the update
150 * notification to a socket bound to a particular FIB.
152 #define RTS_FILTER_FIB M_PROTO8
155 int ip_count; /* attached w/ AF_INET */
156 int ip6_count; /* attached w/ AF_INET6 */
157 int any_count; /* total attached */
159 VNET_DEFINE_STATIC(route_cb_t, route_cb);
160 #define V_route_cb VNET(route_cb)
162 struct mtx rtsock_mtx;
163 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
165 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
166 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
167 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
169 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
176 struct sysctl_req *w_req;
177 struct sockaddr *dst;
178 struct sockaddr *mask;
181 static void rts_input(struct mbuf *m);
182 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
183 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
184 struct walkarg *w, int *plen);
185 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
186 struct rt_addrinfo *rtinfo);
187 static int cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb);
188 static int sysctl_dumpentry(struct rtentry *rt, void *vw);
189 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
190 uint32_t weight, struct walkarg *w);
191 static int sysctl_iflist(int af, struct walkarg *w);
192 static int sysctl_ifmalist(int af, struct walkarg *w);
193 static int route_output(struct mbuf *m, struct socket *so, ...);
194 static void rt_getmetrics(const struct rtentry *rt,
195 const struct nhop_object *nh, struct rt_metrics *out);
196 static void rt_dispatch(struct mbuf *, sa_family_t);
197 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
198 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
199 static int update_rtm_from_rc(struct rt_addrinfo *info,
200 struct rt_msghdr **prtm, int alloc_len,
201 struct rib_cmd_info *rc, struct nhop_object *nh);
202 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
203 struct mbuf *m, sa_family_t saf, u_int fibnum,
205 static bool can_export_rte(struct ucred *td_ucred, bool rt_is_host,
206 const struct sockaddr *rt_dst);
208 static struct netisr_handler rtsock_nh = {
210 .nh_handler = rts_input,
211 .nh_proto = NETISR_ROUTE,
212 .nh_policy = NETISR_POLICY_SOURCE,
216 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
220 netisr_getqlimit(&rtsock_nh, &qlimit);
221 error = sysctl_handle_int(oidp, &qlimit, 0, req);
222 if (error || !req->newptr)
226 return (netisr_setqlimit(&rtsock_nh, qlimit));
228 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
229 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
230 0, 0, sysctl_route_netisr_maxqlen, "I",
231 "maximum routing socket dispatch queue length");
238 if (IS_DEFAULT_VNET(curvnet)) {
239 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
240 rtsock_nh.nh_qlimit = tmp;
241 netisr_register(&rtsock_nh);
245 netisr_register_vnet(&rtsock_nh);
248 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
253 vnet_rts_uninit(void)
256 netisr_unregister_vnet(&rtsock_nh);
258 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
263 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
268 KASSERT(m != NULL, ("%s: m is NULL", __func__));
269 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
270 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
272 /* No filtering requested. */
273 if ((m->m_flags & RTS_FILTER_FIB) == 0)
276 /* Check if it is a rts and the fib matches the one of the socket. */
277 fibnum = M_GETFIB(m);
278 if (proto->sp_family != PF_ROUTE ||
279 rp->rcb_socket == NULL ||
280 rp->rcb_socket->so_fibnum == fibnum)
283 /* Filtering requested and no match, the socket shall be skipped. */
288 rts_input(struct mbuf *m)
290 struct sockproto route_proto;
291 unsigned short *family;
294 route_proto.sp_family = PF_ROUTE;
295 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
297 family = (unsigned short *)(tag + 1);
298 route_proto.sp_protocol = *family;
299 m_tag_delete(m, tag);
301 route_proto.sp_protocol = 0;
303 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
307 * It really doesn't make any sense at all for this code to share much
308 * with raw_usrreq.c, since its functionality is so restricted. XXX
311 rts_abort(struct socket *so)
314 raw_usrreqs.pru_abort(so);
318 rts_close(struct socket *so)
321 raw_usrreqs.pru_close(so);
324 /* pru_accept is EOPNOTSUPP */
327 rts_attach(struct socket *so, int proto, struct thread *td)
332 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
335 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
337 so->so_pcb = (caddr_t)rp;
338 so->so_fibnum = td->td_proc->p_fibnum;
339 error = raw_attach(so, proto);
347 switch(rp->rcb_proto.sp_protocol) {
349 V_route_cb.ip_count++;
352 V_route_cb.ip6_count++;
355 V_route_cb.any_count++;
358 so->so_options |= SO_USELOOPBACK;
363 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
366 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
370 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
373 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
376 /* pru_connect2 is EOPNOTSUPP */
377 /* pru_control is EOPNOTSUPP */
380 rts_detach(struct socket *so)
382 struct rawcb *rp = sotorawcb(so);
384 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
387 switch(rp->rcb_proto.sp_protocol) {
389 V_route_cb.ip_count--;
392 V_route_cb.ip6_count--;
395 V_route_cb.any_count--;
397 raw_usrreqs.pru_detach(so);
401 rts_disconnect(struct socket *so)
404 return (raw_usrreqs.pru_disconnect(so));
407 /* pru_listen is EOPNOTSUPP */
410 rts_peeraddr(struct socket *so, struct sockaddr **nam)
413 return (raw_usrreqs.pru_peeraddr(so, nam));
416 /* pru_rcvd is EOPNOTSUPP */
417 /* pru_rcvoob is EOPNOTSUPP */
420 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
421 struct mbuf *control, struct thread *td)
424 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
427 /* pru_sense is null */
430 rts_shutdown(struct socket *so)
433 return (raw_usrreqs.pru_shutdown(so));
437 rts_sockaddr(struct socket *so, struct sockaddr **nam)
440 return (raw_usrreqs.pru_sockaddr(so, nam));
443 static struct pr_usrreqs route_usrreqs = {
444 .pru_abort = rts_abort,
445 .pru_attach = rts_attach,
446 .pru_bind = rts_bind,
447 .pru_connect = rts_connect,
448 .pru_detach = rts_detach,
449 .pru_disconnect = rts_disconnect,
450 .pru_peeraddr = rts_peeraddr,
451 .pru_send = rts_send,
452 .pru_shutdown = rts_shutdown,
453 .pru_sockaddr = rts_sockaddr,
454 .pru_close = rts_close,
457 #ifndef _SOCKADDR_UNION_DEFINED
458 #define _SOCKADDR_UNION_DEFINED
460 * The union of all possible address formats we handle.
462 union sockaddr_union {
464 struct sockaddr_in sin;
465 struct sockaddr_in6 sin6;
467 #endif /* _SOCKADDR_UNION_DEFINED */
470 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
471 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
473 #if defined(INET) || defined(INET6)
474 struct epoch_tracker et;
477 /* First, see if the returned address is part of the jail. */
478 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
479 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
483 switch (info->rti_info[RTAX_DST]->sa_family) {
493 * Try to find an address on the given outgoing interface
494 * that belongs to the jail.
497 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
500 if (sa->sa_family != AF_INET)
502 ia = ((struct sockaddr_in *)sa)->sin_addr;
503 if (prison_check_ip4(cred, &ia) == 0) {
511 * As a last resort return the 'default' jail address.
513 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
515 if (prison_get_ip4(cred, &ia) != 0)
518 bzero(&saun->sin, sizeof(struct sockaddr_in));
519 saun->sin.sin_len = sizeof(struct sockaddr_in);
520 saun->sin.sin_family = AF_INET;
521 saun->sin.sin_addr.s_addr = ia.s_addr;
522 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
535 * Try to find an address on the given outgoing interface
536 * that belongs to the jail.
539 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
542 if (sa->sa_family != AF_INET6)
544 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
545 &ia6, sizeof(struct in6_addr));
546 if (prison_check_ip6(cred, &ia6) == 0) {
554 * As a last resort return the 'default' jail address.
556 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
558 if (prison_get_ip6(cred, &ia6) != 0)
561 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
562 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
563 saun->sin6.sin6_family = AF_INET6;
564 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
565 if (sa6_recoverscope(&saun->sin6) != 0)
567 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
578 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
583 if (V_loif == NULL) {
584 RTS_PID_PRINTF("Unable to add blackhole/reject nhop without loopback");
587 info->rti_ifp = V_loif;
589 saf = info->rti_info[RTAX_DST]->sa_family;
591 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
592 if (ifa->ifa_addr->sa_family == saf) {
597 if (info->rti_ifa == NULL)
600 bzero(saun, sizeof(union sockaddr_union));
604 saun->sin.sin_family = AF_INET;
605 saun->sin.sin_len = sizeof(struct sockaddr_in);
606 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
611 saun->sin6.sin6_family = AF_INET6;
612 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
613 saun->sin6.sin6_addr = in6addr_loopback;
619 info->rti_info[RTAX_GATEWAY] = &saun->sa;
620 info->rti_flags |= RTF_GATEWAY;
626 * Fills in @info based on userland-provided @rtm message.
628 * Returns 0 on success.
631 fill_addrinfo(struct rt_msghdr *rtm, int len, struct linear_buffer *lb, u_int fibnum,
632 struct rt_addrinfo *info)
637 rtm->rtm_pid = curproc->p_pid;
638 info->rti_addrs = rtm->rtm_addrs;
640 info->rti_mflags = rtm->rtm_inits;
641 info->rti_rmx = &rtm->rtm_rmx;
644 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
645 * link-local address because rtrequest requires addresses with
648 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
651 info->rti_flags = rtm->rtm_flags;
652 error = cleanup_xaddrs(info, lb);
655 saf = info->rti_info[RTAX_DST]->sa_family;
657 * Verify that the caller has the appropriate privilege; RTM_GET
658 * is the only operation the non-superuser is allowed.
660 if (rtm->rtm_type != RTM_GET) {
661 error = priv_check(curthread, PRIV_NET_ROUTE);
667 * The given gateway address may be an interface address.
668 * For example, issuing a "route change" command on a route
669 * entry that was created from a tunnel, and the gateway
670 * address given is the local end point. In this case the
671 * RTF_GATEWAY flag must be cleared or the destination will
672 * not be reachable even though there is no error message.
674 if (info->rti_info[RTAX_GATEWAY] != NULL &&
675 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
676 struct rt_addrinfo ginfo;
677 struct sockaddr *gdst;
678 struct sockaddr_storage ss;
680 bzero(&ginfo, sizeof(ginfo));
681 bzero(&ss, sizeof(ss));
682 ss.ss_len = sizeof(ss);
684 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
685 gdst = info->rti_info[RTAX_GATEWAY];
688 * A host route through the loopback interface is
689 * installed for each interface adddress. In pre 8.0
690 * releases the interface address of a PPP link type
691 * is not reachable locally. This behavior is fixed as
692 * part of the new L2/L3 redesign and rewrite work. The
693 * signature of this interface address route is the
694 * AF_LINK sa_family type of the gateway, and the
695 * rt_ifp has the IFF_LOOPBACK flag set.
697 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
698 if (ss.ss_family == AF_LINK &&
699 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
700 info->rti_flags &= ~RTF_GATEWAY;
701 info->rti_flags |= RTF_GWFLAG_COMPAT;
703 rib_free_info(&ginfo);
710 static struct nhop_object *
711 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
713 if (!NH_IS_NHGRP(nh))
716 struct weightened_nhop *wn;
718 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
721 for (int i = 0; i < num_nhops; i++) {
722 if (match_nhop_gw(wn[i].nh, gw))
730 * Handles RTM_GET message from routing socket, returning matching rt.
733 * 0 on success, with locked and referenced matching rt in @rt_nrt
737 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
738 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
741 struct rib_head *rnh;
742 struct nhop_object *nh;
745 saf = info->rti_info[RTAX_DST]->sa_family;
747 rnh = rt_tables_get_rnh(fibnum, saf);
749 return (EAFNOSUPPORT);
754 * By (implicit) convention host route (one without netmask)
755 * means longest-prefix-match request and the route with netmask
756 * means exact-match lookup.
757 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
758 * prefixes, use original data to check for the netmask presence.
760 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
762 * Provide longest prefix match for
763 * address lookup (no mask).
764 * 'route -n get addr'
766 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
767 info->rti_info[RTAX_DST], &rnh->head);
769 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
770 info->rti_info[RTAX_DST],
771 info->rti_info[RTAX_NETMASK], &rnh->head);
773 if (rc->rc_rt == NULL) {
778 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
784 * If performing proxied L2 entry insertion, and
785 * the actual PPP host entry is found, perform
786 * another search to retrieve the prefix route of
787 * the local end point of the PPP link.
788 * TODO: move this logic to userland.
790 if (rtm->rtm_flags & RTF_ANNOUNCE) {
791 struct sockaddr laddr;
793 if (nh->nh_ifp != NULL &&
794 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
797 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
800 rt_maskedcopy(ifa->ifa_addr,
804 rt_maskedcopy(nh->nh_ifa->ifa_addr,
806 nh->nh_ifa->ifa_netmask);
808 * refactor rt and no lock operation necessary
810 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
812 if (rc->rc_rt == NULL) {
816 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
823 rc->rc_nh_weight = rc->rc_rt->rt_weight;
830 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
833 if (family == AF_INET) {
834 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
835 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
837 bzero(dst4, sizeof(struct sockaddr_in));
838 bzero(mask4, sizeof(struct sockaddr_in));
840 dst4->sin_family = AF_INET;
841 dst4->sin_len = sizeof(struct sockaddr_in);
842 mask4->sin_family = AF_INET;
843 mask4->sin_len = sizeof(struct sockaddr_in);
847 if (family == AF_INET6) {
848 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
849 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
851 bzero(dst6, sizeof(struct sockaddr_in6));
852 bzero(mask6, sizeof(struct sockaddr_in6));
854 dst6->sin6_family = AF_INET6;
855 dst6->sin6_len = sizeof(struct sockaddr_in6);
856 mask6->sin6_family = AF_INET6;
857 mask6->sin6_len = sizeof(struct sockaddr_in6);
863 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
864 struct sockaddr *mask)
867 if (dst->sa_family == AF_INET) {
868 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
869 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
870 uint32_t scopeid = 0;
871 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
877 if (dst->sa_family == AF_INET6) {
878 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
879 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
880 uint32_t scopeid = 0;
881 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
882 &mask6->sin6_addr, &scopeid);
883 dst6->sin6_scope_id = scopeid;
890 update_rtm_from_info(struct rt_addrinfo *info, struct rt_msghdr **prtm,
893 struct rt_msghdr *rtm, *orig_rtm = NULL;
898 /* Check if we need to realloc storage */
899 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
900 if (len > alloc_len) {
901 struct rt_msghdr *tmp_rtm;
903 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
906 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
912 * Delay freeing original rtm as info contains
913 * data referencing it.
917 w.w_tmem = (caddr_t)rtm;
918 w.w_tmemsize = alloc_len;
919 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
920 rtm->rtm_addrs = info->rti_addrs;
922 if (orig_rtm != NULL)
923 free(orig_rtm, M_TEMP);
930 * Update sockaddrs, flags, etc in @prtm based on @rc data.
931 * rtm can be reallocated.
933 * Returns 0 on success, along with pointer to (potentially reallocated)
938 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
939 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
941 union sockaddr_union saun;
942 struct rt_msghdr *rtm;
947 union sockaddr_union sa_dst, sa_mask;
948 int family = info->rti_info[RTAX_DST]->sa_family;
949 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
950 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
952 info->rti_info[RTAX_DST] = &sa_dst.sa;
953 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
954 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
955 info->rti_info[RTAX_GENMASK] = 0;
957 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
959 info->rti_info[RTAX_IFP] =
960 ifp->if_addr->ifa_addr;
961 error = rtm_get_jailed(info, ifp, nh,
962 &saun, curthread->td_ucred);
965 if (ifp->if_flags & IFF_POINTOPOINT)
966 info->rti_info[RTAX_BRD] =
967 nh->nh_ifa->ifa_dstaddr;
968 rtm->rtm_index = ifp->if_index;
970 info->rti_info[RTAX_IFP] = NULL;
971 info->rti_info[RTAX_IFA] = NULL;
973 } else if (ifp != NULL)
974 rtm->rtm_index = ifp->if_index;
976 if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0)
980 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
981 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
982 rtm->rtm_flags = RTF_GATEWAY |
983 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
984 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
985 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
992 save_del_notification(struct rib_cmd_info *rc, void *_cbdata)
994 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
996 if (rc->rc_cmd == RTM_DELETE)
1001 save_add_notification(struct rib_cmd_info *rc, void *_cbdata)
1003 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1005 if (rc->rc_cmd == RTM_ADD)
1010 #if defined(INET6) || defined(INET)
1011 static struct sockaddr *
1012 alloc_sockaddr_aligned(struct linear_buffer *lb, int len)
1014 len = roundup2(len, sizeof(uint64_t));
1015 if (lb->offset + len > lb->size)
1017 struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset);
1025 route_output(struct mbuf *m, struct socket *so, ...)
1027 struct rt_msghdr *rtm = NULL;
1028 struct rtentry *rt = NULL;
1029 struct rt_addrinfo info;
1030 struct epoch_tracker et;
1032 struct sockaddr_storage ss;
1033 struct sockaddr_in6 *sin6;
1034 int i, rti_need_deembed = 0;
1036 int alloc_len = 0, len, error = 0, fibnum;
1037 sa_family_t saf = AF_UNSPEC;
1038 struct rib_cmd_info rc;
1039 struct nhop_object *nh;
1041 fibnum = so->so_fibnum;
1042 #define senderr(e) { error = e; goto flush;}
1043 if (m == NULL || ((m->m_len < sizeof(long)) &&
1044 (m = m_pullup(m, sizeof(long))) == NULL))
1046 if ((m->m_flags & M_PKTHDR) == 0)
1047 panic("route_output");
1048 NET_EPOCH_ENTER(et);
1049 len = m->m_pkthdr.len;
1050 if (len < sizeof(*rtm) ||
1051 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1055 * Most of current messages are in range 200-240 bytes,
1056 * minimize possible re-allocation on reply using larger size
1057 * buffer aligned on 1k boundaty.
1059 alloc_len = roundup2(len, 1024);
1060 int total_len = alloc_len + SCRATCH_BUFFER_SIZE;
1061 if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL)
1064 m_copydata(m, 0, len, (caddr_t)rtm);
1065 bzero(&info, sizeof(info));
1067 struct linear_buffer lb = {
1068 .base = (char *)rtm + alloc_len,
1069 .size = SCRATCH_BUFFER_SIZE,
1072 if (rtm->rtm_version != RTM_VERSION) {
1073 /* Do not touch message since format is unknown */
1076 senderr(EPROTONOSUPPORT);
1080 * Starting from here, it is possible
1081 * to alter original message and insert
1082 * caller PID and error value.
1085 if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) {
1088 /* fill_addringo() embeds scope into IPv6 addresses */
1090 rti_need_deembed = 1;
1093 saf = info.rti_info[RTAX_DST]->sa_family;
1095 /* support for new ARP code */
1096 if (rtm->rtm_flags & RTF_LLDATA) {
1097 error = lla_rt_output(rtm, &info);
1101 union sockaddr_union gw_saun;
1102 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1103 if (blackhole_flags != 0) {
1104 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1105 error = fill_blackholeinfo(&info, &gw_saun);
1112 switch (rtm->rtm_type) {
1115 if (rtm->rtm_type == RTM_ADD) {
1116 if (info.rti_info[RTAX_GATEWAY] == NULL)
1119 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1122 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1123 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1124 struct rib_cmd_info rc_simple = {};
1125 rib_decompose_notification(&rc,
1126 save_add_notification, (void *)&rc_simple);
1131 rtm->rtm_index = nh->nh_ifp->if_index;
1132 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1137 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1140 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1141 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1142 struct rib_cmd_info rc_simple = {};
1143 rib_decompose_notification(&rc,
1144 save_del_notification, (void *)&rc_simple);
1153 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1158 if (!can_export_rte(curthread->td_ucred,
1159 info.rti_info[RTAX_NETMASK] == NULL,
1160 info.rti_info[RTAX_DST])) {
1166 senderr(EOPNOTSUPP);
1170 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1172 * Note that some sockaddr pointers may have changed to
1173 * point to memory outsize @rtm. Some may be pointing
1174 * to the on-stack variables.
1175 * Given that, any pointer in @info CANNOT BE USED.
1179 * scopeid deembedding has been performed while
1180 * writing updated rtm in rtsock_msg_buffer().
1181 * With that in mind, skip deembedding procedure below.
1184 rti_need_deembed = 0;
1194 if (rti_need_deembed) {
1195 /* sin6_scope_id is recovered before sending rtm. */
1196 sin6 = (struct sockaddr_in6 *)&ss;
1197 for (i = 0; i < RTAX_MAX; i++) {
1198 if (info.rti_info[i] == NULL)
1200 if (info.rti_info[i]->sa_family != AF_INET6)
1202 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1203 if (sa6_recoverscope(sin6) == 0)
1204 bcopy(sin6, info.rti_info[i],
1207 if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) {
1214 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1220 * Sends the prepared reply message in @rtm to all rtsock clients.
1221 * Frees @m and @rtm.
1225 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1226 sa_family_t saf, u_int fibnum, int rtm_errno)
1228 struct rawcb *rp = NULL;
1231 * Check to see if we don't want our own messages.
1233 if ((so->so_options & SO_USELOOPBACK) == 0) {
1234 if (V_route_cb.any_count <= 1) {
1240 /* There is another listener, so construct message */
1246 rtm->rtm_errno = rtm_errno;
1248 rtm->rtm_flags |= RTF_DONE;
1250 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1251 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1254 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1255 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1260 M_SETFIB(m, fibnum);
1261 m->m_flags |= RTS_FILTER_FIB;
1264 * XXX insure we don't get a copy by
1265 * invalidating our protocol
1267 unsigned short family = rp->rcb_proto.sp_family;
1268 rp->rcb_proto.sp_family = 0;
1269 rt_dispatch(m, saf);
1270 rp->rcb_proto.sp_family = family;
1272 rt_dispatch(m, saf);
1277 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1278 struct rt_metrics *out)
1281 bzero(out, sizeof(*out));
1282 out->rmx_mtu = nh->nh_mtu;
1283 out->rmx_weight = rt->rt_weight;
1284 out->rmx_nhidx = nhop_get_idx(nh);
1285 /* Kernel -> userland timebase conversion. */
1286 out->rmx_expire = rt->rt_expire ?
1287 rt->rt_expire - time_uptime + time_second : 0;
1291 * Extract the addresses of the passed sockaddrs.
1292 * Do a little sanity checking so as to avoid bad memory references.
1293 * This data is derived straight from userland.
1296 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1298 struct sockaddr *sa;
1301 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1302 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1304 sa = (struct sockaddr *)cp;
1308 if (cp + sa->sa_len > cplim)
1311 * there are no more.. quit now
1312 * If there are more bits, they are in error.
1313 * I've seen this. route(1) can evidently generate these.
1314 * This causes kernel to core dump.
1315 * for compatibility, If we see this, point to a safe address.
1317 if (sa->sa_len == 0) {
1318 rtinfo->rti_info[i] = &sa_zero;
1319 return (0); /* should be EINVAL but for compat */
1323 if (sa->sa_family == AF_INET6)
1324 sa6_embedscope((struct sockaddr_in6 *)sa,
1327 rtinfo->rti_info[i] = sa;
1335 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1338 const struct sockaddr_in nsin = {
1339 .sin_family = AF_INET,
1340 .sin_len = sizeof(struct sockaddr_in),
1349 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1353 const struct sockaddr_in6 nsin6 = {
1354 .sin6_family = AF_INET6,
1355 .sin6_len = sizeof(struct sockaddr_in6),
1356 .sin6_addr = *addr6,
1357 .sin6_scope_id = scopeid,
1363 #if defined(INET6) || defined(INET)
1365 * Checks if gateway is suitable for lltable operations.
1366 * Lltable code requires AF_LINK gateway with ifindex
1367 * and mac address specified.
1368 * Returns 0 on success.
1371 cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1373 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1375 if (sdl->sdl_family != AF_LINK)
1378 if (sdl->sdl_index == 0)
1381 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len)
1388 cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb)
1390 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1391 struct sockaddr *sa;
1393 if (info->rti_flags & RTF_LLDATA)
1394 return (cleanup_xaddrs_lladdr(info));
1396 switch (gw->sa_family) {
1400 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1402 /* Ensure reads do not go beyoud SA boundary */
1403 if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) {
1404 RTS_PID_PRINTF("gateway sin_len too small: %d", gw->sa_len);
1407 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in));
1410 fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr);
1411 info->rti_info[RTAX_GATEWAY] = sa;
1418 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1419 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1420 RTS_PID_PRINTF("gateway sin6_len too small: %d", gw->sa_len);
1423 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1429 struct sockaddr_dl *gw_sdl;
1431 size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data);
1432 gw_sdl = (struct sockaddr_dl *)gw;
1433 if (gw_sdl->sdl_len < sdl_min_len) {
1434 RTS_PID_PRINTF("gateway sdl_len too small: %d", gw_sdl->sdl_len);
1437 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short));
1441 const struct sockaddr_dl_short sdl = {
1442 .sdl_family = AF_LINK,
1443 .sdl_len = sizeof(struct sockaddr_dl_short),
1444 .sdl_index = gw_sdl->sdl_index,
1446 *((struct sockaddr_dl_short *)sa) = sdl;
1447 info->rti_info[RTAX_GATEWAY] = sa;
1457 remove_netmask(struct rt_addrinfo *info)
1459 info->rti_info[RTAX_NETMASK] = NULL;
1460 info->rti_flags |= RTF_HOST;
1461 info->rti_addrs &= ~RTA_NETMASK;
1466 cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb)
1468 struct sockaddr_in *dst_sa, *mask_sa;
1469 const int sa_len = sizeof(struct sockaddr_in);
1470 struct in_addr dst, mask;
1472 /* Check & fixup dst/netmask combination first */
1473 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1474 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1476 /* Ensure reads do not go beyound the buffer size */
1477 if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero))
1480 if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1482 * Some older routing software encode mask length into the
1483 * sin_len, thus resulting in "truncated" sockaddr.
1485 int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr);
1488 if (len > sizeof(struct in_addr))
1489 len = sizeof(struct in_addr);
1490 memcpy(&mask, &mask_sa->sin_addr, len);
1492 RTS_PID_PRINTF("prefix mask sin_len too small: %d", mask_sa->sin_len);
1496 mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST;
1498 dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr));
1500 /* Construct new "clean" dst/mask sockaddresses */
1501 if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1503 fill_sockaddr_inet(dst_sa, dst);
1504 info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa;
1506 if (mask.s_addr != INADDR_BROADCAST) {
1507 if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1509 fill_sockaddr_inet(mask_sa, mask);
1510 info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa;
1511 info->rti_flags &= ~RTF_HOST;
1513 remove_netmask(info);
1516 if (info->rti_info[RTAX_GATEWAY] != NULL)
1517 return (cleanup_xaddrs_gateway(info, lb));
1525 cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb)
1527 struct sockaddr *sa;
1528 struct sockaddr_in6 *dst_sa, *mask_sa;
1529 struct in6_addr mask, *dst;
1530 const int sa_len = sizeof(struct sockaddr_in6);
1532 /* Check & fixup dst/netmask combination first */
1533 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1534 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1536 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1537 RTS_PID_PRINTF("prefix dst sin6_len too small: %d", dst_sa->sin6_len);
1541 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1543 * Some older routing software encode mask length into the
1544 * sin6_len, thus resulting in "truncated" sockaddr.
1546 int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1548 bzero(&mask, sizeof(mask));
1549 if (len > sizeof(struct in6_addr))
1550 len = sizeof(struct in6_addr);
1551 memcpy(&mask, &mask_sa->sin6_addr, len);
1553 RTS_PID_PRINTF("rtsock: prefix mask sin6_len too small: %d", mask_sa->sin6_len);
1557 mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1559 dst = &dst_sa->sin6_addr;
1560 IN6_MASK_ADDR(dst, &mask);
1562 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1564 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0);
1565 info->rti_info[RTAX_DST] = sa;
1567 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) {
1568 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1570 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0);
1571 info->rti_info[RTAX_NETMASK] = sa;
1572 info->rti_flags &= ~RTF_HOST;
1574 remove_netmask(info);
1577 if (info->rti_info[RTAX_GATEWAY] != NULL)
1578 return (cleanup_xaddrs_gateway(info, lb));
1585 cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb)
1587 int error = EAFNOSUPPORT;
1589 if (info->rti_info[RTAX_DST] == NULL)
1592 if (info->rti_flags & RTF_LLDATA) {
1594 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1595 * prefix along with the actual address in RTA_DST.
1596 * Remove netmask to avoid unnecessary address masking.
1598 remove_netmask(info);
1601 switch (info->rti_info[RTAX_DST]->sa_family) {
1604 error = cleanup_xaddrs_inet(info, lb);
1609 error = cleanup_xaddrs_inet6(info, lb);
1618 * Fill in @dmask with valid netmask leaving original @smask
1619 * intact. Mostly used with radix netmasks.
1622 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1623 struct sockaddr_storage *dmask)
1625 if (dst == NULL || smask == NULL)
1628 memset(dmask, 0, dst->sa_len);
1629 memcpy(dmask, smask, smask->sa_len);
1630 dmask->ss_len = dst->sa_len;
1631 dmask->ss_family = dst->sa_family;
1633 return ((struct sockaddr *)dmask);
1637 * Writes information related to @rtinfo object to newly-allocated mbuf.
1638 * Assumes MCLBYTES is enough to construct any message.
1639 * Used for OS notifications of vaious events (if/ifa announces,etc)
1641 * Returns allocated mbuf or NULL on failure.
1643 static struct mbuf *
1644 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1646 struct sockaddr_storage ss;
1647 struct rt_msghdr *rtm;
1650 struct sockaddr *sa;
1652 struct sockaddr_in6 *sin6;
1659 len = sizeof(struct ifa_msghdr);
1664 len = sizeof(struct ifma_msghdr);
1668 len = sizeof(struct if_msghdr);
1671 case RTM_IFANNOUNCE:
1673 len = sizeof(struct if_announcemsghdr);
1677 len = sizeof(struct rt_msghdr);
1680 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1681 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1683 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1685 m = m_gethdr(M_NOWAIT, MT_DATA);
1689 m->m_pkthdr.len = m->m_len = len;
1690 rtm = mtod(m, struct rt_msghdr *);
1691 bzero((caddr_t)rtm, len);
1692 for (i = 0; i < RTAX_MAX; i++) {
1693 if ((sa = rtinfo->rti_info[i]) == NULL)
1695 rtinfo->rti_addrs |= (1 << i);
1698 KASSERT(dlen <= sizeof(ss),
1699 ("%s: sockaddr size overflow", __func__));
1700 bzero(&ss, sizeof(ss));
1701 bcopy(sa, &ss, sa->sa_len);
1702 sa = (struct sockaddr *)&ss;
1704 if (sa->sa_family == AF_INET6) {
1705 sin6 = (struct sockaddr_in6 *)sa;
1706 (void)sa6_recoverscope(sin6);
1709 m_copyback(m, len, dlen, (caddr_t)sa);
1712 if (m->m_pkthdr.len != len) {
1716 rtm->rtm_msglen = len;
1717 rtm->rtm_version = RTM_VERSION;
1718 rtm->rtm_type = type;
1723 * Writes information related to @rtinfo object to preallocated buffer.
1724 * Stores needed size in @plen. If @w is NULL, calculates size without
1726 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1728 * Returns 0 on success.
1732 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1734 struct sockaddr_storage ss;
1735 int len, buflen = 0, dlen, i;
1737 struct rt_msghdr *rtm = NULL;
1739 struct sockaddr_in6 *sin6;
1741 #ifdef COMPAT_FREEBSD32
1742 bool compat32 = false;
1748 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1749 #ifdef COMPAT_FREEBSD32
1750 if (w->w_req->flags & SCTL_MASK32) {
1751 len = sizeof(struct ifa_msghdrl32);
1755 len = sizeof(struct ifa_msghdrl);
1757 len = sizeof(struct ifa_msghdr);
1761 #ifdef COMPAT_FREEBSD32
1762 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1763 if (w->w_op == NET_RT_IFLISTL)
1764 len = sizeof(struct if_msghdrl32);
1766 len = sizeof(struct if_msghdr32);
1771 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1772 len = sizeof(struct if_msghdrl);
1774 len = sizeof(struct if_msghdr);
1778 len = sizeof(struct ifma_msghdr);
1782 len = sizeof(struct rt_msghdr);
1786 rtm = (struct rt_msghdr *)w->w_tmem;
1787 buflen = w->w_tmemsize - len;
1788 cp = (caddr_t)w->w_tmem + len;
1791 rtinfo->rti_addrs = 0;
1792 for (i = 0; i < RTAX_MAX; i++) {
1793 struct sockaddr *sa;
1795 if ((sa = rtinfo->rti_info[i]) == NULL)
1797 rtinfo->rti_addrs |= (1 << i);
1798 #ifdef COMPAT_FREEBSD32
1800 dlen = SA_SIZE32(sa);
1804 if (cp != NULL && buflen >= dlen) {
1805 KASSERT(dlen <= sizeof(ss),
1806 ("%s: sockaddr size overflow", __func__));
1807 bzero(&ss, sizeof(ss));
1808 bcopy(sa, &ss, sa->sa_len);
1809 sa = (struct sockaddr *)&ss;
1811 if (sa->sa_family == AF_INET6) {
1812 sin6 = (struct sockaddr_in6 *)sa;
1813 (void)sa6_recoverscope(sin6);
1816 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1819 } else if (cp != NULL) {
1821 * Buffer too small. Count needed size
1822 * and return with error.
1831 dlen = ALIGN(len) - len;
1843 /* fill header iff buffer is large enough */
1844 rtm->rtm_version = RTM_VERSION;
1845 rtm->rtm_type = type;
1846 rtm->rtm_msglen = len;
1851 if (w != NULL && cp == NULL)
1858 * This routine is called to generate a message from the routing
1859 * socket indicating that a redirect has occurred, a routing lookup
1860 * has failed, or that a protocol has detected timeouts to a particular
1864 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1867 struct rt_msghdr *rtm;
1869 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1871 if (V_route_cb.any_count == 0)
1873 m = rtsock_msg_mbuf(type, rtinfo);
1877 if (fibnum != RT_ALL_FIBS) {
1878 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1879 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1880 M_SETFIB(m, fibnum);
1881 m->m_flags |= RTS_FILTER_FIB;
1884 rtm = mtod(m, struct rt_msghdr *);
1885 rtm->rtm_flags = RTF_DONE | flags;
1886 rtm->rtm_errno = error;
1887 rtm->rtm_addrs = rtinfo->rti_addrs;
1888 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1892 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1895 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1899 * This routine is called to generate a message from the routing
1900 * socket indicating that the status of a network interface has changed.
1903 rt_ifmsg(struct ifnet *ifp)
1905 struct if_msghdr *ifm;
1907 struct rt_addrinfo info;
1909 if (V_route_cb.any_count == 0)
1911 bzero((caddr_t)&info, sizeof(info));
1912 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1915 ifm = mtod(m, struct if_msghdr *);
1916 ifm->ifm_index = ifp->if_index;
1917 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1918 if_data_copy(ifp, &ifm->ifm_data);
1920 rt_dispatch(m, AF_UNSPEC);
1924 * Announce interface address arrival/withdraw.
1925 * Please do not call directly, use rt_addrmsg().
1926 * Assume input data to be valid.
1927 * Returns 0 on success.
1930 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1932 struct rt_addrinfo info;
1933 struct sockaddr *sa;
1936 struct ifa_msghdr *ifam;
1937 struct ifnet *ifp = ifa->ifa_ifp;
1938 struct sockaddr_storage ss;
1940 if (V_route_cb.any_count == 0)
1943 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1945 bzero((caddr_t)&info, sizeof(info));
1946 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1947 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1948 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1949 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1950 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1951 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1953 ifam = mtod(m, struct ifa_msghdr *);
1954 ifam->ifam_index = ifp->if_index;
1955 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1956 ifam->ifam_flags = ifa->ifa_flags;
1957 ifam->ifam_addrs = info.rti_addrs;
1959 if (fibnum != RT_ALL_FIBS) {
1960 M_SETFIB(m, fibnum);
1961 m->m_flags |= RTS_FILTER_FIB;
1964 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1970 * Announce route addition/removal to rtsock based on @rt data.
1971 * Callers are advives to use rt_routemsg() instead of using this
1972 * function directly.
1973 * Assume @rt data is consistent.
1975 * Returns 0 on success.
1978 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
1981 union sockaddr_union dst, mask;
1982 struct rt_addrinfo info;
1984 if (V_route_cb.any_count == 0)
1987 int family = rt_get_family(rt);
1988 init_sockaddrs_family(family, &dst.sa, &mask.sa);
1989 export_rtaddrs(rt, &dst.sa, &mask.sa);
1991 bzero((caddr_t)&info, sizeof(info));
1992 info.rti_info[RTAX_DST] = &dst.sa;
1993 info.rti_info[RTAX_NETMASK] = &mask.sa;
1994 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1995 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
1996 info.rti_ifp = nh->nh_ifp;
1998 return (rtsock_routemsg_info(cmd, &info, fibnum));
2002 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2004 struct rt_msghdr *rtm;
2005 struct sockaddr *sa;
2008 if (V_route_cb.any_count == 0)
2011 if (info->rti_flags & RTF_HOST)
2012 info->rti_info[RTAX_NETMASK] = NULL;
2014 m = rtsock_msg_mbuf(cmd, info);
2018 if (fibnum != RT_ALL_FIBS) {
2019 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
2020 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
2021 M_SETFIB(m, fibnum);
2022 m->m_flags |= RTS_FILTER_FIB;
2025 rtm = mtod(m, struct rt_msghdr *);
2026 rtm->rtm_addrs = info->rti_addrs;
2027 if (info->rti_ifp != NULL)
2028 rtm->rtm_index = info->rti_ifp->if_index;
2029 /* Add RTF_DONE to indicate command 'completion' required by API */
2030 info->rti_flags |= RTF_DONE;
2031 /* Reported routes has to be up */
2032 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
2033 info->rti_flags |= RTF_UP;
2034 rtm->rtm_flags = info->rti_flags;
2036 sa = info->rti_info[RTAX_DST];
2037 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2043 * This is the analogue to the rt_newaddrmsg which performs the same
2044 * function but for multicast group memberhips. This is easier since
2045 * there is no route state to worry about.
2048 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
2050 struct rt_addrinfo info;
2051 struct mbuf *m = NULL;
2052 struct ifnet *ifp = ifma->ifma_ifp;
2053 struct ifma_msghdr *ifmam;
2055 if (V_route_cb.any_count == 0)
2058 bzero((caddr_t)&info, sizeof(info));
2059 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2060 if (ifp && ifp->if_addr)
2061 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
2063 info.rti_info[RTAX_IFP] = NULL;
2065 * If a link-layer address is present, present it as a ``gateway''
2066 * (similarly to how ARP entries, e.g., are presented).
2068 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
2069 m = rtsock_msg_mbuf(cmd, &info);
2072 ifmam = mtod(m, struct ifma_msghdr *);
2073 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
2075 ifmam->ifmam_index = ifp->if_index;
2076 ifmam->ifmam_addrs = info.rti_addrs;
2077 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
2080 static struct mbuf *
2081 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
2082 struct rt_addrinfo *info)
2084 struct if_announcemsghdr *ifan;
2087 if (V_route_cb.any_count == 0)
2089 bzero((caddr_t)info, sizeof(*info));
2090 m = rtsock_msg_mbuf(type, info);
2092 ifan = mtod(m, struct if_announcemsghdr *);
2093 ifan->ifan_index = ifp->if_index;
2094 strlcpy(ifan->ifan_name, ifp->if_xname,
2095 sizeof(ifan->ifan_name));
2096 ifan->ifan_what = what;
2102 * This is called to generate routing socket messages indicating
2103 * IEEE80211 wireless events.
2104 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2107 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2110 struct rt_addrinfo info;
2112 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2115 * Append the ieee80211 data. Try to stick it in the
2116 * mbuf containing the ifannounce msg; otherwise allocate
2117 * a new mbuf and append.
2119 * NB: we assume m is a single mbuf.
2121 if (data_len > M_TRAILINGSPACE(m)) {
2122 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2127 bcopy(data, mtod(n, void *), data_len);
2128 n->m_len = data_len;
2130 } else if (data_len > 0) {
2131 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2132 m->m_len += data_len;
2134 if (m->m_flags & M_PKTHDR)
2135 m->m_pkthdr.len += data_len;
2136 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2137 rt_dispatch(m, AF_UNSPEC);
2142 * This is called to generate routing socket messages indicating
2143 * network interface arrival and departure.
2146 rt_ifannouncemsg(struct ifnet *ifp, int what)
2149 struct rt_addrinfo info;
2151 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2153 rt_dispatch(m, AF_UNSPEC);
2157 rt_dispatch(struct mbuf *m, sa_family_t saf)
2162 * Preserve the family from the sockaddr, if any, in an m_tag for
2163 * use when injecting the mbuf into the routing socket buffer from
2166 if (saf != AF_UNSPEC) {
2167 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
2173 *(unsigned short *)(tag + 1) = saf;
2174 m_tag_prepend(m, tag);
2178 m->m_pkthdr.rcvif = V_loif;
2184 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
2188 * Checks if rte can be exported w.r.t jails/vnets.
2190 * Returns true if it can, false otherwise.
2193 can_export_rte(struct ucred *td_ucred, bool rt_is_host,
2194 const struct sockaddr *rt_dst)
2197 if ((!rt_is_host) ? jailed_without_vnet(td_ucred)
2198 : prison_if(td_ucred, rt_dst) != 0)
2205 * This is used in dumping the kernel table via sysctl().
2208 sysctl_dumpentry(struct rtentry *rt, void *vw)
2210 struct walkarg *w = vw;
2211 struct nhop_object *nh;
2216 export_rtaddrs(rt, w->dst, w->mask);
2217 if (!can_export_rte(w->w_req->td->td_ucred, rt_is_host(rt), w->dst))
2219 nh = rt_get_raw_nhop(rt);
2221 if (NH_IS_NHGRP(nh)) {
2222 struct weightened_nhop *wn;
2224 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2225 for (int i = 0; i < num_nhops; i++) {
2226 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2232 error = sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2239 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2242 struct rt_addrinfo info;
2243 int error = 0, size;
2246 rtflags = nhop_get_rtflags(nh);
2248 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2251 bzero((caddr_t)&info, sizeof(info));
2252 info.rti_info[RTAX_DST] = w->dst;
2253 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2254 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2255 info.rti_info[RTAX_GENMASK] = 0;
2256 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2257 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2258 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2259 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2260 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2262 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2264 if (w->w_req && w->w_tmem) {
2265 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2267 bzero(&rtm->rtm_index,
2268 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2271 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2272 * and RTF_UP (if entry is linked, which is always true here).
2273 * Given that, use nhop rtflags & add RTF_UP.
2275 rtm->rtm_flags = rtflags | RTF_UP;
2276 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2277 rtm->rtm_flags = RTF_GATEWAY |
2278 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2279 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2280 rtm->rtm_rmx.rmx_weight = weight;
2281 rtm->rtm_index = nh->nh_ifp->if_index;
2282 rtm->rtm_addrs = info.rti_addrs;
2283 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2290 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2291 struct rt_addrinfo *info, struct walkarg *w, int len)
2293 struct if_msghdrl *ifm;
2294 struct if_data *ifd;
2296 ifm = (struct if_msghdrl *)w->w_tmem;
2298 #ifdef COMPAT_FREEBSD32
2299 if (w->w_req->flags & SCTL_MASK32) {
2300 struct if_msghdrl32 *ifm32;
2302 ifm32 = (struct if_msghdrl32 *)ifm;
2303 ifm32->ifm_addrs = info->rti_addrs;
2304 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2305 ifm32->ifm_index = ifp->if_index;
2306 ifm32->_ifm_spare1 = 0;
2307 ifm32->ifm_len = sizeof(*ifm32);
2308 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2309 ifm32->_ifm_spare2 = 0;
2310 ifd = &ifm32->ifm_data;
2314 ifm->ifm_addrs = info->rti_addrs;
2315 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2316 ifm->ifm_index = ifp->if_index;
2317 ifm->_ifm_spare1 = 0;
2318 ifm->ifm_len = sizeof(*ifm);
2319 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2320 ifm->_ifm_spare2 = 0;
2321 ifd = &ifm->ifm_data;
2324 memcpy(ifd, src_ifd, sizeof(*ifd));
2326 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2330 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2331 struct rt_addrinfo *info, struct walkarg *w, int len)
2333 struct if_msghdr *ifm;
2334 struct if_data *ifd;
2336 ifm = (struct if_msghdr *)w->w_tmem;
2338 #ifdef COMPAT_FREEBSD32
2339 if (w->w_req->flags & SCTL_MASK32) {
2340 struct if_msghdr32 *ifm32;
2342 ifm32 = (struct if_msghdr32 *)ifm;
2343 ifm32->ifm_addrs = info->rti_addrs;
2344 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2345 ifm32->ifm_index = ifp->if_index;
2346 ifm32->_ifm_spare1 = 0;
2347 ifd = &ifm32->ifm_data;
2351 ifm->ifm_addrs = info->rti_addrs;
2352 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2353 ifm->ifm_index = ifp->if_index;
2354 ifm->_ifm_spare1 = 0;
2355 ifd = &ifm->ifm_data;
2358 memcpy(ifd, src_ifd, sizeof(*ifd));
2360 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2364 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2365 struct walkarg *w, int len)
2367 struct ifa_msghdrl *ifam;
2368 struct if_data *ifd;
2370 ifam = (struct ifa_msghdrl *)w->w_tmem;
2372 #ifdef COMPAT_FREEBSD32
2373 if (w->w_req->flags & SCTL_MASK32) {
2374 struct ifa_msghdrl32 *ifam32;
2376 ifam32 = (struct ifa_msghdrl32 *)ifam;
2377 ifam32->ifam_addrs = info->rti_addrs;
2378 ifam32->ifam_flags = ifa->ifa_flags;
2379 ifam32->ifam_index = ifa->ifa_ifp->if_index;
2380 ifam32->_ifam_spare1 = 0;
2381 ifam32->ifam_len = sizeof(*ifam32);
2382 ifam32->ifam_data_off =
2383 offsetof(struct ifa_msghdrl32, ifam_data);
2384 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2385 ifd = &ifam32->ifam_data;
2389 ifam->ifam_addrs = info->rti_addrs;
2390 ifam->ifam_flags = ifa->ifa_flags;
2391 ifam->ifam_index = ifa->ifa_ifp->if_index;
2392 ifam->_ifam_spare1 = 0;
2393 ifam->ifam_len = sizeof(*ifam);
2394 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2395 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2396 ifd = &ifam->ifam_data;
2399 bzero(ifd, sizeof(*ifd));
2400 ifd->ifi_datalen = sizeof(struct if_data);
2401 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2402 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2403 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2404 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2406 /* Fixup if_data carp(4) vhid. */
2407 if (carp_get_vhid_p != NULL)
2408 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2410 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2414 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2415 struct walkarg *w, int len)
2417 struct ifa_msghdr *ifam;
2419 ifam = (struct ifa_msghdr *)w->w_tmem;
2420 ifam->ifam_addrs = info->rti_addrs;
2421 ifam->ifam_flags = ifa->ifa_flags;
2422 ifam->ifam_index = ifa->ifa_ifp->if_index;
2423 ifam->_ifam_spare1 = 0;
2424 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2426 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2430 sysctl_iflist(int af, struct walkarg *w)
2435 struct rt_addrinfo info;
2437 struct sockaddr_storage ss;
2439 bzero((caddr_t)&info, sizeof(info));
2440 bzero(&ifd, sizeof(ifd));
2441 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2442 if (w->w_arg && w->w_arg != ifp->if_index)
2444 if_data_copy(ifp, &ifd);
2446 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2447 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2450 info.rti_info[RTAX_IFP] = NULL;
2451 if (w->w_req && w->w_tmem) {
2452 if (w->w_op == NET_RT_IFLISTL)
2453 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2456 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2461 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2462 if (af && af != ifa->ifa_addr->sa_family)
2464 if (prison_if(w->w_req->td->td_ucred,
2465 ifa->ifa_addr) != 0)
2467 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2468 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2469 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2470 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2471 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2474 if (w->w_req && w->w_tmem) {
2475 if (w->w_op == NET_RT_IFLISTL)
2476 error = sysctl_iflist_ifaml(ifa, &info,
2479 error = sysctl_iflist_ifam(ifa, &info,
2485 info.rti_info[RTAX_IFA] = NULL;
2486 info.rti_info[RTAX_NETMASK] = NULL;
2487 info.rti_info[RTAX_BRD] = NULL;
2494 sysctl_ifmalist(int af, struct walkarg *w)
2496 struct rt_addrinfo info;
2498 struct ifmultiaddr *ifma;
2505 bzero((caddr_t)&info, sizeof(info));
2507 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2508 if (w->w_arg && w->w_arg != ifp->if_index)
2511 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2512 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2513 if (af && af != ifma->ifma_addr->sa_family)
2515 if (prison_if(w->w_req->td->td_ucred,
2516 ifma->ifma_addr) != 0)
2518 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2519 info.rti_info[RTAX_GATEWAY] =
2520 (ifma->ifma_addr->sa_family != AF_LINK) ?
2521 ifma->ifma_lladdr : NULL;
2522 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2525 if (w->w_req && w->w_tmem) {
2526 struct ifma_msghdr *ifmam;
2528 ifmam = (struct ifma_msghdr *)w->w_tmem;
2529 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2530 ifmam->ifmam_flags = 0;
2531 ifmam->ifmam_addrs = info.rti_addrs;
2532 ifmam->_ifmam_spare1 = 0;
2533 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2545 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2547 union sockaddr_union sa_dst, sa_mask;
2550 w->dst = (struct sockaddr *)&sa_dst;
2551 w->mask = (struct sockaddr *)&sa_mask;
2553 init_sockaddrs_family(family, w->dst, w->mask);
2555 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2559 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2561 struct epoch_tracker et;
2562 int *name = (int *)arg1;
2563 u_int namelen = arg2;
2564 struct rib_head *rnh = NULL; /* silence compiler. */
2565 int i, lim, error = EINVAL;
2577 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2579 fib = req->td->td_proc->p_fibnum;
2580 else if (namelen == 4)
2581 fib = (name[3] == RT_ALL_FIBS) ?
2582 req->td->td_proc->p_fibnum : name[3];
2584 return ((namelen < 3) ? EISDIR : ENOTDIR);
2585 if (fib < 0 || fib >= rt_numfibs)
2587 } else if (namelen != 3)
2588 return ((namelen < 3) ? EISDIR : ENOTDIR);
2592 bzero(&w, sizeof(w));
2597 error = sysctl_wire_old_buffer(req, 0);
2602 * Allocate reply buffer in advance.
2603 * All rtsock messages has maximum length of u_short.
2605 w.w_tmemsize = 65536;
2606 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2608 NET_EPOCH_ENTER(et);
2612 if (af == 0) { /* dump all tables */
2615 } else /* dump only one table */
2619 * take care of llinfo entries, the caller must
2622 if (w.w_op == NET_RT_FLAGS &&
2623 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2625 error = lltable_sysctl_dumparp(af, w.w_req);
2631 * take care of routing entries
2633 for (error = 0; error == 0 && i <= lim; i++) {
2634 rnh = rt_tables_get_rnh(fib, i);
2636 rtable_sysctl_dump(fib, i, &w);
2638 error = EAFNOSUPPORT;
2643 /* Allow dumping one specific af/fib at a time */
2649 if (fib < 0 || fib > rt_numfibs) {
2653 rnh = rt_tables_get_rnh(fib, af);
2655 error = EAFNOSUPPORT;
2658 if (w.w_op == NET_RT_NHOP)
2659 error = nhops_dump_sysctl(rnh, w.w_req);
2662 error = nhgrp_dump_sysctl(rnh, w.w_req);
2668 case NET_RT_IFLISTL:
2669 error = sysctl_iflist(af, &w);
2672 case NET_RT_IFMALIST:
2673 error = sysctl_ifmalist(af, &w);
2678 free(w.w_tmem, M_TEMP);
2682 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2683 sysctl_rtsock, "Return route tables and interface/address lists");
2686 * Definitions of protocols supported in the ROUTE domain.
2689 static struct domain routedomain; /* or at least forward */
2691 static struct protosw routesw[] = {
2693 .pr_type = SOCK_RAW,
2694 .pr_domain = &routedomain,
2695 .pr_flags = PR_ATOMIC|PR_ADDR,
2696 .pr_output = route_output,
2697 .pr_ctlinput = raw_ctlinput,
2698 .pr_init = raw_init,
2699 .pr_usrreqs = &route_usrreqs
2703 static struct domain routedomain = {
2704 .dom_family = PF_ROUTE,
2705 .dom_name = "route",
2706 .dom_protosw = routesw,
2707 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2710 VNET_DOMAIN_SET(route);