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>
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/ip_carp.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/scope6_var.h>
72 #ifdef COMPAT_FREEBSD32
73 #include <sys/mount.h>
74 #include <compat/freebsd32/freebsd32.h>
83 struct if_data ifm_data;
95 uint16_t ifm_data_off;
96 struct if_data ifm_data;
99 struct ifa_msghdrl32 {
100 uint16_t ifam_msglen;
101 uint8_t ifam_version;
106 uint16_t _ifam_spare1;
108 uint16_t ifam_data_off;
110 struct if_data ifam_data;
112 #endif /* COMPAT_FREEBSD32 */
114 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
116 /* NB: these are not modified */
117 static struct sockaddr route_src = { 2, PF_ROUTE, };
118 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
120 /* These are external hooks for CARP. */
121 int (*carp_get_vhid_p)(struct ifaddr *);
124 * Used by rtsock/raw_input callback code to decide whether to filter the update
125 * notification to a socket bound to a particular FIB.
127 #define RTS_FILTER_FIB M_PROTO8
130 int ip_count; /* attached w/ AF_INET */
131 int ip6_count; /* attached w/ AF_INET6 */
132 int any_count; /* total attached */
134 static VNET_DEFINE(route_cb_t, route_cb);
135 #define V_route_cb VNET(route_cb)
137 struct mtx rtsock_mtx;
138 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
140 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
141 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
142 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
144 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
150 struct sysctl_req *w_req;
153 static void rts_input(struct mbuf *m);
154 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
155 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
156 struct walkarg *w, int *plen);
157 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
158 struct rt_addrinfo *rtinfo);
159 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
160 static int sysctl_iflist(int af, struct walkarg *w);
161 static int sysctl_ifmalist(int af, struct walkarg *w);
162 static int route_output(struct mbuf *m, struct socket *so, ...);
163 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
164 static void rt_dispatch(struct mbuf *, sa_family_t);
165 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
166 struct sockaddr *smask, struct sockaddr_storage *dmask);
168 static struct netisr_handler rtsock_nh = {
170 .nh_handler = rts_input,
171 .nh_proto = NETISR_ROUTE,
172 .nh_policy = NETISR_POLICY_SOURCE,
176 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
180 netisr_getqlimit(&rtsock_nh, &qlimit);
181 error = sysctl_handle_int(oidp, &qlimit, 0, req);
182 if (error || !req->newptr)
186 return (netisr_setqlimit(&rtsock_nh, qlimit));
188 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
189 0, 0, sysctl_route_netisr_maxqlen, "I",
190 "maximum routing socket dispatch queue length");
197 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
198 rtsock_nh.nh_qlimit = tmp;
199 netisr_register(&rtsock_nh);
201 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
204 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
209 KASSERT(m != NULL, ("%s: m is NULL", __func__));
210 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
211 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
213 /* No filtering requested. */
214 if ((m->m_flags & RTS_FILTER_FIB) == 0)
217 /* Check if it is a rts and the fib matches the one of the socket. */
218 fibnum = M_GETFIB(m);
219 if (proto->sp_family != PF_ROUTE ||
220 rp->rcb_socket == NULL ||
221 rp->rcb_socket->so_fibnum == fibnum)
224 /* Filtering requested and no match, the socket shall be skipped. */
229 rts_input(struct mbuf *m)
231 struct sockproto route_proto;
232 unsigned short *family;
235 route_proto.sp_family = PF_ROUTE;
236 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
238 family = (unsigned short *)(tag + 1);
239 route_proto.sp_protocol = *family;
240 m_tag_delete(m, tag);
242 route_proto.sp_protocol = 0;
244 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
248 * It really doesn't make any sense at all for this code to share much
249 * with raw_usrreq.c, since its functionality is so restricted. XXX
252 rts_abort(struct socket *so)
255 raw_usrreqs.pru_abort(so);
259 rts_close(struct socket *so)
262 raw_usrreqs.pru_close(so);
265 /* pru_accept is EOPNOTSUPP */
268 rts_attach(struct socket *so, int proto, struct thread *td)
273 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
276 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
280 so->so_pcb = (caddr_t)rp;
281 so->so_fibnum = td->td_proc->p_fibnum;
282 error = raw_attach(so, proto);
290 switch(rp->rcb_proto.sp_protocol) {
292 V_route_cb.ip_count++;
295 V_route_cb.ip6_count++;
298 V_route_cb.any_count++;
301 so->so_options |= SO_USELOOPBACK;
306 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
309 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
313 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
316 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
319 /* pru_connect2 is EOPNOTSUPP */
320 /* pru_control is EOPNOTSUPP */
323 rts_detach(struct socket *so)
325 struct rawcb *rp = sotorawcb(so);
327 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
330 switch(rp->rcb_proto.sp_protocol) {
332 V_route_cb.ip_count--;
335 V_route_cb.ip6_count--;
338 V_route_cb.any_count--;
340 raw_usrreqs.pru_detach(so);
344 rts_disconnect(struct socket *so)
347 return (raw_usrreqs.pru_disconnect(so));
350 /* pru_listen is EOPNOTSUPP */
353 rts_peeraddr(struct socket *so, struct sockaddr **nam)
356 return (raw_usrreqs.pru_peeraddr(so, nam));
359 /* pru_rcvd is EOPNOTSUPP */
360 /* pru_rcvoob is EOPNOTSUPP */
363 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
364 struct mbuf *control, struct thread *td)
367 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
370 /* pru_sense is null */
373 rts_shutdown(struct socket *so)
376 return (raw_usrreqs.pru_shutdown(so));
380 rts_sockaddr(struct socket *so, struct sockaddr **nam)
383 return (raw_usrreqs.pru_sockaddr(so, nam));
386 static struct pr_usrreqs route_usrreqs = {
387 .pru_abort = rts_abort,
388 .pru_attach = rts_attach,
389 .pru_bind = rts_bind,
390 .pru_connect = rts_connect,
391 .pru_detach = rts_detach,
392 .pru_disconnect = rts_disconnect,
393 .pru_peeraddr = rts_peeraddr,
394 .pru_send = rts_send,
395 .pru_shutdown = rts_shutdown,
396 .pru_sockaddr = rts_sockaddr,
397 .pru_close = rts_close,
400 #ifndef _SOCKADDR_UNION_DEFINED
401 #define _SOCKADDR_UNION_DEFINED
403 * The union of all possible address formats we handle.
405 union sockaddr_union {
407 struct sockaddr_in sin;
408 struct sockaddr_in6 sin6;
410 #endif /* _SOCKADDR_UNION_DEFINED */
413 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
414 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
417 /* First, see if the returned address is part of the jail. */
418 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
419 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
423 switch (info->rti_info[RTAX_DST]->sa_family) {
433 * Try to find an address on the given outgoing interface
434 * that belongs to the jail.
437 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
440 if (sa->sa_family != AF_INET)
442 ia = ((struct sockaddr_in *)sa)->sin_addr;
443 if (prison_check_ip4(cred, &ia) == 0) {
448 IF_ADDR_RUNLOCK(ifp);
451 * As a last resort return the 'default' jail address.
453 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
455 if (prison_get_ip4(cred, &ia) != 0)
458 bzero(&saun->sin, sizeof(struct sockaddr_in));
459 saun->sin.sin_len = sizeof(struct sockaddr_in);
460 saun->sin.sin_family = AF_INET;
461 saun->sin.sin_addr.s_addr = ia.s_addr;
462 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
475 * Try to find an address on the given outgoing interface
476 * that belongs to the jail.
479 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
482 if (sa->sa_family != AF_INET6)
484 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
485 &ia6, sizeof(struct in6_addr));
486 if (prison_check_ip6(cred, &ia6) == 0) {
491 IF_ADDR_RUNLOCK(ifp);
494 * As a last resort return the 'default' jail address.
496 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
498 if (prison_get_ip6(cred, &ia6) != 0)
501 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
502 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
503 saun->sin6.sin6_family = AF_INET6;
504 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
505 if (sa6_recoverscope(&saun->sin6) != 0)
507 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
519 route_output(struct mbuf *m, struct socket *so, ...)
521 struct rt_msghdr *rtm = NULL;
522 struct rtentry *rt = NULL;
523 struct radix_node_head *rnh;
524 struct rt_addrinfo info;
525 struct sockaddr_storage ss;
527 struct sockaddr_in6 *sin6;
528 int i, rti_need_deembed = 0;
530 int alloc_len = 0, len, error = 0, fibnum;
531 struct ifnet *ifp = NULL;
532 union sockaddr_union saun;
533 sa_family_t saf = AF_UNSPEC;
534 struct rawcb *rp = NULL;
537 fibnum = so->so_fibnum;
539 #define senderr(e) { error = e; goto flush;}
540 if (m == NULL || ((m->m_len < sizeof(long)) &&
541 (m = m_pullup(m, sizeof(long))) == NULL))
543 if ((m->m_flags & M_PKTHDR) == 0)
544 panic("route_output");
545 len = m->m_pkthdr.len;
546 if (len < sizeof(*rtm) ||
547 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
551 * Most of current messages are in range 200-240 bytes,
552 * minimize possible re-allocation on reply using larger size
553 * buffer aligned on 1k boundaty.
555 alloc_len = roundup2(len, 1024);
556 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
559 m_copydata(m, 0, len, (caddr_t)rtm);
560 bzero(&info, sizeof(info));
561 bzero(&w, sizeof(w));
563 if (rtm->rtm_version != RTM_VERSION) {
564 /* Do not touch message since format is unknown */
567 senderr(EPROTONOSUPPORT);
571 * Starting from here, it is possible
572 * to alter original message and insert
573 * caller PID and error value.
576 rtm->rtm_pid = curproc->p_pid;
577 info.rti_addrs = rtm->rtm_addrs;
579 info.rti_mflags = rtm->rtm_inits;
580 info.rti_rmx = &rtm->rtm_rmx;
583 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
584 * link-local address because rtrequest requires addresses with
587 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
590 info.rti_flags = rtm->rtm_flags;
591 if (info.rti_info[RTAX_DST] == NULL ||
592 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
593 (info.rti_info[RTAX_GATEWAY] != NULL &&
594 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
596 saf = info.rti_info[RTAX_DST]->sa_family;
598 * Verify that the caller has the appropriate privilege; RTM_GET
599 * is the only operation the non-superuser is allowed.
601 if (rtm->rtm_type != RTM_GET) {
602 error = priv_check(curthread, PRIV_NET_ROUTE);
608 * The given gateway address may be an interface address.
609 * For example, issuing a "route change" command on a route
610 * entry that was created from a tunnel, and the gateway
611 * address given is the local end point. In this case the
612 * RTF_GATEWAY flag must be cleared or the destination will
613 * not be reachable even though there is no error message.
615 if (info.rti_info[RTAX_GATEWAY] != NULL &&
616 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
619 bzero(&gw_ro, sizeof(gw_ro));
620 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
621 rtalloc_ign_fib(&gw_ro, 0, fibnum);
623 * A host route through the loopback interface is
624 * installed for each interface adddress. In pre 8.0
625 * releases the interface address of a PPP link type
626 * is not reachable locally. This behavior is fixed as
627 * part of the new L2/L3 redesign and rewrite work. The
628 * signature of this interface address route is the
629 * AF_LINK sa_family type of the rt_gateway, and the
630 * rt_ifp has the IFF_LOOPBACK flag set.
632 if (gw_ro.ro_rt != NULL &&
633 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
634 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
635 info.rti_flags &= ~RTF_GATEWAY;
636 info.rti_flags |= RTF_GWFLAG_COMPAT;
638 if (gw_ro.ro_rt != NULL)
642 switch (rtm->rtm_type) {
643 struct rtentry *saved_nrt;
647 if (info.rti_info[RTAX_GATEWAY] == NULL)
651 /* support for new ARP code */
652 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
653 (rtm->rtm_flags & RTF_LLDATA) != 0) {
654 error = lla_rt_output(rtm, &info);
657 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
661 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
663 if (error == 0 && saved_nrt != NULL) {
665 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
668 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
669 RT_REMREF(saved_nrt);
670 RT_UNLOCK(saved_nrt);
676 /* support for new ARP code */
677 if (info.rti_info[RTAX_GATEWAY] &&
678 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
679 (rtm->rtm_flags & RTF_LLDATA) != 0) {
680 error = lla_rt_output(rtm, &info);
683 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
687 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
694 /* rt_msg2() will not be used when RTM_DELETE fails. */
695 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
700 rnh = rt_tables_get_rnh(fibnum, saf);
702 senderr(EAFNOSUPPORT);
704 RADIX_NODE_HEAD_RLOCK(rnh);
706 if (info.rti_info[RTAX_NETMASK] == NULL &&
707 rtm->rtm_type == RTM_GET) {
709 * Provide logest prefix match for
710 * address lookup (no mask).
711 * 'route -n get addr'
713 rt = (struct rtentry *) rnh->rnh_matchaddr(
714 info.rti_info[RTAX_DST], rnh);
716 rt = (struct rtentry *) rnh->rnh_lookup(
717 info.rti_info[RTAX_DST],
718 info.rti_info[RTAX_NETMASK], rnh);
721 RADIX_NODE_HEAD_RUNLOCK(rnh);
726 * for RTM_CHANGE/LOCK, if we got multipath routes,
727 * we require users to specify a matching RTAX_GATEWAY.
729 * for RTM_GET, gate is optional even with multipath.
730 * if gate == NULL the first match is returned.
731 * (no need to call rt_mpath_matchgate if gate == NULL)
733 if (rn_mpath_capable(rnh) &&
734 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
735 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
737 RADIX_NODE_HEAD_RUNLOCK(rnh);
743 * If performing proxied L2 entry insertion, and
744 * the actual PPP host entry is found, perform
745 * another search to retrieve the prefix route of
746 * the local end point of the PPP link.
748 if (rtm->rtm_flags & RTF_ANNOUNCE) {
749 struct sockaddr laddr;
751 if (rt->rt_ifp != NULL &&
752 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
755 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
758 rt_maskedcopy(ifa->ifa_addr,
762 rt_maskedcopy(rt->rt_ifa->ifa_addr,
764 rt->rt_ifa->ifa_netmask);
766 * refactor rt and no lock operation necessary
768 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
770 RADIX_NODE_HEAD_RUNLOCK(rnh);
776 RADIX_NODE_HEAD_RUNLOCK(rnh);
780 if ((rt->rt_flags & RTF_HOST) == 0
781 ? jailed_without_vnet(curthread->td_ucred)
782 : prison_if(curthread->td_ucred,
787 info.rti_info[RTAX_DST] = rt_key(rt);
788 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
789 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
791 info.rti_info[RTAX_GENMASK] = 0;
792 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
795 info.rti_info[RTAX_IFP] =
796 ifp->if_addr->ifa_addr;
797 error = rtm_get_jailed(&info, ifp, rt,
798 &saun, curthread->td_ucred);
803 if (ifp->if_flags & IFF_POINTOPOINT)
804 info.rti_info[RTAX_BRD] =
805 rt->rt_ifa->ifa_dstaddr;
806 rtm->rtm_index = ifp->if_index;
808 info.rti_info[RTAX_IFP] = NULL;
809 info.rti_info[RTAX_IFA] = NULL;
811 } else if ((ifp = rt->rt_ifp) != NULL) {
812 rtm->rtm_index = ifp->if_index;
815 /* Check if we need to realloc storage */
816 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
817 if (len > alloc_len) {
818 struct rt_msghdr *new_rtm;
819 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
820 if (new_rtm == NULL) {
824 bcopy(rtm, new_rtm, rtm->rtm_msglen);
830 w.w_tmem = (caddr_t)rtm;
831 w.w_tmemsize = alloc_len;
832 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
834 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
835 rtm->rtm_flags = RTF_GATEWAY |
836 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
838 rtm->rtm_flags = rt->rt_flags;
839 rt_getmetrics(rt, &rtm->rtm_rmx);
840 rtm->rtm_addrs = info.rti_addrs;
853 * Check to see if we don't want our own messages.
855 if ((so->so_options & SO_USELOOPBACK) == 0) {
856 if (V_route_cb.any_count <= 1) {
862 /* There is another listener, so construct message */
868 if (rti_need_deembed) {
869 /* sin6_scope_id is recovered before sending rtm. */
870 sin6 = (struct sockaddr_in6 *)&ss;
871 for (i = 0; i < RTAX_MAX; i++) {
872 if (info.rti_info[i] == NULL)
874 if (info.rti_info[i]->sa_family != AF_INET6)
876 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
877 if (sa6_recoverscope(sin6) == 0)
878 bcopy(sin6, info.rti_info[i],
884 rtm->rtm_errno = error;
886 rtm->rtm_flags |= RTF_DONE;
888 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
889 if (m->m_pkthdr.len < rtm->rtm_msglen) {
892 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
893 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
899 m->m_flags |= RTS_FILTER_FIB;
902 * XXX insure we don't get a copy by
903 * invalidating our protocol
905 unsigned short family = rp->rcb_proto.sp_family;
906 rp->rcb_proto.sp_family = 0;
908 rp->rcb_proto.sp_family = family;
917 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
920 bzero(out, sizeof(*out));
921 out->rmx_mtu = rt->rt_mtu;
922 out->rmx_weight = rt->rt_weight;
923 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
924 /* Kernel -> userland timebase conversion. */
925 out->rmx_expire = rt->rt_expire ?
926 rt->rt_expire - time_uptime + time_second : 0;
930 * Extract the addresses of the passed sockaddrs.
931 * Do a little sanity checking so as to avoid bad memory references.
932 * This data is derived straight from userland.
935 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
940 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
941 if ((rtinfo->rti_addrs & (1 << i)) == 0)
943 sa = (struct sockaddr *)cp;
947 if (cp + sa->sa_len > cplim)
950 * there are no more.. quit now
951 * If there are more bits, they are in error.
952 * I've seen this. route(1) can evidently generate these.
953 * This causes kernel to core dump.
954 * for compatibility, If we see this, point to a safe address.
956 if (sa->sa_len == 0) {
957 rtinfo->rti_info[i] = &sa_zero;
958 return (0); /* should be EINVAL but for compat */
962 if (sa->sa_family == AF_INET6)
963 sa6_embedscope((struct sockaddr_in6 *)sa,
966 rtinfo->rti_info[i] = sa;
973 * Fill in @dmask with valid netmask leaving original @smask
974 * intact. Mostly used with radix netmasks.
976 static struct sockaddr *
977 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
978 struct sockaddr_storage *dmask)
980 if (dst == NULL || smask == NULL)
983 memset(dmask, 0, dst->sa_len);
984 memcpy(dmask, smask, smask->sa_len);
985 dmask->ss_len = dst->sa_len;
986 dmask->ss_family = dst->sa_family;
988 return ((struct sockaddr *)dmask);
992 * Writes information related to @rtinfo object to newly-allocated mbuf.
993 * Assumes MCLBYTES is enough to construct any message.
994 * Used for OS notifications of vaious events (if/ifa announces,etc)
996 * Returns allocated mbuf or NULL on failure.
999 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1001 struct rt_msghdr *rtm;
1004 struct sockaddr *sa;
1006 struct sockaddr_storage ss;
1007 struct sockaddr_in6 *sin6;
1015 len = sizeof(struct ifa_msghdr);
1020 len = sizeof(struct ifma_msghdr);
1024 len = sizeof(struct if_msghdr);
1027 case RTM_IFANNOUNCE:
1029 len = sizeof(struct if_announcemsghdr);
1033 len = sizeof(struct rt_msghdr);
1036 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1037 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1039 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1041 m = m_gethdr(M_NOWAIT, MT_DATA);
1045 m->m_pkthdr.len = m->m_len = len;
1046 rtm = mtod(m, struct rt_msghdr *);
1047 bzero((caddr_t)rtm, len);
1048 for (i = 0; i < RTAX_MAX; i++) {
1049 if ((sa = rtinfo->rti_info[i]) == NULL)
1051 rtinfo->rti_addrs |= (1 << i);
1054 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1055 sin6 = (struct sockaddr_in6 *)&ss;
1056 bcopy(sa, sin6, sizeof(*sin6));
1057 if (sa6_recoverscope(sin6) == 0)
1058 sa = (struct sockaddr *)sin6;
1061 m_copyback(m, len, dlen, (caddr_t)sa);
1064 if (m->m_pkthdr.len != len) {
1068 rtm->rtm_msglen = len;
1069 rtm->rtm_version = RTM_VERSION;
1070 rtm->rtm_type = type;
1075 * Writes information related to @rtinfo object to preallocated buffer.
1076 * Stores needed size in @plen. If @w is NULL, calculates size without
1078 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1080 * Returns 0 on success.
1084 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1087 int len, buflen = 0, dlen;
1089 struct rt_msghdr *rtm = NULL;
1091 struct sockaddr_storage ss;
1092 struct sockaddr_in6 *sin6;
1099 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1100 #ifdef COMPAT_FREEBSD32
1101 if (w->w_req->flags & SCTL_MASK32)
1102 len = sizeof(struct ifa_msghdrl32);
1105 len = sizeof(struct ifa_msghdrl);
1107 len = sizeof(struct ifa_msghdr);
1111 #ifdef COMPAT_FREEBSD32
1112 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1113 if (w->w_op == NET_RT_IFLISTL)
1114 len = sizeof(struct if_msghdrl32);
1116 len = sizeof(struct if_msghdr32);
1120 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1121 len = sizeof(struct if_msghdrl);
1123 len = sizeof(struct if_msghdr);
1127 len = sizeof(struct ifma_msghdr);
1131 len = sizeof(struct rt_msghdr);
1135 rtm = (struct rt_msghdr *)w->w_tmem;
1136 buflen = w->w_tmemsize - len;
1137 cp = (caddr_t)w->w_tmem + len;
1140 rtinfo->rti_addrs = 0;
1141 for (i = 0; i < RTAX_MAX; i++) {
1142 struct sockaddr *sa;
1144 if ((sa = rtinfo->rti_info[i]) == NULL)
1146 rtinfo->rti_addrs |= (1 << i);
1148 if (cp != NULL && buflen >= dlen) {
1150 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1151 sin6 = (struct sockaddr_in6 *)&ss;
1152 bcopy(sa, sin6, sizeof(*sin6));
1153 if (sa6_recoverscope(sin6) == 0)
1154 sa = (struct sockaddr *)sin6;
1157 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1160 } else if (cp != NULL) {
1162 * Buffer too small. Count needed size
1163 * and return with error.
1172 dlen = ALIGN(len) - len;
1181 /* fill header iff buffer is large enough */
1182 rtm->rtm_version = RTM_VERSION;
1183 rtm->rtm_type = type;
1184 rtm->rtm_msglen = len;
1189 if (w != NULL && cp == NULL)
1196 * This routine is called to generate a message from the routing
1197 * socket indicating that a redirect has occured, a routing lookup
1198 * has failed, or that a protocol has detected timeouts to a particular
1202 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1205 struct rt_msghdr *rtm;
1207 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1209 if (V_route_cb.any_count == 0)
1211 m = rtsock_msg_mbuf(type, rtinfo);
1215 if (fibnum != RT_ALL_FIBS) {
1216 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1217 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1218 M_SETFIB(m, fibnum);
1219 m->m_flags |= RTS_FILTER_FIB;
1222 rtm = mtod(m, struct rt_msghdr *);
1223 rtm->rtm_flags = RTF_DONE | flags;
1224 rtm->rtm_errno = error;
1225 rtm->rtm_addrs = rtinfo->rti_addrs;
1226 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1230 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1233 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1237 * This routine is called to generate a message from the routing
1238 * socket indicating that the status of a network interface has changed.
1241 rt_ifmsg(struct ifnet *ifp)
1243 struct if_msghdr *ifm;
1245 struct rt_addrinfo info;
1247 if (V_route_cb.any_count == 0)
1249 bzero((caddr_t)&info, sizeof(info));
1250 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1253 ifm = mtod(m, struct if_msghdr *);
1254 ifm->ifm_index = ifp->if_index;
1255 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1256 if_data_copy(ifp, &ifm->ifm_data);
1258 rt_dispatch(m, AF_UNSPEC);
1262 * Announce interface address arrival/withdraw.
1263 * Please do not call directly, use rt_addrmsg().
1264 * Assume input data to be valid.
1265 * Returns 0 on success.
1268 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1270 struct rt_addrinfo info;
1271 struct sockaddr *sa;
1274 struct ifa_msghdr *ifam;
1275 struct ifnet *ifp = ifa->ifa_ifp;
1276 struct sockaddr_storage ss;
1278 if (V_route_cb.any_count == 0)
1281 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1283 bzero((caddr_t)&info, sizeof(info));
1284 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1285 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1286 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1287 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1288 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1289 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1291 ifam = mtod(m, struct ifa_msghdr *);
1292 ifam->ifam_index = ifp->if_index;
1293 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1294 ifam->ifam_flags = ifa->ifa_flags;
1295 ifam->ifam_addrs = info.rti_addrs;
1297 if (fibnum != RT_ALL_FIBS) {
1298 M_SETFIB(m, fibnum);
1299 m->m_flags |= RTS_FILTER_FIB;
1302 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1308 * Announce route addition/removal.
1309 * Please do not call directly, use rt_routemsg().
1310 * Note that @rt data MAY be inconsistent/invalid:
1311 * if some userland app sends us "invalid" route message (invalid mask,
1312 * no dst, wrong address families, etc...) we need to pass it back
1313 * to app (and any other rtsock consumers) with rtm_errno field set to
1316 * Returns 0 on success.
1319 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1322 struct rt_addrinfo info;
1323 struct sockaddr *sa;
1325 struct rt_msghdr *rtm;
1326 struct sockaddr_storage ss;
1328 if (V_route_cb.any_count == 0)
1331 bzero((caddr_t)&info, sizeof(info));
1332 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1333 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1334 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1335 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1337 rtm = mtod(m, struct rt_msghdr *);
1338 rtm->rtm_index = ifp->if_index;
1339 rtm->rtm_flags |= rt->rt_flags;
1340 rtm->rtm_errno = error;
1341 rtm->rtm_addrs = info.rti_addrs;
1343 if (fibnum != RT_ALL_FIBS) {
1344 M_SETFIB(m, fibnum);
1345 m->m_flags |= RTS_FILTER_FIB;
1348 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1354 * This is the analogue to the rt_newaddrmsg which performs the same
1355 * function but for multicast group memberhips. This is easier since
1356 * there is no route state to worry about.
1359 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1361 struct rt_addrinfo info;
1362 struct mbuf *m = NULL;
1363 struct ifnet *ifp = ifma->ifma_ifp;
1364 struct ifma_msghdr *ifmam;
1366 if (V_route_cb.any_count == 0)
1369 bzero((caddr_t)&info, sizeof(info));
1370 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1371 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1373 * If a link-layer address is present, present it as a ``gateway''
1374 * (similarly to how ARP entries, e.g., are presented).
1376 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1377 m = rtsock_msg_mbuf(cmd, &info);
1380 ifmam = mtod(m, struct ifma_msghdr *);
1381 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1383 ifmam->ifmam_index = ifp->if_index;
1384 ifmam->ifmam_addrs = info.rti_addrs;
1385 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1388 static struct mbuf *
1389 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1390 struct rt_addrinfo *info)
1392 struct if_announcemsghdr *ifan;
1395 if (V_route_cb.any_count == 0)
1397 bzero((caddr_t)info, sizeof(*info));
1398 m = rtsock_msg_mbuf(type, info);
1400 ifan = mtod(m, struct if_announcemsghdr *);
1401 ifan->ifan_index = ifp->if_index;
1402 strlcpy(ifan->ifan_name, ifp->if_xname,
1403 sizeof(ifan->ifan_name));
1404 ifan->ifan_what = what;
1410 * This is called to generate routing socket messages indicating
1411 * IEEE80211 wireless events.
1412 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1415 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1418 struct rt_addrinfo info;
1420 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1423 * Append the ieee80211 data. Try to stick it in the
1424 * mbuf containing the ifannounce msg; otherwise allocate
1425 * a new mbuf and append.
1427 * NB: we assume m is a single mbuf.
1429 if (data_len > M_TRAILINGSPACE(m)) {
1430 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1435 bcopy(data, mtod(n, void *), data_len);
1436 n->m_len = data_len;
1438 } else if (data_len > 0) {
1439 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1440 m->m_len += data_len;
1442 if (m->m_flags & M_PKTHDR)
1443 m->m_pkthdr.len += data_len;
1444 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1445 rt_dispatch(m, AF_UNSPEC);
1450 * This is called to generate routing socket messages indicating
1451 * network interface arrival and departure.
1454 rt_ifannouncemsg(struct ifnet *ifp, int what)
1457 struct rt_addrinfo info;
1459 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1461 rt_dispatch(m, AF_UNSPEC);
1465 rt_dispatch(struct mbuf *m, sa_family_t saf)
1470 * Preserve the family from the sockaddr, if any, in an m_tag for
1471 * use when injecting the mbuf into the routing socket buffer from
1474 if (saf != AF_UNSPEC) {
1475 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1481 *(unsigned short *)(tag + 1) = saf;
1482 m_tag_prepend(m, tag);
1486 m->m_pkthdr.rcvif = V_loif;
1492 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1496 * This is used in dumping the kernel table via sysctl().
1499 sysctl_dumpentry(struct radix_node *rn, void *vw)
1501 struct walkarg *w = vw;
1502 struct rtentry *rt = (struct rtentry *)rn;
1503 int error = 0, size;
1504 struct rt_addrinfo info;
1505 struct sockaddr_storage ss;
1507 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1509 if ((rt->rt_flags & RTF_HOST) == 0
1510 ? jailed_without_vnet(w->w_req->td->td_ucred)
1511 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1513 bzero((caddr_t)&info, sizeof(info));
1514 info.rti_info[RTAX_DST] = rt_key(rt);
1515 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1516 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1518 info.rti_info[RTAX_GENMASK] = 0;
1520 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1521 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1522 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1523 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1525 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1527 if (w->w_req && w->w_tmem) {
1528 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1530 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1531 rtm->rtm_flags = RTF_GATEWAY |
1532 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1534 rtm->rtm_flags = rt->rt_flags;
1535 rt_getmetrics(rt, &rtm->rtm_rmx);
1536 rtm->rtm_index = rt->rt_ifp->if_index;
1537 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1538 rtm->rtm_addrs = info.rti_addrs;
1539 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1546 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1547 struct walkarg *w, int len)
1549 struct if_msghdrl *ifm;
1550 struct if_data *ifd;
1552 ifm = (struct if_msghdrl *)w->w_tmem;
1554 #ifdef COMPAT_FREEBSD32
1555 if (w->w_req->flags & SCTL_MASK32) {
1556 struct if_msghdrl32 *ifm32;
1558 ifm32 = (struct if_msghdrl32 *)ifm;
1559 ifm32->ifm_addrs = info->rti_addrs;
1560 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1561 ifm32->ifm_index = ifp->if_index;
1562 ifm32->_ifm_spare1 = 0;
1563 ifm32->ifm_len = sizeof(*ifm32);
1564 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1565 ifd = &ifm32->ifm_data;
1569 ifm->ifm_addrs = info->rti_addrs;
1570 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1571 ifm->ifm_index = ifp->if_index;
1572 ifm->_ifm_spare1 = 0;
1573 ifm->ifm_len = sizeof(*ifm);
1574 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1575 ifd = &ifm->ifm_data;
1578 if_data_copy(ifp, ifd);
1580 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1584 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1585 struct walkarg *w, int len)
1587 struct if_msghdr *ifm;
1588 struct if_data *ifd;
1590 ifm = (struct if_msghdr *)w->w_tmem;
1592 #ifdef COMPAT_FREEBSD32
1593 if (w->w_req->flags & SCTL_MASK32) {
1594 struct if_msghdr32 *ifm32;
1596 ifm32 = (struct if_msghdr32 *)ifm;
1597 ifm32->ifm_addrs = info->rti_addrs;
1598 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1599 ifm32->ifm_index = ifp->if_index;
1600 ifd = &ifm32->ifm_data;
1604 ifm->ifm_addrs = info->rti_addrs;
1605 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1606 ifm->ifm_index = ifp->if_index;
1607 ifd = &ifm->ifm_data;
1610 if_data_copy(ifp, ifd);
1612 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1616 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1617 struct walkarg *w, int len)
1619 struct ifa_msghdrl *ifam;
1620 struct if_data *ifd;
1622 ifam = (struct ifa_msghdrl *)w->w_tmem;
1624 #ifdef COMPAT_FREEBSD32
1625 if (w->w_req->flags & SCTL_MASK32) {
1626 struct ifa_msghdrl32 *ifam32;
1628 ifam32 = (struct ifa_msghdrl32 *)ifam;
1629 ifam32->ifam_addrs = info->rti_addrs;
1630 ifam32->ifam_flags = ifa->ifa_flags;
1631 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1632 ifam32->_ifam_spare1 = 0;
1633 ifam32->ifam_len = sizeof(*ifam32);
1634 ifam32->ifam_data_off =
1635 offsetof(struct ifa_msghdrl32, ifam_data);
1636 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1637 ifd = &ifam32->ifam_data;
1641 ifam->ifam_addrs = info->rti_addrs;
1642 ifam->ifam_flags = ifa->ifa_flags;
1643 ifam->ifam_index = ifa->ifa_ifp->if_index;
1644 ifam->_ifam_spare1 = 0;
1645 ifam->ifam_len = sizeof(*ifam);
1646 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1647 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1648 ifd = &ifam->ifam_data;
1651 bzero(ifd, sizeof(*ifd));
1652 ifd->ifi_datalen = sizeof(struct if_data);
1653 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1654 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1655 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1656 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1658 /* Fixup if_data carp(4) vhid. */
1659 if (carp_get_vhid_p != NULL)
1660 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1662 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1666 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1667 struct walkarg *w, int len)
1669 struct ifa_msghdr *ifam;
1671 ifam = (struct ifa_msghdr *)w->w_tmem;
1672 ifam->ifam_addrs = info->rti_addrs;
1673 ifam->ifam_flags = ifa->ifa_flags;
1674 ifam->ifam_index = ifa->ifa_ifp->if_index;
1675 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1677 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1681 sysctl_iflist(int af, struct walkarg *w)
1685 struct rt_addrinfo info;
1687 struct sockaddr_storage ss;
1689 bzero((caddr_t)&info, sizeof(info));
1690 IFNET_RLOCK_NOSLEEP();
1691 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1692 if (w->w_arg && w->w_arg != ifp->if_index)
1696 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1697 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1700 info.rti_info[RTAX_IFP] = NULL;
1701 if (w->w_req && w->w_tmem) {
1702 if (w->w_op == NET_RT_IFLISTL)
1703 error = sysctl_iflist_ifml(ifp, &info, w, len);
1705 error = sysctl_iflist_ifm(ifp, &info, w, len);
1709 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1710 if (af && af != ifa->ifa_addr->sa_family)
1712 if (prison_if(w->w_req->td->td_ucred,
1713 ifa->ifa_addr) != 0)
1715 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1716 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1717 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1718 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1719 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1722 if (w->w_req && w->w_tmem) {
1723 if (w->w_op == NET_RT_IFLISTL)
1724 error = sysctl_iflist_ifaml(ifa, &info,
1727 error = sysctl_iflist_ifam(ifa, &info,
1733 IF_ADDR_RUNLOCK(ifp);
1734 info.rti_info[RTAX_IFA] = NULL;
1735 info.rti_info[RTAX_NETMASK] = NULL;
1736 info.rti_info[RTAX_BRD] = NULL;
1740 IF_ADDR_RUNLOCK(ifp);
1741 IFNET_RUNLOCK_NOSLEEP();
1746 sysctl_ifmalist(int af, struct walkarg *w)
1749 struct ifmultiaddr *ifma;
1750 struct rt_addrinfo info;
1754 bzero((caddr_t)&info, sizeof(info));
1755 IFNET_RLOCK_NOSLEEP();
1756 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1757 if (w->w_arg && w->w_arg != ifp->if_index)
1760 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1762 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1763 if (af && af != ifma->ifma_addr->sa_family)
1765 if (prison_if(w->w_req->td->td_ucred,
1766 ifma->ifma_addr) != 0)
1768 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1769 info.rti_info[RTAX_GATEWAY] =
1770 (ifma->ifma_addr->sa_family != AF_LINK) ?
1771 ifma->ifma_lladdr : NULL;
1772 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1775 if (w->w_req && w->w_tmem) {
1776 struct ifma_msghdr *ifmam;
1778 ifmam = (struct ifma_msghdr *)w->w_tmem;
1779 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1780 ifmam->ifmam_flags = 0;
1781 ifmam->ifmam_addrs = info.rti_addrs;
1782 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1784 IF_ADDR_RUNLOCK(ifp);
1789 IF_ADDR_RUNLOCK(ifp);
1792 IFNET_RUNLOCK_NOSLEEP();
1797 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1799 int *name = (int *)arg1;
1800 u_int namelen = arg2;
1801 struct radix_node_head *rnh = NULL; /* silence compiler. */
1802 int i, lim, error = EINVAL;
1811 if (name[1] == NET_RT_DUMP) {
1813 fib = req->td->td_proc->p_fibnum;
1814 else if (namelen == 4)
1815 fib = (name[3] == RT_ALL_FIBS) ?
1816 req->td->td_proc->p_fibnum : name[3];
1818 return ((namelen < 3) ? EISDIR : ENOTDIR);
1819 if (fib < 0 || fib >= rt_numfibs)
1821 } else if (namelen != 3)
1822 return ((namelen < 3) ? EISDIR : ENOTDIR);
1826 bzero(&w, sizeof(w));
1831 error = sysctl_wire_old_buffer(req, 0);
1836 * Allocate reply buffer in advance.
1837 * All rtsock messages has maximum length of u_short.
1839 w.w_tmemsize = 65536;
1840 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1846 if (af == 0) { /* dump all tables */
1849 } else /* dump only one table */
1853 * take care of llinfo entries, the caller must
1856 if (w.w_op == NET_RT_FLAGS &&
1857 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1859 error = lltable_sysctl_dumparp(af, w.w_req);
1865 * take care of routing entries
1867 for (error = 0; error == 0 && i <= lim; i++) {
1868 rnh = rt_tables_get_rnh(fib, i);
1870 RADIX_NODE_HEAD_RLOCK(rnh);
1871 error = rnh->rnh_walktree(rnh,
1872 sysctl_dumpentry, &w);
1873 RADIX_NODE_HEAD_RUNLOCK(rnh);
1875 error = EAFNOSUPPORT;
1880 case NET_RT_IFLISTL:
1881 error = sysctl_iflist(af, &w);
1884 case NET_RT_IFMALIST:
1885 error = sysctl_ifmalist(af, &w);
1889 free(w.w_tmem, M_TEMP);
1893 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1896 * Definitions of protocols supported in the ROUTE domain.
1899 static struct domain routedomain; /* or at least forward */
1901 static struct protosw routesw[] = {
1903 .pr_type = SOCK_RAW,
1904 .pr_domain = &routedomain,
1905 .pr_flags = PR_ATOMIC|PR_ADDR,
1906 .pr_output = route_output,
1907 .pr_ctlinput = raw_ctlinput,
1908 .pr_init = raw_init,
1909 .pr_usrreqs = &route_usrreqs
1913 static struct domain routedomain = {
1914 .dom_family = PF_ROUTE,
1915 .dom_name = "route",
1916 .dom_protosw = routesw,
1917 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1920 VNET_DOMAIN_SET(route);