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 if (rtm->rtm_flags & RTF_RNH_LOCKED)
620 info.rti_flags = rtm->rtm_flags;
621 if (info.rti_info[RTAX_DST] == NULL ||
622 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
623 (info.rti_info[RTAX_GATEWAY] != NULL &&
624 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
626 saf = info.rti_info[RTAX_DST]->sa_family;
628 * Verify that the caller has the appropriate privilege; RTM_GET
629 * is the only operation the non-superuser is allowed.
631 if (rtm->rtm_type != RTM_GET) {
632 error = priv_check(curthread, PRIV_NET_ROUTE);
638 * The given gateway address may be an interface address.
639 * For example, issuing a "route change" command on a route
640 * entry that was created from a tunnel, and the gateway
641 * address given is the local end point. In this case the
642 * RTF_GATEWAY flag must be cleared or the destination will
643 * not be reachable even though there is no error message.
645 if (info.rti_info[RTAX_GATEWAY] != NULL &&
646 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
647 struct rt_addrinfo ginfo;
648 struct sockaddr *gdst;
650 bzero(&ginfo, sizeof(ginfo));
651 bzero(&ss, sizeof(ss));
652 ss.ss_len = sizeof(ss);
654 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
655 gdst = info.rti_info[RTAX_GATEWAY];
658 * A host route through the loopback interface is
659 * installed for each interface adddress. In pre 8.0
660 * releases the interface address of a PPP link type
661 * is not reachable locally. This behavior is fixed as
662 * part of the new L2/L3 redesign and rewrite work. The
663 * signature of this interface address route is the
664 * AF_LINK sa_family type of the rt_gateway, and the
665 * rt_ifp has the IFF_LOOPBACK flag set.
667 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
668 if (ss.ss_family == AF_LINK &&
669 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
670 info.rti_flags &= ~RTF_GATEWAY;
671 info.rti_flags |= RTF_GWFLAG_COMPAT;
673 rib_free_info(&ginfo);
677 switch (rtm->rtm_type) {
678 struct rtentry *saved_nrt;
682 if (rtm->rtm_type == RTM_ADD) {
683 if (info.rti_info[RTAX_GATEWAY] == NULL)
688 /* support for new ARP code */
689 if (info.rti_info[RTAX_GATEWAY] != NULL &&
690 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
691 (rtm->rtm_flags & RTF_LLDATA) != 0) {
692 error = lla_rt_output(rtm, &info);
695 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
699 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
701 if (error == 0 && saved_nrt != NULL) {
703 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
706 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
707 RT_REMREF(saved_nrt);
708 RT_UNLOCK(saved_nrt);
714 /* support for new ARP code */
715 if (info.rti_info[RTAX_GATEWAY] &&
716 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
717 (rtm->rtm_flags & RTF_LLDATA) != 0) {
718 error = lla_rt_output(rtm, &info);
721 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
725 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
732 /* rt_msg2() will not be used when RTM_DELETE fails. */
733 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
738 rnh = rt_tables_get_rnh(fibnum, saf);
740 senderr(EAFNOSUPPORT);
744 if (info.rti_info[RTAX_NETMASK] == NULL &&
745 rtm->rtm_type == RTM_GET) {
747 * Provide longest prefix match for
748 * address lookup (no mask).
749 * 'route -n get addr'
751 rt = (struct rtentry *) rnh->rnh_matchaddr(
752 info.rti_info[RTAX_DST], &rnh->head);
754 rt = (struct rtentry *) rnh->rnh_lookup(
755 info.rti_info[RTAX_DST],
756 info.rti_info[RTAX_NETMASK], &rnh->head);
764 * for RTM_CHANGE/LOCK, if we got multipath routes,
765 * we require users to specify a matching RTAX_GATEWAY.
767 * for RTM_GET, gate is optional even with multipath.
768 * if gate == NULL the first match is returned.
769 * (no need to call rt_mpath_matchgate if gate == NULL)
771 if (rt_mpath_capable(rnh) &&
772 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
773 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
781 * If performing proxied L2 entry insertion, and
782 * the actual PPP host entry is found, perform
783 * another search to retrieve the prefix route of
784 * the local end point of the PPP link.
786 if (rtm->rtm_flags & RTF_ANNOUNCE) {
787 struct sockaddr laddr;
789 if (rt->rt_ifp != NULL &&
790 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
794 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
797 rt_maskedcopy(ifa->ifa_addr,
802 rt_maskedcopy(rt->rt_ifa->ifa_addr,
804 rt->rt_ifa->ifa_netmask);
806 * refactor rt and no lock operation necessary
808 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
821 if ((rt->rt_flags & RTF_HOST) == 0
822 ? jailed_without_vnet(curthread->td_ucred)
823 : prison_if(curthread->td_ucred,
828 info.rti_info[RTAX_DST] = rt_key(rt);
829 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
830 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
832 info.rti_info[RTAX_GENMASK] = 0;
833 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
836 info.rti_info[RTAX_IFP] =
837 ifp->if_addr->ifa_addr;
838 error = rtm_get_jailed(&info, ifp, rt,
839 &saun, curthread->td_ucred);
844 if (ifp->if_flags & IFF_POINTOPOINT)
845 info.rti_info[RTAX_BRD] =
846 rt->rt_ifa->ifa_dstaddr;
847 rtm->rtm_index = ifp->if_index;
849 info.rti_info[RTAX_IFP] = NULL;
850 info.rti_info[RTAX_IFA] = NULL;
852 } else if ((ifp = rt->rt_ifp) != NULL) {
853 rtm->rtm_index = ifp->if_index;
856 /* Check if we need to realloc storage */
857 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
858 if (len > alloc_len) {
859 struct rt_msghdr *new_rtm;
860 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
861 if (new_rtm == NULL) {
865 bcopy(rtm, new_rtm, rtm->rtm_msglen);
871 w.w_tmem = (caddr_t)rtm;
872 w.w_tmemsize = alloc_len;
873 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
875 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
876 rtm->rtm_flags = RTF_GATEWAY |
877 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
879 rtm->rtm_flags = rt->rt_flags;
880 rt_getmetrics(rt, &rtm->rtm_rmx);
881 rtm->rtm_addrs = info.rti_addrs;
894 * Check to see if we don't want our own messages.
896 if ((so->so_options & SO_USELOOPBACK) == 0) {
897 if (V_route_cb.any_count <= 1) {
903 /* There is another listener, so construct message */
909 if (rti_need_deembed) {
910 /* sin6_scope_id is recovered before sending rtm. */
911 sin6 = (struct sockaddr_in6 *)&ss;
912 for (i = 0; i < RTAX_MAX; i++) {
913 if (info.rti_info[i] == NULL)
915 if (info.rti_info[i]->sa_family != AF_INET6)
917 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
918 if (sa6_recoverscope(sin6) == 0)
919 bcopy(sin6, info.rti_info[i],
925 rtm->rtm_errno = error;
927 rtm->rtm_flags |= RTF_DONE;
929 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
930 if (m->m_pkthdr.len < rtm->rtm_msglen) {
933 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
934 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
940 m->m_flags |= RTS_FILTER_FIB;
943 * XXX insure we don't get a copy by
944 * invalidating our protocol
946 unsigned short family = rp->rcb_proto.sp_family;
947 rp->rcb_proto.sp_family = 0;
949 rp->rcb_proto.sp_family = family;
958 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
961 bzero(out, sizeof(*out));
962 out->rmx_mtu = rt->rt_mtu;
963 out->rmx_weight = rt->rt_weight;
964 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
965 /* Kernel -> userland timebase conversion. */
966 out->rmx_expire = rt->rt_expire ?
967 rt->rt_expire - time_uptime + time_second : 0;
971 * Extract the addresses of the passed sockaddrs.
972 * Do a little sanity checking so as to avoid bad memory references.
973 * This data is derived straight from userland.
976 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
981 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
982 if ((rtinfo->rti_addrs & (1 << i)) == 0)
984 sa = (struct sockaddr *)cp;
988 if (cp + sa->sa_len > cplim)
991 * there are no more.. quit now
992 * If there are more bits, they are in error.
993 * I've seen this. route(1) can evidently generate these.
994 * This causes kernel to core dump.
995 * for compatibility, If we see this, point to a safe address.
997 if (sa->sa_len == 0) {
998 rtinfo->rti_info[i] = &sa_zero;
999 return (0); /* should be EINVAL but for compat */
1003 if (sa->sa_family == AF_INET6)
1004 sa6_embedscope((struct sockaddr_in6 *)sa,
1007 rtinfo->rti_info[i] = sa;
1014 * Fill in @dmask with valid netmask leaving original @smask
1015 * intact. Mostly used with radix netmasks.
1017 static struct sockaddr *
1018 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1019 struct sockaddr_storage *dmask)
1021 if (dst == NULL || smask == NULL)
1024 memset(dmask, 0, dst->sa_len);
1025 memcpy(dmask, smask, smask->sa_len);
1026 dmask->ss_len = dst->sa_len;
1027 dmask->ss_family = dst->sa_family;
1029 return ((struct sockaddr *)dmask);
1033 * Writes information related to @rtinfo object to newly-allocated mbuf.
1034 * Assumes MCLBYTES is enough to construct any message.
1035 * Used for OS notifications of vaious events (if/ifa announces,etc)
1037 * Returns allocated mbuf or NULL on failure.
1039 static struct mbuf *
1040 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1042 struct rt_msghdr *rtm;
1045 struct sockaddr *sa;
1047 struct sockaddr_storage ss;
1048 struct sockaddr_in6 *sin6;
1056 len = sizeof(struct ifa_msghdr);
1061 len = sizeof(struct ifma_msghdr);
1065 len = sizeof(struct if_msghdr);
1068 case RTM_IFANNOUNCE:
1070 len = sizeof(struct if_announcemsghdr);
1074 len = sizeof(struct rt_msghdr);
1077 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1078 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1080 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1082 m = m_gethdr(M_NOWAIT, MT_DATA);
1086 m->m_pkthdr.len = m->m_len = len;
1087 rtm = mtod(m, struct rt_msghdr *);
1088 bzero((caddr_t)rtm, len);
1089 for (i = 0; i < RTAX_MAX; i++) {
1090 if ((sa = rtinfo->rti_info[i]) == NULL)
1092 rtinfo->rti_addrs |= (1 << i);
1095 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1096 sin6 = (struct sockaddr_in6 *)&ss;
1097 bcopy(sa, sin6, sizeof(*sin6));
1098 if (sa6_recoverscope(sin6) == 0)
1099 sa = (struct sockaddr *)sin6;
1102 m_copyback(m, len, dlen, (caddr_t)sa);
1105 if (m->m_pkthdr.len != len) {
1109 rtm->rtm_msglen = len;
1110 rtm->rtm_version = RTM_VERSION;
1111 rtm->rtm_type = type;
1116 * Writes information related to @rtinfo object to preallocated buffer.
1117 * Stores needed size in @plen. If @w is NULL, calculates size without
1119 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1121 * Returns 0 on success.
1125 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1128 int len, buflen = 0, dlen;
1130 struct rt_msghdr *rtm = NULL;
1132 struct sockaddr_storage ss;
1133 struct sockaddr_in6 *sin6;
1135 #ifdef COMPAT_FREEBSD32
1136 bool compat32 = false;
1143 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1144 #ifdef COMPAT_FREEBSD32
1145 if (w->w_req->flags & SCTL_MASK32) {
1146 len = sizeof(struct ifa_msghdrl32);
1150 len = sizeof(struct ifa_msghdrl);
1152 len = sizeof(struct ifa_msghdr);
1156 #ifdef COMPAT_FREEBSD32
1157 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1158 if (w->w_op == NET_RT_IFLISTL)
1159 len = sizeof(struct if_msghdrl32);
1161 len = sizeof(struct if_msghdr32);
1166 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1167 len = sizeof(struct if_msghdrl);
1169 len = sizeof(struct if_msghdr);
1173 len = sizeof(struct ifma_msghdr);
1177 len = sizeof(struct rt_msghdr);
1181 rtm = (struct rt_msghdr *)w->w_tmem;
1182 buflen = w->w_tmemsize - len;
1183 cp = (caddr_t)w->w_tmem + len;
1186 rtinfo->rti_addrs = 0;
1187 for (i = 0; i < RTAX_MAX; i++) {
1188 struct sockaddr *sa;
1190 if ((sa = rtinfo->rti_info[i]) == NULL)
1192 rtinfo->rti_addrs |= (1 << i);
1193 #ifdef COMPAT_FREEBSD32
1195 dlen = SA_SIZE32(sa);
1199 if (cp != NULL && buflen >= dlen) {
1201 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1202 sin6 = (struct sockaddr_in6 *)&ss;
1203 bcopy(sa, sin6, sizeof(*sin6));
1204 if (sa6_recoverscope(sin6) == 0)
1205 sa = (struct sockaddr *)sin6;
1208 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1211 } else if (cp != NULL) {
1213 * Buffer too small. Count needed size
1214 * and return with error.
1223 dlen = ALIGN(len) - len;
1235 /* fill header iff buffer is large enough */
1236 rtm->rtm_version = RTM_VERSION;
1237 rtm->rtm_type = type;
1238 rtm->rtm_msglen = len;
1243 if (w != NULL && cp == NULL)
1250 * This routine is called to generate a message from the routing
1251 * socket indicating that a redirect has occurred, a routing lookup
1252 * has failed, or that a protocol has detected timeouts to a particular
1256 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1259 struct rt_msghdr *rtm;
1261 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1263 if (V_route_cb.any_count == 0)
1265 m = rtsock_msg_mbuf(type, rtinfo);
1269 if (fibnum != RT_ALL_FIBS) {
1270 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1271 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1272 M_SETFIB(m, fibnum);
1273 m->m_flags |= RTS_FILTER_FIB;
1276 rtm = mtod(m, struct rt_msghdr *);
1277 rtm->rtm_flags = RTF_DONE | flags;
1278 rtm->rtm_errno = error;
1279 rtm->rtm_addrs = rtinfo->rti_addrs;
1280 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1284 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1287 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1291 * This routine is called to generate a message from the routing
1292 * socket indicating that the status of a network interface has changed.
1295 rt_ifmsg(struct ifnet *ifp)
1297 struct if_msghdr *ifm;
1299 struct rt_addrinfo info;
1301 if (V_route_cb.any_count == 0)
1303 bzero((caddr_t)&info, sizeof(info));
1304 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1307 ifm = mtod(m, struct if_msghdr *);
1308 ifm->ifm_index = ifp->if_index;
1309 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1310 if_data_copy(ifp, &ifm->ifm_data);
1312 rt_dispatch(m, AF_UNSPEC);
1316 * Announce interface address arrival/withdraw.
1317 * Please do not call directly, use rt_addrmsg().
1318 * Assume input data to be valid.
1319 * Returns 0 on success.
1322 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1324 struct rt_addrinfo info;
1325 struct sockaddr *sa;
1328 struct ifa_msghdr *ifam;
1329 struct ifnet *ifp = ifa->ifa_ifp;
1330 struct sockaddr_storage ss;
1332 if (V_route_cb.any_count == 0)
1335 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1337 bzero((caddr_t)&info, sizeof(info));
1338 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1339 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1340 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1341 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1342 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1343 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1345 ifam = mtod(m, struct ifa_msghdr *);
1346 ifam->ifam_index = ifp->if_index;
1347 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1348 ifam->ifam_flags = ifa->ifa_flags;
1349 ifam->ifam_addrs = info.rti_addrs;
1351 if (fibnum != RT_ALL_FIBS) {
1352 M_SETFIB(m, fibnum);
1353 m->m_flags |= RTS_FILTER_FIB;
1356 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1362 * Announce route addition/removal.
1363 * Please do not call directly, use rt_routemsg().
1364 * Note that @rt data MAY be inconsistent/invalid:
1365 * if some userland app sends us "invalid" route message (invalid mask,
1366 * no dst, wrong address families, etc...) we need to pass it back
1367 * to app (and any other rtsock consumers) with rtm_errno field set to
1370 * Returns 0 on success.
1373 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1376 struct rt_addrinfo info;
1377 struct sockaddr *sa;
1379 struct rt_msghdr *rtm;
1380 struct sockaddr_storage ss;
1382 if (V_route_cb.any_count == 0)
1385 bzero((caddr_t)&info, sizeof(info));
1386 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1387 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1388 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1389 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1391 rtm = mtod(m, struct rt_msghdr *);
1392 rtm->rtm_index = ifp->if_index;
1393 rtm->rtm_flags |= rt->rt_flags;
1394 rtm->rtm_errno = error;
1395 rtm->rtm_addrs = info.rti_addrs;
1397 if (fibnum != RT_ALL_FIBS) {
1398 M_SETFIB(m, fibnum);
1399 m->m_flags |= RTS_FILTER_FIB;
1402 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1408 * This is the analogue to the rt_newaddrmsg which performs the same
1409 * function but for multicast group memberhips. This is easier since
1410 * there is no route state to worry about.
1413 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1415 struct rt_addrinfo info;
1416 struct mbuf *m = NULL;
1417 struct ifnet *ifp = ifma->ifma_ifp;
1418 struct ifma_msghdr *ifmam;
1420 if (V_route_cb.any_count == 0)
1423 bzero((caddr_t)&info, sizeof(info));
1424 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1425 if (ifp && ifp->if_addr)
1426 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1428 info.rti_info[RTAX_IFP] = NULL;
1430 * If a link-layer address is present, present it as a ``gateway''
1431 * (similarly to how ARP entries, e.g., are presented).
1433 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1434 m = rtsock_msg_mbuf(cmd, &info);
1437 ifmam = mtod(m, struct ifma_msghdr *);
1438 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1440 ifmam->ifmam_index = ifp->if_index;
1441 ifmam->ifmam_addrs = info.rti_addrs;
1442 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1445 static struct mbuf *
1446 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1447 struct rt_addrinfo *info)
1449 struct if_announcemsghdr *ifan;
1452 if (V_route_cb.any_count == 0)
1454 bzero((caddr_t)info, sizeof(*info));
1455 m = rtsock_msg_mbuf(type, info);
1457 ifan = mtod(m, struct if_announcemsghdr *);
1458 ifan->ifan_index = ifp->if_index;
1459 strlcpy(ifan->ifan_name, ifp->if_xname,
1460 sizeof(ifan->ifan_name));
1461 ifan->ifan_what = what;
1467 * This is called to generate routing socket messages indicating
1468 * IEEE80211 wireless events.
1469 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1472 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1475 struct rt_addrinfo info;
1477 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1480 * Append the ieee80211 data. Try to stick it in the
1481 * mbuf containing the ifannounce msg; otherwise allocate
1482 * a new mbuf and append.
1484 * NB: we assume m is a single mbuf.
1486 if (data_len > M_TRAILINGSPACE(m)) {
1487 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1492 bcopy(data, mtod(n, void *), data_len);
1493 n->m_len = data_len;
1495 } else if (data_len > 0) {
1496 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1497 m->m_len += data_len;
1499 if (m->m_flags & M_PKTHDR)
1500 m->m_pkthdr.len += data_len;
1501 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1502 rt_dispatch(m, AF_UNSPEC);
1507 * This is called to generate routing socket messages indicating
1508 * network interface arrival and departure.
1511 rt_ifannouncemsg(struct ifnet *ifp, int what)
1514 struct rt_addrinfo info;
1516 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1518 rt_dispatch(m, AF_UNSPEC);
1522 rt_dispatch(struct mbuf *m, sa_family_t saf)
1527 * Preserve the family from the sockaddr, if any, in an m_tag for
1528 * use when injecting the mbuf into the routing socket buffer from
1531 if (saf != AF_UNSPEC) {
1532 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1538 *(unsigned short *)(tag + 1) = saf;
1539 m_tag_prepend(m, tag);
1543 m->m_pkthdr.rcvif = V_loif;
1549 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1553 * This is used in dumping the kernel table via sysctl().
1556 sysctl_dumpentry(struct radix_node *rn, void *vw)
1558 struct walkarg *w = vw;
1559 struct rtentry *rt = (struct rtentry *)rn;
1560 int error = 0, size;
1561 struct rt_addrinfo info;
1562 struct sockaddr_storage ss;
1564 IFNET_RLOCK_NOSLEEP_ASSERT();
1566 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1568 if ((rt->rt_flags & RTF_HOST) == 0
1569 ? jailed_without_vnet(w->w_req->td->td_ucred)
1570 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1572 bzero((caddr_t)&info, sizeof(info));
1573 info.rti_info[RTAX_DST] = rt_key(rt);
1574 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1575 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1577 info.rti_info[RTAX_GENMASK] = 0;
1578 if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1579 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1580 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1581 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1582 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1584 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1586 if (w->w_req && w->w_tmem) {
1587 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1589 bzero(&rtm->rtm_index,
1590 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1591 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1592 rtm->rtm_flags = RTF_GATEWAY |
1593 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1595 rtm->rtm_flags = rt->rt_flags;
1596 rt_getmetrics(rt, &rtm->rtm_rmx);
1597 rtm->rtm_index = rt->rt_ifp->if_index;
1598 rtm->rtm_addrs = info.rti_addrs;
1599 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1606 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1607 struct rt_addrinfo *info, struct walkarg *w, int len)
1609 struct if_msghdrl *ifm;
1610 struct if_data *ifd;
1612 ifm = (struct if_msghdrl *)w->w_tmem;
1614 #ifdef COMPAT_FREEBSD32
1615 if (w->w_req->flags & SCTL_MASK32) {
1616 struct if_msghdrl32 *ifm32;
1618 ifm32 = (struct if_msghdrl32 *)ifm;
1619 ifm32->ifm_addrs = info->rti_addrs;
1620 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1621 ifm32->ifm_index = ifp->if_index;
1622 ifm32->_ifm_spare1 = 0;
1623 ifm32->ifm_len = sizeof(*ifm32);
1624 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1625 ifm32->_ifm_spare2 = 0;
1626 ifd = &ifm32->ifm_data;
1630 ifm->ifm_addrs = info->rti_addrs;
1631 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1632 ifm->ifm_index = ifp->if_index;
1633 ifm->_ifm_spare1 = 0;
1634 ifm->ifm_len = sizeof(*ifm);
1635 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1636 ifm->_ifm_spare2 = 0;
1637 ifd = &ifm->ifm_data;
1640 memcpy(ifd, src_ifd, sizeof(*ifd));
1642 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1646 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1647 struct rt_addrinfo *info, struct walkarg *w, int len)
1649 struct if_msghdr *ifm;
1650 struct if_data *ifd;
1652 ifm = (struct if_msghdr *)w->w_tmem;
1654 #ifdef COMPAT_FREEBSD32
1655 if (w->w_req->flags & SCTL_MASK32) {
1656 struct if_msghdr32 *ifm32;
1658 ifm32 = (struct if_msghdr32 *)ifm;
1659 ifm32->ifm_addrs = info->rti_addrs;
1660 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1661 ifm32->ifm_index = ifp->if_index;
1662 ifm32->_ifm_spare1 = 0;
1663 ifd = &ifm32->ifm_data;
1667 ifm->ifm_addrs = info->rti_addrs;
1668 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1669 ifm->ifm_index = ifp->if_index;
1670 ifm->_ifm_spare1 = 0;
1671 ifd = &ifm->ifm_data;
1674 memcpy(ifd, src_ifd, sizeof(*ifd));
1676 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1680 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1681 struct walkarg *w, int len)
1683 struct ifa_msghdrl *ifam;
1684 struct if_data *ifd;
1686 ifam = (struct ifa_msghdrl *)w->w_tmem;
1688 #ifdef COMPAT_FREEBSD32
1689 if (w->w_req->flags & SCTL_MASK32) {
1690 struct ifa_msghdrl32 *ifam32;
1692 ifam32 = (struct ifa_msghdrl32 *)ifam;
1693 ifam32->ifam_addrs = info->rti_addrs;
1694 ifam32->ifam_flags = ifa->ifa_flags;
1695 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1696 ifam32->_ifam_spare1 = 0;
1697 ifam32->ifam_len = sizeof(*ifam32);
1698 ifam32->ifam_data_off =
1699 offsetof(struct ifa_msghdrl32, ifam_data);
1700 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1701 ifd = &ifam32->ifam_data;
1705 ifam->ifam_addrs = info->rti_addrs;
1706 ifam->ifam_flags = ifa->ifa_flags;
1707 ifam->ifam_index = ifa->ifa_ifp->if_index;
1708 ifam->_ifam_spare1 = 0;
1709 ifam->ifam_len = sizeof(*ifam);
1710 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1711 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1712 ifd = &ifam->ifam_data;
1715 bzero(ifd, sizeof(*ifd));
1716 ifd->ifi_datalen = sizeof(struct if_data);
1717 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1718 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1719 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1720 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1722 /* Fixup if_data carp(4) vhid. */
1723 if (carp_get_vhid_p != NULL)
1724 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1726 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1730 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1731 struct walkarg *w, int len)
1733 struct ifa_msghdr *ifam;
1735 ifam = (struct ifa_msghdr *)w->w_tmem;
1736 ifam->ifam_addrs = info->rti_addrs;
1737 ifam->ifam_flags = ifa->ifa_flags;
1738 ifam->ifam_index = ifa->ifa_ifp->if_index;
1739 ifam->_ifam_spare1 = 0;
1740 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1742 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1746 sysctl_iflist(int af, struct walkarg *w)
1751 struct rt_addrinfo info;
1753 struct sockaddr_storage ss;
1754 struct epoch_tracker et;
1756 bzero((caddr_t)&info, sizeof(info));
1757 bzero(&ifd, sizeof(ifd));
1758 NET_EPOCH_ENTER_ET(et);
1759 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1760 if (w->w_arg && w->w_arg != ifp->if_index)
1762 if_data_copy(ifp, &ifd);
1764 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1765 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1768 info.rti_info[RTAX_IFP] = NULL;
1769 if (w->w_req && w->w_tmem) {
1770 if (w->w_op == NET_RT_IFLISTL)
1771 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1774 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1779 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1780 if (af && af != ifa->ifa_addr->sa_family)
1782 if (prison_if(w->w_req->td->td_ucred,
1783 ifa->ifa_addr) != 0)
1785 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1786 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1787 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1788 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1789 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1792 if (w->w_req && w->w_tmem) {
1793 if (w->w_op == NET_RT_IFLISTL)
1794 error = sysctl_iflist_ifaml(ifa, &info,
1797 error = sysctl_iflist_ifam(ifa, &info,
1803 info.rti_info[RTAX_IFA] = NULL;
1804 info.rti_info[RTAX_NETMASK] = NULL;
1805 info.rti_info[RTAX_BRD] = NULL;
1808 NET_EPOCH_EXIT_ET(et);
1813 sysctl_ifmalist(int af, struct walkarg *w)
1815 struct rt_addrinfo info;
1817 struct ifmultiaddr *ifma;
1822 bzero((caddr_t)&info, sizeof(info));
1824 IFNET_RLOCK_NOSLEEP();
1825 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1826 if (w->w_arg && w->w_arg != ifp->if_index)
1829 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1831 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1832 if (af && af != ifma->ifma_addr->sa_family)
1834 if (prison_if(w->w_req->td->td_ucred,
1835 ifma->ifma_addr) != 0)
1837 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1838 info.rti_info[RTAX_GATEWAY] =
1839 (ifma->ifma_addr->sa_family != AF_LINK) ?
1840 ifma->ifma_lladdr : NULL;
1841 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1844 if (w->w_req && w->w_tmem) {
1845 struct ifma_msghdr *ifmam;
1847 ifmam = (struct ifma_msghdr *)w->w_tmem;
1848 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1849 ifmam->ifmam_flags = 0;
1850 ifmam->ifmam_addrs = info.rti_addrs;
1851 ifmam->_ifmam_spare1 = 0;
1852 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1857 IF_ADDR_RUNLOCK(ifp);
1861 IFNET_RUNLOCK_NOSLEEP();
1866 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1869 int *name = (int *)arg1;
1870 u_int namelen = arg2;
1871 struct rib_head *rnh = NULL; /* silence compiler. */
1872 int i, lim, error = EINVAL;
1881 if (name[1] == NET_RT_DUMP) {
1883 fib = req->td->td_proc->p_fibnum;
1884 else if (namelen == 4)
1885 fib = (name[3] == RT_ALL_FIBS) ?
1886 req->td->td_proc->p_fibnum : name[3];
1888 return ((namelen < 3) ? EISDIR : ENOTDIR);
1889 if (fib < 0 || fib >= rt_numfibs)
1891 } else if (namelen != 3)
1892 return ((namelen < 3) ? EISDIR : ENOTDIR);
1896 bzero(&w, sizeof(w));
1901 error = sysctl_wire_old_buffer(req, 0);
1906 * Allocate reply buffer in advance.
1907 * All rtsock messages has maximum length of u_short.
1909 w.w_tmemsize = 65536;
1910 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1916 if (af == 0) { /* dump all tables */
1919 } else /* dump only one table */
1923 * take care of llinfo entries, the caller must
1926 if (w.w_op == NET_RT_FLAGS &&
1927 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1929 error = lltable_sysctl_dumparp(af, w.w_req);
1935 * take care of routing entries
1937 for (error = 0; error == 0 && i <= lim; i++) {
1938 rnh = rt_tables_get_rnh(fib, i);
1941 IFNET_RLOCK_NOSLEEP();
1942 error = rnh->rnh_walktree(&rnh->head,
1943 sysctl_dumpentry, &w);
1944 IFNET_RUNLOCK_NOSLEEP();
1947 error = EAFNOSUPPORT;
1952 case NET_RT_IFLISTL:
1953 error = sysctl_iflist(af, &w);
1956 case NET_RT_IFMALIST:
1957 error = sysctl_ifmalist(af, &w);
1961 free(w.w_tmem, M_TEMP);
1965 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1968 * Definitions of protocols supported in the ROUTE domain.
1971 static struct domain routedomain; /* or at least forward */
1973 static struct protosw routesw[] = {
1975 .pr_type = SOCK_RAW,
1976 .pr_domain = &routedomain,
1977 .pr_flags = PR_ATOMIC|PR_ADDR,
1978 .pr_output = route_output,
1979 .pr_ctlinput = raw_ctlinput,
1980 .pr_init = raw_init,
1981 .pr_usrreqs = &route_usrreqs
1985 static struct domain routedomain = {
1986 .dom_family = PF_ROUTE,
1987 .dom_name = "route",
1988 .dom_protosw = routesw,
1989 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1992 VNET_DOMAIN_SET(route);