2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1988, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
62 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
71 #include <sys/malloc.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/domain.h>
79 #include <sys/thread2.h>
80 #include <sys/socketvar2.h>
83 #include <net/if_var.h>
84 #include <net/route.h>
85 #include <net/raw_cb.h>
86 #include <net/netmsg2.h>
87 #include <net/netisr2.h>
89 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
91 static struct route_cb {
98 static const struct sockaddr route_src = { 2, PF_ROUTE, };
104 struct sysctl_req *w_req;
107 #ifndef RTTABLE_DUMP_MSGCNT_MAX
108 /* Should be large enough for dupkeys */
109 #define RTTABLE_DUMP_MSGCNT_MAX 64
112 struct rttable_walkarg {
123 struct sockaddr_storage w_key0;
124 struct sockaddr_storage w_mask0;
127 struct netmsg_rttable_walk {
128 struct netmsg_base base;
130 struct rttable_walkarg *w;
134 struct rawcb rocb_rcb;
135 unsigned int rocb_msgfilter;
136 char *rocb_missfilter;
137 size_t rocb_missfilterlen;
139 #define sotoroutecb(so) ((struct routecb *)(so)->so_pcb)
142 rt_msg_mbuf (int, struct rt_addrinfo *);
143 static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
144 static int rt_msgsize(int type, const struct rt_addrinfo *rtinfo);
145 static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
146 static int sysctl_rttable(int af, struct sysctl_req *req, int op, int arg);
147 static int if_addrflags(const struct ifaddr *ifa);
148 static int sysctl_iflist (int af, struct walkarg *w);
149 static int route_output(struct mbuf *, struct socket *, ...);
150 static void rt_setmetrics (u_long, struct rt_metrics *,
151 struct rt_metrics *);
154 * It really doesn't make any sense at all for this code to share much
155 * with raw_usrreq.c, since its functionality is so restricted. XXX
158 rts_abort(netmsg_t msg)
161 raw_usrreqs.pru_abort(msg);
162 /* msg invalid now */
167 rts_filter(struct mbuf *m, const struct sockproto *proto,
168 const struct rawcb *rp)
170 const struct routecb *rop = (const struct routecb *)rp;
171 const struct rt_msghdr *rtm;
174 KKASSERT(proto != NULL);
175 KKASSERT(rp != NULL);
177 /* Wrong family for this socket. */
178 if (proto->sp_family != PF_ROUTE)
181 /* If no filter set, just return. */
182 if (rop->rocb_msgfilter == 0 && rop->rocb_missfilterlen == 0)
185 /* Ensure we can access rtm_type */
187 offsetof(struct rt_msghdr, rtm_type) + sizeof(rtm->rtm_type))
190 rtm = mtod(m, const struct rt_msghdr *);
191 /* If the rtm type is filtered out, return a positive. */
192 if (rop->rocb_msgfilter != 0 &&
193 !(rop->rocb_msgfilter & ROUTE_FILTER(rtm->rtm_type)))
196 if (rop->rocb_missfilterlen != 0 && rtm->rtm_type == RTM_MISS) {
197 CTASSERT(RTAX_DST == 0);
199 struct sockaddr_storage ss;
200 struct sockaddr *dst = (struct sockaddr *)&ss;
201 char *cp = rop->rocb_missfilter;
202 char *ep = cp + rop->rocb_missfilterlen;
204 /* Ensure we can access sa_len */
205 if (m->m_pkthdr.len < sizeof(*rtm) +
206 offsetof(struct sockaddr, sa_len) + sizeof(dst->sa_len))
208 m_copydata(m, sizeof(*rtm) + offsetof(struct sockaddr, sa_len),
209 sizeof(ss.ss_len), (caddr_t)&ss);
210 if (m->m_pkthdr.len < sizeof(*rtm) + ss.ss_len)
212 /* Copy out the destination sockaddr */
213 m_copydata(m, sizeof(*rtm), ss.ss_len, (caddr_t)&ss);
215 /* Find a matching sockaddr in the filter */
217 sa = (struct sockaddr *)cp;
218 if (sa->sa_len == dst->sa_len &&
219 memcmp(sa, dst, sa->sa_len) == 0)
221 cp += RT_ROUNDUP(sa->sa_len);
227 /* Passed the filter. */
232 /* pru_accept is EOPNOTSUPP */
235 rts_attach(netmsg_t msg)
237 struct socket *so = msg->base.nm_so;
238 struct pru_attach_info *ai = msg->attach.nm_ai;
241 int proto = msg->attach.nm_proto;
245 if (sotorawcb(so) != NULL) {
250 rop = kmalloc(sizeof *rop, M_PCB, M_WAITOK | M_ZERO);
254 * The critical section is necessary to block protocols from sending
255 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
256 * this PCB is extant but incompletely initialized.
257 * Probably we should try to do more of this work beforehand and
258 * eliminate the critical section.
261 soreference(so); /* so_pcb assignment */
262 error = raw_attach(so, proto, ai->sb_rlimit);
268 switch(rp->rcb_proto.sp_protocol) {
273 route_cb.ip6_count++;
276 rp->rcb_faddr = &route_src;
277 rp->rcb_filter = rts_filter;
278 route_cb.any_count++;
280 so->so_options |= SO_USELOOPBACK;
284 lwkt_replymsg(&msg->lmsg, error);
288 rts_bind(netmsg_t msg)
291 raw_usrreqs.pru_bind(msg); /* xxx just EINVAL */
292 /* msg invalid now */
297 rts_connect(netmsg_t msg)
300 raw_usrreqs.pru_connect(msg); /* XXX just EINVAL */
301 /* msg invalid now */
305 /* pru_connect2 is EOPNOTSUPP */
306 /* pru_control is EOPNOTSUPP */
309 rts_detach(netmsg_t msg)
311 struct socket *so = msg->base.nm_so;
312 struct rawcb *rp = sotorawcb(so);
313 struct routecb *rop = (struct routecb *)rp;
316 if (rop->rocb_missfilterlen != 0)
317 kfree(rop->rocb_missfilter, M_PCB);
319 switch(rp->rcb_proto.sp_protocol) {
324 route_cb.ip6_count--;
327 route_cb.any_count--;
329 raw_usrreqs.pru_detach(msg);
330 /* msg invalid now */
335 rts_disconnect(netmsg_t msg)
338 raw_usrreqs.pru_disconnect(msg);
339 /* msg invalid now */
343 /* pru_listen is EOPNOTSUPP */
346 rts_peeraddr(netmsg_t msg)
349 raw_usrreqs.pru_peeraddr(msg);
350 /* msg invalid now */
354 /* pru_rcvd is EOPNOTSUPP */
355 /* pru_rcvoob is EOPNOTSUPP */
358 rts_send(netmsg_t msg)
361 raw_usrreqs.pru_send(msg);
362 /* msg invalid now */
366 /* pru_sense is null */
369 rts_shutdown(netmsg_t msg)
372 raw_usrreqs.pru_shutdown(msg);
373 /* msg invalid now */
378 rts_sockaddr(netmsg_t msg)
381 raw_usrreqs.pru_sockaddr(msg);
382 /* msg invalid now */
386 static struct pr_usrreqs route_usrreqs = {
387 .pru_abort = rts_abort,
388 .pru_accept = pr_generic_notsupp,
389 .pru_attach = rts_attach,
390 .pru_bind = rts_bind,
391 .pru_connect = rts_connect,
392 .pru_connect2 = pr_generic_notsupp,
393 .pru_control = pr_generic_notsupp,
394 .pru_detach = rts_detach,
395 .pru_disconnect = rts_disconnect,
396 .pru_listen = pr_generic_notsupp,
397 .pru_peeraddr = rts_peeraddr,
398 .pru_rcvd = pr_generic_notsupp,
399 .pru_rcvoob = pr_generic_notsupp,
400 .pru_send = rts_send,
401 .pru_sense = pru_sense_null,
402 .pru_shutdown = rts_shutdown,
403 .pru_sockaddr = rts_sockaddr,
404 .pru_sosend = sosend,
405 .pru_soreceive = soreceive
408 static __inline sa_family_t
409 familyof(struct sockaddr *sa)
411 return (sa != NULL ? sa->sa_family : 0);
415 * Routing socket input function. The packet must be serialized onto cpu 0.
416 * We use the cpu0_soport() netisr processing loop to handle it.
418 * This looks messy but it means that anyone, including interrupt code,
419 * can send a message to the routing socket.
422 rts_input_handler(netmsg_t msg)
424 static const struct sockaddr route_dst = { 2, PF_ROUTE, };
425 struct sockproto route_proto;
426 struct netmsg_packet *pmsg = &msg->packet;
431 family = pmsg->base.lmsg.u.ms_result;
432 route_proto.sp_family = PF_ROUTE;
433 route_proto.sp_protocol = family;
438 skip = m->m_pkthdr.header;
439 m->m_pkthdr.header = NULL;
441 raw_input(m, &route_proto, &route_src, &route_dst, skip);
445 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip)
447 struct netmsg_packet *pmsg;
452 port = netisr_cpuport(0); /* XXX same as for routing socket */
453 pmsg = &m->m_hdr.mh_netmsg;
454 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
455 0, rts_input_handler);
457 pmsg->base.lmsg.u.ms_result = family;
458 m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */
459 lwkt_sendmsg(port, &pmsg->base.lmsg);
463 rts_input(struct mbuf *m, sa_family_t family)
465 rts_input_skip(m, family, NULL);
469 route_ctloutput(netmsg_t msg)
471 struct socket *so = msg->ctloutput.base.nm_so;
472 struct sockopt *sopt = msg->ctloutput.nm_sopt;
473 struct routecb *rop = sotoroutecb(so);
475 unsigned int msgfilter;
476 unsigned char *cp, *ep;
480 if (sopt->sopt_level != AF_ROUTE) {
487 switch (sopt->sopt_dir) {
489 switch (sopt->sopt_name) {
490 case ROUTE_MSGFILTER:
491 error = soopt_to_kbuf(sopt, &msgfilter,
492 sizeof(msgfilter), sizeof(msgfilter));
494 rop->rocb_msgfilter = msgfilter;
497 /* Validate the data */
500 ep = cp + sopt->sopt_valsize;
503 offsetof(struct sockaddr, sa_len) +
506 if (++len > RO_FILTSA_MAX) {
510 sa = (struct sockaddr *)cp;
511 cp += RT_ROUNDUP(sa->sa_len);
518 if (rop->rocb_missfilterlen != 0)
519 kfree(rop->rocb_missfilter, M_PCB);
520 if (sopt->sopt_valsize != 0) {
521 rop->rocb_missfilter =
522 kmalloc(sopt->sopt_valsize,
523 M_PCB, M_WAITOK | M_NULLOK);
524 if (rop->rocb_missfilter == NULL) {
525 rop->rocb_missfilterlen = 0;
530 rop->rocb_missfilter = NULL;
531 rop->rocb_missfilterlen = sopt->sopt_valsize;
532 if (rop->rocb_missfilterlen != 0)
533 memcpy(rop->rocb_missfilter, sopt->sopt_val,
534 rop->rocb_missfilterlen);
542 switch (sopt->sopt_name) {
543 case ROUTE_MSGFILTER:
544 msgfilter = rop->rocb_msgfilter;
545 soopt_from_kbuf(sopt, &msgfilter, sizeof(msgfilter));
548 soopt_from_kbuf(sopt, rop->rocb_missfilter,
549 rop->rocb_missfilterlen);
557 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
563 reallocbuf_nofree(void *ptr, size_t len, size_t olen)
567 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
570 bcopy(ptr, newptr, olen);
572 bzero((char *)newptr + olen, len - olen);
578 * Internal helper routine for route_output().
581 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
582 struct rt_addrinfo *rtinfo)
585 struct rt_msghdr *rtm = *prtm;
587 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
588 rtinfo->rti_dst = rt_key(rt);
589 rtinfo->rti_gateway = rt->rt_gateway;
590 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */
591 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */
592 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
593 if (rt->rt_ifp != NULL) {
594 rtinfo->rti_ifpaddr =
595 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])
597 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
598 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
599 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
600 rtm->rtm_index = rt->rt_ifp->if_index;
602 rtinfo->rti_ifpaddr = NULL;
603 rtinfo->rti_ifaaddr = NULL;
605 } else if (rt->rt_ifp != NULL) {
606 rtm->rtm_index = rt->rt_ifp->if_index;
609 msglen = rt_msgsize(rtm->rtm_type, rtinfo);
610 if (rtm->rtm_msglen < msglen) {
611 /* NOTE: Caller will free the old rtm accordingly */
612 rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen);
617 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
619 rtm->rtm_flags = rt->rt_flags;
620 rtm->rtm_rmx = rt->rt_rmx;
621 rtm->rtm_addrs = rtinfo->rti_addrs;
627 struct rt_msghdr *bak_rtm;
628 struct rt_msghdr *new_rtm;
632 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt,
633 struct rt_addrinfo *rtinfo)
635 struct rt_msghdr *rtm = arg->new_rtm;
638 error = _fillrtmsg(&rtm, rt, rtinfo);
640 if (arg->new_rtm != rtm) {
642 * _fillrtmsg() just allocated a new rtm;
643 * if the previously allocated rtm is not
644 * the backing rtm, it should be freed.
646 if (arg->new_rtm != arg->bak_rtm)
647 kfree(arg->new_rtm, M_RTABLE);
654 static void route_output_add_callback(int, int, struct rt_addrinfo *,
655 struct rtentry *, void *);
656 static void route_output_delete_callback(int, int, struct rt_addrinfo *,
657 struct rtentry *, void *);
658 static int route_output_get_callback(int, struct rt_addrinfo *,
659 struct rtentry *, void *, int);
660 static int route_output_change_callback(int, struct rt_addrinfo *,
661 struct rtentry *, void *, int);
662 static int route_output_lock_callback(int, struct rt_addrinfo *,
663 struct rtentry *, void *, int);
667 route_output(struct mbuf *m, struct socket *so, ...)
670 struct rt_msghdr *rtm = NULL;
671 struct rawcb *rp = NULL;
672 struct pr_output_info *oi;
673 struct rt_addrinfo rtinfo;
681 oi = __va_arg(ap, struct pr_output_info *);
684 family = familyof(NULL);
686 #define gotoerr(e) { error = e; goto flush;}
689 (m->m_len < sizeof(long) &&
690 (m = m_pullup(m, sizeof(long))) == NULL))
692 len = m->m_pkthdr.len;
693 if (len < sizeof(struct rt_msghdr) ||
694 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
697 rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
701 m_copydata(m, 0, len, (caddr_t)rtm);
702 if (rtm->rtm_version != RTM_VERSION)
703 gotoerr(EPROTONOSUPPORT);
705 rtm->rtm_pid = oi->p_pid;
706 bzero(&rtinfo, sizeof(struct rt_addrinfo));
707 rtinfo.rti_addrs = rtm->rtm_addrs;
708 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0)
711 rtinfo.rti_flags = rtm->rtm_flags;
712 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
713 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
716 family = familyof(rtinfo.rti_dst);
719 * Verify that the caller has the appropriate privilege; RTM_GET
720 * is the only operation the non-superuser is allowed.
722 if (rtm->rtm_type != RTM_GET &&
723 priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0)
726 if (rtinfo.rti_genmask != NULL) {
727 error = rtmask_add_global(rtinfo.rti_genmask,
728 rtm->rtm_type != RTM_GET ?
729 RTREQ_PRIO_HIGH : RTREQ_PRIO_NORM);
734 switch (rtm->rtm_type) {
736 if (rtinfo.rti_gateway == NULL) {
739 error = rtrequest1_global(RTM_ADD, &rtinfo,
740 route_output_add_callback, rtm, RTREQ_PRIO_HIGH);
745 * Backing rtm (bak_rtm) could _not_ be freed during
746 * rtrequest1_global or rtsearch_global, even if the
747 * callback reallocates the rtm due to its size changes,
748 * since rtinfo points to the backing rtm's memory area.
749 * After rtrequest1_global or rtsearch_global returns,
750 * it is safe to free the backing rtm, since rtinfo will
751 * not be used anymore.
753 * new_rtm will be used to save the new rtm allocated
754 * by rtrequest1_global or rtsearch_global.
758 error = rtrequest1_global(RTM_DELETE, &rtinfo,
759 route_output_delete_callback, &arg, RTREQ_PRIO_HIGH);
761 if (rtm != arg.bak_rtm)
762 kfree(arg.bak_rtm, M_RTABLE);
765 /* See the comment in RTM_DELETE */
768 error = rtsearch_global(RTM_GET, &rtinfo,
769 route_output_get_callback, &arg, RTS_NOEXACTMATCH,
772 if (rtm != arg.bak_rtm)
773 kfree(arg.bak_rtm, M_RTABLE);
776 error = rtsearch_global(RTM_CHANGE, &rtinfo,
777 route_output_change_callback, rtm, RTS_EXACTMATCH,
781 error = rtsearch_global(RTM_LOCK, &rtinfo,
782 route_output_lock_callback, rtm, RTS_EXACTMATCH,
792 rtm->rtm_errno = error;
794 rtm->rtm_flags |= RTF_DONE;
798 * Check to see if we don't want our own messages.
800 if (!(so->so_options & SO_USELOOPBACK)) {
801 if (route_cb.any_count <= 1) {
803 kfree(rtm, M_RTABLE);
807 /* There is another listener, so construct message */
811 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
812 if (m->m_pkthdr.len < rtm->rtm_msglen) {
815 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
816 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
817 kfree(rtm, M_RTABLE);
820 rts_input_skip(m, family, rp);
825 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
826 struct rtentry *rt, void *arg)
828 struct rt_msghdr *rtm = arg;
830 if (error == 0 && rt != NULL) {
831 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
833 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
834 rt->rt_rmx.rmx_locks |=
835 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
836 if (rtinfo->rti_genmask != NULL) {
837 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
838 if (rt->rt_genmask == NULL) {
840 * This should not happen, since we
841 * have already installed genmask
842 * on each CPU before we reach here.
844 panic("genmask is gone!?");
847 rt->rt_genmask = NULL;
849 rtm->rtm_index = rt->rt_ifp->if_index;
854 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
855 struct rtentry *rt, void *arg)
857 if (error == 0 && rt) {
859 if (fillrtmsg(arg, rt, rtinfo) != 0) {
861 /* XXX no way to return the error */
865 if (rt && rt->rt_refcnt == 0) {
872 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo,
873 struct rtentry *rt, void *arg, int found_cnt)
875 int error, found = 0;
877 if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0)
880 error = fillrtmsg(arg, rt, rtinfo);
881 if (!error && found) {
882 /* Got the exact match, we could return now! */
889 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo,
890 struct rtentry *rt, void *arg, int found_cnt)
892 struct rt_msghdr *rtm = arg;
897 * new gateway could require new ifaddr, ifp;
898 * flags may also be different; ifp may be specified
899 * by ll sockaddr when protocol address is ambiguous
901 if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
902 rtinfo->rti_ifpaddr != NULL ||
903 (rtinfo->rti_ifaaddr != NULL &&
904 !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) {
905 error = rt_getifa(rtinfo);
909 if (rtinfo->rti_gateway != NULL) {
911 * We only need to generate rtmsg upon the
912 * first route to be changed.
914 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway);
918 if ((ifa = rtinfo->rti_ifa) != NULL) {
919 struct ifaddr *oifa = rt->rt_ifa;
922 if (oifa && oifa->ifa_rtrequest)
923 oifa->ifa_rtrequest(RTM_DELETE, rt);
927 rt->rt_ifp = rtinfo->rti_ifp;
930 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
931 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
932 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt);
933 if (rtinfo->rti_genmask != NULL) {
934 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
935 if (rt->rt_genmask == NULL) {
937 * This should not happen, since we
938 * have already installed genmask
939 * on each CPU before we reach here.
941 panic("genmask is gone!?");
944 rtm->rtm_index = rt->rt_ifp->if_index;
946 rt_rtmsg(RTM_CHANGE, rt, rt->rt_ifp, 0);
952 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo,
953 struct rtentry *rt, void *arg,
954 int found_cnt __unused)
956 struct rt_msghdr *rtm = arg;
958 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
959 rt->rt_rmx.rmx_locks |=
960 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
965 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
967 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
968 setmetric(RTV_RPIPE, rmx_recvpipe);
969 setmetric(RTV_SPIPE, rmx_sendpipe);
970 setmetric(RTV_SSTHRESH, rmx_ssthresh);
971 setmetric(RTV_RTT, rmx_rtt);
972 setmetric(RTV_RTTVAR, rmx_rttvar);
973 setmetric(RTV_HOPCOUNT, rmx_hopcount);
974 setmetric(RTV_MTU, rmx_mtu);
975 setmetric(RTV_EXPIRE, rmx_expire);
976 setmetric(RTV_MSL, rmx_msl);
977 setmetric(RTV_IWMAXSEGS, rmx_iwmaxsegs);
978 setmetric(RTV_IWCAPSEGS, rmx_iwcapsegs);
983 * Extract the addresses of the passed sockaddrs.
984 * Do a little sanity checking so as to avoid bad memory references.
985 * This data is derived straight from userland.
988 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
993 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
994 if ((rtinfo->rti_addrs & (1 << i)) == 0)
996 sa = (struct sockaddr *)cp;
1000 if ((cp + sa->sa_len) > cplim) {
1005 * There are no more... Quit now.
1006 * If there are more bits, they are in error.
1007 * I've seen this. route(1) can evidently generate these.
1008 * This causes kernel to core dump.
1009 * For compatibility, if we see this, point to a safe address.
1011 if (sa->sa_len == 0) {
1012 static struct sockaddr sa_zero = {
1013 sizeof sa_zero, AF_INET,
1016 rtinfo->rti_info[i] = &sa_zero;
1017 kprintf("rtsock: received more addr bits than sockaddrs.\n");
1018 return (0); /* should be EINVAL but for compat */
1021 /* Accept the sockaddr. */
1022 rtinfo->rti_info[i] = sa;
1023 cp += RT_ROUNDUP(sa->sa_len);
1029 rt_msghdrsize(int type)
1034 return sizeof(struct ifa_msghdr);
1037 return sizeof(struct ifma_msghdr);
1039 return sizeof(struct if_msghdr);
1040 case RTM_IFANNOUNCE:
1042 return sizeof(struct if_announcemsghdr);
1044 return sizeof(struct rt_msghdr);
1049 rt_msgsize(int type, const struct rt_addrinfo *rtinfo)
1053 len = rt_msghdrsize(type);
1054 for (i = 0; i < RTAX_MAX; i++) {
1055 if (rtinfo->rti_info[i] != NULL)
1056 len += RT_ROUNDUP(rtinfo->rti_info[i]->sa_len);
1063 * Build a routing message in a buffer.
1064 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
1065 * to the end of the buffer after the message header.
1067 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
1068 * This side-effect can be avoided if we reorder the addrs bitmask field in all
1069 * the route messages to line up so we can set it here instead of back in the
1072 * NOTE! The buffer may already contain a partially filled-out rtm via
1076 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
1078 struct rt_msghdr *rtm;
1082 rtm = (struct rt_msghdr *) buf;
1083 rtm->rtm_version = RTM_VERSION;
1084 rtm->rtm_type = type;
1085 rtm->rtm_msglen = msglen;
1087 cp = (char *)buf + rt_msghdrsize(type);
1088 rtinfo->rti_addrs = 0;
1089 for (i = 0; i < RTAX_MAX; i++) {
1090 struct sockaddr *sa;
1092 if ((sa = rtinfo->rti_info[i]) == NULL)
1094 rtinfo->rti_addrs |= (1 << i);
1095 dlen = RT_ROUNDUP(sa->sa_len);
1096 bcopy(sa, cp, dlen);
1102 * Build a routing message in a mbuf chain.
1103 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
1104 * to the end of the mbuf after the message header.
1106 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
1107 * This side-effect can be avoided if we reorder the addrs bitmask field in all
1108 * the route messages to line up so we can set it here instead of back in the
1111 static struct mbuf *
1112 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1115 struct rt_msghdr *rtm;
1119 hlen = rt_msghdrsize(type);
1120 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
1122 m = m_getl(hlen, M_NOWAIT, MT_DATA, M_PKTHDR, NULL);
1126 m->m_pkthdr.len = m->m_len = hlen;
1127 m->m_pkthdr.rcvif = NULL;
1128 rtinfo->rti_addrs = 0;
1130 for (i = 0; i < RTAX_MAX; i++) {
1131 struct sockaddr *sa;
1134 if ((sa = rtinfo->rti_info[i]) == NULL)
1136 rtinfo->rti_addrs |= (1 << i);
1137 dlen = RT_ROUNDUP(sa->sa_len);
1138 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
1141 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
1145 rtm = mtod(m, struct rt_msghdr *);
1147 rtm->rtm_msglen = len;
1148 rtm->rtm_version = RTM_VERSION;
1149 rtm->rtm_type = type;
1154 * This routine is called to generate a message from the routing
1155 * socket indicating that a redirect has occurred, a routing lookup
1156 * has failed, or that a protocol has detected timeouts to a particular
1160 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1162 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
1163 struct rt_msghdr *rtm;
1166 if (route_cb.any_count == 0)
1168 m = rt_msg_mbuf(type, rtinfo);
1171 rtm = mtod(m, struct rt_msghdr *);
1172 rtm->rtm_flags = RTF_DONE | flags;
1173 rtm->rtm_errno = error;
1174 rtm->rtm_addrs = rtinfo->rti_addrs;
1175 rts_input(m, familyof(dst));
1179 rt_dstmsg(int type, struct sockaddr *dst, int error)
1181 struct rt_msghdr *rtm;
1182 struct rt_addrinfo addrs;
1185 if (route_cb.any_count == 0)
1187 bzero(&addrs, sizeof(struct rt_addrinfo));
1188 addrs.rti_info[RTAX_DST] = dst;
1189 m = rt_msg_mbuf(type, &addrs);
1192 rtm = mtod(m, struct rt_msghdr *);
1193 rtm->rtm_flags = RTF_DONE;
1194 rtm->rtm_errno = error;
1195 rtm->rtm_addrs = addrs.rti_addrs;
1196 rts_input(m, familyof(dst));
1200 * This routine is called to generate a message from the routing
1201 * socket indicating that the status of a network interface has changed.
1204 rt_ifmsg(struct ifnet *ifp)
1206 struct if_msghdr *ifm;
1208 struct rt_addrinfo rtinfo;
1210 if (route_cb.any_count == 0)
1212 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1213 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
1216 ifm = mtod(m, struct if_msghdr *);
1217 ifm->ifm_index = ifp->if_index;
1218 ifm->ifm_flags = ifp->if_flags;
1219 ifm->ifm_data = ifp->if_data;
1225 rt_ifamsg(int cmd, struct ifaddr *ifa)
1227 struct ifa_msghdr *ifam;
1228 struct rt_addrinfo rtinfo;
1230 struct ifnet *ifp = ifa->ifa_ifp;
1232 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1233 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1234 rtinfo.rti_ifpaddr =
1235 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1236 rtinfo.rti_netmask = ifa->ifa_netmask;
1237 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1239 m = rt_msg_mbuf(cmd, &rtinfo);
1243 ifam = mtod(m, struct ifa_msghdr *);
1244 ifam->ifam_index = ifp->if_index;
1245 ifam->ifam_flags = ifa->ifa_flags;
1246 ifam->ifam_addrs = rtinfo.rti_addrs;
1247 ifam->ifam_addrflags = if_addrflags(ifa);
1248 ifam->ifam_metric = ifa->ifa_metric;
1250 rts_input(m, familyof(ifa->ifa_addr));
1254 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
1256 struct rt_msghdr *rtm;
1257 struct rt_addrinfo rtinfo;
1259 struct sockaddr *dst;
1264 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1265 rtinfo.rti_dst = dst = rt_key(rt);
1266 rtinfo.rti_gateway = rt->rt_gateway;
1267 rtinfo.rti_netmask = rt_mask(rt);
1269 rtinfo.rti_ifpaddr =
1270 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1272 if (rt->rt_ifa != NULL)
1273 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1275 m = rt_msg_mbuf(cmd, &rtinfo);
1279 rtm = mtod(m, struct rt_msghdr *);
1281 rtm->rtm_index = ifp->if_index;
1282 rtm->rtm_flags |= rt->rt_flags;
1283 rtm->rtm_errno = error;
1284 rtm->rtm_addrs = rtinfo.rti_addrs;
1286 rts_input(m, familyof(dst));
1290 * This is called to generate messages from the routing socket
1291 * indicating a network interface has had addresses associated with it.
1292 * if we ever reverse the logic and replace messages TO the routing
1293 * socket indicate a request to configure interfaces, then it will
1294 * be unnecessary as the routing socket will automatically generate
1298 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1300 if (route_cb.any_count == 0)
1303 if (cmd == RTM_ADD) {
1304 rt_ifamsg(RTM_NEWADDR, ifa);
1305 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
1307 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
1308 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
1309 rt_ifamsg(RTM_DELADDR, ifa);
1314 * This is the analogue to the rt_newaddrmsg which performs the same
1315 * function but for multicast group memberhips. This is easier since
1316 * there is no route state to worry about.
1319 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1321 struct rt_addrinfo rtinfo;
1322 struct mbuf *m = NULL;
1323 struct ifnet *ifp = ifma->ifma_ifp;
1324 struct ifma_msghdr *ifmam;
1326 if (route_cb.any_count == 0)
1329 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1330 rtinfo.rti_ifaaddr = ifma->ifma_addr;
1331 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1332 rtinfo.rti_ifpaddr =
1333 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1336 * If a link-layer address is present, present it as a ``gateway''
1337 * (similarly to how ARP entries, e.g., are presented).
1339 rtinfo.rti_gateway = ifma->ifma_lladdr;
1341 m = rt_msg_mbuf(cmd, &rtinfo);
1345 ifmam = mtod(m, struct ifma_msghdr *);
1346 ifmam->ifmam_index = ifp->if_index;
1347 ifmam->ifmam_addrs = rtinfo.rti_addrs;
1349 rts_input(m, familyof(ifma->ifma_addr));
1352 static struct mbuf *
1353 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1354 struct rt_addrinfo *info)
1356 struct if_announcemsghdr *ifan;
1359 if (route_cb.any_count == 0)
1362 bzero(info, sizeof(*info));
1363 m = rt_msg_mbuf(type, info);
1367 ifan = mtod(m, struct if_announcemsghdr *);
1368 ifan->ifan_index = ifp->if_index;
1369 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1370 ifan->ifan_what = what;
1375 * This is called to generate routing socket messages indicating
1376 * IEEE80211 wireless events.
1377 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1380 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1382 struct rt_addrinfo info;
1385 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1390 * Append the ieee80211 data. Try to stick it in the
1391 * mbuf containing the ifannounce msg; otherwise allocate
1392 * a new mbuf and append.
1394 * NB: we assume m is a single mbuf.
1396 if (data_len > M_TRAILINGSPACE(m)) {
1397 /* XXX use m_getb(data_len, M_NOWAIT, MT_DATA, 0); */
1398 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1403 KKASSERT(data_len <= M_TRAILINGSPACE(n));
1404 bcopy(data, mtod(n, void *), data_len);
1405 n->m_len = data_len;
1407 } else if (data_len > 0) {
1408 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1409 m->m_len += data_len;
1412 if (m->m_flags & M_PKTHDR)
1413 m->m_pkthdr.len += data_len;
1414 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1419 * This is called to generate routing socket messages indicating
1420 * network interface arrival and departure.
1423 rt_ifannouncemsg(struct ifnet *ifp, int what)
1425 struct rt_addrinfo addrinfo;
1428 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo);
1434 resizewalkarg(struct walkarg *w, int len)
1438 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1441 if (w->w_tmem != NULL)
1442 kfree(w->w_tmem, M_RTABLE);
1444 w->w_tmemsize = len;
1451 ifnet_compute_stats(struct ifnet *ifp)
1453 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1454 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1455 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1456 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1457 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1458 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1459 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1460 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1461 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1462 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1463 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1467 if_addrflags(const struct ifaddr *ifa)
1469 switch (ifa->ifa_addr->sa_family) {
1472 return ((const struct in6_ifaddr *)ifa)->ia6_flags;
1480 sysctl_iflist(int af, struct walkarg *w)
1483 struct rt_addrinfo rtinfo;
1486 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1489 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1490 struct ifaddr_container *ifac, *ifac_mark;
1491 struct ifaddr_marker mark;
1492 struct ifaddrhead *head;
1495 if (w->w_arg && w->w_arg != ifp->if_index)
1497 head = &ifp->if_addrheads[mycpuid];
1499 * There is no need to reference the first ifaddr
1500 * even if the following resizewalkarg() blocks,
1501 * since the first ifaddr will not be destroyed
1502 * when the ifnet lock is held.
1504 ifac = TAILQ_FIRST(head);
1506 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1507 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1508 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0) {
1512 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1513 rtinfo.rti_ifpaddr = NULL;
1514 if (w->w_req != NULL && w->w_tmem != NULL) {
1515 struct if_msghdr *ifm = w->w_tmem;
1517 ifm->ifm_index = ifp->if_index;
1518 ifm->ifm_flags = ifp->if_flags;
1519 ifnet_compute_stats(ifp);
1520 ifm->ifm_data = ifp->if_data;
1521 ifm->ifm_addrs = rtinfo.rti_addrs;
1522 error = SYSCTL_OUT(w->w_req, ifm, msglen);
1529 * Add a marker, since SYSCTL_OUT() could block and during
1530 * that period the list could be changed.
1532 ifa_marker_init(&mark, ifp);
1533 ifac_mark = &mark.ifac;
1534 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
1535 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
1536 TAILQ_REMOVE(head, ifac_mark, ifa_link);
1537 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
1542 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1545 if (af && af != ifa->ifa_addr->sa_family)
1547 if (curproc->p_ucred->cr_prison &&
1548 prison_if(curproc->p_ucred, ifa->ifa_addr))
1550 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1551 rtinfo.rti_netmask = ifa->ifa_netmask;
1552 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1553 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1555 * Keep a reference on this ifaddr, so that it will
1556 * not be destroyed if the following resizewalkarg()
1560 if (w->w_tmemsize < msglen &&
1561 resizewalkarg(w, msglen) != 0) {
1563 TAILQ_REMOVE(head, ifac_mark, ifa_link);
1567 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1568 if (w->w_req != NULL) {
1569 struct ifa_msghdr *ifam = w->w_tmem;
1571 ifam->ifam_index = ifa->ifa_ifp->if_index;
1572 ifam->ifam_flags = ifa->ifa_flags;
1573 ifam->ifam_addrs = rtinfo.rti_addrs;
1574 ifam->ifam_addrflags = if_addrflags(ifa);
1575 ifam->ifam_metric = ifa->ifa_metric;
1576 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1579 TAILQ_REMOVE(head, ifac_mark, ifa_link);
1586 TAILQ_REMOVE(head, ifac_mark, ifa_link);
1587 rtinfo.rti_netmask = NULL;
1588 rtinfo.rti_ifaaddr = NULL;
1589 rtinfo.rti_bcastaddr = NULL;
1596 rttable_walkarg_create(struct rttable_walkarg *w, int op, int arg)
1598 struct rt_addrinfo rtinfo;
1599 struct sockaddr_storage ss;
1602 memset(w, 0, sizeof(*w));
1606 memset(&ss, 0, sizeof(ss));
1607 ss.ss_len = sizeof(ss);
1609 memset(&rtinfo, 0, sizeof(rtinfo));
1610 for (i = 0; i < RTAX_MAX; ++i)
1611 rtinfo.rti_info[i] = (struct sockaddr *)&ss;
1612 msglen = rt_msgsize(RTM_GET, &rtinfo);
1614 w->w_bufsz = msglen * RTTABLE_DUMP_MSGCNT_MAX;
1615 w->w_buf = kmalloc(w->w_bufsz, M_TEMP, M_WAITOK | M_NULLOK);
1616 if (w->w_buf == NULL)
1622 rttable_walkarg_destroy(struct rttable_walkarg *w)
1624 kfree(w->w_buf, M_TEMP);
1628 rttable_entry_rtinfo(struct rt_addrinfo *rtinfo, struct radix_node *rn)
1630 struct rtentry *rt = (struct rtentry *)rn;
1632 bzero(rtinfo, sizeof(*rtinfo));
1633 rtinfo->rti_dst = rt_key(rt);
1634 rtinfo->rti_gateway = rt->rt_gateway;
1635 rtinfo->rti_netmask = rt_mask(rt);
1636 rtinfo->rti_genmask = rt->rt_genmask;
1637 if (rt->rt_ifp != NULL) {
1638 rtinfo->rti_ifpaddr =
1639 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1640 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
1641 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1642 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1647 rttable_walk_entry(struct radix_node *rn, void *xw)
1649 struct rttable_walkarg *w = xw;
1650 struct rtentry *rt = (struct rtentry *)rn;
1651 struct rt_addrinfo rtinfo;
1652 struct rt_msghdr *rtm;
1653 boolean_t save = FALSE;
1654 int msglen, w_bufleft;
1657 rttable_entry_rtinfo(&rtinfo, rn);
1658 msglen = rt_msgsize(RTM_GET, &rtinfo);
1660 w_bufleft = w->w_bufsz - w->w_buflen;
1662 if (rn->rn_dupedkey != NULL) {
1663 struct radix_node *rn1 = rn;
1664 int total_msglen = msglen;
1667 * Make sure that we have enough space left for all
1668 * dupedkeys, since rn_walktree_at always starts
1669 * from the first dupedkey.
1671 while ((rn1 = rn1->rn_dupedkey) != NULL) {
1672 struct rt_addrinfo rtinfo1;
1675 if (rn1->rn_flags & RNF_ROOT)
1678 rttable_entry_rtinfo(&rtinfo1, rn1);
1679 msglen1 = rt_msgsize(RTM_GET, &rtinfo1);
1680 total_msglen += msglen1;
1683 if (total_msglen > w_bufleft) {
1684 if (total_msglen > w->w_bufsz) {
1685 static int logged = 0;
1688 kprintf("buffer is too small for "
1689 "all dupedkeys, increase "
1690 "RTTABLE_DUMP_MSGCNT_MAX\n");
1697 } else if (msglen > w_bufleft) {
1703 * Not enough buffer left; remember the position
1704 * to start from upon next round.
1706 KASSERT(msglen <= w->w_bufsz, ("msg too long %d", msglen));
1708 KASSERT(rtinfo.rti_dst->sa_len <= sizeof(w->w_key0),
1709 ("key too long %d", rtinfo.rti_dst->sa_len));
1710 memset(&w->w_key0, 0, sizeof(w->w_key0));
1711 memcpy(&w->w_key0, rtinfo.rti_dst, rtinfo.rti_dst->sa_len);
1712 w->w_key = (const char *)&w->w_key0;
1714 if (rtinfo.rti_netmask != NULL) {
1716 rtinfo.rti_netmask->sa_len <= sizeof(w->w_mask0),
1717 ("mask too long %d", rtinfo.rti_netmask->sa_len));
1718 memset(&w->w_mask0, 0, sizeof(w->w_mask0));
1719 memcpy(&w->w_mask0, rtinfo.rti_netmask,
1720 rtinfo.rti_netmask->sa_len);
1721 w->w_mask = (const char *)&w->w_mask0;
1728 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1731 ptr = ((uint8_t *)w->w_buf) + w->w_buflen;
1732 rt_msg_buffer(RTM_GET, &rtinfo, ptr, msglen);
1734 rtm = (struct rt_msghdr *)ptr;
1735 rtm->rtm_flags = rt->rt_flags;
1736 rtm->rtm_use = rt->rt_use;
1737 rtm->rtm_rmx = rt->rt_rmx;
1738 rtm->rtm_index = rt->rt_ifp->if_index;
1739 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1740 rtm->rtm_addrs = rtinfo.rti_addrs;
1742 w->w_buflen += msglen;
1748 rttable_walk_dispatch(netmsg_t msg)
1750 struct netmsg_rttable_walk *nmsg = (struct netmsg_rttable_walk *)msg;
1751 struct radix_node_head *rnh = rt_tables[mycpuid][nmsg->af];
1752 struct rttable_walkarg *w = nmsg->w;
1755 error = rnh->rnh_walktree_at(rnh, w->w_key, w->w_mask,
1756 rttable_walk_entry, w);
1757 lwkt_replymsg(&nmsg->base.lmsg, error);
1761 sysctl_rttable(int af, struct sysctl_req *req, int op, int arg)
1763 struct rttable_walkarg w;
1766 error = rttable_walkarg_create(&w, op, arg);
1771 for (i = 1; i <= AF_MAX; i++) {
1772 if (rt_tables[mycpuid][i] != NULL && (af == 0 || af == i)) {
1776 struct netmsg_rttable_walk nmsg;
1778 netmsg_init(&nmsg.base, NULL,
1779 &curthread->td_msgport, 0,
1780 rttable_walk_dispatch);
1786 error = lwkt_domsg(netisr_cpuport(mycpuid),
1787 &nmsg.base.lmsg, 0);
1788 if (error && error != EJUSTRETURN)
1791 if (req != NULL && w.w_buflen > 0) {
1794 error1 = SYSCTL_OUT(req, w.w_buf,
1801 if (error == 0) /* done */
1807 rttable_walkarg_destroy(&w);
1812 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1814 int *name = (int *)arg1;
1815 u_int namelen = arg2;
1825 if (namelen != 3 && namelen != 4)
1828 bzero(&w, sizeof w);
1834 * Optional third argument specifies cpu, used primarily for
1835 * debugging the route table.
1838 if (name[3] < 0 || name[3] >= netisr_ncpus)
1843 * Target cpu is not specified, use cpu0 then, so that
1844 * the result set will be relatively stable.
1849 lwkt_migratecpu(cpu);
1854 error = sysctl_rttable(af, w.w_req, w.w_op, w.w_arg);
1858 error = sysctl_iflist(af, &w);
1861 if (w.w_tmem != NULL)
1862 kfree(w.w_tmem, M_RTABLE);
1864 lwkt_migratecpu(origcpu);
1868 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1871 * Definitions of protocols supported in the ROUTE domain.
1874 static struct domain routedomain; /* or at least forward */
1876 static struct protosw routesw[] = {
1878 .pr_type = SOCK_RAW,
1879 .pr_domain = &routedomain,
1881 .pr_flags = PR_ATOMIC|PR_ADDR,
1883 .pr_output = route_output,
1884 .pr_ctlinput = raw_ctlinput,
1885 .pr_ctloutput = route_ctloutput,
1886 .pr_ctlport = cpu0_ctlport,
1888 .pr_init = raw_init,
1889 .pr_usrreqs = &route_usrreqs
1893 static struct domain routedomain = {
1894 .dom_family = AF_ROUTE,
1895 .dom_name = "route",
1897 .dom_externalize = NULL,
1898 .dom_dispose = NULL,
1899 .dom_protosw = routesw,
1900 .dom_protoswNPROTOSW = &routesw[(sizeof routesw)/(sizeof routesw[0])],
1901 .dom_next = SLIST_ENTRY_INITIALIZER,
1902 .dom_rtattach = NULL,
1905 .dom_ifattach = NULL,
1906 .dom_ifdetach = NULL