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>
40 #include <sys/protosw.h>
41 #include <sys/signalvar.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
48 #include <net/netisr.h>
49 #include <net/raw_cb.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
54 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
56 /* NB: these are not modified */
57 static struct sockaddr route_dst = { 2, PF_ROUTE, };
58 static struct sockaddr route_src = { 2, PF_ROUTE, };
59 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
62 int ip_count; /* attached w/ AF_INET */
63 int ip6_count; /* attached w/ AF_INET6 */
64 int ipx_count; /* attached w/ AF_IPX */
65 int any_count; /* total attached */
68 struct mtx rtsock_mtx;
69 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
71 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
72 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
73 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
75 static struct ifqueue rtsintrq;
77 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
78 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
79 &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
85 struct sysctl_req *w_req;
88 static void rts_input(struct mbuf *m);
89 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
90 static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
91 caddr_t cp, struct walkarg *w);
92 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
93 struct rt_addrinfo *rtinfo);
94 static int sysctl_dumpentry(struct radix_node *rn, void *vw);
95 static int sysctl_iflist(int af, struct walkarg *w);
96 static int sysctl_ifmalist(int af, struct walkarg *w);
97 static int route_output(struct mbuf *m, struct socket *so);
98 static void rt_setmetrics(u_long which, const struct rt_metrics *in,
99 struct rt_metrics_lite *out);
100 static void rt_getmetrics(const struct rt_metrics_lite *in,
101 struct rt_metrics *out);
102 static void rt_dispatch(struct mbuf *, const struct sockaddr *);
109 rtsintrq.ifq_maxlen = 256;
110 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
111 rtsintrq.ifq_maxlen = tmp;
112 mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
113 netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE);
115 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0)
118 rts_input(struct mbuf *m)
120 struct sockproto route_proto;
121 unsigned short *family;
124 route_proto.sp_family = PF_ROUTE;
125 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
127 family = (unsigned short *)(tag + 1);
128 route_proto.sp_protocol = *family;
129 m_tag_delete(m, tag);
131 route_proto.sp_protocol = 0;
133 raw_input(m, &route_proto, &route_src, &route_dst);
137 * It really doesn't make any sense at all for this code to share much
138 * with raw_usrreq.c, since its functionality is so restricted. XXX
141 rts_abort(struct socket *so)
145 error = raw_usrreqs.pru_abort(so);
150 /* pru_accept is EOPNOTSUPP */
153 rts_attach(struct socket *so, int proto, struct thread *td)
158 if (sotorawcb(so) != NULL)
159 return EISCONN; /* XXX panic? */
161 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
166 * The splnet() is necessary to block protocols from sending
167 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
168 * this PCB is extant but incompletely initialized.
169 * Probably we should try to do more of this work beforehand and
173 so->so_pcb = (caddr_t)rp;
174 error = raw_attach(so, proto);
183 switch(rp->rcb_proto.sp_protocol) {
188 route_cb.ip6_count++;
191 route_cb.ipx_count++;
194 rp->rcb_faddr = &route_src;
195 route_cb.any_count++;
198 so->so_options |= SO_USELOOPBACK;
204 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
208 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
214 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
218 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
223 /* pru_connect2 is EOPNOTSUPP */
224 /* pru_control is EOPNOTSUPP */
227 rts_detach(struct socket *so)
229 struct rawcb *rp = sotorawcb(so);
235 switch(rp->rcb_proto.sp_protocol) {
240 route_cb.ip6_count--;
243 route_cb.ipx_count--;
246 route_cb.any_count--;
249 error = raw_usrreqs.pru_detach(so);
255 rts_disconnect(struct socket *so)
259 error = raw_usrreqs.pru_disconnect(so);
264 /* pru_listen is EOPNOTSUPP */
267 rts_peeraddr(struct socket *so, struct sockaddr **nam)
271 error = raw_usrreqs.pru_peeraddr(so, nam);
276 /* pru_rcvd is EOPNOTSUPP */
277 /* pru_rcvoob is EOPNOTSUPP */
280 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
281 struct mbuf *control, struct thread *td)
285 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
290 /* pru_sense is null */
293 rts_shutdown(struct socket *so)
297 error = raw_usrreqs.pru_shutdown(so);
303 rts_sockaddr(struct socket *so, struct sockaddr **nam)
307 error = raw_usrreqs.pru_sockaddr(so, nam);
312 static struct pr_usrreqs route_usrreqs = {
313 .pru_abort = rts_abort,
314 .pru_attach = rts_attach,
315 .pru_bind = rts_bind,
316 .pru_connect = rts_connect,
317 .pru_detach = rts_detach,
318 .pru_disconnect = rts_disconnect,
319 .pru_peeraddr = rts_peeraddr,
320 .pru_send = rts_send,
321 .pru_shutdown = rts_shutdown,
322 .pru_sockaddr = rts_sockaddr,
327 route_output(struct mbuf *m, struct socket *so)
329 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
330 struct rt_msghdr *rtm = NULL;
331 struct rtentry *rt = NULL;
332 struct radix_node_head *rnh;
333 struct rt_addrinfo info;
335 struct ifnet *ifp = NULL;
336 struct ifaddr *ifa = NULL;
337 struct sockaddr_in jail;
339 #define senderr(e) { error = e; goto flush;}
340 if (m == NULL || ((m->m_len < sizeof(long)) &&
341 (m = m_pullup(m, sizeof(long))) == NULL))
343 if ((m->m_flags & M_PKTHDR) == 0)
344 panic("route_output");
345 len = m->m_pkthdr.len;
346 if (len < sizeof(*rtm) ||
347 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
348 info.rti_info[RTAX_DST] = NULL;
351 R_Malloc(rtm, struct rt_msghdr *, len);
353 info.rti_info[RTAX_DST] = NULL;
356 m_copydata(m, 0, len, (caddr_t)rtm);
357 if (rtm->rtm_version != RTM_VERSION) {
358 info.rti_info[RTAX_DST] = NULL;
359 senderr(EPROTONOSUPPORT);
361 rtm->rtm_pid = curproc->p_pid;
362 bzero(&info, sizeof(info));
363 info.rti_addrs = rtm->rtm_addrs;
364 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
365 info.rti_info[RTAX_DST] = NULL;
368 info.rti_flags = rtm->rtm_flags;
369 if (info.rti_info[RTAX_DST] == NULL ||
370 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
371 (info.rti_info[RTAX_GATEWAY] != NULL &&
372 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
374 if (info.rti_info[RTAX_GENMASK]) {
375 struct radix_node *t;
376 t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
378 bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1,
379 (char *)(void *)t->rn_key + 1,
380 ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0)
381 info.rti_info[RTAX_GENMASK] =
382 (struct sockaddr *)t->rn_key;
388 * Verify that the caller has the appropriate privilege; RTM_GET
389 * is the only operation the non-superuser is allowed.
391 if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0)
394 switch (rtm->rtm_type) {
395 struct rtentry *saved_nrt;
398 if (info.rti_info[RTAX_GATEWAY] == NULL)
401 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
402 if (error == 0 && saved_nrt) {
404 rt_setmetrics(rtm->rtm_inits,
405 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
406 RT_REMREF(saved_nrt);
407 saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
408 RT_UNLOCK(saved_nrt);
414 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
425 rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
427 senderr(EAFNOSUPPORT);
428 RADIX_NODE_HEAD_LOCK(rnh);
429 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
430 info.rti_info[RTAX_NETMASK], rnh);
431 RADIX_NODE_HEAD_UNLOCK(rnh);
432 if (rt == NULL) /* XXX looks bogus */
437 switch(rtm->rtm_type) {
442 info.rti_info[RTAX_DST] = rt_key(rt);
443 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
444 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
445 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
446 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
449 info.rti_info[RTAX_IFP] =
450 ifaddr_byindex(ifp->if_index)->ifa_addr;
451 if (jailed(so->so_cred)) {
452 bzero(&jail, sizeof(jail));
453 jail.sin_family = PF_INET;
454 jail.sin_len = sizeof(jail);
455 jail.sin_addr.s_addr =
456 htonl(prison_getip(so->so_cred));
457 info.rti_info[RTAX_IFA] =
458 (struct sockaddr *)&jail;
460 info.rti_info[RTAX_IFA] =
461 rt->rt_ifa->ifa_addr;
462 if (ifp->if_flags & IFF_POINTOPOINT)
463 info.rti_info[RTAX_BRD] =
464 rt->rt_ifa->ifa_dstaddr;
465 rtm->rtm_index = ifp->if_index;
467 info.rti_info[RTAX_IFP] = NULL;
468 info.rti_info[RTAX_IFA] = NULL;
471 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
472 if (len > rtm->rtm_msglen) {
473 struct rt_msghdr *new_rtm;
474 R_Malloc(new_rtm, struct rt_msghdr *, len);
475 if (new_rtm == NULL) {
479 bcopy(rtm, new_rtm, rtm->rtm_msglen);
480 Free(rtm); rtm = new_rtm;
482 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
483 rtm->rtm_flags = rt->rt_flags;
484 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
485 rtm->rtm_addrs = info.rti_addrs;
490 * New gateway could require new ifaddr, ifp;
491 * flags may also be different; ifp may be specified
492 * by ll sockaddr when protocol address is ambiguous
494 if (((rt->rt_flags & RTF_GATEWAY) &&
495 info.rti_info[RTAX_GATEWAY] != NULL) ||
496 info.rti_info[RTAX_IFP] != NULL ||
497 (info.rti_info[RTAX_IFA] != NULL &&
498 !sa_equal(info.rti_info[RTAX_IFA],
499 rt->rt_ifa->ifa_addr))) {
500 if ((error = rt_getifa(&info)) != 0) {
505 if (info.rti_info[RTAX_GATEWAY] != NULL &&
506 (error = rt_setgate(rt, rt_key(rt),
507 info.rti_info[RTAX_GATEWAY])) != 0) {
511 if ((ifa = info.rti_ifa) != NULL) {
512 struct ifaddr *oifa = rt->rt_ifa;
515 if (oifa->ifa_rtrequest)
523 rt->rt_ifp = info.rti_ifp;
526 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
528 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
529 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
530 if (info.rti_info[RTAX_GENMASK])
531 rt->rt_genmask = info.rti_info[RTAX_GENMASK];
534 /* We don't support locks anymore */
547 rtm->rtm_errno = error;
549 rtm->rtm_flags |= RTF_DONE;
551 if (rt) /* XXX can this be true? */
554 struct rawcb *rp = NULL;
556 * Check to see if we don't want our own messages.
558 if ((so->so_options & SO_USELOOPBACK) == 0) {
559 if (route_cb.any_count <= 1) {
565 /* There is another listener, so construct message */
569 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
570 if (m->m_pkthdr.len < rtm->rtm_msglen) {
573 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
574 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
580 * XXX insure we don't get a copy by
581 * invalidating our protocol
583 unsigned short family = rp->rcb_proto.sp_family;
584 rp->rcb_proto.sp_family = 0;
585 rt_dispatch(m, info.rti_info[RTAX_DST]);
586 rp->rcb_proto.sp_family = family;
588 rt_dispatch(m, info.rti_info[RTAX_DST]);
596 rt_setmetrics(u_long which, const struct rt_metrics *in,
597 struct rt_metrics_lite *out)
599 #define metric(f, e) if (which & (f)) out->e = in->e;
601 * Only these are stored in the routing entry since introduction
602 * of tcp hostcache. The rest is ignored.
604 metric(RTV_MTU, rmx_mtu);
605 metric(RTV_EXPIRE, rmx_expire);
610 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
612 #define metric(e) out->e = in->e;
613 bzero(out, sizeof(*out));
620 * Extract the addresses of the passed sockaddrs.
621 * Do a little sanity checking so as to avoid bad memory references.
622 * This data is derived straight from userland.
625 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
630 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
631 if ((rtinfo->rti_addrs & (1 << i)) == 0)
633 sa = (struct sockaddr *)cp;
637 if (cp + sa->sa_len > cplim)
640 * there are no more.. quit now
641 * If there are more bits, they are in error.
642 * I've seen this. route(1) can evidently generate these.
643 * This causes kernel to core dump.
644 * for compatibility, If we see this, point to a safe address.
646 if (sa->sa_len == 0) {
647 rtinfo->rti_info[i] = &sa_zero;
648 return (0); /* should be EINVAL but for compat */
651 rtinfo->rti_info[i] = sa;
658 rt_msg1(int type, struct rt_addrinfo *rtinfo)
660 struct rt_msghdr *rtm;
670 len = sizeof(struct ifa_msghdr);
675 len = sizeof(struct ifma_msghdr);
679 len = sizeof(struct if_msghdr);
684 len = sizeof(struct if_announcemsghdr);
688 len = sizeof(struct rt_msghdr);
692 m = m_gethdr(M_DONTWAIT, MT_DATA);
693 if (m && len > MHLEN) {
694 MCLGET(m, M_DONTWAIT);
695 if ((m->m_flags & M_EXT) == 0) {
702 m->m_pkthdr.len = m->m_len = len;
703 m->m_pkthdr.rcvif = NULL;
704 rtm = mtod(m, struct rt_msghdr *);
705 bzero((caddr_t)rtm, len);
706 for (i = 0; i < RTAX_MAX; i++) {
707 if ((sa = rtinfo->rti_info[i]) == NULL)
709 rtinfo->rti_addrs |= (1 << i);
711 m_copyback(m, len, dlen, (caddr_t)sa);
714 if (m->m_pkthdr.len != len) {
718 rtm->rtm_msglen = len;
719 rtm->rtm_version = RTM_VERSION;
720 rtm->rtm_type = type;
725 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
728 int len, dlen, second_time = 0;
731 rtinfo->rti_addrs = 0;
737 len = sizeof(struct ifa_msghdr);
741 len = sizeof(struct if_msghdr);
745 len = sizeof(struct ifma_msghdr);
749 len = sizeof(struct rt_msghdr);
754 for (i = 0; i < RTAX_MAX; i++) {
757 if ((sa = rtinfo->rti_info[i]) == NULL)
759 rtinfo->rti_addrs |= (1 << i);
762 bcopy((caddr_t)sa, cp, (unsigned)dlen);
768 if (cp == NULL && w != NULL && !second_time) {
769 struct walkarg *rw = w;
772 if (rw->w_tmemsize < len) {
774 free(rw->w_tmem, M_RTABLE);
775 rw->w_tmem = (caddr_t)
776 malloc(len, M_RTABLE, M_NOWAIT);
778 rw->w_tmemsize = len;
788 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
790 rtm->rtm_version = RTM_VERSION;
791 rtm->rtm_type = type;
792 rtm->rtm_msglen = len;
798 * This routine is called to generate a message from the routing
799 * socket indicating that a redirect has occured, a routing lookup
800 * has failed, or that a protocol has detected timeouts to a particular
804 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
806 struct rt_msghdr *rtm;
808 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
810 if (route_cb.any_count == 0)
812 m = rt_msg1(type, rtinfo);
815 rtm = mtod(m, struct rt_msghdr *);
816 rtm->rtm_flags = RTF_DONE | flags;
817 rtm->rtm_errno = error;
818 rtm->rtm_addrs = rtinfo->rti_addrs;
823 * This routine is called to generate a message from the routing
824 * socket indicating that the status of a network interface has changed.
827 rt_ifmsg(struct ifnet *ifp)
829 struct if_msghdr *ifm;
831 struct rt_addrinfo info;
833 if (route_cb.any_count == 0)
835 bzero((caddr_t)&info, sizeof(info));
836 m = rt_msg1(RTM_IFINFO, &info);
839 ifm = mtod(m, struct if_msghdr *);
840 ifm->ifm_index = ifp->if_index;
841 ifm->ifm_flags = ifp->if_flags;
842 ifm->ifm_data = ifp->if_data;
844 rt_dispatch(m, NULL);
848 * This is called to generate messages from the routing socket
849 * indicating a network interface has had addresses associated with it.
850 * if we ever reverse the logic and replace messages TO the routing
851 * socket indicate a request to configure interfaces, then it will
852 * be unnecessary as the routing socket will automatically generate
856 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
858 struct rt_addrinfo info;
859 struct sockaddr *sa = NULL;
861 struct mbuf *m = NULL;
862 struct ifnet *ifp = ifa->ifa_ifp;
864 if (route_cb.any_count == 0)
866 for (pass = 1; pass < 3; pass++) {
867 bzero((caddr_t)&info, sizeof(info));
868 if ((cmd == RTM_ADD && pass == 1) ||
869 (cmd == RTM_DELETE && pass == 2)) {
870 struct ifa_msghdr *ifam;
871 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
873 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
874 info.rti_info[RTAX_IFP] =
875 ifaddr_byindex(ifp->if_index)->ifa_addr;
876 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
877 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
878 if ((m = rt_msg1(ncmd, &info)) == NULL)
880 ifam = mtod(m, struct ifa_msghdr *);
881 ifam->ifam_index = ifp->if_index;
882 ifam->ifam_metric = ifa->ifa_metric;
883 ifam->ifam_flags = ifa->ifa_flags;
884 ifam->ifam_addrs = info.rti_addrs;
886 if ((cmd == RTM_ADD && pass == 2) ||
887 (cmd == RTM_DELETE && pass == 1)) {
888 struct rt_msghdr *rtm;
892 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
893 info.rti_info[RTAX_DST] = sa = rt_key(rt);
894 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
895 if ((m = rt_msg1(cmd, &info)) == NULL)
897 rtm = mtod(m, struct rt_msghdr *);
898 rtm->rtm_index = ifp->if_index;
899 rtm->rtm_flags |= rt->rt_flags;
900 rtm->rtm_errno = error;
901 rtm->rtm_addrs = info.rti_addrs;
908 * This is the analogue to the rt_newaddrmsg which performs the same
909 * function but for multicast group memberhips. This is easier since
910 * there is no route state to worry about.
913 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
915 struct rt_addrinfo info;
916 struct mbuf *m = NULL;
917 struct ifnet *ifp = ifma->ifma_ifp;
918 struct ifma_msghdr *ifmam;
920 if (route_cb.any_count == 0)
923 bzero((caddr_t)&info, sizeof(info));
924 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
925 info.rti_info[RTAX_IFP] =
926 ifp ? ifaddr_byindex(ifp->if_index)->ifa_addr : NULL;
928 * If a link-layer address is present, present it as a ``gateway''
929 * (similarly to how ARP entries, e.g., are presented).
931 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
932 m = rt_msg1(cmd, &info);
935 ifmam = mtod(m, struct ifma_msghdr *);
936 ifmam->ifmam_index = ifp->if_index;
937 ifmam->ifmam_addrs = info.rti_addrs;
938 rt_dispatch(m, ifma->ifma_addr);
942 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
943 struct rt_addrinfo *info)
945 struct if_announcemsghdr *ifan;
948 if (route_cb.any_count == 0)
950 bzero((caddr_t)info, sizeof(*info));
951 m = rt_msg1(type, info);
953 ifan = mtod(m, struct if_announcemsghdr *);
954 ifan->ifan_index = ifp->if_index;
955 strlcpy(ifan->ifan_name, ifp->if_xname,
956 sizeof(ifan->ifan_name));
957 ifan->ifan_what = what;
963 * This is called to generate routing socket messages indicating
964 * IEEE80211 wireless events.
965 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
968 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
971 struct rt_addrinfo info;
973 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
976 * Append the ieee80211 data. Try to stick it in the
977 * mbuf containing the ifannounce msg; otherwise allocate
978 * a new mbuf and append.
980 * NB: we assume m is a single mbuf.
982 if (data_len > M_TRAILINGSPACE(m)) {
983 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
988 bcopy(data, mtod(n, void *), data_len);
991 } else if (data_len > 0) {
992 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
993 m->m_len += data_len;
995 if (m->m_flags & M_PKTHDR)
996 m->m_pkthdr.len += data_len;
997 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
998 rt_dispatch(m, NULL);
1003 * This is called to generate routing socket messages indicating
1004 * network interface arrival and departure.
1007 rt_ifannouncemsg(struct ifnet *ifp, int what)
1010 struct rt_addrinfo info;
1012 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1014 rt_dispatch(m, NULL);
1018 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1020 unsigned short *family;
1024 * Preserve the family from the sockaddr, if any, in an m_tag for
1025 * use when injecting the mbuf into the routing socket buffer from
1029 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1035 family = (unsigned short *)(tag + 1);
1036 *family = sa ? sa->sa_family : 0;
1037 m_tag_prepend(m, tag);
1039 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1043 * This is used in dumping the kernel table via sysctl().
1046 sysctl_dumpentry(struct radix_node *rn, void *vw)
1048 struct walkarg *w = vw;
1049 struct rtentry *rt = (struct rtentry *)rn;
1050 int error = 0, size;
1051 struct rt_addrinfo info;
1053 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1055 bzero((caddr_t)&info, sizeof(info));
1056 info.rti_info[RTAX_DST] = rt_key(rt);
1057 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1058 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1059 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
1061 info.rti_info[RTAX_IFP] =
1062 ifaddr_byindex(rt->rt_ifp->if_index)->ifa_addr;
1063 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1064 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1065 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1067 size = rt_msg2(RTM_GET, &info, NULL, w);
1068 if (w->w_req && w->w_tmem) {
1069 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1071 rtm->rtm_flags = rt->rt_flags;
1072 rtm->rtm_use = rt->rt_rmx.rmx_pksent;
1073 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1074 rtm->rtm_index = rt->rt_ifp->if_index;
1075 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1076 rtm->rtm_addrs = info.rti_addrs;
1077 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1084 sysctl_iflist(int af, struct walkarg *w)
1088 struct rt_addrinfo info;
1091 bzero((caddr_t)&info, sizeof(info));
1092 /* IFNET_RLOCK(); */ /* could sleep XXX */
1093 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1094 if (w->w_arg && w->w_arg != ifp->if_index)
1096 ifa = ifaddr_byindex(ifp->if_index);
1097 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1098 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1099 info.rti_info[RTAX_IFP] = NULL;
1100 if (w->w_req && w->w_tmem) {
1101 struct if_msghdr *ifm;
1103 ifm = (struct if_msghdr *)w->w_tmem;
1104 ifm->ifm_index = ifp->if_index;
1105 ifm->ifm_flags = ifp->if_flags;
1106 ifm->ifm_data = ifp->if_data;
1107 ifm->ifm_addrs = info.rti_addrs;
1108 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1112 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1113 if (af && af != ifa->ifa_addr->sa_family)
1115 if (jailed(curthread->td_ucred) &&
1116 prison_if(curthread->td_ucred, ifa->ifa_addr))
1118 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1119 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1120 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1121 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1122 if (w->w_req && w->w_tmem) {
1123 struct ifa_msghdr *ifam;
1125 ifam = (struct ifa_msghdr *)w->w_tmem;
1126 ifam->ifam_index = ifa->ifa_ifp->if_index;
1127 ifam->ifam_flags = ifa->ifa_flags;
1128 ifam->ifam_metric = ifa->ifa_metric;
1129 ifam->ifam_addrs = info.rti_addrs;
1130 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1135 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1136 info.rti_info[RTAX_BRD] = NULL;
1139 /* IFNET_RUNLOCK(); */ /* XXX */
1144 sysctl_ifmalist(int af, struct walkarg *w)
1147 struct ifmultiaddr *ifma;
1148 struct rt_addrinfo info;
1152 bzero((caddr_t)&info, sizeof(info));
1153 /* IFNET_RLOCK(); */ /* could sleep XXX */
1154 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1155 if (w->w_arg && w->w_arg != ifp->if_index)
1157 ifa = ifaddr_byindex(ifp->if_index);
1158 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1159 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1160 if (af && af != ifma->ifma_addr->sa_family)
1162 if (jailed(curproc->p_ucred) &&
1163 prison_if(curproc->p_ucred, ifma->ifma_addr))
1165 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1166 info.rti_info[RTAX_GATEWAY] =
1167 (ifma->ifma_addr->sa_family != AF_LINK) ?
1168 ifma->ifma_lladdr : NULL;
1169 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1170 if (w->w_req && w->w_tmem) {
1171 struct ifma_msghdr *ifmam;
1173 ifmam = (struct ifma_msghdr *)w->w_tmem;
1174 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1175 ifmam->ifmam_flags = 0;
1176 ifmam->ifmam_addrs = info.rti_addrs;
1177 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1184 /* IFNET_RUNLOCK(); */ /* XXX */
1189 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1191 int *name = (int *)arg1;
1192 u_int namelen = arg2;
1193 struct radix_node_head *rnh;
1194 int i, lim, s, error = EINVAL;
1203 return ((namelen < 3) ? EISDIR : ENOTDIR);
1207 bzero(&w, sizeof(w));
1217 if (af == 0) { /* dump all tables */
1220 } else /* dump only one table */
1222 for (error = 0; error == 0 && i <= lim; i++)
1223 if ((rnh = rt_tables[i]) != NULL) {
1224 /* RADIX_NODE_HEAD_LOCK(rnh); */
1225 error = rnh->rnh_walktree(rnh,
1226 sysctl_dumpentry, &w);/* could sleep XXX */
1227 /* RADIX_NODE_HEAD_UNLOCK(rnh); */
1229 error = EAFNOSUPPORT;
1233 error = sysctl_iflist(af, &w);
1236 case NET_RT_IFMALIST:
1237 error = sysctl_ifmalist(af, &w);
1242 free(w.w_tmem, M_RTABLE);
1246 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1249 * Definitions of protocols supported in the ROUTE domain.
1252 extern struct domain routedomain; /* or at least forward */
1254 static struct protosw routesw[] = {
1255 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1256 0, route_output, raw_ctlinput, 0,
1263 static struct domain routedomain =
1264 { PF_ROUTE, "route", 0, 0, 0,
1265 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };