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_compat.h"
35 #include "opt_mpath.h"
37 #include "opt_inet6.h"
39 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/domain.h>
44 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/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>
86 struct if_data ifm_data;
98 uint16_t ifm_data_off;
99 struct if_data ifm_data;
102 struct ifa_msghdrl32 {
103 uint16_t ifam_msglen;
104 uint8_t ifam_version;
109 uint16_t _ifam_spare1;
111 uint16_t ifam_data_off;
113 struct if_data ifam_data;
116 #define SA_SIZE32(sa) \
117 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
119 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
121 #endif /* COMPAT_FREEBSD32 */
123 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
125 /* NB: these are not modified */
126 static struct sockaddr route_src = { 2, PF_ROUTE, };
127 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
129 /* These are external hooks for CARP. */
130 int (*carp_get_vhid_p)(struct ifaddr *);
133 * Used by rtsock/raw_input callback code to decide whether to filter the update
134 * notification to a socket bound to a particular FIB.
136 #define RTS_FILTER_FIB M_PROTO8
139 int ip_count; /* attached w/ AF_INET */
140 int ip6_count; /* attached w/ AF_INET6 */
141 int any_count; /* total attached */
143 static VNET_DEFINE(route_cb_t, route_cb);
144 #define V_route_cb VNET(route_cb)
146 struct mtx rtsock_mtx;
147 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
149 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
150 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
151 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
153 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
159 struct sysctl_req *w_req;
162 static void rts_input(struct mbuf *m);
163 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
164 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
165 struct walkarg *w, int *plen);
166 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
167 struct rt_addrinfo *rtinfo);
168 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
169 static int sysctl_iflist(int af, struct walkarg *w);
170 static int sysctl_ifmalist(int af, struct walkarg *w);
171 static int route_output(struct mbuf *m, struct socket *so, ...);
172 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
173 static void rt_dispatch(struct mbuf *, sa_family_t);
174 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
175 struct sockaddr *smask, struct sockaddr_storage *dmask);
177 static struct netisr_handler rtsock_nh = {
179 .nh_handler = rts_input,
180 .nh_proto = NETISR_ROUTE,
181 .nh_policy = NETISR_POLICY_SOURCE,
185 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
189 netisr_getqlimit(&rtsock_nh, &qlimit);
190 error = sysctl_handle_int(oidp, &qlimit, 0, req);
191 if (error || !req->newptr)
195 return (netisr_setqlimit(&rtsock_nh, qlimit));
197 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
198 0, 0, sysctl_route_netisr_maxqlen, "I",
199 "maximum routing socket dispatch queue length");
206 if (IS_DEFAULT_VNET(curvnet)) {
207 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
208 rtsock_nh.nh_qlimit = tmp;
209 netisr_register(&rtsock_nh);
213 netisr_register_vnet(&rtsock_nh);
216 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
221 vnet_rts_uninit(void)
224 netisr_unregister_vnet(&rtsock_nh);
226 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
231 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
236 KASSERT(m != NULL, ("%s: m is NULL", __func__));
237 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
238 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
240 /* No filtering requested. */
241 if ((m->m_flags & RTS_FILTER_FIB) == 0)
244 /* Check if it is a rts and the fib matches the one of the socket. */
245 fibnum = M_GETFIB(m);
246 if (proto->sp_family != PF_ROUTE ||
247 rp->rcb_socket == NULL ||
248 rp->rcb_socket->so_fibnum == fibnum)
251 /* Filtering requested and no match, the socket shall be skipped. */
256 rts_input(struct mbuf *m)
258 struct sockproto route_proto;
259 unsigned short *family;
262 route_proto.sp_family = PF_ROUTE;
263 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
265 family = (unsigned short *)(tag + 1);
266 route_proto.sp_protocol = *family;
267 m_tag_delete(m, tag);
269 route_proto.sp_protocol = 0;
271 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
275 * It really doesn't make any sense at all for this code to share much
276 * with raw_usrreq.c, since its functionality is so restricted. XXX
279 rts_abort(struct socket *so)
282 raw_usrreqs.pru_abort(so);
286 rts_close(struct socket *so)
289 raw_usrreqs.pru_close(so);
292 /* pru_accept is EOPNOTSUPP */
295 rts_attach(struct socket *so, int proto, struct thread *td)
300 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
303 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
305 so->so_pcb = (caddr_t)rp;
306 so->so_fibnum = td->td_proc->p_fibnum;
307 error = raw_attach(so, proto);
315 switch(rp->rcb_proto.sp_protocol) {
317 V_route_cb.ip_count++;
320 V_route_cb.ip6_count++;
323 V_route_cb.any_count++;
326 so->so_options |= SO_USELOOPBACK;
331 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
334 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
338 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
341 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
344 /* pru_connect2 is EOPNOTSUPP */
345 /* pru_control is EOPNOTSUPP */
348 rts_detach(struct socket *so)
350 struct rawcb *rp = sotorawcb(so);
352 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
355 switch(rp->rcb_proto.sp_protocol) {
357 V_route_cb.ip_count--;
360 V_route_cb.ip6_count--;
363 V_route_cb.any_count--;
365 raw_usrreqs.pru_detach(so);
369 rts_disconnect(struct socket *so)
372 return (raw_usrreqs.pru_disconnect(so));
375 /* pru_listen is EOPNOTSUPP */
378 rts_peeraddr(struct socket *so, struct sockaddr **nam)
381 return (raw_usrreqs.pru_peeraddr(so, nam));
384 /* pru_rcvd is EOPNOTSUPP */
385 /* pru_rcvoob is EOPNOTSUPP */
388 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
389 struct mbuf *control, struct thread *td)
392 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
395 /* pru_sense is null */
398 rts_shutdown(struct socket *so)
401 return (raw_usrreqs.pru_shutdown(so));
405 rts_sockaddr(struct socket *so, struct sockaddr **nam)
408 return (raw_usrreqs.pru_sockaddr(so, nam));
411 static struct pr_usrreqs route_usrreqs = {
412 .pru_abort = rts_abort,
413 .pru_attach = rts_attach,
414 .pru_bind = rts_bind,
415 .pru_connect = rts_connect,
416 .pru_detach = rts_detach,
417 .pru_disconnect = rts_disconnect,
418 .pru_peeraddr = rts_peeraddr,
419 .pru_send = rts_send,
420 .pru_shutdown = rts_shutdown,
421 .pru_sockaddr = rts_sockaddr,
422 .pru_close = rts_close,
425 #ifndef _SOCKADDR_UNION_DEFINED
426 #define _SOCKADDR_UNION_DEFINED
428 * The union of all possible address formats we handle.
430 union sockaddr_union {
432 struct sockaddr_in sin;
433 struct sockaddr_in6 sin6;
435 #endif /* _SOCKADDR_UNION_DEFINED */
438 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
439 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
442 /* First, see if the returned address is part of the jail. */
443 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
444 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
448 switch (info->rti_info[RTAX_DST]->sa_family) {
458 * Try to find an address on the given outgoing interface
459 * that belongs to the jail.
462 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
465 if (sa->sa_family != AF_INET)
467 ia = ((struct sockaddr_in *)sa)->sin_addr;
468 if (prison_check_ip4(cred, &ia) == 0) {
473 IF_ADDR_RUNLOCK(ifp);
476 * As a last resort return the 'default' jail address.
478 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
480 if (prison_get_ip4(cred, &ia) != 0)
483 bzero(&saun->sin, sizeof(struct sockaddr_in));
484 saun->sin.sin_len = sizeof(struct sockaddr_in);
485 saun->sin.sin_family = AF_INET;
486 saun->sin.sin_addr.s_addr = ia.s_addr;
487 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
500 * Try to find an address on the given outgoing interface
501 * that belongs to the jail.
504 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
507 if (sa->sa_family != AF_INET6)
509 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
510 &ia6, sizeof(struct in6_addr));
511 if (prison_check_ip6(cred, &ia6) == 0) {
516 IF_ADDR_RUNLOCK(ifp);
519 * As a last resort return the 'default' jail address.
521 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
523 if (prison_get_ip6(cred, &ia6) != 0)
526 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
527 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
528 saun->sin6.sin6_family = AF_INET6;
529 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
530 if (sa6_recoverscope(&saun->sin6) != 0)
532 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
544 route_output(struct mbuf *m, struct socket *so, ...)
546 struct rt_msghdr *rtm = NULL;
547 struct rtentry *rt = NULL;
548 struct rib_head *rnh;
549 struct rt_addrinfo info;
550 struct sockaddr_storage ss;
552 struct sockaddr_in6 *sin6;
553 int i, rti_need_deembed = 0;
555 int alloc_len = 0, len, error = 0, fibnum;
556 struct ifnet *ifp = NULL;
557 union sockaddr_union saun;
558 sa_family_t saf = AF_UNSPEC;
559 struct rawcb *rp = NULL;
562 fibnum = so->so_fibnum;
564 #define senderr(e) { error = e; goto flush;}
565 if (m == NULL || ((m->m_len < sizeof(long)) &&
566 (m = m_pullup(m, sizeof(long))) == NULL))
568 if ((m->m_flags & M_PKTHDR) == 0)
569 panic("route_output");
570 len = m->m_pkthdr.len;
571 if (len < sizeof(*rtm) ||
572 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
576 * Most of current messages are in range 200-240 bytes,
577 * minimize possible re-allocation on reply using larger size
578 * buffer aligned on 1k boundaty.
580 alloc_len = roundup2(len, 1024);
581 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
584 m_copydata(m, 0, len, (caddr_t)rtm);
585 bzero(&info, sizeof(info));
586 bzero(&w, sizeof(w));
588 if (rtm->rtm_version != RTM_VERSION) {
589 /* Do not touch message since format is unknown */
592 senderr(EPROTONOSUPPORT);
596 * Starting from here, it is possible
597 * to alter original message and insert
598 * caller PID and error value.
601 rtm->rtm_pid = curproc->p_pid;
602 info.rti_addrs = rtm->rtm_addrs;
604 info.rti_mflags = rtm->rtm_inits;
605 info.rti_rmx = &rtm->rtm_rmx;
608 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
609 * link-local address because rtrequest requires addresses with
612 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
615 info.rti_flags = rtm->rtm_flags;
616 if (info.rti_info[RTAX_DST] == NULL ||
617 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
618 (info.rti_info[RTAX_GATEWAY] != NULL &&
619 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
621 saf = info.rti_info[RTAX_DST]->sa_family;
623 * Verify that the caller has the appropriate privilege; RTM_GET
624 * is the only operation the non-superuser is allowed.
626 if (rtm->rtm_type != RTM_GET) {
627 error = priv_check(curthread, PRIV_NET_ROUTE);
633 * The given gateway address may be an interface address.
634 * For example, issuing a "route change" command on a route
635 * entry that was created from a tunnel, and the gateway
636 * address given is the local end point. In this case the
637 * RTF_GATEWAY flag must be cleared or the destination will
638 * not be reachable even though there is no error message.
640 if (info.rti_info[RTAX_GATEWAY] != NULL &&
641 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
642 struct rt_addrinfo ginfo;
643 struct sockaddr *gdst;
645 bzero(&ginfo, sizeof(ginfo));
646 bzero(&ss, sizeof(ss));
647 ss.ss_len = sizeof(ss);
649 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
650 gdst = info.rti_info[RTAX_GATEWAY];
653 * A host route through the loopback interface is
654 * installed for each interface adddress. In pre 8.0
655 * releases the interface address of a PPP link type
656 * is not reachable locally. This behavior is fixed as
657 * part of the new L2/L3 redesign and rewrite work. The
658 * signature of this interface address route is the
659 * AF_LINK sa_family type of the rt_gateway, and the
660 * rt_ifp has the IFF_LOOPBACK flag set.
662 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
663 if (ss.ss_family == AF_LINK &&
664 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
665 info.rti_flags &= ~RTF_GATEWAY;
666 info.rti_flags |= RTF_GWFLAG_COMPAT;
668 rib_free_info(&ginfo);
672 switch (rtm->rtm_type) {
673 struct rtentry *saved_nrt;
677 if (info.rti_info[RTAX_GATEWAY] == NULL)
681 /* support for new ARP code */
682 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
683 (rtm->rtm_flags & RTF_LLDATA) != 0) {
684 error = lla_rt_output(rtm, &info);
687 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
691 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
693 if (error == 0 && saved_nrt != NULL) {
695 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
698 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
699 RT_REMREF(saved_nrt);
700 RT_UNLOCK(saved_nrt);
706 /* support for new ARP code */
707 if (info.rti_info[RTAX_GATEWAY] &&
708 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
709 (rtm->rtm_flags & RTF_LLDATA) != 0) {
710 error = lla_rt_output(rtm, &info);
713 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
717 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
724 /* rt_msg2() will not be used when RTM_DELETE fails. */
725 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
730 rnh = rt_tables_get_rnh(fibnum, saf);
732 senderr(EAFNOSUPPORT);
736 if (info.rti_info[RTAX_NETMASK] == NULL &&
737 rtm->rtm_type == RTM_GET) {
739 * Provide longest prefix match for
740 * address lookup (no mask).
741 * 'route -n get addr'
743 rt = (struct rtentry *) rnh->rnh_matchaddr(
744 info.rti_info[RTAX_DST], &rnh->head);
746 rt = (struct rtentry *) rnh->rnh_lookup(
747 info.rti_info[RTAX_DST],
748 info.rti_info[RTAX_NETMASK], &rnh->head);
756 * for RTM_CHANGE/LOCK, if we got multipath routes,
757 * we require users to specify a matching RTAX_GATEWAY.
759 * for RTM_GET, gate is optional even with multipath.
760 * if gate == NULL the first match is returned.
761 * (no need to call rt_mpath_matchgate if gate == NULL)
763 if (rt_mpath_capable(rnh) &&
764 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
765 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
773 * If performing proxied L2 entry insertion, and
774 * the actual PPP host entry is found, perform
775 * another search to retrieve the prefix route of
776 * the local end point of the PPP link.
778 if (rtm->rtm_flags & RTF_ANNOUNCE) {
779 struct sockaddr laddr;
781 if (rt->rt_ifp != NULL &&
782 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
785 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
788 rt_maskedcopy(ifa->ifa_addr,
792 rt_maskedcopy(rt->rt_ifa->ifa_addr,
794 rt->rt_ifa->ifa_netmask);
796 * refactor rt and no lock operation necessary
798 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
811 if ((rt->rt_flags & RTF_HOST) == 0
812 ? jailed_without_vnet(curthread->td_ucred)
813 : prison_if(curthread->td_ucred,
818 info.rti_info[RTAX_DST] = rt_key(rt);
819 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
820 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
822 info.rti_info[RTAX_GENMASK] = 0;
823 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
826 info.rti_info[RTAX_IFP] =
827 ifp->if_addr->ifa_addr;
828 error = rtm_get_jailed(&info, ifp, rt,
829 &saun, curthread->td_ucred);
834 if (ifp->if_flags & IFF_POINTOPOINT)
835 info.rti_info[RTAX_BRD] =
836 rt->rt_ifa->ifa_dstaddr;
837 rtm->rtm_index = ifp->if_index;
839 info.rti_info[RTAX_IFP] = NULL;
840 info.rti_info[RTAX_IFA] = NULL;
842 } else if ((ifp = rt->rt_ifp) != NULL) {
843 rtm->rtm_index = ifp->if_index;
846 /* Check if we need to realloc storage */
847 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
848 if (len > alloc_len) {
849 struct rt_msghdr *new_rtm;
850 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
851 if (new_rtm == NULL) {
855 bcopy(rtm, new_rtm, rtm->rtm_msglen);
861 w.w_tmem = (caddr_t)rtm;
862 w.w_tmemsize = alloc_len;
863 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
865 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
866 rtm->rtm_flags = RTF_GATEWAY |
867 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
869 rtm->rtm_flags = rt->rt_flags;
870 rt_getmetrics(rt, &rtm->rtm_rmx);
871 rtm->rtm_addrs = info.rti_addrs;
884 * Check to see if we don't want our own messages.
886 if ((so->so_options & SO_USELOOPBACK) == 0) {
887 if (V_route_cb.any_count <= 1) {
893 /* There is another listener, so construct message */
899 if (rti_need_deembed) {
900 /* sin6_scope_id is recovered before sending rtm. */
901 sin6 = (struct sockaddr_in6 *)&ss;
902 for (i = 0; i < RTAX_MAX; i++) {
903 if (info.rti_info[i] == NULL)
905 if (info.rti_info[i]->sa_family != AF_INET6)
907 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
908 if (sa6_recoverscope(sin6) == 0)
909 bcopy(sin6, info.rti_info[i],
915 rtm->rtm_errno = error;
917 rtm->rtm_flags |= RTF_DONE;
919 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
920 if (m->m_pkthdr.len < rtm->rtm_msglen) {
923 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
924 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
930 m->m_flags |= RTS_FILTER_FIB;
933 * XXX insure we don't get a copy by
934 * invalidating our protocol
936 unsigned short family = rp->rcb_proto.sp_family;
937 rp->rcb_proto.sp_family = 0;
939 rp->rcb_proto.sp_family = family;
948 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
951 bzero(out, sizeof(*out));
952 out->rmx_mtu = rt->rt_mtu;
953 out->rmx_weight = rt->rt_weight;
954 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
955 /* Kernel -> userland timebase conversion. */
956 out->rmx_expire = rt->rt_expire ?
957 rt->rt_expire - time_uptime + time_second : 0;
961 * Extract the addresses of the passed sockaddrs.
962 * Do a little sanity checking so as to avoid bad memory references.
963 * This data is derived straight from userland.
966 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
971 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
972 if ((rtinfo->rti_addrs & (1 << i)) == 0)
974 sa = (struct sockaddr *)cp;
978 if (cp + sa->sa_len > cplim)
981 * there are no more.. quit now
982 * If there are more bits, they are in error.
983 * I've seen this. route(1) can evidently generate these.
984 * This causes kernel to core dump.
985 * for compatibility, If we see this, point to a safe address.
987 if (sa->sa_len == 0) {
988 rtinfo->rti_info[i] = &sa_zero;
989 return (0); /* should be EINVAL but for compat */
993 if (sa->sa_family == AF_INET6)
994 sa6_embedscope((struct sockaddr_in6 *)sa,
997 rtinfo->rti_info[i] = sa;
1004 * Fill in @dmask with valid netmask leaving original @smask
1005 * intact. Mostly used with radix netmasks.
1007 static struct sockaddr *
1008 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1009 struct sockaddr_storage *dmask)
1011 if (dst == NULL || smask == NULL)
1014 memset(dmask, 0, dst->sa_len);
1015 memcpy(dmask, smask, smask->sa_len);
1016 dmask->ss_len = dst->sa_len;
1017 dmask->ss_family = dst->sa_family;
1019 return ((struct sockaddr *)dmask);
1023 * Writes information related to @rtinfo object to newly-allocated mbuf.
1024 * Assumes MCLBYTES is enough to construct any message.
1025 * Used for OS notifications of vaious events (if/ifa announces,etc)
1027 * Returns allocated mbuf or NULL on failure.
1029 static struct mbuf *
1030 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1032 struct rt_msghdr *rtm;
1035 struct sockaddr *sa;
1037 struct sockaddr_storage ss;
1038 struct sockaddr_in6 *sin6;
1046 len = sizeof(struct ifa_msghdr);
1051 len = sizeof(struct ifma_msghdr);
1055 len = sizeof(struct if_msghdr);
1058 case RTM_IFANNOUNCE:
1060 len = sizeof(struct if_announcemsghdr);
1064 len = sizeof(struct rt_msghdr);
1067 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1068 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1070 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1072 m = m_gethdr(M_NOWAIT, MT_DATA);
1076 m->m_pkthdr.len = m->m_len = len;
1077 rtm = mtod(m, struct rt_msghdr *);
1078 bzero((caddr_t)rtm, len);
1079 for (i = 0; i < RTAX_MAX; i++) {
1080 if ((sa = rtinfo->rti_info[i]) == NULL)
1082 rtinfo->rti_addrs |= (1 << i);
1085 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1086 sin6 = (struct sockaddr_in6 *)&ss;
1087 bcopy(sa, sin6, sizeof(*sin6));
1088 if (sa6_recoverscope(sin6) == 0)
1089 sa = (struct sockaddr *)sin6;
1092 m_copyback(m, len, dlen, (caddr_t)sa);
1095 if (m->m_pkthdr.len != len) {
1099 rtm->rtm_msglen = len;
1100 rtm->rtm_version = RTM_VERSION;
1101 rtm->rtm_type = type;
1106 * Writes information related to @rtinfo object to preallocated buffer.
1107 * Stores needed size in @plen. If @w is NULL, calculates size without
1109 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1111 * Returns 0 on success.
1115 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1118 int len, buflen = 0, dlen;
1120 struct rt_msghdr *rtm = NULL;
1122 struct sockaddr_storage ss;
1123 struct sockaddr_in6 *sin6;
1125 #ifdef COMPAT_FREEBSD32
1126 bool compat32 = false;
1133 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1134 #ifdef COMPAT_FREEBSD32
1135 if (w->w_req->flags & SCTL_MASK32) {
1136 len = sizeof(struct ifa_msghdrl32);
1140 len = sizeof(struct ifa_msghdrl);
1142 len = sizeof(struct ifa_msghdr);
1146 #ifdef COMPAT_FREEBSD32
1147 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1148 if (w->w_op == NET_RT_IFLISTL)
1149 len = sizeof(struct if_msghdrl32);
1151 len = sizeof(struct if_msghdr32);
1156 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1157 len = sizeof(struct if_msghdrl);
1159 len = sizeof(struct if_msghdr);
1163 len = sizeof(struct ifma_msghdr);
1167 len = sizeof(struct rt_msghdr);
1171 rtm = (struct rt_msghdr *)w->w_tmem;
1172 buflen = w->w_tmemsize - len;
1173 cp = (caddr_t)w->w_tmem + len;
1176 rtinfo->rti_addrs = 0;
1177 for (i = 0; i < RTAX_MAX; i++) {
1178 struct sockaddr *sa;
1180 if ((sa = rtinfo->rti_info[i]) == NULL)
1182 rtinfo->rti_addrs |= (1 << i);
1183 #ifdef COMPAT_FREEBSD32
1185 dlen = SA_SIZE32(sa);
1189 if (cp != NULL && buflen >= dlen) {
1191 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1192 sin6 = (struct sockaddr_in6 *)&ss;
1193 bcopy(sa, sin6, sizeof(*sin6));
1194 if (sa6_recoverscope(sin6) == 0)
1195 sa = (struct sockaddr *)sin6;
1198 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1201 } else if (cp != NULL) {
1203 * Buffer too small. Count needed size
1204 * and return with error.
1213 dlen = ALIGN(len) - len;
1222 /* fill header iff buffer is large enough */
1223 rtm->rtm_version = RTM_VERSION;
1224 rtm->rtm_type = type;
1225 rtm->rtm_msglen = len;
1230 if (w != NULL && cp == NULL)
1237 * This routine is called to generate a message from the routing
1238 * socket indicating that a redirect has occurred, a routing lookup
1239 * has failed, or that a protocol has detected timeouts to a particular
1243 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1246 struct rt_msghdr *rtm;
1248 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1250 if (V_route_cb.any_count == 0)
1252 m = rtsock_msg_mbuf(type, rtinfo);
1256 if (fibnum != RT_ALL_FIBS) {
1257 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1258 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1259 M_SETFIB(m, fibnum);
1260 m->m_flags |= RTS_FILTER_FIB;
1263 rtm = mtod(m, struct rt_msghdr *);
1264 rtm->rtm_flags = RTF_DONE | flags;
1265 rtm->rtm_errno = error;
1266 rtm->rtm_addrs = rtinfo->rti_addrs;
1267 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1271 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1274 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1278 * This routine is called to generate a message from the routing
1279 * socket indicating that the status of a network interface has changed.
1282 rt_ifmsg(struct ifnet *ifp)
1284 struct if_msghdr *ifm;
1286 struct rt_addrinfo info;
1288 if (V_route_cb.any_count == 0)
1290 bzero((caddr_t)&info, sizeof(info));
1291 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1294 ifm = mtod(m, struct if_msghdr *);
1295 ifm->ifm_index = ifp->if_index;
1296 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1297 if_data_copy(ifp, &ifm->ifm_data);
1299 rt_dispatch(m, AF_UNSPEC);
1303 * Announce interface address arrival/withdraw.
1304 * Please do not call directly, use rt_addrmsg().
1305 * Assume input data to be valid.
1306 * Returns 0 on success.
1309 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1311 struct rt_addrinfo info;
1312 struct sockaddr *sa;
1315 struct ifa_msghdr *ifam;
1316 struct ifnet *ifp = ifa->ifa_ifp;
1317 struct sockaddr_storage ss;
1319 if (V_route_cb.any_count == 0)
1322 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1324 bzero((caddr_t)&info, sizeof(info));
1325 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1326 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1327 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1328 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1329 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1330 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1332 ifam = mtod(m, struct ifa_msghdr *);
1333 ifam->ifam_index = ifp->if_index;
1334 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1335 ifam->ifam_flags = ifa->ifa_flags;
1336 ifam->ifam_addrs = info.rti_addrs;
1338 if (fibnum != RT_ALL_FIBS) {
1339 M_SETFIB(m, fibnum);
1340 m->m_flags |= RTS_FILTER_FIB;
1343 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1349 * Announce route addition/removal.
1350 * Please do not call directly, use rt_routemsg().
1351 * Note that @rt data MAY be inconsistent/invalid:
1352 * if some userland app sends us "invalid" route message (invalid mask,
1353 * no dst, wrong address families, etc...) we need to pass it back
1354 * to app (and any other rtsock consumers) with rtm_errno field set to
1357 * Returns 0 on success.
1360 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1363 struct rt_addrinfo info;
1364 struct sockaddr *sa;
1366 struct rt_msghdr *rtm;
1367 struct sockaddr_storage ss;
1369 if (V_route_cb.any_count == 0)
1372 bzero((caddr_t)&info, sizeof(info));
1373 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1374 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1375 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1376 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1378 rtm = mtod(m, struct rt_msghdr *);
1379 rtm->rtm_index = ifp->if_index;
1380 rtm->rtm_flags |= rt->rt_flags;
1381 rtm->rtm_errno = error;
1382 rtm->rtm_addrs = info.rti_addrs;
1384 if (fibnum != RT_ALL_FIBS) {
1385 M_SETFIB(m, fibnum);
1386 m->m_flags |= RTS_FILTER_FIB;
1389 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1395 * This is the analogue to the rt_newaddrmsg which performs the same
1396 * function but for multicast group memberhips. This is easier since
1397 * there is no route state to worry about.
1400 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1402 struct rt_addrinfo info;
1403 struct mbuf *m = NULL;
1404 struct ifnet *ifp = ifma->ifma_ifp;
1405 struct ifma_msghdr *ifmam;
1407 if (V_route_cb.any_count == 0)
1410 bzero((caddr_t)&info, sizeof(info));
1411 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1412 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1414 * If a link-layer address is present, present it as a ``gateway''
1415 * (similarly to how ARP entries, e.g., are presented).
1417 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1418 m = rtsock_msg_mbuf(cmd, &info);
1421 ifmam = mtod(m, struct ifma_msghdr *);
1422 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1424 ifmam->ifmam_index = ifp->if_index;
1425 ifmam->ifmam_addrs = info.rti_addrs;
1426 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1429 static struct mbuf *
1430 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1431 struct rt_addrinfo *info)
1433 struct if_announcemsghdr *ifan;
1436 if (V_route_cb.any_count == 0)
1438 bzero((caddr_t)info, sizeof(*info));
1439 m = rtsock_msg_mbuf(type, info);
1441 ifan = mtod(m, struct if_announcemsghdr *);
1442 ifan->ifan_index = ifp->if_index;
1443 strlcpy(ifan->ifan_name, ifp->if_xname,
1444 sizeof(ifan->ifan_name));
1445 ifan->ifan_what = what;
1451 * This is called to generate routing socket messages indicating
1452 * IEEE80211 wireless events.
1453 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1456 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1459 struct rt_addrinfo info;
1461 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1464 * Append the ieee80211 data. Try to stick it in the
1465 * mbuf containing the ifannounce msg; otherwise allocate
1466 * a new mbuf and append.
1468 * NB: we assume m is a single mbuf.
1470 if (data_len > M_TRAILINGSPACE(m)) {
1471 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1476 bcopy(data, mtod(n, void *), data_len);
1477 n->m_len = data_len;
1479 } else if (data_len > 0) {
1480 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1481 m->m_len += data_len;
1483 if (m->m_flags & M_PKTHDR)
1484 m->m_pkthdr.len += data_len;
1485 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1486 rt_dispatch(m, AF_UNSPEC);
1491 * This is called to generate routing socket messages indicating
1492 * network interface arrival and departure.
1495 rt_ifannouncemsg(struct ifnet *ifp, int what)
1498 struct rt_addrinfo info;
1500 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1502 rt_dispatch(m, AF_UNSPEC);
1506 rt_dispatch(struct mbuf *m, sa_family_t saf)
1511 * Preserve the family from the sockaddr, if any, in an m_tag for
1512 * use when injecting the mbuf into the routing socket buffer from
1515 if (saf != AF_UNSPEC) {
1516 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1522 *(unsigned short *)(tag + 1) = saf;
1523 m_tag_prepend(m, tag);
1527 m->m_pkthdr.rcvif = V_loif;
1533 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1537 * This is used in dumping the kernel table via sysctl().
1540 sysctl_dumpentry(struct radix_node *rn, void *vw)
1542 struct walkarg *w = vw;
1543 struct rtentry *rt = (struct rtentry *)rn;
1544 int error = 0, size;
1545 struct rt_addrinfo info;
1546 struct sockaddr_storage ss;
1548 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1550 if ((rt->rt_flags & RTF_HOST) == 0
1551 ? jailed_without_vnet(w->w_req->td->td_ucred)
1552 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1554 bzero((caddr_t)&info, sizeof(info));
1555 info.rti_info[RTAX_DST] = rt_key(rt);
1556 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1557 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1559 info.rti_info[RTAX_GENMASK] = 0;
1561 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1562 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1563 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1564 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1566 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1568 if (w->w_req && w->w_tmem) {
1569 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1571 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1572 rtm->rtm_flags = RTF_GATEWAY |
1573 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1575 rtm->rtm_flags = rt->rt_flags;
1576 rt_getmetrics(rt, &rtm->rtm_rmx);
1577 rtm->rtm_index = rt->rt_ifp->if_index;
1578 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1579 rtm->rtm_addrs = info.rti_addrs;
1580 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1587 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1588 struct rt_addrinfo *info, struct walkarg *w, int len)
1590 struct if_msghdrl *ifm;
1591 struct if_data *ifd;
1593 ifm = (struct if_msghdrl *)w->w_tmem;
1595 #ifdef COMPAT_FREEBSD32
1596 if (w->w_req->flags & SCTL_MASK32) {
1597 struct if_msghdrl32 *ifm32;
1599 ifm32 = (struct if_msghdrl32 *)ifm;
1600 ifm32->ifm_addrs = info->rti_addrs;
1601 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1602 ifm32->ifm_index = ifp->if_index;
1603 ifm32->_ifm_spare1 = 0;
1604 ifm32->ifm_len = sizeof(*ifm32);
1605 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1606 ifd = &ifm32->ifm_data;
1610 ifm->ifm_addrs = info->rti_addrs;
1611 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1612 ifm->ifm_index = ifp->if_index;
1613 ifm->_ifm_spare1 = 0;
1614 ifm->ifm_len = sizeof(*ifm);
1615 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1616 ifd = &ifm->ifm_data;
1619 memcpy(ifd, src_ifd, sizeof(*ifd));
1621 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1625 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1626 struct rt_addrinfo *info, struct walkarg *w, int len)
1628 struct if_msghdr *ifm;
1629 struct if_data *ifd;
1631 ifm = (struct if_msghdr *)w->w_tmem;
1633 #ifdef COMPAT_FREEBSD32
1634 if (w->w_req->flags & SCTL_MASK32) {
1635 struct if_msghdr32 *ifm32;
1637 ifm32 = (struct if_msghdr32 *)ifm;
1638 ifm32->ifm_addrs = info->rti_addrs;
1639 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1640 ifm32->ifm_index = ifp->if_index;
1641 ifd = &ifm32->ifm_data;
1645 ifm->ifm_addrs = info->rti_addrs;
1646 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1647 ifm->ifm_index = ifp->if_index;
1648 ifd = &ifm->ifm_data;
1651 memcpy(ifd, src_ifd, sizeof(*ifd));
1653 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1657 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1658 struct walkarg *w, int len)
1660 struct ifa_msghdrl *ifam;
1661 struct if_data *ifd;
1663 ifam = (struct ifa_msghdrl *)w->w_tmem;
1665 #ifdef COMPAT_FREEBSD32
1666 if (w->w_req->flags & SCTL_MASK32) {
1667 struct ifa_msghdrl32 *ifam32;
1669 ifam32 = (struct ifa_msghdrl32 *)ifam;
1670 ifam32->ifam_addrs = info->rti_addrs;
1671 ifam32->ifam_flags = ifa->ifa_flags;
1672 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1673 ifam32->_ifam_spare1 = 0;
1674 ifam32->ifam_len = sizeof(*ifam32);
1675 ifam32->ifam_data_off =
1676 offsetof(struct ifa_msghdrl32, ifam_data);
1677 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1678 ifd = &ifam32->ifam_data;
1682 ifam->ifam_addrs = info->rti_addrs;
1683 ifam->ifam_flags = ifa->ifa_flags;
1684 ifam->ifam_index = ifa->ifa_ifp->if_index;
1685 ifam->_ifam_spare1 = 0;
1686 ifam->ifam_len = sizeof(*ifam);
1687 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1688 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1689 ifd = &ifam->ifam_data;
1692 bzero(ifd, sizeof(*ifd));
1693 ifd->ifi_datalen = sizeof(struct if_data);
1694 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1695 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1696 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1697 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1699 /* Fixup if_data carp(4) vhid. */
1700 if (carp_get_vhid_p != NULL)
1701 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1703 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1707 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1708 struct walkarg *w, int len)
1710 struct ifa_msghdr *ifam;
1712 ifam = (struct ifa_msghdr *)w->w_tmem;
1713 ifam->ifam_addrs = info->rti_addrs;
1714 ifam->ifam_flags = ifa->ifa_flags;
1715 ifam->ifam_index = ifa->ifa_ifp->if_index;
1716 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1718 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1722 sysctl_iflist(int af, struct walkarg *w)
1727 struct rt_addrinfo info;
1729 struct sockaddr_storage ss;
1731 bzero((caddr_t)&info, sizeof(info));
1732 bzero(&ifd, sizeof(ifd));
1733 IFNET_RLOCK_NOSLEEP();
1734 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1735 if (w->w_arg && w->w_arg != ifp->if_index)
1737 if_data_copy(ifp, &ifd);
1740 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1741 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1744 info.rti_info[RTAX_IFP] = NULL;
1745 if (w->w_req && w->w_tmem) {
1746 if (w->w_op == NET_RT_IFLISTL)
1747 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1750 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1755 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1756 if (af && af != ifa->ifa_addr->sa_family)
1758 if (prison_if(w->w_req->td->td_ucred,
1759 ifa->ifa_addr) != 0)
1761 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1762 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1763 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1764 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1765 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1768 if (w->w_req && w->w_tmem) {
1769 if (w->w_op == NET_RT_IFLISTL)
1770 error = sysctl_iflist_ifaml(ifa, &info,
1773 error = sysctl_iflist_ifam(ifa, &info,
1779 IF_ADDR_RUNLOCK(ifp);
1780 info.rti_info[RTAX_IFA] = NULL;
1781 info.rti_info[RTAX_NETMASK] = NULL;
1782 info.rti_info[RTAX_BRD] = NULL;
1786 IF_ADDR_RUNLOCK(ifp);
1787 IFNET_RUNLOCK_NOSLEEP();
1792 sysctl_ifmalist(int af, struct walkarg *w)
1794 struct rt_addrinfo info;
1796 struct ifmultiaddr *ifma;
1801 bzero((caddr_t)&info, sizeof(info));
1803 IFNET_RLOCK_NOSLEEP();
1804 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1805 if (w->w_arg && w->w_arg != ifp->if_index)
1808 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1810 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1811 if (af && af != ifma->ifma_addr->sa_family)
1813 if (prison_if(w->w_req->td->td_ucred,
1814 ifma->ifma_addr) != 0)
1816 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1817 info.rti_info[RTAX_GATEWAY] =
1818 (ifma->ifma_addr->sa_family != AF_LINK) ?
1819 ifma->ifma_lladdr : NULL;
1820 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1823 if (w->w_req && w->w_tmem) {
1824 struct ifma_msghdr *ifmam;
1826 ifmam = (struct ifma_msghdr *)w->w_tmem;
1827 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1828 ifmam->ifmam_flags = 0;
1829 ifmam->ifmam_addrs = info.rti_addrs;
1830 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1835 IF_ADDR_RUNLOCK(ifp);
1839 IFNET_RUNLOCK_NOSLEEP();
1844 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1846 int *name = (int *)arg1;
1847 u_int namelen = arg2;
1848 struct rib_head *rnh = NULL; /* silence compiler. */
1849 int i, lim, error = EINVAL;
1858 if (name[1] == NET_RT_DUMP) {
1860 fib = req->td->td_proc->p_fibnum;
1861 else if (namelen == 4)
1862 fib = (name[3] == RT_ALL_FIBS) ?
1863 req->td->td_proc->p_fibnum : name[3];
1865 return ((namelen < 3) ? EISDIR : ENOTDIR);
1866 if (fib < 0 || fib >= rt_numfibs)
1868 } else if (namelen != 3)
1869 return ((namelen < 3) ? EISDIR : ENOTDIR);
1873 bzero(&w, sizeof(w));
1878 error = sysctl_wire_old_buffer(req, 0);
1883 * Allocate reply buffer in advance.
1884 * All rtsock messages has maximum length of u_short.
1886 w.w_tmemsize = 65536;
1887 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1893 if (af == 0) { /* dump all tables */
1896 } else /* dump only one table */
1900 * take care of llinfo entries, the caller must
1903 if (w.w_op == NET_RT_FLAGS &&
1904 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1906 error = lltable_sysctl_dumparp(af, w.w_req);
1912 * take care of routing entries
1914 for (error = 0; error == 0 && i <= lim; i++) {
1915 rnh = rt_tables_get_rnh(fib, i);
1918 error = rnh->rnh_walktree(&rnh->head,
1919 sysctl_dumpentry, &w);
1922 error = EAFNOSUPPORT;
1927 case NET_RT_IFLISTL:
1928 error = sysctl_iflist(af, &w);
1931 case NET_RT_IFMALIST:
1932 error = sysctl_ifmalist(af, &w);
1936 free(w.w_tmem, M_TEMP);
1940 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1943 * Definitions of protocols supported in the ROUTE domain.
1946 static struct domain routedomain; /* or at least forward */
1948 static struct protosw routesw[] = {
1950 .pr_type = SOCK_RAW,
1951 .pr_domain = &routedomain,
1952 .pr_flags = PR_ATOMIC|PR_ADDR,
1953 .pr_output = route_output,
1954 .pr_ctlinput = raw_ctlinput,
1955 .pr_init = raw_init,
1956 .pr_usrreqs = &route_usrreqs
1960 static struct domain routedomain = {
1961 .dom_family = PF_ROUTE,
1962 .dom_name = "route",
1963 .dom_protosw = routesw,
1964 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1967 VNET_DOMAIN_SET(route);