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 * 4. 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
{
104 static const struct sockaddr route_src
= { 2, PF_ROUTE
, };
110 struct sysctl_req
*w_req
;
114 rt_msg_mbuf (int, struct rt_addrinfo
*);
115 static void rt_msg_buffer (int, struct rt_addrinfo
*, void *buf
, int len
);
116 static int rt_msgsize (int type
, struct rt_addrinfo
*rtinfo
);
117 static int rt_xaddrs (char *, char *, struct rt_addrinfo
*);
118 static int sysctl_dumpentry (struct radix_node
*rn
, void *vw
);
119 static int sysctl_iflist (int af
, struct walkarg
*w
);
120 static int route_output(struct mbuf
*, struct socket
*, ...);
121 static void rt_setmetrics (u_long
, struct rt_metrics
*,
122 struct rt_metrics
*);
125 * It really doesn't make any sense at all for this code to share much
126 * with raw_usrreq.c, since its functionality is so restricted. XXX
129 rts_abort(netmsg_t msg
)
132 raw_usrreqs
.pru_abort(msg
);
133 /* msg invalid now */
137 /* pru_accept is EOPNOTSUPP */
140 rts_attach(netmsg_t msg
)
142 struct socket
*so
= msg
->base
.nm_so
;
143 struct pru_attach_info
*ai
= msg
->attach
.nm_ai
;
145 int proto
= msg
->attach
.nm_proto
;
149 if (sotorawcb(so
) != NULL
) {
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.
164 soreference(so
); /* so_pcb assignment */
165 error
= raw_attach(so
, proto
, ai
->sb_rlimit
);
171 switch(rp
->rcb_proto
.sp_protocol
) {
176 route_cb
.ip6_count
++;
179 route_cb
.ipx_count
++;
185 rp
->rcb_faddr
= &route_src
;
186 route_cb
.any_count
++;
188 so
->so_options
|= SO_USELOOPBACK
;
192 lwkt_replymsg(&msg
->lmsg
, error
);
196 rts_bind(netmsg_t msg
)
199 raw_usrreqs
.pru_bind(msg
); /* xxx just EINVAL */
200 /* msg invalid now */
205 rts_connect(netmsg_t msg
)
208 raw_usrreqs
.pru_connect(msg
); /* XXX just EINVAL */
209 /* msg invalid now */
213 /* pru_connect2 is EOPNOTSUPP */
214 /* pru_control is EOPNOTSUPP */
217 rts_detach(netmsg_t msg
)
219 struct socket
*so
= msg
->base
.nm_so
;
220 struct rawcb
*rp
= sotorawcb(so
);
224 switch(rp
->rcb_proto
.sp_protocol
) {
229 route_cb
.ip6_count
--;
232 route_cb
.ipx_count
--;
238 route_cb
.any_count
--;
240 raw_usrreqs
.pru_detach(msg
);
241 /* msg invalid now */
246 rts_disconnect(netmsg_t msg
)
249 raw_usrreqs
.pru_disconnect(msg
);
250 /* msg invalid now */
254 /* pru_listen is EOPNOTSUPP */
257 rts_peeraddr(netmsg_t msg
)
260 raw_usrreqs
.pru_peeraddr(msg
);
261 /* msg invalid now */
265 /* pru_rcvd is EOPNOTSUPP */
266 /* pru_rcvoob is EOPNOTSUPP */
269 rts_send(netmsg_t msg
)
272 raw_usrreqs
.pru_send(msg
);
273 /* msg invalid now */
277 /* pru_sense is null */
280 rts_shutdown(netmsg_t msg
)
283 raw_usrreqs
.pru_shutdown(msg
);
284 /* msg invalid now */
289 rts_sockaddr(netmsg_t msg
)
292 raw_usrreqs
.pru_sockaddr(msg
);
293 /* msg invalid now */
297 static struct pr_usrreqs route_usrreqs
= {
298 .pru_abort
= rts_abort
,
299 .pru_accept
= pr_generic_notsupp
,
300 .pru_attach
= rts_attach
,
301 .pru_bind
= rts_bind
,
302 .pru_connect
= rts_connect
,
303 .pru_connect2
= pr_generic_notsupp
,
304 .pru_control
= pr_generic_notsupp
,
305 .pru_detach
= rts_detach
,
306 .pru_disconnect
= rts_disconnect
,
307 .pru_listen
= pr_generic_notsupp
,
308 .pru_peeraddr
= rts_peeraddr
,
309 .pru_rcvd
= pr_generic_notsupp
,
310 .pru_rcvoob
= pr_generic_notsupp
,
311 .pru_send
= rts_send
,
312 .pru_sense
= pru_sense_null
,
313 .pru_shutdown
= rts_shutdown
,
314 .pru_sockaddr
= rts_sockaddr
,
315 .pru_sosend
= sosend
,
316 .pru_soreceive
= soreceive
319 static __inline sa_family_t
320 familyof(struct sockaddr
*sa
)
322 return (sa
!= NULL
? sa
->sa_family
: 0);
326 * Routing socket input function. The packet must be serialized onto cpu 0.
327 * We use the cpu0_soport() netisr processing loop to handle it.
329 * This looks messy but it means that anyone, including interrupt code,
330 * can send a message to the routing socket.
333 rts_input_handler(netmsg_t msg
)
335 static const struct sockaddr route_dst
= { 2, PF_ROUTE
, };
336 struct sockproto route_proto
;
337 struct netmsg_packet
*pmsg
= &msg
->packet
;
342 family
= pmsg
->base
.lmsg
.u
.ms_result
;
343 route_proto
.sp_family
= PF_ROUTE
;
344 route_proto
.sp_protocol
= family
;
349 skip
= m
->m_pkthdr
.header
;
350 m
->m_pkthdr
.header
= NULL
;
352 raw_input(m
, &route_proto
, &route_src
, &route_dst
, skip
);
356 rts_input_skip(struct mbuf
*m
, sa_family_t family
, struct rawcb
*skip
)
358 struct netmsg_packet
*pmsg
;
363 port
= netisr_cpuport(0); /* XXX same as for routing socket */
364 pmsg
= &m
->m_hdr
.mh_netmsg
;
365 netmsg_init(&pmsg
->base
, NULL
, &netisr_apanic_rport
,
366 0, rts_input_handler
);
368 pmsg
->base
.lmsg
.u
.ms_result
= family
;
369 m
->m_pkthdr
.header
= skip
; /* XXX steal field in pkthdr */
370 lwkt_sendmsg(port
, &pmsg
->base
.lmsg
);
374 rts_input(struct mbuf
*m
, sa_family_t family
)
376 rts_input_skip(m
, family
, NULL
);
380 reallocbuf_nofree(void *ptr
, size_t len
, size_t olen
)
384 newptr
= kmalloc(len
, M_RTABLE
, M_INTWAIT
| M_NULLOK
);
387 bcopy(ptr
, newptr
, olen
);
392 * Internal helper routine for route_output().
395 _fillrtmsg(struct rt_msghdr
**prtm
, struct rtentry
*rt
,
396 struct rt_addrinfo
*rtinfo
)
399 struct rt_msghdr
*rtm
= *prtm
;
401 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
402 rtinfo
->rti_dst
= rt_key(rt
);
403 rtinfo
->rti_gateway
= rt
->rt_gateway
;
404 rtinfo
->rti_netmask
= rt_mask(rt
); /* might be NULL */
405 rtinfo
->rti_genmask
= rt
->rt_genmask
; /* might be NULL */
406 if (rtm
->rtm_addrs
& (RTA_IFP
| RTA_IFA
)) {
407 if (rt
->rt_ifp
!= NULL
) {
408 rtinfo
->rti_ifpaddr
=
409 TAILQ_FIRST(&rt
->rt_ifp
->if_addrheads
[mycpuid
])
411 rtinfo
->rti_ifaaddr
= rt
->rt_ifa
->ifa_addr
;
412 if (rt
->rt_ifp
->if_flags
& IFF_POINTOPOINT
)
413 rtinfo
->rti_bcastaddr
= rt
->rt_ifa
->ifa_dstaddr
;
414 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
416 rtinfo
->rti_ifpaddr
= NULL
;
417 rtinfo
->rti_ifaaddr
= NULL
;
419 } else if (rt
->rt_ifp
!= NULL
) {
420 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
423 msglen
= rt_msgsize(rtm
->rtm_type
, rtinfo
);
424 if (rtm
->rtm_msglen
< msglen
) {
425 /* NOTE: Caller will free the old rtm accordingly */
426 rtm
= reallocbuf_nofree(rtm
, msglen
, rtm
->rtm_msglen
);
431 rt_msg_buffer(rtm
->rtm_type
, rtinfo
, rtm
, msglen
);
433 rtm
->rtm_flags
= rt
->rt_flags
;
434 rtm
->rtm_rmx
= rt
->rt_rmx
;
435 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
441 struct rt_msghdr
*bak_rtm
;
442 struct rt_msghdr
*new_rtm
;
446 fillrtmsg(struct rtm_arg
*arg
, struct rtentry
*rt
,
447 struct rt_addrinfo
*rtinfo
)
449 struct rt_msghdr
*rtm
= arg
->new_rtm
;
452 error
= _fillrtmsg(&rtm
, rt
, rtinfo
);
454 if (arg
->new_rtm
!= rtm
) {
456 * _fillrtmsg() just allocated a new rtm;
457 * if the previously allocated rtm is not
458 * the backing rtm, it should be freed.
460 if (arg
->new_rtm
!= arg
->bak_rtm
)
461 kfree(arg
->new_rtm
, M_RTABLE
);
468 static void route_output_add_callback(int, int, struct rt_addrinfo
*,
469 struct rtentry
*, void *);
470 static void route_output_delete_callback(int, int, struct rt_addrinfo
*,
471 struct rtentry
*, void *);
472 static int route_output_get_callback(int, struct rt_addrinfo
*,
473 struct rtentry
*, void *, int);
474 static int route_output_change_callback(int, struct rt_addrinfo
*,
475 struct rtentry
*, void *, int);
476 static int route_output_lock_callback(int, struct rt_addrinfo
*,
477 struct rtentry
*, void *, int);
481 route_output(struct mbuf
*m
, struct socket
*so
, ...)
484 struct rt_msghdr
*rtm
= NULL
;
485 struct rawcb
*rp
= NULL
;
486 struct pr_output_info
*oi
;
487 struct rt_addrinfo rtinfo
;
495 oi
= __va_arg(ap
, struct pr_output_info
*);
498 family
= familyof(NULL
);
500 #define gotoerr(e) { error = e; goto flush;}
503 (m
->m_len
< sizeof(long) &&
504 (m
= m_pullup(m
, sizeof(long))) == NULL
))
506 len
= m
->m_pkthdr
.len
;
507 if (len
< sizeof(struct rt_msghdr
) ||
508 len
!= mtod(m
, struct rt_msghdr
*)->rtm_msglen
)
511 rtm
= kmalloc(len
, M_RTABLE
, M_INTWAIT
| M_NULLOK
);
515 m_copydata(m
, 0, len
, (caddr_t
)rtm
);
516 if (rtm
->rtm_version
!= RTM_VERSION
)
517 gotoerr(EPROTONOSUPPORT
);
519 rtm
->rtm_pid
= oi
->p_pid
;
520 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
521 rtinfo
.rti_addrs
= rtm
->rtm_addrs
;
522 if (rt_xaddrs((char *)(rtm
+ 1), (char *)rtm
+ len
, &rtinfo
) != 0)
525 rtinfo
.rti_flags
= rtm
->rtm_flags
;
526 if (rtinfo
.rti_dst
== NULL
|| rtinfo
.rti_dst
->sa_family
>= AF_MAX
||
527 (rtinfo
.rti_gateway
&& rtinfo
.rti_gateway
->sa_family
>= AF_MAX
))
530 family
= familyof(rtinfo
.rti_dst
);
532 if (rtinfo
.rti_genmask
!= NULL
) {
533 error
= rtmask_add_global(rtinfo
.rti_genmask
);
539 * Verify that the caller has the appropriate privilege; RTM_GET
540 * is the only operation the non-superuser is allowed.
542 if (rtm
->rtm_type
!= RTM_GET
&&
543 priv_check_cred(so
->so_cred
, PRIV_ROOT
, 0) != 0)
546 switch (rtm
->rtm_type
) {
548 if (rtinfo
.rti_gateway
== NULL
) {
551 error
= rtrequest1_global(RTM_ADD
, &rtinfo
,
552 route_output_add_callback
, rtm
);
557 * Backing rtm (bak_rtm) could _not_ be freed during
558 * rtrequest1_global or rtsearch_global, even if the
559 * callback reallocates the rtm due to its size changes,
560 * since rtinfo points to the backing rtm's memory area.
561 * After rtrequest1_global or rtsearch_global returns,
562 * it is safe to free the backing rtm, since rtinfo will
563 * not be used anymore.
565 * new_rtm will be used to save the new rtm allocated
566 * by rtrequest1_global or rtsearch_global.
570 error
= rtrequest1_global(RTM_DELETE
, &rtinfo
,
571 route_output_delete_callback
, &arg
);
573 if (rtm
!= arg
.bak_rtm
)
574 kfree(arg
.bak_rtm
, M_RTABLE
);
577 /* See the comment in RTM_DELETE */
580 error
= rtsearch_global(RTM_GET
, &rtinfo
,
581 route_output_get_callback
, &arg
,
584 if (rtm
!= arg
.bak_rtm
)
585 kfree(arg
.bak_rtm
, M_RTABLE
);
588 error
= rtsearch_global(RTM_CHANGE
, &rtinfo
,
589 route_output_change_callback
, rtm
,
593 error
= rtsearch_global(RTM_LOCK
, &rtinfo
,
594 route_output_lock_callback
, rtm
,
604 rtm
->rtm_errno
= error
;
606 rtm
->rtm_flags
|= RTF_DONE
;
610 * Check to see if we don't want our own messages.
612 if (!(so
->so_options
& SO_USELOOPBACK
)) {
613 if (route_cb
.any_count
<= 1) {
615 kfree(rtm
, M_RTABLE
);
619 /* There is another listener, so construct message */
623 m_copyback(m
, 0, rtm
->rtm_msglen
, (caddr_t
)rtm
);
624 if (m
->m_pkthdr
.len
< rtm
->rtm_msglen
) {
627 } else if (m
->m_pkthdr
.len
> rtm
->rtm_msglen
)
628 m_adj(m
, rtm
->rtm_msglen
- m
->m_pkthdr
.len
);
629 kfree(rtm
, M_RTABLE
);
632 rts_input_skip(m
, family
, rp
);
637 route_output_add_callback(int cmd
, int error
, struct rt_addrinfo
*rtinfo
,
638 struct rtentry
*rt
, void *arg
)
640 struct rt_msghdr
*rtm
= arg
;
642 if (error
== 0 && rt
!= NULL
) {
643 rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
,
645 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
646 rt
->rt_rmx
.rmx_locks
|=
647 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
648 if (rtinfo
->rti_genmask
!= NULL
) {
649 rt
->rt_genmask
= rtmask_purelookup(rtinfo
->rti_genmask
);
650 if (rt
->rt_genmask
== NULL
) {
652 * This should not happen, since we
653 * have already installed genmask
654 * on each CPU before we reach here.
656 panic("genmask is gone!?");
659 rt
->rt_genmask
= NULL
;
661 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
666 route_output_delete_callback(int cmd
, int error
, struct rt_addrinfo
*rtinfo
,
667 struct rtentry
*rt
, void *arg
)
669 if (error
== 0 && rt
) {
671 if (fillrtmsg(arg
, rt
, rtinfo
) != 0) {
673 /* XXX no way to return the error */
677 if (rt
&& rt
->rt_refcnt
== 0) {
684 route_output_get_callback(int cmd
, struct rt_addrinfo
*rtinfo
,
685 struct rtentry
*rt
, void *arg
, int found_cnt
)
687 int error
, found
= 0;
689 if (((rtinfo
->rti_flags
^ rt
->rt_flags
) & RTF_HOST
) == 0)
692 error
= fillrtmsg(arg
, rt
, rtinfo
);
693 if (!error
&& found
) {
694 /* Got the exact match, we could return now! */
701 route_output_change_callback(int cmd
, struct rt_addrinfo
*rtinfo
,
702 struct rtentry
*rt
, void *arg
, int found_cnt
)
704 struct rt_msghdr
*rtm
= arg
;
709 * new gateway could require new ifaddr, ifp;
710 * flags may also be different; ifp may be specified
711 * by ll sockaddr when protocol address is ambiguous
713 if (((rt
->rt_flags
& RTF_GATEWAY
) && rtinfo
->rti_gateway
!= NULL
) ||
714 rtinfo
->rti_ifpaddr
!= NULL
||
715 (rtinfo
->rti_ifaaddr
!= NULL
&&
716 !sa_equal(rtinfo
->rti_ifaaddr
, rt
->rt_ifa
->ifa_addr
))) {
717 error
= rt_getifa(rtinfo
);
721 if (rtinfo
->rti_gateway
!= NULL
) {
723 * We only need to generate rtmsg upon the
724 * first route to be changed.
726 error
= rt_setgate(rt
, rt_key(rt
), rtinfo
->rti_gateway
,
727 found_cnt
== 1 ? RTL_REPORTMSG
: RTL_DONTREPORT
);
731 if ((ifa
= rtinfo
->rti_ifa
) != NULL
) {
732 struct ifaddr
*oifa
= rt
->rt_ifa
;
735 if (oifa
&& oifa
->ifa_rtrequest
)
736 oifa
->ifa_rtrequest(RTM_DELETE
, rt
, rtinfo
);
740 rt
->rt_ifp
= rtinfo
->rti_ifp
;
743 rt_setmetrics(rtm
->rtm_inits
, &rtm
->rtm_rmx
, &rt
->rt_rmx
);
744 if (rt
->rt_ifa
&& rt
->rt_ifa
->ifa_rtrequest
)
745 rt
->rt_ifa
->ifa_rtrequest(RTM_ADD
, rt
, rtinfo
);
746 if (rtinfo
->rti_genmask
!= NULL
) {
747 rt
->rt_genmask
= rtmask_purelookup(rtinfo
->rti_genmask
);
748 if (rt
->rt_genmask
== NULL
) {
750 * This should not happen, since we
751 * have already installed genmask
752 * on each CPU before we reach here.
754 panic("genmask is gone!?");
757 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
763 route_output_lock_callback(int cmd
, struct rt_addrinfo
*rtinfo
,
764 struct rtentry
*rt
, void *arg
,
765 int found_cnt __unused
)
767 struct rt_msghdr
*rtm
= arg
;
769 rt
->rt_rmx
.rmx_locks
&= ~(rtm
->rtm_inits
);
770 rt
->rt_rmx
.rmx_locks
|=
771 (rtm
->rtm_inits
& rtm
->rtm_rmx
.rmx_locks
);
776 rt_setmetrics(u_long which
, struct rt_metrics
*in
, struct rt_metrics
*out
)
778 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
779 setmetric(RTV_RPIPE
, rmx_recvpipe
);
780 setmetric(RTV_SPIPE
, rmx_sendpipe
);
781 setmetric(RTV_SSTHRESH
, rmx_ssthresh
);
782 setmetric(RTV_RTT
, rmx_rtt
);
783 setmetric(RTV_RTTVAR
, rmx_rttvar
);
784 setmetric(RTV_HOPCOUNT
, rmx_hopcount
);
785 setmetric(RTV_MTU
, rmx_mtu
);
786 setmetric(RTV_EXPIRE
, rmx_expire
);
787 setmetric(RTV_MSL
, rmx_msl
);
788 setmetric(RTV_IWMAXSEGS
, rmx_iwmaxsegs
);
789 setmetric(RTV_IWCAPSEGS
, rmx_iwcapsegs
);
794 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
797 * Extract the addresses of the passed sockaddrs.
798 * Do a little sanity checking so as to avoid bad memory references.
799 * This data is derived straight from userland.
802 rt_xaddrs(char *cp
, char *cplim
, struct rt_addrinfo
*rtinfo
)
807 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
808 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
810 sa
= (struct sockaddr
*)cp
;
814 if ((cp
+ sa
->sa_len
) > cplim
) {
819 * There are no more... Quit now.
820 * If there are more bits, they are in error.
821 * I've seen this. route(1) can evidently generate these.
822 * This causes kernel to core dump.
823 * For compatibility, if we see this, point to a safe address.
825 if (sa
->sa_len
== 0) {
826 static struct sockaddr sa_zero
= {
827 sizeof sa_zero
, AF_INET
,
830 rtinfo
->rti_info
[i
] = &sa_zero
;
831 kprintf("rtsock: received more addr bits than sockaddrs.\n");
832 return (0); /* should be EINVAL but for compat */
835 /* Accept the sockaddr. */
836 rtinfo
->rti_info
[i
] = sa
;
837 cp
+= ROUNDUP(sa
->sa_len
);
843 rt_msghdrsize(int type
)
848 return sizeof(struct ifa_msghdr
);
851 return sizeof(struct ifma_msghdr
);
853 return sizeof(struct if_msghdr
);
856 return sizeof(struct if_announcemsghdr
);
858 return sizeof(struct rt_msghdr
);
863 rt_msgsize(int type
, struct rt_addrinfo
*rtinfo
)
867 len
= rt_msghdrsize(type
);
868 for (i
= 0; i
< RTAX_MAX
; i
++) {
869 if (rtinfo
->rti_info
[i
] != NULL
)
870 len
+= ROUNDUP(rtinfo
->rti_info
[i
]->sa_len
);
877 * Build a routing message in a buffer.
878 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
879 * to the end of the buffer after the message header.
881 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
882 * This side-effect can be avoided if we reorder the addrs bitmask field in all
883 * the route messages to line up so we can set it here instead of back in the
887 rt_msg_buffer(int type
, struct rt_addrinfo
*rtinfo
, void *buf
, int msglen
)
889 struct rt_msghdr
*rtm
;
893 rtm
= (struct rt_msghdr
*) buf
;
894 rtm
->rtm_version
= RTM_VERSION
;
895 rtm
->rtm_type
= type
;
896 rtm
->rtm_msglen
= msglen
;
898 cp
= (char *)buf
+ rt_msghdrsize(type
);
899 rtinfo
->rti_addrs
= 0;
900 for (i
= 0; i
< RTAX_MAX
; i
++) {
903 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
905 rtinfo
->rti_addrs
|= (1 << i
);
906 dlen
= ROUNDUP(sa
->sa_len
);
913 * Build a routing message in a mbuf chain.
914 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
915 * to the end of the mbuf after the message header.
917 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
918 * This side-effect can be avoided if we reorder the addrs bitmask field in all
919 * the route messages to line up so we can set it here instead of back in the
923 rt_msg_mbuf(int type
, struct rt_addrinfo
*rtinfo
)
926 struct rt_msghdr
*rtm
;
930 hlen
= rt_msghdrsize(type
);
931 KASSERT(hlen
<= MCLBYTES
, ("rt_msg_mbuf: hlen %d doesn't fit", hlen
));
933 m
= m_getl(hlen
, MB_DONTWAIT
, MT_DATA
, M_PKTHDR
, NULL
);
937 m
->m_pkthdr
.len
= m
->m_len
= hlen
;
938 m
->m_pkthdr
.rcvif
= NULL
;
939 rtinfo
->rti_addrs
= 0;
941 for (i
= 0; i
< RTAX_MAX
; i
++) {
945 if ((sa
= rtinfo
->rti_info
[i
]) == NULL
)
947 rtinfo
->rti_addrs
|= (1 << i
);
948 dlen
= ROUNDUP(sa
->sa_len
);
949 m_copyback(m
, len
, dlen
, (caddr_t
)sa
); /* can grow mbuf chain */
952 if (m
->m_pkthdr
.len
!= len
) { /* one of the m_copyback() calls failed */
956 rtm
= mtod(m
, struct rt_msghdr
*);
958 rtm
->rtm_msglen
= len
;
959 rtm
->rtm_version
= RTM_VERSION
;
960 rtm
->rtm_type
= type
;
965 * This routine is called to generate a message from the routing
966 * socket indicating that a redirect has occurred, a routing lookup
967 * has failed, or that a protocol has detected timeouts to a particular
971 rt_missmsg(int type
, struct rt_addrinfo
*rtinfo
, int flags
, int error
)
973 struct sockaddr
*dst
= rtinfo
->rti_info
[RTAX_DST
];
974 struct rt_msghdr
*rtm
;
977 if (route_cb
.any_count
== 0)
979 m
= rt_msg_mbuf(type
, rtinfo
);
982 rtm
= mtod(m
, struct rt_msghdr
*);
983 rtm
->rtm_flags
= RTF_DONE
| flags
;
984 rtm
->rtm_errno
= error
;
985 rtm
->rtm_addrs
= rtinfo
->rti_addrs
;
986 rts_input(m
, familyof(dst
));
990 rt_dstmsg(int type
, struct sockaddr
*dst
, int error
)
992 struct rt_msghdr
*rtm
;
993 struct rt_addrinfo addrs
;
996 if (route_cb
.any_count
== 0)
998 bzero(&addrs
, sizeof(struct rt_addrinfo
));
999 addrs
.rti_info
[RTAX_DST
] = dst
;
1000 m
= rt_msg_mbuf(type
, &addrs
);
1003 rtm
= mtod(m
, struct rt_msghdr
*);
1004 rtm
->rtm_flags
= RTF_DONE
;
1005 rtm
->rtm_errno
= error
;
1006 rtm
->rtm_addrs
= addrs
.rti_addrs
;
1007 rts_input(m
, familyof(dst
));
1011 * This routine is called to generate a message from the routing
1012 * socket indicating that the status of a network interface has changed.
1015 rt_ifmsg(struct ifnet
*ifp
)
1017 struct if_msghdr
*ifm
;
1019 struct rt_addrinfo rtinfo
;
1021 if (route_cb
.any_count
== 0)
1023 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1024 m
= rt_msg_mbuf(RTM_IFINFO
, &rtinfo
);
1027 ifm
= mtod(m
, struct if_msghdr
*);
1028 ifm
->ifm_index
= ifp
->if_index
;
1029 ifm
->ifm_flags
= ifp
->if_flags
;
1030 ifm
->ifm_data
= ifp
->if_data
;
1036 rt_ifamsg(int cmd
, struct ifaddr
*ifa
)
1038 struct ifa_msghdr
*ifam
;
1039 struct rt_addrinfo rtinfo
;
1041 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1043 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1044 rtinfo
.rti_ifaaddr
= ifa
->ifa_addr
;
1045 rtinfo
.rti_ifpaddr
=
1046 TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
->ifa_addr
;
1047 rtinfo
.rti_netmask
= ifa
->ifa_netmask
;
1048 rtinfo
.rti_bcastaddr
= ifa
->ifa_dstaddr
;
1050 m
= rt_msg_mbuf(cmd
, &rtinfo
);
1054 ifam
= mtod(m
, struct ifa_msghdr
*);
1055 ifam
->ifam_index
= ifp
->if_index
;
1056 ifam
->ifam_metric
= ifa
->ifa_metric
;
1057 ifam
->ifam_flags
= ifa
->ifa_flags
;
1058 ifam
->ifam_addrs
= rtinfo
.rti_addrs
;
1060 rts_input(m
, familyof(ifa
->ifa_addr
));
1064 rt_rtmsg(int cmd
, struct rtentry
*rt
, struct ifnet
*ifp
, int error
)
1066 struct rt_msghdr
*rtm
;
1067 struct rt_addrinfo rtinfo
;
1069 struct sockaddr
*dst
;
1074 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1075 rtinfo
.rti_dst
= dst
= rt_key(rt
);
1076 rtinfo
.rti_gateway
= rt
->rt_gateway
;
1077 rtinfo
.rti_netmask
= rt_mask(rt
);
1079 rtinfo
.rti_ifpaddr
=
1080 TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
->ifa_addr
;
1082 rtinfo
.rti_ifaaddr
= rt
->rt_ifa
->ifa_addr
;
1084 m
= rt_msg_mbuf(cmd
, &rtinfo
);
1088 rtm
= mtod(m
, struct rt_msghdr
*);
1090 rtm
->rtm_index
= ifp
->if_index
;
1091 rtm
->rtm_flags
|= rt
->rt_flags
;
1092 rtm
->rtm_errno
= error
;
1093 rtm
->rtm_addrs
= rtinfo
.rti_addrs
;
1095 rts_input(m
, familyof(dst
));
1099 * This is called to generate messages from the routing socket
1100 * indicating a network interface has had addresses associated with it.
1101 * if we ever reverse the logic and replace messages TO the routing
1102 * socket indicate a request to configure interfaces, then it will
1103 * be unnecessary as the routing socket will automatically generate
1107 rt_newaddrmsg(int cmd
, struct ifaddr
*ifa
, int error
, struct rtentry
*rt
)
1111 * notify the SCTP stack
1112 * this will only get called when an address is added/deleted
1113 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1116 sctp_add_ip_address(ifa
);
1117 else if (cmd
== RTM_DELETE
)
1118 sctp_delete_ip_address(ifa
);
1121 if (route_cb
.any_count
== 0)
1124 if (cmd
== RTM_ADD
) {
1125 rt_ifamsg(RTM_NEWADDR
, ifa
);
1126 rt_rtmsg(RTM_ADD
, rt
, ifa
->ifa_ifp
, error
);
1128 KASSERT((cmd
== RTM_DELETE
), ("unknown cmd %d", cmd
));
1129 rt_rtmsg(RTM_DELETE
, rt
, ifa
->ifa_ifp
, error
);
1130 rt_ifamsg(RTM_DELADDR
, ifa
);
1135 * This is the analogue to the rt_newaddrmsg which performs the same
1136 * function but for multicast group memberhips. This is easier since
1137 * there is no route state to worry about.
1140 rt_newmaddrmsg(int cmd
, struct ifmultiaddr
*ifma
)
1142 struct rt_addrinfo rtinfo
;
1143 struct mbuf
*m
= NULL
;
1144 struct ifnet
*ifp
= ifma
->ifma_ifp
;
1145 struct ifma_msghdr
*ifmam
;
1147 if (route_cb
.any_count
== 0)
1150 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1151 rtinfo
.rti_ifaaddr
= ifma
->ifma_addr
;
1152 if (ifp
!= NULL
&& !TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
])) {
1153 rtinfo
.rti_ifpaddr
=
1154 TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
->ifa_addr
;
1157 * If a link-layer address is present, present it as a ``gateway''
1158 * (similarly to how ARP entries, e.g., are presented).
1160 rtinfo
.rti_gateway
= ifma
->ifma_lladdr
;
1162 m
= rt_msg_mbuf(cmd
, &rtinfo
);
1166 ifmam
= mtod(m
, struct ifma_msghdr
*);
1167 ifmam
->ifmam_index
= ifp
->if_index
;
1168 ifmam
->ifmam_addrs
= rtinfo
.rti_addrs
;
1170 rts_input(m
, familyof(ifma
->ifma_addr
));
1173 static struct mbuf
*
1174 rt_makeifannouncemsg(struct ifnet
*ifp
, int type
, int what
,
1175 struct rt_addrinfo
*info
)
1177 struct if_announcemsghdr
*ifan
;
1180 if (route_cb
.any_count
== 0)
1183 bzero(info
, sizeof(*info
));
1184 m
= rt_msg_mbuf(type
, info
);
1188 ifan
= mtod(m
, struct if_announcemsghdr
*);
1189 ifan
->ifan_index
= ifp
->if_index
;
1190 strlcpy(ifan
->ifan_name
, ifp
->if_xname
, sizeof ifan
->ifan_name
);
1191 ifan
->ifan_what
= what
;
1196 * This is called to generate routing socket messages indicating
1197 * IEEE80211 wireless events.
1198 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1201 rt_ieee80211msg(struct ifnet
*ifp
, int what
, void *data
, size_t data_len
)
1203 struct rt_addrinfo info
;
1206 m
= rt_makeifannouncemsg(ifp
, RTM_IEEE80211
, what
, &info
);
1211 * Append the ieee80211 data. Try to stick it in the
1212 * mbuf containing the ifannounce msg; otherwise allocate
1213 * a new mbuf and append.
1215 * NB: we assume m is a single mbuf.
1217 if (data_len
> M_TRAILINGSPACE(m
)) {
1218 /* XXX use m_getb(data_len, MB_DONTWAIT, MT_DATA, 0); */
1219 struct mbuf
*n
= m_get(MB_DONTWAIT
, MT_DATA
);
1224 KKASSERT(data_len
<= M_TRAILINGSPACE(n
));
1225 bcopy(data
, mtod(n
, void *), data_len
);
1226 n
->m_len
= data_len
;
1228 } else if (data_len
> 0) {
1229 bcopy(data
, mtod(m
, u_int8_t
*) + m
->m_len
, data_len
);
1230 m
->m_len
+= data_len
;
1233 if (m
->m_flags
& M_PKTHDR
)
1234 m
->m_pkthdr
.len
+= data_len
;
1235 mtod(m
, struct if_announcemsghdr
*)->ifan_msglen
+= data_len
;
1240 * This is called to generate routing socket messages indicating
1241 * network interface arrival and departure.
1244 rt_ifannouncemsg(struct ifnet
*ifp
, int what
)
1246 struct rt_addrinfo addrinfo
;
1249 m
= rt_makeifannouncemsg(ifp
, RTM_IFANNOUNCE
, what
, &addrinfo
);
1255 resizewalkarg(struct walkarg
*w
, int len
)
1259 newptr
= kmalloc(len
, M_RTABLE
, M_INTWAIT
| M_NULLOK
);
1262 if (w
->w_tmem
!= NULL
)
1263 kfree(w
->w_tmem
, M_RTABLE
);
1265 w
->w_tmemsize
= len
;
1270 * This is used in dumping the kernel table via sysctl().
1273 sysctl_dumpentry(struct radix_node
*rn
, void *vw
)
1275 struct walkarg
*w
= vw
;
1276 struct rtentry
*rt
= (struct rtentry
*)rn
;
1277 struct rt_addrinfo rtinfo
;
1280 if (w
->w_op
== NET_RT_FLAGS
&& !(rt
->rt_flags
& w
->w_arg
))
1283 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1284 rtinfo
.rti_dst
= rt_key(rt
);
1285 rtinfo
.rti_gateway
= rt
->rt_gateway
;
1286 rtinfo
.rti_netmask
= rt_mask(rt
);
1287 rtinfo
.rti_genmask
= rt
->rt_genmask
;
1288 if (rt
->rt_ifp
!= NULL
) {
1289 rtinfo
.rti_ifpaddr
=
1290 TAILQ_FIRST(&rt
->rt_ifp
->if_addrheads
[mycpuid
])->ifa
->ifa_addr
;
1291 rtinfo
.rti_ifaaddr
= rt
->rt_ifa
->ifa_addr
;
1292 if (rt
->rt_ifp
->if_flags
& IFF_POINTOPOINT
)
1293 rtinfo
.rti_bcastaddr
= rt
->rt_ifa
->ifa_dstaddr
;
1295 msglen
= rt_msgsize(RTM_GET
, &rtinfo
);
1296 if (w
->w_tmemsize
< msglen
&& resizewalkarg(w
, msglen
) != 0)
1298 rt_msg_buffer(RTM_GET
, &rtinfo
, w
->w_tmem
, msglen
);
1299 if (w
->w_req
!= NULL
) {
1300 struct rt_msghdr
*rtm
= w
->w_tmem
;
1302 rtm
->rtm_flags
= rt
->rt_flags
;
1303 rtm
->rtm_use
= rt
->rt_use
;
1304 rtm
->rtm_rmx
= rt
->rt_rmx
;
1305 rtm
->rtm_index
= rt
->rt_ifp
->if_index
;
1306 rtm
->rtm_errno
= rtm
->rtm_pid
= rtm
->rtm_seq
= 0;
1307 rtm
->rtm_addrs
= rtinfo
.rti_addrs
;
1308 error
= SYSCTL_OUT(w
->w_req
, rtm
, msglen
);
1315 sysctl_iflist(int af
, struct walkarg
*w
)
1318 struct rt_addrinfo rtinfo
;
1321 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
1322 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
1323 struct ifaddr_container
*ifac
;
1326 if (w
->w_arg
&& w
->w_arg
!= ifp
->if_index
)
1328 ifac
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
]);
1330 rtinfo
.rti_ifpaddr
= ifa
->ifa_addr
;
1331 msglen
= rt_msgsize(RTM_IFINFO
, &rtinfo
);
1332 if (w
->w_tmemsize
< msglen
&& resizewalkarg(w
, msglen
) != 0)
1334 rt_msg_buffer(RTM_IFINFO
, &rtinfo
, w
->w_tmem
, msglen
);
1335 rtinfo
.rti_ifpaddr
= NULL
;
1336 if (w
->w_req
!= NULL
&& w
->w_tmem
!= NULL
) {
1337 struct if_msghdr
*ifm
= w
->w_tmem
;
1339 ifm
->ifm_index
= ifp
->if_index
;
1340 ifm
->ifm_flags
= ifp
->if_flags
;
1341 ifm
->ifm_data
= ifp
->if_data
;
1342 ifm
->ifm_addrs
= rtinfo
.rti_addrs
;
1343 error
= SYSCTL_OUT(w
->w_req
, ifm
, msglen
);
1347 while ((ifac
= TAILQ_NEXT(ifac
, ifa_link
)) != NULL
) {
1350 if (af
&& af
!= ifa
->ifa_addr
->sa_family
)
1352 if (curproc
->p_ucred
->cr_prison
&&
1353 prison_if(curproc
->p_ucred
, ifa
->ifa_addr
))
1355 rtinfo
.rti_ifaaddr
= ifa
->ifa_addr
;
1356 rtinfo
.rti_netmask
= ifa
->ifa_netmask
;
1357 rtinfo
.rti_bcastaddr
= ifa
->ifa_dstaddr
;
1358 msglen
= rt_msgsize(RTM_NEWADDR
, &rtinfo
);
1359 if (w
->w_tmemsize
< msglen
&&
1360 resizewalkarg(w
, msglen
) != 0)
1362 rt_msg_buffer(RTM_NEWADDR
, &rtinfo
, w
->w_tmem
, msglen
);
1363 if (w
->w_req
!= NULL
) {
1364 struct ifa_msghdr
*ifam
= w
->w_tmem
;
1366 ifam
->ifam_index
= ifa
->ifa_ifp
->if_index
;
1367 ifam
->ifam_flags
= ifa
->ifa_flags
;
1368 ifam
->ifam_metric
= ifa
->ifa_metric
;
1369 ifam
->ifam_addrs
= rtinfo
.rti_addrs
;
1370 error
= SYSCTL_OUT(w
->w_req
, w
->w_tmem
, msglen
);
1375 rtinfo
.rti_netmask
= NULL
;
1376 rtinfo
.rti_ifaaddr
= NULL
;
1377 rtinfo
.rti_bcastaddr
= NULL
;
1383 sysctl_rtsock(SYSCTL_HANDLER_ARGS
)
1385 int *name
= (int *)arg1
;
1386 u_int namelen
= arg2
;
1387 struct radix_node_head
*rnh
;
1388 int i
, error
= EINVAL
;
1397 if (namelen
!= 3 && namelen
!= 4)
1400 bzero(&w
, sizeof w
);
1406 * Optional third argument specifies cpu, used primarily for
1407 * debugging the route table.
1410 if (name
[3] < 0 || name
[3] >= ncpus
)
1413 lwkt_migratecpu(name
[3]);
1421 for (i
= 1; i
<= AF_MAX
; i
++)
1422 if ((rnh
= rt_tables
[mycpuid
][i
]) &&
1423 (af
== 0 || af
== i
) &&
1424 (error
= rnh
->rnh_walktree(rnh
,
1425 sysctl_dumpentry
, &w
)))
1430 error
= sysctl_iflist(af
, &w
);
1433 if (w
.w_tmem
!= NULL
)
1434 kfree(w
.w_tmem
, M_RTABLE
);
1436 lwkt_migratecpu(origcpu
);
1440 SYSCTL_NODE(_net
, PF_ROUTE
, routetable
, CTLFLAG_RD
, sysctl_rtsock
, "");
1443 * Definitions of protocols supported in the ROUTE domain.
1446 static struct domain routedomain
; /* or at least forward */
1448 static struct protosw routesw
[] = {
1450 .pr_type
= SOCK_RAW
,
1451 .pr_domain
= &routedomain
,
1453 .pr_flags
= PR_ATOMIC
|PR_ADDR
,
1455 .pr_output
= route_output
,
1456 .pr_ctlinput
= raw_ctlinput
,
1457 .pr_ctloutput
= NULL
,
1458 .pr_ctlport
= cpu0_ctlport
,
1460 .pr_init
= raw_init
,
1461 .pr_usrreqs
= &route_usrreqs
1465 static struct domain routedomain
= {
1466 PF_ROUTE
, "route", NULL
, NULL
, NULL
,
1467 routesw
, &routesw
[(sizeof routesw
)/(sizeof routesw
[0])],