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>
56 #include <net/if_var.h>
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;
118 struct if_data32 ifm_data;
121 struct if_msghdrl32 {
128 uint16_t _ifm_spare1;
130 uint16_t ifm_data_off;
131 struct if_data32 ifm_data;
134 struct ifa_msghdrl32 {
135 uint16_t ifam_msglen;
136 uint8_t ifam_version;
141 uint16_t _ifam_spare1;
143 uint16_t ifam_data_off;
145 struct if_data32 ifam_data;
147 #endif /* COMPAT_FREEBSD32 */
149 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
151 /* NB: these are not modified */
152 static struct sockaddr route_src = { 2, PF_ROUTE, };
153 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
155 /* These are external hooks for CARP. */
156 int (*carp_get_vhid_p)(struct ifaddr *);
159 * Used by rtsock/raw_input callback code to decide whether to filter the update
160 * notification to a socket bound to a particular FIB.
162 #define RTS_FILTER_FIB M_PROTO8
163 #define RTS_ALLFIBS -1
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(u_long which, const struct rt_metrics *in,
199 struct rt_metrics_lite *out);
200 static void rt_getmetrics(const struct rt_metrics_lite *in,
201 struct rt_metrics *out);
202 static void rt_dispatch(struct mbuf *, sa_family_t);
204 static struct netisr_handler rtsock_nh = {
206 .nh_handler = rts_input,
207 .nh_proto = NETISR_ROUTE,
208 .nh_policy = NETISR_POLICY_SOURCE,
212 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
216 netisr_getqlimit(&rtsock_nh, &qlimit);
217 error = sysctl_handle_int(oidp, &qlimit, 0, req);
218 if (error || !req->newptr)
222 return (netisr_setqlimit(&rtsock_nh, qlimit));
224 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
225 0, 0, sysctl_route_netisr_maxqlen, "I",
226 "maximum routing socket dispatch queue length");
233 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
234 rtsock_nh.nh_qlimit = tmp;
235 netisr_register(&rtsock_nh);
237 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
240 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
245 KASSERT(m != NULL, ("%s: m is NULL", __func__));
246 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
247 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
249 /* No filtering requested. */
250 if ((m->m_flags & RTS_FILTER_FIB) == 0)
253 /* Check if it is a rts and the fib matches the one of the socket. */
254 fibnum = M_GETFIB(m);
255 if (proto->sp_family != PF_ROUTE ||
256 rp->rcb_socket == NULL ||
257 rp->rcb_socket->so_fibnum == fibnum)
260 /* Filtering requested and no match, the socket shall be skipped. */
265 rts_input(struct mbuf *m)
267 struct sockproto route_proto;
268 unsigned short *family;
271 route_proto.sp_family = PF_ROUTE;
272 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
274 family = (unsigned short *)(tag + 1);
275 route_proto.sp_protocol = *family;
276 m_tag_delete(m, tag);
278 route_proto.sp_protocol = 0;
280 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
284 * It really doesn't make any sense at all for this code to share much
285 * with raw_usrreq.c, since its functionality is so restricted. XXX
288 rts_abort(struct socket *so)
291 raw_usrreqs.pru_abort(so);
295 rts_close(struct socket *so)
298 raw_usrreqs.pru_close(so);
301 /* pru_accept is EOPNOTSUPP */
304 rts_attach(struct socket *so, int proto, struct thread *td)
309 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
312 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
316 so->so_pcb = (caddr_t)rp;
317 so->so_fibnum = td->td_proc->p_fibnum;
318 error = raw_attach(so, proto);
326 switch(rp->rcb_proto.sp_protocol) {
331 route_cb.ip6_count++;
334 route_cb.ipx_count++;
337 route_cb.any_count++;
340 so->so_options |= SO_USELOOPBACK;
345 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
348 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
352 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
355 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
358 /* pru_connect2 is EOPNOTSUPP */
359 /* pru_control is EOPNOTSUPP */
362 rts_detach(struct socket *so)
364 struct rawcb *rp = sotorawcb(so);
366 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
369 switch(rp->rcb_proto.sp_protocol) {
374 route_cb.ip6_count--;
377 route_cb.ipx_count--;
380 route_cb.any_count--;
382 raw_usrreqs.pru_detach(so);
386 rts_disconnect(struct socket *so)
389 return (raw_usrreqs.pru_disconnect(so));
392 /* pru_listen is EOPNOTSUPP */
395 rts_peeraddr(struct socket *so, struct sockaddr **nam)
398 return (raw_usrreqs.pru_peeraddr(so, nam));
401 /* pru_rcvd is EOPNOTSUPP */
402 /* pru_rcvoob is EOPNOTSUPP */
405 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
406 struct mbuf *control, struct thread *td)
409 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
412 /* pru_sense is null */
415 rts_shutdown(struct socket *so)
418 return (raw_usrreqs.pru_shutdown(so));
422 rts_sockaddr(struct socket *so, struct sockaddr **nam)
425 return (raw_usrreqs.pru_sockaddr(so, nam));
428 static struct pr_usrreqs route_usrreqs = {
429 .pru_abort = rts_abort,
430 .pru_attach = rts_attach,
431 .pru_bind = rts_bind,
432 .pru_connect = rts_connect,
433 .pru_detach = rts_detach,
434 .pru_disconnect = rts_disconnect,
435 .pru_peeraddr = rts_peeraddr,
436 .pru_send = rts_send,
437 .pru_shutdown = rts_shutdown,
438 .pru_sockaddr = rts_sockaddr,
439 .pru_close = rts_close,
442 #ifndef _SOCKADDR_UNION_DEFINED
443 #define _SOCKADDR_UNION_DEFINED
445 * The union of all possible address formats we handle.
447 union sockaddr_union {
449 struct sockaddr_in sin;
450 struct sockaddr_in6 sin6;
452 #endif /* _SOCKADDR_UNION_DEFINED */
455 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
456 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
459 /* First, see if the returned address is part of the jail. */
460 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
461 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
465 switch (info->rti_info[RTAX_DST]->sa_family) {
475 * Try to find an address on the given outgoing interface
476 * that belongs to the jail.
479 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
482 if (sa->sa_family != AF_INET)
484 ia = ((struct sockaddr_in *)sa)->sin_addr;
485 if (prison_check_ip4(cred, &ia) == 0) {
490 IF_ADDR_RUNLOCK(ifp);
493 * As a last resort return the 'default' jail address.
495 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
497 if (prison_get_ip4(cred, &ia) != 0)
500 bzero(&saun->sin, sizeof(struct sockaddr_in));
501 saun->sin.sin_len = sizeof(struct sockaddr_in);
502 saun->sin.sin_family = AF_INET;
503 saun->sin.sin_addr.s_addr = ia.s_addr;
504 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
517 * Try to find an address on the given outgoing interface
518 * that belongs to the jail.
521 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
524 if (sa->sa_family != AF_INET6)
526 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
527 &ia6, sizeof(struct in6_addr));
528 if (prison_check_ip6(cred, &ia6) == 0) {
533 IF_ADDR_RUNLOCK(ifp);
536 * As a last resort return the 'default' jail address.
538 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
540 if (prison_get_ip6(cred, &ia6) != 0)
543 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
544 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
545 saun->sin6.sin6_family = AF_INET6;
546 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
547 if (sa6_recoverscope(&saun->sin6) != 0)
549 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
561 route_output(struct mbuf *m, struct socket *so)
563 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
564 struct rt_msghdr *rtm = NULL;
565 struct rtentry *rt = NULL;
566 struct radix_node_head *rnh;
567 struct rt_addrinfo info;
569 struct sockaddr_storage ss;
570 struct sockaddr_in6 *sin6;
571 int i, rti_need_deembed = 0;
574 struct ifnet *ifp = NULL;
575 union sockaddr_union saun;
576 sa_family_t saf = AF_UNSPEC;
578 #define senderr(e) { error = e; goto flush;}
579 if (m == NULL || ((m->m_len < sizeof(long)) &&
580 (m = m_pullup(m, sizeof(long))) == NULL))
582 if ((m->m_flags & M_PKTHDR) == 0)
583 panic("route_output");
584 len = m->m_pkthdr.len;
585 if (len < sizeof(*rtm) ||
586 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
587 info.rti_info[RTAX_DST] = NULL;
590 R_Malloc(rtm, struct rt_msghdr *, len);
592 info.rti_info[RTAX_DST] = NULL;
595 m_copydata(m, 0, len, (caddr_t)rtm);
596 if (rtm->rtm_version != RTM_VERSION) {
597 info.rti_info[RTAX_DST] = NULL;
598 senderr(EPROTONOSUPPORT);
600 rtm->rtm_pid = curproc->p_pid;
601 bzero(&info, sizeof(info));
602 info.rti_addrs = rtm->rtm_addrs;
604 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
605 * link-local address because rtrequest requires addresses with
608 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
609 info.rti_info[RTAX_DST] = NULL;
612 info.rti_flags = rtm->rtm_flags;
613 if (info.rti_info[RTAX_DST] == NULL ||
614 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
615 (info.rti_info[RTAX_GATEWAY] != NULL &&
616 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
618 saf = info.rti_info[RTAX_DST]->sa_family;
620 * Verify that the caller has the appropriate privilege; RTM_GET
621 * is the only operation the non-superuser is allowed.
623 if (rtm->rtm_type != RTM_GET) {
624 error = priv_check(curthread, PRIV_NET_ROUTE);
630 * The given gateway address may be an interface address.
631 * For example, issuing a "route change" command on a route
632 * entry that was created from a tunnel, and the gateway
633 * address given is the local end point. In this case the
634 * RTF_GATEWAY flag must be cleared or the destination will
635 * not be reachable even though there is no error message.
637 if (info.rti_info[RTAX_GATEWAY] != NULL &&
638 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
641 bzero(&gw_ro, sizeof(gw_ro));
642 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
643 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
645 * A host route through the loopback interface is
646 * installed for each interface adddress. In pre 8.0
647 * releases the interface address of a PPP link type
648 * is not reachable locally. This behavior is fixed as
649 * part of the new L2/L3 redesign and rewrite work. The
650 * signature of this interface address route is the
651 * AF_LINK sa_family type of the rt_gateway, and the
652 * rt_ifp has the IFF_LOOPBACK flag set.
654 if (gw_ro.ro_rt != NULL &&
655 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
656 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
657 info.rti_flags &= ~RTF_GATEWAY;
658 info.rti_flags |= RTF_GWFLAG_COMPAT;
660 if (gw_ro.ro_rt != NULL)
664 switch (rtm->rtm_type) {
665 struct rtentry *saved_nrt;
668 if (info.rti_info[RTAX_GATEWAY] == NULL)
672 /* support for new ARP code */
673 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
674 (rtm->rtm_flags & RTF_LLDATA) != 0) {
675 error = lla_rt_output(rtm, &info);
678 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
682 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
684 if (error == 0 && saved_nrt) {
686 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
689 rt_setmetrics(rtm->rtm_inits,
690 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
691 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
692 RT_REMREF(saved_nrt);
693 RT_UNLOCK(saved_nrt);
699 /* support for new ARP code */
700 if (info.rti_info[RTAX_GATEWAY] &&
701 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
702 (rtm->rtm_flags & RTF_LLDATA) != 0) {
703 error = lla_rt_output(rtm, &info);
706 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
710 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
718 /* rt_msg2() will not be used when RTM_DELETE fails. */
719 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
726 rnh = rt_tables_get_rnh(so->so_fibnum,
727 info.rti_info[RTAX_DST]->sa_family);
729 senderr(EAFNOSUPPORT);
730 RADIX_NODE_HEAD_RLOCK(rnh);
731 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
732 info.rti_info[RTAX_NETMASK], rnh);
733 if (rt == NULL) { /* XXX looks bogus */
734 RADIX_NODE_HEAD_RUNLOCK(rnh);
739 * for RTM_CHANGE/LOCK, if we got multipath routes,
740 * we require users to specify a matching RTAX_GATEWAY.
742 * for RTM_GET, gate is optional even with multipath.
743 * if gate == NULL the first match is returned.
744 * (no need to call rt_mpath_matchgate if gate == NULL)
746 if (rn_mpath_capable(rnh) &&
747 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
748 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
750 RADIX_NODE_HEAD_RUNLOCK(rnh);
756 * If performing proxied L2 entry insertion, and
757 * the actual PPP host entry is found, perform
758 * another search to retrieve the prefix route of
759 * the local end point of the PPP link.
761 if (rtm->rtm_flags & RTF_ANNOUNCE) {
762 struct sockaddr laddr;
764 if (rt->rt_ifp != NULL &&
765 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
768 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
770 rt_maskedcopy(ifa->ifa_addr,
774 rt_maskedcopy(rt->rt_ifa->ifa_addr,
776 rt->rt_ifa->ifa_netmask);
778 * refactor rt and no lock operation necessary
780 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
782 RADIX_NODE_HEAD_RUNLOCK(rnh);
788 RADIX_NODE_HEAD_RUNLOCK(rnh);
793 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
794 * returns a perfect match in case a netmask is
795 * specified. For host routes only a longest prefix
796 * match is returned so it is necessary to compare the
797 * existence of the netmask. If both have a netmask
798 * rnh_lookup() did a perfect match and if none of them
799 * have a netmask both are host routes which is also a
803 if (rtm->rtm_type != RTM_GET &&
804 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
809 switch(rtm->rtm_type) {
814 if ((rt->rt_flags & RTF_HOST) == 0
815 ? jailed_without_vnet(curthread->td_ucred)
816 : prison_if(curthread->td_ucred,
821 info.rti_info[RTAX_DST] = rt_key(rt);
822 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
823 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
824 info.rti_info[RTAX_GENMASK] = 0;
825 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
828 info.rti_info[RTAX_IFP] =
829 ifp->if_addr->ifa_addr;
830 error = rtm_get_jailed(&info, ifp, rt,
831 &saun, curthread->td_ucred);
836 if (ifp->if_flags & IFF_POINTOPOINT)
837 info.rti_info[RTAX_BRD] =
838 rt->rt_ifa->ifa_dstaddr;
839 rtm->rtm_index = ifp->if_index;
841 info.rti_info[RTAX_IFP] = NULL;
842 info.rti_info[RTAX_IFA] = NULL;
844 } else if ((ifp = rt->rt_ifp) != NULL) {
845 rtm->rtm_index = ifp->if_index;
847 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
848 if (len > rtm->rtm_msglen) {
849 struct rt_msghdr *new_rtm;
850 R_Malloc(new_rtm, struct rt_msghdr *, len);
851 if (new_rtm == NULL) {
855 bcopy(rtm, new_rtm, rtm->rtm_msglen);
856 Free(rtm); rtm = new_rtm;
858 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
859 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
860 rtm->rtm_flags = RTF_GATEWAY |
861 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
863 rtm->rtm_flags = rt->rt_flags;
864 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
865 rtm->rtm_addrs = info.rti_addrs;
870 * New gateway could require new ifaddr, ifp;
871 * flags may also be different; ifp may be specified
872 * by ll sockaddr when protocol address is ambiguous
874 if (((rt->rt_flags & RTF_GATEWAY) &&
875 info.rti_info[RTAX_GATEWAY] != NULL) ||
876 info.rti_info[RTAX_IFP] != NULL ||
877 (info.rti_info[RTAX_IFA] != NULL &&
878 !sa_equal(info.rti_info[RTAX_IFA],
879 rt->rt_ifa->ifa_addr))) {
881 RADIX_NODE_HEAD_LOCK(rnh);
882 error = rt_getifa_fib(&info, rt->rt_fibnum);
884 * XXXRW: Really we should release this
885 * reference later, but this maintains
886 * historical behavior.
888 if (info.rti_ifa != NULL)
889 ifa_free(info.rti_ifa);
890 RADIX_NODE_HEAD_UNLOCK(rnh);
895 if (info.rti_ifa != NULL &&
896 info.rti_ifa != rt->rt_ifa &&
897 rt->rt_ifa != NULL &&
898 rt->rt_ifa->ifa_rtrequest != NULL) {
899 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
901 ifa_free(rt->rt_ifa);
903 if (info.rti_info[RTAX_GATEWAY] != NULL) {
905 RADIX_NODE_HEAD_LOCK(rnh);
908 error = rt_setgate(rt, rt_key(rt),
909 info.rti_info[RTAX_GATEWAY]);
910 RADIX_NODE_HEAD_UNLOCK(rnh);
915 rt->rt_flags &= ~RTF_GATEWAY;
916 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
918 if (info.rti_ifa != NULL &&
919 info.rti_ifa != rt->rt_ifa) {
920 ifa_ref(info.rti_ifa);
921 rt->rt_ifa = info.rti_ifa;
922 rt->rt_ifp = info.rti_ifp;
924 /* Allow some flags to be toggled on change. */
925 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
926 (rtm->rtm_flags & RTF_FMASK);
927 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
929 rtm->rtm_index = rt->rt_ifp->if_index;
930 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
931 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
934 /* We don't support locks anymore */
947 rtm->rtm_errno = error;
949 rtm->rtm_flags |= RTF_DONE;
951 if (rt) /* XXX can this be true? */
954 struct rawcb *rp = NULL;
956 * Check to see if we don't want our own messages.
958 if ((so->so_options & SO_USELOOPBACK) == 0) {
959 if (route_cb.any_count <= 1) {
965 /* There is another listener, so construct message */
970 if (rti_need_deembed) {
971 /* sin6_scope_id is recovered before sending rtm. */
972 sin6 = (struct sockaddr_in6 *)&ss;
973 for (i = 0; i < RTAX_MAX; i++) {
974 if (info.rti_info[i] == NULL)
976 if (info.rti_info[i]->sa_family != AF_INET6)
978 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
979 if (sa6_recoverscope(sin6) == 0)
980 bcopy(sin6, info.rti_info[i],
985 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
986 if (m->m_pkthdr.len < rtm->rtm_msglen) {
989 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
990 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
993 M_SETFIB(m, so->so_fibnum);
994 m->m_flags |= RTS_FILTER_FIB;
997 * XXX insure we don't get a copy by
998 * invalidating our protocol
1000 unsigned short family = rp->rcb_proto.sp_family;
1001 rp->rcb_proto.sp_family = 0;
1002 rt_dispatch(m, saf);
1003 rp->rcb_proto.sp_family = family;
1005 rt_dispatch(m, saf);
1007 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
1016 rt_setmetrics(u_long which, const struct rt_metrics *in,
1017 struct rt_metrics_lite *out)
1019 #define metric(f, e) if (which & (f)) out->e = in->e;
1021 * Only these are stored in the routing entry since introduction
1022 * of tcp hostcache. The rest is ignored.
1024 metric(RTV_MTU, rmx_mtu);
1025 metric(RTV_WEIGHT, rmx_weight);
1026 /* Userland -> kernel timebase conversion. */
1027 if (which & RTV_EXPIRE)
1028 out->rmx_expire = in->rmx_expire ?
1029 in->rmx_expire - time_second + time_uptime : 0;
1034 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
1036 #define metric(e) out->e = in->e;
1037 bzero(out, sizeof(*out));
1040 /* Kernel -> userland timebase conversion. */
1041 out->rmx_expire = in->rmx_expire ?
1042 in->rmx_expire - time_uptime + time_second : 0;
1047 * Extract the addresses of the passed sockaddrs.
1048 * Do a little sanity checking so as to avoid bad memory references.
1049 * This data is derived straight from userland.
1052 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1054 struct sockaddr *sa;
1057 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1058 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1060 sa = (struct sockaddr *)cp;
1064 if (cp + sa->sa_len > cplim)
1067 * there are no more.. quit now
1068 * If there are more bits, they are in error.
1069 * I've seen this. route(1) can evidently generate these.
1070 * This causes kernel to core dump.
1071 * for compatibility, If we see this, point to a safe address.
1073 if (sa->sa_len == 0) {
1074 rtinfo->rti_info[i] = &sa_zero;
1075 return (0); /* should be EINVAL but for compat */
1079 if (sa->sa_family == AF_INET6)
1080 sa6_embedscope((struct sockaddr_in6 *)sa,
1083 rtinfo->rti_info[i] = sa;
1090 * Used by the routing socket.
1092 static struct mbuf *
1093 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1095 struct rt_msghdr *rtm;
1098 struct sockaddr *sa;
1100 struct sockaddr_storage ss;
1101 struct sockaddr_in6 *sin6;
1109 len = sizeof(struct ifa_msghdr);
1114 len = sizeof(struct ifma_msghdr);
1118 len = sizeof(struct if_msghdr);
1121 case RTM_IFANNOUNCE:
1123 len = sizeof(struct if_announcemsghdr);
1127 len = sizeof(struct rt_msghdr);
1130 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1131 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1133 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1135 m = m_gethdr(M_NOWAIT, MT_DATA);
1139 m->m_pkthdr.len = m->m_len = len;
1140 rtm = mtod(m, struct rt_msghdr *);
1141 bzero((caddr_t)rtm, len);
1142 for (i = 0; i < RTAX_MAX; i++) {
1143 if ((sa = rtinfo->rti_info[i]) == NULL)
1145 rtinfo->rti_addrs |= (1 << i);
1148 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1149 sin6 = (struct sockaddr_in6 *)&ss;
1150 bcopy(sa, sin6, sizeof(*sin6));
1151 if (sa6_recoverscope(sin6) == 0)
1152 sa = (struct sockaddr *)sin6;
1155 m_copyback(m, len, dlen, (caddr_t)sa);
1158 if (m->m_pkthdr.len != len) {
1162 rtm->rtm_msglen = len;
1163 rtm->rtm_version = RTM_VERSION;
1164 rtm->rtm_type = type;
1169 * Used by the sysctl code and routing socket.
1172 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1175 int len, dlen, second_time = 0;
1178 struct sockaddr_storage ss;
1179 struct sockaddr_in6 *sin6;
1182 rtinfo->rti_addrs = 0;
1188 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1189 #ifdef COMPAT_FREEBSD32
1190 if (w->w_req->flags & SCTL_MASK32)
1191 len = sizeof(struct ifa_msghdrl32);
1194 len = sizeof(struct ifa_msghdrl);
1196 len = sizeof(struct ifa_msghdr);
1200 #ifdef COMPAT_FREEBSD32
1201 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1202 if (w->w_op == NET_RT_IFLISTL)
1203 len = sizeof(struct if_msghdrl32);
1205 len = sizeof(struct if_msghdr32);
1209 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1210 len = sizeof(struct if_msghdrl);
1212 len = sizeof(struct if_msghdr);
1216 len = sizeof(struct ifma_msghdr);
1220 len = sizeof(struct rt_msghdr);
1225 for (i = 0; i < RTAX_MAX; i++) {
1226 struct sockaddr *sa;
1228 if ((sa = rtinfo->rti_info[i]) == NULL)
1230 rtinfo->rti_addrs |= (1 << i);
1234 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1235 sin6 = (struct sockaddr_in6 *)&ss;
1236 bcopy(sa, sin6, sizeof(*sin6));
1237 if (sa6_recoverscope(sin6) == 0)
1238 sa = (struct sockaddr *)sin6;
1241 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1247 if (cp == NULL && w != NULL && !second_time) {
1248 struct walkarg *rw = w;
1251 if (rw->w_tmemsize < len) {
1253 free(rw->w_tmem, M_RTABLE);
1254 rw->w_tmem = (caddr_t)
1255 malloc(len, M_RTABLE, M_NOWAIT);
1257 rw->w_tmemsize = len;
1267 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1269 rtm->rtm_version = RTM_VERSION;
1270 rtm->rtm_type = type;
1271 rtm->rtm_msglen = len;
1277 * This routine is called to generate a message from the routing
1278 * socket indicating that a redirect has occured, a routing lookup
1279 * has failed, or that a protocol has detected timeouts to a particular
1283 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1286 struct rt_msghdr *rtm;
1288 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1290 if (route_cb.any_count == 0)
1292 m = rt_msg1(type, rtinfo);
1296 if (fibnum != RTS_ALLFIBS) {
1297 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1298 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1299 M_SETFIB(m, fibnum);
1300 m->m_flags |= RTS_FILTER_FIB;
1303 rtm = mtod(m, struct rt_msghdr *);
1304 rtm->rtm_flags = RTF_DONE | flags;
1305 rtm->rtm_errno = error;
1306 rtm->rtm_addrs = rtinfo->rti_addrs;
1307 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1311 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1314 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1318 * This routine is called to generate a message from the routing
1319 * socket indicating that the status of a network interface has changed.
1322 rt_ifmsg(struct ifnet *ifp)
1324 struct if_msghdr *ifm;
1326 struct rt_addrinfo info;
1328 if (route_cb.any_count == 0)
1330 bzero((caddr_t)&info, sizeof(info));
1331 m = rt_msg1(RTM_IFINFO, &info);
1334 ifm = mtod(m, struct if_msghdr *);
1335 ifm->ifm_index = ifp->if_index;
1336 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1337 ifm->ifm_data = ifp->if_data;
1339 rt_dispatch(m, AF_UNSPEC);
1343 * This is called to generate messages from the routing socket
1344 * indicating a network interface has had addresses associated with it.
1345 * if we ever reverse the logic and replace messages TO the routing
1346 * socket indicate a request to configure interfaces, then it will
1347 * be unnecessary as the routing socket will automatically generate
1351 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1354 struct rt_addrinfo info;
1355 struct sockaddr *sa = NULL;
1357 struct mbuf *m = NULL;
1358 struct ifnet *ifp = ifa->ifa_ifp;
1360 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1361 ("unexpected cmd %u", cmd));
1362 #if defined(INET) || defined(INET6)
1365 * notify the SCTP stack
1366 * this will only get called when an address is added/deleted
1367 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1369 sctp_addr_change(ifa, cmd);
1372 if (route_cb.any_count == 0)
1374 for (pass = 1; pass < 3; pass++) {
1375 bzero((caddr_t)&info, sizeof(info));
1376 if ((cmd == RTM_ADD && pass == 1) ||
1377 (cmd == RTM_DELETE && pass == 2)) {
1378 struct ifa_msghdr *ifam;
1379 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1381 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1382 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1383 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1384 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1385 if ((m = rt_msg1(ncmd, &info)) == NULL)
1387 ifam = mtod(m, struct ifa_msghdr *);
1388 ifam->ifam_index = ifp->if_index;
1389 ifam->ifam_metric = ifa->ifa_metric;
1390 ifam->ifam_flags = ifa->ifa_flags;
1391 ifam->ifam_addrs = info.rti_addrs;
1393 if ((cmd == RTM_ADD && pass == 2) ||
1394 (cmd == RTM_DELETE && pass == 1)) {
1395 struct rt_msghdr *rtm;
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 != RTS_ALLFIBS) {
1411 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1412 "fibnum out of range 0 <= %d < %d", __func__,
1413 fibnum, rt_numfibs));
1414 M_SETFIB(m, fibnum);
1415 m->m_flags |= RTS_FILTER_FIB;
1417 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1422 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1425 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1429 * This is the analogue to the rt_newaddrmsg which performs the same
1430 * function but for multicast group memberhips. This is easier since
1431 * there is no route state to worry about.
1434 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1436 struct rt_addrinfo info;
1437 struct mbuf *m = NULL;
1438 struct ifnet *ifp = ifma->ifma_ifp;
1439 struct ifma_msghdr *ifmam;
1441 if (route_cb.any_count == 0)
1444 bzero((caddr_t)&info, sizeof(info));
1445 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1446 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1448 * If a link-layer address is present, present it as a ``gateway''
1449 * (similarly to how ARP entries, e.g., are presented).
1451 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1452 m = rt_msg1(cmd, &info);
1455 ifmam = mtod(m, struct ifma_msghdr *);
1456 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1458 ifmam->ifmam_index = ifp->if_index;
1459 ifmam->ifmam_addrs = info.rti_addrs;
1460 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1463 static struct mbuf *
1464 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1465 struct rt_addrinfo *info)
1467 struct if_announcemsghdr *ifan;
1470 if (route_cb.any_count == 0)
1472 bzero((caddr_t)info, sizeof(*info));
1473 m = rt_msg1(type, info);
1475 ifan = mtod(m, struct if_announcemsghdr *);
1476 ifan->ifan_index = ifp->if_index;
1477 strlcpy(ifan->ifan_name, ifp->if_xname,
1478 sizeof(ifan->ifan_name));
1479 ifan->ifan_what = what;
1485 * This is called to generate routing socket messages indicating
1486 * IEEE80211 wireless events.
1487 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1490 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1493 struct rt_addrinfo info;
1495 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1498 * Append the ieee80211 data. Try to stick it in the
1499 * mbuf containing the ifannounce msg; otherwise allocate
1500 * a new mbuf and append.
1502 * NB: we assume m is a single mbuf.
1504 if (data_len > M_TRAILINGSPACE(m)) {
1505 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1510 bcopy(data, mtod(n, void *), data_len);
1511 n->m_len = data_len;
1513 } else if (data_len > 0) {
1514 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1515 m->m_len += data_len;
1517 if (m->m_flags & M_PKTHDR)
1518 m->m_pkthdr.len += data_len;
1519 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1520 rt_dispatch(m, AF_UNSPEC);
1525 * This is called to generate routing socket messages indicating
1526 * network interface arrival and departure.
1529 rt_ifannouncemsg(struct ifnet *ifp, int what)
1532 struct rt_addrinfo info;
1534 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1536 rt_dispatch(m, AF_UNSPEC);
1540 rt_dispatch(struct mbuf *m, sa_family_t saf)
1545 * Preserve the family from the sockaddr, if any, in an m_tag for
1546 * use when injecting the mbuf into the routing socket buffer from
1549 if (saf != AF_UNSPEC) {
1550 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1556 *(unsigned short *)(tag + 1) = saf;
1557 m_tag_prepend(m, tag);
1561 m->m_pkthdr.rcvif = V_loif;
1567 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1571 * This is used in dumping the kernel table via sysctl().
1574 sysctl_dumpentry(struct radix_node *rn, void *vw)
1576 struct walkarg *w = vw;
1577 struct rtentry *rt = (struct rtentry *)rn;
1578 int error = 0, size;
1579 struct rt_addrinfo info;
1581 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1583 if ((rt->rt_flags & RTF_HOST) == 0
1584 ? jailed_without_vnet(w->w_req->td->td_ucred)
1585 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1587 bzero((caddr_t)&info, sizeof(info));
1588 info.rti_info[RTAX_DST] = rt_key(rt);
1589 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1590 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1591 info.rti_info[RTAX_GENMASK] = 0;
1593 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1594 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1595 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1596 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1598 size = rt_msg2(RTM_GET, &info, NULL, w);
1599 if (w->w_req && w->w_tmem) {
1600 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1602 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1603 rtm->rtm_flags = RTF_GATEWAY |
1604 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1606 rtm->rtm_flags = rt->rt_flags;
1608 * let's be honest about this being a retarded hack
1610 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1611 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1612 rtm->rtm_index = rt->rt_ifp->if_index;
1613 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1614 rtm->rtm_addrs = info.rti_addrs;
1615 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1621 #ifdef COMPAT_FREEBSD32
1623 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1626 bzero(dst, sizeof(*dst));
1627 CP(*src, *dst, ifi_type);
1628 CP(*src, *dst, ifi_physical);
1629 CP(*src, *dst, ifi_addrlen);
1630 CP(*src, *dst, ifi_hdrlen);
1631 CP(*src, *dst, ifi_link_state);
1632 CP(*src, *dst, ifi_vhid);
1633 CP(*src, *dst, ifi_baudrate_pf);
1634 dst->ifi_datalen = sizeof(struct if_data32);
1635 CP(*src, *dst, ifi_mtu);
1636 CP(*src, *dst, ifi_metric);
1637 CP(*src, *dst, ifi_baudrate);
1638 CP(*src, *dst, ifi_ipackets);
1639 CP(*src, *dst, ifi_ierrors);
1640 CP(*src, *dst, ifi_opackets);
1641 CP(*src, *dst, ifi_oerrors);
1642 CP(*src, *dst, ifi_collisions);
1643 CP(*src, *dst, ifi_ibytes);
1644 CP(*src, *dst, ifi_obytes);
1645 CP(*src, *dst, ifi_imcasts);
1646 CP(*src, *dst, ifi_omcasts);
1647 CP(*src, *dst, ifi_iqdrops);
1648 CP(*src, *dst, ifi_noproto);
1649 CP(*src, *dst, ifi_hwassist);
1650 CP(*src, *dst, ifi_epoch);
1651 TV_CP(*src, *dst, ifi_lastchange);
1656 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1657 struct walkarg *w, int len)
1659 struct if_msghdrl *ifm;
1661 #ifdef COMPAT_FREEBSD32
1662 if (w->w_req->flags & SCTL_MASK32) {
1663 struct if_msghdrl32 *ifm32;
1665 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1666 ifm32->ifm_addrs = info->rti_addrs;
1667 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1668 ifm32->ifm_index = ifp->if_index;
1669 ifm32->_ifm_spare1 = 0;
1670 ifm32->ifm_len = sizeof(*ifm32);
1671 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1673 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1674 /* Fixup if_data carp(4) vhid. */
1675 if (carp_get_vhid_p != NULL)
1676 ifm32->ifm_data.ifi_vhid =
1677 (*carp_get_vhid_p)(ifp->if_addr);
1679 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1682 ifm = (struct if_msghdrl *)w->w_tmem;
1683 ifm->ifm_addrs = info->rti_addrs;
1684 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1685 ifm->ifm_index = ifp->if_index;
1686 ifm->_ifm_spare1 = 0;
1687 ifm->ifm_len = sizeof(*ifm);
1688 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1690 ifm->ifm_data = ifp->if_data;
1691 /* Fixup if_data carp(4) vhid. */
1692 if (carp_get_vhid_p != NULL)
1693 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1695 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1699 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1700 struct walkarg *w, int len)
1702 struct if_msghdr *ifm;
1704 #ifdef COMPAT_FREEBSD32
1705 if (w->w_req->flags & SCTL_MASK32) {
1706 struct if_msghdr32 *ifm32;
1708 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1709 ifm32->ifm_addrs = info->rti_addrs;
1710 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1711 ifm32->ifm_index = ifp->if_index;
1713 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1714 /* Fixup if_data carp(4) vhid. */
1715 if (carp_get_vhid_p != NULL)
1716 ifm32->ifm_data.ifi_vhid =
1717 (*carp_get_vhid_p)(ifp->if_addr);
1719 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1722 ifm = (struct if_msghdr *)w->w_tmem;
1723 ifm->ifm_addrs = info->rti_addrs;
1724 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1725 ifm->ifm_index = ifp->if_index;
1727 ifm->ifm_data = ifp->if_data;
1728 /* Fixup if_data carp(4) vhid. */
1729 if (carp_get_vhid_p != NULL)
1730 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1732 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1736 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1737 struct walkarg *w, int len)
1739 struct ifa_msghdrl *ifam;
1741 #ifdef COMPAT_FREEBSD32
1742 if (w->w_req->flags & SCTL_MASK32) {
1743 struct ifa_msghdrl32 *ifam32;
1745 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1746 ifam32->ifam_addrs = info->rti_addrs;
1747 ifam32->ifam_flags = ifa->ifa_flags;
1748 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1749 ifam32->_ifam_spare1 = 0;
1750 ifam32->ifam_len = sizeof(*ifam32);
1751 ifam32->ifam_data_off =
1752 offsetof(struct ifa_msghdrl32, ifam_data);
1753 ifam32->ifam_metric = ifa->ifa_metric;
1755 bzero(&ifam32->ifam_data, sizeof(ifam32->ifam_data));
1756 ifam32->ifam_data.ifi_datalen = sizeof(struct if_data32);
1757 ifam32->ifam_data.ifi_ipackets =
1758 counter_u64_fetch(ifa->ifa_ipackets);
1759 ifam32->ifam_data.ifi_opackets =
1760 counter_u64_fetch(ifa->ifa_opackets);
1761 ifam32->ifam_data.ifi_ibytes =
1762 counter_u64_fetch(ifa->ifa_ibytes);
1763 ifam32->ifam_data.ifi_obytes =
1764 counter_u64_fetch(ifa->ifa_obytes);
1766 /* Fixup if_data carp(4) vhid. */
1767 if (carp_get_vhid_p != NULL)
1768 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1770 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1774 ifam = (struct ifa_msghdrl *)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_spare1 = 0;
1779 ifam->ifam_len = sizeof(*ifam);
1780 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1781 ifam->ifam_metric = ifa->ifa_metric;
1783 bzero(&ifam->ifam_data, sizeof(ifam->ifam_data));
1784 ifam->ifam_data.ifi_datalen = sizeof(struct if_data);
1785 ifam->ifam_data.ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1786 ifam->ifam_data.ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1787 ifam->ifam_data.ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1788 ifam->ifam_data.ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1790 /* Fixup if_data carp(4) vhid. */
1791 if (carp_get_vhid_p != NULL)
1792 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1794 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1798 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1799 struct walkarg *w, int len)
1801 struct ifa_msghdr *ifam;
1803 ifam = (struct ifa_msghdr *)w->w_tmem;
1804 ifam->ifam_addrs = info->rti_addrs;
1805 ifam->ifam_flags = ifa->ifa_flags;
1806 ifam->ifam_index = ifa->ifa_ifp->if_index;
1807 ifam->ifam_metric = ifa->ifa_metric;
1809 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1813 sysctl_iflist(int af, struct walkarg *w)
1817 struct rt_addrinfo info;
1820 bzero((caddr_t)&info, sizeof(info));
1821 IFNET_RLOCK_NOSLEEP();
1822 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1823 if (w->w_arg && w->w_arg != ifp->if_index)
1827 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1828 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1829 info.rti_info[RTAX_IFP] = NULL;
1830 if (w->w_req && w->w_tmem) {
1831 if (w->w_op == NET_RT_IFLISTL)
1832 error = sysctl_iflist_ifml(ifp, &info, w, len);
1834 error = sysctl_iflist_ifm(ifp, &info, w, len);
1838 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1839 if (af && af != ifa->ifa_addr->sa_family)
1841 if (prison_if(w->w_req->td->td_ucred,
1842 ifa->ifa_addr) != 0)
1844 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1845 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1846 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1847 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1848 if (w->w_req && w->w_tmem) {
1849 if (w->w_op == NET_RT_IFLISTL)
1850 error = sysctl_iflist_ifaml(ifa, &info,
1853 error = sysctl_iflist_ifam(ifa, &info,
1859 IF_ADDR_RUNLOCK(ifp);
1860 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1861 info.rti_info[RTAX_BRD] = NULL;
1865 IF_ADDR_RUNLOCK(ifp);
1866 IFNET_RUNLOCK_NOSLEEP();
1871 sysctl_ifmalist(int af, struct walkarg *w)
1874 struct ifmultiaddr *ifma;
1875 struct rt_addrinfo info;
1879 bzero((caddr_t)&info, sizeof(info));
1880 IFNET_RLOCK_NOSLEEP();
1881 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1882 if (w->w_arg && w->w_arg != ifp->if_index)
1885 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1887 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1888 if (af && af != ifma->ifma_addr->sa_family)
1890 if (prison_if(w->w_req->td->td_ucred,
1891 ifma->ifma_addr) != 0)
1893 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1894 info.rti_info[RTAX_GATEWAY] =
1895 (ifma->ifma_addr->sa_family != AF_LINK) ?
1896 ifma->ifma_lladdr : NULL;
1897 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1898 if (w->w_req && w->w_tmem) {
1899 struct ifma_msghdr *ifmam;
1901 ifmam = (struct ifma_msghdr *)w->w_tmem;
1902 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1903 ifmam->ifmam_flags = 0;
1904 ifmam->ifmam_addrs = info.rti_addrs;
1905 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1907 IF_ADDR_RUNLOCK(ifp);
1912 IF_ADDR_RUNLOCK(ifp);
1915 IFNET_RUNLOCK_NOSLEEP();
1920 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1922 int *name = (int *)arg1;
1923 u_int namelen = arg2;
1924 struct radix_node_head *rnh = NULL; /* silence compiler. */
1925 int i, lim, error = EINVAL;
1934 if (name[1] == NET_RT_DUMP) {
1936 fib = req->td->td_proc->p_fibnum;
1937 else if (namelen == 4)
1938 fib = (name[3] == -1) ?
1939 req->td->td_proc->p_fibnum : name[3];
1941 return ((namelen < 3) ? EISDIR : ENOTDIR);
1942 if (fib < 0 || fib >= rt_numfibs)
1944 } else if (namelen != 3)
1945 return ((namelen < 3) ? EISDIR : ENOTDIR);
1949 bzero(&w, sizeof(w));
1954 error = sysctl_wire_old_buffer(req, 0);
1961 if (af == 0) { /* dump all tables */
1964 } else /* dump only one table */
1968 * take care of llinfo entries, the caller must
1971 if (w.w_op == NET_RT_FLAGS &&
1972 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1974 error = lltable_sysctl_dumparp(af, w.w_req);
1980 * take care of routing entries
1982 for (error = 0; error == 0 && i <= lim; i++) {
1983 rnh = rt_tables_get_rnh(fib, i);
1985 RADIX_NODE_HEAD_RLOCK(rnh);
1986 error = rnh->rnh_walktree(rnh,
1987 sysctl_dumpentry, &w);
1988 RADIX_NODE_HEAD_RUNLOCK(rnh);
1990 error = EAFNOSUPPORT;
1995 case NET_RT_IFLISTL:
1996 error = sysctl_iflist(af, &w);
1999 case NET_RT_IFMALIST:
2000 error = sysctl_ifmalist(af, &w);
2004 free(w.w_tmem, M_RTABLE);
2008 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
2011 * Definitions of protocols supported in the ROUTE domain.
2014 static struct domain routedomain; /* or at least forward */
2016 static struct protosw routesw[] = {
2018 .pr_type = SOCK_RAW,
2019 .pr_domain = &routedomain,
2020 .pr_flags = PR_ATOMIC|PR_ADDR,
2021 .pr_output = route_output,
2022 .pr_ctlinput = raw_ctlinput,
2023 .pr_init = raw_init,
2024 .pr_usrreqs = &route_usrreqs
2028 static struct domain routedomain = {
2029 .dom_family = PF_ROUTE,
2030 .dom_name = "route",
2031 .dom_protosw = routesw,
2032 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
2035 VNET_DOMAIN_SET(route);