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.4 2003/06/25 03:56:02 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/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.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 __P((int, struct rt_addrinfo *));
71 static int rt_msg2 __P((int,
72 struct rt_addrinfo *, caddr_t, struct walkarg *));
73 static int rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
74 static int sysctl_dumpentry __P((struct radix_node *rn, void *vw));
75 static int sysctl_iflist __P((int af, struct walkarg *w));
76 static int route_output __P((struct mbuf *, struct socket *));
77 static void rt_setmetrics __P((u_long, struct rt_metrics *, struct rt_metrics *));
79 /* Sleazy use of local variables throughout file, warning!!!! */
80 #define dst info.rti_info[RTAX_DST]
81 #define gate info.rti_info[RTAX_GATEWAY]
82 #define netmask info.rti_info[RTAX_NETMASK]
83 #define genmask info.rti_info[RTAX_GENMASK]
84 #define ifpaddr info.rti_info[RTAX_IFP]
85 #define ifaaddr info.rti_info[RTAX_IFA]
86 #define brdaddr info.rti_info[RTAX_BRD]
89 * It really doesn't make any sense at all for this code to share much
90 * with raw_usrreq.c, since its functionality is so restricted. XXX
93 rts_abort(struct socket *so)
97 error = raw_usrreqs.pru_abort(so);
102 /* pru_accept is EOPNOTSUPP */
105 rts_attach(struct socket *so, int proto, struct thread *td)
110 if (sotorawcb(so) != 0)
111 return EISCONN; /* XXX panic? */
112 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK|M_ZERO);
117 * The splnet() is necessary to block protocols from sending
118 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
119 * this PCB is extant but incompletely initialized.
120 * Probably we should try to do more of this work beforehand and
124 so->so_pcb = (caddr_t)rp;
125 error = raw_attach(so, proto);
132 switch(rp->rcb_proto.sp_protocol) {
137 route_cb.ip6_count++;
140 route_cb.ipx_count++;
146 rp->rcb_faddr = &route_src;
147 route_cb.any_count++;
149 so->so_options |= SO_USELOOPBACK;
155 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
159 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
165 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
169 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
174 /* pru_connect2 is EOPNOTSUPP */
175 /* pru_control is EOPNOTSUPP */
178 rts_detach(struct socket *so)
180 struct rawcb *rp = sotorawcb(so);
185 switch(rp->rcb_proto.sp_protocol) {
190 route_cb.ip6_count--;
193 route_cb.ipx_count--;
199 route_cb.any_count--;
201 error = raw_usrreqs.pru_detach(so);
207 rts_disconnect(struct socket *so)
211 error = raw_usrreqs.pru_disconnect(so);
216 /* pru_listen is EOPNOTSUPP */
219 rts_peeraddr(struct socket *so, struct sockaddr **nam)
223 error = raw_usrreqs.pru_peeraddr(so, nam);
228 /* pru_rcvd is EOPNOTSUPP */
229 /* pru_rcvoob is EOPNOTSUPP */
232 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
233 struct mbuf *control, struct thread *td)
237 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
242 /* pru_sense is null */
245 rts_shutdown(struct socket *so)
249 error = raw_usrreqs.pru_shutdown(so);
255 rts_sockaddr(struct socket *so, struct sockaddr **nam)
259 error = raw_usrreqs.pru_sockaddr(so, nam);
264 static struct pr_usrreqs route_usrreqs = {
265 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
266 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
267 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
268 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
269 sosend, soreceive, sopoll
275 register struct mbuf *m;
278 register struct rt_msghdr *rtm = 0;
279 register struct rtentry *rt = 0;
280 struct rtentry *saved_nrt = 0;
281 struct radix_node_head *rnh;
282 struct rt_addrinfo info;
284 struct ifnet *ifp = 0;
285 struct ifaddr *ifa = 0;
287 #define senderr(e) { error = e; goto flush;}
288 if (m == 0 || ((m->m_len < sizeof(long)) &&
289 (m = m_pullup(m, sizeof(long))) == 0))
291 if ((m->m_flags & M_PKTHDR) == 0)
292 panic("route_output");
293 len = m->m_pkthdr.len;
294 if (len < sizeof(*rtm) ||
295 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
299 R_Malloc(rtm, struct rt_msghdr *, len);
304 m_copydata(m, 0, len, (caddr_t)rtm);
305 if (rtm->rtm_version != RTM_VERSION) {
307 senderr(EPROTONOSUPPORT);
309 rtm->rtm_pid = curproc->p_pid;
310 bzero(&info, sizeof(info));
311 info.rti_addrs = rtm->rtm_addrs;
312 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
316 info.rti_flags = rtm->rtm_flags;
317 if (dst == 0 || (dst->sa_family >= AF_MAX)
318 || (gate != 0 && (gate->sa_family >= AF_MAX)))
321 struct radix_node *t;
322 t = rn_addmask((caddr_t)genmask, 0, 1);
323 if (t && Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1,
324 *(u_char *)t->rn_key - 1) == 0)
325 genmask = (struct sockaddr *)(t->rn_key);
331 * Verify that the caller has the appropriate privilege; RTM_GET
332 * is the only operation the non-superuser is allowed.
334 if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
337 switch (rtm->rtm_type) {
342 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
343 if (error == 0 && saved_nrt) {
344 rt_setmetrics(rtm->rtm_inits,
345 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
346 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
347 saved_nrt->rt_rmx.rmx_locks |=
348 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
349 saved_nrt->rt_refcnt--;
350 saved_nrt->rt_genmask = genmask;
355 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
357 if ((rt = saved_nrt))
366 if ((rnh = rt_tables[dst->sa_family]) == 0) {
367 senderr(EAFNOSUPPORT);
368 } else if ((rt = (struct rtentry *)
369 rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
373 switch(rtm->rtm_type) {
378 gate = rt->rt_gateway;
379 netmask = rt_mask(rt);
380 genmask = rt->rt_genmask;
381 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
384 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
385 ifaaddr = rt->rt_ifa->ifa_addr;
386 if (ifp->if_flags & IFF_POINTOPOINT)
387 brdaddr = rt->rt_ifa->ifa_dstaddr;
388 rtm->rtm_index = ifp->if_index;
394 len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
395 (struct walkarg *)0);
396 if (len > rtm->rtm_msglen) {
397 struct rt_msghdr *new_rtm;
398 R_Malloc(new_rtm, struct rt_msghdr *, len);
401 Bcopy(rtm, new_rtm, rtm->rtm_msglen);
402 Free(rtm); rtm = new_rtm;
404 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
405 (struct walkarg *)0);
406 rtm->rtm_flags = rt->rt_flags;
407 rtm->rtm_rmx = rt->rt_rmx;
408 rtm->rtm_addrs = info.rti_addrs;
412 /* new gateway could require new ifaddr, ifp;
413 flags may also be different; ifp may be specified
414 by ll sockaddr when protocol address is ambiguous */
415 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
416 if ((rt->rt_flags & RTF_GATEWAY && gate != NULL) ||
419 !equal(ifaaddr, rt->rt_ifa->ifa_addr))) {
420 if ((error = rt_getifa(&info)) != 0)
424 (error = rt_setgate(rt, rt_key(rt), gate)) != 0)
426 if ((ifa = info.rti_ifa) != NULL) {
427 register struct ifaddr *oifa = rt->rt_ifa;
429 if (oifa && oifa->ifa_rtrequest)
430 oifa->ifa_rtrequest(RTM_DELETE, rt,
435 rt->rt_ifp = info.rti_ifp;
438 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
440 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
441 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
443 rt->rt_genmask = genmask;
448 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
449 rt->rt_rmx.rmx_locks |=
450 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
462 rtm->rtm_errno = error;
464 rtm->rtm_flags |= RTF_DONE;
469 register struct rawcb *rp = 0;
471 * Check to see if we don't want our own messages.
473 if ((so->so_options & SO_USELOOPBACK) == 0) {
474 if (route_cb.any_count <= 1) {
480 /* There is another listener, so construct message */
484 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
485 if (m->m_pkthdr.len < rtm->rtm_msglen) {
488 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
489 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
493 rp->rcb_proto.sp_family = 0; /* Avoid us */
495 route_proto.sp_protocol = dst->sa_family;
497 raw_input(m, &route_proto, &route_src, &route_dst);
499 rp->rcb_proto.sp_family = PF_ROUTE;
505 rt_setmetrics(which, in, out)
507 register struct rt_metrics *in, *out;
509 #define metric(f, e) if (which & (f)) out->e = in->e;
510 metric(RTV_RPIPE, rmx_recvpipe);
511 metric(RTV_SPIPE, rmx_sendpipe);
512 metric(RTV_SSTHRESH, rmx_ssthresh);
513 metric(RTV_RTT, rmx_rtt);
514 metric(RTV_RTTVAR, rmx_rttvar);
515 metric(RTV_HOPCOUNT, rmx_hopcount);
516 metric(RTV_MTU, rmx_mtu);
517 metric(RTV_EXPIRE, rmx_expire);
522 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
523 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
527 * Extract the addresses of the passed sockaddrs.
528 * Do a little sanity checking so as to avoid bad memory references.
529 * This data is derived straight from userland.
532 rt_xaddrs(cp, cplim, rtinfo)
533 register caddr_t cp, cplim;
534 register struct rt_addrinfo *rtinfo;
536 register struct sockaddr *sa;
539 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
540 if ((rtinfo->rti_addrs & (1 << i)) == 0)
542 sa = (struct sockaddr *)cp;
546 if ( (cp + sa->sa_len) > cplim ) {
551 * there are no more.. quit now
552 * If there are more bits, they are in error.
553 * I've seen this. route(1) can evidently generate these.
554 * This causes kernel to core dump.
555 * for compatibility, If we see this, point to a safe address.
557 if (sa->sa_len == 0) {
558 rtinfo->rti_info[i] = &sa_zero;
559 return (0); /* should be EINVAL but for compat */
563 rtinfo->rti_info[i] = sa;
570 rt_msg1(type, rtinfo)
572 register struct rt_addrinfo *rtinfo;
574 register struct rt_msghdr *rtm;
575 register struct mbuf *m;
577 register struct sockaddr *sa;
584 len = sizeof(struct ifa_msghdr);
589 len = sizeof(struct ifma_msghdr);
593 len = sizeof(struct if_msghdr);
597 len = sizeof(struct if_announcemsghdr);
601 len = sizeof(struct rt_msghdr);
605 m = m_gethdr(M_DONTWAIT, MT_DATA);
606 if (m && len > MHLEN) {
607 MCLGET(m, M_DONTWAIT);
608 if ((m->m_flags & M_EXT) == 0) {
615 m->m_pkthdr.len = m->m_len = len;
616 m->m_pkthdr.rcvif = 0;
617 rtm = mtod(m, struct rt_msghdr *);
618 bzero((caddr_t)rtm, len);
619 for (i = 0; i < RTAX_MAX; i++) {
620 if ((sa = rtinfo->rti_info[i]) == NULL)
622 rtinfo->rti_addrs |= (1 << i);
623 dlen = ROUNDUP(sa->sa_len);
624 m_copyback(m, len, dlen, (caddr_t)sa);
627 if (m->m_pkthdr.len != len) {
631 rtm->rtm_msglen = len;
632 rtm->rtm_version = RTM_VERSION;
633 rtm->rtm_type = type;
638 rt_msg2(type, rtinfo, cp, w)
640 register struct rt_addrinfo *rtinfo;
645 int len, dlen, second_time = 0;
648 rtinfo->rti_addrs = 0;
654 len = sizeof(struct ifa_msghdr);
658 len = sizeof(struct if_msghdr);
662 len = sizeof(struct rt_msghdr);
667 for (i = 0; i < RTAX_MAX; i++) {
668 register struct sockaddr *sa;
670 if ((sa = rtinfo->rti_info[i]) == 0)
672 rtinfo->rti_addrs |= (1 << i);
673 dlen = ROUNDUP(sa->sa_len);
675 bcopy((caddr_t)sa, cp, (unsigned)dlen);
681 if (cp == 0 && w != NULL && !second_time) {
682 register struct walkarg *rw = w;
685 if (rw->w_tmemsize < len) {
687 free(rw->w_tmem, M_RTABLE);
688 rw->w_tmem = (caddr_t)
689 malloc(len, M_RTABLE, M_NOWAIT);
691 rw->w_tmemsize = len;
701 register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
703 rtm->rtm_version = RTM_VERSION;
704 rtm->rtm_type = type;
705 rtm->rtm_msglen = len;
711 * This routine is called to generate a message from the routing
712 * socket indicating that a redirect has occured, a routing lookup
713 * has failed, or that a protocol has detected timeouts to a particular
717 rt_missmsg(type, rtinfo, flags, error)
718 int type, flags, error;
719 register struct rt_addrinfo *rtinfo;
721 register struct rt_msghdr *rtm;
722 register struct mbuf *m;
723 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
725 if (route_cb.any_count == 0)
727 m = rt_msg1(type, rtinfo);
730 rtm = mtod(m, struct rt_msghdr *);
731 rtm->rtm_flags = RTF_DONE | flags;
732 rtm->rtm_errno = error;
733 rtm->rtm_addrs = rtinfo->rti_addrs;
734 route_proto.sp_protocol = sa ? sa->sa_family : 0;
735 raw_input(m, &route_proto, &route_src, &route_dst);
739 * This routine is called to generate a message from the routing
740 * socket indicating that the status of a network interface has changed.
744 register struct ifnet *ifp;
746 register struct if_msghdr *ifm;
748 struct rt_addrinfo info;
750 if (route_cb.any_count == 0)
752 bzero((caddr_t)&info, sizeof(info));
753 m = rt_msg1(RTM_IFINFO, &info);
756 ifm = mtod(m, struct if_msghdr *);
757 ifm->ifm_index = ifp->if_index;
758 ifm->ifm_flags = (u_short)ifp->if_flags;
759 ifm->ifm_data = ifp->if_data;
761 route_proto.sp_protocol = 0;
762 raw_input(m, &route_proto, &route_src, &route_dst);
766 * This is called to generate messages from the routing socket
767 * indicating a network interface has had addresses associated with it.
768 * if we ever reverse the logic and replace messages TO the routing
769 * socket indicate a request to configure interfaces, then it will
770 * be unnecessary as the routing socket will automatically generate
774 rt_newaddrmsg(cmd, ifa, error, rt)
776 register struct ifaddr *ifa;
777 register struct rtentry *rt;
779 struct rt_addrinfo info;
780 struct sockaddr *sa = 0;
783 struct ifnet *ifp = ifa->ifa_ifp;
785 if (route_cb.any_count == 0)
787 for (pass = 1; pass < 3; pass++) {
788 bzero((caddr_t)&info, sizeof(info));
789 if ((cmd == RTM_ADD && pass == 1) ||
790 (cmd == RTM_DELETE && pass == 2)) {
791 register struct ifa_msghdr *ifam;
792 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
794 ifaaddr = sa = ifa->ifa_addr;
795 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
796 netmask = ifa->ifa_netmask;
797 brdaddr = ifa->ifa_dstaddr;
798 if ((m = rt_msg1(ncmd, &info)) == NULL)
800 ifam = mtod(m, struct ifa_msghdr *);
801 ifam->ifam_index = ifp->if_index;
802 ifam->ifam_metric = ifa->ifa_metric;
803 ifam->ifam_flags = ifa->ifa_flags;
804 ifam->ifam_addrs = info.rti_addrs;
806 if ((cmd == RTM_ADD && pass == 2) ||
807 (cmd == RTM_DELETE && pass == 1)) {
808 register struct rt_msghdr *rtm;
812 netmask = rt_mask(rt);
813 dst = sa = rt_key(rt);
814 gate = rt->rt_gateway;
815 if ((m = rt_msg1(cmd, &info)) == NULL)
817 rtm = mtod(m, struct rt_msghdr *);
818 rtm->rtm_index = ifp->if_index;
819 rtm->rtm_flags |= rt->rt_flags;
820 rtm->rtm_errno = error;
821 rtm->rtm_addrs = info.rti_addrs;
823 route_proto.sp_protocol = sa ? sa->sa_family : 0;
824 raw_input(m, &route_proto, &route_src, &route_dst);
829 * This is the analogue to the rt_newaddrmsg which performs the same
830 * function but for multicast group memberhips. This is easier since
831 * there is no route state to worry about.
834 rt_newmaddrmsg(cmd, ifma)
836 struct ifmultiaddr *ifma;
838 struct rt_addrinfo info;
840 struct ifnet *ifp = ifma->ifma_ifp;
841 struct ifma_msghdr *ifmam;
843 if (route_cb.any_count == 0)
846 bzero((caddr_t)&info, sizeof(info));
847 ifaaddr = ifma->ifma_addr;
848 if (ifp && TAILQ_FIRST(&ifp->if_addrhead))
849 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
853 * If a link-layer address is present, present it as a ``gateway''
854 * (similarly to how ARP entries, e.g., are presented).
856 gate = ifma->ifma_lladdr;
857 if ((m = rt_msg1(cmd, &info)) == NULL)
859 ifmam = mtod(m, struct ifma_msghdr *);
860 ifmam->ifmam_index = ifp->if_index;
861 ifmam->ifmam_addrs = info.rti_addrs;
862 route_proto.sp_protocol = ifma->ifma_addr->sa_family;
863 raw_input(m, &route_proto, &route_src, &route_dst);
867 * This is called to generate routing socket messages indicating
868 * network interface arrival and departure.
871 rt_ifannouncemsg(ifp, what)
875 struct if_announcemsghdr *ifan;
877 struct rt_addrinfo info;
879 if (route_cb.any_count == 0)
881 bzero((caddr_t)&info, sizeof(info));
882 m = rt_msg1(RTM_IFANNOUNCE, &info);
885 ifan = mtod(m, struct if_announcemsghdr *);
886 ifan->ifan_index = ifp->if_index;
887 snprintf(ifan->ifan_name, sizeof(ifan->ifan_name),
888 "%s%d", ifp->if_name, ifp->if_unit);
889 ifan->ifan_what = what;
890 route_proto.sp_protocol = 0;
891 raw_input(m, &route_proto, &route_src, &route_dst);
895 * This is used in dumping the kernel table via sysctl().
898 sysctl_dumpentry(rn, vw)
899 struct radix_node *rn;
902 register struct walkarg *w = vw;
903 register struct rtentry *rt = (struct rtentry *)rn;
905 struct rt_addrinfo info;
907 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
909 bzero((caddr_t)&info, sizeof(info));
911 gate = rt->rt_gateway;
912 netmask = rt_mask(rt);
913 genmask = rt->rt_genmask;
915 ifpaddr = TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
916 ifaaddr = rt->rt_ifa->ifa_addr;
917 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
918 brdaddr = rt->rt_ifa->ifa_dstaddr;
920 size = rt_msg2(RTM_GET, &info, 0, w);
921 if (w->w_req && w->w_tmem) {
922 register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
924 rtm->rtm_flags = rt->rt_flags;
925 rtm->rtm_use = rt->rt_use;
926 rtm->rtm_rmx = rt->rt_rmx;
927 rtm->rtm_index = rt->rt_ifp->if_index;
928 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
929 rtm->rtm_addrs = info.rti_addrs;
930 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
939 register struct walkarg *w;
941 register struct ifnet *ifp;
942 register struct ifaddr *ifa;
943 struct rt_addrinfo info;
946 bzero((caddr_t)&info, sizeof(info));
947 TAILQ_FOREACH(ifp, &ifnet, if_link) {
948 if (w->w_arg && w->w_arg != ifp->if_index)
950 ifa = TAILQ_FIRST(&ifp->if_addrhead);
951 ifpaddr = ifa->ifa_addr;
952 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
954 if (w->w_req && w->w_tmem) {
955 register struct if_msghdr *ifm;
957 ifm = (struct if_msghdr *)w->w_tmem;
958 ifm->ifm_index = ifp->if_index;
959 ifm->ifm_flags = (u_short)ifp->if_flags;
960 ifm->ifm_data = ifp->if_data;
961 ifm->ifm_addrs = info.rti_addrs;
962 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
966 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) {
967 if (af && af != ifa->ifa_addr->sa_family)
969 if (curproc->p_ucred->cr_prison && prison_if(curthread, ifa->ifa_addr))
971 ifaaddr = ifa->ifa_addr;
972 netmask = ifa->ifa_netmask;
973 brdaddr = ifa->ifa_dstaddr;
974 len = rt_msg2(RTM_NEWADDR, &info, 0, w);
975 if (w->w_req && w->w_tmem) {
976 register struct ifa_msghdr *ifam;
978 ifam = (struct ifa_msghdr *)w->w_tmem;
979 ifam->ifam_index = ifa->ifa_ifp->if_index;
980 ifam->ifam_flags = ifa->ifa_flags;
981 ifam->ifam_metric = ifa->ifa_metric;
982 ifam->ifam_addrs = info.rti_addrs;
983 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
988 ifaaddr = netmask = brdaddr = 0;
994 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
996 int *name = (int *)arg1;
997 u_int namelen = arg2;
998 register struct radix_node_head *rnh;
999 int i, s, error = EINVAL;
1010 Bzero(&w, sizeof(w));
1020 for (i = 1; i <= AF_MAX; i++)
1021 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1022 (error = rnh->rnh_walktree(rnh,
1023 sysctl_dumpentry, &w)))
1028 error = sysctl_iflist(af, &w);
1032 free(w.w_tmem, M_RTABLE);
1036 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1039 * Definitions of protocols supported in the ROUTE domain.
1042 extern struct domain routedomain; /* or at least forward */
1044 static struct protosw routesw[] = {
1045 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1046 0, route_output, raw_ctlinput, 0,
1053 static struct domain routedomain =
1054 { PF_ROUTE, "route", 0, 0, 0,
1055 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };