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 $
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/sysctl.h>
73 #include <sys/malloc.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/domain.h>
80 #include <sys/thread2.h>
81 #include <sys/socketvar2.h>
84 #include <net/route.h>
85 #include <net/raw_cb.h>
86 #include <net/netmsg2.h>
87 #include <net/netisr2.h>
90 extern void sctp_add_ip_address(struct ifaddr *ifa);
91 extern void sctp_delete_ip_address(struct ifaddr *ifa);
94 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
96 static struct route_cb {
103 static const struct sockaddr route_src = { 2, PF_ROUTE, };
109 struct sysctl_req *w_req;
113 rt_msg_mbuf (int, struct rt_addrinfo *);
114 static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
115 static int rt_msgsize (int type, struct rt_addrinfo *rtinfo);
116 static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
117 static int sysctl_dumpentry (struct radix_node *rn, void *vw);
118 static int sysctl_iflist (int af, struct walkarg *w);
119 static int route_output(struct mbuf *, struct socket *, ...);
120 static void rt_setmetrics (u_long, struct rt_metrics *,
121 struct rt_metrics *);
124 * It really doesn't make any sense at all for this code to share much
125 * with raw_usrreq.c, since its functionality is so restricted. XXX
128 rts_abort(netmsg_t msg)
131 raw_usrreqs.pru_abort(msg);
132 /* msg invalid now */
136 /* pru_accept is EOPNOTSUPP */
139 rts_attach(netmsg_t msg)
141 struct socket *so = msg->base.nm_so;
142 struct pru_attach_info *ai = msg->attach.nm_ai;
144 int proto = msg->attach.nm_proto;
148 if (sotorawcb(so) != NULL) {
153 rp = kmalloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
156 * The critical section is necessary to block protocols from sending
157 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
158 * this PCB is extant but incompletely initialized.
159 * Probably we should try to do more of this work beforehand and
160 * eliminate the critical section.
163 soreference(so); /* so_pcb assignment */
164 error = raw_attach(so, proto, ai->sb_rlimit);
170 switch(rp->rcb_proto.sp_protocol) {
175 route_cb.ip6_count++;
178 rp->rcb_faddr = &route_src;
179 route_cb.any_count++;
181 so->so_options |= SO_USELOOPBACK;
185 lwkt_replymsg(&msg->lmsg, error);
189 rts_bind(netmsg_t msg)
192 raw_usrreqs.pru_bind(msg); /* xxx just EINVAL */
193 /* msg invalid now */
198 rts_connect(netmsg_t msg)
201 raw_usrreqs.pru_connect(msg); /* XXX just EINVAL */
202 /* msg invalid now */
206 /* pru_connect2 is EOPNOTSUPP */
207 /* pru_control is EOPNOTSUPP */
210 rts_detach(netmsg_t msg)
212 struct socket *so = msg->base.nm_so;
213 struct rawcb *rp = sotorawcb(so);
217 switch(rp->rcb_proto.sp_protocol) {
222 route_cb.ip6_count--;
225 route_cb.any_count--;
227 raw_usrreqs.pru_detach(msg);
228 /* msg invalid now */
233 rts_disconnect(netmsg_t msg)
236 raw_usrreqs.pru_disconnect(msg);
237 /* msg invalid now */
241 /* pru_listen is EOPNOTSUPP */
244 rts_peeraddr(netmsg_t msg)
247 raw_usrreqs.pru_peeraddr(msg);
248 /* msg invalid now */
252 /* pru_rcvd is EOPNOTSUPP */
253 /* pru_rcvoob is EOPNOTSUPP */
256 rts_send(netmsg_t msg)
259 raw_usrreqs.pru_send(msg);
260 /* msg invalid now */
264 /* pru_sense is null */
267 rts_shutdown(netmsg_t msg)
270 raw_usrreqs.pru_shutdown(msg);
271 /* msg invalid now */
276 rts_sockaddr(netmsg_t msg)
279 raw_usrreqs.pru_sockaddr(msg);
280 /* msg invalid now */
284 static struct pr_usrreqs route_usrreqs = {
285 .pru_abort = rts_abort,
286 .pru_accept = pr_generic_notsupp,
287 .pru_attach = rts_attach,
288 .pru_bind = rts_bind,
289 .pru_connect = rts_connect,
290 .pru_connect2 = pr_generic_notsupp,
291 .pru_control = pr_generic_notsupp,
292 .pru_detach = rts_detach,
293 .pru_disconnect = rts_disconnect,
294 .pru_listen = pr_generic_notsupp,
295 .pru_peeraddr = rts_peeraddr,
296 .pru_rcvd = pr_generic_notsupp,
297 .pru_rcvoob = pr_generic_notsupp,
298 .pru_send = rts_send,
299 .pru_sense = pru_sense_null,
300 .pru_shutdown = rts_shutdown,
301 .pru_sockaddr = rts_sockaddr,
302 .pru_sosend = sosend,
303 .pru_soreceive = soreceive
306 static __inline sa_family_t
307 familyof(struct sockaddr *sa)
309 return (sa != NULL ? sa->sa_family : 0);
313 * Routing socket input function. The packet must be serialized onto cpu 0.
314 * We use the cpu0_soport() netisr processing loop to handle it.
316 * This looks messy but it means that anyone, including interrupt code,
317 * can send a message to the routing socket.
320 rts_input_handler(netmsg_t msg)
322 static const struct sockaddr route_dst = { 2, PF_ROUTE, };
323 struct sockproto route_proto;
324 struct netmsg_packet *pmsg = &msg->packet;
329 family = pmsg->base.lmsg.u.ms_result;
330 route_proto.sp_family = PF_ROUTE;
331 route_proto.sp_protocol = family;
336 skip = m->m_pkthdr.header;
337 m->m_pkthdr.header = NULL;
339 raw_input(m, &route_proto, &route_src, &route_dst, skip);
343 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip)
345 struct netmsg_packet *pmsg;
350 port = netisr_cpuport(0); /* XXX same as for routing socket */
351 pmsg = &m->m_hdr.mh_netmsg;
352 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
353 0, rts_input_handler);
355 pmsg->base.lmsg.u.ms_result = family;
356 m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */
357 lwkt_sendmsg(port, &pmsg->base.lmsg);
361 rts_input(struct mbuf *m, sa_family_t family)
363 rts_input_skip(m, family, NULL);
367 reallocbuf_nofree(void *ptr, size_t len, size_t olen)
371 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
374 bcopy(ptr, newptr, olen);
379 * Internal helper routine for route_output().
382 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
383 struct rt_addrinfo *rtinfo)
386 struct rt_msghdr *rtm = *prtm;
388 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
389 rtinfo->rti_dst = rt_key(rt);
390 rtinfo->rti_gateway = rt->rt_gateway;
391 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */
392 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */
393 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
394 if (rt->rt_ifp != NULL) {
395 rtinfo->rti_ifpaddr =
396 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])
398 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
399 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
400 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
401 rtm->rtm_index = rt->rt_ifp->if_index;
403 rtinfo->rti_ifpaddr = NULL;
404 rtinfo->rti_ifaaddr = NULL;
406 } else if (rt->rt_ifp != NULL) {
407 rtm->rtm_index = rt->rt_ifp->if_index;
410 msglen = rt_msgsize(rtm->rtm_type, rtinfo);
411 if (rtm->rtm_msglen < msglen) {
412 /* NOTE: Caller will free the old rtm accordingly */
413 rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen);
418 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
420 rtm->rtm_flags = rt->rt_flags;
421 rtm->rtm_rmx = rt->rt_rmx;
422 rtm->rtm_addrs = rtinfo->rti_addrs;
428 struct rt_msghdr *bak_rtm;
429 struct rt_msghdr *new_rtm;
433 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt,
434 struct rt_addrinfo *rtinfo)
436 struct rt_msghdr *rtm = arg->new_rtm;
439 error = _fillrtmsg(&rtm, rt, rtinfo);
441 if (arg->new_rtm != rtm) {
443 * _fillrtmsg() just allocated a new rtm;
444 * if the previously allocated rtm is not
445 * the backing rtm, it should be freed.
447 if (arg->new_rtm != arg->bak_rtm)
448 kfree(arg->new_rtm, M_RTABLE);
455 static void route_output_add_callback(int, int, struct rt_addrinfo *,
456 struct rtentry *, void *);
457 static void route_output_delete_callback(int, int, struct rt_addrinfo *,
458 struct rtentry *, void *);
459 static int route_output_get_callback(int, struct rt_addrinfo *,
460 struct rtentry *, void *, int);
461 static int route_output_change_callback(int, struct rt_addrinfo *,
462 struct rtentry *, void *, int);
463 static int route_output_lock_callback(int, struct rt_addrinfo *,
464 struct rtentry *, void *, int);
468 route_output(struct mbuf *m, struct socket *so, ...)
471 struct rt_msghdr *rtm = NULL;
472 struct rawcb *rp = NULL;
473 struct pr_output_info *oi;
474 struct rt_addrinfo rtinfo;
482 oi = __va_arg(ap, struct pr_output_info *);
485 family = familyof(NULL);
487 #define gotoerr(e) { error = e; goto flush;}
490 (m->m_len < sizeof(long) &&
491 (m = m_pullup(m, sizeof(long))) == NULL))
493 len = m->m_pkthdr.len;
494 if (len < sizeof(struct rt_msghdr) ||
495 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
498 rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
502 m_copydata(m, 0, len, (caddr_t)rtm);
503 if (rtm->rtm_version != RTM_VERSION)
504 gotoerr(EPROTONOSUPPORT);
506 rtm->rtm_pid = oi->p_pid;
507 bzero(&rtinfo, sizeof(struct rt_addrinfo));
508 rtinfo.rti_addrs = rtm->rtm_addrs;
509 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0)
512 rtinfo.rti_flags = rtm->rtm_flags;
513 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
514 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
517 family = familyof(rtinfo.rti_dst);
520 * Verify that the caller has the appropriate privilege; RTM_GET
521 * is the only operation the non-superuser is allowed.
523 if (rtm->rtm_type != RTM_GET &&
524 priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0)
527 if (rtinfo.rti_genmask != NULL) {
528 error = rtmask_add_global(rtinfo.rti_genmask,
529 rtm->rtm_type != RTM_GET ?
530 RTREQ_PRIO_HIGH : RTREQ_PRIO_NORM);
535 switch (rtm->rtm_type) {
537 if (rtinfo.rti_gateway == NULL) {
540 error = rtrequest1_global(RTM_ADD, &rtinfo,
541 route_output_add_callback, rtm, RTREQ_PRIO_HIGH);
546 * Backing rtm (bak_rtm) could _not_ be freed during
547 * rtrequest1_global or rtsearch_global, even if the
548 * callback reallocates the rtm due to its size changes,
549 * since rtinfo points to the backing rtm's memory area.
550 * After rtrequest1_global or rtsearch_global returns,
551 * it is safe to free the backing rtm, since rtinfo will
552 * not be used anymore.
554 * new_rtm will be used to save the new rtm allocated
555 * by rtrequest1_global or rtsearch_global.
559 error = rtrequest1_global(RTM_DELETE, &rtinfo,
560 route_output_delete_callback, &arg, RTREQ_PRIO_HIGH);
562 if (rtm != arg.bak_rtm)
563 kfree(arg.bak_rtm, M_RTABLE);
566 /* See the comment in RTM_DELETE */
569 error = rtsearch_global(RTM_GET, &rtinfo,
570 route_output_get_callback, &arg, RTS_NOEXACTMATCH,
573 if (rtm != arg.bak_rtm)
574 kfree(arg.bak_rtm, M_RTABLE);
577 error = rtsearch_global(RTM_CHANGE, &rtinfo,
578 route_output_change_callback, rtm, RTS_EXACTMATCH,
582 error = rtsearch_global(RTM_LOCK, &rtinfo,
583 route_output_lock_callback, rtm, RTS_EXACTMATCH,
593 rtm->rtm_errno = error;
595 rtm->rtm_flags |= RTF_DONE;
599 * Check to see if we don't want our own messages.
601 if (!(so->so_options & SO_USELOOPBACK)) {
602 if (route_cb.any_count <= 1) {
604 kfree(rtm, M_RTABLE);
608 /* There is another listener, so construct message */
612 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
613 if (m->m_pkthdr.len < rtm->rtm_msglen) {
616 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
617 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
618 kfree(rtm, M_RTABLE);
621 rts_input_skip(m, family, rp);
626 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
627 struct rtentry *rt, void *arg)
629 struct rt_msghdr *rtm = arg;
631 if (error == 0 && rt != NULL) {
632 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
634 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
635 rt->rt_rmx.rmx_locks |=
636 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
637 if (rtinfo->rti_genmask != NULL) {
638 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
639 if (rt->rt_genmask == NULL) {
641 * This should not happen, since we
642 * have already installed genmask
643 * on each CPU before we reach here.
645 panic("genmask is gone!?");
648 rt->rt_genmask = NULL;
650 rtm->rtm_index = rt->rt_ifp->if_index;
655 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
656 struct rtentry *rt, void *arg)
658 if (error == 0 && rt) {
660 if (fillrtmsg(arg, rt, rtinfo) != 0) {
662 /* XXX no way to return the error */
666 if (rt && rt->rt_refcnt == 0) {
673 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo,
674 struct rtentry *rt, void *arg, int found_cnt)
676 int error, found = 0;
678 if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0)
681 error = fillrtmsg(arg, rt, rtinfo);
682 if (!error && found) {
683 /* Got the exact match, we could return now! */
690 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo,
691 struct rtentry *rt, void *arg, int found_cnt)
693 struct rt_msghdr *rtm = arg;
698 * new gateway could require new ifaddr, ifp;
699 * flags may also be different; ifp may be specified
700 * by ll sockaddr when protocol address is ambiguous
702 if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
703 rtinfo->rti_ifpaddr != NULL ||
704 (rtinfo->rti_ifaaddr != NULL &&
705 !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) {
706 error = rt_getifa(rtinfo);
710 if (rtinfo->rti_gateway != NULL) {
712 * We only need to generate rtmsg upon the
713 * first route to be changed.
715 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway,
716 found_cnt == 1 ? RTL_REPORTMSG : RTL_DONTREPORT);
720 if ((ifa = rtinfo->rti_ifa) != NULL) {
721 struct ifaddr *oifa = rt->rt_ifa;
724 if (oifa && oifa->ifa_rtrequest)
725 oifa->ifa_rtrequest(RTM_DELETE, rt);
729 rt->rt_ifp = rtinfo->rti_ifp;
732 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
733 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
734 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt);
735 if (rtinfo->rti_genmask != NULL) {
736 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
737 if (rt->rt_genmask == NULL) {
739 * This should not happen, since we
740 * have already installed genmask
741 * on each CPU before we reach here.
743 panic("genmask is gone!?");
746 rtm->rtm_index = rt->rt_ifp->if_index;
752 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo,
753 struct rtentry *rt, void *arg,
754 int found_cnt __unused)
756 struct rt_msghdr *rtm = arg;
758 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
759 rt->rt_rmx.rmx_locks |=
760 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
765 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
767 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
768 setmetric(RTV_RPIPE, rmx_recvpipe);
769 setmetric(RTV_SPIPE, rmx_sendpipe);
770 setmetric(RTV_SSTHRESH, rmx_ssthresh);
771 setmetric(RTV_RTT, rmx_rtt);
772 setmetric(RTV_RTTVAR, rmx_rttvar);
773 setmetric(RTV_HOPCOUNT, rmx_hopcount);
774 setmetric(RTV_MTU, rmx_mtu);
775 setmetric(RTV_EXPIRE, rmx_expire);
776 setmetric(RTV_MSL, rmx_msl);
777 setmetric(RTV_IWMAXSEGS, rmx_iwmaxsegs);
778 setmetric(RTV_IWCAPSEGS, rmx_iwcapsegs);
783 * Extract the addresses of the passed sockaddrs.
784 * Do a little sanity checking so as to avoid bad memory references.
785 * This data is derived straight from userland.
788 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
793 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
794 if ((rtinfo->rti_addrs & (1 << i)) == 0)
796 sa = (struct sockaddr *)cp;
800 if ((cp + sa->sa_len) > cplim) {
805 * There are no more... Quit now.
806 * If there are more bits, they are in error.
807 * I've seen this. route(1) can evidently generate these.
808 * This causes kernel to core dump.
809 * For compatibility, if we see this, point to a safe address.
811 if (sa->sa_len == 0) {
812 static struct sockaddr sa_zero = {
813 sizeof sa_zero, AF_INET,
816 rtinfo->rti_info[i] = &sa_zero;
817 kprintf("rtsock: received more addr bits than sockaddrs.\n");
818 return (0); /* should be EINVAL but for compat */
821 /* Accept the sockaddr. */
822 rtinfo->rti_info[i] = sa;
823 cp += RT_ROUNDUP(sa->sa_len);
829 rt_msghdrsize(int type)
834 return sizeof(struct ifa_msghdr);
837 return sizeof(struct ifma_msghdr);
839 return sizeof(struct if_msghdr);
842 return sizeof(struct if_announcemsghdr);
844 return sizeof(struct rt_msghdr);
849 rt_msgsize(int type, struct rt_addrinfo *rtinfo)
853 len = rt_msghdrsize(type);
854 for (i = 0; i < RTAX_MAX; i++) {
855 if (rtinfo->rti_info[i] != NULL)
856 len += RT_ROUNDUP(rtinfo->rti_info[i]->sa_len);
863 * Build a routing message in a buffer.
864 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
865 * to the end of the buffer after the message header.
867 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
868 * This side-effect can be avoided if we reorder the addrs bitmask field in all
869 * the route messages to line up so we can set it here instead of back in the
873 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
875 struct rt_msghdr *rtm;
879 rtm = (struct rt_msghdr *) buf;
880 rtm->rtm_version = RTM_VERSION;
881 rtm->rtm_type = type;
882 rtm->rtm_msglen = msglen;
884 cp = (char *)buf + rt_msghdrsize(type);
885 rtinfo->rti_addrs = 0;
886 for (i = 0; i < RTAX_MAX; i++) {
889 if ((sa = rtinfo->rti_info[i]) == NULL)
891 rtinfo->rti_addrs |= (1 << i);
892 dlen = RT_ROUNDUP(sa->sa_len);
899 * Build a routing message in a mbuf chain.
900 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
901 * to the end of the mbuf after the message header.
903 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
904 * This side-effect can be avoided if we reorder the addrs bitmask field in all
905 * the route messages to line up so we can set it here instead of back in the
909 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
912 struct rt_msghdr *rtm;
916 hlen = rt_msghdrsize(type);
917 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
919 m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
923 m->m_pkthdr.len = m->m_len = hlen;
924 m->m_pkthdr.rcvif = NULL;
925 rtinfo->rti_addrs = 0;
927 for (i = 0; i < RTAX_MAX; i++) {
931 if ((sa = rtinfo->rti_info[i]) == NULL)
933 rtinfo->rti_addrs |= (1 << i);
934 dlen = RT_ROUNDUP(sa->sa_len);
935 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
938 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
942 rtm = mtod(m, struct rt_msghdr *);
944 rtm->rtm_msglen = len;
945 rtm->rtm_version = RTM_VERSION;
946 rtm->rtm_type = type;
951 * This routine is called to generate a message from the routing
952 * socket indicating that a redirect has occurred, a routing lookup
953 * has failed, or that a protocol has detected timeouts to a particular
957 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
959 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
960 struct rt_msghdr *rtm;
963 if (route_cb.any_count == 0)
965 m = rt_msg_mbuf(type, rtinfo);
968 rtm = mtod(m, struct rt_msghdr *);
969 rtm->rtm_flags = RTF_DONE | flags;
970 rtm->rtm_errno = error;
971 rtm->rtm_addrs = rtinfo->rti_addrs;
972 rts_input(m, familyof(dst));
976 rt_dstmsg(int type, struct sockaddr *dst, int error)
978 struct rt_msghdr *rtm;
979 struct rt_addrinfo addrs;
982 if (route_cb.any_count == 0)
984 bzero(&addrs, sizeof(struct rt_addrinfo));
985 addrs.rti_info[RTAX_DST] = dst;
986 m = rt_msg_mbuf(type, &addrs);
989 rtm = mtod(m, struct rt_msghdr *);
990 rtm->rtm_flags = RTF_DONE;
991 rtm->rtm_errno = error;
992 rtm->rtm_addrs = addrs.rti_addrs;
993 rts_input(m, familyof(dst));
997 * This routine is called to generate a message from the routing
998 * socket indicating that the status of a network interface has changed.
1001 rt_ifmsg(struct ifnet *ifp)
1003 struct if_msghdr *ifm;
1005 struct rt_addrinfo rtinfo;
1007 if (route_cb.any_count == 0)
1009 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1010 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
1013 ifm = mtod(m, struct if_msghdr *);
1014 ifm->ifm_index = ifp->if_index;
1015 ifm->ifm_flags = ifp->if_flags;
1016 ifm->ifm_data = ifp->if_data;
1022 rt_ifamsg(int cmd, struct ifaddr *ifa)
1024 struct ifa_msghdr *ifam;
1025 struct rt_addrinfo rtinfo;
1027 struct ifnet *ifp = ifa->ifa_ifp;
1029 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1030 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1031 rtinfo.rti_ifpaddr =
1032 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1033 rtinfo.rti_netmask = ifa->ifa_netmask;
1034 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1036 m = rt_msg_mbuf(cmd, &rtinfo);
1040 ifam = mtod(m, struct ifa_msghdr *);
1041 ifam->ifam_index = ifp->if_index;
1042 ifam->ifam_metric = ifa->ifa_metric;
1043 ifam->ifam_flags = ifa->ifa_flags;
1044 ifam->ifam_addrs = rtinfo.rti_addrs;
1046 rts_input(m, familyof(ifa->ifa_addr));
1050 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
1052 struct rt_msghdr *rtm;
1053 struct rt_addrinfo rtinfo;
1055 struct sockaddr *dst;
1060 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1061 rtinfo.rti_dst = dst = rt_key(rt);
1062 rtinfo.rti_gateway = rt->rt_gateway;
1063 rtinfo.rti_netmask = rt_mask(rt);
1065 rtinfo.rti_ifpaddr =
1066 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1068 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1070 m = rt_msg_mbuf(cmd, &rtinfo);
1074 rtm = mtod(m, struct rt_msghdr *);
1076 rtm->rtm_index = ifp->if_index;
1077 rtm->rtm_flags |= rt->rt_flags;
1078 rtm->rtm_errno = error;
1079 rtm->rtm_addrs = rtinfo.rti_addrs;
1081 rts_input(m, familyof(dst));
1085 * This is called to generate messages from the routing socket
1086 * indicating a network interface has had addresses associated with it.
1087 * if we ever reverse the logic and replace messages TO the routing
1088 * socket indicate a request to configure interfaces, then it will
1089 * be unnecessary as the routing socket will automatically generate
1093 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1097 * notify the SCTP stack
1098 * this will only get called when an address is added/deleted
1099 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1102 sctp_add_ip_address(ifa);
1103 else if (cmd == RTM_DELETE)
1104 sctp_delete_ip_address(ifa);
1107 if (route_cb.any_count == 0)
1110 if (cmd == RTM_ADD) {
1111 rt_ifamsg(RTM_NEWADDR, ifa);
1112 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
1114 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
1115 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
1116 rt_ifamsg(RTM_DELADDR, ifa);
1121 * This is the analogue to the rt_newaddrmsg which performs the same
1122 * function but for multicast group memberhips. This is easier since
1123 * there is no route state to worry about.
1126 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1128 struct rt_addrinfo rtinfo;
1129 struct mbuf *m = NULL;
1130 struct ifnet *ifp = ifma->ifma_ifp;
1131 struct ifma_msghdr *ifmam;
1133 if (route_cb.any_count == 0)
1136 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1137 rtinfo.rti_ifaaddr = ifma->ifma_addr;
1138 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1139 rtinfo.rti_ifpaddr =
1140 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1143 * If a link-layer address is present, present it as a ``gateway''
1144 * (similarly to how ARP entries, e.g., are presented).
1146 rtinfo.rti_gateway = ifma->ifma_lladdr;
1148 m = rt_msg_mbuf(cmd, &rtinfo);
1152 ifmam = mtod(m, struct ifma_msghdr *);
1153 ifmam->ifmam_index = ifp->if_index;
1154 ifmam->ifmam_addrs = rtinfo.rti_addrs;
1156 rts_input(m, familyof(ifma->ifma_addr));
1159 static struct mbuf *
1160 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1161 struct rt_addrinfo *info)
1163 struct if_announcemsghdr *ifan;
1166 if (route_cb.any_count == 0)
1169 bzero(info, sizeof(*info));
1170 m = rt_msg_mbuf(type, info);
1174 ifan = mtod(m, struct if_announcemsghdr *);
1175 ifan->ifan_index = ifp->if_index;
1176 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1177 ifan->ifan_what = what;
1182 * This is called to generate routing socket messages indicating
1183 * IEEE80211 wireless events.
1184 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1187 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1189 struct rt_addrinfo info;
1192 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1197 * Append the ieee80211 data. Try to stick it in the
1198 * mbuf containing the ifannounce msg; otherwise allocate
1199 * a new mbuf and append.
1201 * NB: we assume m is a single mbuf.
1203 if (data_len > M_TRAILINGSPACE(m)) {
1204 /* XXX use m_getb(data_len, MB_DONTWAIT, MT_DATA, 0); */
1205 struct mbuf *n = m_get(MB_DONTWAIT, MT_DATA);
1210 KKASSERT(data_len <= M_TRAILINGSPACE(n));
1211 bcopy(data, mtod(n, void *), data_len);
1212 n->m_len = data_len;
1214 } else if (data_len > 0) {
1215 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1216 m->m_len += data_len;
1219 if (m->m_flags & M_PKTHDR)
1220 m->m_pkthdr.len += data_len;
1221 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1226 * This is called to generate routing socket messages indicating
1227 * network interface arrival and departure.
1230 rt_ifannouncemsg(struct ifnet *ifp, int what)
1232 struct rt_addrinfo addrinfo;
1235 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo);
1241 resizewalkarg(struct walkarg *w, int len)
1245 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1248 if (w->w_tmem != NULL)
1249 kfree(w->w_tmem, M_RTABLE);
1251 w->w_tmemsize = len;
1256 * This is used in dumping the kernel table via sysctl().
1259 sysctl_dumpentry(struct radix_node *rn, void *vw)
1261 struct walkarg *w = vw;
1262 struct rtentry *rt = (struct rtentry *)rn;
1263 struct rt_addrinfo rtinfo;
1266 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1269 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1270 rtinfo.rti_dst = rt_key(rt);
1271 rtinfo.rti_gateway = rt->rt_gateway;
1272 rtinfo.rti_netmask = rt_mask(rt);
1273 rtinfo.rti_genmask = rt->rt_genmask;
1274 if (rt->rt_ifp != NULL) {
1275 rtinfo.rti_ifpaddr =
1276 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1277 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1278 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1279 rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1281 msglen = rt_msgsize(RTM_GET, &rtinfo);
1282 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1284 rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen);
1285 if (w->w_req != NULL) {
1286 struct rt_msghdr *rtm = w->w_tmem;
1288 rtm->rtm_flags = rt->rt_flags;
1289 rtm->rtm_use = rt->rt_use;
1290 rtm->rtm_rmx = rt->rt_rmx;
1291 rtm->rtm_index = rt->rt_ifp->if_index;
1292 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1293 rtm->rtm_addrs = rtinfo.rti_addrs;
1294 error = SYSCTL_OUT(w->w_req, rtm, msglen);
1301 ifnet_compute_stats(struct ifnet *ifp)
1303 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1304 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1305 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1306 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1307 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1308 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1309 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1310 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1311 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1312 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1316 sysctl_iflist(int af, struct walkarg *w)
1319 struct rt_addrinfo rtinfo;
1322 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1323 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1324 struct ifaddr_container *ifac;
1327 if (w->w_arg && w->w_arg != ifp->if_index)
1329 ifac = TAILQ_FIRST(&ifp->if_addrheads[mycpuid]);
1331 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1332 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1333 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1335 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1336 rtinfo.rti_ifpaddr = NULL;
1337 if (w->w_req != NULL && w->w_tmem != NULL) {
1338 struct if_msghdr *ifm = w->w_tmem;
1340 ifm->ifm_index = ifp->if_index;
1341 ifm->ifm_flags = ifp->if_flags;
1342 ifnet_compute_stats(ifp);
1343 ifm->ifm_data = ifp->if_data;
1344 ifm->ifm_addrs = rtinfo.rti_addrs;
1345 error = SYSCTL_OUT(w->w_req, ifm, msglen);
1349 while ((ifac = TAILQ_NEXT(ifac, ifa_link)) != NULL) {
1352 if (af && af != ifa->ifa_addr->sa_family)
1354 if (curproc->p_ucred->cr_prison &&
1355 prison_if(curproc->p_ucred, ifa->ifa_addr))
1357 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1358 rtinfo.rti_netmask = ifa->ifa_netmask;
1359 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1360 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1361 if (w->w_tmemsize < msglen &&
1362 resizewalkarg(w, msglen) != 0)
1364 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1365 if (w->w_req != NULL) {
1366 struct ifa_msghdr *ifam = w->w_tmem;
1368 ifam->ifam_index = ifa->ifa_ifp->if_index;
1369 ifam->ifam_flags = ifa->ifa_flags;
1370 ifam->ifam_metric = ifa->ifa_metric;
1371 ifam->ifam_addrs = rtinfo.rti_addrs;
1372 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1377 rtinfo.rti_netmask = NULL;
1378 rtinfo.rti_ifaaddr = NULL;
1379 rtinfo.rti_bcastaddr = NULL;
1385 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1387 int *name = (int *)arg1;
1388 u_int namelen = arg2;
1389 struct radix_node_head *rnh;
1390 int i, error = EINVAL;
1399 if (namelen != 3 && namelen != 4)
1402 bzero(&w, sizeof w);
1408 * Optional third argument specifies cpu, used primarily for
1409 * debugging the route table.
1412 if (name[3] < 0 || name[3] >= ncpus)
1415 lwkt_migratecpu(name[3]);
1423 for (i = 1; i <= AF_MAX; i++)
1424 if ((rnh = rt_tables[mycpuid][i]) &&
1425 (af == 0 || af == i) &&
1426 (error = rnh->rnh_walktree(rnh,
1427 sysctl_dumpentry, &w)))
1432 error = sysctl_iflist(af, &w);
1435 if (w.w_tmem != NULL)
1436 kfree(w.w_tmem, M_RTABLE);
1438 lwkt_migratecpu(origcpu);
1442 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1445 * Definitions of protocols supported in the ROUTE domain.
1448 static struct domain routedomain; /* or at least forward */
1450 static struct protosw routesw[] = {
1452 .pr_type = SOCK_RAW,
1453 .pr_domain = &routedomain,
1455 .pr_flags = PR_ATOMIC|PR_ADDR,
1457 .pr_output = route_output,
1458 .pr_ctlinput = raw_ctlinput,
1459 .pr_ctloutput = NULL,
1460 .pr_ctlport = cpu0_ctlport,
1462 .pr_init = raw_init,
1463 .pr_usrreqs = &route_usrreqs
1467 static struct domain routedomain = {
1468 PF_ROUTE, "route", NULL, NULL, NULL,
1469 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])],