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>
|
|
|
|
#include <sys/errno.h>
|
2004-07-10 21:45:58 +00:00
|
|
|
#include <sys/kdb.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>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/protosw.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/signalvar.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/sx.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/sysctl.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/systm.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#ifdef NOTYET
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#endif
|
|
|
|
#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
|
|
|
|
MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
|
|
|
|
MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
|
|
|
|
#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;
|
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);
|
|
|
|
static void ng_detach_common(struct ngpcb *pcbp, int type);
|
2001-09-12 08:38:13 +00:00
|
|
|
/*static int ng_internalize(struct mbuf *m, struct thread *p); */
|
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);
|
|
|
|
static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
|
|
|
|
struct sockaddr_ng *addr);
|
|
|
|
|
|
|
|
/* 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,
|
|
|
|
.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 */
|
2004-01-23 14:35:44 +00:00
|
|
|
SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
|
|
|
|
&ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
|
1999-10-21 09:06:11 +00:00
|
|
|
static u_long ngpdg_recvspace = 20 * 1024;
|
2004-01-23 14:35:44 +00:00
|
|
|
SYSCTL_INT(_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
|
|
|
|
|
|
|
/* List of all sockets */
|
2002-01-08 10:30:34 +00:00
|
|
|
static LIST_HEAD(, ngpcb) ngsocklist;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2004-07-12 04:45:46 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
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);
|
|
|
|
|
2002-04-01 21:31:13 +00:00
|
|
|
if (suser(td))
|
1999-11-21 23:06:30 +00:00
|
|
|
return (EPERM);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (pcbp != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
return (ng_attach_cntl(so));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ngc_detach(struct socket *so)
|
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
|
|
|
|
if (pcbp == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
ng_detach_common(pcbp, NG_CONTROL);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
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;
|
2001-01-06 00:46:47 +00:00
|
|
|
char *path = NULL;
|
1999-10-21 09:06:11 +00:00
|
|
|
int len, error = 0;
|
|
|
|
|
|
|
|
if (pcbp == NULL) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
#ifdef NOTYET
|
2001-09-12 08:38:13 +00:00
|
|
|
if (control && (error = ng_internalize(control, td))) {
|
1999-10-21 09:06:11 +00:00
|
|
|
if (pcbp->sockdata == NULL) {
|
|
|
|
error = ENOTCONN;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* NOTYET */
|
|
|
|
if (control) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
#endif /* NOTYET */
|
|
|
|
|
|
|
|
/* Require destination as there may be >= 1 hooks on this node */
|
|
|
|
if (addr == NULL) {
|
|
|
|
error = EDESTADDRREQ;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate an expendable buffer for the path, chop off
|
|
|
|
* the sockaddr header, and make sure it's NUL terminated */
|
|
|
|
len = sap->sg_len - 2;
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(path, char *, len + 1, M_NETGRAPH_PATH, M_WAITOK);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (path == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
bcopy(sap->sg_data, path, len);
|
|
|
|
path[len] = '\0';
|
|
|
|
|
|
|
|
/* Move the actual message out of mbufs into a linear buffer.
|
|
|
|
* Start by adding up the size of the data. (could use mh_len?) */
|
|
|
|
for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
|
|
|
|
len += m0->m_len;
|
|
|
|
|
|
|
|
/* Move the data into a linear buffer as well. Messages are not
|
|
|
|
* delivered in mbufs. */
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(msg, struct ng_mesg *, len + 1, M_NETGRAPH_MSG, M_WAITOK);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (msg == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto release;
|
|
|
|
}
|
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) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
2001-01-08 05:34:06 +00:00
|
|
|
#ifdef TRACE_MESSAGES
|
|
|
|
do {
|
|
|
|
item_p item;
|
|
|
|
if ((item = ng_package_msg(msg)) == NULL) {
|
|
|
|
(msg) = NULL;
|
|
|
|
(error) = ENOMEM;
|
|
|
|
printf("err=%d\n",error);
|
|
|
|
break;
|
|
|
|
}
|
2005-02-04 16:08:20 +00:00
|
|
|
if ((error = ng_address_path((pcbp->sockdata->node), (item),
|
|
|
|
(path), 0)) == 0) {
|
2001-01-08 05:34:06 +00:00
|
|
|
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);
|
|
|
|
SAVE_LINE(item);
|
|
|
|
(error) = ng_snd_item((item), 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("errx=%d\n",error);
|
|
|
|
}
|
|
|
|
(msg) = NULL;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
#else
|
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;
|
|
|
|
struct ng_type *type;
|
|
|
|
|
|
|
|
if ((type = ng_findtype(mkp->type)) == NULL) {
|
|
|
|
char filename[NG_TYPESIZ + 3];
|
|
|
|
linker_file_t lf;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Not found, try to load it as a loadable module */
|
|
|
|
snprintf(filename, sizeof(filename), "ng_%s", mkp->type);
|
2004-08-30 14:41:25 +00:00
|
|
|
mtx_lock(&Giant);
|
2004-07-27 20:30:56 +00:00
|
|
|
error = linker_load_module(NULL, filename, NULL, NULL, &lf);
|
2004-08-30 14:41:25 +00:00
|
|
|
mtx_unlock(&Giant);
|
2004-07-27 20:30:56 +00:00
|
|
|
if (error != 0) {
|
|
|
|
FREE(msg, M_NETGRAPH_MSG);
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
lf->userrefs++;
|
|
|
|
|
|
|
|
/* Try again, as now the type should have linked itself in */
|
|
|
|
if ((type = ng_findtype(mkp->type)) == NULL) {
|
|
|
|
FREE(msg, M_NETGRAPH_MSG);
|
|
|
|
error = ENXIO;
|
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
/* The callee will free the msg when done. The path is our business. */
|
2002-08-22 00:30:03 +00:00
|
|
|
NG_SEND_MSG_PATH(error, pcbp->sockdata->node, msg, path, 0);
|
2001-01-08 05:34:06 +00:00
|
|
|
#endif
|
1999-10-21 09:06:11 +00:00
|
|
|
release:
|
|
|
|
if (path != NULL)
|
2001-01-10 07:13:58 +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-08 05:34:06 +00:00
|
|
|
printf(" program tried to connect control socket to remote node\n ");
|
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.
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ngd_detach(struct socket *so)
|
|
|
|
{
|
|
|
|
struct ngpcb *const pcbp = sotongpcb(so);
|
|
|
|
|
|
|
|
if (pcbp == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
ng_detach_common(pcbp, NG_DATA);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
1999-11-21 10:43:05 +00:00
|
|
|
/*
|
|
|
|
* If the user used any of these ways to not specify an address
|
|
|
|
* then handle specially.
|
|
|
|
*/
|
|
|
|
if ((sap == NULL)
|
2001-10-10 19:58:11 +00:00
|
|
|
|| ((len = sap->sg_len - 2) <= 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;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if exactly one hook exists, just use it.
|
|
|
|
* 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' */
|
2001-01-08 05:34:06 +00:00
|
|
|
LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) {
|
2001-02-05 18:57:11 +00:00
|
|
|
if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) {
|
1999-11-21 10:43:05 +00:00
|
|
|
break;
|
2001-02-05 18:57:11 +00:00
|
|
|
}
|
1999-11-21 10:43:05 +00:00
|
|
|
}
|
2001-02-05 18:57:11 +00:00
|
|
|
if (hook == NULL) {
|
1999-11-21 10:43:05 +00:00
|
|
|
error = EHOSTUNREACH;
|
2001-02-05 18:57:11 +00:00
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send data (OK if hook is NULL) */
|
2000-12-12 18:52:14 +00:00
|
|
|
NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */
|
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
|
|
|
|
ng_setsockaddr(struct socket *so, struct sockaddr **addr)
|
|
|
|
{
|
1999-11-11 20:08:04 +00:00
|
|
|
struct ngpcb *pcbp;
|
|
|
|
struct sockaddr_ng *sg;
|
|
|
|
int sg_len, namelen, s;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
1999-11-11 20:08:04 +00:00
|
|
|
/* Why isn't sg_data a `char[1]' ? :-( */
|
|
|
|
sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
|
|
|
|
|
|
|
|
s = splnet();
|
|
|
|
pcbp = sotongpcb(so);
|
2000-12-12 18:52:14 +00:00
|
|
|
if ((pcbp == NULL) || (pcbp->sockdata == NULL)) {
|
1999-11-11 20:08:04 +00:00
|
|
|
splx(s);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (EINVAL);
|
1999-11-11 20:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namelen = 0; /* silence compiler ! */
|
2001-01-08 05:34:06 +00:00
|
|
|
if ( NG_NODE_HAS_NAME(pcbp->sockdata->node))
|
|
|
|
sg_len += namelen = strlen(NG_NODE_NAME(pcbp->sockdata->node));
|
1999-11-11 20:08:04 +00:00
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK | M_ZERO);
|
1999-11-11 20:08:04 +00:00
|
|
|
|
2001-01-08 05:34:06 +00:00
|
|
|
if (NG_NODE_HAS_NAME(pcbp->sockdata->node))
|
|
|
|
bcopy(NG_NODE_NAME(pcbp->sockdata->node), sg->sg_data, namelen);
|
1999-11-11 20:08:04 +00:00
|
|
|
splx(s);
|
|
|
|
|
|
|
|
sg->sg_len = sg_len;
|
|
|
|
sg->sg_family = AF_NETGRAPH;
|
|
|
|
*addr = (struct sockaddr *)sg;
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
struct ngsock *privdata;
|
|
|
|
struct ngpcb *pcbp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Setup protocol control block */
|
|
|
|
if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
|
|
|
|
return (error);
|
1999-11-21 22:18:54 +00:00
|
|
|
pcbp = sotongpcb(so);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Allocate node private info */
|
|
|
|
MALLOC(privdata, struct ngsock *,
|
2003-02-19 05:47:46 +00:00
|
|
|
sizeof(*privdata), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (privdata == NULL) {
|
|
|
|
ng_detach_common(pcbp, NG_CONTROL);
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the generic node components */
|
|
|
|
if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(privdata, M_NETGRAPH_SOCK);
|
1999-10-21 09:06:11 +00:00
|
|
|
ng_detach_common(pcbp, NG_CONTROL);
|
|
|
|
return (error);
|
|
|
|
}
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(privdata->node, privdata);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Link the pcb and the node private data */
|
|
|
|
privdata->ctlsock = pcbp;
|
|
|
|
pcbp->sockdata = privdata;
|
|
|
|
privdata->refs++;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ng_attach_data(struct socket *so)
|
|
|
|
{
|
|
|
|
return(ng_attach_common(so, NG_DATA));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/* Standard socket setup stuff */
|
|
|
|
error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
/* Allocate the pcb */
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (pcbp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
pcbp->type = type;
|
|
|
|
|
|
|
|
/* Link the pcb and the socket */
|
|
|
|
so->so_pcb = (caddr_t) pcbp;
|
|
|
|
pcbp->ng_socket = so;
|
|
|
|
|
|
|
|
/* Add the socket to linked list */
|
2004-07-12 04:45:46 +00:00
|
|
|
mtx_lock(&ngsocketlist_mtx);
|
1999-10-21 09:06:11 +00:00
|
|
|
LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
|
2004-07-12 04:45:46 +00:00
|
|
|
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
|
|
|
|
ng_detach_common(struct ngpcb *pcbp, int which)
|
|
|
|
{
|
2001-02-05 18:57:11 +00:00
|
|
|
struct ngsock *priv;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
if (pcbp->sockdata) {
|
2001-02-05 18:57:11 +00:00
|
|
|
priv = pcbp->sockdata;
|
1999-10-21 09:06:11 +00:00
|
|
|
pcbp->sockdata = NULL;
|
|
|
|
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:
|
2001-12-10 08:09:49 +00:00
|
|
|
panic(__func__);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
2001-02-05 18:57:11 +00:00
|
|
|
if ((--priv->refs == 0) && (priv->node != NULL))
|
|
|
|
ng_rmnode_self(priv->node);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
pcbp->ng_socket->so_pcb = NULL;
|
|
|
|
pcbp->ng_socket = NULL;
|
2004-07-12 04:45:46 +00:00
|
|
|
mtx_lock(&ngsocketlist_mtx);
|
1999-10-21 09:06:11 +00:00
|
|
|
LIST_REMOVE(pcbp, socks);
|
2004-07-12 04:45:46 +00:00
|
|
|
mtx_unlock(&ngsocketlist_mtx);
|
1999-10-21 09:06:11 +00:00
|
|
|
FREE(pcbp, M_PCB);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NOTYET
|
|
|
|
/*
|
2003-01-01 18:49:04 +00:00
|
|
|
* File descriptors can be passed into an AF_NETGRAPH socket.
|
1999-10-21 09:06:11 +00:00
|
|
|
* Note, that file descriptors cannot be passed OUT.
|
|
|
|
* Only character device descriptors are accepted.
|
|
|
|
* Character devices are useful to connect a graph to a device,
|
|
|
|
* which after all is the purpose of this whole system.
|
|
|
|
*/
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
ng_internalize(struct mbuf *control, struct thread *td)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2002-06-05 23:35:31 +00:00
|
|
|
const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
|
1999-10-21 09:06:11 +00:00
|
|
|
struct file *fp;
|
|
|
|
struct vnode *vn;
|
|
|
|
int oldfds;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
|
|
|
|
cm->cmsg_len != control->m_len) {
|
|
|
|
TRAP_ERROR;
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check there is only one FD. XXX what would more than one signify? */
|
2001-10-04 13:11:48 +00:00
|
|
|
oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (oldfds != 1) {
|
|
|
|
TRAP_ERROR;
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the FD given is legit. and change it to a pointer to a
|
|
|
|
* struct file. */
|
2001-10-04 13:11:48 +00:00
|
|
|
fd = CMSG_DATA(cm);
|
2002-01-14 00:13:45 +00:00
|
|
|
if ((error = fget(td, fd, &fp)) != 0)
|
|
|
|
return (error);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Depending on what kind of resource it is, act differently. For
|
2003-01-01 18:49:04 +00:00
|
|
|
* devices, we treat it as a file. For an AF_NETGRAPH socket,
|
1999-10-21 09:06:11 +00:00
|
|
|
* shortcut straight to the node. */
|
|
|
|
switch (fp->f_type) {
|
|
|
|
case DTYPE_VNODE:
|
2003-01-13 00:33:17 +00:00
|
|
|
vn = fp->f_data;
|
1999-10-21 09:06:11 +00:00
|
|
|
if (vn && (vn->v_type == VCHR)) {
|
|
|
|
/* for a VCHR, actually reference the FILE */
|
|
|
|
fp->f_count++;
|
|
|
|
/* XXX then what :) */
|
|
|
|
/* how to pass on to other modules? */
|
|
|
|
} else {
|
2002-01-13 11:58:06 +00:00
|
|
|
fdrop(fp, td);
|
1999-10-21 09:06:11 +00:00
|
|
|
TRAP_ERROR;
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-01-13 11:58:06 +00:00
|
|
|
fdrop(fp, td);
|
1999-10-21 09:06:11 +00:00
|
|
|
TRAP_ERROR;
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-01-13 11:58:06 +00:00
|
|
|
fdrop(fp, td);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif /* NOTYET */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
|
|
/* If we are already connected, don't do it again */
|
|
|
|
if (pcbp->sockdata != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
|
|
|
|
/* Find the target (victim) and check it doesn't already have a data
|
2001-01-06 00:46:47 +00:00
|
|
|
* socket. Also check it is a 'socket' type node.
|
|
|
|
* Use ng_package_data() and address_path() to do this.
|
|
|
|
*/
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
sap = (struct sockaddr_ng *) nam;
|
2001-01-06 00:46:47 +00:00
|
|
|
/* The item will hold the node reference */
|
|
|
|
item = ng_package_data(NULL, NULL);
|
|
|
|
if (item == NULL) {
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock = pcbp;
|
|
|
|
pcbp->sockdata = priv;
|
|
|
|
priv->refs++; /* XXX possible race if it's being freed */
|
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);
|
|
|
|
}
|
2000-12-12 18:52:14 +00:00
|
|
|
if ((sap->sg_len < 4)
|
2004-01-26 14:05:31 +00:00
|
|
|
|| (sap->sg_len > (NG_NODESIZ + 2))
|
2000-12-12 18:52:14 +00:00
|
|
|
|| (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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a message and pass it up to the control socket associated
|
|
|
|
* with the node.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
|
|
|
|
{
|
|
|
|
struct socket *const so = pcbp->ng_socket;
|
|
|
|
struct mbuf *mdata;
|
|
|
|
int msglen;
|
|
|
|
|
|
|
|
/* Copy the message itself into an mbuf chain */
|
|
|
|
msglen = sizeof(struct ng_mesg) + msg->header.arglen;
|
|
|
|
mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
|
|
|
|
|
|
|
|
/* Here we free the message, as we are the end of the line.
|
|
|
|
* We need to do that regardless of whether we got mbufs. */
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_MSG(msg);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
if (mdata == NULL) {
|
|
|
|
TRAP_ERROR;
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send it up to the socket */
|
|
|
|
if (sbappendaddr(&so->so_rcv,
|
|
|
|
(struct sockaddr *) addr, mdata, NULL) == 0) {
|
|
|
|
TRAP_ERROR;
|
|
|
|
m_freem(mdata);
|
|
|
|
return (ENOBUFS);
|
|
|
|
}
|
|
|
|
sorwakeup(so);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-02-05 18:57:11 +00:00
|
|
|
/*
|
|
|
|
* if only one hook, allow read(2) and write(2) to work.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ngs_connect(hook_p hook)
|
|
|
|
{
|
|
|
|
node_p node = NG_HOOK_NODE(hook);
|
|
|
|
struct ngsock *priv = NG_NODE_PRIVATE(node);
|
|
|
|
|
|
|
|
if ((priv->datasock)
|
|
|
|
&& (priv->datasock->ng_socket)) {
|
|
|
|
if (NG_NODE_NUMHOOKS(node) == 1) {
|
|
|
|
priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
|
|
|
|
} else {
|
|
|
|
priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
struct ngpcb *const pcbp = priv->ctlsock;
|
1999-10-21 09:06:11 +00:00
|
|
|
struct sockaddr_ng *addr;
|
|
|
|
int addrlen;
|
|
|
|
int error = 0;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
|
|
|
ng_ID_t retaddr = NGI_RETADDR(item);
|
|
|
|
char retabuf[32];
|
|
|
|
|
|
|
|
NGI_GET_MSG(item, msg);
|
|
|
|
NG_FREE_ITEM(item); /* we have all we need */
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Only allow mesgs to be passed if we have the control socket.
|
|
|
|
* Data sockets can only support the generic messages. */
|
|
|
|
if (pcbp == NULL) {
|
|
|
|
TRAP_ERROR;
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2001-01-08 05:34:06 +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);
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
}
|
|
|
|
/* Free the message and return */
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_MSG(msg);
|
1999-11-05 02:18:08 +00:00
|
|
|
return(error);
|
|
|
|
|
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Get the return address into a sockaddr */
|
2001-01-06 00:46:47 +00:00
|
|
|
sprintf(retabuf,"[%x]:", retaddr);
|
|
|
|
addrlen = strlen(retabuf);
|
2001-01-10 07:13:58 +00:00
|
|
|
MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (addr == NULL) {
|
|
|
|
TRAP_ERROR;
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
addr->sg_len = addrlen + 3;
|
|
|
|
addr->sg_family = AF_NETGRAPH;
|
2001-01-06 00:46:47 +00:00
|
|
|
bcopy(retabuf, addr->sg_data, addrlen);
|
1999-10-21 09:06:11 +00:00
|
|
|
addr->sg_data[addrlen] = '\0';
|
|
|
|
|
|
|
|
/* Send it up */
|
|
|
|
error = ship_msg(pcbp, msg, addr);
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(addr, M_NETGRAPH_PATH);
|
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);
|
1999-10-21 09:06:11 +00:00
|
|
|
/* If there is no data socket, black-hole it */
|
|
|
|
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';
|
|
|
|
|
|
|
|
/* Try to tell the socket which hook it came in on */
|
|
|
|
if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if ((priv->datasock)
|
|
|
|
&& (priv->datasock->ng_socket)) {
|
|
|
|
if (NG_NODE_NUMHOOKS(node) == 1) {
|
|
|
|
priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
|
|
|
|
} else {
|
|
|
|
priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
|
|
|
|
}
|
|
|
|
}
|
1999-11-05 02:18:08 +00:00
|
|
|
|
2001-02-05 18:57:11 +00:00
|
|
|
if ((priv->flags & NGS_FLAG_NOLINGER )
|
|
|
|
&& (NG_NODE_NUMHOOKS(node) == 0)
|
|
|
|
&& (NG_NODE_IS_VALID(node))) {
|
|
|
|
ng_rmnode_self(node);
|
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);
|
|
|
|
struct ngpcb *const dpcbp = priv->datasock;
|
|
|
|
struct ngpcb *const pcbp = priv->ctlsock;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
if (dpcbp != NULL) {
|
|
|
|
soisdisconnected(dpcbp->ng_socket);
|
|
|
|
dpcbp->sockdata = NULL;
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->datasock = NULL;
|
|
|
|
priv->refs--;
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
if (pcbp != NULL) {
|
|
|
|
soisdisconnected(pcbp->ng_socket);
|
|
|
|
pcbp->sockdata = NULL;
|
2001-02-05 18:57:11 +00:00
|
|
|
priv->ctlsock = NULL;
|
|
|
|
priv->refs--;
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
|
|
|
NG_NODE_UNREF(node);
|
2001-02-05 18:57:11 +00:00
|
|
|
FREE(priv, M_NETGRAPH_SOCK);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
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,
|
|
|
|
.pru_sockaddr = ng_setsockaddr,
|
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,
|
|
|
|
.pru_sockaddr ng_setsockaddr,
|
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[] = {
|
|
|
|
{
|
2001-01-06 00:46:47 +00:00
|
|
|
SOCK_DGRAM, /* protocol type */
|
|
|
|
&ngdomain, /* backpointer to domain */
|
1999-10-21 09:06:11 +00:00
|
|
|
NG_CONTROL,
|
2001-01-06 00:46:47 +00:00
|
|
|
PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */, /* flags */
|
|
|
|
0, 0, 0, 0, /* input, output, ctlinput, ctloutput */
|
|
|
|
NULL, /* ousrreq */
|
|
|
|
0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */
|
|
|
|
&ngc_usrreqs, /* usrreq table (above) */
|
|
|
|
/*{NULL}*/ /* pffh (protocol filter head?) */
|
1999-10-21 09:06:11 +00:00
|
|
|
},
|
|
|
|
{
|
2001-01-06 00:46:47 +00:00
|
|
|
SOCK_DGRAM, /* protocol type */
|
|
|
|
&ngdomain, /* backpointer to domain */
|
1999-10-21 09:06:11 +00:00
|
|
|
NG_DATA,
|
2001-01-06 00:46:47 +00:00
|
|
|
PR_ATOMIC | PR_ADDR, /* flags */
|
|
|
|
0, 0, 0, 0, /* input, output, ctlinput, ctloutput */
|
|
|
|
NULL, /* ousrreq() */
|
|
|
|
0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */
|
|
|
|
&ngd_usrreqs, /* usrreq table (above) */
|
|
|
|
/*{NULL}*/ /* pffh (protocol filter head?) */
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct domain ngdomain = {
|
|
|
|
AF_NETGRAPH,
|
|
|
|
"netgraph",
|
2001-01-06 00:46:47 +00:00
|
|
|
NULL, /* init() */
|
|
|
|
NULL, /* externalise() */
|
|
|
|
NULL, /* dispose() */
|
|
|
|
ngsw, /* protosw entry */
|
|
|
|
&ngsw[sizeof(ngsw) / sizeof(ngsw[0])], /* Number of protosw entries */
|
|
|
|
NULL, /* next domain in list */
|
|
|
|
NULL, /* rtattach() */
|
|
|
|
0, /* arg to rtattach in bits */
|
|
|
|
0 /* maxrtkey */
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle loading and unloading for this node type
|
|
|
|
* 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:
|
2005-02-05 08:28:36 +00:00
|
|
|
mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Register protocol domain */
|
|
|
|
net_add_domain(&ngdomain);
|
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
/* Insure there are no open netgraph sockets */
|
|
|
|
if (!LIST_EMPTY(&ngsocklist)) {
|
|
|
|
error = EBUSY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NOTYET
|
2001-01-06 00:46:47 +00:00
|
|
|
if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) {
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Unregister protocol domain XXX can't do this yet.. */
|
2001-01-06 00:46:47 +00:00
|
|
|
if ((error = net_rm_domain(&ngdomain)) != 0)
|
|
|
|
break;
|
2005-02-05 08:28:36 +00:00
|
|
|
mtx_destroy(&ngsocketlist_mtx);
|
2001-01-06 00:46:47 +00:00
|
|
|
} else
|
1999-10-21 09:06:11 +00:00
|
|
|
#endif
|
2001-01-06 00:46:47 +00:00
|
|
|
error = EBUSY;
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
|
|
|
|
SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
|
|
|
|
SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
|
|
|
|
SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
|
|
|
|
SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");
|
|
|
|
|