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 * 3. 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 (IS_DEFAULT_VNET(curvnet)) {
199 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
200 rtsock_nh.nh_qlimit = tmp;
201 netisr_register(&rtsock_nh);
205 netisr_register_vnet(&rtsock_nh);
208 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
213 vnet_rts_uninit(void)
216 netisr_unregister_vnet(&rtsock_nh);
218 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
223 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
228 KASSERT(m != NULL, ("%s: m is NULL", __func__));
229 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
230 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
232 /* No filtering requested. */
233 if ((m->m_flags & RTS_FILTER_FIB) == 0)
236 /* Check if it is a rts and the fib matches the one of the socket. */
237 fibnum = M_GETFIB(m);
238 if (proto->sp_family != PF_ROUTE ||
239 rp->rcb_socket == NULL ||
240 rp->rcb_socket->so_fibnum == fibnum)
243 /* Filtering requested and no match, the socket shall be skipped. */
248 rts_input(struct mbuf *m)
250 struct sockproto route_proto;
251 unsigned short *family;
254 route_proto.sp_family = PF_ROUTE;
255 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
257 family = (unsigned short *)(tag + 1);
258 route_proto.sp_protocol = *family;
259 m_tag_delete(m, tag);
261 route_proto.sp_protocol = 0;
263 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
267 * It really doesn't make any sense at all for this code to share much
268 * with raw_usrreq.c, since its functionality is so restricted. XXX
271 rts_abort(struct socket *so)
274 raw_usrreqs.pru_abort(so);
278 rts_close(struct socket *so)
281 raw_usrreqs.pru_close(so);
284 /* pru_accept is EOPNOTSUPP */
287 rts_attach(struct socket *so, int proto, struct thread *td)
292 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
295 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
297 so->so_pcb = (caddr_t)rp;
298 so->so_fibnum = td->td_proc->p_fibnum;
299 error = raw_attach(so, proto);
307 switch(rp->rcb_proto.sp_protocol) {
309 V_route_cb.ip_count++;
312 V_route_cb.ip6_count++;
315 V_route_cb.any_count++;
318 so->so_options |= SO_USELOOPBACK;
323 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
326 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
330 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
333 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
336 /* pru_connect2 is EOPNOTSUPP */
337 /* pru_control is EOPNOTSUPP */
340 rts_detach(struct socket *so)
342 struct rawcb *rp = sotorawcb(so);
344 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
347 switch(rp->rcb_proto.sp_protocol) {
349 V_route_cb.ip_count--;
352 V_route_cb.ip6_count--;
355 V_route_cb.any_count--;
357 raw_usrreqs.pru_detach(so);
361 rts_disconnect(struct socket *so)
364 return (raw_usrreqs.pru_disconnect(so));
367 /* pru_listen is EOPNOTSUPP */
370 rts_peeraddr(struct socket *so, struct sockaddr **nam)
373 return (raw_usrreqs.pru_peeraddr(so, nam));
376 /* pru_rcvd is EOPNOTSUPP */
377 /* pru_rcvoob is EOPNOTSUPP */
380 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
381 struct mbuf *control, struct thread *td)
384 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
387 /* pru_sense is null */
390 rts_shutdown(struct socket *so)
393 return (raw_usrreqs.pru_shutdown(so));
397 rts_sockaddr(struct socket *so, struct sockaddr **nam)
400 return (raw_usrreqs.pru_sockaddr(so, nam));
403 static struct pr_usrreqs route_usrreqs = {
404 .pru_abort = rts_abort,
405 .pru_attach = rts_attach,
406 .pru_bind = rts_bind,
407 .pru_connect = rts_connect,
408 .pru_detach = rts_detach,
409 .pru_disconnect = rts_disconnect,
410 .pru_peeraddr = rts_peeraddr,
411 .pru_send = rts_send,
412 .pru_shutdown = rts_shutdown,
413 .pru_sockaddr = rts_sockaddr,
414 .pru_close = rts_close,
417 #ifndef _SOCKADDR_UNION_DEFINED
418 #define _SOCKADDR_UNION_DEFINED
420 * The union of all possible address formats we handle.
422 union sockaddr_union {
424 struct sockaddr_in sin;
425 struct sockaddr_in6 sin6;
427 #endif /* _SOCKADDR_UNION_DEFINED */
430 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
431 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
434 /* First, see if the returned address is part of the jail. */
435 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
436 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
440 switch (info->rti_info[RTAX_DST]->sa_family) {
450 * Try to find an address on the given outgoing interface
451 * that belongs to the jail.
454 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
457 if (sa->sa_family != AF_INET)
459 ia = ((struct sockaddr_in *)sa)->sin_addr;
460 if (prison_check_ip4(cred, &ia) == 0) {
465 IF_ADDR_RUNLOCK(ifp);
468 * As a last resort return the 'default' jail address.
470 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
472 if (prison_get_ip4(cred, &ia) != 0)
475 bzero(&saun->sin, sizeof(struct sockaddr_in));
476 saun->sin.sin_len = sizeof(struct sockaddr_in);
477 saun->sin.sin_family = AF_INET;
478 saun->sin.sin_addr.s_addr = ia.s_addr;
479 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
492 * Try to find an address on the given outgoing interface
493 * that belongs to the jail.
496 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
499 if (sa->sa_family != AF_INET6)
501 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
502 &ia6, sizeof(struct in6_addr));
503 if (prison_check_ip6(cred, &ia6) == 0) {
508 IF_ADDR_RUNLOCK(ifp);
511 * As a last resort return the 'default' jail address.
513 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
515 if (prison_get_ip6(cred, &ia6) != 0)
518 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
519 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
520 saun->sin6.sin6_family = AF_INET6;
521 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
522 if (sa6_recoverscope(&saun->sin6) != 0)
524 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
536 route_output(struct mbuf *m, struct socket *so, ...)
538 struct rt_msghdr *rtm = NULL;
539 struct rtentry *rt = NULL;
540 struct rib_head *rnh;
541 struct rt_addrinfo info;
542 struct sockaddr_storage ss;
544 struct sockaddr_in6 *sin6;
545 int i, rti_need_deembed = 0;
547 int alloc_len = 0, len, error = 0, fibnum;
548 struct ifnet *ifp = NULL;
549 union sockaddr_union saun;
550 sa_family_t saf = AF_UNSPEC;
551 struct rawcb *rp = NULL;
554 fibnum = so->so_fibnum;
556 #define senderr(e) { error = e; goto flush;}
557 if (m == NULL || ((m->m_len < sizeof(long)) &&
558 (m = m_pullup(m, sizeof(long))) == NULL))
560 if ((m->m_flags & M_PKTHDR) == 0)
561 panic("route_output");
562 len = m->m_pkthdr.len;
563 if (len < sizeof(*rtm) ||
564 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
568 * Most of current messages are in range 200-240 bytes,
569 * minimize possible re-allocation on reply using larger size
570 * buffer aligned on 1k boundaty.
572 alloc_len = roundup2(len, 1024);
573 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
576 m_copydata(m, 0, len, (caddr_t)rtm);
577 bzero(&info, sizeof(info));
578 bzero(&w, sizeof(w));
580 if (rtm->rtm_version != RTM_VERSION) {
581 /* Do not touch message since format is unknown */
584 senderr(EPROTONOSUPPORT);
588 * Starting from here, it is possible
589 * to alter original message and insert
590 * caller PID and error value.
593 rtm->rtm_pid = curproc->p_pid;
594 info.rti_addrs = rtm->rtm_addrs;
596 info.rti_mflags = rtm->rtm_inits;
597 info.rti_rmx = &rtm->rtm_rmx;
600 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
601 * link-local address because rtrequest requires addresses with
604 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
607 info.rti_flags = rtm->rtm_flags;
608 if (info.rti_info[RTAX_DST] == NULL ||
609 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
610 (info.rti_info[RTAX_GATEWAY] != NULL &&
611 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
613 saf = info.rti_info[RTAX_DST]->sa_family;
615 * Verify that the caller has the appropriate privilege; RTM_GET
616 * is the only operation the non-superuser is allowed.
618 if (rtm->rtm_type != RTM_GET) {
619 error = priv_check(curthread, PRIV_NET_ROUTE);
625 * The given gateway address may be an interface address.
626 * For example, issuing a "route change" command on a route
627 * entry that was created from a tunnel, and the gateway
628 * address given is the local end point. In this case the
629 * RTF_GATEWAY flag must be cleared or the destination will
630 * not be reachable even though there is no error message.
632 if (info.rti_info[RTAX_GATEWAY] != NULL &&
633 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
634 struct rt_addrinfo ginfo;
635 struct sockaddr *gdst;
637 bzero(&ginfo, sizeof(ginfo));
638 bzero(&ss, sizeof(ss));
639 ss.ss_len = sizeof(ss);
641 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
642 gdst = info.rti_info[RTAX_GATEWAY];
645 * A host route through the loopback interface is
646 * installed for each interface adddress. In pre 8.0
647 * releases the interface address of a PPP link type
648 * is not reachable locally. This behavior is fixed as
649 * part of the new L2/L3 redesign and rewrite work. The
650 * signature of this interface address route is the
651 * AF_LINK sa_family type of the rt_gateway, and the
652 * rt_ifp has the IFF_LOOPBACK flag set.
654 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
655 if (ss.ss_family == AF_LINK &&
656 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
657 info.rti_flags &= ~RTF_GATEWAY;
658 info.rti_flags |= RTF_GWFLAG_COMPAT;
660 rib_free_info(&ginfo);
664 switch (rtm->rtm_type) {
665 struct rtentry *saved_nrt;
669 if (info.rti_info[RTAX_GATEWAY] == NULL)
673 /* support for new ARP code */
674 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
675 (rtm->rtm_flags & RTF_LLDATA) != 0) {
676 error = lla_rt_output(rtm, &info);
679 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
683 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
685 if (error == 0 && saved_nrt != NULL) {
687 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
690 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
691 RT_REMREF(saved_nrt);
692 RT_UNLOCK(saved_nrt);
698 /* support for new ARP code */
699 if (info.rti_info[RTAX_GATEWAY] &&
700 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
701 (rtm->rtm_flags & RTF_LLDATA) != 0) {
702 error = lla_rt_output(rtm, &info);
705 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
709 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
716 /* rt_msg2() will not be used when RTM_DELETE fails. */
717 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
722 rnh = rt_tables_get_rnh(fibnum, saf);
724 senderr(EAFNOSUPPORT);
728 if (info.rti_info[RTAX_NETMASK] == NULL &&
729 rtm->rtm_type == RTM_GET) {
731 * Provide longest prefix match for
732 * address lookup (no mask).
733 * 'route -n get addr'
735 rt = (struct rtentry *) rnh->rnh_matchaddr(
736 info.rti_info[RTAX_DST], &rnh->head);
738 rt = (struct rtentry *) rnh->rnh_lookup(
739 info.rti_info[RTAX_DST],
740 info.rti_info[RTAX_NETMASK], &rnh->head);
748 * for RTM_CHANGE/LOCK, if we got multipath routes,
749 * we require users to specify a matching RTAX_GATEWAY.
751 * for RTM_GET, gate is optional even with multipath.
752 * if gate == NULL the first match is returned.
753 * (no need to call rt_mpath_matchgate if gate == NULL)
755 if (rt_mpath_capable(rnh) &&
756 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
757 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
765 * If performing proxied L2 entry insertion, and
766 * the actual PPP host entry is found, perform
767 * another search to retrieve the prefix route of
768 * the local end point of the PPP link.
770 if (rtm->rtm_flags & RTF_ANNOUNCE) {
771 struct sockaddr laddr;
773 if (rt->rt_ifp != NULL &&
774 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
777 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
780 rt_maskedcopy(ifa->ifa_addr,
784 rt_maskedcopy(rt->rt_ifa->ifa_addr,
786 rt->rt_ifa->ifa_netmask);
788 * refactor rt and no lock operation necessary
790 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
803 if ((rt->rt_flags & RTF_HOST) == 0
804 ? jailed_without_vnet(curthread->td_ucred)
805 : prison_if(curthread->td_ucred,
810 info.rti_info[RTAX_DST] = rt_key(rt);
811 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
812 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
814 info.rti_info[RTAX_GENMASK] = 0;
815 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
818 info.rti_info[RTAX_IFP] =
819 ifp->if_addr->ifa_addr;
820 error = rtm_get_jailed(&info, ifp, rt,
821 &saun, curthread->td_ucred);
826 if (ifp->if_flags & IFF_POINTOPOINT)
827 info.rti_info[RTAX_BRD] =
828 rt->rt_ifa->ifa_dstaddr;
829 rtm->rtm_index = ifp->if_index;
831 info.rti_info[RTAX_IFP] = NULL;
832 info.rti_info[RTAX_IFA] = NULL;
834 } else if ((ifp = rt->rt_ifp) != NULL) {
835 rtm->rtm_index = ifp->if_index;
838 /* Check if we need to realloc storage */
839 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
840 if (len > alloc_len) {
841 struct rt_msghdr *new_rtm;
842 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
843 if (new_rtm == NULL) {
847 bcopy(rtm, new_rtm, rtm->rtm_msglen);
853 w.w_tmem = (caddr_t)rtm;
854 w.w_tmemsize = alloc_len;
855 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
857 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
858 rtm->rtm_flags = RTF_GATEWAY |
859 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
861 rtm->rtm_flags = rt->rt_flags;
862 rt_getmetrics(rt, &rtm->rtm_rmx);
863 rtm->rtm_addrs = info.rti_addrs;
876 * Check to see if we don't want our own messages.
878 if ((so->so_options & SO_USELOOPBACK) == 0) {
879 if (V_route_cb.any_count <= 1) {
885 /* There is another listener, so construct message */
891 if (rti_need_deembed) {
892 /* sin6_scope_id is recovered before sending rtm. */
893 sin6 = (struct sockaddr_in6 *)&ss;
894 for (i = 0; i < RTAX_MAX; i++) {
895 if (info.rti_info[i] == NULL)
897 if (info.rti_info[i]->sa_family != AF_INET6)
899 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
900 if (sa6_recoverscope(sin6) == 0)
901 bcopy(sin6, info.rti_info[i],
907 rtm->rtm_errno = error;
909 rtm->rtm_flags |= RTF_DONE;
911 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
912 if (m->m_pkthdr.len < rtm->rtm_msglen) {
915 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
916 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
922 m->m_flags |= RTS_FILTER_FIB;
925 * XXX insure we don't get a copy by
926 * invalidating our protocol
928 unsigned short family = rp->rcb_proto.sp_family;
929 rp->rcb_proto.sp_family = 0;
931 rp->rcb_proto.sp_family = family;
940 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
943 bzero(out, sizeof(*out));
944 out->rmx_mtu = rt->rt_mtu;
945 out->rmx_weight = rt->rt_weight;
946 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
947 /* Kernel -> userland timebase conversion. */
948 out->rmx_expire = rt->rt_expire ?
949 rt->rt_expire - time_uptime + time_second : 0;
953 * Extract the addresses of the passed sockaddrs.
954 * Do a little sanity checking so as to avoid bad memory references.
955 * This data is derived straight from userland.
958 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
963 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
964 if ((rtinfo->rti_addrs & (1 << i)) == 0)
966 sa = (struct sockaddr *)cp;
970 if (cp + sa->sa_len > cplim)
973 * there are no more.. quit now
974 * If there are more bits, they are in error.
975 * I've seen this. route(1) can evidently generate these.
976 * This causes kernel to core dump.
977 * for compatibility, If we see this, point to a safe address.
979 if (sa->sa_len == 0) {
980 rtinfo->rti_info[i] = &sa_zero;
981 return (0); /* should be EINVAL but for compat */
985 if (sa->sa_family == AF_INET6)
986 sa6_embedscope((struct sockaddr_in6 *)sa,
989 rtinfo->rti_info[i] = sa;
996 * Fill in @dmask with valid netmask leaving original @smask
997 * intact. Mostly used with radix netmasks.
999 static struct sockaddr *
1000 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1001 struct sockaddr_storage *dmask)
1003 if (dst == NULL || smask == NULL)
1006 memset(dmask, 0, dst->sa_len);
1007 memcpy(dmask, smask, smask->sa_len);
1008 dmask->ss_len = dst->sa_len;
1009 dmask->ss_family = dst->sa_family;
1011 return ((struct sockaddr *)dmask);
1015 * Writes information related to @rtinfo object to newly-allocated mbuf.
1016 * Assumes MCLBYTES is enough to construct any message.
1017 * Used for OS notifications of vaious events (if/ifa announces,etc)
1019 * Returns allocated mbuf or NULL on failure.
1021 static struct mbuf *
1022 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1024 struct rt_msghdr *rtm;
1027 struct sockaddr *sa;
1029 struct sockaddr_storage ss;
1030 struct sockaddr_in6 *sin6;
1038 len = sizeof(struct ifa_msghdr);
1043 len = sizeof(struct ifma_msghdr);
1047 len = sizeof(struct if_msghdr);
1050 case RTM_IFANNOUNCE:
1052 len = sizeof(struct if_announcemsghdr);
1056 len = sizeof(struct rt_msghdr);
1059 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1060 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1062 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1064 m = m_gethdr(M_NOWAIT, MT_DATA);
1068 m->m_pkthdr.len = m->m_len = len;
1069 rtm = mtod(m, struct rt_msghdr *);
1070 bzero((caddr_t)rtm, len);
1071 for (i = 0; i < RTAX_MAX; i++) {
1072 if ((sa = rtinfo->rti_info[i]) == NULL)
1074 rtinfo->rti_addrs |= (1 << i);
1077 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1078 sin6 = (struct sockaddr_in6 *)&ss;
1079 bcopy(sa, sin6, sizeof(*sin6));
1080 if (sa6_recoverscope(sin6) == 0)
1081 sa = (struct sockaddr *)sin6;
1084 m_copyback(m, len, dlen, (caddr_t)sa);
1087 if (m->m_pkthdr.len != len) {
1091 rtm->rtm_msglen = len;
1092 rtm->rtm_version = RTM_VERSION;
1093 rtm->rtm_type = type;
1098 * Writes information related to @rtinfo object to preallocated buffer.
1099 * Stores needed size in @plen. If @w is NULL, calculates size without
1101 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1103 * Returns 0 on success.
1107 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1110 int len, buflen = 0, dlen;
1112 struct rt_msghdr *rtm = NULL;
1114 struct sockaddr_storage ss;
1115 struct sockaddr_in6 *sin6;
1122 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1123 #ifdef COMPAT_FREEBSD32
1124 if (w->w_req->flags & SCTL_MASK32)
1125 len = sizeof(struct ifa_msghdrl32);
1128 len = sizeof(struct ifa_msghdrl);
1130 len = sizeof(struct ifa_msghdr);
1134 #ifdef COMPAT_FREEBSD32
1135 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1136 if (w->w_op == NET_RT_IFLISTL)
1137 len = sizeof(struct if_msghdrl32);
1139 len = sizeof(struct if_msghdr32);
1143 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1144 len = sizeof(struct if_msghdrl);
1146 len = sizeof(struct if_msghdr);
1150 len = sizeof(struct ifma_msghdr);
1154 len = sizeof(struct rt_msghdr);
1158 rtm = (struct rt_msghdr *)w->w_tmem;
1159 buflen = w->w_tmemsize - len;
1160 cp = (caddr_t)w->w_tmem + len;
1163 rtinfo->rti_addrs = 0;
1164 for (i = 0; i < RTAX_MAX; i++) {
1165 struct sockaddr *sa;
1167 if ((sa = rtinfo->rti_info[i]) == NULL)
1169 rtinfo->rti_addrs |= (1 << i);
1171 if (cp != NULL && buflen >= dlen) {
1173 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1174 sin6 = (struct sockaddr_in6 *)&ss;
1175 bcopy(sa, sin6, sizeof(*sin6));
1176 if (sa6_recoverscope(sin6) == 0)
1177 sa = (struct sockaddr *)sin6;
1180 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1183 } else if (cp != NULL) {
1185 * Buffer too small. Count needed size
1186 * and return with error.
1195 dlen = ALIGN(len) - len;
1204 /* fill header iff buffer is large enough */
1205 rtm->rtm_version = RTM_VERSION;
1206 rtm->rtm_type = type;
1207 rtm->rtm_msglen = len;
1212 if (w != NULL && cp == NULL)
1219 * This routine is called to generate a message from the routing
1220 * socket indicating that a redirect has occurred, a routing lookup
1221 * has failed, or that a protocol has detected timeouts to a particular
1225 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1228 struct rt_msghdr *rtm;
1230 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1232 if (V_route_cb.any_count == 0)
1234 m = rtsock_msg_mbuf(type, rtinfo);
1238 if (fibnum != RT_ALL_FIBS) {
1239 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1240 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1241 M_SETFIB(m, fibnum);
1242 m->m_flags |= RTS_FILTER_FIB;
1245 rtm = mtod(m, struct rt_msghdr *);
1246 rtm->rtm_flags = RTF_DONE | flags;
1247 rtm->rtm_errno = error;
1248 rtm->rtm_addrs = rtinfo->rti_addrs;
1249 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1253 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1256 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1260 * This routine is called to generate a message from the routing
1261 * socket indicating that the status of a network interface has changed.
1264 rt_ifmsg(struct ifnet *ifp)
1266 struct if_msghdr *ifm;
1268 struct rt_addrinfo info;
1270 if (V_route_cb.any_count == 0)
1272 bzero((caddr_t)&info, sizeof(info));
1273 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1276 ifm = mtod(m, struct if_msghdr *);
1277 ifm->ifm_index = ifp->if_index;
1278 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1279 if_data_copy(ifp, &ifm->ifm_data);
1281 rt_dispatch(m, AF_UNSPEC);
1285 * Announce interface address arrival/withdraw.
1286 * Please do not call directly, use rt_addrmsg().
1287 * Assume input data to be valid.
1288 * Returns 0 on success.
1291 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1293 struct rt_addrinfo info;
1294 struct sockaddr *sa;
1297 struct ifa_msghdr *ifam;
1298 struct ifnet *ifp = ifa->ifa_ifp;
1299 struct sockaddr_storage ss;
1301 if (V_route_cb.any_count == 0)
1304 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1306 bzero((caddr_t)&info, sizeof(info));
1307 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1308 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1309 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1310 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1311 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1312 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1314 ifam = mtod(m, struct ifa_msghdr *);
1315 ifam->ifam_index = ifp->if_index;
1316 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1317 ifam->ifam_flags = ifa->ifa_flags;
1318 ifam->ifam_addrs = info.rti_addrs;
1320 if (fibnum != RT_ALL_FIBS) {
1321 M_SETFIB(m, fibnum);
1322 m->m_flags |= RTS_FILTER_FIB;
1325 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1331 * Announce route addition/removal.
1332 * Please do not call directly, use rt_routemsg().
1333 * Note that @rt data MAY be inconsistent/invalid:
1334 * if some userland app sends us "invalid" route message (invalid mask,
1335 * no dst, wrong address families, etc...) we need to pass it back
1336 * to app (and any other rtsock consumers) with rtm_errno field set to
1339 * Returns 0 on success.
1342 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1345 struct rt_addrinfo info;
1346 struct sockaddr *sa;
1348 struct rt_msghdr *rtm;
1349 struct sockaddr_storage ss;
1351 if (V_route_cb.any_count == 0)
1354 bzero((caddr_t)&info, sizeof(info));
1355 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1356 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1357 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1358 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1360 rtm = mtod(m, struct rt_msghdr *);
1361 rtm->rtm_index = ifp->if_index;
1362 rtm->rtm_flags |= rt->rt_flags;
1363 rtm->rtm_errno = error;
1364 rtm->rtm_addrs = info.rti_addrs;
1366 if (fibnum != RT_ALL_FIBS) {
1367 M_SETFIB(m, fibnum);
1368 m->m_flags |= RTS_FILTER_FIB;
1371 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1377 * This is the analogue to the rt_newaddrmsg which performs the same
1378 * function but for multicast group memberhips. This is easier since
1379 * there is no route state to worry about.
1382 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1384 struct rt_addrinfo info;
1385 struct mbuf *m = NULL;
1386 struct ifnet *ifp = ifma->ifma_ifp;
1387 struct ifma_msghdr *ifmam;
1389 if (V_route_cb.any_count == 0)
1392 bzero((caddr_t)&info, sizeof(info));
1393 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1394 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1396 * If a link-layer address is present, present it as a ``gateway''
1397 * (similarly to how ARP entries, e.g., are presented).
1399 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1400 m = rtsock_msg_mbuf(cmd, &info);
1403 ifmam = mtod(m, struct ifma_msghdr *);
1404 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1406 ifmam->ifmam_index = ifp->if_index;
1407 ifmam->ifmam_addrs = info.rti_addrs;
1408 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1411 static struct mbuf *
1412 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1413 struct rt_addrinfo *info)
1415 struct if_announcemsghdr *ifan;
1418 if (V_route_cb.any_count == 0)
1420 bzero((caddr_t)info, sizeof(*info));
1421 m = rtsock_msg_mbuf(type, info);
1423 ifan = mtod(m, struct if_announcemsghdr *);
1424 ifan->ifan_index = ifp->if_index;
1425 strlcpy(ifan->ifan_name, ifp->if_xname,
1426 sizeof(ifan->ifan_name));
1427 ifan->ifan_what = what;
1433 * This is called to generate routing socket messages indicating
1434 * IEEE80211 wireless events.
1435 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1438 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1441 struct rt_addrinfo info;
1443 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1446 * Append the ieee80211 data. Try to stick it in the
1447 * mbuf containing the ifannounce msg; otherwise allocate
1448 * a new mbuf and append.
1450 * NB: we assume m is a single mbuf.
1452 if (data_len > M_TRAILINGSPACE(m)) {
1453 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1458 bcopy(data, mtod(n, void *), data_len);
1459 n->m_len = data_len;
1461 } else if (data_len > 0) {
1462 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1463 m->m_len += data_len;
1465 if (m->m_flags & M_PKTHDR)
1466 m->m_pkthdr.len += data_len;
1467 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1468 rt_dispatch(m, AF_UNSPEC);
1473 * This is called to generate routing socket messages indicating
1474 * network interface arrival and departure.
1477 rt_ifannouncemsg(struct ifnet *ifp, int what)
1480 struct rt_addrinfo info;
1482 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1484 rt_dispatch(m, AF_UNSPEC);
1488 rt_dispatch(struct mbuf *m, sa_family_t saf)
1493 * Preserve the family from the sockaddr, if any, in an m_tag for
1494 * use when injecting the mbuf into the routing socket buffer from
1497 if (saf != AF_UNSPEC) {
1498 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1504 *(unsigned short *)(tag + 1) = saf;
1505 m_tag_prepend(m, tag);
1509 m->m_pkthdr.rcvif = V_loif;
1515 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1519 * This is used in dumping the kernel table via sysctl().
1522 sysctl_dumpentry(struct radix_node *rn, void *vw)
1524 struct walkarg *w = vw;
1525 struct rtentry *rt = (struct rtentry *)rn;
1526 int error = 0, size;
1527 struct rt_addrinfo info;
1528 struct sockaddr_storage ss;
1530 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1532 if ((rt->rt_flags & RTF_HOST) == 0
1533 ? jailed_without_vnet(w->w_req->td->td_ucred)
1534 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1536 bzero((caddr_t)&info, sizeof(info));
1537 info.rti_info[RTAX_DST] = rt_key(rt);
1538 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1539 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1541 info.rti_info[RTAX_GENMASK] = 0;
1543 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1544 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1545 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1546 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1548 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1550 if (w->w_req && w->w_tmem) {
1551 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1553 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1554 rtm->rtm_flags = RTF_GATEWAY |
1555 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1557 rtm->rtm_flags = rt->rt_flags;
1558 rt_getmetrics(rt, &rtm->rtm_rmx);
1559 rtm->rtm_index = rt->rt_ifp->if_index;
1560 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1561 rtm->rtm_addrs = info.rti_addrs;
1562 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1569 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1570 struct rt_addrinfo *info, struct walkarg *w, int len)
1572 struct if_msghdrl *ifm;
1573 struct if_data *ifd;
1575 ifm = (struct if_msghdrl *)w->w_tmem;
1577 #ifdef COMPAT_FREEBSD32
1578 if (w->w_req->flags & SCTL_MASK32) {
1579 struct if_msghdrl32 *ifm32;
1581 ifm32 = (struct if_msghdrl32 *)ifm;
1582 ifm32->ifm_addrs = info->rti_addrs;
1583 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1584 ifm32->ifm_index = ifp->if_index;
1585 ifm32->_ifm_spare1 = 0;
1586 ifm32->ifm_len = sizeof(*ifm32);
1587 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1588 ifd = &ifm32->ifm_data;
1592 ifm->ifm_addrs = info->rti_addrs;
1593 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1594 ifm->ifm_index = ifp->if_index;
1595 ifm->_ifm_spare1 = 0;
1596 ifm->ifm_len = sizeof(*ifm);
1597 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1598 ifd = &ifm->ifm_data;
1601 memcpy(ifd, src_ifd, sizeof(*ifd));
1603 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1607 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1608 struct rt_addrinfo *info, struct walkarg *w, int len)
1610 struct if_msghdr *ifm;
1611 struct if_data *ifd;
1613 ifm = (struct if_msghdr *)w->w_tmem;
1615 #ifdef COMPAT_FREEBSD32
1616 if (w->w_req->flags & SCTL_MASK32) {
1617 struct if_msghdr32 *ifm32;
1619 ifm32 = (struct if_msghdr32 *)ifm;
1620 ifm32->ifm_addrs = info->rti_addrs;
1621 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1622 ifm32->ifm_index = ifp->if_index;
1623 ifd = &ifm32->ifm_data;
1627 ifm->ifm_addrs = info->rti_addrs;
1628 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1629 ifm->ifm_index = ifp->if_index;
1630 ifd = &ifm->ifm_data;
1633 memcpy(ifd, src_ifd, sizeof(*ifd));
1635 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1639 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1640 struct walkarg *w, int len)
1642 struct ifa_msghdrl *ifam;
1643 struct if_data *ifd;
1645 ifam = (struct ifa_msghdrl *)w->w_tmem;
1647 #ifdef COMPAT_FREEBSD32
1648 if (w->w_req->flags & SCTL_MASK32) {
1649 struct ifa_msghdrl32 *ifam32;
1651 ifam32 = (struct ifa_msghdrl32 *)ifam;
1652 ifam32->ifam_addrs = info->rti_addrs;
1653 ifam32->ifam_flags = ifa->ifa_flags;
1654 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1655 ifam32->_ifam_spare1 = 0;
1656 ifam32->ifam_len = sizeof(*ifam32);
1657 ifam32->ifam_data_off =
1658 offsetof(struct ifa_msghdrl32, ifam_data);
1659 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1660 ifd = &ifam32->ifam_data;
1664 ifam->ifam_addrs = info->rti_addrs;
1665 ifam->ifam_flags = ifa->ifa_flags;
1666 ifam->ifam_index = ifa->ifa_ifp->if_index;
1667 ifam->_ifam_spare1 = 0;
1668 ifam->ifam_len = sizeof(*ifam);
1669 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1670 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1671 ifd = &ifam->ifam_data;
1674 bzero(ifd, sizeof(*ifd));
1675 ifd->ifi_datalen = sizeof(struct if_data);
1676 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1677 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1678 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1679 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1681 /* Fixup if_data carp(4) vhid. */
1682 if (carp_get_vhid_p != NULL)
1683 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1685 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1689 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1690 struct walkarg *w, int len)
1692 struct ifa_msghdr *ifam;
1694 ifam = (struct ifa_msghdr *)w->w_tmem;
1695 ifam->ifam_addrs = info->rti_addrs;
1696 ifam->ifam_flags = ifa->ifa_flags;
1697 ifam->ifam_index = ifa->ifa_ifp->if_index;
1698 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1700 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1704 sysctl_iflist(int af, struct walkarg *w)
1709 struct rt_addrinfo info;
1711 struct sockaddr_storage ss;
1713 bzero((caddr_t)&info, sizeof(info));
1714 bzero(&ifd, sizeof(ifd));
1715 IFNET_RLOCK_NOSLEEP();
1716 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1717 if (w->w_arg && w->w_arg != ifp->if_index)
1719 if_data_copy(ifp, &ifd);
1722 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1723 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1726 info.rti_info[RTAX_IFP] = NULL;
1727 if (w->w_req && w->w_tmem) {
1728 if (w->w_op == NET_RT_IFLISTL)
1729 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1732 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1737 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1738 if (af && af != ifa->ifa_addr->sa_family)
1740 if (prison_if(w->w_req->td->td_ucred,
1741 ifa->ifa_addr) != 0)
1743 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1744 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1745 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1746 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1747 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1750 if (w->w_req && w->w_tmem) {
1751 if (w->w_op == NET_RT_IFLISTL)
1752 error = sysctl_iflist_ifaml(ifa, &info,
1755 error = sysctl_iflist_ifam(ifa, &info,
1761 IF_ADDR_RUNLOCK(ifp);
1762 info.rti_info[RTAX_IFA] = NULL;
1763 info.rti_info[RTAX_NETMASK] = NULL;
1764 info.rti_info[RTAX_BRD] = NULL;
1768 IF_ADDR_RUNLOCK(ifp);
1769 IFNET_RUNLOCK_NOSLEEP();
1774 sysctl_ifmalist(int af, struct walkarg *w)
1776 struct rt_addrinfo info;
1778 struct ifmultiaddr *ifma;
1783 bzero((caddr_t)&info, sizeof(info));
1785 IFNET_RLOCK_NOSLEEP();
1786 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1787 if (w->w_arg && w->w_arg != ifp->if_index)
1790 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1792 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1793 if (af && af != ifma->ifma_addr->sa_family)
1795 if (prison_if(w->w_req->td->td_ucred,
1796 ifma->ifma_addr) != 0)
1798 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1799 info.rti_info[RTAX_GATEWAY] =
1800 (ifma->ifma_addr->sa_family != AF_LINK) ?
1801 ifma->ifma_lladdr : NULL;
1802 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1805 if (w->w_req && w->w_tmem) {
1806 struct ifma_msghdr *ifmam;
1808 ifmam = (struct ifma_msghdr *)w->w_tmem;
1809 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1810 ifmam->ifmam_flags = 0;
1811 ifmam->ifmam_addrs = info.rti_addrs;
1812 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1817 IF_ADDR_RUNLOCK(ifp);
1821 IFNET_RUNLOCK_NOSLEEP();
1826 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1828 int *name = (int *)arg1;
1829 u_int namelen = arg2;
1830 struct rib_head *rnh = NULL; /* silence compiler. */
1831 int i, lim, error = EINVAL;
1840 if (name[1] == NET_RT_DUMP) {
1842 fib = req->td->td_proc->p_fibnum;
1843 else if (namelen == 4)
1844 fib = (name[3] == RT_ALL_FIBS) ?
1845 req->td->td_proc->p_fibnum : name[3];
1847 return ((namelen < 3) ? EISDIR : ENOTDIR);
1848 if (fib < 0 || fib >= rt_numfibs)
1850 } else if (namelen != 3)
1851 return ((namelen < 3) ? EISDIR : ENOTDIR);
1855 bzero(&w, sizeof(w));
1860 error = sysctl_wire_old_buffer(req, 0);
1865 * Allocate reply buffer in advance.
1866 * All rtsock messages has maximum length of u_short.
1868 w.w_tmemsize = 65536;
1869 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1875 if (af == 0) { /* dump all tables */
1878 } else /* dump only one table */
1882 * take care of llinfo entries, the caller must
1885 if (w.w_op == NET_RT_FLAGS &&
1886 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1888 error = lltable_sysctl_dumparp(af, w.w_req);
1894 * take care of routing entries
1896 for (error = 0; error == 0 && i <= lim; i++) {
1897 rnh = rt_tables_get_rnh(fib, i);
1900 error = rnh->rnh_walktree(&rnh->head,
1901 sysctl_dumpentry, &w);
1904 error = EAFNOSUPPORT;
1909 case NET_RT_IFLISTL:
1910 error = sysctl_iflist(af, &w);
1913 case NET_RT_IFMALIST:
1914 error = sysctl_ifmalist(af, &w);
1918 free(w.w_tmem, M_TEMP);
1922 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1925 * Definitions of protocols supported in the ROUTE domain.
1928 static struct domain routedomain; /* or at least forward */
1930 static struct protosw routesw[] = {
1932 .pr_type = SOCK_RAW,
1933 .pr_domain = &routedomain,
1934 .pr_flags = PR_ATOMIC|PR_ADDR,
1935 .pr_output = route_output,
1936 .pr_ctlinput = raw_ctlinput,
1937 .pr_init = raw_init,
1938 .pr_usrreqs = &route_usrreqs
1942 static struct domain routedomain = {
1943 .dom_family = PF_ROUTE,
1944 .dom_name = "route",
1945 .dom_protosw = routesw,
1946 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1949 VNET_DOMAIN_SET(route);