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.9 2004/03/05 16:57:15 hsu 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 (int, struct rt_addrinfo *);
71 static int rt_msg2 (int,
72 struct rt_addrinfo *, caddr_t, struct walkarg *);
73 static int rt_xaddrs (caddr_t, caddr_t, struct rt_addrinfo *);
74 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
75 static int sysctl_iflist (int af, struct walkarg *w);
76 static int route_output (struct mbuf *, struct socket *);
77 static void rt_setmetrics (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 pru_attach_info *ai)
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, ai->sb_rlimit);
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
278 struct rt_msghdr *rtm = 0;
279 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 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 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 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)
534 struct rt_addrinfo *rtinfo;
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 struct rt_addrinfo *rtinfo;
574 struct rt_msghdr *rtm;
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 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++) {
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 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 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 struct rt_addrinfo *rtinfo;
721 struct rt_msghdr *rtm;
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.
746 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 rt_ifamsg(int cmd, struct ifaddr *ifa)
768 struct ifa_msghdr *ifam;
769 struct rt_addrinfo info;
772 struct ifnet *ifp = ifa->ifa_ifp;
774 bzero(&info, sizeof(info));
775 ifaaddr = sa = ifa->ifa_addr;
776 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
777 netmask = ifa->ifa_netmask;
778 brdaddr = ifa->ifa_dstaddr;
779 if ((m = rt_msg1(cmd, &info)) == NULL)
781 ifam = mtod(m, struct ifa_msghdr *);
782 ifam->ifam_index = ifp->if_index;
783 ifam->ifam_metric = ifa->ifa_metric;
784 ifam->ifam_flags = ifa->ifa_flags;
785 ifam->ifam_addrs = info.rti_addrs;
787 route_proto.sp_protocol = sa ? sa->sa_family : 0;
788 raw_input(m, &route_proto, &route_src, &route_dst);
792 rt_rtmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
794 struct rt_msghdr *rtm;
795 struct rt_addrinfo info;
798 struct ifnet *ifp = ifa->ifa_ifp;
802 bzero(&info, sizeof(info));
803 netmask = rt_mask(rt);
804 dst = sa = rt_key(rt);
805 gate = rt->rt_gateway;
806 if ((m = rt_msg1(cmd, &info)) == NULL)
808 rtm = mtod(m, struct rt_msghdr *);
809 rtm->rtm_index = ifp->if_index;
810 rtm->rtm_flags |= rt->rt_flags;
811 rtm->rtm_errno = error;
812 rtm->rtm_addrs = info.rti_addrs;
814 route_proto.sp_protocol = sa ? sa->sa_family : 0;
815 raw_input(m, &route_proto, &route_src, &route_dst);
819 * This is called to generate messages from the routing socket
820 * indicating a network interface has had addresses associated with it.
821 * if we ever reverse the logic and replace messages TO the routing
822 * socket indicate a request to configure interfaces, then it will
823 * be unnecessary as the routing socket will automatically generate
827 rt_newaddrmsg(cmd, ifa, error, rt)
832 if (route_cb.any_count == 0)
835 if (cmd == RTM_ADD) {
836 rt_ifamsg(RTM_NEWADDR, ifa);
837 rt_rtmsg(RTM_ADD, ifa, error, rt);
839 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
840 rt_rtmsg(RTM_DELETE, ifa, error, rt);
841 rt_ifamsg(RTM_DELADDR, ifa);
846 * This is the analogue to the rt_newaddrmsg which performs the same
847 * function but for multicast group memberhips. This is easier since
848 * there is no route state to worry about.
851 rt_newmaddrmsg(cmd, ifma)
853 struct ifmultiaddr *ifma;
855 struct rt_addrinfo info;
857 struct ifnet *ifp = ifma->ifma_ifp;
858 struct ifma_msghdr *ifmam;
860 if (route_cb.any_count == 0)
863 bzero((caddr_t)&info, sizeof(info));
864 ifaaddr = ifma->ifma_addr;
865 if (ifp && TAILQ_FIRST(&ifp->if_addrhead))
866 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
870 * If a link-layer address is present, present it as a ``gateway''
871 * (similarly to how ARP entries, e.g., are presented).
873 gate = ifma->ifma_lladdr;
874 if ((m = rt_msg1(cmd, &info)) == NULL)
876 ifmam = mtod(m, struct ifma_msghdr *);
877 ifmam->ifmam_index = ifp->if_index;
878 ifmam->ifmam_addrs = info.rti_addrs;
879 route_proto.sp_protocol = ifma->ifma_addr->sa_family;
880 raw_input(m, &route_proto, &route_src, &route_dst);
884 * This is called to generate routing socket messages indicating
885 * network interface arrival and departure.
888 rt_ifannouncemsg(ifp, what)
892 struct if_announcemsghdr *ifan;
894 struct rt_addrinfo info;
896 if (route_cb.any_count == 0)
898 bzero((caddr_t)&info, sizeof(info));
899 m = rt_msg1(RTM_IFANNOUNCE, &info);
902 ifan = mtod(m, struct if_announcemsghdr *);
903 ifan->ifan_index = ifp->if_index;
904 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name));
905 ifan->ifan_what = what;
906 route_proto.sp_protocol = 0;
907 raw_input(m, &route_proto, &route_src, &route_dst);
911 * This is used in dumping the kernel table via sysctl().
914 sysctl_dumpentry(rn, vw)
915 struct radix_node *rn;
918 struct walkarg *w = vw;
919 struct rtentry *rt = (struct rtentry *)rn;
921 struct rt_addrinfo info;
923 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
925 bzero((caddr_t)&info, sizeof(info));
927 gate = rt->rt_gateway;
928 netmask = rt_mask(rt);
929 genmask = rt->rt_genmask;
931 ifpaddr = TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
932 ifaaddr = rt->rt_ifa->ifa_addr;
933 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
934 brdaddr = rt->rt_ifa->ifa_dstaddr;
936 size = rt_msg2(RTM_GET, &info, 0, w);
937 if (w->w_req && w->w_tmem) {
938 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
940 rtm->rtm_flags = rt->rt_flags;
941 rtm->rtm_use = rt->rt_use;
942 rtm->rtm_rmx = rt->rt_rmx;
943 rtm->rtm_index = rt->rt_ifp->if_index;
944 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
945 rtm->rtm_addrs = info.rti_addrs;
946 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
959 struct rt_addrinfo info;
962 bzero((caddr_t)&info, sizeof(info));
963 TAILQ_FOREACH(ifp, &ifnet, if_link) {
964 if (w->w_arg && w->w_arg != ifp->if_index)
966 ifa = TAILQ_FIRST(&ifp->if_addrhead);
967 ifpaddr = ifa->ifa_addr;
968 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
970 if (w->w_req && w->w_tmem) {
971 struct if_msghdr *ifm;
973 ifm = (struct if_msghdr *)w->w_tmem;
974 ifm->ifm_index = ifp->if_index;
975 ifm->ifm_flags = (u_short)ifp->if_flags;
976 ifm->ifm_data = ifp->if_data;
977 ifm->ifm_addrs = info.rti_addrs;
978 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
982 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) {
983 if (af && af != ifa->ifa_addr->sa_family)
985 if (curproc->p_ucred->cr_prison && prison_if(curthread, ifa->ifa_addr))
987 ifaaddr = ifa->ifa_addr;
988 netmask = ifa->ifa_netmask;
989 brdaddr = ifa->ifa_dstaddr;
990 len = rt_msg2(RTM_NEWADDR, &info, 0, w);
991 if (w->w_req && w->w_tmem) {
992 struct ifa_msghdr *ifam;
994 ifam = (struct ifa_msghdr *)w->w_tmem;
995 ifam->ifam_index = ifa->ifa_ifp->if_index;
996 ifam->ifam_flags = ifa->ifa_flags;
997 ifam->ifam_metric = ifa->ifa_metric;
998 ifam->ifam_addrs = info.rti_addrs;
999 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1004 ifaaddr = netmask = brdaddr = 0;
1010 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1012 int *name = (int *)arg1;
1013 u_int namelen = arg2;
1014 struct radix_node_head *rnh;
1015 int i, s, error = EINVAL;
1026 Bzero(&w, sizeof(w));
1036 for (i = 1; i <= AF_MAX; i++)
1037 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1038 (error = rnh->rnh_walktree(rnh,
1039 sysctl_dumpentry, &w)))
1044 error = sysctl_iflist(af, &w);
1048 free(w.w_tmem, M_RTABLE);
1052 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1055 * Definitions of protocols supported in the ROUTE domain.
1058 extern struct domain routedomain; /* or at least forward */
1060 static struct protosw routesw[] = {
1061 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1062 0, route_output, raw_ctlinput, 0,
1069 static struct domain routedomain =
1070 { PF_ROUTE, "route", 0, 0, 0,
1071 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };