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"
34 #include "opt_mpath.h"
36 #include "opt_inet6.h"
38 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
43 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
56 #include <net/if_dl.h>
57 #include <net/if_llatbl.h>
58 #include <net/if_types.h>
59 #include <net/netisr.h>
60 #include <net/raw_cb.h>
61 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
67 #include <netinet6/scope6_var.h>
70 #if defined(INET) || defined(INET6)
72 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
76 #ifdef COMPAT_FREEBSD32
77 #include <sys/mount.h>
78 #include <compat/freebsd32/freebsd32.h>
85 uint8_t ifi_link_state;
86 uint8_t ifi_spare_char1;
87 uint8_t ifi_spare_char2;
91 uint32_t ifi_baudrate;
92 uint32_t ifi_ipackets;
94 uint32_t ifi_opackets;
96 uint32_t ifi_collisions;
100 uint32_t ifi_omcasts;
101 uint32_t ifi_iqdrops;
102 uint32_t ifi_noproto;
103 uint32_t ifi_hwassist;
105 struct timeval32 ifi_lastchange;
115 struct if_data32 ifm_data;
119 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
121 /* NB: these are not modified */
122 static struct sockaddr route_src = { 2, PF_ROUTE, };
123 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
126 * Used by rtsock/raw_input callback code to decide whether to filter the update
127 * notification to a socket bound to a particular FIB.
129 #define RTS_FILTER_FIB M_PROTO8
130 #define RTS_ALLFIBS -1
133 int ip_count; /* attached w/ AF_INET */
134 int ip6_count; /* attached w/ AF_INET6 */
135 int ipx_count; /* attached w/ AF_IPX */
136 int any_count; /* total attached */
139 struct mtx rtsock_mtx;
140 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
142 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
143 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
144 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
146 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
152 struct sysctl_req *w_req;
155 static void rts_input(struct mbuf *m);
156 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
157 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
158 caddr_t cp, struct walkarg *w);
159 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
160 struct rt_addrinfo *rtinfo);
161 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
162 static int sysctl_iflist(int af, struct walkarg *w);
163 static int sysctl_ifmalist(int af, struct walkarg *w);
164 static int route_output(struct mbuf *m, struct socket *so);
165 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
166 struct rt_metrics_lite *out);
167 static void rt_getmetrics(const struct rt_metrics_lite *in,
168 struct rt_metrics *out);
169 static void rt_dispatch(struct mbuf *, const struct sockaddr *);
171 static struct netisr_handler rtsock_nh = {
173 .nh_handler = rts_input,
174 .nh_proto = NETISR_ROUTE,
175 .nh_policy = NETISR_POLICY_SOURCE,
179 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
183 netisr_getqlimit(&rtsock_nh, &qlimit);
184 error = sysctl_handle_int(oidp, &qlimit, 0, req);
185 if (error || !req->newptr)
189 return (netisr_setqlimit(&rtsock_nh, qlimit));
191 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
192 0, 0, sysctl_route_netisr_maxqlen, "I",
193 "maximum routing socket dispatch queue length");
200 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
201 rtsock_nh.nh_qlimit = tmp;
202 netisr_register(&rtsock_nh);
204 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
207 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
212 KASSERT(m != NULL, ("%s: m is NULL", __func__));
213 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
214 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
216 /* No filtering requested. */
217 if ((m->m_flags & RTS_FILTER_FIB) == 0)
220 /* Check if it is a rts and the fib matches the one of the socket. */
221 fibnum = M_GETFIB(m);
222 if (proto->sp_family != PF_ROUTE ||
223 rp->rcb_socket == NULL ||
224 rp->rcb_socket->so_fibnum == fibnum)
227 /* Filtering requested and no match, the socket shall be skipped. */
232 rts_input(struct mbuf *m)
234 struct sockproto route_proto;
235 unsigned short *family;
238 route_proto.sp_family = PF_ROUTE;
239 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
241 family = (unsigned short *)(tag + 1);
242 route_proto.sp_protocol = *family;
243 m_tag_delete(m, tag);
245 route_proto.sp_protocol = 0;
247 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
251 * It really doesn't make any sense at all for this code to share much
252 * with raw_usrreq.c, since its functionality is so restricted. XXX
255 rts_abort(struct socket *so)
258 raw_usrreqs.pru_abort(so);
262 rts_close(struct socket *so)
265 raw_usrreqs.pru_close(so);
268 /* pru_accept is EOPNOTSUPP */
271 rts_attach(struct socket *so, int proto, struct thread *td)
276 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
279 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
284 * The splnet() is necessary to block protocols from sending
285 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
286 * this PCB is extant but incompletely initialized.
287 * Probably we should try to do more of this work beforehand and
291 so->so_pcb = (caddr_t)rp;
292 so->so_fibnum = td->td_proc->p_fibnum;
293 error = raw_attach(so, proto);
302 switch(rp->rcb_proto.sp_protocol) {
307 route_cb.ip6_count++;
310 route_cb.ipx_count++;
313 route_cb.any_count++;
316 so->so_options |= SO_USELOOPBACK;
322 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
325 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
329 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
332 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
335 /* pru_connect2 is EOPNOTSUPP */
336 /* pru_control is EOPNOTSUPP */
339 rts_detach(struct socket *so)
341 struct rawcb *rp = sotorawcb(so);
343 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
346 switch(rp->rcb_proto.sp_protocol) {
351 route_cb.ip6_count--;
354 route_cb.ipx_count--;
357 route_cb.any_count--;
359 raw_usrreqs.pru_detach(so);
363 rts_disconnect(struct socket *so)
366 return (raw_usrreqs.pru_disconnect(so));
369 /* pru_listen is EOPNOTSUPP */
372 rts_peeraddr(struct socket *so, struct sockaddr **nam)
375 return (raw_usrreqs.pru_peeraddr(so, nam));
378 /* pru_rcvd is EOPNOTSUPP */
379 /* pru_rcvoob is EOPNOTSUPP */
382 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
383 struct mbuf *control, struct thread *td)
386 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
389 /* pru_sense is null */
392 rts_shutdown(struct socket *so)
395 return (raw_usrreqs.pru_shutdown(so));
399 rts_sockaddr(struct socket *so, struct sockaddr **nam)
402 return (raw_usrreqs.pru_sockaddr(so, nam));
405 static struct pr_usrreqs route_usrreqs = {
406 .pru_abort = rts_abort,
407 .pru_attach = rts_attach,
408 .pru_bind = rts_bind,
409 .pru_connect = rts_connect,
410 .pru_detach = rts_detach,
411 .pru_disconnect = rts_disconnect,
412 .pru_peeraddr = rts_peeraddr,
413 .pru_send = rts_send,
414 .pru_shutdown = rts_shutdown,
415 .pru_sockaddr = rts_sockaddr,
416 .pru_close = rts_close,
419 #ifndef _SOCKADDR_UNION_DEFINED
420 #define _SOCKADDR_UNION_DEFINED
422 * The union of all possible address formats we handle.
424 union sockaddr_union {
426 struct sockaddr_in sin;
427 struct sockaddr_in6 sin6;
429 #endif /* _SOCKADDR_UNION_DEFINED */
432 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
433 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
436 /* First, see if the returned address is part of the jail. */
437 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
438 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
442 switch (info->rti_info[RTAX_DST]->sa_family) {
452 * Try to find an address on the given outgoing interface
453 * that belongs to the jail.
456 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
459 if (sa->sa_family != AF_INET)
461 ia = ((struct sockaddr_in *)sa)->sin_addr;
462 if (prison_check_ip4(cred, &ia) == 0) {
470 * As a last resort return the 'default' jail address.
472 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
474 if (prison_get_ip4(cred, &ia) != 0)
477 bzero(&saun->sin, sizeof(struct sockaddr_in));
478 saun->sin.sin_len = sizeof(struct sockaddr_in);
479 saun->sin.sin_family = AF_INET;
480 saun->sin.sin_addr.s_addr = ia.s_addr;
481 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
494 * Try to find an address on the given outgoing interface
495 * that belongs to the jail.
498 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
501 if (sa->sa_family != AF_INET6)
503 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
504 &ia6, sizeof(struct in6_addr));
505 if (prison_check_ip6(cred, &ia6) == 0) {
513 * As a last resort return the 'default' jail address.
515 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
517 if (prison_get_ip6(cred, &ia6) != 0)
520 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
521 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
522 saun->sin6.sin6_family = AF_INET6;
523 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
524 if (sa6_recoverscope(&saun->sin6) != 0)
526 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
538 route_output(struct mbuf *m, struct socket *so)
540 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
541 struct rt_msghdr *rtm = NULL;
542 struct rtentry *rt = NULL;
543 struct radix_node_head *rnh;
544 struct rt_addrinfo info;
546 struct ifnet *ifp = NULL;
547 union sockaddr_union saun;
549 #define senderr(e) { error = e; goto flush;}
550 if (m == NULL || ((m->m_len < sizeof(long)) &&
551 (m = m_pullup(m, sizeof(long))) == NULL))
553 if ((m->m_flags & M_PKTHDR) == 0)
554 panic("route_output");
555 len = m->m_pkthdr.len;
556 if (len < sizeof(*rtm) ||
557 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
558 info.rti_info[RTAX_DST] = NULL;
561 R_Malloc(rtm, struct rt_msghdr *, len);
563 info.rti_info[RTAX_DST] = NULL;
566 m_copydata(m, 0, len, (caddr_t)rtm);
567 if (rtm->rtm_version != RTM_VERSION) {
568 info.rti_info[RTAX_DST] = NULL;
569 senderr(EPROTONOSUPPORT);
571 rtm->rtm_pid = curproc->p_pid;
572 bzero(&info, sizeof(info));
573 info.rti_addrs = rtm->rtm_addrs;
574 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
575 info.rti_info[RTAX_DST] = NULL;
578 info.rti_flags = rtm->rtm_flags;
579 if (info.rti_info[RTAX_DST] == NULL ||
580 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
581 (info.rti_info[RTAX_GATEWAY] != NULL &&
582 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
585 * Verify that the caller has the appropriate privilege; RTM_GET
586 * is the only operation the non-superuser is allowed.
588 if (rtm->rtm_type != RTM_GET) {
589 error = priv_check(curthread, PRIV_NET_ROUTE);
595 * The given gateway address may be an interface address.
596 * For example, issuing a "route change" command on a route
597 * entry that was created from a tunnel, and the gateway
598 * address given is the local end point. In this case the
599 * RTF_GATEWAY flag must be cleared or the destination will
600 * not be reachable even though there is no error message.
602 if (info.rti_info[RTAX_GATEWAY] != NULL &&
603 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
606 bzero(&gw_ro, sizeof(gw_ro));
607 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
608 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
610 * A host route through the loopback interface is
611 * installed for each interface adddress. In pre 8.0
612 * releases the interface address of a PPP link type
613 * is not reachable locally. This behavior is fixed as
614 * part of the new L2/L3 redesign and rewrite work. The
615 * signature of this interface address route is the
616 * AF_LINK sa_family type of the rt_gateway, and the
617 * rt_ifp has the IFF_LOOPBACK flag set.
619 if (gw_ro.ro_rt != NULL &&
620 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
621 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
622 info.rti_flags &= ~RTF_GATEWAY;
623 if (gw_ro.ro_rt != NULL)
627 switch (rtm->rtm_type) {
628 struct rtentry *saved_nrt;
631 if (info.rti_info[RTAX_GATEWAY] == NULL)
635 /* support for new ARP code */
636 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
637 (rtm->rtm_flags & RTF_LLDATA) != 0) {
638 error = lla_rt_output(rtm, &info);
641 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
643 if (error == 0 && saved_nrt) {
645 rt_setmetrics(rtm->rtm_inits,
646 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
647 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
648 RT_REMREF(saved_nrt);
649 RT_UNLOCK(saved_nrt);
655 /* support for new ARP code */
656 if (info.rti_info[RTAX_GATEWAY] &&
657 (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_DELETE, &info, &saved_nrt,
674 rnh = rt_tables_get_rnh(so->so_fibnum,
675 info.rti_info[RTAX_DST]->sa_family);
677 senderr(EAFNOSUPPORT);
678 RADIX_NODE_HEAD_RLOCK(rnh);
679 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
680 info.rti_info[RTAX_NETMASK], rnh);
681 if (rt == NULL) { /* XXX looks bogus */
682 RADIX_NODE_HEAD_RUNLOCK(rnh);
687 * for RTM_CHANGE/LOCK, if we got multipath routes,
688 * we require users to specify a matching RTAX_GATEWAY.
690 * for RTM_GET, gate is optional even with multipath.
691 * if gate == NULL the first match is returned.
692 * (no need to call rt_mpath_matchgate if gate == NULL)
694 if (rn_mpath_capable(rnh) &&
695 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
696 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
698 RADIX_NODE_HEAD_RUNLOCK(rnh);
704 * If performing proxied L2 entry insertion, and
705 * the actual PPP host entry is found, perform
706 * another search to retrieve the prefix route of
707 * the local end point of the PPP link.
709 if (rtm->rtm_flags & RTF_ANNOUNCE) {
710 struct sockaddr laddr;
712 if (rt->rt_ifp != NULL &&
713 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
716 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
718 rt_maskedcopy(ifa->ifa_addr,
722 rt_maskedcopy(rt->rt_ifa->ifa_addr,
724 rt->rt_ifa->ifa_netmask);
726 * refactor rt and no lock operation necessary
728 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
730 RADIX_NODE_HEAD_RUNLOCK(rnh);
736 RADIX_NODE_HEAD_RUNLOCK(rnh);
741 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
742 * returns a perfect match in case a netmask is
743 * specified. For host routes only a longest prefix
744 * match is returned so it is necessary to compare the
745 * existence of the netmask. If both have a netmask
746 * rnh_lookup() did a perfect match and if none of them
747 * have a netmask both are host routes which is also a
751 if (rtm->rtm_type != RTM_GET &&
752 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
757 switch(rtm->rtm_type) {
762 if ((rt->rt_flags & RTF_HOST) == 0
763 ? jailed_without_vnet(curthread->td_ucred)
764 : prison_if(curthread->td_ucred,
769 info.rti_info[RTAX_DST] = rt_key(rt);
770 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
771 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
772 info.rti_info[RTAX_GENMASK] = 0;
773 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
776 info.rti_info[RTAX_IFP] =
777 ifp->if_addr->ifa_addr;
778 error = rtm_get_jailed(&info, ifp, rt,
779 &saun, curthread->td_ucred);
784 if (ifp->if_flags & IFF_POINTOPOINT)
785 info.rti_info[RTAX_BRD] =
786 rt->rt_ifa->ifa_dstaddr;
787 rtm->rtm_index = ifp->if_index;
789 info.rti_info[RTAX_IFP] = NULL;
790 info.rti_info[RTAX_IFA] = NULL;
792 } else if ((ifp = rt->rt_ifp) != NULL) {
793 rtm->rtm_index = ifp->if_index;
795 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
796 if (len > rtm->rtm_msglen) {
797 struct rt_msghdr *new_rtm;
798 R_Malloc(new_rtm, struct rt_msghdr *, len);
799 if (new_rtm == NULL) {
803 bcopy(rtm, new_rtm, rtm->rtm_msglen);
804 Free(rtm); rtm = new_rtm;
806 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
807 rtm->rtm_flags = rt->rt_flags;
808 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
809 rtm->rtm_addrs = info.rti_addrs;
814 * New gateway could require new ifaddr, ifp;
815 * flags may also be different; ifp may be specified
816 * by ll sockaddr when protocol address is ambiguous
818 if (((rt->rt_flags & RTF_GATEWAY) &&
819 info.rti_info[RTAX_GATEWAY] != NULL) ||
820 info.rti_info[RTAX_IFP] != NULL ||
821 (info.rti_info[RTAX_IFA] != NULL &&
822 !sa_equal(info.rti_info[RTAX_IFA],
823 rt->rt_ifa->ifa_addr))) {
825 RADIX_NODE_HEAD_LOCK(rnh);
826 error = rt_getifa_fib(&info, rt->rt_fibnum);
828 * XXXRW: Really we should release this
829 * reference later, but this maintains
830 * historical behavior.
832 if (info.rti_ifa != NULL)
833 ifa_free(info.rti_ifa);
834 RADIX_NODE_HEAD_UNLOCK(rnh);
839 if (info.rti_ifa != NULL &&
840 info.rti_ifa != rt->rt_ifa &&
841 rt->rt_ifa != NULL &&
842 rt->rt_ifa->ifa_rtrequest != NULL) {
843 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
845 ifa_free(rt->rt_ifa);
847 if (info.rti_info[RTAX_GATEWAY] != NULL) {
849 RADIX_NODE_HEAD_LOCK(rnh);
852 error = rt_setgate(rt, rt_key(rt),
853 info.rti_info[RTAX_GATEWAY]);
854 RADIX_NODE_HEAD_UNLOCK(rnh);
859 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
861 if (info.rti_ifa != NULL &&
862 info.rti_ifa != rt->rt_ifa) {
863 ifa_ref(info.rti_ifa);
864 rt->rt_ifa = info.rti_ifa;
865 rt->rt_ifp = info.rti_ifp;
867 /* Allow some flags to be toggled on change. */
868 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
869 (rtm->rtm_flags & RTF_FMASK);
870 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
872 rtm->rtm_index = rt->rt_ifp->if_index;
873 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
874 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
877 /* We don't support locks anymore */
890 rtm->rtm_errno = error;
892 rtm->rtm_flags |= RTF_DONE;
894 if (rt) /* XXX can this be true? */
897 struct rawcb *rp = NULL;
899 * Check to see if we don't want our own messages.
901 if ((so->so_options & SO_USELOOPBACK) == 0) {
902 if (route_cb.any_count <= 1) {
908 /* There is another listener, so construct message */
912 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
913 if (m->m_pkthdr.len < rtm->rtm_msglen) {
916 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
917 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
920 M_SETFIB(m, so->so_fibnum);
921 m->m_flags |= RTS_FILTER_FIB;
924 * XXX insure we don't get a copy by
925 * invalidating our protocol
927 unsigned short family = rp->rcb_proto.sp_family;
928 rp->rcb_proto.sp_family = 0;
929 rt_dispatch(m, info.rti_info[RTAX_DST]);
930 rp->rcb_proto.sp_family = family;
932 rt_dispatch(m, info.rti_info[RTAX_DST]);
934 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
943 rt_setmetrics(u_long which, const struct rt_metrics *in,
944 struct rt_metrics_lite *out)
946 #define metric(f, e) if (which & (f)) out->e = in->e;
948 * Only these are stored in the routing entry since introduction
949 * of tcp hostcache. The rest is ignored.
951 metric(RTV_MTU, rmx_mtu);
952 metric(RTV_WEIGHT, rmx_weight);
953 /* Userland -> kernel timebase conversion. */
954 if (which & RTV_EXPIRE)
955 out->rmx_expire = in->rmx_expire ?
956 in->rmx_expire - time_second + time_uptime : 0;
961 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
963 #define metric(e) out->e = in->e;
964 bzero(out, sizeof(*out));
967 /* Kernel -> userland timebase conversion. */
968 out->rmx_expire = in->rmx_expire ?
969 in->rmx_expire - time_uptime + time_second : 0;
974 * Extract the addresses of the passed sockaddrs.
975 * Do a little sanity checking so as to avoid bad memory references.
976 * This data is derived straight from userland.
979 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
984 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
985 if ((rtinfo->rti_addrs & (1 << i)) == 0)
987 sa = (struct sockaddr *)cp;
991 if (cp + sa->sa_len > cplim)
994 * there are no more.. quit now
995 * If there are more bits, they are in error.
996 * I've seen this. route(1) can evidently generate these.
997 * This causes kernel to core dump.
998 * for compatibility, If we see this, point to a safe address.
1000 if (sa->sa_len == 0) {
1001 rtinfo->rti_info[i] = &sa_zero;
1002 return (0); /* should be EINVAL but for compat */
1005 rtinfo->rti_info[i] = sa;
1011 static struct mbuf *
1012 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1014 struct rt_msghdr *rtm;
1017 struct sockaddr *sa;
1024 len = sizeof(struct ifa_msghdr);
1029 len = sizeof(struct ifma_msghdr);
1033 len = sizeof(struct if_msghdr);
1036 case RTM_IFANNOUNCE:
1038 len = sizeof(struct if_announcemsghdr);
1042 len = sizeof(struct rt_msghdr);
1046 m = m_gethdr(M_DONTWAIT, MT_DATA);
1047 if (m && len > MHLEN) {
1048 MCLGET(m, M_DONTWAIT);
1049 if ((m->m_flags & M_EXT) == 0) {
1056 m->m_pkthdr.len = m->m_len = len;
1057 m->m_pkthdr.rcvif = NULL;
1058 rtm = mtod(m, struct rt_msghdr *);
1059 bzero((caddr_t)rtm, len);
1060 for (i = 0; i < RTAX_MAX; i++) {
1061 if ((sa = rtinfo->rti_info[i]) == NULL)
1063 rtinfo->rti_addrs |= (1 << i);
1065 m_copyback(m, len, dlen, (caddr_t)sa);
1068 if (m->m_pkthdr.len != len) {
1072 rtm->rtm_msglen = len;
1073 rtm->rtm_version = RTM_VERSION;
1074 rtm->rtm_type = type;
1079 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1082 int len, dlen, second_time = 0;
1085 rtinfo->rti_addrs = 0;
1091 len = sizeof(struct ifa_msghdr);
1095 #ifdef COMPAT_FREEBSD32
1096 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1097 len = sizeof(struct if_msghdr32);
1101 len = sizeof(struct if_msghdr);
1105 len = sizeof(struct ifma_msghdr);
1109 len = sizeof(struct rt_msghdr);
1114 for (i = 0; i < RTAX_MAX; i++) {
1115 struct sockaddr *sa;
1117 if ((sa = rtinfo->rti_info[i]) == NULL)
1119 rtinfo->rti_addrs |= (1 << i);
1122 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1128 if (cp == NULL && w != NULL && !second_time) {
1129 struct walkarg *rw = w;
1132 if (rw->w_tmemsize < len) {
1134 free(rw->w_tmem, M_RTABLE);
1135 rw->w_tmem = (caddr_t)
1136 malloc(len, M_RTABLE, M_NOWAIT);
1138 rw->w_tmemsize = len;
1148 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1150 rtm->rtm_version = RTM_VERSION;
1151 rtm->rtm_type = type;
1152 rtm->rtm_msglen = len;
1158 * This routine is called to generate a message from the routing
1159 * socket indicating that a redirect has occured, a routing lookup
1160 * has failed, or that a protocol has detected timeouts to a particular
1164 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1167 struct rt_msghdr *rtm;
1169 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1171 if (route_cb.any_count == 0)
1173 m = rt_msg1(type, rtinfo);
1177 if (fibnum != RTS_ALLFIBS) {
1178 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1179 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1180 M_SETFIB(m, fibnum);
1181 m->m_flags |= RTS_FILTER_FIB;
1184 rtm = mtod(m, struct rt_msghdr *);
1185 rtm->rtm_flags = RTF_DONE | flags;
1186 rtm->rtm_errno = error;
1187 rtm->rtm_addrs = rtinfo->rti_addrs;
1192 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1195 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1199 * This routine is called to generate a message from the routing
1200 * socket indicating that the status of a network interface has changed.
1203 rt_ifmsg(struct ifnet *ifp)
1205 struct if_msghdr *ifm;
1207 struct rt_addrinfo info;
1209 if (route_cb.any_count == 0)
1211 bzero((caddr_t)&info, sizeof(info));
1212 m = rt_msg1(RTM_IFINFO, &info);
1215 ifm = mtod(m, struct if_msghdr *);
1216 ifm->ifm_index = ifp->if_index;
1217 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1218 ifm->ifm_data = ifp->if_data;
1220 rt_dispatch(m, NULL);
1224 * This is called to generate messages from the routing socket
1225 * indicating a network interface has had addresses associated with it.
1226 * if we ever reverse the logic and replace messages TO the routing
1227 * socket indicate a request to configure interfaces, then it will
1228 * be unnecessary as the routing socket will automatically generate
1232 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1235 struct rt_addrinfo info;
1236 struct sockaddr *sa = NULL;
1238 struct mbuf *m = NULL;
1239 struct ifnet *ifp = ifa->ifa_ifp;
1241 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1242 ("unexpected cmd %u", cmd));
1243 #if defined(INET) || defined(INET6)
1246 * notify the SCTP stack
1247 * this will only get called when an address is added/deleted
1248 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1250 sctp_addr_change(ifa, cmd);
1253 if (route_cb.any_count == 0)
1255 for (pass = 1; pass < 3; pass++) {
1256 bzero((caddr_t)&info, sizeof(info));
1257 if ((cmd == RTM_ADD && pass == 1) ||
1258 (cmd == RTM_DELETE && pass == 2)) {
1259 struct ifa_msghdr *ifam;
1260 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1262 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1263 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1264 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1265 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1266 if ((m = rt_msg1(ncmd, &info)) == NULL)
1268 ifam = mtod(m, struct ifa_msghdr *);
1269 ifam->ifam_index = ifp->if_index;
1270 ifam->ifam_metric = ifa->ifa_metric;
1271 ifam->ifam_flags = ifa->ifa_flags;
1272 ifam->ifam_addrs = info.rti_addrs;
1274 if ((cmd == RTM_ADD && pass == 2) ||
1275 (cmd == RTM_DELETE && pass == 1)) {
1276 struct rt_msghdr *rtm;
1280 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1281 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1282 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1283 if ((m = rt_msg1(cmd, &info)) == NULL)
1285 rtm = mtod(m, struct rt_msghdr *);
1286 rtm->rtm_index = ifp->if_index;
1287 rtm->rtm_flags |= rt->rt_flags;
1288 rtm->rtm_errno = error;
1289 rtm->rtm_addrs = info.rti_addrs;
1291 if (fibnum != RTS_ALLFIBS) {
1292 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1293 "fibnum out of range 0 <= %d < %d", __func__,
1294 fibnum, rt_numfibs));
1295 M_SETFIB(m, fibnum);
1296 m->m_flags |= RTS_FILTER_FIB;
1303 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1306 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1310 * This is the analogue to the rt_newaddrmsg which performs the same
1311 * function but for multicast group memberhips. This is easier since
1312 * there is no route state to worry about.
1315 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1317 struct rt_addrinfo info;
1318 struct mbuf *m = NULL;
1319 struct ifnet *ifp = ifma->ifma_ifp;
1320 struct ifma_msghdr *ifmam;
1322 if (route_cb.any_count == 0)
1325 bzero((caddr_t)&info, sizeof(info));
1326 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1327 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1329 * If a link-layer address is present, present it as a ``gateway''
1330 * (similarly to how ARP entries, e.g., are presented).
1332 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1333 m = rt_msg1(cmd, &info);
1336 ifmam = mtod(m, struct ifma_msghdr *);
1337 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1339 ifmam->ifmam_index = ifp->if_index;
1340 ifmam->ifmam_addrs = info.rti_addrs;
1341 rt_dispatch(m, ifma->ifma_addr);
1344 static struct mbuf *
1345 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1346 struct rt_addrinfo *info)
1348 struct if_announcemsghdr *ifan;
1351 if (route_cb.any_count == 0)
1353 bzero((caddr_t)info, sizeof(*info));
1354 m = rt_msg1(type, info);
1356 ifan = mtod(m, struct if_announcemsghdr *);
1357 ifan->ifan_index = ifp->if_index;
1358 strlcpy(ifan->ifan_name, ifp->if_xname,
1359 sizeof(ifan->ifan_name));
1360 ifan->ifan_what = what;
1366 * This is called to generate routing socket messages indicating
1367 * IEEE80211 wireless events.
1368 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1371 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1374 struct rt_addrinfo info;
1376 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1379 * Append the ieee80211 data. Try to stick it in the
1380 * mbuf containing the ifannounce msg; otherwise allocate
1381 * a new mbuf and append.
1383 * NB: we assume m is a single mbuf.
1385 if (data_len > M_TRAILINGSPACE(m)) {
1386 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1391 bcopy(data, mtod(n, void *), data_len);
1392 n->m_len = data_len;
1394 } else if (data_len > 0) {
1395 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1396 m->m_len += data_len;
1398 if (m->m_flags & M_PKTHDR)
1399 m->m_pkthdr.len += data_len;
1400 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1401 rt_dispatch(m, NULL);
1406 * This is called to generate routing socket messages indicating
1407 * network interface arrival and departure.
1410 rt_ifannouncemsg(struct ifnet *ifp, int what)
1413 struct rt_addrinfo info;
1415 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1417 rt_dispatch(m, NULL);
1421 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1426 * Preserve the family from the sockaddr, if any, in an m_tag for
1427 * use when injecting the mbuf into the routing socket buffer from
1431 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1437 *(unsigned short *)(tag + 1) = sa->sa_family;
1438 m_tag_prepend(m, tag);
1442 m->m_pkthdr.rcvif = V_loif;
1448 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1452 * This is used in dumping the kernel table via sysctl().
1455 sysctl_dumpentry(struct radix_node *rn, void *vw)
1457 struct walkarg *w = vw;
1458 struct rtentry *rt = (struct rtentry *)rn;
1459 int error = 0, size;
1460 struct rt_addrinfo info;
1462 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1464 if ((rt->rt_flags & RTF_HOST) == 0
1465 ? jailed_without_vnet(w->w_req->td->td_ucred)
1466 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1468 bzero((caddr_t)&info, sizeof(info));
1469 info.rti_info[RTAX_DST] = rt_key(rt);
1470 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1471 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1472 info.rti_info[RTAX_GENMASK] = 0;
1474 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1475 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1476 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1477 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1479 size = rt_msg2(RTM_GET, &info, NULL, w);
1480 if (w->w_req && w->w_tmem) {
1481 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1483 rtm->rtm_flags = rt->rt_flags;
1485 * let's be honest about this being a retarded hack
1487 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1488 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1489 rtm->rtm_index = rt->rt_ifp->if_index;
1490 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1491 rtm->rtm_addrs = info.rti_addrs;
1492 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1498 #ifdef COMPAT_FREEBSD32
1500 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1503 bzero(dst, sizeof(*dst));
1504 CP(*src, *dst, ifi_type);
1505 CP(*src, *dst, ifi_physical);
1506 CP(*src, *dst, ifi_addrlen);
1507 CP(*src, *dst, ifi_hdrlen);
1508 CP(*src, *dst, ifi_link_state);
1509 dst->ifi_datalen = sizeof(struct if_data32);
1510 CP(*src, *dst, ifi_mtu);
1511 CP(*src, *dst, ifi_metric);
1512 CP(*src, *dst, ifi_baudrate);
1513 CP(*src, *dst, ifi_ipackets);
1514 CP(*src, *dst, ifi_ierrors);
1515 CP(*src, *dst, ifi_opackets);
1516 CP(*src, *dst, ifi_oerrors);
1517 CP(*src, *dst, ifi_collisions);
1518 CP(*src, *dst, ifi_ibytes);
1519 CP(*src, *dst, ifi_obytes);
1520 CP(*src, *dst, ifi_imcasts);
1521 CP(*src, *dst, ifi_omcasts);
1522 CP(*src, *dst, ifi_iqdrops);
1523 CP(*src, *dst, ifi_noproto);
1524 CP(*src, *dst, ifi_hwassist);
1525 CP(*src, *dst, ifi_epoch);
1526 TV_CP(*src, *dst, ifi_lastchange);
1531 sysctl_iflist(int af, struct walkarg *w)
1535 struct rt_addrinfo info;
1538 bzero((caddr_t)&info, sizeof(info));
1540 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1541 if (w->w_arg && w->w_arg != ifp->if_index)
1545 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1546 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1547 info.rti_info[RTAX_IFP] = NULL;
1548 if (w->w_req && w->w_tmem) {
1549 struct if_msghdr *ifm;
1551 #ifdef COMPAT_FREEBSD32
1552 if (w->w_req->flags & SCTL_MASK32) {
1553 struct if_msghdr32 *ifm32;
1555 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1556 ifm32->ifm_index = ifp->if_index;
1557 ifm32->ifm_flags = ifp->if_flags |
1559 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1560 ifm32->ifm_addrs = info.rti_addrs;
1561 error = SYSCTL_OUT(w->w_req, (caddr_t)ifm32,
1566 ifm = (struct if_msghdr *)w->w_tmem;
1567 ifm->ifm_index = ifp->if_index;
1568 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1569 ifm->ifm_data = ifp->if_data;
1570 ifm->ifm_addrs = info.rti_addrs;
1571 error = SYSCTL_OUT(w->w_req, (caddr_t)ifm, len);
1572 #ifdef COMPAT_FREEBSD32
1578 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1579 if (af && af != ifa->ifa_addr->sa_family)
1581 if (prison_if(w->w_req->td->td_ucred,
1582 ifa->ifa_addr) != 0)
1584 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1585 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1586 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1587 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1588 if (w->w_req && w->w_tmem) {
1589 struct ifa_msghdr *ifam;
1591 ifam = (struct ifa_msghdr *)w->w_tmem;
1592 ifam->ifam_index = ifa->ifa_ifp->if_index;
1593 ifam->ifam_flags = ifa->ifa_flags;
1594 ifam->ifam_metric = ifa->ifa_metric;
1595 ifam->ifam_addrs = info.rti_addrs;
1596 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1601 IF_ADDR_UNLOCK(ifp);
1602 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1603 info.rti_info[RTAX_BRD] = NULL;
1607 IF_ADDR_UNLOCK(ifp);
1613 sysctl_ifmalist(int af, struct walkarg *w)
1616 struct ifmultiaddr *ifma;
1617 struct rt_addrinfo info;
1621 bzero((caddr_t)&info, sizeof(info));
1623 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1624 if (w->w_arg && w->w_arg != ifp->if_index)
1627 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1629 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1630 if (af && af != ifma->ifma_addr->sa_family)
1632 if (prison_if(w->w_req->td->td_ucred,
1633 ifma->ifma_addr) != 0)
1635 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1636 info.rti_info[RTAX_GATEWAY] =
1637 (ifma->ifma_addr->sa_family != AF_LINK) ?
1638 ifma->ifma_lladdr : NULL;
1639 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1640 if (w->w_req && w->w_tmem) {
1641 struct ifma_msghdr *ifmam;
1643 ifmam = (struct ifma_msghdr *)w->w_tmem;
1644 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1645 ifmam->ifmam_flags = 0;
1646 ifmam->ifmam_addrs = info.rti_addrs;
1647 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1649 IF_ADDR_UNLOCK(ifp);
1654 IF_ADDR_UNLOCK(ifp);
1662 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1664 int *name = (int *)arg1;
1665 u_int namelen = arg2;
1666 struct radix_node_head *rnh = NULL; /* silence compiler. */
1667 int i, lim, error = EINVAL;
1676 return ((namelen < 3) ? EISDIR : ENOTDIR);
1680 bzero(&w, sizeof(w));
1685 error = sysctl_wire_old_buffer(req, 0);
1692 if (af == 0) { /* dump all tables */
1695 } else /* dump only one table */
1699 * take care of llinfo entries, the caller must
1702 if (w.w_op == NET_RT_FLAGS &&
1703 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1705 error = lltable_sysctl_dumparp(af, w.w_req);
1711 * take care of routing entries
1713 for (error = 0; error == 0 && i <= lim; i++) {
1714 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1716 RADIX_NODE_HEAD_LOCK(rnh);
1717 error = rnh->rnh_walktree(rnh,
1718 sysctl_dumpentry, &w);
1719 RADIX_NODE_HEAD_UNLOCK(rnh);
1721 error = EAFNOSUPPORT;
1726 error = sysctl_iflist(af, &w);
1729 case NET_RT_IFMALIST:
1730 error = sysctl_ifmalist(af, &w);
1734 free(w.w_tmem, M_RTABLE);
1738 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1741 * Definitions of protocols supported in the ROUTE domain.
1744 static struct domain routedomain; /* or at least forward */
1746 static struct protosw routesw[] = {
1748 .pr_type = SOCK_RAW,
1749 .pr_domain = &routedomain,
1750 .pr_flags = PR_ATOMIC|PR_ADDR,
1751 .pr_output = route_output,
1752 .pr_ctlinput = raw_ctlinput,
1753 .pr_init = raw_init,
1754 .pr_usrreqs = &route_usrreqs
1758 static struct domain routedomain = {
1759 .dom_family = PF_ROUTE,
1760 .dom_name = "route",
1761 .dom_protosw = routesw,
1762 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1765 VNET_DOMAIN_SET(route);