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.18 2004/12/28 08:09:59 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/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/domain.h>
51 #include <machine/stdarg.h>
54 #include <net/route.h>
55 #include <net/raw_cb.h>
57 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
59 static struct route_cb {
67 static struct sockaddr route_dst = { 2, PF_ROUTE, };
68 static struct sockaddr route_src = { 2, PF_ROUTE, };
69 static struct sockaddr sa_zero = { sizeof sa_zero, AF_INET, };
70 static struct sockproto route_proto = { PF_ROUTE, };
76 struct sysctl_req *w_req;
80 rt_msg1 (int, struct rt_addrinfo *);
81 static int rt_msg2 (int, struct rt_addrinfo *, caddr_t, struct walkarg *);
82 static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
83 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
84 static int sysctl_iflist (int af, struct walkarg *w);
85 static int route_output(struct mbuf *, struct socket *, ...);
86 static void rt_setmetrics (u_long, struct rt_metrics *,
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)
99 error = raw_usrreqs.pru_abort(so);
104 /* pru_accept is EOPNOTSUPP */
107 rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
112 if (sotorawcb(so) != NULL)
113 return EISCONN; /* XXX panic? */
115 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK|M_ZERO);
120 * The splnet() is necessary to block protocols from sending
121 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
122 * this PCB is extant but incompletely initialized.
123 * Probably we should try to do more of this work beforehand and
128 error = raw_attach(so, proto, ai->sb_rlimit);
135 switch(rp->rcb_proto.sp_protocol) {
140 route_cb.ip6_count++;
143 route_cb.ipx_count++;
149 rp->rcb_faddr = &route_src;
150 route_cb.any_count++;
152 so->so_options |= SO_USELOOPBACK;
158 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
163 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
169 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
174 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
179 /* pru_connect2 is EOPNOTSUPP */
180 /* pru_control is EOPNOTSUPP */
183 rts_detach(struct socket *so)
185 struct rawcb *rp = sotorawcb(so);
190 switch(rp->rcb_proto.sp_protocol) {
195 route_cb.ip6_count--;
198 route_cb.ipx_count--;
204 route_cb.any_count--;
206 error = raw_usrreqs.pru_detach(so);
212 rts_disconnect(struct socket *so)
217 error = raw_usrreqs.pru_disconnect(so);
222 /* pru_listen is EOPNOTSUPP */
225 rts_peeraddr(struct socket *so, struct sockaddr **nam)
230 error = raw_usrreqs.pru_peeraddr(so, nam);
235 /* pru_rcvd is EOPNOTSUPP */
236 /* pru_rcvoob is EOPNOTSUPP */
239 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
240 struct mbuf *control, struct thread *td)
245 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
250 /* pru_sense is null */
253 rts_shutdown(struct socket *so)
258 error = raw_usrreqs.pru_shutdown(so);
264 rts_sockaddr(struct socket *so, struct sockaddr **nam)
269 error = raw_usrreqs.pru_sockaddr(so, nam);
274 static struct pr_usrreqs route_usrreqs = {
275 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
276 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
277 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
278 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
279 sosend, soreceive, sopoll
284 route_output(struct mbuf *m, struct socket *so, ...)
286 struct rt_msghdr *rtm = NULL;
287 struct rtentry *rt = NULL;
288 struct rtentry *saved_nrt = NULL;
289 struct radix_node_head *rnh;
290 struct ifnet *ifp = NULL;
291 struct ifaddr *ifa = NULL;
292 struct rawcb *rp = NULL;
293 struct pr_output_info *oi;
294 struct rt_addrinfo info;
299 oi = __va_arg(ap, struct pr_output_info *);
302 #define gotoerr(e) { error = e; goto flush;}
303 if (m == NULL || ((m->m_len < sizeof(long)) &&
304 (m = m_pullup(m, sizeof(long))) == NULL))
306 if (!(m->m_flags & M_PKTHDR))
307 panic("route_output");
308 len = m->m_pkthdr.len;
309 if (len < sizeof *rtm ||
310 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
314 R_Malloc(rtm, struct rt_msghdr *, len);
319 m_copydata(m, 0, len, (caddr_t)rtm);
320 if (rtm->rtm_version != RTM_VERSION) {
322 gotoerr(EPROTONOSUPPORT);
324 rtm->rtm_pid = oi->p_pid;
325 bzero(&info, sizeof info);
326 info.rti_addrs = rtm->rtm_addrs;
327 if (rt_xaddrs((char *)(rtm + 1), len + (char *)rtm, &info)) {
331 info.rti_flags = rtm->rtm_flags;
332 if (info.sa_dst == NULL || info.sa_dst->sa_family >= AF_MAX ||
333 (info.sa_gateway != NULL && (info.sa_gateway->sa_family >= AF_MAX)))
336 if (info.sa_genmask != NULL) {
337 struct radix_node *t;
340 t = rn_addmask((char *)info.sa_genmask, TRUE, 1);
342 info.sa_genmask->sa_len >= (klen = *(u_char *)t->rn_key) &&
343 bcmp((char *)info.sa_genmask + 1, (char *)t->rn_key + 1,
345 info.sa_genmask = (struct sockaddr *)(t->rn_key);
351 * Verify that the caller has the appropriate privilege; RTM_GET
352 * is the only operation the non-superuser is allowed.
354 if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
357 switch (rtm->rtm_type) {
360 if (info.sa_gateway == NULL)
362 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
363 if (error == 0 && saved_nrt != NULL) {
364 rt_setmetrics(rtm->rtm_inits,
365 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
366 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
367 saved_nrt->rt_rmx.rmx_locks |=
368 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
369 saved_nrt->rt_refcnt--;
370 saved_nrt->rt_genmask = info.sa_genmask;
375 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
377 if ((rt = saved_nrt))
386 if ((rnh = rt_tables[info.sa_dst->sa_family]) == NULL) {
387 gotoerr(EAFNOSUPPORT);
388 } else if ((rt = (struct rtentry *) rnh->rnh_lookup(
389 (char *)info.sa_dst, (char *)info.sa_netmask, rnh)) != NULL)
393 switch(rtm->rtm_type) {
397 info.sa_dst = rt_key(rt);
398 info.sa_gateway = rt->rt_gateway;
399 info.sa_netmask = rt_mask(rt);
400 info.sa_genmask = rt->rt_genmask;
401 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
405 TAILQ_FIRST(&ifp->if_addrhead)->
407 info.sa_ifaaddr = rt->rt_ifa->ifa_addr;
408 if (ifp->if_flags & IFF_POINTOPOINT)
410 rt->rt_ifa->ifa_dstaddr;
411 rtm->rtm_index = ifp->if_index;
413 info.sa_ifpaddr = NULL;
414 info.sa_ifaaddr = NULL;
417 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
418 if (len > rtm->rtm_msglen) {
419 struct rt_msghdr *new_rtm;
420 R_Malloc(new_rtm, struct rt_msghdr *, len);
423 bcopy(rtm, new_rtm, rtm->rtm_msglen);
424 Free(rtm); rtm = new_rtm;
426 rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
427 rtm->rtm_flags = rt->rt_flags;
428 rtm->rtm_rmx = rt->rt_rmx;
429 rtm->rtm_addrs = info.rti_addrs;
434 * new gateway could require new ifaddr, ifp;
435 * flags may also be different; ifp may be specified
436 * by ll sockaddr when protocol address is ambiguous
438 if (((rt->rt_flags & RTF_GATEWAY) &&
439 info.sa_gateway != NULL) ||
440 info.sa_ifpaddr != NULL ||
441 (info.sa_ifaaddr != NULL &&
442 bcmp(info.sa_ifaaddr, rt->rt_ifa->ifa_addr,
443 info.sa_ifaaddr->sa_len) == 0)) {
444 if ((error = rt_getifa(&info)) != 0)
447 if (info.sa_gateway != NULL &&
448 (error = rt_setgate(rt, rt_key(rt),
449 info.sa_gateway)) != 0)
451 if ((ifa = info.rti_ifa) != NULL) {
452 struct ifaddr *oifa = rt->rt_ifa;
455 if (oifa && oifa->ifa_rtrequest)
456 oifa->ifa_rtrequest(RTM_DELETE,
461 rt->rt_ifp = info.rti_ifp;
464 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
466 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
467 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
468 if (info.sa_genmask != NULL)
469 rt->rt_genmask = info.sa_genmask;
474 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
475 rt->rt_rmx.rmx_locks |=
476 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
488 rtm->rtm_errno = error;
490 rtm->rtm_flags |= RTF_DONE;
495 * Check to see if we don't want our own messages.
497 if (!(so->so_options & SO_USELOOPBACK)) {
498 if (route_cb.any_count <= 1) {
504 /* There is another listener, so construct message */
508 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
509 if (m->m_pkthdr.len < rtm->rtm_msglen) {
512 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
513 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
517 rp->rcb_proto.sp_family = 0; /* Avoid us */
518 if (info.sa_dst != NULL)
519 route_proto.sp_protocol = info.sa_dst->sa_family;
521 raw_input(m, &route_proto, &route_src, &route_dst);
523 rp->rcb_proto.sp_family = PF_ROUTE;
528 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
530 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
531 setmetric(RTV_RPIPE, rmx_recvpipe);
532 setmetric(RTV_SPIPE, rmx_sendpipe);
533 setmetric(RTV_SSTHRESH, rmx_ssthresh);
534 setmetric(RTV_RTT, rmx_rtt);
535 setmetric(RTV_RTTVAR, rmx_rttvar);
536 setmetric(RTV_HOPCOUNT, rmx_hopcount);
537 setmetric(RTV_MTU, rmx_mtu);
538 setmetric(RTV_EXPIRE, rmx_expire);
543 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
544 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
547 * Extract the addresses of the passed sockaddrs.
548 * Do a little sanity checking so as to avoid bad memory references.
549 * This data is derived straight from userland.
552 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
557 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
558 if ((rtinfo->rti_addrs & (1 << i)) == 0)
560 sa = (struct sockaddr *)cp;
564 if ( (cp + sa->sa_len) > cplim ) {
569 * There are no more... Quit now.
570 * If there are more bits, they are in error.
571 * I've seen this. route(1) can evidently generate these.
572 * This causes kernel to core dump.
573 * For compatibility, if we see this, point to a safe address.
575 if (sa->sa_len == 0) {
576 rtinfo->rti_info[i] = &sa_zero;
577 return (0); /* should be EINVAL but for compat */
580 /* Accept the sockaddr. */
581 rtinfo->rti_info[i] = sa;
588 rt_msg1(type, rtinfo)
590 struct rt_addrinfo *rtinfo;
592 struct rt_msghdr *rtm;
602 len = sizeof(struct ifa_msghdr);
607 len = sizeof(struct ifma_msghdr);
611 len = sizeof(struct if_msghdr);
615 len = sizeof(struct if_announcemsghdr);
619 len = sizeof(struct rt_msghdr);
623 m = m_gethdr(MB_DONTWAIT, MT_DATA);
624 if (m && len > MHLEN) {
625 MCLGET(m, MB_DONTWAIT);
626 if (!(m->m_flags & M_EXT)) {
633 m->m_pkthdr.len = m->m_len = len;
634 m->m_pkthdr.rcvif = NULL;
635 rtm = mtod(m, struct rt_msghdr *);
637 for (i = 0; i < RTAX_MAX; i++) {
638 if ((sa = rtinfo->rti_info[i]) == NULL)
640 rtinfo->rti_addrs |= (1 << i);
641 dlen = ROUNDUP(sa->sa_len);
642 m_copyback(m, len, dlen, (caddr_t)sa);
645 if (m->m_pkthdr.len != len) {
649 rtm->rtm_msglen = len;
650 rtm->rtm_version = RTM_VERSION;
651 rtm->rtm_type = type;
656 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
660 boolean_t second_time = FALSE;
663 rtinfo->rti_addrs = NULL;
669 len = sizeof(struct ifa_msghdr);
673 len = sizeof(struct if_msghdr);
677 len = sizeof(struct rt_msghdr);
683 for (i = 0; i < RTAX_MAX; i++) {
686 if ((sa = rtinfo->rti_info[i]) == NULL)
688 rtinfo->rti_addrs |= (1 << i);
689 dlen = ROUNDUP(sa->sa_len);
697 if (cp == NULL && w != NULL && !second_time) {
698 struct walkarg *rw = w;
700 if (rw->w_req != NULL) {
701 if (rw->w_tmemsize < len) {
703 free(rw->w_tmem, M_RTABLE);
704 rw->w_tmem = malloc(len, M_RTABLE,
705 M_INTWAIT | M_NULLOK);
707 rw->w_tmemsize = len;
709 if (rw->w_tmem != NULL) {
717 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
719 rtm->rtm_version = RTM_VERSION;
720 rtm->rtm_type = type;
721 rtm->rtm_msglen = len;
727 * This routine is called to generate a message from the routing
728 * socket indicating that a redirect has occurred, a routing lookup
729 * has failed, or that a protocol has detected timeouts to a particular
733 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
735 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
736 struct rt_msghdr *rtm;
739 if (route_cb.any_count == 0)
741 m = rt_msg1(type, rtinfo);
744 rtm = mtod(m, struct rt_msghdr *);
745 rtm->rtm_flags = RTF_DONE | flags;
746 rtm->rtm_errno = error;
747 rtm->rtm_addrs = rtinfo->rti_addrs;
748 route_proto.sp_protocol = sa ? sa->sa_family : 0;
749 raw_input(m, &route_proto, &route_src, &route_dst);
753 * This routine is called to generate a message from the routing
754 * socket indicating that the status of a network interface has changed.
760 struct if_msghdr *ifm;
762 struct rt_addrinfo info;
764 if (route_cb.any_count == 0)
766 bzero(&info, sizeof info);
767 m = rt_msg1(RTM_IFINFO, &info);
770 ifm = mtod(m, struct if_msghdr *);
771 ifm->ifm_index = ifp->if_index;
772 ifm->ifm_flags = (u_short)ifp->if_flags;
773 ifm->ifm_data = ifp->if_data;
774 ifm->ifm_addrs = NULL;
775 route_proto.sp_protocol = 0;
776 raw_input(m, &route_proto, &route_src, &route_dst);
780 rt_ifamsg(int cmd, struct ifaddr *ifa)
782 struct ifa_msghdr *ifam;
783 struct rt_addrinfo info;
786 struct ifnet *ifp = ifa->ifa_ifp;
788 bzero(&info, sizeof info);
789 info.sa_ifaaddr = sa = ifa->ifa_addr;
790 info.sa_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
791 info.sa_netmask = ifa->ifa_netmask;
792 info.sa_bcastaddr = ifa->ifa_dstaddr;
794 m = rt_msg1(cmd, &info);
798 ifam = mtod(m, struct ifa_msghdr *);
799 ifam->ifam_index = ifp->if_index;
800 ifam->ifam_metric = ifa->ifa_metric;
801 ifam->ifam_flags = ifa->ifa_flags;
802 ifam->ifam_addrs = info.rti_addrs;
804 route_proto.sp_protocol = sa ? sa->sa_family : 0;
806 raw_input(m, &route_proto, &route_src, &route_dst);
810 rt_rtmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
812 struct rt_msghdr *rtm;
813 struct rt_addrinfo info;
816 struct ifnet *ifp = ifa->ifa_ifp;
821 bzero(&info, sizeof info);
822 info.sa_netmask = rt_mask(rt);
823 info.sa_dst = sa = rt_key(rt);
824 info.sa_gateway = rt->rt_gateway;
826 m = rt_msg1(cmd, &info);
830 rtm = mtod(m, struct rt_msghdr *);
831 rtm->rtm_index = ifp->if_index;
832 rtm->rtm_flags |= rt->rt_flags;
833 rtm->rtm_errno = error;
834 rtm->rtm_addrs = info.rti_addrs;
836 route_proto.sp_protocol = sa ? sa->sa_family : 0;
838 raw_input(m, &route_proto, &route_src, &route_dst);
842 * This is called to generate messages from the routing socket
843 * indicating a network interface has had addresses associated with it.
844 * if we ever reverse the logic and replace messages TO the routing
845 * socket indicate a request to configure interfaces, then it will
846 * be unnecessary as the routing socket will automatically generate
850 rt_newaddrmsg(cmd, ifa, error, rt)
855 if (route_cb.any_count == 0)
858 if (cmd == RTM_ADD) {
859 rt_ifamsg(RTM_NEWADDR, ifa);
860 rt_rtmsg(RTM_ADD, ifa, error, rt);
862 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
863 rt_rtmsg(RTM_DELETE, ifa, error, rt);
864 rt_ifamsg(RTM_DELADDR, ifa);
869 * This is the analogue to the rt_newaddrmsg which performs the same
870 * function but for multicast group memberhips. This is easier since
871 * there is no route state to worry about.
874 rt_newmaddrmsg(cmd, ifma)
876 struct ifmultiaddr *ifma;
878 struct rt_addrinfo info;
879 struct mbuf *m = NULL;
880 struct ifnet *ifp = ifma->ifma_ifp;
881 struct ifma_msghdr *ifmam;
883 if (route_cb.any_count == 0)
886 bzero(&info, sizeof info);
887 info.sa_ifaaddr = ifma->ifma_addr;
888 if (ifp != NULL && TAILQ_FIRST(&ifp->if_addrhead) != NULL)
889 info.sa_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
891 info.sa_ifpaddr = NULL;
893 * If a link-layer address is present, present it as a ``gateway''
894 * (similarly to how ARP entries, e.g., are presented).
896 info.sa_gateway = ifma->ifma_lladdr;
898 m = rt_msg1(cmd, &info);
902 ifmam = mtod(m, struct ifma_msghdr *);
903 ifmam->ifmam_index = ifp->if_index;
904 ifmam->ifmam_addrs = info.rti_addrs;
905 route_proto.sp_protocol = ifma->ifma_addr->sa_family;
907 raw_input(m, &route_proto, &route_src, &route_dst);
911 * This is called to generate routing socket messages indicating
912 * network interface arrival and departure.
915 rt_ifannouncemsg(ifp, what)
919 struct if_announcemsghdr *ifan;
921 struct rt_addrinfo info;
923 if (route_cb.any_count == 0)
926 bzero(&info, sizeof info);
928 m = rt_msg1(RTM_IFANNOUNCE, &info);
932 ifan = mtod(m, struct if_announcemsghdr *);
933 ifan->ifan_index = ifp->if_index;
934 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
935 ifan->ifan_what = what;
937 route_proto.sp_protocol = 0;
939 raw_input(m, &route_proto, &route_src, &route_dst);
943 * This is used in dumping the kernel table via sysctl().
946 sysctl_dumpentry(rn, vw)
947 struct radix_node *rn;
950 struct walkarg *w = vw;
951 struct rtentry *rt = (struct rtentry *)rn;
953 struct rt_addrinfo info;
955 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
958 bzero(&info, sizeof info);
959 info.sa_dst = rt_key(rt);
960 info.sa_gateway = rt->rt_gateway;
961 info.sa_netmask = rt_mask(rt);
962 info.sa_genmask = rt->rt_genmask;
963 if (rt->rt_ifp != NULL) {
965 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
966 info.sa_ifaaddr = rt->rt_ifa->ifa_addr;
967 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
968 info.sa_bcastaddr = rt->rt_ifa->ifa_dstaddr;
970 size = rt_msg2(RTM_GET, &info, NULL, w);
971 if (w->w_req != NULL && w->w_tmem != NULL) {
972 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
974 rtm->rtm_flags = rt->rt_flags;
975 rtm->rtm_use = rt->rt_use;
976 rtm->rtm_rmx = rt->rt_rmx;
977 rtm->rtm_index = rt->rt_ifp->if_index;
978 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
979 rtm->rtm_addrs = info.rti_addrs;
980 error = SYSCTL_OUT(w->w_req, rtm, size);
993 struct rt_addrinfo info;
996 bzero(&info, sizeof info);
997 TAILQ_FOREACH(ifp, &ifnet, if_link) {
998 if (w->w_arg && w->w_arg != ifp->if_index)
1000 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1001 info.sa_ifpaddr = ifa->ifa_addr;
1002 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1003 info.sa_ifpaddr = NULL;
1004 if (w->w_req != NULL && w->w_tmem != NULL) {
1005 struct if_msghdr *ifm;
1007 ifm = (struct if_msghdr *)w->w_tmem;
1008 ifm->ifm_index = ifp->if_index;
1009 ifm->ifm_flags = (u_short)ifp->if_flags;
1010 ifm->ifm_data = ifp->if_data;
1011 ifm->ifm_addrs = info.rti_addrs;
1012 error = SYSCTL_OUT(w->w_req, ifm, len);
1016 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1017 if (af && af != ifa->ifa_addr->sa_family)
1019 if (curproc->p_ucred->cr_prison && prison_if(curthread, ifa->ifa_addr))
1021 info.sa_ifaaddr = ifa->ifa_addr;
1022 info.sa_netmask = ifa->ifa_netmask;
1023 info.sa_bcastaddr = ifa->ifa_dstaddr;
1024 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1025 if (w->w_req && w->w_tmem) {
1026 struct ifa_msghdr *ifam;
1028 ifam = (struct ifa_msghdr *)w->w_tmem;
1029 ifam->ifam_index = ifa->ifa_ifp->if_index;
1030 ifam->ifam_flags = ifa->ifa_flags;
1031 ifam->ifam_metric = ifa->ifa_metric;
1032 ifam->ifam_addrs = info.rti_addrs;
1033 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1038 info.sa_netmask = info.sa_ifaaddr = info.sa_bcastaddr = NULL;
1044 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1046 int *name = (int *)arg1;
1047 u_int namelen = arg2;
1048 struct radix_node_head *rnh;
1049 int i, s, error = EINVAL;
1060 bzero(&w, sizeof w);
1070 for (i = 1; i <= AF_MAX; i++)
1071 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1072 (error = rnh->rnh_walktree(rnh,
1073 sysctl_dumpentry, &w)))
1078 error = sysctl_iflist(af, &w);
1082 free(w.w_tmem, M_RTABLE);
1086 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1089 * Definitions of protocols supported in the ROUTE domain.
1092 extern struct domain routedomain; /* or at least forward */
1094 static struct protosw routesw[] = {
1095 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1096 0, route_output, raw_ctlinput, 0,
1103 static struct domain routedomain =
1104 { PF_ROUTE, "route", 0, 0, 0,
1105 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])] };