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_mpath.h"
36 #include "opt_inet6.h"
38 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
43 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/rmlock.h>
49 #include <sys/rwlock.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_llatbl.h>
60 #include <net/if_types.h>
61 #include <net/netisr.h>
62 #include <net/raw_cb.h>
63 #include <net/route.h>
64 #include <net/route_var.h>
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip_carp.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/scope6_var.h>
75 #ifdef COMPAT_FREEBSD32
76 #include <sys/mount.h>
77 #include <compat/freebsd32/freebsd32.h>
87 struct if_data ifm_data;
99 uint16_t ifm_data_off;
100 uint32_t _ifm_spare2;
101 struct if_data ifm_data;
104 struct ifa_msghdrl32 {
105 uint16_t ifam_msglen;
106 uint8_t ifam_version;
111 uint16_t _ifam_spare1;
113 uint16_t ifam_data_off;
115 struct if_data ifam_data;
118 #define SA_SIZE32(sa) \
119 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
121 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
123 #endif /* COMPAT_FREEBSD32 */
125 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
127 /* NB: these are not modified */
128 static struct sockaddr route_src = { 2, PF_ROUTE, };
129 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
131 /* These are external hooks for CARP. */
132 int (*carp_get_vhid_p)(struct ifaddr *);
135 * Used by rtsock/raw_input callback code to decide whether to filter the update
136 * notification to a socket bound to a particular FIB.
138 #define RTS_FILTER_FIB M_PROTO8
141 int ip_count; /* attached w/ AF_INET */
142 int ip6_count; /* attached w/ AF_INET6 */
143 int any_count; /* total attached */
145 VNET_DEFINE_STATIC(route_cb_t, route_cb);
146 #define V_route_cb VNET(route_cb)
148 struct mtx rtsock_mtx;
149 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
151 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
152 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
153 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
155 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
161 struct sysctl_req *w_req;
164 static void rts_input(struct mbuf *m);
165 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
166 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
167 struct walkarg *w, int *plen);
168 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
169 struct rt_addrinfo *rtinfo);
170 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
171 static int sysctl_iflist(int af, struct walkarg *w);
172 static int sysctl_ifmalist(int af, struct walkarg *w);
173 static int route_output(struct mbuf *m, struct socket *so, ...);
174 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
175 static void rt_dispatch(struct mbuf *, sa_family_t);
176 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
177 struct sockaddr *smask, struct sockaddr_storage *dmask);
179 static struct netisr_handler rtsock_nh = {
181 .nh_handler = rts_input,
182 .nh_proto = NETISR_ROUTE,
183 .nh_policy = NETISR_POLICY_SOURCE,
187 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
191 netisr_getqlimit(&rtsock_nh, &qlimit);
192 error = sysctl_handle_int(oidp, &qlimit, 0, req);
193 if (error || !req->newptr)
197 return (netisr_setqlimit(&rtsock_nh, qlimit));
199 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
200 0, 0, sysctl_route_netisr_maxqlen, "I",
201 "maximum routing socket dispatch queue length");
208 if (IS_DEFAULT_VNET(curvnet)) {
209 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
210 rtsock_nh.nh_qlimit = tmp;
211 netisr_register(&rtsock_nh);
215 netisr_register_vnet(&rtsock_nh);
218 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
223 vnet_rts_uninit(void)
226 netisr_unregister_vnet(&rtsock_nh);
228 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
233 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
238 KASSERT(m != NULL, ("%s: m is NULL", __func__));
239 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
240 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
242 /* No filtering requested. */
243 if ((m->m_flags & RTS_FILTER_FIB) == 0)
246 /* Check if it is a rts and the fib matches the one of the socket. */
247 fibnum = M_GETFIB(m);
248 if (proto->sp_family != PF_ROUTE ||
249 rp->rcb_socket == NULL ||
250 rp->rcb_socket->so_fibnum == fibnum)
253 /* Filtering requested and no match, the socket shall be skipped. */
258 rts_input(struct mbuf *m)
260 struct sockproto route_proto;
261 unsigned short *family;
264 route_proto.sp_family = PF_ROUTE;
265 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
267 family = (unsigned short *)(tag + 1);
268 route_proto.sp_protocol = *family;
269 m_tag_delete(m, tag);
271 route_proto.sp_protocol = 0;
273 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
277 * It really doesn't make any sense at all for this code to share much
278 * with raw_usrreq.c, since its functionality is so restricted. XXX
281 rts_abort(struct socket *so)
284 raw_usrreqs.pru_abort(so);
288 rts_close(struct socket *so)
291 raw_usrreqs.pru_close(so);
294 /* pru_accept is EOPNOTSUPP */
297 rts_attach(struct socket *so, int proto, struct thread *td)
302 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
305 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
307 so->so_pcb = (caddr_t)rp;
308 so->so_fibnum = td->td_proc->p_fibnum;
309 error = raw_attach(so, proto);
317 switch(rp->rcb_proto.sp_protocol) {
319 V_route_cb.ip_count++;
322 V_route_cb.ip6_count++;
325 V_route_cb.any_count++;
328 so->so_options |= SO_USELOOPBACK;
333 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
336 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
340 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
343 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
346 /* pru_connect2 is EOPNOTSUPP */
347 /* pru_control is EOPNOTSUPP */
350 rts_detach(struct socket *so)
352 struct rawcb *rp = sotorawcb(so);
354 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
357 switch(rp->rcb_proto.sp_protocol) {
359 V_route_cb.ip_count--;
362 V_route_cb.ip6_count--;
365 V_route_cb.any_count--;
367 raw_usrreqs.pru_detach(so);
371 rts_disconnect(struct socket *so)
374 return (raw_usrreqs.pru_disconnect(so));
377 /* pru_listen is EOPNOTSUPP */
380 rts_peeraddr(struct socket *so, struct sockaddr **nam)
383 return (raw_usrreqs.pru_peeraddr(so, nam));
386 /* pru_rcvd is EOPNOTSUPP */
387 /* pru_rcvoob is EOPNOTSUPP */
390 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
391 struct mbuf *control, struct thread *td)
394 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
397 /* pru_sense is null */
400 rts_shutdown(struct socket *so)
403 return (raw_usrreqs.pru_shutdown(so));
407 rts_sockaddr(struct socket *so, struct sockaddr **nam)
410 return (raw_usrreqs.pru_sockaddr(so, nam));
413 static struct pr_usrreqs route_usrreqs = {
414 .pru_abort = rts_abort,
415 .pru_attach = rts_attach,
416 .pru_bind = rts_bind,
417 .pru_connect = rts_connect,
418 .pru_detach = rts_detach,
419 .pru_disconnect = rts_disconnect,
420 .pru_peeraddr = rts_peeraddr,
421 .pru_send = rts_send,
422 .pru_shutdown = rts_shutdown,
423 .pru_sockaddr = rts_sockaddr,
424 .pru_close = rts_close,
427 #ifndef _SOCKADDR_UNION_DEFINED
428 #define _SOCKADDR_UNION_DEFINED
430 * The union of all possible address formats we handle.
432 union sockaddr_union {
434 struct sockaddr_in sin;
435 struct sockaddr_in6 sin6;
437 #endif /* _SOCKADDR_UNION_DEFINED */
440 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
441 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
444 /* First, see if the returned address is part of the jail. */
445 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
446 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
450 switch (info->rti_info[RTAX_DST]->sa_family) {
460 * Try to find an address on the given outgoing interface
461 * that belongs to the jail.
464 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
467 if (sa->sa_family != AF_INET)
469 ia = ((struct sockaddr_in *)sa)->sin_addr;
470 if (prison_check_ip4(cred, &ia) == 0) {
475 IF_ADDR_RUNLOCK(ifp);
478 * As a last resort return the 'default' jail address.
480 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
482 if (prison_get_ip4(cred, &ia) != 0)
485 bzero(&saun->sin, sizeof(struct sockaddr_in));
486 saun->sin.sin_len = sizeof(struct sockaddr_in);
487 saun->sin.sin_family = AF_INET;
488 saun->sin.sin_addr.s_addr = ia.s_addr;
489 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
502 * Try to find an address on the given outgoing interface
503 * that belongs to the jail.
506 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
509 if (sa->sa_family != AF_INET6)
511 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
512 &ia6, sizeof(struct in6_addr));
513 if (prison_check_ip6(cred, &ia6) == 0) {
518 IF_ADDR_RUNLOCK(ifp);
521 * As a last resort return the 'default' jail address.
523 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
525 if (prison_get_ip6(cred, &ia6) != 0)
528 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
529 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
530 saun->sin6.sin6_family = AF_INET6;
531 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
532 if (sa6_recoverscope(&saun->sin6) != 0)
534 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
546 route_output(struct mbuf *m, struct socket *so, ...)
549 struct rt_msghdr *rtm = NULL;
550 struct rtentry *rt = NULL;
551 struct rib_head *rnh;
552 struct rt_addrinfo info;
553 struct sockaddr_storage ss;
555 struct sockaddr_in6 *sin6;
556 int i, rti_need_deembed = 0;
558 int alloc_len = 0, len, error = 0, fibnum;
559 struct ifnet *ifp = NULL;
560 union sockaddr_union saun;
561 sa_family_t saf = AF_UNSPEC;
562 struct rawcb *rp = NULL;
565 fibnum = so->so_fibnum;
567 #define senderr(e) { error = e; goto flush;}
568 if (m == NULL || ((m->m_len < sizeof(long)) &&
569 (m = m_pullup(m, sizeof(long))) == NULL))
571 if ((m->m_flags & M_PKTHDR) == 0)
572 panic("route_output");
573 len = m->m_pkthdr.len;
574 if (len < sizeof(*rtm) ||
575 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
579 * Most of current messages are in range 200-240 bytes,
580 * minimize possible re-allocation on reply using larger size
581 * buffer aligned on 1k boundaty.
583 alloc_len = roundup2(len, 1024);
584 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
587 m_copydata(m, 0, len, (caddr_t)rtm);
588 bzero(&info, sizeof(info));
589 bzero(&w, sizeof(w));
591 if (rtm->rtm_version != RTM_VERSION) {
592 /* Do not touch message since format is unknown */
595 senderr(EPROTONOSUPPORT);
599 * Starting from here, it is possible
600 * to alter original message and insert
601 * caller PID and error value.
604 rtm->rtm_pid = curproc->p_pid;
605 info.rti_addrs = rtm->rtm_addrs;
607 info.rti_mflags = rtm->rtm_inits;
608 info.rti_rmx = &rtm->rtm_rmx;
611 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
612 * link-local address because rtrequest requires addresses with
615 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
618 info.rti_flags = rtm->rtm_flags;
619 if (info.rti_info[RTAX_DST] == NULL ||
620 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
621 (info.rti_info[RTAX_GATEWAY] != NULL &&
622 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
624 saf = info.rti_info[RTAX_DST]->sa_family;
626 * Verify that the caller has the appropriate privilege; RTM_GET
627 * is the only operation the non-superuser is allowed.
629 if (rtm->rtm_type != RTM_GET) {
630 error = priv_check(curthread, PRIV_NET_ROUTE);
636 * The given gateway address may be an interface address.
637 * For example, issuing a "route change" command on a route
638 * entry that was created from a tunnel, and the gateway
639 * address given is the local end point. In this case the
640 * RTF_GATEWAY flag must be cleared or the destination will
641 * not be reachable even though there is no error message.
643 if (info.rti_info[RTAX_GATEWAY] != NULL &&
644 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
645 struct rt_addrinfo ginfo;
646 struct sockaddr *gdst;
648 bzero(&ginfo, sizeof(ginfo));
649 bzero(&ss, sizeof(ss));
650 ss.ss_len = sizeof(ss);
652 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
653 gdst = info.rti_info[RTAX_GATEWAY];
656 * A host route through the loopback interface is
657 * installed for each interface adddress. In pre 8.0
658 * releases the interface address of a PPP link type
659 * is not reachable locally. This behavior is fixed as
660 * part of the new L2/L3 redesign and rewrite work. The
661 * signature of this interface address route is the
662 * AF_LINK sa_family type of the rt_gateway, and the
663 * rt_ifp has the IFF_LOOPBACK flag set.
665 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
666 if (ss.ss_family == AF_LINK &&
667 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
668 info.rti_flags &= ~RTF_GATEWAY;
669 info.rti_flags |= RTF_GWFLAG_COMPAT;
671 rib_free_info(&ginfo);
675 switch (rtm->rtm_type) {
676 struct rtentry *saved_nrt;
680 if (rtm->rtm_type == RTM_ADD) {
681 if (info.rti_info[RTAX_GATEWAY] == NULL)
686 /* support for new ARP code */
687 if (info.rti_info[RTAX_GATEWAY] != NULL &&
688 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
689 (rtm->rtm_flags & RTF_LLDATA) != 0) {
690 error = lla_rt_output(rtm, &info);
693 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
697 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
699 if (error == 0 && saved_nrt != NULL) {
701 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
704 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
705 RT_REMREF(saved_nrt);
706 RT_UNLOCK(saved_nrt);
712 /* support for new ARP code */
713 if (info.rti_info[RTAX_GATEWAY] &&
714 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
715 (rtm->rtm_flags & RTF_LLDATA) != 0) {
716 error = lla_rt_output(rtm, &info);
719 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
723 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
730 /* rt_msg2() will not be used when RTM_DELETE fails. */
731 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
736 rnh = rt_tables_get_rnh(fibnum, saf);
738 senderr(EAFNOSUPPORT);
742 if (info.rti_info[RTAX_NETMASK] == NULL &&
743 rtm->rtm_type == RTM_GET) {
745 * Provide longest prefix match for
746 * address lookup (no mask).
747 * 'route -n get addr'
749 rt = (struct rtentry *) rnh->rnh_matchaddr(
750 info.rti_info[RTAX_DST], &rnh->head);
752 rt = (struct rtentry *) rnh->rnh_lookup(
753 info.rti_info[RTAX_DST],
754 info.rti_info[RTAX_NETMASK], &rnh->head);
762 * for RTM_CHANGE/LOCK, if we got multipath routes,
763 * we require users to specify a matching RTAX_GATEWAY.
765 * for RTM_GET, gate is optional even with multipath.
766 * if gate == NULL the first match is returned.
767 * (no need to call rt_mpath_matchgate if gate == NULL)
769 if (rt_mpath_capable(rnh) &&
770 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
771 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
779 * If performing proxied L2 entry insertion, and
780 * the actual PPP host entry is found, perform
781 * another search to retrieve the prefix route of
782 * the local end point of the PPP link.
784 if (rtm->rtm_flags & RTF_ANNOUNCE) {
785 struct sockaddr laddr;
787 if (rt->rt_ifp != NULL &&
788 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
792 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
795 rt_maskedcopy(ifa->ifa_addr,
800 rt_maskedcopy(rt->rt_ifa->ifa_addr,
802 rt->rt_ifa->ifa_netmask);
804 * refactor rt and no lock operation necessary
806 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
819 if ((rt->rt_flags & RTF_HOST) == 0
820 ? jailed_without_vnet(curthread->td_ucred)
821 : prison_if(curthread->td_ucred,
826 info.rti_info[RTAX_DST] = rt_key(rt);
827 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
828 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
830 info.rti_info[RTAX_GENMASK] = 0;
831 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
834 info.rti_info[RTAX_IFP] =
835 ifp->if_addr->ifa_addr;
836 error = rtm_get_jailed(&info, ifp, rt,
837 &saun, curthread->td_ucred);
842 if (ifp->if_flags & IFF_POINTOPOINT)
843 info.rti_info[RTAX_BRD] =
844 rt->rt_ifa->ifa_dstaddr;
845 rtm->rtm_index = ifp->if_index;
847 info.rti_info[RTAX_IFP] = NULL;
848 info.rti_info[RTAX_IFA] = NULL;
850 } else if ((ifp = rt->rt_ifp) != NULL) {
851 rtm->rtm_index = ifp->if_index;
854 /* Check if we need to realloc storage */
855 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
856 if (len > alloc_len) {
857 struct rt_msghdr *new_rtm;
858 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
859 if (new_rtm == NULL) {
863 bcopy(rtm, new_rtm, rtm->rtm_msglen);
869 w.w_tmem = (caddr_t)rtm;
870 w.w_tmemsize = alloc_len;
871 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
873 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
874 rtm->rtm_flags = RTF_GATEWAY |
875 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
877 rtm->rtm_flags = rt->rt_flags;
878 rt_getmetrics(rt, &rtm->rtm_rmx);
879 rtm->rtm_addrs = info.rti_addrs;
892 * Check to see if we don't want our own messages.
894 if ((so->so_options & SO_USELOOPBACK) == 0) {
895 if (V_route_cb.any_count <= 1) {
901 /* There is another listener, so construct message */
907 if (rti_need_deembed) {
908 /* sin6_scope_id is recovered before sending rtm. */
909 sin6 = (struct sockaddr_in6 *)&ss;
910 for (i = 0; i < RTAX_MAX; i++) {
911 if (info.rti_info[i] == NULL)
913 if (info.rti_info[i]->sa_family != AF_INET6)
915 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
916 if (sa6_recoverscope(sin6) == 0)
917 bcopy(sin6, info.rti_info[i],
923 rtm->rtm_errno = error;
925 rtm->rtm_flags |= RTF_DONE;
927 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
928 if (m->m_pkthdr.len < rtm->rtm_msglen) {
931 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
932 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
938 m->m_flags |= RTS_FILTER_FIB;
941 * XXX insure we don't get a copy by
942 * invalidating our protocol
944 unsigned short family = rp->rcb_proto.sp_family;
945 rp->rcb_proto.sp_family = 0;
947 rp->rcb_proto.sp_family = family;
956 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
959 bzero(out, sizeof(*out));
960 out->rmx_mtu = rt->rt_mtu;
961 out->rmx_weight = rt->rt_weight;
962 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
963 /* Kernel -> userland timebase conversion. */
964 out->rmx_expire = rt->rt_expire ?
965 rt->rt_expire - time_uptime + time_second : 0;
969 * Extract the addresses of the passed sockaddrs.
970 * Do a little sanity checking so as to avoid bad memory references.
971 * This data is derived straight from userland.
974 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
979 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
980 if ((rtinfo->rti_addrs & (1 << i)) == 0)
982 sa = (struct sockaddr *)cp;
986 if (cp + sa->sa_len > cplim)
989 * there are no more.. quit now
990 * If there are more bits, they are in error.
991 * I've seen this. route(1) can evidently generate these.
992 * This causes kernel to core dump.
993 * for compatibility, If we see this, point to a safe address.
995 if (sa->sa_len == 0) {
996 rtinfo->rti_info[i] = &sa_zero;
997 return (0); /* should be EINVAL but for compat */
1001 if (sa->sa_family == AF_INET6)
1002 sa6_embedscope((struct sockaddr_in6 *)sa,
1005 rtinfo->rti_info[i] = sa;
1012 * Fill in @dmask with valid netmask leaving original @smask
1013 * intact. Mostly used with radix netmasks.
1015 static struct sockaddr *
1016 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1017 struct sockaddr_storage *dmask)
1019 if (dst == NULL || smask == NULL)
1022 memset(dmask, 0, dst->sa_len);
1023 memcpy(dmask, smask, smask->sa_len);
1024 dmask->ss_len = dst->sa_len;
1025 dmask->ss_family = dst->sa_family;
1027 return ((struct sockaddr *)dmask);
1031 * Writes information related to @rtinfo object to newly-allocated mbuf.
1032 * Assumes MCLBYTES is enough to construct any message.
1033 * Used for OS notifications of vaious events (if/ifa announces,etc)
1035 * Returns allocated mbuf or NULL on failure.
1037 static struct mbuf *
1038 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1040 struct rt_msghdr *rtm;
1043 struct sockaddr *sa;
1045 struct sockaddr_storage ss;
1046 struct sockaddr_in6 *sin6;
1054 len = sizeof(struct ifa_msghdr);
1059 len = sizeof(struct ifma_msghdr);
1063 len = sizeof(struct if_msghdr);
1066 case RTM_IFANNOUNCE:
1068 len = sizeof(struct if_announcemsghdr);
1072 len = sizeof(struct rt_msghdr);
1075 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1076 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1078 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1080 m = m_gethdr(M_NOWAIT, MT_DATA);
1084 m->m_pkthdr.len = m->m_len = len;
1085 rtm = mtod(m, struct rt_msghdr *);
1086 bzero((caddr_t)rtm, len);
1087 for (i = 0; i < RTAX_MAX; i++) {
1088 if ((sa = rtinfo->rti_info[i]) == NULL)
1090 rtinfo->rti_addrs |= (1 << i);
1093 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1094 sin6 = (struct sockaddr_in6 *)&ss;
1095 bcopy(sa, sin6, sizeof(*sin6));
1096 if (sa6_recoverscope(sin6) == 0)
1097 sa = (struct sockaddr *)sin6;
1100 m_copyback(m, len, dlen, (caddr_t)sa);
1103 if (m->m_pkthdr.len != len) {
1107 rtm->rtm_msglen = len;
1108 rtm->rtm_version = RTM_VERSION;
1109 rtm->rtm_type = type;
1114 * Writes information related to @rtinfo object to preallocated buffer.
1115 * Stores needed size in @plen. If @w is NULL, calculates size without
1117 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1119 * Returns 0 on success.
1123 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1126 int len, buflen = 0, dlen;
1128 struct rt_msghdr *rtm = NULL;
1130 struct sockaddr_storage ss;
1131 struct sockaddr_in6 *sin6;
1133 #ifdef COMPAT_FREEBSD32
1134 bool compat32 = false;
1141 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1142 #ifdef COMPAT_FREEBSD32
1143 if (w->w_req->flags & SCTL_MASK32) {
1144 len = sizeof(struct ifa_msghdrl32);
1148 len = sizeof(struct ifa_msghdrl);
1150 len = sizeof(struct ifa_msghdr);
1154 #ifdef COMPAT_FREEBSD32
1155 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1156 if (w->w_op == NET_RT_IFLISTL)
1157 len = sizeof(struct if_msghdrl32);
1159 len = sizeof(struct if_msghdr32);
1164 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1165 len = sizeof(struct if_msghdrl);
1167 len = sizeof(struct if_msghdr);
1171 len = sizeof(struct ifma_msghdr);
1175 len = sizeof(struct rt_msghdr);
1179 rtm = (struct rt_msghdr *)w->w_tmem;
1180 buflen = w->w_tmemsize - len;
1181 cp = (caddr_t)w->w_tmem + len;
1184 rtinfo->rti_addrs = 0;
1185 for (i = 0; i < RTAX_MAX; i++) {
1186 struct sockaddr *sa;
1188 if ((sa = rtinfo->rti_info[i]) == NULL)
1190 rtinfo->rti_addrs |= (1 << i);
1191 #ifdef COMPAT_FREEBSD32
1193 dlen = SA_SIZE32(sa);
1197 if (cp != NULL && buflen >= dlen) {
1199 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1200 sin6 = (struct sockaddr_in6 *)&ss;
1201 bcopy(sa, sin6, sizeof(*sin6));
1202 if (sa6_recoverscope(sin6) == 0)
1203 sa = (struct sockaddr *)sin6;
1206 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1209 } else if (cp != NULL) {
1211 * Buffer too small. Count needed size
1212 * and return with error.
1221 dlen = ALIGN(len) - len;
1233 /* fill header iff buffer is large enough */
1234 rtm->rtm_version = RTM_VERSION;
1235 rtm->rtm_type = type;
1236 rtm->rtm_msglen = len;
1241 if (w != NULL && cp == NULL)
1248 * This routine is called to generate a message from the routing
1249 * socket indicating that a redirect has occurred, a routing lookup
1250 * has failed, or that a protocol has detected timeouts to a particular
1254 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1257 struct rt_msghdr *rtm;
1259 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1261 if (V_route_cb.any_count == 0)
1263 m = rtsock_msg_mbuf(type, rtinfo);
1267 if (fibnum != RT_ALL_FIBS) {
1268 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1269 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1270 M_SETFIB(m, fibnum);
1271 m->m_flags |= RTS_FILTER_FIB;
1274 rtm = mtod(m, struct rt_msghdr *);
1275 rtm->rtm_flags = RTF_DONE | flags;
1276 rtm->rtm_errno = error;
1277 rtm->rtm_addrs = rtinfo->rti_addrs;
1278 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1282 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1285 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1289 * This routine is called to generate a message from the routing
1290 * socket indicating that the status of a network interface has changed.
1293 rt_ifmsg(struct ifnet *ifp)
1295 struct if_msghdr *ifm;
1297 struct rt_addrinfo info;
1299 if (V_route_cb.any_count == 0)
1301 bzero((caddr_t)&info, sizeof(info));
1302 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1305 ifm = mtod(m, struct if_msghdr *);
1306 ifm->ifm_index = ifp->if_index;
1307 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1308 if_data_copy(ifp, &ifm->ifm_data);
1310 rt_dispatch(m, AF_UNSPEC);
1314 * Announce interface address arrival/withdraw.
1315 * Please do not call directly, use rt_addrmsg().
1316 * Assume input data to be valid.
1317 * Returns 0 on success.
1320 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1322 struct rt_addrinfo info;
1323 struct sockaddr *sa;
1326 struct ifa_msghdr *ifam;
1327 struct ifnet *ifp = ifa->ifa_ifp;
1328 struct sockaddr_storage ss;
1330 if (V_route_cb.any_count == 0)
1333 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1335 bzero((caddr_t)&info, sizeof(info));
1336 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1337 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1338 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1339 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1340 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1341 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1343 ifam = mtod(m, struct ifa_msghdr *);
1344 ifam->ifam_index = ifp->if_index;
1345 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1346 ifam->ifam_flags = ifa->ifa_flags;
1347 ifam->ifam_addrs = info.rti_addrs;
1349 if (fibnum != RT_ALL_FIBS) {
1350 M_SETFIB(m, fibnum);
1351 m->m_flags |= RTS_FILTER_FIB;
1354 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1360 * Announce route addition/removal.
1361 * Please do not call directly, use rt_routemsg().
1362 * Note that @rt data MAY be inconsistent/invalid:
1363 * if some userland app sends us "invalid" route message (invalid mask,
1364 * no dst, wrong address families, etc...) we need to pass it back
1365 * to app (and any other rtsock consumers) with rtm_errno field set to
1368 * Returns 0 on success.
1371 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1374 struct rt_addrinfo info;
1375 struct sockaddr *sa;
1377 struct rt_msghdr *rtm;
1378 struct sockaddr_storage ss;
1380 if (V_route_cb.any_count == 0)
1383 bzero((caddr_t)&info, sizeof(info));
1384 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1385 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1386 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1387 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1389 rtm = mtod(m, struct rt_msghdr *);
1390 rtm->rtm_index = ifp->if_index;
1391 rtm->rtm_flags |= rt->rt_flags;
1392 rtm->rtm_errno = error;
1393 rtm->rtm_addrs = info.rti_addrs;
1395 if (fibnum != RT_ALL_FIBS) {
1396 M_SETFIB(m, fibnum);
1397 m->m_flags |= RTS_FILTER_FIB;
1400 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1406 * This is the analogue to the rt_newaddrmsg which performs the same
1407 * function but for multicast group memberhips. This is easier since
1408 * there is no route state to worry about.
1411 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1413 struct rt_addrinfo info;
1414 struct mbuf *m = NULL;
1415 struct ifnet *ifp = ifma->ifma_ifp;
1416 struct ifma_msghdr *ifmam;
1418 if (V_route_cb.any_count == 0)
1421 bzero((caddr_t)&info, sizeof(info));
1422 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1423 if (ifp && ifp->if_addr)
1424 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1426 info.rti_info[RTAX_IFP] = NULL;
1428 * If a link-layer address is present, present it as a ``gateway''
1429 * (similarly to how ARP entries, e.g., are presented).
1431 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1432 m = rtsock_msg_mbuf(cmd, &info);
1435 ifmam = mtod(m, struct ifma_msghdr *);
1436 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1438 ifmam->ifmam_index = ifp->if_index;
1439 ifmam->ifmam_addrs = info.rti_addrs;
1440 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1443 static struct mbuf *
1444 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1445 struct rt_addrinfo *info)
1447 struct if_announcemsghdr *ifan;
1450 if (V_route_cb.any_count == 0)
1452 bzero((caddr_t)info, sizeof(*info));
1453 m = rtsock_msg_mbuf(type, info);
1455 ifan = mtod(m, struct if_announcemsghdr *);
1456 ifan->ifan_index = ifp->if_index;
1457 strlcpy(ifan->ifan_name, ifp->if_xname,
1458 sizeof(ifan->ifan_name));
1459 ifan->ifan_what = what;
1465 * This is called to generate routing socket messages indicating
1466 * IEEE80211 wireless events.
1467 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1470 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1473 struct rt_addrinfo info;
1475 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1478 * Append the ieee80211 data. Try to stick it in the
1479 * mbuf containing the ifannounce msg; otherwise allocate
1480 * a new mbuf and append.
1482 * NB: we assume m is a single mbuf.
1484 if (data_len > M_TRAILINGSPACE(m)) {
1485 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1490 bcopy(data, mtod(n, void *), data_len);
1491 n->m_len = data_len;
1493 } else if (data_len > 0) {
1494 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1495 m->m_len += data_len;
1497 if (m->m_flags & M_PKTHDR)
1498 m->m_pkthdr.len += data_len;
1499 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1500 rt_dispatch(m, AF_UNSPEC);
1505 * This is called to generate routing socket messages indicating
1506 * network interface arrival and departure.
1509 rt_ifannouncemsg(struct ifnet *ifp, int what)
1512 struct rt_addrinfo info;
1514 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1516 rt_dispatch(m, AF_UNSPEC);
1520 rt_dispatch(struct mbuf *m, sa_family_t saf)
1525 * Preserve the family from the sockaddr, if any, in an m_tag for
1526 * use when injecting the mbuf into the routing socket buffer from
1529 if (saf != AF_UNSPEC) {
1530 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1536 *(unsigned short *)(tag + 1) = saf;
1537 m_tag_prepend(m, tag);
1541 m->m_pkthdr.rcvif = V_loif;
1547 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1551 * This is used in dumping the kernel table via sysctl().
1554 sysctl_dumpentry(struct radix_node *rn, void *vw)
1556 struct walkarg *w = vw;
1557 struct rtentry *rt = (struct rtentry *)rn;
1558 int error = 0, size;
1559 struct rt_addrinfo info;
1560 struct sockaddr_storage ss;
1562 IFNET_RLOCK_NOSLEEP_ASSERT();
1564 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1566 if ((rt->rt_flags & RTF_HOST) == 0
1567 ? jailed_without_vnet(w->w_req->td->td_ucred)
1568 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1570 bzero((caddr_t)&info, sizeof(info));
1571 info.rti_info[RTAX_DST] = rt_key(rt);
1572 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1573 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1575 info.rti_info[RTAX_GENMASK] = 0;
1576 if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1577 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1578 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1579 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1580 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1582 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1584 if (w->w_req && w->w_tmem) {
1585 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1587 bzero(&rtm->rtm_index,
1588 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1589 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1590 rtm->rtm_flags = RTF_GATEWAY |
1591 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1593 rtm->rtm_flags = rt->rt_flags;
1594 rt_getmetrics(rt, &rtm->rtm_rmx);
1595 rtm->rtm_index = rt->rt_ifp->if_index;
1596 rtm->rtm_addrs = info.rti_addrs;
1597 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1604 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1605 struct rt_addrinfo *info, struct walkarg *w, int len)
1607 struct if_msghdrl *ifm;
1608 struct if_data *ifd;
1610 ifm = (struct if_msghdrl *)w->w_tmem;
1612 #ifdef COMPAT_FREEBSD32
1613 if (w->w_req->flags & SCTL_MASK32) {
1614 struct if_msghdrl32 *ifm32;
1616 ifm32 = (struct if_msghdrl32 *)ifm;
1617 ifm32->ifm_addrs = info->rti_addrs;
1618 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1619 ifm32->ifm_index = ifp->if_index;
1620 ifm32->_ifm_spare1 = 0;
1621 ifm32->ifm_len = sizeof(*ifm32);
1622 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1623 ifm32->_ifm_spare2 = 0;
1624 ifd = &ifm32->ifm_data;
1628 ifm->ifm_addrs = info->rti_addrs;
1629 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1630 ifm->ifm_index = ifp->if_index;
1631 ifm->_ifm_spare1 = 0;
1632 ifm->ifm_len = sizeof(*ifm);
1633 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1634 ifm->_ifm_spare2 = 0;
1635 ifd = &ifm->ifm_data;
1638 memcpy(ifd, src_ifd, sizeof(*ifd));
1640 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1644 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1645 struct rt_addrinfo *info, struct walkarg *w, int len)
1647 struct if_msghdr *ifm;
1648 struct if_data *ifd;
1650 ifm = (struct if_msghdr *)w->w_tmem;
1652 #ifdef COMPAT_FREEBSD32
1653 if (w->w_req->flags & SCTL_MASK32) {
1654 struct if_msghdr32 *ifm32;
1656 ifm32 = (struct if_msghdr32 *)ifm;
1657 ifm32->ifm_addrs = info->rti_addrs;
1658 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1659 ifm32->ifm_index = ifp->if_index;
1660 ifm32->_ifm_spare1 = 0;
1661 ifd = &ifm32->ifm_data;
1665 ifm->ifm_addrs = info->rti_addrs;
1666 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1667 ifm->ifm_index = ifp->if_index;
1668 ifm->_ifm_spare1 = 0;
1669 ifd = &ifm->ifm_data;
1672 memcpy(ifd, src_ifd, sizeof(*ifd));
1674 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1678 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1679 struct walkarg *w, int len)
1681 struct ifa_msghdrl *ifam;
1682 struct if_data *ifd;
1684 ifam = (struct ifa_msghdrl *)w->w_tmem;
1686 #ifdef COMPAT_FREEBSD32
1687 if (w->w_req->flags & SCTL_MASK32) {
1688 struct ifa_msghdrl32 *ifam32;
1690 ifam32 = (struct ifa_msghdrl32 *)ifam;
1691 ifam32->ifam_addrs = info->rti_addrs;
1692 ifam32->ifam_flags = ifa->ifa_flags;
1693 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1694 ifam32->_ifam_spare1 = 0;
1695 ifam32->ifam_len = sizeof(*ifam32);
1696 ifam32->ifam_data_off =
1697 offsetof(struct ifa_msghdrl32, ifam_data);
1698 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1699 ifd = &ifam32->ifam_data;
1703 ifam->ifam_addrs = info->rti_addrs;
1704 ifam->ifam_flags = ifa->ifa_flags;
1705 ifam->ifam_index = ifa->ifa_ifp->if_index;
1706 ifam->_ifam_spare1 = 0;
1707 ifam->ifam_len = sizeof(*ifam);
1708 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1709 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1710 ifd = &ifam->ifam_data;
1713 bzero(ifd, sizeof(*ifd));
1714 ifd->ifi_datalen = sizeof(struct if_data);
1715 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1716 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1717 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1718 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1720 /* Fixup if_data carp(4) vhid. */
1721 if (carp_get_vhid_p != NULL)
1722 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1724 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1728 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1729 struct walkarg *w, int len)
1731 struct ifa_msghdr *ifam;
1733 ifam = (struct ifa_msghdr *)w->w_tmem;
1734 ifam->ifam_addrs = info->rti_addrs;
1735 ifam->ifam_flags = ifa->ifa_flags;
1736 ifam->ifam_index = ifa->ifa_ifp->if_index;
1737 ifam->_ifam_spare1 = 0;
1738 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1740 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1744 sysctl_iflist(int af, struct walkarg *w)
1749 struct rt_addrinfo info;
1751 struct sockaddr_storage ss;
1752 struct epoch_tracker et;
1754 bzero((caddr_t)&info, sizeof(info));
1755 bzero(&ifd, sizeof(ifd));
1756 NET_EPOCH_ENTER_ET(et);
1757 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1758 if (w->w_arg && w->w_arg != ifp->if_index)
1760 if_data_copy(ifp, &ifd);
1762 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1763 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1766 info.rti_info[RTAX_IFP] = NULL;
1767 if (w->w_req && w->w_tmem) {
1768 if (w->w_op == NET_RT_IFLISTL)
1769 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1772 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1777 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1778 if (af && af != ifa->ifa_addr->sa_family)
1780 if (prison_if(w->w_req->td->td_ucred,
1781 ifa->ifa_addr) != 0)
1783 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1784 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1785 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1786 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1787 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1790 if (w->w_req && w->w_tmem) {
1791 if (w->w_op == NET_RT_IFLISTL)
1792 error = sysctl_iflist_ifaml(ifa, &info,
1795 error = sysctl_iflist_ifam(ifa, &info,
1801 info.rti_info[RTAX_IFA] = NULL;
1802 info.rti_info[RTAX_NETMASK] = NULL;
1803 info.rti_info[RTAX_BRD] = NULL;
1806 NET_EPOCH_EXIT_ET(et);
1811 sysctl_ifmalist(int af, struct walkarg *w)
1813 struct rt_addrinfo info;
1815 struct ifmultiaddr *ifma;
1820 bzero((caddr_t)&info, sizeof(info));
1822 IFNET_RLOCK_NOSLEEP();
1823 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1824 if (w->w_arg && w->w_arg != ifp->if_index)
1827 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1829 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1830 if (af && af != ifma->ifma_addr->sa_family)
1832 if (prison_if(w->w_req->td->td_ucred,
1833 ifma->ifma_addr) != 0)
1835 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1836 info.rti_info[RTAX_GATEWAY] =
1837 (ifma->ifma_addr->sa_family != AF_LINK) ?
1838 ifma->ifma_lladdr : NULL;
1839 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1842 if (w->w_req && w->w_tmem) {
1843 struct ifma_msghdr *ifmam;
1845 ifmam = (struct ifma_msghdr *)w->w_tmem;
1846 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1847 ifmam->ifmam_flags = 0;
1848 ifmam->ifmam_addrs = info.rti_addrs;
1849 ifmam->_ifmam_spare1 = 0;
1850 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1855 IF_ADDR_RUNLOCK(ifp);
1859 IFNET_RUNLOCK_NOSLEEP();
1864 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1867 int *name = (int *)arg1;
1868 u_int namelen = arg2;
1869 struct rib_head *rnh = NULL; /* silence compiler. */
1870 int i, lim, error = EINVAL;
1879 if (name[1] == NET_RT_DUMP) {
1881 fib = req->td->td_proc->p_fibnum;
1882 else if (namelen == 4)
1883 fib = (name[3] == RT_ALL_FIBS) ?
1884 req->td->td_proc->p_fibnum : name[3];
1886 return ((namelen < 3) ? EISDIR : ENOTDIR);
1887 if (fib < 0 || fib >= rt_numfibs)
1889 } else if (namelen != 3)
1890 return ((namelen < 3) ? EISDIR : ENOTDIR);
1894 bzero(&w, sizeof(w));
1899 error = sysctl_wire_old_buffer(req, 0);
1904 * Allocate reply buffer in advance.
1905 * All rtsock messages has maximum length of u_short.
1907 w.w_tmemsize = 65536;
1908 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1914 if (af == 0) { /* dump all tables */
1917 } else /* dump only one table */
1921 * take care of llinfo entries, the caller must
1924 if (w.w_op == NET_RT_FLAGS &&
1925 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1927 error = lltable_sysctl_dumparp(af, w.w_req);
1933 * take care of routing entries
1935 for (error = 0; error == 0 && i <= lim; i++) {
1936 rnh = rt_tables_get_rnh(fib, i);
1939 IFNET_RLOCK_NOSLEEP();
1940 error = rnh->rnh_walktree(&rnh->head,
1941 sysctl_dumpentry, &w);
1942 IFNET_RUNLOCK_NOSLEEP();
1945 error = EAFNOSUPPORT;
1950 case NET_RT_IFLISTL:
1951 error = sysctl_iflist(af, &w);
1954 case NET_RT_IFMALIST:
1955 error = sysctl_ifmalist(af, &w);
1959 free(w.w_tmem, M_TEMP);
1963 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1966 * Definitions of protocols supported in the ROUTE domain.
1969 static struct domain routedomain; /* or at least forward */
1971 static struct protosw routesw[] = {
1973 .pr_type = SOCK_RAW,
1974 .pr_domain = &routedomain,
1975 .pr_flags = PR_ATOMIC|PR_ADDR,
1976 .pr_output = route_output,
1977 .pr_ctlinput = raw_ctlinput,
1978 .pr_init = raw_init,
1979 .pr_usrreqs = &route_usrreqs
1983 static struct domain routedomain = {
1984 .dom_family = PF_ROUTE,
1985 .dom_name = "route",
1986 .dom_protosw = routesw,
1987 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1990 VNET_DOMAIN_SET(route);