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/scope6_var.h>
71 #if defined(INET) || defined(INET6)
73 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
77 #ifdef COMPAT_FREEBSD32
78 #include <sys/mount.h>
79 #include <compat/freebsd32/freebsd32.h>
86 uint8_t ifi_link_state;
88 uint8_t ifi_spare_char2;
92 uint32_t ifi_baudrate;
93 uint32_t ifi_ipackets;
95 uint32_t ifi_opackets;
97 uint32_t ifi_collisions;
100 uint32_t ifi_imcasts;
101 uint32_t ifi_omcasts;
102 uint32_t ifi_iqdrops;
103 uint32_t ifi_noproto;
104 uint32_t ifi_hwassist;
106 struct timeval32 ifi_lastchange;
116 struct if_data32 ifm_data;
119 struct if_msghdrl32 {
126 uint16_t _ifm_spare1;
128 uint16_t ifm_data_off;
129 struct if_data32 ifm_data;
132 struct ifa_msghdrl32 {
133 uint16_t ifam_msglen;
134 uint8_t ifam_version;
139 uint16_t _ifam_spare1;
141 uint16_t ifam_data_off;
143 struct if_data32 ifam_data;
145 #endif /* COMPAT_FREEBSD32 */
147 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
149 /* NB: these are not modified */
150 static struct sockaddr route_src = { 2, PF_ROUTE, };
151 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
153 /* These are external hooks for CARP. */
154 int (*carp_get_vhid_p)(struct ifaddr *);
157 * Used by rtsock/raw_input callback code to decide whether to filter the update
158 * notification to a socket bound to a particular FIB.
160 #define RTS_FILTER_FIB M_PROTO8
161 #define RTS_ALLFIBS -1
164 int ip_count; /* attached w/ AF_INET */
165 int ip6_count; /* attached w/ AF_INET6 */
166 int ipx_count; /* attached w/ AF_IPX */
167 int any_count; /* total attached */
170 struct mtx rtsock_mtx;
171 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
173 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
174 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
175 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
177 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
183 struct sysctl_req *w_req;
186 static void rts_input(struct mbuf *m);
187 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
188 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
189 caddr_t cp, struct walkarg *w);
190 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
191 struct rt_addrinfo *rtinfo);
192 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
193 static int sysctl_iflist(int af, struct walkarg *w);
194 static int sysctl_ifmalist(int af, struct walkarg *w);
195 static int route_output(struct mbuf *m, struct socket *so);
196 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
197 struct rt_metrics_lite *out);
198 static void rt_getmetrics(const struct rt_metrics_lite *in,
199 struct rt_metrics *out);
200 static void rt_dispatch(struct mbuf *, sa_family_t);
202 static struct netisr_handler rtsock_nh = {
204 .nh_handler = rts_input,
205 .nh_proto = NETISR_ROUTE,
206 .nh_policy = NETISR_POLICY_SOURCE,
210 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
214 netisr_getqlimit(&rtsock_nh, &qlimit);
215 error = sysctl_handle_int(oidp, &qlimit, 0, req);
216 if (error || !req->newptr)
220 return (netisr_setqlimit(&rtsock_nh, qlimit));
222 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
223 0, 0, sysctl_route_netisr_maxqlen, "I",
224 "maximum routing socket dispatch queue length");
231 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
232 rtsock_nh.nh_qlimit = tmp;
233 netisr_register(&rtsock_nh);
235 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
238 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
243 KASSERT(m != NULL, ("%s: m is NULL", __func__));
244 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
245 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
247 /* No filtering requested. */
248 if ((m->m_flags & RTS_FILTER_FIB) == 0)
251 /* Check if it is a rts and the fib matches the one of the socket. */
252 fibnum = M_GETFIB(m);
253 if (proto->sp_family != PF_ROUTE ||
254 rp->rcb_socket == NULL ||
255 rp->rcb_socket->so_fibnum == fibnum)
258 /* Filtering requested and no match, the socket shall be skipped. */
263 rts_input(struct mbuf *m)
265 struct sockproto route_proto;
266 unsigned short *family;
269 route_proto.sp_family = PF_ROUTE;
270 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
272 family = (unsigned short *)(tag + 1);
273 route_proto.sp_protocol = *family;
274 m_tag_delete(m, tag);
276 route_proto.sp_protocol = 0;
278 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
282 * It really doesn't make any sense at all for this code to share much
283 * with raw_usrreq.c, since its functionality is so restricted. XXX
286 rts_abort(struct socket *so)
289 raw_usrreqs.pru_abort(so);
293 rts_close(struct socket *so)
296 raw_usrreqs.pru_close(so);
299 /* pru_accept is EOPNOTSUPP */
302 rts_attach(struct socket *so, int proto, struct thread *td)
307 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
310 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
315 * The splnet() is necessary to block protocols from sending
316 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
317 * this PCB is extant but incompletely initialized.
318 * Probably we should try to do more of this work beforehand and
322 so->so_pcb = (caddr_t)rp;
323 so->so_fibnum = td->td_proc->p_fibnum;
324 error = raw_attach(so, proto);
333 switch(rp->rcb_proto.sp_protocol) {
338 route_cb.ip6_count++;
341 route_cb.ipx_count++;
344 route_cb.any_count++;
347 so->so_options |= SO_USELOOPBACK;
353 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
356 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
360 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
363 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
366 /* pru_connect2 is EOPNOTSUPP */
367 /* pru_control is EOPNOTSUPP */
370 rts_detach(struct socket *so)
372 struct rawcb *rp = sotorawcb(so);
374 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
377 switch(rp->rcb_proto.sp_protocol) {
382 route_cb.ip6_count--;
385 route_cb.ipx_count--;
388 route_cb.any_count--;
390 raw_usrreqs.pru_detach(so);
394 rts_disconnect(struct socket *so)
397 return (raw_usrreqs.pru_disconnect(so));
400 /* pru_listen is EOPNOTSUPP */
403 rts_peeraddr(struct socket *so, struct sockaddr **nam)
406 return (raw_usrreqs.pru_peeraddr(so, nam));
409 /* pru_rcvd is EOPNOTSUPP */
410 /* pru_rcvoob is EOPNOTSUPP */
413 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
414 struct mbuf *control, struct thread *td)
417 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
420 /* pru_sense is null */
423 rts_shutdown(struct socket *so)
426 return (raw_usrreqs.pru_shutdown(so));
430 rts_sockaddr(struct socket *so, struct sockaddr **nam)
433 return (raw_usrreqs.pru_sockaddr(so, nam));
436 static struct pr_usrreqs route_usrreqs = {
437 .pru_abort = rts_abort,
438 .pru_attach = rts_attach,
439 .pru_bind = rts_bind,
440 .pru_connect = rts_connect,
441 .pru_detach = rts_detach,
442 .pru_disconnect = rts_disconnect,
443 .pru_peeraddr = rts_peeraddr,
444 .pru_send = rts_send,
445 .pru_shutdown = rts_shutdown,
446 .pru_sockaddr = rts_sockaddr,
447 .pru_close = rts_close,
450 #ifndef _SOCKADDR_UNION_DEFINED
451 #define _SOCKADDR_UNION_DEFINED
453 * The union of all possible address formats we handle.
455 union sockaddr_union {
457 struct sockaddr_in sin;
458 struct sockaddr_in6 sin6;
460 #endif /* _SOCKADDR_UNION_DEFINED */
463 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
464 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
467 /* First, see if the returned address is part of the jail. */
468 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
469 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
473 switch (info->rti_info[RTAX_DST]->sa_family) {
483 * Try to find an address on the given outgoing interface
484 * that belongs to the jail.
487 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
490 if (sa->sa_family != AF_INET)
492 ia = ((struct sockaddr_in *)sa)->sin_addr;
493 if (prison_check_ip4(cred, &ia) == 0) {
498 IF_ADDR_RUNLOCK(ifp);
501 * As a last resort return the 'default' jail address.
503 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
505 if (prison_get_ip4(cred, &ia) != 0)
508 bzero(&saun->sin, sizeof(struct sockaddr_in));
509 saun->sin.sin_len = sizeof(struct sockaddr_in);
510 saun->sin.sin_family = AF_INET;
511 saun->sin.sin_addr.s_addr = ia.s_addr;
512 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
525 * Try to find an address on the given outgoing interface
526 * that belongs to the jail.
529 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
532 if (sa->sa_family != AF_INET6)
534 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
535 &ia6, sizeof(struct in6_addr));
536 if (prison_check_ip6(cred, &ia6) == 0) {
541 IF_ADDR_RUNLOCK(ifp);
544 * As a last resort return the 'default' jail address.
546 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
548 if (prison_get_ip6(cred, &ia6) != 0)
551 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
552 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
553 saun->sin6.sin6_family = AF_INET6;
554 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
555 if (sa6_recoverscope(&saun->sin6) != 0)
557 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
569 route_output(struct mbuf *m, struct socket *so)
571 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
572 struct rt_msghdr *rtm = NULL;
573 struct rtentry *rt = NULL;
574 struct radix_node_head *rnh;
575 struct rt_addrinfo info;
577 struct ifnet *ifp = NULL;
578 union sockaddr_union saun;
579 sa_family_t saf = AF_UNSPEC;
581 #define senderr(e) { error = e; goto flush;}
582 if (m == NULL || ((m->m_len < sizeof(long)) &&
583 (m = m_pullup(m, sizeof(long))) == NULL))
585 if ((m->m_flags & M_PKTHDR) == 0)
586 panic("route_output");
587 len = m->m_pkthdr.len;
588 if (len < sizeof(*rtm) ||
589 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
590 info.rti_info[RTAX_DST] = NULL;
593 R_Malloc(rtm, struct rt_msghdr *, len);
595 info.rti_info[RTAX_DST] = NULL;
598 m_copydata(m, 0, len, (caddr_t)rtm);
599 if (rtm->rtm_version != RTM_VERSION) {
600 info.rti_info[RTAX_DST] = NULL;
601 senderr(EPROTONOSUPPORT);
603 rtm->rtm_pid = curproc->p_pid;
604 bzero(&info, sizeof(info));
605 info.rti_addrs = rtm->rtm_addrs;
606 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
607 info.rti_info[RTAX_DST] = NULL;
610 info.rti_flags = rtm->rtm_flags;
611 if (info.rti_info[RTAX_DST] == NULL ||
612 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
613 (info.rti_info[RTAX_GATEWAY] != NULL &&
614 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
616 saf = info.rti_info[RTAX_DST]->sa_family;
618 * Verify that the caller has the appropriate privilege; RTM_GET
619 * is the only operation the non-superuser is allowed.
621 if (rtm->rtm_type != RTM_GET) {
622 error = priv_check(curthread, PRIV_NET_ROUTE);
628 * The given gateway address may be an interface address.
629 * For example, issuing a "route change" command on a route
630 * entry that was created from a tunnel, and the gateway
631 * address given is the local end point. In this case the
632 * RTF_GATEWAY flag must be cleared or the destination will
633 * not be reachable even though there is no error message.
635 if (info.rti_info[RTAX_GATEWAY] != NULL &&
636 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
639 bzero(&gw_ro, sizeof(gw_ro));
640 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
641 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
643 * A host route through the loopback interface is
644 * installed for each interface adddress. In pre 8.0
645 * releases the interface address of a PPP link type
646 * is not reachable locally. This behavior is fixed as
647 * part of the new L2/L3 redesign and rewrite work. The
648 * signature of this interface address route is the
649 * AF_LINK sa_family type of the rt_gateway, and the
650 * rt_ifp has the IFF_LOOPBACK flag set.
652 if (gw_ro.ro_rt != NULL &&
653 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
654 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
655 info.rti_flags &= ~RTF_GATEWAY;
656 if (gw_ro.ro_rt != NULL)
660 switch (rtm->rtm_type) {
661 struct rtentry *saved_nrt;
664 if (info.rti_info[RTAX_GATEWAY] == NULL)
668 /* support for new ARP code */
669 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
670 (rtm->rtm_flags & RTF_LLDATA) != 0) {
671 error = lla_rt_output(rtm, &info);
674 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
676 if (error == 0 && saved_nrt) {
678 rt_setmetrics(rtm->rtm_inits,
679 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
680 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
681 RT_REMREF(saved_nrt);
682 RT_UNLOCK(saved_nrt);
688 /* support for new ARP code */
689 if (info.rti_info[RTAX_GATEWAY] &&
690 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
691 (rtm->rtm_flags & RTF_LLDATA) != 0) {
692 error = lla_rt_output(rtm, &info);
695 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
707 rnh = rt_tables_get_rnh(so->so_fibnum,
708 info.rti_info[RTAX_DST]->sa_family);
710 senderr(EAFNOSUPPORT);
711 RADIX_NODE_HEAD_RLOCK(rnh);
712 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
713 info.rti_info[RTAX_NETMASK], rnh);
714 if (rt == NULL) { /* XXX looks bogus */
715 RADIX_NODE_HEAD_RUNLOCK(rnh);
720 * for RTM_CHANGE/LOCK, if we got multipath routes,
721 * we require users to specify a matching RTAX_GATEWAY.
723 * for RTM_GET, gate is optional even with multipath.
724 * if gate == NULL the first match is returned.
725 * (no need to call rt_mpath_matchgate if gate == NULL)
727 if (rn_mpath_capable(rnh) &&
728 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
729 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
731 RADIX_NODE_HEAD_RUNLOCK(rnh);
737 * If performing proxied L2 entry insertion, and
738 * the actual PPP host entry is found, perform
739 * another search to retrieve the prefix route of
740 * the local end point of the PPP link.
742 if (rtm->rtm_flags & RTF_ANNOUNCE) {
743 struct sockaddr laddr;
745 if (rt->rt_ifp != NULL &&
746 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
749 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
751 rt_maskedcopy(ifa->ifa_addr,
755 rt_maskedcopy(rt->rt_ifa->ifa_addr,
757 rt->rt_ifa->ifa_netmask);
759 * refactor rt and no lock operation necessary
761 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
763 RADIX_NODE_HEAD_RUNLOCK(rnh);
769 RADIX_NODE_HEAD_RUNLOCK(rnh);
774 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
775 * returns a perfect match in case a netmask is
776 * specified. For host routes only a longest prefix
777 * match is returned so it is necessary to compare the
778 * existence of the netmask. If both have a netmask
779 * rnh_lookup() did a perfect match and if none of them
780 * have a netmask both are host routes which is also a
784 if (rtm->rtm_type != RTM_GET &&
785 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
790 switch(rtm->rtm_type) {
795 if ((rt->rt_flags & RTF_HOST) == 0
796 ? jailed_without_vnet(curthread->td_ucred)
797 : prison_if(curthread->td_ucred,
802 info.rti_info[RTAX_DST] = rt_key(rt);
803 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
804 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
805 info.rti_info[RTAX_GENMASK] = 0;
806 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
809 info.rti_info[RTAX_IFP] =
810 ifp->if_addr->ifa_addr;
811 error = rtm_get_jailed(&info, ifp, rt,
812 &saun, curthread->td_ucred);
817 if (ifp->if_flags & IFF_POINTOPOINT)
818 info.rti_info[RTAX_BRD] =
819 rt->rt_ifa->ifa_dstaddr;
820 rtm->rtm_index = ifp->if_index;
822 info.rti_info[RTAX_IFP] = NULL;
823 info.rti_info[RTAX_IFA] = NULL;
825 } else if ((ifp = rt->rt_ifp) != NULL) {
826 rtm->rtm_index = ifp->if_index;
828 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
829 if (len > rtm->rtm_msglen) {
830 struct rt_msghdr *new_rtm;
831 R_Malloc(new_rtm, struct rt_msghdr *, len);
832 if (new_rtm == NULL) {
836 bcopy(rtm, new_rtm, rtm->rtm_msglen);
837 Free(rtm); rtm = new_rtm;
839 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
840 rtm->rtm_flags = rt->rt_flags;
841 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
842 rtm->rtm_addrs = info.rti_addrs;
847 * New gateway could require new ifaddr, ifp;
848 * flags may also be different; ifp may be specified
849 * by ll sockaddr when protocol address is ambiguous
851 if (((rt->rt_flags & RTF_GATEWAY) &&
852 info.rti_info[RTAX_GATEWAY] != NULL) ||
853 info.rti_info[RTAX_IFP] != NULL ||
854 (info.rti_info[RTAX_IFA] != NULL &&
855 !sa_equal(info.rti_info[RTAX_IFA],
856 rt->rt_ifa->ifa_addr))) {
858 RADIX_NODE_HEAD_LOCK(rnh);
859 error = rt_getifa_fib(&info, rt->rt_fibnum);
861 * XXXRW: Really we should release this
862 * reference later, but this maintains
863 * historical behavior.
865 if (info.rti_ifa != NULL)
866 ifa_free(info.rti_ifa);
867 RADIX_NODE_HEAD_UNLOCK(rnh);
872 if (info.rti_ifa != NULL &&
873 info.rti_ifa != rt->rt_ifa &&
874 rt->rt_ifa != NULL &&
875 rt->rt_ifa->ifa_rtrequest != NULL) {
876 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
878 ifa_free(rt->rt_ifa);
880 if (info.rti_info[RTAX_GATEWAY] != NULL) {
882 RADIX_NODE_HEAD_LOCK(rnh);
885 error = rt_setgate(rt, rt_key(rt),
886 info.rti_info[RTAX_GATEWAY]);
887 RADIX_NODE_HEAD_UNLOCK(rnh);
892 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
894 if (info.rti_ifa != NULL &&
895 info.rti_ifa != rt->rt_ifa) {
896 ifa_ref(info.rti_ifa);
897 rt->rt_ifa = info.rti_ifa;
898 rt->rt_ifp = info.rti_ifp;
900 /* Allow some flags to be toggled on change. */
901 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
902 (rtm->rtm_flags & RTF_FMASK);
903 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
905 rtm->rtm_index = rt->rt_ifp->if_index;
906 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
907 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
910 /* We don't support locks anymore */
923 rtm->rtm_errno = error;
925 rtm->rtm_flags |= RTF_DONE;
927 if (rt) /* XXX can this be true? */
930 struct rawcb *rp = NULL;
932 * Check to see if we don't want our own messages.
934 if ((so->so_options & SO_USELOOPBACK) == 0) {
935 if (route_cb.any_count <= 1) {
941 /* There is another listener, so construct message */
945 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
946 if (m->m_pkthdr.len < rtm->rtm_msglen) {
949 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
950 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
953 M_SETFIB(m, so->so_fibnum);
954 m->m_flags |= RTS_FILTER_FIB;
957 * XXX insure we don't get a copy by
958 * invalidating our protocol
960 unsigned short family = rp->rcb_proto.sp_family;
961 rp->rcb_proto.sp_family = 0;
963 rp->rcb_proto.sp_family = family;
967 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
976 rt_setmetrics(u_long which, const struct rt_metrics *in,
977 struct rt_metrics_lite *out)
979 #define metric(f, e) if (which & (f)) out->e = in->e;
981 * Only these are stored in the routing entry since introduction
982 * of tcp hostcache. The rest is ignored.
984 metric(RTV_MTU, rmx_mtu);
985 metric(RTV_WEIGHT, rmx_weight);
986 /* Userland -> kernel timebase conversion. */
987 if (which & RTV_EXPIRE)
988 out->rmx_expire = in->rmx_expire ?
989 in->rmx_expire - time_second + time_uptime : 0;
994 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
996 #define metric(e) out->e = in->e;
997 bzero(out, sizeof(*out));
1000 /* Kernel -> userland timebase conversion. */
1001 out->rmx_expire = in->rmx_expire ?
1002 in->rmx_expire - time_uptime + time_second : 0;
1007 * Extract the addresses of the passed sockaddrs.
1008 * Do a little sanity checking so as to avoid bad memory references.
1009 * This data is derived straight from userland.
1012 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1014 struct sockaddr *sa;
1017 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1018 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1020 sa = (struct sockaddr *)cp;
1024 if (cp + sa->sa_len > cplim)
1027 * there are no more.. quit now
1028 * If there are more bits, they are in error.
1029 * I've seen this. route(1) can evidently generate these.
1030 * This causes kernel to core dump.
1031 * for compatibility, If we see this, point to a safe address.
1033 if (sa->sa_len == 0) {
1034 rtinfo->rti_info[i] = &sa_zero;
1035 return (0); /* should be EINVAL but for compat */
1038 rtinfo->rti_info[i] = sa;
1045 * Used by the routing socket.
1047 static struct mbuf *
1048 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1050 struct rt_msghdr *rtm;
1053 struct sockaddr *sa;
1060 len = sizeof(struct ifa_msghdr);
1065 len = sizeof(struct ifma_msghdr);
1069 len = sizeof(struct if_msghdr);
1072 case RTM_IFANNOUNCE:
1074 len = sizeof(struct if_announcemsghdr);
1078 len = sizeof(struct rt_msghdr);
1082 m = m_gethdr(M_DONTWAIT, MT_DATA);
1083 if (m && len > MHLEN) {
1084 MCLGET(m, M_DONTWAIT);
1085 if ((m->m_flags & M_EXT) == 0) {
1092 m->m_pkthdr.len = m->m_len = len;
1093 m->m_pkthdr.rcvif = NULL;
1094 rtm = mtod(m, struct rt_msghdr *);
1095 bzero((caddr_t)rtm, len);
1096 for (i = 0; i < RTAX_MAX; i++) {
1097 if ((sa = rtinfo->rti_info[i]) == NULL)
1099 rtinfo->rti_addrs |= (1 << i);
1101 m_copyback(m, len, dlen, (caddr_t)sa);
1104 if (m->m_pkthdr.len != len) {
1108 rtm->rtm_msglen = len;
1109 rtm->rtm_version = RTM_VERSION;
1110 rtm->rtm_type = type;
1115 * Used by the sysctl code and routing socket.
1118 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1121 int len, dlen, second_time = 0;
1124 rtinfo->rti_addrs = 0;
1130 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1131 #ifdef COMPAT_FREEBSD32
1132 if (w->w_req->flags & SCTL_MASK32)
1133 len = sizeof(struct ifa_msghdrl32);
1136 len = sizeof(struct ifa_msghdrl);
1138 len = sizeof(struct ifa_msghdr);
1142 #ifdef COMPAT_FREEBSD32
1143 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1144 if (w->w_op == NET_RT_IFLISTL)
1145 len = sizeof(struct if_msghdrl32);
1147 len = sizeof(struct if_msghdr32);
1151 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1152 len = sizeof(struct if_msghdrl);
1154 len = sizeof(struct if_msghdr);
1158 len = sizeof(struct ifma_msghdr);
1162 len = sizeof(struct rt_msghdr);
1167 for (i = 0; i < RTAX_MAX; i++) {
1168 struct sockaddr *sa;
1170 if ((sa = rtinfo->rti_info[i]) == NULL)
1172 rtinfo->rti_addrs |= (1 << i);
1175 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1181 if (cp == NULL && w != NULL && !second_time) {
1182 struct walkarg *rw = w;
1185 if (rw->w_tmemsize < len) {
1187 free(rw->w_tmem, M_RTABLE);
1188 rw->w_tmem = (caddr_t)
1189 malloc(len, M_RTABLE, M_NOWAIT);
1191 rw->w_tmemsize = len;
1201 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1203 rtm->rtm_version = RTM_VERSION;
1204 rtm->rtm_type = type;
1205 rtm->rtm_msglen = len;
1211 * This routine is called to generate a message from the routing
1212 * socket indicating that a redirect has occured, a routing lookup
1213 * has failed, or that a protocol has detected timeouts to a particular
1217 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1220 struct rt_msghdr *rtm;
1222 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1224 if (route_cb.any_count == 0)
1226 m = rt_msg1(type, rtinfo);
1230 if (fibnum != RTS_ALLFIBS) {
1231 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1232 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1233 M_SETFIB(m, fibnum);
1234 m->m_flags |= RTS_FILTER_FIB;
1237 rtm = mtod(m, struct rt_msghdr *);
1238 rtm->rtm_flags = RTF_DONE | flags;
1239 rtm->rtm_errno = error;
1240 rtm->rtm_addrs = rtinfo->rti_addrs;
1241 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1245 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1248 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1252 * This routine is called to generate a message from the routing
1253 * socket indicating that the status of a network interface has changed.
1256 rt_ifmsg(struct ifnet *ifp)
1258 struct if_msghdr *ifm;
1260 struct rt_addrinfo info;
1262 if (route_cb.any_count == 0)
1264 bzero((caddr_t)&info, sizeof(info));
1265 m = rt_msg1(RTM_IFINFO, &info);
1268 ifm = mtod(m, struct if_msghdr *);
1269 ifm->ifm_index = ifp->if_index;
1270 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1271 ifm->ifm_data = ifp->if_data;
1273 rt_dispatch(m, AF_UNSPEC);
1277 * This is called to generate messages from the routing socket
1278 * indicating a network interface has had addresses associated with it.
1279 * if we ever reverse the logic and replace messages TO the routing
1280 * socket indicate a request to configure interfaces, then it will
1281 * be unnecessary as the routing socket will automatically generate
1285 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1288 struct rt_addrinfo info;
1289 struct sockaddr *sa = NULL;
1291 struct mbuf *m = NULL;
1292 struct ifnet *ifp = ifa->ifa_ifp;
1294 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1295 ("unexpected cmd %u", cmd));
1296 #if defined(INET) || defined(INET6)
1299 * notify the SCTP stack
1300 * this will only get called when an address is added/deleted
1301 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1303 sctp_addr_change(ifa, cmd);
1306 if (route_cb.any_count == 0)
1308 for (pass = 1; pass < 3; pass++) {
1309 bzero((caddr_t)&info, sizeof(info));
1310 if ((cmd == RTM_ADD && pass == 1) ||
1311 (cmd == RTM_DELETE && pass == 2)) {
1312 struct ifa_msghdr *ifam;
1313 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1315 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1316 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1317 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1318 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1319 if ((m = rt_msg1(ncmd, &info)) == NULL)
1321 ifam = mtod(m, struct ifa_msghdr *);
1322 ifam->ifam_index = ifp->if_index;
1323 ifam->ifam_metric = ifa->ifa_metric;
1324 ifam->ifam_flags = ifa->ifa_flags;
1325 ifam->ifam_addrs = info.rti_addrs;
1327 if ((cmd == RTM_ADD && pass == 2) ||
1328 (cmd == RTM_DELETE && pass == 1)) {
1329 struct rt_msghdr *rtm;
1333 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1334 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1335 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1336 if ((m = rt_msg1(cmd, &info)) == NULL)
1338 rtm = mtod(m, struct rt_msghdr *);
1339 rtm->rtm_index = ifp->if_index;
1340 rtm->rtm_flags |= rt->rt_flags;
1341 rtm->rtm_errno = error;
1342 rtm->rtm_addrs = info.rti_addrs;
1344 if (fibnum != RTS_ALLFIBS) {
1345 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1346 "fibnum out of range 0 <= %d < %d", __func__,
1347 fibnum, rt_numfibs));
1348 M_SETFIB(m, fibnum);
1349 m->m_flags |= RTS_FILTER_FIB;
1351 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1356 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1359 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1363 * This is the analogue to the rt_newaddrmsg which performs the same
1364 * function but for multicast group memberhips. This is easier since
1365 * there is no route state to worry about.
1368 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1370 struct rt_addrinfo info;
1371 struct mbuf *m = NULL;
1372 struct ifnet *ifp = ifma->ifma_ifp;
1373 struct ifma_msghdr *ifmam;
1375 if (route_cb.any_count == 0)
1378 bzero((caddr_t)&info, sizeof(info));
1379 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1380 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1382 * If a link-layer address is present, present it as a ``gateway''
1383 * (similarly to how ARP entries, e.g., are presented).
1385 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1386 m = rt_msg1(cmd, &info);
1389 ifmam = mtod(m, struct ifma_msghdr *);
1390 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1392 ifmam->ifmam_index = ifp->if_index;
1393 ifmam->ifmam_addrs = info.rti_addrs;
1394 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1397 static struct mbuf *
1398 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1399 struct rt_addrinfo *info)
1401 struct if_announcemsghdr *ifan;
1404 if (route_cb.any_count == 0)
1406 bzero((caddr_t)info, sizeof(*info));
1407 m = rt_msg1(type, info);
1409 ifan = mtod(m, struct if_announcemsghdr *);
1410 ifan->ifan_index = ifp->if_index;
1411 strlcpy(ifan->ifan_name, ifp->if_xname,
1412 sizeof(ifan->ifan_name));
1413 ifan->ifan_what = what;
1419 * This is called to generate routing socket messages indicating
1420 * IEEE80211 wireless events.
1421 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1424 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1427 struct rt_addrinfo info;
1429 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1432 * Append the ieee80211 data. Try to stick it in the
1433 * mbuf containing the ifannounce msg; otherwise allocate
1434 * a new mbuf and append.
1436 * NB: we assume m is a single mbuf.
1438 if (data_len > M_TRAILINGSPACE(m)) {
1439 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1444 bcopy(data, mtod(n, void *), data_len);
1445 n->m_len = data_len;
1447 } else if (data_len > 0) {
1448 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1449 m->m_len += data_len;
1451 if (m->m_flags & M_PKTHDR)
1452 m->m_pkthdr.len += data_len;
1453 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1454 rt_dispatch(m, AF_UNSPEC);
1459 * This is called to generate routing socket messages indicating
1460 * network interface arrival and departure.
1463 rt_ifannouncemsg(struct ifnet *ifp, int what)
1466 struct rt_addrinfo info;
1468 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1470 rt_dispatch(m, AF_UNSPEC);
1474 rt_dispatch(struct mbuf *m, sa_family_t saf)
1479 * Preserve the family from the sockaddr, if any, in an m_tag for
1480 * use when injecting the mbuf into the routing socket buffer from
1483 if (saf != AF_UNSPEC) {
1484 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1490 *(unsigned short *)(tag + 1) = saf;
1491 m_tag_prepend(m, tag);
1495 m->m_pkthdr.rcvif = V_loif;
1501 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1505 * This is used in dumping the kernel table via sysctl().
1508 sysctl_dumpentry(struct radix_node *rn, void *vw)
1510 struct walkarg *w = vw;
1511 struct rtentry *rt = (struct rtentry *)rn;
1512 int error = 0, size;
1513 struct rt_addrinfo info;
1515 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1517 if ((rt->rt_flags & RTF_HOST) == 0
1518 ? jailed_without_vnet(w->w_req->td->td_ucred)
1519 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1521 bzero((caddr_t)&info, sizeof(info));
1522 info.rti_info[RTAX_DST] = rt_key(rt);
1523 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1524 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1525 info.rti_info[RTAX_GENMASK] = 0;
1527 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1528 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1529 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1530 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1532 size = rt_msg2(RTM_GET, &info, NULL, w);
1533 if (w->w_req && w->w_tmem) {
1534 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1536 rtm->rtm_flags = rt->rt_flags;
1538 * let's be honest about this being a retarded hack
1540 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1541 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1542 rtm->rtm_index = rt->rt_ifp->if_index;
1543 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1544 rtm->rtm_addrs = info.rti_addrs;
1545 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1551 #ifdef COMPAT_FREEBSD32
1553 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1556 bzero(dst, sizeof(*dst));
1557 CP(*src, *dst, ifi_type);
1558 CP(*src, *dst, ifi_physical);
1559 CP(*src, *dst, ifi_addrlen);
1560 CP(*src, *dst, ifi_hdrlen);
1561 CP(*src, *dst, ifi_link_state);
1562 CP(*src, *dst, ifi_vhid);
1563 dst->ifi_datalen = sizeof(struct if_data32);
1564 CP(*src, *dst, ifi_mtu);
1565 CP(*src, *dst, ifi_metric);
1566 CP(*src, *dst, ifi_baudrate);
1567 CP(*src, *dst, ifi_ipackets);
1568 CP(*src, *dst, ifi_ierrors);
1569 CP(*src, *dst, ifi_opackets);
1570 CP(*src, *dst, ifi_oerrors);
1571 CP(*src, *dst, ifi_collisions);
1572 CP(*src, *dst, ifi_ibytes);
1573 CP(*src, *dst, ifi_obytes);
1574 CP(*src, *dst, ifi_imcasts);
1575 CP(*src, *dst, ifi_omcasts);
1576 CP(*src, *dst, ifi_iqdrops);
1577 CP(*src, *dst, ifi_noproto);
1578 CP(*src, *dst, ifi_hwassist);
1579 CP(*src, *dst, ifi_epoch);
1580 TV_CP(*src, *dst, ifi_lastchange);
1585 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1586 struct walkarg *w, int len)
1588 struct if_msghdrl *ifm;
1590 #ifdef COMPAT_FREEBSD32
1591 if (w->w_req->flags & SCTL_MASK32) {
1592 struct if_msghdrl32 *ifm32;
1594 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1595 ifm32->ifm_addrs = info->rti_addrs;
1596 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1597 ifm32->ifm_index = ifp->if_index;
1598 ifm32->_ifm_spare1 = 0;
1599 ifm32->ifm_len = sizeof(*ifm32);
1600 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1602 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1603 /* Fixup if_data carp(4) vhid. */
1604 if (carp_get_vhid_p != NULL)
1605 ifm32->ifm_data.ifi_vhid =
1606 (*carp_get_vhid_p)(ifp->if_addr);
1608 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1611 ifm = (struct if_msghdrl *)w->w_tmem;
1612 ifm->ifm_addrs = info->rti_addrs;
1613 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1614 ifm->ifm_index = ifp->if_index;
1615 ifm->_ifm_spare1 = 0;
1616 ifm->ifm_len = sizeof(*ifm);
1617 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1619 ifm->ifm_data = ifp->if_data;
1620 /* Fixup if_data carp(4) vhid. */
1621 if (carp_get_vhid_p != NULL)
1622 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1624 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1628 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1629 struct walkarg *w, int len)
1631 struct if_msghdr *ifm;
1633 #ifdef COMPAT_FREEBSD32
1634 if (w->w_req->flags & SCTL_MASK32) {
1635 struct if_msghdr32 *ifm32;
1637 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1638 ifm32->ifm_addrs = info->rti_addrs;
1639 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1640 ifm32->ifm_index = ifp->if_index;
1642 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1643 /* Fixup if_data carp(4) vhid. */
1644 if (carp_get_vhid_p != NULL)
1645 ifm32->ifm_data.ifi_vhid =
1646 (*carp_get_vhid_p)(ifp->if_addr);
1648 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1651 ifm = (struct if_msghdr *)w->w_tmem;
1652 ifm->ifm_addrs = info->rti_addrs;
1653 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1654 ifm->ifm_index = ifp->if_index;
1656 ifm->ifm_data = ifp->if_data;
1657 /* Fixup if_data carp(4) vhid. */
1658 if (carp_get_vhid_p != NULL)
1659 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1661 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1665 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1666 struct walkarg *w, int len)
1668 struct ifa_msghdrl *ifam;
1670 #ifdef COMPAT_FREEBSD32
1671 if (w->w_req->flags & SCTL_MASK32) {
1672 struct ifa_msghdrl32 *ifam32;
1674 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1675 ifam32->ifam_addrs = info->rti_addrs;
1676 ifam32->ifam_flags = ifa->ifa_flags;
1677 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1678 ifam32->_ifam_spare1 = 0;
1679 ifam32->ifam_len = sizeof(*ifam32);
1680 ifam32->ifam_data_off =
1681 offsetof(struct ifa_msghdrl32, ifam_data);
1682 ifam32->ifam_metric = ifa->ifa_metric;
1684 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1685 /* Fixup if_data carp(4) vhid. */
1686 if (carp_get_vhid_p != NULL)
1687 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1689 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1693 ifam = (struct ifa_msghdrl *)w->w_tmem;
1694 ifam->ifam_addrs = info->rti_addrs;
1695 ifam->ifam_flags = ifa->ifa_flags;
1696 ifam->ifam_index = ifa->ifa_ifp->if_index;
1697 ifam->_ifam_spare1 = 0;
1698 ifam->ifam_len = sizeof(*ifam);
1699 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1700 ifam->ifam_metric = ifa->ifa_metric;
1702 ifam->ifam_data = ifa->if_data;
1703 /* Fixup if_data carp(4) vhid. */
1704 if (carp_get_vhid_p != NULL)
1705 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1707 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1711 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1712 struct walkarg *w, int len)
1714 struct ifa_msghdr *ifam;
1716 ifam = (struct ifa_msghdr *)w->w_tmem;
1717 ifam->ifam_addrs = info->rti_addrs;
1718 ifam->ifam_flags = ifa->ifa_flags;
1719 ifam->ifam_index = ifa->ifa_ifp->if_index;
1720 ifam->ifam_metric = ifa->ifa_metric;
1722 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1726 sysctl_iflist(int af, struct walkarg *w)
1730 struct rt_addrinfo info;
1733 bzero((caddr_t)&info, sizeof(info));
1735 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1736 if (w->w_arg && w->w_arg != ifp->if_index)
1740 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1741 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1742 info.rti_info[RTAX_IFP] = NULL;
1743 if (w->w_req && w->w_tmem) {
1744 if (w->w_op == NET_RT_IFLISTL)
1745 error = sysctl_iflist_ifml(ifp, &info, w, len);
1747 error = sysctl_iflist_ifm(ifp, &info, w, len);
1751 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1752 if (af && af != ifa->ifa_addr->sa_family)
1754 if (prison_if(w->w_req->td->td_ucred,
1755 ifa->ifa_addr) != 0)
1757 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1758 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1759 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1760 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1761 if (w->w_req && w->w_tmem) {
1762 if (w->w_op == NET_RT_IFLISTL)
1763 error = sysctl_iflist_ifaml(ifa, &info,
1766 error = sysctl_iflist_ifam(ifa, &info,
1772 IF_ADDR_RUNLOCK(ifp);
1773 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1774 info.rti_info[RTAX_BRD] = NULL;
1778 IF_ADDR_RUNLOCK(ifp);
1784 sysctl_ifmalist(int af, struct walkarg *w)
1787 struct ifmultiaddr *ifma;
1788 struct rt_addrinfo info;
1792 bzero((caddr_t)&info, sizeof(info));
1794 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1795 if (w->w_arg && w->w_arg != ifp->if_index)
1798 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1800 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1801 if (af && af != ifma->ifma_addr->sa_family)
1803 if (prison_if(w->w_req->td->td_ucred,
1804 ifma->ifma_addr) != 0)
1806 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1807 info.rti_info[RTAX_GATEWAY] =
1808 (ifma->ifma_addr->sa_family != AF_LINK) ?
1809 ifma->ifma_lladdr : NULL;
1810 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1811 if (w->w_req && w->w_tmem) {
1812 struct ifma_msghdr *ifmam;
1814 ifmam = (struct ifma_msghdr *)w->w_tmem;
1815 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1816 ifmam->ifmam_flags = 0;
1817 ifmam->ifmam_addrs = info.rti_addrs;
1818 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1820 IF_ADDR_RUNLOCK(ifp);
1825 IF_ADDR_RUNLOCK(ifp);
1833 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1835 int *name = (int *)arg1;
1836 u_int namelen = arg2;
1837 struct radix_node_head *rnh = NULL; /* silence compiler. */
1838 int i, lim, error = EINVAL;
1847 return ((namelen < 3) ? EISDIR : ENOTDIR);
1851 bzero(&w, sizeof(w));
1856 error = sysctl_wire_old_buffer(req, 0);
1863 if (af == 0) { /* dump all tables */
1866 } else /* dump only one table */
1870 * take care of llinfo entries, the caller must
1873 if (w.w_op == NET_RT_FLAGS &&
1874 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1876 error = lltable_sysctl_dumparp(af, w.w_req);
1882 * take care of routing entries
1884 for (error = 0; error == 0 && i <= lim; i++) {
1885 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1887 RADIX_NODE_HEAD_LOCK(rnh);
1888 error = rnh->rnh_walktree(rnh,
1889 sysctl_dumpentry, &w);
1890 RADIX_NODE_HEAD_UNLOCK(rnh);
1892 error = EAFNOSUPPORT;
1897 case NET_RT_IFLISTL:
1898 error = sysctl_iflist(af, &w);
1901 case NET_RT_IFMALIST:
1902 error = sysctl_ifmalist(af, &w);
1906 free(w.w_tmem, M_RTABLE);
1910 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1913 * Definitions of protocols supported in the ROUTE domain.
1916 static struct domain routedomain; /* or at least forward */
1918 static struct protosw routesw[] = {
1920 .pr_type = SOCK_RAW,
1921 .pr_domain = &routedomain,
1922 .pr_flags = PR_ATOMIC|PR_ADDR,
1923 .pr_output = route_output,
1924 .pr_ctlinput = raw_ctlinput,
1925 .pr_init = raw_init,
1926 .pr_usrreqs = &route_usrreqs
1930 static struct domain routedomain = {
1931 .dom_family = PF_ROUTE,
1932 .dom_name = "route",
1933 .dom_protosw = routesw,
1934 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1937 VNET_DOMAIN_SET(route);