1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* ng_socket.c
|
2005-01-07 01:45:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
1999-10-21 09:06:11 +00:00
|
|
|
* Copyright (c) 1996-1999 Whistle Communications, Inc.
|
|
|
|
* All rights reserved.
|
2001-01-08 05:34:06 +00:00
|
|
|
*
|
1999-10-21 09:06:11 +00:00
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
|
|
|
* without modifications are expressly permitted by Whistle Communications;
|
|
|
|
* provided, however, that:
|
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
|
|
|
* 2. No rights are granted, in any manner or form, to use Whistle
|
|
|
|
* Communications, Inc. trademarks, including the mark "WHISTLE
|
|
|
|
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
|
|
|
|
* such appears in the above copyright notice or in the software.
|
2001-01-08 05:34:06 +00:00
|
|
|
*
|
1999-10-21 09:06:11 +00:00
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
|
|
|
|
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
|
|
|
|
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
|
|
|
|
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
|
|
|
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
|
|
|
|
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
|
|
|
|
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
|
|
|
|
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER 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 WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
2000-10-24 17:32:45 +00:00
|
|
|
* Author: Julian Elischer <julian@freebsd.org>
|
1999-10-21 09:06:11 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1999-11-01 10:00:40 +00:00
|
|
|
* $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Netgraph socket nodes
|
|
|
|
*
|
|
|
|
* There are two types of netgraph sockets, control and data.
|
|
|
|
* Control sockets have a netgraph node, but data sockets are
|
|
|
|
* parasitic on control sockets, and have no node of their own.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/domain.h>
|
2012-01-23 16:43:13 +00:00
|
|
|
#include <sys/hash.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/kernel.h>
|
2004-07-27 20:30:56 +00:00
|
|
|
#include <sys/linker.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/lock.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
2004-07-12 04:45:46 +00:00
|
|
|
#include <sys/mutex.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/protosw.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/queue.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
2006-06-13 21:36:23 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/sysctl.h>
|
Introduce and use a sysinit-based initialization scheme for virtual
network stacks, VNET_SYSINIT:
- Add VNET_SYSINIT and VNET_SYSUNINIT macros to declare events that will
occur each time a network stack is instantiated and destroyed. In the
!VIMAGE case, these are simply mapped into regular SYSINIT/SYSUNINIT.
For the VIMAGE case, we instead use SYSINIT's to track their order and
properties on registration, using them for each vnet when created/
destroyed, or immediately on module load for already-started vnets.
- Remove vnet_modinfo mechanism that existed to serve this purpose
previously, as well as its dependency scheme: we now just use the
SYSINIT ordering scheme.
- Implement VNET_DOMAIN_SET() to allow protocol domains to declare that
they want init functions to be called for each virtual network stack
rather than just once at boot, compiling down to DOMAIN_SET() in the
non-VIMAGE case.
- Walk all virtualized kernel subsystems and make use of these instead
of modinfo or DOMAIN_SET() for init/uninit events. In some cases,
convert modular components from using modevent to using sysinit (where
appropriate). In some cases, do minor rejuggling of SYSINIT ordering
to make room for or better manage events.
Portions submitted by: jhb (VNET_SYSINIT), bz (cleanup)
Discussed with: jhb, bz, julian, zec
Reviewed by: bz
Approved by: re (VIMAGE blanket)
2009-07-23 20:46:49 +00:00
|
|
|
|
|
|
|
#include <net/vnet.h>
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
1999-11-06 19:27:04 +00:00
|
|
|
#include <netgraph/ng_socketvar.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <netgraph/ng_socket.h>
|
|
|
|
|
2001-01-10 07:13:58 +00:00
|
|
|
#ifdef NG_SEPARATE_MALLOC
|
2011-11-07 06:44:47 +00:00
|
|
|
static MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info");
|
|
|
|
static MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info");
|
2001-01-10 07:13:58 +00:00
|
|
|
#else
|
|
|
|
#define M_NETGRAPH_PATH M_NETGRAPH
|
|
|
|
#define M_NETGRAPH_SOCK M_NETGRAPH
|
|
|
|
#endif
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* It's Ascii-art time!
|
|
|
|
* +-------------+ +-------------+
|
|
|
|
* |socket (ctl)| |socket (data)|
|
|
|
|
* +-------------+ +-------------+
|
|
|
|
* ^ ^
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* +-----------+ +-----------+
|
|
|
|
* |pcb (ctl)| |pcb (data)|
|
|
|
|
* +-----------+ +-----------+
|
|
|
|
* ^ ^
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* +--------------------------+
|
|
|
|
* | Socket type private |
|
|
|
|
* | data |
|
|
|
|
* +--------------------------+
|
|
|
|
* ^
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* +----------------+
|
|
|
|
* | struct ng_node |
|
|
|
|
* +----------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Netgraph node methods */
|
1999-11-01 10:00:40 +00:00
|
|
|
static ng_constructor_t ngs_constructor;
|
|
|
|
static ng_rcvmsg_t ngs_rcvmsg;
|
2001-01-06 00:46:47 +00:00
|
|
|
static ng_shutdown_t ngs_shutdown;
|
1999-11-01 10:00:40 +00:00
|
|
|
static ng_newhook_t ngs_newhook;
|
2001-02-05 18:57:11 +00:00
|
|
|
static ng_connect_t ngs_connect;
|
2012-01-23 16:43:13 +00:00
|
|
|
static ng_findhook_t ngs_findhook;
|
1999-11-01 10:00:40 +00:00
|
|
|
static ng_rcvdata_t ngs_rcvdata;
|
1999-11-05 02:18:08 +00:00
|
|
|
static ng_disconnect_t ngs_disconnect;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Internal methods */
|
|
|
|
static int ng_attach_data(struct socket *so);
|
|
|
|
static int ng_attach_cntl(struct socket *so);
|
|
|
|
static int ng_attach_common(struct socket *so, int type);
|
2005-11-02 15:34:42 +00:00
|
|
|
static void ng_detach_common(struct ngpcb *pcbp, int type);
|
|
|
|
static void ng_socket_free_priv(struct ngsock *priv);
|
1999-10-21 09:06:11 +00:00
|
|
|
static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
|
|
|
|
static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
|
|
|
|
|
|
|
|
static int ngs_mod_event(module_t mod, int event, void *data);
|
2005-07-05 17:35:20 +00:00
|
|
|
static void ng_socket_item_applied(void *context, int error);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Netgraph type descriptor */
|
|
|
|
static struct ng_type typestruct = {
|
2004-05-29 00:51:19 +00:00
|
|
|
.version = NG_ABI_VERSION,
|
|
|
|
.name = NG_SOCKET_NODE_TYPE,
|
|
|
|
.mod_event = ngs_mod_event,
|
|
|
|
.constructor = ngs_constructor,
|
|
|
|
.rcvmsg = ngs_rcvmsg,
|
|
|
|
.shutdown = ngs_shutdown,
|
|
|
|
.newhook = ngs_newhook,
|
|
|
|
.connect = ngs_connect,
|
2012-01-23 16:43:13 +00:00
|
|
|
.findhook = ngs_findhook,
|
2004-05-29 00:51:19 +00:00
|
|
|
.rcvdata = ngs_rcvdata,
|
|
|
|
.disconnect = ngs_disconnect,
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
2004-11-30 22:28:50 +00:00
|
|
|
NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Buffer space */
|
2000-08-10 22:51:57 +00:00
|
|
|
static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
|
2011-01-12 19:53:39 +00:00
|
|
|
SYSCTL_ULONG(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
|
2004-01-23 14:35:44 +00:00
|
|
|
&ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
|
1999-10-21 09:06:11 +00:00
|
|
|
static u_long ngpdg_recvspace = 20 * 1024;
|
2011-01-12 19:53:39 +00:00
|
|
|
SYSCTL_ULONG(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
|
2004-01-27 22:02:01 +00:00
|
|
|
&ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2010-03-12 14:51:42 +00:00
|
|
|
/* List of all sockets (for netstat -f netgraph) */
|
|
|
|
static LIST_HEAD(, ngpcb) ngsocklist;
|
|
|
|
|
|
|
|
static struct mtx ngsocketlist_mtx;
|
|
|
|
|
1999-11-21 22:18:54 +00:00
|
|
|
#define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2004-07-10 21:45:58 +00:00
|
|
|
/* If getting unexplained errors returned, set this to "kdb_enter("X"); */
|
1999-10-21 09:06:11 +00:00
|
|
|
#ifndef TRAP_ERROR
|
|
|
|
#define TRAP_ERROR
|
|
|
|
#endif
|
|
|
|
|
2012-01-23 16:43:13 +00:00
|
|
|
struct hookpriv {
|
|
|
|
LIST_ENTRY(hookpriv) next;
|
|
|
|
hook_p hook;
|
|
|
|
};
|
|
|
|
LIST_HEAD(ngshash, hookpriv);
|
|
|
|
|
2012-01-23 15:39:45 +00:00
|
|
|
/* Per-node private data */
|
|
|
|
struct ngsock {
|
|
|
|
struct ng_node *node; /* the associated netgraph node */
|
|
|
|
struct ngpcb *datasock; /* optional data socket */
|
|
|
|
struct ngpcb *ctlsock; /* optional control socket */
|
2012-01-23 16:43:13 +00:00
|
|
|
struct ngshash *hash; /* hash for hook names */
|
|
|
|
u_long hmask; /* hash mask */
|
2012-01-23 15:39:45 +00:00
|
|
|
int flags;
|
|
|
|
int refs;
|
|
|
|
struct mtx mtx; /* mtx to wait on */
|
|
|
|
int error; /* place to store error */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NGS_FLAG_NOLINGER 1 /* close with last hook */
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/***************************************************************
|
|
|
|
Control sockets
|
|
|
|
***************************************************************/
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ngc_attach(struct socket *so, int proto, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
2006-11-06 13:42:10 +00:00
|
|
|
int error;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2006-11-06 13:42:10 +00:00
|
|
|
error = priv_check(td, PRIV_NETGRAPH_CONTROL);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (pcbp != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
return (ng_attach_cntl(so));
|
|
|
|
}
|
|
|
|
|
Chance protocol switch method pru_detach() so that it returns void
rather than an error. Detaches do not "fail", they other occur or
the protocol flags SS_PROTOREF to take ownership of the socket.
soclose() no longer looks at so_pcb to see if it's NULL, relying
entirely on the protocol to decide whether it's time to free the
socket or not using SS_PROTOREF. so_pcb is now entirely owned and
managed by the protocol code. Likewise, no longer test so_pcb in
other socket functions, such as soreceive(), which have no business
digging into protocol internals.
Protocol detach routines no longer try to free the socket on detach,
this is performed in the socket code if the protocol permits it.
In rts_detach(), no longer test for rp != NULL in detach, and
likewise in other protocols that don't permit a NULL so_pcb, reduce
the incidence of testing for it during detach.
netinet and netinet6 are not fully updated to this change, which
will be in an upcoming commit. In their current state they may leak
memory or panic.
MFC after: 3 months
2006-04-01 15:42:02 +00:00
|
|
|
static void
|
1999-10-21 09:06:11 +00:00
|
|
|
ngc_detach(struct socket *so)
|
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
|
Chance protocol switch method pru_detach() so that it returns void
rather than an error. Detaches do not "fail", they other occur or
the protocol flags SS_PROTOREF to take ownership of the socket.
soclose() no longer looks at so_pcb to see if it's NULL, relying
entirely on the protocol to decide whether it's time to free the
socket or not using SS_PROTOREF. so_pcb is now entirely owned and
managed by the protocol code. Likewise, no longer test so_pcb in
other socket functions, such as soreceive(), which have no business
digging into protocol internals.
Protocol detach routines no longer try to free the socket on detach,
this is performed in the socket code if the protocol permits it.
In rts_detach(), no longer test for rp != NULL in detach, and
likewise in other protocols that don't permit a NULL so_pcb, reduce
the incidence of testing for it during detach.
netinet and netinet6 are not fully updated to this change, which
will be in an upcoming commit. In their current state they may leak
memory or panic.
MFC after: 3 months
2006-04-01 15:42:02 +00:00
|
|
|
KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
|
2005-11-02 15:34:42 +00:00
|
|
|
ng_detach_common(pcbp, NG_CONTROL);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
|
2001-09-12 08:38:13 +00:00
|
|
|
struct mbuf *control, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
2005-07-05 17:35:20 +00:00
|
|
|
struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
|
1999-10-21 09:06:11 +00:00
|
|
|
struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
1999-10-21 09:06:11 +00:00
|
|
|
struct mbuf *m0;
|
2005-05-17 12:18:13 +00:00
|
|
|
item_p item;
|
2001-01-06 00:46:47 +00:00
|
|
|
char *path = NULL;
|
1999-10-21 09:06:11 +00:00
|
|
|
int len, error = 0;
|
2007-10-19 15:04:17 +00:00
|
|
|
struct ng_apply_info apply;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
if (control) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/* Require destination as there may be >= 1 hooks on this node. */
|
1999-10-21 09:06:11 +00:00
|
|
|
if (addr == NULL) {
|
|
|
|
error = EDESTADDRREQ;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/*
|
|
|
|
* Allocate an expendable buffer for the path, chop off
|
|
|
|
* the sockaddr header, and make sure it's NUL terminated.
|
|
|
|
*/
|
1999-10-21 09:06:11 +00:00
|
|
|
len = sap->sg_len - 2;
|
2006-10-17 12:21:48 +00:00
|
|
|
path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
|
1999-10-21 09:06:11 +00:00
|
|
|
bcopy(sap->sg_data, path, len);
|
|
|
|
path[len] = '\0';
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/*
|
|
|
|
* Move the actual message out of mbufs into a linear buffer.
|
|
|
|
* Start by adding up the size of the data. (could use mh_len?)
|
|
|
|
*/
|
1999-10-21 09:06:11 +00:00
|
|
|
for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
|
|
|
|
len += m0->m_len;
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/*
|
|
|
|
* Move the data into a linear buffer as well.
|
|
|
|
* Messages are not delivered in mbufs.
|
|
|
|
*/
|
|
|
|
msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
|
2001-01-06 00:46:47 +00:00
|
|
|
m_copydata(m, 0, len, (char *)msg);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2005-02-04 21:38:42 +00:00
|
|
|
if (msg->header.version != NG_VERSION) {
|
2006-10-17 12:21:48 +00:00
|
|
|
free(msg, M_NETGRAPH_MSG);
|
2005-02-04 21:38:42 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
2004-07-27 20:30:56 +00:00
|
|
|
/*
|
|
|
|
* Hack alert!
|
|
|
|
* We look into the message and if it mkpeers a node of unknown type, we
|
|
|
|
* try to load it. We need to do this now, in syscall thread, because if
|
|
|
|
* message gets queued and applied later we will get panic.
|
|
|
|
*/
|
2004-07-31 21:32:55 +00:00
|
|
|
if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
|
|
|
|
msg->header.cmd == NGM_MKPEER) {
|
2004-07-27 20:30:56 +00:00
|
|
|
struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
|
|
|
|
|
2008-11-22 16:55:55 +00:00
|
|
|
if (ng_findtype(mkp->type) == NULL) {
|
2004-07-27 20:30:56 +00:00
|
|
|
char filename[NG_TYPESIZ + 3];
|
2006-06-13 21:36:23 +00:00
|
|
|
int fileid;
|
2004-07-27 20:30:56 +00:00
|
|
|
|
2005-05-16 18:50:26 +00:00
|
|
|
/* Not found, try to load it as a loadable module. */
|
|
|
|
snprintf(filename, sizeof(filename), "ng_%s",
|
|
|
|
mkp->type);
|
2006-06-13 21:36:23 +00:00
|
|
|
error = kern_kldload(curthread, filename, &fileid);
|
2004-07-27 20:30:56 +00:00
|
|
|
if (error != 0) {
|
2006-10-17 12:21:48 +00:00
|
|
|
free(msg, M_NETGRAPH_MSG);
|
2004-07-27 20:30:56 +00:00
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
2005-05-16 18:50:26 +00:00
|
|
|
/* See if type has been loaded successfully. */
|
2008-11-22 16:55:55 +00:00
|
|
|
if (ng_findtype(mkp->type) == NULL) {
|
2006-10-17 12:21:48 +00:00
|
|
|
free(msg, M_NETGRAPH_MSG);
|
2006-06-13 21:36:23 +00:00
|
|
|
(void)kern_kldunload(curthread, fileid,
|
|
|
|
LINKER_UNLOAD_NORMAL);
|
2004-07-27 20:30:56 +00:00
|
|
|
error = ENXIO;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 06:40:07 +00:00
|
|
|
item = ng_package_msg(msg, NG_WAITOK);
|
2006-10-17 12:21:48 +00:00
|
|
|
if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
|
|
|
|
!= 0) {
|
2005-05-17 12:18:13 +00:00
|
|
|
#ifdef TRACE_MESSAGES
|
|
|
|
printf("ng_address_path: errx=%d\n", error);
|
|
|
|
#endif
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TRACE_MESSAGES
|
|
|
|
printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
|
|
|
|
item->el_dest->nd_ID,
|
|
|
|
msg->header.typecookie,
|
|
|
|
msg->header.cmd,
|
|
|
|
msg->header.cmdstr,
|
|
|
|
msg->header.flags,
|
|
|
|
msg->header.token,
|
|
|
|
item->el_dest->nd_type->name);
|
2001-01-08 05:34:06 +00:00
|
|
|
#endif
|
2005-05-17 12:18:13 +00:00
|
|
|
SAVE_LINE(item);
|
2005-07-05 17:35:20 +00:00
|
|
|
/*
|
|
|
|
* We do not want to return from syscall until the item
|
|
|
|
* is processed by destination node. We register callback
|
|
|
|
* on the item, which will update priv->error when item
|
|
|
|
* was applied.
|
|
|
|
* If ng_snd_item() has queued item, we sleep until
|
|
|
|
* callback wakes us up.
|
|
|
|
*/
|
2007-10-19 15:04:17 +00:00
|
|
|
bzero(&apply, sizeof(apply));
|
|
|
|
apply.apply = ng_socket_item_applied;
|
|
|
|
apply.context = priv;
|
|
|
|
item->apply = &apply;
|
2005-07-05 17:35:20 +00:00
|
|
|
priv->error = -1;
|
|
|
|
|
2007-10-19 15:04:17 +00:00
|
|
|
error = ng_snd_item(item, 0);
|
2005-07-05 17:35:20 +00:00
|
|
|
|
2007-10-19 15:04:17 +00:00
|
|
|
mtx_lock(&priv->mtx);
|
|
|
|
if (priv->error == -1)
|
|
|
|
msleep(priv, &priv->mtx, 0, "ngsock", 0);
|
|
|
|
mtx_unlock(&priv->mtx);
|
|
|
|
KASSERT(priv->error != -1,
|
|
|
|
("ng_socket: priv->error wasn't updated"));
|
|
|
|
error = priv->error;
|
2005-05-16 18:50:26 +00:00
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
release:
|
|
|
|
if (path != NULL)
|
2006-10-17 12:21:48 +00:00
|
|
|
free(path, M_NETGRAPH_PATH);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (control != NULL)
|
|
|
|
m_freem(control);
|
|
|
|
if (m != NULL)
|
|
|
|
m_freem(m);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
|
|
|
|
if (pcbp == 0)
|
|
|
|
return (EINVAL);
|
|
|
|
return (ng_bind(nam, pcbp));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-01-06 00:46:47 +00:00
|
|
|
/*
|
2001-01-08 05:34:06 +00:00
|
|
|
* At this time refuse to do this.. it used to
|
2001-01-06 00:46:47 +00:00
|
|
|
* do something but it was undocumented and not used.
|
|
|
|
*/
|
2006-10-17 12:21:48 +00:00
|
|
|
printf("program tried to connect control socket to remote node\n");
|
2001-01-06 00:46:47 +00:00
|
|
|
return (EINVAL);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
Data sockets
|
|
|
|
***************************************************************/
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ngd_attach(struct socket *so, int proto, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
|
|
|
|
if (pcbp != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
return (ng_attach_data(so));
|
|
|
|
}
|
|
|
|
|
Chance protocol switch method pru_detach() so that it returns void
rather than an error. Detaches do not "fail", they other occur or
the protocol flags SS_PROTOREF to take ownership of the socket.
soclose() no longer looks at so_pcb to see if it's NULL, relying
entirely on the protocol to decide whether it's time to free the
socket or not using SS_PROTOREF. so_pcb is now entirely owned and
managed by the protocol code. Likewise, no longer test so_pcb in
other socket functions, such as soreceive(), which have no business
digging into protocol internals.
Protocol detach routines no longer try to free the socket on detach,
this is performed in the socket code if the protocol permits it.
In rts_detach(), no longer test for rp != NULL in detach, and
likewise in other protocols that don't permit a NULL so_pcb, reduce
the incidence of testing for it during detach.
netinet and netinet6 are not fully updated to this change, which
will be in an upcoming commit. In their current state they may leak
memory or panic.
MFC after: 3 months
2006-04-01 15:42:02 +00:00
|
|
|
static void
|
1999-10-21 09:06:11 +00:00
|
|
|
ngd_detach(struct socket *so)
|
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
|
2006-04-06 02:54:42 +00:00
|
|
|
KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
|
2005-11-02 15:34:42 +00:00
|
|
|
ng_detach_common(pcbp, NG_DATA);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
|
2001-09-12 08:38:13 +00:00
|
|
|
struct mbuf *control, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
|
2006-10-17 12:21:48 +00:00
|
|
|
int len, error;
|
1999-11-21 10:43:05 +00:00
|
|
|
hook_p hook = NULL;
|
2004-01-26 14:05:31 +00:00
|
|
|
char hookname[NG_HOOKSIZ];
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
if ((pcbp == NULL) || (control != NULL)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
if (pcbp->sockdata == NULL) {
|
|
|
|
error = ENOTCONN;
|
|
|
|
goto release;
|
|
|
|
}
|
2005-05-28 13:15:44 +00:00
|
|
|
|
|
|
|
if (sap == NULL)
|
|
|
|
len = 0; /* Make compiler happy. */
|
|
|
|
else
|
|
|
|
len = sap->sg_len - 2;
|
|
|
|
|
1999-11-21 10:43:05 +00:00
|
|
|
/*
|
|
|
|
* If the user used any of these ways to not specify an address
|
|
|
|
* then handle specially.
|
|
|
|
*/
|
2005-05-28 13:15:44 +00:00
|
|
|
if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
|
2001-01-08 05:34:06 +00:00
|
|
|
if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
|
1999-11-21 10:43:05 +00:00
|
|
|
error = EDESTADDRREQ;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
/*
|
2006-10-17 12:21:48 +00:00
|
|
|
* If exactly one hook exists, just use it.
|
1999-11-21 10:43:05 +00:00
|
|
|
* Special case to allow write(2) to work on an ng_socket.
|
|
|
|
*/
|
2001-01-08 05:34:06 +00:00
|
|
|
hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
|
1999-11-21 10:43:05 +00:00
|
|
|
} else {
|
2004-01-26 14:05:31 +00:00
|
|
|
if (len >= NG_HOOKSIZ) {
|
1999-11-21 10:43:05 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
|
1999-11-21 10:43:05 +00:00
|
|
|
/*
|
|
|
|
* chop off the sockaddr header, and make sure it's NUL
|
|
|
|
* terminated
|
|
|
|
*/
|
|
|
|
bcopy(sap->sg_data, hookname, len);
|
|
|
|
hookname[len] = '\0';
|
|
|
|
|
|
|
|
/* Find the correct hook from 'hookname' */
|
2006-10-17 12:21:48 +00:00
|
|
|
hook = ng_findhook(pcbp->sockdata->node, hookname);
|
2001-02-05 18:57:11 +00:00
|
|
|
if (hook == NULL) {
|
1999-11-21 10:43:05 +00:00
|
|
|
error = EHOSTUNREACH;
|
2006-10-17 12:21:48 +00:00
|
|
|
goto release;
|
2001-02-05 18:57:11 +00:00
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/* Send data. */
|
|
|
|
NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
release:
|
|
|
|
if (control != NULL)
|
|
|
|
m_freem(control);
|
|
|
|
if (m != NULL)
|
|
|
|
m_freem(m);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
|
|
|
|
if (pcbp == 0)
|
|
|
|
return (EINVAL);
|
|
|
|
return (ng_connect_data(nam, pcbp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used for both data and control sockets
|
|
|
|
*/
|
|
|
|
static int
|
2007-05-11 10:20:51 +00:00
|
|
|
ng_getsockaddr(struct socket *so, struct sockaddr **addr)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
1999-11-11 20:08:04 +00:00
|
|
|
struct ngpcb *pcbp;
|
|
|
|
struct sockaddr_ng *sg;
|
2005-11-02 15:34:42 +00:00
|
|
|
int sg_len;
|
|
|
|
int error = 0;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
1999-11-11 20:08:04 +00:00
|
|
|
pcbp = sotongpcb(so);
|
2005-11-02 15:34:42 +00:00
|
|
|
if ((pcbp == NULL) || (pcbp->sockdata == NULL))
|
|
|
|
/* XXXGL: can this still happen? */
|
1999-10-21 09:06:11 +00:00
|
|
|
return (EINVAL);
|
1999-11-11 20:08:04 +00:00
|
|
|
|
2012-02-16 14:44:52 +00:00
|
|
|
sg_len = sizeof(struct sockaddr_ng) + NG_NODESIZ -
|
|
|
|
sizeof(sg->sg_data);
|
|
|
|
sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
|
|
|
|
|
2005-11-02 15:34:42 +00:00
|
|
|
mtx_lock(&pcbp->sockdata->mtx);
|
|
|
|
if (pcbp->sockdata->node != NULL) {
|
|
|
|
node_p node = pcbp->sockdata->node;
|
1999-11-11 20:08:04 +00:00
|
|
|
|
2005-11-02 15:34:42 +00:00
|
|
|
if (NG_NODE_HAS_NAME(node))
|
2012-02-16 14:44:52 +00:00
|
|
|
bcopy(NG_NODE_NAME(node), sg->sg_data,
|
|
|
|
strlen(NG_NODE_NAME(node)));
|
|
|
|
mtx_unlock(&pcbp->sockdata->mtx);
|
1999-11-11 20:08:04 +00:00
|
|
|
|
2005-11-02 15:34:42 +00:00
|
|
|
sg->sg_len = sg_len;
|
|
|
|
sg->sg_family = AF_NETGRAPH;
|
|
|
|
*addr = (struct sockaddr *)sg;
|
|
|
|
} else {
|
|
|
|
mtx_unlock(&pcbp->sockdata->mtx);
|
2012-02-16 14:44:52 +00:00
|
|
|
free(sg, M_SONAME);
|
2005-11-02 15:34:42 +00:00
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach a socket to it's protocol specific partner.
|
|
|
|
* For a control socket, actually create a netgraph node and attach
|
|
|
|
* to it as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
ng_attach_cntl(struct socket *so)
|
|
|
|
{
|
2005-11-02 15:34:42 +00:00
|
|
|
struct ngsock *priv;
|
1999-10-21 09:06:11 +00:00
|
|
|
struct ngpcb *pcbp;
|
2011-07-14 18:38:10 +00:00
|
|
|
node_p node;
|
1999-10-21 09:06:11 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Setup protocol control block */
|
2011-07-14 18:38:10 +00:00
|
|
|
if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
1999-11-21 22:18:54 +00:00
|
|
|
pcbp = sotongpcb(so);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Make the generic node components */
|
2011-07-14 18:38:10 +00:00
|
|
|
if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
|
2005-11-02 15:34:42 +00:00
|
|
|
ng_detach_common(pcbp, NG_CONTROL);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2012-01-23 16:43:13 +00:00
|
|
|
/*
|
|
|
|
* Allocate node private info and hash. We start
|
|
|
|
* with 16 hash entries, however we may grow later
|
|
|
|
* in ngs_newhook(). We can't predict how much hooks
|
|
|
|
* does this node plan to have.
|
|
|
|
*/
|
2011-07-14 18:38:10 +00:00
|
|
|
priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
|
2012-01-23 16:43:13 +00:00
|
|
|
priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
|
2011-07-14 18:38:10 +00:00
|
|
|
|
|
|
|
/* Initialize mutex. */
|
|
|
|
mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
|
|
|
|
|
|
|
|
/* Link the pcb the private data. */
|
|
|
|
priv->ctlsock = pcbp;
|
|
|
|
pcbp->sockdata = priv;
|
|
|
|
priv->refs++;
|
|
|
|
priv->node = node;
|
2012-01-23 15:39:45 +00:00
|
|
|
pcbp->node_id = node->nd_ID; /* hint for netstat(1) */
|
2010-03-12 15:04:59 +00:00
|
|
|
|
2005-11-02 15:34:42 +00:00
|
|
|
/* Link the node and the private data. */
|
|
|
|
NG_NODE_SET_PRIVATE(priv->node, priv);
|
|
|
|
NG_NODE_REF(priv->node);
|
|
|
|
priv->refs++;
|
2005-07-05 17:35:20 +00:00
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ng_attach_data(struct socket *so)
|
|
|
|
{
|
2006-10-17 12:21:48 +00:00
|
|
|
return (ng_attach_common(so, NG_DATA));
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a socket protocol control block.
|
|
|
|
* This code is shared between control and data sockets.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_attach_common(struct socket *so, int type)
|
|
|
|
{
|
|
|
|
struct ngpcb *pcbp;
|
|
|
|
int error;
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/* Standard socket setup stuff. */
|
1999-10-21 09:06:11 +00:00
|
|
|
error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/* Allocate the pcb. */
|
|
|
|
pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
|
1999-10-21 09:06:11 +00:00
|
|
|
pcbp->type = type;
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/* Link the pcb and the socket. */
|
|
|
|
so->so_pcb = (caddr_t)pcbp;
|
1999-10-21 09:06:11 +00:00
|
|
|
pcbp->ng_socket = so;
|
|
|
|
|
2010-03-12 14:51:42 +00:00
|
|
|
/* Add the socket to linked list */
|
|
|
|
mtx_lock(&ngsocketlist_mtx);
|
|
|
|
LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
|
|
|
|
mtx_unlock(&ngsocketlist_mtx);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disassociate the socket from it's protocol specific
|
|
|
|
* partner. If it's attached to a node's private data structure,
|
|
|
|
* then unlink from that too. If we were the last socket attached to it,
|
|
|
|
* then shut down the entire node. Shared code for control and data sockets.
|
|
|
|
*/
|
|
|
|
static void
|
2005-11-02 15:34:42 +00:00
|
|
|
ng_detach_common(struct ngpcb *pcbp, int which)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2005-11-02 15:34:42 +00:00
|
|
|
struct ngsock *priv = pcbp->sockdata;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2005-11-02 15:34:42 +00:00
|
|
|
if (priv != NULL) {
|
|
|
|
mtx_lock(&priv->mtx);
|
2005-05-16 17:25:49 +00:00
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
switch (which) {
|
|
|
|
case NG_CONTROL:
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->ctlsock = NULL;
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
case NG_DATA:
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock = NULL;
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-10-13 17:21:21 +00:00
|
|
|
panic("%s", __func__);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
2005-11-02 15:34:42 +00:00
|
|
|
pcbp->sockdata = NULL;
|
2012-01-23 15:39:45 +00:00
|
|
|
pcbp->node_id = 0;
|
2005-11-02 15:34:42 +00:00
|
|
|
|
|
|
|
ng_socket_free_priv(priv);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
2005-11-02 15:34:42 +00:00
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
pcbp->ng_socket->so_pcb = NULL;
|
2010-03-12 14:51:42 +00:00
|
|
|
mtx_lock(&ngsocketlist_mtx);
|
|
|
|
LIST_REMOVE(pcbp, socks);
|
|
|
|
mtx_unlock(&ngsocketlist_mtx);
|
2006-10-17 12:21:48 +00:00
|
|
|
free(pcbp, M_PCB);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2005-11-02 15:34:42 +00:00
|
|
|
/*
|
|
|
|
* Remove a reference from node private data.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_socket_free_priv(struct ngsock *priv)
|
|
|
|
{
|
|
|
|
mtx_assert(&priv->mtx, MA_OWNED);
|
|
|
|
|
|
|
|
priv->refs--;
|
|
|
|
|
|
|
|
if (priv->refs == 0) {
|
|
|
|
mtx_destroy(&priv->mtx);
|
2012-01-23 16:43:13 +00:00
|
|
|
hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
|
2006-10-17 12:21:48 +00:00
|
|
|
free(priv, M_NETGRAPH_SOCK);
|
2005-11-02 15:34:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((priv->refs == 1) && (priv->node != NULL)) {
|
|
|
|
node_p node = priv->node;
|
|
|
|
|
|
|
|
priv->node = NULL;
|
|
|
|
mtx_unlock(&priv->mtx);
|
|
|
|
NG_NODE_UNREF(node);
|
|
|
|
ng_rmnode_self(node);
|
|
|
|
} else
|
|
|
|
mtx_unlock(&priv->mtx);
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* Connect the data socket to a named control socket node.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
|
|
|
|
{
|
|
|
|
struct sockaddr_ng *sap;
|
|
|
|
node_p farnode;
|
2001-02-05 18:57:11 +00:00
|
|
|
struct ngsock *priv;
|
1999-10-21 09:06:11 +00:00
|
|
|
int error;
|
2001-01-06 00:46:47 +00:00
|
|
|
item_p item;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/* If we are already connected, don't do it again. */
|
1999-10-21 09:06:11 +00:00
|
|
|
if (pcbp->sockdata != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/*
|
|
|
|
* Find the target (victim) and check it doesn't already have
|
|
|
|
* a data socket. Also check it is a 'socket' type node.
|
|
|
|
* Use ng_package_data() and ng_address_path() to do this.
|
2001-01-06 00:46:47 +00:00
|
|
|
*/
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
sap = (struct sockaddr_ng *) nam;
|
2006-10-17 12:21:48 +00:00
|
|
|
/* The item will hold the node reference. */
|
2005-05-16 17:09:35 +00:00
|
|
|
item = ng_package_data(NULL, NG_WAITOK);
|
2006-10-17 12:21:48 +00:00
|
|
|
|
2002-08-22 00:30:03 +00:00
|
|
|
if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
|
2001-01-06 00:46:47 +00:00
|
|
|
return (error); /* item is freed on failure */
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
/*
|
2001-01-08 05:34:06 +00:00
|
|
|
* Extract node from item and free item. Remember we now have
|
2001-01-06 00:46:47 +00:00
|
|
|
* a reference on the node. The item holds it for us.
|
|
|
|
* when we free the item we release the reference.
|
|
|
|
*/
|
|
|
|
farnode = item->el_dest; /* shortcut */
|
2001-01-08 05:34:06 +00:00
|
|
|
if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item); /* drop the reference to the node */
|
1999-10-21 09:06:11 +00:00
|
|
|
return (EINVAL);
|
2001-01-06 00:46:47 +00:00
|
|
|
}
|
2001-02-05 18:57:11 +00:00
|
|
|
priv = NG_NODE_PRIVATE(farnode);
|
|
|
|
if (priv->datasock != NULL) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item); /* drop the reference to the node */
|
1999-10-21 09:06:11 +00:00
|
|
|
return (EADDRINUSE);
|
2001-01-06 00:46:47 +00:00
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
/*
|
|
|
|
* Link the PCB and the private data struct. and note the extra
|
|
|
|
* reference. Drop the extra reference on the node.
|
|
|
|
*/
|
2005-11-02 15:34:42 +00:00
|
|
|
mtx_lock(&priv->mtx);
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock = pcbp;
|
|
|
|
pcbp->sockdata = priv;
|
2012-01-23 15:39:45 +00:00
|
|
|
pcbp->node_id = priv->node->nd_ID; /* hint for netstat(1) */
|
2005-11-02 15:34:42 +00:00
|
|
|
priv->refs++;
|
|
|
|
mtx_unlock(&priv->mtx);
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item); /* drop the reference to the node */
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Binding a socket means giving the corresponding node a name
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
|
|
|
|
{
|
2001-02-05 18:57:11 +00:00
|
|
|
struct ngsock *const priv = pcbp->sockdata;
|
1999-10-21 09:06:11 +00:00
|
|
|
struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
|
|
|
|
|
2001-02-05 18:57:11 +00:00
|
|
|
if (priv == NULL) {
|
1999-10-21 09:06:11 +00:00
|
|
|
TRAP_ERROR;
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2006-10-17 12:21:48 +00:00
|
|
|
if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
|
|
|
|
(sap->sg_data[0] == '\0') ||
|
|
|
|
(sap->sg_data[sap->sg_len - 3] != '\0')) {
|
1999-10-21 09:06:11 +00:00
|
|
|
TRAP_ERROR;
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2001-02-05 18:57:11 +00:00
|
|
|
return (ng_name_node(priv->node, sap->sg_data));
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2005-07-05 17:35:20 +00:00
|
|
|
/***************************************************************
|
|
|
|
Netgraph node
|
|
|
|
***************************************************************/
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* You can only create new nodes from the socket end of things.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ngs_constructor(node_p nodep)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-01-23 16:43:13 +00:00
|
|
|
static void
|
|
|
|
ngs_rehash(node_p node)
|
|
|
|
{
|
|
|
|
struct ngsock *priv = NG_NODE_PRIVATE(node);
|
|
|
|
struct ngshash *new;
|
|
|
|
struct hookpriv *hp;
|
|
|
|
hook_p hook;
|
|
|
|
uint32_t h;
|
|
|
|
u_long hmask;
|
|
|
|
|
|
|
|
new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
|
|
|
|
HASH_NOWAIT);
|
|
|
|
if (new == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
|
|
|
|
hp = NG_HOOK_PRIVATE(hook);
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
LIST_REMOVE(hp, next);
|
|
|
|
#endif
|
|
|
|
h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
|
|
|
|
LIST_INSERT_HEAD(&new[h], hp, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
|
|
|
|
priv->hash = new;
|
|
|
|
priv->hmask = hmask;
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* We allow any hook to be connected to the node.
|
|
|
|
* There is no per-hook private information though.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ngs_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
2012-01-23 16:43:13 +00:00
|
|
|
struct ngsock *const priv = NG_NODE_PRIVATE(node);
|
|
|
|
struct hookpriv *hp;
|
|
|
|
uint32_t h;
|
|
|
|
|
|
|
|
hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
|
|
|
|
if (hp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
if (node->nd_numhooks * 2 > priv->hmask)
|
|
|
|
ngs_rehash(node);
|
|
|
|
hp->hook = hook;
|
|
|
|
h = hash32_str(name, HASHINIT) & priv->hmask;
|
|
|
|
LIST_INSERT_HEAD(&priv->hash[h], hp, next);
|
|
|
|
NG_HOOK_SET_PRIVATE(hook, hp);
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/*
|
|
|
|
* If only one hook, allow read(2) and write(2) to work.
|
2001-02-05 18:57:11 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ngs_connect(hook_p hook)
|
|
|
|
{
|
|
|
|
node_p node = NG_HOOK_NODE(hook);
|
|
|
|
struct ngsock *priv = NG_NODE_PRIVATE(node);
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
if ((priv->datasock) && (priv->datasock->ng_socket)) {
|
|
|
|
if (NG_NODE_NUMHOOKS(node) == 1)
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
|
2006-10-17 12:21:48 +00:00
|
|
|
else
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-01-23 16:43:13 +00:00
|
|
|
/* Look up hook by name */
|
|
|
|
static hook_p
|
|
|
|
ngs_findhook(node_p node, const char *name)
|
|
|
|
{
|
|
|
|
struct ngsock *priv = NG_NODE_PRIVATE(node);
|
|
|
|
struct hookpriv *hp;
|
|
|
|
uint32_t h;
|
|
|
|
|
|
|
|
/*
|
2012-02-13 13:07:56 +00:00
|
|
|
* Microoptimisation for an ng_socket with
|
|
|
|
* a single hook, which is a common case.
|
2012-01-23 16:43:13 +00:00
|
|
|
*/
|
|
|
|
if (node->nd_numhooks == 1) {
|
|
|
|
hook_p hook;
|
|
|
|
|
|
|
|
hook = LIST_FIRST(&node->nd_hooks);
|
|
|
|
|
|
|
|
if (strcmp(NG_HOOK_NAME(hook), name) == 0)
|
|
|
|
return (hook);
|
|
|
|
else
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
h = hash32_str(name, HASHINIT) & priv->hmask;
|
|
|
|
|
|
|
|
LIST_FOREACH(hp, &priv->hash[h], next)
|
|
|
|
if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
|
|
|
|
return (hp->hook);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* Incoming messages get passed up to the control socket.
|
1999-11-05 02:18:08 +00:00
|
|
|
* Unless they are for us specifically (socket_type)
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-02-05 18:57:11 +00:00
|
|
|
struct ngsock *const priv = NG_NODE_PRIVATE(node);
|
2010-05-19 15:06:09 +00:00
|
|
|
struct ngpcb *pcbp;
|
2006-10-18 07:47:07 +00:00
|
|
|
struct socket *so;
|
2006-10-17 16:52:09 +00:00
|
|
|
struct sockaddr_ng addr;
|
|
|
|
struct ng_mesg *msg;
|
|
|
|
struct mbuf *m;
|
|
|
|
ng_ID_t retaddr = NGI_RETADDR(item);
|
1999-10-21 09:06:11 +00:00
|
|
|
int addrlen;
|
|
|
|
int error = 0;
|
2001-01-06 00:46:47 +00:00
|
|
|
|
|
|
|
NGI_GET_MSG(item, msg);
|
2006-10-17 16:52:09 +00:00
|
|
|
NG_FREE_ITEM(item);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2010-05-19 15:06:09 +00:00
|
|
|
/*
|
|
|
|
* Grab priv->mtx here to prevent destroying of control socket
|
|
|
|
* after checking that priv->ctlsock is not NULL.
|
|
|
|
*/
|
|
|
|
mtx_lock(&priv->mtx);
|
|
|
|
pcbp = priv->ctlsock;
|
|
|
|
|
2006-10-17 16:52:09 +00:00
|
|
|
/*
|
|
|
|
* Only allow mesgs to be passed if we have the control socket.
|
|
|
|
* Data sockets can only support the generic messages.
|
|
|
|
*/
|
1999-10-21 09:06:11 +00:00
|
|
|
if (pcbp == NULL) {
|
2010-05-19 15:06:09 +00:00
|
|
|
mtx_unlock(&priv->mtx);
|
1999-10-21 09:06:11 +00:00
|
|
|
TRAP_ERROR;
|
2006-10-17 16:52:09 +00:00
|
|
|
NG_FREE_MSG(msg);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2006-10-18 07:47:07 +00:00
|
|
|
so = pcbp->ng_socket;
|
2010-05-19 15:06:09 +00:00
|
|
|
SOCKBUF_LOCK(&so->so_rcv);
|
|
|
|
|
|
|
|
/* As long as the race is handled, priv->mtx may be unlocked now. */
|
|
|
|
mtx_unlock(&priv->mtx);
|
2001-01-08 05:34:06 +00:00
|
|
|
|
2005-05-16 18:50:26 +00:00
|
|
|
#ifdef TRACE_MESSAGES
|
|
|
|
printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
|
|
|
|
retaddr,
|
|
|
|
msg->header.typecookie,
|
|
|
|
msg->header.cmd,
|
|
|
|
msg->header.cmdstr,
|
|
|
|
msg->header.flags,
|
|
|
|
msg->header.token);
|
2001-01-08 05:34:06 +00:00
|
|
|
#endif
|
1999-10-21 09:06:11 +00:00
|
|
|
|
1999-11-05 02:18:08 +00:00
|
|
|
if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_SOCK_CMD_NOLINGER:
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->flags |= NGS_FLAG_NOLINGER;
|
1999-11-05 02:18:08 +00:00
|
|
|
break;
|
|
|
|
case NGM_SOCK_CMD_LINGER:
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->flags &= ~NGS_FLAG_NOLINGER;
|
1999-11-05 02:18:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL; /* unknown command */
|
|
|
|
}
|
2010-05-19 15:06:09 +00:00
|
|
|
SOCKBUF_UNLOCK(&so->so_rcv);
|
|
|
|
|
2006-10-17 16:52:09 +00:00
|
|
|
/* Free the message and return. */
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_MSG(msg);
|
2006-10-17 16:52:09 +00:00
|
|
|
return (error);
|
|
|
|
}
|
1999-11-05 02:18:08 +00:00
|
|
|
|
2006-10-17 16:52:09 +00:00
|
|
|
/* Get the return address into a sockaddr. */
|
|
|
|
bzero(&addr, sizeof(addr));
|
|
|
|
addr.sg_len = sizeof(addr);
|
|
|
|
addr.sg_family = AF_NETGRAPH;
|
|
|
|
addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
|
|
|
|
"[%x]:", retaddr);
|
|
|
|
if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
|
2010-05-19 15:06:09 +00:00
|
|
|
SOCKBUF_UNLOCK(&so->so_rcv);
|
2006-10-17 16:52:09 +00:00
|
|
|
printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
|
|
|
|
addrlen);
|
|
|
|
NG_FREE_MSG(msg);
|
|
|
|
return (EINVAL);
|
1999-11-05 02:18:08 +00:00
|
|
|
}
|
2006-10-17 16:52:09 +00:00
|
|
|
|
|
|
|
/* Copy the message itself into an mbuf chain. */
|
|
|
|
m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
|
|
|
|
0, NULL, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we free the message. We need to do that
|
|
|
|
* regardless of whether we got mbufs.
|
|
|
|
*/
|
|
|
|
NG_FREE_MSG(msg);
|
|
|
|
|
|
|
|
if (m == NULL) {
|
2010-05-19 15:06:09 +00:00
|
|
|
SOCKBUF_UNLOCK(&so->so_rcv);
|
1999-10-21 09:06:11 +00:00
|
|
|
TRAP_ERROR;
|
2006-10-17 16:52:09 +00:00
|
|
|
return (ENOBUFS);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2006-10-17 16:52:09 +00:00
|
|
|
/* Send it up to the socket. */
|
2010-05-19 15:06:09 +00:00
|
|
|
if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
|
|
|
|
NULL) == 0) {
|
|
|
|
SOCKBUF_UNLOCK(&so->so_rcv);
|
2006-10-17 16:52:09 +00:00
|
|
|
TRAP_ERROR;
|
|
|
|
m_freem(m);
|
2008-03-11 21:58:48 +00:00
|
|
|
return (ENOBUFS);
|
2006-10-17 16:52:09 +00:00
|
|
|
}
|
2010-05-19 15:06:09 +00:00
|
|
|
sorwakeup_locked(so);
|
2006-10-17 16:52:09 +00:00
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive data on a hook
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ngs_rcvdata(hook_p hook, item_p item)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-02-05 18:57:11 +00:00
|
|
|
struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
|
|
|
struct ngpcb *const pcbp = priv->datasock;
|
1999-10-21 09:06:11 +00:00
|
|
|
struct socket *so;
|
|
|
|
struct sockaddr_ng *addr;
|
2004-01-26 14:05:31 +00:00
|
|
|
char *addrbuf[NG_HOOKSIZ + 4];
|
1999-10-21 09:06:11 +00:00
|
|
|
int addrlen;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NGI_GET_M(item, m);
|
|
|
|
NG_FREE_ITEM(item);
|
2006-10-17 12:21:48 +00:00
|
|
|
|
|
|
|
/* If there is no data socket, black-hole it. */
|
1999-10-21 09:06:11 +00:00
|
|
|
if (pcbp == NULL) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_M(m);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
so = pcbp->ng_socket;
|
|
|
|
|
|
|
|
/* Get the return address into a sockaddr. */
|
2004-01-26 14:05:31 +00:00
|
|
|
addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
|
1999-10-21 09:06:11 +00:00
|
|
|
addr = (struct sockaddr_ng *) addrbuf;
|
|
|
|
addr->sg_len = addrlen + 3;
|
|
|
|
addr->sg_family = AF_NETGRAPH;
|
2001-01-08 05:34:06 +00:00
|
|
|
bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
|
1999-10-21 09:06:11 +00:00
|
|
|
addr->sg_data[addrlen] = '\0';
|
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
/* Try to tell the socket which hook it came in on. */
|
|
|
|
if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
|
1999-10-21 09:06:11 +00:00
|
|
|
m_freem(m);
|
|
|
|
TRAP_ERROR;
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
sorwakeup(so);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-11-05 02:18:08 +00:00
|
|
|
/*
|
1999-11-21 10:43:05 +00:00
|
|
|
* Hook disconnection
|
1999-11-05 02:18:08 +00:00
|
|
|
*
|
|
|
|
* For this type, removal of the last link destroys the node
|
|
|
|
* if the NOLINGER flag is set.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ngs_disconnect(hook_p hook)
|
|
|
|
{
|
2001-02-05 18:57:11 +00:00
|
|
|
node_p node = NG_HOOK_NODE(hook);
|
|
|
|
struct ngsock *const priv = NG_NODE_PRIVATE(node);
|
2012-01-23 16:43:13 +00:00
|
|
|
struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
|
|
|
|
|
|
|
|
LIST_REMOVE(hp, next);
|
|
|
|
free(hp, M_NETGRAPH_SOCK);
|
2001-02-05 18:57:11 +00:00
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
if ((priv->datasock) && (priv->datasock->ng_socket)) {
|
|
|
|
if (NG_NODE_NUMHOOKS(node) == 1)
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
|
2006-10-17 12:21:48 +00:00
|
|
|
else
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
|
|
|
|
}
|
1999-11-05 02:18:08 +00:00
|
|
|
|
2006-10-17 12:21:48 +00:00
|
|
|
if ((priv->flags & NGS_FLAG_NOLINGER) &&
|
|
|
|
(NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
|
2001-02-05 18:57:11 +00:00
|
|
|
ng_rmnode_self(node);
|
2006-10-17 12:21:48 +00:00
|
|
|
|
1999-11-05 02:18:08 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* Do local shutdown processing.
|
|
|
|
* In this case, that involves making sure the socket
|
|
|
|
* knows we should be shutting down.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ngs_shutdown(node_p node)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-02-05 18:57:11 +00:00
|
|
|
struct ngsock *const priv = NG_NODE_PRIVATE(node);
|
2010-05-19 15:06:09 +00:00
|
|
|
struct ngpcb *dpcbp, *pcbp;
|
|
|
|
|
|
|
|
mtx_lock(&priv->mtx);
|
|
|
|
dpcbp = priv->datasock;
|
|
|
|
pcbp = priv->ctlsock;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2005-11-02 15:34:42 +00:00
|
|
|
if (dpcbp != NULL)
|
1999-10-21 09:06:11 +00:00
|
|
|
soisdisconnected(dpcbp->ng_socket);
|
2005-11-02 15:34:42 +00:00
|
|
|
|
|
|
|
if (pcbp != NULL)
|
1999-10-21 09:06:11 +00:00
|
|
|
soisdisconnected(pcbp->ng_socket);
|
2005-11-02 15:34:42 +00:00
|
|
|
|
|
|
|
priv->node = NULL;
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
2005-11-02 15:34:42 +00:00
|
|
|
ng_socket_free_priv(priv);
|
|
|
|
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_UNREF(node);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-07-05 17:35:20 +00:00
|
|
|
static void
|
|
|
|
ng_socket_item_applied(void *context, int error)
|
|
|
|
{
|
|
|
|
struct ngsock *const priv = (struct ngsock *)context;
|
|
|
|
|
|
|
|
mtx_lock(&priv->mtx);
|
|
|
|
priv->error = error;
|
|
|
|
wakeup(priv);
|
|
|
|
mtx_unlock(&priv->mtx);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-02-05 20:48:04 +00:00
|
|
|
static int
|
|
|
|
dummy_disconnect(struct socket *so)
|
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* Control and data socket type descriptors
|
2006-07-21 17:11:15 +00:00
|
|
|
*
|
|
|
|
* XXXRW: Perhaps _close should do something?
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static struct pr_usrreqs ngc_usrreqs = {
|
2004-11-08 14:44:54 +00:00
|
|
|
.pru_abort = NULL,
|
|
|
|
.pru_attach = ngc_attach,
|
|
|
|
.pru_bind = ngc_bind,
|
|
|
|
.pru_connect = ngc_connect,
|
|
|
|
.pru_detach = ngc_detach,
|
|
|
|
.pru_disconnect = dummy_disconnect,
|
|
|
|
.pru_peeraddr = NULL,
|
|
|
|
.pru_send = ngc_send,
|
|
|
|
.pru_shutdown = NULL,
|
2007-05-11 10:20:51 +00:00
|
|
|
.pru_sockaddr = ng_getsockaddr,
|
2006-07-21 17:11:15 +00:00
|
|
|
.pru_close = NULL,
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct pr_usrreqs ngd_usrreqs = {
|
2004-11-08 14:44:54 +00:00
|
|
|
.pru_abort = NULL,
|
|
|
|
.pru_attach = ngd_attach,
|
|
|
|
.pru_bind = NULL,
|
|
|
|
.pru_connect = ngd_connect,
|
|
|
|
.pru_detach = ngd_detach,
|
|
|
|
.pru_disconnect = dummy_disconnect,
|
|
|
|
.pru_peeraddr = NULL,
|
|
|
|
.pru_send = ngd_send,
|
|
|
|
.pru_shutdown = NULL,
|
2007-05-11 10:20:51 +00:00
|
|
|
.pru_sockaddr = ng_getsockaddr,
|
2006-07-21 17:11:15 +00:00
|
|
|
.pru_close = NULL,
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Definitions of protocols supported in the NETGRAPH domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern struct domain ngdomain; /* stop compiler warnings */
|
|
|
|
|
|
|
|
static struct protosw ngsw[] = {
|
2005-11-09 13:29:16 +00:00
|
|
|
{
|
|
|
|
.pr_type = SOCK_DGRAM,
|
|
|
|
.pr_domain = &ngdomain,
|
|
|
|
.pr_protocol = NG_CONTROL,
|
|
|
|
.pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
|
|
|
|
.pr_usrreqs = &ngc_usrreqs
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.pr_type = SOCK_DGRAM,
|
|
|
|
.pr_domain = &ngdomain,
|
|
|
|
.pr_protocol = NG_DATA,
|
|
|
|
.pr_flags = PR_ATOMIC | PR_ADDR,
|
|
|
|
.pr_usrreqs = &ngd_usrreqs
|
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct domain ngdomain = {
|
2005-11-09 13:29:16 +00:00
|
|
|
.dom_family = AF_NETGRAPH,
|
|
|
|
.dom_name = "netgraph",
|
|
|
|
.dom_protosw = ngsw,
|
|
|
|
.dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2006-10-17 12:21:48 +00:00
|
|
|
* Handle loading and unloading for this node type.
|
1999-10-21 09:06:11 +00:00
|
|
|
* This is to handle auxiliary linkages (e.g protocol domain addition).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ngs_mod_event(module_t mod, int event, void *data)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case MOD_LOAD:
|
2010-03-12 14:51:42 +00:00
|
|
|
mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
2010-03-12 14:51:42 +00:00
|
|
|
/* Ensure there are no open netgraph sockets. */
|
|
|
|
if (!LIST_EMPTY(&ngsocklist)) {
|
|
|
|
error = EBUSY;
|
|
|
|
break;
|
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
#ifdef NOTYET
|
|
|
|
/* Unregister protocol domain XXX can't do this yet.. */
|
|
|
|
#endif
|
Introduce and use a sysinit-based initialization scheme for virtual
network stacks, VNET_SYSINIT:
- Add VNET_SYSINIT and VNET_SYSUNINIT macros to declare events that will
occur each time a network stack is instantiated and destroyed. In the
!VIMAGE case, these are simply mapped into regular SYSINIT/SYSUNINIT.
For the VIMAGE case, we instead use SYSINIT's to track their order and
properties on registration, using them for each vnet when created/
destroyed, or immediately on module load for already-started vnets.
- Remove vnet_modinfo mechanism that existed to serve this purpose
previously, as well as its dependency scheme: we now just use the
SYSINIT ordering scheme.
- Implement VNET_DOMAIN_SET() to allow protocol domains to declare that
they want init functions to be called for each virtual network stack
rather than just once at boot, compiling down to DOMAIN_SET() in the
non-VIMAGE case.
- Walk all virtualized kernel subsystems and make use of these instead
of modinfo or DOMAIN_SET() for init/uninit events. In some cases,
convert modular components from using modevent to using sysinit (where
appropriate). In some cases, do minor rejuggling of SYSINIT ordering
to make room for or better manage events.
Portions submitted by: jhb (VNET_SYSINIT), bz (cleanup)
Discussed with: jhb, bz, julian, zec
Reviewed by: bz
Approved by: re (VIMAGE blanket)
2009-07-23 20:46:49 +00:00
|
|
|
error = EBUSY;
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
Introduce and use a sysinit-based initialization scheme for virtual
network stacks, VNET_SYSINIT:
- Add VNET_SYSINIT and VNET_SYSUNINIT macros to declare events that will
occur each time a network stack is instantiated and destroyed. In the
!VIMAGE case, these are simply mapped into regular SYSINIT/SYSUNINIT.
For the VIMAGE case, we instead use SYSINIT's to track their order and
properties on registration, using them for each vnet when created/
destroyed, or immediately on module load for already-started vnets.
- Remove vnet_modinfo mechanism that existed to serve this purpose
previously, as well as its dependency scheme: we now just use the
SYSINIT ordering scheme.
- Implement VNET_DOMAIN_SET() to allow protocol domains to declare that
they want init functions to be called for each virtual network stack
rather than just once at boot, compiling down to DOMAIN_SET() in the
non-VIMAGE case.
- Walk all virtualized kernel subsystems and make use of these instead
of modinfo or DOMAIN_SET() for init/uninit events. In some cases,
convert modular components from using modevent to using sysinit (where
appropriate). In some cases, do minor rejuggling of SYSINIT ordering
to make room for or better manage events.
Portions submitted by: jhb (VNET_SYSINIT), bz (cleanup)
Discussed with: jhb, bz, julian, zec
Reviewed by: bz
Approved by: re (VIMAGE blanket)
2009-07-23 20:46:49 +00:00
|
|
|
VNET_DOMAIN_SET(ng);
|
|
|
|
|
2014-10-21 07:31:21 +00:00
|
|
|
SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, AF_NETGRAPH, "");
|
2011-11-07 15:43:11 +00:00
|
|
|
static SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
|
2014-10-21 07:31:21 +00:00
|
|
|
SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_DATA, "");
|
2011-11-07 15:43:11 +00:00
|
|
|
static SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
|
2014-10-21 07:31:21 +00:00
|
|
|
SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_CONTROL, "");
|
1999-10-21 09:06:11 +00:00
|
|
|
|