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
33 #include "opt_mpath.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/domain.h>
42 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/rwlock.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
55 #include <net/if_dl.h>
56 #include <net/if_llatbl.h>
57 #include <net/netisr.h>
58 #include <net/raw_cb.h>
59 #include <net/route.h>
62 #include <netinet/in.h>
64 #include <netinet6/scope6_var.h>
67 #if defined(INET) || defined(INET6)
69 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
73 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
75 /* NB: these are not modified */
76 static struct sockaddr route_src = { 2, PF_ROUTE, };
77 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
80 int ip_count; /* attached w/ AF_INET */
81 int ip6_count; /* attached w/ AF_INET6 */
82 int ipx_count; /* attached w/ AF_IPX */
83 int any_count; /* total attached */
86 struct mtx rtsock_mtx;
87 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
89 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
90 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
91 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
93 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
99 struct sysctl_req *w_req;
102 static void rts_input(struct mbuf *m);
103 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
104 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
105 caddr_t cp, struct walkarg *w);
106 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
107 struct rt_addrinfo *rtinfo);
108 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
109 static int sysctl_iflist(int af, struct walkarg *w);
110 static int sysctl_ifmalist(int af, struct walkarg *w);
111 static int route_output(struct mbuf *m, struct socket *so);
112 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
113 struct rt_metrics_lite *out);
114 static void rt_getmetrics(const struct rt_metrics_lite *in,
115 struct rt_metrics *out);
116 static void rt_dispatch(struct mbuf *, const struct sockaddr *);
118 static struct netisr_handler rtsock_nh = {
120 .nh_handler = rts_input,
121 .nh_proto = NETISR_ROUTE,
122 .nh_policy = NETISR_POLICY_SOURCE,
126 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
130 netisr_getqlimit(&rtsock_nh, &qlimit);
131 error = sysctl_handle_int(oidp, &qlimit, 0, req);
132 if (error || !req->newptr)
136 return (netisr_setqlimit(&rtsock_nh, qlimit));
138 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
139 0, 0, sysctl_route_netisr_maxqlen, "I",
140 "maximum routing socket dispatch queue length");
147 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
148 rtsock_nh.nh_qlimit = tmp;
149 netisr_register(&rtsock_nh);
151 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
154 rts_input(struct mbuf *m)
156 struct sockproto route_proto;
157 unsigned short *family;
160 route_proto.sp_family = PF_ROUTE;
161 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
163 family = (unsigned short *)(tag + 1);
164 route_proto.sp_protocol = *family;
165 m_tag_delete(m, tag);
167 route_proto.sp_protocol = 0;
169 raw_input(m, &route_proto, &route_src);
173 * It really doesn't make any sense at all for this code to share much
174 * with raw_usrreq.c, since its functionality is so restricted. XXX
177 rts_abort(struct socket *so)
180 raw_usrreqs.pru_abort(so);
184 rts_close(struct socket *so)
187 raw_usrreqs.pru_close(so);
190 /* pru_accept is EOPNOTSUPP */
193 rts_attach(struct socket *so, int proto, struct thread *td)
198 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
201 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
206 * The splnet() is necessary to block protocols from sending
207 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
208 * this PCB is extant but incompletely initialized.
209 * Probably we should try to do more of this work beforehand and
213 so->so_pcb = (caddr_t)rp;
214 so->so_fibnum = td->td_proc->p_fibnum;
215 error = raw_attach(so, proto);
224 switch(rp->rcb_proto.sp_protocol) {
229 route_cb.ip6_count++;
232 route_cb.ipx_count++;
235 route_cb.any_count++;
238 so->so_options |= SO_USELOOPBACK;
244 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
247 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
251 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
254 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
257 /* pru_connect2 is EOPNOTSUPP */
258 /* pru_control is EOPNOTSUPP */
261 rts_detach(struct socket *so)
263 struct rawcb *rp = sotorawcb(so);
265 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
268 switch(rp->rcb_proto.sp_protocol) {
273 route_cb.ip6_count--;
276 route_cb.ipx_count--;
279 route_cb.any_count--;
281 raw_usrreqs.pru_detach(so);
285 rts_disconnect(struct socket *so)
288 return (raw_usrreqs.pru_disconnect(so));
291 /* pru_listen is EOPNOTSUPP */
294 rts_peeraddr(struct socket *so, struct sockaddr **nam)
297 return (raw_usrreqs.pru_peeraddr(so, nam));
300 /* pru_rcvd is EOPNOTSUPP */
301 /* pru_rcvoob is EOPNOTSUPP */
304 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
305 struct mbuf *control, struct thread *td)
308 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
311 /* pru_sense is null */
314 rts_shutdown(struct socket *so)
317 return (raw_usrreqs.pru_shutdown(so));
321 rts_sockaddr(struct socket *so, struct sockaddr **nam)
324 return (raw_usrreqs.pru_sockaddr(so, nam));
327 static struct pr_usrreqs route_usrreqs = {
328 .pru_abort = rts_abort,
329 .pru_attach = rts_attach,
330 .pru_bind = rts_bind,
331 .pru_connect = rts_connect,
332 .pru_detach = rts_detach,
333 .pru_disconnect = rts_disconnect,
334 .pru_peeraddr = rts_peeraddr,
335 .pru_send = rts_send,
336 .pru_shutdown = rts_shutdown,
337 .pru_sockaddr = rts_sockaddr,
338 .pru_close = rts_close,
341 #ifndef _SOCKADDR_UNION_DEFINED
342 #define _SOCKADDR_UNION_DEFINED
344 * The union of all possible address formats we handle.
346 union sockaddr_union {
348 struct sockaddr_in sin;
349 struct sockaddr_in6 sin6;
351 #endif /* _SOCKADDR_UNION_DEFINED */
354 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
355 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
358 /* First, see if the returned address is part of the jail. */
359 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
360 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
364 switch (info->rti_info[RTAX_DST]->sa_family) {
374 * Try to find an address on the given outgoing interface
375 * that belongs to the jail.
378 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
381 if (sa->sa_family != AF_INET)
383 ia = ((struct sockaddr_in *)sa)->sin_addr;
384 if (prison_check_ip4(cred, &ia) == 0) {
392 * As a last resort return the 'default' jail address.
394 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
396 if (prison_get_ip4(cred, &ia) != 0)
399 bzero(&saun->sin, sizeof(struct sockaddr_in));
400 saun->sin.sin_len = sizeof(struct sockaddr_in);
401 saun->sin.sin_family = AF_INET;
402 saun->sin.sin_addr.s_addr = ia.s_addr;
403 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
416 * Try to find an address on the given outgoing interface
417 * that belongs to the jail.
420 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
423 if (sa->sa_family != AF_INET6)
425 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
426 &ia6, sizeof(struct in6_addr));
427 if (prison_check_ip6(cred, &ia6) == 0) {
435 * As a last resort return the 'default' jail address.
437 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
439 if (prison_get_ip6(cred, &ia6) != 0)
442 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
443 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
444 saun->sin6.sin6_family = AF_INET6;
445 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
446 if (sa6_recoverscope(&saun->sin6) != 0)
448 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
460 route_output(struct mbuf *m, struct socket *so)
462 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
463 struct rt_msghdr *rtm = NULL;
464 struct rtentry *rt = NULL;
465 struct radix_node_head *rnh;
466 struct rt_addrinfo info;
468 struct ifnet *ifp = NULL;
469 union sockaddr_union saun;
471 #define senderr(e) { error = e; goto flush;}
472 if (m == NULL || ((m->m_len < sizeof(long)) &&
473 (m = m_pullup(m, sizeof(long))) == NULL))
475 if ((m->m_flags & M_PKTHDR) == 0)
476 panic("route_output");
477 len = m->m_pkthdr.len;
478 if (len < sizeof(*rtm) ||
479 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
480 info.rti_info[RTAX_DST] = NULL;
483 R_Malloc(rtm, struct rt_msghdr *, len);
485 info.rti_info[RTAX_DST] = NULL;
488 m_copydata(m, 0, len, (caddr_t)rtm);
489 if (rtm->rtm_version != RTM_VERSION) {
490 info.rti_info[RTAX_DST] = NULL;
491 senderr(EPROTONOSUPPORT);
493 rtm->rtm_pid = curproc->p_pid;
494 bzero(&info, sizeof(info));
495 info.rti_addrs = rtm->rtm_addrs;
496 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
497 info.rti_info[RTAX_DST] = NULL;
500 info.rti_flags = rtm->rtm_flags;
501 if (info.rti_info[RTAX_DST] == NULL ||
502 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
503 (info.rti_info[RTAX_GATEWAY] != NULL &&
504 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
507 * Verify that the caller has the appropriate privilege; RTM_GET
508 * is the only operation the non-superuser is allowed.
510 if (rtm->rtm_type != RTM_GET) {
511 error = priv_check(curthread, PRIV_NET_ROUTE);
516 switch (rtm->rtm_type) {
517 struct rtentry *saved_nrt;
520 if (info.rti_info[RTAX_GATEWAY] == NULL)
524 /* support for new ARP code */
525 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
526 (rtm->rtm_flags & RTF_LLDATA) != 0) {
527 error = lla_rt_output(rtm, &info);
530 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
532 if (error == 0 && saved_nrt) {
534 rt_setmetrics(rtm->rtm_inits,
535 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
536 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
537 RT_REMREF(saved_nrt);
538 RT_UNLOCK(saved_nrt);
544 /* support for new ARP code */
545 if (info.rti_info[RTAX_GATEWAY] &&
546 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
547 (rtm->rtm_flags & RTF_LLDATA) != 0) {
548 error = lla_rt_output(rtm, &info);
551 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
563 rnh = rt_tables_get_rnh(so->so_fibnum,
564 info.rti_info[RTAX_DST]->sa_family);
566 senderr(EAFNOSUPPORT);
567 RADIX_NODE_HEAD_RLOCK(rnh);
568 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
569 info.rti_info[RTAX_NETMASK], rnh);
570 if (rt == NULL) { /* XXX looks bogus */
571 RADIX_NODE_HEAD_RUNLOCK(rnh);
576 * for RTM_CHANGE/LOCK, if we got multipath routes,
577 * we require users to specify a matching RTAX_GATEWAY.
579 * for RTM_GET, gate is optional even with multipath.
580 * if gate == NULL the first match is returned.
581 * (no need to call rt_mpath_matchgate if gate == NULL)
583 if (rn_mpath_capable(rnh) &&
584 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
585 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
587 RADIX_NODE_HEAD_RUNLOCK(rnh);
594 RADIX_NODE_HEAD_RUNLOCK(rnh);
599 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
600 * returns a perfect match in case a netmask is
601 * specified. For host routes only a longest prefix
602 * match is returned so it is necessary to compare the
603 * existence of the netmask. If both have a netmask
604 * rnh_lookup() did a perfect match and if none of them
605 * have a netmask both are host routes which is also a
609 if (rtm->rtm_type != RTM_GET &&
610 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
615 switch(rtm->rtm_type) {
620 if ((rt->rt_flags & RTF_HOST) == 0
621 ? jailed(curthread->td_ucred)
622 : prison_if(curthread->td_ucred,
627 info.rti_info[RTAX_DST] = rt_key(rt);
628 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
629 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
630 info.rti_info[RTAX_GENMASK] = 0;
631 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
634 info.rti_info[RTAX_IFP] =
635 ifp->if_addr->ifa_addr;
636 error = rtm_get_jailed(&info, ifp, rt,
637 &saun, curthread->td_ucred);
642 if (ifp->if_flags & IFF_POINTOPOINT)
643 info.rti_info[RTAX_BRD] =
644 rt->rt_ifa->ifa_dstaddr;
645 rtm->rtm_index = ifp->if_index;
647 info.rti_info[RTAX_IFP] = NULL;
648 info.rti_info[RTAX_IFA] = NULL;
650 } else if ((ifp = rt->rt_ifp) != NULL) {
651 rtm->rtm_index = ifp->if_index;
653 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
654 if (len > rtm->rtm_msglen) {
655 struct rt_msghdr *new_rtm;
656 R_Malloc(new_rtm, struct rt_msghdr *, len);
657 if (new_rtm == NULL) {
661 bcopy(rtm, new_rtm, rtm->rtm_msglen);
662 Free(rtm); rtm = new_rtm;
664 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
665 rtm->rtm_flags = rt->rt_flags;
666 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
667 rtm->rtm_addrs = info.rti_addrs;
672 * New gateway could require new ifaddr, ifp;
673 * flags may also be different; ifp may be specified
674 * by ll sockaddr when protocol address is ambiguous
676 if (((rt->rt_flags & RTF_GATEWAY) &&
677 info.rti_info[RTAX_GATEWAY] != NULL) ||
678 info.rti_info[RTAX_IFP] != NULL ||
679 (info.rti_info[RTAX_IFA] != NULL &&
680 !sa_equal(info.rti_info[RTAX_IFA],
681 rt->rt_ifa->ifa_addr))) {
683 RADIX_NODE_HEAD_LOCK(rnh);
684 error = rt_getifa_fib(&info, rt->rt_fibnum);
686 * XXXRW: Really we should release this
687 * reference later, but this maintains
688 * historical behavior.
690 if (info.rti_ifa != NULL)
691 ifa_free(info.rti_ifa);
692 RADIX_NODE_HEAD_UNLOCK(rnh);
697 if (info.rti_ifa != NULL &&
698 info.rti_ifa != rt->rt_ifa &&
699 rt->rt_ifa != NULL &&
700 rt->rt_ifa->ifa_rtrequest != NULL) {
701 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
703 ifa_free(rt->rt_ifa);
705 if (info.rti_info[RTAX_GATEWAY] != NULL) {
707 RADIX_NODE_HEAD_LOCK(rnh);
710 error = rt_setgate(rt, rt_key(rt),
711 info.rti_info[RTAX_GATEWAY]);
712 RADIX_NODE_HEAD_UNLOCK(rnh);
717 rt->rt_flags |= RTF_GATEWAY;
719 if (info.rti_ifa != NULL &&
720 info.rti_ifa != rt->rt_ifa) {
721 ifa_ref(info.rti_ifa);
722 rt->rt_ifa = info.rti_ifa;
723 rt->rt_ifp = info.rti_ifp;
725 /* Allow some flags to be toggled on change. */
726 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
727 (rtm->rtm_flags & RTF_FMASK);
728 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
730 rtm->rtm_index = rt->rt_ifp->if_index;
731 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
732 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
735 /* We don't support locks anymore */
748 rtm->rtm_errno = error;
750 rtm->rtm_flags |= RTF_DONE;
752 if (rt) /* XXX can this be true? */
755 struct rawcb *rp = NULL;
757 * Check to see if we don't want our own messages.
759 if ((so->so_options & SO_USELOOPBACK) == 0) {
760 if (route_cb.any_count <= 1) {
766 /* There is another listener, so construct message */
770 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
771 if (m->m_pkthdr.len < rtm->rtm_msglen) {
774 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
775 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
781 * XXX insure we don't get a copy by
782 * invalidating our protocol
784 unsigned short family = rp->rcb_proto.sp_family;
785 rp->rcb_proto.sp_family = 0;
786 rt_dispatch(m, info.rti_info[RTAX_DST]);
787 rp->rcb_proto.sp_family = family;
789 rt_dispatch(m, info.rti_info[RTAX_DST]);
797 rt_setmetrics(u_long which, const struct rt_metrics *in,
798 struct rt_metrics_lite *out)
800 #define metric(f, e) if (which & (f)) out->e = in->e;
802 * Only these are stored in the routing entry since introduction
803 * of tcp hostcache. The rest is ignored.
805 metric(RTV_MTU, rmx_mtu);
806 metric(RTV_WEIGHT, rmx_weight);
807 /* Userland -> kernel timebase conversion. */
808 if (which & RTV_EXPIRE)
809 out->rmx_expire = in->rmx_expire ?
810 in->rmx_expire - time_second + time_uptime : 0;
815 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
817 #define metric(e) out->e = in->e;
818 bzero(out, sizeof(*out));
821 /* Kernel -> userland timebase conversion. */
822 out->rmx_expire = in->rmx_expire ?
823 in->rmx_expire - time_uptime + time_second : 0;
828 * Extract the addresses of the passed sockaddrs.
829 * Do a little sanity checking so as to avoid bad memory references.
830 * This data is derived straight from userland.
833 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
838 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
839 if ((rtinfo->rti_addrs & (1 << i)) == 0)
841 sa = (struct sockaddr *)cp;
845 if (cp + sa->sa_len > cplim)
848 * there are no more.. quit now
849 * If there are more bits, they are in error.
850 * I've seen this. route(1) can evidently generate these.
851 * This causes kernel to core dump.
852 * for compatibility, If we see this, point to a safe address.
854 if (sa->sa_len == 0) {
855 rtinfo->rti_info[i] = &sa_zero;
856 return (0); /* should be EINVAL but for compat */
859 rtinfo->rti_info[i] = sa;
866 rt_msg1(int type, struct rt_addrinfo *rtinfo)
868 struct rt_msghdr *rtm;
878 len = sizeof(struct ifa_msghdr);
883 len = sizeof(struct ifma_msghdr);
887 len = sizeof(struct if_msghdr);
892 len = sizeof(struct if_announcemsghdr);
896 len = sizeof(struct rt_msghdr);
900 m = m_gethdr(M_DONTWAIT, MT_DATA);
901 if (m && len > MHLEN) {
902 MCLGET(m, M_DONTWAIT);
903 if ((m->m_flags & M_EXT) == 0) {
910 m->m_pkthdr.len = m->m_len = len;
911 m->m_pkthdr.rcvif = NULL;
912 rtm = mtod(m, struct rt_msghdr *);
913 bzero((caddr_t)rtm, len);
914 for (i = 0; i < RTAX_MAX; i++) {
915 if ((sa = rtinfo->rti_info[i]) == NULL)
917 rtinfo->rti_addrs |= (1 << i);
919 m_copyback(m, len, dlen, (caddr_t)sa);
922 if (m->m_pkthdr.len != len) {
926 rtm->rtm_msglen = len;
927 rtm->rtm_version = RTM_VERSION;
928 rtm->rtm_type = type;
933 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
936 int len, dlen, second_time = 0;
939 rtinfo->rti_addrs = 0;
945 len = sizeof(struct ifa_msghdr);
949 len = sizeof(struct if_msghdr);
953 len = sizeof(struct ifma_msghdr);
957 len = sizeof(struct rt_msghdr);
962 for (i = 0; i < RTAX_MAX; i++) {
965 if ((sa = rtinfo->rti_info[i]) == NULL)
967 rtinfo->rti_addrs |= (1 << i);
970 bcopy((caddr_t)sa, cp, (unsigned)dlen);
976 if (cp == NULL && w != NULL && !second_time) {
977 struct walkarg *rw = w;
980 if (rw->w_tmemsize < len) {
982 free(rw->w_tmem, M_RTABLE);
983 rw->w_tmem = (caddr_t)
984 malloc(len, M_RTABLE, M_NOWAIT);
986 rw->w_tmemsize = len;
996 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
998 rtm->rtm_version = RTM_VERSION;
999 rtm->rtm_type = type;
1000 rtm->rtm_msglen = len;
1006 * This routine is called to generate a message from the routing
1007 * socket indicating that a redirect has occured, a routing lookup
1008 * has failed, or that a protocol has detected timeouts to a particular
1012 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1014 struct rt_msghdr *rtm;
1016 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1018 if (route_cb.any_count == 0)
1020 m = rt_msg1(type, rtinfo);
1023 rtm = mtod(m, struct rt_msghdr *);
1024 rtm->rtm_flags = RTF_DONE | flags;
1025 rtm->rtm_errno = error;
1026 rtm->rtm_addrs = rtinfo->rti_addrs;
1031 * This routine is called to generate a message from the routing
1032 * socket indicating that the status of a network interface has changed.
1035 rt_ifmsg(struct ifnet *ifp)
1037 struct if_msghdr *ifm;
1039 struct rt_addrinfo info;
1041 if (route_cb.any_count == 0)
1043 bzero((caddr_t)&info, sizeof(info));
1044 m = rt_msg1(RTM_IFINFO, &info);
1047 ifm = mtod(m, struct if_msghdr *);
1048 ifm->ifm_index = ifp->if_index;
1049 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1050 ifm->ifm_data = ifp->if_data;
1052 rt_dispatch(m, NULL);
1056 * This is called to generate messages from the routing socket
1057 * indicating a network interface has had addresses associated with it.
1058 * if we ever reverse the logic and replace messages TO the routing
1059 * socket indicate a request to configure interfaces, then it will
1060 * be unnecessary as the routing socket will automatically generate
1064 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1066 struct rt_addrinfo info;
1067 struct sockaddr *sa = NULL;
1069 struct mbuf *m = NULL;
1070 struct ifnet *ifp = ifa->ifa_ifp;
1072 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1073 ("unexpected cmd %u", cmd));
1074 #if defined(INET) || defined(INET6)
1077 * notify the SCTP stack
1078 * this will only get called when an address is added/deleted
1079 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1081 sctp_addr_change(ifa, cmd);
1084 if (route_cb.any_count == 0)
1086 for (pass = 1; pass < 3; pass++) {
1087 bzero((caddr_t)&info, sizeof(info));
1088 if ((cmd == RTM_ADD && pass == 1) ||
1089 (cmd == RTM_DELETE && pass == 2)) {
1090 struct ifa_msghdr *ifam;
1091 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1093 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1094 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1095 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1096 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1097 if ((m = rt_msg1(ncmd, &info)) == NULL)
1099 ifam = mtod(m, struct ifa_msghdr *);
1100 ifam->ifam_index = ifp->if_index;
1101 ifam->ifam_metric = ifa->ifa_metric;
1102 ifam->ifam_flags = ifa->ifa_flags;
1103 ifam->ifam_addrs = info.rti_addrs;
1105 if ((cmd == RTM_ADD && pass == 2) ||
1106 (cmd == RTM_DELETE && pass == 1)) {
1107 struct rt_msghdr *rtm;
1111 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1112 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1113 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1114 if ((m = rt_msg1(cmd, &info)) == NULL)
1116 rtm = mtod(m, struct rt_msghdr *);
1117 rtm->rtm_index = ifp->if_index;
1118 rtm->rtm_flags |= rt->rt_flags;
1119 rtm->rtm_errno = error;
1120 rtm->rtm_addrs = info.rti_addrs;
1127 * This is the analogue to the rt_newaddrmsg which performs the same
1128 * function but for multicast group memberhips. This is easier since
1129 * there is no route state to worry about.
1132 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1134 struct rt_addrinfo info;
1135 struct mbuf *m = NULL;
1136 struct ifnet *ifp = ifma->ifma_ifp;
1137 struct ifma_msghdr *ifmam;
1139 if (route_cb.any_count == 0)
1142 bzero((caddr_t)&info, sizeof(info));
1143 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1144 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1146 * If a link-layer address is present, present it as a ``gateway''
1147 * (similarly to how ARP entries, e.g., are presented).
1149 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1150 m = rt_msg1(cmd, &info);
1153 ifmam = mtod(m, struct ifma_msghdr *);
1154 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1156 ifmam->ifmam_index = ifp->if_index;
1157 ifmam->ifmam_addrs = info.rti_addrs;
1158 rt_dispatch(m, ifma->ifma_addr);
1161 static struct mbuf *
1162 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1163 struct rt_addrinfo *info)
1165 struct if_announcemsghdr *ifan;
1168 if (route_cb.any_count == 0)
1170 bzero((caddr_t)info, sizeof(*info));
1171 m = rt_msg1(type, info);
1173 ifan = mtod(m, struct if_announcemsghdr *);
1174 ifan->ifan_index = ifp->if_index;
1175 strlcpy(ifan->ifan_name, ifp->if_xname,
1176 sizeof(ifan->ifan_name));
1177 ifan->ifan_what = what;
1183 * This is called to generate routing socket messages indicating
1184 * IEEE80211 wireless events.
1185 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1188 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1191 struct rt_addrinfo info;
1193 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1196 * Append the ieee80211 data. Try to stick it in the
1197 * mbuf containing the ifannounce msg; otherwise allocate
1198 * a new mbuf and append.
1200 * NB: we assume m is a single mbuf.
1202 if (data_len > M_TRAILINGSPACE(m)) {
1203 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1208 bcopy(data, mtod(n, void *), data_len);
1209 n->m_len = data_len;
1211 } else if (data_len > 0) {
1212 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1213 m->m_len += data_len;
1215 if (m->m_flags & M_PKTHDR)
1216 m->m_pkthdr.len += data_len;
1217 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1218 rt_dispatch(m, NULL);
1223 * This is called to generate routing socket messages indicating
1224 * network interface arrival and departure.
1227 rt_ifannouncemsg(struct ifnet *ifp, int what)
1230 struct rt_addrinfo info;
1232 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1234 rt_dispatch(m, NULL);
1238 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1243 * Preserve the family from the sockaddr, if any, in an m_tag for
1244 * use when injecting the mbuf into the routing socket buffer from
1248 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1254 *(unsigned short *)(tag + 1) = sa->sa_family;
1255 m_tag_prepend(m, tag);
1259 m->m_pkthdr.rcvif = V_loif;
1265 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1269 * This is used in dumping the kernel table via sysctl().
1272 sysctl_dumpentry(struct radix_node *rn, void *vw)
1274 struct walkarg *w = vw;
1275 struct rtentry *rt = (struct rtentry *)rn;
1276 int error = 0, size;
1277 struct rt_addrinfo info;
1279 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1281 if ((rt->rt_flags & RTF_HOST) == 0
1282 ? jailed(w->w_req->td->td_ucred)
1283 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1285 bzero((caddr_t)&info, sizeof(info));
1286 info.rti_info[RTAX_DST] = rt_key(rt);
1287 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1288 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1289 info.rti_info[RTAX_GENMASK] = 0;
1291 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1292 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1293 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1294 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1296 size = rt_msg2(RTM_GET, &info, NULL, w);
1297 if (w->w_req && w->w_tmem) {
1298 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1300 rtm->rtm_flags = rt->rt_flags;
1302 * let's be honest about this being a retarded hack
1304 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1305 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1306 rtm->rtm_index = rt->rt_ifp->if_index;
1307 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1308 rtm->rtm_addrs = info.rti_addrs;
1309 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1316 sysctl_iflist(int af, struct walkarg *w)
1320 struct rt_addrinfo info;
1323 bzero((caddr_t)&info, sizeof(info));
1325 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1326 if (w->w_arg && w->w_arg != ifp->if_index)
1329 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1330 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1331 info.rti_info[RTAX_IFP] = NULL;
1332 if (w->w_req && w->w_tmem) {
1333 struct if_msghdr *ifm;
1335 ifm = (struct if_msghdr *)w->w_tmem;
1336 ifm->ifm_index = ifp->if_index;
1337 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1338 ifm->ifm_data = ifp->if_data;
1339 ifm->ifm_addrs = info.rti_addrs;
1340 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1344 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1345 if (af && af != ifa->ifa_addr->sa_family)
1347 if (prison_if(w->w_req->td->td_ucred,
1348 ifa->ifa_addr) != 0)
1350 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1351 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1352 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1353 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1354 if (w->w_req && w->w_tmem) {
1355 struct ifa_msghdr *ifam;
1357 ifam = (struct ifa_msghdr *)w->w_tmem;
1358 ifam->ifam_index = ifa->ifa_ifp->if_index;
1359 ifam->ifam_flags = ifa->ifa_flags;
1360 ifam->ifam_metric = ifa->ifa_metric;
1361 ifam->ifam_addrs = info.rti_addrs;
1362 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1367 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1368 info.rti_info[RTAX_BRD] = NULL;
1376 sysctl_ifmalist(int af, struct walkarg *w)
1379 struct ifmultiaddr *ifma;
1380 struct rt_addrinfo info;
1384 bzero((caddr_t)&info, sizeof(info));
1386 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1387 if (w->w_arg && w->w_arg != ifp->if_index)
1390 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1392 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1393 if (af && af != ifma->ifma_addr->sa_family)
1395 if (prison_if(w->w_req->td->td_ucred,
1396 ifma->ifma_addr) != 0)
1398 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1399 info.rti_info[RTAX_GATEWAY] =
1400 (ifma->ifma_addr->sa_family != AF_LINK) ?
1401 ifma->ifma_lladdr : NULL;
1402 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1403 if (w->w_req && w->w_tmem) {
1404 struct ifma_msghdr *ifmam;
1406 ifmam = (struct ifma_msghdr *)w->w_tmem;
1407 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1408 ifmam->ifmam_flags = 0;
1409 ifmam->ifmam_addrs = info.rti_addrs;
1410 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1412 IF_ADDR_UNLOCK(ifp);
1417 IF_ADDR_UNLOCK(ifp);
1425 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1427 int *name = (int *)arg1;
1428 u_int namelen = arg2;
1429 struct radix_node_head *rnh = NULL; /* silence compiler. */
1430 int i, lim, error = EINVAL;
1439 return ((namelen < 3) ? EISDIR : ENOTDIR);
1443 bzero(&w, sizeof(w));
1448 error = sysctl_wire_old_buffer(req, 0);
1455 if (af == 0) { /* dump all tables */
1458 } else /* dump only one table */
1462 * take care of llinfo entries, the caller must
1465 if (w.w_op == NET_RT_FLAGS &&
1466 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1468 error = lltable_sysctl_dumparp(af, w.w_req);
1474 * take care of routing entries
1476 for (error = 0; error == 0 && i <= lim; i++)
1477 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1479 RADIX_NODE_HEAD_LOCK(rnh);
1480 error = rnh->rnh_walktree(rnh,
1481 sysctl_dumpentry, &w);
1482 RADIX_NODE_HEAD_UNLOCK(rnh);
1484 error = EAFNOSUPPORT;
1488 error = sysctl_iflist(af, &w);
1491 case NET_RT_IFMALIST:
1492 error = sysctl_ifmalist(af, &w);
1496 free(w.w_tmem, M_RTABLE);
1500 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1503 * Definitions of protocols supported in the ROUTE domain.
1506 static struct domain routedomain; /* or at least forward */
1508 static struct protosw routesw[] = {
1510 .pr_type = SOCK_RAW,
1511 .pr_domain = &routedomain,
1512 .pr_flags = PR_ATOMIC|PR_ADDR,
1513 .pr_output = route_output,
1514 .pr_ctlinput = raw_ctlinput,
1515 .pr_init = raw_init,
1516 .pr_usrreqs = &route_usrreqs
1520 static struct domain routedomain = {
1521 .dom_family = PF_ROUTE,
1522 .dom_name = "route",
1523 .dom_protosw = routesw,
1524 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1527 VNET_DOMAIN_SET(route);