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"
34 #include "opt_mpath.h"
36 #include "opt_inet6.h"
38 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
43 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
55 #define _IN_NET_RTSOCK_C
57 #include <net/if_dl.h>
58 #include <net/if_llatbl.h>
59 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/raw_cb.h>
62 #include <net/route.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 #if defined(INET) || defined(INET6)
75 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
79 #ifdef COMPAT_FREEBSD32
80 #include <sys/mount.h>
81 #include <compat/freebsd32/freebsd32.h>
88 uint8_t ifi_link_state;
90 uint8_t ifi_baudrate_pf;
94 uint32_t ifi_baudrate;
95 uint32_t ifi_ipackets;
97 uint32_t ifi_opackets;
99 uint32_t ifi_collisions;
102 uint32_t ifi_imcasts;
103 uint32_t ifi_omcasts;
104 uint32_t ifi_iqdrops;
105 uint32_t ifi_noproto;
106 uint32_t ifi_hwassist;
108 struct timeval32 ifi_lastchange;
109 uint32_t ifi_oqdrops;
119 struct if_data32 ifm_data;
122 struct if_msghdrl32 {
129 uint16_t _ifm_spare1;
131 uint16_t ifm_data_off;
132 struct if_data32 ifm_data;
135 struct ifa_msghdrl32 {
136 uint16_t ifam_msglen;
137 uint8_t ifam_version;
142 uint16_t _ifam_spare1;
144 uint16_t ifam_data_off;
146 struct if_data32 ifam_data;
148 #endif /* COMPAT_FREEBSD32 */
150 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
152 /* NB: these are not modified */
153 static struct sockaddr route_src = { 2, PF_ROUTE, };
154 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
156 /* These are external hooks for CARP. */
157 int (*carp_get_vhid_p)(struct ifaddr *);
160 * Used by rtsock/raw_input callback code to decide whether to filter the update
161 * notification to a socket bound to a particular FIB.
163 #define RTS_FILTER_FIB M_PROTO8
166 int ip_count; /* attached w/ AF_INET */
167 int ip6_count; /* attached w/ AF_INET6 */
168 int ipx_count; /* attached w/ AF_IPX */
169 int any_count; /* total attached */
172 struct mtx rtsock_mtx;
173 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
175 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
176 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
177 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
179 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
185 struct sysctl_req *w_req;
188 static void rts_input(struct mbuf *m);
189 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
190 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
191 caddr_t cp, struct walkarg *w);
192 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
193 struct rt_addrinfo *rtinfo);
194 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
195 static int sysctl_iflist(int af, struct walkarg *w);
196 static int sysctl_ifmalist(int af, struct walkarg *w);
197 static int route_output(struct mbuf *m, struct socket *so);
198 static void rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt);
199 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
200 static void rt_dispatch(struct mbuf *, sa_family_t);
202 static struct netisr_handler rtsock_nh = {
204 .nh_handler = rts_input,
205 .nh_proto = NETISR_ROUTE,
206 .nh_policy = NETISR_POLICY_SOURCE,
210 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
214 netisr_getqlimit(&rtsock_nh, &qlimit);
215 error = sysctl_handle_int(oidp, &qlimit, 0, req);
216 if (error || !req->newptr)
220 return (netisr_setqlimit(&rtsock_nh, qlimit));
222 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
223 0, 0, sysctl_route_netisr_maxqlen, "I",
224 "maximum routing socket dispatch queue length");
231 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
232 rtsock_nh.nh_qlimit = tmp;
233 netisr_register(&rtsock_nh);
235 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
238 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
243 KASSERT(m != NULL, ("%s: m is NULL", __func__));
244 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
245 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
247 /* No filtering requested. */
248 if ((m->m_flags & RTS_FILTER_FIB) == 0)
251 /* Check if it is a rts and the fib matches the one of the socket. */
252 fibnum = M_GETFIB(m);
253 if (proto->sp_family != PF_ROUTE ||
254 rp->rcb_socket == NULL ||
255 rp->rcb_socket->so_fibnum == fibnum)
258 /* Filtering requested and no match, the socket shall be skipped. */
263 rts_input(struct mbuf *m)
265 struct sockproto route_proto;
266 unsigned short *family;
269 route_proto.sp_family = PF_ROUTE;
270 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
272 family = (unsigned short *)(tag + 1);
273 route_proto.sp_protocol = *family;
274 m_tag_delete(m, tag);
276 route_proto.sp_protocol = 0;
278 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
282 * It really doesn't make any sense at all for this code to share much
283 * with raw_usrreq.c, since its functionality is so restricted. XXX
286 rts_abort(struct socket *so)
289 raw_usrreqs.pru_abort(so);
293 rts_close(struct socket *so)
296 raw_usrreqs.pru_close(so);
299 /* pru_accept is EOPNOTSUPP */
302 rts_attach(struct socket *so, int proto, struct thread *td)
307 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
310 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
314 so->so_pcb = (caddr_t)rp;
315 so->so_fibnum = td->td_proc->p_fibnum;
316 error = raw_attach(so, proto);
324 switch(rp->rcb_proto.sp_protocol) {
329 route_cb.ip6_count++;
332 route_cb.ipx_count++;
335 route_cb.any_count++;
338 so->so_options |= SO_USELOOPBACK;
343 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
346 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
350 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
353 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
356 /* pru_connect2 is EOPNOTSUPP */
357 /* pru_control is EOPNOTSUPP */
360 rts_detach(struct socket *so)
362 struct rawcb *rp = sotorawcb(so);
364 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
367 switch(rp->rcb_proto.sp_protocol) {
372 route_cb.ip6_count--;
375 route_cb.ipx_count--;
378 route_cb.any_count--;
380 raw_usrreqs.pru_detach(so);
384 rts_disconnect(struct socket *so)
387 return (raw_usrreqs.pru_disconnect(so));
390 /* pru_listen is EOPNOTSUPP */
393 rts_peeraddr(struct socket *so, struct sockaddr **nam)
396 return (raw_usrreqs.pru_peeraddr(so, nam));
399 /* pru_rcvd is EOPNOTSUPP */
400 /* pru_rcvoob is EOPNOTSUPP */
403 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
404 struct mbuf *control, struct thread *td)
407 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
410 /* pru_sense is null */
413 rts_shutdown(struct socket *so)
416 return (raw_usrreqs.pru_shutdown(so));
420 rts_sockaddr(struct socket *so, struct sockaddr **nam)
423 return (raw_usrreqs.pru_sockaddr(so, nam));
426 static struct pr_usrreqs route_usrreqs = {
427 .pru_abort = rts_abort,
428 .pru_attach = rts_attach,
429 .pru_bind = rts_bind,
430 .pru_connect = rts_connect,
431 .pru_detach = rts_detach,
432 .pru_disconnect = rts_disconnect,
433 .pru_peeraddr = rts_peeraddr,
434 .pru_send = rts_send,
435 .pru_shutdown = rts_shutdown,
436 .pru_sockaddr = rts_sockaddr,
437 .pru_close = rts_close,
440 #ifndef _SOCKADDR_UNION_DEFINED
441 #define _SOCKADDR_UNION_DEFINED
443 * The union of all possible address formats we handle.
445 union sockaddr_union {
447 struct sockaddr_in sin;
448 struct sockaddr_in6 sin6;
450 #endif /* _SOCKADDR_UNION_DEFINED */
453 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
454 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
457 /* First, see if the returned address is part of the jail. */
458 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
459 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
463 switch (info->rti_info[RTAX_DST]->sa_family) {
473 * Try to find an address on the given outgoing interface
474 * that belongs to the jail.
477 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
480 if (sa->sa_family != AF_INET)
482 ia = ((struct sockaddr_in *)sa)->sin_addr;
483 if (prison_check_ip4(cred, &ia) == 0) {
488 IF_ADDR_RUNLOCK(ifp);
491 * As a last resort return the 'default' jail address.
493 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
495 if (prison_get_ip4(cred, &ia) != 0)
498 bzero(&saun->sin, sizeof(struct sockaddr_in));
499 saun->sin.sin_len = sizeof(struct sockaddr_in);
500 saun->sin.sin_family = AF_INET;
501 saun->sin.sin_addr.s_addr = ia.s_addr;
502 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
515 * Try to find an address on the given outgoing interface
516 * that belongs to the jail.
519 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
522 if (sa->sa_family != AF_INET6)
524 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
525 &ia6, sizeof(struct in6_addr));
526 if (prison_check_ip6(cred, &ia6) == 0) {
531 IF_ADDR_RUNLOCK(ifp);
534 * As a last resort return the 'default' jail address.
536 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
538 if (prison_get_ip6(cred, &ia6) != 0)
541 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
542 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
543 saun->sin6.sin6_family = AF_INET6;
544 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
545 if (sa6_recoverscope(&saun->sin6) != 0)
547 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
559 route_output(struct mbuf *m, struct socket *so)
561 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
562 struct rt_msghdr *rtm = NULL;
563 struct rtentry *rt = NULL;
564 struct radix_node_head *rnh;
565 struct rt_addrinfo info;
567 struct sockaddr_storage ss;
568 struct sockaddr_in6 *sin6;
569 int i, rti_need_deembed = 0;
572 struct ifnet *ifp = NULL;
573 union sockaddr_union saun;
574 sa_family_t saf = AF_UNSPEC;
576 #define senderr(e) { error = e; goto flush;}
577 if (m == NULL || ((m->m_len < sizeof(long)) &&
578 (m = m_pullup(m, sizeof(long))) == NULL))
580 if ((m->m_flags & M_PKTHDR) == 0)
581 panic("route_output");
582 len = m->m_pkthdr.len;
583 if (len < sizeof(*rtm) ||
584 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
585 info.rti_info[RTAX_DST] = NULL;
588 R_Malloc(rtm, struct rt_msghdr *, len);
590 info.rti_info[RTAX_DST] = NULL;
593 m_copydata(m, 0, len, (caddr_t)rtm);
594 if (rtm->rtm_version != RTM_VERSION) {
595 info.rti_info[RTAX_DST] = NULL;
596 senderr(EPROTONOSUPPORT);
598 rtm->rtm_pid = curproc->p_pid;
599 bzero(&info, sizeof(info));
600 info.rti_addrs = rtm->rtm_addrs;
602 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
603 * link-local address because rtrequest requires addresses with
606 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
607 info.rti_info[RTAX_DST] = NULL;
610 info.rti_flags = rtm->rtm_flags;
611 if (info.rti_info[RTAX_DST] == NULL ||
612 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
613 (info.rti_info[RTAX_GATEWAY] != NULL &&
614 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
616 saf = info.rti_info[RTAX_DST]->sa_family;
618 * Verify that the caller has the appropriate privilege; RTM_GET
619 * is the only operation the non-superuser is allowed.
621 if (rtm->rtm_type != RTM_GET) {
622 error = priv_check(curthread, PRIV_NET_ROUTE);
628 * The given gateway address may be an interface address.
629 * For example, issuing a "route change" command on a route
630 * entry that was created from a tunnel, and the gateway
631 * address given is the local end point. In this case the
632 * RTF_GATEWAY flag must be cleared or the destination will
633 * not be reachable even though there is no error message.
635 if (info.rti_info[RTAX_GATEWAY] != NULL &&
636 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
639 bzero(&gw_ro, sizeof(gw_ro));
640 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
641 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
643 * A host route through the loopback interface is
644 * installed for each interface adddress. In pre 8.0
645 * releases the interface address of a PPP link type
646 * is not reachable locally. This behavior is fixed as
647 * part of the new L2/L3 redesign and rewrite work. The
648 * signature of this interface address route is the
649 * AF_LINK sa_family type of the rt_gateway, and the
650 * rt_ifp has the IFF_LOOPBACK flag set.
652 if (gw_ro.ro_rt != NULL &&
653 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
654 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
655 info.rti_flags &= ~RTF_GATEWAY;
656 info.rti_flags |= RTF_GWFLAG_COMPAT;
658 if (gw_ro.ro_rt != NULL)
662 switch (rtm->rtm_type) {
663 struct rtentry *saved_nrt;
666 if (info.rti_info[RTAX_GATEWAY] == NULL)
670 /* support for new ARP code */
671 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
672 (rtm->rtm_flags & RTF_LLDATA) != 0) {
673 error = lla_rt_output(rtm, &info);
676 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
680 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
682 if (error == 0 && saved_nrt) {
684 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
687 rt_setmetrics(rtm, saved_nrt);
688 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
689 RT_REMREF(saved_nrt);
690 RT_UNLOCK(saved_nrt);
696 /* support for new ARP code */
697 if (info.rti_info[RTAX_GATEWAY] &&
698 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
699 (rtm->rtm_flags & RTF_LLDATA) != 0) {
700 error = lla_rt_output(rtm, &info);
703 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
707 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
715 /* rt_msg2() will not be used when RTM_DELETE fails. */
716 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
723 rnh = rt_tables_get_rnh(so->so_fibnum,
724 info.rti_info[RTAX_DST]->sa_family);
726 senderr(EAFNOSUPPORT);
728 RADIX_NODE_HEAD_RLOCK(rnh);
730 if (info.rti_info[RTAX_NETMASK] == NULL &&
731 rtm->rtm_type == RTM_GET) {
733 * Provide logest prefix match for
734 * address lookup (no mask).
735 * 'route -n get addr'
737 rt = (struct rtentry *) rnh->rnh_matchaddr(
738 info.rti_info[RTAX_DST], rnh);
740 rt = (struct rtentry *) rnh->rnh_lookup(
741 info.rti_info[RTAX_DST],
742 info.rti_info[RTAX_NETMASK], rnh);
745 RADIX_NODE_HEAD_RUNLOCK(rnh);
750 * for RTM_CHANGE/LOCK, if we got multipath routes,
751 * we require users to specify a matching RTAX_GATEWAY.
753 * for RTM_GET, gate is optional even with multipath.
754 * if gate == NULL the first match is returned.
755 * (no need to call rt_mpath_matchgate if gate == NULL)
757 if (rn_mpath_capable(rnh) &&
758 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
759 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
761 RADIX_NODE_HEAD_RUNLOCK(rnh);
767 * If performing proxied L2 entry insertion, and
768 * the actual PPP host entry is found, perform
769 * another search to retrieve the prefix route of
770 * the local end point of the PPP link.
772 if (rtm->rtm_flags & RTF_ANNOUNCE) {
773 struct sockaddr laddr;
775 if (rt->rt_ifp != NULL &&
776 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
779 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
781 rt_maskedcopy(ifa->ifa_addr,
785 rt_maskedcopy(rt->rt_ifa->ifa_addr,
787 rt->rt_ifa->ifa_netmask);
789 * refactor rt and no lock operation necessary
791 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
793 RADIX_NODE_HEAD_RUNLOCK(rnh);
799 RADIX_NODE_HEAD_RUNLOCK(rnh);
801 switch(rtm->rtm_type) {
806 if ((rt->rt_flags & RTF_HOST) == 0
807 ? jailed_without_vnet(curthread->td_ucred)
808 : prison_if(curthread->td_ucred,
813 info.rti_info[RTAX_DST] = rt_key(rt);
814 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
815 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
816 info.rti_info[RTAX_GENMASK] = 0;
817 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
820 info.rti_info[RTAX_IFP] =
821 ifp->if_addr->ifa_addr;
822 error = rtm_get_jailed(&info, ifp, rt,
823 &saun, curthread->td_ucred);
828 if (ifp->if_flags & IFF_POINTOPOINT)
829 info.rti_info[RTAX_BRD] =
830 rt->rt_ifa->ifa_dstaddr;
831 rtm->rtm_index = ifp->if_index;
833 info.rti_info[RTAX_IFP] = NULL;
834 info.rti_info[RTAX_IFA] = NULL;
836 } else if ((ifp = rt->rt_ifp) != NULL) {
837 rtm->rtm_index = ifp->if_index;
839 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
840 if (len > rtm->rtm_msglen) {
841 struct rt_msghdr *new_rtm;
842 R_Malloc(new_rtm, struct rt_msghdr *, len);
843 if (new_rtm == NULL) {
847 bcopy(rtm, new_rtm, rtm->rtm_msglen);
848 Free(rtm); rtm = new_rtm;
850 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
851 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
852 rtm->rtm_flags = RTF_GATEWAY |
853 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
855 rtm->rtm_flags = rt->rt_flags;
856 rt_getmetrics(rt, &rtm->rtm_rmx);
857 rtm->rtm_addrs = info.rti_addrs;
862 * New gateway could require new ifaddr, ifp;
863 * flags may also be different; ifp may be specified
864 * by ll sockaddr when protocol address is ambiguous
866 if (((rt->rt_flags & RTF_GATEWAY) &&
867 info.rti_info[RTAX_GATEWAY] != NULL) ||
868 info.rti_info[RTAX_IFP] != NULL ||
869 (info.rti_info[RTAX_IFA] != NULL &&
870 !sa_equal(info.rti_info[RTAX_IFA],
871 rt->rt_ifa->ifa_addr))) {
873 RADIX_NODE_HEAD_LOCK(rnh);
874 error = rt_getifa_fib(&info, rt->rt_fibnum);
876 * XXXRW: Really we should release this
877 * reference later, but this maintains
878 * historical behavior.
880 if (info.rti_ifa != NULL)
881 ifa_free(info.rti_ifa);
882 RADIX_NODE_HEAD_UNLOCK(rnh);
887 if (info.rti_ifa != NULL &&
888 info.rti_ifa != rt->rt_ifa &&
889 rt->rt_ifa != NULL &&
890 rt->rt_ifa->ifa_rtrequest != NULL) {
891 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
893 ifa_free(rt->rt_ifa);
895 if (info.rti_info[RTAX_GATEWAY] != NULL) {
897 RADIX_NODE_HEAD_LOCK(rnh);
900 error = rt_setgate(rt, rt_key(rt),
901 info.rti_info[RTAX_GATEWAY]);
902 RADIX_NODE_HEAD_UNLOCK(rnh);
907 rt->rt_flags &= ~RTF_GATEWAY;
908 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
910 if (info.rti_ifa != NULL &&
911 info.rti_ifa != rt->rt_ifa) {
912 ifa_ref(info.rti_ifa);
913 rt->rt_ifa = info.rti_ifa;
914 rt->rt_ifp = info.rti_ifp;
916 /* Allow some flags to be toggled on change. */
917 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
918 (rtm->rtm_flags & RTF_FMASK);
919 rt_setmetrics(rtm, rt);
920 rtm->rtm_index = rt->rt_ifp->if_index;
921 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
922 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
925 /* We don't support locks anymore */
938 rtm->rtm_errno = error;
940 rtm->rtm_flags |= RTF_DONE;
942 if (rt) /* XXX can this be true? */
945 struct rawcb *rp = NULL;
947 * Check to see if we don't want our own messages.
949 if ((so->so_options & SO_USELOOPBACK) == 0) {
950 if (route_cb.any_count <= 1) {
956 /* There is another listener, so construct message */
961 if (rti_need_deembed) {
962 /* sin6_scope_id is recovered before sending rtm. */
963 sin6 = (struct sockaddr_in6 *)&ss;
964 for (i = 0; i < RTAX_MAX; i++) {
965 if (info.rti_info[i] == NULL)
967 if (info.rti_info[i]->sa_family != AF_INET6)
969 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
970 if (sa6_recoverscope(sin6) == 0)
971 bcopy(sin6, info.rti_info[i],
976 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
977 if (m->m_pkthdr.len < rtm->rtm_msglen) {
980 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
981 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
984 M_SETFIB(m, so->so_fibnum);
985 m->m_flags |= RTS_FILTER_FIB;
988 * XXX insure we don't get a copy by
989 * invalidating our protocol
991 unsigned short family = rp->rcb_proto.sp_family;
992 rp->rcb_proto.sp_family = 0;
994 rp->rcb_proto.sp_family = family;
998 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
1007 rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt)
1010 if (rtm->rtm_inits & RTV_MTU)
1011 rt->rt_mtu = rtm->rtm_rmx.rmx_mtu;
1012 if (rtm->rtm_inits & RTV_WEIGHT)
1013 rt->rt_weight = rtm->rtm_rmx.rmx_weight;
1014 /* Kernel -> userland timebase conversion. */
1015 if (rtm->rtm_inits & RTV_EXPIRE)
1016 rt->rt_expire = rtm->rtm_rmx.rmx_expire ?
1017 rtm->rtm_rmx.rmx_expire - time_second + time_uptime : 0;
1021 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
1024 bzero(out, sizeof(*out));
1025 out->rmx_mtu = rt->rt_mtu;
1026 out->rmx_weight = rt->rt_weight;
1027 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
1028 /* Kernel -> userland timebase conversion. */
1029 out->rmx_expire = rt->rt_expire ?
1030 rt->rt_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 * This is called to generate messages from the routing socket
1331 * indicating a network interface has had addresses associated with it.
1332 * if we ever reverse the logic and replace messages TO the routing
1333 * socket indicate a request to configure interfaces, then it will
1334 * be unnecessary as the routing socket will automatically generate
1338 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1341 struct rt_addrinfo info;
1342 struct sockaddr *sa = NULL;
1344 struct mbuf *m = NULL;
1345 struct ifnet *ifp = ifa->ifa_ifp;
1347 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1348 ("unexpected cmd %u", cmd));
1349 #if defined(INET) || defined(INET6)
1352 * notify the SCTP stack
1353 * this will only get called when an address is added/deleted
1354 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1356 sctp_addr_change(ifa, cmd);
1359 if (route_cb.any_count == 0)
1361 for (pass = 1; pass < 3; pass++) {
1362 bzero((caddr_t)&info, sizeof(info));
1363 if ((cmd == RTM_ADD && pass == 1) ||
1364 (cmd == RTM_DELETE && pass == 2)) {
1365 struct ifa_msghdr *ifam;
1366 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1368 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1369 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1370 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1371 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1372 if ((m = rt_msg1(ncmd, &info)) == NULL)
1374 ifam = mtod(m, struct ifa_msghdr *);
1375 ifam->ifam_index = ifp->if_index;
1376 ifam->ifam_metric = ifa->ifa_metric;
1377 ifam->ifam_flags = ifa->ifa_flags;
1378 ifam->ifam_addrs = info.rti_addrs;
1380 if ((cmd == RTM_ADD && pass == 2) ||
1381 (cmd == RTM_DELETE && pass == 1)) {
1382 struct rt_msghdr *rtm;
1386 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1387 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1388 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1389 if ((m = rt_msg1(cmd, &info)) == NULL)
1391 rtm = mtod(m, struct rt_msghdr *);
1392 rtm->rtm_index = ifp->if_index;
1393 rtm->rtm_flags |= rt->rt_flags;
1394 rtm->rtm_errno = error;
1395 rtm->rtm_addrs = info.rti_addrs;
1397 if (fibnum != RT_ALL_FIBS) {
1398 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1399 "fibnum out of range 0 <= %d < %d", __func__,
1400 fibnum, rt_numfibs));
1401 M_SETFIB(m, fibnum);
1402 m->m_flags |= RTS_FILTER_FIB;
1404 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1409 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1412 rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
1416 * This is the analogue to the rt_newaddrmsg which performs the same
1417 * function but for multicast group memberhips. This is easier since
1418 * there is no route state to worry about.
1421 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1423 struct rt_addrinfo info;
1424 struct mbuf *m = NULL;
1425 struct ifnet *ifp = ifma->ifma_ifp;
1426 struct ifma_msghdr *ifmam;
1428 if (route_cb.any_count == 0)
1431 bzero((caddr_t)&info, sizeof(info));
1432 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1433 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1435 * If a link-layer address is present, present it as a ``gateway''
1436 * (similarly to how ARP entries, e.g., are presented).
1438 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1439 m = rt_msg1(cmd, &info);
1442 ifmam = mtod(m, struct ifma_msghdr *);
1443 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1445 ifmam->ifmam_index = ifp->if_index;
1446 ifmam->ifmam_addrs = info.rti_addrs;
1447 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1450 static struct mbuf *
1451 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1452 struct rt_addrinfo *info)
1454 struct if_announcemsghdr *ifan;
1457 if (route_cb.any_count == 0)
1459 bzero((caddr_t)info, sizeof(*info));
1460 m = rt_msg1(type, info);
1462 ifan = mtod(m, struct if_announcemsghdr *);
1463 ifan->ifan_index = ifp->if_index;
1464 strlcpy(ifan->ifan_name, ifp->if_xname,
1465 sizeof(ifan->ifan_name));
1466 ifan->ifan_what = what;
1472 * This is called to generate routing socket messages indicating
1473 * IEEE80211 wireless events.
1474 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1477 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1480 struct rt_addrinfo info;
1482 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1485 * Append the ieee80211 data. Try to stick it in the
1486 * mbuf containing the ifannounce msg; otherwise allocate
1487 * a new mbuf and append.
1489 * NB: we assume m is a single mbuf.
1491 if (data_len > M_TRAILINGSPACE(m)) {
1492 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1497 bcopy(data, mtod(n, void *), data_len);
1498 n->m_len = data_len;
1500 } else if (data_len > 0) {
1501 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1502 m->m_len += data_len;
1504 if (m->m_flags & M_PKTHDR)
1505 m->m_pkthdr.len += data_len;
1506 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1507 rt_dispatch(m, AF_UNSPEC);
1512 * This is called to generate routing socket messages indicating
1513 * network interface arrival and departure.
1516 rt_ifannouncemsg(struct ifnet *ifp, int what)
1519 struct rt_addrinfo info;
1521 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1523 rt_dispatch(m, AF_UNSPEC);
1527 rt_dispatch(struct mbuf *m, sa_family_t saf)
1532 * Preserve the family from the sockaddr, if any, in an m_tag for
1533 * use when injecting the mbuf into the routing socket buffer from
1536 if (saf != AF_UNSPEC) {
1537 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1543 *(unsigned short *)(tag + 1) = saf;
1544 m_tag_prepend(m, tag);
1548 m->m_pkthdr.rcvif = V_loif;
1554 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1558 * This is used in dumping the kernel table via sysctl().
1561 sysctl_dumpentry(struct radix_node *rn, void *vw)
1563 struct walkarg *w = vw;
1564 struct rtentry *rt = (struct rtentry *)rn;
1565 int error = 0, size;
1566 struct rt_addrinfo info;
1568 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1570 if ((rt->rt_flags & RTF_HOST) == 0
1571 ? jailed_without_vnet(w->w_req->td->td_ucred)
1572 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1574 bzero((caddr_t)&info, sizeof(info));
1575 info.rti_info[RTAX_DST] = rt_key(rt);
1576 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1577 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1578 info.rti_info[RTAX_GENMASK] = 0;
1580 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1581 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1582 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1583 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1585 size = rt_msg2(RTM_GET, &info, NULL, w);
1586 if (w->w_req && w->w_tmem) {
1587 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1589 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1590 rtm->rtm_flags = RTF_GATEWAY |
1591 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1593 rtm->rtm_flags = rt->rt_flags;
1594 rt_getmetrics(rt, &rtm->rtm_rmx);
1595 rtm->rtm_index = rt->rt_ifp->if_index;
1596 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1597 rtm->rtm_addrs = info.rti_addrs;
1598 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1604 #ifdef COMPAT_FREEBSD32
1606 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1609 bzero(dst, sizeof(*dst));
1610 CP(*src, *dst, ifi_type);
1611 CP(*src, *dst, ifi_physical);
1612 CP(*src, *dst, ifi_addrlen);
1613 CP(*src, *dst, ifi_hdrlen);
1614 CP(*src, *dst, ifi_link_state);
1615 CP(*src, *dst, ifi_vhid);
1616 CP(*src, *dst, ifi_baudrate_pf);
1617 dst->ifi_datalen = sizeof(struct if_data32);
1618 CP(*src, *dst, ifi_mtu);
1619 CP(*src, *dst, ifi_metric);
1620 CP(*src, *dst, ifi_baudrate);
1621 CP(*src, *dst, ifi_ipackets);
1622 CP(*src, *dst, ifi_ierrors);
1623 CP(*src, *dst, ifi_opackets);
1624 CP(*src, *dst, ifi_oerrors);
1625 CP(*src, *dst, ifi_collisions);
1626 CP(*src, *dst, ifi_ibytes);
1627 CP(*src, *dst, ifi_obytes);
1628 CP(*src, *dst, ifi_imcasts);
1629 CP(*src, *dst, ifi_omcasts);
1630 CP(*src, *dst, ifi_iqdrops);
1631 CP(*src, *dst, ifi_noproto);
1632 CP(*src, *dst, ifi_hwassist);
1633 CP(*src, *dst, ifi_epoch);
1634 TV_CP(*src, *dst, ifi_lastchange);
1639 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1640 struct walkarg *w, int len)
1642 struct if_msghdrl *ifm;
1644 #ifdef COMPAT_FREEBSD32
1645 if (w->w_req->flags & SCTL_MASK32) {
1646 struct if_msghdrl32 *ifm32;
1648 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1649 ifm32->ifm_addrs = info->rti_addrs;
1650 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1651 ifm32->ifm_index = ifp->if_index;
1652 ifm32->_ifm_spare1 = 0;
1653 ifm32->ifm_len = sizeof(*ifm32);
1654 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1656 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1657 /* Fixup if_data carp(4) vhid. */
1658 if (carp_get_vhid_p != NULL)
1659 ifm32->ifm_data.ifi_vhid =
1660 (*carp_get_vhid_p)(ifp->if_addr);
1661 ifm32->ifm_data.ifi_oqdrops = ifp->if_snd.ifq_drops;
1663 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1666 ifm = (struct if_msghdrl *)w->w_tmem;
1667 ifm->ifm_addrs = info->rti_addrs;
1668 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1669 ifm->ifm_index = ifp->if_index;
1670 ifm->_ifm_spare1 = 0;
1671 ifm->ifm_len = sizeof(*ifm);
1672 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1674 ifm->ifm_data = ifp->if_data;
1675 /* Fixup if_data carp(4) vhid. */
1676 if (carp_get_vhid_p != NULL)
1677 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1679 ifm->ifm_data.ifi_datalen += sizeof(u_long);
1680 ifm->ifi_oqdrops = ifp->if_snd.ifq_drops;
1682 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1686 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1687 struct walkarg *w, int len)
1689 struct if_msghdr *ifm;
1691 #ifdef COMPAT_FREEBSD32
1692 if (w->w_req->flags & SCTL_MASK32) {
1693 struct if_msghdr32 *ifm32;
1695 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1696 ifm32->ifm_addrs = info->rti_addrs;
1697 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1698 ifm32->ifm_index = ifp->if_index;
1700 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1701 /* Fixup if_data carp(4) vhid. */
1702 if (carp_get_vhid_p != NULL)
1703 ifm32->ifm_data.ifi_vhid =
1704 (*carp_get_vhid_p)(ifp->if_addr);
1706 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1709 ifm = (struct if_msghdr *)w->w_tmem;
1710 ifm->ifm_addrs = info->rti_addrs;
1711 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1712 ifm->ifm_index = ifp->if_index;
1714 ifm->ifm_data = ifp->if_data;
1715 /* Fixup if_data carp(4) vhid. */
1716 if (carp_get_vhid_p != NULL)
1717 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1719 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1723 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1724 struct walkarg *w, int len)
1726 struct ifa_msghdrl *ifam;
1728 #ifdef COMPAT_FREEBSD32
1729 if (w->w_req->flags & SCTL_MASK32) {
1730 struct ifa_msghdrl32 *ifam32;
1732 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1733 ifam32->ifam_addrs = info->rti_addrs;
1734 ifam32->ifam_flags = ifa->ifa_flags;
1735 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1736 ifam32->_ifam_spare1 = 0;
1737 ifam32->ifam_len = sizeof(*ifam32);
1738 ifam32->ifam_data_off =
1739 offsetof(struct ifa_msghdrl32, ifam_data);
1740 ifam32->ifam_metric = ifa->ifa_metric;
1742 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1743 /* Fixup if_data carp(4) vhid. */
1744 if (carp_get_vhid_p != NULL)
1745 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1747 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1751 ifam = (struct ifa_msghdrl *)w->w_tmem;
1752 ifam->ifam_addrs = info->rti_addrs;
1753 ifam->ifam_flags = ifa->ifa_flags;
1754 ifam->ifam_index = ifa->ifa_ifp->if_index;
1755 ifam->_ifam_spare1 = 0;
1756 ifam->ifam_len = sizeof(*ifam);
1757 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1758 ifam->ifam_metric = ifa->ifa_metric;
1760 ifam->ifam_data = ifa->if_data;
1761 /* Fixup if_data carp(4) vhid. */
1762 if (carp_get_vhid_p != NULL)
1763 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1765 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1769 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1770 struct walkarg *w, int len)
1772 struct ifa_msghdr *ifam;
1774 ifam = (struct ifa_msghdr *)w->w_tmem;
1775 ifam->ifam_addrs = info->rti_addrs;
1776 ifam->ifam_flags = ifa->ifa_flags;
1777 ifam->ifam_index = ifa->ifa_ifp->if_index;
1778 ifam->ifam_metric = ifa->ifa_metric;
1780 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1784 sysctl_iflist(int af, struct walkarg *w)
1788 struct rt_addrinfo info;
1791 bzero((caddr_t)&info, sizeof(info));
1792 IFNET_RLOCK_NOSLEEP();
1793 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1794 if (w->w_arg && w->w_arg != ifp->if_index)
1798 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1799 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1800 info.rti_info[RTAX_IFP] = NULL;
1801 if (w->w_req && w->w_tmem) {
1802 if (w->w_op == NET_RT_IFLISTL)
1803 error = sysctl_iflist_ifml(ifp, &info, w, len);
1805 error = sysctl_iflist_ifm(ifp, &info, w, len);
1809 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1810 if (af && af != ifa->ifa_addr->sa_family)
1812 if (prison_if(w->w_req->td->td_ucred,
1813 ifa->ifa_addr) != 0)
1815 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1816 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1817 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1818 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1819 if (w->w_req && w->w_tmem) {
1820 if (w->w_op == NET_RT_IFLISTL)
1821 error = sysctl_iflist_ifaml(ifa, &info,
1824 error = sysctl_iflist_ifam(ifa, &info,
1830 IF_ADDR_RUNLOCK(ifp);
1831 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1832 info.rti_info[RTAX_BRD] = NULL;
1836 IF_ADDR_RUNLOCK(ifp);
1837 IFNET_RUNLOCK_NOSLEEP();
1842 sysctl_ifmalist(int af, struct walkarg *w)
1845 struct ifmultiaddr *ifma;
1846 struct rt_addrinfo info;
1850 bzero((caddr_t)&info, sizeof(info));
1851 IFNET_RLOCK_NOSLEEP();
1852 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1853 if (w->w_arg && w->w_arg != ifp->if_index)
1856 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1858 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1859 if (af && af != ifma->ifma_addr->sa_family)
1861 if (prison_if(w->w_req->td->td_ucred,
1862 ifma->ifma_addr) != 0)
1864 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1865 info.rti_info[RTAX_GATEWAY] =
1866 (ifma->ifma_addr->sa_family != AF_LINK) ?
1867 ifma->ifma_lladdr : NULL;
1868 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1869 if (w->w_req && w->w_tmem) {
1870 struct ifma_msghdr *ifmam;
1872 ifmam = (struct ifma_msghdr *)w->w_tmem;
1873 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1874 ifmam->ifmam_flags = 0;
1875 ifmam->ifmam_addrs = info.rti_addrs;
1876 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1878 IF_ADDR_RUNLOCK(ifp);
1883 IF_ADDR_RUNLOCK(ifp);
1886 IFNET_RUNLOCK_NOSLEEP();
1891 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1893 int *name = (int *)arg1;
1894 u_int namelen = arg2;
1895 struct radix_node_head *rnh = NULL; /* silence compiler. */
1896 int i, lim, error = EINVAL;
1905 if (name[1] == NET_RT_DUMP) {
1907 fib = req->td->td_proc->p_fibnum;
1908 else if (namelen == 4)
1909 fib = (name[3] == RT_ALL_FIBS) ?
1910 req->td->td_proc->p_fibnum : name[3];
1912 return ((namelen < 3) ? EISDIR : ENOTDIR);
1913 if (fib < 0 || fib >= rt_numfibs)
1915 } else if (namelen != 3)
1916 return ((namelen < 3) ? EISDIR : ENOTDIR);
1920 bzero(&w, sizeof(w));
1925 error = sysctl_wire_old_buffer(req, 0);
1932 if (af == 0) { /* dump all tables */
1935 } else /* dump only one table */
1939 * take care of llinfo entries, the caller must
1942 if (w.w_op == NET_RT_FLAGS &&
1943 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1945 error = lltable_sysctl_dumparp(af, w.w_req);
1951 * take care of routing entries
1953 for (error = 0; error == 0 && i <= lim; i++) {
1954 rnh = rt_tables_get_rnh(fib, i);
1956 RADIX_NODE_HEAD_RLOCK(rnh);
1957 error = rnh->rnh_walktree(rnh,
1958 sysctl_dumpentry, &w);
1959 RADIX_NODE_HEAD_RUNLOCK(rnh);
1961 error = EAFNOSUPPORT;
1966 case NET_RT_IFLISTL:
1967 error = sysctl_iflist(af, &w);
1970 case NET_RT_IFMALIST:
1971 error = sysctl_ifmalist(af, &w);
1975 free(w.w_tmem, M_RTABLE);
1979 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1982 * Definitions of protocols supported in the ROUTE domain.
1985 static struct domain routedomain; /* or at least forward */
1987 static struct protosw routesw[] = {
1989 .pr_type = SOCK_RAW,
1990 .pr_domain = &routedomain,
1991 .pr_flags = PR_ATOMIC|PR_ADDR,
1992 .pr_output = route_output,
1993 .pr_ctlinput = raw_ctlinput,
1994 .pr_init = raw_init,
1995 .pr_usrreqs = &route_usrreqs
1999 static struct domain routedomain = {
2000 .dom_family = PF_ROUTE,
2001 .dom_name = "route",
2002 .dom_protosw = routesw,
2003 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
2006 VNET_DOMAIN_SET(route);