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. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
66 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
67 * $DragonFly: src/sys/net/rtsock.c,v 1.45 2008/10/27 02:56:30 sephe Exp $
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
78 #include <sys/malloc.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/domain.h>
85 #include <sys/thread2.h>
86 #include <sys/socketvar2.h>
89 #include <net/route.h>
90 #include <net/raw_cb.h>
91 #include <net/netmsg2.h>
94 extern void sctp_add_ip_address(struct ifaddr *ifa);
95 extern void sctp_delete_ip_address(struct ifaddr *ifa);
98 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
100 static struct route_cb {
108 static const struct sockaddr route_src = { 2, PF_ROUTE, };
114 struct sysctl_req *w_req;
118 rt_msg_mbuf (int, struct rt_addrinfo *);
119 static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
120 static int rt_msgsize (int type, struct rt_addrinfo *rtinfo);
121 static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
122 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
123 static int sysctl_iflist (int af, struct walkarg *w);
124 static int route_output(struct mbuf *, struct socket *, ...);
125 static void rt_setmetrics (u_long, struct rt_metrics *,
126 struct rt_metrics *);
129 * It really doesn't make any sense at all for this code to share much
130 * with raw_usrreq.c, since its functionality is so restricted. XXX
133 rts_abort(struct socket *so)
138 error = raw_usrreqs.pru_abort(so);
143 /* pru_accept is EOPNOTSUPP */
146 rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
151 if (sotorawcb(so) != NULL)
152 return EISCONN; /* XXX panic? */
154 rp = kmalloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
157 * The critical section is necessary to block protocols from sending
158 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
159 * this PCB is extant but incompletely initialized.
160 * Probably we should try to do more of this work beforehand and
161 * eliminate the critical section.
165 soreference(so); /* so_pcb assignment */
166 error = raw_attach(so, proto, ai->sb_rlimit);
173 switch(rp->rcb_proto.sp_protocol) {
178 route_cb.ip6_count++;
181 route_cb.ipx_count++;
187 rp->rcb_faddr = &route_src;
188 route_cb.any_count++;
190 so->so_options |= SO_USELOOPBACK;
196 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
201 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
207 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
212 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
217 /* pru_connect2 is EOPNOTSUPP */
218 /* pru_control is EOPNOTSUPP */
221 rts_detach(struct socket *so)
223 struct rawcb *rp = sotorawcb(so);
228 switch(rp->rcb_proto.sp_protocol) {
233 route_cb.ip6_count--;
236 route_cb.ipx_count--;
242 route_cb.any_count--;
244 error = raw_usrreqs.pru_detach(so);
250 rts_disconnect(struct socket *so)
255 error = raw_usrreqs.pru_disconnect(so);
260 /* pru_listen is EOPNOTSUPP */
263 rts_peeraddr(struct socket *so, struct sockaddr **nam)
268 error = raw_usrreqs.pru_peeraddr(so, nam);
273 /* pru_rcvd is EOPNOTSUPP */
274 /* pru_rcvoob is EOPNOTSUPP */
277 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
278 struct mbuf *control, struct thread *td)
283 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
288 /* pru_sense is null */
291 rts_shutdown(struct socket *so)
296 error = raw_usrreqs.pru_shutdown(so);
302 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_accept = pru_accept_notsupp,
315 .pru_attach = rts_attach,
316 .pru_bind = rts_bind,
317 .pru_connect = rts_connect,
318 .pru_connect2 = pru_connect2_notsupp,
319 .pru_control = pru_control_notsupp,
320 .pru_detach = rts_detach,
321 .pru_disconnect = rts_disconnect,
322 .pru_listen = pru_listen_notsupp,
323 .pru_peeraddr = rts_peeraddr,
324 .pru_rcvd = pru_rcvd_notsupp,
325 .pru_rcvoob = pru_rcvoob_notsupp,
326 .pru_send = rts_send,
327 .pru_sense = pru_sense_null,
328 .pru_shutdown = rts_shutdown,
329 .pru_sockaddr = rts_sockaddr,
330 .pru_sosend = sosend,
331 .pru_soreceive = soreceive
334 static __inline sa_family_t
335 familyof(struct sockaddr *sa)
337 return (sa != NULL ? sa->sa_family : 0);
341 * Routing socket input function. The packet must be serialized onto cpu 0.
342 * We use the cpu0_soport() netisr processing loop to handle it.
344 * This looks messy but it means that anyone, including interrupt code,
345 * can send a message to the routing socket.
348 rts_input_handler(struct netmsg *msg)
350 static const struct sockaddr route_dst = { 2, PF_ROUTE, };
351 struct sockproto route_proto;
352 struct netmsg_packet *pmsg;
358 family = pmsg->nm_netmsg.nm_lmsg.u.ms_result;
359 route_proto.sp_family = PF_ROUTE;
360 route_proto.sp_protocol = family;
365 skip = m->m_pkthdr.header;
366 m->m_pkthdr.header = NULL;
368 raw_input(m, &route_proto, &route_src, &route_dst, skip);
372 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip)
374 struct netmsg_packet *pmsg;
379 port = cpu0_soport(NULL, NULL, NULL); /* same as for routing socket */
380 pmsg = &m->m_hdr.mh_netmsg;
381 netmsg_init(&pmsg->nm_netmsg, NULL, &netisr_apanic_rport,
382 0, rts_input_handler);
384 pmsg->nm_netmsg.nm_lmsg.u.ms_result = family;
385 m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */
386 lwkt_sendmsg(port, &pmsg->nm_netmsg.nm_lmsg);
390 rts_input(struct mbuf *m, sa_family_t family)
392 rts_input_skip(m, family, NULL);
396 reallocbuf_nofree(void *ptr, size_t len, size_t olen)
400 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
403 bcopy(ptr, newptr, olen);
408 * Internal helper routine for route_output().
411 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
412 struct rt_addrinfo *rtinfo)
415 struct rt_msghdr *rtm = *prtm;
417 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
418 rtinfo->rti_dst = rt_key(rt);
419 rtinfo->rti_gateway = rt->rt_gateway;
420 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */
421 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */
422 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
423 if (rt->rt_ifp != NULL) {
424 rtinfo->rti_ifpaddr =
425 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])
427 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
428 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
429 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
430 rtm->rtm_index = rt->rt_ifp->if_index;
432 rtinfo->rti_ifpaddr = NULL;
433 rtinfo->rti_ifaaddr = NULL;
435 } else if (rt->rt_ifp != NULL) {
436 rtm->rtm_index = rt->rt_ifp->if_index;
439 msglen = rt_msgsize(rtm->rtm_type, rtinfo);
440 if (rtm->rtm_msglen < msglen) {
441 /* NOTE: Caller will free the old rtm accordingly */
442 rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen);
447 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
449 rtm->rtm_flags = rt->rt_flags;
450 rtm->rtm_rmx = rt->rt_rmx;
451 rtm->rtm_addrs = rtinfo->rti_addrs;
457 struct rt_msghdr *bak_rtm;
458 struct rt_msghdr *new_rtm;
462 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt,
463 struct rt_addrinfo *rtinfo)
465 struct rt_msghdr *rtm = arg->new_rtm;
468 error = _fillrtmsg(&rtm, rt, rtinfo);
470 if (arg->new_rtm != rtm) {
472 * _fillrtmsg() just allocated a new rtm;
473 * if the previously allocated rtm is not
474 * the backing rtm, it should be freed.
476 if (arg->new_rtm != arg->bak_rtm)
477 kfree(arg->new_rtm, M_RTABLE);
484 static void route_output_add_callback(int, int, struct rt_addrinfo *,
485 struct rtentry *, void *);
486 static void route_output_delete_callback(int, int, struct rt_addrinfo *,
487 struct rtentry *, void *);
488 static int route_output_get_callback(int, struct rt_addrinfo *,
489 struct rtentry *, void *, int);
490 static int route_output_change_callback(int, struct rt_addrinfo *,
491 struct rtentry *, void *, int);
492 static int route_output_lock_callback(int, struct rt_addrinfo *,
493 struct rtentry *, void *, int);
497 route_output(struct mbuf *m, struct socket *so, ...)
500 struct rt_msghdr *rtm = NULL;
501 struct rawcb *rp = NULL;
502 struct pr_output_info *oi;
503 struct rt_addrinfo rtinfo;
511 oi = __va_arg(ap, struct pr_output_info *);
514 family = familyof(NULL);
516 #define gotoerr(e) { error = e; goto flush;}
519 (m->m_len < sizeof(long) &&
520 (m = m_pullup(m, sizeof(long))) == NULL))
522 len = m->m_pkthdr.len;
523 if (len < sizeof(struct rt_msghdr) ||
524 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
527 rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
531 m_copydata(m, 0, len, (caddr_t)rtm);
532 if (rtm->rtm_version != RTM_VERSION)
533 gotoerr(EPROTONOSUPPORT);
535 rtm->rtm_pid = oi->p_pid;
536 bzero(&rtinfo, sizeof(struct rt_addrinfo));
537 rtinfo.rti_addrs = rtm->rtm_addrs;
538 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0)
541 rtinfo.rti_flags = rtm->rtm_flags;
542 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
543 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
546 family = familyof(rtinfo.rti_dst);
548 if (rtinfo.rti_genmask != NULL) {
549 error = rtmask_add_global(rtinfo.rti_genmask);
555 * Verify that the caller has the appropriate privilege; RTM_GET
556 * is the only operation the non-superuser is allowed.
558 if (rtm->rtm_type != RTM_GET &&
559 priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0)
562 switch (rtm->rtm_type) {
564 if (rtinfo.rti_gateway == NULL) {
567 error = rtrequest1_global(RTM_ADD, &rtinfo,
568 route_output_add_callback, rtm);
573 * Backing rtm (bak_rtm) could _not_ be freed during
574 * rtrequest1_global or rtsearch_global, even if the
575 * callback reallocates the rtm due to its size changes,
576 * since rtinfo points to the backing rtm's memory area.
577 * After rtrequest1_global or rtsearch_global returns,
578 * it is safe to free the backing rtm, since rtinfo will
579 * not be used anymore.
581 * new_rtm will be used to save the new rtm allocated
582 * by rtrequest1_global or rtsearch_global.
586 error = rtrequest1_global(RTM_DELETE, &rtinfo,
587 route_output_delete_callback, &arg);
589 if (rtm != arg.bak_rtm)
590 kfree(arg.bak_rtm, M_RTABLE);
593 /* See the comment in RTM_DELETE */
596 error = rtsearch_global(RTM_GET, &rtinfo,
597 route_output_get_callback, &arg,
600 if (rtm != arg.bak_rtm)
601 kfree(arg.bak_rtm, M_RTABLE);
604 error = rtsearch_global(RTM_CHANGE, &rtinfo,
605 route_output_change_callback, rtm,
609 error = rtsearch_global(RTM_LOCK, &rtinfo,
610 route_output_lock_callback, rtm,
620 rtm->rtm_errno = error;
622 rtm->rtm_flags |= RTF_DONE;
626 * Check to see if we don't want our own messages.
628 if (!(so->so_options & SO_USELOOPBACK)) {
629 if (route_cb.any_count <= 1) {
631 kfree(rtm, M_RTABLE);
635 /* There is another listener, so construct message */
639 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
640 if (m->m_pkthdr.len < rtm->rtm_msglen) {
643 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
644 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
645 kfree(rtm, M_RTABLE);
648 rts_input_skip(m, family, rp);
653 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
654 struct rtentry *rt, void *arg)
656 struct rt_msghdr *rtm = arg;
658 if (error == 0 && rt != NULL) {
659 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
661 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
662 rt->rt_rmx.rmx_locks |=
663 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
664 if (rtinfo->rti_genmask != NULL) {
665 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
666 if (rt->rt_genmask == NULL) {
668 * This should not happen, since we
669 * have already installed genmask
670 * on each CPU before we reach here.
672 panic("genmask is gone!?");
675 rt->rt_genmask = NULL;
677 rtm->rtm_index = rt->rt_ifp->if_index;
682 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
683 struct rtentry *rt, void *arg)
685 if (error == 0 && rt) {
687 if (fillrtmsg(arg, rt, rtinfo) != 0) {
689 /* XXX no way to return the error */
693 if (rt && rt->rt_refcnt == 0) {
700 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo,
701 struct rtentry *rt, void *arg, int found_cnt)
703 int error, found = 0;
705 if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0)
708 error = fillrtmsg(arg, rt, rtinfo);
709 if (!error && found) {
710 /* Got the exact match, we could return now! */
717 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo,
718 struct rtentry *rt, void *arg, int found_cnt)
720 struct rt_msghdr *rtm = arg;
725 * new gateway could require new ifaddr, ifp;
726 * flags may also be different; ifp may be specified
727 * by ll sockaddr when protocol address is ambiguous
729 if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
730 rtinfo->rti_ifpaddr != NULL ||
731 (rtinfo->rti_ifaaddr != NULL &&
732 !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) {
733 error = rt_getifa(rtinfo);
737 if (rtinfo->rti_gateway != NULL) {
739 * We only need to generate rtmsg upon the
740 * first route to be changed.
742 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway,
743 found_cnt == 1 ? RTL_REPORTMSG : RTL_DONTREPORT);
747 if ((ifa = rtinfo->rti_ifa) != NULL) {
748 struct ifaddr *oifa = rt->rt_ifa;
751 if (oifa && oifa->ifa_rtrequest)
752 oifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
756 rt->rt_ifp = rtinfo->rti_ifp;
759 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
760 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
761 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, rtinfo);
762 if (rtinfo->rti_genmask != NULL) {
763 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
764 if (rt->rt_genmask == NULL) {
766 * This should not happen, since we
767 * have already installed genmask
768 * on each CPU before we reach here.
770 panic("genmask is gone!?\n");
773 rtm->rtm_index = rt->rt_ifp->if_index;
779 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo,
780 struct rtentry *rt, void *arg,
781 int found_cnt __unused)
783 struct rt_msghdr *rtm = arg;
785 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
786 rt->rt_rmx.rmx_locks |=
787 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
792 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
794 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
795 setmetric(RTV_RPIPE, rmx_recvpipe);
796 setmetric(RTV_SPIPE, rmx_sendpipe);
797 setmetric(RTV_SSTHRESH, rmx_ssthresh);
798 setmetric(RTV_RTT, rmx_rtt);
799 setmetric(RTV_RTTVAR, rmx_rttvar);
800 setmetric(RTV_HOPCOUNT, rmx_hopcount);
801 setmetric(RTV_MTU, rmx_mtu);
802 setmetric(RTV_EXPIRE, rmx_expire);
807 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
810 * Extract the addresses of the passed sockaddrs.
811 * Do a little sanity checking so as to avoid bad memory references.
812 * This data is derived straight from userland.
815 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
820 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
821 if ((rtinfo->rti_addrs & (1 << i)) == 0)
823 sa = (struct sockaddr *)cp;
827 if ((cp + sa->sa_len) > cplim) {
832 * There are no more... Quit now.
833 * If there are more bits, they are in error.
834 * I've seen this. route(1) can evidently generate these.
835 * This causes kernel to core dump.
836 * For compatibility, if we see this, point to a safe address.
838 if (sa->sa_len == 0) {
839 static struct sockaddr sa_zero = {
840 sizeof sa_zero, AF_INET,
843 rtinfo->rti_info[i] = &sa_zero;
844 kprintf("rtsock: received more addr bits than sockaddrs.\n");
845 return (0); /* should be EINVAL but for compat */
848 /* Accept the sockaddr. */
849 rtinfo->rti_info[i] = sa;
850 cp += ROUNDUP(sa->sa_len);
856 rt_msghdrsize(int type)
861 return sizeof(struct ifa_msghdr);
864 return sizeof(struct ifma_msghdr);
866 return sizeof(struct if_msghdr);
869 return sizeof(struct if_announcemsghdr);
871 return sizeof(struct rt_msghdr);
876 rt_msgsize(int type, struct rt_addrinfo *rtinfo)
880 len = rt_msghdrsize(type);
881 for (i = 0; i < RTAX_MAX; i++) {
882 if (rtinfo->rti_info[i] != NULL)
883 len += ROUNDUP(rtinfo->rti_info[i]->sa_len);
890 * Build a routing message in a buffer.
891 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
892 * to the end of the buffer after the message header.
894 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
895 * This side-effect can be avoided if we reorder the addrs bitmask field in all
896 * the route messages to line up so we can set it here instead of back in the
900 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
902 struct rt_msghdr *rtm;
906 rtm = (struct rt_msghdr *) buf;
907 rtm->rtm_version = RTM_VERSION;
908 rtm->rtm_type = type;
909 rtm->rtm_msglen = msglen;
911 cp = (char *)buf + rt_msghdrsize(type);
912 rtinfo->rti_addrs = 0;
913 for (i = 0; i < RTAX_MAX; i++) {
916 if ((sa = rtinfo->rti_info[i]) == NULL)
918 rtinfo->rti_addrs |= (1 << i);
919 dlen = ROUNDUP(sa->sa_len);
926 * Build a routing message in a mbuf chain.
927 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
928 * to the end of the mbuf after the message header.
930 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
931 * This side-effect can be avoided if we reorder the addrs bitmask field in all
932 * the route messages to line up so we can set it here instead of back in the
936 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
939 struct rt_msghdr *rtm;
943 hlen = rt_msghdrsize(type);
944 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
946 m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
950 m->m_pkthdr.len = m->m_len = hlen;
951 m->m_pkthdr.rcvif = NULL;
952 rtinfo->rti_addrs = 0;
954 for (i = 0; i < RTAX_MAX; i++) {
958 if ((sa = rtinfo->rti_info[i]) == NULL)
960 rtinfo->rti_addrs |= (1 << i);
961 dlen = ROUNDUP(sa->sa_len);
962 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
965 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
969 rtm = mtod(m, struct rt_msghdr *);
971 rtm->rtm_msglen = len;
972 rtm->rtm_version = RTM_VERSION;
973 rtm->rtm_type = type;
978 * This routine is called to generate a message from the routing
979 * socket indicating that a redirect has occurred, a routing lookup
980 * has failed, or that a protocol has detected timeouts to a particular
984 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
986 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
987 struct rt_msghdr *rtm;
990 if (route_cb.any_count == 0)
992 m = rt_msg_mbuf(type, rtinfo);
995 rtm = mtod(m, struct rt_msghdr *);
996 rtm->rtm_flags = RTF_DONE | flags;
997 rtm->rtm_errno = error;
998 rtm->rtm_addrs = rtinfo->rti_addrs;
999 rts_input(m, familyof(dst));
1003 rt_dstmsg(int type, struct sockaddr *dst, int error)
1005 struct rt_msghdr *rtm;
1006 struct rt_addrinfo addrs;
1009 if (route_cb.any_count == 0)
1011 bzero(&addrs, sizeof(struct rt_addrinfo));
1012 addrs.rti_info[RTAX_DST] = dst;
1013 m = rt_msg_mbuf(type, &addrs);
1016 rtm = mtod(m, struct rt_msghdr *);
1017 rtm->rtm_flags = RTF_DONE;
1018 rtm->rtm_errno = error;
1019 rtm->rtm_addrs = addrs.rti_addrs;
1020 rts_input(m, familyof(dst));
1024 * This routine is called to generate a message from the routing
1025 * socket indicating that the status of a network interface has changed.
1028 rt_ifmsg(struct ifnet *ifp)
1030 struct if_msghdr *ifm;
1032 struct rt_addrinfo rtinfo;
1034 if (route_cb.any_count == 0)
1036 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1037 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
1040 ifm = mtod(m, struct if_msghdr *);
1041 ifm->ifm_index = ifp->if_index;
1042 ifm->ifm_flags = ifp->if_flags;
1043 ifm->ifm_data = ifp->if_data;
1049 rt_ifamsg(int cmd, struct ifaddr *ifa)
1051 struct ifa_msghdr *ifam;
1052 struct rt_addrinfo rtinfo;
1054 struct ifnet *ifp = ifa->ifa_ifp;
1056 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1057 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1058 rtinfo.rti_ifpaddr =
1059 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1060 rtinfo.rti_netmask = ifa->ifa_netmask;
1061 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1063 m = rt_msg_mbuf(cmd, &rtinfo);
1067 ifam = mtod(m, struct ifa_msghdr *);
1068 ifam->ifam_index = ifp->if_index;
1069 ifam->ifam_metric = ifa->ifa_metric;
1070 ifam->ifam_flags = ifa->ifa_flags;
1071 ifam->ifam_addrs = rtinfo.rti_addrs;
1073 rts_input(m, familyof(ifa->ifa_addr));
1077 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
1079 struct rt_msghdr *rtm;
1080 struct rt_addrinfo rtinfo;
1082 struct sockaddr *dst;
1087 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1088 rtinfo.rti_dst = dst = rt_key(rt);
1089 rtinfo.rti_gateway = rt->rt_gateway;
1090 rtinfo.rti_netmask = rt_mask(rt);
1092 rtinfo.rti_ifpaddr =
1093 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1095 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1097 m = rt_msg_mbuf(cmd, &rtinfo);
1101 rtm = mtod(m, struct rt_msghdr *);
1103 rtm->rtm_index = ifp->if_index;
1104 rtm->rtm_flags |= rt->rt_flags;
1105 rtm->rtm_errno = error;
1106 rtm->rtm_addrs = rtinfo.rti_addrs;
1108 rts_input(m, familyof(dst));
1112 * This is called to generate messages from the routing socket
1113 * indicating a network interface has had addresses associated with it.
1114 * if we ever reverse the logic and replace messages TO the routing
1115 * socket indicate a request to configure interfaces, then it will
1116 * be unnecessary as the routing socket will automatically generate
1120 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1124 * notify the SCTP stack
1125 * this will only get called when an address is added/deleted
1126 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1129 sctp_add_ip_address(ifa);
1130 else if (cmd == RTM_DELETE)
1131 sctp_delete_ip_address(ifa);
1134 if (route_cb.any_count == 0)
1137 if (cmd == RTM_ADD) {
1138 rt_ifamsg(RTM_NEWADDR, ifa);
1139 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
1141 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
1142 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
1143 rt_ifamsg(RTM_DELADDR, ifa);
1148 * This is the analogue to the rt_newaddrmsg which performs the same
1149 * function but for multicast group memberhips. This is easier since
1150 * there is no route state to worry about.
1153 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1155 struct rt_addrinfo rtinfo;
1156 struct mbuf *m = NULL;
1157 struct ifnet *ifp = ifma->ifma_ifp;
1158 struct ifma_msghdr *ifmam;
1160 if (route_cb.any_count == 0)
1163 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1164 rtinfo.rti_ifaaddr = ifma->ifma_addr;
1165 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1166 rtinfo.rti_ifpaddr =
1167 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1170 * If a link-layer address is present, present it as a ``gateway''
1171 * (similarly to how ARP entries, e.g., are presented).
1173 rtinfo.rti_gateway = ifma->ifma_lladdr;
1175 m = rt_msg_mbuf(cmd, &rtinfo);
1179 ifmam = mtod(m, struct ifma_msghdr *);
1180 ifmam->ifmam_index = ifp->if_index;
1181 ifmam->ifmam_addrs = rtinfo.rti_addrs;
1183 rts_input(m, familyof(ifma->ifma_addr));
1186 static struct mbuf *
1187 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1188 struct rt_addrinfo *info)
1190 struct if_announcemsghdr *ifan;
1193 if (route_cb.any_count == 0)
1196 bzero(info, sizeof(*info));
1197 m = rt_msg_mbuf(type, info);
1201 ifan = mtod(m, struct if_announcemsghdr *);
1202 ifan->ifan_index = ifp->if_index;
1203 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1204 ifan->ifan_what = what;
1209 * This is called to generate routing socket messages indicating
1210 * IEEE80211 wireless events.
1211 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1214 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1216 struct rt_addrinfo info;
1219 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1224 * Append the ieee80211 data. Try to stick it in the
1225 * mbuf containing the ifannounce msg; otherwise allocate
1226 * a new mbuf and append.
1228 * NB: we assume m is a single mbuf.
1230 if (data_len > M_TRAILINGSPACE(m)) {
1231 struct mbuf *n = m_get(MB_DONTWAIT, MT_DATA);
1236 bcopy(data, mtod(n, void *), data_len);
1237 n->m_len = data_len;
1239 } else if (data_len > 0) {
1240 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1241 m->m_len += data_len;
1244 if (m->m_flags & M_PKTHDR)
1245 m->m_pkthdr.len += data_len;
1246 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1251 * This is called to generate routing socket messages indicating
1252 * network interface arrival and departure.
1255 rt_ifannouncemsg(struct ifnet *ifp, int what)
1257 struct rt_addrinfo addrinfo;
1260 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo);
1266 resizewalkarg(struct walkarg *w, int len)
1270 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1273 if (w->w_tmem != NULL)
1274 kfree(w->w_tmem, M_RTABLE);
1276 w->w_tmemsize = len;
1281 * This is used in dumping the kernel table via sysctl().
1284 sysctl_dumpentry(struct radix_node *rn, void *vw)
1286 struct walkarg *w = vw;
1287 struct rtentry *rt = (struct rtentry *)rn;
1288 struct rt_addrinfo rtinfo;
1291 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1294 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1295 rtinfo.rti_dst = rt_key(rt);
1296 rtinfo.rti_gateway = rt->rt_gateway;
1297 rtinfo.rti_netmask = rt_mask(rt);
1298 rtinfo.rti_genmask = rt->rt_genmask;
1299 if (rt->rt_ifp != NULL) {
1300 rtinfo.rti_ifpaddr =
1301 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1302 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1303 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1304 rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1306 msglen = rt_msgsize(RTM_GET, &rtinfo);
1307 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1309 rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen);
1310 if (w->w_req != NULL) {
1311 struct rt_msghdr *rtm = w->w_tmem;
1313 rtm->rtm_flags = rt->rt_flags;
1314 rtm->rtm_use = rt->rt_use;
1315 rtm->rtm_rmx = rt->rt_rmx;
1316 rtm->rtm_index = rt->rt_ifp->if_index;
1317 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1318 rtm->rtm_addrs = rtinfo.rti_addrs;
1319 error = SYSCTL_OUT(w->w_req, rtm, msglen);
1326 sysctl_iflist(int af, struct walkarg *w)
1329 struct rt_addrinfo rtinfo;
1332 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1333 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1334 struct ifaddr_container *ifac;
1337 if (w->w_arg && w->w_arg != ifp->if_index)
1339 ifac = TAILQ_FIRST(&ifp->if_addrheads[mycpuid]);
1341 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1342 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1343 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1345 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1346 rtinfo.rti_ifpaddr = NULL;
1347 if (w->w_req != NULL && w->w_tmem != NULL) {
1348 struct if_msghdr *ifm = w->w_tmem;
1350 ifm->ifm_index = ifp->if_index;
1351 ifm->ifm_flags = ifp->if_flags;
1352 ifm->ifm_data = ifp->if_data;
1353 ifm->ifm_addrs = rtinfo.rti_addrs;
1354 error = SYSCTL_OUT(w->w_req, ifm, msglen);
1358 while ((ifac = TAILQ_NEXT(ifac, ifa_link)) != NULL) {
1361 if (af && af != ifa->ifa_addr->sa_family)
1363 if (curproc->p_ucred->cr_prison &&
1364 prison_if(curproc->p_ucred, ifa->ifa_addr))
1366 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1367 rtinfo.rti_netmask = ifa->ifa_netmask;
1368 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1369 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1370 if (w->w_tmemsize < msglen &&
1371 resizewalkarg(w, msglen) != 0)
1373 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1374 if (w->w_req != NULL) {
1375 struct ifa_msghdr *ifam = w->w_tmem;
1377 ifam->ifam_index = ifa->ifa_ifp->if_index;
1378 ifam->ifam_flags = ifa->ifa_flags;
1379 ifam->ifam_metric = ifa->ifa_metric;
1380 ifam->ifam_addrs = rtinfo.rti_addrs;
1381 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1386 rtinfo.rti_netmask = NULL;
1387 rtinfo.rti_ifaaddr = NULL;
1388 rtinfo.rti_bcastaddr = NULL;
1394 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1396 int *name = (int *)arg1;
1397 u_int namelen = arg2;
1398 struct radix_node_head *rnh;
1399 int i, error = EINVAL;
1408 if (namelen != 3 && namelen != 4)
1411 bzero(&w, sizeof w);
1417 * Optional third argument specifies cpu, used primarily for
1418 * debugging the route table.
1421 if (name[3] < 0 || name[3] >= ncpus)
1424 lwkt_migratecpu(name[3]);
1432 for (i = 1; i <= AF_MAX; i++)
1433 if ((rnh = rt_tables[mycpuid][i]) &&
1434 (af == 0 || af == i) &&
1435 (error = rnh->rnh_walktree(rnh,
1436 sysctl_dumpentry, &w)))
1441 error = sysctl_iflist(af, &w);
1444 if (w.w_tmem != NULL)
1445 kfree(w.w_tmem, M_RTABLE);
1447 lwkt_migratecpu(origcpu);
1451 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1454 * Definitions of protocols supported in the ROUTE domain.
1457 static struct domain routedomain; /* or at least forward */
1459 static struct protosw routesw[] = {
1460 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1461 0, route_output, raw_ctlinput, 0,
1462 cpu0_soport, cpu0_ctlport,
1468 static struct domain routedomain = {
1469 PF_ROUTE, "route", NULL, NULL, NULL,
1470 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])],