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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
34 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
35 * $DragonFly: src/sys/net/rtsock.c,v 1.12 2004/04/22 04:21:29 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/domain.h>
52 #include <net/route.h>
53 #include <net/raw_cb.h>
55 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
57 static struct sockaddr route_dst = { 2, PF_ROUTE, };
58 static struct sockaddr route_src = { 2, PF_ROUTE, };
59 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
60 static struct sockproto route_proto = { PF_ROUTE, };
66 struct sysctl_req *w_req;
70 rt_msg1 (int, struct rt_addrinfo *);
71 static int rt_msg2 (int, struct rt_addrinfo *, caddr_t, struct walkarg *);
72 static int rt_xaddrs (caddr_t, caddr_t, struct rt_addrinfo *);
73 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
74 static int sysctl_iflist (int af, struct walkarg *w);
75 static int route_output (struct mbuf *, struct socket *,
76 struct pr_output_info *);
77 static void rt_setmetrics (u_long, struct rt_metrics *,
80 /* Sleazy use of local variables throughout file, warning!!!! */
81 #define dst info.rti_info[RTAX_DST]
82 #define gate info.rti_info[RTAX_GATEWAY]
83 #define netmask info.rti_info[RTAX_NETMASK]
84 #define genmask info.rti_info[RTAX_GENMASK]
85 #define ifpaddr info.rti_info[RTAX_IFP]
86 #define ifaaddr info.rti_info[RTAX_IFA]
87 #define brdaddr info.rti_info[RTAX_BRD]
90 * It really doesn't make any sense at all for this code to share much
91 * with raw_usrreq.c, since its functionality is so restricted. XXX
94 rts_abort(struct socket *so)
98 error = raw_usrreqs.pru_abort(so);
103 /* pru_accept is EOPNOTSUPP */
106 rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
111 if (sotorawcb(so) != 0)
112 return EISCONN; /* XXX panic? */
113 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK|M_ZERO);
118 * The splnet() is necessary to block protocols from sending
119 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
120 * this PCB is extant but incompletely initialized.
121 * Probably we should try to do more of this work beforehand and
125 so->so_pcb = (caddr_t)rp;
126 error = raw_attach(so, proto, ai->sb_rlimit);
133 switch(rp->rcb_proto.sp_protocol) {
138 route_cb.ip6_count++;
141 route_cb.ipx_count++;
147 rp->rcb_faddr = &route_src;
148 route_cb.any_count++;
150 so->so_options |= SO_USELOOPBACK;
156 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
160 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
166 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
170 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
175 /* pru_connect2 is EOPNOTSUPP */
176 /* pru_control is EOPNOTSUPP */
179 rts_detach(struct socket *so)
181 struct rawcb *rp = sotorawcb(so);
186 switch(rp->rcb_proto.sp_protocol) {
191 route_cb.ip6_count--;
194 route_cb.ipx_count--;
200 route_cb.any_count--;
202 error = raw_usrreqs.pru_detach(so);
208 rts_disconnect(struct socket *so)
212 error = raw_usrreqs.pru_disconnect(so);
217 /* pru_listen is EOPNOTSUPP */
220 rts_peeraddr(struct socket *so, struct sockaddr **nam)
224 error = raw_usrreqs.pru_peeraddr(so, nam);
229 /* pru_rcvd is EOPNOTSUPP */
230 /* pru_rcvoob is EOPNOTSUPP */
233 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
234 struct mbuf *control, struct thread *td)
238 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
243 /* pru_sense is null */
246 rts_shutdown(struct socket *so)
250 error = raw_usrreqs.pru_shutdown(so);
256 rts_sockaddr(struct socket *so, struct sockaddr **nam)
260 error = raw_usrreqs.pru_sockaddr(so, nam);
265 static struct pr_usrreqs route_usrreqs = {
266 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
267 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
268 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
269 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
270 sosend, soreceive, sopoll
275 route_output(struct mbuf *m, struct socket *so, struct pr_output_info *oi)
277 struct rt_msghdr *rtm = 0;
278 struct rtentry *rt = 0;
279 struct rtentry *saved_nrt = 0;
280 struct radix_node_head *rnh;
281 struct rt_addrinfo info;
283 struct ifnet *ifp = 0;
284 struct ifaddr *ifa = 0;
286 #define senderr(e) { error = e; goto flush;}
287 if (m == 0 || ((m->m_len < sizeof(long)) &&
288 (m = m_pullup(m, sizeof(long))) == 0))
290 if ((m->m_flags & M_PKTHDR) == 0)
291 panic("route_output");
292 len = m->m_pkthdr.len;
293 if (len < sizeof(*rtm) ||
294 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
298 R_Malloc(rtm, struct rt_msghdr *, len);
303 m_copydata(m, 0, len, (caddr_t)rtm);
304 if (rtm->rtm_version != RTM_VERSION) {
306 senderr(EPROTONOSUPPORT);
308 rtm->rtm_pid = oi->p_pid;
309 bzero(&info, sizeof(info));
310 info.rti_addrs = rtm->rtm_addrs;
311 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
315 info.rti_flags = rtm->rtm_flags;
316 if (dst == 0 || (dst->sa_family >= AF_MAX)
317 || (gate != 0 && (gate->sa_family >= AF_MAX)))
320 struct radix_node *t;
321 t = rn_addmask((caddr_t)genmask, 0, 1);
322 if (t && Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1,
323 *(u_char *)t->rn_key - 1) == 0)
324 genmask = (struct sockaddr *)(t->rn_key);
330 * Verify that the caller has the appropriate privilege; RTM_GET
331 * is the only operation the non-superuser is allowed.
333 if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
336 switch (rtm->rtm_type) {
341 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
342 if (error == 0 && saved_nrt) {
343 rt_setmetrics(rtm->rtm_inits,
344 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
345 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
346 saved_nrt->rt_rmx.rmx_locks |=
347 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
348 saved_nrt->rt_refcnt--;
349 saved_nrt->rt_genmask = genmask;
354 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
356 if ((rt = saved_nrt))
365 if ((rnh = rt_tables[dst->sa_family]) == 0) {
366 senderr(EAFNOSUPPORT);
367 } else if ((rt = (struct rtentry *)
368 rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
372 switch(rtm->rtm_type) {
377 gate = rt->rt_gateway;
378 netmask = rt_mask(rt);
379 genmask = rt->rt_genmask;
380 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
383 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
384 ifaaddr = rt->rt_ifa->ifa_addr;
385 if (ifp->if_flags & IFF_POINTOPOINT)
386 brdaddr = rt->rt_ifa->ifa_dstaddr;
387 rtm->rtm_index = ifp->if_index;
393 len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
394 (struct walkarg *)0);
395 if (len > rtm->rtm_msglen) {
396 struct rt_msghdr *new_rtm;
397 R_Malloc(new_rtm, struct rt_msghdr *, len);
400 Bcopy(rtm, new_rtm, rtm->rtm_msglen);
401 Free(rtm); rtm = new_rtm;
403 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
404 (struct walkarg *)0);
405 rtm->rtm_flags = rt->rt_flags;
406 rtm->rtm_rmx = rt->rt_rmx;
407 rtm->rtm_addrs = info.rti_addrs;
411 /* new gateway could require new ifaddr, ifp;
412 flags may also be different; ifp may be specified
413 by ll sockaddr when protocol address is ambiguous */
414 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
415 if ((rt->rt_flags & RTF_GATEWAY && gate != NULL) ||
418 !equal(ifaaddr, rt->rt_ifa->ifa_addr))) {
419 if ((error = rt_getifa(&info)) != 0)
423 (error = rt_setgate(rt, rt_key(rt), gate)) != 0)
425 if ((ifa = info.rti_ifa) != NULL) {
426 struct ifaddr *oifa = rt->rt_ifa;
428 if (oifa && oifa->ifa_rtrequest)
429 oifa->ifa_rtrequest(RTM_DELETE, rt,
434 rt->rt_ifp = info.rti_ifp;
437 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
439 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
440 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
442 rt->rt_genmask = genmask;
447 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
448 rt->rt_rmx.rmx_locks |=
449 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
461 rtm->rtm_errno = error;
463 rtm->rtm_flags |= RTF_DONE;
468 struct rawcb *rp = 0;
470 * Check to see if we don't want our own messages.
472 if ((so->so_options & SO_USELOOPBACK) == 0) {
473 if (route_cb.any_count <= 1) {
479 /* There is another listener, so construct message */
483 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
484 if (m->m_pkthdr.len < rtm->rtm_msglen) {
487 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
488 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
492 rp->rcb_proto.sp_family = 0; /* Avoid us */
494 route_proto.sp_protocol = dst->sa_family;
496 raw_input(m, &route_proto, &route_src, &route_dst);
498 rp->rcb_proto.sp_family = PF_ROUTE;
504 rt_setmetrics(which, in, out)
506 struct rt_metrics *in, *out;
508 #define metric(f, e) if (which & (f)) out->e = in->e;
509 metric(RTV_RPIPE, rmx_recvpipe);
510 metric(RTV_SPIPE, rmx_sendpipe);
511 metric(RTV_SSTHRESH, rmx_ssthresh);
512 metric(RTV_RTT, rmx_rtt);
513 metric(RTV_RTTVAR, rmx_rttvar);
514 metric(RTV_HOPCOUNT, rmx_hopcount);
515 metric(RTV_MTU, rmx_mtu);
516 metric(RTV_EXPIRE, rmx_expire);
521 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
522 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
526 * Extract the addresses of the passed sockaddrs.
527 * Do a little sanity checking so as to avoid bad memory references.
528 * This data is derived straight from userland.
531 rt_xaddrs(cp, cplim, rtinfo)
533 struct rt_addrinfo *rtinfo;
538 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
539 if ((rtinfo->rti_addrs & (1 << i)) == 0)
541 sa = (struct sockaddr *)cp;
545 if ( (cp + sa->sa_len) > cplim ) {
550 * there are no more.. quit now
551 * If there are more bits, they are in error.
552 * I've seen this. route(1) can evidently generate these.
553 * This causes kernel to core dump.
554 * for compatibility, If we see this, point to a safe address.
556 if (sa->sa_len == 0) {
557 rtinfo->rti_info[i] = &sa_zero;
558 return (0); /* should be EINVAL but for compat */
562 rtinfo->rti_info[i] = sa;
569 rt_msg1(type, rtinfo)
571 struct rt_addrinfo *rtinfo;
573 struct rt_msghdr *rtm;
583 len = sizeof(struct ifa_msghdr);
588 len = sizeof(struct ifma_msghdr);
592 len = sizeof(struct if_msghdr);
596 len = sizeof(struct if_announcemsghdr);
600 len = sizeof(struct rt_msghdr);
604 m = m_gethdr(M_DONTWAIT, MT_DATA);
605 if (m && len > MHLEN) {
606 MCLGET(m, M_DONTWAIT);
607 if ((m->m_flags & M_EXT) == 0) {
614 m->m_pkthdr.len = m->m_len = len;
615 m->m_pkthdr.rcvif = 0;
616 rtm = mtod(m, struct rt_msghdr *);
617 bzero((caddr_t)rtm, len);
618 for (i = 0; i < RTAX_MAX; i++) {
619 if ((sa = rtinfo->rti_info[i]) == NULL)
621 rtinfo->rti_addrs |= (1 << i);
622 dlen = ROUNDUP(sa->sa_len);
623 m_copyback(m, len, dlen, (caddr_t)sa);
626 if (m->m_pkthdr.len != len) {
630 rtm->rtm_msglen = len;
631 rtm->rtm_version = RTM_VERSION;
632 rtm->rtm_type = type;
637 rt_msg2(type, rtinfo, cp, w)
639 struct rt_addrinfo *rtinfo;
644 int len, dlen, second_time = 0;
647 rtinfo->rti_addrs = 0;
653 len = sizeof(struct ifa_msghdr);
657 len = sizeof(struct if_msghdr);
661 len = sizeof(struct rt_msghdr);
666 for (i = 0; i < RTAX_MAX; i++) {
669 if ((sa = rtinfo->rti_info[i]) == 0)
671 rtinfo->rti_addrs |= (1 << i);
672 dlen = ROUNDUP(sa->sa_len);
674 bcopy((caddr_t)sa, cp, (unsigned)dlen);
680 if (cp == 0 && w != NULL && !second_time) {
681 struct walkarg *rw = w;
684 if (rw->w_tmemsize < len) {
686 free(rw->w_tmem, M_RTABLE);
687 rw->w_tmem = (caddr_t)malloc(len, M_RTABLE,
688 M_INTWAIT | M_NULLOK);
690 rw->w_tmemsize = len;
700 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
702 rtm->rtm_version = RTM_VERSION;
703 rtm->rtm_type = type;
704 rtm->rtm_msglen = len;
710 * This routine is called to generate a message from the routing
711 * socket indicating that a redirect has occured, a routing lookup
712 * has failed, or that a protocol has detected timeouts to a particular
716 rt_missmsg(type, rtinfo, flags, error)
717 int type, flags, error;
718 struct rt_addrinfo *rtinfo;
720 struct rt_msghdr *rtm;
722 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
724 if (route_cb.any_count == 0)
726 m = rt_msg1(type, rtinfo);
729 rtm = mtod(m, struct rt_msghdr *);
730 rtm->rtm_flags = RTF_DONE | flags;
731 rtm->rtm_errno = error;
732 rtm->rtm_addrs = rtinfo->rti_addrs;
733 route_proto.sp_protocol = sa ? sa->sa_family : 0;
734 raw_input(m, &route_proto, &route_src, &route_dst);
738 * This routine is called to generate a message from the routing
739 * socket indicating that the status of a network interface has changed.
745 struct if_msghdr *ifm;
747 struct rt_addrinfo info;
749 if (route_cb.any_count == 0)
751 bzero((caddr_t)&info, sizeof(info));
752 m = rt_msg1(RTM_IFINFO, &info);
755 ifm = mtod(m, struct if_msghdr *);
756 ifm->ifm_index = ifp->if_index;
757 ifm->ifm_flags = (u_short)ifp->if_flags;
758 ifm->ifm_data = ifp->if_data;
760 route_proto.sp_protocol = 0;
761 raw_input(m, &route_proto, &route_src, &route_dst);
765 rt_ifamsg(int cmd, struct ifaddr *ifa)
767 struct ifa_msghdr *ifam;
768 struct rt_addrinfo info;
771 struct ifnet *ifp = ifa->ifa_ifp;
773 bzero(&info, sizeof(info));
774 ifaaddr = sa = ifa->ifa_addr;
775 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
776 netmask = ifa->ifa_netmask;
777 brdaddr = ifa->ifa_dstaddr;
778 if ((m = rt_msg1(cmd, &info)) == NULL)
780 ifam = mtod(m, struct ifa_msghdr *);
781 ifam->ifam_index = ifp->if_index;
782 ifam->ifam_metric = ifa->ifa_metric;
783 ifam->ifam_flags = ifa->ifa_flags;
784 ifam->ifam_addrs = info.rti_addrs;
786 route_proto.sp_protocol = sa ? sa->sa_family : 0;
787 raw_input(m, &route_proto, &route_src, &route_dst);
791 rt_rtmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
793 struct rt_msghdr *rtm;
794 struct rt_addrinfo info;
797 struct ifnet *ifp = ifa->ifa_ifp;
801 bzero(&info, sizeof(info));
802 netmask = rt_mask(rt);
803 dst = sa = rt_key(rt);
804 gate = rt->rt_gateway;
805 if ((m = rt_msg1(cmd, &info)) == NULL)
807 rtm = mtod(m, struct rt_msghdr *);
808 rtm->rtm_index = ifp->if_index;
809 rtm->rtm_flags |= rt->rt_flags;
810 rtm->rtm_errno = error;
811 rtm->rtm_addrs = info.rti_addrs;
813 route_proto.sp_protocol = sa ? sa->sa_family : 0;
814 raw_input(m, &route_proto, &route_src, &route_dst);
818 * This is called to generate messages from the routing socket
819 * indicating a network interface has had addresses associated with it.
820 * if we ever reverse the logic and replace messages TO the routing
821 * socket indicate a request to configure interfaces, then it will
822 * be unnecessary as the routing socket will automatically generate
826 rt_newaddrmsg(cmd, ifa, error, rt)
831 if (route_cb.any_count == 0)
834 if (cmd == RTM_ADD) {
835 rt_ifamsg(RTM_NEWADDR, ifa);
836 rt_rtmsg(RTM_ADD, ifa, error, rt);
838 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
839 rt_rtmsg(RTM_DELETE, ifa, error, rt);
840 rt_ifamsg(RTM_DELADDR, ifa);
845 * This is the analogue to the rt_newaddrmsg which performs the same
846 * function but for multicast group memberhips. This is easier since
847 * there is no route state to worry about.
850 rt_newmaddrmsg(cmd, ifma)
852 struct ifmultiaddr *ifma;
854 struct rt_addrinfo info;
856 struct ifnet *ifp = ifma->ifma_ifp;
857 struct ifma_msghdr *ifmam;
859 if (route_cb.any_count == 0)
862 bzero((caddr_t)&info, sizeof(info));
863 ifaaddr = ifma->ifma_addr;
864 if (ifp && TAILQ_FIRST(&ifp->if_addrhead))
865 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
869 * If a link-layer address is present, present it as a ``gateway''
870 * (similarly to how ARP entries, e.g., are presented).
872 gate = ifma->ifma_lladdr;
873 if ((m = rt_msg1(cmd, &info)) == NULL)
875 ifmam = mtod(m, struct ifma_msghdr *);
876 ifmam->ifmam_index = ifp->if_index;
877 ifmam->ifmam_addrs = info.rti_addrs;
878 route_proto.sp_protocol = ifma->ifma_addr->sa_family;
879 raw_input(m, &route_proto, &route_src, &route_dst);
883 * This is called to generate routing socket messages indicating
884 * network interface arrival and departure.
887 rt_ifannouncemsg(ifp, what)
891 struct if_announcemsghdr *ifan;
893 struct rt_addrinfo info;
895 if (route_cb.any_count == 0)
897 bzero((caddr_t)&info, sizeof(info));
898 m = rt_msg1(RTM_IFANNOUNCE, &info);
901 ifan = mtod(m, struct if_announcemsghdr *);
902 ifan->ifan_index = ifp->if_index;
903 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name));
904 ifan->ifan_what = what;
905 route_proto.sp_protocol = 0;
906 raw_input(m, &route_proto, &route_src, &route_dst);
910 * This is used in dumping the kernel table via sysctl().
913 sysctl_dumpentry(rn, vw)
914 struct radix_node *rn;
917 struct walkarg *w = vw;
918 struct rtentry *rt = (struct rtentry *)rn;
920 struct rt_addrinfo info;
922 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
924 bzero((caddr_t)&info, sizeof(info));
926 gate = rt->rt_gateway;
927 netmask = rt_mask(rt);
928 genmask = rt->rt_genmask;
930 ifpaddr = TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
931 ifaaddr = rt->rt_ifa->ifa_addr;
932 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
933 brdaddr = rt->rt_ifa->ifa_dstaddr;
935 size = rt_msg2(RTM_GET, &info, 0, w);
936 if (w->w_req && w->w_tmem) {
937 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
939 rtm->rtm_flags = rt->rt_flags;
940 rtm->rtm_use = rt->rt_use;
941 rtm->rtm_rmx = rt->rt_rmx;
942 rtm->rtm_index = rt->rt_ifp->if_index;
943 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
944 rtm->rtm_addrs = info.rti_addrs;
945 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
958 struct rt_addrinfo info;
961 bzero((caddr_t)&info, sizeof(info));
962 TAILQ_FOREACH(ifp, &ifnet, if_link) {
963 if (w->w_arg && w->w_arg != ifp->if_index)
965 ifa = TAILQ_FIRST(&ifp->if_addrhead);
966 ifpaddr = ifa->ifa_addr;
967 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
969 if (w->w_req && w->w_tmem) {
970 struct if_msghdr *ifm;
972 ifm = (struct if_msghdr *)w->w_tmem;
973 ifm->ifm_index = ifp->if_index;
974 ifm->ifm_flags = (u_short)ifp->if_flags;
975 ifm->ifm_data = ifp->if_data;
976 ifm->ifm_addrs = info.rti_addrs;
977 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
981 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) {
982 if (af && af != ifa->ifa_addr->sa_family)
984 if (curproc->p_ucred->cr_prison && prison_if(curthread, ifa->ifa_addr))
986 ifaaddr = ifa->ifa_addr;
987 netmask = ifa->ifa_netmask;
988 brdaddr = ifa->ifa_dstaddr;
989 len = rt_msg2(RTM_NEWADDR, &info, 0, w);
990 if (w->w_req && w->w_tmem) {
991 struct ifa_msghdr *ifam;
993 ifam = (struct ifa_msghdr *)w->w_tmem;
994 ifam->ifam_index = ifa->ifa_ifp->if_index;
995 ifam->ifam_flags = ifa->ifa_flags;
996 ifam->ifam_metric = ifa->ifa_metric;
997 ifam->ifam_addrs = info.rti_addrs;
998 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1003 ifaaddr = netmask = brdaddr = 0;
1009 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1011 int *name = (int *)arg1;
1012 u_int namelen = arg2;
1013 struct radix_node_head *rnh;
1014 int i, s, error = EINVAL;
1025 Bzero(&w, sizeof(w));
1035 for (i = 1; i <= AF_MAX; i++)
1036 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1037 (error = rnh->rnh_walktree(rnh,
1038 sysctl_dumpentry, &w)))
1043 error = sysctl_iflist(af, &w);
1047 free(w.w_tmem, M_RTABLE);
1051 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1054 * Definitions of protocols supported in the ROUTE domain.
1057 extern struct domain routedomain; /* or at least forward */
1059 static struct protosw routesw[] = {
1060 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1061 0, route_output, raw_ctlinput, 0,
1068 static struct domain routedomain =
1069 { PF_ROUTE, "route", 0, 0, 0,
1070 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };