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) 2004, 2005 Jeffrey M. Hsu. All rights reserved.
36 * License terms: all terms for the DragonFly license above plus the following:
38 * 4. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
41 * This product includes software developed by Jeffrey M. Hsu
42 * for the DragonFly Project.
44 * This requirement may be waived with permission from Jeffrey Hsu.
45 * Permission will be granted to any DragonFly user for free.
46 * This requirement will sunset and may be removed on Jan 31, 2006,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1988, 1991, 1993
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
84 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
85 * $DragonFly: src/sys/net/rtsock.c,v 1.30 2006/01/31 19:05:35 dillon Exp $
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/kernel.h>
93 #include <sys/sysctl.h>
95 #include <sys/malloc.h>
97 #include <sys/protosw.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/domain.h>
101 #include <sys/thread2.h>
104 #include <net/route.h>
105 #include <net/raw_cb.h>
108 extern void sctp_add_ip_address(struct ifaddr *ifa);
109 extern void sctp_delete_ip_address(struct ifaddr *ifa);
112 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
114 static struct route_cb {
122 static const struct sockaddr route_src = { 2, PF_ROUTE, };
128 struct sysctl_req *w_req;
132 rt_msg_mbuf (int, struct rt_addrinfo *);
133 static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
134 static int rt_msgsize (int type, struct rt_addrinfo *rtinfo);
135 static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
136 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
137 static int sysctl_iflist (int af, struct walkarg *w);
138 static int route_output(struct mbuf *, struct socket *, ...);
139 static void rt_setmetrics (u_long, struct rt_metrics *,
140 struct rt_metrics *);
143 * It really doesn't make any sense at all for this code to share much
144 * with raw_usrreq.c, since its functionality is so restricted. XXX
147 rts_abort(struct socket *so)
152 error = raw_usrreqs.pru_abort(so);
157 /* pru_accept is EOPNOTSUPP */
160 rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
165 if (sotorawcb(so) != NULL)
166 return EISCONN; /* XXX panic? */
168 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
173 * The critical section is necessary to block protocols from sending
174 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
175 * this PCB is extant but incompletely initialized.
176 * Probably we should try to do more of this work beforehand and
177 * eliminate the critical section.
181 error = raw_attach(so, proto, ai->sb_rlimit);
188 switch(rp->rcb_proto.sp_protocol) {
193 route_cb.ip6_count++;
196 route_cb.ipx_count++;
202 rp->rcb_faddr = &route_src;
203 route_cb.any_count++;
205 so->so_options |= SO_USELOOPBACK;
211 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
216 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
222 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
227 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
232 /* pru_connect2 is EOPNOTSUPP */
233 /* pru_control is EOPNOTSUPP */
236 rts_detach(struct socket *so)
238 struct rawcb *rp = sotorawcb(so);
243 switch(rp->rcb_proto.sp_protocol) {
248 route_cb.ip6_count--;
251 route_cb.ipx_count--;
257 route_cb.any_count--;
259 error = raw_usrreqs.pru_detach(so);
265 rts_disconnect(struct socket *so)
270 error = raw_usrreqs.pru_disconnect(so);
275 /* pru_listen is EOPNOTSUPP */
278 rts_peeraddr(struct socket *so, struct sockaddr **nam)
283 error = raw_usrreqs.pru_peeraddr(so, nam);
288 /* pru_rcvd is EOPNOTSUPP */
289 /* pru_rcvoob is EOPNOTSUPP */
292 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
293 struct mbuf *control, struct thread *td)
298 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
303 /* pru_sense is null */
306 rts_shutdown(struct socket *so)
311 error = raw_usrreqs.pru_shutdown(so);
317 rts_sockaddr(struct socket *so, struct sockaddr **nam)
322 error = raw_usrreqs.pru_sockaddr(so, nam);
327 static struct pr_usrreqs route_usrreqs = {
328 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
329 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
330 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
331 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
332 sosend, soreceive, sopoll
335 static __inline sa_family_t
336 familyof(struct sockaddr *sa)
338 return (sa != NULL ? sa->sa_family : 0);
342 rts_input(struct mbuf *m, sa_family_t family)
344 static const struct sockaddr route_dst = { 2, PF_ROUTE, };
345 struct sockproto route_proto = { PF_ROUTE, family };
347 raw_input(m, &route_proto, &route_src, &route_dst);
351 reallocbuf(void *ptr, size_t len, size_t olen)
355 newptr = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
358 bcopy(ptr, newptr, olen);
364 * Internal helper routine for route_output().
367 fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
368 struct rt_addrinfo *rtinfo)
371 struct rt_msghdr *rtm = *prtm;
373 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
374 rtinfo->rti_dst = rt_key(rt);
375 rtinfo->rti_gateway = rt->rt_gateway;
376 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */
377 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */
378 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
379 if (rt->rt_ifp != NULL) {
380 rtinfo->rti_ifpaddr =
381 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
382 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
383 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
384 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
385 rtm->rtm_index = rt->rt_ifp->if_index;
387 rtinfo->rti_ifpaddr = NULL;
388 rtinfo->rti_ifaaddr = NULL;
392 msglen = rt_msgsize(rtm->rtm_type, rtinfo);
393 if (rtm->rtm_msglen < msglen) {
394 rtm = reallocbuf(rtm, msglen, rtm->rtm_msglen);
399 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
401 rtm->rtm_flags = rt->rt_flags;
402 rtm->rtm_rmx = rt->rt_rmx;
403 rtm->rtm_addrs = rtinfo->rti_addrs;
408 static void route_output_add_callback(int, int, struct rt_addrinfo *,
409 struct rtentry *, void *);
410 static void route_output_delete_callback(int, int, struct rt_addrinfo *,
411 struct rtentry *, void *);
412 static void route_output_change_callback(int, int, struct rt_addrinfo *,
413 struct rtentry *, void *);
414 static void route_output_lock_callback(int, int, struct rt_addrinfo *,
415 struct rtentry *, void *);
419 route_output(struct mbuf *m, struct socket *so, ...)
421 struct rt_msghdr *rtm = NULL;
423 struct radix_node_head *rnh;
424 struct rawcb *rp = NULL;
425 struct pr_output_info *oi;
426 struct rt_addrinfo rtinfo;
431 oi = __va_arg(ap, struct pr_output_info *);
434 #define gotoerr(e) { error = e; goto flush;}
437 (m->m_len < sizeof(long) &&
438 (m = m_pullup(m, sizeof(long))) == NULL))
440 if (!(m->m_flags & M_PKTHDR))
441 panic("route_output");
442 len = m->m_pkthdr.len;
443 if (len < sizeof(struct rt_msghdr) ||
444 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
445 rtinfo.rti_dst = NULL;
448 rtm = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
450 rtinfo.rti_dst = NULL;
453 m_copydata(m, 0, len, (caddr_t)rtm);
454 if (rtm->rtm_version != RTM_VERSION) {
455 rtinfo.rti_dst = NULL;
456 gotoerr(EPROTONOSUPPORT);
458 rtm->rtm_pid = oi->p_pid;
459 bzero(&rtinfo, sizeof(struct rt_addrinfo));
460 rtinfo.rti_addrs = rtm->rtm_addrs;
461 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0) {
462 rtinfo.rti_dst = NULL;
465 rtinfo.rti_flags = rtm->rtm_flags;
466 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
467 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
470 if (rtinfo.rti_genmask != NULL) {
471 struct radix_node *n;
473 #define clen(s) (*(u_char *)(s))
474 n = rn_addmask((char *)rtinfo.rti_genmask, TRUE, 1);
476 rtinfo.rti_genmask->sa_len >= clen(n->rn_key) &&
477 bcmp((char *)rtinfo.rti_genmask + 1,
478 (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0)
479 rtinfo.rti_genmask = (struct sockaddr *)n->rn_key;
485 * Verify that the caller has the appropriate privilege; RTM_GET
486 * is the only operation the non-superuser is allowed.
488 if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
491 switch (rtm->rtm_type) {
493 if (rtinfo.rti_gateway == NULL) {
496 error = rtrequest1_global(RTM_ADD, &rtinfo,
497 route_output_add_callback, rtm);
502 * note: &rtm passed as argument so 'rtm' can be replaced.
504 error = rtrequest1_global(RTM_DELETE, &rtinfo,
505 route_output_delete_callback, &rtm);
508 rnh = rt_tables[mycpuid][rtinfo.rti_dst->sa_family];
510 error = EAFNOSUPPORT;
513 rt = (struct rtentry *)
514 rnh->rnh_lookup((char *)rtinfo.rti_dst,
515 (char *)rtinfo.rti_netmask, rnh);
521 if (fillrtmsg(&rtm, rt, &rtinfo) != 0)
526 error = rtrequest1_global(RTM_GET, &rtinfo,
527 route_output_change_callback, rtm);
530 error = rtrequest1_global(RTM_GET, &rtinfo,
531 route_output_lock_callback, rtm);
541 rtm->rtm_errno = error;
543 rtm->rtm_flags |= RTF_DONE;
547 * Check to see if we don't want our own messages.
549 if (!(so->so_options & SO_USELOOPBACK)) {
550 if (route_cb.any_count <= 1) {
556 /* There is another listener, so construct message */
560 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
561 if (m->m_pkthdr.len < rtm->rtm_msglen) {
564 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
565 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
569 rp->rcb_proto.sp_family = 0; /* Avoid us */
571 rts_input(m, familyof(rtinfo.rti_dst));
573 rp->rcb_proto.sp_family = PF_ROUTE;
578 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
579 struct rtentry *rt, void *arg)
581 struct rt_msghdr *rtm = arg;
583 if (error == 0 && rt != NULL) {
584 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
586 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
587 rt->rt_rmx.rmx_locks |=
588 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
589 rt->rt_genmask = rtinfo->rti_genmask;
594 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
595 struct rtentry *rt, void *arg)
597 struct rt_msghdr **rtm = arg;
599 if (error == 0 && rt) {
601 if (fillrtmsg(rtm, rt, rtinfo) != 0) {
603 /* XXX no way to return the error */
610 route_output_change_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
611 struct rtentry *rt, void *arg)
613 struct rt_msghdr *rtm = arg;
620 * new gateway could require new ifaddr, ifp;
621 * flags may also be different; ifp may be specified
622 * by ll sockaddr when protocol address is ambiguous
624 if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
625 rtinfo->rti_ifpaddr != NULL || (rtinfo->rti_ifaaddr != NULL &&
626 sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))
628 error = rt_getifa(rtinfo);
632 if (rtinfo->rti_gateway != NULL) {
633 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway);
637 if ((ifa = rtinfo->rti_ifa) != NULL) {
638 struct ifaddr *oifa = rt->rt_ifa;
641 if (oifa && oifa->ifa_rtrequest)
642 oifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
646 rt->rt_ifp = rtinfo->rti_ifp;
649 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
650 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
651 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, rtinfo);
652 if (rtinfo->rti_genmask != NULL)
653 rt->rt_genmask = rtinfo->rti_genmask;
655 /* XXX no way to return error */
660 route_output_lock_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
661 struct rtentry *rt, void *arg)
663 struct rt_msghdr *rtm = arg;
665 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
666 rt->rt_rmx.rmx_locks |=
667 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
671 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
673 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
674 setmetric(RTV_RPIPE, rmx_recvpipe);
675 setmetric(RTV_SPIPE, rmx_sendpipe);
676 setmetric(RTV_SSTHRESH, rmx_ssthresh);
677 setmetric(RTV_RTT, rmx_rtt);
678 setmetric(RTV_RTTVAR, rmx_rttvar);
679 setmetric(RTV_HOPCOUNT, rmx_hopcount);
680 setmetric(RTV_MTU, rmx_mtu);
681 setmetric(RTV_EXPIRE, rmx_expire);
686 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
689 * Extract the addresses of the passed sockaddrs.
690 * Do a little sanity checking so as to avoid bad memory references.
691 * This data is derived straight from userland.
694 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
699 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
700 if ((rtinfo->rti_addrs & (1 << i)) == 0)
702 sa = (struct sockaddr *)cp;
706 if ((cp + sa->sa_len) > cplim) {
711 * There are no more... Quit now.
712 * If there are more bits, they are in error.
713 * I've seen this. route(1) can evidently generate these.
714 * This causes kernel to core dump.
715 * For compatibility, if we see this, point to a safe address.
717 if (sa->sa_len == 0) {
718 static struct sockaddr sa_zero = {
719 sizeof sa_zero, AF_INET,
722 rtinfo->rti_info[i] = &sa_zero;
723 return (0); /* should be EINVAL but for compat */
726 /* Accept the sockaddr. */
727 rtinfo->rti_info[i] = sa;
728 cp += ROUNDUP(sa->sa_len);
734 rt_msghdrsize(int type)
739 return sizeof(struct ifa_msghdr);
742 return sizeof(struct ifma_msghdr);
744 return sizeof(struct if_msghdr);
746 return sizeof(struct if_announcemsghdr);
748 return sizeof(struct rt_msghdr);
753 rt_msgsize(int type, struct rt_addrinfo *rtinfo)
757 len = rt_msghdrsize(type);
758 for (i = 0; i < RTAX_MAX; i++) {
759 if (rtinfo->rti_info[i] != NULL)
760 len += ROUNDUP(rtinfo->rti_info[i]->sa_len);
767 * Build a routing message in a buffer.
768 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
769 * to the end of the buffer after the message header.
771 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
772 * This side-effect can be avoided if we reorder the addrs bitmask field in all
773 * the route messages to line up so we can set it here instead of back in the
777 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
779 struct rt_msghdr *rtm;
783 rtm = (struct rt_msghdr *) buf;
784 rtm->rtm_version = RTM_VERSION;
785 rtm->rtm_type = type;
786 rtm->rtm_msglen = msglen;
788 cp = (char *)buf + rt_msghdrsize(type);
789 rtinfo->rti_addrs = 0;
790 for (i = 0; i < RTAX_MAX; i++) {
793 if ((sa = rtinfo->rti_info[i]) == NULL)
795 rtinfo->rti_addrs |= (1 << i);
796 dlen = ROUNDUP(sa->sa_len);
803 * Build a routing message in a mbuf chain.
804 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
805 * to the end of the mbuf after the message header.
807 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
808 * This side-effect can be avoided if we reorder the addrs bitmask field in all
809 * the route messages to line up so we can set it here instead of back in the
813 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
816 struct rt_msghdr *rtm;
820 hlen = rt_msghdrsize(type);
821 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
823 m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
826 m->m_pkthdr.len = m->m_len = hlen;
827 m->m_pkthdr.rcvif = NULL;
828 rtinfo->rti_addrs = 0;
830 for (i = 0; i < RTAX_MAX; i++) {
834 if ((sa = rtinfo->rti_info[i]) == NULL)
836 rtinfo->rti_addrs |= (1 << i);
837 dlen = ROUNDUP(sa->sa_len);
838 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
841 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
845 rtm = mtod(m, struct rt_msghdr *);
847 rtm->rtm_msglen = len;
848 rtm->rtm_version = RTM_VERSION;
849 rtm->rtm_type = type;
854 * This routine is called to generate a message from the routing
855 * socket indicating that a redirect has occurred, a routing lookup
856 * has failed, or that a protocol has detected timeouts to a particular
860 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
862 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
863 struct rt_msghdr *rtm;
866 if (route_cb.any_count == 0)
868 m = rt_msg_mbuf(type, rtinfo);
871 rtm = mtod(m, struct rt_msghdr *);
872 rtm->rtm_flags = RTF_DONE | flags;
873 rtm->rtm_errno = error;
874 rtm->rtm_addrs = rtinfo->rti_addrs;
875 rts_input(m, familyof(dst));
879 rt_dstmsg(int type, struct sockaddr *dst, int error)
881 struct rt_msghdr *rtm;
882 struct rt_addrinfo addrs;
885 if (route_cb.any_count == 0)
887 bzero(&addrs, sizeof(struct rt_addrinfo));
888 addrs.rti_info[RTAX_DST] = dst;
889 m = rt_msg_mbuf(type, &addrs);
892 rtm = mtod(m, struct rt_msghdr *);
893 rtm->rtm_flags = RTF_DONE;
894 rtm->rtm_errno = error;
895 rtm->rtm_addrs = addrs.rti_addrs;
896 rts_input(m, familyof(dst));
900 * This routine is called to generate a message from the routing
901 * socket indicating that the status of a network interface has changed.
904 rt_ifmsg(struct ifnet *ifp)
906 struct if_msghdr *ifm;
908 struct rt_addrinfo rtinfo;
910 if (route_cb.any_count == 0)
912 bzero(&rtinfo, sizeof(struct rt_addrinfo));
913 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
916 ifm = mtod(m, struct if_msghdr *);
917 ifm->ifm_index = ifp->if_index;
918 ifm->ifm_flags = ifp->if_flags;
919 ifm->ifm_data = ifp->if_data;
925 rt_ifamsg(int cmd, struct ifaddr *ifa)
927 struct ifa_msghdr *ifam;
928 struct rt_addrinfo rtinfo;
930 struct ifnet *ifp = ifa->ifa_ifp;
932 bzero(&rtinfo, sizeof(struct rt_addrinfo));
933 rtinfo.rti_ifaaddr = ifa->ifa_addr;
934 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
935 rtinfo.rti_netmask = ifa->ifa_netmask;
936 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
938 m = rt_msg_mbuf(cmd, &rtinfo);
942 ifam = mtod(m, struct ifa_msghdr *);
943 ifam->ifam_index = ifp->if_index;
944 ifam->ifam_metric = ifa->ifa_metric;
945 ifam->ifam_flags = ifa->ifa_flags;
946 ifam->ifam_addrs = rtinfo.rti_addrs;
948 rts_input(m, familyof(ifa->ifa_addr));
952 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
954 struct rt_msghdr *rtm;
955 struct rt_addrinfo rtinfo;
957 struct sockaddr *dst;
962 bzero(&rtinfo, sizeof(struct rt_addrinfo));
963 rtinfo.rti_dst = dst = rt_key(rt);
964 rtinfo.rti_gateway = rt->rt_gateway;
965 rtinfo.rti_netmask = rt_mask(rt);
967 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
968 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
970 m = rt_msg_mbuf(cmd, &rtinfo);
974 rtm = mtod(m, struct rt_msghdr *);
976 rtm->rtm_index = ifp->if_index;
977 rtm->rtm_flags |= rt->rt_flags;
978 rtm->rtm_errno = error;
979 rtm->rtm_addrs = rtinfo.rti_addrs;
981 rts_input(m, familyof(dst));
985 * This is called to generate messages from the routing socket
986 * indicating a network interface has had addresses associated with it.
987 * if we ever reverse the logic and replace messages TO the routing
988 * socket indicate a request to configure interfaces, then it will
989 * be unnecessary as the routing socket will automatically generate
993 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
997 * notify the SCTP stack
998 * this will only get called when an address is added/deleted
999 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1002 sctp_add_ip_address(ifa);
1003 else if (cmd == RTM_DELETE)
1004 sctp_delete_ip_address(ifa);
1007 if (route_cb.any_count == 0)
1010 if (cmd == RTM_ADD) {
1011 rt_ifamsg(RTM_NEWADDR, ifa);
1012 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
1014 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
1015 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
1016 rt_ifamsg(RTM_DELADDR, ifa);
1021 * This is the analogue to the rt_newaddrmsg which performs the same
1022 * function but for multicast group memberhips. This is easier since
1023 * there is no route state to worry about.
1026 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1028 struct rt_addrinfo rtinfo;
1029 struct mbuf *m = NULL;
1030 struct ifnet *ifp = ifma->ifma_ifp;
1031 struct ifma_msghdr *ifmam;
1033 if (route_cb.any_count == 0)
1036 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1037 rtinfo.rti_ifaaddr = ifma->ifma_addr;
1038 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrhead))
1039 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
1041 * If a link-layer address is present, present it as a ``gateway''
1042 * (similarly to how ARP entries, e.g., are presented).
1044 rtinfo.rti_gateway = ifma->ifma_lladdr;
1046 m = rt_msg_mbuf(cmd, &rtinfo);
1050 ifmam = mtod(m, struct ifma_msghdr *);
1051 ifmam->ifmam_index = ifp->if_index;
1052 ifmam->ifmam_addrs = rtinfo.rti_addrs;
1054 rts_input(m, familyof(ifma->ifma_addr));
1058 * This is called to generate routing socket messages indicating
1059 * network interface arrival and departure.
1062 rt_ifannouncemsg(struct ifnet *ifp, int what)
1064 struct rt_addrinfo addrinfo;
1066 struct if_announcemsghdr *ifan;
1068 if (route_cb.any_count == 0)
1071 bzero(&addrinfo, sizeof addrinfo);
1072 m = rt_msg_mbuf(RTM_IFANNOUNCE, &addrinfo);
1076 ifan = mtod(m, struct if_announcemsghdr *);
1077 ifan->ifan_index = ifp->if_index;
1078 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1079 ifan->ifan_what = what;
1085 resizewalkarg(struct walkarg *w, int len)
1089 newptr = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1092 if (w->w_tmem != NULL)
1093 free(w->w_tmem, M_RTABLE);
1095 w->w_tmemsize = len;
1100 * This is used in dumping the kernel table via sysctl().
1103 sysctl_dumpentry(struct radix_node *rn, void *vw)
1105 struct walkarg *w = vw;
1106 struct rtentry *rt = (struct rtentry *)rn;
1107 struct rt_addrinfo rtinfo;
1110 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1113 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1114 rtinfo.rti_dst = rt_key(rt);
1115 rtinfo.rti_gateway = rt->rt_gateway;
1116 rtinfo.rti_netmask = rt_mask(rt);
1117 rtinfo.rti_genmask = rt->rt_genmask;
1118 if (rt->rt_ifp != NULL) {
1119 rtinfo.rti_ifpaddr =
1120 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
1121 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1122 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1123 rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1125 msglen = rt_msgsize(RTM_GET, &rtinfo);
1126 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1128 rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen);
1129 if (w->w_req != NULL) {
1130 struct rt_msghdr *rtm = w->w_tmem;
1132 rtm->rtm_flags = rt->rt_flags;
1133 rtm->rtm_use = rt->rt_use;
1134 rtm->rtm_rmx = rt->rt_rmx;
1135 rtm->rtm_index = rt->rt_ifp->if_index;
1136 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1137 rtm->rtm_addrs = rtinfo.rti_addrs;
1138 error = SYSCTL_OUT(w->w_req, rtm, msglen);
1145 sysctl_iflist(int af, struct walkarg *w)
1149 struct rt_addrinfo rtinfo;
1152 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1153 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1154 if (w->w_arg && w->w_arg != ifp->if_index)
1156 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1157 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1158 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1159 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1161 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1162 rtinfo.rti_ifpaddr = NULL;
1163 if (w->w_req != NULL && w->w_tmem != NULL) {
1164 struct if_msghdr *ifm = w->w_tmem;
1166 ifm->ifm_index = ifp->if_index;
1167 ifm->ifm_flags = ifp->if_flags;
1168 ifm->ifm_data = ifp->if_data;
1169 ifm->ifm_addrs = rtinfo.rti_addrs;
1170 error = SYSCTL_OUT(w->w_req, ifm, msglen);
1174 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1175 if (af && af != ifa->ifa_addr->sa_family)
1177 if (curproc->p_ucred->cr_prison &&
1178 prison_if(curthread, ifa->ifa_addr))
1180 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1181 rtinfo.rti_netmask = ifa->ifa_netmask;
1182 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1183 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1184 if (w->w_tmemsize < msglen &&
1185 resizewalkarg(w, msglen) != 0)
1187 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1188 if (w->w_req != NULL) {
1189 struct ifa_msghdr *ifam = w->w_tmem;
1191 ifam->ifam_index = ifa->ifa_ifp->if_index;
1192 ifam->ifam_flags = ifa->ifa_flags;
1193 ifam->ifam_metric = ifa->ifa_metric;
1194 ifam->ifam_addrs = rtinfo.rti_addrs;
1195 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1200 rtinfo.rti_netmask = NULL;
1201 rtinfo.rti_ifaaddr = NULL;
1202 rtinfo.rti_bcastaddr = NULL;
1208 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1210 int *name = (int *)arg1;
1211 u_int namelen = arg2;
1212 struct radix_node_head *rnh;
1213 int i, error = EINVAL;
1222 if (namelen != 3 && namelen != 4)
1225 bzero(&w, sizeof w);
1231 * Optional third argument specifies cpu, used primarily for
1232 * debugging the route table.
1235 if (name[3] < 0 || name[3] >= ncpus)
1238 lwkt_migratecpu(name[3]);
1246 for (i = 1; i <= AF_MAX; i++)
1247 if ((rnh = rt_tables[mycpuid][i]) &&
1248 (af == 0 || af == i) &&
1249 (error = rnh->rnh_walktree(rnh,
1250 sysctl_dumpentry, &w)))
1255 error = sysctl_iflist(af, &w);
1258 if (w.w_tmem != NULL)
1259 free(w.w_tmem, M_RTABLE);
1261 lwkt_migratecpu(origcpu);
1265 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1268 * Definitions of protocols supported in the ROUTE domain.
1271 extern struct domain routedomain; /* or at least forward */
1273 static struct protosw routesw[] = {
1274 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1275 0, route_output, raw_ctlinput, 0,
1282 static struct domain routedomain = {
1283 PF_ROUTE, "route", NULL, NULL, NULL,
1284 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])],