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;
115 #endif /* COMPAT_FREEBSD32 */
117 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
119 /* NB: these are not modified */
120 static struct sockaddr route_src = { 2, PF_ROUTE, };
121 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
123 /* These are external hooks for CARP. */
124 int (*carp_get_vhid_p)(struct ifaddr *);
127 * Used by rtsock/raw_input callback code to decide whether to filter the update
128 * notification to a socket bound to a particular FIB.
130 #define RTS_FILTER_FIB M_PROTO8
133 int ip_count; /* attached w/ AF_INET */
134 int ip6_count; /* attached w/ AF_INET6 */
135 int any_count; /* total attached */
137 static VNET_DEFINE(route_cb_t, route_cb);
138 #define V_route_cb VNET(route_cb)
140 struct mtx rtsock_mtx;
141 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
143 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
144 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
145 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
147 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
153 struct sysctl_req *w_req;
156 static void rts_input(struct mbuf *m);
157 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
158 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
159 struct walkarg *w, int *plen);
160 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
161 struct rt_addrinfo *rtinfo);
162 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
163 static int sysctl_iflist(int af, struct walkarg *w);
164 static int sysctl_ifmalist(int af, struct walkarg *w);
165 static int route_output(struct mbuf *m, struct socket *so, ...);
166 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
167 static void rt_dispatch(struct mbuf *, sa_family_t);
168 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
169 struct sockaddr *smask, struct sockaddr_storage *dmask);
171 static struct netisr_handler rtsock_nh = {
173 .nh_handler = rts_input,
174 .nh_proto = NETISR_ROUTE,
175 .nh_policy = NETISR_POLICY_SOURCE,
179 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
183 netisr_getqlimit(&rtsock_nh, &qlimit);
184 error = sysctl_handle_int(oidp, &qlimit, 0, req);
185 if (error || !req->newptr)
189 return (netisr_setqlimit(&rtsock_nh, qlimit));
191 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
192 0, 0, sysctl_route_netisr_maxqlen, "I",
193 "maximum routing socket dispatch queue length");
200 if (IS_DEFAULT_VNET(curvnet)) {
201 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
202 rtsock_nh.nh_qlimit = tmp;
203 netisr_register(&rtsock_nh);
207 netisr_register_vnet(&rtsock_nh);
210 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
215 vnet_rts_uninit(void)
218 netisr_unregister_vnet(&rtsock_nh);
220 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
225 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
230 KASSERT(m != NULL, ("%s: m is NULL", __func__));
231 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
232 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
234 /* No filtering requested. */
235 if ((m->m_flags & RTS_FILTER_FIB) == 0)
238 /* Check if it is a rts and the fib matches the one of the socket. */
239 fibnum = M_GETFIB(m);
240 if (proto->sp_family != PF_ROUTE ||
241 rp->rcb_socket == NULL ||
242 rp->rcb_socket->so_fibnum == fibnum)
245 /* Filtering requested and no match, the socket shall be skipped. */
250 rts_input(struct mbuf *m)
252 struct sockproto route_proto;
253 unsigned short *family;
256 route_proto.sp_family = PF_ROUTE;
257 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
259 family = (unsigned short *)(tag + 1);
260 route_proto.sp_protocol = *family;
261 m_tag_delete(m, tag);
263 route_proto.sp_protocol = 0;
265 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
269 * It really doesn't make any sense at all for this code to share much
270 * with raw_usrreq.c, since its functionality is so restricted. XXX
273 rts_abort(struct socket *so)
276 raw_usrreqs.pru_abort(so);
280 rts_close(struct socket *so)
283 raw_usrreqs.pru_close(so);
286 /* pru_accept is EOPNOTSUPP */
289 rts_attach(struct socket *so, int proto, struct thread *td)
294 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
297 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
299 so->so_pcb = (caddr_t)rp;
300 so->so_fibnum = td->td_proc->p_fibnum;
301 error = raw_attach(so, proto);
309 switch(rp->rcb_proto.sp_protocol) {
311 V_route_cb.ip_count++;
314 V_route_cb.ip6_count++;
317 V_route_cb.any_count++;
320 so->so_options |= SO_USELOOPBACK;
325 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
328 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
332 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
335 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
338 /* pru_connect2 is EOPNOTSUPP */
339 /* pru_control is EOPNOTSUPP */
342 rts_detach(struct socket *so)
344 struct rawcb *rp = sotorawcb(so);
346 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
349 switch(rp->rcb_proto.sp_protocol) {
351 V_route_cb.ip_count--;
354 V_route_cb.ip6_count--;
357 V_route_cb.any_count--;
359 raw_usrreqs.pru_detach(so);
363 rts_disconnect(struct socket *so)
366 return (raw_usrreqs.pru_disconnect(so));
369 /* pru_listen is EOPNOTSUPP */
372 rts_peeraddr(struct socket *so, struct sockaddr **nam)
375 return (raw_usrreqs.pru_peeraddr(so, nam));
378 /* pru_rcvd is EOPNOTSUPP */
379 /* pru_rcvoob is EOPNOTSUPP */
382 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
383 struct mbuf *control, struct thread *td)
386 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
389 /* pru_sense is null */
392 rts_shutdown(struct socket *so)
395 return (raw_usrreqs.pru_shutdown(so));
399 rts_sockaddr(struct socket *so, struct sockaddr **nam)
402 return (raw_usrreqs.pru_sockaddr(so, nam));
405 static struct pr_usrreqs route_usrreqs = {
406 .pru_abort = rts_abort,
407 .pru_attach = rts_attach,
408 .pru_bind = rts_bind,
409 .pru_connect = rts_connect,
410 .pru_detach = rts_detach,
411 .pru_disconnect = rts_disconnect,
412 .pru_peeraddr = rts_peeraddr,
413 .pru_send = rts_send,
414 .pru_shutdown = rts_shutdown,
415 .pru_sockaddr = rts_sockaddr,
416 .pru_close = rts_close,
419 #ifndef _SOCKADDR_UNION_DEFINED
420 #define _SOCKADDR_UNION_DEFINED
422 * The union of all possible address formats we handle.
424 union sockaddr_union {
426 struct sockaddr_in sin;
427 struct sockaddr_in6 sin6;
429 #endif /* _SOCKADDR_UNION_DEFINED */
432 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
433 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
436 /* First, see if the returned address is part of the jail. */
437 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
438 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
442 switch (info->rti_info[RTAX_DST]->sa_family) {
452 * Try to find an address on the given outgoing interface
453 * that belongs to the jail.
456 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
459 if (sa->sa_family != AF_INET)
461 ia = ((struct sockaddr_in *)sa)->sin_addr;
462 if (prison_check_ip4(cred, &ia) == 0) {
467 IF_ADDR_RUNLOCK(ifp);
470 * As a last resort return the 'default' jail address.
472 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
474 if (prison_get_ip4(cred, &ia) != 0)
477 bzero(&saun->sin, sizeof(struct sockaddr_in));
478 saun->sin.sin_len = sizeof(struct sockaddr_in);
479 saun->sin.sin_family = AF_INET;
480 saun->sin.sin_addr.s_addr = ia.s_addr;
481 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
494 * Try to find an address on the given outgoing interface
495 * that belongs to the jail.
498 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
501 if (sa->sa_family != AF_INET6)
503 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
504 &ia6, sizeof(struct in6_addr));
505 if (prison_check_ip6(cred, &ia6) == 0) {
510 IF_ADDR_RUNLOCK(ifp);
513 * As a last resort return the 'default' jail address.
515 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
517 if (prison_get_ip6(cred, &ia6) != 0)
520 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
521 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
522 saun->sin6.sin6_family = AF_INET6;
523 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
524 if (sa6_recoverscope(&saun->sin6) != 0)
526 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
538 route_output(struct mbuf *m, struct socket *so, ...)
540 struct rt_msghdr *rtm = NULL;
541 struct rtentry *rt = NULL;
542 struct rib_head *rnh;
543 struct rt_addrinfo info;
544 struct sockaddr_storage ss;
546 struct sockaddr_in6 *sin6;
547 int i, rti_need_deembed = 0;
549 int alloc_len = 0, len, error = 0, fibnum;
550 struct ifnet *ifp = NULL;
551 union sockaddr_union saun;
552 sa_family_t saf = AF_UNSPEC;
553 struct rawcb *rp = NULL;
556 fibnum = so->so_fibnum;
558 #define senderr(e) { error = e; goto flush;}
559 if (m == NULL || ((m->m_len < sizeof(long)) &&
560 (m = m_pullup(m, sizeof(long))) == NULL))
562 if ((m->m_flags & M_PKTHDR) == 0)
563 panic("route_output");
564 len = m->m_pkthdr.len;
565 if (len < sizeof(*rtm) ||
566 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
570 * Most of current messages are in range 200-240 bytes,
571 * minimize possible re-allocation on reply using larger size
572 * buffer aligned on 1k boundaty.
574 alloc_len = roundup2(len, 1024);
575 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
578 m_copydata(m, 0, len, (caddr_t)rtm);
579 bzero(&info, sizeof(info));
580 bzero(&w, sizeof(w));
582 if (rtm->rtm_version != RTM_VERSION) {
583 /* Do not touch message since format is unknown */
586 senderr(EPROTONOSUPPORT);
590 * Starting from here, it is possible
591 * to alter original message and insert
592 * caller PID and error value.
595 rtm->rtm_pid = curproc->p_pid;
596 info.rti_addrs = rtm->rtm_addrs;
598 info.rti_mflags = rtm->rtm_inits;
599 info.rti_rmx = &rtm->rtm_rmx;
602 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
603 * link-local address because rtrequest requires addresses with
606 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
609 info.rti_flags = rtm->rtm_flags;
610 if (info.rti_info[RTAX_DST] == NULL ||
611 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
612 (info.rti_info[RTAX_GATEWAY] != NULL &&
613 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
615 saf = info.rti_info[RTAX_DST]->sa_family;
617 * Verify that the caller has the appropriate privilege; RTM_GET
618 * is the only operation the non-superuser is allowed.
620 if (rtm->rtm_type != RTM_GET) {
621 error = priv_check(curthread, PRIV_NET_ROUTE);
627 * The given gateway address may be an interface address.
628 * For example, issuing a "route change" command on a route
629 * entry that was created from a tunnel, and the gateway
630 * address given is the local end point. In this case the
631 * RTF_GATEWAY flag must be cleared or the destination will
632 * not be reachable even though there is no error message.
634 if (info.rti_info[RTAX_GATEWAY] != NULL &&
635 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
636 struct rt_addrinfo ginfo;
637 struct sockaddr *gdst;
639 bzero(&ginfo, sizeof(ginfo));
640 bzero(&ss, sizeof(ss));
641 ss.ss_len = sizeof(ss);
643 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
644 gdst = info.rti_info[RTAX_GATEWAY];
647 * A host route through the loopback interface is
648 * installed for each interface adddress. In pre 8.0
649 * releases the interface address of a PPP link type
650 * is not reachable locally. This behavior is fixed as
651 * part of the new L2/L3 redesign and rewrite work. The
652 * signature of this interface address route is the
653 * AF_LINK sa_family type of the rt_gateway, and the
654 * rt_ifp has the IFF_LOOPBACK flag set.
656 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
657 if (ss.ss_family == AF_LINK &&
658 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
659 info.rti_flags &= ~RTF_GATEWAY;
660 info.rti_flags |= RTF_GWFLAG_COMPAT;
662 rib_free_info(&ginfo);
666 switch (rtm->rtm_type) {
667 struct rtentry *saved_nrt;
671 if (info.rti_info[RTAX_GATEWAY] == NULL)
675 /* support for new ARP code */
676 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
677 (rtm->rtm_flags & RTF_LLDATA) != 0) {
678 error = lla_rt_output(rtm, &info);
681 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
685 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
687 if (error == 0 && saved_nrt != NULL) {
689 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
692 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
693 RT_REMREF(saved_nrt);
694 RT_UNLOCK(saved_nrt);
700 /* support for new ARP code */
701 if (info.rti_info[RTAX_GATEWAY] &&
702 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
703 (rtm->rtm_flags & RTF_LLDATA) != 0) {
704 error = lla_rt_output(rtm, &info);
707 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
711 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
718 /* rt_msg2() will not be used when RTM_DELETE fails. */
719 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
724 rnh = rt_tables_get_rnh(fibnum, saf);
726 senderr(EAFNOSUPPORT);
730 if (info.rti_info[RTAX_NETMASK] == NULL &&
731 rtm->rtm_type == RTM_GET) {
733 * Provide longest prefix match for
734 * address lookup (no mask).
735 * 'route -n get addr'
737 rt = (struct rtentry *) rnh->rnh_matchaddr(
738 info.rti_info[RTAX_DST], &rnh->head);
740 rt = (struct rtentry *) rnh->rnh_lookup(
741 info.rti_info[RTAX_DST],
742 info.rti_info[RTAX_NETMASK], &rnh->head);
750 * for RTM_CHANGE/LOCK, if we got multipath routes,
751 * we require users to specify a matching RTAX_GATEWAY.
753 * for RTM_GET, gate is optional even with multipath.
754 * if gate == NULL the first match is returned.
755 * (no need to call rt_mpath_matchgate if gate == NULL)
757 if (rt_mpath_capable(rnh) &&
758 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
759 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
767 * If performing proxied L2 entry insertion, and
768 * the actual PPP host entry is found, perform
769 * another search to retrieve the prefix route of
770 * the local end point of the PPP link.
772 if (rtm->rtm_flags & RTF_ANNOUNCE) {
773 struct sockaddr laddr;
775 if (rt->rt_ifp != NULL &&
776 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
779 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
782 rt_maskedcopy(ifa->ifa_addr,
786 rt_maskedcopy(rt->rt_ifa->ifa_addr,
788 rt->rt_ifa->ifa_netmask);
790 * refactor rt and no lock operation necessary
792 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
805 if ((rt->rt_flags & RTF_HOST) == 0
806 ? jailed_without_vnet(curthread->td_ucred)
807 : prison_if(curthread->td_ucred,
812 info.rti_info[RTAX_DST] = rt_key(rt);
813 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
814 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
816 info.rti_info[RTAX_GENMASK] = 0;
817 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
820 info.rti_info[RTAX_IFP] =
821 ifp->if_addr->ifa_addr;
822 error = rtm_get_jailed(&info, ifp, rt,
823 &saun, curthread->td_ucred);
828 if (ifp->if_flags & IFF_POINTOPOINT)
829 info.rti_info[RTAX_BRD] =
830 rt->rt_ifa->ifa_dstaddr;
831 rtm->rtm_index = ifp->if_index;
833 info.rti_info[RTAX_IFP] = NULL;
834 info.rti_info[RTAX_IFA] = NULL;
836 } else if ((ifp = rt->rt_ifp) != NULL) {
837 rtm->rtm_index = ifp->if_index;
840 /* Check if we need to realloc storage */
841 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
842 if (len > alloc_len) {
843 struct rt_msghdr *new_rtm;
844 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
845 if (new_rtm == NULL) {
849 bcopy(rtm, new_rtm, rtm->rtm_msglen);
855 w.w_tmem = (caddr_t)rtm;
856 w.w_tmemsize = alloc_len;
857 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
859 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
860 rtm->rtm_flags = RTF_GATEWAY |
861 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
863 rtm->rtm_flags = rt->rt_flags;
864 rt_getmetrics(rt, &rtm->rtm_rmx);
865 rtm->rtm_addrs = info.rti_addrs;
878 * Check to see if we don't want our own messages.
880 if ((so->so_options & SO_USELOOPBACK) == 0) {
881 if (V_route_cb.any_count <= 1) {
887 /* There is another listener, so construct message */
893 if (rti_need_deembed) {
894 /* sin6_scope_id is recovered before sending rtm. */
895 sin6 = (struct sockaddr_in6 *)&ss;
896 for (i = 0; i < RTAX_MAX; i++) {
897 if (info.rti_info[i] == NULL)
899 if (info.rti_info[i]->sa_family != AF_INET6)
901 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
902 if (sa6_recoverscope(sin6) == 0)
903 bcopy(sin6, info.rti_info[i],
909 rtm->rtm_errno = error;
911 rtm->rtm_flags |= RTF_DONE;
913 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
914 if (m->m_pkthdr.len < rtm->rtm_msglen) {
917 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
918 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
924 m->m_flags |= RTS_FILTER_FIB;
927 * XXX insure we don't get a copy by
928 * invalidating our protocol
930 unsigned short family = rp->rcb_proto.sp_family;
931 rp->rcb_proto.sp_family = 0;
933 rp->rcb_proto.sp_family = family;
942 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
945 bzero(out, sizeof(*out));
946 out->rmx_mtu = rt->rt_mtu;
947 out->rmx_weight = rt->rt_weight;
948 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
949 /* Kernel -> userland timebase conversion. */
950 out->rmx_expire = rt->rt_expire ?
951 rt->rt_expire - time_uptime + time_second : 0;
955 * Extract the addresses of the passed sockaddrs.
956 * Do a little sanity checking so as to avoid bad memory references.
957 * This data is derived straight from userland.
960 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
965 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
966 if ((rtinfo->rti_addrs & (1 << i)) == 0)
968 sa = (struct sockaddr *)cp;
972 if (cp + sa->sa_len > cplim)
975 * there are no more.. quit now
976 * If there are more bits, they are in error.
977 * I've seen this. route(1) can evidently generate these.
978 * This causes kernel to core dump.
979 * for compatibility, If we see this, point to a safe address.
981 if (sa->sa_len == 0) {
982 rtinfo->rti_info[i] = &sa_zero;
983 return (0); /* should be EINVAL but for compat */
987 if (sa->sa_family == AF_INET6)
988 sa6_embedscope((struct sockaddr_in6 *)sa,
991 rtinfo->rti_info[i] = sa;
998 * Fill in @dmask with valid netmask leaving original @smask
999 * intact. Mostly used with radix netmasks.
1001 static struct sockaddr *
1002 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1003 struct sockaddr_storage *dmask)
1005 if (dst == NULL || smask == NULL)
1008 memset(dmask, 0, dst->sa_len);
1009 memcpy(dmask, smask, smask->sa_len);
1010 dmask->ss_len = dst->sa_len;
1011 dmask->ss_family = dst->sa_family;
1013 return ((struct sockaddr *)dmask);
1017 * Writes information related to @rtinfo object to newly-allocated mbuf.
1018 * Assumes MCLBYTES is enough to construct any message.
1019 * Used for OS notifications of vaious events (if/ifa announces,etc)
1021 * Returns allocated mbuf or NULL on failure.
1023 static struct mbuf *
1024 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1026 struct rt_msghdr *rtm;
1029 struct sockaddr *sa;
1031 struct sockaddr_storage ss;
1032 struct sockaddr_in6 *sin6;
1040 len = sizeof(struct ifa_msghdr);
1045 len = sizeof(struct ifma_msghdr);
1049 len = sizeof(struct if_msghdr);
1052 case RTM_IFANNOUNCE:
1054 len = sizeof(struct if_announcemsghdr);
1058 len = sizeof(struct rt_msghdr);
1061 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1062 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1064 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1066 m = m_gethdr(M_NOWAIT, MT_DATA);
1070 m->m_pkthdr.len = m->m_len = len;
1071 rtm = mtod(m, struct rt_msghdr *);
1072 bzero((caddr_t)rtm, len);
1073 for (i = 0; i < RTAX_MAX; i++) {
1074 if ((sa = rtinfo->rti_info[i]) == NULL)
1076 rtinfo->rti_addrs |= (1 << i);
1079 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1080 sin6 = (struct sockaddr_in6 *)&ss;
1081 bcopy(sa, sin6, sizeof(*sin6));
1082 if (sa6_recoverscope(sin6) == 0)
1083 sa = (struct sockaddr *)sin6;
1086 m_copyback(m, len, dlen, (caddr_t)sa);
1089 if (m->m_pkthdr.len != len) {
1093 rtm->rtm_msglen = len;
1094 rtm->rtm_version = RTM_VERSION;
1095 rtm->rtm_type = type;
1100 * Writes information related to @rtinfo object to preallocated buffer.
1101 * Stores needed size in @plen. If @w is NULL, calculates size without
1103 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1105 * Returns 0 on success.
1109 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1112 int len, buflen = 0, dlen;
1114 struct rt_msghdr *rtm = NULL;
1116 struct sockaddr_storage ss;
1117 struct sockaddr_in6 *sin6;
1124 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1125 #ifdef COMPAT_FREEBSD32
1126 if (w->w_req->flags & SCTL_MASK32)
1127 len = sizeof(struct ifa_msghdrl32);
1130 len = sizeof(struct ifa_msghdrl);
1132 len = sizeof(struct ifa_msghdr);
1136 #ifdef COMPAT_FREEBSD32
1137 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1138 if (w->w_op == NET_RT_IFLISTL)
1139 len = sizeof(struct if_msghdrl32);
1141 len = sizeof(struct if_msghdr32);
1145 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1146 len = sizeof(struct if_msghdrl);
1148 len = sizeof(struct if_msghdr);
1152 len = sizeof(struct ifma_msghdr);
1156 len = sizeof(struct rt_msghdr);
1160 rtm = (struct rt_msghdr *)w->w_tmem;
1161 buflen = w->w_tmemsize - len;
1162 cp = (caddr_t)w->w_tmem + len;
1165 rtinfo->rti_addrs = 0;
1166 for (i = 0; i < RTAX_MAX; i++) {
1167 struct sockaddr *sa;
1169 if ((sa = rtinfo->rti_info[i]) == NULL)
1171 rtinfo->rti_addrs |= (1 << i);
1173 if (cp != NULL && buflen >= dlen) {
1175 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1176 sin6 = (struct sockaddr_in6 *)&ss;
1177 bcopy(sa, sin6, sizeof(*sin6));
1178 if (sa6_recoverscope(sin6) == 0)
1179 sa = (struct sockaddr *)sin6;
1182 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1185 } else if (cp != NULL) {
1187 * Buffer too small. Count needed size
1188 * and return with error.
1197 dlen = ALIGN(len) - len;
1206 /* fill header iff buffer is large enough */
1207 rtm->rtm_version = RTM_VERSION;
1208 rtm->rtm_type = type;
1209 rtm->rtm_msglen = len;
1214 if (w != NULL && cp == NULL)
1221 * This routine is called to generate a message from the routing
1222 * socket indicating that a redirect has occurred, a routing lookup
1223 * has failed, or that a protocol has detected timeouts to a particular
1227 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1230 struct rt_msghdr *rtm;
1232 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1234 if (V_route_cb.any_count == 0)
1236 m = rtsock_msg_mbuf(type, rtinfo);
1240 if (fibnum != RT_ALL_FIBS) {
1241 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1242 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1243 M_SETFIB(m, fibnum);
1244 m->m_flags |= RTS_FILTER_FIB;
1247 rtm = mtod(m, struct rt_msghdr *);
1248 rtm->rtm_flags = RTF_DONE | flags;
1249 rtm->rtm_errno = error;
1250 rtm->rtm_addrs = rtinfo->rti_addrs;
1251 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1255 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1258 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1262 * This routine is called to generate a message from the routing
1263 * socket indicating that the status of a network interface has changed.
1266 rt_ifmsg(struct ifnet *ifp)
1268 struct if_msghdr *ifm;
1270 struct rt_addrinfo info;
1272 if (V_route_cb.any_count == 0)
1274 bzero((caddr_t)&info, sizeof(info));
1275 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1278 ifm = mtod(m, struct if_msghdr *);
1279 ifm->ifm_index = ifp->if_index;
1280 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1281 if_data_copy(ifp, &ifm->ifm_data);
1283 rt_dispatch(m, AF_UNSPEC);
1287 * Announce interface address arrival/withdraw.
1288 * Please do not call directly, use rt_addrmsg().
1289 * Assume input data to be valid.
1290 * Returns 0 on success.
1293 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1295 struct rt_addrinfo info;
1296 struct sockaddr *sa;
1299 struct ifa_msghdr *ifam;
1300 struct ifnet *ifp = ifa->ifa_ifp;
1301 struct sockaddr_storage ss;
1303 if (V_route_cb.any_count == 0)
1306 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1308 bzero((caddr_t)&info, sizeof(info));
1309 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1310 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1311 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1312 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1313 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1314 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1316 ifam = mtod(m, struct ifa_msghdr *);
1317 ifam->ifam_index = ifp->if_index;
1318 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1319 ifam->ifam_flags = ifa->ifa_flags;
1320 ifam->ifam_addrs = info.rti_addrs;
1322 if (fibnum != RT_ALL_FIBS) {
1323 M_SETFIB(m, fibnum);
1324 m->m_flags |= RTS_FILTER_FIB;
1327 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1333 * Announce route addition/removal.
1334 * Please do not call directly, use rt_routemsg().
1335 * Note that @rt data MAY be inconsistent/invalid:
1336 * if some userland app sends us "invalid" route message (invalid mask,
1337 * no dst, wrong address families, etc...) we need to pass it back
1338 * to app (and any other rtsock consumers) with rtm_errno field set to
1341 * Returns 0 on success.
1344 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1347 struct rt_addrinfo info;
1348 struct sockaddr *sa;
1350 struct rt_msghdr *rtm;
1351 struct sockaddr_storage ss;
1353 if (V_route_cb.any_count == 0)
1356 bzero((caddr_t)&info, sizeof(info));
1357 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1358 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1359 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1360 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1362 rtm = mtod(m, struct rt_msghdr *);
1363 rtm->rtm_index = ifp->if_index;
1364 rtm->rtm_flags |= rt->rt_flags;
1365 rtm->rtm_errno = error;
1366 rtm->rtm_addrs = info.rti_addrs;
1368 if (fibnum != RT_ALL_FIBS) {
1369 M_SETFIB(m, fibnum);
1370 m->m_flags |= RTS_FILTER_FIB;
1373 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1379 * This is the analogue to the rt_newaddrmsg which performs the same
1380 * function but for multicast group memberhips. This is easier since
1381 * there is no route state to worry about.
1384 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1386 struct rt_addrinfo info;
1387 struct mbuf *m = NULL;
1388 struct ifnet *ifp = ifma->ifma_ifp;
1389 struct ifma_msghdr *ifmam;
1391 if (V_route_cb.any_count == 0)
1394 bzero((caddr_t)&info, sizeof(info));
1395 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1396 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1398 * If a link-layer address is present, present it as a ``gateway''
1399 * (similarly to how ARP entries, e.g., are presented).
1401 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1402 m = rtsock_msg_mbuf(cmd, &info);
1405 ifmam = mtod(m, struct ifma_msghdr *);
1406 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1408 ifmam->ifmam_index = ifp->if_index;
1409 ifmam->ifmam_addrs = info.rti_addrs;
1410 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1413 static struct mbuf *
1414 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1415 struct rt_addrinfo *info)
1417 struct if_announcemsghdr *ifan;
1420 if (V_route_cb.any_count == 0)
1422 bzero((caddr_t)info, sizeof(*info));
1423 m = rtsock_msg_mbuf(type, info);
1425 ifan = mtod(m, struct if_announcemsghdr *);
1426 ifan->ifan_index = ifp->if_index;
1427 strlcpy(ifan->ifan_name, ifp->if_xname,
1428 sizeof(ifan->ifan_name));
1429 ifan->ifan_what = what;
1435 * This is called to generate routing socket messages indicating
1436 * IEEE80211 wireless events.
1437 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1440 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1443 struct rt_addrinfo info;
1445 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1448 * Append the ieee80211 data. Try to stick it in the
1449 * mbuf containing the ifannounce msg; otherwise allocate
1450 * a new mbuf and append.
1452 * NB: we assume m is a single mbuf.
1454 if (data_len > M_TRAILINGSPACE(m)) {
1455 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1460 bcopy(data, mtod(n, void *), data_len);
1461 n->m_len = data_len;
1463 } else if (data_len > 0) {
1464 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1465 m->m_len += data_len;
1467 if (m->m_flags & M_PKTHDR)
1468 m->m_pkthdr.len += data_len;
1469 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1470 rt_dispatch(m, AF_UNSPEC);
1475 * This is called to generate routing socket messages indicating
1476 * network interface arrival and departure.
1479 rt_ifannouncemsg(struct ifnet *ifp, int what)
1482 struct rt_addrinfo info;
1484 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1486 rt_dispatch(m, AF_UNSPEC);
1490 rt_dispatch(struct mbuf *m, sa_family_t saf)
1495 * Preserve the family from the sockaddr, if any, in an m_tag for
1496 * use when injecting the mbuf into the routing socket buffer from
1499 if (saf != AF_UNSPEC) {
1500 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1506 *(unsigned short *)(tag + 1) = saf;
1507 m_tag_prepend(m, tag);
1511 m->m_pkthdr.rcvif = V_loif;
1517 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1521 * This is used in dumping the kernel table via sysctl().
1524 sysctl_dumpentry(struct radix_node *rn, void *vw)
1526 struct walkarg *w = vw;
1527 struct rtentry *rt = (struct rtentry *)rn;
1528 int error = 0, size;
1529 struct rt_addrinfo info;
1530 struct sockaddr_storage ss;
1532 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1534 if ((rt->rt_flags & RTF_HOST) == 0
1535 ? jailed_without_vnet(w->w_req->td->td_ucred)
1536 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1538 bzero((caddr_t)&info, sizeof(info));
1539 info.rti_info[RTAX_DST] = rt_key(rt);
1540 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1541 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1543 info.rti_info[RTAX_GENMASK] = 0;
1545 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1546 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1547 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1548 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1550 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1552 if (w->w_req && w->w_tmem) {
1553 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1555 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1556 rtm->rtm_flags = RTF_GATEWAY |
1557 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1559 rtm->rtm_flags = rt->rt_flags;
1560 rt_getmetrics(rt, &rtm->rtm_rmx);
1561 rtm->rtm_index = rt->rt_ifp->if_index;
1562 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1563 rtm->rtm_addrs = info.rti_addrs;
1564 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1571 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1572 struct rt_addrinfo *info, struct walkarg *w, int len)
1574 struct if_msghdrl *ifm;
1575 struct if_data *ifd;
1577 ifm = (struct if_msghdrl *)w->w_tmem;
1579 #ifdef COMPAT_FREEBSD32
1580 if (w->w_req->flags & SCTL_MASK32) {
1581 struct if_msghdrl32 *ifm32;
1583 ifm32 = (struct if_msghdrl32 *)ifm;
1584 ifm32->ifm_addrs = info->rti_addrs;
1585 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1586 ifm32->ifm_index = ifp->if_index;
1587 ifm32->_ifm_spare1 = 0;
1588 ifm32->ifm_len = sizeof(*ifm32);
1589 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1590 ifd = &ifm32->ifm_data;
1594 ifm->ifm_addrs = info->rti_addrs;
1595 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1596 ifm->ifm_index = ifp->if_index;
1597 ifm->_ifm_spare1 = 0;
1598 ifm->ifm_len = sizeof(*ifm);
1599 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1600 ifd = &ifm->ifm_data;
1603 memcpy(ifd, src_ifd, sizeof(*ifd));
1605 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1609 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1610 struct rt_addrinfo *info, struct walkarg *w, int len)
1612 struct if_msghdr *ifm;
1613 struct if_data *ifd;
1615 ifm = (struct if_msghdr *)w->w_tmem;
1617 #ifdef COMPAT_FREEBSD32
1618 if (w->w_req->flags & SCTL_MASK32) {
1619 struct if_msghdr32 *ifm32;
1621 ifm32 = (struct if_msghdr32 *)ifm;
1622 ifm32->ifm_addrs = info->rti_addrs;
1623 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1624 ifm32->ifm_index = ifp->if_index;
1625 ifd = &ifm32->ifm_data;
1629 ifm->ifm_addrs = info->rti_addrs;
1630 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1631 ifm->ifm_index = ifp->if_index;
1632 ifd = &ifm->ifm_data;
1635 memcpy(ifd, src_ifd, sizeof(*ifd));
1637 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1641 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1642 struct walkarg *w, int len)
1644 struct ifa_msghdrl *ifam;
1645 struct if_data *ifd;
1647 ifam = (struct ifa_msghdrl *)w->w_tmem;
1649 #ifdef COMPAT_FREEBSD32
1650 if (w->w_req->flags & SCTL_MASK32) {
1651 struct ifa_msghdrl32 *ifam32;
1653 ifam32 = (struct ifa_msghdrl32 *)ifam;
1654 ifam32->ifam_addrs = info->rti_addrs;
1655 ifam32->ifam_flags = ifa->ifa_flags;
1656 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1657 ifam32->_ifam_spare1 = 0;
1658 ifam32->ifam_len = sizeof(*ifam32);
1659 ifam32->ifam_data_off =
1660 offsetof(struct ifa_msghdrl32, ifam_data);
1661 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1662 ifd = &ifam32->ifam_data;
1666 ifam->ifam_addrs = info->rti_addrs;
1667 ifam->ifam_flags = ifa->ifa_flags;
1668 ifam->ifam_index = ifa->ifa_ifp->if_index;
1669 ifam->_ifam_spare1 = 0;
1670 ifam->ifam_len = sizeof(*ifam);
1671 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1672 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1673 ifd = &ifam->ifam_data;
1676 bzero(ifd, sizeof(*ifd));
1677 ifd->ifi_datalen = sizeof(struct if_data);
1678 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1679 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1680 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1681 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1683 /* Fixup if_data carp(4) vhid. */
1684 if (carp_get_vhid_p != NULL)
1685 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1687 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1691 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1692 struct walkarg *w, int len)
1694 struct ifa_msghdr *ifam;
1696 ifam = (struct ifa_msghdr *)w->w_tmem;
1697 ifam->ifam_addrs = info->rti_addrs;
1698 ifam->ifam_flags = ifa->ifa_flags;
1699 ifam->ifam_index = ifa->ifa_ifp->if_index;
1700 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1702 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1706 sysctl_iflist(int af, struct walkarg *w)
1711 struct rt_addrinfo info;
1713 struct sockaddr_storage ss;
1715 bzero((caddr_t)&info, sizeof(info));
1716 bzero(&ifd, sizeof(ifd));
1717 IFNET_RLOCK_NOSLEEP();
1718 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1719 if (w->w_arg && w->w_arg != ifp->if_index)
1721 if_data_copy(ifp, &ifd);
1724 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1725 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1728 info.rti_info[RTAX_IFP] = NULL;
1729 if (w->w_req && w->w_tmem) {
1730 if (w->w_op == NET_RT_IFLISTL)
1731 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1734 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1739 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1740 if (af && af != ifa->ifa_addr->sa_family)
1742 if (prison_if(w->w_req->td->td_ucred,
1743 ifa->ifa_addr) != 0)
1745 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1746 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1747 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1748 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1749 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1752 if (w->w_req && w->w_tmem) {
1753 if (w->w_op == NET_RT_IFLISTL)
1754 error = sysctl_iflist_ifaml(ifa, &info,
1757 error = sysctl_iflist_ifam(ifa, &info,
1763 IF_ADDR_RUNLOCK(ifp);
1764 info.rti_info[RTAX_IFA] = NULL;
1765 info.rti_info[RTAX_NETMASK] = NULL;
1766 info.rti_info[RTAX_BRD] = NULL;
1770 IF_ADDR_RUNLOCK(ifp);
1771 IFNET_RUNLOCK_NOSLEEP();
1776 sysctl_ifmalist(int af, struct walkarg *w)
1778 struct rt_addrinfo info;
1780 struct ifmultiaddr *ifma;
1785 bzero((caddr_t)&info, sizeof(info));
1787 IFNET_RLOCK_NOSLEEP();
1788 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1789 if (w->w_arg && w->w_arg != ifp->if_index)
1792 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1794 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1795 if (af && af != ifma->ifma_addr->sa_family)
1797 if (prison_if(w->w_req->td->td_ucred,
1798 ifma->ifma_addr) != 0)
1800 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1801 info.rti_info[RTAX_GATEWAY] =
1802 (ifma->ifma_addr->sa_family != AF_LINK) ?
1803 ifma->ifma_lladdr : NULL;
1804 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1807 if (w->w_req && w->w_tmem) {
1808 struct ifma_msghdr *ifmam;
1810 ifmam = (struct ifma_msghdr *)w->w_tmem;
1811 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1812 ifmam->ifmam_flags = 0;
1813 ifmam->ifmam_addrs = info.rti_addrs;
1814 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1819 IF_ADDR_RUNLOCK(ifp);
1823 IFNET_RUNLOCK_NOSLEEP();
1828 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1830 int *name = (int *)arg1;
1831 u_int namelen = arg2;
1832 struct rib_head *rnh = NULL; /* silence compiler. */
1833 int i, lim, error = EINVAL;
1842 if (name[1] == NET_RT_DUMP) {
1844 fib = req->td->td_proc->p_fibnum;
1845 else if (namelen == 4)
1846 fib = (name[3] == RT_ALL_FIBS) ?
1847 req->td->td_proc->p_fibnum : name[3];
1849 return ((namelen < 3) ? EISDIR : ENOTDIR);
1850 if (fib < 0 || fib >= rt_numfibs)
1852 } else if (namelen != 3)
1853 return ((namelen < 3) ? EISDIR : ENOTDIR);
1857 bzero(&w, sizeof(w));
1862 error = sysctl_wire_old_buffer(req, 0);
1867 * Allocate reply buffer in advance.
1868 * All rtsock messages has maximum length of u_short.
1870 w.w_tmemsize = 65536;
1871 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1877 if (af == 0) { /* dump all tables */
1880 } else /* dump only one table */
1884 * take care of llinfo entries, the caller must
1887 if (w.w_op == NET_RT_FLAGS &&
1888 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1890 error = lltable_sysctl_dumparp(af, w.w_req);
1896 * take care of routing entries
1898 for (error = 0; error == 0 && i <= lim; i++) {
1899 rnh = rt_tables_get_rnh(fib, i);
1902 error = rnh->rnh_walktree(&rnh->head,
1903 sysctl_dumpentry, &w);
1906 error = EAFNOSUPPORT;
1911 case NET_RT_IFLISTL:
1912 error = sysctl_iflist(af, &w);
1915 case NET_RT_IFMALIST:
1916 error = sysctl_ifmalist(af, &w);
1920 free(w.w_tmem, M_TEMP);
1924 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1927 * Definitions of protocols supported in the ROUTE domain.
1930 static struct domain routedomain; /* or at least forward */
1932 static struct protosw routesw[] = {
1934 .pr_type = SOCK_RAW,
1935 .pr_domain = &routedomain,
1936 .pr_flags = PR_ATOMIC|PR_ADDR,
1937 .pr_output = route_output,
1938 .pr_ctlinput = raw_ctlinput,
1939 .pr_init = raw_init,
1940 .pr_usrreqs = &route_usrreqs
1944 static struct domain routedomain = {
1945 .dom_family = PF_ROUTE,
1946 .dom_name = "route",
1947 .dom_protosw = routesw,
1948 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1951 VNET_DOMAIN_SET(route);