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>
81 uint8_t ifi_link_state;
83 uint8_t ifi_baudrate_pf;
87 uint32_t ifi_baudrate;
88 uint32_t ifi_ipackets;
90 uint32_t ifi_opackets;
92 uint32_t ifi_collisions;
99 uint32_t ifi_hwassist;
101 struct timeval32 ifi_lastchange;
111 struct if_data32 ifm_data;
114 struct if_msghdrl32 {
121 uint16_t _ifm_spare1;
123 uint16_t ifm_data_off;
124 struct if_data32 ifm_data;
127 struct ifa_msghdrl32 {
128 uint16_t ifam_msglen;
129 uint8_t ifam_version;
134 uint16_t _ifam_spare1;
136 uint16_t ifam_data_off;
138 struct if_data32 ifam_data;
140 #endif /* COMPAT_FREEBSD32 */
142 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
144 /* NB: these are not modified */
145 static struct sockaddr route_src = { 2, PF_ROUTE, };
146 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
148 /* These are external hooks for CARP. */
149 int (*carp_get_vhid_p)(struct ifaddr *);
152 * Used by rtsock/raw_input callback code to decide whether to filter the update
153 * notification to a socket bound to a particular FIB.
155 #define RTS_FILTER_FIB M_PROTO8
158 int ip_count; /* attached w/ AF_INET */
159 int ip6_count; /* attached w/ AF_INET6 */
160 int ipx_count; /* attached w/ AF_IPX */
161 int any_count; /* total attached */
164 struct mtx rtsock_mtx;
165 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
167 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
168 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
169 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
171 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
177 struct sysctl_req *w_req;
180 static void rts_input(struct mbuf *m);
181 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
182 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
183 caddr_t cp, struct walkarg *w);
184 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
185 struct rt_addrinfo *rtinfo);
186 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
187 static int sysctl_iflist(int af, struct walkarg *w);
188 static int sysctl_ifmalist(int af, struct walkarg *w);
189 static int route_output(struct mbuf *m, struct socket *so);
190 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
191 struct rt_metrics_lite *out);
192 static void rt_getmetrics(const struct rt_metrics_lite *in,
193 struct rt_metrics *out);
194 static void rt_dispatch(struct mbuf *, sa_family_t);
196 static struct netisr_handler rtsock_nh = {
198 .nh_handler = rts_input,
199 .nh_proto = NETISR_ROUTE,
200 .nh_policy = NETISR_POLICY_SOURCE,
204 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
208 netisr_getqlimit(&rtsock_nh, &qlimit);
209 error = sysctl_handle_int(oidp, &qlimit, 0, req);
210 if (error || !req->newptr)
214 return (netisr_setqlimit(&rtsock_nh, qlimit));
216 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
217 0, 0, sysctl_route_netisr_maxqlen, "I",
218 "maximum routing socket dispatch queue length");
225 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
226 rtsock_nh.nh_qlimit = tmp;
227 netisr_register(&rtsock_nh);
229 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
232 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
237 KASSERT(m != NULL, ("%s: m is NULL", __func__));
238 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
239 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
241 /* No filtering requested. */
242 if ((m->m_flags & RTS_FILTER_FIB) == 0)
245 /* Check if it is a rts and the fib matches the one of the socket. */
246 fibnum = M_GETFIB(m);
247 if (proto->sp_family != PF_ROUTE ||
248 rp->rcb_socket == NULL ||
249 rp->rcb_socket->so_fibnum == fibnum)
252 /* Filtering requested and no match, the socket shall be skipped. */
257 rts_input(struct mbuf *m)
259 struct sockproto route_proto;
260 unsigned short *family;
263 route_proto.sp_family = PF_ROUTE;
264 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
266 family = (unsigned short *)(tag + 1);
267 route_proto.sp_protocol = *family;
268 m_tag_delete(m, tag);
270 route_proto.sp_protocol = 0;
272 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
276 * It really doesn't make any sense at all for this code to share much
277 * with raw_usrreq.c, since its functionality is so restricted. XXX
280 rts_abort(struct socket *so)
283 raw_usrreqs.pru_abort(so);
287 rts_close(struct socket *so)
290 raw_usrreqs.pru_close(so);
293 /* pru_accept is EOPNOTSUPP */
296 rts_attach(struct socket *so, int proto, struct thread *td)
301 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
304 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
308 so->so_pcb = (caddr_t)rp;
309 so->so_fibnum = td->td_proc->p_fibnum;
310 error = raw_attach(so, proto);
318 switch(rp->rcb_proto.sp_protocol) {
323 route_cb.ip6_count++;
326 route_cb.ipx_count++;
329 route_cb.any_count++;
332 so->so_options |= SO_USELOOPBACK;
337 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
340 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
344 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
347 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
350 /* pru_connect2 is EOPNOTSUPP */
351 /* pru_control is EOPNOTSUPP */
354 rts_detach(struct socket *so)
356 struct rawcb *rp = sotorawcb(so);
358 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
361 switch(rp->rcb_proto.sp_protocol) {
366 route_cb.ip6_count--;
369 route_cb.ipx_count--;
372 route_cb.any_count--;
374 raw_usrreqs.pru_detach(so);
378 rts_disconnect(struct socket *so)
381 return (raw_usrreqs.pru_disconnect(so));
384 /* pru_listen is EOPNOTSUPP */
387 rts_peeraddr(struct socket *so, struct sockaddr **nam)
390 return (raw_usrreqs.pru_peeraddr(so, nam));
393 /* pru_rcvd is EOPNOTSUPP */
394 /* pru_rcvoob is EOPNOTSUPP */
397 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
398 struct mbuf *control, struct thread *td)
401 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
404 /* pru_sense is null */
407 rts_shutdown(struct socket *so)
410 return (raw_usrreqs.pru_shutdown(so));
414 rts_sockaddr(struct socket *so, struct sockaddr **nam)
417 return (raw_usrreqs.pru_sockaddr(so, nam));
420 static struct pr_usrreqs route_usrreqs = {
421 .pru_abort = rts_abort,
422 .pru_attach = rts_attach,
423 .pru_bind = rts_bind,
424 .pru_connect = rts_connect,
425 .pru_detach = rts_detach,
426 .pru_disconnect = rts_disconnect,
427 .pru_peeraddr = rts_peeraddr,
428 .pru_send = rts_send,
429 .pru_shutdown = rts_shutdown,
430 .pru_sockaddr = rts_sockaddr,
431 .pru_close = rts_close,
434 #ifndef _SOCKADDR_UNION_DEFINED
435 #define _SOCKADDR_UNION_DEFINED
437 * The union of all possible address formats we handle.
439 union sockaddr_union {
441 struct sockaddr_in sin;
442 struct sockaddr_in6 sin6;
444 #endif /* _SOCKADDR_UNION_DEFINED */
447 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
448 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
451 /* First, see if the returned address is part of the jail. */
452 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
453 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
457 switch (info->rti_info[RTAX_DST]->sa_family) {
467 * Try to find an address on the given outgoing interface
468 * that belongs to the jail.
471 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
474 if (sa->sa_family != AF_INET)
476 ia = ((struct sockaddr_in *)sa)->sin_addr;
477 if (prison_check_ip4(cred, &ia) == 0) {
482 IF_ADDR_RUNLOCK(ifp);
485 * As a last resort return the 'default' jail address.
487 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
489 if (prison_get_ip4(cred, &ia) != 0)
492 bzero(&saun->sin, sizeof(struct sockaddr_in));
493 saun->sin.sin_len = sizeof(struct sockaddr_in);
494 saun->sin.sin_family = AF_INET;
495 saun->sin.sin_addr.s_addr = ia.s_addr;
496 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
509 * Try to find an address on the given outgoing interface
510 * that belongs to the jail.
513 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
516 if (sa->sa_family != AF_INET6)
518 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
519 &ia6, sizeof(struct in6_addr));
520 if (prison_check_ip6(cred, &ia6) == 0) {
525 IF_ADDR_RUNLOCK(ifp);
528 * As a last resort return the 'default' jail address.
530 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
532 if (prison_get_ip6(cred, &ia6) != 0)
535 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
536 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
537 saun->sin6.sin6_family = AF_INET6;
538 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
539 if (sa6_recoverscope(&saun->sin6) != 0)
541 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
553 route_output(struct mbuf *m, struct socket *so)
555 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
556 struct rt_msghdr *rtm = NULL;
557 struct rtentry *rt = NULL;
558 struct radix_node_head *rnh;
559 struct rt_addrinfo info;
561 struct sockaddr_storage ss;
562 struct sockaddr_in6 *sin6;
563 int i, rti_need_deembed = 0;
566 struct ifnet *ifp = NULL;
567 union sockaddr_union saun;
568 sa_family_t saf = AF_UNSPEC;
570 #define senderr(e) { error = e; goto flush;}
571 if (m == NULL || ((m->m_len < sizeof(long)) &&
572 (m = m_pullup(m, sizeof(long))) == NULL))
574 if ((m->m_flags & M_PKTHDR) == 0)
575 panic("route_output");
576 len = m->m_pkthdr.len;
577 if (len < sizeof(*rtm) ||
578 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
579 info.rti_info[RTAX_DST] = NULL;
582 R_Malloc(rtm, struct rt_msghdr *, len);
584 info.rti_info[RTAX_DST] = NULL;
587 m_copydata(m, 0, len, (caddr_t)rtm);
588 if (rtm->rtm_version != RTM_VERSION) {
589 info.rti_info[RTAX_DST] = NULL;
590 senderr(EPROTONOSUPPORT);
592 rtm->rtm_pid = curproc->p_pid;
593 bzero(&info, sizeof(info));
594 info.rti_addrs = rtm->rtm_addrs;
596 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
597 * link-local address because rtrequest requires addresses with
600 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
601 info.rti_info[RTAX_DST] = NULL;
604 info.rti_flags = rtm->rtm_flags;
605 if (info.rti_info[RTAX_DST] == NULL ||
606 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
607 (info.rti_info[RTAX_GATEWAY] != NULL &&
608 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
610 saf = info.rti_info[RTAX_DST]->sa_family;
612 * Verify that the caller has the appropriate privilege; RTM_GET
613 * is the only operation the non-superuser is allowed.
615 if (rtm->rtm_type != RTM_GET) {
616 error = priv_check(curthread, PRIV_NET_ROUTE);
622 * The given gateway address may be an interface address.
623 * For example, issuing a "route change" command on a route
624 * entry that was created from a tunnel, and the gateway
625 * address given is the local end point. In this case the
626 * RTF_GATEWAY flag must be cleared or the destination will
627 * not be reachable even though there is no error message.
629 if (info.rti_info[RTAX_GATEWAY] != NULL &&
630 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
633 bzero(&gw_ro, sizeof(gw_ro));
634 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
635 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
637 * A host route through the loopback interface is
638 * installed for each interface adddress. In pre 8.0
639 * releases the interface address of a PPP link type
640 * is not reachable locally. This behavior is fixed as
641 * part of the new L2/L3 redesign and rewrite work. The
642 * signature of this interface address route is the
643 * AF_LINK sa_family type of the rt_gateway, and the
644 * rt_ifp has the IFF_LOOPBACK flag set.
646 if (gw_ro.ro_rt != NULL &&
647 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
648 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
649 info.rti_flags &= ~RTF_GATEWAY;
650 info.rti_flags |= RTF_GWFLAG_COMPAT;
652 if (gw_ro.ro_rt != NULL)
656 switch (rtm->rtm_type) {
657 struct rtentry *saved_nrt;
660 if (info.rti_info[RTAX_GATEWAY] == NULL)
664 /* support for new ARP code */
665 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
666 (rtm->rtm_flags & RTF_LLDATA) != 0) {
667 error = lla_rt_output(rtm, &info);
670 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
674 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
676 if (error == 0 && saved_nrt) {
678 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
681 rt_setmetrics(rtm->rtm_inits,
682 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
683 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
684 RT_REMREF(saved_nrt);
685 RT_UNLOCK(saved_nrt);
691 /* support for new ARP code */
692 if (info.rti_info[RTAX_GATEWAY] &&
693 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
694 (rtm->rtm_flags & RTF_LLDATA) != 0) {
695 error = lla_rt_output(rtm, &info);
698 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
702 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
710 /* rt_msg2() will not be used when RTM_DELETE fails. */
711 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
718 rnh = rt_tables_get_rnh(so->so_fibnum,
719 info.rti_info[RTAX_DST]->sa_family);
721 senderr(EAFNOSUPPORT);
723 RADIX_NODE_HEAD_RLOCK(rnh);
725 if (info.rti_info[RTAX_NETMASK] == NULL &&
726 rtm->rtm_type == RTM_GET) {
728 * Provide logest prefix match for
729 * address lookup (no mask).
730 * 'route -n get addr'
732 rt = (struct rtentry *) rnh->rnh_matchaddr(
733 info.rti_info[RTAX_DST], rnh);
735 rt = (struct rtentry *) rnh->rnh_lookup(
736 info.rti_info[RTAX_DST],
737 info.rti_info[RTAX_NETMASK], rnh);
740 RADIX_NODE_HEAD_RUNLOCK(rnh);
745 * for RTM_CHANGE/LOCK, if we got multipath routes,
746 * we require users to specify a matching RTAX_GATEWAY.
748 * for RTM_GET, gate is optional even with multipath.
749 * if gate == NULL the first match is returned.
750 * (no need to call rt_mpath_matchgate if gate == NULL)
752 if (rn_mpath_capable(rnh) &&
753 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
754 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
756 RADIX_NODE_HEAD_RUNLOCK(rnh);
762 * If performing proxied L2 entry insertion, and
763 * the actual PPP host entry is found, perform
764 * another search to retrieve the prefix route of
765 * the local end point of the PPP link.
767 if (rtm->rtm_flags & RTF_ANNOUNCE) {
768 struct sockaddr laddr;
770 if (rt->rt_ifp != NULL &&
771 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
774 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
776 rt_maskedcopy(ifa->ifa_addr,
780 rt_maskedcopy(rt->rt_ifa->ifa_addr,
782 rt->rt_ifa->ifa_netmask);
784 * refactor rt and no lock operation necessary
786 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
788 RADIX_NODE_HEAD_RUNLOCK(rnh);
794 RADIX_NODE_HEAD_RUNLOCK(rnh);
796 switch(rtm->rtm_type) {
801 if ((rt->rt_flags & RTF_HOST) == 0
802 ? jailed_without_vnet(curthread->td_ucred)
803 : prison_if(curthread->td_ucred,
808 info.rti_info[RTAX_DST] = rt_key(rt);
809 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
810 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
811 info.rti_info[RTAX_GENMASK] = 0;
812 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
815 info.rti_info[RTAX_IFP] =
816 ifp->if_addr->ifa_addr;
817 error = rtm_get_jailed(&info, ifp, rt,
818 &saun, curthread->td_ucred);
823 if (ifp->if_flags & IFF_POINTOPOINT)
824 info.rti_info[RTAX_BRD] =
825 rt->rt_ifa->ifa_dstaddr;
826 rtm->rtm_index = ifp->if_index;
828 info.rti_info[RTAX_IFP] = NULL;
829 info.rti_info[RTAX_IFA] = NULL;
831 } else if ((ifp = rt->rt_ifp) != NULL) {
832 rtm->rtm_index = ifp->if_index;
834 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
835 if (len > rtm->rtm_msglen) {
836 struct rt_msghdr *new_rtm;
837 R_Malloc(new_rtm, struct rt_msghdr *, len);
838 if (new_rtm == NULL) {
842 bcopy(rtm, new_rtm, rtm->rtm_msglen);
843 Free(rtm); rtm = new_rtm;
845 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
846 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
847 rtm->rtm_flags = RTF_GATEWAY |
848 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
850 rtm->rtm_flags = rt->rt_flags;
851 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
852 rtm->rtm_addrs = info.rti_addrs;
857 * New gateway could require new ifaddr, ifp;
858 * flags may also be different; ifp may be specified
859 * by ll sockaddr when protocol address is ambiguous
861 if (((rt->rt_flags & RTF_GATEWAY) &&
862 info.rti_info[RTAX_GATEWAY] != NULL) ||
863 info.rti_info[RTAX_IFP] != NULL ||
864 (info.rti_info[RTAX_IFA] != NULL &&
865 !sa_equal(info.rti_info[RTAX_IFA],
866 rt->rt_ifa->ifa_addr))) {
868 RADIX_NODE_HEAD_LOCK(rnh);
869 error = rt_getifa_fib(&info, rt->rt_fibnum);
871 * XXXRW: Really we should release this
872 * reference later, but this maintains
873 * historical behavior.
875 if (info.rti_ifa != NULL)
876 ifa_free(info.rti_ifa);
877 RADIX_NODE_HEAD_UNLOCK(rnh);
882 if (info.rti_ifa != NULL &&
883 info.rti_ifa != rt->rt_ifa &&
884 rt->rt_ifa != NULL &&
885 rt->rt_ifa->ifa_rtrequest != NULL) {
886 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
888 ifa_free(rt->rt_ifa);
890 if (info.rti_info[RTAX_GATEWAY] != NULL) {
892 RADIX_NODE_HEAD_LOCK(rnh);
895 error = rt_setgate(rt, rt_key(rt),
896 info.rti_info[RTAX_GATEWAY]);
897 RADIX_NODE_HEAD_UNLOCK(rnh);
902 rt->rt_flags &= ~RTF_GATEWAY;
903 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
905 if (info.rti_ifa != NULL &&
906 info.rti_ifa != rt->rt_ifa) {
907 ifa_ref(info.rti_ifa);
908 rt->rt_ifa = info.rti_ifa;
909 rt->rt_ifp = info.rti_ifp;
911 /* Allow some flags to be toggled on change. */
912 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
913 (rtm->rtm_flags & RTF_FMASK);
914 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
916 rtm->rtm_index = rt->rt_ifp->if_index;
917 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
918 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
921 /* We don't support locks anymore */
934 rtm->rtm_errno = error;
936 rtm->rtm_flags |= RTF_DONE;
938 if (rt) /* XXX can this be true? */
941 struct rawcb *rp = NULL;
943 * Check to see if we don't want our own messages.
945 if ((so->so_options & SO_USELOOPBACK) == 0) {
946 if (route_cb.any_count <= 1) {
952 /* There is another listener, so construct message */
957 if (rti_need_deembed) {
958 /* sin6_scope_id is recovered before sending rtm. */
959 sin6 = (struct sockaddr_in6 *)&ss;
960 for (i = 0; i < RTAX_MAX; i++) {
961 if (info.rti_info[i] == NULL)
963 if (info.rti_info[i]->sa_family != AF_INET6)
965 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
966 if (sa6_recoverscope(sin6) == 0)
967 bcopy(sin6, info.rti_info[i],
972 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
973 if (m->m_pkthdr.len < rtm->rtm_msglen) {
976 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
977 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
980 M_SETFIB(m, so->so_fibnum);
981 m->m_flags |= RTS_FILTER_FIB;
984 * XXX insure we don't get a copy by
985 * invalidating our protocol
987 unsigned short family = rp->rcb_proto.sp_family;
988 rp->rcb_proto.sp_family = 0;
990 rp->rcb_proto.sp_family = family;
994 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
1003 rt_setmetrics(u_long which, const struct rt_metrics *in,
1004 struct rt_metrics_lite *out)
1006 #define metric(f, e) if (which & (f)) out->e = in->e;
1008 * Only these are stored in the routing entry since introduction
1009 * of tcp hostcache. The rest is ignored.
1011 metric(RTV_MTU, rmx_mtu);
1012 metric(RTV_WEIGHT, rmx_weight);
1013 /* Userland -> kernel timebase conversion. */
1014 if (which & RTV_EXPIRE)
1015 out->rmx_expire = in->rmx_expire ?
1016 in->rmx_expire - time_second + time_uptime : 0;
1021 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
1023 #define metric(e) out->e = in->e;
1024 bzero(out, sizeof(*out));
1027 /* Kernel -> userland timebase conversion. */
1028 out->rmx_expire = in->rmx_expire ?
1029 in->rmx_expire - time_uptime + time_second : 0;
1034 * Extract the addresses of the passed sockaddrs.
1035 * Do a little sanity checking so as to avoid bad memory references.
1036 * This data is derived straight from userland.
1039 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1041 struct sockaddr *sa;
1044 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1045 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1047 sa = (struct sockaddr *)cp;
1051 if (cp + sa->sa_len > cplim)
1054 * there are no more.. quit now
1055 * If there are more bits, they are in error.
1056 * I've seen this. route(1) can evidently generate these.
1057 * This causes kernel to core dump.
1058 * for compatibility, If we see this, point to a safe address.
1060 if (sa->sa_len == 0) {
1061 rtinfo->rti_info[i] = &sa_zero;
1062 return (0); /* should be EINVAL but for compat */
1066 if (sa->sa_family == AF_INET6)
1067 sa6_embedscope((struct sockaddr_in6 *)sa,
1070 rtinfo->rti_info[i] = sa;
1077 * Used by the routing socket.
1079 static struct mbuf *
1080 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1082 struct rt_msghdr *rtm;
1085 struct sockaddr *sa;
1087 struct sockaddr_storage ss;
1088 struct sockaddr_in6 *sin6;
1096 len = sizeof(struct ifa_msghdr);
1101 len = sizeof(struct ifma_msghdr);
1105 len = sizeof(struct if_msghdr);
1108 case RTM_IFANNOUNCE:
1110 len = sizeof(struct if_announcemsghdr);
1114 len = sizeof(struct rt_msghdr);
1117 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1118 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1120 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1122 m = m_gethdr(M_NOWAIT, MT_DATA);
1126 m->m_pkthdr.len = m->m_len = len;
1127 rtm = mtod(m, struct rt_msghdr *);
1128 bzero((caddr_t)rtm, len);
1129 for (i = 0; i < RTAX_MAX; i++) {
1130 if ((sa = rtinfo->rti_info[i]) == NULL)
1132 rtinfo->rti_addrs |= (1 << i);
1135 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1136 sin6 = (struct sockaddr_in6 *)&ss;
1137 bcopy(sa, sin6, sizeof(*sin6));
1138 if (sa6_recoverscope(sin6) == 0)
1139 sa = (struct sockaddr *)sin6;
1142 m_copyback(m, len, dlen, (caddr_t)sa);
1145 if (m->m_pkthdr.len != len) {
1149 rtm->rtm_msglen = len;
1150 rtm->rtm_version = RTM_VERSION;
1151 rtm->rtm_type = type;
1156 * Used by the sysctl code and routing socket.
1159 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1162 int len, dlen, second_time = 0;
1165 struct sockaddr_storage ss;
1166 struct sockaddr_in6 *sin6;
1169 rtinfo->rti_addrs = 0;
1175 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1176 #ifdef COMPAT_FREEBSD32
1177 if (w->w_req->flags & SCTL_MASK32)
1178 len = sizeof(struct ifa_msghdrl32);
1181 len = sizeof(struct ifa_msghdrl);
1183 len = sizeof(struct ifa_msghdr);
1187 #ifdef COMPAT_FREEBSD32
1188 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1189 if (w->w_op == NET_RT_IFLISTL)
1190 len = sizeof(struct if_msghdrl32);
1192 len = sizeof(struct if_msghdr32);
1196 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1197 len = sizeof(struct if_msghdrl);
1199 len = sizeof(struct if_msghdr);
1203 len = sizeof(struct ifma_msghdr);
1207 len = sizeof(struct rt_msghdr);
1212 for (i = 0; i < RTAX_MAX; i++) {
1213 struct sockaddr *sa;
1215 if ((sa = rtinfo->rti_info[i]) == NULL)
1217 rtinfo->rti_addrs |= (1 << i);
1221 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1222 sin6 = (struct sockaddr_in6 *)&ss;
1223 bcopy(sa, sin6, sizeof(*sin6));
1224 if (sa6_recoverscope(sin6) == 0)
1225 sa = (struct sockaddr *)sin6;
1228 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1234 if (cp == NULL && w != NULL && !second_time) {
1235 struct walkarg *rw = w;
1238 if (rw->w_tmemsize < len) {
1240 free(rw->w_tmem, M_RTABLE);
1241 rw->w_tmem = (caddr_t)
1242 malloc(len, M_RTABLE, M_NOWAIT);
1244 rw->w_tmemsize = len;
1254 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1256 rtm->rtm_version = RTM_VERSION;
1257 rtm->rtm_type = type;
1258 rtm->rtm_msglen = len;
1264 * This routine is called to generate a message from the routing
1265 * socket indicating that a redirect has occured, a routing lookup
1266 * has failed, or that a protocol has detected timeouts to a particular
1270 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1273 struct rt_msghdr *rtm;
1275 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1277 if (route_cb.any_count == 0)
1279 m = rt_msg1(type, rtinfo);
1283 if (fibnum != RT_ALL_FIBS) {
1284 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1285 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1286 M_SETFIB(m, fibnum);
1287 m->m_flags |= RTS_FILTER_FIB;
1290 rtm = mtod(m, struct rt_msghdr *);
1291 rtm->rtm_flags = RTF_DONE | flags;
1292 rtm->rtm_errno = error;
1293 rtm->rtm_addrs = rtinfo->rti_addrs;
1294 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1298 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1301 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1305 * This routine is called to generate a message from the routing
1306 * socket indicating that the status of a network interface has changed.
1309 rt_ifmsg(struct ifnet *ifp)
1311 struct if_msghdr *ifm;
1313 struct rt_addrinfo info;
1315 if (route_cb.any_count == 0)
1317 bzero((caddr_t)&info, sizeof(info));
1318 m = rt_msg1(RTM_IFINFO, &info);
1321 ifm = mtod(m, struct if_msghdr *);
1322 ifm->ifm_index = ifp->if_index;
1323 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1324 ifm->ifm_data = ifp->if_data;
1326 rt_dispatch(m, AF_UNSPEC);
1330 * Announce interface address arrival/withdraw.
1331 * Please do not call directly, use rt_addrmsg().
1332 * Assume input data to be valid.
1333 * Returns 0 on success.
1336 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1338 struct rt_addrinfo info;
1339 struct sockaddr *sa;
1342 struct ifa_msghdr *ifam;
1343 struct ifnet *ifp = ifa->ifa_ifp;
1345 if (route_cb.any_count == 0)
1348 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1350 bzero((caddr_t)&info, sizeof(info));
1351 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1352 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1353 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1354 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1355 if ((m = rt_msg1(ncmd, &info)) == NULL)
1357 ifam = mtod(m, struct ifa_msghdr *);
1358 ifam->ifam_index = ifp->if_index;
1359 ifam->ifam_metric = ifa->ifa_metric;
1360 ifam->ifam_flags = ifa->ifa_flags;
1361 ifam->ifam_addrs = info.rti_addrs;
1363 if (fibnum != RT_ALL_FIBS) {
1364 M_SETFIB(m, fibnum);
1365 m->m_flags |= RTS_FILTER_FIB;
1368 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1374 * Announce route addition/removal.
1375 * Please do not call directly, use rt_routemsg().
1376 * Note that @rt data MAY be inconsistent/invalid:
1377 * if some userland app sends us "invalid" route message (invalid mask,
1378 * no dst, wrong address families, etc...) we need to pass it back
1379 * to app (and any other rtsock consumers) with rtm_errno field set to
1382 * Returns 0 on success.
1385 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1388 struct rt_addrinfo info;
1389 struct sockaddr *sa;
1391 struct rt_msghdr *rtm;
1393 if (route_cb.any_count == 0)
1396 bzero((caddr_t)&info, sizeof(info));
1397 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1398 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1399 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1400 if ((m = rt_msg1(cmd, &info)) == NULL)
1402 rtm = mtod(m, struct rt_msghdr *);
1403 rtm->rtm_index = ifp->if_index;
1404 rtm->rtm_flags |= rt->rt_flags;
1405 rtm->rtm_errno = error;
1406 rtm->rtm_addrs = info.rti_addrs;
1408 if (fibnum != RT_ALL_FIBS) {
1409 M_SETFIB(m, fibnum);
1410 m->m_flags |= RTS_FILTER_FIB;
1413 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1419 * This is the analogue to the rt_newaddrmsg which performs the same
1420 * function but for multicast group memberhips. This is easier since
1421 * there is no route state to worry about.
1424 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1426 struct rt_addrinfo info;
1427 struct mbuf *m = NULL;
1428 struct ifnet *ifp = ifma->ifma_ifp;
1429 struct ifma_msghdr *ifmam;
1431 if (route_cb.any_count == 0)
1434 bzero((caddr_t)&info, sizeof(info));
1435 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1436 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1438 * If a link-layer address is present, present it as a ``gateway''
1439 * (similarly to how ARP entries, e.g., are presented).
1441 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1442 m = rt_msg1(cmd, &info);
1445 ifmam = mtod(m, struct ifma_msghdr *);
1446 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1448 ifmam->ifmam_index = ifp->if_index;
1449 ifmam->ifmam_addrs = info.rti_addrs;
1450 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1453 static struct mbuf *
1454 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1455 struct rt_addrinfo *info)
1457 struct if_announcemsghdr *ifan;
1460 if (route_cb.any_count == 0)
1462 bzero((caddr_t)info, sizeof(*info));
1463 m = rt_msg1(type, info);
1465 ifan = mtod(m, struct if_announcemsghdr *);
1466 ifan->ifan_index = ifp->if_index;
1467 strlcpy(ifan->ifan_name, ifp->if_xname,
1468 sizeof(ifan->ifan_name));
1469 ifan->ifan_what = what;
1475 * This is called to generate routing socket messages indicating
1476 * IEEE80211 wireless events.
1477 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1480 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1483 struct rt_addrinfo info;
1485 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1488 * Append the ieee80211 data. Try to stick it in the
1489 * mbuf containing the ifannounce msg; otherwise allocate
1490 * a new mbuf and append.
1492 * NB: we assume m is a single mbuf.
1494 if (data_len > M_TRAILINGSPACE(m)) {
1495 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1500 bcopy(data, mtod(n, void *), data_len);
1501 n->m_len = data_len;
1503 } else if (data_len > 0) {
1504 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1505 m->m_len += data_len;
1507 if (m->m_flags & M_PKTHDR)
1508 m->m_pkthdr.len += data_len;
1509 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1510 rt_dispatch(m, AF_UNSPEC);
1515 * This is called to generate routing socket messages indicating
1516 * network interface arrival and departure.
1519 rt_ifannouncemsg(struct ifnet *ifp, int what)
1522 struct rt_addrinfo info;
1524 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1526 rt_dispatch(m, AF_UNSPEC);
1530 rt_dispatch(struct mbuf *m, sa_family_t saf)
1535 * Preserve the family from the sockaddr, if any, in an m_tag for
1536 * use when injecting the mbuf into the routing socket buffer from
1539 if (saf != AF_UNSPEC) {
1540 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1546 *(unsigned short *)(tag + 1) = saf;
1547 m_tag_prepend(m, tag);
1551 m->m_pkthdr.rcvif = V_loif;
1557 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1561 * This is used in dumping the kernel table via sysctl().
1564 sysctl_dumpentry(struct radix_node *rn, void *vw)
1566 struct walkarg *w = vw;
1567 struct rtentry *rt = (struct rtentry *)rn;
1568 int error = 0, size;
1569 struct rt_addrinfo info;
1571 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1573 if ((rt->rt_flags & RTF_HOST) == 0
1574 ? jailed_without_vnet(w->w_req->td->td_ucred)
1575 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1577 bzero((caddr_t)&info, sizeof(info));
1578 info.rti_info[RTAX_DST] = rt_key(rt);
1579 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1580 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1581 info.rti_info[RTAX_GENMASK] = 0;
1583 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1584 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1585 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1586 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1588 size = rt_msg2(RTM_GET, &info, NULL, w);
1589 if (w->w_req && w->w_tmem) {
1590 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1592 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1593 rtm->rtm_flags = RTF_GATEWAY |
1594 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1596 rtm->rtm_flags = rt->rt_flags;
1598 * let's be honest about this being a retarded hack
1600 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1601 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1602 rtm->rtm_index = rt->rt_ifp->if_index;
1603 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1604 rtm->rtm_addrs = info.rti_addrs;
1605 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1611 #ifdef COMPAT_FREEBSD32
1613 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1616 bzero(dst, sizeof(*dst));
1617 CP(*src, *dst, ifi_type);
1618 CP(*src, *dst, ifi_physical);
1619 CP(*src, *dst, ifi_addrlen);
1620 CP(*src, *dst, ifi_hdrlen);
1621 CP(*src, *dst, ifi_link_state);
1622 CP(*src, *dst, ifi_vhid);
1623 CP(*src, *dst, ifi_baudrate_pf);
1624 dst->ifi_datalen = sizeof(struct if_data32);
1625 CP(*src, *dst, ifi_mtu);
1626 CP(*src, *dst, ifi_metric);
1627 CP(*src, *dst, ifi_baudrate);
1628 CP(*src, *dst, ifi_ipackets);
1629 CP(*src, *dst, ifi_ierrors);
1630 CP(*src, *dst, ifi_opackets);
1631 CP(*src, *dst, ifi_oerrors);
1632 CP(*src, *dst, ifi_collisions);
1633 CP(*src, *dst, ifi_ibytes);
1634 CP(*src, *dst, ifi_obytes);
1635 CP(*src, *dst, ifi_imcasts);
1636 CP(*src, *dst, ifi_omcasts);
1637 CP(*src, *dst, ifi_iqdrops);
1638 CP(*src, *dst, ifi_noproto);
1639 CP(*src, *dst, ifi_hwassist);
1640 CP(*src, *dst, ifi_epoch);
1641 TV_CP(*src, *dst, ifi_lastchange);
1646 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1647 struct walkarg *w, int len)
1649 struct if_msghdrl *ifm;
1651 #ifdef COMPAT_FREEBSD32
1652 if (w->w_req->flags & SCTL_MASK32) {
1653 struct if_msghdrl32 *ifm32;
1655 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1656 ifm32->ifm_addrs = info->rti_addrs;
1657 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1658 ifm32->ifm_index = ifp->if_index;
1659 ifm32->_ifm_spare1 = 0;
1660 ifm32->ifm_len = sizeof(*ifm32);
1661 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1663 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1664 /* Fixup if_data carp(4) vhid. */
1665 if (carp_get_vhid_p != NULL)
1666 ifm32->ifm_data.ifi_vhid =
1667 (*carp_get_vhid_p)(ifp->if_addr);
1669 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1672 ifm = (struct if_msghdrl *)w->w_tmem;
1673 ifm->ifm_addrs = info->rti_addrs;
1674 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1675 ifm->ifm_index = ifp->if_index;
1676 ifm->_ifm_spare1 = 0;
1677 ifm->ifm_len = sizeof(*ifm);
1678 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1680 ifm->ifm_data = ifp->if_data;
1681 /* Fixup if_data carp(4) vhid. */
1682 if (carp_get_vhid_p != NULL)
1683 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1685 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1689 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1690 struct walkarg *w, int len)
1692 struct if_msghdr *ifm;
1694 #ifdef COMPAT_FREEBSD32
1695 if (w->w_req->flags & SCTL_MASK32) {
1696 struct if_msghdr32 *ifm32;
1698 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1699 ifm32->ifm_addrs = info->rti_addrs;
1700 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1701 ifm32->ifm_index = ifp->if_index;
1703 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1704 /* Fixup if_data carp(4) vhid. */
1705 if (carp_get_vhid_p != NULL)
1706 ifm32->ifm_data.ifi_vhid =
1707 (*carp_get_vhid_p)(ifp->if_addr);
1709 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1712 ifm = (struct if_msghdr *)w->w_tmem;
1713 ifm->ifm_addrs = info->rti_addrs;
1714 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1715 ifm->ifm_index = ifp->if_index;
1717 ifm->ifm_data = ifp->if_data;
1718 /* Fixup if_data carp(4) vhid. */
1719 if (carp_get_vhid_p != NULL)
1720 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1722 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1726 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1727 struct walkarg *w, int len)
1729 struct ifa_msghdrl *ifam;
1731 #ifdef COMPAT_FREEBSD32
1732 if (w->w_req->flags & SCTL_MASK32) {
1733 struct ifa_msghdrl32 *ifam32;
1735 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1736 ifam32->ifam_addrs = info->rti_addrs;
1737 ifam32->ifam_flags = ifa->ifa_flags;
1738 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1739 ifam32->_ifam_spare1 = 0;
1740 ifam32->ifam_len = sizeof(*ifam32);
1741 ifam32->ifam_data_off =
1742 offsetof(struct ifa_msghdrl32, ifam_data);
1743 ifam32->ifam_metric = ifa->ifa_metric;
1745 bzero(&ifam32->ifam_data, sizeof(ifam32->ifam_data));
1746 ifam32->ifam_data.ifi_datalen = sizeof(struct if_data32);
1747 ifam32->ifam_data.ifi_ipackets =
1748 counter_u64_fetch(ifa->ifa_ipackets);
1749 ifam32->ifam_data.ifi_opackets =
1750 counter_u64_fetch(ifa->ifa_opackets);
1751 ifam32->ifam_data.ifi_ibytes =
1752 counter_u64_fetch(ifa->ifa_ibytes);
1753 ifam32->ifam_data.ifi_obytes =
1754 counter_u64_fetch(ifa->ifa_obytes);
1756 /* Fixup if_data carp(4) vhid. */
1757 if (carp_get_vhid_p != NULL)
1758 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1760 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1764 ifam = (struct ifa_msghdrl *)w->w_tmem;
1765 ifam->ifam_addrs = info->rti_addrs;
1766 ifam->ifam_flags = ifa->ifa_flags;
1767 ifam->ifam_index = ifa->ifa_ifp->if_index;
1768 ifam->_ifam_spare1 = 0;
1769 ifam->ifam_len = sizeof(*ifam);
1770 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1771 ifam->ifam_metric = ifa->ifa_metric;
1773 bzero(&ifam->ifam_data, sizeof(ifam->ifam_data));
1774 ifam->ifam_data.ifi_datalen = sizeof(struct if_data);
1775 ifam->ifam_data.ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1776 ifam->ifam_data.ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1777 ifam->ifam_data.ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1778 ifam->ifam_data.ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1780 /* Fixup if_data carp(4) vhid. */
1781 if (carp_get_vhid_p != NULL)
1782 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1784 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1788 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1789 struct walkarg *w, int len)
1791 struct ifa_msghdr *ifam;
1793 ifam = (struct ifa_msghdr *)w->w_tmem;
1794 ifam->ifam_addrs = info->rti_addrs;
1795 ifam->ifam_flags = ifa->ifa_flags;
1796 ifam->ifam_index = ifa->ifa_ifp->if_index;
1797 ifam->ifam_metric = ifa->ifa_metric;
1799 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1803 sysctl_iflist(int af, struct walkarg *w)
1807 struct rt_addrinfo info;
1810 bzero((caddr_t)&info, sizeof(info));
1811 IFNET_RLOCK_NOSLEEP();
1812 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1813 if (w->w_arg && w->w_arg != ifp->if_index)
1817 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1818 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1819 info.rti_info[RTAX_IFP] = NULL;
1820 if (w->w_req && w->w_tmem) {
1821 if (w->w_op == NET_RT_IFLISTL)
1822 error = sysctl_iflist_ifml(ifp, &info, w, len);
1824 error = sysctl_iflist_ifm(ifp, &info, w, len);
1828 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1829 if (af && af != ifa->ifa_addr->sa_family)
1831 if (prison_if(w->w_req->td->td_ucred,
1832 ifa->ifa_addr) != 0)
1834 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1835 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1836 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1837 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1838 if (w->w_req && w->w_tmem) {
1839 if (w->w_op == NET_RT_IFLISTL)
1840 error = sysctl_iflist_ifaml(ifa, &info,
1843 error = sysctl_iflist_ifam(ifa, &info,
1849 IF_ADDR_RUNLOCK(ifp);
1850 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1851 info.rti_info[RTAX_BRD] = NULL;
1855 IF_ADDR_RUNLOCK(ifp);
1856 IFNET_RUNLOCK_NOSLEEP();
1861 sysctl_ifmalist(int af, struct walkarg *w)
1864 struct ifmultiaddr *ifma;
1865 struct rt_addrinfo info;
1869 bzero((caddr_t)&info, sizeof(info));
1870 IFNET_RLOCK_NOSLEEP();
1871 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1872 if (w->w_arg && w->w_arg != ifp->if_index)
1875 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1877 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1878 if (af && af != ifma->ifma_addr->sa_family)
1880 if (prison_if(w->w_req->td->td_ucred,
1881 ifma->ifma_addr) != 0)
1883 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1884 info.rti_info[RTAX_GATEWAY] =
1885 (ifma->ifma_addr->sa_family != AF_LINK) ?
1886 ifma->ifma_lladdr : NULL;
1887 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1888 if (w->w_req && w->w_tmem) {
1889 struct ifma_msghdr *ifmam;
1891 ifmam = (struct ifma_msghdr *)w->w_tmem;
1892 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1893 ifmam->ifmam_flags = 0;
1894 ifmam->ifmam_addrs = info.rti_addrs;
1895 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1897 IF_ADDR_RUNLOCK(ifp);
1902 IF_ADDR_RUNLOCK(ifp);
1905 IFNET_RUNLOCK_NOSLEEP();
1910 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1912 int *name = (int *)arg1;
1913 u_int namelen = arg2;
1914 struct radix_node_head *rnh = NULL; /* silence compiler. */
1915 int i, lim, error = EINVAL;
1924 if (name[1] == NET_RT_DUMP) {
1926 fib = req->td->td_proc->p_fibnum;
1927 else if (namelen == 4)
1928 fib = (name[3] == RT_ALL_FIBS) ?
1929 req->td->td_proc->p_fibnum : name[3];
1931 return ((namelen < 3) ? EISDIR : ENOTDIR);
1932 if (fib < 0 || fib >= rt_numfibs)
1934 } else if (namelen != 3)
1935 return ((namelen < 3) ? EISDIR : ENOTDIR);
1939 bzero(&w, sizeof(w));
1944 error = sysctl_wire_old_buffer(req, 0);
1951 if (af == 0) { /* dump all tables */
1954 } else /* dump only one table */
1958 * take care of llinfo entries, the caller must
1961 if (w.w_op == NET_RT_FLAGS &&
1962 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1964 error = lltable_sysctl_dumparp(af, w.w_req);
1970 * take care of routing entries
1972 for (error = 0; error == 0 && i <= lim; i++) {
1973 rnh = rt_tables_get_rnh(fib, i);
1975 RADIX_NODE_HEAD_RLOCK(rnh);
1976 error = rnh->rnh_walktree(rnh,
1977 sysctl_dumpentry, &w);
1978 RADIX_NODE_HEAD_RUNLOCK(rnh);
1980 error = EAFNOSUPPORT;
1985 case NET_RT_IFLISTL:
1986 error = sysctl_iflist(af, &w);
1989 case NET_RT_IFMALIST:
1990 error = sysctl_ifmalist(af, &w);
1994 free(w.w_tmem, M_RTABLE);
1998 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
2001 * Definitions of protocols supported in the ROUTE domain.
2004 static struct domain routedomain; /* or at least forward */
2006 static struct protosw routesw[] = {
2008 .pr_type = SOCK_RAW,
2009 .pr_domain = &routedomain,
2010 .pr_flags = PR_ATOMIC|PR_ADDR,
2011 .pr_output = route_output,
2012 .pr_ctlinput = raw_ctlinput,
2013 .pr_init = raw_init,
2014 .pr_usrreqs = &route_usrreqs
2018 static struct domain routedomain = {
2019 .dom_family = PF_ROUTE,
2020 .dom_name = "route",
2021 .dom_protosw = routesw,
2022 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
2025 VNET_DOMAIN_SET(route);