2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
33 #include <sys/param.h>
34 #include <sys/domain.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
41 #include <sys/protosw.h>
42 #include <sys/signalvar.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
49 #include <net/netisr.h>
50 #include <net/raw_cb.h>
51 #include <net/route.h>
53 #include <netinet/in.h>
56 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
59 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
61 /* NB: these are not modified */
62 static struct sockaddr route_dst = { 2, PF_ROUTE, };
63 static struct sockaddr route_src = { 2, PF_ROUTE, };
64 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
67 int ip_count; /* attached w/ AF_INET */
68 int ip6_count; /* attached w/ AF_INET6 */
69 int ipx_count; /* attached w/ AF_IPX */
70 int any_count; /* total attached */
73 struct mtx rtsock_mtx;
74 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
76 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
77 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
78 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
80 static struct ifqueue rtsintrq;
82 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
83 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
84 &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
90 struct sysctl_req *w_req;
93 static void rts_input(struct mbuf *m);
94 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
95 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
96 caddr_t cp, struct walkarg *w);
97 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
98 struct rt_addrinfo *rtinfo);
99 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
100 static int sysctl_iflist(int af, struct walkarg *w);
101 static int sysctl_ifmalist(int af, struct walkarg *w);
102 static int route_output(struct mbuf *m, struct socket *so);
103 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
104 struct rt_metrics_lite *out);
105 static void rt_getmetrics(const struct rt_metrics_lite *in,
106 struct rt_metrics *out);
107 static void rt_dispatch(struct mbuf *, const struct sockaddr *);
114 rtsintrq.ifq_maxlen = 256;
115 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
116 rtsintrq.ifq_maxlen = tmp;
117 mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
118 netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE);
120 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0)
123 rts_input(struct mbuf *m)
125 struct sockproto route_proto;
126 unsigned short *family;
129 route_proto.sp_family = PF_ROUTE;
130 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
132 family = (unsigned short *)(tag + 1);
133 route_proto.sp_protocol = *family;
134 m_tag_delete(m, tag);
136 route_proto.sp_protocol = 0;
138 raw_input(m, &route_proto, &route_src, &route_dst);
142 * It really doesn't make any sense at all for this code to share much
143 * with raw_usrreq.c, since its functionality is so restricted. XXX
146 rts_abort(struct socket *so)
149 raw_usrreqs.pru_abort(so);
153 rts_close(struct socket *so)
156 raw_usrreqs.pru_close(so);
159 /* pru_accept is EOPNOTSUPP */
162 rts_attach(struct socket *so, int proto, struct thread *td)
167 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
170 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
175 * The splnet() is necessary to block protocols from sending
176 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
177 * this PCB is extant but incompletely initialized.
178 * Probably we should try to do more of this work beforehand and
182 so->so_pcb = (caddr_t)rp;
183 error = raw_attach(so, proto);
192 switch(rp->rcb_proto.sp_protocol) {
197 route_cb.ip6_count++;
200 route_cb.ipx_count++;
203 rp->rcb_faddr = &route_src;
204 route_cb.any_count++;
207 so->so_options |= SO_USELOOPBACK;
213 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
216 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
220 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
223 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
226 /* pru_connect2 is EOPNOTSUPP */
227 /* pru_control is EOPNOTSUPP */
230 rts_detach(struct socket *so)
232 struct rawcb *rp = sotorawcb(so);
234 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
237 switch(rp->rcb_proto.sp_protocol) {
242 route_cb.ip6_count--;
245 route_cb.ipx_count--;
248 route_cb.any_count--;
250 raw_usrreqs.pru_detach(so);
254 rts_disconnect(struct socket *so)
257 return (raw_usrreqs.pru_disconnect(so));
260 /* pru_listen is EOPNOTSUPP */
263 rts_peeraddr(struct socket *so, struct sockaddr **nam)
266 return (raw_usrreqs.pru_peeraddr(so, nam));
269 /* pru_rcvd is EOPNOTSUPP */
270 /* pru_rcvoob is EOPNOTSUPP */
273 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
274 struct mbuf *control, struct thread *td)
277 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
280 /* pru_sense is null */
283 rts_shutdown(struct socket *so)
286 return (raw_usrreqs.pru_shutdown(so));
290 rts_sockaddr(struct socket *so, struct sockaddr **nam)
293 return (raw_usrreqs.pru_sockaddr(so, nam));
296 static struct pr_usrreqs route_usrreqs = {
297 .pru_abort = rts_abort,
298 .pru_attach = rts_attach,
299 .pru_bind = rts_bind,
300 .pru_connect = rts_connect,
301 .pru_detach = rts_detach,
302 .pru_disconnect = rts_disconnect,
303 .pru_peeraddr = rts_peeraddr,
304 .pru_send = rts_send,
305 .pru_shutdown = rts_shutdown,
306 .pru_sockaddr = rts_sockaddr,
307 .pru_close = rts_close,
312 route_output(struct mbuf *m, struct socket *so)
314 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
315 struct rt_msghdr *rtm = NULL;
316 struct rtentry *rt = NULL;
317 struct radix_node_head *rnh;
318 struct rt_addrinfo info;
320 struct ifnet *ifp = NULL;
321 struct sockaddr_in jail;
323 #define senderr(e) { error = e; goto flush;}
324 if (m == NULL || ((m->m_len < sizeof(long)) &&
325 (m = m_pullup(m, sizeof(long))) == NULL))
327 if ((m->m_flags & M_PKTHDR) == 0)
328 panic("route_output");
329 len = m->m_pkthdr.len;
330 if (len < sizeof(*rtm) ||
331 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
332 info.rti_info[RTAX_DST] = NULL;
335 R_Malloc(rtm, struct rt_msghdr *, len);
337 info.rti_info[RTAX_DST] = NULL;
340 m_copydata(m, 0, len, (caddr_t)rtm);
341 if (rtm->rtm_version != RTM_VERSION) {
342 info.rti_info[RTAX_DST] = NULL;
343 senderr(EPROTONOSUPPORT);
345 rtm->rtm_pid = curproc->p_pid;
346 bzero(&info, sizeof(info));
347 info.rti_addrs = rtm->rtm_addrs;
348 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
349 info.rti_info[RTAX_DST] = NULL;
352 info.rti_flags = rtm->rtm_flags;
353 if (info.rti_info[RTAX_DST] == NULL ||
354 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
355 (info.rti_info[RTAX_GATEWAY] != NULL &&
356 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
358 if (info.rti_info[RTAX_GENMASK]) {
359 struct radix_node *t;
360 t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
362 bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1,
363 (char *)(void *)t->rn_key + 1,
364 ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0)
365 info.rti_info[RTAX_GENMASK] =
366 (struct sockaddr *)t->rn_key;
372 * Verify that the caller has the appropriate privilege; RTM_GET
373 * is the only operation the non-superuser is allowed.
375 if (rtm->rtm_type != RTM_GET) {
376 error = priv_check(curthread, PRIV_NET_ROUTE);
381 switch (rtm->rtm_type) {
382 struct rtentry *saved_nrt;
385 if (info.rti_info[RTAX_GATEWAY] == NULL)
388 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
389 if (error == 0 && saved_nrt) {
391 rt_setmetrics(rtm->rtm_inits,
392 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
393 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
394 RT_REMREF(saved_nrt);
395 saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
396 RT_UNLOCK(saved_nrt);
402 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
413 rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
415 senderr(EAFNOSUPPORT);
416 RADIX_NODE_HEAD_LOCK(rnh);
417 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
418 info.rti_info[RTAX_NETMASK], rnh);
419 if (rt == NULL) { /* XXX looks bogus */
420 RADIX_NODE_HEAD_UNLOCK(rnh);
425 RADIX_NODE_HEAD_UNLOCK(rnh);
430 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
431 * returns a perfect match in case a netmask is
432 * specified. For host routes only a longest prefix
433 * match is returned so it is necessary to compare the
434 * existence of the netmask. If both have a netmask
435 * rnh_lookup() did a perfect match and if none of them
436 * have a netmask both are host routes which is also a
440 if (rtm->rtm_type != RTM_GET &&
441 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
446 switch(rtm->rtm_type) {
451 info.rti_info[RTAX_DST] = rt_key(rt);
452 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
453 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
454 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
455 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
458 info.rti_info[RTAX_IFP] =
459 ifp->if_addr->ifa_addr;
460 if (jailed(so->so_cred)) {
461 bzero(&jail, sizeof(jail));
462 jail.sin_family = PF_INET;
463 jail.sin_len = sizeof(jail);
464 jail.sin_addr.s_addr =
465 htonl(prison_getip(so->so_cred));
466 info.rti_info[RTAX_IFA] =
467 (struct sockaddr *)&jail;
469 info.rti_info[RTAX_IFA] =
470 rt->rt_ifa->ifa_addr;
471 if (ifp->if_flags & IFF_POINTOPOINT)
472 info.rti_info[RTAX_BRD] =
473 rt->rt_ifa->ifa_dstaddr;
474 rtm->rtm_index = ifp->if_index;
476 info.rti_info[RTAX_IFP] = NULL;
477 info.rti_info[RTAX_IFA] = NULL;
479 } else if ((ifp = rt->rt_ifp) != NULL) {
480 rtm->rtm_index = ifp->if_index;
482 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
483 if (len > rtm->rtm_msglen) {
484 struct rt_msghdr *new_rtm;
485 R_Malloc(new_rtm, struct rt_msghdr *, len);
486 if (new_rtm == NULL) {
490 bcopy(rtm, new_rtm, rtm->rtm_msglen);
491 Free(rtm); rtm = new_rtm;
493 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
494 rtm->rtm_flags = rt->rt_flags;
496 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
497 rtm->rtm_addrs = info.rti_addrs;
502 * New gateway could require new ifaddr, ifp;
503 * flags may also be different; ifp may be specified
504 * by ll sockaddr when protocol address is ambiguous
506 if (((rt->rt_flags & RTF_GATEWAY) &&
507 info.rti_info[RTAX_GATEWAY] != NULL) ||
508 info.rti_info[RTAX_IFP] != NULL ||
509 (info.rti_info[RTAX_IFA] != NULL &&
510 !sa_equal(info.rti_info[RTAX_IFA],
511 rt->rt_ifa->ifa_addr))) {
513 if ((error = rt_getifa(&info)) != 0)
517 if (info.rti_ifa != NULL &&
518 info.rti_ifa != rt->rt_ifa &&
519 rt->rt_ifa != NULL &&
520 rt->rt_ifa->ifa_rtrequest != NULL) {
521 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
525 if (info.rti_info[RTAX_GATEWAY] != NULL) {
526 if ((error = rt_setgate(rt, rt_key(rt),
527 info.rti_info[RTAX_GATEWAY])) != 0) {
531 rt->rt_flags |= RTF_GATEWAY;
533 if (info.rti_ifa != NULL &&
534 info.rti_ifa != rt->rt_ifa) {
535 IFAREF(info.rti_ifa);
536 rt->rt_ifa = info.rti_ifa;
537 rt->rt_ifp = info.rti_ifp;
539 /* Allow some flags to be toggled on change. */
540 if (rtm->rtm_fmask & RTF_FMASK)
541 rt->rt_flags = (rt->rt_flags &
543 (rtm->rtm_flags & rtm->rtm_fmask);
544 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
546 rtm->rtm_index = rt->rt_ifp->if_index;
547 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
548 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
549 if (info.rti_info[RTAX_GENMASK])
550 rt->rt_genmask = info.rti_info[RTAX_GENMASK];
553 /* We don't support locks anymore */
566 rtm->rtm_errno = error;
568 rtm->rtm_flags |= RTF_DONE;
570 if (rt) /* XXX can this be true? */
573 struct rawcb *rp = NULL;
575 * Check to see if we don't want our own messages.
577 if ((so->so_options & SO_USELOOPBACK) == 0) {
578 if (route_cb.any_count <= 1) {
584 /* There is another listener, so construct message */
588 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
589 if (m->m_pkthdr.len < rtm->rtm_msglen) {
592 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
593 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
599 * XXX insure we don't get a copy by
600 * invalidating our protocol
602 unsigned short family = rp->rcb_proto.sp_family;
603 rp->rcb_proto.sp_family = 0;
604 rt_dispatch(m, info.rti_info[RTAX_DST]);
605 rp->rcb_proto.sp_family = family;
607 rt_dispatch(m, info.rti_info[RTAX_DST]);
615 rt_setmetrics(u_long which, const struct rt_metrics *in,
616 struct rt_metrics_lite *out)
618 #define metric(f, e) if (which & (f)) out->e = in->e;
620 * Only these are stored in the routing entry since introduction
621 * of tcp hostcache. The rest is ignored.
623 metric(RTV_MTU, rmx_mtu);
624 /* Userland -> kernel timebase conversion. */
625 if (which & RTV_EXPIRE)
626 out->rmx_expire = in->rmx_expire ?
627 in->rmx_expire - time_second + time_uptime : 0;
632 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
634 #define metric(e) out->e = in->e;
635 bzero(out, sizeof(*out));
637 /* Kernel -> userland timebase conversion. */
638 out->rmx_expire = in->rmx_expire ?
639 in->rmx_expire - time_uptime + time_second : 0;
644 * Extract the addresses of the passed sockaddrs.
645 * Do a little sanity checking so as to avoid bad memory references.
646 * This data is derived straight from userland.
649 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
654 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
655 if ((rtinfo->rti_addrs & (1 << i)) == 0)
657 sa = (struct sockaddr *)cp;
661 if (cp + sa->sa_len > cplim)
664 * there are no more.. quit now
665 * If there are more bits, they are in error.
666 * I've seen this. route(1) can evidently generate these.
667 * This causes kernel to core dump.
668 * for compatibility, If we see this, point to a safe address.
670 if (sa->sa_len == 0) {
671 rtinfo->rti_info[i] = &sa_zero;
672 return (0); /* should be EINVAL but for compat */
675 rtinfo->rti_info[i] = sa;
682 rt_msg1(int type, struct rt_addrinfo *rtinfo)
684 struct rt_msghdr *rtm;
694 len = sizeof(struct ifa_msghdr);
699 len = sizeof(struct ifma_msghdr);
703 len = sizeof(struct if_msghdr);
708 len = sizeof(struct if_announcemsghdr);
712 len = sizeof(struct rt_msghdr);
716 m = m_gethdr(M_DONTWAIT, MT_DATA);
717 if (m && len > MHLEN) {
718 MCLGET(m, M_DONTWAIT);
719 if ((m->m_flags & M_EXT) == 0) {
726 m->m_pkthdr.len = m->m_len = len;
727 m->m_pkthdr.rcvif = NULL;
728 rtm = mtod(m, struct rt_msghdr *);
729 bzero((caddr_t)rtm, len);
730 for (i = 0; i < RTAX_MAX; i++) {
731 if ((sa = rtinfo->rti_info[i]) == NULL)
733 rtinfo->rti_addrs |= (1 << i);
735 m_copyback(m, len, dlen, (caddr_t)sa);
738 if (m->m_pkthdr.len != len) {
742 rtm->rtm_msglen = len;
743 rtm->rtm_version = RTM_VERSION;
744 rtm->rtm_type = type;
749 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
752 int len, dlen, second_time = 0;
755 rtinfo->rti_addrs = 0;
761 len = sizeof(struct ifa_msghdr);
765 len = sizeof(struct if_msghdr);
769 len = sizeof(struct ifma_msghdr);
773 len = sizeof(struct rt_msghdr);
778 for (i = 0; i < RTAX_MAX; i++) {
781 if ((sa = rtinfo->rti_info[i]) == NULL)
783 rtinfo->rti_addrs |= (1 << i);
786 bcopy((caddr_t)sa, cp, (unsigned)dlen);
792 if (cp == NULL && w != NULL && !second_time) {
793 struct walkarg *rw = w;
796 if (rw->w_tmemsize < len) {
798 free(rw->w_tmem, M_RTABLE);
799 rw->w_tmem = (caddr_t)
800 malloc(len, M_RTABLE, M_NOWAIT);
802 rw->w_tmemsize = len;
812 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
814 rtm->rtm_version = RTM_VERSION;
815 rtm->rtm_type = type;
816 rtm->rtm_msglen = len;
822 * This routine is called to generate a message from the routing
823 * socket indicating that a redirect has occured, a routing lookup
824 * has failed, or that a protocol has detected timeouts to a particular
828 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
830 struct rt_msghdr *rtm;
832 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
834 if (route_cb.any_count == 0)
836 m = rt_msg1(type, rtinfo);
839 rtm = mtod(m, struct rt_msghdr *);
840 rtm->rtm_flags = RTF_DONE | flags;
841 rtm->rtm_errno = error;
842 rtm->rtm_addrs = rtinfo->rti_addrs;
847 * This routine is called to generate a message from the routing
848 * socket indicating that the status of a network interface has changed.
851 rt_ifmsg(struct ifnet *ifp)
853 struct if_msghdr *ifm;
855 struct rt_addrinfo info;
857 if (route_cb.any_count == 0)
859 bzero((caddr_t)&info, sizeof(info));
860 m = rt_msg1(RTM_IFINFO, &info);
863 ifm = mtod(m, struct if_msghdr *);
864 ifm->ifm_index = ifp->if_index;
865 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
866 ifm->ifm_data = ifp->if_data;
868 rt_dispatch(m, NULL);
872 * This is called to generate messages from the routing socket
873 * indicating a network interface has had addresses associated with it.
874 * if we ever reverse the logic and replace messages TO the routing
875 * socket indicate a request to configure interfaces, then it will
876 * be unnecessary as the routing socket will automatically generate
880 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
882 struct rt_addrinfo info;
883 struct sockaddr *sa = NULL;
885 struct mbuf *m = NULL;
886 struct ifnet *ifp = ifa->ifa_ifp;
888 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
889 ("unexpected cmd %u", cmd));
892 * notify the SCTP stack
893 * this will only get called when an address is added/deleted
894 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
896 sctp_addr_change(ifa, cmd);
898 if (route_cb.any_count == 0)
900 for (pass = 1; pass < 3; pass++) {
901 bzero((caddr_t)&info, sizeof(info));
902 if ((cmd == RTM_ADD && pass == 1) ||
903 (cmd == RTM_DELETE && pass == 2)) {
904 struct ifa_msghdr *ifam;
905 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
907 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
908 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
909 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
910 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
911 if ((m = rt_msg1(ncmd, &info)) == NULL)
913 ifam = mtod(m, struct ifa_msghdr *);
914 ifam->ifam_index = ifp->if_index;
915 ifam->ifam_metric = ifa->ifa_metric;
916 ifam->ifam_flags = ifa->ifa_flags;
917 ifam->ifam_addrs = info.rti_addrs;
919 if ((cmd == RTM_ADD && pass == 2) ||
920 (cmd == RTM_DELETE && pass == 1)) {
921 struct rt_msghdr *rtm;
925 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
926 info.rti_info[RTAX_DST] = sa = rt_key(rt);
927 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
928 if ((m = rt_msg1(cmd, &info)) == NULL)
930 rtm = mtod(m, struct rt_msghdr *);
931 rtm->rtm_index = ifp->if_index;
932 rtm->rtm_flags |= rt->rt_flags;
933 rtm->rtm_errno = error;
934 rtm->rtm_addrs = info.rti_addrs;
941 * This is the analogue to the rt_newaddrmsg which performs the same
942 * function but for multicast group memberhips. This is easier since
943 * there is no route state to worry about.
946 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
948 struct rt_addrinfo info;
949 struct mbuf *m = NULL;
950 struct ifnet *ifp = ifma->ifma_ifp;
951 struct ifma_msghdr *ifmam;
953 if (route_cb.any_count == 0)
956 bzero((caddr_t)&info, sizeof(info));
957 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
958 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
960 * If a link-layer address is present, present it as a ``gateway''
961 * (similarly to how ARP entries, e.g., are presented).
963 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
964 m = rt_msg1(cmd, &info);
967 ifmam = mtod(m, struct ifma_msghdr *);
968 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
970 ifmam->ifmam_index = ifp->if_index;
971 ifmam->ifmam_addrs = info.rti_addrs;
972 rt_dispatch(m, ifma->ifma_addr);
976 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
977 struct rt_addrinfo *info)
979 struct if_announcemsghdr *ifan;
982 if (route_cb.any_count == 0)
984 bzero((caddr_t)info, sizeof(*info));
985 m = rt_msg1(type, info);
987 ifan = mtod(m, struct if_announcemsghdr *);
988 ifan->ifan_index = ifp->if_index;
989 strlcpy(ifan->ifan_name, ifp->if_xname,
990 sizeof(ifan->ifan_name));
991 ifan->ifan_what = what;
997 * This is called to generate routing socket messages indicating
998 * IEEE80211 wireless events.
999 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1002 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1005 struct rt_addrinfo info;
1007 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1010 * Append the ieee80211 data. Try to stick it in the
1011 * mbuf containing the ifannounce msg; otherwise allocate
1012 * a new mbuf and append.
1014 * NB: we assume m is a single mbuf.
1016 if (data_len > M_TRAILINGSPACE(m)) {
1017 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1022 bcopy(data, mtod(n, void *), data_len);
1023 n->m_len = data_len;
1025 } else if (data_len > 0) {
1026 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1027 m->m_len += data_len;
1029 if (m->m_flags & M_PKTHDR)
1030 m->m_pkthdr.len += data_len;
1031 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1032 rt_dispatch(m, NULL);
1037 * This is called to generate routing socket messages indicating
1038 * network interface arrival and departure.
1041 rt_ifannouncemsg(struct ifnet *ifp, int what)
1044 struct rt_addrinfo info;
1046 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1048 rt_dispatch(m, NULL);
1052 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1057 * Preserve the family from the sockaddr, if any, in an m_tag for
1058 * use when injecting the mbuf into the routing socket buffer from
1062 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1068 *(unsigned short *)(tag + 1) = sa->sa_family;
1069 m_tag_prepend(m, tag);
1071 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1075 * This is used in dumping the kernel table via sysctl().
1078 sysctl_dumpentry(struct radix_node *rn, void *vw)
1080 struct walkarg *w = vw;
1081 struct rtentry *rt = (struct rtentry *)rn;
1082 int error = 0, size;
1083 struct rt_addrinfo info;
1085 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1087 bzero((caddr_t)&info, sizeof(info));
1088 info.rti_info[RTAX_DST] = rt_key(rt);
1089 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1090 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1091 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
1093 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1094 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1095 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1096 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1098 size = rt_msg2(RTM_GET, &info, NULL, w);
1099 if (w->w_req && w->w_tmem) {
1100 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1102 rtm->rtm_flags = rt->rt_flags;
1103 rtm->rtm_use = rt->rt_rmx.rmx_pksent;
1104 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1105 rtm->rtm_index = rt->rt_ifp->if_index;
1106 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1107 rtm->rtm_addrs = info.rti_addrs;
1108 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1115 sysctl_iflist(int af, struct walkarg *w)
1119 struct rt_addrinfo info;
1122 bzero((caddr_t)&info, sizeof(info));
1124 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1125 if (w->w_arg && w->w_arg != ifp->if_index)
1128 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1129 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1130 info.rti_info[RTAX_IFP] = NULL;
1131 if (w->w_req && w->w_tmem) {
1132 struct if_msghdr *ifm;
1134 ifm = (struct if_msghdr *)w->w_tmem;
1135 ifm->ifm_index = ifp->if_index;
1136 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1137 ifm->ifm_data = ifp->if_data;
1138 ifm->ifm_addrs = info.rti_addrs;
1139 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1143 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1144 if (af && af != ifa->ifa_addr->sa_family)
1146 if (jailed(curthread->td_ucred) &&
1147 prison_if(curthread->td_ucred, ifa->ifa_addr))
1149 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1150 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1151 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1152 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1153 if (w->w_req && w->w_tmem) {
1154 struct ifa_msghdr *ifam;
1156 ifam = (struct ifa_msghdr *)w->w_tmem;
1157 ifam->ifam_index = ifa->ifa_ifp->if_index;
1158 ifam->ifam_flags = ifa->ifa_flags;
1159 ifam->ifam_metric = ifa->ifa_metric;
1160 ifam->ifam_addrs = info.rti_addrs;
1161 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1166 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1167 info.rti_info[RTAX_BRD] = NULL;
1175 sysctl_ifmalist(int af, struct walkarg *w)
1178 struct ifmultiaddr *ifma;
1179 struct rt_addrinfo info;
1183 bzero((caddr_t)&info, sizeof(info));
1185 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1186 if (w->w_arg && w->w_arg != ifp->if_index)
1189 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1191 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1192 if (af && af != ifma->ifma_addr->sa_family)
1194 if (jailed(curproc->p_ucred) &&
1195 prison_if(curproc->p_ucred, ifma->ifma_addr))
1197 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1198 info.rti_info[RTAX_GATEWAY] =
1199 (ifma->ifma_addr->sa_family != AF_LINK) ?
1200 ifma->ifma_lladdr : NULL;
1201 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1202 if (w->w_req && w->w_tmem) {
1203 struct ifma_msghdr *ifmam;
1205 ifmam = (struct ifma_msghdr *)w->w_tmem;
1206 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1207 ifmam->ifmam_flags = 0;
1208 ifmam->ifmam_addrs = info.rti_addrs;
1209 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1211 IF_ADDR_UNLOCK(ifp);
1216 IF_ADDR_UNLOCK(ifp);
1224 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1226 int *name = (int *)arg1;
1227 u_int namelen = arg2;
1228 struct radix_node_head *rnh;
1229 int i, lim, error = EINVAL;
1238 return ((namelen < 3) ? EISDIR : ENOTDIR);
1242 bzero(&w, sizeof(w));
1247 error = sysctl_wire_old_buffer(req, 0);
1254 if (af == 0) { /* dump all tables */
1257 } else /* dump only one table */
1259 for (error = 0; error == 0 && i <= lim; i++)
1260 if ((rnh = rt_tables[i]) != NULL) {
1261 RADIX_NODE_HEAD_LOCK(rnh);
1262 error = rnh->rnh_walktree(rnh,
1263 sysctl_dumpentry, &w);
1264 RADIX_NODE_HEAD_UNLOCK(rnh);
1266 error = EAFNOSUPPORT;
1270 error = sysctl_iflist(af, &w);
1273 case NET_RT_IFMALIST:
1274 error = sysctl_ifmalist(af, &w);
1278 free(w.w_tmem, M_RTABLE);
1282 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1285 * Definitions of protocols supported in the ROUTE domain.
1288 static struct domain routedomain; /* or at least forward */
1290 static struct protosw routesw[] = {
1292 .pr_type = SOCK_RAW,
1293 .pr_domain = &routedomain,
1294 .pr_flags = PR_ATOMIC|PR_ADDR,
1295 .pr_output = route_output,
1296 .pr_ctlinput = raw_ctlinput,
1297 .pr_init = raw_init,
1298 .pr_usrreqs = &route_usrreqs
1302 static struct domain routedomain = {
1303 .dom_family = PF_ROUTE,
1304 .dom_name = "route",
1305 .dom_protosw = routesw,
1306 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]