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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/domain.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
44 #include <sys/protosw.h>
45 #include <sys/signalvar.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
52 #include <net/netisr.h>
53 #include <net/raw_cb.h>
54 #include <net/route.h>
56 #include <netinet/in.h>
58 #include <netinet6/scope6_var.h>
62 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
65 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
67 /* NB: these are not modified */
68 static struct sockaddr route_src = { 2, PF_ROUTE, };
69 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
72 int ip_count; /* attached w/ AF_INET */
73 int ip6_count; /* attached w/ AF_INET6 */
74 int ipx_count; /* attached w/ AF_IPX */
75 int any_count; /* total attached */
78 struct mtx rtsock_mtx;
79 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
81 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
82 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
83 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
85 static struct ifqueue rtsintrq;
87 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
88 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
89 &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
95 struct sysctl_req *w_req;
98 static void rts_input(struct mbuf *m);
99 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
100 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
101 caddr_t cp, struct walkarg *w);
102 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
103 struct rt_addrinfo *rtinfo);
104 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
105 static int sysctl_iflist(int af, struct walkarg *w);
106 static int sysctl_ifmalist(int af, struct walkarg *w);
107 static int route_output(struct mbuf *m, struct socket *so);
108 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
109 struct rt_metrics_lite *out);
110 static void rt_getmetrics(const struct rt_metrics_lite *in,
111 struct rt_metrics *out);
112 static void rt_dispatch(struct mbuf *, const struct sockaddr *);
119 rtsintrq.ifq_maxlen = 256;
120 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
121 rtsintrq.ifq_maxlen = tmp;
122 mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
123 netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE);
125 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
128 rts_input(struct mbuf *m)
130 struct sockproto route_proto;
131 unsigned short *family;
134 route_proto.sp_family = PF_ROUTE;
135 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
137 family = (unsigned short *)(tag + 1);
138 route_proto.sp_protocol = *family;
139 m_tag_delete(m, tag);
141 route_proto.sp_protocol = 0;
143 raw_input(m, &route_proto, &route_src);
147 * It really doesn't make any sense at all for this code to share much
148 * with raw_usrreq.c, since its functionality is so restricted. XXX
151 rts_abort(struct socket *so)
154 raw_usrreqs.pru_abort(so);
158 rts_close(struct socket *so)
161 raw_usrreqs.pru_close(so);
164 /* pru_accept is EOPNOTSUPP */
167 rts_attach(struct socket *so, int proto, struct thread *td)
172 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
175 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
180 * The splnet() is necessary to block protocols from sending
181 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
182 * this PCB is extant but incompletely initialized.
183 * Probably we should try to do more of this work beforehand and
187 so->so_pcb = (caddr_t)rp;
188 so->so_fibnum = td->td_proc->p_fibnum;
189 error = raw_attach(so, proto);
198 switch(rp->rcb_proto.sp_protocol) {
203 route_cb.ip6_count++;
206 route_cb.ipx_count++;
209 route_cb.any_count++;
212 so->so_options |= SO_USELOOPBACK;
218 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
221 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
225 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
228 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
231 /* pru_connect2 is EOPNOTSUPP */
232 /* pru_control is EOPNOTSUPP */
235 rts_detach(struct socket *so)
237 struct rawcb *rp = sotorawcb(so);
239 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
242 switch(rp->rcb_proto.sp_protocol) {
247 route_cb.ip6_count--;
250 route_cb.ipx_count--;
253 route_cb.any_count--;
255 raw_usrreqs.pru_detach(so);
259 rts_disconnect(struct socket *so)
262 return (raw_usrreqs.pru_disconnect(so));
265 /* pru_listen is EOPNOTSUPP */
268 rts_peeraddr(struct socket *so, struct sockaddr **nam)
271 return (raw_usrreqs.pru_peeraddr(so, nam));
274 /* pru_rcvd is EOPNOTSUPP */
275 /* pru_rcvoob is EOPNOTSUPP */
278 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
279 struct mbuf *control, struct thread *td)
282 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
285 /* pru_sense is null */
288 rts_shutdown(struct socket *so)
291 return (raw_usrreqs.pru_shutdown(so));
295 rts_sockaddr(struct socket *so, struct sockaddr **nam)
298 return (raw_usrreqs.pru_sockaddr(so, nam));
301 static struct pr_usrreqs route_usrreqs = {
302 .pru_abort = rts_abort,
303 .pru_attach = rts_attach,
304 .pru_bind = rts_bind,
305 .pru_connect = rts_connect,
306 .pru_detach = rts_detach,
307 .pru_disconnect = rts_disconnect,
308 .pru_peeraddr = rts_peeraddr,
309 .pru_send = rts_send,
310 .pru_shutdown = rts_shutdown,
311 .pru_sockaddr = rts_sockaddr,
312 .pru_close = rts_close,
315 #ifndef _SOCKADDR_UNION_DEFINED
316 #define _SOCKADDR_UNION_DEFINED
318 * The union of all possible address formats we handle.
320 union sockaddr_union {
322 struct sockaddr_in sin;
323 struct sockaddr_in6 sin6;
325 #endif /* _SOCKADDR_UNION_DEFINED */
328 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
329 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
332 /* First, see if the returned address is part of the jail. */
333 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
334 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
338 switch (info->rti_info[RTAX_DST]->sa_family) {
348 * Try to find an address on the given outgoing interface
349 * that belongs to the jail.
351 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
354 if (sa->sa_family != AF_INET)
356 ia = ((struct sockaddr_in *)sa)->sin_addr;
357 if (prison_check_ip4(cred, &ia) == 0) {
364 * As a last resort return the 'default' jail address.
366 if (prison_get_ip4(cred, &ia) != 0)
369 bzero(&saun->sin, sizeof(struct sockaddr_in));
370 saun->sin.sin_len = sizeof(struct sockaddr_in);
371 saun->sin.sin_family = AF_INET;
372 saun->sin.sin_addr.s_addr = ia.s_addr;
373 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
386 * Try to find an address on the given outgoing interface
387 * that belongs to the jail.
389 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
392 if (sa->sa_family != AF_INET6)
394 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
395 &ia6, sizeof(struct in6_addr));
396 if (prison_check_ip6(cred, &ia6) == 0) {
403 * As a last resort return the 'default' jail address.
405 if (prison_get_ip6(cred, &ia6) != 0)
408 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
409 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
410 saun->sin6.sin6_family = AF_INET6;
411 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
412 if (sa6_recoverscope(&saun->sin6) != 0)
414 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
426 route_output(struct mbuf *m, struct socket *so)
428 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
429 struct rt_msghdr *rtm = NULL;
430 struct rtentry *rt = NULL;
431 struct radix_node_head *rnh;
432 struct rt_addrinfo info;
434 struct ifnet *ifp = NULL;
435 union sockaddr_union saun;
437 #define senderr(e) { error = e; goto flush;}
438 if (m == NULL || ((m->m_len < sizeof(long)) &&
439 (m = m_pullup(m, sizeof(long))) == NULL))
441 if ((m->m_flags & M_PKTHDR) == 0)
442 panic("route_output");
443 len = m->m_pkthdr.len;
444 if (len < sizeof(*rtm) ||
445 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
446 info.rti_info[RTAX_DST] = NULL;
449 R_Malloc(rtm, struct rt_msghdr *, len);
451 info.rti_info[RTAX_DST] = NULL;
454 m_copydata(m, 0, len, (caddr_t)rtm);
455 if (rtm->rtm_version != RTM_VERSION) {
456 info.rti_info[RTAX_DST] = NULL;
457 senderr(EPROTONOSUPPORT);
459 rtm->rtm_pid = curproc->p_pid;
460 bzero(&info, sizeof(info));
461 info.rti_addrs = rtm->rtm_addrs;
462 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
463 info.rti_info[RTAX_DST] = NULL;
466 info.rti_flags = rtm->rtm_flags;
467 if (info.rti_info[RTAX_DST] == NULL ||
468 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
469 (info.rti_info[RTAX_GATEWAY] != NULL &&
470 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
472 if (info.rti_info[RTAX_GENMASK]) {
473 struct radix_node *t;
474 t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
476 bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1,
477 (char *)(void *)t->rn_key + 1,
478 ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0)
479 info.rti_info[RTAX_GENMASK] =
480 (struct sockaddr *)t->rn_key;
486 * Verify that the caller has the appropriate privilege; RTM_GET
487 * is the only operation the non-superuser is allowed.
489 if (rtm->rtm_type != RTM_GET) {
490 error = priv_check(curthread, PRIV_NET_ROUTE);
495 switch (rtm->rtm_type) {
496 struct rtentry *saved_nrt;
499 if (info.rti_info[RTAX_GATEWAY] == NULL)
502 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
504 if (error == 0 && saved_nrt) {
506 rt_setmetrics(rtm->rtm_inits,
507 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
508 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
509 RT_REMREF(saved_nrt);
510 saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
511 RT_UNLOCK(saved_nrt);
517 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
529 rnh = rt_tables[so->so_fibnum][info.rti_info[RTAX_DST]->sa_family];
531 senderr(EAFNOSUPPORT);
532 RADIX_NODE_HEAD_LOCK(rnh);
533 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
534 info.rti_info[RTAX_NETMASK], rnh);
535 if (rt == NULL) { /* XXX looks bogus */
536 RADIX_NODE_HEAD_UNLOCK(rnh);
541 RADIX_NODE_HEAD_UNLOCK(rnh);
546 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
547 * returns a perfect match in case a netmask is
548 * specified. For host routes only a longest prefix
549 * match is returned so it is necessary to compare the
550 * existence of the netmask. If both have a netmask
551 * rnh_lookup() did a perfect match and if none of them
552 * have a netmask both are host routes which is also a
556 if (rtm->rtm_type != RTM_GET &&
557 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
562 switch(rtm->rtm_type) {
567 if ((rt->rt_flags & RTF_HOST) == 0
568 ? jailed(curthread->td_ucred)
569 : prison_if(curthread->td_ucred,
574 info.rti_info[RTAX_DST] = rt_key(rt);
575 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
576 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
577 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
578 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
581 info.rti_info[RTAX_IFP] =
582 ifp->if_addr->ifa_addr;
583 error = rtm_get_jailed(&info, ifp, rt,
584 &saun, curthread->td_ucred);
589 if (ifp->if_flags & IFF_POINTOPOINT)
590 info.rti_info[RTAX_BRD] =
591 rt->rt_ifa->ifa_dstaddr;
592 rtm->rtm_index = ifp->if_index;
594 info.rti_info[RTAX_IFP] = NULL;
595 info.rti_info[RTAX_IFA] = NULL;
597 } else if ((ifp = rt->rt_ifp) != NULL) {
598 rtm->rtm_index = ifp->if_index;
600 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
601 if (len > rtm->rtm_msglen) {
602 struct rt_msghdr *new_rtm;
603 R_Malloc(new_rtm, struct rt_msghdr *, len);
604 if (new_rtm == NULL) {
608 bcopy(rtm, new_rtm, rtm->rtm_msglen);
609 Free(rtm); rtm = new_rtm;
611 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
612 rtm->rtm_flags = rt->rt_flags;
614 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
615 rtm->rtm_addrs = info.rti_addrs;
620 * New gateway could require new ifaddr, ifp;
621 * flags may also be different; ifp may be specified
622 * by ll sockaddr when protocol address is ambiguous
624 if (((rt->rt_flags & RTF_GATEWAY) &&
625 info.rti_info[RTAX_GATEWAY] != NULL) ||
626 info.rti_info[RTAX_IFP] != NULL ||
627 (info.rti_info[RTAX_IFA] != NULL &&
628 !sa_equal(info.rti_info[RTAX_IFA],
629 rt->rt_ifa->ifa_addr))) {
631 RADIX_NODE_HEAD_LOCK(rnh);
632 error = rt_getifa_fib(&info, rt->rt_fibnum);
633 RADIX_NODE_HEAD_UNLOCK(rnh);
638 if (info.rti_ifa != NULL &&
639 info.rti_ifa != rt->rt_ifa &&
640 rt->rt_ifa != NULL &&
641 rt->rt_ifa->ifa_rtrequest != NULL) {
642 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
646 if (info.rti_info[RTAX_GATEWAY] != NULL) {
648 RADIX_NODE_HEAD_LOCK(rnh);
651 error = rt_setgate(rt, rt_key(rt),
652 info.rti_info[RTAX_GATEWAY]);
653 RADIX_NODE_HEAD_UNLOCK(rnh);
658 if (!(rt->rt_flags & RTF_LLINFO))
659 rt->rt_flags |= RTF_GATEWAY;
661 if (info.rti_ifa != NULL &&
662 info.rti_ifa != rt->rt_ifa) {
663 IFAREF(info.rti_ifa);
664 rt->rt_ifa = info.rti_ifa;
665 rt->rt_ifp = info.rti_ifp;
667 /* Allow some flags to be toggled on change. */
668 if (rtm->rtm_fmask & RTF_FMASK)
669 rt->rt_flags = (rt->rt_flags &
671 (rtm->rtm_flags & rtm->rtm_fmask);
672 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
674 rtm->rtm_index = rt->rt_ifp->if_index;
675 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
676 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
677 if (info.rti_info[RTAX_GENMASK])
678 rt->rt_genmask = info.rti_info[RTAX_GENMASK];
681 /* We don't support locks anymore */
694 rtm->rtm_errno = error;
696 rtm->rtm_flags |= RTF_DONE;
698 if (rt) /* XXX can this be true? */
701 struct rawcb *rp = NULL;
703 * Check to see if we don't want our own messages.
705 if ((so->so_options & SO_USELOOPBACK) == 0) {
706 if (route_cb.any_count <= 1) {
712 /* There is another listener, so construct message */
716 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
717 if (m->m_pkthdr.len < rtm->rtm_msglen) {
720 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
721 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
727 * XXX insure we don't get a copy by
728 * invalidating our protocol
730 unsigned short family = rp->rcb_proto.sp_family;
731 rp->rcb_proto.sp_family = 0;
732 rt_dispatch(m, info.rti_info[RTAX_DST]);
733 rp->rcb_proto.sp_family = family;
735 rt_dispatch(m, info.rti_info[RTAX_DST]);
743 rt_setmetrics(u_long which, const struct rt_metrics *in,
744 struct rt_metrics_lite *out)
746 #define metric(f, e) if (which & (f)) out->e = in->e;
748 * Only these are stored in the routing entry since introduction
749 * of tcp hostcache. The rest is ignored.
751 metric(RTV_MTU, rmx_mtu);
752 /* Userland -> kernel timebase conversion. */
753 if (which & RTV_EXPIRE)
754 out->rmx_expire = in->rmx_expire ?
755 in->rmx_expire - time_second + time_uptime : 0;
760 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
762 #define metric(e) out->e = in->e;
763 bzero(out, sizeof(*out));
765 /* Kernel -> userland timebase conversion. */
766 out->rmx_expire = in->rmx_expire ?
767 in->rmx_expire - time_uptime + time_second : 0;
772 * Extract the addresses of the passed sockaddrs.
773 * Do a little sanity checking so as to avoid bad memory references.
774 * This data is derived straight from userland.
777 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
782 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
783 if ((rtinfo->rti_addrs & (1 << i)) == 0)
785 sa = (struct sockaddr *)cp;
789 if (cp + sa->sa_len > cplim)
792 * there are no more.. quit now
793 * If there are more bits, they are in error.
794 * I've seen this. route(1) can evidently generate these.
795 * This causes kernel to core dump.
796 * for compatibility, If we see this, point to a safe address.
798 if (sa->sa_len == 0) {
799 rtinfo->rti_info[i] = &sa_zero;
800 return (0); /* should be EINVAL but for compat */
803 rtinfo->rti_info[i] = sa;
810 rt_msg1(int type, struct rt_addrinfo *rtinfo)
812 struct rt_msghdr *rtm;
822 len = sizeof(struct ifa_msghdr);
827 len = sizeof(struct ifma_msghdr);
831 len = sizeof(struct if_msghdr);
836 len = sizeof(struct if_announcemsghdr);
840 len = sizeof(struct rt_msghdr);
844 m = m_gethdr(M_DONTWAIT, MT_DATA);
845 if (m && len > MHLEN) {
846 MCLGET(m, M_DONTWAIT);
847 if ((m->m_flags & M_EXT) == 0) {
854 m->m_pkthdr.len = m->m_len = len;
855 m->m_pkthdr.rcvif = NULL;
856 rtm = mtod(m, struct rt_msghdr *);
857 bzero((caddr_t)rtm, len);
858 for (i = 0; i < RTAX_MAX; i++) {
859 if ((sa = rtinfo->rti_info[i]) == NULL)
861 rtinfo->rti_addrs |= (1 << i);
863 m_copyback(m, len, dlen, (caddr_t)sa);
866 if (m->m_pkthdr.len != len) {
870 rtm->rtm_msglen = len;
871 rtm->rtm_version = RTM_VERSION;
872 rtm->rtm_type = type;
877 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
880 int len, dlen, second_time = 0;
883 rtinfo->rti_addrs = 0;
889 len = sizeof(struct ifa_msghdr);
893 len = sizeof(struct if_msghdr);
897 len = sizeof(struct ifma_msghdr);
901 len = sizeof(struct rt_msghdr);
906 for (i = 0; i < RTAX_MAX; i++) {
909 if ((sa = rtinfo->rti_info[i]) == NULL)
911 rtinfo->rti_addrs |= (1 << i);
914 bcopy((caddr_t)sa, cp, (unsigned)dlen);
920 if (cp == NULL && w != NULL && !second_time) {
921 struct walkarg *rw = w;
924 if (rw->w_tmemsize < len) {
926 free(rw->w_tmem, M_RTABLE);
927 rw->w_tmem = (caddr_t)
928 malloc(len, M_RTABLE, M_NOWAIT);
930 rw->w_tmemsize = len;
940 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
942 rtm->rtm_version = RTM_VERSION;
943 rtm->rtm_type = type;
944 rtm->rtm_msglen = len;
950 * This routine is called to generate a message from the routing
951 * socket indicating that a redirect has occured, a routing lookup
952 * has failed, or that a protocol has detected timeouts to a particular
956 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
958 struct rt_msghdr *rtm;
960 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
962 if (route_cb.any_count == 0)
964 m = rt_msg1(type, rtinfo);
967 rtm = mtod(m, struct rt_msghdr *);
968 rtm->rtm_flags = RTF_DONE | flags;
969 rtm->rtm_errno = error;
970 rtm->rtm_addrs = rtinfo->rti_addrs;
975 * This routine is called to generate a message from the routing
976 * socket indicating that the status of a network interface has changed.
979 rt_ifmsg(struct ifnet *ifp)
981 struct if_msghdr *ifm;
983 struct rt_addrinfo info;
985 if (route_cb.any_count == 0)
987 bzero((caddr_t)&info, sizeof(info));
988 m = rt_msg1(RTM_IFINFO, &info);
991 ifm = mtod(m, struct if_msghdr *);
992 ifm->ifm_index = ifp->if_index;
993 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
994 ifm->ifm_data = ifp->if_data;
996 rt_dispatch(m, NULL);
1000 * This is called to generate messages from the routing socket
1001 * indicating a network interface has had addresses associated with it.
1002 * if we ever reverse the logic and replace messages TO the routing
1003 * socket indicate a request to configure interfaces, then it will
1004 * be unnecessary as the routing socket will automatically generate
1008 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1010 struct rt_addrinfo info;
1011 struct sockaddr *sa = NULL;
1013 struct mbuf *m = NULL;
1014 struct ifnet *ifp = ifa->ifa_ifp;
1016 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1017 ("unexpected cmd %u", cmd));
1020 * notify the SCTP stack
1021 * this will only get called when an address is added/deleted
1022 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1024 sctp_addr_change(ifa, cmd);
1026 if (route_cb.any_count == 0)
1028 for (pass = 1; pass < 3; pass++) {
1029 bzero((caddr_t)&info, sizeof(info));
1030 if ((cmd == RTM_ADD && pass == 1) ||
1031 (cmd == RTM_DELETE && pass == 2)) {
1032 struct ifa_msghdr *ifam;
1033 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1035 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1036 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1037 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1038 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1039 if ((m = rt_msg1(ncmd, &info)) == NULL)
1041 ifam = mtod(m, struct ifa_msghdr *);
1042 ifam->ifam_index = ifp->if_index;
1043 ifam->ifam_metric = ifa->ifa_metric;
1044 ifam->ifam_flags = ifa->ifa_flags;
1045 ifam->ifam_addrs = info.rti_addrs;
1047 if ((cmd == RTM_ADD && pass == 2) ||
1048 (cmd == RTM_DELETE && pass == 1)) {
1049 struct rt_msghdr *rtm;
1053 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1054 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1055 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1056 if ((m = rt_msg1(cmd, &info)) == NULL)
1058 rtm = mtod(m, struct rt_msghdr *);
1059 rtm->rtm_index = ifp->if_index;
1060 rtm->rtm_flags |= rt->rt_flags;
1061 rtm->rtm_errno = error;
1062 rtm->rtm_addrs = info.rti_addrs;
1069 * This is the analogue to the rt_newaddrmsg which performs the same
1070 * function but for multicast group memberhips. This is easier since
1071 * there is no route state to worry about.
1074 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1076 struct rt_addrinfo info;
1077 struct mbuf *m = NULL;
1078 struct ifnet *ifp = ifma->ifma_ifp;
1079 struct ifma_msghdr *ifmam;
1081 if (route_cb.any_count == 0)
1084 bzero((caddr_t)&info, sizeof(info));
1085 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1086 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1088 * If a link-layer address is present, present it as a ``gateway''
1089 * (similarly to how ARP entries, e.g., are presented).
1091 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1092 m = rt_msg1(cmd, &info);
1095 ifmam = mtod(m, struct ifma_msghdr *);
1096 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1098 ifmam->ifmam_index = ifp->if_index;
1099 ifmam->ifmam_addrs = info.rti_addrs;
1100 rt_dispatch(m, ifma->ifma_addr);
1103 static struct mbuf *
1104 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1105 struct rt_addrinfo *info)
1107 struct if_announcemsghdr *ifan;
1110 if (route_cb.any_count == 0)
1112 bzero((caddr_t)info, sizeof(*info));
1113 m = rt_msg1(type, info);
1115 ifan = mtod(m, struct if_announcemsghdr *);
1116 ifan->ifan_index = ifp->if_index;
1117 strlcpy(ifan->ifan_name, ifp->if_xname,
1118 sizeof(ifan->ifan_name));
1119 ifan->ifan_what = what;
1125 * This is called to generate routing socket messages indicating
1126 * IEEE80211 wireless events.
1127 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1130 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1133 struct rt_addrinfo info;
1135 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1138 * Append the ieee80211 data. Try to stick it in the
1139 * mbuf containing the ifannounce msg; otherwise allocate
1140 * a new mbuf and append.
1142 * NB: we assume m is a single mbuf.
1144 if (data_len > M_TRAILINGSPACE(m)) {
1145 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1150 bcopy(data, mtod(n, void *), data_len);
1151 n->m_len = data_len;
1153 } else if (data_len > 0) {
1154 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1155 m->m_len += data_len;
1157 if (m->m_flags & M_PKTHDR)
1158 m->m_pkthdr.len += data_len;
1159 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1160 rt_dispatch(m, NULL);
1165 * This is called to generate routing socket messages indicating
1166 * network interface arrival and departure.
1169 rt_ifannouncemsg(struct ifnet *ifp, int what)
1172 struct rt_addrinfo info;
1174 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1176 rt_dispatch(m, NULL);
1180 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1185 * Preserve the family from the sockaddr, if any, in an m_tag for
1186 * use when injecting the mbuf into the routing socket buffer from
1190 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1196 *(unsigned short *)(tag + 1) = sa->sa_family;
1197 m_tag_prepend(m, tag);
1199 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1203 * This is used in dumping the kernel table via sysctl().
1206 sysctl_dumpentry(struct radix_node *rn, void *vw)
1208 struct walkarg *w = vw;
1209 struct rtentry *rt = (struct rtentry *)rn;
1210 int error = 0, size;
1211 struct rt_addrinfo info;
1213 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1215 if ((rt->rt_flags & RTF_HOST) == 0
1216 ? jailed(w->w_req->td->td_ucred)
1217 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1219 bzero((caddr_t)&info, sizeof(info));
1220 info.rti_info[RTAX_DST] = rt_key(rt);
1221 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1222 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1223 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
1225 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1226 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1227 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1228 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1230 size = rt_msg2(RTM_GET, &info, NULL, w);
1231 if (w->w_req && w->w_tmem) {
1232 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1234 rtm->rtm_flags = rt->rt_flags;
1235 rtm->rtm_use = rt->rt_rmx.rmx_pksent;
1236 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1237 rtm->rtm_index = rt->rt_ifp->if_index;
1238 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1239 rtm->rtm_addrs = info.rti_addrs;
1240 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1247 sysctl_iflist(int af, struct walkarg *w)
1251 struct rt_addrinfo info;
1254 bzero((caddr_t)&info, sizeof(info));
1256 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1257 if (w->w_arg && w->w_arg != ifp->if_index)
1260 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1261 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1262 info.rti_info[RTAX_IFP] = NULL;
1263 if (w->w_req && w->w_tmem) {
1264 struct if_msghdr *ifm;
1266 ifm = (struct if_msghdr *)w->w_tmem;
1267 ifm->ifm_index = ifp->if_index;
1268 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1269 ifm->ifm_data = ifp->if_data;
1270 ifm->ifm_addrs = info.rti_addrs;
1271 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1275 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1276 if (af && af != ifa->ifa_addr->sa_family)
1278 if (prison_if(w->w_req->td->td_ucred,
1279 ifa->ifa_addr) != 0)
1281 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1282 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1283 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1284 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1285 if (w->w_req && w->w_tmem) {
1286 struct ifa_msghdr *ifam;
1288 ifam = (struct ifa_msghdr *)w->w_tmem;
1289 ifam->ifam_index = ifa->ifa_ifp->if_index;
1290 ifam->ifam_flags = ifa->ifa_flags;
1291 ifam->ifam_metric = ifa->ifa_metric;
1292 ifam->ifam_addrs = info.rti_addrs;
1293 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1298 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1299 info.rti_info[RTAX_BRD] = NULL;
1307 sysctl_ifmalist(int af, struct walkarg *w)
1310 struct ifmultiaddr *ifma;
1311 struct rt_addrinfo info;
1315 bzero((caddr_t)&info, sizeof(info));
1317 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1318 if (w->w_arg && w->w_arg != ifp->if_index)
1321 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1323 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1324 if (af && af != ifma->ifma_addr->sa_family)
1326 if (prison_if(w->w_req->td->td_ucred,
1327 ifma->ifma_addr) != 0)
1329 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1330 info.rti_info[RTAX_GATEWAY] =
1331 (ifma->ifma_addr->sa_family != AF_LINK) ?
1332 ifma->ifma_lladdr : NULL;
1333 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1334 if (w->w_req && w->w_tmem) {
1335 struct ifma_msghdr *ifmam;
1337 ifmam = (struct ifma_msghdr *)w->w_tmem;
1338 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1339 ifmam->ifmam_flags = 0;
1340 ifmam->ifmam_addrs = info.rti_addrs;
1341 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1343 IF_ADDR_UNLOCK(ifp);
1348 IF_ADDR_UNLOCK(ifp);
1356 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1358 int *name = (int *)arg1;
1359 u_int namelen = arg2;
1360 struct radix_node_head *rnh;
1361 int i, lim, error = EINVAL;
1370 return ((namelen < 3) ? EISDIR : ENOTDIR);
1374 bzero(&w, sizeof(w));
1379 error = sysctl_wire_old_buffer(req, 0);
1386 if (af == 0) { /* dump all tables */
1389 } else /* dump only one table */
1391 for (error = 0; error == 0 && i <= lim; i++)
1392 if ((rnh = rt_tables[req->td->td_proc->p_fibnum][i]) != NULL) {
1393 RADIX_NODE_HEAD_LOCK(rnh);
1394 error = rnh->rnh_walktree(rnh,
1395 sysctl_dumpentry, &w);
1396 RADIX_NODE_HEAD_UNLOCK(rnh);
1398 error = EAFNOSUPPORT;
1402 error = sysctl_iflist(af, &w);
1405 case NET_RT_IFMALIST:
1406 error = sysctl_ifmalist(af, &w);
1410 free(w.w_tmem, M_RTABLE);
1414 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1417 * Definitions of protocols supported in the ROUTE domain.
1420 static struct domain routedomain; /* or at least forward */
1422 static struct protosw routesw[] = {
1424 .pr_type = SOCK_RAW,
1425 .pr_domain = &routedomain,
1426 .pr_flags = PR_ATOMIC|PR_ADDR,
1427 .pr_output = route_output,
1428 .pr_ctlinput = raw_ctlinput,
1429 .pr_init = raw_init,
1430 .pr_usrreqs = &route_usrreqs
1434 static struct domain routedomain = {
1435 .dom_family = PF_ROUTE,
1436 .dom_name = "route",
1437 .dom_protosw = routesw,
1438 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]