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_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 #if defined(INET) || defined(INET6)
74 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
78 #ifdef COMPAT_FREEBSD32
79 #include <sys/mount.h>
80 #include <compat/freebsd32/freebsd32.h>
87 uint8_t ifi_link_state;
89 uint8_t ifi_baudrate_pf;
93 uint32_t ifi_baudrate;
94 uint32_t ifi_ipackets;
96 uint32_t ifi_opackets;
98 uint32_t ifi_collisions;
101 uint32_t ifi_imcasts;
102 uint32_t ifi_omcasts;
103 uint32_t ifi_iqdrops;
104 uint32_t ifi_noproto;
105 uint32_t ifi_hwassist;
107 struct timeval32 ifi_lastchange;
117 struct if_data32 ifm_data;
120 struct if_msghdrl32 {
127 uint16_t _ifm_spare1;
129 uint16_t ifm_data_off;
130 struct if_data32 ifm_data;
133 struct ifa_msghdrl32 {
134 uint16_t ifam_msglen;
135 uint8_t ifam_version;
140 uint16_t _ifam_spare1;
142 uint16_t ifam_data_off;
144 struct if_data32 ifam_data;
146 #endif /* COMPAT_FREEBSD32 */
148 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
150 /* NB: these are not modified */
151 static struct sockaddr route_src = { 2, PF_ROUTE, };
152 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
154 /* These are external hooks for CARP. */
155 int (*carp_get_vhid_p)(struct ifaddr *);
158 * Used by rtsock/raw_input callback code to decide whether to filter the update
159 * notification to a socket bound to a particular FIB.
161 #define RTS_FILTER_FIB M_PROTO8
162 #define RTS_ALLFIBS -1
165 int ip_count; /* attached w/ AF_INET */
166 int ip6_count; /* attached w/ AF_INET6 */
167 int ipx_count; /* attached w/ AF_IPX */
168 int any_count; /* total attached */
171 struct mtx rtsock_mtx;
172 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
174 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
175 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
176 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
178 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
184 struct sysctl_req *w_req;
187 static void rts_input(struct mbuf *m);
188 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
189 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
190 caddr_t cp, struct walkarg *w);
191 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
192 struct rt_addrinfo *rtinfo);
193 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
194 static int sysctl_iflist(int af, struct walkarg *w);
195 static int sysctl_ifmalist(int af, struct walkarg *w);
196 static int route_output(struct mbuf *m, struct socket *so);
197 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
198 struct rt_metrics_lite *out);
199 static void rt_getmetrics(const struct rt_metrics_lite *in,
200 struct rt_metrics *out);
201 static void rt_dispatch(struct mbuf *, sa_family_t);
203 static struct netisr_handler rtsock_nh = {
205 .nh_handler = rts_input,
206 .nh_proto = NETISR_ROUTE,
207 .nh_policy = NETISR_POLICY_SOURCE,
211 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
215 netisr_getqlimit(&rtsock_nh, &qlimit);
216 error = sysctl_handle_int(oidp, &qlimit, 0, req);
217 if (error || !req->newptr)
221 return (netisr_setqlimit(&rtsock_nh, qlimit));
223 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
224 0, 0, sysctl_route_netisr_maxqlen, "I",
225 "maximum routing socket dispatch queue length");
232 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
233 rtsock_nh.nh_qlimit = tmp;
234 netisr_register(&rtsock_nh);
236 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
239 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
244 KASSERT(m != NULL, ("%s: m is NULL", __func__));
245 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
246 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
248 /* No filtering requested. */
249 if ((m->m_flags & RTS_FILTER_FIB) == 0)
252 /* Check if it is a rts and the fib matches the one of the socket. */
253 fibnum = M_GETFIB(m);
254 if (proto->sp_family != PF_ROUTE ||
255 rp->rcb_socket == NULL ||
256 rp->rcb_socket->so_fibnum == fibnum)
259 /* Filtering requested and no match, the socket shall be skipped. */
264 rts_input(struct mbuf *m)
266 struct sockproto route_proto;
267 unsigned short *family;
270 route_proto.sp_family = PF_ROUTE;
271 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
273 family = (unsigned short *)(tag + 1);
274 route_proto.sp_protocol = *family;
275 m_tag_delete(m, tag);
277 route_proto.sp_protocol = 0;
279 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
283 * It really doesn't make any sense at all for this code to share much
284 * with raw_usrreq.c, since its functionality is so restricted. XXX
287 rts_abort(struct socket *so)
290 raw_usrreqs.pru_abort(so);
294 rts_close(struct socket *so)
297 raw_usrreqs.pru_close(so);
300 /* pru_accept is EOPNOTSUPP */
303 rts_attach(struct socket *so, int proto, struct thread *td)
308 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
311 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
315 so->so_pcb = (caddr_t)rp;
316 so->so_fibnum = td->td_proc->p_fibnum;
317 error = raw_attach(so, proto);
325 switch(rp->rcb_proto.sp_protocol) {
330 route_cb.ip6_count++;
333 route_cb.ipx_count++;
336 route_cb.any_count++;
339 so->so_options |= SO_USELOOPBACK;
344 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
347 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
351 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
354 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
357 /* pru_connect2 is EOPNOTSUPP */
358 /* pru_control is EOPNOTSUPP */
361 rts_detach(struct socket *so)
363 struct rawcb *rp = sotorawcb(so);
365 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
368 switch(rp->rcb_proto.sp_protocol) {
373 route_cb.ip6_count--;
376 route_cb.ipx_count--;
379 route_cb.any_count--;
381 raw_usrreqs.pru_detach(so);
385 rts_disconnect(struct socket *so)
388 return (raw_usrreqs.pru_disconnect(so));
391 /* pru_listen is EOPNOTSUPP */
394 rts_peeraddr(struct socket *so, struct sockaddr **nam)
397 return (raw_usrreqs.pru_peeraddr(so, nam));
400 /* pru_rcvd is EOPNOTSUPP */
401 /* pru_rcvoob is EOPNOTSUPP */
404 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
405 struct mbuf *control, struct thread *td)
408 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
411 /* pru_sense is null */
414 rts_shutdown(struct socket *so)
417 return (raw_usrreqs.pru_shutdown(so));
421 rts_sockaddr(struct socket *so, struct sockaddr **nam)
424 return (raw_usrreqs.pru_sockaddr(so, nam));
427 static struct pr_usrreqs route_usrreqs = {
428 .pru_abort = rts_abort,
429 .pru_attach = rts_attach,
430 .pru_bind = rts_bind,
431 .pru_connect = rts_connect,
432 .pru_detach = rts_detach,
433 .pru_disconnect = rts_disconnect,
434 .pru_peeraddr = rts_peeraddr,
435 .pru_send = rts_send,
436 .pru_shutdown = rts_shutdown,
437 .pru_sockaddr = rts_sockaddr,
438 .pru_close = rts_close,
441 #ifndef _SOCKADDR_UNION_DEFINED
442 #define _SOCKADDR_UNION_DEFINED
444 * The union of all possible address formats we handle.
446 union sockaddr_union {
448 struct sockaddr_in sin;
449 struct sockaddr_in6 sin6;
451 #endif /* _SOCKADDR_UNION_DEFINED */
454 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
455 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
458 /* First, see if the returned address is part of the jail. */
459 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
460 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
464 switch (info->rti_info[RTAX_DST]->sa_family) {
474 * Try to find an address on the given outgoing interface
475 * that belongs to the jail.
478 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
481 if (sa->sa_family != AF_INET)
483 ia = ((struct sockaddr_in *)sa)->sin_addr;
484 if (prison_check_ip4(cred, &ia) == 0) {
489 IF_ADDR_RUNLOCK(ifp);
492 * As a last resort return the 'default' jail address.
494 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
496 if (prison_get_ip4(cred, &ia) != 0)
499 bzero(&saun->sin, sizeof(struct sockaddr_in));
500 saun->sin.sin_len = sizeof(struct sockaddr_in);
501 saun->sin.sin_family = AF_INET;
502 saun->sin.sin_addr.s_addr = ia.s_addr;
503 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
516 * Try to find an address on the given outgoing interface
517 * that belongs to the jail.
520 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
523 if (sa->sa_family != AF_INET6)
525 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
526 &ia6, sizeof(struct in6_addr));
527 if (prison_check_ip6(cred, &ia6) == 0) {
532 IF_ADDR_RUNLOCK(ifp);
535 * As a last resort return the 'default' jail address.
537 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
539 if (prison_get_ip6(cred, &ia6) != 0)
542 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
543 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
544 saun->sin6.sin6_family = AF_INET6;
545 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
546 if (sa6_recoverscope(&saun->sin6) != 0)
548 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
560 route_output(struct mbuf *m, struct socket *so)
562 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
563 struct rt_msghdr *rtm = NULL;
564 struct rtentry *rt = NULL;
565 struct radix_node_head *rnh;
566 struct rt_addrinfo info;
568 struct sockaddr_storage ss;
569 struct sockaddr_in6 *sin6;
570 int i, rti_need_deembed = 0;
573 struct ifnet *ifp = NULL;
574 union sockaddr_union saun;
575 sa_family_t saf = AF_UNSPEC;
577 #define senderr(e) { error = e; goto flush;}
578 if (m == NULL || ((m->m_len < sizeof(long)) &&
579 (m = m_pullup(m, sizeof(long))) == NULL))
581 if ((m->m_flags & M_PKTHDR) == 0)
582 panic("route_output");
583 len = m->m_pkthdr.len;
584 if (len < sizeof(*rtm) ||
585 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
586 info.rti_info[RTAX_DST] = NULL;
589 R_Malloc(rtm, struct rt_msghdr *, len);
591 info.rti_info[RTAX_DST] = NULL;
594 m_copydata(m, 0, len, (caddr_t)rtm);
595 if (rtm->rtm_version != RTM_VERSION) {
596 info.rti_info[RTAX_DST] = NULL;
597 senderr(EPROTONOSUPPORT);
599 rtm->rtm_pid = curproc->p_pid;
600 bzero(&info, sizeof(info));
601 info.rti_addrs = rtm->rtm_addrs;
603 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
604 * link-local address because rtrequest requires addresses with
607 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
608 info.rti_info[RTAX_DST] = NULL;
611 info.rti_flags = rtm->rtm_flags;
612 if (info.rti_info[RTAX_DST] == NULL ||
613 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
614 (info.rti_info[RTAX_GATEWAY] != NULL &&
615 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
617 saf = info.rti_info[RTAX_DST]->sa_family;
619 * Verify that the caller has the appropriate privilege; RTM_GET
620 * is the only operation the non-superuser is allowed.
622 if (rtm->rtm_type != RTM_GET) {
623 error = priv_check(curthread, PRIV_NET_ROUTE);
629 * The given gateway address may be an interface address.
630 * For example, issuing a "route change" command on a route
631 * entry that was created from a tunnel, and the gateway
632 * address given is the local end point. In this case the
633 * RTF_GATEWAY flag must be cleared or the destination will
634 * not be reachable even though there is no error message.
636 if (info.rti_info[RTAX_GATEWAY] != NULL &&
637 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
640 bzero(&gw_ro, sizeof(gw_ro));
641 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
642 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
644 * A host route through the loopback interface is
645 * installed for each interface adddress. In pre 8.0
646 * releases the interface address of a PPP link type
647 * is not reachable locally. This behavior is fixed as
648 * part of the new L2/L3 redesign and rewrite work. The
649 * signature of this interface address route is the
650 * AF_LINK sa_family type of the rt_gateway, and the
651 * rt_ifp has the IFF_LOOPBACK flag set.
653 if (gw_ro.ro_rt != NULL &&
654 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
655 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
656 info.rti_flags &= ~RTF_GATEWAY;
657 info.rti_flags |= RTF_GWFLAG_COMPAT;
659 if (gw_ro.ro_rt != NULL)
663 switch (rtm->rtm_type) {
664 struct rtentry *saved_nrt;
667 if (info.rti_info[RTAX_GATEWAY] == NULL)
671 /* support for new ARP code */
672 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
673 (rtm->rtm_flags & RTF_LLDATA) != 0) {
674 error = lla_rt_output(rtm, &info);
677 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
681 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
683 if (error == 0 && saved_nrt) {
685 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
688 rt_setmetrics(rtm->rtm_inits,
689 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
690 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
691 RT_REMREF(saved_nrt);
692 RT_UNLOCK(saved_nrt);
698 /* support for new ARP code */
699 if (info.rti_info[RTAX_GATEWAY] &&
700 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
701 (rtm->rtm_flags & RTF_LLDATA) != 0) {
702 error = lla_rt_output(rtm, &info);
705 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
709 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
717 /* rt_msg2() will not be used when RTM_DELETE fails. */
718 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
725 rnh = rt_tables_get_rnh(so->so_fibnum,
726 info.rti_info[RTAX_DST]->sa_family);
728 senderr(EAFNOSUPPORT);
729 RADIX_NODE_HEAD_RLOCK(rnh);
730 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
731 info.rti_info[RTAX_NETMASK], rnh);
732 if (rt == NULL) { /* XXX looks bogus */
733 RADIX_NODE_HEAD_RUNLOCK(rnh);
738 * for RTM_CHANGE/LOCK, if we got multipath routes,
739 * we require users to specify a matching RTAX_GATEWAY.
741 * for RTM_GET, gate is optional even with multipath.
742 * if gate == NULL the first match is returned.
743 * (no need to call rt_mpath_matchgate if gate == NULL)
745 if (rn_mpath_capable(rnh) &&
746 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
747 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
749 RADIX_NODE_HEAD_RUNLOCK(rnh);
755 * If performing proxied L2 entry insertion, and
756 * the actual PPP host entry is found, perform
757 * another search to retrieve the prefix route of
758 * the local end point of the PPP link.
760 if (rtm->rtm_flags & RTF_ANNOUNCE) {
761 struct sockaddr laddr;
763 if (rt->rt_ifp != NULL &&
764 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
767 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
769 rt_maskedcopy(ifa->ifa_addr,
773 rt_maskedcopy(rt->rt_ifa->ifa_addr,
775 rt->rt_ifa->ifa_netmask);
777 * refactor rt and no lock operation necessary
779 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
781 RADIX_NODE_HEAD_RUNLOCK(rnh);
787 RADIX_NODE_HEAD_RUNLOCK(rnh);
792 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
793 * returns a perfect match in case a netmask is
794 * specified. For host routes only a longest prefix
795 * match is returned so it is necessary to compare the
796 * existence of the netmask. If both have a netmask
797 * rnh_lookup() did a perfect match and if none of them
798 * have a netmask both are host routes which is also a
802 if (rtm->rtm_type != RTM_GET &&
803 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
808 switch(rtm->rtm_type) {
813 if ((rt->rt_flags & RTF_HOST) == 0
814 ? jailed_without_vnet(curthread->td_ucred)
815 : prison_if(curthread->td_ucred,
820 info.rti_info[RTAX_DST] = rt_key(rt);
821 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
822 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
823 info.rti_info[RTAX_GENMASK] = 0;
824 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
827 info.rti_info[RTAX_IFP] =
828 ifp->if_addr->ifa_addr;
829 error = rtm_get_jailed(&info, ifp, rt,
830 &saun, curthread->td_ucred);
835 if (ifp->if_flags & IFF_POINTOPOINT)
836 info.rti_info[RTAX_BRD] =
837 rt->rt_ifa->ifa_dstaddr;
838 rtm->rtm_index = ifp->if_index;
840 info.rti_info[RTAX_IFP] = NULL;
841 info.rti_info[RTAX_IFA] = NULL;
843 } else if ((ifp = rt->rt_ifp) != NULL) {
844 rtm->rtm_index = ifp->if_index;
846 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
847 if (len > rtm->rtm_msglen) {
848 struct rt_msghdr *new_rtm;
849 R_Malloc(new_rtm, struct rt_msghdr *, len);
850 if (new_rtm == NULL) {
854 bcopy(rtm, new_rtm, rtm->rtm_msglen);
855 Free(rtm); rtm = new_rtm;
857 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
858 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
859 rtm->rtm_flags = RTF_GATEWAY |
860 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
862 rtm->rtm_flags = rt->rt_flags;
863 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
864 rtm->rtm_addrs = info.rti_addrs;
869 * New gateway could require new ifaddr, ifp;
870 * flags may also be different; ifp may be specified
871 * by ll sockaddr when protocol address is ambiguous
873 if (((rt->rt_flags & RTF_GATEWAY) &&
874 info.rti_info[RTAX_GATEWAY] != NULL) ||
875 info.rti_info[RTAX_IFP] != NULL ||
876 (info.rti_info[RTAX_IFA] != NULL &&
877 !sa_equal(info.rti_info[RTAX_IFA],
878 rt->rt_ifa->ifa_addr))) {
880 RADIX_NODE_HEAD_LOCK(rnh);
881 error = rt_getifa_fib(&info, rt->rt_fibnum);
883 * XXXRW: Really we should release this
884 * reference later, but this maintains
885 * historical behavior.
887 if (info.rti_ifa != NULL)
888 ifa_free(info.rti_ifa);
889 RADIX_NODE_HEAD_UNLOCK(rnh);
894 if (info.rti_ifa != NULL &&
895 info.rti_ifa != rt->rt_ifa &&
896 rt->rt_ifa != NULL &&
897 rt->rt_ifa->ifa_rtrequest != NULL) {
898 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
900 ifa_free(rt->rt_ifa);
902 if (info.rti_info[RTAX_GATEWAY] != NULL) {
904 RADIX_NODE_HEAD_LOCK(rnh);
907 error = rt_setgate(rt, rt_key(rt),
908 info.rti_info[RTAX_GATEWAY]);
909 RADIX_NODE_HEAD_UNLOCK(rnh);
914 rt->rt_flags &= ~RTF_GATEWAY;
915 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
917 if (info.rti_ifa != NULL &&
918 info.rti_ifa != rt->rt_ifa) {
919 ifa_ref(info.rti_ifa);
920 rt->rt_ifa = info.rti_ifa;
921 rt->rt_ifp = info.rti_ifp;
923 /* Allow some flags to be toggled on change. */
924 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
925 (rtm->rtm_flags & RTF_FMASK);
926 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
928 rtm->rtm_index = rt->rt_ifp->if_index;
929 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
930 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
933 /* We don't support locks anymore */
946 rtm->rtm_errno = error;
948 rtm->rtm_flags |= RTF_DONE;
950 if (rt) /* XXX can this be true? */
953 struct rawcb *rp = NULL;
955 * Check to see if we don't want our own messages.
957 if ((so->so_options & SO_USELOOPBACK) == 0) {
958 if (route_cb.any_count <= 1) {
964 /* There is another listener, so construct message */
969 if (rti_need_deembed) {
970 /* sin6_scope_id is recovered before sending rtm. */
971 sin6 = (struct sockaddr_in6 *)&ss;
972 for (i = 0; i < RTAX_MAX; i++) {
973 if (info.rti_info[i] == NULL)
975 if (info.rti_info[i]->sa_family != AF_INET6)
977 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
978 if (sa6_recoverscope(sin6) == 0)
979 bcopy(sin6, info.rti_info[i],
984 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
985 if (m->m_pkthdr.len < rtm->rtm_msglen) {
988 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
989 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
992 M_SETFIB(m, so->so_fibnum);
993 m->m_flags |= RTS_FILTER_FIB;
996 * XXX insure we don't get a copy by
997 * invalidating our protocol
999 unsigned short family = rp->rcb_proto.sp_family;
1000 rp->rcb_proto.sp_family = 0;
1001 rt_dispatch(m, saf);
1002 rp->rcb_proto.sp_family = family;
1004 rt_dispatch(m, saf);
1006 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
1015 rt_setmetrics(u_long which, const struct rt_metrics *in,
1016 struct rt_metrics_lite *out)
1018 #define metric(f, e) if (which & (f)) out->e = in->e;
1020 * Only these are stored in the routing entry since introduction
1021 * of tcp hostcache. The rest is ignored.
1023 metric(RTV_MTU, rmx_mtu);
1024 metric(RTV_WEIGHT, rmx_weight);
1025 /* Userland -> kernel timebase conversion. */
1026 if (which & RTV_EXPIRE)
1027 out->rmx_expire = in->rmx_expire ?
1028 in->rmx_expire - time_second + time_uptime : 0;
1033 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
1035 #define metric(e) out->e = in->e;
1036 bzero(out, sizeof(*out));
1039 /* Kernel -> userland timebase conversion. */
1040 out->rmx_expire = in->rmx_expire ?
1041 in->rmx_expire - time_uptime + time_second : 0;
1046 * Extract the addresses of the passed sockaddrs.
1047 * Do a little sanity checking so as to avoid bad memory references.
1048 * This data is derived straight from userland.
1051 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1053 struct sockaddr *sa;
1056 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1057 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1059 sa = (struct sockaddr *)cp;
1063 if (cp + sa->sa_len > cplim)
1066 * there are no more.. quit now
1067 * If there are more bits, they are in error.
1068 * I've seen this. route(1) can evidently generate these.
1069 * This causes kernel to core dump.
1070 * for compatibility, If we see this, point to a safe address.
1072 if (sa->sa_len == 0) {
1073 rtinfo->rti_info[i] = &sa_zero;
1074 return (0); /* should be EINVAL but for compat */
1078 if (sa->sa_family == AF_INET6)
1079 sa6_embedscope((struct sockaddr_in6 *)sa,
1082 rtinfo->rti_info[i] = sa;
1089 * Used by the routing socket.
1091 static struct mbuf *
1092 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1094 struct rt_msghdr *rtm;
1097 struct sockaddr *sa;
1099 struct sockaddr_storage ss;
1100 struct sockaddr_in6 *sin6;
1108 len = sizeof(struct ifa_msghdr);
1113 len = sizeof(struct ifma_msghdr);
1117 len = sizeof(struct if_msghdr);
1120 case RTM_IFANNOUNCE:
1122 len = sizeof(struct if_announcemsghdr);
1126 len = sizeof(struct rt_msghdr);
1129 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1130 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1132 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1134 m = m_gethdr(M_NOWAIT, MT_DATA);
1138 m->m_pkthdr.len = m->m_len = len;
1139 rtm = mtod(m, struct rt_msghdr *);
1140 bzero((caddr_t)rtm, len);
1141 for (i = 0; i < RTAX_MAX; i++) {
1142 if ((sa = rtinfo->rti_info[i]) == NULL)
1144 rtinfo->rti_addrs |= (1 << i);
1147 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1148 sin6 = (struct sockaddr_in6 *)&ss;
1149 bcopy(sa, sin6, sizeof(*sin6));
1150 if (sa6_recoverscope(sin6) == 0)
1151 sa = (struct sockaddr *)sin6;
1154 m_copyback(m, len, dlen, (caddr_t)sa);
1157 if (m->m_pkthdr.len != len) {
1161 rtm->rtm_msglen = len;
1162 rtm->rtm_version = RTM_VERSION;
1163 rtm->rtm_type = type;
1168 * Used by the sysctl code and routing socket.
1171 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1174 int len, dlen, second_time = 0;
1177 struct sockaddr_storage ss;
1178 struct sockaddr_in6 *sin6;
1181 rtinfo->rti_addrs = 0;
1187 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1188 #ifdef COMPAT_FREEBSD32
1189 if (w->w_req->flags & SCTL_MASK32)
1190 len = sizeof(struct ifa_msghdrl32);
1193 len = sizeof(struct ifa_msghdrl);
1195 len = sizeof(struct ifa_msghdr);
1199 #ifdef COMPAT_FREEBSD32
1200 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1201 if (w->w_op == NET_RT_IFLISTL)
1202 len = sizeof(struct if_msghdrl32);
1204 len = sizeof(struct if_msghdr32);
1208 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1209 len = sizeof(struct if_msghdrl);
1211 len = sizeof(struct if_msghdr);
1215 len = sizeof(struct ifma_msghdr);
1219 len = sizeof(struct rt_msghdr);
1224 for (i = 0; i < RTAX_MAX; i++) {
1225 struct sockaddr *sa;
1227 if ((sa = rtinfo->rti_info[i]) == NULL)
1229 rtinfo->rti_addrs |= (1 << i);
1233 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1234 sin6 = (struct sockaddr_in6 *)&ss;
1235 bcopy(sa, sin6, sizeof(*sin6));
1236 if (sa6_recoverscope(sin6) == 0)
1237 sa = (struct sockaddr *)sin6;
1240 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1246 if (cp == NULL && w != NULL && !second_time) {
1247 struct walkarg *rw = w;
1250 if (rw->w_tmemsize < len) {
1252 free(rw->w_tmem, M_RTABLE);
1253 rw->w_tmem = (caddr_t)
1254 malloc(len, M_RTABLE, M_NOWAIT);
1256 rw->w_tmemsize = len;
1266 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1268 rtm->rtm_version = RTM_VERSION;
1269 rtm->rtm_type = type;
1270 rtm->rtm_msglen = len;
1276 * This routine is called to generate a message from the routing
1277 * socket indicating that a redirect has occured, a routing lookup
1278 * has failed, or that a protocol has detected timeouts to a particular
1282 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1285 struct rt_msghdr *rtm;
1287 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1289 if (route_cb.any_count == 0)
1291 m = rt_msg1(type, rtinfo);
1295 if (fibnum != RTS_ALLFIBS) {
1296 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1297 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1298 M_SETFIB(m, fibnum);
1299 m->m_flags |= RTS_FILTER_FIB;
1302 rtm = mtod(m, struct rt_msghdr *);
1303 rtm->rtm_flags = RTF_DONE | flags;
1304 rtm->rtm_errno = error;
1305 rtm->rtm_addrs = rtinfo->rti_addrs;
1306 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1310 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1313 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1317 * This routine is called to generate a message from the routing
1318 * socket indicating that the status of a network interface has changed.
1321 rt_ifmsg(struct ifnet *ifp)
1323 struct if_msghdr *ifm;
1325 struct rt_addrinfo info;
1327 if (route_cb.any_count == 0)
1329 bzero((caddr_t)&info, sizeof(info));
1330 m = rt_msg1(RTM_IFINFO, &info);
1333 ifm = mtod(m, struct if_msghdr *);
1334 ifm->ifm_index = ifp->if_index;
1335 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1336 ifm->ifm_data = ifp->if_data;
1338 rt_dispatch(m, AF_UNSPEC);
1342 * This is called to generate messages from the routing socket
1343 * indicating a network interface has had addresses associated with it.
1344 * if we ever reverse the logic and replace messages TO the routing
1345 * socket indicate a request to configure interfaces, then it will
1346 * be unnecessary as the routing socket will automatically generate
1350 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1353 struct rt_addrinfo info;
1354 struct sockaddr *sa = NULL;
1356 struct mbuf *m = NULL;
1357 struct ifnet *ifp = ifa->ifa_ifp;
1359 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1360 ("unexpected cmd %u", cmd));
1361 #if defined(INET) || defined(INET6)
1364 * notify the SCTP stack
1365 * this will only get called when an address is added/deleted
1366 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1368 sctp_addr_change(ifa, cmd);
1371 if (route_cb.any_count == 0)
1373 for (pass = 1; pass < 3; pass++) {
1374 bzero((caddr_t)&info, sizeof(info));
1375 if ((cmd == RTM_ADD && pass == 1) ||
1376 (cmd == RTM_DELETE && pass == 2)) {
1377 struct ifa_msghdr *ifam;
1378 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1380 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1381 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1382 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1383 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1384 if ((m = rt_msg1(ncmd, &info)) == NULL)
1386 ifam = mtod(m, struct ifa_msghdr *);
1387 ifam->ifam_index = ifp->if_index;
1388 ifam->ifam_metric = ifa->ifa_metric;
1389 ifam->ifam_flags = ifa->ifa_flags;
1390 ifam->ifam_addrs = info.rti_addrs;
1392 if ((cmd == RTM_ADD && pass == 2) ||
1393 (cmd == RTM_DELETE && pass == 1)) {
1394 struct rt_msghdr *rtm;
1398 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1399 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1400 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1401 if ((m = rt_msg1(cmd, &info)) == NULL)
1403 rtm = mtod(m, struct rt_msghdr *);
1404 rtm->rtm_index = ifp->if_index;
1405 rtm->rtm_flags |= rt->rt_flags;
1406 rtm->rtm_errno = error;
1407 rtm->rtm_addrs = info.rti_addrs;
1409 if (fibnum != RTS_ALLFIBS) {
1410 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1411 "fibnum out of range 0 <= %d < %d", __func__,
1412 fibnum, rt_numfibs));
1413 M_SETFIB(m, fibnum);
1414 m->m_flags |= RTS_FILTER_FIB;
1416 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1421 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1424 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1428 * This is the analogue to the rt_newaddrmsg which performs the same
1429 * function but for multicast group memberhips. This is easier since
1430 * there is no route state to worry about.
1433 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1435 struct rt_addrinfo info;
1436 struct mbuf *m = NULL;
1437 struct ifnet *ifp = ifma->ifma_ifp;
1438 struct ifma_msghdr *ifmam;
1440 if (route_cb.any_count == 0)
1443 bzero((caddr_t)&info, sizeof(info));
1444 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1445 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1447 * If a link-layer address is present, present it as a ``gateway''
1448 * (similarly to how ARP entries, e.g., are presented).
1450 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1451 m = rt_msg1(cmd, &info);
1454 ifmam = mtod(m, struct ifma_msghdr *);
1455 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1457 ifmam->ifmam_index = ifp->if_index;
1458 ifmam->ifmam_addrs = info.rti_addrs;
1459 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1462 static struct mbuf *
1463 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1464 struct rt_addrinfo *info)
1466 struct if_announcemsghdr *ifan;
1469 if (route_cb.any_count == 0)
1471 bzero((caddr_t)info, sizeof(*info));
1472 m = rt_msg1(type, info);
1474 ifan = mtod(m, struct if_announcemsghdr *);
1475 ifan->ifan_index = ifp->if_index;
1476 strlcpy(ifan->ifan_name, ifp->if_xname,
1477 sizeof(ifan->ifan_name));
1478 ifan->ifan_what = what;
1484 * This is called to generate routing socket messages indicating
1485 * IEEE80211 wireless events.
1486 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1489 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1492 struct rt_addrinfo info;
1494 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1497 * Append the ieee80211 data. Try to stick it in the
1498 * mbuf containing the ifannounce msg; otherwise allocate
1499 * a new mbuf and append.
1501 * NB: we assume m is a single mbuf.
1503 if (data_len > M_TRAILINGSPACE(m)) {
1504 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1509 bcopy(data, mtod(n, void *), data_len);
1510 n->m_len = data_len;
1512 } else if (data_len > 0) {
1513 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1514 m->m_len += data_len;
1516 if (m->m_flags & M_PKTHDR)
1517 m->m_pkthdr.len += data_len;
1518 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1519 rt_dispatch(m, AF_UNSPEC);
1524 * This is called to generate routing socket messages indicating
1525 * network interface arrival and departure.
1528 rt_ifannouncemsg(struct ifnet *ifp, int what)
1531 struct rt_addrinfo info;
1533 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1535 rt_dispatch(m, AF_UNSPEC);
1539 rt_dispatch(struct mbuf *m, sa_family_t saf)
1544 * Preserve the family from the sockaddr, if any, in an m_tag for
1545 * use when injecting the mbuf into the routing socket buffer from
1548 if (saf != AF_UNSPEC) {
1549 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1555 *(unsigned short *)(tag + 1) = saf;
1556 m_tag_prepend(m, tag);
1560 m->m_pkthdr.rcvif = V_loif;
1566 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1570 * This is used in dumping the kernel table via sysctl().
1573 sysctl_dumpentry(struct radix_node *rn, void *vw)
1575 struct walkarg *w = vw;
1576 struct rtentry *rt = (struct rtentry *)rn;
1577 int error = 0, size;
1578 struct rt_addrinfo info;
1580 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1582 if ((rt->rt_flags & RTF_HOST) == 0
1583 ? jailed_without_vnet(w->w_req->td->td_ucred)
1584 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1586 bzero((caddr_t)&info, sizeof(info));
1587 info.rti_info[RTAX_DST] = rt_key(rt);
1588 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1589 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1590 info.rti_info[RTAX_GENMASK] = 0;
1592 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1593 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1594 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1595 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1597 size = rt_msg2(RTM_GET, &info, NULL, w);
1598 if (w->w_req && w->w_tmem) {
1599 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1601 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1602 rtm->rtm_flags = RTF_GATEWAY |
1603 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1605 rtm->rtm_flags = rt->rt_flags;
1607 * let's be honest about this being a retarded hack
1609 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1610 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1611 rtm->rtm_index = rt->rt_ifp->if_index;
1612 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1613 rtm->rtm_addrs = info.rti_addrs;
1614 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1620 #ifdef COMPAT_FREEBSD32
1622 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1625 bzero(dst, sizeof(*dst));
1626 CP(*src, *dst, ifi_type);
1627 CP(*src, *dst, ifi_physical);
1628 CP(*src, *dst, ifi_addrlen);
1629 CP(*src, *dst, ifi_hdrlen);
1630 CP(*src, *dst, ifi_link_state);
1631 CP(*src, *dst, ifi_vhid);
1632 CP(*src, *dst, ifi_baudrate_pf);
1633 dst->ifi_datalen = sizeof(struct if_data32);
1634 CP(*src, *dst, ifi_mtu);
1635 CP(*src, *dst, ifi_metric);
1636 CP(*src, *dst, ifi_baudrate);
1637 CP(*src, *dst, ifi_ipackets);
1638 CP(*src, *dst, ifi_ierrors);
1639 CP(*src, *dst, ifi_opackets);
1640 CP(*src, *dst, ifi_oerrors);
1641 CP(*src, *dst, ifi_collisions);
1642 CP(*src, *dst, ifi_ibytes);
1643 CP(*src, *dst, ifi_obytes);
1644 CP(*src, *dst, ifi_imcasts);
1645 CP(*src, *dst, ifi_omcasts);
1646 CP(*src, *dst, ifi_iqdrops);
1647 CP(*src, *dst, ifi_noproto);
1648 CP(*src, *dst, ifi_hwassist);
1649 CP(*src, *dst, ifi_epoch);
1650 TV_CP(*src, *dst, ifi_lastchange);
1655 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1656 struct walkarg *w, int len)
1658 struct if_msghdrl *ifm;
1660 #ifdef COMPAT_FREEBSD32
1661 if (w->w_req->flags & SCTL_MASK32) {
1662 struct if_msghdrl32 *ifm32;
1664 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1665 ifm32->ifm_addrs = info->rti_addrs;
1666 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1667 ifm32->ifm_index = ifp->if_index;
1668 ifm32->_ifm_spare1 = 0;
1669 ifm32->ifm_len = sizeof(*ifm32);
1670 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1672 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1673 /* Fixup if_data carp(4) vhid. */
1674 if (carp_get_vhid_p != NULL)
1675 ifm32->ifm_data.ifi_vhid =
1676 (*carp_get_vhid_p)(ifp->if_addr);
1678 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1681 ifm = (struct if_msghdrl *)w->w_tmem;
1682 ifm->ifm_addrs = info->rti_addrs;
1683 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1684 ifm->ifm_index = ifp->if_index;
1685 ifm->_ifm_spare1 = 0;
1686 ifm->ifm_len = sizeof(*ifm);
1687 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1689 ifm->ifm_data = ifp->if_data;
1690 /* Fixup if_data carp(4) vhid. */
1691 if (carp_get_vhid_p != NULL)
1692 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1694 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1698 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1699 struct walkarg *w, int len)
1701 struct if_msghdr *ifm;
1703 #ifdef COMPAT_FREEBSD32
1704 if (w->w_req->flags & SCTL_MASK32) {
1705 struct if_msghdr32 *ifm32;
1707 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1708 ifm32->ifm_addrs = info->rti_addrs;
1709 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1710 ifm32->ifm_index = ifp->if_index;
1712 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1713 /* Fixup if_data carp(4) vhid. */
1714 if (carp_get_vhid_p != NULL)
1715 ifm32->ifm_data.ifi_vhid =
1716 (*carp_get_vhid_p)(ifp->if_addr);
1718 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1721 ifm = (struct if_msghdr *)w->w_tmem;
1722 ifm->ifm_addrs = info->rti_addrs;
1723 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1724 ifm->ifm_index = ifp->if_index;
1726 ifm->ifm_data = ifp->if_data;
1727 /* Fixup if_data carp(4) vhid. */
1728 if (carp_get_vhid_p != NULL)
1729 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1731 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1735 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1736 struct walkarg *w, int len)
1738 struct ifa_msghdrl *ifam;
1740 #ifdef COMPAT_FREEBSD32
1741 if (w->w_req->flags & SCTL_MASK32) {
1742 struct ifa_msghdrl32 *ifam32;
1744 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1745 ifam32->ifam_addrs = info->rti_addrs;
1746 ifam32->ifam_flags = ifa->ifa_flags;
1747 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1748 ifam32->_ifam_spare1 = 0;
1749 ifam32->ifam_len = sizeof(*ifam32);
1750 ifam32->ifam_data_off =
1751 offsetof(struct ifa_msghdrl32, ifam_data);
1752 ifam32->ifam_metric = ifa->ifa_metric;
1754 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1755 /* Fixup if_data carp(4) vhid. */
1756 if (carp_get_vhid_p != NULL)
1757 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1759 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1763 ifam = (struct ifa_msghdrl *)w->w_tmem;
1764 ifam->ifam_addrs = info->rti_addrs;
1765 ifam->ifam_flags = ifa->ifa_flags;
1766 ifam->ifam_index = ifa->ifa_ifp->if_index;
1767 ifam->_ifam_spare1 = 0;
1768 ifam->ifam_len = sizeof(*ifam);
1769 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1770 ifam->ifam_metric = ifa->ifa_metric;
1772 ifam->ifam_data = ifa->if_data;
1773 /* Fixup if_data carp(4) vhid. */
1774 if (carp_get_vhid_p != NULL)
1775 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1777 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1781 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1782 struct walkarg *w, int len)
1784 struct ifa_msghdr *ifam;
1786 ifam = (struct ifa_msghdr *)w->w_tmem;
1787 ifam->ifam_addrs = info->rti_addrs;
1788 ifam->ifam_flags = ifa->ifa_flags;
1789 ifam->ifam_index = ifa->ifa_ifp->if_index;
1790 ifam->ifam_metric = ifa->ifa_metric;
1792 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1796 sysctl_iflist(int af, struct walkarg *w)
1800 struct rt_addrinfo info;
1803 bzero((caddr_t)&info, sizeof(info));
1804 IFNET_RLOCK_NOSLEEP();
1805 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1806 if (w->w_arg && w->w_arg != ifp->if_index)
1810 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1811 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1812 info.rti_info[RTAX_IFP] = NULL;
1813 if (w->w_req && w->w_tmem) {
1814 if (w->w_op == NET_RT_IFLISTL)
1815 error = sysctl_iflist_ifml(ifp, &info, w, len);
1817 error = sysctl_iflist_ifm(ifp, &info, w, len);
1821 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1822 if (af && af != ifa->ifa_addr->sa_family)
1824 if (prison_if(w->w_req->td->td_ucred,
1825 ifa->ifa_addr) != 0)
1827 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1828 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1829 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1830 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1831 if (w->w_req && w->w_tmem) {
1832 if (w->w_op == NET_RT_IFLISTL)
1833 error = sysctl_iflist_ifaml(ifa, &info,
1836 error = sysctl_iflist_ifam(ifa, &info,
1842 IF_ADDR_RUNLOCK(ifp);
1843 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1844 info.rti_info[RTAX_BRD] = NULL;
1848 IF_ADDR_RUNLOCK(ifp);
1849 IFNET_RUNLOCK_NOSLEEP();
1854 sysctl_ifmalist(int af, struct walkarg *w)
1857 struct ifmultiaddr *ifma;
1858 struct rt_addrinfo info;
1862 bzero((caddr_t)&info, sizeof(info));
1863 IFNET_RLOCK_NOSLEEP();
1864 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1865 if (w->w_arg && w->w_arg != ifp->if_index)
1868 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1870 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1871 if (af && af != ifma->ifma_addr->sa_family)
1873 if (prison_if(w->w_req->td->td_ucred,
1874 ifma->ifma_addr) != 0)
1876 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1877 info.rti_info[RTAX_GATEWAY] =
1878 (ifma->ifma_addr->sa_family != AF_LINK) ?
1879 ifma->ifma_lladdr : NULL;
1880 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1881 if (w->w_req && w->w_tmem) {
1882 struct ifma_msghdr *ifmam;
1884 ifmam = (struct ifma_msghdr *)w->w_tmem;
1885 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1886 ifmam->ifmam_flags = 0;
1887 ifmam->ifmam_addrs = info.rti_addrs;
1888 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1890 IF_ADDR_RUNLOCK(ifp);
1895 IF_ADDR_RUNLOCK(ifp);
1898 IFNET_RUNLOCK_NOSLEEP();
1903 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1905 int *name = (int *)arg1;
1906 u_int namelen = arg2;
1907 struct radix_node_head *rnh = NULL; /* silence compiler. */
1908 int i, lim, error = EINVAL;
1917 if (name[1] == NET_RT_DUMP) {
1919 fib = req->td->td_proc->p_fibnum;
1920 else if (namelen == 4)
1921 fib = (name[3] == -1) ?
1922 req->td->td_proc->p_fibnum : name[3];
1924 return ((namelen < 3) ? EISDIR : ENOTDIR);
1925 if (fib < 0 || fib >= rt_numfibs)
1927 } else if (namelen != 3)
1928 return ((namelen < 3) ? EISDIR : ENOTDIR);
1932 bzero(&w, sizeof(w));
1937 error = sysctl_wire_old_buffer(req, 0);
1944 if (af == 0) { /* dump all tables */
1947 } else /* dump only one table */
1951 * take care of llinfo entries, the caller must
1954 if (w.w_op == NET_RT_FLAGS &&
1955 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1957 error = lltable_sysctl_dumparp(af, w.w_req);
1963 * take care of routing entries
1965 for (error = 0; error == 0 && i <= lim; i++) {
1966 rnh = rt_tables_get_rnh(fib, i);
1968 RADIX_NODE_HEAD_RLOCK(rnh);
1969 error = rnh->rnh_walktree(rnh,
1970 sysctl_dumpentry, &w);
1971 RADIX_NODE_HEAD_RUNLOCK(rnh);
1973 error = EAFNOSUPPORT;
1978 case NET_RT_IFLISTL:
1979 error = sysctl_iflist(af, &w);
1982 case NET_RT_IFMALIST:
1983 error = sysctl_ifmalist(af, &w);
1987 free(w.w_tmem, M_RTABLE);
1991 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1994 * Definitions of protocols supported in the ROUTE domain.
1997 static struct domain routedomain; /* or at least forward */
1999 static struct protosw routesw[] = {
2001 .pr_type = SOCK_RAW,
2002 .pr_domain = &routedomain,
2003 .pr_flags = PR_ATOMIC|PR_ADDR,
2004 .pr_output = route_output,
2005 .pr_ctlinput = raw_ctlinput,
2006 .pr_init = raw_init,
2007 .pr_usrreqs = &route_usrreqs
2011 static struct domain routedomain = {
2012 .dom_family = PF_ROUTE,
2013 .dom_name = "route",
2014 .dom_protosw = routesw,
2015 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
2018 VNET_DOMAIN_SET(route);