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 */
163 static VNET_DEFINE(route_cb_t, route_cb);
164 #define V_route_cb VNET(route_cb)
166 struct mtx rtsock_mtx;
167 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
169 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
170 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
171 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
173 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
179 struct sysctl_req *w_req;
182 static void rts_input(struct mbuf *m);
183 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
184 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
185 caddr_t cp, struct walkarg *w);
186 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
187 struct rt_addrinfo *rtinfo);
188 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
189 static int sysctl_iflist(int af, struct walkarg *w);
190 static int sysctl_ifmalist(int af, struct walkarg *w);
191 static int route_output(struct mbuf *m, struct socket *so);
192 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
193 struct rt_metrics_lite *out);
194 static void rt_getmetrics(const struct rt_metrics_lite *in,
195 struct rt_metrics *out);
196 static void rt_dispatch(struct mbuf *, sa_family_t);
198 static struct netisr_handler rtsock_nh = {
200 .nh_handler = rts_input,
201 .nh_proto = NETISR_ROUTE,
202 .nh_policy = NETISR_POLICY_SOURCE,
206 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
210 netisr_getqlimit(&rtsock_nh, &qlimit);
211 error = sysctl_handle_int(oidp, &qlimit, 0, req);
212 if (error || !req->newptr)
216 return (netisr_setqlimit(&rtsock_nh, qlimit));
218 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
219 0, 0, sysctl_route_netisr_maxqlen, "I",
220 "maximum routing socket dispatch queue length");
227 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
228 rtsock_nh.nh_qlimit = tmp;
229 netisr_register(&rtsock_nh);
231 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
234 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
239 KASSERT(m != NULL, ("%s: m is NULL", __func__));
240 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
241 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
243 /* No filtering requested. */
244 if ((m->m_flags & RTS_FILTER_FIB) == 0)
247 /* Check if it is a rts and the fib matches the one of the socket. */
248 fibnum = M_GETFIB(m);
249 if (proto->sp_family != PF_ROUTE ||
250 rp->rcb_socket == NULL ||
251 rp->rcb_socket->so_fibnum == fibnum)
254 /* Filtering requested and no match, the socket shall be skipped. */
259 rts_input(struct mbuf *m)
261 struct sockproto route_proto;
262 unsigned short *family;
265 route_proto.sp_family = PF_ROUTE;
266 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
268 family = (unsigned short *)(tag + 1);
269 route_proto.sp_protocol = *family;
270 m_tag_delete(m, tag);
272 route_proto.sp_protocol = 0;
274 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
278 * It really doesn't make any sense at all for this code to share much
279 * with raw_usrreq.c, since its functionality is so restricted. XXX
282 rts_abort(struct socket *so)
285 raw_usrreqs.pru_abort(so);
289 rts_close(struct socket *so)
292 raw_usrreqs.pru_close(so);
295 /* pru_accept is EOPNOTSUPP */
298 rts_attach(struct socket *so, int proto, struct thread *td)
303 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
306 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
310 so->so_pcb = (caddr_t)rp;
311 so->so_fibnum = td->td_proc->p_fibnum;
312 error = raw_attach(so, proto);
320 switch(rp->rcb_proto.sp_protocol) {
322 V_route_cb.ip_count++;
325 V_route_cb.ip6_count++;
328 V_route_cb.ipx_count++;
331 V_route_cb.any_count++;
334 so->so_options |= SO_USELOOPBACK;
339 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
342 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
346 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
349 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
352 /* pru_connect2 is EOPNOTSUPP */
353 /* pru_control is EOPNOTSUPP */
356 rts_detach(struct socket *so)
358 struct rawcb *rp = sotorawcb(so);
360 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
363 switch(rp->rcb_proto.sp_protocol) {
365 V_route_cb.ip_count--;
368 V_route_cb.ip6_count--;
371 V_route_cb.ipx_count--;
374 V_route_cb.any_count--;
376 raw_usrreqs.pru_detach(so);
380 rts_disconnect(struct socket *so)
383 return (raw_usrreqs.pru_disconnect(so));
386 /* pru_listen is EOPNOTSUPP */
389 rts_peeraddr(struct socket *so, struct sockaddr **nam)
392 return (raw_usrreqs.pru_peeraddr(so, nam));
395 /* pru_rcvd is EOPNOTSUPP */
396 /* pru_rcvoob is EOPNOTSUPP */
399 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
400 struct mbuf *control, struct thread *td)
403 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
406 /* pru_sense is null */
409 rts_shutdown(struct socket *so)
412 return (raw_usrreqs.pru_shutdown(so));
416 rts_sockaddr(struct socket *so, struct sockaddr **nam)
419 return (raw_usrreqs.pru_sockaddr(so, nam));
422 static struct pr_usrreqs route_usrreqs = {
423 .pru_abort = rts_abort,
424 .pru_attach = rts_attach,
425 .pru_bind = rts_bind,
426 .pru_connect = rts_connect,
427 .pru_detach = rts_detach,
428 .pru_disconnect = rts_disconnect,
429 .pru_peeraddr = rts_peeraddr,
430 .pru_send = rts_send,
431 .pru_shutdown = rts_shutdown,
432 .pru_sockaddr = rts_sockaddr,
433 .pru_close = rts_close,
436 #ifndef _SOCKADDR_UNION_DEFINED
437 #define _SOCKADDR_UNION_DEFINED
439 * The union of all possible address formats we handle.
441 union sockaddr_union {
443 struct sockaddr_in sin;
444 struct sockaddr_in6 sin6;
446 #endif /* _SOCKADDR_UNION_DEFINED */
449 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
450 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
453 /* First, see if the returned address is part of the jail. */
454 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
455 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
459 switch (info->rti_info[RTAX_DST]->sa_family) {
469 * Try to find an address on the given outgoing interface
470 * that belongs to the jail.
473 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
476 if (sa->sa_family != AF_INET)
478 ia = ((struct sockaddr_in *)sa)->sin_addr;
479 if (prison_check_ip4(cred, &ia) == 0) {
484 IF_ADDR_RUNLOCK(ifp);
487 * As a last resort return the 'default' jail address.
489 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
491 if (prison_get_ip4(cred, &ia) != 0)
494 bzero(&saun->sin, sizeof(struct sockaddr_in));
495 saun->sin.sin_len = sizeof(struct sockaddr_in);
496 saun->sin.sin_family = AF_INET;
497 saun->sin.sin_addr.s_addr = ia.s_addr;
498 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
511 * Try to find an address on the given outgoing interface
512 * that belongs to the jail.
515 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
518 if (sa->sa_family != AF_INET6)
520 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
521 &ia6, sizeof(struct in6_addr));
522 if (prison_check_ip6(cred, &ia6) == 0) {
527 IF_ADDR_RUNLOCK(ifp);
530 * As a last resort return the 'default' jail address.
532 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
534 if (prison_get_ip6(cred, &ia6) != 0)
537 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
538 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
539 saun->sin6.sin6_family = AF_INET6;
540 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
541 if (sa6_recoverscope(&saun->sin6) != 0)
543 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
555 route_output(struct mbuf *m, struct socket *so)
557 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
558 struct rt_msghdr *rtm = NULL;
559 struct rtentry *rt = NULL;
560 struct radix_node_head *rnh;
561 struct rt_addrinfo info;
563 struct sockaddr_storage ss;
564 struct sockaddr_in6 *sin6;
565 int i, rti_need_deembed = 0;
568 struct ifnet *ifp = NULL;
569 union sockaddr_union saun;
570 sa_family_t saf = AF_UNSPEC;
572 #define senderr(e) { error = e; goto flush;}
573 if (m == NULL || ((m->m_len < sizeof(long)) &&
574 (m = m_pullup(m, sizeof(long))) == NULL))
576 if ((m->m_flags & M_PKTHDR) == 0)
577 panic("route_output");
578 len = m->m_pkthdr.len;
579 if (len < sizeof(*rtm) ||
580 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
581 info.rti_info[RTAX_DST] = NULL;
584 R_Malloc(rtm, struct rt_msghdr *, len);
586 info.rti_info[RTAX_DST] = NULL;
589 m_copydata(m, 0, len, (caddr_t)rtm);
590 if (rtm->rtm_version != RTM_VERSION) {
591 info.rti_info[RTAX_DST] = NULL;
592 senderr(EPROTONOSUPPORT);
594 rtm->rtm_pid = curproc->p_pid;
595 bzero(&info, sizeof(info));
596 info.rti_addrs = rtm->rtm_addrs;
598 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
599 * link-local address because rtrequest requires addresses with
602 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
603 info.rti_info[RTAX_DST] = NULL;
606 info.rti_flags = rtm->rtm_flags;
607 if (info.rti_info[RTAX_DST] == NULL ||
608 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
609 (info.rti_info[RTAX_GATEWAY] != NULL &&
610 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
612 saf = info.rti_info[RTAX_DST]->sa_family;
614 * Verify that the caller has the appropriate privilege; RTM_GET
615 * is the only operation the non-superuser is allowed.
617 if (rtm->rtm_type != RTM_GET) {
618 error = priv_check(curthread, PRIV_NET_ROUTE);
624 * The given gateway address may be an interface address.
625 * For example, issuing a "route change" command on a route
626 * entry that was created from a tunnel, and the gateway
627 * address given is the local end point. In this case the
628 * RTF_GATEWAY flag must be cleared or the destination will
629 * not be reachable even though there is no error message.
631 if (info.rti_info[RTAX_GATEWAY] != NULL &&
632 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
635 bzero(&gw_ro, sizeof(gw_ro));
636 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
637 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
639 * A host route through the loopback interface is
640 * installed for each interface adddress. In pre 8.0
641 * releases the interface address of a PPP link type
642 * is not reachable locally. This behavior is fixed as
643 * part of the new L2/L3 redesign and rewrite work. The
644 * signature of this interface address route is the
645 * AF_LINK sa_family type of the rt_gateway, and the
646 * rt_ifp has the IFF_LOOPBACK flag set.
648 if (gw_ro.ro_rt != NULL &&
649 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
650 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
651 info.rti_flags &= ~RTF_GATEWAY;
652 info.rti_flags |= RTF_GWFLAG_COMPAT;
654 if (gw_ro.ro_rt != NULL)
658 switch (rtm->rtm_type) {
659 struct rtentry *saved_nrt;
662 if (info.rti_info[RTAX_GATEWAY] == NULL)
666 /* support for new ARP code */
667 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
668 (rtm->rtm_flags & RTF_LLDATA) != 0) {
669 error = lla_rt_output(rtm, &info);
672 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
676 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
678 if (error == 0 && saved_nrt) {
680 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
683 rt_setmetrics(rtm->rtm_inits,
684 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
685 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
686 RT_REMREF(saved_nrt);
687 RT_UNLOCK(saved_nrt);
693 /* support for new ARP code */
694 if (info.rti_info[RTAX_GATEWAY] &&
695 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
696 (rtm->rtm_flags & RTF_LLDATA) != 0) {
697 error = lla_rt_output(rtm, &info);
700 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
704 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
712 /* rt_msg2() will not be used when RTM_DELETE fails. */
713 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
720 rnh = rt_tables_get_rnh(so->so_fibnum,
721 info.rti_info[RTAX_DST]->sa_family);
723 senderr(EAFNOSUPPORT);
725 RADIX_NODE_HEAD_RLOCK(rnh);
727 if (info.rti_info[RTAX_NETMASK] == NULL &&
728 rtm->rtm_type == RTM_GET) {
730 * Provide logest prefix match for
731 * address lookup (no mask).
732 * 'route -n get addr'
734 rt = (struct rtentry *) rnh->rnh_matchaddr(
735 info.rti_info[RTAX_DST], rnh);
737 rt = (struct rtentry *) rnh->rnh_lookup(
738 info.rti_info[RTAX_DST],
739 info.rti_info[RTAX_NETMASK], rnh);
742 RADIX_NODE_HEAD_RUNLOCK(rnh);
747 * for RTM_CHANGE/LOCK, if we got multipath routes,
748 * we require users to specify a matching RTAX_GATEWAY.
750 * for RTM_GET, gate is optional even with multipath.
751 * if gate == NULL the first match is returned.
752 * (no need to call rt_mpath_matchgate if gate == NULL)
754 if (rn_mpath_capable(rnh) &&
755 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
756 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
758 RADIX_NODE_HEAD_RUNLOCK(rnh);
764 * If performing proxied L2 entry insertion, and
765 * the actual PPP host entry is found, perform
766 * another search to retrieve the prefix route of
767 * the local end point of the PPP link.
769 if (rtm->rtm_flags & RTF_ANNOUNCE) {
770 struct sockaddr laddr;
772 if (rt->rt_ifp != NULL &&
773 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
776 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
778 rt_maskedcopy(ifa->ifa_addr,
782 rt_maskedcopy(rt->rt_ifa->ifa_addr,
784 rt->rt_ifa->ifa_netmask);
786 * refactor rt and no lock operation necessary
788 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
790 RADIX_NODE_HEAD_RUNLOCK(rnh);
796 RADIX_NODE_HEAD_RUNLOCK(rnh);
798 switch(rtm->rtm_type) {
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] = rt_mask(rt);
813 info.rti_info[RTAX_GENMASK] = 0;
814 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
817 info.rti_info[RTAX_IFP] =
818 ifp->if_addr->ifa_addr;
819 error = rtm_get_jailed(&info, ifp, rt,
820 &saun, curthread->td_ucred);
825 if (ifp->if_flags & IFF_POINTOPOINT)
826 info.rti_info[RTAX_BRD] =
827 rt->rt_ifa->ifa_dstaddr;
828 rtm->rtm_index = ifp->if_index;
830 info.rti_info[RTAX_IFP] = NULL;
831 info.rti_info[RTAX_IFA] = NULL;
833 } else if ((ifp = rt->rt_ifp) != NULL) {
834 rtm->rtm_index = ifp->if_index;
836 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
837 if (len > rtm->rtm_msglen) {
838 struct rt_msghdr *new_rtm;
839 R_Malloc(new_rtm, struct rt_msghdr *, len);
840 if (new_rtm == NULL) {
844 bcopy(rtm, new_rtm, rtm->rtm_msglen);
845 Free(rtm); rtm = new_rtm;
847 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
848 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
849 rtm->rtm_flags = RTF_GATEWAY |
850 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
852 rtm->rtm_flags = rt->rt_flags;
853 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
854 rtm->rtm_addrs = info.rti_addrs;
859 * New gateway could require new ifaddr, ifp;
860 * flags may also be different; ifp may be specified
861 * by ll sockaddr when protocol address is ambiguous
863 if (((rt->rt_flags & RTF_GATEWAY) &&
864 info.rti_info[RTAX_GATEWAY] != NULL) ||
865 info.rti_info[RTAX_IFP] != NULL ||
866 (info.rti_info[RTAX_IFA] != NULL &&
867 !sa_equal(info.rti_info[RTAX_IFA],
868 rt->rt_ifa->ifa_addr))) {
870 RADIX_NODE_HEAD_LOCK(rnh);
871 error = rt_getifa_fib(&info, rt->rt_fibnum);
873 * XXXRW: Really we should release this
874 * reference later, but this maintains
875 * historical behavior.
877 if (info.rti_ifa != NULL)
878 ifa_free(info.rti_ifa);
879 RADIX_NODE_HEAD_UNLOCK(rnh);
884 if (info.rti_ifa != NULL &&
885 info.rti_ifa != rt->rt_ifa &&
886 rt->rt_ifa != NULL &&
887 rt->rt_ifa->ifa_rtrequest != NULL) {
888 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
890 ifa_free(rt->rt_ifa);
892 if (info.rti_info[RTAX_GATEWAY] != NULL) {
894 RADIX_NODE_HEAD_LOCK(rnh);
897 error = rt_setgate(rt, rt_key(rt),
898 info.rti_info[RTAX_GATEWAY]);
899 RADIX_NODE_HEAD_UNLOCK(rnh);
904 rt->rt_flags &= ~RTF_GATEWAY;
905 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
907 if (info.rti_ifa != NULL &&
908 info.rti_ifa != rt->rt_ifa) {
909 ifa_ref(info.rti_ifa);
910 rt->rt_ifa = info.rti_ifa;
911 rt->rt_ifp = info.rti_ifp;
913 /* Allow some flags to be toggled on change. */
914 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
915 (rtm->rtm_flags & RTF_FMASK);
916 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
918 rtm->rtm_index = rt->rt_ifp->if_index;
919 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
920 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
923 /* We don't support locks anymore */
936 rtm->rtm_errno = error;
938 rtm->rtm_flags |= RTF_DONE;
940 if (rt) /* XXX can this be true? */
943 struct rawcb *rp = NULL;
945 * Check to see if we don't want our own messages.
947 if ((so->so_options & SO_USELOOPBACK) == 0) {
948 if (V_route_cb.any_count <= 1) {
954 /* There is another listener, so construct message */
959 if (rti_need_deembed) {
960 /* sin6_scope_id is recovered before sending rtm. */
961 sin6 = (struct sockaddr_in6 *)&ss;
962 for (i = 0; i < RTAX_MAX; i++) {
963 if (info.rti_info[i] == NULL)
965 if (info.rti_info[i]->sa_family != AF_INET6)
967 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
968 if (sa6_recoverscope(sin6) == 0)
969 bcopy(sin6, info.rti_info[i],
974 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
975 if (m->m_pkthdr.len < rtm->rtm_msglen) {
978 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
979 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
982 M_SETFIB(m, so->so_fibnum);
983 m->m_flags |= RTS_FILTER_FIB;
986 * XXX insure we don't get a copy by
987 * invalidating our protocol
989 unsigned short family = rp->rcb_proto.sp_family;
990 rp->rcb_proto.sp_family = 0;
992 rp->rcb_proto.sp_family = family;
996 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
1005 rt_setmetrics(u_long which, const struct rt_metrics *in,
1006 struct rt_metrics_lite *out)
1008 #define metric(f, e) if (which & (f)) out->e = in->e;
1010 * Only these are stored in the routing entry since introduction
1011 * of tcp hostcache. The rest is ignored.
1013 metric(RTV_MTU, rmx_mtu);
1014 metric(RTV_WEIGHT, rmx_weight);
1015 /* Userland -> kernel timebase conversion. */
1016 if (which & RTV_EXPIRE)
1017 out->rmx_expire = in->rmx_expire ?
1018 in->rmx_expire - time_second + time_uptime : 0;
1023 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
1025 #define metric(e) out->e = in->e;
1026 bzero(out, sizeof(*out));
1029 /* Kernel -> userland timebase conversion. */
1030 out->rmx_expire = in->rmx_expire ?
1031 in->rmx_expire - time_uptime + time_second : 0;
1036 * Extract the addresses of the passed sockaddrs.
1037 * Do a little sanity checking so as to avoid bad memory references.
1038 * This data is derived straight from userland.
1041 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1043 struct sockaddr *sa;
1046 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1047 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1049 sa = (struct sockaddr *)cp;
1053 if (cp + sa->sa_len > cplim)
1056 * there are no more.. quit now
1057 * If there are more bits, they are in error.
1058 * I've seen this. route(1) can evidently generate these.
1059 * This causes kernel to core dump.
1060 * for compatibility, If we see this, point to a safe address.
1062 if (sa->sa_len == 0) {
1063 rtinfo->rti_info[i] = &sa_zero;
1064 return (0); /* should be EINVAL but for compat */
1068 if (sa->sa_family == AF_INET6)
1069 sa6_embedscope((struct sockaddr_in6 *)sa,
1072 rtinfo->rti_info[i] = sa;
1079 * Used by the routing socket.
1081 static struct mbuf *
1082 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1084 struct rt_msghdr *rtm;
1087 struct sockaddr *sa;
1089 struct sockaddr_storage ss;
1090 struct sockaddr_in6 *sin6;
1098 len = sizeof(struct ifa_msghdr);
1103 len = sizeof(struct ifma_msghdr);
1107 len = sizeof(struct if_msghdr);
1110 case RTM_IFANNOUNCE:
1112 len = sizeof(struct if_announcemsghdr);
1116 len = sizeof(struct rt_msghdr);
1119 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1120 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1122 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1124 m = m_gethdr(M_NOWAIT, MT_DATA);
1128 m->m_pkthdr.len = m->m_len = len;
1129 rtm = mtod(m, struct rt_msghdr *);
1130 bzero((caddr_t)rtm, len);
1131 for (i = 0; i < RTAX_MAX; i++) {
1132 if ((sa = rtinfo->rti_info[i]) == NULL)
1134 rtinfo->rti_addrs |= (1 << i);
1137 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1138 sin6 = (struct sockaddr_in6 *)&ss;
1139 bcopy(sa, sin6, sizeof(*sin6));
1140 if (sa6_recoverscope(sin6) == 0)
1141 sa = (struct sockaddr *)sin6;
1144 m_copyback(m, len, dlen, (caddr_t)sa);
1147 if (m->m_pkthdr.len != len) {
1151 rtm->rtm_msglen = len;
1152 rtm->rtm_version = RTM_VERSION;
1153 rtm->rtm_type = type;
1158 * Used by the sysctl code and routing socket.
1161 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1164 int len, dlen, second_time = 0;
1167 struct sockaddr_storage ss;
1168 struct sockaddr_in6 *sin6;
1171 rtinfo->rti_addrs = 0;
1177 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1178 #ifdef COMPAT_FREEBSD32
1179 if (w->w_req->flags & SCTL_MASK32)
1180 len = sizeof(struct ifa_msghdrl32);
1183 len = sizeof(struct ifa_msghdrl);
1185 len = sizeof(struct ifa_msghdr);
1189 #ifdef COMPAT_FREEBSD32
1190 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1191 if (w->w_op == NET_RT_IFLISTL)
1192 len = sizeof(struct if_msghdrl32);
1194 len = sizeof(struct if_msghdr32);
1198 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1199 len = sizeof(struct if_msghdrl);
1201 len = sizeof(struct if_msghdr);
1205 len = sizeof(struct ifma_msghdr);
1209 len = sizeof(struct rt_msghdr);
1214 for (i = 0; i < RTAX_MAX; i++) {
1215 struct sockaddr *sa;
1217 if ((sa = rtinfo->rti_info[i]) == NULL)
1219 rtinfo->rti_addrs |= (1 << i);
1223 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1224 sin6 = (struct sockaddr_in6 *)&ss;
1225 bcopy(sa, sin6, sizeof(*sin6));
1226 if (sa6_recoverscope(sin6) == 0)
1227 sa = (struct sockaddr *)sin6;
1230 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1236 if (cp == NULL && w != NULL && !second_time) {
1237 struct walkarg *rw = w;
1240 if (rw->w_tmemsize < len) {
1242 free(rw->w_tmem, M_RTABLE);
1243 rw->w_tmem = (caddr_t)
1244 malloc(len, M_RTABLE, M_NOWAIT);
1246 rw->w_tmemsize = len;
1256 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1258 rtm->rtm_version = RTM_VERSION;
1259 rtm->rtm_type = type;
1260 rtm->rtm_msglen = len;
1266 * This routine is called to generate a message from the routing
1267 * socket indicating that a redirect has occured, a routing lookup
1268 * has failed, or that a protocol has detected timeouts to a particular
1272 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1275 struct rt_msghdr *rtm;
1277 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1279 if (V_route_cb.any_count == 0)
1281 m = rt_msg1(type, rtinfo);
1285 if (fibnum != RT_ALL_FIBS) {
1286 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1287 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1288 M_SETFIB(m, fibnum);
1289 m->m_flags |= RTS_FILTER_FIB;
1292 rtm = mtod(m, struct rt_msghdr *);
1293 rtm->rtm_flags = RTF_DONE | flags;
1294 rtm->rtm_errno = error;
1295 rtm->rtm_addrs = rtinfo->rti_addrs;
1296 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1300 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1303 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1307 * This routine is called to generate a message from the routing
1308 * socket indicating that the status of a network interface has changed.
1311 rt_ifmsg(struct ifnet *ifp)
1313 struct if_msghdr *ifm;
1315 struct rt_addrinfo info;
1317 if (V_route_cb.any_count == 0)
1319 bzero((caddr_t)&info, sizeof(info));
1320 m = rt_msg1(RTM_IFINFO, &info);
1323 ifm = mtod(m, struct if_msghdr *);
1324 ifm->ifm_index = ifp->if_index;
1325 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1326 ifm->ifm_data = ifp->if_data;
1328 rt_dispatch(m, AF_UNSPEC);
1332 * Announce interface address arrival/withdraw.
1333 * Please do not call directly, use rt_addrmsg().
1334 * Assume input data to be valid.
1335 * Returns 0 on success.
1338 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1340 struct rt_addrinfo info;
1341 struct sockaddr *sa;
1344 struct ifa_msghdr *ifam;
1345 struct ifnet *ifp = ifa->ifa_ifp;
1347 if (V_route_cb.any_count == 0)
1350 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1352 bzero((caddr_t)&info, sizeof(info));
1353 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1354 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1355 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1356 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1357 if ((m = rt_msg1(ncmd, &info)) == NULL)
1359 ifam = mtod(m, struct ifa_msghdr *);
1360 ifam->ifam_index = ifp->if_index;
1361 ifam->ifam_metric = ifa->ifa_metric;
1362 ifam->ifam_flags = ifa->ifa_flags;
1363 ifam->ifam_addrs = info.rti_addrs;
1365 if (fibnum != RT_ALL_FIBS) {
1366 M_SETFIB(m, fibnum);
1367 m->m_flags |= RTS_FILTER_FIB;
1370 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1376 * Announce route addition/removal.
1377 * Please do not call directly, use rt_routemsg().
1378 * Note that @rt data MAY be inconsistent/invalid:
1379 * if some userland app sends us "invalid" route message (invalid mask,
1380 * no dst, wrong address families, etc...) we need to pass it back
1381 * to app (and any other rtsock consumers) with rtm_errno field set to
1384 * Returns 0 on success.
1387 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1390 struct rt_addrinfo info;
1391 struct sockaddr *sa;
1393 struct rt_msghdr *rtm;
1395 if (V_route_cb.any_count == 0)
1398 bzero((caddr_t)&info, sizeof(info));
1399 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1400 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1401 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1402 if ((m = rt_msg1(cmd, &info)) == NULL)
1404 rtm = mtod(m, struct rt_msghdr *);
1405 rtm->rtm_index = ifp->if_index;
1406 rtm->rtm_flags |= rt->rt_flags;
1407 rtm->rtm_errno = error;
1408 rtm->rtm_addrs = info.rti_addrs;
1410 if (fibnum != RT_ALL_FIBS) {
1411 M_SETFIB(m, fibnum);
1412 m->m_flags |= RTS_FILTER_FIB;
1415 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1421 * This is the analogue to the rt_newaddrmsg which performs the same
1422 * function but for multicast group memberhips. This is easier since
1423 * there is no route state to worry about.
1426 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1428 struct rt_addrinfo info;
1429 struct mbuf *m = NULL;
1430 struct ifnet *ifp = ifma->ifma_ifp;
1431 struct ifma_msghdr *ifmam;
1433 if (V_route_cb.any_count == 0)
1436 bzero((caddr_t)&info, sizeof(info));
1437 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1438 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1440 * If a link-layer address is present, present it as a ``gateway''
1441 * (similarly to how ARP entries, e.g., are presented).
1443 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1444 m = rt_msg1(cmd, &info);
1447 ifmam = mtod(m, struct ifma_msghdr *);
1448 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1450 ifmam->ifmam_index = ifp->if_index;
1451 ifmam->ifmam_addrs = info.rti_addrs;
1452 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1455 static struct mbuf *
1456 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1457 struct rt_addrinfo *info)
1459 struct if_announcemsghdr *ifan;
1462 if (V_route_cb.any_count == 0)
1464 bzero((caddr_t)info, sizeof(*info));
1465 m = rt_msg1(type, info);
1467 ifan = mtod(m, struct if_announcemsghdr *);
1468 ifan->ifan_index = ifp->if_index;
1469 strlcpy(ifan->ifan_name, ifp->if_xname,
1470 sizeof(ifan->ifan_name));
1471 ifan->ifan_what = what;
1477 * This is called to generate routing socket messages indicating
1478 * IEEE80211 wireless events.
1479 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1482 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1485 struct rt_addrinfo info;
1487 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1490 * Append the ieee80211 data. Try to stick it in the
1491 * mbuf containing the ifannounce msg; otherwise allocate
1492 * a new mbuf and append.
1494 * NB: we assume m is a single mbuf.
1496 if (data_len > M_TRAILINGSPACE(m)) {
1497 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1502 bcopy(data, mtod(n, void *), data_len);
1503 n->m_len = data_len;
1505 } else if (data_len > 0) {
1506 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1507 m->m_len += data_len;
1509 if (m->m_flags & M_PKTHDR)
1510 m->m_pkthdr.len += data_len;
1511 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1512 rt_dispatch(m, AF_UNSPEC);
1517 * This is called to generate routing socket messages indicating
1518 * network interface arrival and departure.
1521 rt_ifannouncemsg(struct ifnet *ifp, int what)
1524 struct rt_addrinfo info;
1526 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1528 rt_dispatch(m, AF_UNSPEC);
1532 rt_dispatch(struct mbuf *m, sa_family_t saf)
1537 * Preserve the family from the sockaddr, if any, in an m_tag for
1538 * use when injecting the mbuf into the routing socket buffer from
1541 if (saf != AF_UNSPEC) {
1542 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1548 *(unsigned short *)(tag + 1) = saf;
1549 m_tag_prepend(m, tag);
1553 m->m_pkthdr.rcvif = V_loif;
1559 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1563 * This is used in dumping the kernel table via sysctl().
1566 sysctl_dumpentry(struct radix_node *rn, void *vw)
1568 struct walkarg *w = vw;
1569 struct rtentry *rt = (struct rtentry *)rn;
1570 int error = 0, size;
1571 struct rt_addrinfo info;
1573 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1575 if ((rt->rt_flags & RTF_HOST) == 0
1576 ? jailed_without_vnet(w->w_req->td->td_ucred)
1577 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1579 bzero((caddr_t)&info, sizeof(info));
1580 info.rti_info[RTAX_DST] = rt_key(rt);
1581 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1582 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1583 info.rti_info[RTAX_GENMASK] = 0;
1585 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1586 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1587 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1588 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1590 size = rt_msg2(RTM_GET, &info, NULL, w);
1591 if (w->w_req && w->w_tmem) {
1592 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1594 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1595 rtm->rtm_flags = RTF_GATEWAY |
1596 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1598 rtm->rtm_flags = rt->rt_flags;
1600 * let's be honest about this being a retarded hack
1602 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1603 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1604 rtm->rtm_index = rt->rt_ifp->if_index;
1605 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1606 rtm->rtm_addrs = info.rti_addrs;
1607 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1613 #ifdef COMPAT_FREEBSD32
1615 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1618 bzero(dst, sizeof(*dst));
1619 CP(*src, *dst, ifi_type);
1620 CP(*src, *dst, ifi_physical);
1621 CP(*src, *dst, ifi_addrlen);
1622 CP(*src, *dst, ifi_hdrlen);
1623 CP(*src, *dst, ifi_link_state);
1624 CP(*src, *dst, ifi_vhid);
1625 CP(*src, *dst, ifi_baudrate_pf);
1626 dst->ifi_datalen = sizeof(struct if_data32);
1627 CP(*src, *dst, ifi_mtu);
1628 CP(*src, *dst, ifi_metric);
1629 CP(*src, *dst, ifi_baudrate);
1630 CP(*src, *dst, ifi_ipackets);
1631 CP(*src, *dst, ifi_ierrors);
1632 CP(*src, *dst, ifi_opackets);
1633 CP(*src, *dst, ifi_oerrors);
1634 CP(*src, *dst, ifi_collisions);
1635 CP(*src, *dst, ifi_ibytes);
1636 CP(*src, *dst, ifi_obytes);
1637 CP(*src, *dst, ifi_imcasts);
1638 CP(*src, *dst, ifi_omcasts);
1639 CP(*src, *dst, ifi_iqdrops);
1640 CP(*src, *dst, ifi_noproto);
1641 CP(*src, *dst, ifi_hwassist);
1642 CP(*src, *dst, ifi_epoch);
1643 TV_CP(*src, *dst, ifi_lastchange);
1648 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1649 struct walkarg *w, int len)
1651 struct if_msghdrl *ifm;
1653 #ifdef COMPAT_FREEBSD32
1654 if (w->w_req->flags & SCTL_MASK32) {
1655 struct if_msghdrl32 *ifm32;
1657 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1658 ifm32->ifm_addrs = info->rti_addrs;
1659 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1660 ifm32->ifm_index = ifp->if_index;
1661 ifm32->_ifm_spare1 = 0;
1662 ifm32->ifm_len = sizeof(*ifm32);
1663 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1665 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1666 /* Fixup if_data carp(4) vhid. */
1667 if (carp_get_vhid_p != NULL)
1668 ifm32->ifm_data.ifi_vhid =
1669 (*carp_get_vhid_p)(ifp->if_addr);
1671 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1674 ifm = (struct if_msghdrl *)w->w_tmem;
1675 ifm->ifm_addrs = info->rti_addrs;
1676 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1677 ifm->ifm_index = ifp->if_index;
1678 ifm->_ifm_spare1 = 0;
1679 ifm->ifm_len = sizeof(*ifm);
1680 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1682 ifm->ifm_data = ifp->if_data;
1683 /* Fixup if_data carp(4) vhid. */
1684 if (carp_get_vhid_p != NULL)
1685 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1687 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1691 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1692 struct walkarg *w, int len)
1694 struct if_msghdr *ifm;
1696 #ifdef COMPAT_FREEBSD32
1697 if (w->w_req->flags & SCTL_MASK32) {
1698 struct if_msghdr32 *ifm32;
1700 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1701 ifm32->ifm_addrs = info->rti_addrs;
1702 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1703 ifm32->ifm_index = ifp->if_index;
1705 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1706 /* Fixup if_data carp(4) vhid. */
1707 if (carp_get_vhid_p != NULL)
1708 ifm32->ifm_data.ifi_vhid =
1709 (*carp_get_vhid_p)(ifp->if_addr);
1711 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1714 ifm = (struct if_msghdr *)w->w_tmem;
1715 ifm->ifm_addrs = info->rti_addrs;
1716 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1717 ifm->ifm_index = ifp->if_index;
1719 ifm->ifm_data = ifp->if_data;
1720 /* Fixup if_data carp(4) vhid. */
1721 if (carp_get_vhid_p != NULL)
1722 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1724 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1728 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1729 struct walkarg *w, int len)
1731 struct ifa_msghdrl *ifam;
1733 #ifdef COMPAT_FREEBSD32
1734 if (w->w_req->flags & SCTL_MASK32) {
1735 struct ifa_msghdrl32 *ifam32;
1737 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1738 ifam32->ifam_addrs = info->rti_addrs;
1739 ifam32->ifam_flags = ifa->ifa_flags;
1740 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1741 ifam32->_ifam_spare1 = 0;
1742 ifam32->ifam_len = sizeof(*ifam32);
1743 ifam32->ifam_data_off =
1744 offsetof(struct ifa_msghdrl32, ifam_data);
1745 ifam32->ifam_metric = ifa->ifa_metric;
1747 bzero(&ifam32->ifam_data, sizeof(ifam32->ifam_data));
1748 ifam32->ifam_data.ifi_datalen = sizeof(struct if_data32);
1749 ifam32->ifam_data.ifi_ipackets =
1750 counter_u64_fetch(ifa->ifa_ipackets);
1751 ifam32->ifam_data.ifi_opackets =
1752 counter_u64_fetch(ifa->ifa_opackets);
1753 ifam32->ifam_data.ifi_ibytes =
1754 counter_u64_fetch(ifa->ifa_ibytes);
1755 ifam32->ifam_data.ifi_obytes =
1756 counter_u64_fetch(ifa->ifa_obytes);
1758 /* Fixup if_data carp(4) vhid. */
1759 if (carp_get_vhid_p != NULL)
1760 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1762 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1766 ifam = (struct ifa_msghdrl *)w->w_tmem;
1767 ifam->ifam_addrs = info->rti_addrs;
1768 ifam->ifam_flags = ifa->ifa_flags;
1769 ifam->ifam_index = ifa->ifa_ifp->if_index;
1770 ifam->_ifam_spare1 = 0;
1771 ifam->ifam_len = sizeof(*ifam);
1772 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1773 ifam->ifam_metric = ifa->ifa_metric;
1775 bzero(&ifam->ifam_data, sizeof(ifam->ifam_data));
1776 ifam->ifam_data.ifi_datalen = sizeof(struct if_data);
1777 ifam->ifam_data.ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1778 ifam->ifam_data.ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1779 ifam->ifam_data.ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1780 ifam->ifam_data.ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1782 /* Fixup if_data carp(4) vhid. */
1783 if (carp_get_vhid_p != NULL)
1784 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1786 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1790 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1791 struct walkarg *w, int len)
1793 struct ifa_msghdr *ifam;
1795 ifam = (struct ifa_msghdr *)w->w_tmem;
1796 ifam->ifam_addrs = info->rti_addrs;
1797 ifam->ifam_flags = ifa->ifa_flags;
1798 ifam->ifam_index = ifa->ifa_ifp->if_index;
1799 ifam->ifam_metric = ifa->ifa_metric;
1801 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1805 sysctl_iflist(int af, struct walkarg *w)
1809 struct rt_addrinfo info;
1812 bzero((caddr_t)&info, sizeof(info));
1813 IFNET_RLOCK_NOSLEEP();
1814 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1815 if (w->w_arg && w->w_arg != ifp->if_index)
1819 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1820 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1821 info.rti_info[RTAX_IFP] = NULL;
1822 if (w->w_req && w->w_tmem) {
1823 if (w->w_op == NET_RT_IFLISTL)
1824 error = sysctl_iflist_ifml(ifp, &info, w, len);
1826 error = sysctl_iflist_ifm(ifp, &info, w, len);
1830 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1831 if (af && af != ifa->ifa_addr->sa_family)
1833 if (prison_if(w->w_req->td->td_ucred,
1834 ifa->ifa_addr) != 0)
1836 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1837 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1838 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1839 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1840 if (w->w_req && w->w_tmem) {
1841 if (w->w_op == NET_RT_IFLISTL)
1842 error = sysctl_iflist_ifaml(ifa, &info,
1845 error = sysctl_iflist_ifam(ifa, &info,
1851 IF_ADDR_RUNLOCK(ifp);
1852 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1853 info.rti_info[RTAX_BRD] = NULL;
1857 IF_ADDR_RUNLOCK(ifp);
1858 IFNET_RUNLOCK_NOSLEEP();
1863 sysctl_ifmalist(int af, struct walkarg *w)
1866 struct ifmultiaddr *ifma;
1867 struct rt_addrinfo info;
1871 bzero((caddr_t)&info, sizeof(info));
1872 IFNET_RLOCK_NOSLEEP();
1873 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1874 if (w->w_arg && w->w_arg != ifp->if_index)
1877 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1879 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1880 if (af && af != ifma->ifma_addr->sa_family)
1882 if (prison_if(w->w_req->td->td_ucred,
1883 ifma->ifma_addr) != 0)
1885 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1886 info.rti_info[RTAX_GATEWAY] =
1887 (ifma->ifma_addr->sa_family != AF_LINK) ?
1888 ifma->ifma_lladdr : NULL;
1889 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1890 if (w->w_req && w->w_tmem) {
1891 struct ifma_msghdr *ifmam;
1893 ifmam = (struct ifma_msghdr *)w->w_tmem;
1894 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1895 ifmam->ifmam_flags = 0;
1896 ifmam->ifmam_addrs = info.rti_addrs;
1897 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1899 IF_ADDR_RUNLOCK(ifp);
1904 IF_ADDR_RUNLOCK(ifp);
1907 IFNET_RUNLOCK_NOSLEEP();
1912 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1914 int *name = (int *)arg1;
1915 u_int namelen = arg2;
1916 struct radix_node_head *rnh = NULL; /* silence compiler. */
1917 int i, lim, error = EINVAL;
1926 if (name[1] == NET_RT_DUMP) {
1928 fib = req->td->td_proc->p_fibnum;
1929 else if (namelen == 4)
1930 fib = (name[3] == RT_ALL_FIBS) ?
1931 req->td->td_proc->p_fibnum : name[3];
1933 return ((namelen < 3) ? EISDIR : ENOTDIR);
1934 if (fib < 0 || fib >= rt_numfibs)
1936 } else if (namelen != 3)
1937 return ((namelen < 3) ? EISDIR : ENOTDIR);
1941 bzero(&w, sizeof(w));
1946 error = sysctl_wire_old_buffer(req, 0);
1953 if (af == 0) { /* dump all tables */
1956 } else /* dump only one table */
1960 * take care of llinfo entries, the caller must
1963 if (w.w_op == NET_RT_FLAGS &&
1964 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1966 error = lltable_sysctl_dumparp(af, w.w_req);
1972 * take care of routing entries
1974 for (error = 0; error == 0 && i <= lim; i++) {
1975 rnh = rt_tables_get_rnh(fib, i);
1977 RADIX_NODE_HEAD_RLOCK(rnh);
1978 error = rnh->rnh_walktree(rnh,
1979 sysctl_dumpentry, &w);
1980 RADIX_NODE_HEAD_RUNLOCK(rnh);
1982 error = EAFNOSUPPORT;
1987 case NET_RT_IFLISTL:
1988 error = sysctl_iflist(af, &w);
1991 case NET_RT_IFMALIST:
1992 error = sysctl_ifmalist(af, &w);
1996 free(w.w_tmem, M_RTABLE);
2000 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
2003 * Definitions of protocols supported in the ROUTE domain.
2006 static struct domain routedomain; /* or at least forward */
2008 static struct protosw routesw[] = {
2010 .pr_type = SOCK_RAW,
2011 .pr_domain = &routedomain,
2012 .pr_flags = PR_ATOMIC|PR_ADDR,
2013 .pr_output = route_output,
2014 .pr_ctlinput = raw_ctlinput,
2015 .pr_init = raw_init,
2016 .pr_usrreqs = &route_usrreqs
2020 static struct domain routedomain = {
2021 .dom_family = PF_ROUTE,
2022 .dom_name = "route",
2023 .dom_protosw = routesw,
2024 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
2027 VNET_DOMAIN_SET(route);