6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Julian Elischer <julian@freebsd.org>
41 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
45 * Netgraph socket nodes
47 * There are two types of netgraph sockets, control and data.
48 * Control sockets have a netgraph node, but data sockets are
49 * parasitic on control sockets, and have no node of their own.
52 #include <sys/param.h>
53 #include <sys/domain.h>
55 #include <sys/kernel.h>
56 #include <sys/linker.h>
58 #include <sys/malloc.h>
60 #include <sys/mutex.h>
62 #include <sys/epoch.h>
64 #include <sys/protosw.h>
65 #include <sys/queue.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
73 #include <netgraph/ng_message.h>
74 #include <netgraph/netgraph.h>
75 #include <netgraph/ng_socketvar.h>
76 #include <netgraph/ng_socket.h>
78 #ifdef NG_SEPARATE_MALLOC
79 static MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info");
80 static MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info");
82 #define M_NETGRAPH_PATH M_NETGRAPH
83 #define M_NETGRAPH_SOCK M_NETGRAPH
87 * It's Ascii-art time!
88 * +-------------+ +-------------+
89 * |socket (ctl)| |socket (data)|
90 * +-------------+ +-------------+
94 * +-----------+ +-----------+
95 * |pcb (ctl)| |pcb (data)|
96 * +-----------+ +-----------+
100 * +--------------------------+
101 * | Socket type private |
103 * +--------------------------+
112 /* Netgraph node methods */
113 static ng_constructor_t ngs_constructor;
114 static ng_rcvmsg_t ngs_rcvmsg;
115 static ng_shutdown_t ngs_shutdown;
116 static ng_newhook_t ngs_newhook;
117 static ng_connect_t ngs_connect;
118 static ng_findhook_t ngs_findhook;
119 static ng_rcvdata_t ngs_rcvdata;
120 static ng_disconnect_t ngs_disconnect;
122 /* Internal methods */
123 static int ng_attach_data(struct socket *so);
124 static int ng_attach_cntl(struct socket *so);
125 static int ng_attach_common(struct socket *so, int type);
126 static void ng_detach_common(struct ngpcb *pcbp, int type);
127 static void ng_socket_free_priv(struct ngsock *priv);
128 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
129 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
131 static int ngs_mod_event(module_t mod, int event, void *data);
132 static void ng_socket_item_applied(void *context, int error);
134 /* Netgraph type descriptor */
135 static struct ng_type typestruct = {
136 .version = NG_ABI_VERSION,
137 .name = NG_SOCKET_NODE_TYPE,
138 .mod_event = ngs_mod_event,
139 .constructor = ngs_constructor,
140 .rcvmsg = ngs_rcvmsg,
141 .shutdown = ngs_shutdown,
142 .newhook = ngs_newhook,
143 .connect = ngs_connect,
144 .findhook = ngs_findhook,
145 .rcvdata = ngs_rcvdata,
146 .disconnect = ngs_disconnect,
148 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
151 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
152 SYSCTL_ULONG(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
153 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
154 static u_long ngpdg_recvspace = 20 * 1024;
155 SYSCTL_ULONG(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
156 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
158 /* List of all sockets (for netstat -f netgraph) */
159 static LIST_HEAD(, ngpcb) ngsocklist;
161 static struct mtx ngsocketlist_mtx;
163 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
165 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
171 LIST_ENTRY(hookpriv) next;
174 LIST_HEAD(ngshash, hookpriv);
176 /* Per-node private data */
178 struct ng_node *node; /* the associated netgraph node */
179 struct ngpcb *datasock; /* optional data socket */
180 struct ngpcb *ctlsock; /* optional control socket */
181 struct ngshash *hash; /* hash for hook names */
182 u_long hmask; /* hash mask */
185 struct mtx mtx; /* mtx to wait on */
186 int error; /* place to store error */
189 #define NGS_FLAG_NOLINGER 1 /* close with last hook */
191 /***************************************************************
193 ***************************************************************/
196 ngc_attach(struct socket *so, int proto, struct thread *td)
198 struct ngpcb *const pcbp = sotongpcb(so);
201 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
206 return (ng_attach_cntl(so));
210 ngc_detach(struct socket *so)
212 struct ngpcb *const pcbp = sotongpcb(so);
214 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
215 ng_detach_common(pcbp, NG_CONTROL);
219 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
220 struct mbuf *control, struct thread *td)
222 struct ngpcb *const pcbp = sotongpcb(so);
223 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
224 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
230 struct ng_apply_info apply;
237 /* Require destination as there may be >= 1 hooks on this node. */
239 error = EDESTADDRREQ;
243 if (sap->sg_len > NG_NODESIZ + offsetof(struct sockaddr_ng, sg_data)) {
249 * Allocate an expendable buffer for the path, chop off
250 * the sockaddr header, and make sure it's NUL terminated.
252 len = sap->sg_len - offsetof(struct sockaddr_ng, sg_data);
253 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
254 bcopy(sap->sg_data, path, len);
258 * Move the actual message out of mbufs into a linear buffer.
259 * Start by adding up the size of the data. (could use mh_len?)
261 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
265 * Move the data into a linear buffer as well.
266 * Messages are not delivered in mbufs.
268 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
269 m_copydata(m, 0, len, (char *)msg);
271 if (msg->header.version != NG_VERSION) {
272 free(msg, M_NETGRAPH_MSG);
279 * We look into the message and if it mkpeers a node of unknown type, we
280 * try to load it. We need to do this now, in syscall thread, because if
281 * message gets queued and applied later we will get panic.
283 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
284 msg->header.cmd == NGM_MKPEER) {
285 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
287 if (ng_findtype(mkp->type) == NULL) {
288 char filename[NG_TYPESIZ + 3];
291 /* Not found, try to load it as a loadable module. */
292 snprintf(filename, sizeof(filename), "ng_%s",
294 error = kern_kldload(curthread, filename, &fileid);
296 free(msg, M_NETGRAPH_MSG);
300 /* See if type has been loaded successfully. */
301 if (ng_findtype(mkp->type) == NULL) {
302 free(msg, M_NETGRAPH_MSG);
303 (void)kern_kldunload(curthread, fileid,
304 LINKER_UNLOAD_NORMAL);
311 item = ng_package_msg(msg, NG_WAITOK);
312 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
314 #ifdef TRACE_MESSAGES
315 printf("ng_address_path: errx=%d\n", error);
320 #ifdef TRACE_MESSAGES
321 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
322 item->el_dest->nd_ID,
323 msg->header.typecookie,
328 item->el_dest->nd_type->name);
332 * We do not want to return from syscall until the item
333 * is processed by destination node. We register callback
334 * on the item, which will update priv->error when item
336 * If ng_snd_item() has queued item, we sleep until
337 * callback wakes us up.
339 bzero(&apply, sizeof(apply));
340 apply.apply = ng_socket_item_applied;
341 apply.context = priv;
342 item->apply = &apply;
345 error = ng_snd_item(item, 0);
347 mtx_lock(&priv->mtx);
348 if (priv->error == -1)
349 msleep(priv, &priv->mtx, 0, "ngsock", 0);
350 mtx_unlock(&priv->mtx);
351 KASSERT(priv->error != -1,
352 ("ng_socket: priv->error wasn't updated"));
357 free(path, M_NETGRAPH_PATH);
366 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
368 struct ngpcb *const pcbp = sotongpcb(so);
372 return (ng_bind(nam, pcbp));
376 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
379 * At this time refuse to do this.. it used to
380 * do something but it was undocumented and not used.
382 printf("program tried to connect control socket to remote node\n");
386 /***************************************************************
388 ***************************************************************/
391 ngd_attach(struct socket *so, int proto, struct thread *td)
393 struct ngpcb *const pcbp = sotongpcb(so);
397 return (ng_attach_data(so));
401 ngd_detach(struct socket *so)
403 struct ngpcb *const pcbp = sotongpcb(so);
405 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
406 ng_detach_common(pcbp, NG_DATA);
410 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
411 struct mbuf *control, struct thread *td)
413 struct epoch_tracker et;
414 struct ngpcb *const pcbp = sotongpcb(so);
415 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
419 char hookname[NG_HOOKSIZ];
421 if ((pcbp == NULL) || (control != NULL)) {
425 if (pcbp->sockdata == NULL) {
431 len = 0; /* Make compiler happy. */
433 if (sap->sg_len > NG_NODESIZ +
434 offsetof(struct sockaddr_ng, sg_data)) {
438 len = sap->sg_len - offsetof(struct sockaddr_ng, sg_data);
442 * If the user used any of these ways to not specify an address
443 * then handle specially.
445 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
446 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
447 error = EDESTADDRREQ;
451 * If exactly one hook exists, just use it.
452 * Special case to allow write(2) to work on an ng_socket.
454 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
456 if (len >= NG_HOOKSIZ) {
462 * chop off the sockaddr header, and make sure it's NUL
465 bcopy(sap->sg_data, hookname, len);
466 hookname[len] = '\0';
468 /* Find the correct hook from 'hookname' */
469 hook = ng_findhook(pcbp->sockdata->node, hookname);
471 error = EHOSTUNREACH;
477 item = ng_package_data(m, NG_WAITOK);
480 NG_FWD_ITEM_HOOK(error, item, hook);
492 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
494 struct ngpcb *const pcbp = sotongpcb(so);
498 return (ng_connect_data(nam, pcbp));
502 * Used for both data and control sockets
505 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
508 struct sockaddr_ng *sg;
512 pcbp = sotongpcb(so);
513 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
514 /* XXXGL: can this still happen? */
517 sg_len = sizeof(struct sockaddr_ng) + NG_NODESIZ -
519 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
521 mtx_lock(&pcbp->sockdata->mtx);
522 if (pcbp->sockdata->node != NULL) {
523 node_p node = pcbp->sockdata->node;
525 if (NG_NODE_HAS_NAME(node))
526 bcopy(NG_NODE_NAME(node), sg->sg_data,
527 strlen(NG_NODE_NAME(node)));
528 mtx_unlock(&pcbp->sockdata->mtx);
531 sg->sg_family = AF_NETGRAPH;
532 *addr = (struct sockaddr *)sg;
534 mtx_unlock(&pcbp->sockdata->mtx);
543 * Attach a socket to it's protocol specific partner.
544 * For a control socket, actually create a netgraph node and attach
549 ng_attach_cntl(struct socket *so)
556 /* Setup protocol control block */
557 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
559 pcbp = sotongpcb(so);
561 /* Make the generic node components */
562 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
563 ng_detach_common(pcbp, NG_CONTROL);
568 * Allocate node private info and hash. We start
569 * with 16 hash entries, however we may grow later
570 * in ngs_newhook(). We can't predict how much hooks
571 * does this node plan to have.
573 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
574 priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
576 /* Initialize mutex. */
577 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
579 /* Link the pcb the private data. */
580 priv->ctlsock = pcbp;
581 pcbp->sockdata = priv;
584 pcbp->node_id = node->nd_ID; /* hint for netstat(1) */
586 /* Link the node and the private data. */
587 NG_NODE_SET_PRIVATE(priv->node, priv);
588 NG_NODE_REF(priv->node);
595 ng_attach_data(struct socket *so)
597 return (ng_attach_common(so, NG_DATA));
601 * Set up a socket protocol control block.
602 * This code is shared between control and data sockets.
605 ng_attach_common(struct socket *so, int type)
610 /* Standard socket setup stuff. */
611 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
615 /* Allocate the pcb. */
616 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
619 /* Link the pcb and the socket. */
620 so->so_pcb = (caddr_t)pcbp;
621 pcbp->ng_socket = so;
623 /* Add the socket to linked list */
624 mtx_lock(&ngsocketlist_mtx);
625 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
626 mtx_unlock(&ngsocketlist_mtx);
631 * Disassociate the socket from it's protocol specific
632 * partner. If it's attached to a node's private data structure,
633 * then unlink from that too. If we were the last socket attached to it,
634 * then shut down the entire node. Shared code for control and data sockets.
637 ng_detach_common(struct ngpcb *pcbp, int which)
639 struct ngsock *priv = pcbp->sockdata;
642 mtx_lock(&priv->mtx);
646 priv->ctlsock = NULL;
649 priv->datasock = NULL;
652 panic("%s", __func__);
654 pcbp->sockdata = NULL;
657 ng_socket_free_priv(priv);
660 pcbp->ng_socket->so_pcb = NULL;
661 mtx_lock(&ngsocketlist_mtx);
662 LIST_REMOVE(pcbp, socks);
663 mtx_unlock(&ngsocketlist_mtx);
668 * Remove a reference from node private data.
671 ng_socket_free_priv(struct ngsock *priv)
673 mtx_assert(&priv->mtx, MA_OWNED);
677 if (priv->refs == 0) {
678 mtx_destroy(&priv->mtx);
679 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
680 free(priv, M_NETGRAPH_SOCK);
684 if ((priv->refs == 1) && (priv->node != NULL)) {
685 node_p node = priv->node;
688 mtx_unlock(&priv->mtx);
690 ng_rmnode_self(node);
692 mtx_unlock(&priv->mtx);
696 * Connect the data socket to a named control socket node.
699 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
701 struct sockaddr_ng *sap;
707 /* If we are already connected, don't do it again. */
708 if (pcbp->sockdata != NULL)
712 * Find the target (victim) and check it doesn't already have
713 * a data socket. Also check it is a 'socket' type node.
714 * Use ng_package_data() and ng_address_path() to do this.
717 sap = (struct sockaddr_ng *) nam;
718 /* The item will hold the node reference. */
719 item = ng_package_data(NULL, NG_WAITOK);
721 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
722 return (error); /* item is freed on failure */
725 * Extract node from item and free item. Remember we now have
726 * a reference on the node. The item holds it for us.
727 * when we free the item we release the reference.
729 farnode = item->el_dest; /* shortcut */
730 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
731 NG_FREE_ITEM(item); /* drop the reference to the node */
734 priv = NG_NODE_PRIVATE(farnode);
735 if (priv->datasock != NULL) {
736 NG_FREE_ITEM(item); /* drop the reference to the node */
741 * Link the PCB and the private data struct. and note the extra
742 * reference. Drop the extra reference on the node.
744 mtx_lock(&priv->mtx);
745 priv->datasock = pcbp;
746 pcbp->sockdata = priv;
747 pcbp->node_id = priv->node->nd_ID; /* hint for netstat(1) */
749 mtx_unlock(&priv->mtx);
750 NG_FREE_ITEM(item); /* drop the reference to the node */
755 * Binding a socket means giving the corresponding node a name
758 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
760 struct ngsock *const priv = pcbp->sockdata;
761 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
767 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
768 (sap->sg_data[0] == '\0') ||
769 (sap->sg_data[sap->sg_len - 3] != '\0')) {
773 return (ng_name_node(priv->node, sap->sg_data));
776 /***************************************************************
778 ***************************************************************/
781 * You can only create new nodes from the socket end of things.
784 ngs_constructor(node_p nodep)
790 ngs_rehash(node_p node)
792 struct ngsock *priv = NG_NODE_PRIVATE(node);
799 new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
804 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
805 hp = NG_HOOK_PRIVATE(hook);
807 LIST_REMOVE(hp, next);
809 h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
810 LIST_INSERT_HEAD(&new[h], hp, next);
813 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
819 * We allow any hook to be connected to the node.
820 * There is no per-hook private information though.
823 ngs_newhook(node_p node, hook_p hook, const char *name)
825 struct ngsock *const priv = NG_NODE_PRIVATE(node);
829 hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
832 if (node->nd_numhooks * 2 > priv->hmask)
835 h = hash32_str(name, HASHINIT) & priv->hmask;
836 LIST_INSERT_HEAD(&priv->hash[h], hp, next);
837 NG_HOOK_SET_PRIVATE(hook, hp);
843 * If only one hook, allow read(2) and write(2) to work.
846 ngs_connect(hook_p hook)
848 node_p node = NG_HOOK_NODE(hook);
849 struct ngsock *priv = NG_NODE_PRIVATE(node);
851 if ((priv->datasock) && (priv->datasock->ng_socket)) {
852 if (NG_NODE_NUMHOOKS(node) == 1)
853 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
855 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
860 /* Look up hook by name */
862 ngs_findhook(node_p node, const char *name)
864 struct ngsock *priv = NG_NODE_PRIVATE(node);
869 * Microoptimisation for an ng_socket with
870 * a single hook, which is a common case.
872 if (node->nd_numhooks == 1) {
875 hook = LIST_FIRST(&node->nd_hooks);
877 if (strcmp(NG_HOOK_NAME(hook), name) == 0)
883 h = hash32_str(name, HASHINIT) & priv->hmask;
885 LIST_FOREACH(hp, &priv->hash[h], next)
886 if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
893 * Incoming messages get passed up to the control socket.
894 * Unless they are for us specifically (socket_type)
897 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
899 struct ngsock *const priv = NG_NODE_PRIVATE(node);
902 struct sockaddr_ng addr;
905 ng_ID_t retaddr = NGI_RETADDR(item);
909 NGI_GET_MSG(item, msg);
913 * Grab priv->mtx here to prevent destroying of control socket
914 * after checking that priv->ctlsock is not NULL.
916 mtx_lock(&priv->mtx);
917 pcbp = priv->ctlsock;
920 * Only allow mesgs to be passed if we have the control socket.
921 * Data sockets can only support the generic messages.
924 mtx_unlock(&priv->mtx);
929 so = pcbp->ng_socket;
930 SOCKBUF_LOCK(&so->so_rcv);
932 /* As long as the race is handled, priv->mtx may be unlocked now. */
933 mtx_unlock(&priv->mtx);
935 #ifdef TRACE_MESSAGES
936 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
938 msg->header.typecookie,
945 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
946 switch (msg->header.cmd) {
947 case NGM_SOCK_CMD_NOLINGER:
948 priv->flags |= NGS_FLAG_NOLINGER;
950 case NGM_SOCK_CMD_LINGER:
951 priv->flags &= ~NGS_FLAG_NOLINGER;
954 error = EINVAL; /* unknown command */
956 SOCKBUF_UNLOCK(&so->so_rcv);
958 /* Free the message and return. */
963 /* Get the return address into a sockaddr. */
964 bzero(&addr, sizeof(addr));
965 addr.sg_len = sizeof(addr);
966 addr.sg_family = AF_NETGRAPH;
967 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
969 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
970 SOCKBUF_UNLOCK(&so->so_rcv);
971 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
977 /* Copy the message itself into an mbuf chain. */
978 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
982 * Here we free the message. We need to do that
983 * regardless of whether we got mbufs.
988 SOCKBUF_UNLOCK(&so->so_rcv);
993 /* Send it up to the socket. */
994 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
996 soroverflow_locked(so);
1002 /* sorwakeup_locked () releases the lock internally. */
1003 sorwakeup_locked(so);
1009 * Receive data on a hook
1012 ngs_rcvdata(hook_p hook, item_p item)
1014 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1015 struct ngpcb *const pcbp = priv->datasock;
1017 struct sockaddr_ng *addr;
1018 char *addrbuf[NG_HOOKSIZ + 4];
1025 /* If there is no data socket, black-hole it. */
1030 so = pcbp->ng_socket;
1032 /* Get the return address into a sockaddr. */
1033 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1034 addr = (struct sockaddr_ng *) addrbuf;
1035 addr->sg_len = addrlen + 3;
1036 addr->sg_family = AF_NETGRAPH;
1037 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1038 addr->sg_data[addrlen] = '\0';
1040 /* Try to tell the socket which hook it came in on. */
1041 SOCKBUF_LOCK(&so->so_rcv);
1042 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)addr, m,
1044 SOCKBUF_UNLOCK(&so->so_rcv);
1050 /* sorwakeup_locked () releases the lock internally. */
1051 sorwakeup_locked(so);
1056 * Hook disconnection
1058 * For this type, removal of the last link destroys the node
1059 * if the NOLINGER flag is set.
1062 ngs_disconnect(hook_p hook)
1064 node_p node = NG_HOOK_NODE(hook);
1065 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1066 struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
1068 LIST_REMOVE(hp, next);
1069 free(hp, M_NETGRAPH_SOCK);
1071 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1072 if (NG_NODE_NUMHOOKS(node) == 1)
1073 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1075 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1078 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1079 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1080 ng_rmnode_self(node);
1086 * Do local shutdown processing.
1087 * In this case, that involves making sure the socket
1088 * knows we should be shutting down.
1091 ngs_shutdown(node_p node)
1093 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1094 struct ngpcb *dpcbp, *pcbp;
1096 mtx_lock(&priv->mtx);
1097 dpcbp = priv->datasock;
1098 pcbp = priv->ctlsock;
1101 soisdisconnected(dpcbp->ng_socket);
1104 soisdisconnected(pcbp->ng_socket);
1107 NG_NODE_SET_PRIVATE(node, NULL);
1108 ng_socket_free_priv(priv);
1110 NG_NODE_UNREF(node);
1115 ng_socket_item_applied(void *context, int error)
1117 struct ngsock *const priv = (struct ngsock *)context;
1119 mtx_lock(&priv->mtx);
1120 priv->error = error;
1122 mtx_unlock(&priv->mtx);
1127 dummy_disconnect(struct socket *so)
1132 * Control and data socket type descriptors
1134 * XXXRW: Perhaps _close should do something?
1137 static struct pr_usrreqs ngc_usrreqs = {
1139 .pru_attach = ngc_attach,
1140 .pru_bind = ngc_bind,
1141 .pru_connect = ngc_connect,
1142 .pru_detach = ngc_detach,
1143 .pru_disconnect = dummy_disconnect,
1144 .pru_peeraddr = NULL,
1145 .pru_send = ngc_send,
1146 .pru_shutdown = NULL,
1147 .pru_sockaddr = ng_getsockaddr,
1151 static struct pr_usrreqs ngd_usrreqs = {
1153 .pru_attach = ngd_attach,
1155 .pru_connect = ngd_connect,
1156 .pru_detach = ngd_detach,
1157 .pru_disconnect = dummy_disconnect,
1158 .pru_peeraddr = NULL,
1159 .pru_send = ngd_send,
1160 .pru_shutdown = NULL,
1161 .pru_sockaddr = ng_getsockaddr,
1166 * Definitions of protocols supported in the NETGRAPH domain.
1169 extern struct domain ngdomain; /* stop compiler warnings */
1171 static struct protosw ngsw[] = {
1173 .pr_type = SOCK_DGRAM,
1174 .pr_domain = &ngdomain,
1175 .pr_protocol = NG_CONTROL,
1176 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1177 .pr_usrreqs = &ngc_usrreqs
1180 .pr_type = SOCK_DGRAM,
1181 .pr_domain = &ngdomain,
1182 .pr_protocol = NG_DATA,
1183 .pr_flags = PR_ATOMIC | PR_ADDR,
1184 .pr_usrreqs = &ngd_usrreqs
1188 struct domain ngdomain = {
1189 .dom_family = AF_NETGRAPH,
1190 .dom_name = "netgraph",
1191 .dom_protosw = ngsw,
1192 .dom_protoswNPROTOSW = &ngsw[nitems(ngsw)]
1196 * Handle loading and unloading for this node type.
1197 * This is to handle auxiliary linkages (e.g protocol domain addition).
1200 ngs_mod_event(module_t mod, int event, void *data)
1206 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1209 /* Ensure there are no open netgraph sockets. */
1210 if (!LIST_EMPTY(&ngsocklist)) {
1215 /* Unregister protocol domain XXX can't do this yet.. */
1226 VNET_DOMAIN_SET(ng);
1228 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, AF_NETGRAPH, "");
1229 static SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1231 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_DATA, "");
1232 static SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1234 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_CONTROL, "");