1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2001-10-17 11:23:59 +00:00
|
|
|
* @(#)rtsock.c 8.7 (Berkeley) 10/12/95
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/domain.h>
|
1995-05-11 00:13:26 +00:00
|
|
|
#include <sys/kernel.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/jail.h>
|
1997-09-02 01:19:47 +00:00
|
|
|
#include <sys/malloc.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/mbuf.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/signalvar.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/systm.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
#include <net/netisr.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <net/raw_cb.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <net/route.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2004-04-26 19:46:52 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
1997-10-12 20:26:33 +00:00
|
|
|
MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
|
|
|
|
|
2003-10-03 18:15:54 +00:00
|
|
|
/* NB: these are not modified */
|
1995-11-16 19:00:27 +00:00
|
|
|
static struct sockaddr route_dst = { 2, PF_ROUTE, };
|
|
|
|
static struct sockaddr route_src = { 2, PF_ROUTE, };
|
1997-07-18 11:44:24 +00:00
|
|
|
static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
|
2003-10-03 18:15:54 +00:00
|
|
|
|
|
|
|
static struct {
|
2004-08-24 08:47:15 +00:00
|
|
|
int ip_count; /* attached w/ AF_INET */
|
2003-10-03 18:15:54 +00:00
|
|
|
int ip6_count; /* attached w/ AF_INET6 */
|
|
|
|
int ipx_count; /* attached w/ AF_IPX */
|
|
|
|
int any_count; /* total attached */
|
|
|
|
} route_cb;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2003-10-03 18:27:02 +00:00
|
|
|
struct mtx rtsock_mtx;
|
|
|
|
MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
|
|
|
|
|
|
|
|
#define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
|
|
|
|
#define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
|
|
|
|
#define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
|
|
|
|
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
static struct ifqueue rtsintrq;
|
|
|
|
|
2004-08-21 21:20:06 +00:00
|
|
|
SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
|
|
|
|
SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
|
|
|
|
&rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
struct walkarg {
|
1995-11-16 19:00:27 +00:00
|
|
|
int w_tmemsize;
|
|
|
|
int w_op, w_arg;
|
|
|
|
caddr_t w_tmem;
|
|
|
|
struct sysctl_req *w_req;
|
1994-05-24 10:09:53 +00:00
|
|
|
};
|
|
|
|
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
static void rts_input(struct mbuf *m);
|
2004-04-18 00:56:44 +00:00
|
|
|
static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
|
|
|
|
static int rt_msg2(int type, struct rt_addrinfo *rtinfo,
|
|
|
|
caddr_t cp, struct walkarg *w);
|
|
|
|
static int rt_xaddrs(caddr_t cp, caddr_t cplim,
|
|
|
|
struct rt_addrinfo *rtinfo);
|
2002-03-19 21:54:18 +00:00
|
|
|
static int sysctl_dumpentry(struct radix_node *rn, void *vw);
|
|
|
|
static int sysctl_iflist(int af, struct walkarg *w);
|
2003-11-14 18:48:15 +00:00
|
|
|
static int sysctl_ifmalist(int af, struct walkarg *w);
|
2004-04-18 00:56:44 +00:00
|
|
|
static int route_output(struct mbuf *m, struct socket *so);
|
|
|
|
static void rt_setmetrics(u_long which, const struct rt_metrics *in,
|
|
|
|
struct rt_metrics_lite *out);
|
|
|
|
static void rt_getmetrics(const struct rt_metrics_lite *in,
|
|
|
|
struct rt_metrics *out);
|
|
|
|
static void rt_dispatch(struct mbuf *, const struct sockaddr *);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
static void
|
|
|
|
rts_init(void)
|
|
|
|
{
|
2004-08-21 21:45:40 +00:00
|
|
|
int tmp;
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
|
2004-08-21 21:20:06 +00:00
|
|
|
rtsintrq.ifq_maxlen = 256;
|
2004-08-21 21:45:40 +00:00
|
|
|
if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
|
|
|
|
rtsintrq.ifq_maxlen = tmp;
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
|
|
|
|
netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE);
|
|
|
|
}
|
|
|
|
SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0)
|
|
|
|
|
|
|
|
static void
|
|
|
|
rts_input(struct mbuf *m)
|
|
|
|
{
|
|
|
|
struct sockproto route_proto;
|
|
|
|
unsigned short *family;
|
|
|
|
struct m_tag *tag;
|
|
|
|
|
|
|
|
route_proto.sp_family = PF_ROUTE;
|
|
|
|
tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
|
|
|
|
if (tag != NULL) {
|
|
|
|
family = (unsigned short *)(tag + 1);
|
|
|
|
route_proto.sp_protocol = *family;
|
|
|
|
m_tag_delete(m, tag);
|
|
|
|
} else
|
|
|
|
route_proto.sp_protocol = 0;
|
|
|
|
|
|
|
|
raw_input(m, &route_proto, &route_src, &route_dst);
|
|
|
|
}
|
|
|
|
|
1997-04-27 20:01:29 +00:00
|
|
|
/*
|
|
|
|
* It really doesn't make any sense at all for this code to share much
|
|
|
|
* with raw_usrreq.c, since its functionality is so restricted. XXX
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rts_abort(struct socket *so)
|
|
|
|
{
|
|
|
|
int s, error;
|
|
|
|
s = splnet();
|
|
|
|
error = raw_usrreqs.pru_abort(so);
|
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pru_accept is EOPNOTSUPP */
|
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
rts_attach(struct socket *so, int proto, struct thread *td)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1997-04-27 20:01:29 +00:00
|
|
|
struct rawcb *rp;
|
|
|
|
int s, error;
|
|
|
|
|
2004-04-18 00:56:44 +00:00
|
|
|
if (sotorawcb(so) != NULL)
|
1997-04-27 20:01:29 +00:00
|
|
|
return EISCONN; /* XXX panic? */
|
2000-12-08 21:51:06 +00:00
|
|
|
/* XXX */
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
|
2004-04-18 00:56:44 +00:00
|
|
|
if (rp == NULL)
|
1997-04-27 20:01:29 +00:00
|
|
|
return ENOBUFS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The splnet() is necessary to block protocols from sending
|
|
|
|
* error notifications (like RTM_REDIRECT or RTM_LOSING) while
|
|
|
|
* this PCB is extant but incompletely initialized.
|
|
|
|
* Probably we should try to do more of this work beforehand and
|
|
|
|
* eliminate the spl.
|
|
|
|
*/
|
|
|
|
s = splnet();
|
|
|
|
so->so_pcb = (caddr_t)rp;
|
2001-08-31 12:31:09 +00:00
|
|
|
error = raw_attach(so, proto);
|
1997-04-27 20:01:29 +00:00
|
|
|
rp = sotorawcb(so);
|
|
|
|
if (error) {
|
|
|
|
splx(s);
|
2001-08-02 19:56:29 +00:00
|
|
|
so->so_pcb = NULL;
|
1997-04-27 20:01:29 +00:00
|
|
|
free(rp, M_PCB);
|
|
|
|
return error;
|
|
|
|
}
|
2003-10-03 18:27:02 +00:00
|
|
|
RTSOCK_LOCK();
|
1997-04-27 20:01:29 +00:00
|
|
|
switch(rp->rcb_proto.sp_protocol) {
|
|
|
|
case AF_INET:
|
|
|
|
route_cb.ip_count++;
|
|
|
|
break;
|
2000-01-28 20:10:51 +00:00
|
|
|
case AF_INET6:
|
|
|
|
route_cb.ip6_count++;
|
|
|
|
break;
|
1997-04-27 20:01:29 +00:00
|
|
|
case AF_IPX:
|
|
|
|
route_cb.ipx_count++;
|
|
|
|
break;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1997-04-27 20:01:29 +00:00
|
|
|
rp->rcb_faddr = &route_src;
|
|
|
|
route_cb.any_count++;
|
2003-10-03 18:27:02 +00:00
|
|
|
RTSOCK_UNLOCK();
|
2002-06-18 07:42:02 +00:00
|
|
|
soisconnected(so);
|
1997-04-27 20:01:29 +00:00
|
|
|
so->so_options |= SO_USELOOPBACK;
|
|
|
|
splx(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
|
1997-04-27 20:01:29 +00:00
|
|
|
{
|
|
|
|
int s, error;
|
|
|
|
s = splnet();
|
2001-09-12 08:38:13 +00:00
|
|
|
error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
|
1997-04-27 20:01:29 +00:00
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
|
1997-04-27 20:01:29 +00:00
|
|
|
{
|
|
|
|
int s, error;
|
|
|
|
s = splnet();
|
2001-09-12 08:38:13 +00:00
|
|
|
error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
|
1997-04-27 20:01:29 +00:00
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pru_connect2 is EOPNOTSUPP */
|
|
|
|
/* pru_control is EOPNOTSUPP */
|
|
|
|
|
|
|
|
static int
|
|
|
|
rts_detach(struct socket *so)
|
|
|
|
{
|
|
|
|
struct rawcb *rp = sotorawcb(so);
|
|
|
|
int s, error;
|
|
|
|
|
|
|
|
s = splnet();
|
2004-04-18 00:56:44 +00:00
|
|
|
if (rp != NULL) {
|
2003-10-03 18:27:02 +00:00
|
|
|
RTSOCK_LOCK();
|
1997-04-27 20:01:29 +00:00
|
|
|
switch(rp->rcb_proto.sp_protocol) {
|
|
|
|
case AF_INET:
|
1994-05-24 10:09:53 +00:00
|
|
|
route_cb.ip_count--;
|
1997-04-27 20:01:29 +00:00
|
|
|
break;
|
2000-01-28 20:10:51 +00:00
|
|
|
case AF_INET6:
|
|
|
|
route_cb.ip6_count--;
|
|
|
|
break;
|
1997-04-27 20:01:29 +00:00
|
|
|
case AF_IPX:
|
1995-10-26 20:31:59 +00:00
|
|
|
route_cb.ipx_count--;
|
1997-04-27 20:01:29 +00:00
|
|
|
break;
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
route_cb.any_count--;
|
2003-10-03 18:27:02 +00:00
|
|
|
RTSOCK_UNLOCK();
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1997-04-27 20:01:29 +00:00
|
|
|
error = raw_usrreqs.pru_detach(so);
|
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rts_disconnect(struct socket *so)
|
|
|
|
{
|
|
|
|
int s, error;
|
1994-05-24 10:09:53 +00:00
|
|
|
s = splnet();
|
1997-04-27 20:01:29 +00:00
|
|
|
error = raw_usrreqs.pru_disconnect(so);
|
1994-05-24 10:09:53 +00:00
|
|
|
splx(s);
|
1997-04-27 20:01:29 +00:00
|
|
|
return error;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1997-04-27 20:01:29 +00:00
|
|
|
/* pru_listen is EOPNOTSUPP */
|
|
|
|
|
|
|
|
static int
|
1997-08-16 19:16:27 +00:00
|
|
|
rts_peeraddr(struct socket *so, struct sockaddr **nam)
|
1997-04-27 20:01:29 +00:00
|
|
|
{
|
|
|
|
int s, error;
|
|
|
|
s = splnet();
|
|
|
|
error = raw_usrreqs.pru_peeraddr(so, nam);
|
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pru_rcvd is EOPNOTSUPP */
|
|
|
|
/* pru_rcvoob is EOPNOTSUPP */
|
|
|
|
|
|
|
|
static int
|
1997-08-16 19:16:27 +00:00
|
|
|
rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
|
2001-09-12 08:38:13 +00:00
|
|
|
struct mbuf *control, struct thread *td)
|
1997-04-27 20:01:29 +00:00
|
|
|
{
|
|
|
|
int s, error;
|
|
|
|
s = splnet();
|
2001-09-12 08:38:13 +00:00
|
|
|
error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
|
1997-04-27 20:01:29 +00:00
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pru_sense is null */
|
|
|
|
|
|
|
|
static int
|
|
|
|
rts_shutdown(struct socket *so)
|
|
|
|
{
|
|
|
|
int s, error;
|
|
|
|
s = splnet();
|
|
|
|
error = raw_usrreqs.pru_shutdown(so);
|
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1997-08-16 19:16:27 +00:00
|
|
|
rts_sockaddr(struct socket *so, struct sockaddr **nam)
|
1997-04-27 20:01:29 +00:00
|
|
|
{
|
|
|
|
int s, error;
|
|
|
|
s = splnet();
|
|
|
|
error = raw_usrreqs.pru_sockaddr(so, nam);
|
|
|
|
splx(s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pr_usrreqs route_usrreqs = {
|
|
|
|
rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
|
|
|
|
pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
|
|
|
|
pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
|
|
|
|
rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
|
Introduce a MAC label reference in 'struct inpcb', which caches
the MAC label referenced from 'struct socket' in the IPv4 and
IPv6-based protocols. This permits MAC labels to be checked during
network delivery operations without dereferencing inp->inp_socket
to get to so->so_label, which will eventually avoid our having to
grab the socket lock during delivery at the network layer.
This change introduces 'struct inpcb' as a labeled object to the
MAC Framework, along with the normal circus of entry points:
initialization, creation from socket, destruction, as well as a
delivery access control check.
For most policies, the inpcb label will simply be a cache of the
socket label, so a new protocol switch method is introduced,
pr_sosetlabel() to notify protocols that the socket layer label
has been updated so that the cache can be updated while holding
appropriate locks. Most protocols implement this using
pru_sosetlabel_null(), but IPv4/IPv6 protocols using inpcbs use
the the worker function in_pcbsosetlabel(), which calls into the
MAC Framework to perform a cache update.
Biba, LOMAC, and MLS implement these entry points, as do the stub
policy, and test policy.
Reviewed by: sam, bms
Obtained from: TrustedBSD Project
Sponsored by: DARPA, Network Associates Laboratories
2003-11-18 00:39:07 +00:00
|
|
|
sosend, soreceive, sopoll, pru_sosetlabel_null
|
1997-04-27 20:01:29 +00:00
|
|
|
};
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*ARGSUSED*/
|
1995-11-16 19:00:27 +00:00
|
|
|
static int
|
2004-04-18 00:56:44 +00:00
|
|
|
route_output(struct mbuf *m, struct socket *so)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2003-10-03 18:15:54 +00:00
|
|
|
#define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
|
2004-04-18 00:56:44 +00:00
|
|
|
struct rt_msghdr *rtm = NULL;
|
|
|
|
struct rtentry *rt = NULL;
|
1995-05-10 16:48:52 +00:00
|
|
|
struct radix_node_head *rnh;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct rt_addrinfo info;
|
|
|
|
int len, error = 0;
|
2004-04-18 00:56:44 +00:00
|
|
|
struct ifnet *ifp = NULL;
|
|
|
|
struct ifaddr *ifa = NULL;
|
2004-04-26 19:46:52 +00:00
|
|
|
struct sockaddr_in jail;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#define senderr(e) { error = e; goto flush;}
|
2004-04-18 00:56:44 +00:00
|
|
|
if (m == NULL || ((m->m_len < sizeof(long)) &&
|
|
|
|
(m = m_pullup(m, sizeof(long))) == NULL))
|
1994-05-24 10:09:53 +00:00
|
|
|
return (ENOBUFS);
|
|
|
|
if ((m->m_flags & M_PKTHDR) == 0)
|
|
|
|
panic("route_output");
|
|
|
|
len = m->m_pkthdr.len;
|
|
|
|
if (len < sizeof(*rtm) ||
|
|
|
|
len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
|
2004-04-18 00:56:44 +00:00
|
|
|
info.rti_info[RTAX_DST] = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
senderr(EINVAL);
|
|
|
|
}
|
|
|
|
R_Malloc(rtm, struct rt_msghdr *, len);
|
2004-04-18 00:56:44 +00:00
|
|
|
if (rtm == NULL) {
|
|
|
|
info.rti_info[RTAX_DST] = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
senderr(ENOBUFS);
|
|
|
|
}
|
|
|
|
m_copydata(m, 0, len, (caddr_t)rtm);
|
|
|
|
if (rtm->rtm_version != RTM_VERSION) {
|
2004-04-18 00:56:44 +00:00
|
|
|
info.rti_info[RTAX_DST] = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
senderr(EPROTONOSUPPORT);
|
|
|
|
}
|
|
|
|
rtm->rtm_pid = curproc->p_pid;
|
2001-10-17 18:07:05 +00:00
|
|
|
bzero(&info, sizeof(info));
|
1994-05-24 10:09:53 +00:00
|
|
|
info.rti_addrs = rtm->rtm_addrs;
|
1997-07-18 11:44:24 +00:00
|
|
|
if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
|
2004-04-18 00:56:44 +00:00
|
|
|
info.rti_info[RTAX_DST] = NULL;
|
1997-07-18 11:44:24 +00:00
|
|
|
senderr(EINVAL);
|
|
|
|
}
|
2001-10-17 18:07:05 +00:00
|
|
|
info.rti_flags = rtm->rtm_flags;
|
2004-04-18 00:56:44 +00:00
|
|
|
if (info.rti_info[RTAX_DST] == NULL ||
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
|
2004-04-18 00:56:44 +00:00
|
|
|
(info.rti_info[RTAX_GATEWAY] != NULL &&
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
|
1994-05-24 10:09:53 +00:00
|
|
|
senderr(EINVAL);
|
2003-10-03 18:15:54 +00:00
|
|
|
if (info.rti_info[RTAX_GENMASK]) {
|
1994-05-24 10:09:53 +00:00
|
|
|
struct radix_node *t;
|
2003-10-03 18:15:54 +00:00
|
|
|
t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
|
2004-04-18 19:38:20 +00:00
|
|
|
if (t != NULL &&
|
2004-04-19 07:20:32 +00:00
|
|
|
bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1,
|
|
|
|
(char *)(void *)t->rn_key + 1,
|
|
|
|
((struct sockaddr *)t->rn_key)->sa_len - 1) == 0)
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_GENMASK] =
|
2004-04-19 07:20:32 +00:00
|
|
|
(struct sockaddr *)t->rn_key;
|
1994-05-24 10:09:53 +00:00
|
|
|
else
|
|
|
|
senderr(ENOBUFS);
|
|
|
|
}
|
2001-08-31 12:31:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the caller has the appropriate privilege; RTM_GET
|
|
|
|
* is the only operation the non-superuser is allowed.
|
|
|
|
*/
|
2002-04-01 21:31:13 +00:00
|
|
|
if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0)
|
2001-09-20 08:25:25 +00:00
|
|
|
senderr(error);
|
2001-08-31 12:31:09 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
switch (rtm->rtm_type) {
|
2003-10-03 18:15:54 +00:00
|
|
|
struct rtentry *saved_nrt;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
case RTM_ADD:
|
2004-04-18 00:56:44 +00:00
|
|
|
if (info.rti_info[RTAX_GATEWAY] == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
senderr(EINVAL);
|
2004-04-18 00:56:44 +00:00
|
|
|
saved_nrt = NULL;
|
2001-10-17 18:07:05 +00:00
|
|
|
error = rtrequest1(RTM_ADD, &info, &saved_nrt);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (error == 0 && saved_nrt) {
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_LOCK(saved_nrt);
|
1994-05-24 10:09:53 +00:00
|
|
|
rt_setmetrics(rtm->rtm_inits,
|
|
|
|
&rtm->rtm_rmx, &saved_nrt->rt_rmx);
|
2003-11-08 23:36:32 +00:00
|
|
|
RT_REMREF(saved_nrt);
|
2003-10-03 18:15:54 +00:00
|
|
|
saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_UNLOCK(saved_nrt);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTM_DELETE:
|
2004-04-18 00:56:44 +00:00
|
|
|
saved_nrt = NULL;
|
2001-10-17 18:07:05 +00:00
|
|
|
error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
|
1995-05-10 16:48:52 +00:00
|
|
|
if (error == 0) {
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_LOCK(saved_nrt);
|
2002-12-25 10:21:02 +00:00
|
|
|
rt = saved_nrt;
|
1995-05-10 16:48:52 +00:00
|
|
|
goto report;
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RTM_GET:
|
|
|
|
case RTM_CHANGE:
|
|
|
|
case RTM_LOCK:
|
2003-10-03 18:15:54 +00:00
|
|
|
rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
|
2004-04-18 00:56:44 +00:00
|
|
|
if (rnh == NULL)
|
1995-05-10 16:48:52 +00:00
|
|
|
senderr(EAFNOSUPPORT);
|
2002-12-24 03:03:39 +00:00
|
|
|
RADIX_NODE_HEAD_LOCK(rnh);
|
2003-10-03 18:15:54 +00:00
|
|
|
rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
|
|
|
|
info.rti_info[RTAX_NETMASK], rnh);
|
2002-12-24 03:03:39 +00:00
|
|
|
RADIX_NODE_HEAD_UNLOCK(rnh);
|
2003-10-03 18:15:54 +00:00
|
|
|
if (rt == NULL) /* XXX looks bogus */
|
1994-05-24 10:09:53 +00:00
|
|
|
senderr(ESRCH);
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_LOCK(rt);
|
2003-11-08 23:36:32 +00:00
|
|
|
RT_ADDREF(rt);
|
2002-12-24 03:03:39 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
switch(rtm->rtm_type) {
|
|
|
|
|
|
|
|
case RTM_GET:
|
1995-05-10 16:48:52 +00:00
|
|
|
report:
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_LOCK_ASSERT(rt);
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_DST] = rt_key(rt);
|
|
|
|
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
|
|
|
|
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
|
|
|
|
info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
|
1994-05-24 10:09:53 +00:00
|
|
|
if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
|
1994-10-08 22:38:27 +00:00
|
|
|
ifp = rt->rt_ifp;
|
|
|
|
if (ifp) {
|
2004-04-16 08:14:34 +00:00
|
|
|
info.rti_info[RTAX_IFP] =
|
|
|
|
ifaddr_byindex(ifp->if_index)->ifa_addr;
|
2004-04-26 19:46:52 +00:00
|
|
|
if (jailed(so->so_cred)) {
|
2004-07-06 03:29:41 +00:00
|
|
|
bzero(&jail, sizeof(jail));
|
2004-04-26 19:46:52 +00:00
|
|
|
jail.sin_family = PF_INET;
|
|
|
|
jail.sin_len = sizeof(jail);
|
|
|
|
jail.sin_addr.s_addr =
|
|
|
|
htonl(prison_getip(so->so_cred));
|
|
|
|
info.rti_info[RTAX_IFA] =
|
2004-05-03 17:15:34 +00:00
|
|
|
(struct sockaddr *)&jail;
|
2004-04-26 19:46:52 +00:00
|
|
|
} else
|
|
|
|
info.rti_info[RTAX_IFA] =
|
2004-05-03 17:15:34 +00:00
|
|
|
rt->rt_ifa->ifa_addr;
|
2001-10-17 11:23:59 +00:00
|
|
|
if (ifp->if_flags & IFF_POINTOPOINT)
|
2004-05-03 17:15:34 +00:00
|
|
|
info.rti_info[RTAX_BRD] =
|
|
|
|
rt->rt_ifa->ifa_dstaddr;
|
1994-05-24 10:09:53 +00:00
|
|
|
rtm->rtm_index = ifp->if_index;
|
|
|
|
} else {
|
2004-04-18 00:56:44 +00:00
|
|
|
info.rti_info[RTAX_IFP] = NULL;
|
|
|
|
info.rti_info[RTAX_IFA] = NULL;
|
2002-10-01 15:48:31 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2004-04-17 15:09:36 +00:00
|
|
|
len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (len > rtm->rtm_msglen) {
|
|
|
|
struct rt_msghdr *new_rtm;
|
|
|
|
R_Malloc(new_rtm, struct rt_msghdr *, len);
|
2004-04-18 00:56:44 +00:00
|
|
|
if (new_rtm == NULL) {
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_UNLOCK(rt);
|
1994-05-24 10:09:53 +00:00
|
|
|
senderr(ENOBUFS);
|
2003-10-03 18:15:54 +00:00
|
|
|
}
|
2004-04-18 11:47:04 +00:00
|
|
|
bcopy(rtm, new_rtm, rtm->rtm_msglen);
|
1994-05-24 10:09:53 +00:00
|
|
|
Free(rtm); rtm = new_rtm;
|
|
|
|
}
|
2004-04-17 15:09:36 +00:00
|
|
|
(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
|
1994-05-24 10:09:53 +00:00
|
|
|
rtm->rtm_flags = rt->rt_flags;
|
2003-11-20 20:07:39 +00:00
|
|
|
rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
|
1994-05-24 10:09:53 +00:00
|
|
|
rtm->rtm_addrs = info.rti_addrs;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTM_CHANGE:
|
2003-10-03 18:15:54 +00:00
|
|
|
/*
|
|
|
|
* New gateway could require new ifaddr, ifp;
|
|
|
|
* flags may also be different; ifp may be specified
|
|
|
|
* by ll sockaddr when protocol address is ambiguous
|
|
|
|
*/
|
|
|
|
if (((rt->rt_flags & RTF_GATEWAY) &&
|
|
|
|
info.rti_info[RTAX_GATEWAY] != NULL) ||
|
|
|
|
info.rti_info[RTAX_IFP] != NULL ||
|
|
|
|
(info.rti_info[RTAX_IFA] != NULL &&
|
|
|
|
!sa_equal(info.rti_info[RTAX_IFA],
|
|
|
|
rt->rt_ifa->ifa_addr))) {
|
|
|
|
if ((error = rt_getifa(&info)) != 0) {
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_UNLOCK(rt);
|
2001-12-19 16:03:27 +00:00
|
|
|
senderr(error);
|
2003-10-03 18:15:54 +00:00
|
|
|
}
|
2001-12-19 16:03:27 +00:00
|
|
|
}
|
2003-10-03 18:15:54 +00:00
|
|
|
if (info.rti_info[RTAX_GATEWAY] != NULL &&
|
|
|
|
(error = rt_setgate(rt, rt_key(rt),
|
|
|
|
info.rti_info[RTAX_GATEWAY])) != 0) {
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_UNLOCK(rt);
|
2001-10-17 18:07:05 +00:00
|
|
|
senderr(error);
|
2003-10-03 18:15:54 +00:00
|
|
|
}
|
2001-10-17 18:07:05 +00:00
|
|
|
if ((ifa = info.rti_ifa) != NULL) {
|
2003-10-03 18:15:54 +00:00
|
|
|
struct ifaddr *oifa = rt->rt_ifa;
|
1994-05-24 10:09:53 +00:00
|
|
|
if (oifa != ifa) {
|
2003-10-03 18:15:54 +00:00
|
|
|
if (oifa) {
|
|
|
|
if (oifa->ifa_rtrequest)
|
|
|
|
oifa->ifa_rtrequest(
|
|
|
|
RTM_DELETE, rt,
|
|
|
|
&info);
|
|
|
|
IFAFREE(oifa);
|
|
|
|
}
|
|
|
|
IFAREF(ifa);
|
|
|
|
rt->rt_ifa = ifa;
|
|
|
|
rt->rt_ifp = info.rti_ifp;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
|
|
|
|
&rt->rt_rmx);
|
|
|
|
if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
|
2001-10-17 18:07:05 +00:00
|
|
|
rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
|
2003-10-03 18:15:54 +00:00
|
|
|
if (info.rti_info[RTAX_GENMASK])
|
|
|
|
rt->rt_genmask = info.rti_info[RTAX_GENMASK];
|
2002-08-25 13:23:09 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-24 10:09:53 +00:00
|
|
|
case RTM_LOCK:
|
2003-11-20 20:07:39 +00:00
|
|
|
/* We don't support locks anymore */
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_UNLOCK(rt);
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
senderr(EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
|
|
|
|
flush:
|
|
|
|
if (rtm) {
|
|
|
|
if (error)
|
|
|
|
rtm->rtm_errno = error;
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1994-05-24 10:09:53 +00:00
|
|
|
rtm->rtm_flags |= RTF_DONE;
|
|
|
|
}
|
2003-10-03 18:15:54 +00:00
|
|
|
if (rt) /* XXX can this be true? */
|
|
|
|
RTFREE(rt);
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2004-04-18 00:56:44 +00:00
|
|
|
struct rawcb *rp = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Check to see if we don't want our own messages.
|
|
|
|
*/
|
|
|
|
if ((so->so_options & SO_USELOOPBACK) == 0) {
|
|
|
|
if (route_cb.any_count <= 1) {
|
|
|
|
if (rtm)
|
|
|
|
Free(rtm);
|
|
|
|
m_freem(m);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
/* There is another listener, so construct message */
|
|
|
|
rp = sotorawcb(so);
|
2002-05-31 11:52:35 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
if (rtm) {
|
|
|
|
m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
|
2001-07-04 11:29:55 +00:00
|
|
|
if (m->m_pkthdr.len < rtm->rtm_msglen) {
|
|
|
|
m_freem(m);
|
|
|
|
m = NULL;
|
|
|
|
} else if (m->m_pkthdr.len > rtm->rtm_msglen)
|
|
|
|
m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
|
1994-05-24 10:09:53 +00:00
|
|
|
Free(rtm);
|
|
|
|
}
|
2003-10-03 18:15:54 +00:00
|
|
|
if (m) {
|
|
|
|
if (rp) {
|
|
|
|
/*
|
|
|
|
* XXX insure we don't get a copy by
|
|
|
|
* invalidating our protocol
|
|
|
|
*/
|
|
|
|
unsigned short family = rp->rcb_proto.sp_family;
|
|
|
|
rp->rcb_proto.sp_family = 0;
|
|
|
|
rt_dispatch(m, info.rti_info[RTAX_DST]);
|
|
|
|
rp->rcb_proto.sp_family = family;
|
|
|
|
} else
|
|
|
|
rt_dispatch(m, info.rti_info[RTAX_DST]);
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
return (error);
|
2003-10-03 18:15:54 +00:00
|
|
|
#undef sa_equal
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
static void
|
2004-04-18 00:56:44 +00:00
|
|
|
rt_setmetrics(u_long which, const struct rt_metrics *in,
|
|
|
|
struct rt_metrics_lite *out)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
#define metric(f, e) if (which & (f)) out->e = in->e;
|
2003-11-20 20:07:39 +00:00
|
|
|
/*
|
|
|
|
* Only these are stored in the routing entry since introduction
|
|
|
|
* of tcp hostcache. The rest is ignored.
|
|
|
|
*/
|
1994-05-24 10:09:53 +00:00
|
|
|
metric(RTV_MTU, rmx_mtu);
|
|
|
|
metric(RTV_EXPIRE, rmx_expire);
|
|
|
|
#undef metric
|
|
|
|
}
|
|
|
|
|
2003-11-20 20:07:39 +00:00
|
|
|
static void
|
2004-04-18 00:56:44 +00:00
|
|
|
rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
|
2003-11-20 20:07:39 +00:00
|
|
|
{
|
|
|
|
#define metric(e) out->e = in->e;
|
|
|
|
bzero(out, sizeof(*out));
|
|
|
|
metric(rmx_mtu);
|
|
|
|
metric(rmx_expire);
|
|
|
|
#undef metric
|
|
|
|
}
|
|
|
|
|
1997-07-15 23:25:32 +00:00
|
|
|
/*
|
|
|
|
* Extract the addresses of the passed sockaddrs.
|
|
|
|
* Do a little sanity checking so as to avoid bad memory references.
|
1997-07-18 11:44:24 +00:00
|
|
|
* This data is derived straight from userland.
|
1997-07-15 23:25:32 +00:00
|
|
|
*/
|
1997-07-18 11:44:24 +00:00
|
|
|
static int
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2004-04-13 11:22:22 +00:00
|
|
|
struct sockaddr *sa;
|
|
|
|
int i;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2003-10-03 18:15:54 +00:00
|
|
|
for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
|
1994-05-24 10:09:53 +00:00
|
|
|
if ((rtinfo->rti_addrs & (1 << i)) == 0)
|
|
|
|
continue;
|
1997-07-16 14:55:14 +00:00
|
|
|
sa = (struct sockaddr *)cp;
|
1997-07-15 23:25:32 +00:00
|
|
|
/*
|
1997-07-18 11:44:24 +00:00
|
|
|
* It won't fit.
|
1997-07-15 23:25:32 +00:00
|
|
|
*/
|
2003-10-03 18:15:54 +00:00
|
|
|
if (cp + sa->sa_len > cplim)
|
1997-07-18 11:44:24 +00:00
|
|
|
return (EINVAL);
|
1997-07-15 23:25:32 +00:00
|
|
|
/*
|
|
|
|
* there are no more.. quit now
|
|
|
|
* If there are more bits, they are in error.
|
|
|
|
* I've seen this. route(1) can evidently generate these.
|
|
|
|
* This causes kernel to core dump.
|
1997-07-18 11:44:24 +00:00
|
|
|
* for compatibility, If we see this, point to a safe address.
|
1997-07-15 23:25:32 +00:00
|
|
|
*/
|
1997-07-18 11:44:24 +00:00
|
|
|
if (sa->sa_len == 0) {
|
|
|
|
rtinfo->rti_info[i] = &sa_zero;
|
|
|
|
return (0); /* should be EINVAL but for compat */
|
|
|
|
}
|
|
|
|
/* accept it */
|
|
|
|
rtinfo->rti_info[i] = sa;
|
2004-04-13 11:22:22 +00:00
|
|
|
cp += SA_SIZE(sa);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1997-07-18 11:44:24 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct mbuf *
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_msg1(int type, struct rt_addrinfo *rtinfo)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2004-04-18 00:56:44 +00:00
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct mbuf *m;
|
|
|
|
int i;
|
|
|
|
struct sockaddr *sa;
|
1994-05-24 10:09:53 +00:00
|
|
|
int len, dlen;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case RTM_DELADDR:
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
len = sizeof(struct ifa_msghdr);
|
|
|
|
break;
|
|
|
|
|
1997-01-13 21:26:53 +00:00
|
|
|
case RTM_DELMADDR:
|
|
|
|
case RTM_NEWMADDR:
|
|
|
|
len = sizeof(struct ifma_msghdr);
|
|
|
|
break;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
case RTM_IFINFO:
|
|
|
|
len = sizeof(struct if_msghdr);
|
|
|
|
break;
|
|
|
|
|
2002-01-18 14:33:04 +00:00
|
|
|
case RTM_IFANNOUNCE:
|
|
|
|
len = sizeof(struct if_announcemsghdr);
|
|
|
|
break;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
default:
|
|
|
|
len = sizeof(struct rt_msghdr);
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
if (len > MCLBYTES)
|
1994-05-24 10:09:53 +00:00
|
|
|
panic("rt_msg1");
|
2003-02-19 05:47:46 +00:00
|
|
|
m = m_gethdr(M_DONTWAIT, MT_DATA);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (m && len > MHLEN) {
|
2003-02-19 05:47:46 +00:00
|
|
|
MCLGET(m, M_DONTWAIT);
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((m->m_flags & M_EXT) == 0) {
|
|
|
|
m_free(m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
}
|
2004-04-18 00:56:44 +00:00
|
|
|
if (m == NULL)
|
2001-06-11 12:39:29 +00:00
|
|
|
return (m);
|
1994-05-24 10:09:53 +00:00
|
|
|
m->m_pkthdr.len = m->m_len = len;
|
2004-04-18 00:56:44 +00:00
|
|
|
m->m_pkthdr.rcvif = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
rtm = mtod(m, struct rt_msghdr *);
|
|
|
|
bzero((caddr_t)rtm, len);
|
|
|
|
for (i = 0; i < RTAX_MAX; i++) {
|
|
|
|
if ((sa = rtinfo->rti_info[i]) == NULL)
|
|
|
|
continue;
|
|
|
|
rtinfo->rti_addrs |= (1 << i);
|
2004-04-13 11:22:22 +00:00
|
|
|
dlen = SA_SIZE(sa);
|
1994-05-24 10:09:53 +00:00
|
|
|
m_copyback(m, len, dlen, (caddr_t)sa);
|
|
|
|
len += dlen;
|
|
|
|
}
|
|
|
|
if (m->m_pkthdr.len != len) {
|
|
|
|
m_freem(m);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
rtm->rtm_msglen = len;
|
|
|
|
rtm->rtm_version = RTM_VERSION;
|
|
|
|
rtm->rtm_type = type;
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2004-04-18 00:56:44 +00:00
|
|
|
int i;
|
1994-05-24 10:09:53 +00:00
|
|
|
int len, dlen, second_time = 0;
|
|
|
|
caddr_t cp0;
|
|
|
|
|
|
|
|
rtinfo->rti_addrs = 0;
|
|
|
|
again:
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case RTM_DELADDR:
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
len = sizeof(struct ifa_msghdr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTM_IFINFO:
|
|
|
|
len = sizeof(struct if_msghdr);
|
|
|
|
break;
|
|
|
|
|
2003-11-14 18:48:15 +00:00
|
|
|
case RTM_NEWMADDR:
|
|
|
|
len = sizeof(struct ifma_msghdr);
|
|
|
|
break;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
default:
|
|
|
|
len = sizeof(struct rt_msghdr);
|
|
|
|
}
|
1994-10-08 22:38:27 +00:00
|
|
|
cp0 = cp;
|
|
|
|
if (cp0)
|
1994-05-24 10:09:53 +00:00
|
|
|
cp += len;
|
|
|
|
for (i = 0; i < RTAX_MAX; i++) {
|
2004-04-18 00:56:44 +00:00
|
|
|
struct sockaddr *sa;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2004-04-18 00:56:44 +00:00
|
|
|
if ((sa = rtinfo->rti_info[i]) == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
|
|
|
rtinfo->rti_addrs |= (1 << i);
|
2004-04-13 11:22:22 +00:00
|
|
|
dlen = SA_SIZE(sa);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (cp) {
|
|
|
|
bcopy((caddr_t)sa, cp, (unsigned)dlen);
|
|
|
|
cp += dlen;
|
|
|
|
}
|
|
|
|
len += dlen;
|
|
|
|
}
|
2002-01-27 20:39:01 +00:00
|
|
|
len = ALIGN(len);
|
2004-04-18 00:56:44 +00:00
|
|
|
if (cp == NULL && w != NULL && !second_time) {
|
|
|
|
struct walkarg *rw = w;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
if (rw->w_req) {
|
1994-05-24 10:09:53 +00:00
|
|
|
if (rw->w_tmemsize < len) {
|
|
|
|
if (rw->w_tmem)
|
|
|
|
free(rw->w_tmem, M_RTABLE);
|
1994-10-08 22:38:27 +00:00
|
|
|
rw->w_tmem = (caddr_t)
|
|
|
|
malloc(len, M_RTABLE, M_NOWAIT);
|
|
|
|
if (rw->w_tmem)
|
1994-05-24 10:09:53 +00:00
|
|
|
rw->w_tmemsize = len;
|
|
|
|
}
|
|
|
|
if (rw->w_tmem) {
|
|
|
|
cp = rw->w_tmem;
|
|
|
|
second_time = 1;
|
|
|
|
goto again;
|
1995-11-16 19:00:27 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cp) {
|
2004-04-18 00:56:44 +00:00
|
|
|
struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
rtm->rtm_version = RTM_VERSION;
|
|
|
|
rtm->rtm_type = type;
|
|
|
|
rtm->rtm_msglen = len;
|
|
|
|
}
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is called to generate a message from the routing
|
|
|
|
* socket indicating that a redirect has occured, a routing lookup
|
|
|
|
* has failed, or that a protocol has detected timeouts to a particular
|
|
|
|
* destination.
|
|
|
|
*/
|
|
|
|
void
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2003-10-03 18:15:54 +00:00
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct mbuf *m;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
|
|
|
|
|
|
|
|
if (route_cb.any_count == 0)
|
|
|
|
return;
|
|
|
|
m = rt_msg1(type, rtinfo);
|
2004-04-18 00:56:44 +00:00
|
|
|
if (m == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
return;
|
|
|
|
rtm = mtod(m, struct rt_msghdr *);
|
|
|
|
rtm->rtm_flags = RTF_DONE | flags;
|
|
|
|
rtm->rtm_errno = error;
|
|
|
|
rtm->rtm_addrs = rtinfo->rti_addrs;
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_dispatch(m, sa);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is called to generate a message from the routing
|
|
|
|
* socket indicating that the status of a network interface has changed.
|
|
|
|
*/
|
|
|
|
void
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_ifmsg(struct ifnet *ifp)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2003-10-03 18:15:54 +00:00
|
|
|
struct if_msghdr *ifm;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
struct rt_addrinfo info;
|
|
|
|
|
|
|
|
if (route_cb.any_count == 0)
|
|
|
|
return;
|
|
|
|
bzero((caddr_t)&info, sizeof(info));
|
|
|
|
m = rt_msg1(RTM_IFINFO, &info);
|
2004-04-18 00:56:44 +00:00
|
|
|
if (m == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
return;
|
|
|
|
ifm = mtod(m, struct if_msghdr *);
|
|
|
|
ifm->ifm_index = ifp->if_index;
|
2002-08-18 07:05:00 +00:00
|
|
|
ifm->ifm_flags = ifp->if_flags;
|
1994-05-24 10:09:53 +00:00
|
|
|
ifm->ifm_data = ifp->if_data;
|
|
|
|
ifm->ifm_addrs = 0;
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_dispatch(m, NULL);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called to generate messages from the routing socket
|
|
|
|
* indicating a network interface has had addresses associated with it.
|
|
|
|
* if we ever reverse the logic and replace messages TO the routing
|
|
|
|
* socket indicate a request to configure interfaces, then it will
|
|
|
|
* be unnecessary as the routing socket will automatically generate
|
|
|
|
* copies of it.
|
|
|
|
*/
|
|
|
|
void
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
struct rt_addrinfo info;
|
2004-04-18 00:56:44 +00:00
|
|
|
struct sockaddr *sa = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
int pass;
|
2004-04-18 00:56:44 +00:00
|
|
|
struct mbuf *m = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct ifnet *ifp = ifa->ifa_ifp;
|
|
|
|
|
|
|
|
if (route_cb.any_count == 0)
|
|
|
|
return;
|
|
|
|
for (pass = 1; pass < 3; pass++) {
|
|
|
|
bzero((caddr_t)&info, sizeof(info));
|
|
|
|
if ((cmd == RTM_ADD && pass == 1) ||
|
|
|
|
(cmd == RTM_DELETE && pass == 2)) {
|
2004-04-18 00:56:44 +00:00
|
|
|
struct ifa_msghdr *ifam;
|
1994-05-24 10:09:53 +00:00
|
|
|
int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
|
|
|
|
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
|
2004-04-16 08:14:34 +00:00
|
|
|
info.rti_info[RTAX_IFP] =
|
|
|
|
ifaddr_byindex(ifp->if_index)->ifa_addr;
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
|
|
|
|
info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
|
1994-05-24 10:09:53 +00:00
|
|
|
if ((m = rt_msg1(ncmd, &info)) == NULL)
|
|
|
|
continue;
|
|
|
|
ifam = mtod(m, struct ifa_msghdr *);
|
|
|
|
ifam->ifam_index = ifp->if_index;
|
|
|
|
ifam->ifam_metric = ifa->ifa_metric;
|
|
|
|
ifam->ifam_flags = ifa->ifa_flags;
|
|
|
|
ifam->ifam_addrs = info.rti_addrs;
|
|
|
|
}
|
|
|
|
if ((cmd == RTM_ADD && pass == 2) ||
|
|
|
|
(cmd == RTM_DELETE && pass == 1)) {
|
2004-04-18 00:56:44 +00:00
|
|
|
struct rt_msghdr *rtm;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
2004-04-18 00:56:44 +00:00
|
|
|
if (rt == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
continue;
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
|
|
|
|
info.rti_info[RTAX_DST] = sa = rt_key(rt);
|
|
|
|
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
|
1994-05-24 10:09:53 +00:00
|
|
|
if ((m = rt_msg1(cmd, &info)) == NULL)
|
|
|
|
continue;
|
|
|
|
rtm = mtod(m, struct rt_msghdr *);
|
|
|
|
rtm->rtm_index = ifp->if_index;
|
|
|
|
rtm->rtm_flags |= rt->rt_flags;
|
|
|
|
rtm->rtm_errno = error;
|
|
|
|
rtm->rtm_addrs = info.rti_addrs;
|
|
|
|
}
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_dispatch(m, sa);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-13 21:26:53 +00:00
|
|
|
/*
|
|
|
|
* This is the analogue to the rt_newaddrmsg which performs the same
|
|
|
|
* function but for multicast group memberhips. This is easier since
|
|
|
|
* there is no route state to worry about.
|
|
|
|
*/
|
|
|
|
void
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
|
1997-01-13 21:26:53 +00:00
|
|
|
{
|
|
|
|
struct rt_addrinfo info;
|
2004-04-18 00:56:44 +00:00
|
|
|
struct mbuf *m = NULL;
|
1997-01-13 21:26:53 +00:00
|
|
|
struct ifnet *ifp = ifma->ifma_ifp;
|
|
|
|
struct ifma_msghdr *ifmam;
|
|
|
|
|
|
|
|
if (route_cb.any_count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bzero((caddr_t)&info, sizeof(info));
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_IFA] = ifma->ifma_addr;
|
2004-04-16 08:14:34 +00:00
|
|
|
info.rti_info[RTAX_IFP] =
|
|
|
|
ifp ? ifaddr_byindex(ifp->if_index)->ifa_addr : NULL;
|
1997-01-13 21:26:53 +00:00
|
|
|
/*
|
|
|
|
* If a link-layer address is present, present it as a ``gateway''
|
|
|
|
* (similarly to how ARP entries, e.g., are presented).
|
|
|
|
*/
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
|
|
|
|
m = rt_msg1(cmd, &info);
|
|
|
|
if (m == NULL)
|
1997-01-13 21:26:53 +00:00
|
|
|
return;
|
|
|
|
ifmam = mtod(m, struct ifma_msghdr *);
|
|
|
|
ifmam->ifmam_index = ifp->if_index;
|
|
|
|
ifmam->ifmam_addrs = info.rti_addrs;
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_dispatch(m, ifma->ifma_addr);
|
1997-01-13 21:26:53 +00:00
|
|
|
}
|
1995-11-16 19:00:27 +00:00
|
|
|
|
2002-01-18 14:33:04 +00:00
|
|
|
/*
|
|
|
|
* This is called to generate routing socket messages indicating
|
|
|
|
* network interface arrival and departure.
|
|
|
|
*/
|
|
|
|
void
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_ifannouncemsg(struct ifnet *ifp, int what)
|
2002-01-18 14:33:04 +00:00
|
|
|
{
|
|
|
|
struct if_announcemsghdr *ifan;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct rt_addrinfo info;
|
|
|
|
|
|
|
|
if (route_cb.any_count == 0)
|
|
|
|
return;
|
|
|
|
bzero((caddr_t)&info, sizeof(info));
|
|
|
|
m = rt_msg1(RTM_IFANNOUNCE, &info);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
ifan = mtod(m, struct if_announcemsghdr *);
|
|
|
|
ifan->ifan_index = ifp->if_index;
|
2003-10-31 18:32:15 +00:00
|
|
|
strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name));
|
2002-01-18 14:33:04 +00:00
|
|
|
ifan->ifan_what = what;
|
2003-10-03 18:15:54 +00:00
|
|
|
rt_dispatch(m, NULL);
|
2002-01-18 14:33:04 +00:00
|
|
|
}
|
|
|
|
|
2003-10-03 18:15:54 +00:00
|
|
|
static void
|
2004-04-18 00:56:44 +00:00
|
|
|
rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
|
2003-10-03 18:15:54 +00:00
|
|
|
{
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
unsigned short *family;
|
|
|
|
struct m_tag *tag;
|
2003-10-03 18:15:54 +00:00
|
|
|
|
Introduce a netisr to deliver kernel-generated routing, avoiding
recursive entering of the socket code from the routing code:
- Modify rt_dispatch() to bundle up the sockaddr family, if any,
associated with a pending mbuf to dispatch to routing sockets, in
an m_tag on the mbuf.
- Allocate NETISR_ROUTE for use by routing sockets.
- Introduce rtsintrq, an ifqueue to be used by the netisr, and
introduce rts_input(), a function to unbundle the tagged sockaddr
and inject the mbuf and address into raw_input(), which previously
occurred in rt_dispatch().
- Introduce rts_init() to initialize rtsintrq, its mutex, and
register the netisr. Perform this at the same point in system
initialization as setup of the domains.
This change introduces asynchrony between the generation of a
pending routing socket message and delivery to sockets for use
by userspace. It avoids socket->routing->rtsock->socket use and
helps to avoid lock order reversals between the routing code and
socket code (in particular, raw socket control blocks), as route
locks are held over calls to rt_dispatch().
Reviewed by: "George V.Neville-Neil" <gnn@neville-neil.com>
Conceptual head nod by: sam
2004-06-09 02:48:23 +00:00
|
|
|
/*
|
|
|
|
* Preserve the family from the sockaddr, if any, in an m_tag for
|
|
|
|
* use when injecting the mbuf into the routing socket buffer from
|
|
|
|
* the netisr.
|
|
|
|
*/
|
|
|
|
if (sa != NULL) {
|
|
|
|
tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
|
|
|
|
M_NOWAIT);
|
|
|
|
if (tag == NULL) {
|
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
family = (unsigned short *)(tag + 1);
|
|
|
|
*family = sa ? sa->sa_family : 0;
|
|
|
|
m_tag_prepend(m, tag);
|
|
|
|
}
|
|
|
|
netisr_queue(NETISR_ROUTE, m);
|
2003-10-03 18:15:54 +00:00
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* This is used in dumping the kernel table via sysctl().
|
|
|
|
*/
|
2002-09-28 17:15:38 +00:00
|
|
|
static int
|
2003-10-03 18:15:54 +00:00
|
|
|
sysctl_dumpentry(struct radix_node *rn, void *vw)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2003-10-03 18:15:54 +00:00
|
|
|
struct walkarg *w = vw;
|
|
|
|
struct rtentry *rt = (struct rtentry *)rn;
|
1994-05-24 10:09:53 +00:00
|
|
|
int error = 0, size;
|
|
|
|
struct rt_addrinfo info;
|
|
|
|
|
|
|
|
if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
|
|
|
|
return 0;
|
|
|
|
bzero((caddr_t)&info, sizeof(info));
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_DST] = rt_key(rt);
|
|
|
|
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
|
|
|
|
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
|
|
|
|
info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
|
2001-10-17 11:23:59 +00:00
|
|
|
if (rt->rt_ifp) {
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_IFP] =
|
2004-04-16 08:14:34 +00:00
|
|
|
ifaddr_byindex(rt->rt_ifp->if_index)->ifa_addr;
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
|
2001-10-17 11:23:59 +00:00
|
|
|
if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
|
2001-10-17 11:23:59 +00:00
|
|
|
}
|
2004-04-17 15:09:36 +00:00
|
|
|
size = rt_msg2(RTM_GET, &info, NULL, w);
|
1995-11-16 19:00:27 +00:00
|
|
|
if (w->w_req && w->w_tmem) {
|
2003-10-03 18:15:54 +00:00
|
|
|
struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
rtm->rtm_flags = rt->rt_flags;
|
2003-11-20 20:07:39 +00:00
|
|
|
rtm->rtm_use = rt->rt_rmx.rmx_pksent;
|
|
|
|
rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
|
1994-05-24 10:09:53 +00:00
|
|
|
rtm->rtm_index = rt->rt_ifp->if_index;
|
|
|
|
rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
|
|
|
|
rtm->rtm_addrs = info.rti_addrs;
|
1995-11-16 19:00:27 +00:00
|
|
|
error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
|
|
|
|
return (error);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2002-09-28 17:15:38 +00:00
|
|
|
static int
|
2003-10-03 18:15:54 +00:00
|
|
|
sysctl_iflist(int af, struct walkarg *w)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2003-10-03 18:15:54 +00:00
|
|
|
struct ifnet *ifp;
|
|
|
|
struct ifaddr *ifa;
|
|
|
|
struct rt_addrinfo info;
|
|
|
|
int len, error = 0;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
bzero((caddr_t)&info, sizeof(info));
|
2002-12-22 05:35:03 +00:00
|
|
|
/* IFNET_RLOCK(); */ /* could sleep XXX */
|
2001-02-04 13:13:25 +00:00
|
|
|
TAILQ_FOREACH(ifp, &ifnet, if_link) {
|
1994-05-24 10:09:53 +00:00
|
|
|
if (w->w_arg && w->w_arg != ifp->if_index)
|
|
|
|
continue;
|
2004-04-16 08:14:34 +00:00
|
|
|
ifa = ifaddr_byindex(ifp->if_index);
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_IFP] = ifa->ifa_addr;
|
2004-04-17 15:09:36 +00:00
|
|
|
len = rt_msg2(RTM_IFINFO, &info, NULL, w);
|
2004-04-18 00:56:44 +00:00
|
|
|
info.rti_info[RTAX_IFP] = NULL;
|
1995-11-16 19:00:27 +00:00
|
|
|
if (w->w_req && w->w_tmem) {
|
2003-10-03 18:15:54 +00:00
|
|
|
struct if_msghdr *ifm;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
ifm = (struct if_msghdr *)w->w_tmem;
|
|
|
|
ifm->ifm_index = ifp->if_index;
|
2002-08-18 07:05:00 +00:00
|
|
|
ifm->ifm_flags = ifp->if_flags;
|
1994-05-24 10:09:53 +00:00
|
|
|
ifm->ifm_data = ifp->if_data;
|
|
|
|
ifm->ifm_addrs = info.rti_addrs;
|
1995-11-16 19:00:27 +00:00
|
|
|
error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
|
1994-10-08 22:38:27 +00:00
|
|
|
if (error)
|
2001-09-29 05:08:04 +00:00
|
|
|
goto done;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2004-04-18 00:56:44 +00:00
|
|
|
while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
|
1994-05-24 10:09:53 +00:00
|
|
|
if (af && af != ifa->ifa_addr->sa_family)
|
|
|
|
continue;
|
2002-02-27 18:32:23 +00:00
|
|
|
if (jailed(curthread->td_ucred) &&
|
|
|
|
prison_if(curthread->td_ucred, ifa->ifa_addr))
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
continue;
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_IFA] = ifa->ifa_addr;
|
|
|
|
info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
|
|
|
|
info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
|
2004-04-17 15:09:36 +00:00
|
|
|
len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
|
1995-11-16 19:00:27 +00:00
|
|
|
if (w->w_req && w->w_tmem) {
|
2003-10-03 18:15:54 +00:00
|
|
|
struct ifa_msghdr *ifam;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
ifam = (struct ifa_msghdr *)w->w_tmem;
|
|
|
|
ifam->ifam_index = ifa->ifa_ifp->if_index;
|
|
|
|
ifam->ifam_flags = ifa->ifa_flags;
|
|
|
|
ifam->ifam_metric = ifa->ifa_metric;
|
|
|
|
ifam->ifam_addrs = info.rti_addrs;
|
1995-11-16 19:00:27 +00:00
|
|
|
error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
|
1994-10-08 22:38:27 +00:00
|
|
|
if (error)
|
2001-09-29 05:08:04 +00:00
|
|
|
goto done;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-03 18:15:54 +00:00
|
|
|
info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
|
2004-04-18 00:56:44 +00:00
|
|
|
info.rti_info[RTAX_BRD] = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2001-09-29 05:08:04 +00:00
|
|
|
done:
|
2002-12-22 05:35:03 +00:00
|
|
|
/* IFNET_RUNLOCK(); */ /* XXX */
|
2001-09-29 05:08:04 +00:00
|
|
|
return (error);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
2003-11-14 18:48:15 +00:00
|
|
|
int
|
2004-04-16 08:14:34 +00:00
|
|
|
sysctl_ifmalist(int af, struct walkarg *w)
|
2003-11-14 18:48:15 +00:00
|
|
|
{
|
2004-04-16 08:14:34 +00:00
|
|
|
struct ifnet *ifp;
|
2003-11-14 18:48:15 +00:00
|
|
|
struct ifmultiaddr *ifma;
|
|
|
|
struct rt_addrinfo info;
|
|
|
|
int len, error = 0;
|
2004-04-16 08:14:34 +00:00
|
|
|
struct ifaddr *ifa;
|
2003-11-14 18:48:15 +00:00
|
|
|
|
|
|
|
bzero((caddr_t)&info, sizeof(info));
|
|
|
|
/* IFNET_RLOCK(); */ /* could sleep XXX */
|
|
|
|
TAILQ_FOREACH(ifp, &ifnet, if_link) {
|
|
|
|
if (w->w_arg && w->w_arg != ifp->if_index)
|
|
|
|
continue;
|
2004-04-16 08:14:34 +00:00
|
|
|
ifa = ifaddr_byindex(ifp->if_index);
|
2004-04-17 15:09:36 +00:00
|
|
|
info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
|
2003-11-14 18:48:15 +00:00
|
|
|
TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
|
|
|
|
if (af && af != ifma->ifma_addr->sa_family)
|
|
|
|
continue;
|
|
|
|
if (jailed(curproc->p_ucred) &&
|
|
|
|
prison_if(curproc->p_ucred, ifma->ifma_addr))
|
|
|
|
continue;
|
|
|
|
info.rti_info[RTAX_IFA] = ifma->ifma_addr;
|
2004-04-17 15:09:36 +00:00
|
|
|
info.rti_info[RTAX_GATEWAY] =
|
|
|
|
(ifma->ifma_addr->sa_family != AF_LINK) ?
|
|
|
|
ifma->ifma_lladdr : NULL;
|
|
|
|
len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
|
2003-11-14 18:48:15 +00:00
|
|
|
if (w->w_req && w->w_tmem) {
|
2004-04-16 08:14:34 +00:00
|
|
|
struct ifma_msghdr *ifmam;
|
2003-11-14 18:48:15 +00:00
|
|
|
|
|
|
|
ifmam = (struct ifma_msghdr *)w->w_tmem;
|
|
|
|
ifmam->ifmam_index = ifma->ifma_ifp->if_index;
|
|
|
|
ifmam->ifmam_flags = 0;
|
|
|
|
ifmam->ifmam_addrs = info.rti_addrs;
|
|
|
|
error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
|
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
/* IFNET_RUNLOCK(); */ /* XXX */
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
static int
|
2000-07-04 11:25:35 +00:00
|
|
|
sysctl_rtsock(SYSCTL_HANDLER_ARGS)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1995-11-16 19:00:27 +00:00
|
|
|
int *name = (int *)arg1;
|
|
|
|
u_int namelen = arg2;
|
2003-10-03 18:15:54 +00:00
|
|
|
struct radix_node_head *rnh;
|
2004-04-12 20:26:01 +00:00
|
|
|
int i, lim, s, error = EINVAL;
|
|
|
|
u_char af;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct walkarg w;
|
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
name ++;
|
|
|
|
namelen--;
|
|
|
|
if (req->newptr)
|
1994-05-24 10:09:53 +00:00
|
|
|
return (EPERM);
|
|
|
|
if (namelen != 3)
|
2002-01-25 00:50:49 +00:00
|
|
|
return ((namelen < 3) ? EISDIR : ENOTDIR);
|
1994-05-24 10:09:53 +00:00
|
|
|
af = name[0];
|
2002-12-25 10:51:20 +00:00
|
|
|
if (af > AF_MAX)
|
|
|
|
return (EINVAL);
|
2004-04-18 11:47:04 +00:00
|
|
|
bzero(&w, sizeof(w));
|
1994-05-24 10:09:53 +00:00
|
|
|
w.w_op = name[1];
|
|
|
|
w.w_arg = name[2];
|
1995-11-16 19:00:27 +00:00
|
|
|
w.w_req = req;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
s = splnet();
|
|
|
|
switch (w.w_op) {
|
|
|
|
|
|
|
|
case NET_RT_DUMP:
|
|
|
|
case NET_RT_FLAGS:
|
2004-04-12 20:26:01 +00:00
|
|
|
if (af == 0) { /* dump all tables */
|
|
|
|
i = 1;
|
|
|
|
lim = AF_MAX;
|
|
|
|
} else /* dump only one table */
|
|
|
|
i = lim = af;
|
|
|
|
for (error = 0; error == 0 && i <= lim; i++)
|
|
|
|
if ((rnh = rt_tables[i]) != NULL) {
|
2002-12-26 03:35:57 +00:00
|
|
|
/* RADIX_NODE_HEAD_LOCK(rnh); */
|
2002-12-24 03:03:39 +00:00
|
|
|
error = rnh->rnh_walktree(rnh,
|
2002-12-26 03:35:57 +00:00
|
|
|
sysctl_dumpentry, &w);/* could sleep XXX */
|
|
|
|
/* RADIX_NODE_HEAD_UNLOCK(rnh); */
|
2004-04-12 20:26:01 +00:00
|
|
|
} else if (af != 0)
|
2002-12-24 03:03:39 +00:00
|
|
|
error = EAFNOSUPPORT;
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NET_RT_IFLIST:
|
|
|
|
error = sysctl_iflist(af, &w);
|
2003-11-14 18:48:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NET_RT_IFMALIST:
|
|
|
|
error = sysctl_ifmalist(af, &w);
|
|
|
|
break;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
if (w.w_tmem)
|
|
|
|
free(w.w_tmem, M_RTABLE);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1997-04-27 20:01:29 +00:00
|
|
|
SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
|
1995-11-16 19:00:27 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Definitions of protocols supported in the ROUTE domain.
|
|
|
|
*/
|
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
extern struct domain routedomain; /* or at least forward */
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
static struct protosw routesw[] = {
|
1994-05-24 10:09:53 +00:00
|
|
|
{ SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
|
1996-05-08 04:29:08 +00:00
|
|
|
0, route_output, raw_ctlinput, 0,
|
1997-04-27 20:01:29 +00:00
|
|
|
0,
|
|
|
|
raw_init, 0, 0, 0,
|
|
|
|
&route_usrreqs
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
1995-11-16 19:00:27 +00:00
|
|
|
static struct domain routedomain =
|
1999-04-29 03:22:19 +00:00
|
|
|
{ PF_ROUTE, "route", 0, 0, 0,
|
1994-05-24 10:09:53 +00:00
|
|
|
routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
|
1995-05-10 16:48:52 +00:00
|
|
|
|
1995-05-11 00:13:26 +00:00
|
|
|
DOMAIN_SET(route);
|