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>
66 #include <netinet/ip_carp.h>
68 #include <netinet6/scope6_var.h>
71 #if defined(INET) || defined(INET6)
73 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
77 #ifdef COMPAT_FREEBSD32
78 #include <sys/mount.h>
79 #include <compat/freebsd32/freebsd32.h>
86 uint8_t ifi_link_state;
88 uint8_t ifi_spare_char2;
92 uint32_t ifi_baudrate;
93 uint32_t ifi_ipackets;
95 uint32_t ifi_opackets;
97 uint32_t ifi_collisions;
100 uint32_t ifi_imcasts;
101 uint32_t ifi_omcasts;
102 uint32_t ifi_iqdrops;
103 uint32_t ifi_noproto;
104 uint32_t ifi_hwassist;
106 struct timeval32 ifi_lastchange;
116 struct if_data32 ifm_data;
119 struct if_msghdrl32 {
126 uint16_t _ifm_spare1;
128 uint16_t ifm_data_off;
129 struct if_data32 ifm_data;
132 struct ifa_msghdrl32 {
133 uint16_t ifam_msglen;
134 uint8_t ifam_version;
139 uint16_t _ifam_spare1;
141 uint16_t ifam_data_off;
143 struct if_data32 ifam_data;
145 #endif /* COMPAT_FREEBSD32 */
147 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
149 /* NB: these are not modified */
150 static struct sockaddr route_src = { 2, PF_ROUTE, };
151 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
153 /* These are external hooks for CARP. */
154 int (*carp_get_vhid_p)(struct ifaddr *);
157 * Used by rtsock/raw_input callback code to decide whether to filter the update
158 * notification to a socket bound to a particular FIB.
160 #define RTS_FILTER_FIB M_PROTO8
161 #define RTS_ALLFIBS -1
164 int ip_count; /* attached w/ AF_INET */
165 int ip6_count; /* attached w/ AF_INET6 */
166 int ipx_count; /* attached w/ AF_IPX */
167 int any_count; /* total attached */
170 struct mtx rtsock_mtx;
171 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
173 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
174 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
175 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
177 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
183 struct sysctl_req *w_req;
186 static void rts_input(struct mbuf *m);
187 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
188 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
189 caddr_t cp, struct walkarg *w);
190 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
191 struct rt_addrinfo *rtinfo);
192 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
193 static int sysctl_iflist(int af, struct walkarg *w);
194 static int sysctl_ifmalist(int af, struct walkarg *w);
195 static int route_output(struct mbuf *m, struct socket *so);
196 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
197 struct rt_metrics_lite *out);
198 static void rt_getmetrics(const struct rt_metrics_lite *in,
199 struct rt_metrics *out);
200 static void rt_dispatch(struct mbuf *, sa_family_t);
202 static struct netisr_handler rtsock_nh = {
204 .nh_handler = rts_input,
205 .nh_proto = NETISR_ROUTE,
206 .nh_policy = NETISR_POLICY_SOURCE,
210 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
214 netisr_getqlimit(&rtsock_nh, &qlimit);
215 error = sysctl_handle_int(oidp, &qlimit, 0, req);
216 if (error || !req->newptr)
220 return (netisr_setqlimit(&rtsock_nh, qlimit));
222 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
223 0, 0, sysctl_route_netisr_maxqlen, "I",
224 "maximum routing socket dispatch queue length");
231 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
232 rtsock_nh.nh_qlimit = tmp;
233 netisr_register(&rtsock_nh);
235 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
238 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
243 KASSERT(m != NULL, ("%s: m is NULL", __func__));
244 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
245 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
247 /* No filtering requested. */
248 if ((m->m_flags & RTS_FILTER_FIB) == 0)
251 /* Check if it is a rts and the fib matches the one of the socket. */
252 fibnum = M_GETFIB(m);
253 if (proto->sp_family != PF_ROUTE ||
254 rp->rcb_socket == NULL ||
255 rp->rcb_socket->so_fibnum == fibnum)
258 /* Filtering requested and no match, the socket shall be skipped. */
263 rts_input(struct mbuf *m)
265 struct sockproto route_proto;
266 unsigned short *family;
269 route_proto.sp_family = PF_ROUTE;
270 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
272 family = (unsigned short *)(tag + 1);
273 route_proto.sp_protocol = *family;
274 m_tag_delete(m, tag);
276 route_proto.sp_protocol = 0;
278 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
282 * It really doesn't make any sense at all for this code to share much
283 * with raw_usrreq.c, since its functionality is so restricted. XXX
286 rts_abort(struct socket *so)
289 raw_usrreqs.pru_abort(so);
293 rts_close(struct socket *so)
296 raw_usrreqs.pru_close(so);
299 /* pru_accept is EOPNOTSUPP */
302 rts_attach(struct socket *so, int proto, struct thread *td)
307 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
310 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
314 so->so_pcb = (caddr_t)rp;
315 so->so_fibnum = td->td_proc->p_fibnum;
316 error = raw_attach(so, proto);
324 switch(rp->rcb_proto.sp_protocol) {
329 route_cb.ip6_count++;
332 route_cb.ipx_count++;
335 route_cb.any_count++;
338 so->so_options |= SO_USELOOPBACK;
343 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
346 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
350 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
353 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
356 /* pru_connect2 is EOPNOTSUPP */
357 /* pru_control is EOPNOTSUPP */
360 rts_detach(struct socket *so)
362 struct rawcb *rp = sotorawcb(so);
364 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
367 switch(rp->rcb_proto.sp_protocol) {
372 route_cb.ip6_count--;
375 route_cb.ipx_count--;
378 route_cb.any_count--;
380 raw_usrreqs.pru_detach(so);
384 rts_disconnect(struct socket *so)
387 return (raw_usrreqs.pru_disconnect(so));
390 /* pru_listen is EOPNOTSUPP */
393 rts_peeraddr(struct socket *so, struct sockaddr **nam)
396 return (raw_usrreqs.pru_peeraddr(so, nam));
399 /* pru_rcvd is EOPNOTSUPP */
400 /* pru_rcvoob is EOPNOTSUPP */
403 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
404 struct mbuf *control, struct thread *td)
407 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
410 /* pru_sense is null */
413 rts_shutdown(struct socket *so)
416 return (raw_usrreqs.pru_shutdown(so));
420 rts_sockaddr(struct socket *so, struct sockaddr **nam)
423 return (raw_usrreqs.pru_sockaddr(so, nam));
426 static struct pr_usrreqs route_usrreqs = {
427 .pru_abort = rts_abort,
428 .pru_attach = rts_attach,
429 .pru_bind = rts_bind,
430 .pru_connect = rts_connect,
431 .pru_detach = rts_detach,
432 .pru_disconnect = rts_disconnect,
433 .pru_peeraddr = rts_peeraddr,
434 .pru_send = rts_send,
435 .pru_shutdown = rts_shutdown,
436 .pru_sockaddr = rts_sockaddr,
437 .pru_close = rts_close,
440 #ifndef _SOCKADDR_UNION_DEFINED
441 #define _SOCKADDR_UNION_DEFINED
443 * The union of all possible address formats we handle.
445 union sockaddr_union {
447 struct sockaddr_in sin;
448 struct sockaddr_in6 sin6;
450 #endif /* _SOCKADDR_UNION_DEFINED */
453 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
454 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
457 /* First, see if the returned address is part of the jail. */
458 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
459 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
463 switch (info->rti_info[RTAX_DST]->sa_family) {
473 * Try to find an address on the given outgoing interface
474 * that belongs to the jail.
477 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
480 if (sa->sa_family != AF_INET)
482 ia = ((struct sockaddr_in *)sa)->sin_addr;
483 if (prison_check_ip4(cred, &ia) == 0) {
488 IF_ADDR_RUNLOCK(ifp);
491 * As a last resort return the 'default' jail address.
493 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
495 if (prison_get_ip4(cred, &ia) != 0)
498 bzero(&saun->sin, sizeof(struct sockaddr_in));
499 saun->sin.sin_len = sizeof(struct sockaddr_in);
500 saun->sin.sin_family = AF_INET;
501 saun->sin.sin_addr.s_addr = ia.s_addr;
502 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
515 * Try to find an address on the given outgoing interface
516 * that belongs to the jail.
519 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
522 if (sa->sa_family != AF_INET6)
524 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
525 &ia6, sizeof(struct in6_addr));
526 if (prison_check_ip6(cred, &ia6) == 0) {
531 IF_ADDR_RUNLOCK(ifp);
534 * As a last resort return the 'default' jail address.
536 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
538 if (prison_get_ip6(cred, &ia6) != 0)
541 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
542 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
543 saun->sin6.sin6_family = AF_INET6;
544 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
545 if (sa6_recoverscope(&saun->sin6) != 0)
547 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
559 route_output(struct mbuf *m, struct socket *so)
561 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
562 struct rt_msghdr *rtm = NULL;
563 struct rtentry *rt = NULL;
564 struct radix_node_head *rnh;
565 struct rt_addrinfo info;
567 struct ifnet *ifp = NULL;
568 union sockaddr_union saun;
569 sa_family_t saf = AF_UNSPEC;
571 #define senderr(e) { error = e; goto flush;}
572 if (m == NULL || ((m->m_len < sizeof(long)) &&
573 (m = m_pullup(m, sizeof(long))) == NULL))
575 if ((m->m_flags & M_PKTHDR) == 0)
576 panic("route_output");
577 len = m->m_pkthdr.len;
578 if (len < sizeof(*rtm) ||
579 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
580 info.rti_info[RTAX_DST] = NULL;
583 R_Malloc(rtm, struct rt_msghdr *, len);
585 info.rti_info[RTAX_DST] = NULL;
588 m_copydata(m, 0, len, (caddr_t)rtm);
589 if (rtm->rtm_version != RTM_VERSION) {
590 info.rti_info[RTAX_DST] = NULL;
591 senderr(EPROTONOSUPPORT);
593 rtm->rtm_pid = curproc->p_pid;
594 bzero(&info, sizeof(info));
595 info.rti_addrs = rtm->rtm_addrs;
596 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
597 info.rti_info[RTAX_DST] = NULL;
600 info.rti_flags = rtm->rtm_flags;
601 if (info.rti_info[RTAX_DST] == NULL ||
602 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
603 (info.rti_info[RTAX_GATEWAY] != NULL &&
604 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
606 saf = info.rti_info[RTAX_DST]->sa_family;
608 * Verify that the caller has the appropriate privilege; RTM_GET
609 * is the only operation the non-superuser is allowed.
611 if (rtm->rtm_type != RTM_GET) {
612 error = priv_check(curthread, PRIV_NET_ROUTE);
618 * The given gateway address may be an interface address.
619 * For example, issuing a "route change" command on a route
620 * entry that was created from a tunnel, and the gateway
621 * address given is the local end point. In this case the
622 * RTF_GATEWAY flag must be cleared or the destination will
623 * not be reachable even though there is no error message.
625 if (info.rti_info[RTAX_GATEWAY] != NULL &&
626 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
629 bzero(&gw_ro, sizeof(gw_ro));
630 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
631 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
633 * A host route through the loopback interface is
634 * installed for each interface adddress. In pre 8.0
635 * releases the interface address of a PPP link type
636 * is not reachable locally. This behavior is fixed as
637 * part of the new L2/L3 redesign and rewrite work. The
638 * signature of this interface address route is the
639 * AF_LINK sa_family type of the rt_gateway, and the
640 * rt_ifp has the IFF_LOOPBACK flag set.
642 if (gw_ro.ro_rt != NULL &&
643 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
644 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
645 info.rti_flags &= ~RTF_GATEWAY;
646 if (gw_ro.ro_rt != NULL)
650 switch (rtm->rtm_type) {
651 struct rtentry *saved_nrt;
654 if (info.rti_info[RTAX_GATEWAY] == NULL)
658 /* support for new ARP code */
659 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
660 (rtm->rtm_flags & RTF_LLDATA) != 0) {
661 error = lla_rt_output(rtm, &info);
664 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
666 if (error == 0 && saved_nrt) {
668 rt_setmetrics(rtm->rtm_inits,
669 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
670 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
671 RT_REMREF(saved_nrt);
672 RT_UNLOCK(saved_nrt);
678 /* support for new ARP code */
679 if (info.rti_info[RTAX_GATEWAY] &&
680 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
681 (rtm->rtm_flags & RTF_LLDATA) != 0) {
682 error = lla_rt_output(rtm, &info);
685 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
697 rnh = rt_tables_get_rnh(so->so_fibnum,
698 info.rti_info[RTAX_DST]->sa_family);
700 senderr(EAFNOSUPPORT);
701 RADIX_NODE_HEAD_RLOCK(rnh);
702 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
703 info.rti_info[RTAX_NETMASK], rnh);
704 if (rt == NULL) { /* XXX looks bogus */
705 RADIX_NODE_HEAD_RUNLOCK(rnh);
710 * for RTM_CHANGE/LOCK, if we got multipath routes,
711 * we require users to specify a matching RTAX_GATEWAY.
713 * for RTM_GET, gate is optional even with multipath.
714 * if gate == NULL the first match is returned.
715 * (no need to call rt_mpath_matchgate if gate == NULL)
717 if (rn_mpath_capable(rnh) &&
718 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
719 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
721 RADIX_NODE_HEAD_RUNLOCK(rnh);
727 * If performing proxied L2 entry insertion, and
728 * the actual PPP host entry is found, perform
729 * another search to retrieve the prefix route of
730 * the local end point of the PPP link.
732 if (rtm->rtm_flags & RTF_ANNOUNCE) {
733 struct sockaddr laddr;
735 if (rt->rt_ifp != NULL &&
736 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
739 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
741 rt_maskedcopy(ifa->ifa_addr,
745 rt_maskedcopy(rt->rt_ifa->ifa_addr,
747 rt->rt_ifa->ifa_netmask);
749 * refactor rt and no lock operation necessary
751 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
753 RADIX_NODE_HEAD_RUNLOCK(rnh);
759 RADIX_NODE_HEAD_RUNLOCK(rnh);
764 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
765 * returns a perfect match in case a netmask is
766 * specified. For host routes only a longest prefix
767 * match is returned so it is necessary to compare the
768 * existence of the netmask. If both have a netmask
769 * rnh_lookup() did a perfect match and if none of them
770 * have a netmask both are host routes which is also a
774 if (rtm->rtm_type != RTM_GET &&
775 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
780 switch(rtm->rtm_type) {
785 if ((rt->rt_flags & RTF_HOST) == 0
786 ? jailed_without_vnet(curthread->td_ucred)
787 : prison_if(curthread->td_ucred,
792 info.rti_info[RTAX_DST] = rt_key(rt);
793 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
794 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
795 info.rti_info[RTAX_GENMASK] = 0;
796 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
799 info.rti_info[RTAX_IFP] =
800 ifp->if_addr->ifa_addr;
801 error = rtm_get_jailed(&info, ifp, rt,
802 &saun, curthread->td_ucred);
807 if (ifp->if_flags & IFF_POINTOPOINT)
808 info.rti_info[RTAX_BRD] =
809 rt->rt_ifa->ifa_dstaddr;
810 rtm->rtm_index = ifp->if_index;
812 info.rti_info[RTAX_IFP] = NULL;
813 info.rti_info[RTAX_IFA] = NULL;
815 } else if ((ifp = rt->rt_ifp) != NULL) {
816 rtm->rtm_index = ifp->if_index;
818 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
819 if (len > rtm->rtm_msglen) {
820 struct rt_msghdr *new_rtm;
821 R_Malloc(new_rtm, struct rt_msghdr *, len);
822 if (new_rtm == NULL) {
826 bcopy(rtm, new_rtm, rtm->rtm_msglen);
827 Free(rtm); rtm = new_rtm;
829 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
830 rtm->rtm_flags = rt->rt_flags;
831 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
832 rtm->rtm_addrs = info.rti_addrs;
837 * New gateway could require new ifaddr, ifp;
838 * flags may also be different; ifp may be specified
839 * by ll sockaddr when protocol address is ambiguous
841 if (((rt->rt_flags & RTF_GATEWAY) &&
842 info.rti_info[RTAX_GATEWAY] != NULL) ||
843 info.rti_info[RTAX_IFP] != NULL ||
844 (info.rti_info[RTAX_IFA] != NULL &&
845 !sa_equal(info.rti_info[RTAX_IFA],
846 rt->rt_ifa->ifa_addr))) {
848 RADIX_NODE_HEAD_LOCK(rnh);
849 error = rt_getifa_fib(&info, rt->rt_fibnum);
851 * XXXRW: Really we should release this
852 * reference later, but this maintains
853 * historical behavior.
855 if (info.rti_ifa != NULL)
856 ifa_free(info.rti_ifa);
857 RADIX_NODE_HEAD_UNLOCK(rnh);
862 if (info.rti_ifa != NULL &&
863 info.rti_ifa != rt->rt_ifa &&
864 rt->rt_ifa != NULL &&
865 rt->rt_ifa->ifa_rtrequest != NULL) {
866 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
868 ifa_free(rt->rt_ifa);
870 if (info.rti_info[RTAX_GATEWAY] != NULL) {
872 RADIX_NODE_HEAD_LOCK(rnh);
875 error = rt_setgate(rt, rt_key(rt),
876 info.rti_info[RTAX_GATEWAY]);
877 RADIX_NODE_HEAD_UNLOCK(rnh);
882 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
884 if (info.rti_ifa != NULL &&
885 info.rti_ifa != rt->rt_ifa) {
886 ifa_ref(info.rti_ifa);
887 rt->rt_ifa = info.rti_ifa;
888 rt->rt_ifp = info.rti_ifp;
890 /* Allow some flags to be toggled on change. */
891 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
892 (rtm->rtm_flags & RTF_FMASK);
893 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
895 rtm->rtm_index = rt->rt_ifp->if_index;
896 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
897 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
900 /* We don't support locks anymore */
913 rtm->rtm_errno = error;
915 rtm->rtm_flags |= RTF_DONE;
917 if (rt) /* XXX can this be true? */
920 struct rawcb *rp = NULL;
922 * Check to see if we don't want our own messages.
924 if ((so->so_options & SO_USELOOPBACK) == 0) {
925 if (route_cb.any_count <= 1) {
931 /* There is another listener, so construct message */
935 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
936 if (m->m_pkthdr.len < rtm->rtm_msglen) {
939 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
940 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
943 M_SETFIB(m, so->so_fibnum);
944 m->m_flags |= RTS_FILTER_FIB;
947 * XXX insure we don't get a copy by
948 * invalidating our protocol
950 unsigned short family = rp->rcb_proto.sp_family;
951 rp->rcb_proto.sp_family = 0;
953 rp->rcb_proto.sp_family = family;
957 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
966 rt_setmetrics(u_long which, const struct rt_metrics *in,
967 struct rt_metrics_lite *out)
969 #define metric(f, e) if (which & (f)) out->e = in->e;
971 * Only these are stored in the routing entry since introduction
972 * of tcp hostcache. The rest is ignored.
974 metric(RTV_MTU, rmx_mtu);
975 metric(RTV_WEIGHT, rmx_weight);
976 /* Userland -> kernel timebase conversion. */
977 if (which & RTV_EXPIRE)
978 out->rmx_expire = in->rmx_expire ?
979 in->rmx_expire - time_second + time_uptime : 0;
984 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
986 #define metric(e) out->e = in->e;
987 bzero(out, sizeof(*out));
990 /* Kernel -> userland timebase conversion. */
991 out->rmx_expire = in->rmx_expire ?
992 in->rmx_expire - time_uptime + time_second : 0;
997 * Extract the addresses of the passed sockaddrs.
998 * Do a little sanity checking so as to avoid bad memory references.
999 * This data is derived straight from userland.
1002 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1004 struct sockaddr *sa;
1007 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1008 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1010 sa = (struct sockaddr *)cp;
1014 if (cp + sa->sa_len > cplim)
1017 * there are no more.. quit now
1018 * If there are more bits, they are in error.
1019 * I've seen this. route(1) can evidently generate these.
1020 * This causes kernel to core dump.
1021 * for compatibility, If we see this, point to a safe address.
1023 if (sa->sa_len == 0) {
1024 rtinfo->rti_info[i] = &sa_zero;
1025 return (0); /* should be EINVAL but for compat */
1028 rtinfo->rti_info[i] = sa;
1035 * Used by the routing socket.
1037 static struct mbuf *
1038 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1040 struct rt_msghdr *rtm;
1043 struct sockaddr *sa;
1050 len = sizeof(struct ifa_msghdr);
1055 len = sizeof(struct ifma_msghdr);
1059 len = sizeof(struct if_msghdr);
1062 case RTM_IFANNOUNCE:
1064 len = sizeof(struct if_announcemsghdr);
1068 len = sizeof(struct rt_msghdr);
1072 m = m_gethdr(M_DONTWAIT, MT_DATA);
1073 if (m && len > MHLEN) {
1074 MCLGET(m, M_DONTWAIT);
1075 if ((m->m_flags & M_EXT) == 0) {
1082 m->m_pkthdr.len = m->m_len = len;
1083 m->m_pkthdr.rcvif = NULL;
1084 rtm = mtod(m, struct rt_msghdr *);
1085 bzero((caddr_t)rtm, len);
1086 for (i = 0; i < RTAX_MAX; i++) {
1087 if ((sa = rtinfo->rti_info[i]) == NULL)
1089 rtinfo->rti_addrs |= (1 << i);
1091 m_copyback(m, len, dlen, (caddr_t)sa);
1094 if (m->m_pkthdr.len != len) {
1098 rtm->rtm_msglen = len;
1099 rtm->rtm_version = RTM_VERSION;
1100 rtm->rtm_type = type;
1105 * Used by the sysctl code and routing socket.
1108 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1111 int len, dlen, second_time = 0;
1114 rtinfo->rti_addrs = 0;
1120 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1121 #ifdef COMPAT_FREEBSD32
1122 if (w->w_req->flags & SCTL_MASK32)
1123 len = sizeof(struct ifa_msghdrl32);
1126 len = sizeof(struct ifa_msghdrl);
1128 len = sizeof(struct ifa_msghdr);
1132 #ifdef COMPAT_FREEBSD32
1133 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1134 if (w->w_op == NET_RT_IFLISTL)
1135 len = sizeof(struct if_msghdrl32);
1137 len = sizeof(struct if_msghdr32);
1141 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1142 len = sizeof(struct if_msghdrl);
1144 len = sizeof(struct if_msghdr);
1148 len = sizeof(struct ifma_msghdr);
1152 len = sizeof(struct rt_msghdr);
1157 for (i = 0; i < RTAX_MAX; i++) {
1158 struct sockaddr *sa;
1160 if ((sa = rtinfo->rti_info[i]) == NULL)
1162 rtinfo->rti_addrs |= (1 << i);
1165 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1171 if (cp == NULL && w != NULL && !second_time) {
1172 struct walkarg *rw = w;
1175 if (rw->w_tmemsize < len) {
1177 free(rw->w_tmem, M_RTABLE);
1178 rw->w_tmem = (caddr_t)
1179 malloc(len, M_RTABLE, M_NOWAIT);
1181 rw->w_tmemsize = len;
1191 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1193 rtm->rtm_version = RTM_VERSION;
1194 rtm->rtm_type = type;
1195 rtm->rtm_msglen = len;
1201 * This routine is called to generate a message from the routing
1202 * socket indicating that a redirect has occured, a routing lookup
1203 * has failed, or that a protocol has detected timeouts to a particular
1207 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1210 struct rt_msghdr *rtm;
1212 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1214 if (route_cb.any_count == 0)
1216 m = rt_msg1(type, rtinfo);
1220 if (fibnum != RTS_ALLFIBS) {
1221 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1222 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1223 M_SETFIB(m, fibnum);
1224 m->m_flags |= RTS_FILTER_FIB;
1227 rtm = mtod(m, struct rt_msghdr *);
1228 rtm->rtm_flags = RTF_DONE | flags;
1229 rtm->rtm_errno = error;
1230 rtm->rtm_addrs = rtinfo->rti_addrs;
1231 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1235 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1238 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1242 * This routine is called to generate a message from the routing
1243 * socket indicating that the status of a network interface has changed.
1246 rt_ifmsg(struct ifnet *ifp)
1248 struct if_msghdr *ifm;
1250 struct rt_addrinfo info;
1252 if (route_cb.any_count == 0)
1254 bzero((caddr_t)&info, sizeof(info));
1255 m = rt_msg1(RTM_IFINFO, &info);
1258 ifm = mtod(m, struct if_msghdr *);
1259 ifm->ifm_index = ifp->if_index;
1260 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1261 ifm->ifm_data = ifp->if_data;
1263 rt_dispatch(m, AF_UNSPEC);
1267 * This is called to generate messages from the routing socket
1268 * indicating a network interface has had addresses associated with it.
1269 * if we ever reverse the logic and replace messages TO the routing
1270 * socket indicate a request to configure interfaces, then it will
1271 * be unnecessary as the routing socket will automatically generate
1275 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1278 struct rt_addrinfo info;
1279 struct sockaddr *sa = NULL;
1281 struct mbuf *m = NULL;
1282 struct ifnet *ifp = ifa->ifa_ifp;
1284 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1285 ("unexpected cmd %u", cmd));
1286 #if defined(INET) || defined(INET6)
1289 * notify the SCTP stack
1290 * this will only get called when an address is added/deleted
1291 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1293 sctp_addr_change(ifa, cmd);
1296 if (route_cb.any_count == 0)
1298 for (pass = 1; pass < 3; pass++) {
1299 bzero((caddr_t)&info, sizeof(info));
1300 if ((cmd == RTM_ADD && pass == 1) ||
1301 (cmd == RTM_DELETE && pass == 2)) {
1302 struct ifa_msghdr *ifam;
1303 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1305 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1306 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1307 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1308 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1309 if ((m = rt_msg1(ncmd, &info)) == NULL)
1311 ifam = mtod(m, struct ifa_msghdr *);
1312 ifam->ifam_index = ifp->if_index;
1313 ifam->ifam_metric = ifa->ifa_metric;
1314 ifam->ifam_flags = ifa->ifa_flags;
1315 ifam->ifam_addrs = info.rti_addrs;
1317 if ((cmd == RTM_ADD && pass == 2) ||
1318 (cmd == RTM_DELETE && pass == 1)) {
1319 struct rt_msghdr *rtm;
1323 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1324 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1325 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1326 if ((m = rt_msg1(cmd, &info)) == NULL)
1328 rtm = mtod(m, struct rt_msghdr *);
1329 rtm->rtm_index = ifp->if_index;
1330 rtm->rtm_flags |= rt->rt_flags;
1331 rtm->rtm_errno = error;
1332 rtm->rtm_addrs = info.rti_addrs;
1334 if (fibnum != RTS_ALLFIBS) {
1335 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1336 "fibnum out of range 0 <= %d < %d", __func__,
1337 fibnum, rt_numfibs));
1338 M_SETFIB(m, fibnum);
1339 m->m_flags |= RTS_FILTER_FIB;
1341 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1346 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1349 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1353 * This is the analogue to the rt_newaddrmsg which performs the same
1354 * function but for multicast group memberhips. This is easier since
1355 * there is no route state to worry about.
1358 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1360 struct rt_addrinfo info;
1361 struct mbuf *m = NULL;
1362 struct ifnet *ifp = ifma->ifma_ifp;
1363 struct ifma_msghdr *ifmam;
1365 if (route_cb.any_count == 0)
1368 bzero((caddr_t)&info, sizeof(info));
1369 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1370 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1372 * If a link-layer address is present, present it as a ``gateway''
1373 * (similarly to how ARP entries, e.g., are presented).
1375 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1376 m = rt_msg1(cmd, &info);
1379 ifmam = mtod(m, struct ifma_msghdr *);
1380 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1382 ifmam->ifmam_index = ifp->if_index;
1383 ifmam->ifmam_addrs = info.rti_addrs;
1384 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1387 static struct mbuf *
1388 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1389 struct rt_addrinfo *info)
1391 struct if_announcemsghdr *ifan;
1394 if (route_cb.any_count == 0)
1396 bzero((caddr_t)info, sizeof(*info));
1397 m = rt_msg1(type, info);
1399 ifan = mtod(m, struct if_announcemsghdr *);
1400 ifan->ifan_index = ifp->if_index;
1401 strlcpy(ifan->ifan_name, ifp->if_xname,
1402 sizeof(ifan->ifan_name));
1403 ifan->ifan_what = what;
1409 * This is called to generate routing socket messages indicating
1410 * IEEE80211 wireless events.
1411 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1414 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1417 struct rt_addrinfo info;
1419 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1422 * Append the ieee80211 data. Try to stick it in the
1423 * mbuf containing the ifannounce msg; otherwise allocate
1424 * a new mbuf and append.
1426 * NB: we assume m is a single mbuf.
1428 if (data_len > M_TRAILINGSPACE(m)) {
1429 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1434 bcopy(data, mtod(n, void *), data_len);
1435 n->m_len = data_len;
1437 } else if (data_len > 0) {
1438 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1439 m->m_len += data_len;
1441 if (m->m_flags & M_PKTHDR)
1442 m->m_pkthdr.len += data_len;
1443 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1444 rt_dispatch(m, AF_UNSPEC);
1449 * This is called to generate routing socket messages indicating
1450 * network interface arrival and departure.
1453 rt_ifannouncemsg(struct ifnet *ifp, int what)
1456 struct rt_addrinfo info;
1458 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1460 rt_dispatch(m, AF_UNSPEC);
1464 rt_dispatch(struct mbuf *m, sa_family_t saf)
1469 * Preserve the family from the sockaddr, if any, in an m_tag for
1470 * use when injecting the mbuf into the routing socket buffer from
1473 if (saf != AF_UNSPEC) {
1474 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1480 *(unsigned short *)(tag + 1) = saf;
1481 m_tag_prepend(m, tag);
1485 m->m_pkthdr.rcvif = V_loif;
1491 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1495 * This is used in dumping the kernel table via sysctl().
1498 sysctl_dumpentry(struct radix_node *rn, void *vw)
1500 struct walkarg *w = vw;
1501 struct rtentry *rt = (struct rtentry *)rn;
1502 int error = 0, size;
1503 struct rt_addrinfo info;
1505 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1507 if ((rt->rt_flags & RTF_HOST) == 0
1508 ? jailed_without_vnet(w->w_req->td->td_ucred)
1509 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1511 bzero((caddr_t)&info, sizeof(info));
1512 info.rti_info[RTAX_DST] = rt_key(rt);
1513 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1514 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1515 info.rti_info[RTAX_GENMASK] = 0;
1517 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1518 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1519 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1520 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1522 size = rt_msg2(RTM_GET, &info, NULL, w);
1523 if (w->w_req && w->w_tmem) {
1524 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1526 rtm->rtm_flags = rt->rt_flags;
1528 * let's be honest about this being a retarded hack
1530 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1531 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1532 rtm->rtm_index = rt->rt_ifp->if_index;
1533 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1534 rtm->rtm_addrs = info.rti_addrs;
1535 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1541 #ifdef COMPAT_FREEBSD32
1543 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1546 bzero(dst, sizeof(*dst));
1547 CP(*src, *dst, ifi_type);
1548 CP(*src, *dst, ifi_physical);
1549 CP(*src, *dst, ifi_addrlen);
1550 CP(*src, *dst, ifi_hdrlen);
1551 CP(*src, *dst, ifi_link_state);
1552 CP(*src, *dst, ifi_vhid);
1553 dst->ifi_datalen = sizeof(struct if_data32);
1554 CP(*src, *dst, ifi_mtu);
1555 CP(*src, *dst, ifi_metric);
1556 CP(*src, *dst, ifi_baudrate);
1557 CP(*src, *dst, ifi_ipackets);
1558 CP(*src, *dst, ifi_ierrors);
1559 CP(*src, *dst, ifi_opackets);
1560 CP(*src, *dst, ifi_oerrors);
1561 CP(*src, *dst, ifi_collisions);
1562 CP(*src, *dst, ifi_ibytes);
1563 CP(*src, *dst, ifi_obytes);
1564 CP(*src, *dst, ifi_imcasts);
1565 CP(*src, *dst, ifi_omcasts);
1566 CP(*src, *dst, ifi_iqdrops);
1567 CP(*src, *dst, ifi_noproto);
1568 CP(*src, *dst, ifi_hwassist);
1569 CP(*src, *dst, ifi_epoch);
1570 TV_CP(*src, *dst, ifi_lastchange);
1575 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1576 struct walkarg *w, int len)
1578 struct if_msghdrl *ifm;
1580 #ifdef COMPAT_FREEBSD32
1581 if (w->w_req->flags & SCTL_MASK32) {
1582 struct if_msghdrl32 *ifm32;
1584 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1585 ifm32->ifm_addrs = info->rti_addrs;
1586 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1587 ifm32->ifm_index = ifp->if_index;
1588 ifm32->_ifm_spare1 = 0;
1589 ifm32->ifm_len = sizeof(*ifm32);
1590 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1592 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1593 /* Fixup if_data carp(4) vhid. */
1594 if (carp_get_vhid_p != NULL)
1595 ifm32->ifm_data.ifi_vhid =
1596 (*carp_get_vhid_p)(ifp->if_addr);
1598 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1601 ifm = (struct if_msghdrl *)w->w_tmem;
1602 ifm->ifm_addrs = info->rti_addrs;
1603 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1604 ifm->ifm_index = ifp->if_index;
1605 ifm->_ifm_spare1 = 0;
1606 ifm->ifm_len = sizeof(*ifm);
1607 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1609 ifm->ifm_data = ifp->if_data;
1610 /* Fixup if_data carp(4) vhid. */
1611 if (carp_get_vhid_p != NULL)
1612 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1614 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1618 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1619 struct walkarg *w, int len)
1621 struct if_msghdr *ifm;
1623 #ifdef COMPAT_FREEBSD32
1624 if (w->w_req->flags & SCTL_MASK32) {
1625 struct if_msghdr32 *ifm32;
1627 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1628 ifm32->ifm_addrs = info->rti_addrs;
1629 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1630 ifm32->ifm_index = ifp->if_index;
1632 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1633 /* Fixup if_data carp(4) vhid. */
1634 if (carp_get_vhid_p != NULL)
1635 ifm32->ifm_data.ifi_vhid =
1636 (*carp_get_vhid_p)(ifp->if_addr);
1638 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1641 ifm = (struct if_msghdr *)w->w_tmem;
1642 ifm->ifm_addrs = info->rti_addrs;
1643 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1644 ifm->ifm_index = ifp->if_index;
1646 ifm->ifm_data = ifp->if_data;
1647 /* Fixup if_data carp(4) vhid. */
1648 if (carp_get_vhid_p != NULL)
1649 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1651 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1655 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1656 struct walkarg *w, int len)
1658 struct ifa_msghdrl *ifam;
1660 #ifdef COMPAT_FREEBSD32
1661 if (w->w_req->flags & SCTL_MASK32) {
1662 struct ifa_msghdrl32 *ifam32;
1664 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1665 ifam32->ifam_addrs = info->rti_addrs;
1666 ifam32->ifam_flags = ifa->ifa_flags;
1667 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1668 ifam32->_ifam_spare1 = 0;
1669 ifam32->ifam_len = sizeof(*ifam32);
1670 ifam32->ifam_data_off =
1671 offsetof(struct ifa_msghdrl32, ifam_data);
1672 ifam32->ifam_metric = ifa->ifa_metric;
1674 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1675 /* Fixup if_data carp(4) vhid. */
1676 if (carp_get_vhid_p != NULL)
1677 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1679 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1683 ifam = (struct ifa_msghdrl *)w->w_tmem;
1684 ifam->ifam_addrs = info->rti_addrs;
1685 ifam->ifam_flags = ifa->ifa_flags;
1686 ifam->ifam_index = ifa->ifa_ifp->if_index;
1687 ifam->_ifam_spare1 = 0;
1688 ifam->ifam_len = sizeof(*ifam);
1689 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1690 ifam->ifam_metric = ifa->ifa_metric;
1692 ifam->ifam_data = ifa->if_data;
1693 /* Fixup if_data carp(4) vhid. */
1694 if (carp_get_vhid_p != NULL)
1695 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1697 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1701 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1702 struct walkarg *w, int len)
1704 struct ifa_msghdr *ifam;
1706 ifam = (struct ifa_msghdr *)w->w_tmem;
1707 ifam->ifam_addrs = info->rti_addrs;
1708 ifam->ifam_flags = ifa->ifa_flags;
1709 ifam->ifam_index = ifa->ifa_ifp->if_index;
1710 ifam->ifam_metric = ifa->ifa_metric;
1712 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1716 sysctl_iflist(int af, struct walkarg *w)
1720 struct rt_addrinfo info;
1723 bzero((caddr_t)&info, sizeof(info));
1725 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1726 if (w->w_arg && w->w_arg != ifp->if_index)
1730 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1731 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1732 info.rti_info[RTAX_IFP] = NULL;
1733 if (w->w_req && w->w_tmem) {
1734 if (w->w_op == NET_RT_IFLISTL)
1735 error = sysctl_iflist_ifml(ifp, &info, w, len);
1737 error = sysctl_iflist_ifm(ifp, &info, w, len);
1741 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1742 if (af && af != ifa->ifa_addr->sa_family)
1744 if (prison_if(w->w_req->td->td_ucred,
1745 ifa->ifa_addr) != 0)
1747 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1748 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1749 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1750 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1751 if (w->w_req && w->w_tmem) {
1752 if (w->w_op == NET_RT_IFLISTL)
1753 error = sysctl_iflist_ifaml(ifa, &info,
1756 error = sysctl_iflist_ifam(ifa, &info,
1762 IF_ADDR_RUNLOCK(ifp);
1763 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1764 info.rti_info[RTAX_BRD] = NULL;
1768 IF_ADDR_RUNLOCK(ifp);
1774 sysctl_ifmalist(int af, struct walkarg *w)
1777 struct ifmultiaddr *ifma;
1778 struct rt_addrinfo info;
1782 bzero((caddr_t)&info, sizeof(info));
1784 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1785 if (w->w_arg && w->w_arg != ifp->if_index)
1788 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1790 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1791 if (af && af != ifma->ifma_addr->sa_family)
1793 if (prison_if(w->w_req->td->td_ucred,
1794 ifma->ifma_addr) != 0)
1796 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1797 info.rti_info[RTAX_GATEWAY] =
1798 (ifma->ifma_addr->sa_family != AF_LINK) ?
1799 ifma->ifma_lladdr : NULL;
1800 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1801 if (w->w_req && w->w_tmem) {
1802 struct ifma_msghdr *ifmam;
1804 ifmam = (struct ifma_msghdr *)w->w_tmem;
1805 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1806 ifmam->ifmam_flags = 0;
1807 ifmam->ifmam_addrs = info.rti_addrs;
1808 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1810 IF_ADDR_RUNLOCK(ifp);
1815 IF_ADDR_RUNLOCK(ifp);
1823 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1825 int *name = (int *)arg1;
1826 u_int namelen = arg2;
1827 struct radix_node_head *rnh = NULL; /* silence compiler. */
1828 int i, lim, error = EINVAL;
1837 return ((namelen < 3) ? EISDIR : ENOTDIR);
1841 bzero(&w, sizeof(w));
1846 error = sysctl_wire_old_buffer(req, 0);
1853 if (af == 0) { /* dump all tables */
1856 } else /* dump only one table */
1860 * take care of llinfo entries, the caller must
1863 if (w.w_op == NET_RT_FLAGS &&
1864 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1866 error = lltable_sysctl_dumparp(af, w.w_req);
1872 * take care of routing entries
1874 for (error = 0; error == 0 && i <= lim; i++) {
1875 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1877 RADIX_NODE_HEAD_RLOCK(rnh);
1878 error = rnh->rnh_walktree(rnh,
1879 sysctl_dumpentry, &w);
1880 RADIX_NODE_HEAD_RUNLOCK(rnh);
1882 error = EAFNOSUPPORT;
1887 case NET_RT_IFLISTL:
1888 error = sysctl_iflist(af, &w);
1891 case NET_RT_IFMALIST:
1892 error = sysctl_ifmalist(af, &w);
1896 free(w.w_tmem, M_RTABLE);
1900 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1903 * Definitions of protocols supported in the ROUTE domain.
1906 static struct domain routedomain; /* or at least forward */
1908 static struct protosw routesw[] = {
1910 .pr_type = SOCK_RAW,
1911 .pr_domain = &routedomain,
1912 .pr_flags = PR_ATOMIC|PR_ADDR,
1913 .pr_output = route_output,
1914 .pr_ctlinput = raw_ctlinput,
1915 .pr_init = raw_init,
1916 .pr_usrreqs = &route_usrreqs
1920 static struct domain routedomain = {
1921 .dom_family = PF_ROUTE,
1922 .dom_name = "route",
1923 .dom_protosw = routesw,
1924 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1927 VNET_DOMAIN_SET(route);