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/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
56 #include <net/if_var.h>
57 #include <net/if_dl.h>
58 #include <net/if_llatbl.h>
59 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/raw_cb.h>
62 #include <net/route.h>
63 #include <net/route_var.h>
66 #include <netinet/in.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/ip_carp.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/scope6_var.h>
74 #ifdef COMPAT_FREEBSD32
75 #include <sys/mount.h>
76 #include <compat/freebsd32/freebsd32.h>
85 struct if_data ifm_data;
97 uint16_t ifm_data_off;
98 struct if_data ifm_data;
101 struct ifa_msghdrl32 {
102 uint16_t ifam_msglen;
103 uint8_t ifam_version;
108 uint16_t _ifam_spare1;
110 uint16_t ifam_data_off;
112 struct if_data ifam_data;
115 #define SA_SIZE32(sa) \
116 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
118 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
120 #endif /* COMPAT_FREEBSD32 */
122 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
124 /* NB: these are not modified */
125 static struct sockaddr route_src = { 2, PF_ROUTE, };
126 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
128 /* These are external hooks for CARP. */
129 int (*carp_get_vhid_p)(struct ifaddr *);
132 * Used by rtsock/raw_input callback code to decide whether to filter the update
133 * notification to a socket bound to a particular FIB.
135 #define RTS_FILTER_FIB M_PROTO8
138 int ip_count; /* attached w/ AF_INET */
139 int ip6_count; /* attached w/ AF_INET6 */
140 int any_count; /* total attached */
142 static VNET_DEFINE(route_cb_t, route_cb);
143 #define V_route_cb VNET(route_cb)
145 struct mtx rtsock_mtx;
146 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
148 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
149 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
150 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
152 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
158 struct sysctl_req *w_req;
161 static void rts_input(struct mbuf *m);
162 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
163 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
164 struct walkarg *w, int *plen);
165 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
166 struct rt_addrinfo *rtinfo);
167 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
168 static int sysctl_iflist(int af, struct walkarg *w);
169 static int sysctl_ifmalist(int af, struct walkarg *w);
170 static int route_output(struct mbuf *m, struct socket *so, ...);
171 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
172 static void rt_dispatch(struct mbuf *, sa_family_t);
173 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
174 struct sockaddr *smask, struct sockaddr_storage *dmask);
176 static struct netisr_handler rtsock_nh = {
178 .nh_handler = rts_input,
179 .nh_proto = NETISR_ROUTE,
180 .nh_policy = NETISR_POLICY_SOURCE,
184 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
188 netisr_getqlimit(&rtsock_nh, &qlimit);
189 error = sysctl_handle_int(oidp, &qlimit, 0, req);
190 if (error || !req->newptr)
194 return (netisr_setqlimit(&rtsock_nh, qlimit));
196 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
197 0, 0, sysctl_route_netisr_maxqlen, "I",
198 "maximum routing socket dispatch queue length");
205 if (IS_DEFAULT_VNET(curvnet)) {
206 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
207 rtsock_nh.nh_qlimit = tmp;
208 netisr_register(&rtsock_nh);
212 netisr_register_vnet(&rtsock_nh);
215 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
220 vnet_rts_uninit(void)
223 netisr_unregister_vnet(&rtsock_nh);
225 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
230 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
235 KASSERT(m != NULL, ("%s: m is NULL", __func__));
236 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
237 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
239 /* No filtering requested. */
240 if ((m->m_flags & RTS_FILTER_FIB) == 0)
243 /* Check if it is a rts and the fib matches the one of the socket. */
244 fibnum = M_GETFIB(m);
245 if (proto->sp_family != PF_ROUTE ||
246 rp->rcb_socket == NULL ||
247 rp->rcb_socket->so_fibnum == fibnum)
250 /* Filtering requested and no match, the socket shall be skipped. */
255 rts_input(struct mbuf *m)
257 struct sockproto route_proto;
258 unsigned short *family;
261 route_proto.sp_family = PF_ROUTE;
262 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
264 family = (unsigned short *)(tag + 1);
265 route_proto.sp_protocol = *family;
266 m_tag_delete(m, tag);
268 route_proto.sp_protocol = 0;
270 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
274 * It really doesn't make any sense at all for this code to share much
275 * with raw_usrreq.c, since its functionality is so restricted. XXX
278 rts_abort(struct socket *so)
281 raw_usrreqs.pru_abort(so);
285 rts_close(struct socket *so)
288 raw_usrreqs.pru_close(so);
291 /* pru_accept is EOPNOTSUPP */
294 rts_attach(struct socket *so, int proto, struct thread *td)
299 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
302 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
304 so->so_pcb = (caddr_t)rp;
305 so->so_fibnum = td->td_proc->p_fibnum;
306 error = raw_attach(so, proto);
314 switch(rp->rcb_proto.sp_protocol) {
316 V_route_cb.ip_count++;
319 V_route_cb.ip6_count++;
322 V_route_cb.any_count++;
325 so->so_options |= SO_USELOOPBACK;
330 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
333 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
337 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
340 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
343 /* pru_connect2 is EOPNOTSUPP */
344 /* pru_control is EOPNOTSUPP */
347 rts_detach(struct socket *so)
349 struct rawcb *rp = sotorawcb(so);
351 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
354 switch(rp->rcb_proto.sp_protocol) {
356 V_route_cb.ip_count--;
359 V_route_cb.ip6_count--;
362 V_route_cb.any_count--;
364 raw_usrreqs.pru_detach(so);
368 rts_disconnect(struct socket *so)
371 return (raw_usrreqs.pru_disconnect(so));
374 /* pru_listen is EOPNOTSUPP */
377 rts_peeraddr(struct socket *so, struct sockaddr **nam)
380 return (raw_usrreqs.pru_peeraddr(so, nam));
383 /* pru_rcvd is EOPNOTSUPP */
384 /* pru_rcvoob is EOPNOTSUPP */
387 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
388 struct mbuf *control, struct thread *td)
391 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
394 /* pru_sense is null */
397 rts_shutdown(struct socket *so)
400 return (raw_usrreqs.pru_shutdown(so));
404 rts_sockaddr(struct socket *so, struct sockaddr **nam)
407 return (raw_usrreqs.pru_sockaddr(so, nam));
410 static struct pr_usrreqs route_usrreqs = {
411 .pru_abort = rts_abort,
412 .pru_attach = rts_attach,
413 .pru_bind = rts_bind,
414 .pru_connect = rts_connect,
415 .pru_detach = rts_detach,
416 .pru_disconnect = rts_disconnect,
417 .pru_peeraddr = rts_peeraddr,
418 .pru_send = rts_send,
419 .pru_shutdown = rts_shutdown,
420 .pru_sockaddr = rts_sockaddr,
421 .pru_close = rts_close,
424 #ifndef _SOCKADDR_UNION_DEFINED
425 #define _SOCKADDR_UNION_DEFINED
427 * The union of all possible address formats we handle.
429 union sockaddr_union {
431 struct sockaddr_in sin;
432 struct sockaddr_in6 sin6;
434 #endif /* _SOCKADDR_UNION_DEFINED */
437 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
438 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
441 /* First, see if the returned address is part of the jail. */
442 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
443 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
447 switch (info->rti_info[RTAX_DST]->sa_family) {
457 * Try to find an address on the given outgoing interface
458 * that belongs to the jail.
461 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
464 if (sa->sa_family != AF_INET)
466 ia = ((struct sockaddr_in *)sa)->sin_addr;
467 if (prison_check_ip4(cred, &ia) == 0) {
472 IF_ADDR_RUNLOCK(ifp);
475 * As a last resort return the 'default' jail address.
477 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
479 if (prison_get_ip4(cred, &ia) != 0)
482 bzero(&saun->sin, sizeof(struct sockaddr_in));
483 saun->sin.sin_len = sizeof(struct sockaddr_in);
484 saun->sin.sin_family = AF_INET;
485 saun->sin.sin_addr.s_addr = ia.s_addr;
486 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
499 * Try to find an address on the given outgoing interface
500 * that belongs to the jail.
503 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
506 if (sa->sa_family != AF_INET6)
508 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
509 &ia6, sizeof(struct in6_addr));
510 if (prison_check_ip6(cred, &ia6) == 0) {
515 IF_ADDR_RUNLOCK(ifp);
518 * As a last resort return the 'default' jail address.
520 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
522 if (prison_get_ip6(cred, &ia6) != 0)
525 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
526 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
527 saun->sin6.sin6_family = AF_INET6;
528 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
529 if (sa6_recoverscope(&saun->sin6) != 0)
531 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
543 route_output(struct mbuf *m, struct socket *so, ...)
545 struct rt_msghdr *rtm = NULL;
546 struct rtentry *rt = NULL;
547 struct rib_head *rnh;
548 struct rt_addrinfo info;
549 struct sockaddr_storage ss;
551 struct sockaddr_in6 *sin6;
552 int i, rti_need_deembed = 0;
554 int alloc_len = 0, len, error = 0, fibnum;
555 struct ifnet *ifp = NULL;
556 union sockaddr_union saun;
557 sa_family_t saf = AF_UNSPEC;
558 struct rawcb *rp = NULL;
561 fibnum = so->so_fibnum;
563 #define senderr(e) { error = e; goto flush;}
564 if (m == NULL || ((m->m_len < sizeof(long)) &&
565 (m = m_pullup(m, sizeof(long))) == NULL))
567 if ((m->m_flags & M_PKTHDR) == 0)
568 panic("route_output");
569 len = m->m_pkthdr.len;
570 if (len < sizeof(*rtm) ||
571 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
575 * Most of current messages are in range 200-240 bytes,
576 * minimize possible re-allocation on reply using larger size
577 * buffer aligned on 1k boundaty.
579 alloc_len = roundup2(len, 1024);
580 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
583 m_copydata(m, 0, len, (caddr_t)rtm);
584 bzero(&info, sizeof(info));
585 bzero(&w, sizeof(w));
587 if (rtm->rtm_version != RTM_VERSION) {
588 /* Do not touch message since format is unknown */
591 senderr(EPROTONOSUPPORT);
595 * Starting from here, it is possible
596 * to alter original message and insert
597 * caller PID and error value.
600 rtm->rtm_pid = curproc->p_pid;
601 info.rti_addrs = rtm->rtm_addrs;
603 info.rti_mflags = rtm->rtm_inits;
604 info.rti_rmx = &rtm->rtm_rmx;
607 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
608 * link-local address because rtrequest requires addresses with
611 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
614 info.rti_flags = rtm->rtm_flags;
615 if (info.rti_info[RTAX_DST] == NULL ||
616 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
617 (info.rti_info[RTAX_GATEWAY] != NULL &&
618 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
620 saf = info.rti_info[RTAX_DST]->sa_family;
622 * Verify that the caller has the appropriate privilege; RTM_GET
623 * is the only operation the non-superuser is allowed.
625 if (rtm->rtm_type != RTM_GET) {
626 error = priv_check(curthread, PRIV_NET_ROUTE);
632 * The given gateway address may be an interface address.
633 * For example, issuing a "route change" command on a route
634 * entry that was created from a tunnel, and the gateway
635 * address given is the local end point. In this case the
636 * RTF_GATEWAY flag must be cleared or the destination will
637 * not be reachable even though there is no error message.
639 if (info.rti_info[RTAX_GATEWAY] != NULL &&
640 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
641 struct rt_addrinfo ginfo;
642 struct sockaddr *gdst;
644 bzero(&ginfo, sizeof(ginfo));
645 bzero(&ss, sizeof(ss));
646 ss.ss_len = sizeof(ss);
648 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
649 gdst = info.rti_info[RTAX_GATEWAY];
652 * A host route through the loopback interface is
653 * installed for each interface adddress. In pre 8.0
654 * releases the interface address of a PPP link type
655 * is not reachable locally. This behavior is fixed as
656 * part of the new L2/L3 redesign and rewrite work. The
657 * signature of this interface address route is the
658 * AF_LINK sa_family type of the rt_gateway, and the
659 * rt_ifp has the IFF_LOOPBACK flag set.
661 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
662 if (ss.ss_family == AF_LINK &&
663 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
664 info.rti_flags &= ~RTF_GATEWAY;
665 info.rti_flags |= RTF_GWFLAG_COMPAT;
667 rib_free_info(&ginfo);
671 switch (rtm->rtm_type) {
672 struct rtentry *saved_nrt;
676 if (rtm->rtm_type == RTM_ADD) {
677 if (info.rti_info[RTAX_GATEWAY] == NULL)
682 /* support for new ARP code */
683 if (info.rti_info[RTAX_GATEWAY] != NULL &&
684 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
685 (rtm->rtm_flags & RTF_LLDATA) != 0) {
686 error = lla_rt_output(rtm, &info);
689 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
693 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
695 if (error == 0 && saved_nrt != NULL) {
697 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
700 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
701 RT_REMREF(saved_nrt);
702 RT_UNLOCK(saved_nrt);
708 /* support for new ARP code */
709 if (info.rti_info[RTAX_GATEWAY] &&
710 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
711 (rtm->rtm_flags & RTF_LLDATA) != 0) {
712 error = lla_rt_output(rtm, &info);
715 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
719 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
726 /* rt_msg2() will not be used when RTM_DELETE fails. */
727 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
732 rnh = rt_tables_get_rnh(fibnum, saf);
734 senderr(EAFNOSUPPORT);
738 if (info.rti_info[RTAX_NETMASK] == NULL &&
739 rtm->rtm_type == RTM_GET) {
741 * Provide longest prefix match for
742 * address lookup (no mask).
743 * 'route -n get addr'
745 rt = (struct rtentry *) rnh->rnh_matchaddr(
746 info.rti_info[RTAX_DST], &rnh->head);
748 rt = (struct rtentry *) rnh->rnh_lookup(
749 info.rti_info[RTAX_DST],
750 info.rti_info[RTAX_NETMASK], &rnh->head);
758 * for RTM_CHANGE/LOCK, if we got multipath routes,
759 * we require users to specify a matching RTAX_GATEWAY.
761 * for RTM_GET, gate is optional even with multipath.
762 * if gate == NULL the first match is returned.
763 * (no need to call rt_mpath_matchgate if gate == NULL)
765 if (rt_mpath_capable(rnh) &&
766 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
767 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
775 * If performing proxied L2 entry insertion, and
776 * the actual PPP host entry is found, perform
777 * another search to retrieve the prefix route of
778 * the local end point of the PPP link.
780 if (rtm->rtm_flags & RTF_ANNOUNCE) {
781 struct sockaddr laddr;
783 if (rt->rt_ifp != NULL &&
784 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
787 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
790 rt_maskedcopy(ifa->ifa_addr,
794 rt_maskedcopy(rt->rt_ifa->ifa_addr,
796 rt->rt_ifa->ifa_netmask);
798 * refactor rt and no lock operation necessary
800 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
813 if ((rt->rt_flags & RTF_HOST) == 0
814 ? jailed_without_vnet(curthread->td_ucred)
815 : prison_if(curthread->td_ucred,
820 info.rti_info[RTAX_DST] = rt_key(rt);
821 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
822 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
824 info.rti_info[RTAX_GENMASK] = 0;
825 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
828 info.rti_info[RTAX_IFP] =
829 ifp->if_addr->ifa_addr;
830 error = rtm_get_jailed(&info, ifp, rt,
831 &saun, curthread->td_ucred);
836 if (ifp->if_flags & IFF_POINTOPOINT)
837 info.rti_info[RTAX_BRD] =
838 rt->rt_ifa->ifa_dstaddr;
839 rtm->rtm_index = ifp->if_index;
841 info.rti_info[RTAX_IFP] = NULL;
842 info.rti_info[RTAX_IFA] = NULL;
844 } else if ((ifp = rt->rt_ifp) != NULL) {
845 rtm->rtm_index = ifp->if_index;
848 /* Check if we need to realloc storage */
849 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
850 if (len > alloc_len) {
851 struct rt_msghdr *new_rtm;
852 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
853 if (new_rtm == NULL) {
857 bcopy(rtm, new_rtm, rtm->rtm_msglen);
863 w.w_tmem = (caddr_t)rtm;
864 w.w_tmemsize = alloc_len;
865 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
867 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
868 rtm->rtm_flags = RTF_GATEWAY |
869 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
871 rtm->rtm_flags = rt->rt_flags;
872 rt_getmetrics(rt, &rtm->rtm_rmx);
873 rtm->rtm_addrs = info.rti_addrs;
886 * Check to see if we don't want our own messages.
888 if ((so->so_options & SO_USELOOPBACK) == 0) {
889 if (V_route_cb.any_count <= 1) {
895 /* There is another listener, so construct message */
901 if (rti_need_deembed) {
902 /* sin6_scope_id is recovered before sending rtm. */
903 sin6 = (struct sockaddr_in6 *)&ss;
904 for (i = 0; i < RTAX_MAX; i++) {
905 if (info.rti_info[i] == NULL)
907 if (info.rti_info[i]->sa_family != AF_INET6)
909 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
910 if (sa6_recoverscope(sin6) == 0)
911 bcopy(sin6, info.rti_info[i],
917 rtm->rtm_errno = error;
919 rtm->rtm_flags |= RTF_DONE;
921 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
922 if (m->m_pkthdr.len < rtm->rtm_msglen) {
925 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
926 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
932 m->m_flags |= RTS_FILTER_FIB;
935 * XXX insure we don't get a copy by
936 * invalidating our protocol
938 unsigned short family = rp->rcb_proto.sp_family;
939 rp->rcb_proto.sp_family = 0;
941 rp->rcb_proto.sp_family = family;
950 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
953 bzero(out, sizeof(*out));
954 out->rmx_mtu = rt->rt_mtu;
955 out->rmx_weight = rt->rt_weight;
956 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
957 /* Kernel -> userland timebase conversion. */
958 out->rmx_expire = rt->rt_expire ?
959 rt->rt_expire - time_uptime + time_second : 0;
963 * Extract the addresses of the passed sockaddrs.
964 * Do a little sanity checking so as to avoid bad memory references.
965 * This data is derived straight from userland.
968 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
973 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
974 if ((rtinfo->rti_addrs & (1 << i)) == 0)
976 sa = (struct sockaddr *)cp;
980 if (cp + sa->sa_len > cplim)
983 * there are no more.. quit now
984 * If there are more bits, they are in error.
985 * I've seen this. route(1) can evidently generate these.
986 * This causes kernel to core dump.
987 * for compatibility, If we see this, point to a safe address.
989 if (sa->sa_len == 0) {
990 rtinfo->rti_info[i] = &sa_zero;
991 return (0); /* should be EINVAL but for compat */
995 if (sa->sa_family == AF_INET6)
996 sa6_embedscope((struct sockaddr_in6 *)sa,
999 rtinfo->rti_info[i] = sa;
1006 * Fill in @dmask with valid netmask leaving original @smask
1007 * intact. Mostly used with radix netmasks.
1009 static struct sockaddr *
1010 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1011 struct sockaddr_storage *dmask)
1013 if (dst == NULL || smask == NULL)
1016 memset(dmask, 0, dst->sa_len);
1017 memcpy(dmask, smask, smask->sa_len);
1018 dmask->ss_len = dst->sa_len;
1019 dmask->ss_family = dst->sa_family;
1021 return ((struct sockaddr *)dmask);
1025 * Writes information related to @rtinfo object to newly-allocated mbuf.
1026 * Assumes MCLBYTES is enough to construct any message.
1027 * Used for OS notifications of vaious events (if/ifa announces,etc)
1029 * Returns allocated mbuf or NULL on failure.
1031 static struct mbuf *
1032 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1034 struct rt_msghdr *rtm;
1037 struct sockaddr *sa;
1039 struct sockaddr_storage ss;
1040 struct sockaddr_in6 *sin6;
1048 len = sizeof(struct ifa_msghdr);
1053 len = sizeof(struct ifma_msghdr);
1057 len = sizeof(struct if_msghdr);
1060 case RTM_IFANNOUNCE:
1062 len = sizeof(struct if_announcemsghdr);
1066 len = sizeof(struct rt_msghdr);
1069 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1070 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1072 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1074 m = m_gethdr(M_NOWAIT, MT_DATA);
1078 m->m_pkthdr.len = m->m_len = len;
1079 rtm = mtod(m, struct rt_msghdr *);
1080 bzero((caddr_t)rtm, len);
1081 for (i = 0; i < RTAX_MAX; i++) {
1082 if ((sa = rtinfo->rti_info[i]) == NULL)
1084 rtinfo->rti_addrs |= (1 << i);
1087 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1088 sin6 = (struct sockaddr_in6 *)&ss;
1089 bcopy(sa, sin6, sizeof(*sin6));
1090 if (sa6_recoverscope(sin6) == 0)
1091 sa = (struct sockaddr *)sin6;
1094 m_copyback(m, len, dlen, (caddr_t)sa);
1097 if (m->m_pkthdr.len != len) {
1101 rtm->rtm_msglen = len;
1102 rtm->rtm_version = RTM_VERSION;
1103 rtm->rtm_type = type;
1108 * Writes information related to @rtinfo object to preallocated buffer.
1109 * Stores needed size in @plen. If @w is NULL, calculates size without
1111 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1113 * Returns 0 on success.
1117 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1120 int len, buflen = 0, dlen;
1122 struct rt_msghdr *rtm = NULL;
1124 struct sockaddr_storage ss;
1125 struct sockaddr_in6 *sin6;
1127 #ifdef COMPAT_FREEBSD32
1128 bool compat32 = false;
1135 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1136 #ifdef COMPAT_FREEBSD32
1137 if (w->w_req->flags & SCTL_MASK32) {
1138 len = sizeof(struct ifa_msghdrl32);
1142 len = sizeof(struct ifa_msghdrl);
1144 len = sizeof(struct ifa_msghdr);
1148 #ifdef COMPAT_FREEBSD32
1149 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1150 if (w->w_op == NET_RT_IFLISTL)
1151 len = sizeof(struct if_msghdrl32);
1153 len = sizeof(struct if_msghdr32);
1158 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1159 len = sizeof(struct if_msghdrl);
1161 len = sizeof(struct if_msghdr);
1165 len = sizeof(struct ifma_msghdr);
1169 len = sizeof(struct rt_msghdr);
1173 rtm = (struct rt_msghdr *)w->w_tmem;
1174 buflen = w->w_tmemsize - len;
1175 cp = (caddr_t)w->w_tmem + len;
1178 rtinfo->rti_addrs = 0;
1179 for (i = 0; i < RTAX_MAX; i++) {
1180 struct sockaddr *sa;
1182 if ((sa = rtinfo->rti_info[i]) == NULL)
1184 rtinfo->rti_addrs |= (1 << i);
1185 #ifdef COMPAT_FREEBSD32
1187 dlen = SA_SIZE32(sa);
1191 if (cp != NULL && buflen >= dlen) {
1193 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1194 sin6 = (struct sockaddr_in6 *)&ss;
1195 bcopy(sa, sin6, sizeof(*sin6));
1196 if (sa6_recoverscope(sin6) == 0)
1197 sa = (struct sockaddr *)sin6;
1200 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1203 } else if (cp != NULL) {
1205 * Buffer too small. Count needed size
1206 * and return with error.
1215 dlen = ALIGN(len) - len;
1224 /* fill header iff buffer is large enough */
1225 rtm->rtm_version = RTM_VERSION;
1226 rtm->rtm_type = type;
1227 rtm->rtm_msglen = len;
1232 if (w != NULL && cp == NULL)
1239 * This routine is called to generate a message from the routing
1240 * socket indicating that a redirect has occurred, a routing lookup
1241 * has failed, or that a protocol has detected timeouts to a particular
1245 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1248 struct rt_msghdr *rtm;
1250 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1252 if (V_route_cb.any_count == 0)
1254 m = rtsock_msg_mbuf(type, rtinfo);
1258 if (fibnum != RT_ALL_FIBS) {
1259 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1260 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1261 M_SETFIB(m, fibnum);
1262 m->m_flags |= RTS_FILTER_FIB;
1265 rtm = mtod(m, struct rt_msghdr *);
1266 rtm->rtm_flags = RTF_DONE | flags;
1267 rtm->rtm_errno = error;
1268 rtm->rtm_addrs = rtinfo->rti_addrs;
1269 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1273 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1276 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1280 * This routine is called to generate a message from the routing
1281 * socket indicating that the status of a network interface has changed.
1284 rt_ifmsg(struct ifnet *ifp)
1286 struct if_msghdr *ifm;
1288 struct rt_addrinfo info;
1290 if (V_route_cb.any_count == 0)
1292 bzero((caddr_t)&info, sizeof(info));
1293 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1296 ifm = mtod(m, struct if_msghdr *);
1297 ifm->ifm_index = ifp->if_index;
1298 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1299 if_data_copy(ifp, &ifm->ifm_data);
1301 rt_dispatch(m, AF_UNSPEC);
1305 * Announce interface address arrival/withdraw.
1306 * Please do not call directly, use rt_addrmsg().
1307 * Assume input data to be valid.
1308 * Returns 0 on success.
1311 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1313 struct rt_addrinfo info;
1314 struct sockaddr *sa;
1317 struct ifa_msghdr *ifam;
1318 struct ifnet *ifp = ifa->ifa_ifp;
1319 struct sockaddr_storage ss;
1321 if (V_route_cb.any_count == 0)
1324 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1326 bzero((caddr_t)&info, sizeof(info));
1327 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1328 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1329 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1330 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1331 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1332 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1334 ifam = mtod(m, struct ifa_msghdr *);
1335 ifam->ifam_index = ifp->if_index;
1336 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1337 ifam->ifam_flags = ifa->ifa_flags;
1338 ifam->ifam_addrs = info.rti_addrs;
1340 if (fibnum != RT_ALL_FIBS) {
1341 M_SETFIB(m, fibnum);
1342 m->m_flags |= RTS_FILTER_FIB;
1345 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1351 * Announce route addition/removal.
1352 * Please do not call directly, use rt_routemsg().
1353 * Note that @rt data MAY be inconsistent/invalid:
1354 * if some userland app sends us "invalid" route message (invalid mask,
1355 * no dst, wrong address families, etc...) we need to pass it back
1356 * to app (and any other rtsock consumers) with rtm_errno field set to
1359 * Returns 0 on success.
1362 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1365 struct rt_addrinfo info;
1366 struct sockaddr *sa;
1368 struct rt_msghdr *rtm;
1369 struct sockaddr_storage ss;
1371 if (V_route_cb.any_count == 0)
1374 bzero((caddr_t)&info, sizeof(info));
1375 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1376 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1377 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1378 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1380 rtm = mtod(m, struct rt_msghdr *);
1381 rtm->rtm_index = ifp->if_index;
1382 rtm->rtm_flags |= rt->rt_flags;
1383 rtm->rtm_errno = error;
1384 rtm->rtm_addrs = info.rti_addrs;
1386 if (fibnum != RT_ALL_FIBS) {
1387 M_SETFIB(m, fibnum);
1388 m->m_flags |= RTS_FILTER_FIB;
1391 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1397 * This is the analogue to the rt_newaddrmsg which performs the same
1398 * function but for multicast group memberhips. This is easier since
1399 * there is no route state to worry about.
1402 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1404 struct rt_addrinfo info;
1405 struct mbuf *m = NULL;
1406 struct ifnet *ifp = ifma->ifma_ifp;
1407 struct ifma_msghdr *ifmam;
1409 if (V_route_cb.any_count == 0)
1412 bzero((caddr_t)&info, sizeof(info));
1413 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1414 if (ifp && ifp->if_addr)
1415 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1417 info.rti_info[RTAX_IFP] = NULL;
1419 * If a link-layer address is present, present it as a ``gateway''
1420 * (similarly to how ARP entries, e.g., are presented).
1422 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1423 m = rtsock_msg_mbuf(cmd, &info);
1426 ifmam = mtod(m, struct ifma_msghdr *);
1427 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1429 ifmam->ifmam_index = ifp->if_index;
1430 ifmam->ifmam_addrs = info.rti_addrs;
1431 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1434 static struct mbuf *
1435 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1436 struct rt_addrinfo *info)
1438 struct if_announcemsghdr *ifan;
1441 if (V_route_cb.any_count == 0)
1443 bzero((caddr_t)info, sizeof(*info));
1444 m = rtsock_msg_mbuf(type, info);
1446 ifan = mtod(m, struct if_announcemsghdr *);
1447 ifan->ifan_index = ifp->if_index;
1448 strlcpy(ifan->ifan_name, ifp->if_xname,
1449 sizeof(ifan->ifan_name));
1450 ifan->ifan_what = what;
1456 * This is called to generate routing socket messages indicating
1457 * IEEE80211 wireless events.
1458 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1461 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1464 struct rt_addrinfo info;
1466 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1469 * Append the ieee80211 data. Try to stick it in the
1470 * mbuf containing the ifannounce msg; otherwise allocate
1471 * a new mbuf and append.
1473 * NB: we assume m is a single mbuf.
1475 if (data_len > M_TRAILINGSPACE(m)) {
1476 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1481 bcopy(data, mtod(n, void *), data_len);
1482 n->m_len = data_len;
1484 } else if (data_len > 0) {
1485 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1486 m->m_len += data_len;
1488 if (m->m_flags & M_PKTHDR)
1489 m->m_pkthdr.len += data_len;
1490 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1491 rt_dispatch(m, AF_UNSPEC);
1496 * This is called to generate routing socket messages indicating
1497 * network interface arrival and departure.
1500 rt_ifannouncemsg(struct ifnet *ifp, int what)
1503 struct rt_addrinfo info;
1505 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1507 rt_dispatch(m, AF_UNSPEC);
1511 rt_dispatch(struct mbuf *m, sa_family_t saf)
1516 * Preserve the family from the sockaddr, if any, in an m_tag for
1517 * use when injecting the mbuf into the routing socket buffer from
1520 if (saf != AF_UNSPEC) {
1521 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1527 *(unsigned short *)(tag + 1) = saf;
1528 m_tag_prepend(m, tag);
1532 m->m_pkthdr.rcvif = V_loif;
1538 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1542 * This is used in dumping the kernel table via sysctl().
1545 sysctl_dumpentry(struct radix_node *rn, void *vw)
1547 struct walkarg *w = vw;
1548 struct rtentry *rt = (struct rtentry *)rn;
1549 int error = 0, size;
1550 struct rt_addrinfo info;
1551 struct sockaddr_storage ss;
1553 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1555 if ((rt->rt_flags & RTF_HOST) == 0
1556 ? jailed_without_vnet(w->w_req->td->td_ucred)
1557 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1559 bzero((caddr_t)&info, sizeof(info));
1560 info.rti_info[RTAX_DST] = rt_key(rt);
1561 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1562 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1564 info.rti_info[RTAX_GENMASK] = 0;
1566 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1567 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1568 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1569 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1571 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1573 if (w->w_req && w->w_tmem) {
1574 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1576 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1577 rtm->rtm_flags = RTF_GATEWAY |
1578 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1580 rtm->rtm_flags = rt->rt_flags;
1581 rt_getmetrics(rt, &rtm->rtm_rmx);
1582 rtm->rtm_index = rt->rt_ifp->if_index;
1583 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1584 rtm->rtm_addrs = info.rti_addrs;
1585 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1592 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1593 struct rt_addrinfo *info, struct walkarg *w, int len)
1595 struct if_msghdrl *ifm;
1596 struct if_data *ifd;
1598 ifm = (struct if_msghdrl *)w->w_tmem;
1600 #ifdef COMPAT_FREEBSD32
1601 if (w->w_req->flags & SCTL_MASK32) {
1602 struct if_msghdrl32 *ifm32;
1604 ifm32 = (struct if_msghdrl32 *)ifm;
1605 ifm32->ifm_addrs = info->rti_addrs;
1606 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1607 ifm32->ifm_index = ifp->if_index;
1608 ifm32->_ifm_spare1 = 0;
1609 ifm32->ifm_len = sizeof(*ifm32);
1610 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1611 ifd = &ifm32->ifm_data;
1615 ifm->ifm_addrs = info->rti_addrs;
1616 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1617 ifm->ifm_index = ifp->if_index;
1618 ifm->_ifm_spare1 = 0;
1619 ifm->ifm_len = sizeof(*ifm);
1620 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1621 ifd = &ifm->ifm_data;
1624 memcpy(ifd, src_ifd, sizeof(*ifd));
1626 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1630 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1631 struct rt_addrinfo *info, struct walkarg *w, int len)
1633 struct if_msghdr *ifm;
1634 struct if_data *ifd;
1636 ifm = (struct if_msghdr *)w->w_tmem;
1638 #ifdef COMPAT_FREEBSD32
1639 if (w->w_req->flags & SCTL_MASK32) {
1640 struct if_msghdr32 *ifm32;
1642 ifm32 = (struct if_msghdr32 *)ifm;
1643 ifm32->ifm_addrs = info->rti_addrs;
1644 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1645 ifm32->ifm_index = ifp->if_index;
1646 ifd = &ifm32->ifm_data;
1650 ifm->ifm_addrs = info->rti_addrs;
1651 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1652 ifm->ifm_index = ifp->if_index;
1653 ifd = &ifm->ifm_data;
1656 memcpy(ifd, src_ifd, sizeof(*ifd));
1658 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1662 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1663 struct walkarg *w, int len)
1665 struct ifa_msghdrl *ifam;
1666 struct if_data *ifd;
1668 ifam = (struct ifa_msghdrl *)w->w_tmem;
1670 #ifdef COMPAT_FREEBSD32
1671 if (w->w_req->flags & SCTL_MASK32) {
1672 struct ifa_msghdrl32 *ifam32;
1674 ifam32 = (struct ifa_msghdrl32 *)ifam;
1675 ifam32->ifam_addrs = info->rti_addrs;
1676 ifam32->ifam_flags = ifa->ifa_flags;
1677 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1678 ifam32->_ifam_spare1 = 0;
1679 ifam32->ifam_len = sizeof(*ifam32);
1680 ifam32->ifam_data_off =
1681 offsetof(struct ifa_msghdrl32, ifam_data);
1682 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1683 ifd = &ifam32->ifam_data;
1687 ifam->ifam_addrs = info->rti_addrs;
1688 ifam->ifam_flags = ifa->ifa_flags;
1689 ifam->ifam_index = ifa->ifa_ifp->if_index;
1690 ifam->_ifam_spare1 = 0;
1691 ifam->ifam_len = sizeof(*ifam);
1692 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1693 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1694 ifd = &ifam->ifam_data;
1697 bzero(ifd, sizeof(*ifd));
1698 ifd->ifi_datalen = sizeof(struct if_data);
1699 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1700 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1701 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1702 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1704 /* Fixup if_data carp(4) vhid. */
1705 if (carp_get_vhid_p != NULL)
1706 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1708 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1712 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1713 struct walkarg *w, int len)
1715 struct ifa_msghdr *ifam;
1717 ifam = (struct ifa_msghdr *)w->w_tmem;
1718 ifam->ifam_addrs = info->rti_addrs;
1719 ifam->ifam_flags = ifa->ifa_flags;
1720 ifam->ifam_index = ifa->ifa_ifp->if_index;
1721 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1723 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1727 sysctl_iflist(int af, struct walkarg *w)
1732 struct rt_addrinfo info;
1734 struct sockaddr_storage ss;
1736 bzero((caddr_t)&info, sizeof(info));
1737 bzero(&ifd, sizeof(ifd));
1738 IFNET_RLOCK_NOSLEEP();
1739 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1740 if (w->w_arg && w->w_arg != ifp->if_index)
1742 if_data_copy(ifp, &ifd);
1745 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1746 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1749 info.rti_info[RTAX_IFP] = NULL;
1750 if (w->w_req && w->w_tmem) {
1751 if (w->w_op == NET_RT_IFLISTL)
1752 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1755 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1760 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1761 if (af && af != ifa->ifa_addr->sa_family)
1763 if (prison_if(w->w_req->td->td_ucred,
1764 ifa->ifa_addr) != 0)
1766 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1767 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1768 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1769 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1770 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1773 if (w->w_req && w->w_tmem) {
1774 if (w->w_op == NET_RT_IFLISTL)
1775 error = sysctl_iflist_ifaml(ifa, &info,
1778 error = sysctl_iflist_ifam(ifa, &info,
1784 IF_ADDR_RUNLOCK(ifp);
1785 info.rti_info[RTAX_IFA] = NULL;
1786 info.rti_info[RTAX_NETMASK] = NULL;
1787 info.rti_info[RTAX_BRD] = NULL;
1791 IF_ADDR_RUNLOCK(ifp);
1792 IFNET_RUNLOCK_NOSLEEP();
1797 sysctl_ifmalist(int af, struct walkarg *w)
1799 struct rt_addrinfo info;
1801 struct ifmultiaddr *ifma;
1806 bzero((caddr_t)&info, sizeof(info));
1808 IFNET_RLOCK_NOSLEEP();
1809 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1810 if (w->w_arg && w->w_arg != ifp->if_index)
1813 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1815 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1816 if (af && af != ifma->ifma_addr->sa_family)
1818 if (prison_if(w->w_req->td->td_ucred,
1819 ifma->ifma_addr) != 0)
1821 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1822 info.rti_info[RTAX_GATEWAY] =
1823 (ifma->ifma_addr->sa_family != AF_LINK) ?
1824 ifma->ifma_lladdr : NULL;
1825 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1828 if (w->w_req && w->w_tmem) {
1829 struct ifma_msghdr *ifmam;
1831 ifmam = (struct ifma_msghdr *)w->w_tmem;
1832 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1833 ifmam->ifmam_flags = 0;
1834 ifmam->ifmam_addrs = info.rti_addrs;
1835 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1840 IF_ADDR_RUNLOCK(ifp);
1844 IFNET_RUNLOCK_NOSLEEP();
1849 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1851 int *name = (int *)arg1;
1852 u_int namelen = arg2;
1853 struct rib_head *rnh = NULL; /* silence compiler. */
1854 int i, lim, error = EINVAL;
1863 if (name[1] == NET_RT_DUMP) {
1865 fib = req->td->td_proc->p_fibnum;
1866 else if (namelen == 4)
1867 fib = (name[3] == RT_ALL_FIBS) ?
1868 req->td->td_proc->p_fibnum : name[3];
1870 return ((namelen < 3) ? EISDIR : ENOTDIR);
1871 if (fib < 0 || fib >= rt_numfibs)
1873 } else if (namelen != 3)
1874 return ((namelen < 3) ? EISDIR : ENOTDIR);
1878 bzero(&w, sizeof(w));
1883 error = sysctl_wire_old_buffer(req, 0);
1888 * Allocate reply buffer in advance.
1889 * All rtsock messages has maximum length of u_short.
1891 w.w_tmemsize = 65536;
1892 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1898 if (af == 0) { /* dump all tables */
1901 } else /* dump only one table */
1905 * take care of llinfo entries, the caller must
1908 if (w.w_op == NET_RT_FLAGS &&
1909 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1911 error = lltable_sysctl_dumparp(af, w.w_req);
1917 * take care of routing entries
1919 for (error = 0; error == 0 && i <= lim; i++) {
1920 rnh = rt_tables_get_rnh(fib, i);
1923 error = rnh->rnh_walktree(&rnh->head,
1924 sysctl_dumpentry, &w);
1927 error = EAFNOSUPPORT;
1932 case NET_RT_IFLISTL:
1933 error = sysctl_iflist(af, &w);
1936 case NET_RT_IFMALIST:
1937 error = sysctl_ifmalist(af, &w);
1941 free(w.w_tmem, M_TEMP);
1945 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1948 * Definitions of protocols supported in the ROUTE domain.
1951 static struct domain routedomain; /* or at least forward */
1953 static struct protosw routesw[] = {
1955 .pr_type = SOCK_RAW,
1956 .pr_domain = &routedomain,
1957 .pr_flags = PR_ATOMIC|PR_ADDR,
1958 .pr_output = route_output,
1959 .pr_ctlinput = raw_ctlinput,
1960 .pr_init = raw_init,
1961 .pr_usrreqs = &route_usrreqs
1965 static struct domain routedomain = {
1966 .dom_family = PF_ROUTE,
1967 .dom_name = "route",
1968 .dom_protosw = routesw,
1969 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1972 VNET_DOMAIN_SET(route);