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 static 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) {
494 IF_ADDR_RUNLOCK(ifp);
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) {
537 IF_ADDR_RUNLOCK(ifp);
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);
949 M_SETFIB(m, so->so_fibnum);
950 m->m_flags |= RTS_FILTER_FIB;
953 * XXX insure we don't get a copy by
954 * invalidating our protocol
956 unsigned short family = rp->rcb_proto.sp_family;
957 rp->rcb_proto.sp_family = 0;
959 rp->rcb_proto.sp_family = family;
963 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
972 rt_setmetrics(u_long which, const struct rt_metrics *in,
973 struct rt_metrics_lite *out)
975 #define metric(f, e) if (which & (f)) out->e = in->e;
977 * Only these are stored in the routing entry since introduction
978 * of tcp hostcache. The rest is ignored.
980 metric(RTV_MTU, rmx_mtu);
981 metric(RTV_WEIGHT, rmx_weight);
982 /* Userland -> kernel timebase conversion. */
983 if (which & RTV_EXPIRE)
984 out->rmx_expire = in->rmx_expire ?
985 in->rmx_expire - time_second + time_uptime : 0;
990 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
992 #define metric(e) out->e = in->e;
993 bzero(out, sizeof(*out));
996 /* Kernel -> userland timebase conversion. */
997 out->rmx_expire = in->rmx_expire ?
998 in->rmx_expire - time_uptime + time_second : 0;
1003 * Extract the addresses of the passed sockaddrs.
1004 * Do a little sanity checking so as to avoid bad memory references.
1005 * This data is derived straight from userland.
1008 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1010 struct sockaddr *sa;
1013 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1014 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1016 sa = (struct sockaddr *)cp;
1020 if (cp + sa->sa_len > cplim)
1023 * there are no more.. quit now
1024 * If there are more bits, they are in error.
1025 * I've seen this. route(1) can evidently generate these.
1026 * This causes kernel to core dump.
1027 * for compatibility, If we see this, point to a safe address.
1029 if (sa->sa_len == 0) {
1030 rtinfo->rti_info[i] = &sa_zero;
1031 return (0); /* should be EINVAL but for compat */
1034 rtinfo->rti_info[i] = sa;
1041 * Used by the routing socket.
1043 static struct mbuf *
1044 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1046 struct rt_msghdr *rtm;
1049 struct sockaddr *sa;
1056 len = sizeof(struct ifa_msghdr);
1061 len = sizeof(struct ifma_msghdr);
1065 len = sizeof(struct if_msghdr);
1068 case RTM_IFANNOUNCE:
1070 len = sizeof(struct if_announcemsghdr);
1074 len = sizeof(struct rt_msghdr);
1078 m = m_gethdr(M_DONTWAIT, MT_DATA);
1079 if (m && len > MHLEN) {
1080 MCLGET(m, M_DONTWAIT);
1081 if ((m->m_flags & M_EXT) == 0) {
1088 m->m_pkthdr.len = m->m_len = len;
1089 m->m_pkthdr.rcvif = NULL;
1090 rtm = mtod(m, struct rt_msghdr *);
1091 bzero((caddr_t)rtm, len);
1092 for (i = 0; i < RTAX_MAX; i++) {
1093 if ((sa = rtinfo->rti_info[i]) == NULL)
1095 rtinfo->rti_addrs |= (1 << i);
1097 m_copyback(m, len, dlen, (caddr_t)sa);
1100 if (m->m_pkthdr.len != len) {
1104 rtm->rtm_msglen = len;
1105 rtm->rtm_version = RTM_VERSION;
1106 rtm->rtm_type = type;
1111 * Used by the sysctl code and routing socket.
1114 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1117 int len, dlen, second_time = 0;
1120 rtinfo->rti_addrs = 0;
1126 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1127 #ifdef COMPAT_FREEBSD32
1128 if (w->w_req->flags & SCTL_MASK32)
1129 len = sizeof(struct ifa_msghdrl32);
1132 len = sizeof(struct ifa_msghdrl);
1134 len = sizeof(struct ifa_msghdr);
1138 #ifdef COMPAT_FREEBSD32
1139 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1140 if (w->w_op == NET_RT_IFLISTL)
1141 len = sizeof(struct if_msghdrl32);
1143 len = sizeof(struct if_msghdr32);
1147 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1148 len = sizeof(struct if_msghdrl);
1150 len = sizeof(struct if_msghdr);
1154 len = sizeof(struct ifma_msghdr);
1158 len = sizeof(struct rt_msghdr);
1163 for (i = 0; i < RTAX_MAX; i++) {
1164 struct sockaddr *sa;
1166 if ((sa = rtinfo->rti_info[i]) == NULL)
1168 rtinfo->rti_addrs |= (1 << i);
1171 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1177 if (cp == NULL && w != NULL && !second_time) {
1178 struct walkarg *rw = w;
1181 if (rw->w_tmemsize < len) {
1183 free(rw->w_tmem, M_RTABLE);
1184 rw->w_tmem = (caddr_t)
1185 malloc(len, M_RTABLE, M_NOWAIT);
1187 rw->w_tmemsize = len;
1197 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1199 rtm->rtm_version = RTM_VERSION;
1200 rtm->rtm_type = type;
1201 rtm->rtm_msglen = len;
1207 * This routine is called to generate a message from the routing
1208 * socket indicating that a redirect has occured, a routing lookup
1209 * has failed, or that a protocol has detected timeouts to a particular
1213 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1216 struct rt_msghdr *rtm;
1218 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1220 if (route_cb.any_count == 0)
1222 m = rt_msg1(type, rtinfo);
1226 if (fibnum != RTS_ALLFIBS) {
1227 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1228 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1229 M_SETFIB(m, fibnum);
1230 m->m_flags |= RTS_FILTER_FIB;
1233 rtm = mtod(m, struct rt_msghdr *);
1234 rtm->rtm_flags = RTF_DONE | flags;
1235 rtm->rtm_errno = error;
1236 rtm->rtm_addrs = rtinfo->rti_addrs;
1237 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1241 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1244 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1248 * This routine is called to generate a message from the routing
1249 * socket indicating that the status of a network interface has changed.
1252 rt_ifmsg(struct ifnet *ifp)
1254 struct if_msghdr *ifm;
1256 struct rt_addrinfo info;
1258 if (route_cb.any_count == 0)
1260 bzero((caddr_t)&info, sizeof(info));
1261 m = rt_msg1(RTM_IFINFO, &info);
1264 ifm = mtod(m, struct if_msghdr *);
1265 ifm->ifm_index = ifp->if_index;
1266 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1267 ifm->ifm_data = ifp->if_data;
1269 rt_dispatch(m, AF_UNSPEC);
1273 * This is called to generate messages from the routing socket
1274 * indicating a network interface has had addresses associated with it.
1275 * if we ever reverse the logic and replace messages TO the routing
1276 * socket indicate a request to configure interfaces, then it will
1277 * be unnecessary as the routing socket will automatically generate
1281 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1284 struct rt_addrinfo info;
1285 struct sockaddr *sa = NULL;
1287 struct mbuf *m = NULL;
1288 struct ifnet *ifp = ifa->ifa_ifp;
1290 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1291 ("unexpected cmd %u", cmd));
1292 #if defined(INET) || defined(INET6)
1295 * notify the SCTP stack
1296 * this will only get called when an address is added/deleted
1297 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1299 sctp_addr_change(ifa, cmd);
1302 if (route_cb.any_count == 0)
1304 for (pass = 1; pass < 3; pass++) {
1305 bzero((caddr_t)&info, sizeof(info));
1306 if ((cmd == RTM_ADD && pass == 1) ||
1307 (cmd == RTM_DELETE && pass == 2)) {
1308 struct ifa_msghdr *ifam;
1309 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1311 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1312 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1313 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1314 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1315 if ((m = rt_msg1(ncmd, &info)) == NULL)
1317 ifam = mtod(m, struct ifa_msghdr *);
1318 ifam->ifam_index = ifp->if_index;
1319 ifam->ifam_metric = ifa->ifa_metric;
1320 ifam->ifam_flags = ifa->ifa_flags;
1321 ifam->ifam_addrs = info.rti_addrs;
1323 if ((cmd == RTM_ADD && pass == 2) ||
1324 (cmd == RTM_DELETE && pass == 1)) {
1325 struct rt_msghdr *rtm;
1329 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1330 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1331 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1332 if ((m = rt_msg1(cmd, &info)) == NULL)
1334 rtm = mtod(m, struct rt_msghdr *);
1335 rtm->rtm_index = ifp->if_index;
1336 rtm->rtm_flags |= rt->rt_flags;
1337 rtm->rtm_errno = error;
1338 rtm->rtm_addrs = info.rti_addrs;
1340 if (fibnum != RTS_ALLFIBS) {
1341 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1342 "fibnum out of range 0 <= %d < %d", __func__,
1343 fibnum, rt_numfibs));
1344 M_SETFIB(m, fibnum);
1345 m->m_flags |= RTS_FILTER_FIB;
1347 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1352 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1355 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1359 * This is the analogue to the rt_newaddrmsg which performs the same
1360 * function but for multicast group memberhips. This is easier since
1361 * there is no route state to worry about.
1364 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1366 struct rt_addrinfo info;
1367 struct mbuf *m = NULL;
1368 struct ifnet *ifp = ifma->ifma_ifp;
1369 struct ifma_msghdr *ifmam;
1371 if (route_cb.any_count == 0)
1374 bzero((caddr_t)&info, sizeof(info));
1375 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1376 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1378 * If a link-layer address is present, present it as a ``gateway''
1379 * (similarly to how ARP entries, e.g., are presented).
1381 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1382 m = rt_msg1(cmd, &info);
1385 ifmam = mtod(m, struct ifma_msghdr *);
1386 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1388 ifmam->ifmam_index = ifp->if_index;
1389 ifmam->ifmam_addrs = info.rti_addrs;
1390 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1393 static struct mbuf *
1394 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1395 struct rt_addrinfo *info)
1397 struct if_announcemsghdr *ifan;
1400 if (route_cb.any_count == 0)
1402 bzero((caddr_t)info, sizeof(*info));
1403 m = rt_msg1(type, info);
1405 ifan = mtod(m, struct if_announcemsghdr *);
1406 ifan->ifan_index = ifp->if_index;
1407 strlcpy(ifan->ifan_name, ifp->if_xname,
1408 sizeof(ifan->ifan_name));
1409 ifan->ifan_what = what;
1415 * This is called to generate routing socket messages indicating
1416 * IEEE80211 wireless events.
1417 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1420 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1423 struct rt_addrinfo info;
1425 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1428 * Append the ieee80211 data. Try to stick it in the
1429 * mbuf containing the ifannounce msg; otherwise allocate
1430 * a new mbuf and append.
1432 * NB: we assume m is a single mbuf.
1434 if (data_len > M_TRAILINGSPACE(m)) {
1435 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1440 bcopy(data, mtod(n, void *), data_len);
1441 n->m_len = data_len;
1443 } else if (data_len > 0) {
1444 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1445 m->m_len += data_len;
1447 if (m->m_flags & M_PKTHDR)
1448 m->m_pkthdr.len += data_len;
1449 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1450 rt_dispatch(m, AF_UNSPEC);
1455 * This is called to generate routing socket messages indicating
1456 * network interface arrival and departure.
1459 rt_ifannouncemsg(struct ifnet *ifp, int what)
1462 struct rt_addrinfo info;
1464 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1466 rt_dispatch(m, AF_UNSPEC);
1470 rt_dispatch(struct mbuf *m, sa_family_t saf)
1475 * Preserve the family from the sockaddr, if any, in an m_tag for
1476 * use when injecting the mbuf into the routing socket buffer from
1479 if (saf != AF_UNSPEC) {
1480 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1486 *(unsigned short *)(tag + 1) = saf;
1487 m_tag_prepend(m, tag);
1491 m->m_pkthdr.rcvif = V_loif;
1497 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1501 * This is used in dumping the kernel table via sysctl().
1504 sysctl_dumpentry(struct radix_node *rn, void *vw)
1506 struct walkarg *w = vw;
1507 struct rtentry *rt = (struct rtentry *)rn;
1508 int error = 0, size;
1509 struct rt_addrinfo info;
1511 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1513 if ((rt->rt_flags & RTF_HOST) == 0
1514 ? jailed_without_vnet(w->w_req->td->td_ucred)
1515 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1517 bzero((caddr_t)&info, sizeof(info));
1518 info.rti_info[RTAX_DST] = rt_key(rt);
1519 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1520 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1521 info.rti_info[RTAX_GENMASK] = 0;
1523 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1524 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1525 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1526 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1528 size = rt_msg2(RTM_GET, &info, NULL, w);
1529 if (w->w_req && w->w_tmem) {
1530 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1532 rtm->rtm_flags = rt->rt_flags;
1534 * let's be honest about this being a retarded hack
1536 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1537 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1538 rtm->rtm_index = rt->rt_ifp->if_index;
1539 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1540 rtm->rtm_addrs = info.rti_addrs;
1541 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1547 #ifdef COMPAT_FREEBSD32
1549 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1552 bzero(dst, sizeof(*dst));
1553 CP(*src, *dst, ifi_type);
1554 CP(*src, *dst, ifi_physical);
1555 CP(*src, *dst, ifi_addrlen);
1556 CP(*src, *dst, ifi_hdrlen);
1557 CP(*src, *dst, ifi_link_state);
1558 dst->ifi_datalen = sizeof(struct if_data32);
1559 CP(*src, *dst, ifi_mtu);
1560 CP(*src, *dst, ifi_metric);
1561 CP(*src, *dst, ifi_baudrate);
1562 CP(*src, *dst, ifi_ipackets);
1563 CP(*src, *dst, ifi_ierrors);
1564 CP(*src, *dst, ifi_opackets);
1565 CP(*src, *dst, ifi_oerrors);
1566 CP(*src, *dst, ifi_collisions);
1567 CP(*src, *dst, ifi_ibytes);
1568 CP(*src, *dst, ifi_obytes);
1569 CP(*src, *dst, ifi_imcasts);
1570 CP(*src, *dst, ifi_omcasts);
1571 CP(*src, *dst, ifi_iqdrops);
1572 CP(*src, *dst, ifi_noproto);
1573 CP(*src, *dst, ifi_hwassist);
1574 CP(*src, *dst, ifi_epoch);
1575 TV_CP(*src, *dst, ifi_lastchange);
1580 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1581 struct walkarg *w, int len)
1583 struct if_msghdrl *ifm;
1585 #ifdef COMPAT_FREEBSD32
1586 if (w->w_req->flags & SCTL_MASK32) {
1587 struct if_msghdrl32 *ifm32;
1589 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1590 ifm32->ifm_addrs = info->rti_addrs;
1591 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1592 ifm32->ifm_index = ifp->if_index;
1593 ifm32->_ifm_spare1 = 0;
1594 ifm32->ifm_len = sizeof(*ifm32);
1595 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1597 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1599 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1602 ifm = (struct if_msghdrl *)w->w_tmem;
1603 ifm->ifm_addrs = info->rti_addrs;
1604 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1605 ifm->ifm_index = ifp->if_index;
1606 ifm->_ifm_spare1 = 0;
1607 ifm->ifm_len = sizeof(*ifm);
1608 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1610 ifm->ifm_data = ifp->if_data;
1612 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1616 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1617 struct walkarg *w, int len)
1619 struct if_msghdr *ifm;
1621 #ifdef COMPAT_FREEBSD32
1622 if (w->w_req->flags & SCTL_MASK32) {
1623 struct if_msghdr32 *ifm32;
1625 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1626 ifm32->ifm_addrs = info->rti_addrs;
1627 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1628 ifm32->ifm_index = ifp->if_index;
1630 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1632 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1635 ifm = (struct if_msghdr *)w->w_tmem;
1636 ifm->ifm_addrs = info->rti_addrs;
1637 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1638 ifm->ifm_index = ifp->if_index;
1640 ifm->ifm_data = ifp->if_data;
1642 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1646 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1647 struct walkarg *w, int len)
1649 struct ifa_msghdrl *ifam;
1651 #ifdef COMPAT_FREEBSD32
1652 if (w->w_req->flags & SCTL_MASK32) {
1653 struct ifa_msghdrl32 *ifam32;
1655 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1656 ifam32->ifam_addrs = info->rti_addrs;
1657 ifam32->ifam_flags = ifa->ifa_flags;
1658 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1659 ifam32->_ifam_spare1 = 0;
1660 ifam32->ifam_len = sizeof(*ifam32);
1661 ifam32->ifam_data_off =
1662 offsetof(struct ifa_msghdrl32, ifam_data);
1663 ifam32->ifam_metric = ifa->ifa_metric;
1665 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1667 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1671 ifam = (struct ifa_msghdrl *)w->w_tmem;
1672 ifam->ifam_addrs = info->rti_addrs;
1673 ifam->ifam_flags = ifa->ifa_flags;
1674 ifam->ifam_index = ifa->ifa_ifp->if_index;
1675 ifam->_ifam_spare1 = 0;
1676 ifam->ifam_len = sizeof(*ifam);
1677 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1678 ifam->ifam_metric = ifa->ifa_metric;
1680 ifam->ifam_data = ifa->if_data;
1682 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1686 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1687 struct walkarg *w, int len)
1689 struct ifa_msghdr *ifam;
1691 ifam = (struct ifa_msghdr *)w->w_tmem;
1692 ifam->ifam_addrs = info->rti_addrs;
1693 ifam->ifam_flags = ifa->ifa_flags;
1694 ifam->ifam_index = ifa->ifa_ifp->if_index;
1695 ifam->ifam_metric = ifa->ifa_metric;
1697 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1701 sysctl_iflist(int af, struct walkarg *w)
1705 struct rt_addrinfo info;
1708 bzero((caddr_t)&info, sizeof(info));
1710 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1711 if (w->w_arg && w->w_arg != ifp->if_index)
1715 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1716 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1717 info.rti_info[RTAX_IFP] = NULL;
1718 if (w->w_req && w->w_tmem) {
1719 if (w->w_op == NET_RT_IFLISTL)
1720 error = sysctl_iflist_ifml(ifp, &info, w, len);
1722 error = sysctl_iflist_ifm(ifp, &info, w, len);
1726 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1727 if (af && af != ifa->ifa_addr->sa_family)
1729 if (prison_if(w->w_req->td->td_ucred,
1730 ifa->ifa_addr) != 0)
1732 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1733 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1734 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1735 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1736 if (w->w_req && w->w_tmem) {
1737 if (w->w_op == NET_RT_IFLISTL)
1738 error = sysctl_iflist_ifaml(ifa, &info,
1741 error = sysctl_iflist_ifam(ifa, &info,
1747 IF_ADDR_RUNLOCK(ifp);
1748 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1749 info.rti_info[RTAX_BRD] = NULL;
1753 IF_ADDR_RUNLOCK(ifp);
1759 sysctl_ifmalist(int af, struct walkarg *w)
1762 struct ifmultiaddr *ifma;
1763 struct rt_addrinfo info;
1767 bzero((caddr_t)&info, sizeof(info));
1769 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1770 if (w->w_arg && w->w_arg != ifp->if_index)
1773 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1775 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1776 if (af && af != ifma->ifma_addr->sa_family)
1778 if (prison_if(w->w_req->td->td_ucred,
1779 ifma->ifma_addr) != 0)
1781 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1782 info.rti_info[RTAX_GATEWAY] =
1783 (ifma->ifma_addr->sa_family != AF_LINK) ?
1784 ifma->ifma_lladdr : NULL;
1785 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1786 if (w->w_req && w->w_tmem) {
1787 struct ifma_msghdr *ifmam;
1789 ifmam = (struct ifma_msghdr *)w->w_tmem;
1790 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1791 ifmam->ifmam_flags = 0;
1792 ifmam->ifmam_addrs = info.rti_addrs;
1793 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1795 IF_ADDR_RUNLOCK(ifp);
1800 IF_ADDR_RUNLOCK(ifp);
1808 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1810 int *name = (int *)arg1;
1811 u_int namelen = arg2;
1812 struct radix_node_head *rnh = NULL; /* silence compiler. */
1813 int i, lim, error = EINVAL;
1822 if (name[1] == NET_RT_DUMP) {
1824 fib = req->td->td_proc->p_fibnum;
1825 else if (namelen == 4)
1826 fib = (name[3] == -1) ?
1827 req->td->td_proc->p_fibnum : name[3];
1829 return ((namelen < 3) ? EISDIR : ENOTDIR);
1830 if (fib < 0 || fib >= rt_numfibs)
1832 } else if (namelen != 3)
1833 return ((namelen < 3) ? EISDIR : ENOTDIR);
1837 bzero(&w, sizeof(w));
1842 error = sysctl_wire_old_buffer(req, 0);
1849 if (af == 0) { /* dump all tables */
1852 } else /* dump only one table */
1856 * take care of llinfo entries, the caller must
1859 if (w.w_op == NET_RT_FLAGS &&
1860 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1862 error = lltable_sysctl_dumparp(af, w.w_req);
1868 * take care of routing entries
1870 for (error = 0; error == 0 && i <= lim; i++) {
1871 rnh = rt_tables_get_rnh(fib, i);
1873 RADIX_NODE_HEAD_RLOCK(rnh);
1874 error = rnh->rnh_walktree(rnh,
1875 sysctl_dumpentry, &w);
1876 RADIX_NODE_HEAD_RUNLOCK(rnh);
1878 error = EAFNOSUPPORT;
1883 case NET_RT_IFLISTL:
1884 error = sysctl_iflist(af, &w);
1887 case NET_RT_IFMALIST:
1888 error = sysctl_ifmalist(af, &w);
1892 free(w.w_tmem, M_RTABLE);
1896 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1899 * Definitions of protocols supported in the ROUTE domain.
1902 static struct domain routedomain; /* or at least forward */
1904 static struct protosw routesw[] = {
1906 .pr_type = SOCK_RAW,
1907 .pr_domain = &routedomain,
1908 .pr_flags = PR_ATOMIC|PR_ADDR,
1909 .pr_output = route_output,
1910 .pr_ctlinput = raw_ctlinput,
1911 .pr_init = raw_init,
1912 .pr_usrreqs = &route_usrreqs
1916 static struct domain routedomain = {
1917 .dom_family = PF_ROUTE,
1918 .dom_name = "route",
1919 .dom_protosw = routesw,
1920 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1923 VNET_DOMAIN_SET(route);