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>
54 #define _IN_NET_RTSOCK_C
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;
102 uint32_t ifi_oqdrops;
112 struct if_data32 ifm_data;
115 struct if_msghdrl32 {
122 uint16_t _ifm_spare1;
124 uint16_t ifm_data_off;
125 struct if_data32 ifm_data;
128 struct ifa_msghdrl32 {
129 uint16_t ifam_msglen;
130 uint8_t ifam_version;
135 uint16_t _ifam_spare1;
137 uint16_t ifam_data_off;
139 struct if_data32 ifam_data;
141 #endif /* COMPAT_FREEBSD32 */
143 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
145 /* NB: these are not modified */
146 static struct sockaddr route_src = { 2, PF_ROUTE, };
147 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
149 /* These are external hooks for CARP. */
150 int (*carp_get_vhid_p)(struct ifaddr *);
153 * Used by rtsock/raw_input callback code to decide whether to filter the update
154 * notification to a socket bound to a particular FIB.
156 #define RTS_FILTER_FIB M_PROTO8
159 int ip_count; /* attached w/ AF_INET */
160 int ip6_count; /* attached w/ AF_INET6 */
161 int ipx_count; /* attached w/ AF_IPX */
162 int any_count; /* total attached */
165 struct mtx rtsock_mtx;
166 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
168 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
169 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
170 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
172 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
178 struct sysctl_req *w_req;
181 static void rts_input(struct mbuf *m);
182 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
183 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
184 caddr_t cp, struct walkarg *w);
185 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
186 struct rt_addrinfo *rtinfo);
187 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
188 static int sysctl_iflist(int af, struct walkarg *w);
189 static int sysctl_ifmalist(int af, struct walkarg *w);
190 static int route_output(struct mbuf *m, struct socket *so);
191 static void rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt);
192 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
193 static void rt_dispatch(struct mbuf *, sa_family_t);
195 static struct netisr_handler rtsock_nh = {
197 .nh_handler = rts_input,
198 .nh_proto = NETISR_ROUTE,
199 .nh_policy = NETISR_POLICY_SOURCE,
203 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
207 netisr_getqlimit(&rtsock_nh, &qlimit);
208 error = sysctl_handle_int(oidp, &qlimit, 0, req);
209 if (error || !req->newptr)
213 return (netisr_setqlimit(&rtsock_nh, qlimit));
215 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
216 0, 0, sysctl_route_netisr_maxqlen, "I",
217 "maximum routing socket dispatch queue length");
224 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
225 rtsock_nh.nh_qlimit = tmp;
226 netisr_register(&rtsock_nh);
228 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
231 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
236 KASSERT(m != NULL, ("%s: m is NULL", __func__));
237 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
238 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
240 /* No filtering requested. */
241 if ((m->m_flags & RTS_FILTER_FIB) == 0)
244 /* Check if it is a rts and the fib matches the one of the socket. */
245 fibnum = M_GETFIB(m);
246 if (proto->sp_family != PF_ROUTE ||
247 rp->rcb_socket == NULL ||
248 rp->rcb_socket->so_fibnum == fibnum)
251 /* Filtering requested and no match, the socket shall be skipped. */
256 rts_input(struct mbuf *m)
258 struct sockproto route_proto;
259 unsigned short *family;
262 route_proto.sp_family = PF_ROUTE;
263 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
265 family = (unsigned short *)(tag + 1);
266 route_proto.sp_protocol = *family;
267 m_tag_delete(m, tag);
269 route_proto.sp_protocol = 0;
271 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
275 * It really doesn't make any sense at all for this code to share much
276 * with raw_usrreq.c, since its functionality is so restricted. XXX
279 rts_abort(struct socket *so)
282 raw_usrreqs.pru_abort(so);
286 rts_close(struct socket *so)
289 raw_usrreqs.pru_close(so);
292 /* pru_accept is EOPNOTSUPP */
295 rts_attach(struct socket *so, int proto, struct thread *td)
300 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
303 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
305 so->so_pcb = (caddr_t)rp;
306 so->so_fibnum = td->td_proc->p_fibnum;
307 error = raw_attach(so, proto);
315 switch(rp->rcb_proto.sp_protocol) {
320 route_cb.ip6_count++;
323 route_cb.ipx_count++;
326 route_cb.any_count++;
329 so->so_options |= SO_USELOOPBACK;
334 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
337 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
341 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
344 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
347 /* pru_connect2 is EOPNOTSUPP */
348 /* pru_control is EOPNOTSUPP */
351 rts_detach(struct socket *so)
353 struct rawcb *rp = sotorawcb(so);
355 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
358 switch(rp->rcb_proto.sp_protocol) {
363 route_cb.ip6_count--;
366 route_cb.ipx_count--;
369 route_cb.any_count--;
371 raw_usrreqs.pru_detach(so);
375 rts_disconnect(struct socket *so)
378 return (raw_usrreqs.pru_disconnect(so));
381 /* pru_listen is EOPNOTSUPP */
384 rts_peeraddr(struct socket *so, struct sockaddr **nam)
387 return (raw_usrreqs.pru_peeraddr(so, nam));
390 /* pru_rcvd is EOPNOTSUPP */
391 /* pru_rcvoob is EOPNOTSUPP */
394 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
395 struct mbuf *control, struct thread *td)
398 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
401 /* pru_sense is null */
404 rts_shutdown(struct socket *so)
407 return (raw_usrreqs.pru_shutdown(so));
411 rts_sockaddr(struct socket *so, struct sockaddr **nam)
414 return (raw_usrreqs.pru_sockaddr(so, nam));
417 static struct pr_usrreqs route_usrreqs = {
418 .pru_abort = rts_abort,
419 .pru_attach = rts_attach,
420 .pru_bind = rts_bind,
421 .pru_connect = rts_connect,
422 .pru_detach = rts_detach,
423 .pru_disconnect = rts_disconnect,
424 .pru_peeraddr = rts_peeraddr,
425 .pru_send = rts_send,
426 .pru_shutdown = rts_shutdown,
427 .pru_sockaddr = rts_sockaddr,
428 .pru_close = rts_close,
431 #ifndef _SOCKADDR_UNION_DEFINED
432 #define _SOCKADDR_UNION_DEFINED
434 * The union of all possible address formats we handle.
436 union sockaddr_union {
438 struct sockaddr_in sin;
439 struct sockaddr_in6 sin6;
441 #endif /* _SOCKADDR_UNION_DEFINED */
444 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
445 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
448 /* First, see if the returned address is part of the jail. */
449 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
450 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
454 switch (info->rti_info[RTAX_DST]->sa_family) {
464 * Try to find an address on the given outgoing interface
465 * that belongs to the jail.
468 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
471 if (sa->sa_family != AF_INET)
473 ia = ((struct sockaddr_in *)sa)->sin_addr;
474 if (prison_check_ip4(cred, &ia) == 0) {
479 IF_ADDR_RUNLOCK(ifp);
482 * As a last resort return the 'default' jail address.
484 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
486 if (prison_get_ip4(cred, &ia) != 0)
489 bzero(&saun->sin, sizeof(struct sockaddr_in));
490 saun->sin.sin_len = sizeof(struct sockaddr_in);
491 saun->sin.sin_family = AF_INET;
492 saun->sin.sin_addr.s_addr = ia.s_addr;
493 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
506 * Try to find an address on the given outgoing interface
507 * that belongs to the jail.
510 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
513 if (sa->sa_family != AF_INET6)
515 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
516 &ia6, sizeof(struct in6_addr));
517 if (prison_check_ip6(cred, &ia6) == 0) {
522 IF_ADDR_RUNLOCK(ifp);
525 * As a last resort return the 'default' jail address.
527 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
529 if (prison_get_ip6(cred, &ia6) != 0)
532 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
533 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
534 saun->sin6.sin6_family = AF_INET6;
535 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
536 if (sa6_recoverscope(&saun->sin6) != 0)
538 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
550 route_output(struct mbuf *m, struct socket *so)
552 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
553 struct rt_msghdr *rtm = NULL;
554 struct rtentry *rt = NULL;
555 struct radix_node_head *rnh;
556 struct rt_addrinfo info;
558 struct sockaddr_storage ss;
559 struct sockaddr_in6 *sin6;
560 int i, rti_need_deembed = 0;
563 struct ifnet *ifp = NULL;
564 union sockaddr_union saun;
565 sa_family_t saf = AF_UNSPEC;
567 #define senderr(e) { error = e; goto flush;}
568 if (m == NULL || ((m->m_len < sizeof(long)) &&
569 (m = m_pullup(m, sizeof(long))) == NULL))
571 if ((m->m_flags & M_PKTHDR) == 0)
572 panic("route_output");
573 len = m->m_pkthdr.len;
574 if (len < sizeof(*rtm) ||
575 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
576 info.rti_info[RTAX_DST] = NULL;
579 R_Malloc(rtm, struct rt_msghdr *, len);
581 info.rti_info[RTAX_DST] = NULL;
584 m_copydata(m, 0, len, (caddr_t)rtm);
585 if (rtm->rtm_version != RTM_VERSION) {
586 info.rti_info[RTAX_DST] = NULL;
587 senderr(EPROTONOSUPPORT);
589 rtm->rtm_pid = curproc->p_pid;
590 bzero(&info, sizeof(info));
591 info.rti_addrs = rtm->rtm_addrs;
593 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
594 * link-local address because rtrequest requires addresses with
597 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
598 info.rti_info[RTAX_DST] = NULL;
601 info.rti_flags = rtm->rtm_flags;
602 if (info.rti_info[RTAX_DST] == NULL ||
603 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
604 (info.rti_info[RTAX_GATEWAY] != NULL &&
605 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
607 saf = info.rti_info[RTAX_DST]->sa_family;
609 * Verify that the caller has the appropriate privilege; RTM_GET
610 * is the only operation the non-superuser is allowed.
612 if (rtm->rtm_type != RTM_GET) {
613 error = priv_check(curthread, PRIV_NET_ROUTE);
619 * The given gateway address may be an interface address.
620 * For example, issuing a "route change" command on a route
621 * entry that was created from a tunnel, and the gateway
622 * address given is the local end point. In this case the
623 * RTF_GATEWAY flag must be cleared or the destination will
624 * not be reachable even though there is no error message.
626 if (info.rti_info[RTAX_GATEWAY] != NULL &&
627 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
630 bzero(&gw_ro, sizeof(gw_ro));
631 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
632 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
634 * A host route through the loopback interface is
635 * installed for each interface adddress. In pre 8.0
636 * releases the interface address of a PPP link type
637 * is not reachable locally. This behavior is fixed as
638 * part of the new L2/L3 redesign and rewrite work. The
639 * signature of this interface address route is the
640 * AF_LINK sa_family type of the rt_gateway, and the
641 * rt_ifp has the IFF_LOOPBACK flag set.
643 if (gw_ro.ro_rt != NULL &&
644 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
645 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
646 info.rti_flags &= ~RTF_GATEWAY;
647 info.rti_flags |= RTF_GWFLAG_COMPAT;
649 if (gw_ro.ro_rt != NULL)
653 switch (rtm->rtm_type) {
654 struct rtentry *saved_nrt;
657 if (info.rti_info[RTAX_GATEWAY] == NULL)
661 /* support for new ARP code */
662 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
663 (rtm->rtm_flags & RTF_LLDATA) != 0) {
664 error = lla_rt_output(rtm, &info);
667 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
671 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
673 if (error == 0 && saved_nrt) {
675 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
678 rt_setmetrics(rtm, saved_nrt);
679 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
680 RT_REMREF(saved_nrt);
681 RT_UNLOCK(saved_nrt);
687 /* support for new ARP code */
688 if (info.rti_info[RTAX_GATEWAY] &&
689 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
690 (rtm->rtm_flags & RTF_LLDATA) != 0) {
691 error = lla_rt_output(rtm, &info);
694 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
698 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
706 /* rt_msg2() will not be used when RTM_DELETE fails. */
707 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
714 rnh = rt_tables_get_rnh(so->so_fibnum,
715 info.rti_info[RTAX_DST]->sa_family);
717 senderr(EAFNOSUPPORT);
719 RADIX_NODE_HEAD_RLOCK(rnh);
721 if (info.rti_info[RTAX_NETMASK] == NULL &&
722 rtm->rtm_type == RTM_GET) {
724 * Provide logest prefix match for
725 * address lookup (no mask).
726 * 'route -n get addr'
728 rt = (struct rtentry *) rnh->rnh_matchaddr(
729 info.rti_info[RTAX_DST], rnh);
731 rt = (struct rtentry *) rnh->rnh_lookup(
732 info.rti_info[RTAX_DST],
733 info.rti_info[RTAX_NETMASK], rnh);
736 RADIX_NODE_HEAD_RUNLOCK(rnh);
741 * for RTM_CHANGE/LOCK, if we got multipath routes,
742 * we require users to specify a matching RTAX_GATEWAY.
744 * for RTM_GET, gate is optional even with multipath.
745 * if gate == NULL the first match is returned.
746 * (no need to call rt_mpath_matchgate if gate == NULL)
748 if (rn_mpath_capable(rnh) &&
749 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
750 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
752 RADIX_NODE_HEAD_RUNLOCK(rnh);
758 * If performing proxied L2 entry insertion, and
759 * the actual PPP host entry is found, perform
760 * another search to retrieve the prefix route of
761 * the local end point of the PPP link.
763 if (rtm->rtm_flags & RTF_ANNOUNCE) {
764 struct sockaddr laddr;
766 if (rt->rt_ifp != NULL &&
767 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
770 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
772 rt_maskedcopy(ifa->ifa_addr,
776 rt_maskedcopy(rt->rt_ifa->ifa_addr,
778 rt->rt_ifa->ifa_netmask);
780 * refactor rt and no lock operation necessary
782 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
784 RADIX_NODE_HEAD_RUNLOCK(rnh);
790 RADIX_NODE_HEAD_RUNLOCK(rnh);
792 switch(rtm->rtm_type) {
797 if ((rt->rt_flags & RTF_HOST) == 0
798 ? jailed_without_vnet(curthread->td_ucred)
799 : prison_if(curthread->td_ucred,
804 info.rti_info[RTAX_DST] = rt_key(rt);
805 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
806 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
807 info.rti_info[RTAX_GENMASK] = 0;
808 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
811 info.rti_info[RTAX_IFP] =
812 ifp->if_addr->ifa_addr;
813 error = rtm_get_jailed(&info, ifp, rt,
814 &saun, curthread->td_ucred);
819 if (ifp->if_flags & IFF_POINTOPOINT)
820 info.rti_info[RTAX_BRD] =
821 rt->rt_ifa->ifa_dstaddr;
822 rtm->rtm_index = ifp->if_index;
824 info.rti_info[RTAX_IFP] = NULL;
825 info.rti_info[RTAX_IFA] = NULL;
827 } else if ((ifp = rt->rt_ifp) != NULL) {
828 rtm->rtm_index = ifp->if_index;
830 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
831 if (len > rtm->rtm_msglen) {
832 struct rt_msghdr *new_rtm;
833 R_Malloc(new_rtm, struct rt_msghdr *, len);
834 if (new_rtm == NULL) {
838 bcopy(rtm, new_rtm, rtm->rtm_msglen);
839 Free(rtm); rtm = new_rtm;
841 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
842 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
843 rtm->rtm_flags = RTF_GATEWAY |
844 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
846 rtm->rtm_flags = rt->rt_flags;
847 rt_getmetrics(rt, &rtm->rtm_rmx);
848 rtm->rtm_addrs = info.rti_addrs;
853 * New gateway could require new ifaddr, ifp;
854 * flags may also be different; ifp may be specified
855 * by ll sockaddr when protocol address is ambiguous
857 if (((rt->rt_flags & RTF_GATEWAY) &&
858 info.rti_info[RTAX_GATEWAY] != NULL) ||
859 info.rti_info[RTAX_IFP] != NULL ||
860 (info.rti_info[RTAX_IFA] != NULL &&
861 !sa_equal(info.rti_info[RTAX_IFA],
862 rt->rt_ifa->ifa_addr))) {
864 RADIX_NODE_HEAD_LOCK(rnh);
865 error = rt_getifa_fib(&info, rt->rt_fibnum);
867 * XXXRW: Really we should release this
868 * reference later, but this maintains
869 * historical behavior.
871 if (info.rti_ifa != NULL)
872 ifa_free(info.rti_ifa);
873 RADIX_NODE_HEAD_UNLOCK(rnh);
878 if (info.rti_ifa != NULL &&
879 info.rti_ifa != rt->rt_ifa &&
880 rt->rt_ifa != NULL &&
881 rt->rt_ifa->ifa_rtrequest != NULL) {
882 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
884 ifa_free(rt->rt_ifa);
886 if (info.rti_info[RTAX_GATEWAY] != NULL) {
888 RADIX_NODE_HEAD_LOCK(rnh);
891 error = rt_setgate(rt, rt_key(rt),
892 info.rti_info[RTAX_GATEWAY]);
893 RADIX_NODE_HEAD_UNLOCK(rnh);
898 rt->rt_flags &= ~RTF_GATEWAY;
899 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
901 if (info.rti_ifa != NULL &&
902 info.rti_ifa != rt->rt_ifa) {
903 ifa_ref(info.rti_ifa);
904 rt->rt_ifa = info.rti_ifa;
905 rt->rt_ifp = info.rti_ifp;
907 /* Allow some flags to be toggled on change. */
908 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
909 (rtm->rtm_flags & RTF_FMASK);
910 rt_setmetrics(rtm, rt);
911 rtm->rtm_index = rt->rt_ifp->if_index;
912 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
913 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
916 /* We don't support locks anymore */
929 rtm->rtm_errno = error;
931 rtm->rtm_flags |= RTF_DONE;
933 if (rt) /* XXX can this be true? */
936 struct rawcb *rp = NULL;
938 * Check to see if we don't want our own messages.
940 if ((so->so_options & SO_USELOOPBACK) == 0) {
941 if (route_cb.any_count <= 1) {
947 /* There is another listener, so construct message */
952 if (rti_need_deembed) {
953 /* sin6_scope_id is recovered before sending rtm. */
954 sin6 = (struct sockaddr_in6 *)&ss;
955 for (i = 0; i < RTAX_MAX; i++) {
956 if (info.rti_info[i] == NULL)
958 if (info.rti_info[i]->sa_family != AF_INET6)
960 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
961 if (sa6_recoverscope(sin6) == 0)
962 bcopy(sin6, info.rti_info[i],
967 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
968 if (m->m_pkthdr.len < rtm->rtm_msglen) {
971 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
972 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
975 M_SETFIB(m, so->so_fibnum);
976 m->m_flags |= RTS_FILTER_FIB;
979 * XXX insure we don't get a copy by
980 * invalidating our protocol
982 unsigned short family = rp->rcb_proto.sp_family;
983 rp->rcb_proto.sp_family = 0;
985 rp->rcb_proto.sp_family = family;
989 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
998 rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt)
1001 if (rtm->rtm_inits & RTV_MTU)
1002 rt->rt_mtu = rtm->rtm_rmx.rmx_mtu;
1003 if (rtm->rtm_inits & RTV_WEIGHT)
1004 rt->rt_weight = rtm->rtm_rmx.rmx_weight;
1005 /* Kernel -> userland timebase conversion. */
1006 if (rtm->rtm_inits & RTV_EXPIRE)
1007 rt->rt_expire = rtm->rtm_rmx.rmx_expire ?
1008 rtm->rtm_rmx.rmx_expire - time_second + time_uptime : 0;
1012 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
1015 bzero(out, sizeof(*out));
1016 out->rmx_mtu = rt->rt_mtu;
1017 out->rmx_weight = rt->rt_weight;
1018 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
1019 /* Kernel -> userland timebase conversion. */
1020 out->rmx_expire = rt->rt_expire ?
1021 rt->rt_expire - time_uptime + time_second : 0;
1025 * Extract the addresses of the passed sockaddrs.
1026 * Do a little sanity checking so as to avoid bad memory references.
1027 * This data is derived straight from userland.
1030 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1032 struct sockaddr *sa;
1035 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1036 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1038 sa = (struct sockaddr *)cp;
1042 if (cp + sa->sa_len > cplim)
1045 * there are no more.. quit now
1046 * If there are more bits, they are in error.
1047 * I've seen this. route(1) can evidently generate these.
1048 * This causes kernel to core dump.
1049 * for compatibility, If we see this, point to a safe address.
1051 if (sa->sa_len == 0) {
1052 rtinfo->rti_info[i] = &sa_zero;
1053 return (0); /* should be EINVAL but for compat */
1057 if (sa->sa_family == AF_INET6)
1058 sa6_embedscope((struct sockaddr_in6 *)sa,
1061 rtinfo->rti_info[i] = sa;
1068 * Used by the routing socket.
1070 static struct mbuf *
1071 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1073 struct rt_msghdr *rtm;
1076 struct sockaddr *sa;
1078 struct sockaddr_storage ss;
1079 struct sockaddr_in6 *sin6;
1087 len = sizeof(struct ifa_msghdr);
1092 len = sizeof(struct ifma_msghdr);
1096 len = sizeof(struct if_msghdr);
1099 case RTM_IFANNOUNCE:
1101 len = sizeof(struct if_announcemsghdr);
1105 len = sizeof(struct rt_msghdr);
1108 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1109 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1111 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1113 m = m_gethdr(M_NOWAIT, MT_DATA);
1117 m->m_pkthdr.len = m->m_len = len;
1118 rtm = mtod(m, struct rt_msghdr *);
1119 bzero((caddr_t)rtm, len);
1120 for (i = 0; i < RTAX_MAX; i++) {
1121 if ((sa = rtinfo->rti_info[i]) == NULL)
1123 rtinfo->rti_addrs |= (1 << i);
1126 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1127 sin6 = (struct sockaddr_in6 *)&ss;
1128 bcopy(sa, sin6, sizeof(*sin6));
1129 if (sa6_recoverscope(sin6) == 0)
1130 sa = (struct sockaddr *)sin6;
1133 m_copyback(m, len, dlen, (caddr_t)sa);
1136 if (m->m_pkthdr.len != len) {
1140 rtm->rtm_msglen = len;
1141 rtm->rtm_version = RTM_VERSION;
1142 rtm->rtm_type = type;
1147 * Used by the sysctl code and routing socket.
1150 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1153 int len, dlen, second_time = 0;
1156 struct sockaddr_storage ss;
1157 struct sockaddr_in6 *sin6;
1160 rtinfo->rti_addrs = 0;
1166 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1167 #ifdef COMPAT_FREEBSD32
1168 if (w->w_req->flags & SCTL_MASK32)
1169 len = sizeof(struct ifa_msghdrl32);
1172 len = sizeof(struct ifa_msghdrl);
1174 len = sizeof(struct ifa_msghdr);
1178 #ifdef COMPAT_FREEBSD32
1179 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1180 if (w->w_op == NET_RT_IFLISTL)
1181 len = sizeof(struct if_msghdrl32);
1183 len = sizeof(struct if_msghdr32);
1187 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1188 len = sizeof(struct if_msghdrl);
1190 len = sizeof(struct if_msghdr);
1194 len = sizeof(struct ifma_msghdr);
1198 len = sizeof(struct rt_msghdr);
1203 for (i = 0; i < RTAX_MAX; i++) {
1204 struct sockaddr *sa;
1206 if ((sa = rtinfo->rti_info[i]) == NULL)
1208 rtinfo->rti_addrs |= (1 << i);
1212 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1213 sin6 = (struct sockaddr_in6 *)&ss;
1214 bcopy(sa, sin6, sizeof(*sin6));
1215 if (sa6_recoverscope(sin6) == 0)
1216 sa = (struct sockaddr *)sin6;
1219 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1225 if (cp == NULL && w != NULL && !second_time) {
1226 struct walkarg *rw = w;
1229 if (rw->w_tmemsize < len) {
1231 free(rw->w_tmem, M_RTABLE);
1232 rw->w_tmem = (caddr_t)
1233 malloc(len, M_RTABLE, M_NOWAIT);
1235 rw->w_tmemsize = len;
1245 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1247 rtm->rtm_version = RTM_VERSION;
1248 rtm->rtm_type = type;
1249 rtm->rtm_msglen = len;
1255 * This routine is called to generate a message from the routing
1256 * socket indicating that a redirect has occured, a routing lookup
1257 * has failed, or that a protocol has detected timeouts to a particular
1261 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1264 struct rt_msghdr *rtm;
1266 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1268 if (route_cb.any_count == 0)
1270 m = rt_msg1(type, rtinfo);
1274 if (fibnum != RT_ALL_FIBS) {
1275 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1276 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1277 M_SETFIB(m, fibnum);
1278 m->m_flags |= RTS_FILTER_FIB;
1281 rtm = mtod(m, struct rt_msghdr *);
1282 rtm->rtm_flags = RTF_DONE | flags;
1283 rtm->rtm_errno = error;
1284 rtm->rtm_addrs = rtinfo->rti_addrs;
1285 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1289 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1292 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1296 * This routine is called to generate a message from the routing
1297 * socket indicating that the status of a network interface has changed.
1300 rt_ifmsg(struct ifnet *ifp)
1302 struct if_msghdr *ifm;
1304 struct rt_addrinfo info;
1306 if (route_cb.any_count == 0)
1308 bzero((caddr_t)&info, sizeof(info));
1309 m = rt_msg1(RTM_IFINFO, &info);
1312 ifm = mtod(m, struct if_msghdr *);
1313 ifm->ifm_index = ifp->if_index;
1314 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1315 ifm->ifm_data = ifp->if_data;
1317 rt_dispatch(m, AF_UNSPEC);
1321 * Announce interface address arrival/withdraw.
1322 * Please do not call directly, use rt_addrmsg().
1323 * Assume input data to be valid.
1324 * Returns 0 on success.
1327 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1329 struct rt_addrinfo info;
1330 struct sockaddr *sa;
1333 struct ifa_msghdr *ifam;
1334 struct ifnet *ifp = ifa->ifa_ifp;
1336 if (route_cb.any_count == 0)
1339 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1341 bzero((caddr_t)&info, sizeof(info));
1342 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1343 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1344 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1345 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1346 if ((m = rt_msg1(ncmd, &info)) == NULL)
1348 ifam = mtod(m, struct ifa_msghdr *);
1349 ifam->ifam_index = ifp->if_index;
1350 ifam->ifam_metric = ifa->ifa_metric;
1351 ifam->ifam_flags = ifa->ifa_flags;
1352 ifam->ifam_addrs = info.rti_addrs;
1354 if (fibnum != RT_ALL_FIBS) {
1355 M_SETFIB(m, fibnum);
1356 m->m_flags |= RTS_FILTER_FIB;
1359 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1365 * Announce route addition/removal.
1366 * Please do not call directly, use rt_routemsg().
1367 * Note that @rt data MAY be inconsistent/invalid:
1368 * if some userland app sends us "invalid" route message (invalid mask,
1369 * no dst, wrong address families, etc...) we need to pass it back
1370 * to app (and any other rtsock consumers) with rtm_errno field set to
1373 * Returns 0 on success.
1376 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1379 struct rt_addrinfo info;
1380 struct sockaddr *sa;
1382 struct rt_msghdr *rtm;
1384 if (route_cb.any_count == 0)
1387 bzero((caddr_t)&info, sizeof(info));
1388 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1389 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1390 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1391 if ((m = rt_msg1(cmd, &info)) == NULL)
1393 rtm = mtod(m, struct rt_msghdr *);
1394 rtm->rtm_index = ifp->if_index;
1395 rtm->rtm_flags |= rt->rt_flags;
1396 rtm->rtm_errno = error;
1397 rtm->rtm_addrs = info.rti_addrs;
1399 if (fibnum != RT_ALL_FIBS) {
1400 M_SETFIB(m, fibnum);
1401 m->m_flags |= RTS_FILTER_FIB;
1404 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1410 * This is the analogue to the rt_newaddrmsg which performs the same
1411 * function but for multicast group memberhips. This is easier since
1412 * there is no route state to worry about.
1415 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1417 struct rt_addrinfo info;
1418 struct mbuf *m = NULL;
1419 struct ifnet *ifp = ifma->ifma_ifp;
1420 struct ifma_msghdr *ifmam;
1422 if (route_cb.any_count == 0)
1425 bzero((caddr_t)&info, sizeof(info));
1426 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1427 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1429 * If a link-layer address is present, present it as a ``gateway''
1430 * (similarly to how ARP entries, e.g., are presented).
1432 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1433 m = rt_msg1(cmd, &info);
1436 ifmam = mtod(m, struct ifma_msghdr *);
1437 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1439 ifmam->ifmam_index = ifp->if_index;
1440 ifmam->ifmam_addrs = info.rti_addrs;
1441 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1444 static struct mbuf *
1445 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1446 struct rt_addrinfo *info)
1448 struct if_announcemsghdr *ifan;
1451 if (route_cb.any_count == 0)
1453 bzero((caddr_t)info, sizeof(*info));
1454 m = rt_msg1(type, info);
1456 ifan = mtod(m, struct if_announcemsghdr *);
1457 ifan->ifan_index = ifp->if_index;
1458 strlcpy(ifan->ifan_name, ifp->if_xname,
1459 sizeof(ifan->ifan_name));
1460 ifan->ifan_what = what;
1466 * This is called to generate routing socket messages indicating
1467 * IEEE80211 wireless events.
1468 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1471 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1474 struct rt_addrinfo info;
1476 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1479 * Append the ieee80211 data. Try to stick it in the
1480 * mbuf containing the ifannounce msg; otherwise allocate
1481 * a new mbuf and append.
1483 * NB: we assume m is a single mbuf.
1485 if (data_len > M_TRAILINGSPACE(m)) {
1486 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1491 bcopy(data, mtod(n, void *), data_len);
1492 n->m_len = data_len;
1494 } else if (data_len > 0) {
1495 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1496 m->m_len += data_len;
1498 if (m->m_flags & M_PKTHDR)
1499 m->m_pkthdr.len += data_len;
1500 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1501 rt_dispatch(m, AF_UNSPEC);
1506 * This is called to generate routing socket messages indicating
1507 * network interface arrival and departure.
1510 rt_ifannouncemsg(struct ifnet *ifp, int what)
1513 struct rt_addrinfo info;
1515 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1517 rt_dispatch(m, AF_UNSPEC);
1521 rt_dispatch(struct mbuf *m, sa_family_t saf)
1526 * Preserve the family from the sockaddr, if any, in an m_tag for
1527 * use when injecting the mbuf into the routing socket buffer from
1530 if (saf != AF_UNSPEC) {
1531 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1537 *(unsigned short *)(tag + 1) = saf;
1538 m_tag_prepend(m, tag);
1542 m->m_pkthdr.rcvif = V_loif;
1548 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1552 * This is used in dumping the kernel table via sysctl().
1555 sysctl_dumpentry(struct radix_node *rn, void *vw)
1557 struct walkarg *w = vw;
1558 struct rtentry *rt = (struct rtentry *)rn;
1559 int error = 0, size;
1560 struct rt_addrinfo info;
1562 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1564 if ((rt->rt_flags & RTF_HOST) == 0
1565 ? jailed_without_vnet(w->w_req->td->td_ucred)
1566 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1568 bzero((caddr_t)&info, sizeof(info));
1569 info.rti_info[RTAX_DST] = rt_key(rt);
1570 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1571 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1572 info.rti_info[RTAX_GENMASK] = 0;
1574 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1575 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1576 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1577 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1579 size = rt_msg2(RTM_GET, &info, NULL, w);
1580 if (w->w_req && w->w_tmem) {
1581 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1583 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1584 rtm->rtm_flags = RTF_GATEWAY |
1585 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1587 rtm->rtm_flags = rt->rt_flags;
1588 rt_getmetrics(rt, &rtm->rtm_rmx);
1589 rtm->rtm_index = rt->rt_ifp->if_index;
1590 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1591 rtm->rtm_addrs = info.rti_addrs;
1592 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1598 #ifdef COMPAT_FREEBSD32
1600 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1603 bzero(dst, sizeof(*dst));
1604 CP(*src, *dst, ifi_type);
1605 CP(*src, *dst, ifi_physical);
1606 CP(*src, *dst, ifi_addrlen);
1607 CP(*src, *dst, ifi_hdrlen);
1608 CP(*src, *dst, ifi_link_state);
1609 CP(*src, *dst, ifi_vhid);
1610 CP(*src, *dst, ifi_baudrate_pf);
1611 dst->ifi_datalen = sizeof(struct if_data32);
1612 CP(*src, *dst, ifi_mtu);
1613 CP(*src, *dst, ifi_metric);
1614 CP(*src, *dst, ifi_baudrate);
1615 CP(*src, *dst, ifi_ipackets);
1616 CP(*src, *dst, ifi_ierrors);
1617 CP(*src, *dst, ifi_opackets);
1618 CP(*src, *dst, ifi_oerrors);
1619 CP(*src, *dst, ifi_collisions);
1620 CP(*src, *dst, ifi_ibytes);
1621 CP(*src, *dst, ifi_obytes);
1622 CP(*src, *dst, ifi_imcasts);
1623 CP(*src, *dst, ifi_omcasts);
1624 CP(*src, *dst, ifi_iqdrops);
1625 CP(*src, *dst, ifi_noproto);
1626 CP(*src, *dst, ifi_hwassist);
1627 CP(*src, *dst, ifi_epoch);
1628 TV_CP(*src, *dst, ifi_lastchange);
1633 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1634 struct walkarg *w, int len)
1636 struct if_msghdrl *ifm;
1638 #ifdef COMPAT_FREEBSD32
1639 if (w->w_req->flags & SCTL_MASK32) {
1640 struct if_msghdrl32 *ifm32;
1642 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1643 ifm32->ifm_addrs = info->rti_addrs;
1644 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1645 ifm32->ifm_index = ifp->if_index;
1646 ifm32->_ifm_spare1 = 0;
1647 ifm32->ifm_len = sizeof(*ifm32);
1648 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1650 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1651 /* Fixup if_data carp(4) vhid. */
1652 if (carp_get_vhid_p != NULL)
1653 ifm32->ifm_data.ifi_vhid =
1654 (*carp_get_vhid_p)(ifp->if_addr);
1655 ifm32->ifm_data.ifi_oqdrops = ifp->if_snd.ifq_drops;
1657 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1660 ifm = (struct if_msghdrl *)w->w_tmem;
1661 ifm->ifm_addrs = info->rti_addrs;
1662 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1663 ifm->ifm_index = ifp->if_index;
1664 ifm->_ifm_spare1 = 0;
1665 ifm->ifm_len = sizeof(*ifm);
1666 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1668 ifm->ifm_data = ifp->if_data;
1669 /* Fixup if_data carp(4) vhid. */
1670 if (carp_get_vhid_p != NULL)
1671 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1673 ifm->ifm_data.ifi_datalen += sizeof(u_long);
1674 ifm->ifi_oqdrops = ifp->if_snd.ifq_drops;
1676 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1680 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1681 struct walkarg *w, int len)
1683 struct if_msghdr *ifm;
1685 #ifdef COMPAT_FREEBSD32
1686 if (w->w_req->flags & SCTL_MASK32) {
1687 struct if_msghdr32 *ifm32;
1689 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1690 ifm32->ifm_addrs = info->rti_addrs;
1691 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1692 ifm32->ifm_index = ifp->if_index;
1694 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1695 /* Fixup if_data carp(4) vhid. */
1696 if (carp_get_vhid_p != NULL)
1697 ifm32->ifm_data.ifi_vhid =
1698 (*carp_get_vhid_p)(ifp->if_addr);
1700 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1703 ifm = (struct if_msghdr *)w->w_tmem;
1704 ifm->ifm_addrs = info->rti_addrs;
1705 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1706 ifm->ifm_index = ifp->if_index;
1708 ifm->ifm_data = ifp->if_data;
1709 /* Fixup if_data carp(4) vhid. */
1710 if (carp_get_vhid_p != NULL)
1711 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1713 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1717 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1718 struct walkarg *w, int len)
1720 struct ifa_msghdrl *ifam;
1722 #ifdef COMPAT_FREEBSD32
1723 if (w->w_req->flags & SCTL_MASK32) {
1724 struct ifa_msghdrl32 *ifam32;
1726 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1727 ifam32->ifam_addrs = info->rti_addrs;
1728 ifam32->ifam_flags = ifa->ifa_flags;
1729 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1730 ifam32->_ifam_spare1 = 0;
1731 ifam32->ifam_len = sizeof(*ifam32);
1732 ifam32->ifam_data_off =
1733 offsetof(struct ifa_msghdrl32, ifam_data);
1734 ifam32->ifam_metric = ifa->ifa_metric;
1736 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1737 /* Fixup if_data carp(4) vhid. */
1738 if (carp_get_vhid_p != NULL)
1739 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1741 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1745 ifam = (struct ifa_msghdrl *)w->w_tmem;
1746 ifam->ifam_addrs = info->rti_addrs;
1747 ifam->ifam_flags = ifa->ifa_flags;
1748 ifam->ifam_index = ifa->ifa_ifp->if_index;
1749 ifam->_ifam_spare1 = 0;
1750 ifam->ifam_len = sizeof(*ifam);
1751 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1752 ifam->ifam_metric = ifa->ifa_metric;
1754 ifam->ifam_data = ifa->if_data;
1755 /* Fixup if_data carp(4) vhid. */
1756 if (carp_get_vhid_p != NULL)
1757 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1759 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1763 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1764 struct walkarg *w, int len)
1766 struct ifa_msghdr *ifam;
1768 ifam = (struct ifa_msghdr *)w->w_tmem;
1769 ifam->ifam_addrs = info->rti_addrs;
1770 ifam->ifam_flags = ifa->ifa_flags;
1771 ifam->ifam_index = ifa->ifa_ifp->if_index;
1772 ifam->ifam_metric = ifa->ifa_metric;
1774 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1778 sysctl_iflist(int af, struct walkarg *w)
1782 struct rt_addrinfo info;
1785 bzero((caddr_t)&info, sizeof(info));
1786 IFNET_RLOCK_NOSLEEP();
1787 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1788 if (w->w_arg && w->w_arg != ifp->if_index)
1792 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1793 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1794 info.rti_info[RTAX_IFP] = NULL;
1795 if (w->w_req && w->w_tmem) {
1796 if (w->w_op == NET_RT_IFLISTL)
1797 error = sysctl_iflist_ifml(ifp, &info, w, len);
1799 error = sysctl_iflist_ifm(ifp, &info, w, len);
1803 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1804 if (af && af != ifa->ifa_addr->sa_family)
1806 if (prison_if(w->w_req->td->td_ucred,
1807 ifa->ifa_addr) != 0)
1809 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1810 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1811 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1812 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1813 if (w->w_req && w->w_tmem) {
1814 if (w->w_op == NET_RT_IFLISTL)
1815 error = sysctl_iflist_ifaml(ifa, &info,
1818 error = sysctl_iflist_ifam(ifa, &info,
1824 IF_ADDR_RUNLOCK(ifp);
1825 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1826 info.rti_info[RTAX_BRD] = NULL;
1830 IF_ADDR_RUNLOCK(ifp);
1831 IFNET_RUNLOCK_NOSLEEP();
1836 sysctl_ifmalist(int af, struct walkarg *w)
1839 struct ifmultiaddr *ifma;
1840 struct rt_addrinfo info;
1844 bzero((caddr_t)&info, sizeof(info));
1845 IFNET_RLOCK_NOSLEEP();
1846 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1847 if (w->w_arg && w->w_arg != ifp->if_index)
1850 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1852 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1853 if (af && af != ifma->ifma_addr->sa_family)
1855 if (prison_if(w->w_req->td->td_ucred,
1856 ifma->ifma_addr) != 0)
1858 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1859 info.rti_info[RTAX_GATEWAY] =
1860 (ifma->ifma_addr->sa_family != AF_LINK) ?
1861 ifma->ifma_lladdr : NULL;
1862 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1863 if (w->w_req && w->w_tmem) {
1864 struct ifma_msghdr *ifmam;
1866 ifmam = (struct ifma_msghdr *)w->w_tmem;
1867 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1868 ifmam->ifmam_flags = 0;
1869 ifmam->ifmam_addrs = info.rti_addrs;
1870 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1872 IF_ADDR_RUNLOCK(ifp);
1877 IF_ADDR_RUNLOCK(ifp);
1880 IFNET_RUNLOCK_NOSLEEP();
1885 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1887 int *name = (int *)arg1;
1888 u_int namelen = arg2;
1889 struct radix_node_head *rnh = NULL; /* silence compiler. */
1890 int i, lim, error = EINVAL;
1899 if (name[1] == NET_RT_DUMP) {
1901 fib = req->td->td_proc->p_fibnum;
1902 else if (namelen == 4)
1903 fib = (name[3] == RT_ALL_FIBS) ?
1904 req->td->td_proc->p_fibnum : name[3];
1906 return ((namelen < 3) ? EISDIR : ENOTDIR);
1907 if (fib < 0 || fib >= rt_numfibs)
1909 } else if (namelen != 3)
1910 return ((namelen < 3) ? EISDIR : ENOTDIR);
1914 bzero(&w, sizeof(w));
1919 error = sysctl_wire_old_buffer(req, 0);
1926 if (af == 0) { /* dump all tables */
1929 } else /* dump only one table */
1933 * take care of llinfo entries, the caller must
1936 if (w.w_op == NET_RT_FLAGS &&
1937 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1939 error = lltable_sysctl_dumparp(af, w.w_req);
1945 * take care of routing entries
1947 for (error = 0; error == 0 && i <= lim; i++) {
1948 rnh = rt_tables_get_rnh(fib, i);
1950 RADIX_NODE_HEAD_RLOCK(rnh);
1951 error = rnh->rnh_walktree(rnh,
1952 sysctl_dumpentry, &w);
1953 RADIX_NODE_HEAD_RUNLOCK(rnh);
1955 error = EAFNOSUPPORT;
1960 case NET_RT_IFLISTL:
1961 error = sysctl_iflist(af, &w);
1964 case NET_RT_IFMALIST:
1965 error = sysctl_ifmalist(af, &w);
1969 free(w.w_tmem, M_RTABLE);
1973 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1976 * Definitions of protocols supported in the ROUTE domain.
1979 static struct domain routedomain; /* or at least forward */
1981 static struct protosw routesw[] = {
1983 .pr_type = SOCK_RAW,
1984 .pr_domain = &routedomain,
1985 .pr_flags = PR_ATOMIC|PR_ADDR,
1986 .pr_output = route_output,
1987 .pr_ctlinput = raw_ctlinput,
1988 .pr_init = raw_init,
1989 .pr_usrreqs = &route_usrreqs
1993 static struct domain routedomain = {
1994 .dom_family = PF_ROUTE,
1995 .dom_name = "route",
1996 .dom_protosw = routesw,
1997 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
2000 VNET_DOMAIN_SET(route);