2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 2004, 2005 Jeffrey M. Hsu. All rights reserved.
36 * License terms: all terms for the DragonFly license above plus the following:
38 * 4. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
41 * This product includes software developed by Jeffrey M. Hsu
42 * for the DragonFly Project.
44 * This requirement may be waived with permission from Jeffrey Hsu.
45 * Permission will be granted to any DragonFly user for free.
46 * This requirement will sunset and may be removed on Jan 31, 2006,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1988, 1991, 1993
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
84 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
85 * $DragonFly: src/sys/net/rtsock.c,v 1.20 2005/01/06 17:59:32 hsu Exp $
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/sysctl.h>
93 #include <sys/malloc.h>
95 #include <sys/protosw.h>
96 #include <sys/socket.h>
97 #include <sys/socketvar.h>
98 #include <sys/domain.h>
100 #include <machine/stdarg.h>
103 #include <net/route.h>
104 #include <net/raw_cb.h>
106 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
108 static struct route_cb {
116 static struct sockaddr route_dst = { 2, PF_ROUTE, };
117 static struct sockaddr route_src = { 2, PF_ROUTE, };
118 static struct sockaddr sa_zero = { sizeof sa_zero, AF_INET, };
119 static struct sockproto route_proto = { PF_ROUTE, };
125 struct sysctl_req *w_req;
129 rt_msg1 (int, struct rt_addrinfo *);
130 static int rt_msg2 (int, struct rt_addrinfo *, caddr_t, struct walkarg *);
131 static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
132 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
133 static int sysctl_iflist (int af, struct walkarg *w);
134 static int route_output(struct mbuf *, struct socket *, ...);
135 static void rt_setmetrics (u_long, struct rt_metrics *,
136 struct rt_metrics *);
139 * It really doesn't make any sense at all for this code to share much
140 * with raw_usrreq.c, since its functionality is so restricted. XXX
143 rts_abort(struct socket *so)
148 error = raw_usrreqs.pru_abort(so);
153 /* pru_accept is EOPNOTSUPP */
156 rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
161 if (sotorawcb(so) != NULL)
162 return EISCONN; /* XXX panic? */
164 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK|M_ZERO);
169 * The splnet() is necessary to block protocols from sending
170 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
171 * this PCB is extant but incompletely initialized.
172 * Probably we should try to do more of this work beforehand and
177 error = raw_attach(so, proto, ai->sb_rlimit);
184 switch(rp->rcb_proto.sp_protocol) {
189 route_cb.ip6_count++;
192 route_cb.ipx_count++;
198 rp->rcb_faddr = &route_src;
199 route_cb.any_count++;
201 so->so_options |= SO_USELOOPBACK;
207 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
212 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
218 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
223 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
228 /* pru_connect2 is EOPNOTSUPP */
229 /* pru_control is EOPNOTSUPP */
232 rts_detach(struct socket *so)
234 struct rawcb *rp = sotorawcb(so);
239 switch(rp->rcb_proto.sp_protocol) {
244 route_cb.ip6_count--;
247 route_cb.ipx_count--;
253 route_cb.any_count--;
255 error = raw_usrreqs.pru_detach(so);
261 rts_disconnect(struct socket *so)
266 error = raw_usrreqs.pru_disconnect(so);
271 /* pru_listen is EOPNOTSUPP */
274 rts_peeraddr(struct socket *so, struct sockaddr **nam)
279 error = raw_usrreqs.pru_peeraddr(so, nam);
284 /* pru_rcvd is EOPNOTSUPP */
285 /* pru_rcvoob is EOPNOTSUPP */
288 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
289 struct mbuf *control, struct thread *td)
294 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
299 /* pru_sense is null */
302 rts_shutdown(struct socket *so)
307 error = raw_usrreqs.pru_shutdown(so);
313 rts_sockaddr(struct socket *so, struct sockaddr **nam)
318 error = raw_usrreqs.pru_sockaddr(so, nam);
323 static struct pr_usrreqs route_usrreqs = {
324 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
325 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
326 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
327 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
328 sosend, soreceive, sopoll
333 route_output(struct mbuf *m, struct socket *so, ...)
335 struct rt_msghdr *rtm = NULL;
336 struct rtentry *rt = NULL;
337 struct rtentry *saved_nrt = NULL;
338 struct radix_node_head *rnh;
339 struct ifnet *ifp = NULL;
340 struct ifaddr *ifa = NULL;
341 struct rawcb *rp = NULL;
342 struct pr_output_info *oi;
343 struct rt_addrinfo info;
348 oi = __va_arg(ap, struct pr_output_info *);
351 #define gotoerr(e) { error = e; goto flush;}
352 if (m == NULL || ((m->m_len < sizeof(long)) &&
353 (m = m_pullup(m, sizeof(long))) == NULL))
355 if (!(m->m_flags & M_PKTHDR))
356 panic("route_output");
357 len = m->m_pkthdr.len;
358 if (len < sizeof *rtm ||
359 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
363 R_Malloc(rtm, struct rt_msghdr *, len);
368 m_copydata(m, 0, len, (caddr_t)rtm);
369 if (rtm->rtm_version != RTM_VERSION) {
371 gotoerr(EPROTONOSUPPORT);
373 rtm->rtm_pid = oi->p_pid;
374 bzero(&info, sizeof info);
375 info.rti_addrs = rtm->rtm_addrs;
376 if (rt_xaddrs((char *)(rtm + 1), len + (char *)rtm, &info)) {
380 info.rti_flags = rtm->rtm_flags;
381 if (info.sa_dst == NULL || info.sa_dst->sa_family >= AF_MAX ||
382 (info.sa_gateway != NULL && (info.sa_gateway->sa_family >= AF_MAX)))
385 if (info.sa_genmask != NULL) {
386 struct radix_node *t;
389 t = rn_addmask((char *)info.sa_genmask, TRUE, 1);
391 info.sa_genmask->sa_len >= (klen = *(u_char *)t->rn_key) &&
392 bcmp((char *)info.sa_genmask + 1, (char *)t->rn_key + 1,
394 info.sa_genmask = (struct sockaddr *)(t->rn_key);
400 * Verify that the caller has the appropriate privilege; RTM_GET
401 * is the only operation the non-superuser is allowed.
403 if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
406 switch (rtm->rtm_type) {
409 if (info.sa_gateway == NULL)
411 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
412 if (error == 0 && saved_nrt != NULL) {
413 rt_setmetrics(rtm->rtm_inits,
414 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
415 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
416 saved_nrt->rt_rmx.rmx_locks |=
417 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
418 saved_nrt->rt_refcnt--;
419 saved_nrt->rt_genmask = info.sa_genmask;
424 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
426 if ((rt = saved_nrt))
435 if ((rnh = rt_tables[info.sa_dst->sa_family]) == NULL) {
436 gotoerr(EAFNOSUPPORT);
437 } else if ((rt = (struct rtentry *) rnh->rnh_lookup(
438 (char *)info.sa_dst, (char *)info.sa_netmask, rnh)) != NULL)
442 switch(rtm->rtm_type) {
446 info.sa_dst = rt_key(rt);
447 info.sa_gateway = rt->rt_gateway;
448 info.sa_netmask = rt_mask(rt);
449 info.sa_genmask = rt->rt_genmask;
450 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
454 TAILQ_FIRST(&ifp->if_addrhead)->
456 info.sa_ifaaddr = rt->rt_ifa->ifa_addr;
457 if (ifp->if_flags & IFF_POINTOPOINT)
459 rt->rt_ifa->ifa_dstaddr;
460 rtm->rtm_index = ifp->if_index;
462 info.sa_ifpaddr = NULL;
463 info.sa_ifaaddr = NULL;
466 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
467 if (len > rtm->rtm_msglen) {
468 struct rt_msghdr *new_rtm;
469 R_Malloc(new_rtm, struct rt_msghdr *, len);
472 bcopy(rtm, new_rtm, rtm->rtm_msglen);
473 Free(rtm); rtm = new_rtm;
475 rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
476 rtm->rtm_flags = rt->rt_flags;
477 rtm->rtm_rmx = rt->rt_rmx;
478 rtm->rtm_addrs = info.rti_addrs;
483 * new gateway could require new ifaddr, ifp;
484 * flags may also be different; ifp may be specified
485 * by ll sockaddr when protocol address is ambiguous
487 if (((rt->rt_flags & RTF_GATEWAY) &&
488 info.sa_gateway != NULL) ||
489 info.sa_ifpaddr != NULL ||
490 (info.sa_ifaaddr != NULL &&
491 sa_equal(info.sa_ifaaddr, rt->rt_ifa->ifa_addr))) {
492 if ((error = rt_getifa(&info)) != 0)
495 if (info.sa_gateway != NULL &&
496 (error = rt_setgate(rt, rt_key(rt),
497 info.sa_gateway)) != 0)
499 if ((ifa = info.rti_ifa) != NULL) {
500 struct ifaddr *oifa = rt->rt_ifa;
503 if (oifa && oifa->ifa_rtrequest)
504 oifa->ifa_rtrequest(RTM_DELETE,
509 rt->rt_ifp = info.rti_ifp;
512 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
514 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
515 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
516 if (info.sa_genmask != NULL)
517 rt->rt_genmask = info.sa_genmask;
522 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
523 rt->rt_rmx.rmx_locks |=
524 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
536 rtm->rtm_errno = error;
538 rtm->rtm_flags |= RTF_DONE;
543 * Check to see if we don't want our own messages.
545 if (!(so->so_options & SO_USELOOPBACK)) {
546 if (route_cb.any_count <= 1) {
552 /* There is another listener, so construct message */
556 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
557 if (m->m_pkthdr.len < rtm->rtm_msglen) {
560 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
561 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
565 rp->rcb_proto.sp_family = 0; /* Avoid us */
566 if (info.sa_dst != NULL)
567 route_proto.sp_protocol = info.sa_dst->sa_family;
569 raw_input(m, &route_proto, &route_src, &route_dst);
571 rp->rcb_proto.sp_family = PF_ROUTE;
576 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
578 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
579 setmetric(RTV_RPIPE, rmx_recvpipe);
580 setmetric(RTV_SPIPE, rmx_sendpipe);
581 setmetric(RTV_SSTHRESH, rmx_ssthresh);
582 setmetric(RTV_RTT, rmx_rtt);
583 setmetric(RTV_RTTVAR, rmx_rttvar);
584 setmetric(RTV_HOPCOUNT, rmx_hopcount);
585 setmetric(RTV_MTU, rmx_mtu);
586 setmetric(RTV_EXPIRE, rmx_expire);
591 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
592 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
595 * Extract the addresses of the passed sockaddrs.
596 * Do a little sanity checking so as to avoid bad memory references.
597 * This data is derived straight from userland.
600 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
605 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
606 if ((rtinfo->rti_addrs & (1 << i)) == 0)
608 sa = (struct sockaddr *)cp;
612 if ((cp + sa->sa_len) > cplim) {
617 * There are no more... Quit now.
618 * If there are more bits, they are in error.
619 * I've seen this. route(1) can evidently generate these.
620 * This causes kernel to core dump.
621 * For compatibility, if we see this, point to a safe address.
623 if (sa->sa_len == 0) {
624 rtinfo->rti_info[i] = &sa_zero;
625 return (0); /* should be EINVAL but for compat */
628 /* Accept the sockaddr. */
629 rtinfo->rti_info[i] = sa;
636 rt_msg1(int type, struct rt_addrinfo *rtinfo)
638 struct rt_msghdr *rtm;
648 len = sizeof(struct ifa_msghdr);
653 len = sizeof(struct ifma_msghdr);
657 len = sizeof(struct if_msghdr);
661 len = sizeof(struct if_announcemsghdr);
665 len = sizeof(struct rt_msghdr);
669 m = m_gethdr(MB_DONTWAIT, MT_DATA);
670 if (m && len > MHLEN) {
671 MCLGET(m, MB_DONTWAIT);
672 if (!(m->m_flags & M_EXT)) {
679 m->m_pkthdr.len = m->m_len = len;
680 m->m_pkthdr.rcvif = NULL;
681 rtm = mtod(m, struct rt_msghdr *);
683 for (i = 0; i < RTAX_MAX; i++) {
684 if ((sa = rtinfo->rti_info[i]) == NULL)
686 rtinfo->rti_addrs |= (1 << i);
687 dlen = ROUNDUP(sa->sa_len);
688 m_copyback(m, len, dlen, (caddr_t)sa);
691 if (m->m_pkthdr.len != len) {
695 rtm->rtm_msglen = len;
696 rtm->rtm_version = RTM_VERSION;
697 rtm->rtm_type = type;
702 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
706 boolean_t second_time = FALSE;
709 rtinfo->rti_addrs = NULL;
715 len = sizeof(struct ifa_msghdr);
719 len = sizeof(struct if_msghdr);
723 len = sizeof(struct rt_msghdr);
729 for (i = 0; i < RTAX_MAX; i++) {
732 if ((sa = rtinfo->rti_info[i]) == NULL)
734 rtinfo->rti_addrs |= (1 << i);
735 dlen = ROUNDUP(sa->sa_len);
743 if (cp == NULL && w != NULL && !second_time) {
744 struct walkarg *rw = w;
746 if (rw->w_req != NULL) {
747 if (rw->w_tmemsize < len) {
749 free(rw->w_tmem, M_RTABLE);
750 rw->w_tmem = malloc(len, M_RTABLE,
751 M_INTWAIT | M_NULLOK);
753 rw->w_tmemsize = len;
755 if (rw->w_tmem != NULL) {
763 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
765 rtm->rtm_version = RTM_VERSION;
766 rtm->rtm_type = type;
767 rtm->rtm_msglen = len;
773 * This routine is called to generate a message from the routing
774 * socket indicating that a redirect has occurred, a routing lookup
775 * has failed, or that a protocol has detected timeouts to a particular
779 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
781 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
782 struct rt_msghdr *rtm;
785 if (route_cb.any_count == 0)
787 m = rt_msg1(type, rtinfo);
790 rtm = mtod(m, struct rt_msghdr *);
791 rtm->rtm_flags = RTF_DONE | flags;
792 rtm->rtm_errno = error;
793 rtm->rtm_addrs = rtinfo->rti_addrs;
794 route_proto.sp_protocol = (dst != NULL) ? dst->sa_family : 0;
795 raw_input(m, &route_proto, &route_src, &route_dst);
799 rt_dstmsg(int type, struct sockaddr *dst, int error)
801 struct rt_msghdr *rtm;
802 struct rt_addrinfo addrs;
805 if (route_cb.any_count == 0)
807 bzero(&addrs, sizeof(struct rt_addrinfo));
808 addrs.rti_info[RTAX_DST] = dst;
809 m = rt_msg1(type, &addrs);
812 rtm = mtod(m, struct rt_msghdr *);
813 rtm->rtm_flags = RTF_DONE;
814 rtm->rtm_errno = error;
815 rtm->rtm_addrs = addrs.rti_addrs;
816 route_proto.sp_protocol = (dst != NULL) ? dst->sa_family : 0;
817 raw_input(m, &route_proto, &route_src, &route_dst);
821 * This routine is called to generate a message from the routing
822 * socket indicating that the status of a network interface has changed.
825 rt_ifmsg(struct ifnet *ifp)
827 struct if_msghdr *ifm;
829 struct rt_addrinfo info;
831 if (route_cb.any_count == 0)
833 bzero(&info, sizeof info);
834 m = rt_msg1(RTM_IFINFO, &info);
837 ifm = mtod(m, struct if_msghdr *);
838 ifm->ifm_index = ifp->if_index;
839 ifm->ifm_flags = (u_short)ifp->if_flags;
840 ifm->ifm_data = ifp->if_data;
841 ifm->ifm_addrs = NULL;
842 route_proto.sp_protocol = 0;
843 raw_input(m, &route_proto, &route_src, &route_dst);
847 rt_ifamsg(int cmd, struct ifaddr *ifa)
849 struct ifa_msghdr *ifam;
850 struct rt_addrinfo info;
853 struct ifnet *ifp = ifa->ifa_ifp;
855 bzero(&info, sizeof info);
856 info.sa_ifaaddr = sa = ifa->ifa_addr;
857 info.sa_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
858 info.sa_netmask = ifa->ifa_netmask;
859 info.sa_bcastaddr = ifa->ifa_dstaddr;
861 m = rt_msg1(cmd, &info);
865 ifam = mtod(m, struct ifa_msghdr *);
866 ifam->ifam_index = ifp->if_index;
867 ifam->ifam_metric = ifa->ifa_metric;
868 ifam->ifam_flags = ifa->ifa_flags;
869 ifam->ifam_addrs = info.rti_addrs;
871 route_proto.sp_protocol = sa ? sa->sa_family : 0;
873 raw_input(m, &route_proto, &route_src, &route_dst);
877 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
879 struct rt_msghdr *rtm;
880 struct rt_addrinfo info;
882 struct sockaddr *dst;
887 bzero(&info, sizeof info);
888 info.sa_dst = dst = rt_key(rt);
889 info.sa_gateway = rt->rt_gateway;
890 info.sa_netmask = rt_mask(rt);
892 info.sa_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
893 info.sa_ifaaddr = rt->rt_ifa->ifa_addr;
895 m = rt_msg1(cmd, &info);
899 rtm = mtod(m, struct rt_msghdr *);
901 rtm->rtm_index = ifp->if_index;
902 rtm->rtm_flags |= rt->rt_flags;
903 rtm->rtm_errno = error;
904 rtm->rtm_addrs = info.rti_addrs;
906 route_proto.sp_protocol = (dst != NULL) ? dst->sa_family : 0;
908 raw_input(m, &route_proto, &route_src, &route_dst);
912 * This is called to generate messages from the routing socket
913 * indicating a network interface has had addresses associated with it.
914 * if we ever reverse the logic and replace messages TO the routing
915 * socket indicate a request to configure interfaces, then it will
916 * be unnecessary as the routing socket will automatically generate
920 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
922 if (route_cb.any_count == 0)
925 if (cmd == RTM_ADD) {
926 rt_ifamsg(RTM_NEWADDR, ifa);
927 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
929 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
930 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
931 rt_ifamsg(RTM_DELADDR, ifa);
936 * This is the analogue to the rt_newaddrmsg which performs the same
937 * function but for multicast group memberhips. This is easier since
938 * there is no route state to worry about.
941 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
943 struct rt_addrinfo info;
944 struct mbuf *m = NULL;
945 struct ifnet *ifp = ifma->ifma_ifp;
946 struct ifma_msghdr *ifmam;
948 if (route_cb.any_count == 0)
951 bzero(&info, sizeof info);
952 info.sa_ifaaddr = ifma->ifma_addr;
953 if (ifp != NULL && TAILQ_FIRST(&ifp->if_addrhead) != NULL)
954 info.sa_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
956 info.sa_ifpaddr = NULL;
958 * If a link-layer address is present, present it as a ``gateway''
959 * (similarly to how ARP entries, e.g., are presented).
961 info.sa_gateway = ifma->ifma_lladdr;
963 m = rt_msg1(cmd, &info);
967 ifmam = mtod(m, struct ifma_msghdr *);
968 ifmam->ifmam_index = ifp->if_index;
969 ifmam->ifmam_addrs = info.rti_addrs;
970 route_proto.sp_protocol = ifma->ifma_addr->sa_family;
972 raw_input(m, &route_proto, &route_src, &route_dst);
976 * This is called to generate routing socket messages indicating
977 * network interface arrival and departure.
980 rt_ifannouncemsg(ifp, what)
984 struct if_announcemsghdr *ifan;
986 struct rt_addrinfo info;
988 if (route_cb.any_count == 0)
991 bzero(&info, sizeof info);
993 m = rt_msg1(RTM_IFANNOUNCE, &info);
997 ifan = mtod(m, struct if_announcemsghdr *);
998 ifan->ifan_index = ifp->if_index;
999 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1000 ifan->ifan_what = what;
1002 route_proto.sp_protocol = 0;
1004 raw_input(m, &route_proto, &route_src, &route_dst);
1008 * This is used in dumping the kernel table via sysctl().
1011 sysctl_dumpentry(rn, vw)
1012 struct radix_node *rn;
1015 struct walkarg *w = vw;
1016 struct rtentry *rt = (struct rtentry *)rn;
1017 int error = 0, size;
1018 struct rt_addrinfo info;
1020 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1023 bzero(&info, sizeof info);
1024 info.sa_dst = rt_key(rt);
1025 info.sa_gateway = rt->rt_gateway;
1026 info.sa_netmask = rt_mask(rt);
1027 info.sa_genmask = rt->rt_genmask;
1028 if (rt->rt_ifp != NULL) {
1030 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
1031 info.sa_ifaaddr = rt->rt_ifa->ifa_addr;
1032 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1033 info.sa_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1035 size = rt_msg2(RTM_GET, &info, NULL, w);
1036 if (w->w_req != NULL && w->w_tmem != NULL) {
1037 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1039 rtm->rtm_flags = rt->rt_flags;
1040 rtm->rtm_use = rt->rt_use;
1041 rtm->rtm_rmx = rt->rt_rmx;
1042 rtm->rtm_index = rt->rt_ifp->if_index;
1043 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1044 rtm->rtm_addrs = info.rti_addrs;
1045 error = SYSCTL_OUT(w->w_req, rtm, size);
1052 sysctl_iflist(af, w)
1058 struct rt_addrinfo info;
1061 bzero(&info, sizeof info);
1062 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1063 if (w->w_arg && w->w_arg != ifp->if_index)
1065 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1066 info.sa_ifpaddr = ifa->ifa_addr;
1067 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1068 info.sa_ifpaddr = NULL;
1069 if (w->w_req != NULL && w->w_tmem != NULL) {
1070 struct if_msghdr *ifm;
1072 ifm = (struct if_msghdr *)w->w_tmem;
1073 ifm->ifm_index = ifp->if_index;
1074 ifm->ifm_flags = (u_short)ifp->if_flags;
1075 ifm->ifm_data = ifp->if_data;
1076 ifm->ifm_addrs = info.rti_addrs;
1077 error = SYSCTL_OUT(w->w_req, ifm, len);
1081 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1082 if (af && af != ifa->ifa_addr->sa_family)
1084 if (curproc->p_ucred->cr_prison && prison_if(curthread, ifa->ifa_addr))
1086 info.sa_ifaaddr = ifa->ifa_addr;
1087 info.sa_netmask = ifa->ifa_netmask;
1088 info.sa_bcastaddr = ifa->ifa_dstaddr;
1089 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1090 if (w->w_req && w->w_tmem) {
1091 struct ifa_msghdr *ifam;
1093 ifam = (struct ifa_msghdr *)w->w_tmem;
1094 ifam->ifam_index = ifa->ifa_ifp->if_index;
1095 ifam->ifam_flags = ifa->ifa_flags;
1096 ifam->ifam_metric = ifa->ifa_metric;
1097 ifam->ifam_addrs = info.rti_addrs;
1098 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1103 info.sa_netmask = info.sa_ifaaddr = info.sa_bcastaddr = NULL;
1109 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1111 int *name = (int *)arg1;
1112 u_int namelen = arg2;
1113 struct radix_node_head *rnh;
1114 int i, s, error = EINVAL;
1125 bzero(&w, sizeof w);
1135 for (i = 1; i <= AF_MAX; i++)
1136 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1137 (error = rnh->rnh_walktree(rnh,
1138 sysctl_dumpentry, &w)))
1143 error = sysctl_iflist(af, &w);
1147 free(w.w_tmem, M_RTABLE);
1151 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1154 * Definitions of protocols supported in the ROUTE domain.
1157 extern struct domain routedomain; /* or at least forward */
1159 static struct protosw routesw[] = {
1160 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1161 0, route_output, raw_ctlinput, 0,
1168 static struct domain routedomain =
1169 { PF_ROUTE, "route", 0, 0, 0,
1170 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])] };