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
34 #include "opt_route.h"
36 #include "opt_inet6.h"
38 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/eventhandler.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 #define DEBUG_MOD_NAME rtsock
80 #define DEBUG_MAX_LEVEL LOG_DEBUG
81 #include <net/route/route_debug.h>
82 _DECLARE_DEBUG(LOG_INFO);
84 #ifdef COMPAT_FREEBSD32
85 #include <sys/mount.h>
86 #include <compat/freebsd32/freebsd32.h>
96 struct if_data ifm_data;
106 uint16_t _ifm_spare1;
108 uint16_t ifm_data_off;
109 uint32_t _ifm_spare2;
110 struct if_data ifm_data;
113 struct ifa_msghdrl32 {
114 uint16_t ifam_msglen;
115 uint8_t ifam_version;
120 uint16_t _ifam_spare1;
122 uint16_t ifam_data_off;
124 struct if_data ifam_data;
127 #define SA_SIZE32(sa) \
128 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
130 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
132 #endif /* COMPAT_FREEBSD32 */
134 struct linear_buffer {
135 char *base; /* Base allocated memory pointer */
136 uint32_t offset; /* Currently used offset */
137 uint32_t size; /* Total buffer size */
139 #define SCRATCH_BUFFER_SIZE 1024
141 #define RTS_PID_LOG(_l, _fmt, ...) RT_LOG_##_l(_l, "PID %d: " _fmt, curproc ? curproc->p_pid : 0, ## __VA_ARGS__)
143 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
145 /* NB: these are not modified */
146 static struct sockaddr route_src = { 2, PF_ROUTE, };
147 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
149 /* These are external hooks for CARP. */
150 int (*carp_get_vhid_p)(struct ifaddr *);
153 * Used by rtsock/raw_input callback code to decide whether to filter the update
154 * notification to a socket bound to a particular FIB.
156 #define RTS_FILTER_FIB M_PROTO8
159 int ip_count; /* attached w/ AF_INET */
160 int ip6_count; /* attached w/ AF_INET6 */
161 int any_count; /* total attached */
163 VNET_DEFINE_STATIC(route_cb_t, route_cb);
164 #define V_route_cb VNET(route_cb)
166 struct mtx rtsock_mtx;
167 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
169 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
170 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
171 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
173 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
180 struct sysctl_req *w_req;
181 struct sockaddr *dst;
182 struct sockaddr *mask;
185 static void rts_input(struct mbuf *m);
186 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
187 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
188 struct walkarg *w, int *plen);
189 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
190 struct rt_addrinfo *rtinfo);
191 static int cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb);
192 static int sysctl_dumpentry(struct rtentry *rt, void *vw);
193 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
194 uint32_t weight, struct walkarg *w);
195 static int sysctl_iflist(int af, struct walkarg *w);
196 static int sysctl_ifmalist(int af, struct walkarg *w);
197 static int route_output(struct mbuf *m, struct socket *so, ...);
198 static void rt_getmetrics(const struct rtentry *rt,
199 const struct nhop_object *nh, struct rt_metrics *out);
200 static void rt_dispatch(struct mbuf *, sa_family_t);
201 static void rt_ifannouncemsg(struct ifnet *ifp, int what);
202 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
203 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
204 static int update_rtm_from_rc(struct rt_addrinfo *info,
205 struct rt_msghdr **prtm, int alloc_len,
206 struct rib_cmd_info *rc, struct nhop_object *nh);
207 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
208 struct mbuf *m, sa_family_t saf, u_int fibnum,
210 static void rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc);
211 static void rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask);
213 static struct netisr_handler rtsock_nh = {
215 .nh_handler = rts_input,
216 .nh_proto = NETISR_ROUTE,
217 .nh_policy = NETISR_POLICY_SOURCE,
221 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
225 netisr_getqlimit(&rtsock_nh, &qlimit);
226 error = sysctl_handle_int(oidp, &qlimit, 0, req);
227 if (error || !req->newptr)
231 return (netisr_setqlimit(&rtsock_nh, qlimit));
233 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
234 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
235 0, 0, sysctl_route_netisr_maxqlen, "I",
236 "maximum routing socket dispatch queue length");
243 if (IS_DEFAULT_VNET(curvnet)) {
244 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
245 rtsock_nh.nh_qlimit = tmp;
246 netisr_register(&rtsock_nh);
250 netisr_register_vnet(&rtsock_nh);
253 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
258 vnet_rts_uninit(void)
261 netisr_unregister_vnet(&rtsock_nh);
263 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
268 report_route_event(const struct rib_cmd_info *rc, void *_cbdata)
270 uint32_t fibnum = (uint32_t)(uintptr_t)_cbdata;
271 struct nhop_object *nh;
273 nh = rc->rc_cmd == RTM_DELETE ? rc->rc_nh_old : rc->rc_nh_new;
274 rt_routemsg(rc->rc_cmd, rc->rc_rt, nh, fibnum);
278 rts_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
281 if ((rc->rc_nh_new && NH_IS_NHGRP(rc->rc_nh_new)) ||
282 (rc->rc_nh_old && NH_IS_NHGRP(rc->rc_nh_old))) {
283 rib_decompose_notification(rc, report_route_event,
284 (void *)(uintptr_t)fibnum);
287 report_route_event(rc, (void *)(uintptr_t)fibnum);
289 static struct rtbridge rtsbridge = {
290 .route_f = rts_handle_route_event,
291 .ifmsg_f = rtsock_ifmsg,
293 static struct rtbridge *rtsbridge_orig_p;
296 rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc)
298 netlink_callback_p->route_f(fibnum, rc);
304 rtsbridge_orig_p = rtsock_callback_p;
305 rtsock_callback_p = &rtsbridge;
307 SYSINIT(rtsock_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtsock_init, NULL);
310 rts_handle_ifnet_arrival(void *arg __unused, struct ifnet *ifp)
312 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
314 EVENTHANDLER_DEFINE(ifnet_arrival_event, rts_handle_ifnet_arrival, NULL, 0);
317 rts_handle_ifnet_departure(void *arg __unused, struct ifnet *ifp)
319 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
321 EVENTHANDLER_DEFINE(ifnet_departure_event, rts_handle_ifnet_departure, NULL, 0);
324 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
329 KASSERT(m != NULL, ("%s: m is NULL", __func__));
330 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
331 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
333 /* No filtering requested. */
334 if ((m->m_flags & RTS_FILTER_FIB) == 0)
337 /* Check if it is a rts and the fib matches the one of the socket. */
338 fibnum = M_GETFIB(m);
339 if (proto->sp_family != PF_ROUTE ||
340 rp->rcb_socket == NULL ||
341 rp->rcb_socket->so_fibnum == fibnum)
344 /* Filtering requested and no match, the socket shall be skipped. */
349 rts_input(struct mbuf *m)
351 struct sockproto route_proto;
352 unsigned short *family;
355 route_proto.sp_family = PF_ROUTE;
356 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
358 family = (unsigned short *)(tag + 1);
359 route_proto.sp_protocol = *family;
360 m_tag_delete(m, tag);
362 route_proto.sp_protocol = 0;
364 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
368 * It really doesn't make any sense at all for this code to share much
369 * with raw_usrreq.c, since its functionality is so restricted. XXX
372 rts_abort(struct socket *so)
375 raw_usrreqs.pru_abort(so);
379 rts_close(struct socket *so)
382 raw_usrreqs.pru_close(so);
385 /* pru_accept is EOPNOTSUPP */
388 rts_attach(struct socket *so, int proto, struct thread *td)
393 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
396 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
398 so->so_pcb = (caddr_t)rp;
399 so->so_fibnum = td->td_proc->p_fibnum;
400 error = raw_attach(so, proto);
408 switch(rp->rcb_proto.sp_protocol) {
410 V_route_cb.ip_count++;
413 V_route_cb.ip6_count++;
416 V_route_cb.any_count++;
419 so->so_options |= SO_USELOOPBACK;
424 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
427 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
431 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
434 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
437 /* pru_connect2 is EOPNOTSUPP */
438 /* pru_control is EOPNOTSUPP */
441 rts_detach(struct socket *so)
443 struct rawcb *rp = sotorawcb(so);
445 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
448 switch(rp->rcb_proto.sp_protocol) {
450 V_route_cb.ip_count--;
453 V_route_cb.ip6_count--;
456 V_route_cb.any_count--;
458 raw_usrreqs.pru_detach(so);
462 rts_disconnect(struct socket *so)
465 return (raw_usrreqs.pru_disconnect(so));
468 /* pru_listen is EOPNOTSUPP */
471 rts_peeraddr(struct socket *so, struct sockaddr **nam)
474 return (raw_usrreqs.pru_peeraddr(so, nam));
477 /* pru_rcvd is EOPNOTSUPP */
478 /* pru_rcvoob is EOPNOTSUPP */
481 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
482 struct mbuf *control, struct thread *td)
485 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
488 /* pru_sense is null */
491 rts_shutdown(struct socket *so)
494 return (raw_usrreqs.pru_shutdown(so));
498 rts_sockaddr(struct socket *so, struct sockaddr **nam)
501 return (raw_usrreqs.pru_sockaddr(so, nam));
504 static struct pr_usrreqs route_usrreqs = {
505 .pru_abort = rts_abort,
506 .pru_attach = rts_attach,
507 .pru_bind = rts_bind,
508 .pru_connect = rts_connect,
509 .pru_detach = rts_detach,
510 .pru_disconnect = rts_disconnect,
511 .pru_peeraddr = rts_peeraddr,
512 .pru_send = rts_send,
513 .pru_shutdown = rts_shutdown,
514 .pru_sockaddr = rts_sockaddr,
515 .pru_close = rts_close,
518 #ifndef _SOCKADDR_UNION_DEFINED
519 #define _SOCKADDR_UNION_DEFINED
521 * The union of all possible address formats we handle.
523 union sockaddr_union {
525 struct sockaddr_in sin;
526 struct sockaddr_in6 sin6;
528 #endif /* _SOCKADDR_UNION_DEFINED */
531 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
532 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
534 #if defined(INET) || defined(INET6)
535 struct epoch_tracker et;
538 /* First, see if the returned address is part of the jail. */
539 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
540 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
544 switch (info->rti_info[RTAX_DST]->sa_family) {
554 * Try to find an address on the given outgoing interface
555 * that belongs to the jail.
558 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
561 if (sa->sa_family != AF_INET)
563 ia = ((struct sockaddr_in *)sa)->sin_addr;
564 if (prison_check_ip4(cred, &ia) == 0) {
572 * As a last resort return the 'default' jail address.
574 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
576 if (prison_get_ip4(cred, &ia) != 0)
579 bzero(&saun->sin, sizeof(struct sockaddr_in));
580 saun->sin.sin_len = sizeof(struct sockaddr_in);
581 saun->sin.sin_family = AF_INET;
582 saun->sin.sin_addr.s_addr = ia.s_addr;
583 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
596 * Try to find an address on the given outgoing interface
597 * that belongs to the jail.
600 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
603 if (sa->sa_family != AF_INET6)
605 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
606 &ia6, sizeof(struct in6_addr));
607 if (prison_check_ip6(cred, &ia6) == 0) {
615 * As a last resort return the 'default' jail address.
617 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
619 if (prison_get_ip6(cred, &ia6) != 0)
622 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
623 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
624 saun->sin6.sin6_family = AF_INET6;
625 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
626 if (sa6_recoverscope(&saun->sin6) != 0)
628 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
639 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
644 if (V_loif == NULL) {
645 RTS_PID_LOG(LOG_INFO, "Unable to add blackhole/reject nhop without loopback");
648 info->rti_ifp = V_loif;
650 saf = info->rti_info[RTAX_DST]->sa_family;
652 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
653 if (ifa->ifa_addr->sa_family == saf) {
658 if (info->rti_ifa == NULL) {
659 RTS_PID_LOG(LOG_INFO, "Unable to find ifa for blackhole/reject nhop");
663 bzero(saun, sizeof(union sockaddr_union));
667 saun->sin.sin_family = AF_INET;
668 saun->sin.sin_len = sizeof(struct sockaddr_in);
669 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
674 saun->sin6.sin6_family = AF_INET6;
675 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
676 saun->sin6.sin6_addr = in6addr_loopback;
680 RTS_PID_LOG(LOG_INFO, "unsupported family: %d", saf);
683 info->rti_info[RTAX_GATEWAY] = &saun->sa;
684 info->rti_flags |= RTF_GATEWAY;
690 * Fills in @info based on userland-provided @rtm message.
692 * Returns 0 on success.
695 fill_addrinfo(struct rt_msghdr *rtm, int len, struct linear_buffer *lb, u_int fibnum,
696 struct rt_addrinfo *info)
701 rtm->rtm_pid = curproc->p_pid;
702 info->rti_addrs = rtm->rtm_addrs;
704 info->rti_mflags = rtm->rtm_inits;
705 info->rti_rmx = &rtm->rtm_rmx;
708 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
709 * link-local address because rtrequest requires addresses with
712 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
715 info->rti_flags = rtm->rtm_flags;
716 error = cleanup_xaddrs(info, lb);
719 saf = info->rti_info[RTAX_DST]->sa_family;
721 * Verify that the caller has the appropriate privilege; RTM_GET
722 * is the only operation the non-superuser is allowed.
724 if (rtm->rtm_type != RTM_GET) {
725 error = priv_check(curthread, PRIV_NET_ROUTE);
731 * The given gateway address may be an interface address.
732 * For example, issuing a "route change" command on a route
733 * entry that was created from a tunnel, and the gateway
734 * address given is the local end point. In this case the
735 * RTF_GATEWAY flag must be cleared or the destination will
736 * not be reachable even though there is no error message.
738 if (info->rti_info[RTAX_GATEWAY] != NULL &&
739 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
740 struct rt_addrinfo ginfo;
741 struct sockaddr *gdst;
742 struct sockaddr_storage ss;
744 bzero(&ginfo, sizeof(ginfo));
745 bzero(&ss, sizeof(ss));
746 ss.ss_len = sizeof(ss);
748 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
749 gdst = info->rti_info[RTAX_GATEWAY];
752 * A host route through the loopback interface is
753 * installed for each interface adddress. In pre 8.0
754 * releases the interface address of a PPP link type
755 * is not reachable locally. This behavior is fixed as
756 * part of the new L2/L3 redesign and rewrite work. The
757 * signature of this interface address route is the
758 * AF_LINK sa_family type of the gateway, and the
759 * rt_ifp has the IFF_LOOPBACK flag set.
761 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
762 if (ss.ss_family == AF_LINK &&
763 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
764 info->rti_flags &= ~RTF_GATEWAY;
765 info->rti_flags |= RTF_GWFLAG_COMPAT;
767 rib_free_info(&ginfo);
774 static struct nhop_object *
775 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
777 if (!NH_IS_NHGRP(nh))
780 const struct weightened_nhop *wn;
782 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
785 for (int i = 0; i < num_nhops; i++) {
786 if (match_nhop_gw(wn[i].nh, gw))
794 * Handles RTM_GET message from routing socket, returning matching rt.
797 * 0 on success, with locked and referenced matching rt in @rt_nrt
801 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
802 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
805 struct rib_head *rnh;
806 struct nhop_object *nh;
809 saf = info->rti_info[RTAX_DST]->sa_family;
811 rnh = rt_tables_get_rnh(fibnum, saf);
813 return (EAFNOSUPPORT);
818 * By (implicit) convention host route (one without netmask)
819 * means longest-prefix-match request and the route with netmask
820 * means exact-match lookup.
821 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
822 * prefixes, use original data to check for the netmask presence.
824 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
826 * Provide longest prefix match for
827 * address lookup (no mask).
828 * 'route -n get addr'
830 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
831 info->rti_info[RTAX_DST], &rnh->head);
833 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
834 info->rti_info[RTAX_DST],
835 info->rti_info[RTAX_NETMASK], &rnh->head);
837 if (rc->rc_rt == NULL) {
842 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
848 * If performing proxied L2 entry insertion, and
849 * the actual PPP host entry is found, perform
850 * another search to retrieve the prefix route of
851 * the local end point of the PPP link.
852 * TODO: move this logic to userland.
854 if (rtm->rtm_flags & RTF_ANNOUNCE) {
855 struct sockaddr_storage laddr;
857 if (nh->nh_ifp != NULL &&
858 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
861 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
864 rt_maskedcopy(ifa->ifa_addr,
865 (struct sockaddr *)&laddr,
868 rt_maskedcopy(nh->nh_ifa->ifa_addr,
869 (struct sockaddr *)&laddr,
870 nh->nh_ifa->ifa_netmask);
872 * refactor rt and no lock operation necessary
874 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(
875 (struct sockaddr *)&laddr, &rnh->head);
876 if (rc->rc_rt == NULL) {
880 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
887 rc->rc_nh_weight = rc->rc_rt->rt_weight;
894 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
897 if (family == AF_INET) {
898 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
899 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
901 bzero(dst4, sizeof(struct sockaddr_in));
902 bzero(mask4, sizeof(struct sockaddr_in));
904 dst4->sin_family = AF_INET;
905 dst4->sin_len = sizeof(struct sockaddr_in);
906 mask4->sin_family = AF_INET;
907 mask4->sin_len = sizeof(struct sockaddr_in);
911 if (family == AF_INET6) {
912 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
913 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
915 bzero(dst6, sizeof(struct sockaddr_in6));
916 bzero(mask6, sizeof(struct sockaddr_in6));
918 dst6->sin6_family = AF_INET6;
919 dst6->sin6_len = sizeof(struct sockaddr_in6);
920 mask6->sin6_family = AF_INET6;
921 mask6->sin6_len = sizeof(struct sockaddr_in6);
927 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
928 struct sockaddr *mask)
931 if (dst->sa_family == AF_INET) {
932 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
933 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
934 uint32_t scopeid = 0;
935 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
941 if (dst->sa_family == AF_INET6) {
942 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
943 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
944 uint32_t scopeid = 0;
945 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
946 &mask6->sin6_addr, &scopeid);
947 dst6->sin6_scope_id = scopeid;
954 update_rtm_from_info(struct rt_addrinfo *info, struct rt_msghdr **prtm,
957 struct rt_msghdr *rtm, *orig_rtm = NULL;
962 /* Check if we need to realloc storage */
963 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
964 if (len > alloc_len) {
965 struct rt_msghdr *tmp_rtm;
967 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
970 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
976 * Delay freeing original rtm as info contains
977 * data referencing it.
981 w.w_tmem = (caddr_t)rtm;
982 w.w_tmemsize = alloc_len;
983 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
984 rtm->rtm_addrs = info->rti_addrs;
986 if (orig_rtm != NULL)
987 free(orig_rtm, M_TEMP);
994 * Update sockaddrs, flags, etc in @prtm based on @rc data.
995 * rtm can be reallocated.
997 * Returns 0 on success, along with pointer to (potentially reallocated)
1002 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
1003 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
1005 union sockaddr_union saun;
1006 struct rt_msghdr *rtm;
1011 union sockaddr_union sa_dst, sa_mask;
1012 int family = info->rti_info[RTAX_DST]->sa_family;
1013 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
1014 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
1016 info->rti_info[RTAX_DST] = &sa_dst.sa;
1017 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
1018 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1019 info->rti_info[RTAX_GENMASK] = 0;
1021 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
1023 info->rti_info[RTAX_IFP] =
1024 ifp->if_addr->ifa_addr;
1025 error = rtm_get_jailed(info, ifp, nh,
1026 &saun, curthread->td_ucred);
1029 if (ifp->if_flags & IFF_POINTOPOINT)
1030 info->rti_info[RTAX_BRD] =
1031 nh->nh_ifa->ifa_dstaddr;
1032 rtm->rtm_index = ifp->if_index;
1034 info->rti_info[RTAX_IFP] = NULL;
1035 info->rti_info[RTAX_IFA] = NULL;
1037 } else if (ifp != NULL)
1038 rtm->rtm_index = ifp->if_index;
1040 if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0)
1044 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
1045 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
1046 rtm->rtm_flags = RTF_GATEWAY |
1047 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
1048 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
1049 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
1056 save_del_notification(const struct rib_cmd_info *rc, void *_cbdata)
1058 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1060 if (rc->rc_cmd == RTM_DELETE)
1065 save_add_notification(const struct rib_cmd_info *rc, void *_cbdata)
1067 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1069 if (rc->rc_cmd == RTM_ADD)
1074 static struct sockaddr *
1075 alloc_sockaddr_aligned(struct linear_buffer *lb, int len)
1077 len = roundup2(len, sizeof(uint64_t));
1078 if (lb->offset + len > lb->size)
1080 struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset);
1087 route_output(struct mbuf *m, struct socket *so, ...)
1089 struct rt_msghdr *rtm = NULL;
1090 struct rtentry *rt = NULL;
1091 struct rt_addrinfo info;
1092 struct epoch_tracker et;
1094 struct sockaddr_storage ss;
1095 struct sockaddr_in6 *sin6;
1096 int i, rti_need_deembed = 0;
1098 int alloc_len = 0, len, error = 0, fibnum;
1099 sa_family_t saf = AF_UNSPEC;
1100 struct rib_cmd_info rc;
1101 struct nhop_object *nh;
1103 fibnum = so->so_fibnum;
1104 #define senderr(e) { error = e; goto flush;}
1105 if (m == NULL || ((m->m_len < sizeof(long)) &&
1106 (m = m_pullup(m, sizeof(long))) == NULL))
1108 if ((m->m_flags & M_PKTHDR) == 0)
1109 panic("route_output");
1110 NET_EPOCH_ENTER(et);
1111 len = m->m_pkthdr.len;
1112 if (len < sizeof(*rtm) ||
1113 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1117 * Most of current messages are in range 200-240 bytes,
1118 * minimize possible re-allocation on reply using larger size
1119 * buffer aligned on 1k boundaty.
1121 alloc_len = roundup2(len, 1024);
1122 int total_len = alloc_len + SCRATCH_BUFFER_SIZE;
1123 if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL)
1126 m_copydata(m, 0, len, (caddr_t)rtm);
1127 bzero(&info, sizeof(info));
1129 struct linear_buffer lb = {
1130 .base = (char *)rtm + alloc_len,
1131 .size = SCRATCH_BUFFER_SIZE,
1134 if (rtm->rtm_version != RTM_VERSION) {
1135 /* Do not touch message since format is unknown */
1138 senderr(EPROTONOSUPPORT);
1142 * Starting from here, it is possible
1143 * to alter original message and insert
1144 * caller PID and error value.
1147 if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) {
1150 /* fill_addringo() embeds scope into IPv6 addresses */
1152 rti_need_deembed = 1;
1155 saf = info.rti_info[RTAX_DST]->sa_family;
1157 /* support for new ARP code */
1158 if (rtm->rtm_flags & RTF_LLDATA) {
1159 error = lla_rt_output(rtm, &info);
1163 union sockaddr_union gw_saun;
1164 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1165 if (blackhole_flags != 0) {
1166 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1167 error = fill_blackholeinfo(&info, &gw_saun);
1169 RTS_PID_LOG(LOG_DEBUG, "both BLACKHOLE and REJECT flags specifiied");
1176 switch (rtm->rtm_type) {
1179 if (rtm->rtm_type == RTM_ADD) {
1180 if (info.rti_info[RTAX_GATEWAY] == NULL) {
1181 RTS_PID_LOG(LOG_DEBUG, "RTM_ADD w/o gateway");
1185 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1187 rtsock_notify_event(fibnum, &rc);
1189 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1190 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1191 struct rib_cmd_info rc_simple = {};
1192 rib_decompose_notification(&rc,
1193 save_add_notification, (void *)&rc_simple);
1197 /* nh MAY be empty if RTM_CHANGE request is no-op */
1200 rtm->rtm_index = nh->nh_ifp->if_index;
1201 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1207 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1209 rtsock_notify_event(fibnum, &rc);
1211 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1212 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1213 struct rib_cmd_info rc_simple = {};
1214 rib_decompose_notification(&rc,
1215 save_del_notification, (void *)&rc_simple);
1224 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1229 if (!rt_is_exportable(rc.rc_rt, curthread->td_ucred))
1234 senderr(EOPNOTSUPP);
1237 if (error == 0 && nh != NULL) {
1238 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1240 * Note that some sockaddr pointers may have changed to
1241 * point to memory outsize @rtm. Some may be pointing
1242 * to the on-stack variables.
1243 * Given that, any pointer in @info CANNOT BE USED.
1247 * scopeid deembedding has been performed while
1248 * writing updated rtm in rtsock_msg_buffer().
1249 * With that in mind, skip deembedding procedure below.
1252 rti_need_deembed = 0;
1262 if (rti_need_deembed) {
1263 /* sin6_scope_id is recovered before sending rtm. */
1264 sin6 = (struct sockaddr_in6 *)&ss;
1265 for (i = 0; i < RTAX_MAX; i++) {
1266 if (info.rti_info[i] == NULL)
1268 if (info.rti_info[i]->sa_family != AF_INET6)
1270 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1271 if (sa6_recoverscope(sin6) == 0)
1272 bcopy(sin6, info.rti_info[i],
1275 if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) {
1282 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1288 * Sends the prepared reply message in @rtm to all rtsock clients.
1289 * Frees @m and @rtm.
1293 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1294 sa_family_t saf, u_int fibnum, int rtm_errno)
1296 struct rawcb *rp = NULL;
1299 * Check to see if we don't want our own messages.
1301 if ((so->so_options & SO_USELOOPBACK) == 0) {
1302 if (V_route_cb.any_count <= 1) {
1308 /* There is another listener, so construct message */
1314 rtm->rtm_errno = rtm_errno;
1316 rtm->rtm_flags |= RTF_DONE;
1318 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1319 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1322 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1323 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1328 M_SETFIB(m, fibnum);
1329 m->m_flags |= RTS_FILTER_FIB;
1332 * XXX insure we don't get a copy by
1333 * invalidating our protocol
1335 unsigned short family = rp->rcb_proto.sp_family;
1336 rp->rcb_proto.sp_family = 0;
1337 rt_dispatch(m, saf);
1338 rp->rcb_proto.sp_family = family;
1340 rt_dispatch(m, saf);
1345 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1346 struct rt_metrics *out)
1349 bzero(out, sizeof(*out));
1350 out->rmx_mtu = nh->nh_mtu;
1351 out->rmx_weight = rt->rt_weight;
1352 out->rmx_nhidx = nhop_get_idx(nh);
1353 /* Kernel -> userland timebase conversion. */
1354 out->rmx_expire = nhop_get_expire(nh) ?
1355 nhop_get_expire(nh) - time_uptime + time_second : 0;
1359 * Extract the addresses of the passed sockaddrs.
1360 * Do a little sanity checking so as to avoid bad memory references.
1361 * This data is derived straight from userland.
1364 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1366 struct sockaddr *sa;
1369 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1370 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1372 sa = (struct sockaddr *)cp;
1376 if (cp + sa->sa_len > cplim) {
1377 RTS_PID_LOG(LOG_DEBUG, "sa_len too big for sa type %d", i);
1381 * there are no more.. quit now
1382 * If there are more bits, they are in error.
1383 * I've seen this. route(1) can evidently generate these.
1384 * This causes kernel to core dump.
1385 * for compatibility, If we see this, point to a safe address.
1387 if (sa->sa_len == 0) {
1388 rtinfo->rti_info[i] = &sa_zero;
1389 return (0); /* should be EINVAL but for compat */
1393 if (sa->sa_family == AF_INET6)
1394 sa6_embedscope((struct sockaddr_in6 *)sa,
1397 rtinfo->rti_info[i] = sa;
1405 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1408 const struct sockaddr_in nsin = {
1409 .sin_family = AF_INET,
1410 .sin_len = sizeof(struct sockaddr_in),
1419 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1423 const struct sockaddr_in6 nsin6 = {
1424 .sin6_family = AF_INET6,
1425 .sin6_len = sizeof(struct sockaddr_in6),
1426 .sin6_addr = *addr6,
1427 .sin6_scope_id = scopeid,
1434 * Checks if gateway is suitable for lltable operations.
1435 * Lltable code requires AF_LINK gateway with ifindex
1436 * and mac address specified.
1437 * Returns 0 on success.
1440 cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1442 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1444 if (sdl->sdl_family != AF_LINK)
1447 if (sdl->sdl_index == 0) {
1448 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gateway w/o ifindex");
1452 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len) {
1453 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gw: sdl_nlen/sdl_alen too large");
1461 cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb)
1463 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1464 struct sockaddr *sa;
1466 if (info->rti_flags & RTF_LLDATA)
1467 return (cleanup_xaddrs_lladdr(info));
1469 switch (gw->sa_family) {
1473 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1475 /* Ensure reads do not go beyoud SA boundary */
1476 if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) {
1477 RTS_PID_LOG(LOG_DEBUG, "gateway sin_len too small: %d",
1481 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in));
1484 fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr);
1485 info->rti_info[RTAX_GATEWAY] = sa;
1492 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1493 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1494 RTS_PID_LOG(LOG_DEBUG, "gateway sin6_len too small: %d",
1498 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1504 struct sockaddr_dl *gw_sdl;
1506 size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data);
1507 gw_sdl = (struct sockaddr_dl *)gw;
1508 if (gw_sdl->sdl_len < sdl_min_len) {
1509 RTS_PID_LOG(LOG_DEBUG, "gateway sdl_len too small: %d",
1513 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short));
1517 const struct sockaddr_dl_short sdl = {
1518 .sdl_family = AF_LINK,
1519 .sdl_len = sizeof(struct sockaddr_dl_short),
1520 .sdl_index = gw_sdl->sdl_index,
1522 *((struct sockaddr_dl_short *)sa) = sdl;
1523 info->rti_info[RTAX_GATEWAY] = sa;
1532 remove_netmask(struct rt_addrinfo *info)
1534 info->rti_info[RTAX_NETMASK] = NULL;
1535 info->rti_flags |= RTF_HOST;
1536 info->rti_addrs &= ~RTA_NETMASK;
1541 cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb)
1543 struct sockaddr_in *dst_sa, *mask_sa;
1544 const int sa_len = sizeof(struct sockaddr_in);
1545 struct in_addr dst, mask;
1547 /* Check & fixup dst/netmask combination first */
1548 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1549 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1551 /* Ensure reads do not go beyound the buffer size */
1552 if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero)) {
1553 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin_len too small: %d",
1558 if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1560 * Some older routing software encode mask length into the
1561 * sin_len, thus resulting in "truncated" sockaddr.
1563 int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr);
1566 if (len > sizeof(struct in_addr))
1567 len = sizeof(struct in_addr);
1568 memcpy(&mask, &mask_sa->sin_addr, len);
1570 RTS_PID_LOG(LOG_DEBUG, "prefix mask sin_len too small: %d",
1575 mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST;
1577 dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr));
1579 /* Construct new "clean" dst/mask sockaddresses */
1580 if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1582 fill_sockaddr_inet(dst_sa, dst);
1583 info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa;
1585 if (mask.s_addr != INADDR_BROADCAST) {
1586 if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1588 fill_sockaddr_inet(mask_sa, mask);
1589 info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa;
1590 info->rti_flags &= ~RTF_HOST;
1592 remove_netmask(info);
1595 if (info->rti_info[RTAX_GATEWAY] != NULL)
1596 return (cleanup_xaddrs_gateway(info, lb));
1604 cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb)
1606 struct sockaddr *sa;
1607 struct sockaddr_in6 *dst_sa, *mask_sa;
1608 struct in6_addr mask, *dst;
1609 const int sa_len = sizeof(struct sockaddr_in6);
1611 /* Check & fixup dst/netmask combination first */
1612 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1613 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1615 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1616 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin6_len too small: %d",
1621 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1623 * Some older routing software encode mask length into the
1624 * sin6_len, thus resulting in "truncated" sockaddr.
1626 int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1628 bzero(&mask, sizeof(mask));
1629 if (len > sizeof(struct in6_addr))
1630 len = sizeof(struct in6_addr);
1631 memcpy(&mask, &mask_sa->sin6_addr, len);
1633 RTS_PID_LOG(LOG_DEBUG, "rtsock: prefix mask sin6_len too small: %d",
1638 mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1640 dst = &dst_sa->sin6_addr;
1641 IN6_MASK_ADDR(dst, &mask);
1643 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1645 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0);
1646 info->rti_info[RTAX_DST] = sa;
1648 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) {
1649 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1651 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0);
1652 info->rti_info[RTAX_NETMASK] = sa;
1653 info->rti_flags &= ~RTF_HOST;
1655 remove_netmask(info);
1658 if (info->rti_info[RTAX_GATEWAY] != NULL)
1659 return (cleanup_xaddrs_gateway(info, lb));
1666 cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb)
1668 int error = EAFNOSUPPORT;
1670 if (info->rti_info[RTAX_DST] == NULL) {
1671 RTS_PID_LOG(LOG_DEBUG, "prefix dst is not set");
1675 if (info->rti_flags & RTF_LLDATA) {
1677 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1678 * prefix along with the actual address in RTA_DST.
1679 * Remove netmask to avoid unnecessary address masking.
1681 remove_netmask(info);
1684 switch (info->rti_info[RTAX_DST]->sa_family) {
1687 error = cleanup_xaddrs_inet(info, lb);
1692 error = cleanup_xaddrs_inet6(info, lb);
1701 * Fill in @dmask with valid netmask leaving original @smask
1702 * intact. Mostly used with radix netmasks.
1705 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1706 struct sockaddr_storage *dmask)
1708 if (dst == NULL || smask == NULL)
1711 memset(dmask, 0, dst->sa_len);
1712 memcpy(dmask, smask, smask->sa_len);
1713 dmask->ss_len = dst->sa_len;
1714 dmask->ss_family = dst->sa_family;
1716 return ((struct sockaddr *)dmask);
1720 * Writes information related to @rtinfo object to newly-allocated mbuf.
1721 * Assumes MCLBYTES is enough to construct any message.
1722 * Used for OS notifications of vaious events (if/ifa announces,etc)
1724 * Returns allocated mbuf or NULL on failure.
1726 static struct mbuf *
1727 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1729 struct sockaddr_storage ss;
1730 struct rt_msghdr *rtm;
1733 struct sockaddr *sa;
1735 struct sockaddr_in6 *sin6;
1742 len = sizeof(struct ifa_msghdr);
1747 len = sizeof(struct ifma_msghdr);
1751 len = sizeof(struct if_msghdr);
1754 case RTM_IFANNOUNCE:
1756 len = sizeof(struct if_announcemsghdr);
1760 len = sizeof(struct rt_msghdr);
1763 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1764 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1766 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1768 m = m_gethdr(M_NOWAIT, MT_DATA);
1772 m->m_pkthdr.len = m->m_len = len;
1773 rtm = mtod(m, struct rt_msghdr *);
1774 bzero((caddr_t)rtm, len);
1775 for (i = 0; i < RTAX_MAX; i++) {
1776 if ((sa = rtinfo->rti_info[i]) == NULL)
1778 rtinfo->rti_addrs |= (1 << i);
1781 KASSERT(dlen <= sizeof(ss),
1782 ("%s: sockaddr size overflow", __func__));
1783 bzero(&ss, sizeof(ss));
1784 bcopy(sa, &ss, sa->sa_len);
1785 sa = (struct sockaddr *)&ss;
1787 if (sa->sa_family == AF_INET6) {
1788 sin6 = (struct sockaddr_in6 *)sa;
1789 (void)sa6_recoverscope(sin6);
1792 m_copyback(m, len, dlen, (caddr_t)sa);
1795 if (m->m_pkthdr.len != len) {
1799 rtm->rtm_msglen = len;
1800 rtm->rtm_version = RTM_VERSION;
1801 rtm->rtm_type = type;
1806 * Writes information related to @rtinfo object to preallocated buffer.
1807 * Stores needed size in @plen. If @w is NULL, calculates size without
1809 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1811 * Returns 0 on success.
1815 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1817 struct sockaddr_storage ss;
1818 int len, buflen = 0, dlen, i;
1820 struct rt_msghdr *rtm = NULL;
1822 struct sockaddr_in6 *sin6;
1824 #ifdef COMPAT_FREEBSD32
1825 bool compat32 = false;
1831 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1832 #ifdef COMPAT_FREEBSD32
1833 if (w->w_req->flags & SCTL_MASK32) {
1834 len = sizeof(struct ifa_msghdrl32);
1838 len = sizeof(struct ifa_msghdrl);
1840 len = sizeof(struct ifa_msghdr);
1844 #ifdef COMPAT_FREEBSD32
1845 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1846 if (w->w_op == NET_RT_IFLISTL)
1847 len = sizeof(struct if_msghdrl32);
1849 len = sizeof(struct if_msghdr32);
1854 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1855 len = sizeof(struct if_msghdrl);
1857 len = sizeof(struct if_msghdr);
1861 len = sizeof(struct ifma_msghdr);
1865 len = sizeof(struct rt_msghdr);
1869 rtm = (struct rt_msghdr *)w->w_tmem;
1870 buflen = w->w_tmemsize - len;
1871 cp = (caddr_t)w->w_tmem + len;
1874 rtinfo->rti_addrs = 0;
1875 for (i = 0; i < RTAX_MAX; i++) {
1876 struct sockaddr *sa;
1878 if ((sa = rtinfo->rti_info[i]) == NULL)
1880 rtinfo->rti_addrs |= (1 << i);
1881 #ifdef COMPAT_FREEBSD32
1883 dlen = SA_SIZE32(sa);
1887 if (cp != NULL && buflen >= dlen) {
1888 KASSERT(dlen <= sizeof(ss),
1889 ("%s: sockaddr size overflow", __func__));
1890 bzero(&ss, sizeof(ss));
1891 bcopy(sa, &ss, sa->sa_len);
1892 sa = (struct sockaddr *)&ss;
1894 if (sa->sa_family == AF_INET6) {
1895 sin6 = (struct sockaddr_in6 *)sa;
1896 (void)sa6_recoverscope(sin6);
1899 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1902 } else if (cp != NULL) {
1904 * Buffer too small. Count needed size
1905 * and return with error.
1914 dlen = ALIGN(len) - len;
1926 /* fill header iff buffer is large enough */
1927 rtm->rtm_version = RTM_VERSION;
1928 rtm->rtm_type = type;
1929 rtm->rtm_msglen = len;
1934 if (w != NULL && cp == NULL)
1941 * This routine is called to generate a message from the routing
1942 * socket indicating that a redirect has occurred, a routing lookup
1943 * has failed, or that a protocol has detected timeouts to a particular
1947 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1950 struct rt_msghdr *rtm;
1952 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1954 if (V_route_cb.any_count == 0)
1956 m = rtsock_msg_mbuf(type, rtinfo);
1960 if (fibnum != RT_ALL_FIBS) {
1961 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1962 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1963 M_SETFIB(m, fibnum);
1964 m->m_flags |= RTS_FILTER_FIB;
1967 rtm = mtod(m, struct rt_msghdr *);
1968 rtm->rtm_flags = RTF_DONE | flags;
1969 rtm->rtm_errno = error;
1970 rtm->rtm_addrs = rtinfo->rti_addrs;
1971 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1975 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1978 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1982 * This routine is called to generate a message from the routing
1983 * socket indicating that the status of a network interface has changed.
1986 rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask __unused)
1988 struct if_msghdr *ifm;
1990 struct rt_addrinfo info;
1992 if (V_route_cb.any_count == 0)
1994 bzero((caddr_t)&info, sizeof(info));
1995 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1998 ifm = mtod(m, struct if_msghdr *);
1999 ifm->ifm_index = ifp->if_index;
2000 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2001 if_data_copy(ifp, &ifm->ifm_data);
2003 rt_dispatch(m, AF_UNSPEC);
2007 * Announce interface address arrival/withdraw.
2008 * Please do not call directly, use rt_addrmsg().
2009 * Assume input data to be valid.
2010 * Returns 0 on success.
2013 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
2015 struct rt_addrinfo info;
2016 struct sockaddr *sa;
2019 struct ifa_msghdr *ifam;
2020 struct ifnet *ifp = ifa->ifa_ifp;
2021 struct sockaddr_storage ss;
2023 if (V_route_cb.any_count == 0)
2026 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
2028 bzero((caddr_t)&info, sizeof(info));
2029 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
2030 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
2031 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2032 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
2033 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2034 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
2036 ifam = mtod(m, struct ifa_msghdr *);
2037 ifam->ifam_index = ifp->if_index;
2038 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2039 ifam->ifam_flags = ifa->ifa_flags;
2040 ifam->ifam_addrs = info.rti_addrs;
2042 if (fibnum != RT_ALL_FIBS) {
2043 M_SETFIB(m, fibnum);
2044 m->m_flags |= RTS_FILTER_FIB;
2047 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2053 * Announce route addition/removal to rtsock based on @rt data.
2054 * Callers are advives to use rt_routemsg() instead of using this
2055 * function directly.
2056 * Assume @rt data is consistent.
2058 * Returns 0 on success.
2061 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
2064 union sockaddr_union dst, mask;
2065 struct rt_addrinfo info;
2067 if (V_route_cb.any_count == 0)
2070 int family = rt_get_family(rt);
2071 init_sockaddrs_family(family, &dst.sa, &mask.sa);
2072 export_rtaddrs(rt, &dst.sa, &mask.sa);
2074 bzero((caddr_t)&info, sizeof(info));
2075 info.rti_info[RTAX_DST] = &dst.sa;
2076 info.rti_info[RTAX_NETMASK] = &mask.sa;
2077 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2078 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
2079 info.rti_ifp = nh->nh_ifp;
2081 return (rtsock_routemsg_info(cmd, &info, fibnum));
2085 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2087 struct rt_msghdr *rtm;
2088 struct sockaddr *sa;
2091 if (V_route_cb.any_count == 0)
2094 if (info->rti_flags & RTF_HOST)
2095 info->rti_info[RTAX_NETMASK] = NULL;
2097 m = rtsock_msg_mbuf(cmd, info);
2101 if (fibnum != RT_ALL_FIBS) {
2102 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
2103 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
2104 M_SETFIB(m, fibnum);
2105 m->m_flags |= RTS_FILTER_FIB;
2108 rtm = mtod(m, struct rt_msghdr *);
2109 rtm->rtm_addrs = info->rti_addrs;
2110 if (info->rti_ifp != NULL)
2111 rtm->rtm_index = info->rti_ifp->if_index;
2112 /* Add RTF_DONE to indicate command 'completion' required by API */
2113 info->rti_flags |= RTF_DONE;
2114 /* Reported routes has to be up */
2115 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
2116 info->rti_flags |= RTF_UP;
2117 rtm->rtm_flags = info->rti_flags;
2119 sa = info->rti_info[RTAX_DST];
2120 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2126 * This is the analogue to the rt_newaddrmsg which performs the same
2127 * function but for multicast group memberhips. This is easier since
2128 * there is no route state to worry about.
2131 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
2133 struct rt_addrinfo info;
2134 struct mbuf *m = NULL;
2135 struct ifnet *ifp = ifma->ifma_ifp;
2136 struct ifma_msghdr *ifmam;
2138 if (V_route_cb.any_count == 0)
2141 bzero((caddr_t)&info, sizeof(info));
2142 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2143 if (ifp && ifp->if_addr)
2144 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
2146 info.rti_info[RTAX_IFP] = NULL;
2148 * If a link-layer address is present, present it as a ``gateway''
2149 * (similarly to how ARP entries, e.g., are presented).
2151 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
2152 m = rtsock_msg_mbuf(cmd, &info);
2155 ifmam = mtod(m, struct ifma_msghdr *);
2156 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
2158 ifmam->ifmam_index = ifp->if_index;
2159 ifmam->ifmam_addrs = info.rti_addrs;
2160 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
2163 static struct mbuf *
2164 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
2165 struct rt_addrinfo *info)
2167 struct if_announcemsghdr *ifan;
2170 if (V_route_cb.any_count == 0)
2172 bzero((caddr_t)info, sizeof(*info));
2173 m = rtsock_msg_mbuf(type, info);
2175 ifan = mtod(m, struct if_announcemsghdr *);
2176 ifan->ifan_index = ifp->if_index;
2177 strlcpy(ifan->ifan_name, ifp->if_xname,
2178 sizeof(ifan->ifan_name));
2179 ifan->ifan_what = what;
2185 * This is called to generate routing socket messages indicating
2186 * IEEE80211 wireless events.
2187 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2190 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2193 struct rt_addrinfo info;
2195 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2198 * Append the ieee80211 data. Try to stick it in the
2199 * mbuf containing the ifannounce msg; otherwise allocate
2200 * a new mbuf and append.
2202 * NB: we assume m is a single mbuf.
2204 if (data_len > M_TRAILINGSPACE(m)) {
2205 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2210 bcopy(data, mtod(n, void *), data_len);
2211 n->m_len = data_len;
2213 } else if (data_len > 0) {
2214 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2215 m->m_len += data_len;
2217 if (m->m_flags & M_PKTHDR)
2218 m->m_pkthdr.len += data_len;
2219 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2220 rt_dispatch(m, AF_UNSPEC);
2225 * This is called to generate routing socket messages indicating
2226 * network interface arrival and departure.
2229 rt_ifannouncemsg(struct ifnet *ifp, int what)
2232 struct rt_addrinfo info;
2234 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2236 rt_dispatch(m, AF_UNSPEC);
2240 rt_dispatch(struct mbuf *m, sa_family_t saf)
2245 * Preserve the family from the sockaddr, if any, in an m_tag for
2246 * use when injecting the mbuf into the routing socket buffer from
2249 if (saf != AF_UNSPEC) {
2250 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
2256 *(unsigned short *)(tag + 1) = saf;
2257 m_tag_prepend(m, tag);
2261 m->m_pkthdr.rcvif = V_loif;
2267 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
2271 * This is used in dumping the kernel table via sysctl().
2274 sysctl_dumpentry(struct rtentry *rt, void *vw)
2276 struct walkarg *w = vw;
2277 struct nhop_object *nh;
2281 if (!rt_is_exportable(rt, w->w_req->td->td_ucred))
2284 export_rtaddrs(rt, w->dst, w->mask);
2285 nh = rt_get_raw_nhop(rt);
2287 if (NH_IS_NHGRP(nh)) {
2288 const struct weightened_nhop *wn;
2291 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2292 for (int i = 0; i < num_nhops; i++) {
2293 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2299 sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2306 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2309 struct rt_addrinfo info;
2310 int error = 0, size;
2313 rtflags = nhop_get_rtflags(nh);
2315 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2318 bzero((caddr_t)&info, sizeof(info));
2319 info.rti_info[RTAX_DST] = w->dst;
2320 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2321 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2322 info.rti_info[RTAX_GENMASK] = 0;
2323 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2324 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2325 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2326 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2327 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2329 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2331 if (w->w_req && w->w_tmem) {
2332 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2334 bzero(&rtm->rtm_index,
2335 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2338 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2339 * and RTF_UP (if entry is linked, which is always true here).
2340 * Given that, use nhop rtflags & add RTF_UP.
2342 rtm->rtm_flags = rtflags | RTF_UP;
2343 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2344 rtm->rtm_flags = RTF_GATEWAY |
2345 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2346 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2347 rtm->rtm_rmx.rmx_weight = weight;
2348 rtm->rtm_index = nh->nh_ifp->if_index;
2349 rtm->rtm_addrs = info.rti_addrs;
2350 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2357 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2358 struct rt_addrinfo *info, struct walkarg *w, int len)
2360 struct if_msghdrl *ifm;
2361 struct if_data *ifd;
2363 ifm = (struct if_msghdrl *)w->w_tmem;
2365 #ifdef COMPAT_FREEBSD32
2366 if (w->w_req->flags & SCTL_MASK32) {
2367 struct if_msghdrl32 *ifm32;
2369 ifm32 = (struct if_msghdrl32 *)ifm;
2370 ifm32->ifm_addrs = info->rti_addrs;
2371 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2372 ifm32->ifm_index = ifp->if_index;
2373 ifm32->_ifm_spare1 = 0;
2374 ifm32->ifm_len = sizeof(*ifm32);
2375 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2376 ifm32->_ifm_spare2 = 0;
2377 ifd = &ifm32->ifm_data;
2381 ifm->ifm_addrs = info->rti_addrs;
2382 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2383 ifm->ifm_index = ifp->if_index;
2384 ifm->_ifm_spare1 = 0;
2385 ifm->ifm_len = sizeof(*ifm);
2386 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2387 ifm->_ifm_spare2 = 0;
2388 ifd = &ifm->ifm_data;
2391 memcpy(ifd, src_ifd, sizeof(*ifd));
2393 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2397 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2398 struct rt_addrinfo *info, struct walkarg *w, int len)
2400 struct if_msghdr *ifm;
2401 struct if_data *ifd;
2403 ifm = (struct if_msghdr *)w->w_tmem;
2405 #ifdef COMPAT_FREEBSD32
2406 if (w->w_req->flags & SCTL_MASK32) {
2407 struct if_msghdr32 *ifm32;
2409 ifm32 = (struct if_msghdr32 *)ifm;
2410 ifm32->ifm_addrs = info->rti_addrs;
2411 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2412 ifm32->ifm_index = ifp->if_index;
2413 ifm32->_ifm_spare1 = 0;
2414 ifd = &ifm32->ifm_data;
2418 ifm->ifm_addrs = info->rti_addrs;
2419 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2420 ifm->ifm_index = ifp->if_index;
2421 ifm->_ifm_spare1 = 0;
2422 ifd = &ifm->ifm_data;
2425 memcpy(ifd, src_ifd, sizeof(*ifd));
2427 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2431 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2432 struct walkarg *w, int len)
2434 struct ifa_msghdrl *ifam;
2435 struct if_data *ifd;
2437 ifam = (struct ifa_msghdrl *)w->w_tmem;
2439 #ifdef COMPAT_FREEBSD32
2440 if (w->w_req->flags & SCTL_MASK32) {
2441 struct ifa_msghdrl32 *ifam32;
2443 ifam32 = (struct ifa_msghdrl32 *)ifam;
2444 ifam32->ifam_addrs = info->rti_addrs;
2445 ifam32->ifam_flags = ifa->ifa_flags;
2446 ifam32->ifam_index = ifa->ifa_ifp->if_index;
2447 ifam32->_ifam_spare1 = 0;
2448 ifam32->ifam_len = sizeof(*ifam32);
2449 ifam32->ifam_data_off =
2450 offsetof(struct ifa_msghdrl32, ifam_data);
2451 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2452 ifd = &ifam32->ifam_data;
2456 ifam->ifam_addrs = info->rti_addrs;
2457 ifam->ifam_flags = ifa->ifa_flags;
2458 ifam->ifam_index = ifa->ifa_ifp->if_index;
2459 ifam->_ifam_spare1 = 0;
2460 ifam->ifam_len = sizeof(*ifam);
2461 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2462 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2463 ifd = &ifam->ifam_data;
2466 bzero(ifd, sizeof(*ifd));
2467 ifd->ifi_datalen = sizeof(struct if_data);
2468 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2469 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2470 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2471 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2473 /* Fixup if_data carp(4) vhid. */
2474 if (carp_get_vhid_p != NULL)
2475 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2477 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2481 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2482 struct walkarg *w, int len)
2484 struct ifa_msghdr *ifam;
2486 ifam = (struct ifa_msghdr *)w->w_tmem;
2487 ifam->ifam_addrs = info->rti_addrs;
2488 ifam->ifam_flags = ifa->ifa_flags;
2489 ifam->ifam_index = ifa->ifa_ifp->if_index;
2490 ifam->_ifam_spare1 = 0;
2491 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2493 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2497 sysctl_iflist(int af, struct walkarg *w)
2502 struct rt_addrinfo info;
2504 struct sockaddr_storage ss;
2506 bzero((caddr_t)&info, sizeof(info));
2507 bzero(&ifd, sizeof(ifd));
2508 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2509 if (w->w_arg && w->w_arg != ifp->if_index)
2511 if_data_copy(ifp, &ifd);
2513 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2514 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2517 info.rti_info[RTAX_IFP] = NULL;
2518 if (w->w_req && w->w_tmem) {
2519 if (w->w_op == NET_RT_IFLISTL)
2520 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2523 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2528 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2529 if (af && af != ifa->ifa_addr->sa_family)
2531 if (prison_if(w->w_req->td->td_ucred,
2532 ifa->ifa_addr) != 0)
2534 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2535 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2536 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2537 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2538 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2541 if (w->w_req && w->w_tmem) {
2542 if (w->w_op == NET_RT_IFLISTL)
2543 error = sysctl_iflist_ifaml(ifa, &info,
2546 error = sysctl_iflist_ifam(ifa, &info,
2552 info.rti_info[RTAX_IFA] = NULL;
2553 info.rti_info[RTAX_NETMASK] = NULL;
2554 info.rti_info[RTAX_BRD] = NULL;
2561 sysctl_ifmalist(int af, struct walkarg *w)
2563 struct rt_addrinfo info;
2565 struct ifmultiaddr *ifma;
2572 bzero((caddr_t)&info, sizeof(info));
2574 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2575 if (w->w_arg && w->w_arg != ifp->if_index)
2578 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2579 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2580 if (af && af != ifma->ifma_addr->sa_family)
2582 if (prison_if(w->w_req->td->td_ucred,
2583 ifma->ifma_addr) != 0)
2585 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2586 info.rti_info[RTAX_GATEWAY] =
2587 (ifma->ifma_addr->sa_family != AF_LINK) ?
2588 ifma->ifma_lladdr : NULL;
2589 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2592 if (w->w_req && w->w_tmem) {
2593 struct ifma_msghdr *ifmam;
2595 ifmam = (struct ifma_msghdr *)w->w_tmem;
2596 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2597 ifmam->ifmam_flags = 0;
2598 ifmam->ifmam_addrs = info.rti_addrs;
2599 ifmam->_ifmam_spare1 = 0;
2600 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2612 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2614 union sockaddr_union sa_dst, sa_mask;
2617 w->dst = (struct sockaddr *)&sa_dst;
2618 w->mask = (struct sockaddr *)&sa_mask;
2620 init_sockaddrs_family(family, w->dst, w->mask);
2622 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2626 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2628 struct epoch_tracker et;
2629 int *name = (int *)arg1;
2630 u_int namelen = arg2;
2631 struct rib_head *rnh = NULL; /* silence compiler. */
2632 int i, lim, error = EINVAL;
2644 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2646 fib = req->td->td_proc->p_fibnum;
2647 else if (namelen == 4)
2648 fib = (name[3] == RT_ALL_FIBS) ?
2649 req->td->td_proc->p_fibnum : name[3];
2651 return ((namelen < 3) ? EISDIR : ENOTDIR);
2652 if (fib < 0 || fib >= rt_numfibs)
2654 } else if (namelen != 3)
2655 return ((namelen < 3) ? EISDIR : ENOTDIR);
2659 bzero(&w, sizeof(w));
2664 error = sysctl_wire_old_buffer(req, 0);
2669 * Allocate reply buffer in advance.
2670 * All rtsock messages has maximum length of u_short.
2672 w.w_tmemsize = 65536;
2673 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2675 NET_EPOCH_ENTER(et);
2679 if (af == 0) { /* dump all tables */
2682 } else /* dump only one table */
2686 * take care of llinfo entries, the caller must
2689 if (w.w_op == NET_RT_FLAGS &&
2690 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2692 error = lltable_sysctl_dumparp(af, w.w_req);
2698 * take care of routing entries
2700 for (error = 0; error == 0 && i <= lim; i++) {
2701 rnh = rt_tables_get_rnh(fib, i);
2703 rtable_sysctl_dump(fib, i, &w);
2705 error = EAFNOSUPPORT;
2710 /* Allow dumping one specific af/fib at a time */
2716 if (fib < 0 || fib > rt_numfibs) {
2720 rnh = rt_tables_get_rnh(fib, af);
2722 error = EAFNOSUPPORT;
2725 if (w.w_op == NET_RT_NHOP)
2726 error = nhops_dump_sysctl(rnh, w.w_req);
2729 error = nhgrp_dump_sysctl(rnh, w.w_req);
2735 case NET_RT_IFLISTL:
2736 error = sysctl_iflist(af, &w);
2739 case NET_RT_IFMALIST:
2740 error = sysctl_ifmalist(af, &w);
2745 free(w.w_tmem, M_TEMP);
2749 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2750 sysctl_rtsock, "Return route tables and interface/address lists");
2753 * Definitions of protocols supported in the ROUTE domain.
2756 static struct domain routedomain; /* or at least forward */
2758 static struct protosw routesw[] = {
2760 .pr_type = SOCK_RAW,
2761 .pr_domain = &routedomain,
2762 .pr_flags = PR_ATOMIC|PR_ADDR,
2763 .pr_output = route_output,
2764 .pr_ctlinput = raw_ctlinput,
2765 .pr_init = raw_init,
2766 .pr_usrreqs = &route_usrreqs
2770 static struct domain routedomain = {
2771 .dom_family = PF_ROUTE,
2772 .dom_name = "route",
2773 .dom_protosw = routesw,
2774 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2777 VNET_DOMAIN_SET(route);