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 *, sa_family_t);
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;
548 sa_family_t saf = AF_UNSPEC;
550 #define senderr(e) { error = e; goto flush;}
551 if (m == NULL || ((m->m_len < sizeof(long)) &&
552 (m = m_pullup(m, sizeof(long))) == NULL))
554 if ((m->m_flags & M_PKTHDR) == 0)
555 panic("route_output");
556 len = m->m_pkthdr.len;
557 if (len < sizeof(*rtm) ||
558 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
559 info.rti_info[RTAX_DST] = NULL;
562 R_Malloc(rtm, struct rt_msghdr *, len);
564 info.rti_info[RTAX_DST] = NULL;
567 m_copydata(m, 0, len, (caddr_t)rtm);
568 if (rtm->rtm_version != RTM_VERSION) {
569 info.rti_info[RTAX_DST] = NULL;
570 senderr(EPROTONOSUPPORT);
572 rtm->rtm_pid = curproc->p_pid;
573 bzero(&info, sizeof(info));
574 info.rti_addrs = rtm->rtm_addrs;
575 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
576 info.rti_info[RTAX_DST] = NULL;
579 info.rti_flags = rtm->rtm_flags;
580 if (info.rti_info[RTAX_DST] == NULL ||
581 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
582 (info.rti_info[RTAX_GATEWAY] != NULL &&
583 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
585 saf = info.rti_info[RTAX_DST]->sa_family;
587 * Verify that the caller has the appropriate privilege; RTM_GET
588 * is the only operation the non-superuser is allowed.
590 if (rtm->rtm_type != RTM_GET) {
591 error = priv_check(curthread, PRIV_NET_ROUTE);
597 * The given gateway address may be an interface address.
598 * For example, issuing a "route change" command on a route
599 * entry that was created from a tunnel, and the gateway
600 * address given is the local end point. In this case the
601 * RTF_GATEWAY flag must be cleared or the destination will
602 * not be reachable even though there is no error message.
604 if (info.rti_info[RTAX_GATEWAY] != NULL &&
605 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
608 bzero(&gw_ro, sizeof(gw_ro));
609 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
610 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
612 * A host route through the loopback interface is
613 * installed for each interface adddress. In pre 8.0
614 * releases the interface address of a PPP link type
615 * is not reachable locally. This behavior is fixed as
616 * part of the new L2/L3 redesign and rewrite work. The
617 * signature of this interface address route is the
618 * AF_LINK sa_family type of the rt_gateway, and the
619 * rt_ifp has the IFF_LOOPBACK flag set.
621 if (gw_ro.ro_rt != NULL &&
622 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
623 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
624 info.rti_flags &= ~RTF_GATEWAY;
625 if (gw_ro.ro_rt != NULL)
629 switch (rtm->rtm_type) {
630 struct rtentry *saved_nrt;
633 if (info.rti_info[RTAX_GATEWAY] == NULL)
637 /* support for new ARP code */
638 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
639 (rtm->rtm_flags & RTF_LLDATA) != 0) {
640 error = lla_rt_output(rtm, &info);
643 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
645 if (error == 0 && saved_nrt) {
647 rt_setmetrics(rtm->rtm_inits,
648 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
649 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
650 RT_REMREF(saved_nrt);
651 RT_UNLOCK(saved_nrt);
657 /* support for new ARP code */
658 if (info.rti_info[RTAX_GATEWAY] &&
659 (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_DELETE, &info, &saved_nrt,
676 rnh = rt_tables_get_rnh(so->so_fibnum,
677 info.rti_info[RTAX_DST]->sa_family);
679 senderr(EAFNOSUPPORT);
680 RADIX_NODE_HEAD_RLOCK(rnh);
681 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
682 info.rti_info[RTAX_NETMASK], rnh);
683 if (rt == NULL) { /* XXX looks bogus */
684 RADIX_NODE_HEAD_RUNLOCK(rnh);
689 * for RTM_CHANGE/LOCK, if we got multipath routes,
690 * we require users to specify a matching RTAX_GATEWAY.
692 * for RTM_GET, gate is optional even with multipath.
693 * if gate == NULL the first match is returned.
694 * (no need to call rt_mpath_matchgate if gate == NULL)
696 if (rn_mpath_capable(rnh) &&
697 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
698 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
700 RADIX_NODE_HEAD_RUNLOCK(rnh);
706 * If performing proxied L2 entry insertion, and
707 * the actual PPP host entry is found, perform
708 * another search to retrieve the prefix route of
709 * the local end point of the PPP link.
711 if (rtm->rtm_flags & RTF_ANNOUNCE) {
712 struct sockaddr laddr;
714 if (rt->rt_ifp != NULL &&
715 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
718 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
720 rt_maskedcopy(ifa->ifa_addr,
724 rt_maskedcopy(rt->rt_ifa->ifa_addr,
726 rt->rt_ifa->ifa_netmask);
728 * refactor rt and no lock operation necessary
730 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
732 RADIX_NODE_HEAD_RUNLOCK(rnh);
738 RADIX_NODE_HEAD_RUNLOCK(rnh);
743 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
744 * returns a perfect match in case a netmask is
745 * specified. For host routes only a longest prefix
746 * match is returned so it is necessary to compare the
747 * existence of the netmask. If both have a netmask
748 * rnh_lookup() did a perfect match and if none of them
749 * have a netmask both are host routes which is also a
753 if (rtm->rtm_type != RTM_GET &&
754 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
759 switch(rtm->rtm_type) {
764 if ((rt->rt_flags & RTF_HOST) == 0
765 ? jailed_without_vnet(curthread->td_ucred)
766 : prison_if(curthread->td_ucred,
771 info.rti_info[RTAX_DST] = rt_key(rt);
772 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
773 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
774 info.rti_info[RTAX_GENMASK] = 0;
775 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
778 info.rti_info[RTAX_IFP] =
779 ifp->if_addr->ifa_addr;
780 error = rtm_get_jailed(&info, ifp, rt,
781 &saun, curthread->td_ucred);
786 if (ifp->if_flags & IFF_POINTOPOINT)
787 info.rti_info[RTAX_BRD] =
788 rt->rt_ifa->ifa_dstaddr;
789 rtm->rtm_index = ifp->if_index;
791 info.rti_info[RTAX_IFP] = NULL;
792 info.rti_info[RTAX_IFA] = NULL;
794 } else if ((ifp = rt->rt_ifp) != NULL) {
795 rtm->rtm_index = ifp->if_index;
797 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
798 if (len > rtm->rtm_msglen) {
799 struct rt_msghdr *new_rtm;
800 R_Malloc(new_rtm, struct rt_msghdr *, len);
801 if (new_rtm == NULL) {
805 bcopy(rtm, new_rtm, rtm->rtm_msglen);
806 Free(rtm); rtm = new_rtm;
808 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
809 rtm->rtm_flags = rt->rt_flags;
810 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
811 rtm->rtm_addrs = info.rti_addrs;
816 * New gateway could require new ifaddr, ifp;
817 * flags may also be different; ifp may be specified
818 * by ll sockaddr when protocol address is ambiguous
820 if (((rt->rt_flags & RTF_GATEWAY) &&
821 info.rti_info[RTAX_GATEWAY] != NULL) ||
822 info.rti_info[RTAX_IFP] != NULL ||
823 (info.rti_info[RTAX_IFA] != NULL &&
824 !sa_equal(info.rti_info[RTAX_IFA],
825 rt->rt_ifa->ifa_addr))) {
827 RADIX_NODE_HEAD_LOCK(rnh);
828 error = rt_getifa_fib(&info, rt->rt_fibnum);
830 * XXXRW: Really we should release this
831 * reference later, but this maintains
832 * historical behavior.
834 if (info.rti_ifa != NULL)
835 ifa_free(info.rti_ifa);
836 RADIX_NODE_HEAD_UNLOCK(rnh);
841 if (info.rti_ifa != NULL &&
842 info.rti_ifa != rt->rt_ifa &&
843 rt->rt_ifa != NULL &&
844 rt->rt_ifa->ifa_rtrequest != NULL) {
845 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
847 ifa_free(rt->rt_ifa);
849 if (info.rti_info[RTAX_GATEWAY] != NULL) {
851 RADIX_NODE_HEAD_LOCK(rnh);
854 error = rt_setgate(rt, rt_key(rt),
855 info.rti_info[RTAX_GATEWAY]);
856 RADIX_NODE_HEAD_UNLOCK(rnh);
861 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
863 if (info.rti_ifa != NULL &&
864 info.rti_ifa != rt->rt_ifa) {
865 ifa_ref(info.rti_ifa);
866 rt->rt_ifa = info.rti_ifa;
867 rt->rt_ifp = info.rti_ifp;
869 /* Allow some flags to be toggled on change. */
870 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
871 (rtm->rtm_flags & RTF_FMASK);
872 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
874 rtm->rtm_index = rt->rt_ifp->if_index;
875 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
876 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
879 /* We don't support locks anymore */
892 rtm->rtm_errno = error;
894 rtm->rtm_flags |= RTF_DONE;
896 if (rt) /* XXX can this be true? */
899 struct rawcb *rp = NULL;
901 * Check to see if we don't want our own messages.
903 if ((so->so_options & SO_USELOOPBACK) == 0) {
904 if (route_cb.any_count <= 1) {
910 /* There is another listener, so construct message */
914 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
915 if (m->m_pkthdr.len < rtm->rtm_msglen) {
918 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
919 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
922 M_SETFIB(m, so->so_fibnum);
923 m->m_flags |= RTS_FILTER_FIB;
926 * XXX insure we don't get a copy by
927 * invalidating our protocol
929 unsigned short family = rp->rcb_proto.sp_family;
930 rp->rcb_proto.sp_family = 0;
932 rp->rcb_proto.sp_family = family;
936 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
945 rt_setmetrics(u_long which, const struct rt_metrics *in,
946 struct rt_metrics_lite *out)
948 #define metric(f, e) if (which & (f)) out->e = in->e;
950 * Only these are stored in the routing entry since introduction
951 * of tcp hostcache. The rest is ignored.
953 metric(RTV_MTU, rmx_mtu);
954 metric(RTV_WEIGHT, rmx_weight);
955 /* Userland -> kernel timebase conversion. */
956 if (which & RTV_EXPIRE)
957 out->rmx_expire = in->rmx_expire ?
958 in->rmx_expire - time_second + time_uptime : 0;
963 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
965 #define metric(e) out->e = in->e;
966 bzero(out, sizeof(*out));
969 /* Kernel -> userland timebase conversion. */
970 out->rmx_expire = in->rmx_expire ?
971 in->rmx_expire - time_uptime + time_second : 0;
976 * Extract the addresses of the passed sockaddrs.
977 * Do a little sanity checking so as to avoid bad memory references.
978 * This data is derived straight from userland.
981 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
986 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
987 if ((rtinfo->rti_addrs & (1 << i)) == 0)
989 sa = (struct sockaddr *)cp;
993 if (cp + sa->sa_len > cplim)
996 * there are no more.. quit now
997 * If there are more bits, they are in error.
998 * I've seen this. route(1) can evidently generate these.
999 * This causes kernel to core dump.
1000 * for compatibility, If we see this, point to a safe address.
1002 if (sa->sa_len == 0) {
1003 rtinfo->rti_info[i] = &sa_zero;
1004 return (0); /* should be EINVAL but for compat */
1007 rtinfo->rti_info[i] = sa;
1013 static struct mbuf *
1014 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1016 struct rt_msghdr *rtm;
1019 struct sockaddr *sa;
1026 len = sizeof(struct ifa_msghdr);
1031 len = sizeof(struct ifma_msghdr);
1035 len = sizeof(struct if_msghdr);
1038 case RTM_IFANNOUNCE:
1040 len = sizeof(struct if_announcemsghdr);
1044 len = sizeof(struct rt_msghdr);
1048 m = m_gethdr(M_DONTWAIT, MT_DATA);
1049 if (m && len > MHLEN) {
1050 MCLGET(m, M_DONTWAIT);
1051 if ((m->m_flags & M_EXT) == 0) {
1058 m->m_pkthdr.len = m->m_len = len;
1059 m->m_pkthdr.rcvif = NULL;
1060 rtm = mtod(m, struct rt_msghdr *);
1061 bzero((caddr_t)rtm, len);
1062 for (i = 0; i < RTAX_MAX; i++) {
1063 if ((sa = rtinfo->rti_info[i]) == NULL)
1065 rtinfo->rti_addrs |= (1 << i);
1067 m_copyback(m, len, dlen, (caddr_t)sa);
1070 if (m->m_pkthdr.len != len) {
1074 rtm->rtm_msglen = len;
1075 rtm->rtm_version = RTM_VERSION;
1076 rtm->rtm_type = type;
1081 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1084 int len, dlen, second_time = 0;
1087 rtinfo->rti_addrs = 0;
1093 len = sizeof(struct ifa_msghdr);
1097 #ifdef COMPAT_FREEBSD32
1098 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1099 len = sizeof(struct if_msghdr32);
1103 len = sizeof(struct if_msghdr);
1107 len = sizeof(struct ifma_msghdr);
1111 len = sizeof(struct rt_msghdr);
1116 for (i = 0; i < RTAX_MAX; i++) {
1117 struct sockaddr *sa;
1119 if ((sa = rtinfo->rti_info[i]) == NULL)
1121 rtinfo->rti_addrs |= (1 << i);
1124 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1130 if (cp == NULL && w != NULL && !second_time) {
1131 struct walkarg *rw = w;
1134 if (rw->w_tmemsize < len) {
1136 free(rw->w_tmem, M_RTABLE);
1137 rw->w_tmem = (caddr_t)
1138 malloc(len, M_RTABLE, M_NOWAIT);
1140 rw->w_tmemsize = len;
1150 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1152 rtm->rtm_version = RTM_VERSION;
1153 rtm->rtm_type = type;
1154 rtm->rtm_msglen = len;
1160 * This routine is called to generate a message from the routing
1161 * socket indicating that a redirect has occured, a routing lookup
1162 * has failed, or that a protocol has detected timeouts to a particular
1166 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1169 struct rt_msghdr *rtm;
1171 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1173 if (route_cb.any_count == 0)
1175 m = rt_msg1(type, rtinfo);
1179 if (fibnum != RTS_ALLFIBS) {
1180 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1181 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1182 M_SETFIB(m, fibnum);
1183 m->m_flags |= RTS_FILTER_FIB;
1186 rtm = mtod(m, struct rt_msghdr *);
1187 rtm->rtm_flags = RTF_DONE | flags;
1188 rtm->rtm_errno = error;
1189 rtm->rtm_addrs = rtinfo->rti_addrs;
1190 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1194 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1197 rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1201 * This routine is called to generate a message from the routing
1202 * socket indicating that the status of a network interface has changed.
1205 rt_ifmsg(struct ifnet *ifp)
1207 struct if_msghdr *ifm;
1209 struct rt_addrinfo info;
1211 if (route_cb.any_count == 0)
1213 bzero((caddr_t)&info, sizeof(info));
1214 m = rt_msg1(RTM_IFINFO, &info);
1217 ifm = mtod(m, struct if_msghdr *);
1218 ifm->ifm_index = ifp->if_index;
1219 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1220 ifm->ifm_data = ifp->if_data;
1222 rt_dispatch(m, AF_UNSPEC);
1226 * This is called to generate messages from the routing socket
1227 * indicating a network interface has had addresses associated with it.
1228 * if we ever reverse the logic and replace messages TO the routing
1229 * socket indicate a request to configure interfaces, then it will
1230 * be unnecessary as the routing socket will automatically generate
1234 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1237 struct rt_addrinfo info;
1238 struct sockaddr *sa = NULL;
1240 struct mbuf *m = NULL;
1241 struct ifnet *ifp = ifa->ifa_ifp;
1243 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1244 ("unexpected cmd %u", cmd));
1245 #if defined(INET) || defined(INET6)
1248 * notify the SCTP stack
1249 * this will only get called when an address is added/deleted
1250 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1252 sctp_addr_change(ifa, cmd);
1255 if (route_cb.any_count == 0)
1257 for (pass = 1; pass < 3; pass++) {
1258 bzero((caddr_t)&info, sizeof(info));
1259 if ((cmd == RTM_ADD && pass == 1) ||
1260 (cmd == RTM_DELETE && pass == 2)) {
1261 struct ifa_msghdr *ifam;
1262 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1264 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1265 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1266 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1267 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1268 if ((m = rt_msg1(ncmd, &info)) == NULL)
1270 ifam = mtod(m, struct ifa_msghdr *);
1271 ifam->ifam_index = ifp->if_index;
1272 ifam->ifam_metric = ifa->ifa_metric;
1273 ifam->ifam_flags = ifa->ifa_flags;
1274 ifam->ifam_addrs = info.rti_addrs;
1276 if ((cmd == RTM_ADD && pass == 2) ||
1277 (cmd == RTM_DELETE && pass == 1)) {
1278 struct rt_msghdr *rtm;
1282 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1283 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1284 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1285 if ((m = rt_msg1(cmd, &info)) == NULL)
1287 rtm = mtod(m, struct rt_msghdr *);
1288 rtm->rtm_index = ifp->if_index;
1289 rtm->rtm_flags |= rt->rt_flags;
1290 rtm->rtm_errno = error;
1291 rtm->rtm_addrs = info.rti_addrs;
1293 if (fibnum != RTS_ALLFIBS) {
1294 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1295 "fibnum out of range 0 <= %d < %d", __func__,
1296 fibnum, rt_numfibs));
1297 M_SETFIB(m, fibnum);
1298 m->m_flags |= RTS_FILTER_FIB;
1300 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1305 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1308 rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1312 * This is the analogue to the rt_newaddrmsg which performs the same
1313 * function but for multicast group memberhips. This is easier since
1314 * there is no route state to worry about.
1317 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1319 struct rt_addrinfo info;
1320 struct mbuf *m = NULL;
1321 struct ifnet *ifp = ifma->ifma_ifp;
1322 struct ifma_msghdr *ifmam;
1324 if (route_cb.any_count == 0)
1327 bzero((caddr_t)&info, sizeof(info));
1328 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1329 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1331 * If a link-layer address is present, present it as a ``gateway''
1332 * (similarly to how ARP entries, e.g., are presented).
1334 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1335 m = rt_msg1(cmd, &info);
1338 ifmam = mtod(m, struct ifma_msghdr *);
1339 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1341 ifmam->ifmam_index = ifp->if_index;
1342 ifmam->ifmam_addrs = info.rti_addrs;
1343 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1346 static struct mbuf *
1347 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1348 struct rt_addrinfo *info)
1350 struct if_announcemsghdr *ifan;
1353 if (route_cb.any_count == 0)
1355 bzero((caddr_t)info, sizeof(*info));
1356 m = rt_msg1(type, info);
1358 ifan = mtod(m, struct if_announcemsghdr *);
1359 ifan->ifan_index = ifp->if_index;
1360 strlcpy(ifan->ifan_name, ifp->if_xname,
1361 sizeof(ifan->ifan_name));
1362 ifan->ifan_what = what;
1368 * This is called to generate routing socket messages indicating
1369 * IEEE80211 wireless events.
1370 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1373 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1376 struct rt_addrinfo info;
1378 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1381 * Append the ieee80211 data. Try to stick it in the
1382 * mbuf containing the ifannounce msg; otherwise allocate
1383 * a new mbuf and append.
1385 * NB: we assume m is a single mbuf.
1387 if (data_len > M_TRAILINGSPACE(m)) {
1388 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1393 bcopy(data, mtod(n, void *), data_len);
1394 n->m_len = data_len;
1396 } else if (data_len > 0) {
1397 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1398 m->m_len += data_len;
1400 if (m->m_flags & M_PKTHDR)
1401 m->m_pkthdr.len += data_len;
1402 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1403 rt_dispatch(m, AF_UNSPEC);
1408 * This is called to generate routing socket messages indicating
1409 * network interface arrival and departure.
1412 rt_ifannouncemsg(struct ifnet *ifp, int what)
1415 struct rt_addrinfo info;
1417 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1419 rt_dispatch(m, AF_UNSPEC);
1423 rt_dispatch(struct mbuf *m, sa_family_t saf)
1428 * Preserve the family from the sockaddr, if any, in an m_tag for
1429 * use when injecting the mbuf into the routing socket buffer from
1432 if (saf != AF_UNSPEC) {
1433 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1439 *(unsigned short *)(tag + 1) = saf;
1440 m_tag_prepend(m, tag);
1444 m->m_pkthdr.rcvif = V_loif;
1450 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1454 * This is used in dumping the kernel table via sysctl().
1457 sysctl_dumpentry(struct radix_node *rn, void *vw)
1459 struct walkarg *w = vw;
1460 struct rtentry *rt = (struct rtentry *)rn;
1461 int error = 0, size;
1462 struct rt_addrinfo info;
1464 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1466 if ((rt->rt_flags & RTF_HOST) == 0
1467 ? jailed_without_vnet(w->w_req->td->td_ucred)
1468 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1470 bzero((caddr_t)&info, sizeof(info));
1471 info.rti_info[RTAX_DST] = rt_key(rt);
1472 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1473 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1474 info.rti_info[RTAX_GENMASK] = 0;
1476 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1477 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1478 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1479 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1481 size = rt_msg2(RTM_GET, &info, NULL, w);
1482 if (w->w_req && w->w_tmem) {
1483 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1485 rtm->rtm_flags = rt->rt_flags;
1487 * let's be honest about this being a retarded hack
1489 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1490 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1491 rtm->rtm_index = rt->rt_ifp->if_index;
1492 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1493 rtm->rtm_addrs = info.rti_addrs;
1494 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1500 #ifdef COMPAT_FREEBSD32
1502 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1505 bzero(dst, sizeof(*dst));
1506 CP(*src, *dst, ifi_type);
1507 CP(*src, *dst, ifi_physical);
1508 CP(*src, *dst, ifi_addrlen);
1509 CP(*src, *dst, ifi_hdrlen);
1510 CP(*src, *dst, ifi_link_state);
1511 dst->ifi_datalen = sizeof(struct if_data32);
1512 CP(*src, *dst, ifi_mtu);
1513 CP(*src, *dst, ifi_metric);
1514 CP(*src, *dst, ifi_baudrate);
1515 CP(*src, *dst, ifi_ipackets);
1516 CP(*src, *dst, ifi_ierrors);
1517 CP(*src, *dst, ifi_opackets);
1518 CP(*src, *dst, ifi_oerrors);
1519 CP(*src, *dst, ifi_collisions);
1520 CP(*src, *dst, ifi_ibytes);
1521 CP(*src, *dst, ifi_obytes);
1522 CP(*src, *dst, ifi_imcasts);
1523 CP(*src, *dst, ifi_omcasts);
1524 CP(*src, *dst, ifi_iqdrops);
1525 CP(*src, *dst, ifi_noproto);
1526 CP(*src, *dst, ifi_hwassist);
1527 CP(*src, *dst, ifi_epoch);
1528 TV_CP(*src, *dst, ifi_lastchange);
1533 sysctl_iflist(int af, struct walkarg *w)
1537 struct rt_addrinfo info;
1540 bzero((caddr_t)&info, sizeof(info));
1542 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1543 if (w->w_arg && w->w_arg != ifp->if_index)
1547 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1548 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1549 info.rti_info[RTAX_IFP] = NULL;
1550 if (w->w_req && w->w_tmem) {
1551 struct if_msghdr *ifm;
1553 #ifdef COMPAT_FREEBSD32
1554 if (w->w_req->flags & SCTL_MASK32) {
1555 struct if_msghdr32 *ifm32;
1557 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1558 ifm32->ifm_index = ifp->if_index;
1559 ifm32->ifm_flags = ifp->if_flags |
1561 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1562 ifm32->ifm_addrs = info.rti_addrs;
1563 error = SYSCTL_OUT(w->w_req, (caddr_t)ifm32,
1568 ifm = (struct if_msghdr *)w->w_tmem;
1569 ifm->ifm_index = ifp->if_index;
1570 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1571 ifm->ifm_data = ifp->if_data;
1572 ifm->ifm_addrs = info.rti_addrs;
1573 error = SYSCTL_OUT(w->w_req, (caddr_t)ifm, len);
1574 #ifdef COMPAT_FREEBSD32
1580 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1581 if (af && af != ifa->ifa_addr->sa_family)
1583 if (prison_if(w->w_req->td->td_ucred,
1584 ifa->ifa_addr) != 0)
1586 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1587 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1588 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1589 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1590 if (w->w_req && w->w_tmem) {
1591 struct ifa_msghdr *ifam;
1593 ifam = (struct ifa_msghdr *)w->w_tmem;
1594 ifam->ifam_index = ifa->ifa_ifp->if_index;
1595 ifam->ifam_flags = ifa->ifa_flags;
1596 ifam->ifam_metric = ifa->ifa_metric;
1597 ifam->ifam_addrs = info.rti_addrs;
1598 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1603 IF_ADDR_UNLOCK(ifp);
1604 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1605 info.rti_info[RTAX_BRD] = NULL;
1609 IF_ADDR_UNLOCK(ifp);
1615 sysctl_ifmalist(int af, struct walkarg *w)
1618 struct ifmultiaddr *ifma;
1619 struct rt_addrinfo info;
1623 bzero((caddr_t)&info, sizeof(info));
1625 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1626 if (w->w_arg && w->w_arg != ifp->if_index)
1629 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1631 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1632 if (af && af != ifma->ifma_addr->sa_family)
1634 if (prison_if(w->w_req->td->td_ucred,
1635 ifma->ifma_addr) != 0)
1637 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1638 info.rti_info[RTAX_GATEWAY] =
1639 (ifma->ifma_addr->sa_family != AF_LINK) ?
1640 ifma->ifma_lladdr : NULL;
1641 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1642 if (w->w_req && w->w_tmem) {
1643 struct ifma_msghdr *ifmam;
1645 ifmam = (struct ifma_msghdr *)w->w_tmem;
1646 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1647 ifmam->ifmam_flags = 0;
1648 ifmam->ifmam_addrs = info.rti_addrs;
1649 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1651 IF_ADDR_UNLOCK(ifp);
1656 IF_ADDR_UNLOCK(ifp);
1664 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1666 int *name = (int *)arg1;
1667 u_int namelen = arg2;
1668 struct radix_node_head *rnh = NULL; /* silence compiler. */
1669 int i, lim, error = EINVAL;
1678 return ((namelen < 3) ? EISDIR : ENOTDIR);
1682 bzero(&w, sizeof(w));
1687 error = sysctl_wire_old_buffer(req, 0);
1694 if (af == 0) { /* dump all tables */
1697 } else /* dump only one table */
1701 * take care of llinfo entries, the caller must
1704 if (w.w_op == NET_RT_FLAGS &&
1705 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1707 error = lltable_sysctl_dumparp(af, w.w_req);
1713 * take care of routing entries
1715 for (error = 0; error == 0 && i <= lim; i++) {
1716 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1718 RADIX_NODE_HEAD_LOCK(rnh);
1719 error = rnh->rnh_walktree(rnh,
1720 sysctl_dumpentry, &w);
1721 RADIX_NODE_HEAD_UNLOCK(rnh);
1723 error = EAFNOSUPPORT;
1728 error = sysctl_iflist(af, &w);
1731 case NET_RT_IFMALIST:
1732 error = sysctl_ifmalist(af, &w);
1736 free(w.w_tmem, M_RTABLE);
1740 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1743 * Definitions of protocols supported in the ROUTE domain.
1746 static struct domain routedomain; /* or at least forward */
1748 static struct protosw routesw[] = {
1750 .pr_type = SOCK_RAW,
1751 .pr_domain = &routedomain,
1752 .pr_flags = PR_ATOMIC|PR_ADDR,
1753 .pr_output = route_output,
1754 .pr_ctlinput = raw_ctlinput,
1755 .pr_init = raw_init,
1756 .pr_usrreqs = &route_usrreqs
1760 static struct domain routedomain = {
1761 .dom_family = PF_ROUTE,
1762 .dom_name = "route",
1763 .dom_protosw = routesw,
1764 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1767 VNET_DOMAIN_SET(route);