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
32 #include "opt_compat.h"
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/if_types.h>
58 #include <net/netisr.h>
59 #include <net/raw_cb.h>
60 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/if_ether.h>
66 #include <netinet6/scope6_var.h>
69 #ifdef COMPAT_FREEBSD32
70 #include <sys/mount.h>
71 #include <compat/freebsd32/freebsd32.h>
78 uint8_t ifi_link_state;
79 uint8_t ifi_spare_char1;
80 uint8_t ifi_spare_char2;
84 uint32_t ifi_baudrate;
85 uint32_t ifi_ipackets;
87 uint32_t ifi_opackets;
89 uint32_t ifi_collisions;
96 uint32_t ifi_hwassist;
98 struct timeval32 ifi_lastchange;
108 struct if_data32 ifm_data;
111 struct if_msghdrl32 {
118 uint16_t _ifm_spare1;
120 uint16_t ifm_data_off;
121 struct if_data32 ifm_data;
124 struct ifa_msghdrl32 {
125 uint16_t ifam_msglen;
126 uint8_t ifam_version;
131 uint16_t _ifam_spare1;
133 uint16_t ifam_data_off;
135 struct if_data32 ifam_data;
137 #endif /* COMPAT_FREEBSD32 */
139 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
141 /* NB: these are not modified */
142 static struct sockaddr route_src = { 2, PF_ROUTE, };
143 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
146 * Used by rtsock/raw_input callback code to decide whether to filter the update
147 * notification to a socket bound to a particular FIB.
149 #define RTS_FILTER_FIB M_PROTO8
152 int ip_count; /* attached w/ AF_INET */
153 int ip6_count; /* attached w/ AF_INET6 */
154 int ipx_count; /* attached w/ AF_IPX */
155 int any_count; /* total attached */
158 struct mtx rtsock_mtx;
159 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
161 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
162 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
163 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
165 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
171 struct sysctl_req *w_req;
174 static void rts_input(struct mbuf *m);
175 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
176 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
177 caddr_t cp, struct walkarg *w);
178 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
179 struct rt_addrinfo *rtinfo);
180 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
181 static int sysctl_iflist(int af, struct walkarg *w);
182 static int sysctl_ifmalist(int af, struct walkarg *w);
183 static int route_output(struct mbuf *m, struct socket *so);
184 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
185 struct rt_metrics_lite *out);
186 static void rt_getmetrics(const struct rt_metrics_lite *in,
187 struct rt_metrics *out);
188 static void rt_dispatch(struct mbuf *, sa_family_t);
190 static struct netisr_handler rtsock_nh = {
192 .nh_handler = rts_input,
193 .nh_proto = NETISR_ROUTE,
194 .nh_policy = NETISR_POLICY_SOURCE,
198 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
202 netisr_getqlimit(&rtsock_nh, &qlimit);
203 error = sysctl_handle_int(oidp, &qlimit, 0, req);
204 if (error || !req->newptr)
208 return (netisr_setqlimit(&rtsock_nh, qlimit));
210 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
211 0, 0, sysctl_route_netisr_maxqlen, "I",
212 "maximum routing socket dispatch queue length");
219 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
220 rtsock_nh.nh_qlimit = tmp;
221 netisr_register(&rtsock_nh);
223 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
226 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
231 KASSERT(m != NULL, ("%s: m is NULL", __func__));
232 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
233 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
235 /* No filtering requested. */
236 if ((m->m_flags & RTS_FILTER_FIB) == 0)
239 /* Check if it is a rts and the fib matches the one of the socket. */
240 fibnum = M_GETFIB(m);
241 if (proto->sp_family != PF_ROUTE ||
242 rp->rcb_socket == NULL ||
243 rp->rcb_socket->so_fibnum == fibnum)
246 /* Filtering requested and no match, the socket shall be skipped. */
251 rts_input(struct mbuf *m)
253 struct sockproto route_proto;
254 unsigned short *family;
257 route_proto.sp_family = PF_ROUTE;
258 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
260 family = (unsigned short *)(tag + 1);
261 route_proto.sp_protocol = *family;
262 m_tag_delete(m, tag);
264 route_proto.sp_protocol = 0;
266 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
270 * It really doesn't make any sense at all for this code to share much
271 * with raw_usrreq.c, since its functionality is so restricted. XXX
274 rts_abort(struct socket *so)
277 raw_usrreqs.pru_abort(so);
281 rts_close(struct socket *so)
284 raw_usrreqs.pru_close(so);
287 /* pru_accept is EOPNOTSUPP */
290 rts_attach(struct socket *so, int proto, struct thread *td)
295 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
298 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
303 * The splnet() is necessary to block protocols from sending
304 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
305 * this PCB is extant but incompletely initialized.
306 * Probably we should try to do more of this work beforehand and
310 so->so_pcb = (caddr_t)rp;
311 so->so_fibnum = td->td_proc->p_fibnum;
312 error = raw_attach(so, proto);
321 switch(rp->rcb_proto.sp_protocol) {
326 route_cb.ip6_count++;
329 route_cb.ipx_count++;
332 route_cb.any_count++;
335 so->so_options |= SO_USELOOPBACK;
341 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
344 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
348 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
351 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
354 /* pru_connect2 is EOPNOTSUPP */
355 /* pru_control is EOPNOTSUPP */
358 rts_detach(struct socket *so)
360 struct rawcb *rp = sotorawcb(so);
362 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
365 switch(rp->rcb_proto.sp_protocol) {
370 route_cb.ip6_count--;
373 route_cb.ipx_count--;
376 route_cb.any_count--;
378 raw_usrreqs.pru_detach(so);
382 rts_disconnect(struct socket *so)
385 return (raw_usrreqs.pru_disconnect(so));
388 /* pru_listen is EOPNOTSUPP */
391 rts_peeraddr(struct socket *so, struct sockaddr **nam)
394 return (raw_usrreqs.pru_peeraddr(so, nam));
397 /* pru_rcvd is EOPNOTSUPP */
398 /* pru_rcvoob is EOPNOTSUPP */
401 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
402 struct mbuf *control, struct thread *td)
405 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
408 /* pru_sense is null */
411 rts_shutdown(struct socket *so)
414 return (raw_usrreqs.pru_shutdown(so));
418 rts_sockaddr(struct socket *so, struct sockaddr **nam)
421 return (raw_usrreqs.pru_sockaddr(so, nam));
424 static struct pr_usrreqs route_usrreqs = {
425 .pru_abort = rts_abort,
426 .pru_attach = rts_attach,
427 .pru_bind = rts_bind,
428 .pru_connect = rts_connect,
429 .pru_detach = rts_detach,
430 .pru_disconnect = rts_disconnect,
431 .pru_peeraddr = rts_peeraddr,
432 .pru_send = rts_send,
433 .pru_shutdown = rts_shutdown,
434 .pru_sockaddr = rts_sockaddr,
435 .pru_close = rts_close,
438 #ifndef _SOCKADDR_UNION_DEFINED
439 #define _SOCKADDR_UNION_DEFINED
441 * The union of all possible address formats we handle.
443 union sockaddr_union {
445 struct sockaddr_in sin;
446 struct sockaddr_in6 sin6;
448 #endif /* _SOCKADDR_UNION_DEFINED */
451 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
452 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
455 /* First, see if the returned address is part of the jail. */
456 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
457 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
461 switch (info->rti_info[RTAX_DST]->sa_family) {
471 * Try to find an address on the given outgoing interface
472 * that belongs to the jail.
475 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
478 if (sa->sa_family != AF_INET)
480 ia = ((struct sockaddr_in *)sa)->sin_addr;
481 if (prison_check_ip4(cred, &ia) == 0) {
486 IF_ADDR_RUNLOCK(ifp);
489 * As a last resort return the 'default' jail address.
491 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
493 if (prison_get_ip4(cred, &ia) != 0)
496 bzero(&saun->sin, sizeof(struct sockaddr_in));
497 saun->sin.sin_len = sizeof(struct sockaddr_in);
498 saun->sin.sin_family = AF_INET;
499 saun->sin.sin_addr.s_addr = ia.s_addr;
500 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
513 * Try to find an address on the given outgoing interface
514 * that belongs to the jail.
517 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
520 if (sa->sa_family != AF_INET6)
522 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
523 &ia6, sizeof(struct in6_addr));
524 if (prison_check_ip6(cred, &ia6) == 0) {
529 IF_ADDR_RUNLOCK(ifp);
532 * As a last resort return the 'default' jail address.
534 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
536 if (prison_get_ip6(cred, &ia6) != 0)
539 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
540 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
541 saun->sin6.sin6_family = AF_INET6;
542 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
543 if (sa6_recoverscope(&saun->sin6) != 0)
545 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
557 route_output(struct mbuf *m, struct socket *so)
559 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
560 struct rt_msghdr *rtm = NULL;
561 struct rtentry *rt = NULL;
562 struct radix_node_head *rnh;
563 struct rt_addrinfo info;
565 struct ifnet *ifp = NULL;
566 union sockaddr_union saun;
567 sa_family_t saf = AF_UNSPEC;
569 #define senderr(e) { error = e; goto flush;}
570 if (m == NULL || ((m->m_len < sizeof(long)) &&
571 (m = m_pullup(m, sizeof(long))) == NULL))
573 if ((m->m_flags & M_PKTHDR) == 0)
574 panic("route_output");
575 len = m->m_pkthdr.len;
576 if (len < sizeof(*rtm) ||
577 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
578 info.rti_info[RTAX_DST] = NULL;
581 R_Malloc(rtm, struct rt_msghdr *, len);
583 info.rti_info[RTAX_DST] = NULL;
586 m_copydata(m, 0, len, (caddr_t)rtm);
587 if (rtm->rtm_version != RTM_VERSION) {
588 info.rti_info[RTAX_DST] = NULL;
589 senderr(EPROTONOSUPPORT);
591 rtm->rtm_pid = curproc->p_pid;
592 bzero(&info, sizeof(info));
593 info.rti_addrs = rtm->rtm_addrs;
594 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
595 info.rti_info[RTAX_DST] = NULL;
598 info.rti_flags = rtm->rtm_flags;
599 if (info.rti_info[RTAX_DST] == NULL ||
600 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
601 (info.rti_info[RTAX_GATEWAY] != NULL &&
602 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
604 saf = info.rti_info[RTAX_DST]->sa_family;
606 * Verify that the caller has the appropriate privilege; RTM_GET
607 * is the only operation the non-superuser is allowed.
609 if (rtm->rtm_type != RTM_GET) {
610 error = priv_check(curthread, PRIV_NET_ROUTE);
616 * The given gateway address may be an interface address.
617 * For example, issuing a "route change" command on a route
618 * entry that was created from a tunnel, and the gateway
619 * address given is the local end point. In this case the
620 * RTF_GATEWAY flag must be cleared or the destination will
621 * not be reachable even though there is no error message.
623 if (info.rti_info[RTAX_GATEWAY] != NULL &&
624 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
627 bzero(&gw_ro, sizeof(gw_ro));
628 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
629 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
631 * A host route through the loopback interface is
632 * installed for each interface adddress. In pre 8.0
633 * releases the interface address of a PPP link type
634 * is not reachable locally. This behavior is fixed as
635 * part of the new L2/L3 redesign and rewrite work. The
636 * signature of this interface address route is the
637 * AF_LINK sa_family type of the rt_gateway, and the
638 * rt_ifp has the IFF_LOOPBACK flag set.
640 if (gw_ro.ro_rt != NULL &&
641 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
642 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
643 info.rti_flags &= ~RTF_GATEWAY;
644 if (gw_ro.ro_rt != NULL)
648 switch (rtm->rtm_type) {
649 struct rtentry *saved_nrt;
652 if (info.rti_info[RTAX_GATEWAY] == NULL)
656 /* support for new ARP code */
657 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
658 (rtm->rtm_flags & RTF_LLDATA) != 0) {
659 error = lla_rt_output(rtm, &info);
662 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
664 if (error == 0 && saved_nrt) {
666 rt_setmetrics(rtm->rtm_inits,
667 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
668 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
669 RT_REMREF(saved_nrt);
670 RT_UNLOCK(saved_nrt);
676 /* support for new ARP code */
677 if (info.rti_info[RTAX_GATEWAY] &&
678 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
679 (rtm->rtm_flags & RTF_LLDATA) != 0) {
680 error = lla_rt_output(rtm, &info);
683 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
695 rnh = rt_tables_get_rnh(so->so_fibnum,
696 info.rti_info[RTAX_DST]->sa_family);
698 senderr(EAFNOSUPPORT);
700 RADIX_NODE_HEAD_RLOCK(rnh);
702 if (info.rti_info[RTAX_NETMASK] == NULL &&
703 rtm->rtm_type == RTM_GET) {
705 * Provide logest prefix match for
706 * address lookup (no mask).
707 * 'route -n get addr'
709 rt = (struct rtentry *) rnh->rnh_matchaddr(
710 info.rti_info[RTAX_DST], rnh);
712 rt = (struct rtentry *) rnh->rnh_lookup(
713 info.rti_info[RTAX_DST],
714 info.rti_info[RTAX_NETMASK], rnh);
717 RADIX_NODE_HEAD_RUNLOCK(rnh);
722 * for RTM_CHANGE/LOCK, if we got multipath routes,
723 * we require users to specify a matching RTAX_GATEWAY.
725 * for RTM_GET, gate is optional even with multipath.
726 * if gate == NULL the first match is returned.
727 * (no need to call rt_mpath_matchgate if gate == NULL)
729 if (rn_mpath_capable(rnh) &&
730 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
731 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
733 RADIX_NODE_HEAD_RUNLOCK(rnh);
739 * If performing proxied L2 entry insertion, and
740 * the actual PPP host entry is found, perform
741 * another search to retrieve the prefix route of
742 * the local end point of the PPP link.
744 if (rtm->rtm_flags & RTF_ANNOUNCE) {
745 struct sockaddr laddr;
747 if (rt->rt_ifp != NULL &&
748 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
751 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
753 rt_maskedcopy(ifa->ifa_addr,
757 rt_maskedcopy(rt->rt_ifa->ifa_addr,
759 rt->rt_ifa->ifa_netmask);
761 * refactor rt and no lock operation necessary
763 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
765 RADIX_NODE_HEAD_RUNLOCK(rnh);
771 RADIX_NODE_HEAD_RUNLOCK(rnh);
773 switch(rtm->rtm_type) {
778 if ((rt->rt_flags & RTF_HOST) == 0
779 ? jailed_without_vnet(curthread->td_ucred)
780 : prison_if(curthread->td_ucred,
785 info.rti_info[RTAX_DST] = rt_key(rt);
786 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
787 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
788 info.rti_info[RTAX_GENMASK] = 0;
789 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
792 info.rti_info[RTAX_IFP] =
793 ifp->if_addr->ifa_addr;
794 error = rtm_get_jailed(&info, ifp, rt,
795 &saun, curthread->td_ucred);
800 if (ifp->if_flags & IFF_POINTOPOINT)
801 info.rti_info[RTAX_BRD] =
802 rt->rt_ifa->ifa_dstaddr;
803 rtm->rtm_index = ifp->if_index;
805 info.rti_info[RTAX_IFP] = NULL;
806 info.rti_info[RTAX_IFA] = NULL;
808 } else if ((ifp = rt->rt_ifp) != NULL) {
809 rtm->rtm_index = ifp->if_index;
811 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
812 if (len > rtm->rtm_msglen) {
813 struct rt_msghdr *new_rtm;
814 R_Malloc(new_rtm, struct rt_msghdr *, len);
815 if (new_rtm == NULL) {
819 bcopy(rtm, new_rtm, rtm->rtm_msglen);
820 Free(rtm); rtm = new_rtm;
822 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
823 rtm->rtm_flags = rt->rt_flags;
824 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
825 rtm->rtm_addrs = info.rti_addrs;
830 * New gateway could require new ifaddr, ifp;
831 * flags may also be different; ifp may be specified
832 * by ll sockaddr when protocol address is ambiguous
834 if (((rt->rt_flags & RTF_GATEWAY) &&
835 info.rti_info[RTAX_GATEWAY] != NULL) ||
836 info.rti_info[RTAX_IFP] != NULL ||
837 (info.rti_info[RTAX_IFA] != NULL &&
838 !sa_equal(info.rti_info[RTAX_IFA],
839 rt->rt_ifa->ifa_addr))) {
841 RADIX_NODE_HEAD_LOCK(rnh);
842 error = rt_getifa_fib(&info, rt->rt_fibnum);
844 * XXXRW: Really we should release this
845 * reference later, but this maintains
846 * historical behavior.
848 if (info.rti_ifa != NULL)
849 ifa_free(info.rti_ifa);
850 RADIX_NODE_HEAD_UNLOCK(rnh);
855 if (info.rti_ifa != NULL &&
856 info.rti_ifa != rt->rt_ifa &&
857 rt->rt_ifa != NULL &&
858 rt->rt_ifa->ifa_rtrequest != NULL) {
859 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
861 ifa_free(rt->rt_ifa);
863 if (info.rti_info[RTAX_GATEWAY] != NULL) {
865 RADIX_NODE_HEAD_LOCK(rnh);
868 error = rt_setgate(rt, rt_key(rt),
869 info.rti_info[RTAX_GATEWAY]);
870 RADIX_NODE_HEAD_UNLOCK(rnh);
875 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
877 if (info.rti_ifa != NULL &&
878 info.rti_ifa != rt->rt_ifa) {
879 ifa_ref(info.rti_ifa);
880 rt->rt_ifa = info.rti_ifa;
881 rt->rt_ifp = info.rti_ifp;
883 /* Allow some flags to be toggled on change. */
884 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
885 (rtm->rtm_flags & RTF_FMASK);
886 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
888 rtm->rtm_index = rt->rt_ifp->if_index;
889 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
890 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
893 /* We don't support locks anymore */
906 rtm->rtm_errno = error;
908 rtm->rtm_flags |= RTF_DONE;
910 if (rt) /* XXX can this be true? */
913 struct rawcb *rp = NULL;
915 * Check to see if we don't want our own messages.
917 if ((so->so_options & SO_USELOOPBACK) == 0) {
918 if (route_cb.any_count <= 1) {
924 /* There is another listener, so construct message */
928 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
929 if (m->m_pkthdr.len < rtm->rtm_msglen) {
932 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
933 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
936 M_SETFIB(m, so->so_fibnum);
937 m->m_flags |= RTS_FILTER_FIB;
940 * XXX insure we don't get a copy by
941 * invalidating our protocol
943 unsigned short family = rp->rcb_proto.sp_family;
944 rp->rcb_proto.sp_family = 0;
946 rp->rcb_proto.sp_family = family;
950 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
959 rt_setmetrics(u_long which, const struct rt_metrics *in,
960 struct rt_metrics_lite *out)
962 #define metric(f, e) if (which & (f)) out->e = in->e;
964 * Only these are stored in the routing entry since introduction
965 * of tcp hostcache. The rest is ignored.
967 metric(RTV_MTU, rmx_mtu);
968 metric(RTV_WEIGHT, rmx_weight);
969 /* Userland -> kernel timebase conversion. */
970 if (which & RTV_EXPIRE)
971 out->rmx_expire = in->rmx_expire ?
972 in->rmx_expire - time_second + time_uptime : 0;
977 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
979 #define metric(e) out->e = in->e;
980 bzero(out, sizeof(*out));
983 /* Kernel -> userland timebase conversion. */
984 out->rmx_expire = in->rmx_expire ?
985 in->rmx_expire - time_uptime + time_second : 0;
990 * Extract the addresses of the passed sockaddrs.
991 * Do a little sanity checking so as to avoid bad memory references.
992 * This data is derived straight from userland.
995 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1000 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1001 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1003 sa = (struct sockaddr *)cp;
1007 if (cp + sa->sa_len > cplim)
1010 * there are no more.. quit now
1011 * If there are more bits, they are in error.
1012 * I've seen this. route(1) can evidently generate these.
1013 * This causes kernel to core dump.
1014 * for compatibility, If we see this, point to a safe address.
1016 if (sa->sa_len == 0) {
1017 rtinfo->rti_info[i] = &sa_zero;
1018 return (0); /* should be EINVAL but for compat */
1021 rtinfo->rti_info[i] = sa;
1028 * Used by the routing socket.
1030 static struct mbuf *
1031 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1033 struct rt_msghdr *rtm;
1036 struct sockaddr *sa;
1043 len = sizeof(struct ifa_msghdr);
1048 len = sizeof(struct ifma_msghdr);
1052 len = sizeof(struct if_msghdr);
1055 case RTM_IFANNOUNCE:
1057 len = sizeof(struct if_announcemsghdr);
1061 len = sizeof(struct rt_msghdr);
1065 m = m_gethdr(M_DONTWAIT, MT_DATA);
1066 if (m && len > MHLEN) {
1067 MCLGET(m, M_DONTWAIT);
1068 if ((m->m_flags & M_EXT) == 0) {
1075 m->m_pkthdr.len = m->m_len = len;
1076 m->m_pkthdr.rcvif = NULL;
1077 rtm = mtod(m, struct rt_msghdr *);
1078 bzero((caddr_t)rtm, len);
1079 for (i = 0; i < RTAX_MAX; i++) {
1080 if ((sa = rtinfo->rti_info[i]) == NULL)
1082 rtinfo->rti_addrs |= (1 << i);
1084 m_copyback(m, len, dlen, (caddr_t)sa);
1087 if (m->m_pkthdr.len != len) {
1091 rtm->rtm_msglen = len;
1092 rtm->rtm_version = RTM_VERSION;
1093 rtm->rtm_type = type;
1098 * Used by the sysctl code and routing socket.
1101 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1104 int len, dlen, second_time = 0;
1107 rtinfo->rti_addrs = 0;
1113 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1114 #ifdef COMPAT_FREEBSD32
1115 if (w->w_req->flags & SCTL_MASK32)
1116 len = sizeof(struct ifa_msghdrl32);
1119 len = sizeof(struct ifa_msghdrl);
1121 len = sizeof(struct ifa_msghdr);
1125 #ifdef COMPAT_FREEBSD32
1126 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1127 if (w->w_op == NET_RT_IFLISTL)
1128 len = sizeof(struct if_msghdrl32);
1130 len = sizeof(struct if_msghdr32);
1134 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1135 len = sizeof(struct if_msghdrl);
1137 len = sizeof(struct if_msghdr);
1141 len = sizeof(struct ifma_msghdr);
1145 len = sizeof(struct rt_msghdr);
1150 for (i = 0; i < RTAX_MAX; i++) {
1151 struct sockaddr *sa;
1153 if ((sa = rtinfo->rti_info[i]) == NULL)
1155 rtinfo->rti_addrs |= (1 << i);
1158 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1164 if (cp == NULL && w != NULL && !second_time) {
1165 struct walkarg *rw = w;
1168 if (rw->w_tmemsize < len) {
1170 free(rw->w_tmem, M_RTABLE);
1171 rw->w_tmem = (caddr_t)
1172 malloc(len, M_RTABLE, M_NOWAIT);
1174 rw->w_tmemsize = len;
1184 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1186 rtm->rtm_version = RTM_VERSION;
1187 rtm->rtm_type = type;
1188 rtm->rtm_msglen = len;
1194 * This routine is called to generate a message from the routing
1195 * socket indicating that a redirect has occured, a routing lookup
1196 * has failed, or that a protocol has detected timeouts to a particular
1200 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1203 struct rt_msghdr *rtm;
1205 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1207 if (route_cb.any_count == 0)
1209 m = rt_msg1(type, rtinfo);
1213 if (fibnum != RT_ALL_FIBS) {
1214 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1215 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1216 M_SETFIB(m, fibnum);
1217 m->m_flags |= RTS_FILTER_FIB;
1220 rtm = mtod(m, struct rt_msghdr *);
1221 rtm->rtm_flags = RTF_DONE | flags;
1222 rtm->rtm_errno = error;
1223 rtm->rtm_addrs = rtinfo->rti_addrs;
1224 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1228 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1231 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1235 * This routine is called to generate a message from the routing
1236 * socket indicating that the status of a network interface has changed.
1239 rt_ifmsg(struct ifnet *ifp)
1241 struct if_msghdr *ifm;
1243 struct rt_addrinfo info;
1245 if (route_cb.any_count == 0)
1247 bzero((caddr_t)&info, sizeof(info));
1248 m = rt_msg1(RTM_IFINFO, &info);
1251 ifm = mtod(m, struct if_msghdr *);
1252 ifm->ifm_index = ifp->if_index;
1253 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1254 ifm->ifm_data = ifp->if_data;
1256 rt_dispatch(m, AF_UNSPEC);
1260 * Announce interface address arrival/withdraw.
1261 * Please do not call directly, use rt_addrmsg().
1262 * Assume input data to be valid.
1263 * Returns 0 on success.
1266 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1268 struct rt_addrinfo info;
1269 struct sockaddr *sa;
1272 struct ifa_msghdr *ifam;
1273 struct ifnet *ifp = ifa->ifa_ifp;
1275 if (route_cb.any_count == 0)
1278 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1280 bzero((caddr_t)&info, sizeof(info));
1281 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1282 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1283 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1284 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1285 if ((m = rt_msg1(ncmd, &info)) == NULL)
1287 ifam = mtod(m, struct ifa_msghdr *);
1288 ifam->ifam_index = ifp->if_index;
1289 ifam->ifam_metric = ifa->ifa_metric;
1290 ifam->ifam_flags = ifa->ifa_flags;
1291 ifam->ifam_addrs = info.rti_addrs;
1293 if (fibnum != RT_ALL_FIBS) {
1294 M_SETFIB(m, fibnum);
1295 m->m_flags |= RTS_FILTER_FIB;
1298 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1304 * Announce route addition/removal.
1305 * Please do not call directly, use rt_routemsg().
1306 * Note that @rt data MAY be inconsistent/invalid:
1307 * if some userland app sends us "invalid" route message (invalid mask,
1308 * no dst, wrong address families, etc...) we need to pass it back
1309 * to app (and any other rtsock consumers) with rtm_errno field set to
1312 * Returns 0 on success.
1315 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1318 struct rt_addrinfo info;
1319 struct sockaddr *sa;
1321 struct rt_msghdr *rtm;
1323 if (route_cb.any_count == 0)
1326 bzero((caddr_t)&info, sizeof(info));
1327 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1328 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1329 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1330 if ((m = rt_msg1(cmd, &info)) == NULL)
1332 rtm = mtod(m, struct rt_msghdr *);
1333 rtm->rtm_index = ifp->if_index;
1334 rtm->rtm_flags |= rt->rt_flags;
1335 rtm->rtm_errno = error;
1336 rtm->rtm_addrs = info.rti_addrs;
1338 if (fibnum != RT_ALL_FIBS) {
1339 M_SETFIB(m, fibnum);
1340 m->m_flags |= RTS_FILTER_FIB;
1343 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1349 * This is the analogue to the rt_newaddrmsg which performs the same
1350 * function but for multicast group memberhips. This is easier since
1351 * there is no route state to worry about.
1354 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1356 struct rt_addrinfo info;
1357 struct mbuf *m = NULL;
1358 struct ifnet *ifp = ifma->ifma_ifp;
1359 struct ifma_msghdr *ifmam;
1361 if (route_cb.any_count == 0)
1364 bzero((caddr_t)&info, sizeof(info));
1365 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1366 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1368 * If a link-layer address is present, present it as a ``gateway''
1369 * (similarly to how ARP entries, e.g., are presented).
1371 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1372 m = rt_msg1(cmd, &info);
1375 ifmam = mtod(m, struct ifma_msghdr *);
1376 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1378 ifmam->ifmam_index = ifp->if_index;
1379 ifmam->ifmam_addrs = info.rti_addrs;
1380 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1383 static struct mbuf *
1384 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1385 struct rt_addrinfo *info)
1387 struct if_announcemsghdr *ifan;
1390 if (route_cb.any_count == 0)
1392 bzero((caddr_t)info, sizeof(*info));
1393 m = rt_msg1(type, info);
1395 ifan = mtod(m, struct if_announcemsghdr *);
1396 ifan->ifan_index = ifp->if_index;
1397 strlcpy(ifan->ifan_name, ifp->if_xname,
1398 sizeof(ifan->ifan_name));
1399 ifan->ifan_what = what;
1405 * This is called to generate routing socket messages indicating
1406 * IEEE80211 wireless events.
1407 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1410 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1413 struct rt_addrinfo info;
1415 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1418 * Append the ieee80211 data. Try to stick it in the
1419 * mbuf containing the ifannounce msg; otherwise allocate
1420 * a new mbuf and append.
1422 * NB: we assume m is a single mbuf.
1424 if (data_len > M_TRAILINGSPACE(m)) {
1425 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1430 bcopy(data, mtod(n, void *), data_len);
1431 n->m_len = data_len;
1433 } else if (data_len > 0) {
1434 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1435 m->m_len += data_len;
1437 if (m->m_flags & M_PKTHDR)
1438 m->m_pkthdr.len += data_len;
1439 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1440 rt_dispatch(m, AF_UNSPEC);
1445 * This is called to generate routing socket messages indicating
1446 * network interface arrival and departure.
1449 rt_ifannouncemsg(struct ifnet *ifp, int what)
1452 struct rt_addrinfo info;
1454 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1456 rt_dispatch(m, AF_UNSPEC);
1460 rt_dispatch(struct mbuf *m, sa_family_t saf)
1465 * Preserve the family from the sockaddr, if any, in an m_tag for
1466 * use when injecting the mbuf into the routing socket buffer from
1469 if (saf != AF_UNSPEC) {
1470 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1476 *(unsigned short *)(tag + 1) = saf;
1477 m_tag_prepend(m, tag);
1481 m->m_pkthdr.rcvif = V_loif;
1487 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1491 * This is used in dumping the kernel table via sysctl().
1494 sysctl_dumpentry(struct radix_node *rn, void *vw)
1496 struct walkarg *w = vw;
1497 struct rtentry *rt = (struct rtentry *)rn;
1498 int error = 0, size;
1499 struct rt_addrinfo info;
1501 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1503 if ((rt->rt_flags & RTF_HOST) == 0
1504 ? jailed_without_vnet(w->w_req->td->td_ucred)
1505 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1507 bzero((caddr_t)&info, sizeof(info));
1508 info.rti_info[RTAX_DST] = rt_key(rt);
1509 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1510 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1511 info.rti_info[RTAX_GENMASK] = 0;
1513 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1514 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1515 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1516 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1518 size = rt_msg2(RTM_GET, &info, NULL, w);
1519 if (w->w_req && w->w_tmem) {
1520 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1522 rtm->rtm_flags = rt->rt_flags;
1524 * let's be honest about this being a retarded hack
1526 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1527 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1528 rtm->rtm_index = rt->rt_ifp->if_index;
1529 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1530 rtm->rtm_addrs = info.rti_addrs;
1531 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1537 #ifdef COMPAT_FREEBSD32
1539 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1542 bzero(dst, sizeof(*dst));
1543 CP(*src, *dst, ifi_type);
1544 CP(*src, *dst, ifi_physical);
1545 CP(*src, *dst, ifi_addrlen);
1546 CP(*src, *dst, ifi_hdrlen);
1547 CP(*src, *dst, ifi_link_state);
1548 dst->ifi_datalen = sizeof(struct if_data32);
1549 CP(*src, *dst, ifi_mtu);
1550 CP(*src, *dst, ifi_metric);
1551 CP(*src, *dst, ifi_baudrate);
1552 CP(*src, *dst, ifi_ipackets);
1553 CP(*src, *dst, ifi_ierrors);
1554 CP(*src, *dst, ifi_opackets);
1555 CP(*src, *dst, ifi_oerrors);
1556 CP(*src, *dst, ifi_collisions);
1557 CP(*src, *dst, ifi_ibytes);
1558 CP(*src, *dst, ifi_obytes);
1559 CP(*src, *dst, ifi_imcasts);
1560 CP(*src, *dst, ifi_omcasts);
1561 CP(*src, *dst, ifi_iqdrops);
1562 CP(*src, *dst, ifi_noproto);
1563 CP(*src, *dst, ifi_hwassist);
1564 CP(*src, *dst, ifi_epoch);
1565 TV_CP(*src, *dst, ifi_lastchange);
1570 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1571 struct walkarg *w, int len)
1573 struct if_msghdrl *ifm;
1575 #ifdef COMPAT_FREEBSD32
1576 if (w->w_req->flags & SCTL_MASK32) {
1577 struct if_msghdrl32 *ifm32;
1579 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1580 ifm32->ifm_addrs = info->rti_addrs;
1581 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1582 ifm32->ifm_index = ifp->if_index;
1583 ifm32->_ifm_spare1 = 0;
1584 ifm32->ifm_len = sizeof(*ifm32);
1585 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1587 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1589 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1592 ifm = (struct if_msghdrl *)w->w_tmem;
1593 ifm->ifm_addrs = info->rti_addrs;
1594 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1595 ifm->ifm_index = ifp->if_index;
1596 ifm->_ifm_spare1 = 0;
1597 ifm->ifm_len = sizeof(*ifm);
1598 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1600 ifm->ifm_data = ifp->if_data;
1602 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1606 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1607 struct walkarg *w, int len)
1609 struct if_msghdr *ifm;
1611 #ifdef COMPAT_FREEBSD32
1612 if (w->w_req->flags & SCTL_MASK32) {
1613 struct if_msghdr32 *ifm32;
1615 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1616 ifm32->ifm_addrs = info->rti_addrs;
1617 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1618 ifm32->ifm_index = ifp->if_index;
1620 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1622 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1625 ifm = (struct if_msghdr *)w->w_tmem;
1626 ifm->ifm_addrs = info->rti_addrs;
1627 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1628 ifm->ifm_index = ifp->if_index;
1630 ifm->ifm_data = ifp->if_data;
1632 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1636 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1637 struct walkarg *w, int len)
1639 struct ifa_msghdrl *ifam;
1641 #ifdef COMPAT_FREEBSD32
1642 if (w->w_req->flags & SCTL_MASK32) {
1643 struct ifa_msghdrl32 *ifam32;
1645 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1646 ifam32->ifam_addrs = info->rti_addrs;
1647 ifam32->ifam_flags = ifa->ifa_flags;
1648 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1649 ifam32->_ifam_spare1 = 0;
1650 ifam32->ifam_len = sizeof(*ifam32);
1651 ifam32->ifam_data_off =
1652 offsetof(struct ifa_msghdrl32, ifam_data);
1653 ifam32->ifam_metric = ifa->ifa_metric;
1655 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1657 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1661 ifam = (struct ifa_msghdrl *)w->w_tmem;
1662 ifam->ifam_addrs = info->rti_addrs;
1663 ifam->ifam_flags = ifa->ifa_flags;
1664 ifam->ifam_index = ifa->ifa_ifp->if_index;
1665 ifam->_ifam_spare1 = 0;
1666 ifam->ifam_len = sizeof(*ifam);
1667 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1668 ifam->ifam_metric = ifa->ifa_metric;
1670 ifam->ifam_data = ifa->if_data;
1672 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1676 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1677 struct walkarg *w, int len)
1679 struct ifa_msghdr *ifam;
1681 ifam = (struct ifa_msghdr *)w->w_tmem;
1682 ifam->ifam_addrs = info->rti_addrs;
1683 ifam->ifam_flags = ifa->ifa_flags;
1684 ifam->ifam_index = ifa->ifa_ifp->if_index;
1685 ifam->ifam_metric = ifa->ifa_metric;
1687 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1691 sysctl_iflist(int af, struct walkarg *w)
1695 struct rt_addrinfo info;
1698 bzero((caddr_t)&info, sizeof(info));
1700 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1701 if (w->w_arg && w->w_arg != ifp->if_index)
1705 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1706 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1707 info.rti_info[RTAX_IFP] = NULL;
1708 if (w->w_req && w->w_tmem) {
1709 if (w->w_op == NET_RT_IFLISTL)
1710 error = sysctl_iflist_ifml(ifp, &info, w, len);
1712 error = sysctl_iflist_ifm(ifp, &info, w, len);
1716 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1717 if (af && af != ifa->ifa_addr->sa_family)
1719 if (prison_if(w->w_req->td->td_ucred,
1720 ifa->ifa_addr) != 0)
1722 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1723 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1724 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1725 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1726 if (w->w_req && w->w_tmem) {
1727 if (w->w_op == NET_RT_IFLISTL)
1728 error = sysctl_iflist_ifaml(ifa, &info,
1731 error = sysctl_iflist_ifam(ifa, &info,
1737 IF_ADDR_RUNLOCK(ifp);
1738 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1739 info.rti_info[RTAX_BRD] = NULL;
1743 IF_ADDR_RUNLOCK(ifp);
1749 sysctl_ifmalist(int af, struct walkarg *w)
1752 struct ifmultiaddr *ifma;
1753 struct rt_addrinfo info;
1757 bzero((caddr_t)&info, sizeof(info));
1759 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1760 if (w->w_arg && w->w_arg != ifp->if_index)
1763 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1765 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1766 if (af && af != ifma->ifma_addr->sa_family)
1768 if (prison_if(w->w_req->td->td_ucred,
1769 ifma->ifma_addr) != 0)
1771 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1772 info.rti_info[RTAX_GATEWAY] =
1773 (ifma->ifma_addr->sa_family != AF_LINK) ?
1774 ifma->ifma_lladdr : NULL;
1775 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1776 if (w->w_req && w->w_tmem) {
1777 struct ifma_msghdr *ifmam;
1779 ifmam = (struct ifma_msghdr *)w->w_tmem;
1780 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1781 ifmam->ifmam_flags = 0;
1782 ifmam->ifmam_addrs = info.rti_addrs;
1783 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1785 IF_ADDR_RUNLOCK(ifp);
1790 IF_ADDR_RUNLOCK(ifp);
1798 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1800 int *name = (int *)arg1;
1801 u_int namelen = arg2;
1802 struct radix_node_head *rnh = NULL; /* silence compiler. */
1803 int i, lim, error = EINVAL;
1812 if (name[1] == NET_RT_DUMP) {
1814 fib = req->td->td_proc->p_fibnum;
1815 else if (namelen == 4)
1816 fib = (name[3] == RT_ALL_FIBS) ?
1817 req->td->td_proc->p_fibnum : name[3];
1819 return ((namelen < 3) ? EISDIR : ENOTDIR);
1820 if (fib < 0 || fib >= rt_numfibs)
1822 } else if (namelen != 3)
1823 return ((namelen < 3) ? EISDIR : ENOTDIR);
1827 bzero(&w, sizeof(w));
1832 error = sysctl_wire_old_buffer(req, 0);
1839 if (af == 0) { /* dump all tables */
1842 } else /* dump only one table */
1846 * take care of llinfo entries, the caller must
1849 if (w.w_op == NET_RT_FLAGS &&
1850 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1852 error = lltable_sysctl_dumparp(af, w.w_req);
1858 * take care of routing entries
1860 for (error = 0; error == 0 && i <= lim; i++) {
1861 rnh = rt_tables_get_rnh(fib, i);
1863 RADIX_NODE_HEAD_RLOCK(rnh);
1864 error = rnh->rnh_walktree(rnh,
1865 sysctl_dumpentry, &w);
1866 RADIX_NODE_HEAD_RUNLOCK(rnh);
1868 error = EAFNOSUPPORT;
1873 case NET_RT_IFLISTL:
1874 error = sysctl_iflist(af, &w);
1877 case NET_RT_IFMALIST:
1878 error = sysctl_ifmalist(af, &w);
1882 free(w.w_tmem, M_RTABLE);
1886 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1889 * Definitions of protocols supported in the ROUTE domain.
1892 static struct domain routedomain; /* or at least forward */
1894 static struct protosw routesw[] = {
1896 .pr_type = SOCK_RAW,
1897 .pr_domain = &routedomain,
1898 .pr_flags = PR_ATOMIC|PR_ADDR,
1899 .pr_output = route_output,
1900 .pr_ctlinput = raw_ctlinput,
1901 .pr_init = raw_init,
1902 .pr_usrreqs = &route_usrreqs
1906 static struct domain routedomain = {
1907 .dom_family = PF_ROUTE,
1908 .dom_name = "route",
1909 .dom_protosw = routesw,
1910 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1913 VNET_DOMAIN_SET(route);