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)
443 #if defined(INET) || defined(INET6)
444 struct epoch_tracker et;
447 /* First, see if the returned address is part of the jail. */
448 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
449 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
453 switch (info->rti_info[RTAX_DST]->sa_family) {
463 * Try to find an address on the given outgoing interface
464 * that belongs to the jail.
467 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
470 if (sa->sa_family != AF_INET)
472 ia = ((struct sockaddr_in *)sa)->sin_addr;
473 if (prison_check_ip4(cred, &ia) == 0) {
481 * As a last resort return the 'default' jail address.
483 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
485 if (prison_get_ip4(cred, &ia) != 0)
488 bzero(&saun->sin, sizeof(struct sockaddr_in));
489 saun->sin.sin_len = sizeof(struct sockaddr_in);
490 saun->sin.sin_family = AF_INET;
491 saun->sin.sin_addr.s_addr = ia.s_addr;
492 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
505 * Try to find an address on the given outgoing interface
506 * that belongs to the jail.
509 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
512 if (sa->sa_family != AF_INET6)
514 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
515 &ia6, sizeof(struct in6_addr));
516 if (prison_check_ip6(cred, &ia6) == 0) {
524 * As a last resort return the 'default' jail address.
526 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
528 if (prison_get_ip6(cred, &ia6) != 0)
531 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
532 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
533 saun->sin6.sin6_family = AF_INET6;
534 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
535 if (sa6_recoverscope(&saun->sin6) != 0)
537 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
549 route_output(struct mbuf *m, struct socket *so, ...)
552 struct rt_msghdr *rtm = NULL;
553 struct rtentry *rt = NULL;
554 struct rib_head *rnh;
555 struct rt_addrinfo info;
556 struct sockaddr_storage ss;
558 struct sockaddr_in6 *sin6;
559 int i, rti_need_deembed = 0;
561 int alloc_len = 0, len, error = 0, fibnum;
562 struct ifnet *ifp = NULL;
563 union sockaddr_union saun;
564 sa_family_t saf = AF_UNSPEC;
565 struct rawcb *rp = NULL;
568 fibnum = so->so_fibnum;
570 #define senderr(e) { error = e; goto flush;}
571 if (m == NULL || ((m->m_len < sizeof(long)) &&
572 (m = m_pullup(m, sizeof(long))) == NULL))
574 if ((m->m_flags & M_PKTHDR) == 0)
575 panic("route_output");
576 len = m->m_pkthdr.len;
577 if (len < sizeof(*rtm) ||
578 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
582 * Most of current messages are in range 200-240 bytes,
583 * minimize possible re-allocation on reply using larger size
584 * buffer aligned on 1k boundaty.
586 alloc_len = roundup2(len, 1024);
587 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
590 m_copydata(m, 0, len, (caddr_t)rtm);
591 bzero(&info, sizeof(info));
592 bzero(&w, sizeof(w));
594 if (rtm->rtm_version != RTM_VERSION) {
595 /* Do not touch message since format is unknown */
598 senderr(EPROTONOSUPPORT);
602 * Starting from here, it is possible
603 * to alter original message and insert
604 * caller PID and error value.
607 rtm->rtm_pid = curproc->p_pid;
608 info.rti_addrs = rtm->rtm_addrs;
610 info.rti_mflags = rtm->rtm_inits;
611 info.rti_rmx = &rtm->rtm_rmx;
614 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
615 * link-local address because rtrequest requires addresses with
618 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
621 if (rtm->rtm_flags & RTF_RNH_LOCKED)
623 info.rti_flags = rtm->rtm_flags;
624 if (info.rti_info[RTAX_DST] == NULL ||
625 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
626 (info.rti_info[RTAX_GATEWAY] != NULL &&
627 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
629 saf = info.rti_info[RTAX_DST]->sa_family;
631 * Verify that the caller has the appropriate privilege; RTM_GET
632 * is the only operation the non-superuser is allowed.
634 if (rtm->rtm_type != RTM_GET) {
635 error = priv_check(curthread, PRIV_NET_ROUTE);
641 * The given gateway address may be an interface address.
642 * For example, issuing a "route change" command on a route
643 * entry that was created from a tunnel, and the gateway
644 * address given is the local end point. In this case the
645 * RTF_GATEWAY flag must be cleared or the destination will
646 * not be reachable even though there is no error message.
648 if (info.rti_info[RTAX_GATEWAY] != NULL &&
649 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
650 struct rt_addrinfo ginfo;
651 struct sockaddr *gdst;
653 bzero(&ginfo, sizeof(ginfo));
654 bzero(&ss, sizeof(ss));
655 ss.ss_len = sizeof(ss);
657 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
658 gdst = info.rti_info[RTAX_GATEWAY];
661 * A host route through the loopback interface is
662 * installed for each interface adddress. In pre 8.0
663 * releases the interface address of a PPP link type
664 * is not reachable locally. This behavior is fixed as
665 * part of the new L2/L3 redesign and rewrite work. The
666 * signature of this interface address route is the
667 * AF_LINK sa_family type of the rt_gateway, and the
668 * rt_ifp has the IFF_LOOPBACK flag set.
670 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
671 if (ss.ss_family == AF_LINK &&
672 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
673 info.rti_flags &= ~RTF_GATEWAY;
674 info.rti_flags |= RTF_GWFLAG_COMPAT;
676 rib_free_info(&ginfo);
680 switch (rtm->rtm_type) {
681 struct rtentry *saved_nrt;
685 if (rtm->rtm_type == RTM_ADD) {
686 if (info.rti_info[RTAX_GATEWAY] == NULL)
691 /* support for new ARP code */
692 if (info.rti_info[RTAX_GATEWAY] != NULL &&
693 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
694 (rtm->rtm_flags & RTF_LLDATA) != 0) {
695 error = lla_rt_output(rtm, &info);
698 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
702 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
704 if (error == 0 && saved_nrt != NULL) {
706 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
709 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
710 RT_REMREF(saved_nrt);
711 RT_UNLOCK(saved_nrt);
717 /* support for new ARP code */
718 if (info.rti_info[RTAX_GATEWAY] &&
719 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
720 (rtm->rtm_flags & RTF_LLDATA) != 0) {
721 error = lla_rt_output(rtm, &info);
724 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
728 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
735 /* rt_msg2() will not be used when RTM_DELETE fails. */
736 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
741 rnh = rt_tables_get_rnh(fibnum, saf);
743 senderr(EAFNOSUPPORT);
747 if (info.rti_info[RTAX_NETMASK] == NULL &&
748 rtm->rtm_type == RTM_GET) {
750 * Provide longest prefix match for
751 * address lookup (no mask).
752 * 'route -n get addr'
754 rt = (struct rtentry *) rnh->rnh_matchaddr(
755 info.rti_info[RTAX_DST], &rnh->head);
757 rt = (struct rtentry *) rnh->rnh_lookup(
758 info.rti_info[RTAX_DST],
759 info.rti_info[RTAX_NETMASK], &rnh->head);
767 * for RTM_CHANGE/LOCK, if we got multipath routes,
768 * we require users to specify a matching RTAX_GATEWAY.
770 * for RTM_GET, gate is optional even with multipath.
771 * if gate == NULL the first match is returned.
772 * (no need to call rt_mpath_matchgate if gate == NULL)
774 if (rt_mpath_capable(rnh) &&
775 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
776 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
784 * If performing proxied L2 entry insertion, and
785 * the actual PPP host entry is found, perform
786 * another search to retrieve the prefix route of
787 * the local end point of the PPP link.
789 if (rtm->rtm_flags & RTF_ANNOUNCE) {
790 struct sockaddr laddr;
792 if (rt->rt_ifp != NULL &&
793 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
794 struct epoch_tracker et;
798 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
801 rt_maskedcopy(ifa->ifa_addr,
806 rt_maskedcopy(rt->rt_ifa->ifa_addr,
808 rt->rt_ifa->ifa_netmask);
810 * refactor rt and no lock operation necessary
812 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
825 if ((rt->rt_flags & RTF_HOST) == 0
826 ? jailed_without_vnet(curthread->td_ucred)
827 : prison_if(curthread->td_ucred,
832 info.rti_info[RTAX_DST] = rt_key(rt);
833 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
834 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
836 info.rti_info[RTAX_GENMASK] = 0;
837 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
840 info.rti_info[RTAX_IFP] =
841 ifp->if_addr->ifa_addr;
842 error = rtm_get_jailed(&info, ifp, rt,
843 &saun, curthread->td_ucred);
848 if (ifp->if_flags & IFF_POINTOPOINT)
849 info.rti_info[RTAX_BRD] =
850 rt->rt_ifa->ifa_dstaddr;
851 rtm->rtm_index = ifp->if_index;
853 info.rti_info[RTAX_IFP] = NULL;
854 info.rti_info[RTAX_IFA] = NULL;
856 } else if ((ifp = rt->rt_ifp) != NULL) {
857 rtm->rtm_index = ifp->if_index;
860 /* Check if we need to realloc storage */
861 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
862 if (len > alloc_len) {
863 struct rt_msghdr *new_rtm;
864 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
865 if (new_rtm == NULL) {
869 bcopy(rtm, new_rtm, rtm->rtm_msglen);
875 w.w_tmem = (caddr_t)rtm;
876 w.w_tmemsize = alloc_len;
877 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
879 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
880 rtm->rtm_flags = RTF_GATEWAY |
881 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
883 rtm->rtm_flags = rt->rt_flags;
884 rt_getmetrics(rt, &rtm->rtm_rmx);
885 rtm->rtm_addrs = info.rti_addrs;
898 * Check to see if we don't want our own messages.
900 if ((so->so_options & SO_USELOOPBACK) == 0) {
901 if (V_route_cb.any_count <= 1) {
907 /* There is another listener, so construct message */
913 if (rti_need_deembed) {
914 /* sin6_scope_id is recovered before sending rtm. */
915 sin6 = (struct sockaddr_in6 *)&ss;
916 for (i = 0; i < RTAX_MAX; i++) {
917 if (info.rti_info[i] == NULL)
919 if (info.rti_info[i]->sa_family != AF_INET6)
921 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
922 if (sa6_recoverscope(sin6) == 0)
923 bcopy(sin6, info.rti_info[i],
929 rtm->rtm_errno = error;
931 rtm->rtm_flags |= RTF_DONE;
933 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
934 if (m->m_pkthdr.len < rtm->rtm_msglen) {
937 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
938 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
944 m->m_flags |= RTS_FILTER_FIB;
947 * XXX insure we don't get a copy by
948 * invalidating our protocol
950 unsigned short family = rp->rcb_proto.sp_family;
951 rp->rcb_proto.sp_family = 0;
953 rp->rcb_proto.sp_family = family;
962 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
965 bzero(out, sizeof(*out));
966 out->rmx_mtu = rt->rt_mtu;
967 out->rmx_weight = rt->rt_weight;
968 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
969 /* Kernel -> userland timebase conversion. */
970 out->rmx_expire = rt->rt_expire ?
971 rt->rt_expire - time_uptime + time_second : 0;
975 * Extract the addresses of the passed sockaddrs.
976 * Do a little sanity checking so as to avoid bad memory references.
977 * This data is derived straight from userland.
980 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
985 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
986 if ((rtinfo->rti_addrs & (1 << i)) == 0)
988 sa = (struct sockaddr *)cp;
992 if (cp + sa->sa_len > cplim)
995 * there are no more.. quit now
996 * If there are more bits, they are in error.
997 * I've seen this. route(1) can evidently generate these.
998 * This causes kernel to core dump.
999 * for compatibility, If we see this, point to a safe address.
1001 if (sa->sa_len == 0) {
1002 rtinfo->rti_info[i] = &sa_zero;
1003 return (0); /* should be EINVAL but for compat */
1007 if (sa->sa_family == AF_INET6)
1008 sa6_embedscope((struct sockaddr_in6 *)sa,
1011 rtinfo->rti_info[i] = sa;
1018 * Fill in @dmask with valid netmask leaving original @smask
1019 * intact. Mostly used with radix netmasks.
1021 static struct sockaddr *
1022 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1023 struct sockaddr_storage *dmask)
1025 if (dst == NULL || smask == NULL)
1028 memset(dmask, 0, dst->sa_len);
1029 memcpy(dmask, smask, smask->sa_len);
1030 dmask->ss_len = dst->sa_len;
1031 dmask->ss_family = dst->sa_family;
1033 return ((struct sockaddr *)dmask);
1037 * Writes information related to @rtinfo object to newly-allocated mbuf.
1038 * Assumes MCLBYTES is enough to construct any message.
1039 * Used for OS notifications of vaious events (if/ifa announces,etc)
1041 * Returns allocated mbuf or NULL on failure.
1043 static struct mbuf *
1044 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1046 struct rt_msghdr *rtm;
1049 struct sockaddr *sa;
1051 struct sockaddr_storage ss;
1052 struct sockaddr_in6 *sin6;
1060 len = sizeof(struct ifa_msghdr);
1065 len = sizeof(struct ifma_msghdr);
1069 len = sizeof(struct if_msghdr);
1072 case RTM_IFANNOUNCE:
1074 len = sizeof(struct if_announcemsghdr);
1078 len = sizeof(struct rt_msghdr);
1081 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1082 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1084 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1086 m = m_gethdr(M_NOWAIT, MT_DATA);
1090 m->m_pkthdr.len = m->m_len = len;
1091 rtm = mtod(m, struct rt_msghdr *);
1092 bzero((caddr_t)rtm, len);
1093 for (i = 0; i < RTAX_MAX; i++) {
1094 if ((sa = rtinfo->rti_info[i]) == NULL)
1096 rtinfo->rti_addrs |= (1 << i);
1099 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1100 sin6 = (struct sockaddr_in6 *)&ss;
1101 bcopy(sa, sin6, sizeof(*sin6));
1102 if (sa6_recoverscope(sin6) == 0)
1103 sa = (struct sockaddr *)sin6;
1106 m_copyback(m, len, dlen, (caddr_t)sa);
1109 if (m->m_pkthdr.len != len) {
1113 rtm->rtm_msglen = len;
1114 rtm->rtm_version = RTM_VERSION;
1115 rtm->rtm_type = type;
1120 * Writes information related to @rtinfo object to preallocated buffer.
1121 * Stores needed size in @plen. If @w is NULL, calculates size without
1123 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1125 * Returns 0 on success.
1129 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1132 int len, buflen = 0, dlen;
1134 struct rt_msghdr *rtm = NULL;
1136 struct sockaddr_storage ss;
1137 struct sockaddr_in6 *sin6;
1139 #ifdef COMPAT_FREEBSD32
1140 bool compat32 = false;
1147 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1148 #ifdef COMPAT_FREEBSD32
1149 if (w->w_req->flags & SCTL_MASK32) {
1150 len = sizeof(struct ifa_msghdrl32);
1154 len = sizeof(struct ifa_msghdrl);
1156 len = sizeof(struct ifa_msghdr);
1160 #ifdef COMPAT_FREEBSD32
1161 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1162 if (w->w_op == NET_RT_IFLISTL)
1163 len = sizeof(struct if_msghdrl32);
1165 len = sizeof(struct if_msghdr32);
1170 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1171 len = sizeof(struct if_msghdrl);
1173 len = sizeof(struct if_msghdr);
1177 len = sizeof(struct ifma_msghdr);
1181 len = sizeof(struct rt_msghdr);
1185 rtm = (struct rt_msghdr *)w->w_tmem;
1186 buflen = w->w_tmemsize - len;
1187 cp = (caddr_t)w->w_tmem + len;
1190 rtinfo->rti_addrs = 0;
1191 for (i = 0; i < RTAX_MAX; i++) {
1192 struct sockaddr *sa;
1194 if ((sa = rtinfo->rti_info[i]) == NULL)
1196 rtinfo->rti_addrs |= (1 << i);
1197 #ifdef COMPAT_FREEBSD32
1199 dlen = SA_SIZE32(sa);
1203 if (cp != NULL && buflen >= dlen) {
1205 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1206 sin6 = (struct sockaddr_in6 *)&ss;
1207 bcopy(sa, sin6, sizeof(*sin6));
1208 if (sa6_recoverscope(sin6) == 0)
1209 sa = (struct sockaddr *)sin6;
1212 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1215 } else if (cp != NULL) {
1217 * Buffer too small. Count needed size
1218 * and return with error.
1227 dlen = ALIGN(len) - len;
1239 /* fill header iff buffer is large enough */
1240 rtm->rtm_version = RTM_VERSION;
1241 rtm->rtm_type = type;
1242 rtm->rtm_msglen = len;
1247 if (w != NULL && cp == NULL)
1254 * This routine is called to generate a message from the routing
1255 * socket indicating that a redirect has occurred, a routing lookup
1256 * has failed, or that a protocol has detected timeouts to a particular
1260 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1263 struct rt_msghdr *rtm;
1265 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1267 if (V_route_cb.any_count == 0)
1269 m = rtsock_msg_mbuf(type, rtinfo);
1273 if (fibnum != RT_ALL_FIBS) {
1274 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1275 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1276 M_SETFIB(m, fibnum);
1277 m->m_flags |= RTS_FILTER_FIB;
1280 rtm = mtod(m, struct rt_msghdr *);
1281 rtm->rtm_flags = RTF_DONE | flags;
1282 rtm->rtm_errno = error;
1283 rtm->rtm_addrs = rtinfo->rti_addrs;
1284 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1288 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1291 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1295 * This routine is called to generate a message from the routing
1296 * socket indicating that the status of a network interface has changed.
1299 rt_ifmsg(struct ifnet *ifp)
1301 struct if_msghdr *ifm;
1303 struct rt_addrinfo info;
1305 if (V_route_cb.any_count == 0)
1307 bzero((caddr_t)&info, sizeof(info));
1308 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1311 ifm = mtod(m, struct if_msghdr *);
1312 ifm->ifm_index = ifp->if_index;
1313 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1314 if_data_copy(ifp, &ifm->ifm_data);
1316 rt_dispatch(m, AF_UNSPEC);
1320 * Announce interface address arrival/withdraw.
1321 * Please do not call directly, use rt_addrmsg().
1322 * Assume input data to be valid.
1323 * Returns 0 on success.
1326 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1328 struct rt_addrinfo info;
1329 struct sockaddr *sa;
1332 struct ifa_msghdr *ifam;
1333 struct ifnet *ifp = ifa->ifa_ifp;
1334 struct sockaddr_storage ss;
1336 if (V_route_cb.any_count == 0)
1339 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1341 bzero((caddr_t)&info, sizeof(info));
1342 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1343 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1344 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1345 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1346 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1347 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1349 ifam = mtod(m, struct ifa_msghdr *);
1350 ifam->ifam_index = ifp->if_index;
1351 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1352 ifam->ifam_flags = ifa->ifa_flags;
1353 ifam->ifam_addrs = info.rti_addrs;
1355 if (fibnum != RT_ALL_FIBS) {
1356 M_SETFIB(m, fibnum);
1357 m->m_flags |= RTS_FILTER_FIB;
1360 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1366 * Announce route addition/removal.
1367 * Please do not call directly, use rt_routemsg().
1368 * Note that @rt data MAY be inconsistent/invalid:
1369 * if some userland app sends us "invalid" route message (invalid mask,
1370 * no dst, wrong address families, etc...) we need to pass it back
1371 * to app (and any other rtsock consumers) with rtm_errno field set to
1374 * Returns 0 on success.
1377 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1380 struct rt_addrinfo info;
1381 struct sockaddr *sa;
1383 struct rt_msghdr *rtm;
1384 struct sockaddr_storage ss;
1386 if (V_route_cb.any_count == 0)
1389 bzero((caddr_t)&info, sizeof(info));
1390 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1391 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1392 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1393 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1395 rtm = mtod(m, struct rt_msghdr *);
1396 rtm->rtm_index = ifp->if_index;
1397 rtm->rtm_flags |= rt->rt_flags;
1398 rtm->rtm_errno = error;
1399 rtm->rtm_addrs = info.rti_addrs;
1401 if (fibnum != RT_ALL_FIBS) {
1402 M_SETFIB(m, fibnum);
1403 m->m_flags |= RTS_FILTER_FIB;
1406 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1412 * This is the analogue to the rt_newaddrmsg which performs the same
1413 * function but for multicast group memberhips. This is easier since
1414 * there is no route state to worry about.
1417 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1419 struct rt_addrinfo info;
1420 struct mbuf *m = NULL;
1421 struct ifnet *ifp = ifma->ifma_ifp;
1422 struct ifma_msghdr *ifmam;
1424 if (V_route_cb.any_count == 0)
1427 bzero((caddr_t)&info, sizeof(info));
1428 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1429 if (ifp && ifp->if_addr)
1430 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1432 info.rti_info[RTAX_IFP] = NULL;
1434 * If a link-layer address is present, present it as a ``gateway''
1435 * (similarly to how ARP entries, e.g., are presented).
1437 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1438 m = rtsock_msg_mbuf(cmd, &info);
1441 ifmam = mtod(m, struct ifma_msghdr *);
1442 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1444 ifmam->ifmam_index = ifp->if_index;
1445 ifmam->ifmam_addrs = info.rti_addrs;
1446 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1449 static struct mbuf *
1450 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1451 struct rt_addrinfo *info)
1453 struct if_announcemsghdr *ifan;
1456 if (V_route_cb.any_count == 0)
1458 bzero((caddr_t)info, sizeof(*info));
1459 m = rtsock_msg_mbuf(type, info);
1461 ifan = mtod(m, struct if_announcemsghdr *);
1462 ifan->ifan_index = ifp->if_index;
1463 strlcpy(ifan->ifan_name, ifp->if_xname,
1464 sizeof(ifan->ifan_name));
1465 ifan->ifan_what = what;
1471 * This is called to generate routing socket messages indicating
1472 * IEEE80211 wireless events.
1473 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1476 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1479 struct rt_addrinfo info;
1481 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1484 * Append the ieee80211 data. Try to stick it in the
1485 * mbuf containing the ifannounce msg; otherwise allocate
1486 * a new mbuf and append.
1488 * NB: we assume m is a single mbuf.
1490 if (data_len > M_TRAILINGSPACE(m)) {
1491 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1496 bcopy(data, mtod(n, void *), data_len);
1497 n->m_len = data_len;
1499 } else if (data_len > 0) {
1500 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1501 m->m_len += data_len;
1503 if (m->m_flags & M_PKTHDR)
1504 m->m_pkthdr.len += data_len;
1505 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1506 rt_dispatch(m, AF_UNSPEC);
1511 * This is called to generate routing socket messages indicating
1512 * network interface arrival and departure.
1515 rt_ifannouncemsg(struct ifnet *ifp, int what)
1518 struct rt_addrinfo info;
1520 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1522 rt_dispatch(m, AF_UNSPEC);
1526 rt_dispatch(struct mbuf *m, sa_family_t saf)
1531 * Preserve the family from the sockaddr, if any, in an m_tag for
1532 * use when injecting the mbuf into the routing socket buffer from
1535 if (saf != AF_UNSPEC) {
1536 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1542 *(unsigned short *)(tag + 1) = saf;
1543 m_tag_prepend(m, tag);
1547 m->m_pkthdr.rcvif = V_loif;
1553 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1557 * This is used in dumping the kernel table via sysctl().
1560 sysctl_dumpentry(struct radix_node *rn, void *vw)
1562 struct walkarg *w = vw;
1563 struct rtentry *rt = (struct rtentry *)rn;
1564 int error = 0, size;
1565 struct rt_addrinfo info;
1566 struct sockaddr_storage ss;
1570 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1572 if ((rt->rt_flags & RTF_HOST) == 0
1573 ? jailed_without_vnet(w->w_req->td->td_ucred)
1574 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1576 bzero((caddr_t)&info, sizeof(info));
1577 info.rti_info[RTAX_DST] = rt_key(rt);
1578 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1579 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1581 info.rti_info[RTAX_GENMASK] = 0;
1582 if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1583 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1584 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1585 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1586 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1588 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1590 if (w->w_req && w->w_tmem) {
1591 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1593 bzero(&rtm->rtm_index,
1594 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1595 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1596 rtm->rtm_flags = RTF_GATEWAY |
1597 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1599 rtm->rtm_flags = rt->rt_flags;
1600 rt_getmetrics(rt, &rtm->rtm_rmx);
1601 rtm->rtm_index = rt->rt_ifp->if_index;
1602 rtm->rtm_addrs = info.rti_addrs;
1603 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1610 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1611 struct rt_addrinfo *info, struct walkarg *w, int len)
1613 struct if_msghdrl *ifm;
1614 struct if_data *ifd;
1616 ifm = (struct if_msghdrl *)w->w_tmem;
1618 #ifdef COMPAT_FREEBSD32
1619 if (w->w_req->flags & SCTL_MASK32) {
1620 struct if_msghdrl32 *ifm32;
1622 ifm32 = (struct if_msghdrl32 *)ifm;
1623 ifm32->ifm_addrs = info->rti_addrs;
1624 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1625 ifm32->ifm_index = ifp->if_index;
1626 ifm32->_ifm_spare1 = 0;
1627 ifm32->ifm_len = sizeof(*ifm32);
1628 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1629 ifm32->_ifm_spare2 = 0;
1630 ifd = &ifm32->ifm_data;
1634 ifm->ifm_addrs = info->rti_addrs;
1635 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1636 ifm->ifm_index = ifp->if_index;
1637 ifm->_ifm_spare1 = 0;
1638 ifm->ifm_len = sizeof(*ifm);
1639 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1640 ifm->_ifm_spare2 = 0;
1641 ifd = &ifm->ifm_data;
1644 memcpy(ifd, src_ifd, sizeof(*ifd));
1646 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1650 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1651 struct rt_addrinfo *info, struct walkarg *w, int len)
1653 struct if_msghdr *ifm;
1654 struct if_data *ifd;
1656 ifm = (struct if_msghdr *)w->w_tmem;
1658 #ifdef COMPAT_FREEBSD32
1659 if (w->w_req->flags & SCTL_MASK32) {
1660 struct if_msghdr32 *ifm32;
1662 ifm32 = (struct if_msghdr32 *)ifm;
1663 ifm32->ifm_addrs = info->rti_addrs;
1664 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1665 ifm32->ifm_index = ifp->if_index;
1666 ifm32->_ifm_spare1 = 0;
1667 ifd = &ifm32->ifm_data;
1671 ifm->ifm_addrs = info->rti_addrs;
1672 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1673 ifm->ifm_index = ifp->if_index;
1674 ifm->_ifm_spare1 = 0;
1675 ifd = &ifm->ifm_data;
1678 memcpy(ifd, src_ifd, sizeof(*ifd));
1680 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1684 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1685 struct walkarg *w, int len)
1687 struct ifa_msghdrl *ifam;
1688 struct if_data *ifd;
1690 ifam = (struct ifa_msghdrl *)w->w_tmem;
1692 #ifdef COMPAT_FREEBSD32
1693 if (w->w_req->flags & SCTL_MASK32) {
1694 struct ifa_msghdrl32 *ifam32;
1696 ifam32 = (struct ifa_msghdrl32 *)ifam;
1697 ifam32->ifam_addrs = info->rti_addrs;
1698 ifam32->ifam_flags = ifa->ifa_flags;
1699 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1700 ifam32->_ifam_spare1 = 0;
1701 ifam32->ifam_len = sizeof(*ifam32);
1702 ifam32->ifam_data_off =
1703 offsetof(struct ifa_msghdrl32, ifam_data);
1704 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1705 ifd = &ifam32->ifam_data;
1709 ifam->ifam_addrs = info->rti_addrs;
1710 ifam->ifam_flags = ifa->ifa_flags;
1711 ifam->ifam_index = ifa->ifa_ifp->if_index;
1712 ifam->_ifam_spare1 = 0;
1713 ifam->ifam_len = sizeof(*ifam);
1714 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1715 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1716 ifd = &ifam->ifam_data;
1719 bzero(ifd, sizeof(*ifd));
1720 ifd->ifi_datalen = sizeof(struct if_data);
1721 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1722 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1723 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1724 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1726 /* Fixup if_data carp(4) vhid. */
1727 if (carp_get_vhid_p != NULL)
1728 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1730 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1734 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1735 struct walkarg *w, int len)
1737 struct ifa_msghdr *ifam;
1739 ifam = (struct ifa_msghdr *)w->w_tmem;
1740 ifam->ifam_addrs = info->rti_addrs;
1741 ifam->ifam_flags = ifa->ifa_flags;
1742 ifam->ifam_index = ifa->ifa_ifp->if_index;
1743 ifam->_ifam_spare1 = 0;
1744 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1746 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1750 sysctl_iflist(int af, struct walkarg *w)
1755 struct rt_addrinfo info;
1757 struct sockaddr_storage ss;
1758 struct epoch_tracker et;
1760 bzero((caddr_t)&info, sizeof(info));
1761 bzero(&ifd, sizeof(ifd));
1762 NET_EPOCH_ENTER(et);
1763 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1764 if (w->w_arg && w->w_arg != ifp->if_index)
1766 if_data_copy(ifp, &ifd);
1768 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1769 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1772 info.rti_info[RTAX_IFP] = NULL;
1773 if (w->w_req && w->w_tmem) {
1774 if (w->w_op == NET_RT_IFLISTL)
1775 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1778 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1783 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1784 if (af && af != ifa->ifa_addr->sa_family)
1786 if (prison_if(w->w_req->td->td_ucred,
1787 ifa->ifa_addr) != 0)
1789 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1790 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1791 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1792 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1793 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1796 if (w->w_req && w->w_tmem) {
1797 if (w->w_op == NET_RT_IFLISTL)
1798 error = sysctl_iflist_ifaml(ifa, &info,
1801 error = sysctl_iflist_ifam(ifa, &info,
1807 info.rti_info[RTAX_IFA] = NULL;
1808 info.rti_info[RTAX_NETMASK] = NULL;
1809 info.rti_info[RTAX_BRD] = NULL;
1817 sysctl_ifmalist(int af, struct walkarg *w)
1819 struct rt_addrinfo info;
1820 struct epoch_tracker et;
1822 struct ifmultiaddr *ifma;
1827 bzero((caddr_t)&info, sizeof(info));
1829 NET_EPOCH_ENTER(et);
1830 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1831 if (w->w_arg && w->w_arg != ifp->if_index)
1834 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1835 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1836 if (af && af != ifma->ifma_addr->sa_family)
1838 if (prison_if(w->w_req->td->td_ucred,
1839 ifma->ifma_addr) != 0)
1841 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1842 info.rti_info[RTAX_GATEWAY] =
1843 (ifma->ifma_addr->sa_family != AF_LINK) ?
1844 ifma->ifma_lladdr : NULL;
1845 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1848 if (w->w_req && w->w_tmem) {
1849 struct ifma_msghdr *ifmam;
1851 ifmam = (struct ifma_msghdr *)w->w_tmem;
1852 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1853 ifmam->ifmam_flags = 0;
1854 ifmam->ifmam_addrs = info.rti_addrs;
1855 ifmam->_ifmam_spare1 = 0;
1856 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1869 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1872 int *name = (int *)arg1;
1873 u_int namelen = arg2;
1874 struct rib_head *rnh = NULL; /* silence compiler. */
1875 int i, lim, error = EINVAL;
1884 if (name[1] == NET_RT_DUMP) {
1886 fib = req->td->td_proc->p_fibnum;
1887 else if (namelen == 4)
1888 fib = (name[3] == RT_ALL_FIBS) ?
1889 req->td->td_proc->p_fibnum : name[3];
1891 return ((namelen < 3) ? EISDIR : ENOTDIR);
1892 if (fib < 0 || fib >= rt_numfibs)
1894 } else if (namelen != 3)
1895 return ((namelen < 3) ? EISDIR : ENOTDIR);
1899 bzero(&w, sizeof(w));
1904 error = sysctl_wire_old_buffer(req, 0);
1909 * Allocate reply buffer in advance.
1910 * All rtsock messages has maximum length of u_short.
1912 w.w_tmemsize = 65536;
1913 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1919 if (af == 0) { /* dump all tables */
1922 } else /* dump only one table */
1926 * take care of llinfo entries, the caller must
1929 if (w.w_op == NET_RT_FLAGS &&
1930 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1932 error = lltable_sysctl_dumparp(af, w.w_req);
1938 * take care of routing entries
1940 for (error = 0; error == 0 && i <= lim; i++) {
1941 rnh = rt_tables_get_rnh(fib, i);
1943 struct epoch_tracker et;
1946 NET_EPOCH_ENTER(et);
1947 error = rnh->rnh_walktree(&rnh->head,
1948 sysctl_dumpentry, &w);
1952 error = EAFNOSUPPORT;
1957 case NET_RT_IFLISTL:
1958 error = sysctl_iflist(af, &w);
1961 case NET_RT_IFMALIST:
1962 error = sysctl_ifmalist(af, &w);
1966 free(w.w_tmem, M_TEMP);
1970 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1973 * Definitions of protocols supported in the ROUTE domain.
1976 static struct domain routedomain; /* or at least forward */
1978 static struct protosw routesw[] = {
1980 .pr_type = SOCK_RAW,
1981 .pr_domain = &routedomain,
1982 .pr_flags = PR_ATOMIC|PR_ADDR,
1983 .pr_output = route_output,
1984 .pr_ctlinput = raw_ctlinput,
1985 .pr_init = raw_init,
1986 .pr_usrreqs = &route_usrreqs
1990 static struct domain routedomain = {
1991 .dom_family = PF_ROUTE,
1992 .dom_name = "route",
1993 .dom_protosw = routesw,
1994 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1997 VNET_DOMAIN_SET(route);