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);
517 * The given gateway address may be an interface address.
518 * For example, issuing a "route change" command on a route
519 * entry that was created from a tunnel, and the gateway
520 * address given is the local end point. In this case the
521 * RTF_GATEWAY flag must be cleared or the destination will
522 * not be reachable even though there is no error message.
524 if (info.rti_info[RTAX_GATEWAY] != NULL &&
525 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
528 bzero(&gw_ro, sizeof(gw_ro));
529 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
530 rtalloc_ign(&gw_ro, 0);
532 * A host route through the loopback interface is
533 * installed for each interface adddress. In pre 8.0
534 * releases the interface address of a PPP link type
535 * is not reachable locally. This behavior is fixed as
536 * part of the new L2/L3 redesign and rewrite work. The
537 * signature of this interface address route is the
538 * AF_LINK sa_family type of the rt_gateway, and the
539 * rt_ifp has the IFF_LOOPBACK flag set.
541 if (gw_ro.ro_rt != NULL &&
542 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
543 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
544 info.rti_flags &= ~RTF_GATEWAY;
545 if (gw_ro.ro_rt != NULL)
549 switch (rtm->rtm_type) {
550 struct rtentry *saved_nrt;
553 if (info.rti_info[RTAX_GATEWAY] == NULL)
557 /* support for new ARP code */
558 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
559 (rtm->rtm_flags & RTF_LLDATA) != 0) {
560 error = lla_rt_output(rtm, &info);
563 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
565 if (error == 0 && saved_nrt) {
567 rt_setmetrics(rtm->rtm_inits,
568 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
569 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
570 RT_REMREF(saved_nrt);
571 RT_UNLOCK(saved_nrt);
577 /* support for new ARP code */
578 if (info.rti_info[RTAX_GATEWAY] &&
579 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
580 (rtm->rtm_flags & RTF_LLDATA) != 0) {
581 error = lla_rt_output(rtm, &info);
584 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
596 rnh = rt_tables_get_rnh(so->so_fibnum,
597 info.rti_info[RTAX_DST]->sa_family);
599 senderr(EAFNOSUPPORT);
600 RADIX_NODE_HEAD_RLOCK(rnh);
601 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
602 info.rti_info[RTAX_NETMASK], rnh);
603 if (rt == NULL) { /* XXX looks bogus */
604 RADIX_NODE_HEAD_RUNLOCK(rnh);
609 * for RTM_CHANGE/LOCK, if we got multipath routes,
610 * we require users to specify a matching RTAX_GATEWAY.
612 * for RTM_GET, gate is optional even with multipath.
613 * if gate == NULL the first match is returned.
614 * (no need to call rt_mpath_matchgate if gate == NULL)
616 if (rn_mpath_capable(rnh) &&
617 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
618 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
620 RADIX_NODE_HEAD_RUNLOCK(rnh);
627 RADIX_NODE_HEAD_RUNLOCK(rnh);
632 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
633 * returns a perfect match in case a netmask is
634 * specified. For host routes only a longest prefix
635 * match is returned so it is necessary to compare the
636 * existence of the netmask. If both have a netmask
637 * rnh_lookup() did a perfect match and if none of them
638 * have a netmask both are host routes which is also a
642 if (rtm->rtm_type != RTM_GET &&
643 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
648 switch(rtm->rtm_type) {
653 if ((rt->rt_flags & RTF_HOST) == 0
654 ? jailed(curthread->td_ucred)
655 : prison_if(curthread->td_ucred,
660 info.rti_info[RTAX_DST] = rt_key(rt);
661 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
662 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
663 info.rti_info[RTAX_GENMASK] = 0;
664 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
667 info.rti_info[RTAX_IFP] =
668 ifp->if_addr->ifa_addr;
669 error = rtm_get_jailed(&info, ifp, rt,
670 &saun, curthread->td_ucred);
675 if (ifp->if_flags & IFF_POINTOPOINT)
676 info.rti_info[RTAX_BRD] =
677 rt->rt_ifa->ifa_dstaddr;
678 rtm->rtm_index = ifp->if_index;
680 info.rti_info[RTAX_IFP] = NULL;
681 info.rti_info[RTAX_IFA] = NULL;
683 } else if ((ifp = rt->rt_ifp) != NULL) {
684 rtm->rtm_index = ifp->if_index;
686 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
687 if (len > rtm->rtm_msglen) {
688 struct rt_msghdr *new_rtm;
689 R_Malloc(new_rtm, struct rt_msghdr *, len);
690 if (new_rtm == NULL) {
694 bcopy(rtm, new_rtm, rtm->rtm_msglen);
695 Free(rtm); rtm = new_rtm;
697 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
698 rtm->rtm_flags = rt->rt_flags;
699 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
700 rtm->rtm_addrs = info.rti_addrs;
705 * New gateway could require new ifaddr, ifp;
706 * flags may also be different; ifp may be specified
707 * by ll sockaddr when protocol address is ambiguous
709 if (((rt->rt_flags & RTF_GATEWAY) &&
710 info.rti_info[RTAX_GATEWAY] != NULL) ||
711 info.rti_info[RTAX_IFP] != NULL ||
712 (info.rti_info[RTAX_IFA] != NULL &&
713 !sa_equal(info.rti_info[RTAX_IFA],
714 rt->rt_ifa->ifa_addr))) {
716 RADIX_NODE_HEAD_LOCK(rnh);
717 error = rt_getifa_fib(&info, rt->rt_fibnum);
719 * XXXRW: Really we should release this
720 * reference later, but this maintains
721 * historical behavior.
723 if (info.rti_ifa != NULL)
724 ifa_free(info.rti_ifa);
725 RADIX_NODE_HEAD_UNLOCK(rnh);
730 if (info.rti_ifa != NULL &&
731 info.rti_ifa != rt->rt_ifa &&
732 rt->rt_ifa != NULL &&
733 rt->rt_ifa->ifa_rtrequest != NULL) {
734 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
736 ifa_free(rt->rt_ifa);
738 if (info.rti_info[RTAX_GATEWAY] != NULL) {
740 RADIX_NODE_HEAD_LOCK(rnh);
743 error = rt_setgate(rt, rt_key(rt),
744 info.rti_info[RTAX_GATEWAY]);
745 RADIX_NODE_HEAD_UNLOCK(rnh);
750 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
752 if (info.rti_ifa != NULL &&
753 info.rti_ifa != rt->rt_ifa) {
754 ifa_ref(info.rti_ifa);
755 rt->rt_ifa = info.rti_ifa;
756 rt->rt_ifp = info.rti_ifp;
758 /* Allow some flags to be toggled on change. */
759 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
760 (rtm->rtm_flags & RTF_FMASK);
761 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
763 rtm->rtm_index = rt->rt_ifp->if_index;
764 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
765 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
768 /* We don't support locks anymore */
781 rtm->rtm_errno = error;
783 rtm->rtm_flags |= RTF_DONE;
785 if (rt) /* XXX can this be true? */
788 struct rawcb *rp = NULL;
790 * Check to see if we don't want our own messages.
792 if ((so->so_options & SO_USELOOPBACK) == 0) {
793 if (route_cb.any_count <= 1) {
799 /* There is another listener, so construct message */
803 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
804 if (m->m_pkthdr.len < rtm->rtm_msglen) {
807 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
808 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
814 * XXX insure we don't get a copy by
815 * invalidating our protocol
817 unsigned short family = rp->rcb_proto.sp_family;
818 rp->rcb_proto.sp_family = 0;
819 rt_dispatch(m, info.rti_info[RTAX_DST]);
820 rp->rcb_proto.sp_family = family;
822 rt_dispatch(m, info.rti_info[RTAX_DST]);
830 rt_setmetrics(u_long which, const struct rt_metrics *in,
831 struct rt_metrics_lite *out)
833 #define metric(f, e) if (which & (f)) out->e = in->e;
835 * Only these are stored in the routing entry since introduction
836 * of tcp hostcache. The rest is ignored.
838 metric(RTV_MTU, rmx_mtu);
839 metric(RTV_WEIGHT, rmx_weight);
840 /* Userland -> kernel timebase conversion. */
841 if (which & RTV_EXPIRE)
842 out->rmx_expire = in->rmx_expire ?
843 in->rmx_expire - time_second + time_uptime : 0;
848 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
850 #define metric(e) out->e = in->e;
851 bzero(out, sizeof(*out));
854 /* Kernel -> userland timebase conversion. */
855 out->rmx_expire = in->rmx_expire ?
856 in->rmx_expire - time_uptime + time_second : 0;
861 * Extract the addresses of the passed sockaddrs.
862 * Do a little sanity checking so as to avoid bad memory references.
863 * This data is derived straight from userland.
866 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
871 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
872 if ((rtinfo->rti_addrs & (1 << i)) == 0)
874 sa = (struct sockaddr *)cp;
878 if (cp + sa->sa_len > cplim)
881 * there are no more.. quit now
882 * If there are more bits, they are in error.
883 * I've seen this. route(1) can evidently generate these.
884 * This causes kernel to core dump.
885 * for compatibility, If we see this, point to a safe address.
887 if (sa->sa_len == 0) {
888 rtinfo->rti_info[i] = &sa_zero;
889 return (0); /* should be EINVAL but for compat */
892 rtinfo->rti_info[i] = sa;
899 rt_msg1(int type, struct rt_addrinfo *rtinfo)
901 struct rt_msghdr *rtm;
911 len = sizeof(struct ifa_msghdr);
916 len = sizeof(struct ifma_msghdr);
920 len = sizeof(struct if_msghdr);
925 len = sizeof(struct if_announcemsghdr);
929 len = sizeof(struct rt_msghdr);
933 m = m_gethdr(M_DONTWAIT, MT_DATA);
934 if (m && len > MHLEN) {
935 MCLGET(m, M_DONTWAIT);
936 if ((m->m_flags & M_EXT) == 0) {
943 m->m_pkthdr.len = m->m_len = len;
944 m->m_pkthdr.rcvif = NULL;
945 rtm = mtod(m, struct rt_msghdr *);
946 bzero((caddr_t)rtm, len);
947 for (i = 0; i < RTAX_MAX; i++) {
948 if ((sa = rtinfo->rti_info[i]) == NULL)
950 rtinfo->rti_addrs |= (1 << i);
952 m_copyback(m, len, dlen, (caddr_t)sa);
955 if (m->m_pkthdr.len != len) {
959 rtm->rtm_msglen = len;
960 rtm->rtm_version = RTM_VERSION;
961 rtm->rtm_type = type;
966 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
969 int len, dlen, second_time = 0;
972 rtinfo->rti_addrs = 0;
978 len = sizeof(struct ifa_msghdr);
982 len = sizeof(struct if_msghdr);
986 len = sizeof(struct ifma_msghdr);
990 len = sizeof(struct rt_msghdr);
995 for (i = 0; i < RTAX_MAX; i++) {
998 if ((sa = rtinfo->rti_info[i]) == NULL)
1000 rtinfo->rti_addrs |= (1 << i);
1003 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1009 if (cp == NULL && w != NULL && !second_time) {
1010 struct walkarg *rw = w;
1013 if (rw->w_tmemsize < len) {
1015 free(rw->w_tmem, M_RTABLE);
1016 rw->w_tmem = (caddr_t)
1017 malloc(len, M_RTABLE, M_NOWAIT);
1019 rw->w_tmemsize = len;
1029 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1031 rtm->rtm_version = RTM_VERSION;
1032 rtm->rtm_type = type;
1033 rtm->rtm_msglen = len;
1039 * This routine is called to generate a message from the routing
1040 * socket indicating that a redirect has occured, a routing lookup
1041 * has failed, or that a protocol has detected timeouts to a particular
1045 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1047 struct rt_msghdr *rtm;
1049 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1051 if (route_cb.any_count == 0)
1053 m = rt_msg1(type, rtinfo);
1056 rtm = mtod(m, struct rt_msghdr *);
1057 rtm->rtm_flags = RTF_DONE | flags;
1058 rtm->rtm_errno = error;
1059 rtm->rtm_addrs = rtinfo->rti_addrs;
1064 * This routine is called to generate a message from the routing
1065 * socket indicating that the status of a network interface has changed.
1068 rt_ifmsg(struct ifnet *ifp)
1070 struct if_msghdr *ifm;
1072 struct rt_addrinfo info;
1074 if (route_cb.any_count == 0)
1076 bzero((caddr_t)&info, sizeof(info));
1077 m = rt_msg1(RTM_IFINFO, &info);
1080 ifm = mtod(m, struct if_msghdr *);
1081 ifm->ifm_index = ifp->if_index;
1082 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1083 ifm->ifm_data = ifp->if_data;
1085 rt_dispatch(m, NULL);
1089 * This is called to generate messages from the routing socket
1090 * indicating a network interface has had addresses associated with it.
1091 * if we ever reverse the logic and replace messages TO the routing
1092 * socket indicate a request to configure interfaces, then it will
1093 * be unnecessary as the routing socket will automatically generate
1097 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1099 struct rt_addrinfo info;
1100 struct sockaddr *sa = NULL;
1102 struct mbuf *m = NULL;
1103 struct ifnet *ifp = ifa->ifa_ifp;
1105 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1106 ("unexpected cmd %u", cmd));
1107 #if defined(INET) || defined(INET6)
1110 * notify the SCTP stack
1111 * this will only get called when an address is added/deleted
1112 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1114 sctp_addr_change(ifa, cmd);
1117 if (route_cb.any_count == 0)
1119 for (pass = 1; pass < 3; pass++) {
1120 bzero((caddr_t)&info, sizeof(info));
1121 if ((cmd == RTM_ADD && pass == 1) ||
1122 (cmd == RTM_DELETE && pass == 2)) {
1123 struct ifa_msghdr *ifam;
1124 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1126 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1127 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1128 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1129 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1130 if ((m = rt_msg1(ncmd, &info)) == NULL)
1132 ifam = mtod(m, struct ifa_msghdr *);
1133 ifam->ifam_index = ifp->if_index;
1134 ifam->ifam_metric = ifa->ifa_metric;
1135 ifam->ifam_flags = ifa->ifa_flags;
1136 ifam->ifam_addrs = info.rti_addrs;
1138 if ((cmd == RTM_ADD && pass == 2) ||
1139 (cmd == RTM_DELETE && pass == 1)) {
1140 struct rt_msghdr *rtm;
1144 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1145 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1146 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1147 if ((m = rt_msg1(cmd, &info)) == NULL)
1149 rtm = mtod(m, struct rt_msghdr *);
1150 rtm->rtm_index = ifp->if_index;
1151 rtm->rtm_flags |= rt->rt_flags;
1152 rtm->rtm_errno = error;
1153 rtm->rtm_addrs = info.rti_addrs;
1160 * This is the analogue to the rt_newaddrmsg which performs the same
1161 * function but for multicast group memberhips. This is easier since
1162 * there is no route state to worry about.
1165 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1167 struct rt_addrinfo info;
1168 struct mbuf *m = NULL;
1169 struct ifnet *ifp = ifma->ifma_ifp;
1170 struct ifma_msghdr *ifmam;
1172 if (route_cb.any_count == 0)
1175 bzero((caddr_t)&info, sizeof(info));
1176 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1177 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1179 * If a link-layer address is present, present it as a ``gateway''
1180 * (similarly to how ARP entries, e.g., are presented).
1182 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1183 m = rt_msg1(cmd, &info);
1186 ifmam = mtod(m, struct ifma_msghdr *);
1187 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1189 ifmam->ifmam_index = ifp->if_index;
1190 ifmam->ifmam_addrs = info.rti_addrs;
1191 rt_dispatch(m, ifma->ifma_addr);
1194 static struct mbuf *
1195 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1196 struct rt_addrinfo *info)
1198 struct if_announcemsghdr *ifan;
1201 if (route_cb.any_count == 0)
1203 bzero((caddr_t)info, sizeof(*info));
1204 m = rt_msg1(type, info);
1206 ifan = mtod(m, struct if_announcemsghdr *);
1207 ifan->ifan_index = ifp->if_index;
1208 strlcpy(ifan->ifan_name, ifp->if_xname,
1209 sizeof(ifan->ifan_name));
1210 ifan->ifan_what = what;
1216 * This is called to generate routing socket messages indicating
1217 * IEEE80211 wireless events.
1218 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1221 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1224 struct rt_addrinfo info;
1226 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1229 * Append the ieee80211 data. Try to stick it in the
1230 * mbuf containing the ifannounce msg; otherwise allocate
1231 * a new mbuf and append.
1233 * NB: we assume m is a single mbuf.
1235 if (data_len > M_TRAILINGSPACE(m)) {
1236 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1241 bcopy(data, mtod(n, void *), data_len);
1242 n->m_len = data_len;
1244 } else if (data_len > 0) {
1245 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1246 m->m_len += data_len;
1248 if (m->m_flags & M_PKTHDR)
1249 m->m_pkthdr.len += data_len;
1250 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1251 rt_dispatch(m, NULL);
1256 * This is called to generate routing socket messages indicating
1257 * network interface arrival and departure.
1260 rt_ifannouncemsg(struct ifnet *ifp, int what)
1263 struct rt_addrinfo info;
1265 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1267 rt_dispatch(m, NULL);
1271 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1276 * Preserve the family from the sockaddr, if any, in an m_tag for
1277 * use when injecting the mbuf into the routing socket buffer from
1281 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1287 *(unsigned short *)(tag + 1) = sa->sa_family;
1288 m_tag_prepend(m, tag);
1292 m->m_pkthdr.rcvif = V_loif;
1298 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1302 * This is used in dumping the kernel table via sysctl().
1305 sysctl_dumpentry(struct radix_node *rn, void *vw)
1307 struct walkarg *w = vw;
1308 struct rtentry *rt = (struct rtentry *)rn;
1309 int error = 0, size;
1310 struct rt_addrinfo info;
1312 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1314 if ((rt->rt_flags & RTF_HOST) == 0
1315 ? jailed(w->w_req->td->td_ucred)
1316 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1318 bzero((caddr_t)&info, sizeof(info));
1319 info.rti_info[RTAX_DST] = rt_key(rt);
1320 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1321 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1322 info.rti_info[RTAX_GENMASK] = 0;
1324 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1325 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1326 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1327 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1329 size = rt_msg2(RTM_GET, &info, NULL, w);
1330 if (w->w_req && w->w_tmem) {
1331 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1333 rtm->rtm_flags = rt->rt_flags;
1335 * let's be honest about this being a retarded hack
1337 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1338 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1339 rtm->rtm_index = rt->rt_ifp->if_index;
1340 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1341 rtm->rtm_addrs = info.rti_addrs;
1342 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1349 sysctl_iflist(int af, struct walkarg *w)
1353 struct rt_addrinfo info;
1356 bzero((caddr_t)&info, sizeof(info));
1358 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1359 if (w->w_arg && w->w_arg != ifp->if_index)
1362 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1363 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1364 info.rti_info[RTAX_IFP] = NULL;
1365 if (w->w_req && w->w_tmem) {
1366 struct if_msghdr *ifm;
1368 ifm = (struct if_msghdr *)w->w_tmem;
1369 ifm->ifm_index = ifp->if_index;
1370 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1371 ifm->ifm_data = ifp->if_data;
1372 ifm->ifm_addrs = info.rti_addrs;
1373 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1377 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1378 if (af && af != ifa->ifa_addr->sa_family)
1380 if (prison_if(w->w_req->td->td_ucred,
1381 ifa->ifa_addr) != 0)
1383 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1384 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1385 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1386 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1387 if (w->w_req && w->w_tmem) {
1388 struct ifa_msghdr *ifam;
1390 ifam = (struct ifa_msghdr *)w->w_tmem;
1391 ifam->ifam_index = ifa->ifa_ifp->if_index;
1392 ifam->ifam_flags = ifa->ifa_flags;
1393 ifam->ifam_metric = ifa->ifa_metric;
1394 ifam->ifam_addrs = info.rti_addrs;
1395 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1400 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1401 info.rti_info[RTAX_BRD] = NULL;
1409 sysctl_ifmalist(int af, struct walkarg *w)
1412 struct ifmultiaddr *ifma;
1413 struct rt_addrinfo info;
1417 bzero((caddr_t)&info, sizeof(info));
1419 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1420 if (w->w_arg && w->w_arg != ifp->if_index)
1423 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1425 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1426 if (af && af != ifma->ifma_addr->sa_family)
1428 if (prison_if(w->w_req->td->td_ucred,
1429 ifma->ifma_addr) != 0)
1431 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1432 info.rti_info[RTAX_GATEWAY] =
1433 (ifma->ifma_addr->sa_family != AF_LINK) ?
1434 ifma->ifma_lladdr : NULL;
1435 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1436 if (w->w_req && w->w_tmem) {
1437 struct ifma_msghdr *ifmam;
1439 ifmam = (struct ifma_msghdr *)w->w_tmem;
1440 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1441 ifmam->ifmam_flags = 0;
1442 ifmam->ifmam_addrs = info.rti_addrs;
1443 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1445 IF_ADDR_UNLOCK(ifp);
1450 IF_ADDR_UNLOCK(ifp);
1458 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1460 int *name = (int *)arg1;
1461 u_int namelen = arg2;
1462 struct radix_node_head *rnh = NULL; /* silence compiler. */
1463 int i, lim, error = EINVAL;
1472 return ((namelen < 3) ? EISDIR : ENOTDIR);
1476 bzero(&w, sizeof(w));
1481 error = sysctl_wire_old_buffer(req, 0);
1488 if (af == 0) { /* dump all tables */
1491 } else /* dump only one table */
1495 * take care of llinfo entries, the caller must
1498 if (w.w_op == NET_RT_FLAGS &&
1499 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1501 error = lltable_sysctl_dumparp(af, w.w_req);
1507 * take care of routing entries
1509 for (error = 0; error == 0 && i <= lim; i++) {
1510 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1512 RADIX_NODE_HEAD_LOCK(rnh);
1513 error = rnh->rnh_walktree(rnh,
1514 sysctl_dumpentry, &w);
1515 RADIX_NODE_HEAD_UNLOCK(rnh);
1517 error = EAFNOSUPPORT;
1522 error = sysctl_iflist(af, &w);
1525 case NET_RT_IFMALIST:
1526 error = sysctl_ifmalist(af, &w);
1530 free(w.w_tmem, M_RTABLE);
1534 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1537 * Definitions of protocols supported in the ROUTE domain.
1540 static struct domain routedomain; /* or at least forward */
1542 static struct protosw routesw[] = {
1544 .pr_type = SOCK_RAW,
1545 .pr_domain = &routedomain,
1546 .pr_flags = PR_ATOMIC|PR_ADDR,
1547 .pr_output = route_output,
1548 .pr_ctlinput = raw_ctlinput,
1549 .pr_init = raw_init,
1550 .pr_usrreqs = &route_usrreqs
1554 static struct domain routedomain = {
1555 .dom_family = PF_ROUTE,
1556 .dom_name = "route",
1557 .dom_protosw = routesw,
1558 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1561 VNET_DOMAIN_SET(route);