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>
67 #include <netinet6/scope6_var.h>
70 #if defined(INET) || defined(INET6)
72 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
76 #ifdef COMPAT_FREEBSD32
77 #include <sys/mount.h>
78 #include <compat/freebsd32/freebsd32.h>
85 uint8_t ifi_link_state;
86 uint8_t ifi_spare_char1;
87 uint8_t ifi_spare_char2;
91 uint32_t ifi_baudrate;
92 uint32_t ifi_ipackets;
94 uint32_t ifi_opackets;
96 uint32_t ifi_collisions;
100 uint32_t ifi_omcasts;
101 uint32_t ifi_iqdrops;
102 uint32_t ifi_noproto;
103 uint32_t ifi_hwassist;
105 struct timeval32 ifi_lastchange;
115 struct if_data32 ifm_data;
118 struct if_msghdrl32 {
125 uint16_t _ifm_spare1;
127 uint16_t ifm_data_off;
128 struct if_data32 ifm_data;
131 struct ifa_msghdrl32 {
132 uint16_t ifam_msglen;
133 uint8_t ifam_version;
138 uint16_t _ifam_spare1;
140 uint16_t ifam_data_off;
142 struct if_data32 ifam_data;
144 #endif /* COMPAT_FREEBSD32 */
146 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
148 /* NB: these are not modified */
149 static struct sockaddr route_src = { 2, PF_ROUTE, };
150 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
153 * Used by rtsock/raw_input callback code to decide whether to filter the update
154 * notification to a socket bound to a particular FIB.
156 #define RTS_FILTER_FIB M_PROTO8
157 #define RTS_ALLFIBS -1
160 int ip_count; /* attached w/ AF_INET */
161 int ip6_count; /* attached w/ AF_INET6 */
162 int ipx_count; /* attached w/ AF_IPX */
163 int any_count; /* total attached */
166 struct mtx rtsock_mtx;
167 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
169 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
170 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
171 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
173 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
179 struct sysctl_req *w_req;
182 static void rts_input(struct mbuf *m);
183 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
184 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
185 caddr_t cp, struct walkarg *w);
186 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
187 struct rt_addrinfo *rtinfo);
188 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
189 static int sysctl_iflist(int af, struct walkarg *w);
190 static int sysctl_ifmalist(int af, struct walkarg *w);
191 static int route_output(struct mbuf *m, struct socket *so);
192 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
193 struct rt_metrics_lite *out);
194 static void rt_getmetrics(const struct rt_metrics_lite *in,
195 struct rt_metrics *out);
196 static void rt_dispatch(struct mbuf *, sa_family_t);
198 static struct netisr_handler rtsock_nh = {
200 .nh_handler = rts_input,
201 .nh_proto = NETISR_ROUTE,
202 .nh_policy = NETISR_POLICY_SOURCE,
206 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
210 netisr_getqlimit(&rtsock_nh, &qlimit);
211 error = sysctl_handle_int(oidp, &qlimit, 0, req);
212 if (error || !req->newptr)
216 return (netisr_setqlimit(&rtsock_nh, qlimit));
218 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
219 0, 0, sysctl_route_netisr_maxqlen, "I",
220 "maximum routing socket dispatch queue length");
227 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
228 rtsock_nh.nh_qlimit = tmp;
229 netisr_register(&rtsock_nh);
231 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
234 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
239 KASSERT(m != NULL, ("%s: m is NULL", __func__));
240 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
241 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
243 /* No filtering requested. */
244 if ((m->m_flags & RTS_FILTER_FIB) == 0)
247 /* Check if it is a rts and the fib matches the one of the socket. */
248 fibnum = M_GETFIB(m);
249 if (proto->sp_family != PF_ROUTE ||
250 rp->rcb_socket == NULL ||
251 rp->rcb_socket->so_fibnum == fibnum)
254 /* Filtering requested and no match, the socket shall be skipped. */
259 rts_input(struct mbuf *m)
261 struct sockproto route_proto;
262 unsigned short *family;
265 route_proto.sp_family = PF_ROUTE;
266 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
268 family = (unsigned short *)(tag + 1);
269 route_proto.sp_protocol = *family;
270 m_tag_delete(m, tag);
272 route_proto.sp_protocol = 0;
274 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
278 * It really doesn't make any sense at all for this code to share much
279 * with raw_usrreq.c, since its functionality is so restricted. XXX
282 rts_abort(struct socket *so)
285 raw_usrreqs.pru_abort(so);
289 rts_close(struct socket *so)
292 raw_usrreqs.pru_close(so);
295 /* pru_accept is EOPNOTSUPP */
298 rts_attach(struct socket *so, int proto, struct thread *td)
303 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
306 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
311 * The splnet() is necessary to block protocols from sending
312 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
313 * this PCB is extant but incompletely initialized.
314 * Probably we should try to do more of this work beforehand and
318 so->so_pcb = (caddr_t)rp;
319 so->so_fibnum = td->td_proc->p_fibnum;
320 error = raw_attach(so, proto);
329 switch(rp->rcb_proto.sp_protocol) {
334 route_cb.ip6_count++;
337 route_cb.ipx_count++;
340 route_cb.any_count++;
343 so->so_options |= SO_USELOOPBACK;
349 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
352 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
356 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
359 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
362 /* pru_connect2 is EOPNOTSUPP */
363 /* pru_control is EOPNOTSUPP */
366 rts_detach(struct socket *so)
368 struct rawcb *rp = sotorawcb(so);
370 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
373 switch(rp->rcb_proto.sp_protocol) {
378 route_cb.ip6_count--;
381 route_cb.ipx_count--;
384 route_cb.any_count--;
386 raw_usrreqs.pru_detach(so);
390 rts_disconnect(struct socket *so)
393 return (raw_usrreqs.pru_disconnect(so));
396 /* pru_listen is EOPNOTSUPP */
399 rts_peeraddr(struct socket *so, struct sockaddr **nam)
402 return (raw_usrreqs.pru_peeraddr(so, nam));
405 /* pru_rcvd is EOPNOTSUPP */
406 /* pru_rcvoob is EOPNOTSUPP */
409 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
410 struct mbuf *control, struct thread *td)
413 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
416 /* pru_sense is null */
419 rts_shutdown(struct socket *so)
422 return (raw_usrreqs.pru_shutdown(so));
426 rts_sockaddr(struct socket *so, struct sockaddr **nam)
429 return (raw_usrreqs.pru_sockaddr(so, nam));
432 static struct pr_usrreqs route_usrreqs = {
433 .pru_abort = rts_abort,
434 .pru_attach = rts_attach,
435 .pru_bind = rts_bind,
436 .pru_connect = rts_connect,
437 .pru_detach = rts_detach,
438 .pru_disconnect = rts_disconnect,
439 .pru_peeraddr = rts_peeraddr,
440 .pru_send = rts_send,
441 .pru_shutdown = rts_shutdown,
442 .pru_sockaddr = rts_sockaddr,
443 .pru_close = rts_close,
446 #ifndef _SOCKADDR_UNION_DEFINED
447 #define _SOCKADDR_UNION_DEFINED
449 * The union of all possible address formats we handle.
451 union sockaddr_union {
453 struct sockaddr_in sin;
454 struct sockaddr_in6 sin6;
456 #endif /* _SOCKADDR_UNION_DEFINED */
459 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
460 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
463 /* First, see if the returned address is part of the jail. */
464 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
465 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
469 switch (info->rti_info[RTAX_DST]->sa_family) {
479 * Try to find an address on the given outgoing interface
480 * that belongs to the jail.
483 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
486 if (sa->sa_family != AF_INET)
488 ia = ((struct sockaddr_in *)sa)->sin_addr;
489 if (prison_check_ip4(cred, &ia) == 0) {
497 * As a last resort return the 'default' jail address.
499 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
501 if (prison_get_ip4(cred, &ia) != 0)
504 bzero(&saun->sin, sizeof(struct sockaddr_in));
505 saun->sin.sin_len = sizeof(struct sockaddr_in);
506 saun->sin.sin_family = AF_INET;
507 saun->sin.sin_addr.s_addr = ia.s_addr;
508 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
521 * Try to find an address on the given outgoing interface
522 * that belongs to the jail.
525 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
528 if (sa->sa_family != AF_INET6)
530 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
531 &ia6, sizeof(struct in6_addr));
532 if (prison_check_ip6(cred, &ia6) == 0) {
540 * As a last resort return the 'default' jail address.
542 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
544 if (prison_get_ip6(cred, &ia6) != 0)
547 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
548 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
549 saun->sin6.sin6_family = AF_INET6;
550 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
551 if (sa6_recoverscope(&saun->sin6) != 0)
553 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
565 route_output(struct mbuf *m, struct socket *so)
567 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
568 struct rt_msghdr *rtm = NULL;
569 struct rtentry *rt = NULL;
570 struct radix_node_head *rnh;
571 struct rt_addrinfo info;
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;
602 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
603 info.rti_info[RTAX_DST] = NULL;
606 info.rti_flags = rtm->rtm_flags;
607 if (info.rti_info[RTAX_DST] == NULL ||
608 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
609 (info.rti_info[RTAX_GATEWAY] != NULL &&
610 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
612 saf = info.rti_info[RTAX_DST]->sa_family;
614 * Verify that the caller has the appropriate privilege; RTM_GET
615 * is the only operation the non-superuser is allowed.
617 if (rtm->rtm_type != RTM_GET) {
618 error = priv_check(curthread, PRIV_NET_ROUTE);
624 * The given gateway address may be an interface address.
625 * For example, issuing a "route change" command on a route
626 * entry that was created from a tunnel, and the gateway
627 * address given is the local end point. In this case the
628 * RTF_GATEWAY flag must be cleared or the destination will
629 * not be reachable even though there is no error message.
631 if (info.rti_info[RTAX_GATEWAY] != NULL &&
632 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
635 bzero(&gw_ro, sizeof(gw_ro));
636 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
637 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
639 * A host route through the loopback interface is
640 * installed for each interface adddress. In pre 8.0
641 * releases the interface address of a PPP link type
642 * is not reachable locally. This behavior is fixed as
643 * part of the new L2/L3 redesign and rewrite work. The
644 * signature of this interface address route is the
645 * AF_LINK sa_family type of the rt_gateway, and the
646 * rt_ifp has the IFF_LOOPBACK flag set.
648 if (gw_ro.ro_rt != NULL &&
649 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
650 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
651 info.rti_flags &= ~RTF_GATEWAY;
652 if (gw_ro.ro_rt != NULL)
656 switch (rtm->rtm_type) {
657 struct rtentry *saved_nrt;
660 if (info.rti_info[RTAX_GATEWAY] == NULL)
664 /* support for new ARP code */
665 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
666 (rtm->rtm_flags & RTF_LLDATA) != 0) {
667 error = lla_rt_output(rtm, &info);
670 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
672 if (error == 0 && saved_nrt) {
674 rt_setmetrics(rtm->rtm_inits,
675 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
676 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
677 RT_REMREF(saved_nrt);
678 RT_UNLOCK(saved_nrt);
684 /* support for new ARP code */
685 if (info.rti_info[RTAX_GATEWAY] &&
686 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
687 (rtm->rtm_flags & RTF_LLDATA) != 0) {
688 error = lla_rt_output(rtm, &info);
691 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
703 rnh = rt_tables_get_rnh(so->so_fibnum,
704 info.rti_info[RTAX_DST]->sa_family);
706 senderr(EAFNOSUPPORT);
707 RADIX_NODE_HEAD_RLOCK(rnh);
708 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
709 info.rti_info[RTAX_NETMASK], rnh);
710 if (rt == NULL) { /* XXX looks bogus */
711 RADIX_NODE_HEAD_RUNLOCK(rnh);
716 * for RTM_CHANGE/LOCK, if we got multipath routes,
717 * we require users to specify a matching RTAX_GATEWAY.
719 * for RTM_GET, gate is optional even with multipath.
720 * if gate == NULL the first match is returned.
721 * (no need to call rt_mpath_matchgate if gate == NULL)
723 if (rn_mpath_capable(rnh) &&
724 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
725 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
727 RADIX_NODE_HEAD_RUNLOCK(rnh);
733 * If performing proxied L2 entry insertion, and
734 * the actual PPP host entry is found, perform
735 * another search to retrieve the prefix route of
736 * the local end point of the PPP link.
738 if (rtm->rtm_flags & RTF_ANNOUNCE) {
739 struct sockaddr laddr;
741 if (rt->rt_ifp != NULL &&
742 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
745 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
747 rt_maskedcopy(ifa->ifa_addr,
751 rt_maskedcopy(rt->rt_ifa->ifa_addr,
753 rt->rt_ifa->ifa_netmask);
755 * refactor rt and no lock operation necessary
757 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
759 RADIX_NODE_HEAD_RUNLOCK(rnh);
765 RADIX_NODE_HEAD_RUNLOCK(rnh);
770 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
771 * returns a perfect match in case a netmask is
772 * specified. For host routes only a longest prefix
773 * match is returned so it is necessary to compare the
774 * existence of the netmask. If both have a netmask
775 * rnh_lookup() did a perfect match and if none of them
776 * have a netmask both are host routes which is also a
780 if (rtm->rtm_type != RTM_GET &&
781 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
786 switch(rtm->rtm_type) {
791 if ((rt->rt_flags & RTF_HOST) == 0
792 ? jailed_without_vnet(curthread->td_ucred)
793 : prison_if(curthread->td_ucred,
798 info.rti_info[RTAX_DST] = rt_key(rt);
799 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
800 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
801 info.rti_info[RTAX_GENMASK] = 0;
802 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
805 info.rti_info[RTAX_IFP] =
806 ifp->if_addr->ifa_addr;
807 error = rtm_get_jailed(&info, ifp, rt,
808 &saun, curthread->td_ucred);
813 if (ifp->if_flags & IFF_POINTOPOINT)
814 info.rti_info[RTAX_BRD] =
815 rt->rt_ifa->ifa_dstaddr;
816 rtm->rtm_index = ifp->if_index;
818 info.rti_info[RTAX_IFP] = NULL;
819 info.rti_info[RTAX_IFA] = NULL;
821 } else if ((ifp = rt->rt_ifp) != NULL) {
822 rtm->rtm_index = ifp->if_index;
824 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
825 if (len > rtm->rtm_msglen) {
826 struct rt_msghdr *new_rtm;
827 R_Malloc(new_rtm, struct rt_msghdr *, len);
828 if (new_rtm == NULL) {
832 bcopy(rtm, new_rtm, rtm->rtm_msglen);
833 Free(rtm); rtm = new_rtm;
835 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
836 rtm->rtm_flags = rt->rt_flags;
837 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
838 rtm->rtm_addrs = info.rti_addrs;
843 * New gateway could require new ifaddr, ifp;
844 * flags may also be different; ifp may be specified
845 * by ll sockaddr when protocol address is ambiguous
847 if (((rt->rt_flags & RTF_GATEWAY) &&
848 info.rti_info[RTAX_GATEWAY] != NULL) ||
849 info.rti_info[RTAX_IFP] != NULL ||
850 (info.rti_info[RTAX_IFA] != NULL &&
851 !sa_equal(info.rti_info[RTAX_IFA],
852 rt->rt_ifa->ifa_addr))) {
854 RADIX_NODE_HEAD_LOCK(rnh);
855 error = rt_getifa_fib(&info, rt->rt_fibnum);
857 * XXXRW: Really we should release this
858 * reference later, but this maintains
859 * historical behavior.
861 if (info.rti_ifa != NULL)
862 ifa_free(info.rti_ifa);
863 RADIX_NODE_HEAD_UNLOCK(rnh);
868 if (info.rti_ifa != NULL &&
869 info.rti_ifa != rt->rt_ifa &&
870 rt->rt_ifa != NULL &&
871 rt->rt_ifa->ifa_rtrequest != NULL) {
872 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
874 ifa_free(rt->rt_ifa);
876 if (info.rti_info[RTAX_GATEWAY] != NULL) {
878 RADIX_NODE_HEAD_LOCK(rnh);
881 error = rt_setgate(rt, rt_key(rt),
882 info.rti_info[RTAX_GATEWAY]);
883 RADIX_NODE_HEAD_UNLOCK(rnh);
888 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
890 if (info.rti_ifa != NULL &&
891 info.rti_ifa != rt->rt_ifa) {
892 ifa_ref(info.rti_ifa);
893 rt->rt_ifa = info.rti_ifa;
894 rt->rt_ifp = info.rti_ifp;
896 /* Allow some flags to be toggled on change. */
897 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
898 (rtm->rtm_flags & RTF_FMASK);
899 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
901 rtm->rtm_index = rt->rt_ifp->if_index;
902 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
903 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
906 /* We don't support locks anymore */
919 rtm->rtm_errno = error;
921 rtm->rtm_flags |= RTF_DONE;
923 if (rt) /* XXX can this be true? */
926 struct rawcb *rp = NULL;
928 * Check to see if we don't want our own messages.
930 if ((so->so_options & SO_USELOOPBACK) == 0) {
931 if (route_cb.any_count <= 1) {
937 /* There is another listener, so construct message */
941 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
942 if (m->m_pkthdr.len < rtm->rtm_msglen) {
945 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
946 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
950 M_SETFIB(m, so->so_fibnum);
951 m->m_flags |= RTS_FILTER_FIB;
954 * XXX insure we don't get a copy by
955 * invalidating our protocol
957 unsigned short family = rp->rcb_proto.sp_family;
958 rp->rcb_proto.sp_family = 0;
960 rp->rcb_proto.sp_family = family;
970 rt_setmetrics(u_long which, const struct rt_metrics *in,
971 struct rt_metrics_lite *out)
973 #define metric(f, e) if (which & (f)) out->e = in->e;
975 * Only these are stored in the routing entry since introduction
976 * of tcp hostcache. The rest is ignored.
978 metric(RTV_MTU, rmx_mtu);
979 metric(RTV_WEIGHT, rmx_weight);
980 /* Userland -> kernel timebase conversion. */
981 if (which & RTV_EXPIRE)
982 out->rmx_expire = in->rmx_expire ?
983 in->rmx_expire - time_second + time_uptime : 0;
988 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
990 #define metric(e) out->e = in->e;
991 bzero(out, sizeof(*out));
994 /* Kernel -> userland timebase conversion. */
995 out->rmx_expire = in->rmx_expire ?
996 in->rmx_expire - time_uptime + time_second : 0;
1001 * Extract the addresses of the passed sockaddrs.
1002 * Do a little sanity checking so as to avoid bad memory references.
1003 * This data is derived straight from userland.
1006 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1008 struct sockaddr *sa;
1011 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1012 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1014 sa = (struct sockaddr *)cp;
1018 if (cp + sa->sa_len > cplim)
1021 * there are no more.. quit now
1022 * If there are more bits, they are in error.
1023 * I've seen this. route(1) can evidently generate these.
1024 * This causes kernel to core dump.
1025 * for compatibility, If we see this, point to a safe address.
1027 if (sa->sa_len == 0) {
1028 rtinfo->rti_info[i] = &sa_zero;
1029 return (0); /* should be EINVAL but for compat */
1032 rtinfo->rti_info[i] = sa;
1039 * Used by the routing socket.
1041 static struct mbuf *
1042 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1044 struct rt_msghdr *rtm;
1047 struct sockaddr *sa;
1054 len = sizeof(struct ifa_msghdr);
1059 len = sizeof(struct ifma_msghdr);
1063 len = sizeof(struct if_msghdr);
1066 case RTM_IFANNOUNCE:
1068 len = sizeof(struct if_announcemsghdr);
1072 len = sizeof(struct rt_msghdr);
1076 m = m_gethdr(M_DONTWAIT, MT_DATA);
1077 if (m && len > MHLEN) {
1078 MCLGET(m, M_DONTWAIT);
1079 if ((m->m_flags & M_EXT) == 0) {
1086 m->m_pkthdr.len = m->m_len = len;
1087 m->m_pkthdr.rcvif = NULL;
1088 rtm = mtod(m, struct rt_msghdr *);
1089 bzero((caddr_t)rtm, len);
1090 for (i = 0; i < RTAX_MAX; i++) {
1091 if ((sa = rtinfo->rti_info[i]) == NULL)
1093 rtinfo->rti_addrs |= (1 << i);
1095 m_copyback(m, len, dlen, (caddr_t)sa);
1098 if (m->m_pkthdr.len != len) {
1102 rtm->rtm_msglen = len;
1103 rtm->rtm_version = RTM_VERSION;
1104 rtm->rtm_type = type;
1109 * Used by the sysctl code and routing socket.
1112 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1115 int len, dlen, second_time = 0;
1118 rtinfo->rti_addrs = 0;
1124 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1125 #ifdef COMPAT_FREEBSD32
1126 if (w->w_req->flags & SCTL_MASK32)
1127 len = sizeof(struct ifa_msghdrl32);
1130 len = sizeof(struct ifa_msghdrl);
1132 len = sizeof(struct ifa_msghdr);
1136 #ifdef COMPAT_FREEBSD32
1137 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1138 if (w->w_op == NET_RT_IFLISTL)
1139 len = sizeof(struct if_msghdrl32);
1141 len = sizeof(struct if_msghdr32);
1145 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1146 len = sizeof(struct if_msghdrl);
1148 len = sizeof(struct if_msghdr);
1152 len = sizeof(struct ifma_msghdr);
1156 len = sizeof(struct rt_msghdr);
1161 for (i = 0; i < RTAX_MAX; i++) {
1162 struct sockaddr *sa;
1164 if ((sa = rtinfo->rti_info[i]) == NULL)
1166 rtinfo->rti_addrs |= (1 << i);
1169 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1175 if (cp == NULL && w != NULL && !second_time) {
1176 struct walkarg *rw = w;
1179 if (rw->w_tmemsize < len) {
1181 free(rw->w_tmem, M_RTABLE);
1182 rw->w_tmem = (caddr_t)
1183 malloc(len, M_RTABLE, M_NOWAIT);
1185 rw->w_tmemsize = len;
1195 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1197 rtm->rtm_version = RTM_VERSION;
1198 rtm->rtm_type = type;
1199 rtm->rtm_msglen = len;
1205 * This routine is called to generate a message from the routing
1206 * socket indicating that a redirect has occured, a routing lookup
1207 * has failed, or that a protocol has detected timeouts to a particular
1211 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1214 struct rt_msghdr *rtm;
1216 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1218 if (route_cb.any_count == 0)
1220 m = rt_msg1(type, rtinfo);
1224 if (fibnum != RTS_ALLFIBS) {
1225 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1226 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1227 M_SETFIB(m, fibnum);
1228 m->m_flags |= RTS_FILTER_FIB;
1231 rtm = mtod(m, struct rt_msghdr *);
1232 rtm->rtm_flags = RTF_DONE | flags;
1233 rtm->rtm_errno = error;
1234 rtm->rtm_addrs = rtinfo->rti_addrs;
1235 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1239 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1242 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1246 * This routine is called to generate a message from the routing
1247 * socket indicating that the status of a network interface has changed.
1250 rt_ifmsg(struct ifnet *ifp)
1252 struct if_msghdr *ifm;
1254 struct rt_addrinfo info;
1256 if (route_cb.any_count == 0)
1258 bzero((caddr_t)&info, sizeof(info));
1259 m = rt_msg1(RTM_IFINFO, &info);
1262 ifm = mtod(m, struct if_msghdr *);
1263 ifm->ifm_index = ifp->if_index;
1264 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1265 ifm->ifm_data = ifp->if_data;
1267 rt_dispatch(m, AF_UNSPEC);
1271 * This is called to generate messages from the routing socket
1272 * indicating a network interface has had addresses associated with it.
1273 * if we ever reverse the logic and replace messages TO the routing
1274 * socket indicate a request to configure interfaces, then it will
1275 * be unnecessary as the routing socket will automatically generate
1279 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1282 struct rt_addrinfo info;
1283 struct sockaddr *sa = NULL;
1285 struct mbuf *m = NULL;
1286 struct ifnet *ifp = ifa->ifa_ifp;
1288 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1289 ("unexpected cmd %u", cmd));
1290 #if defined(INET) || defined(INET6)
1293 * notify the SCTP stack
1294 * this will only get called when an address is added/deleted
1295 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1297 sctp_addr_change(ifa, cmd);
1300 if (route_cb.any_count == 0)
1302 for (pass = 1; pass < 3; pass++) {
1303 bzero((caddr_t)&info, sizeof(info));
1304 if ((cmd == RTM_ADD && pass == 1) ||
1305 (cmd == RTM_DELETE && pass == 2)) {
1306 struct ifa_msghdr *ifam;
1307 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1309 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1310 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1311 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1312 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1313 if ((m = rt_msg1(ncmd, &info)) == NULL)
1315 ifam = mtod(m, struct ifa_msghdr *);
1316 ifam->ifam_index = ifp->if_index;
1317 ifam->ifam_metric = ifa->ifa_metric;
1318 ifam->ifam_flags = ifa->ifa_flags;
1319 ifam->ifam_addrs = info.rti_addrs;
1321 if ((cmd == RTM_ADD && pass == 2) ||
1322 (cmd == RTM_DELETE && pass == 1)) {
1323 struct rt_msghdr *rtm;
1327 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1328 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1329 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1330 if ((m = rt_msg1(cmd, &info)) == NULL)
1332 rtm = mtod(m, struct rt_msghdr *);
1333 rtm->rtm_index = ifp->if_index;
1334 rtm->rtm_flags |= rt->rt_flags;
1335 rtm->rtm_errno = error;
1336 rtm->rtm_addrs = info.rti_addrs;
1338 if (fibnum != RTS_ALLFIBS) {
1339 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1340 "fibnum out of range 0 <= %d < %d", __func__,
1341 fibnum, rt_numfibs));
1342 M_SETFIB(m, fibnum);
1343 m->m_flags |= RTS_FILTER_FIB;
1345 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1350 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1353 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1357 * This is the analogue to the rt_newaddrmsg which performs the same
1358 * function but for multicast group memberhips. This is easier since
1359 * there is no route state to worry about.
1362 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1364 struct rt_addrinfo info;
1365 struct mbuf *m = NULL;
1366 struct ifnet *ifp = ifma->ifma_ifp;
1367 struct ifma_msghdr *ifmam;
1369 if (route_cb.any_count == 0)
1372 bzero((caddr_t)&info, sizeof(info));
1373 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1374 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1376 * If a link-layer address is present, present it as a ``gateway''
1377 * (similarly to how ARP entries, e.g., are presented).
1379 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1380 m = rt_msg1(cmd, &info);
1383 ifmam = mtod(m, struct ifma_msghdr *);
1384 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1386 ifmam->ifmam_index = ifp->if_index;
1387 ifmam->ifmam_addrs = info.rti_addrs;
1388 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1391 static struct mbuf *
1392 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1393 struct rt_addrinfo *info)
1395 struct if_announcemsghdr *ifan;
1398 if (route_cb.any_count == 0)
1400 bzero((caddr_t)info, sizeof(*info));
1401 m = rt_msg1(type, info);
1403 ifan = mtod(m, struct if_announcemsghdr *);
1404 ifan->ifan_index = ifp->if_index;
1405 strlcpy(ifan->ifan_name, ifp->if_xname,
1406 sizeof(ifan->ifan_name));
1407 ifan->ifan_what = what;
1413 * This is called to generate routing socket messages indicating
1414 * IEEE80211 wireless events.
1415 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1418 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1421 struct rt_addrinfo info;
1423 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1426 * Append the ieee80211 data. Try to stick it in the
1427 * mbuf containing the ifannounce msg; otherwise allocate
1428 * a new mbuf and append.
1430 * NB: we assume m is a single mbuf.
1432 if (data_len > M_TRAILINGSPACE(m)) {
1433 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1438 bcopy(data, mtod(n, void *), data_len);
1439 n->m_len = data_len;
1441 } else if (data_len > 0) {
1442 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1443 m->m_len += data_len;
1445 if (m->m_flags & M_PKTHDR)
1446 m->m_pkthdr.len += data_len;
1447 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1448 rt_dispatch(m, AF_UNSPEC);
1453 * This is called to generate routing socket messages indicating
1454 * network interface arrival and departure.
1457 rt_ifannouncemsg(struct ifnet *ifp, int what)
1460 struct rt_addrinfo info;
1462 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1464 rt_dispatch(m, AF_UNSPEC);
1468 rt_dispatch(struct mbuf *m, sa_family_t saf)
1473 * Preserve the family from the sockaddr, if any, in an m_tag for
1474 * use when injecting the mbuf into the routing socket buffer from
1477 if (saf != AF_UNSPEC) {
1478 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1484 *(unsigned short *)(tag + 1) = saf;
1485 m_tag_prepend(m, tag);
1489 m->m_pkthdr.rcvif = V_loif;
1495 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1499 * This is used in dumping the kernel table via sysctl().
1502 sysctl_dumpentry(struct radix_node *rn, void *vw)
1504 struct walkarg *w = vw;
1505 struct rtentry *rt = (struct rtentry *)rn;
1506 int error = 0, size;
1507 struct rt_addrinfo info;
1509 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1511 if ((rt->rt_flags & RTF_HOST) == 0
1512 ? jailed_without_vnet(w->w_req->td->td_ucred)
1513 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1515 bzero((caddr_t)&info, sizeof(info));
1516 info.rti_info[RTAX_DST] = rt_key(rt);
1517 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1518 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1519 info.rti_info[RTAX_GENMASK] = 0;
1521 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1522 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1523 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1524 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1526 size = rt_msg2(RTM_GET, &info, NULL, w);
1527 if (w->w_req && w->w_tmem) {
1528 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1530 rtm->rtm_flags = rt->rt_flags;
1532 * let's be honest about this being a retarded hack
1534 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1535 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1536 rtm->rtm_index = rt->rt_ifp->if_index;
1537 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1538 rtm->rtm_addrs = info.rti_addrs;
1539 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1545 #ifdef COMPAT_FREEBSD32
1547 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1550 bzero(dst, sizeof(*dst));
1551 CP(*src, *dst, ifi_type);
1552 CP(*src, *dst, ifi_physical);
1553 CP(*src, *dst, ifi_addrlen);
1554 CP(*src, *dst, ifi_hdrlen);
1555 CP(*src, *dst, ifi_link_state);
1556 dst->ifi_datalen = sizeof(struct if_data32);
1557 CP(*src, *dst, ifi_mtu);
1558 CP(*src, *dst, ifi_metric);
1559 CP(*src, *dst, ifi_baudrate);
1560 CP(*src, *dst, ifi_ipackets);
1561 CP(*src, *dst, ifi_ierrors);
1562 CP(*src, *dst, ifi_opackets);
1563 CP(*src, *dst, ifi_oerrors);
1564 CP(*src, *dst, ifi_collisions);
1565 CP(*src, *dst, ifi_ibytes);
1566 CP(*src, *dst, ifi_obytes);
1567 CP(*src, *dst, ifi_imcasts);
1568 CP(*src, *dst, ifi_omcasts);
1569 CP(*src, *dst, ifi_iqdrops);
1570 CP(*src, *dst, ifi_noproto);
1571 CP(*src, *dst, ifi_hwassist);
1572 CP(*src, *dst, ifi_epoch);
1573 TV_CP(*src, *dst, ifi_lastchange);
1578 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1579 struct walkarg *w, int len)
1581 struct if_msghdrl *ifm;
1583 #ifdef COMPAT_FREEBSD32
1584 if (w->w_req->flags & SCTL_MASK32) {
1585 struct if_msghdrl32 *ifm32;
1587 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1588 ifm32->ifm_addrs = info->rti_addrs;
1589 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1590 ifm32->ifm_index = ifp->if_index;
1591 ifm32->_ifm_spare1 = 0;
1592 ifm32->ifm_len = sizeof(*ifm32);
1593 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1595 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1597 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1600 ifm = (struct if_msghdrl *)w->w_tmem;
1601 ifm->ifm_addrs = info->rti_addrs;
1602 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1603 ifm->ifm_index = ifp->if_index;
1604 ifm->_ifm_spare1 = 0;
1605 ifm->ifm_len = sizeof(*ifm);
1606 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1608 ifm->ifm_data = ifp->if_data;
1610 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1614 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1615 struct walkarg *w, int len)
1617 struct if_msghdr *ifm;
1619 #ifdef COMPAT_FREEBSD32
1620 if (w->w_req->flags & SCTL_MASK32) {
1621 struct if_msghdr32 *ifm32;
1623 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1624 ifm32->ifm_addrs = info->rti_addrs;
1625 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1626 ifm32->ifm_index = ifp->if_index;
1628 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1630 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1633 ifm = (struct if_msghdr *)w->w_tmem;
1634 ifm->ifm_addrs = info->rti_addrs;
1635 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1636 ifm->ifm_index = ifp->if_index;
1638 ifm->ifm_data = ifp->if_data;
1640 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1644 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1645 struct walkarg *w, int len)
1647 struct ifa_msghdrl *ifam;
1649 #ifdef COMPAT_FREEBSD32
1650 if (w->w_req->flags & SCTL_MASK32) {
1651 struct ifa_msghdrl32 *ifam32;
1653 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1654 ifam32->ifam_addrs = info->rti_addrs;
1655 ifam32->ifam_flags = ifa->ifa_flags;
1656 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1657 ifam32->_ifam_spare1 = 0;
1658 ifam32->ifam_len = sizeof(*ifam32);
1659 ifam32->ifam_data_off =
1660 offsetof(struct ifa_msghdrl32, ifam_data);
1661 ifam32->ifam_metric = ifa->ifa_metric;
1663 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1665 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1669 ifam = (struct ifa_msghdrl *)w->w_tmem;
1670 ifam->ifam_addrs = info->rti_addrs;
1671 ifam->ifam_flags = ifa->ifa_flags;
1672 ifam->ifam_index = ifa->ifa_ifp->if_index;
1673 ifam->_ifam_spare1 = 0;
1674 ifam->ifam_len = sizeof(*ifam);
1675 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1676 ifam->ifam_metric = ifa->ifa_metric;
1678 ifam->ifam_data = ifa->if_data;
1680 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1684 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1685 struct walkarg *w, int len)
1687 struct ifa_msghdr *ifam;
1689 ifam = (struct ifa_msghdr *)w->w_tmem;
1690 ifam->ifam_addrs = info->rti_addrs;
1691 ifam->ifam_flags = ifa->ifa_flags;
1692 ifam->ifam_index = ifa->ifa_ifp->if_index;
1693 ifam->ifam_metric = ifa->ifa_metric;
1695 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1699 sysctl_iflist(int af, struct walkarg *w)
1703 struct rt_addrinfo info;
1706 bzero((caddr_t)&info, sizeof(info));
1708 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1709 if (w->w_arg && w->w_arg != ifp->if_index)
1713 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1714 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1715 info.rti_info[RTAX_IFP] = NULL;
1716 if (w->w_req && w->w_tmem) {
1717 if (w->w_op == NET_RT_IFLISTL)
1718 error = sysctl_iflist_ifml(ifp, &info, w, len);
1720 error = sysctl_iflist_ifm(ifp, &info, w, len);
1724 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1725 if (af && af != ifa->ifa_addr->sa_family)
1727 if (prison_if(w->w_req->td->td_ucred,
1728 ifa->ifa_addr) != 0)
1730 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1731 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1732 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1733 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1734 if (w->w_req && w->w_tmem) {
1735 if (w->w_op == NET_RT_IFLISTL)
1736 error = sysctl_iflist_ifaml(ifa, &info,
1739 error = sysctl_iflist_ifam(ifa, &info,
1745 IF_ADDR_UNLOCK(ifp);
1746 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1747 info.rti_info[RTAX_BRD] = NULL;
1751 IF_ADDR_UNLOCK(ifp);
1757 sysctl_ifmalist(int af, struct walkarg *w)
1760 struct ifmultiaddr *ifma;
1761 struct rt_addrinfo info;
1765 bzero((caddr_t)&info, sizeof(info));
1767 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1768 if (w->w_arg && w->w_arg != ifp->if_index)
1771 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1773 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1774 if (af && af != ifma->ifma_addr->sa_family)
1776 if (prison_if(w->w_req->td->td_ucred,
1777 ifma->ifma_addr) != 0)
1779 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1780 info.rti_info[RTAX_GATEWAY] =
1781 (ifma->ifma_addr->sa_family != AF_LINK) ?
1782 ifma->ifma_lladdr : NULL;
1783 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1784 if (w->w_req && w->w_tmem) {
1785 struct ifma_msghdr *ifmam;
1787 ifmam = (struct ifma_msghdr *)w->w_tmem;
1788 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1789 ifmam->ifmam_flags = 0;
1790 ifmam->ifmam_addrs = info.rti_addrs;
1791 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1793 IF_ADDR_UNLOCK(ifp);
1798 IF_ADDR_UNLOCK(ifp);
1806 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1808 int *name = (int *)arg1;
1809 u_int namelen = arg2;
1810 struct radix_node_head *rnh = NULL; /* silence compiler. */
1811 int i, lim, error = EINVAL;
1820 return ((namelen < 3) ? EISDIR : ENOTDIR);
1824 bzero(&w, sizeof(w));
1829 error = sysctl_wire_old_buffer(req, 0);
1836 if (af == 0) { /* dump all tables */
1839 } else /* dump only one table */
1843 * take care of llinfo entries, the caller must
1846 if (w.w_op == NET_RT_FLAGS &&
1847 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1849 error = lltable_sysctl_dumparp(af, w.w_req);
1855 * take care of routing entries
1857 for (error = 0; error == 0 && i <= lim; i++) {
1858 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1860 RADIX_NODE_HEAD_LOCK(rnh);
1861 error = rnh->rnh_walktree(rnh,
1862 sysctl_dumpentry, &w);
1863 RADIX_NODE_HEAD_UNLOCK(rnh);
1865 error = EAFNOSUPPORT;
1870 case NET_RT_IFLISTL:
1871 error = sysctl_iflist(af, &w);
1874 case NET_RT_IFMALIST:
1875 error = sysctl_ifmalist(af, &w);
1879 free(w.w_tmem, M_RTABLE);
1883 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1886 * Definitions of protocols supported in the ROUTE domain.
1889 static struct domain routedomain; /* or at least forward */
1891 static struct protosw routesw[] = {
1893 .pr_type = SOCK_RAW,
1894 .pr_domain = &routedomain,
1895 .pr_flags = PR_ATOMIC|PR_ADDR,
1896 .pr_output = route_output,
1897 .pr_ctlinput = raw_ctlinput,
1898 .pr_init = raw_init,
1899 .pr_usrreqs = &route_usrreqs
1903 static struct domain routedomain = {
1904 .dom_family = PF_ROUTE,
1905 .dom_name = "route",
1906 .dom_protosw = routesw,
1907 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1910 VNET_DOMAIN_SET(route);