2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
32 #include "opt_compat.h"
33 #include "opt_mpath.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/domain.h>
42 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/rwlock.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/if_llatbl.h>
58 #include <net/if_types.h>
59 #include <net/netisr.h>
60 #include <net/raw_cb.h>
61 #include <net/route.h>
62 #include <net/route_var.h>
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 #include <netinet/ip_carp.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
73 #ifdef COMPAT_FREEBSD32
74 #include <sys/mount.h>
75 #include <compat/freebsd32/freebsd32.h>
84 struct if_data ifm_data;
96 uint16_t ifm_data_off;
97 struct if_data ifm_data;
100 struct ifa_msghdrl32 {
101 uint16_t ifam_msglen;
102 uint8_t ifam_version;
107 uint16_t _ifam_spare1;
109 uint16_t ifam_data_off;
111 struct if_data ifam_data;
113 #endif /* COMPAT_FREEBSD32 */
115 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
117 /* NB: these are not modified */
118 static struct sockaddr route_src = { 2, PF_ROUTE, };
119 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
121 /* These are external hooks for CARP. */
122 int (*carp_get_vhid_p)(struct ifaddr *);
125 * Used by rtsock/raw_input callback code to decide whether to filter the update
126 * notification to a socket bound to a particular FIB.
128 #define RTS_FILTER_FIB M_PROTO8
131 int ip_count; /* attached w/ AF_INET */
132 int ip6_count; /* attached w/ AF_INET6 */
133 int any_count; /* total attached */
135 static VNET_DEFINE(route_cb_t, route_cb);
136 #define V_route_cb VNET(route_cb)
138 struct mtx rtsock_mtx;
139 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
141 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
142 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
143 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
145 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
151 struct sysctl_req *w_req;
154 static void rts_input(struct mbuf *m);
155 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
156 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
157 struct walkarg *w, int *plen);
158 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
159 struct rt_addrinfo *rtinfo);
160 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
161 static int sysctl_iflist(int af, struct walkarg *w);
162 static int sysctl_ifmalist(int af, struct walkarg *w);
163 static int route_output(struct mbuf *m, struct socket *so, ...);
164 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
165 static void rt_dispatch(struct mbuf *, sa_family_t);
166 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
167 struct sockaddr *smask, struct sockaddr_storage *dmask);
169 static struct netisr_handler rtsock_nh = {
171 .nh_handler = rts_input,
172 .nh_proto = NETISR_ROUTE,
173 .nh_policy = NETISR_POLICY_SOURCE,
177 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
181 netisr_getqlimit(&rtsock_nh, &qlimit);
182 error = sysctl_handle_int(oidp, &qlimit, 0, req);
183 if (error || !req->newptr)
187 return (netisr_setqlimit(&rtsock_nh, qlimit));
189 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
190 0, 0, sysctl_route_netisr_maxqlen, "I",
191 "maximum routing socket dispatch queue length");
198 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
199 rtsock_nh.nh_qlimit = tmp;
200 netisr_register(&rtsock_nh);
202 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
205 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
210 KASSERT(m != NULL, ("%s: m is NULL", __func__));
211 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
212 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
214 /* No filtering requested. */
215 if ((m->m_flags & RTS_FILTER_FIB) == 0)
218 /* Check if it is a rts and the fib matches the one of the socket. */
219 fibnum = M_GETFIB(m);
220 if (proto->sp_family != PF_ROUTE ||
221 rp->rcb_socket == NULL ||
222 rp->rcb_socket->so_fibnum == fibnum)
225 /* Filtering requested and no match, the socket shall be skipped. */
230 rts_input(struct mbuf *m)
232 struct sockproto route_proto;
233 unsigned short *family;
236 route_proto.sp_family = PF_ROUTE;
237 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
239 family = (unsigned short *)(tag + 1);
240 route_proto.sp_protocol = *family;
241 m_tag_delete(m, tag);
243 route_proto.sp_protocol = 0;
245 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
249 * It really doesn't make any sense at all for this code to share much
250 * with raw_usrreq.c, since its functionality is so restricted. XXX
253 rts_abort(struct socket *so)
256 raw_usrreqs.pru_abort(so);
260 rts_close(struct socket *so)
263 raw_usrreqs.pru_close(so);
266 /* pru_accept is EOPNOTSUPP */
269 rts_attach(struct socket *so, int proto, struct thread *td)
274 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
277 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
279 so->so_pcb = (caddr_t)rp;
280 so->so_fibnum = td->td_proc->p_fibnum;
281 error = raw_attach(so, proto);
289 switch(rp->rcb_proto.sp_protocol) {
291 V_route_cb.ip_count++;
294 V_route_cb.ip6_count++;
297 V_route_cb.any_count++;
300 so->so_options |= SO_USELOOPBACK;
305 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
308 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
312 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
315 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
318 /* pru_connect2 is EOPNOTSUPP */
319 /* pru_control is EOPNOTSUPP */
322 rts_detach(struct socket *so)
324 struct rawcb *rp = sotorawcb(so);
326 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
329 switch(rp->rcb_proto.sp_protocol) {
331 V_route_cb.ip_count--;
334 V_route_cb.ip6_count--;
337 V_route_cb.any_count--;
339 raw_usrreqs.pru_detach(so);
343 rts_disconnect(struct socket *so)
346 return (raw_usrreqs.pru_disconnect(so));
349 /* pru_listen is EOPNOTSUPP */
352 rts_peeraddr(struct socket *so, struct sockaddr **nam)
355 return (raw_usrreqs.pru_peeraddr(so, nam));
358 /* pru_rcvd is EOPNOTSUPP */
359 /* pru_rcvoob is EOPNOTSUPP */
362 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
363 struct mbuf *control, struct thread *td)
366 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
369 /* pru_sense is null */
372 rts_shutdown(struct socket *so)
375 return (raw_usrreqs.pru_shutdown(so));
379 rts_sockaddr(struct socket *so, struct sockaddr **nam)
382 return (raw_usrreqs.pru_sockaddr(so, nam));
385 static struct pr_usrreqs route_usrreqs = {
386 .pru_abort = rts_abort,
387 .pru_attach = rts_attach,
388 .pru_bind = rts_bind,
389 .pru_connect = rts_connect,
390 .pru_detach = rts_detach,
391 .pru_disconnect = rts_disconnect,
392 .pru_peeraddr = rts_peeraddr,
393 .pru_send = rts_send,
394 .pru_shutdown = rts_shutdown,
395 .pru_sockaddr = rts_sockaddr,
396 .pru_close = rts_close,
399 #ifndef _SOCKADDR_UNION_DEFINED
400 #define _SOCKADDR_UNION_DEFINED
402 * The union of all possible address formats we handle.
404 union sockaddr_union {
406 struct sockaddr_in sin;
407 struct sockaddr_in6 sin6;
409 #endif /* _SOCKADDR_UNION_DEFINED */
412 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
413 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
416 /* First, see if the returned address is part of the jail. */
417 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
418 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
422 switch (info->rti_info[RTAX_DST]->sa_family) {
432 * Try to find an address on the given outgoing interface
433 * that belongs to the jail.
436 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
439 if (sa->sa_family != AF_INET)
441 ia = ((struct sockaddr_in *)sa)->sin_addr;
442 if (prison_check_ip4(cred, &ia) == 0) {
447 IF_ADDR_RUNLOCK(ifp);
450 * As a last resort return the 'default' jail address.
452 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
454 if (prison_get_ip4(cred, &ia) != 0)
457 bzero(&saun->sin, sizeof(struct sockaddr_in));
458 saun->sin.sin_len = sizeof(struct sockaddr_in);
459 saun->sin.sin_family = AF_INET;
460 saun->sin.sin_addr.s_addr = ia.s_addr;
461 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
474 * Try to find an address on the given outgoing interface
475 * that belongs to the jail.
478 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
481 if (sa->sa_family != AF_INET6)
483 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
484 &ia6, sizeof(struct in6_addr));
485 if (prison_check_ip6(cred, &ia6) == 0) {
490 IF_ADDR_RUNLOCK(ifp);
493 * As a last resort return the 'default' jail address.
495 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
497 if (prison_get_ip6(cred, &ia6) != 0)
500 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
501 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
502 saun->sin6.sin6_family = AF_INET6;
503 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
504 if (sa6_recoverscope(&saun->sin6) != 0)
506 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
518 route_output(struct mbuf *m, struct socket *so, ...)
520 struct rt_msghdr *rtm = NULL;
521 struct rtentry *rt = NULL;
522 struct rib_head *rnh;
523 struct rt_addrinfo info;
524 struct sockaddr_storage ss;
526 struct sockaddr_in6 *sin6;
527 int i, rti_need_deembed = 0;
529 int alloc_len = 0, len, error = 0, fibnum;
530 struct ifnet *ifp = NULL;
531 union sockaddr_union saun;
532 sa_family_t saf = AF_UNSPEC;
533 struct rawcb *rp = NULL;
536 fibnum = so->so_fibnum;
538 #define senderr(e) { error = e; goto flush;}
539 if (m == NULL || ((m->m_len < sizeof(long)) &&
540 (m = m_pullup(m, sizeof(long))) == NULL))
542 if ((m->m_flags & M_PKTHDR) == 0)
543 panic("route_output");
544 len = m->m_pkthdr.len;
545 if (len < sizeof(*rtm) ||
546 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
550 * Most of current messages are in range 200-240 bytes,
551 * minimize possible re-allocation on reply using larger size
552 * buffer aligned on 1k boundaty.
554 alloc_len = roundup2(len, 1024);
555 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
558 m_copydata(m, 0, len, (caddr_t)rtm);
559 bzero(&info, sizeof(info));
560 bzero(&w, sizeof(w));
562 if (rtm->rtm_version != RTM_VERSION) {
563 /* Do not touch message since format is unknown */
566 senderr(EPROTONOSUPPORT);
570 * Starting from here, it is possible
571 * to alter original message and insert
572 * caller PID and error value.
575 rtm->rtm_pid = curproc->p_pid;
576 info.rti_addrs = rtm->rtm_addrs;
578 info.rti_mflags = rtm->rtm_inits;
579 info.rti_rmx = &rtm->rtm_rmx;
582 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
583 * link-local address because rtrequest requires addresses with
586 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
589 info.rti_flags = rtm->rtm_flags;
590 if (info.rti_info[RTAX_DST] == NULL ||
591 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
592 (info.rti_info[RTAX_GATEWAY] != NULL &&
593 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
595 saf = info.rti_info[RTAX_DST]->sa_family;
597 * Verify that the caller has the appropriate privilege; RTM_GET
598 * is the only operation the non-superuser is allowed.
600 if (rtm->rtm_type != RTM_GET) {
601 error = priv_check(curthread, PRIV_NET_ROUTE);
607 * The given gateway address may be an interface address.
608 * For example, issuing a "route change" command on a route
609 * entry that was created from a tunnel, and the gateway
610 * address given is the local end point. In this case the
611 * RTF_GATEWAY flag must be cleared or the destination will
612 * not be reachable even though there is no error message.
614 if (info.rti_info[RTAX_GATEWAY] != NULL &&
615 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
616 struct rt_addrinfo ginfo;
617 struct sockaddr *gdst;
619 bzero(&ginfo, sizeof(ginfo));
620 bzero(&ss, sizeof(ss));
621 ss.ss_len = sizeof(ss);
623 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
624 gdst = info.rti_info[RTAX_GATEWAY];
627 * A host route through the loopback interface is
628 * installed for each interface adddress. In pre 8.0
629 * releases the interface address of a PPP link type
630 * is not reachable locally. This behavior is fixed as
631 * part of the new L2/L3 redesign and rewrite work. The
632 * signature of this interface address route is the
633 * AF_LINK sa_family type of the rt_gateway, and the
634 * rt_ifp has the IFF_LOOPBACK flag set.
636 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
637 if (ss.ss_family == AF_LINK &&
638 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
639 info.rti_flags &= ~RTF_GATEWAY;
640 info.rti_flags |= RTF_GWFLAG_COMPAT;
642 rib_free_info(&ginfo);
646 switch (rtm->rtm_type) {
647 struct rtentry *saved_nrt;
651 if (info.rti_info[RTAX_GATEWAY] == NULL)
655 /* support for new ARP code */
656 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
657 (rtm->rtm_flags & RTF_LLDATA) != 0) {
658 error = lla_rt_output(rtm, &info);
661 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
665 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
667 if (error == 0 && saved_nrt != NULL) {
669 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
672 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
673 RT_REMREF(saved_nrt);
674 RT_UNLOCK(saved_nrt);
680 /* support for new ARP code */
681 if (info.rti_info[RTAX_GATEWAY] &&
682 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
683 (rtm->rtm_flags & RTF_LLDATA) != 0) {
684 error = lla_rt_output(rtm, &info);
687 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
691 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
698 /* rt_msg2() will not be used when RTM_DELETE fails. */
699 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
704 rnh = rt_tables_get_rnh(fibnum, saf);
706 senderr(EAFNOSUPPORT);
710 if (info.rti_info[RTAX_NETMASK] == NULL &&
711 rtm->rtm_type == RTM_GET) {
713 * Provide logest prefix match for
714 * address lookup (no mask).
715 * 'route -n get addr'
717 rt = (struct rtentry *) rnh->rnh_matchaddr(
718 info.rti_info[RTAX_DST], &rnh->head);
720 rt = (struct rtentry *) rnh->rnh_lookup(
721 info.rti_info[RTAX_DST],
722 info.rti_info[RTAX_NETMASK], &rnh->head);
730 * for RTM_CHANGE/LOCK, if we got multipath routes,
731 * we require users to specify a matching RTAX_GATEWAY.
733 * for RTM_GET, gate is optional even with multipath.
734 * if gate == NULL the first match is returned.
735 * (no need to call rt_mpath_matchgate if gate == NULL)
737 if (rt_mpath_capable(rnh) &&
738 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
739 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
747 * If performing proxied L2 entry insertion, and
748 * the actual PPP host entry is found, perform
749 * another search to retrieve the prefix route of
750 * the local end point of the PPP link.
752 if (rtm->rtm_flags & RTF_ANNOUNCE) {
753 struct sockaddr laddr;
755 if (rt->rt_ifp != NULL &&
756 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
759 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
762 rt_maskedcopy(ifa->ifa_addr,
766 rt_maskedcopy(rt->rt_ifa->ifa_addr,
768 rt->rt_ifa->ifa_netmask);
770 * refactor rt and no lock operation necessary
772 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
785 if ((rt->rt_flags & RTF_HOST) == 0
786 ? jailed_without_vnet(curthread->td_ucred)
787 : prison_if(curthread->td_ucred,
792 info.rti_info[RTAX_DST] = rt_key(rt);
793 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
794 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
796 info.rti_info[RTAX_GENMASK] = 0;
797 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
800 info.rti_info[RTAX_IFP] =
801 ifp->if_addr->ifa_addr;
802 error = rtm_get_jailed(&info, ifp, rt,
803 &saun, curthread->td_ucred);
808 if (ifp->if_flags & IFF_POINTOPOINT)
809 info.rti_info[RTAX_BRD] =
810 rt->rt_ifa->ifa_dstaddr;
811 rtm->rtm_index = ifp->if_index;
813 info.rti_info[RTAX_IFP] = NULL;
814 info.rti_info[RTAX_IFA] = NULL;
816 } else if ((ifp = rt->rt_ifp) != NULL) {
817 rtm->rtm_index = ifp->if_index;
820 /* Check if we need to realloc storage */
821 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
822 if (len > alloc_len) {
823 struct rt_msghdr *new_rtm;
824 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
825 if (new_rtm == NULL) {
829 bcopy(rtm, new_rtm, rtm->rtm_msglen);
835 w.w_tmem = (caddr_t)rtm;
836 w.w_tmemsize = alloc_len;
837 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
839 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
840 rtm->rtm_flags = RTF_GATEWAY |
841 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
843 rtm->rtm_flags = rt->rt_flags;
844 rt_getmetrics(rt, &rtm->rtm_rmx);
845 rtm->rtm_addrs = info.rti_addrs;
858 * Check to see if we don't want our own messages.
860 if ((so->so_options & SO_USELOOPBACK) == 0) {
861 if (V_route_cb.any_count <= 1) {
867 /* There is another listener, so construct message */
873 if (rti_need_deembed) {
874 /* sin6_scope_id is recovered before sending rtm. */
875 sin6 = (struct sockaddr_in6 *)&ss;
876 for (i = 0; i < RTAX_MAX; i++) {
877 if (info.rti_info[i] == NULL)
879 if (info.rti_info[i]->sa_family != AF_INET6)
881 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
882 if (sa6_recoverscope(sin6) == 0)
883 bcopy(sin6, info.rti_info[i],
889 rtm->rtm_errno = error;
891 rtm->rtm_flags |= RTF_DONE;
893 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
894 if (m->m_pkthdr.len < rtm->rtm_msglen) {
897 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
898 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
904 m->m_flags |= RTS_FILTER_FIB;
907 * XXX insure we don't get a copy by
908 * invalidating our protocol
910 unsigned short family = rp->rcb_proto.sp_family;
911 rp->rcb_proto.sp_family = 0;
913 rp->rcb_proto.sp_family = family;
922 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
925 bzero(out, sizeof(*out));
926 out->rmx_mtu = rt->rt_mtu;
927 out->rmx_weight = rt->rt_weight;
928 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
929 /* Kernel -> userland timebase conversion. */
930 out->rmx_expire = rt->rt_expire ?
931 rt->rt_expire - time_uptime + time_second : 0;
935 * Extract the addresses of the passed sockaddrs.
936 * Do a little sanity checking so as to avoid bad memory references.
937 * This data is derived straight from userland.
940 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
945 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
946 if ((rtinfo->rti_addrs & (1 << i)) == 0)
948 sa = (struct sockaddr *)cp;
952 if (cp + sa->sa_len > cplim)
955 * there are no more.. quit now
956 * If there are more bits, they are in error.
957 * I've seen this. route(1) can evidently generate these.
958 * This causes kernel to core dump.
959 * for compatibility, If we see this, point to a safe address.
961 if (sa->sa_len == 0) {
962 rtinfo->rti_info[i] = &sa_zero;
963 return (0); /* should be EINVAL but for compat */
967 if (sa->sa_family == AF_INET6)
968 sa6_embedscope((struct sockaddr_in6 *)sa,
971 rtinfo->rti_info[i] = sa;
978 * Fill in @dmask with valid netmask leaving original @smask
979 * intact. Mostly used with radix netmasks.
981 static struct sockaddr *
982 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
983 struct sockaddr_storage *dmask)
985 if (dst == NULL || smask == NULL)
988 memset(dmask, 0, dst->sa_len);
989 memcpy(dmask, smask, smask->sa_len);
990 dmask->ss_len = dst->sa_len;
991 dmask->ss_family = dst->sa_family;
993 return ((struct sockaddr *)dmask);
997 * Writes information related to @rtinfo object to newly-allocated mbuf.
998 * Assumes MCLBYTES is enough to construct any message.
999 * Used for OS notifications of vaious events (if/ifa announces,etc)
1001 * Returns allocated mbuf or NULL on failure.
1003 static struct mbuf *
1004 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1006 struct rt_msghdr *rtm;
1009 struct sockaddr *sa;
1011 struct sockaddr_storage ss;
1012 struct sockaddr_in6 *sin6;
1020 len = sizeof(struct ifa_msghdr);
1025 len = sizeof(struct ifma_msghdr);
1029 len = sizeof(struct if_msghdr);
1032 case RTM_IFANNOUNCE:
1034 len = sizeof(struct if_announcemsghdr);
1038 len = sizeof(struct rt_msghdr);
1041 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1042 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1044 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1046 m = m_gethdr(M_NOWAIT, MT_DATA);
1050 m->m_pkthdr.len = m->m_len = len;
1051 rtm = mtod(m, struct rt_msghdr *);
1052 bzero((caddr_t)rtm, len);
1053 for (i = 0; i < RTAX_MAX; i++) {
1054 if ((sa = rtinfo->rti_info[i]) == NULL)
1056 rtinfo->rti_addrs |= (1 << i);
1059 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1060 sin6 = (struct sockaddr_in6 *)&ss;
1061 bcopy(sa, sin6, sizeof(*sin6));
1062 if (sa6_recoverscope(sin6) == 0)
1063 sa = (struct sockaddr *)sin6;
1066 m_copyback(m, len, dlen, (caddr_t)sa);
1069 if (m->m_pkthdr.len != len) {
1073 rtm->rtm_msglen = len;
1074 rtm->rtm_version = RTM_VERSION;
1075 rtm->rtm_type = type;
1080 * Writes information related to @rtinfo object to preallocated buffer.
1081 * Stores needed size in @plen. If @w is NULL, calculates size without
1083 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1085 * Returns 0 on success.
1089 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1092 int len, buflen = 0, dlen;
1094 struct rt_msghdr *rtm = NULL;
1096 struct sockaddr_storage ss;
1097 struct sockaddr_in6 *sin6;
1104 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1105 #ifdef COMPAT_FREEBSD32
1106 if (w->w_req->flags & SCTL_MASK32)
1107 len = sizeof(struct ifa_msghdrl32);
1110 len = sizeof(struct ifa_msghdrl);
1112 len = sizeof(struct ifa_msghdr);
1116 #ifdef COMPAT_FREEBSD32
1117 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1118 if (w->w_op == NET_RT_IFLISTL)
1119 len = sizeof(struct if_msghdrl32);
1121 len = sizeof(struct if_msghdr32);
1125 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1126 len = sizeof(struct if_msghdrl);
1128 len = sizeof(struct if_msghdr);
1132 len = sizeof(struct ifma_msghdr);
1136 len = sizeof(struct rt_msghdr);
1140 rtm = (struct rt_msghdr *)w->w_tmem;
1141 buflen = w->w_tmemsize - len;
1142 cp = (caddr_t)w->w_tmem + len;
1145 rtinfo->rti_addrs = 0;
1146 for (i = 0; i < RTAX_MAX; i++) {
1147 struct sockaddr *sa;
1149 if ((sa = rtinfo->rti_info[i]) == NULL)
1151 rtinfo->rti_addrs |= (1 << i);
1153 if (cp != NULL && buflen >= dlen) {
1155 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1156 sin6 = (struct sockaddr_in6 *)&ss;
1157 bcopy(sa, sin6, sizeof(*sin6));
1158 if (sa6_recoverscope(sin6) == 0)
1159 sa = (struct sockaddr *)sin6;
1162 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1165 } else if (cp != NULL) {
1167 * Buffer too small. Count needed size
1168 * and return with error.
1177 dlen = ALIGN(len) - len;
1186 /* fill header iff buffer is large enough */
1187 rtm->rtm_version = RTM_VERSION;
1188 rtm->rtm_type = type;
1189 rtm->rtm_msglen = len;
1194 if (w != NULL && cp == NULL)
1201 * This routine is called to generate a message from the routing
1202 * socket indicating that a redirect has occurred, a routing lookup
1203 * has failed, or that a protocol has detected timeouts to a particular
1207 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1210 struct rt_msghdr *rtm;
1212 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1214 if (V_route_cb.any_count == 0)
1216 m = rtsock_msg_mbuf(type, rtinfo);
1220 if (fibnum != RT_ALL_FIBS) {
1221 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1222 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1223 M_SETFIB(m, fibnum);
1224 m->m_flags |= RTS_FILTER_FIB;
1227 rtm = mtod(m, struct rt_msghdr *);
1228 rtm->rtm_flags = RTF_DONE | flags;
1229 rtm->rtm_errno = error;
1230 rtm->rtm_addrs = rtinfo->rti_addrs;
1231 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1235 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1238 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1242 * This routine is called to generate a message from the routing
1243 * socket indicating that the status of a network interface has changed.
1246 rt_ifmsg(struct ifnet *ifp)
1248 struct if_msghdr *ifm;
1250 struct rt_addrinfo info;
1252 if (V_route_cb.any_count == 0)
1254 bzero((caddr_t)&info, sizeof(info));
1255 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1258 ifm = mtod(m, struct if_msghdr *);
1259 ifm->ifm_index = ifp->if_index;
1260 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1261 if_data_copy(ifp, &ifm->ifm_data);
1263 rt_dispatch(m, AF_UNSPEC);
1267 * Announce interface address arrival/withdraw.
1268 * Please do not call directly, use rt_addrmsg().
1269 * Assume input data to be valid.
1270 * Returns 0 on success.
1273 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1275 struct rt_addrinfo info;
1276 struct sockaddr *sa;
1279 struct ifa_msghdr *ifam;
1280 struct ifnet *ifp = ifa->ifa_ifp;
1281 struct sockaddr_storage ss;
1283 if (V_route_cb.any_count == 0)
1286 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1288 bzero((caddr_t)&info, sizeof(info));
1289 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1290 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1291 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1292 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1293 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1294 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1296 ifam = mtod(m, struct ifa_msghdr *);
1297 ifam->ifam_index = ifp->if_index;
1298 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1299 ifam->ifam_flags = ifa->ifa_flags;
1300 ifam->ifam_addrs = info.rti_addrs;
1302 if (fibnum != RT_ALL_FIBS) {
1303 M_SETFIB(m, fibnum);
1304 m->m_flags |= RTS_FILTER_FIB;
1307 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1313 * Announce route addition/removal.
1314 * Please do not call directly, use rt_routemsg().
1315 * Note that @rt data MAY be inconsistent/invalid:
1316 * if some userland app sends us "invalid" route message (invalid mask,
1317 * no dst, wrong address families, etc...) we need to pass it back
1318 * to app (and any other rtsock consumers) with rtm_errno field set to
1321 * Returns 0 on success.
1324 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1327 struct rt_addrinfo info;
1328 struct sockaddr *sa;
1330 struct rt_msghdr *rtm;
1331 struct sockaddr_storage ss;
1333 if (V_route_cb.any_count == 0)
1336 bzero((caddr_t)&info, sizeof(info));
1337 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1338 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1339 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1340 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1342 rtm = mtod(m, struct rt_msghdr *);
1343 rtm->rtm_index = ifp->if_index;
1344 rtm->rtm_flags |= rt->rt_flags;
1345 rtm->rtm_errno = error;
1346 rtm->rtm_addrs = info.rti_addrs;
1348 if (fibnum != RT_ALL_FIBS) {
1349 M_SETFIB(m, fibnum);
1350 m->m_flags |= RTS_FILTER_FIB;
1353 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1359 * This is the analogue to the rt_newaddrmsg which performs the same
1360 * function but for multicast group memberhips. This is easier since
1361 * there is no route state to worry about.
1364 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1366 struct rt_addrinfo info;
1367 struct mbuf *m = NULL;
1368 struct ifnet *ifp = ifma->ifma_ifp;
1369 struct ifma_msghdr *ifmam;
1371 if (V_route_cb.any_count == 0)
1374 bzero((caddr_t)&info, sizeof(info));
1375 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1376 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1378 * If a link-layer address is present, present it as a ``gateway''
1379 * (similarly to how ARP entries, e.g., are presented).
1381 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1382 m = rtsock_msg_mbuf(cmd, &info);
1385 ifmam = mtod(m, struct ifma_msghdr *);
1386 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1388 ifmam->ifmam_index = ifp->if_index;
1389 ifmam->ifmam_addrs = info.rti_addrs;
1390 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1393 static struct mbuf *
1394 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1395 struct rt_addrinfo *info)
1397 struct if_announcemsghdr *ifan;
1400 if (V_route_cb.any_count == 0)
1402 bzero((caddr_t)info, sizeof(*info));
1403 m = rtsock_msg_mbuf(type, info);
1405 ifan = mtod(m, struct if_announcemsghdr *);
1406 ifan->ifan_index = ifp->if_index;
1407 strlcpy(ifan->ifan_name, ifp->if_xname,
1408 sizeof(ifan->ifan_name));
1409 ifan->ifan_what = what;
1415 * This is called to generate routing socket messages indicating
1416 * IEEE80211 wireless events.
1417 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1420 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1423 struct rt_addrinfo info;
1425 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1428 * Append the ieee80211 data. Try to stick it in the
1429 * mbuf containing the ifannounce msg; otherwise allocate
1430 * a new mbuf and append.
1432 * NB: we assume m is a single mbuf.
1434 if (data_len > M_TRAILINGSPACE(m)) {
1435 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1440 bcopy(data, mtod(n, void *), data_len);
1441 n->m_len = data_len;
1443 } else if (data_len > 0) {
1444 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1445 m->m_len += data_len;
1447 if (m->m_flags & M_PKTHDR)
1448 m->m_pkthdr.len += data_len;
1449 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1450 rt_dispatch(m, AF_UNSPEC);
1455 * This is called to generate routing socket messages indicating
1456 * network interface arrival and departure.
1459 rt_ifannouncemsg(struct ifnet *ifp, int what)
1462 struct rt_addrinfo info;
1464 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1466 rt_dispatch(m, AF_UNSPEC);
1470 rt_dispatch(struct mbuf *m, sa_family_t saf)
1475 * Preserve the family from the sockaddr, if any, in an m_tag for
1476 * use when injecting the mbuf into the routing socket buffer from
1479 if (saf != AF_UNSPEC) {
1480 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1486 *(unsigned short *)(tag + 1) = saf;
1487 m_tag_prepend(m, tag);
1491 m->m_pkthdr.rcvif = V_loif;
1497 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1501 * This is used in dumping the kernel table via sysctl().
1504 sysctl_dumpentry(struct radix_node *rn, void *vw)
1506 struct walkarg *w = vw;
1507 struct rtentry *rt = (struct rtentry *)rn;
1508 int error = 0, size;
1509 struct rt_addrinfo info;
1510 struct sockaddr_storage ss;
1512 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1514 if ((rt->rt_flags & RTF_HOST) == 0
1515 ? jailed_without_vnet(w->w_req->td->td_ucred)
1516 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1518 bzero((caddr_t)&info, sizeof(info));
1519 info.rti_info[RTAX_DST] = rt_key(rt);
1520 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1521 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1523 info.rti_info[RTAX_GENMASK] = 0;
1525 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1526 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1527 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1528 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1530 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1532 if (w->w_req && w->w_tmem) {
1533 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1535 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1536 rtm->rtm_flags = RTF_GATEWAY |
1537 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1539 rtm->rtm_flags = rt->rt_flags;
1540 rt_getmetrics(rt, &rtm->rtm_rmx);
1541 rtm->rtm_index = rt->rt_ifp->if_index;
1542 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1543 rtm->rtm_addrs = info.rti_addrs;
1544 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1551 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1552 struct walkarg *w, int len)
1554 struct if_msghdrl *ifm;
1555 struct if_data *ifd;
1557 ifm = (struct if_msghdrl *)w->w_tmem;
1559 #ifdef COMPAT_FREEBSD32
1560 if (w->w_req->flags & SCTL_MASK32) {
1561 struct if_msghdrl32 *ifm32;
1563 ifm32 = (struct if_msghdrl32 *)ifm;
1564 ifm32->ifm_addrs = info->rti_addrs;
1565 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1566 ifm32->ifm_index = ifp->if_index;
1567 ifm32->_ifm_spare1 = 0;
1568 ifm32->ifm_len = sizeof(*ifm32);
1569 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1570 ifd = &ifm32->ifm_data;
1574 ifm->ifm_addrs = info->rti_addrs;
1575 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1576 ifm->ifm_index = ifp->if_index;
1577 ifm->_ifm_spare1 = 0;
1578 ifm->ifm_len = sizeof(*ifm);
1579 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1580 ifd = &ifm->ifm_data;
1583 if_data_copy(ifp, ifd);
1585 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1589 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1590 struct walkarg *w, int len)
1592 struct if_msghdr *ifm;
1593 struct if_data *ifd;
1595 ifm = (struct if_msghdr *)w->w_tmem;
1597 #ifdef COMPAT_FREEBSD32
1598 if (w->w_req->flags & SCTL_MASK32) {
1599 struct if_msghdr32 *ifm32;
1601 ifm32 = (struct if_msghdr32 *)ifm;
1602 ifm32->ifm_addrs = info->rti_addrs;
1603 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1604 ifm32->ifm_index = ifp->if_index;
1605 ifd = &ifm32->ifm_data;
1609 ifm->ifm_addrs = info->rti_addrs;
1610 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1611 ifm->ifm_index = ifp->if_index;
1612 ifd = &ifm->ifm_data;
1615 if_data_copy(ifp, ifd);
1617 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1621 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1622 struct walkarg *w, int len)
1624 struct ifa_msghdrl *ifam;
1625 struct if_data *ifd;
1627 ifam = (struct ifa_msghdrl *)w->w_tmem;
1629 #ifdef COMPAT_FREEBSD32
1630 if (w->w_req->flags & SCTL_MASK32) {
1631 struct ifa_msghdrl32 *ifam32;
1633 ifam32 = (struct ifa_msghdrl32 *)ifam;
1634 ifam32->ifam_addrs = info->rti_addrs;
1635 ifam32->ifam_flags = ifa->ifa_flags;
1636 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1637 ifam32->_ifam_spare1 = 0;
1638 ifam32->ifam_len = sizeof(*ifam32);
1639 ifam32->ifam_data_off =
1640 offsetof(struct ifa_msghdrl32, ifam_data);
1641 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1642 ifd = &ifam32->ifam_data;
1646 ifam->ifam_addrs = info->rti_addrs;
1647 ifam->ifam_flags = ifa->ifa_flags;
1648 ifam->ifam_index = ifa->ifa_ifp->if_index;
1649 ifam->_ifam_spare1 = 0;
1650 ifam->ifam_len = sizeof(*ifam);
1651 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1652 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1653 ifd = &ifam->ifam_data;
1656 bzero(ifd, sizeof(*ifd));
1657 ifd->ifi_datalen = sizeof(struct if_data);
1658 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1659 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1660 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1661 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1663 /* Fixup if_data carp(4) vhid. */
1664 if (carp_get_vhid_p != NULL)
1665 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1667 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1671 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1672 struct walkarg *w, int len)
1674 struct ifa_msghdr *ifam;
1676 ifam = (struct ifa_msghdr *)w->w_tmem;
1677 ifam->ifam_addrs = info->rti_addrs;
1678 ifam->ifam_flags = ifa->ifa_flags;
1679 ifam->ifam_index = ifa->ifa_ifp->if_index;
1680 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1682 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1686 sysctl_iflist(int af, struct walkarg *w)
1690 struct rt_addrinfo info;
1692 struct sockaddr_storage ss;
1694 bzero((caddr_t)&info, sizeof(info));
1695 IFNET_RLOCK_NOSLEEP();
1696 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1697 if (w->w_arg && w->w_arg != ifp->if_index)
1701 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1702 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1705 info.rti_info[RTAX_IFP] = NULL;
1706 if (w->w_req && w->w_tmem) {
1707 if (w->w_op == NET_RT_IFLISTL)
1708 error = sysctl_iflist_ifml(ifp, &info, w, len);
1710 error = sysctl_iflist_ifm(ifp, &info, w, len);
1714 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1715 if (af && af != ifa->ifa_addr->sa_family)
1717 if (prison_if(w->w_req->td->td_ucred,
1718 ifa->ifa_addr) != 0)
1720 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1721 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1722 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1723 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1724 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1727 if (w->w_req && w->w_tmem) {
1728 if (w->w_op == NET_RT_IFLISTL)
1729 error = sysctl_iflist_ifaml(ifa, &info,
1732 error = sysctl_iflist_ifam(ifa, &info,
1738 IF_ADDR_RUNLOCK(ifp);
1739 info.rti_info[RTAX_IFA] = NULL;
1740 info.rti_info[RTAX_NETMASK] = NULL;
1741 info.rti_info[RTAX_BRD] = NULL;
1745 IF_ADDR_RUNLOCK(ifp);
1746 IFNET_RUNLOCK_NOSLEEP();
1751 sysctl_ifmalist(int af, struct walkarg *w)
1754 struct ifmultiaddr *ifma;
1755 struct rt_addrinfo info;
1759 bzero((caddr_t)&info, sizeof(info));
1760 IFNET_RLOCK_NOSLEEP();
1761 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1762 if (w->w_arg && w->w_arg != ifp->if_index)
1765 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1767 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1768 if (af && af != ifma->ifma_addr->sa_family)
1770 if (prison_if(w->w_req->td->td_ucred,
1771 ifma->ifma_addr) != 0)
1773 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1774 info.rti_info[RTAX_GATEWAY] =
1775 (ifma->ifma_addr->sa_family != AF_LINK) ?
1776 ifma->ifma_lladdr : NULL;
1777 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1780 if (w->w_req && w->w_tmem) {
1781 struct ifma_msghdr *ifmam;
1783 ifmam = (struct ifma_msghdr *)w->w_tmem;
1784 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1785 ifmam->ifmam_flags = 0;
1786 ifmam->ifmam_addrs = info.rti_addrs;
1787 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1789 IF_ADDR_RUNLOCK(ifp);
1794 IF_ADDR_RUNLOCK(ifp);
1797 IFNET_RUNLOCK_NOSLEEP();
1802 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1804 int *name = (int *)arg1;
1805 u_int namelen = arg2;
1806 struct rib_head *rnh = NULL; /* silence compiler. */
1807 int i, lim, error = EINVAL;
1816 if (name[1] == NET_RT_DUMP) {
1818 fib = req->td->td_proc->p_fibnum;
1819 else if (namelen == 4)
1820 fib = (name[3] == RT_ALL_FIBS) ?
1821 req->td->td_proc->p_fibnum : name[3];
1823 return ((namelen < 3) ? EISDIR : ENOTDIR);
1824 if (fib < 0 || fib >= rt_numfibs)
1826 } else if (namelen != 3)
1827 return ((namelen < 3) ? EISDIR : ENOTDIR);
1831 bzero(&w, sizeof(w));
1836 error = sysctl_wire_old_buffer(req, 0);
1841 * Allocate reply buffer in advance.
1842 * All rtsock messages has maximum length of u_short.
1844 w.w_tmemsize = 65536;
1845 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1851 if (af == 0) { /* dump all tables */
1854 } else /* dump only one table */
1858 * take care of llinfo entries, the caller must
1861 if (w.w_op == NET_RT_FLAGS &&
1862 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1864 error = lltable_sysctl_dumparp(af, w.w_req);
1870 * take care of routing entries
1872 for (error = 0; error == 0 && i <= lim; i++) {
1873 rnh = rt_tables_get_rnh(fib, i);
1876 error = rnh->rnh_walktree(&rnh->head,
1877 sysctl_dumpentry, &w);
1880 error = EAFNOSUPPORT;
1885 case NET_RT_IFLISTL:
1886 error = sysctl_iflist(af, &w);
1889 case NET_RT_IFMALIST:
1890 error = sysctl_ifmalist(af, &w);
1894 free(w.w_tmem, M_TEMP);
1898 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1901 * Definitions of protocols supported in the ROUTE domain.
1904 static struct domain routedomain; /* or at least forward */
1906 static struct protosw routesw[] = {
1908 .pr_type = SOCK_RAW,
1909 .pr_domain = &routedomain,
1910 .pr_flags = PR_ATOMIC|PR_ADDR,
1911 .pr_output = route_output,
1912 .pr_ctlinput = raw_ctlinput,
1913 .pr_init = raw_init,
1914 .pr_usrreqs = &route_usrreqs
1918 static struct domain routedomain = {
1919 .dom_family = PF_ROUTE,
1920 .dom_name = "route",
1921 .dom_protosw = routesw,
1922 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1925 VNET_DOMAIN_SET(route);