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/protosw.h>
63 #include <sys/queue.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysctl.h>
71 #include <netgraph/ng_message.h>
72 #include <netgraph/netgraph.h>
73 #include <netgraph/ng_socketvar.h>
74 #include <netgraph/ng_socket.h>
76 #ifdef NG_SEPARATE_MALLOC
77 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
78 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
80 #define M_NETGRAPH_PATH M_NETGRAPH
81 #define M_NETGRAPH_SOCK M_NETGRAPH
85 * It's Ascii-art time!
86 * +-------------+ +-------------+
87 * |socket (ctl)| |socket (data)|
88 * +-------------+ +-------------+
92 * +-----------+ +-----------+
93 * |pcb (ctl)| |pcb (data)|
94 * +-----------+ +-----------+
98 * +--------------------------+
99 * | Socket type private |
101 * +--------------------------+
110 /* Netgraph node methods */
111 static ng_constructor_t ngs_constructor;
112 static ng_rcvmsg_t ngs_rcvmsg;
113 static ng_shutdown_t ngs_shutdown;
114 static ng_newhook_t ngs_newhook;
115 static ng_connect_t ngs_connect;
116 static ng_findhook_t ngs_findhook;
117 static ng_rcvdata_t ngs_rcvdata;
118 static ng_disconnect_t ngs_disconnect;
120 /* Internal methods */
121 static int ng_attach_data(struct socket *so);
122 static int ng_attach_cntl(struct socket *so);
123 static int ng_attach_common(struct socket *so, int type);
124 static void ng_detach_common(struct ngpcb *pcbp, int type);
125 static void ng_socket_free_priv(struct ngsock *priv);
126 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
127 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
129 static int ngs_mod_event(module_t mod, int event, void *data);
130 static void ng_socket_item_applied(void *context, int error);
132 /* Netgraph type descriptor */
133 static struct ng_type typestruct = {
134 .version = NG_ABI_VERSION,
135 .name = NG_SOCKET_NODE_TYPE,
136 .mod_event = ngs_mod_event,
137 .constructor = ngs_constructor,
138 .rcvmsg = ngs_rcvmsg,
139 .shutdown = ngs_shutdown,
140 .newhook = ngs_newhook,
141 .connect = ngs_connect,
142 .findhook = ngs_findhook,
143 .rcvdata = ngs_rcvdata,
144 .disconnect = ngs_disconnect,
146 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
149 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
150 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
151 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
152 static u_long ngpdg_recvspace = 20 * 1024;
153 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
154 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
156 /* List of all sockets (for netstat -f netgraph) */
157 static LIST_HEAD(, ngpcb) ngsocklist;
159 static struct mtx ngsocketlist_mtx;
161 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
163 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
168 /***************************************************************
170 ***************************************************************/
173 ngc_attach(struct socket *so, int proto, struct thread *td)
175 struct ngpcb *const pcbp = sotongpcb(so);
178 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
183 return (ng_attach_cntl(so));
187 ngc_detach(struct socket *so)
189 struct ngpcb *const pcbp = sotongpcb(so);
191 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
192 ng_detach_common(pcbp, NG_CONTROL);
196 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
197 struct mbuf *control, struct thread *td)
199 struct ngpcb *const pcbp = sotongpcb(so);
200 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
201 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
207 struct ng_apply_info apply;
214 /* Require destination as there may be >= 1 hooks on this node. */
216 error = EDESTADDRREQ;
221 * Allocate an expendable buffer for the path, chop off
222 * the sockaddr header, and make sure it's NUL terminated.
224 len = sap->sg_len - 2;
225 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
226 bcopy(sap->sg_data, path, len);
230 * Move the actual message out of mbufs into a linear buffer.
231 * Start by adding up the size of the data. (could use mh_len?)
233 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
237 * Move the data into a linear buffer as well.
238 * Messages are not delivered in mbufs.
240 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
241 m_copydata(m, 0, len, (char *)msg);
243 if (msg->header.version != NG_VERSION) {
244 free(msg, M_NETGRAPH_MSG);
251 * We look into the message and if it mkpeers a node of unknown type, we
252 * try to load it. We need to do this now, in syscall thread, because if
253 * message gets queued and applied later we will get panic.
255 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
256 msg->header.cmd == NGM_MKPEER) {
257 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
259 if (ng_findtype(mkp->type) == NULL) {
260 char filename[NG_TYPESIZ + 3];
263 /* Not found, try to load it as a loadable module. */
264 snprintf(filename, sizeof(filename), "ng_%s",
266 error = kern_kldload(curthread, filename, &fileid);
268 free(msg, M_NETGRAPH_MSG);
272 /* See if type has been loaded successfully. */
273 if (ng_findtype(mkp->type) == NULL) {
274 free(msg, M_NETGRAPH_MSG);
275 (void)kern_kldunload(curthread, fileid,
276 LINKER_UNLOAD_NORMAL);
283 item = ng_package_msg(msg, M_WAITOK);
284 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
286 #ifdef TRACE_MESSAGES
287 printf("ng_address_path: errx=%d\n", error);
292 #ifdef TRACE_MESSAGES
293 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
294 item->el_dest->nd_ID,
295 msg->header.typecookie,
300 item->el_dest->nd_type->name);
304 * We do not want to return from syscall until the item
305 * is processed by destination node. We register callback
306 * on the item, which will update priv->error when item
308 * If ng_snd_item() has queued item, we sleep until
309 * callback wakes us up.
311 bzero(&apply, sizeof(apply));
312 apply.apply = ng_socket_item_applied;
313 apply.context = priv;
314 item->apply = &apply;
317 error = ng_snd_item(item, 0);
319 mtx_lock(&priv->mtx);
320 if (priv->error == -1)
321 msleep(priv, &priv->mtx, 0, "ngsock", 0);
322 mtx_unlock(&priv->mtx);
323 KASSERT(priv->error != -1,
324 ("ng_socket: priv->error wasn't updated"));
329 free(path, M_NETGRAPH_PATH);
338 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
340 struct ngpcb *const pcbp = sotongpcb(so);
344 return (ng_bind(nam, pcbp));
348 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
351 * At this time refuse to do this.. it used to
352 * do something but it was undocumented and not used.
354 printf("program tried to connect control socket to remote node\n");
358 /***************************************************************
360 ***************************************************************/
363 ngd_attach(struct socket *so, int proto, struct thread *td)
365 struct ngpcb *const pcbp = sotongpcb(so);
369 return (ng_attach_data(so));
373 ngd_detach(struct socket *so)
375 struct ngpcb *const pcbp = sotongpcb(so);
377 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
378 ng_detach_common(pcbp, NG_DATA);
382 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
383 struct mbuf *control, struct thread *td)
385 struct ngpcb *const pcbp = sotongpcb(so);
386 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
389 char hookname[NG_HOOKSIZ];
391 if ((pcbp == NULL) || (control != NULL)) {
395 if (pcbp->sockdata == NULL) {
401 len = 0; /* Make compiler happy. */
403 len = sap->sg_len - 2;
406 * If the user used any of these ways to not specify an address
407 * then handle specially.
409 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
410 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
411 error = EDESTADDRREQ;
415 * If exactly one hook exists, just use it.
416 * Special case to allow write(2) to work on an ng_socket.
418 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
420 if (len >= NG_HOOKSIZ) {
426 * chop off the sockaddr header, and make sure it's NUL
429 bcopy(sap->sg_data, hookname, len);
430 hookname[len] = '\0';
432 /* Find the correct hook from 'hookname' */
433 hook = ng_findhook(pcbp->sockdata->node, hookname);
435 error = EHOSTUNREACH;
441 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
452 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
454 struct ngpcb *const pcbp = sotongpcb(so);
458 return (ng_connect_data(nam, pcbp));
462 * Used for both data and control sockets
465 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
468 struct sockaddr_ng *sg;
472 /* Why isn't sg_data a `char[1]' ? :-( */
473 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
475 pcbp = sotongpcb(so);
476 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
477 /* XXXGL: can this still happen? */
480 mtx_lock(&pcbp->sockdata->mtx);
481 if (pcbp->sockdata->node != NULL) {
482 node_p node = pcbp->sockdata->node;
483 int namelen = 0; /* silence compiler! */
485 if (NG_NODE_HAS_NAME(node))
486 sg_len += namelen = strlen(NG_NODE_NAME(node));
488 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
490 if (NG_NODE_HAS_NAME(node))
491 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
494 sg->sg_family = AF_NETGRAPH;
495 *addr = (struct sockaddr *)sg;
496 mtx_unlock(&pcbp->sockdata->mtx);
498 mtx_unlock(&pcbp->sockdata->mtx);
506 * Attach a socket to it's protocol specific partner.
507 * For a control socket, actually create a netgraph node and attach
512 ng_attach_cntl(struct socket *so)
519 /* Setup protocol control block */
520 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
522 pcbp = sotongpcb(so);
524 /* Make the generic node components */
525 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
526 ng_detach_common(pcbp, NG_CONTROL);
531 * Allocate node private info and hash. We start
532 * with 16 hash entries, however we may grow later
533 * in ngs_newhook(). We can't predict how much hooks
534 * does this node plan to have.
536 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
537 priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
539 /* Initialize mutex. */
540 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
542 /* Link the pcb the private data. */
543 priv->ctlsock = pcbp;
544 pcbp->sockdata = priv;
548 /* Store a hint for netstat(1). */
549 priv->node_id = priv->node->nd_ID;
551 /* Link the node and the private data. */
552 NG_NODE_SET_PRIVATE(priv->node, priv);
553 NG_NODE_REF(priv->node);
560 ng_attach_data(struct socket *so)
562 return (ng_attach_common(so, NG_DATA));
566 * Set up a socket protocol control block.
567 * This code is shared between control and data sockets.
570 ng_attach_common(struct socket *so, int type)
575 /* Standard socket setup stuff. */
576 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
580 /* Allocate the pcb. */
581 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
584 /* Link the pcb and the socket. */
585 so->so_pcb = (caddr_t)pcbp;
586 pcbp->ng_socket = so;
588 /* Add the socket to linked list */
589 mtx_lock(&ngsocketlist_mtx);
590 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
591 mtx_unlock(&ngsocketlist_mtx);
596 * Disassociate the socket from it's protocol specific
597 * partner. If it's attached to a node's private data structure,
598 * then unlink from that too. If we were the last socket attached to it,
599 * then shut down the entire node. Shared code for control and data sockets.
602 ng_detach_common(struct ngpcb *pcbp, int which)
604 struct ngsock *priv = pcbp->sockdata;
607 mtx_lock(&priv->mtx);
611 priv->ctlsock = NULL;
614 priv->datasock = NULL;
619 pcbp->sockdata = NULL;
621 ng_socket_free_priv(priv);
624 pcbp->ng_socket->so_pcb = NULL;
625 mtx_lock(&ngsocketlist_mtx);
626 LIST_REMOVE(pcbp, socks);
627 mtx_unlock(&ngsocketlist_mtx);
632 * Remove a reference from node private data.
635 ng_socket_free_priv(struct ngsock *priv)
637 mtx_assert(&priv->mtx, MA_OWNED);
641 if (priv->refs == 0) {
642 mtx_destroy(&priv->mtx);
643 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
644 free(priv, M_NETGRAPH_SOCK);
648 if ((priv->refs == 1) && (priv->node != NULL)) {
649 node_p node = priv->node;
652 mtx_unlock(&priv->mtx);
654 ng_rmnode_self(node);
656 mtx_unlock(&priv->mtx);
660 * Connect the data socket to a named control socket node.
663 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
665 struct sockaddr_ng *sap;
671 /* If we are already connected, don't do it again. */
672 if (pcbp->sockdata != NULL)
676 * Find the target (victim) and check it doesn't already have
677 * a data socket. Also check it is a 'socket' type node.
678 * Use ng_package_data() and ng_address_path() to do this.
681 sap = (struct sockaddr_ng *) nam;
682 /* The item will hold the node reference. */
683 item = ng_package_data(NULL, NG_WAITOK);
685 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
686 return (error); /* item is freed on failure */
689 * Extract node from item and free item. Remember we now have
690 * a reference on the node. The item holds it for us.
691 * when we free the item we release the reference.
693 farnode = item->el_dest; /* shortcut */
694 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
695 NG_FREE_ITEM(item); /* drop the reference to the node */
698 priv = NG_NODE_PRIVATE(farnode);
699 if (priv->datasock != NULL) {
700 NG_FREE_ITEM(item); /* drop the reference to the node */
705 * Link the PCB and the private data struct. and note the extra
706 * reference. Drop the extra reference on the node.
708 mtx_lock(&priv->mtx);
709 priv->datasock = pcbp;
710 pcbp->sockdata = priv;
712 mtx_unlock(&priv->mtx);
713 NG_FREE_ITEM(item); /* drop the reference to the node */
718 * Binding a socket means giving the corresponding node a name
721 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
723 struct ngsock *const priv = pcbp->sockdata;
724 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
730 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
731 (sap->sg_data[0] == '\0') ||
732 (sap->sg_data[sap->sg_len - 3] != '\0')) {
736 return (ng_name_node(priv->node, sap->sg_data));
739 /***************************************************************
741 ***************************************************************/
744 * You can only create new nodes from the socket end of things.
747 ngs_constructor(node_p nodep)
753 ngs_rehash(node_p node)
755 struct ngsock *priv = NG_NODE_PRIVATE(node);
762 new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
767 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
768 hp = NG_HOOK_PRIVATE(hook);
770 LIST_REMOVE(hp, next);
772 h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
773 LIST_INSERT_HEAD(&new[h], hp, next);
776 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
782 * We allow any hook to be connected to the node.
783 * There is no per-hook private information though.
786 ngs_newhook(node_p node, hook_p hook, const char *name)
788 struct ngsock *const priv = NG_NODE_PRIVATE(node);
792 hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
795 if (node->nd_numhooks * 2 > priv->hmask)
798 h = hash32_str(name, HASHINIT) & priv->hmask;
799 LIST_INSERT_HEAD(&priv->hash[h], hp, next);
800 NG_HOOK_SET_PRIVATE(hook, hp);
806 * If only one hook, allow read(2) and write(2) to work.
809 ngs_connect(hook_p hook)
811 node_p node = NG_HOOK_NODE(hook);
812 struct ngsock *priv = NG_NODE_PRIVATE(node);
814 if ((priv->datasock) && (priv->datasock->ng_socket)) {
815 if (NG_NODE_NUMHOOKS(node) == 1)
816 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
818 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
823 /* Look up hook by name */
825 ngs_findhook(node_p node, const char *name)
827 struct ngsock *priv = NG_NODE_PRIVATE(node);
832 * Microoptimisation for an ng_socket with
833 * a single hook, which is a common case.
835 if (node->nd_numhooks == 1) {
838 hook = LIST_FIRST(&node->nd_hooks);
840 if (strcmp(NG_HOOK_NAME(hook), name) == 0)
846 h = hash32_str(name, HASHINIT) & priv->hmask;
848 LIST_FOREACH(hp, &priv->hash[h], next)
849 if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
856 * Incoming messages get passed up to the control socket.
857 * Unless they are for us specifically (socket_type)
860 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
862 struct ngsock *const priv = NG_NODE_PRIVATE(node);
865 struct sockaddr_ng addr;
868 ng_ID_t retaddr = NGI_RETADDR(item);
872 NGI_GET_MSG(item, msg);
876 * Grab priv->mtx here to prevent destroying of control socket
877 * after checking that priv->ctlsock is not NULL.
879 mtx_lock(&priv->mtx);
880 pcbp = priv->ctlsock;
883 * Only allow mesgs to be passed if we have the control socket.
884 * Data sockets can only support the generic messages.
887 mtx_unlock(&priv->mtx);
892 so = pcbp->ng_socket;
893 SOCKBUF_LOCK(&so->so_rcv);
895 /* As long as the race is handled, priv->mtx may be unlocked now. */
896 mtx_unlock(&priv->mtx);
898 #ifdef TRACE_MESSAGES
899 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
901 msg->header.typecookie,
908 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
909 switch (msg->header.cmd) {
910 case NGM_SOCK_CMD_NOLINGER:
911 priv->flags |= NGS_FLAG_NOLINGER;
913 case NGM_SOCK_CMD_LINGER:
914 priv->flags &= ~NGS_FLAG_NOLINGER;
917 error = EINVAL; /* unknown command */
919 SOCKBUF_UNLOCK(&so->so_rcv);
921 /* Free the message and return. */
926 /* Get the return address into a sockaddr. */
927 bzero(&addr, sizeof(addr));
928 addr.sg_len = sizeof(addr);
929 addr.sg_family = AF_NETGRAPH;
930 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
932 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
933 SOCKBUF_UNLOCK(&so->so_rcv);
934 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
940 /* Copy the message itself into an mbuf chain. */
941 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
945 * Here we free the message. We need to do that
946 * regardless of whether we got mbufs.
951 SOCKBUF_UNLOCK(&so->so_rcv);
956 /* Send it up to the socket. */
957 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
959 SOCKBUF_UNLOCK(&so->so_rcv);
964 sorwakeup_locked(so);
970 * Receive data on a hook
973 ngs_rcvdata(hook_p hook, item_p item)
975 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
976 struct ngpcb *const pcbp = priv->datasock;
978 struct sockaddr_ng *addr;
979 char *addrbuf[NG_HOOKSIZ + 4];
986 /* If there is no data socket, black-hole it. */
991 so = pcbp->ng_socket;
993 /* Get the return address into a sockaddr. */
994 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
995 addr = (struct sockaddr_ng *) addrbuf;
996 addr->sg_len = addrlen + 3;
997 addr->sg_family = AF_NETGRAPH;
998 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
999 addr->sg_data[addrlen] = '\0';
1001 /* Try to tell the socket which hook it came in on. */
1002 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
1012 * Hook disconnection
1014 * For this type, removal of the last link destroys the node
1015 * if the NOLINGER flag is set.
1018 ngs_disconnect(hook_p hook)
1020 node_p node = NG_HOOK_NODE(hook);
1021 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1022 struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
1024 LIST_REMOVE(hp, next);
1025 free(hp, M_NETGRAPH_SOCK);
1027 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1028 if (NG_NODE_NUMHOOKS(node) == 1)
1029 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1031 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1034 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1035 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1036 ng_rmnode_self(node);
1042 * Do local shutdown processing.
1043 * In this case, that involves making sure the socket
1044 * knows we should be shutting down.
1047 ngs_shutdown(node_p node)
1049 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1050 struct ngpcb *dpcbp, *pcbp;
1052 mtx_lock(&priv->mtx);
1053 dpcbp = priv->datasock;
1054 pcbp = priv->ctlsock;
1057 soisdisconnected(dpcbp->ng_socket);
1060 soisdisconnected(pcbp->ng_socket);
1063 NG_NODE_SET_PRIVATE(node, NULL);
1064 ng_socket_free_priv(priv);
1066 NG_NODE_UNREF(node);
1071 ng_socket_item_applied(void *context, int error)
1073 struct ngsock *const priv = (struct ngsock *)context;
1075 mtx_lock(&priv->mtx);
1076 priv->error = error;
1078 mtx_unlock(&priv->mtx);
1083 dummy_disconnect(struct socket *so)
1088 * Control and data socket type descriptors
1090 * XXXRW: Perhaps _close should do something?
1093 static struct pr_usrreqs ngc_usrreqs = {
1095 .pru_attach = ngc_attach,
1096 .pru_bind = ngc_bind,
1097 .pru_connect = ngc_connect,
1098 .pru_detach = ngc_detach,
1099 .pru_disconnect = dummy_disconnect,
1100 .pru_peeraddr = NULL,
1101 .pru_send = ngc_send,
1102 .pru_shutdown = NULL,
1103 .pru_sockaddr = ng_getsockaddr,
1107 static struct pr_usrreqs ngd_usrreqs = {
1109 .pru_attach = ngd_attach,
1111 .pru_connect = ngd_connect,
1112 .pru_detach = ngd_detach,
1113 .pru_disconnect = dummy_disconnect,
1114 .pru_peeraddr = NULL,
1115 .pru_send = ngd_send,
1116 .pru_shutdown = NULL,
1117 .pru_sockaddr = ng_getsockaddr,
1122 * Definitions of protocols supported in the NETGRAPH domain.
1125 extern struct domain ngdomain; /* stop compiler warnings */
1127 static struct protosw ngsw[] = {
1129 .pr_type = SOCK_DGRAM,
1130 .pr_domain = &ngdomain,
1131 .pr_protocol = NG_CONTROL,
1132 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1133 .pr_usrreqs = &ngc_usrreqs
1136 .pr_type = SOCK_DGRAM,
1137 .pr_domain = &ngdomain,
1138 .pr_protocol = NG_DATA,
1139 .pr_flags = PR_ATOMIC | PR_ADDR,
1140 .pr_usrreqs = &ngd_usrreqs
1144 struct domain ngdomain = {
1145 .dom_family = AF_NETGRAPH,
1146 .dom_name = "netgraph",
1147 .dom_protosw = ngsw,
1148 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1152 * Handle loading and unloading for this node type.
1153 * This is to handle auxiliary linkages (e.g protocol domain addition).
1156 ngs_mod_event(module_t mod, int event, void *data)
1162 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1165 /* Ensure there are no open netgraph sockets. */
1166 if (!LIST_EMPTY(&ngsocklist)) {
1171 /* Unregister protocol domain XXX can't do this yet.. */
1182 VNET_DOMAIN_SET(ng);
1184 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1185 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1186 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1187 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1188 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");