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;
470 } else if ((ifp = rt->rt_ifp) != NULL) {
471 rtm->rtm_index = ifp->if_index;
473 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
474 if (len > rtm->rtm_msglen) {
475 struct rt_msghdr *new_rtm;
476 R_Malloc(new_rtm, struct rt_msghdr *, len);
477 if (new_rtm == NULL) {
481 bcopy(rtm, new_rtm, rtm->rtm_msglen);
482 Free(rtm); rtm = new_rtm;
484 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
485 rtm->rtm_flags = rt->rt_flags;
486 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
487 rtm->rtm_addrs = info.rti_addrs;
492 * New gateway could require new ifaddr, ifp;
493 * flags may also be different; ifp may be specified
494 * by ll sockaddr when protocol address is ambiguous
496 if (((rt->rt_flags & RTF_GATEWAY) &&
497 info.rti_info[RTAX_GATEWAY] != NULL) ||
498 info.rti_info[RTAX_IFP] != NULL ||
499 (info.rti_info[RTAX_IFA] != NULL &&
500 !sa_equal(info.rti_info[RTAX_IFA],
501 rt->rt_ifa->ifa_addr))) {
502 if ((error = rt_getifa(&info)) != 0) {
507 if (info.rti_info[RTAX_GATEWAY] != NULL &&
508 (error = rt_setgate(rt, rt_key(rt),
509 info.rti_info[RTAX_GATEWAY])) != 0) {
513 if ((ifa = info.rti_ifa) != NULL) {
514 struct ifaddr *oifa = rt->rt_ifa;
517 if (oifa->ifa_rtrequest)
525 rt->rt_ifp = info.rti_ifp;
528 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
530 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
531 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
532 if (info.rti_info[RTAX_GENMASK])
533 rt->rt_genmask = info.rti_info[RTAX_GENMASK];
536 /* We don't support locks anymore */
549 rtm->rtm_errno = error;
551 rtm->rtm_flags |= RTF_DONE;
553 if (rt) /* XXX can this be true? */
556 struct rawcb *rp = NULL;
558 * Check to see if we don't want our own messages.
560 if ((so->so_options & SO_USELOOPBACK) == 0) {
561 if (route_cb.any_count <= 1) {
567 /* There is another listener, so construct message */
571 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
572 if (m->m_pkthdr.len < rtm->rtm_msglen) {
575 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
576 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
582 * XXX insure we don't get a copy by
583 * invalidating our protocol
585 unsigned short family = rp->rcb_proto.sp_family;
586 rp->rcb_proto.sp_family = 0;
587 rt_dispatch(m, info.rti_info[RTAX_DST]);
588 rp->rcb_proto.sp_family = family;
590 rt_dispatch(m, info.rti_info[RTAX_DST]);
598 rt_setmetrics(u_long which, const struct rt_metrics *in,
599 struct rt_metrics_lite *out)
601 #define metric(f, e) if (which & (f)) out->e = in->e;
603 * Only these are stored in the routing entry since introduction
604 * of tcp hostcache. The rest is ignored.
606 metric(RTV_MTU, rmx_mtu);
607 metric(RTV_EXPIRE, rmx_expire);
612 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
614 #define metric(e) out->e = in->e;
615 bzero(out, sizeof(*out));
622 * Extract the addresses of the passed sockaddrs.
623 * Do a little sanity checking so as to avoid bad memory references.
624 * This data is derived straight from userland.
627 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
632 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
633 if ((rtinfo->rti_addrs & (1 << i)) == 0)
635 sa = (struct sockaddr *)cp;
639 if (cp + sa->sa_len > cplim)
642 * there are no more.. quit now
643 * If there are more bits, they are in error.
644 * I've seen this. route(1) can evidently generate these.
645 * This causes kernel to core dump.
646 * for compatibility, If we see this, point to a safe address.
648 if (sa->sa_len == 0) {
649 rtinfo->rti_info[i] = &sa_zero;
650 return (0); /* should be EINVAL but for compat */
653 rtinfo->rti_info[i] = sa;
660 rt_msg1(int type, struct rt_addrinfo *rtinfo)
662 struct rt_msghdr *rtm;
672 len = sizeof(struct ifa_msghdr);
677 len = sizeof(struct ifma_msghdr);
681 len = sizeof(struct if_msghdr);
686 len = sizeof(struct if_announcemsghdr);
690 len = sizeof(struct rt_msghdr);
694 m = m_gethdr(M_DONTWAIT, MT_DATA);
695 if (m && len > MHLEN) {
696 MCLGET(m, M_DONTWAIT);
697 if ((m->m_flags & M_EXT) == 0) {
704 m->m_pkthdr.len = m->m_len = len;
705 m->m_pkthdr.rcvif = NULL;
706 rtm = mtod(m, struct rt_msghdr *);
707 bzero((caddr_t)rtm, len);
708 for (i = 0; i < RTAX_MAX; i++) {
709 if ((sa = rtinfo->rti_info[i]) == NULL)
711 rtinfo->rti_addrs |= (1 << i);
713 m_copyback(m, len, dlen, (caddr_t)sa);
716 if (m->m_pkthdr.len != len) {
720 rtm->rtm_msglen = len;
721 rtm->rtm_version = RTM_VERSION;
722 rtm->rtm_type = type;
727 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
730 int len, dlen, second_time = 0;
733 rtinfo->rti_addrs = 0;
739 len = sizeof(struct ifa_msghdr);
743 len = sizeof(struct if_msghdr);
747 len = sizeof(struct ifma_msghdr);
751 len = sizeof(struct rt_msghdr);
756 for (i = 0; i < RTAX_MAX; i++) {
759 if ((sa = rtinfo->rti_info[i]) == NULL)
761 rtinfo->rti_addrs |= (1 << i);
764 bcopy((caddr_t)sa, cp, (unsigned)dlen);
770 if (cp == NULL && w != NULL && !second_time) {
771 struct walkarg *rw = w;
774 if (rw->w_tmemsize < len) {
776 free(rw->w_tmem, M_RTABLE);
777 rw->w_tmem = (caddr_t)
778 malloc(len, M_RTABLE, M_NOWAIT);
780 rw->w_tmemsize = len;
790 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
792 rtm->rtm_version = RTM_VERSION;
793 rtm->rtm_type = type;
794 rtm->rtm_msglen = len;
800 * This routine is called to generate a message from the routing
801 * socket indicating that a redirect has occured, a routing lookup
802 * has failed, or that a protocol has detected timeouts to a particular
806 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
808 struct rt_msghdr *rtm;
810 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
812 if (route_cb.any_count == 0)
814 m = rt_msg1(type, rtinfo);
817 rtm = mtod(m, struct rt_msghdr *);
818 rtm->rtm_flags = RTF_DONE | flags;
819 rtm->rtm_errno = error;
820 rtm->rtm_addrs = rtinfo->rti_addrs;
825 * This routine is called to generate a message from the routing
826 * socket indicating that the status of a network interface has changed.
829 rt_ifmsg(struct ifnet *ifp)
831 struct if_msghdr *ifm;
833 struct rt_addrinfo info;
835 if (route_cb.any_count == 0)
837 bzero((caddr_t)&info, sizeof(info));
838 m = rt_msg1(RTM_IFINFO, &info);
841 ifm = mtod(m, struct if_msghdr *);
842 ifm->ifm_index = ifp->if_index;
843 ifm->ifm_flags = ifp->if_flags;
844 ifm->ifm_data = ifp->if_data;
846 rt_dispatch(m, NULL);
850 * This is called to generate messages from the routing socket
851 * indicating a network interface has had addresses associated with it.
852 * if we ever reverse the logic and replace messages TO the routing
853 * socket indicate a request to configure interfaces, then it will
854 * be unnecessary as the routing socket will automatically generate
858 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
860 struct rt_addrinfo info;
861 struct sockaddr *sa = NULL;
863 struct mbuf *m = NULL;
864 struct ifnet *ifp = ifa->ifa_ifp;
866 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
867 ("unexpected cmd %u", cmd));
869 if (route_cb.any_count == 0)
871 for (pass = 1; pass < 3; pass++) {
872 bzero((caddr_t)&info, sizeof(info));
873 if ((cmd == RTM_ADD && pass == 1) ||
874 (cmd == RTM_DELETE && pass == 2)) {
875 struct ifa_msghdr *ifam;
876 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
878 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
879 info.rti_info[RTAX_IFP] =
880 ifaddr_byindex(ifp->if_index)->ifa_addr;
881 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
882 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
883 if ((m = rt_msg1(ncmd, &info)) == NULL)
885 ifam = mtod(m, struct ifa_msghdr *);
886 ifam->ifam_index = ifp->if_index;
887 ifam->ifam_metric = ifa->ifa_metric;
888 ifam->ifam_flags = ifa->ifa_flags;
889 ifam->ifam_addrs = info.rti_addrs;
891 if ((cmd == RTM_ADD && pass == 2) ||
892 (cmd == RTM_DELETE && pass == 1)) {
893 struct rt_msghdr *rtm;
897 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
898 info.rti_info[RTAX_DST] = sa = rt_key(rt);
899 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
900 if ((m = rt_msg1(cmd, &info)) == NULL)
902 rtm = mtod(m, struct rt_msghdr *);
903 rtm->rtm_index = ifp->if_index;
904 rtm->rtm_flags |= rt->rt_flags;
905 rtm->rtm_errno = error;
906 rtm->rtm_addrs = info.rti_addrs;
913 * This is the analogue to the rt_newaddrmsg which performs the same
914 * function but for multicast group memberhips. This is easier since
915 * there is no route state to worry about.
918 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
920 struct rt_addrinfo info;
921 struct mbuf *m = NULL;
922 struct ifnet *ifp = ifma->ifma_ifp;
923 struct ifma_msghdr *ifmam;
925 if (route_cb.any_count == 0)
928 bzero((caddr_t)&info, sizeof(info));
929 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
930 info.rti_info[RTAX_IFP] =
931 ifp ? ifaddr_byindex(ifp->if_index)->ifa_addr : NULL;
933 * If a link-layer address is present, present it as a ``gateway''
934 * (similarly to how ARP entries, e.g., are presented).
936 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
937 m = rt_msg1(cmd, &info);
940 ifmam = mtod(m, struct ifma_msghdr *);
941 ifmam->ifmam_index = ifp->if_index;
942 ifmam->ifmam_addrs = info.rti_addrs;
943 rt_dispatch(m, ifma->ifma_addr);
947 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
948 struct rt_addrinfo *info)
950 struct if_announcemsghdr *ifan;
953 if (route_cb.any_count == 0)
955 bzero((caddr_t)info, sizeof(*info));
956 m = rt_msg1(type, info);
958 ifan = mtod(m, struct if_announcemsghdr *);
959 ifan->ifan_index = ifp->if_index;
960 strlcpy(ifan->ifan_name, ifp->if_xname,
961 sizeof(ifan->ifan_name));
962 ifan->ifan_what = what;
968 * This is called to generate routing socket messages indicating
969 * IEEE80211 wireless events.
970 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
973 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
976 struct rt_addrinfo info;
978 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
981 * Append the ieee80211 data. Try to stick it in the
982 * mbuf containing the ifannounce msg; otherwise allocate
983 * a new mbuf and append.
985 * NB: we assume m is a single mbuf.
987 if (data_len > M_TRAILINGSPACE(m)) {
988 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
993 bcopy(data, mtod(n, void *), data_len);
996 } else if (data_len > 0) {
997 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
998 m->m_len += data_len;
1000 if (m->m_flags & M_PKTHDR)
1001 m->m_pkthdr.len += data_len;
1002 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1003 rt_dispatch(m, NULL);
1008 * This is called to generate routing socket messages indicating
1009 * network interface arrival and departure.
1012 rt_ifannouncemsg(struct ifnet *ifp, int what)
1015 struct rt_addrinfo info;
1017 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1019 rt_dispatch(m, NULL);
1023 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1028 * Preserve the family from the sockaddr, if any, in an m_tag for
1029 * use when injecting the mbuf into the routing socket buffer from
1033 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1039 *(unsigned short *)(tag + 1) = sa->sa_family;
1040 m_tag_prepend(m, tag);
1042 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1046 * This is used in dumping the kernel table via sysctl().
1049 sysctl_dumpentry(struct radix_node *rn, void *vw)
1051 struct walkarg *w = vw;
1052 struct rtentry *rt = (struct rtentry *)rn;
1053 int error = 0, size;
1054 struct rt_addrinfo info;
1056 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1058 bzero((caddr_t)&info, sizeof(info));
1059 info.rti_info[RTAX_DST] = rt_key(rt);
1060 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1061 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1062 info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
1064 info.rti_info[RTAX_IFP] =
1065 ifaddr_byindex(rt->rt_ifp->if_index)->ifa_addr;
1066 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1067 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1068 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1070 size = rt_msg2(RTM_GET, &info, NULL, w);
1071 if (w->w_req && w->w_tmem) {
1072 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1074 rtm->rtm_flags = rt->rt_flags;
1075 rtm->rtm_use = rt->rt_rmx.rmx_pksent;
1076 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1077 rtm->rtm_index = rt->rt_ifp->if_index;
1078 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1079 rtm->rtm_addrs = info.rti_addrs;
1080 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1087 sysctl_iflist(int af, struct walkarg *w)
1091 struct rt_addrinfo info;
1094 bzero((caddr_t)&info, sizeof(info));
1095 /* IFNET_RLOCK(); */ /* could sleep XXX */
1096 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1097 if (w->w_arg && w->w_arg != ifp->if_index)
1099 ifa = ifaddr_byindex(ifp->if_index);
1100 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1101 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1102 info.rti_info[RTAX_IFP] = NULL;
1103 if (w->w_req && w->w_tmem) {
1104 struct if_msghdr *ifm;
1106 ifm = (struct if_msghdr *)w->w_tmem;
1107 ifm->ifm_index = ifp->if_index;
1108 ifm->ifm_flags = ifp->if_flags;
1109 ifm->ifm_data = ifp->if_data;
1110 ifm->ifm_addrs = info.rti_addrs;
1111 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1115 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1116 if (af && af != ifa->ifa_addr->sa_family)
1118 if (jailed(curthread->td_ucred) &&
1119 prison_if(curthread->td_ucred, ifa->ifa_addr))
1121 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1122 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1123 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1124 len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1125 if (w->w_req && w->w_tmem) {
1126 struct ifa_msghdr *ifam;
1128 ifam = (struct ifa_msghdr *)w->w_tmem;
1129 ifam->ifam_index = ifa->ifa_ifp->if_index;
1130 ifam->ifam_flags = ifa->ifa_flags;
1131 ifam->ifam_metric = ifa->ifa_metric;
1132 ifam->ifam_addrs = info.rti_addrs;
1133 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1138 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1139 info.rti_info[RTAX_BRD] = NULL;
1142 /* IFNET_RUNLOCK(); */ /* XXX */
1147 sysctl_ifmalist(int af, struct walkarg *w)
1150 struct ifmultiaddr *ifma;
1151 struct rt_addrinfo info;
1155 bzero((caddr_t)&info, sizeof(info));
1156 /* IFNET_RLOCK(); */ /* could sleep XXX */
1157 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1158 if (w->w_arg && w->w_arg != ifp->if_index)
1160 ifa = ifaddr_byindex(ifp->if_index);
1161 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1162 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1163 if (af && af != ifma->ifma_addr->sa_family)
1165 if (jailed(curproc->p_ucred) &&
1166 prison_if(curproc->p_ucred, ifma->ifma_addr))
1168 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1169 info.rti_info[RTAX_GATEWAY] =
1170 (ifma->ifma_addr->sa_family != AF_LINK) ?
1171 ifma->ifma_lladdr : NULL;
1172 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1173 if (w->w_req && w->w_tmem) {
1174 struct ifma_msghdr *ifmam;
1176 ifmam = (struct ifma_msghdr *)w->w_tmem;
1177 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1178 ifmam->ifmam_flags = 0;
1179 ifmam->ifmam_addrs = info.rti_addrs;
1180 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1187 /* IFNET_RUNLOCK(); */ /* XXX */
1192 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1194 int *name = (int *)arg1;
1195 u_int namelen = arg2;
1196 struct radix_node_head *rnh;
1197 int i, lim, s, error = EINVAL;
1206 return ((namelen < 3) ? EISDIR : ENOTDIR);
1210 bzero(&w, sizeof(w));
1220 if (af == 0) { /* dump all tables */
1223 } else /* dump only one table */
1225 for (error = 0; error == 0 && i <= lim; i++)
1226 if ((rnh = rt_tables[i]) != NULL) {
1227 /* RADIX_NODE_HEAD_LOCK(rnh); */
1228 error = rnh->rnh_walktree(rnh,
1229 sysctl_dumpentry, &w);/* could sleep XXX */
1230 /* RADIX_NODE_HEAD_UNLOCK(rnh); */
1232 error = EAFNOSUPPORT;
1236 error = sysctl_iflist(af, &w);
1239 case NET_RT_IFMALIST:
1240 error = sysctl_ifmalist(af, &w);
1245 free(w.w_tmem, M_RTABLE);
1249 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1252 * Definitions of protocols supported in the ROUTE domain.
1255 extern struct domain routedomain; /* or at least forward */
1257 static struct protosw routesw[] = {
1258 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1259 0, route_output, raw_ctlinput, 0,
1266 static struct domain routedomain =
1267 { PF_ROUTE, "route", 0, 0, 0,
1268 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };