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.27 2005/05/29 10:08:36 hsu Exp $
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/sysctl.h>
93 #include <sys/malloc.h>
95 #include <sys/protosw.h>
96 #include <sys/socket.h>
97 #include <sys/socketvar.h>
98 #include <sys/domain.h>
100 #include <machine/stdarg.h>
103 #include <net/route.h>
104 #include <net/raw_cb.h>
106 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
108 static struct route_cb {
116 static const struct sockaddr route_src = { 2, PF_ROUTE, };
122 struct sysctl_req *w_req;
126 rt_msg_mbuf (int, struct rt_addrinfo *);
127 static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
128 static int rt_msgsize (int type, struct rt_addrinfo *rtinfo);
129 static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
130 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
131 static int sysctl_iflist (int af, struct walkarg *w);
132 static int route_output(struct mbuf *, struct socket *, ...);
133 static void rt_setmetrics (u_long, struct rt_metrics *,
134 struct rt_metrics *);
137 * It really doesn't make any sense at all for this code to share much
138 * with raw_usrreq.c, since its functionality is so restricted. XXX
141 rts_abort(struct socket *so)
146 error = raw_usrreqs.pru_abort(so);
151 /* pru_accept is EOPNOTSUPP */
154 rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
159 if (sotorawcb(so) != NULL)
160 return EISCONN; /* XXX panic? */
162 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
167 * The splnet() is necessary to block protocols from sending
168 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
169 * this PCB is extant but incompletely initialized.
170 * Probably we should try to do more of this work beforehand and
175 error = raw_attach(so, proto, ai->sb_rlimit);
182 switch(rp->rcb_proto.sp_protocol) {
187 route_cb.ip6_count++;
190 route_cb.ipx_count++;
196 rp->rcb_faddr = &route_src;
197 route_cb.any_count++;
199 so->so_options |= SO_USELOOPBACK;
205 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
210 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
216 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
221 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
226 /* pru_connect2 is EOPNOTSUPP */
227 /* pru_control is EOPNOTSUPP */
230 rts_detach(struct socket *so)
232 struct rawcb *rp = sotorawcb(so);
237 switch(rp->rcb_proto.sp_protocol) {
242 route_cb.ip6_count--;
245 route_cb.ipx_count--;
251 route_cb.any_count--;
253 error = raw_usrreqs.pru_detach(so);
259 rts_disconnect(struct socket *so)
264 error = raw_usrreqs.pru_disconnect(so);
269 /* pru_listen is EOPNOTSUPP */
272 rts_peeraddr(struct socket *so, struct sockaddr **nam)
277 error = raw_usrreqs.pru_peeraddr(so, nam);
282 /* pru_rcvd is EOPNOTSUPP */
283 /* pru_rcvoob is EOPNOTSUPP */
286 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
287 struct mbuf *control, struct thread *td)
292 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
297 /* pru_sense is null */
300 rts_shutdown(struct socket *so)
305 error = raw_usrreqs.pru_shutdown(so);
311 rts_sockaddr(struct socket *so, struct sockaddr **nam)
316 error = raw_usrreqs.pru_sockaddr(so, nam);
321 static struct pr_usrreqs route_usrreqs = {
322 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
323 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
324 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
325 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
326 sosend, soreceive, sopoll
329 static __inline sa_family_t
330 familyof(struct sockaddr *sa)
332 return (sa != NULL ? sa->sa_family : 0);
336 rts_input(struct mbuf *m, sa_family_t family)
338 static const struct sockaddr route_dst = { 2, PF_ROUTE, };
339 struct sockproto route_proto = { PF_ROUTE, family };
341 raw_input(m, &route_proto, &route_src, &route_dst);
345 reallocbuf(void *ptr, size_t len, size_t olen)
349 newptr = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
352 bcopy(ptr, newptr, olen);
358 * Internal helper routine for route_output().
361 fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
362 struct rt_addrinfo *rtinfo)
365 struct rt_msghdr *rtm = *prtm;
367 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
368 rtinfo->rti_dst = rt_key(rt);
369 rtinfo->rti_gateway = rt->rt_gateway;
370 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */
371 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */
372 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
373 if (rt->rt_ifp != NULL) {
374 rtinfo->rti_ifpaddr =
375 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
376 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
377 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
378 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
379 rtm->rtm_index = rt->rt_ifp->if_index;
381 rtinfo->rti_ifpaddr = NULL;
382 rtinfo->rti_ifaaddr = NULL;
386 msglen = rt_msgsize(rtm->rtm_type, rtinfo);
387 if (rtm->rtm_msglen < msglen) {
388 rtm = reallocbuf(rtm, msglen, rtm->rtm_msglen);
393 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
395 rtm->rtm_flags = rt->rt_flags;
396 rtm->rtm_rmx = rt->rt_rmx;
397 rtm->rtm_addrs = rtinfo->rti_addrs;
404 route_output(struct mbuf *m, struct socket *so, ...)
406 struct rt_msghdr *rtm = NULL;
407 struct rtentry *rt = NULL;
408 struct rtentry *saved_nrt = NULL;
409 struct radix_node_head *rnh;
410 struct ifaddr *ifa = NULL;
411 struct rawcb *rp = NULL;
412 struct pr_output_info *oi;
413 struct rt_addrinfo rtinfo;
418 oi = __va_arg(ap, struct pr_output_info *);
421 #define gotoerr(e) { error = e; goto flush;}
424 (m->m_len < sizeof(long) &&
425 (m = m_pullup(m, sizeof(long))) == NULL))
427 if (!(m->m_flags & M_PKTHDR))
428 panic("route_output");
429 len = m->m_pkthdr.len;
430 if (len < sizeof(struct rt_msghdr) ||
431 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
432 rtinfo.rti_dst = NULL;
435 rtm = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
437 rtinfo.rti_dst = NULL;
440 m_copydata(m, 0, len, (caddr_t)rtm);
441 if (rtm->rtm_version != RTM_VERSION) {
442 rtinfo.rti_dst = NULL;
443 gotoerr(EPROTONOSUPPORT);
445 rtm->rtm_pid = oi->p_pid;
446 bzero(&rtinfo, sizeof(struct rt_addrinfo));
447 rtinfo.rti_addrs = rtm->rtm_addrs;
448 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0) {
449 rtinfo.rti_dst = NULL;
452 rtinfo.rti_flags = rtm->rtm_flags;
453 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
454 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
457 if (rtinfo.rti_genmask != NULL) {
458 struct radix_node *n;
460 #define clen(s) (*(u_char *)(s))
461 n = rn_addmask((char *)rtinfo.rti_genmask, TRUE, 1);
463 rtinfo.rti_genmask->sa_len >= clen(n->rn_key) &&
464 bcmp((char *)rtinfo.rti_genmask + 1,
465 (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0)
466 rtinfo.rti_genmask = (struct sockaddr *)n->rn_key;
472 * Verify that the caller has the appropriate privilege; RTM_GET
473 * is the only operation the non-superuser is allowed.
475 if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
478 switch (rtm->rtm_type) {
480 if (rtinfo.rti_gateway == NULL)
482 error = rtrequest1(RTM_ADD, &rtinfo, &saved_nrt);
483 if (error == 0 && saved_nrt != NULL) {
484 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
486 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
487 saved_nrt->rt_rmx.rmx_locks |=
488 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
489 --saved_nrt->rt_refcnt;
490 saved_nrt->rt_genmask = rtinfo.rti_genmask;
494 error = rtrequest1(RTM_DELETE, &rtinfo, &saved_nrt);
496 if ((rt = saved_nrt))
498 if (fillrtmsg(&rtm, rt, &rtinfo) != 0)
505 if ((rnh = rt_tables[rtinfo.rti_dst->sa_family]) == NULL)
506 gotoerr(EAFNOSUPPORT);
507 rt = (struct rtentry *)
508 rnh->rnh_lookup((char *)rtinfo.rti_dst,
509 (char *)rtinfo.rti_netmask, rnh);
514 switch(rtm->rtm_type) {
516 if (fillrtmsg(&rtm, rt, &rtinfo) != 0)
521 * new gateway could require new ifaddr, ifp;
522 * flags may also be different; ifp may be specified
523 * by ll sockaddr when protocol address is ambiguous
525 if (((rt->rt_flags & RTF_GATEWAY) &&
526 rtinfo.rti_gateway != NULL) ||
527 rtinfo.rti_ifpaddr != NULL ||
528 (rtinfo.rti_ifaaddr != NULL &&
529 sa_equal(rtinfo.rti_ifaaddr,
530 rt->rt_ifa->ifa_addr))) {
531 error = rt_getifa(&rtinfo);
535 if (rtinfo.rti_gateway != NULL) {
536 error = rt_setgate(rt, rt_key(rt),
541 if ((ifa = rtinfo.rti_ifa) != NULL) {
542 struct ifaddr *oifa = rt->rt_ifa;
545 if (oifa && oifa->ifa_rtrequest)
546 oifa->ifa_rtrequest(RTM_DELETE,
551 rt->rt_ifp = rtinfo.rti_ifp;
554 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
556 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
557 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &rtinfo);
558 if (rtinfo.rti_genmask != NULL)
559 rt->rt_genmask = rtinfo.rti_genmask;
564 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
565 rt->rt_rmx.rmx_locks |=
566 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
578 rtm->rtm_errno = error;
580 rtm->rtm_flags |= RTF_DONE;
585 * Check to see if we don't want our own messages.
587 if (!(so->so_options & SO_USELOOPBACK)) {
588 if (route_cb.any_count <= 1) {
594 /* There is another listener, so construct message */
598 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
599 if (m->m_pkthdr.len < rtm->rtm_msglen) {
602 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
603 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
607 rp->rcb_proto.sp_family = 0; /* Avoid us */
609 rts_input(m, familyof(rtinfo.rti_dst));
611 rp->rcb_proto.sp_family = PF_ROUTE;
616 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
618 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
619 setmetric(RTV_RPIPE, rmx_recvpipe);
620 setmetric(RTV_SPIPE, rmx_sendpipe);
621 setmetric(RTV_SSTHRESH, rmx_ssthresh);
622 setmetric(RTV_RTT, rmx_rtt);
623 setmetric(RTV_RTTVAR, rmx_rttvar);
624 setmetric(RTV_HOPCOUNT, rmx_hopcount);
625 setmetric(RTV_MTU, rmx_mtu);
626 setmetric(RTV_EXPIRE, rmx_expire);
631 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
634 * Extract the addresses of the passed sockaddrs.
635 * Do a little sanity checking so as to avoid bad memory references.
636 * This data is derived straight from userland.
639 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
644 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
645 if ((rtinfo->rti_addrs & (1 << i)) == 0)
647 sa = (struct sockaddr *)cp;
651 if ((cp + sa->sa_len) > cplim) {
656 * There are no more... Quit now.
657 * If there are more bits, they are in error.
658 * I've seen this. route(1) can evidently generate these.
659 * This causes kernel to core dump.
660 * For compatibility, if we see this, point to a safe address.
662 if (sa->sa_len == 0) {
663 static struct sockaddr sa_zero = {
664 sizeof sa_zero, AF_INET,
667 rtinfo->rti_info[i] = &sa_zero;
668 return (0); /* should be EINVAL but for compat */
671 /* Accept the sockaddr. */
672 rtinfo->rti_info[i] = sa;
673 cp += ROUNDUP(sa->sa_len);
679 rt_msghdrsize(int type)
684 return sizeof(struct ifa_msghdr);
687 return sizeof(struct ifma_msghdr);
689 return sizeof(struct if_msghdr);
691 return sizeof(struct if_announcemsghdr);
693 return sizeof(struct rt_msghdr);
698 rt_msgsize(int type, struct rt_addrinfo *rtinfo)
702 len = rt_msghdrsize(type);
703 for (i = 0; i < RTAX_MAX; i++) {
704 if (rtinfo->rti_info[i] != NULL)
705 len += ROUNDUP(rtinfo->rti_info[i]->sa_len);
712 * Build a routing message in a buffer.
713 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
714 * to the end of the buffer after the message header.
716 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
717 * This side-effect can be avoided if we reorder the addrs bitmask field in all
718 * the route messages to line up so we can set it here instead of back in the
722 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
724 struct rt_msghdr *rtm;
728 rtm = (struct rt_msghdr *) buf;
729 rtm->rtm_version = RTM_VERSION;
730 rtm->rtm_type = type;
731 rtm->rtm_msglen = msglen;
733 cp = (char *)buf + rt_msghdrsize(type);
734 rtinfo->rti_addrs = 0;
735 for (i = 0; i < RTAX_MAX; i++) {
738 if ((sa = rtinfo->rti_info[i]) == NULL)
740 rtinfo->rti_addrs |= (1 << i);
741 dlen = ROUNDUP(sa->sa_len);
748 * Build a routing message in a mbuf chain.
749 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
750 * to the end of the mbuf after the message header.
752 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
753 * This side-effect can be avoided if we reorder the addrs bitmask field in all
754 * the route messages to line up so we can set it here instead of back in the
758 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
761 struct rt_msghdr *rtm;
765 hlen = rt_msghdrsize(type);
766 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
768 m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
771 m->m_pkthdr.len = m->m_len = hlen;
772 m->m_pkthdr.rcvif = NULL;
773 rtinfo->rti_addrs = 0;
775 for (i = 0; i < RTAX_MAX; i++) {
779 if ((sa = rtinfo->rti_info[i]) == NULL)
781 rtinfo->rti_addrs |= (1 << i);
782 dlen = ROUNDUP(sa->sa_len);
783 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
786 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
790 rtm = mtod(m, struct rt_msghdr *);
792 rtm->rtm_msglen = len;
793 rtm->rtm_version = RTM_VERSION;
794 rtm->rtm_type = type;
799 * This routine is called to generate a message from the routing
800 * socket indicating that a redirect has occurred, a routing lookup
801 * has failed, or that a protocol has detected timeouts to a particular
805 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
807 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
808 struct rt_msghdr *rtm;
811 if (route_cb.any_count == 0)
813 m = rt_msg_mbuf(type, rtinfo);
816 rtm = mtod(m, struct rt_msghdr *);
817 rtm->rtm_flags = RTF_DONE | flags;
818 rtm->rtm_errno = error;
819 rtm->rtm_addrs = rtinfo->rti_addrs;
820 rts_input(m, familyof(dst));
824 rt_dstmsg(int type, struct sockaddr *dst, int error)
826 struct rt_msghdr *rtm;
827 struct rt_addrinfo addrs;
830 if (route_cb.any_count == 0)
832 bzero(&addrs, sizeof(struct rt_addrinfo));
833 addrs.rti_info[RTAX_DST] = dst;
834 m = rt_msg_mbuf(type, &addrs);
837 rtm = mtod(m, struct rt_msghdr *);
838 rtm->rtm_flags = RTF_DONE;
839 rtm->rtm_errno = error;
840 rtm->rtm_addrs = addrs.rti_addrs;
841 rts_input(m, familyof(dst));
845 * This routine is called to generate a message from the routing
846 * socket indicating that the status of a network interface has changed.
849 rt_ifmsg(struct ifnet *ifp)
851 struct if_msghdr *ifm;
853 struct rt_addrinfo rtinfo;
855 if (route_cb.any_count == 0)
857 bzero(&rtinfo, sizeof(struct rt_addrinfo));
858 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
861 ifm = mtod(m, struct if_msghdr *);
862 ifm->ifm_index = ifp->if_index;
863 ifm->ifm_flags = ifp->if_flags;
864 ifm->ifm_data = ifp->if_data;
870 rt_ifamsg(int cmd, struct ifaddr *ifa)
872 struct ifa_msghdr *ifam;
873 struct rt_addrinfo rtinfo;
875 struct ifnet *ifp = ifa->ifa_ifp;
877 bzero(&rtinfo, sizeof(struct rt_addrinfo));
878 rtinfo.rti_ifaaddr = ifa->ifa_addr;
879 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
880 rtinfo.rti_netmask = ifa->ifa_netmask;
881 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
883 m = rt_msg_mbuf(cmd, &rtinfo);
887 ifam = mtod(m, struct ifa_msghdr *);
888 ifam->ifam_index = ifp->if_index;
889 ifam->ifam_metric = ifa->ifa_metric;
890 ifam->ifam_flags = ifa->ifa_flags;
891 ifam->ifam_addrs = rtinfo.rti_addrs;
893 rts_input(m, familyof(ifa->ifa_addr));
897 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
899 struct rt_msghdr *rtm;
900 struct rt_addrinfo rtinfo;
902 struct sockaddr *dst;
907 bzero(&rtinfo, sizeof(struct rt_addrinfo));
908 rtinfo.rti_dst = dst = rt_key(rt);
909 rtinfo.rti_gateway = rt->rt_gateway;
910 rtinfo.rti_netmask = rt_mask(rt);
912 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
913 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
915 m = rt_msg_mbuf(cmd, &rtinfo);
919 rtm = mtod(m, struct rt_msghdr *);
921 rtm->rtm_index = ifp->if_index;
922 rtm->rtm_flags |= rt->rt_flags;
923 rtm->rtm_errno = error;
924 rtm->rtm_addrs = rtinfo.rti_addrs;
926 rts_input(m, familyof(dst));
930 * This is called to generate messages from the routing socket
931 * indicating a network interface has had addresses associated with it.
932 * if we ever reverse the logic and replace messages TO the routing
933 * socket indicate a request to configure interfaces, then it will
934 * be unnecessary as the routing socket will automatically generate
938 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
940 if (route_cb.any_count == 0)
943 if (cmd == RTM_ADD) {
944 rt_ifamsg(RTM_NEWADDR, ifa);
945 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
947 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
948 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
949 rt_ifamsg(RTM_DELADDR, ifa);
954 * This is the analogue to the rt_newaddrmsg which performs the same
955 * function but for multicast group memberhips. This is easier since
956 * there is no route state to worry about.
959 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
961 struct rt_addrinfo rtinfo;
962 struct mbuf *m = NULL;
963 struct ifnet *ifp = ifma->ifma_ifp;
964 struct ifma_msghdr *ifmam;
966 if (route_cb.any_count == 0)
969 bzero(&rtinfo, sizeof(struct rt_addrinfo));
970 rtinfo.rti_ifaaddr = ifma->ifma_addr;
971 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrhead))
972 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
974 * If a link-layer address is present, present it as a ``gateway''
975 * (similarly to how ARP entries, e.g., are presented).
977 rtinfo.rti_gateway = ifma->ifma_lladdr;
979 m = rt_msg_mbuf(cmd, &rtinfo);
983 ifmam = mtod(m, struct ifma_msghdr *);
984 ifmam->ifmam_index = ifp->if_index;
985 ifmam->ifmam_addrs = rtinfo.rti_addrs;
987 rts_input(m, familyof(ifma->ifma_addr));
991 * This is called to generate routing socket messages indicating
992 * network interface arrival and departure.
995 rt_ifannouncemsg(struct ifnet *ifp, int what)
997 struct rt_addrinfo addrinfo;
999 struct if_announcemsghdr *ifan;
1001 if (route_cb.any_count == 0)
1004 bzero(&addrinfo, sizeof addrinfo);
1005 m = rt_msg_mbuf(RTM_IFANNOUNCE, &addrinfo);
1009 ifan = mtod(m, struct if_announcemsghdr *);
1010 ifan->ifan_index = ifp->if_index;
1011 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1012 ifan->ifan_what = what;
1018 resizewalkarg(struct walkarg *w, int len)
1022 newptr = malloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1025 if (w->w_tmem != NULL)
1026 free(w->w_tmem, M_RTABLE);
1028 w->w_tmemsize = len;
1033 * This is used in dumping the kernel table via sysctl().
1036 sysctl_dumpentry(struct radix_node *rn, void *vw)
1038 struct walkarg *w = vw;
1039 struct rtentry *rt = (struct rtentry *)rn;
1040 struct rt_addrinfo rtinfo;
1043 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1046 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1047 rtinfo.rti_dst = rt_key(rt);
1048 rtinfo.rti_gateway = rt->rt_gateway;
1049 rtinfo.rti_netmask = rt_mask(rt);
1050 rtinfo.rti_genmask = rt->rt_genmask;
1051 if (rt->rt_ifp != NULL) {
1052 rtinfo.rti_ifpaddr =
1053 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
1054 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1055 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1056 rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1058 msglen = rt_msgsize(RTM_GET, &rtinfo);
1059 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1061 rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen);
1062 if (w->w_req != NULL) {
1063 struct rt_msghdr *rtm = w->w_tmem;
1065 rtm->rtm_flags = rt->rt_flags;
1066 rtm->rtm_use = rt->rt_use;
1067 rtm->rtm_rmx = rt->rt_rmx;
1068 rtm->rtm_index = rt->rt_ifp->if_index;
1069 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1070 rtm->rtm_addrs = rtinfo.rti_addrs;
1071 error = SYSCTL_OUT(w->w_req, rtm, msglen);
1078 sysctl_iflist(int af, struct walkarg *w)
1082 struct rt_addrinfo rtinfo;
1085 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1086 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1087 if (w->w_arg && w->w_arg != ifp->if_index)
1089 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1090 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1091 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1092 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1094 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1095 rtinfo.rti_ifpaddr = NULL;
1096 if (w->w_req != NULL && w->w_tmem != NULL) {
1097 struct if_msghdr *ifm = w->w_tmem;
1099 ifm->ifm_index = ifp->if_index;
1100 ifm->ifm_flags = ifp->if_flags;
1101 ifm->ifm_data = ifp->if_data;
1102 ifm->ifm_addrs = rtinfo.rti_addrs;
1103 error = SYSCTL_OUT(w->w_req, ifm, msglen);
1107 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1108 if (af && af != ifa->ifa_addr->sa_family)
1110 if (curproc->p_ucred->cr_prison &&
1111 prison_if(curthread, ifa->ifa_addr))
1113 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1114 rtinfo.rti_netmask = ifa->ifa_netmask;
1115 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1116 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1117 if (w->w_tmemsize < msglen &&
1118 resizewalkarg(w, msglen) != 0)
1120 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1121 if (w->w_req != NULL) {
1122 struct ifa_msghdr *ifam = w->w_tmem;
1124 ifam->ifam_index = ifa->ifa_ifp->if_index;
1125 ifam->ifam_flags = ifa->ifa_flags;
1126 ifam->ifam_metric = ifa->ifa_metric;
1127 ifam->ifam_addrs = rtinfo.rti_addrs;
1128 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1133 rtinfo.rti_netmask = NULL;
1134 rtinfo.rti_ifaaddr = NULL;
1135 rtinfo.rti_bcastaddr = NULL;
1141 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1143 int *name = (int *)arg1;
1144 u_int namelen = arg2;
1145 struct radix_node_head *rnh;
1146 int i, s, error = EINVAL;
1157 bzero(&w, sizeof w);
1167 for (i = 1; i <= AF_MAX; i++)
1168 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1169 (error = rnh->rnh_walktree(rnh,
1170 sysctl_dumpentry, &w)))
1175 error = sysctl_iflist(af, &w);
1178 if (w.w_tmem != NULL)
1179 free(w.w_tmem, M_RTABLE);
1183 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1186 * Definitions of protocols supported in the ROUTE domain.
1189 extern struct domain routedomain; /* or at least forward */
1191 static struct protosw routesw[] = {
1192 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1193 0, route_output, raw_ctlinput, 0,
1200 static struct domain routedomain = {
1201 PF_ROUTE, "route", NULL, NULL, NULL,
1202 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])],