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 static MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info");
78 static 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_ULONG(_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_ULONG(_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"); */
169 LIST_ENTRY(hookpriv) next;
172 LIST_HEAD(ngshash, hookpriv);
174 /* Per-node private data */
176 struct ng_node *node; /* the associated netgraph node */
177 struct ngpcb *datasock; /* optional data socket */
178 struct ngpcb *ctlsock; /* optional control socket */
179 struct ngshash *hash; /* hash for hook names */
180 u_long hmask; /* hash mask */
183 struct mtx mtx; /* mtx to wait on */
184 int error; /* place to store error */
187 #define NGS_FLAG_NOLINGER 1 /* close with last hook */
189 /***************************************************************
191 ***************************************************************/
194 ngc_attach(struct socket *so, int proto, struct thread *td)
196 struct ngpcb *const pcbp = sotongpcb(so);
199 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
204 return (ng_attach_cntl(so));
208 ngc_detach(struct socket *so)
210 struct ngpcb *const pcbp = sotongpcb(so);
212 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
213 ng_detach_common(pcbp, NG_CONTROL);
217 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
218 struct mbuf *control, struct thread *td)
220 struct ngpcb *const pcbp = sotongpcb(so);
221 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
222 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
228 struct ng_apply_info apply;
235 /* Require destination as there may be >= 1 hooks on this node. */
237 error = EDESTADDRREQ;
242 * Allocate an expendable buffer for the path, chop off
243 * the sockaddr header, and make sure it's NUL terminated.
245 len = sap->sg_len - 2;
246 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
247 bcopy(sap->sg_data, path, len);
251 * Move the actual message out of mbufs into a linear buffer.
252 * Start by adding up the size of the data. (could use mh_len?)
254 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
258 * Move the data into a linear buffer as well.
259 * Messages are not delivered in mbufs.
261 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
262 m_copydata(m, 0, len, (char *)msg);
264 if (msg->header.version != NG_VERSION) {
265 free(msg, M_NETGRAPH_MSG);
272 * We look into the message and if it mkpeers a node of unknown type, we
273 * try to load it. We need to do this now, in syscall thread, because if
274 * message gets queued and applied later we will get panic.
276 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
277 msg->header.cmd == NGM_MKPEER) {
278 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
280 if (ng_findtype(mkp->type) == NULL) {
281 char filename[NG_TYPESIZ + 3];
284 /* Not found, try to load it as a loadable module. */
285 snprintf(filename, sizeof(filename), "ng_%s",
287 error = kern_kldload(curthread, filename, &fileid);
289 free(msg, M_NETGRAPH_MSG);
293 /* See if type has been loaded successfully. */
294 if (ng_findtype(mkp->type) == NULL) {
295 free(msg, M_NETGRAPH_MSG);
296 (void)kern_kldunload(curthread, fileid,
297 LINKER_UNLOAD_NORMAL);
304 item = ng_package_msg(msg, M_WAITOK);
305 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
307 #ifdef TRACE_MESSAGES
308 printf("ng_address_path: errx=%d\n", error);
313 #ifdef TRACE_MESSAGES
314 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
315 item->el_dest->nd_ID,
316 msg->header.typecookie,
321 item->el_dest->nd_type->name);
325 * We do not want to return from syscall until the item
326 * is processed by destination node. We register callback
327 * on the item, which will update priv->error when item
329 * If ng_snd_item() has queued item, we sleep until
330 * callback wakes us up.
332 bzero(&apply, sizeof(apply));
333 apply.apply = ng_socket_item_applied;
334 apply.context = priv;
335 item->apply = &apply;
338 error = ng_snd_item(item, 0);
340 mtx_lock(&priv->mtx);
341 if (priv->error == -1)
342 msleep(priv, &priv->mtx, 0, "ngsock", 0);
343 mtx_unlock(&priv->mtx);
344 KASSERT(priv->error != -1,
345 ("ng_socket: priv->error wasn't updated"));
350 free(path, M_NETGRAPH_PATH);
359 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
361 struct ngpcb *const pcbp = sotongpcb(so);
365 return (ng_bind(nam, pcbp));
369 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
372 * At this time refuse to do this.. it used to
373 * do something but it was undocumented and not used.
375 printf("program tried to connect control socket to remote node\n");
379 /***************************************************************
381 ***************************************************************/
384 ngd_attach(struct socket *so, int proto, struct thread *td)
386 struct ngpcb *const pcbp = sotongpcb(so);
390 return (ng_attach_data(so));
394 ngd_detach(struct socket *so)
396 struct ngpcb *const pcbp = sotongpcb(so);
398 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
399 ng_detach_common(pcbp, NG_DATA);
403 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
404 struct mbuf *control, struct thread *td)
406 struct ngpcb *const pcbp = sotongpcb(so);
407 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
410 char hookname[NG_HOOKSIZ];
412 if ((pcbp == NULL) || (control != NULL)) {
416 if (pcbp->sockdata == NULL) {
422 len = 0; /* Make compiler happy. */
424 len = sap->sg_len - 2;
427 * If the user used any of these ways to not specify an address
428 * then handle specially.
430 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
431 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
432 error = EDESTADDRREQ;
436 * If exactly one hook exists, just use it.
437 * Special case to allow write(2) to work on an ng_socket.
439 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
441 if (len >= NG_HOOKSIZ) {
447 * chop off the sockaddr header, and make sure it's NUL
450 bcopy(sap->sg_data, hookname, len);
451 hookname[len] = '\0';
453 /* Find the correct hook from 'hookname' */
454 hook = ng_findhook(pcbp->sockdata->node, hookname);
456 error = EHOSTUNREACH;
462 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
473 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
475 struct ngpcb *const pcbp = sotongpcb(so);
479 return (ng_connect_data(nam, pcbp));
483 * Used for both data and control sockets
486 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
489 struct sockaddr_ng *sg;
493 pcbp = sotongpcb(so);
494 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
495 /* XXXGL: can this still happen? */
498 sg_len = sizeof(struct sockaddr_ng) + NG_NODESIZ -
500 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
502 mtx_lock(&pcbp->sockdata->mtx);
503 if (pcbp->sockdata->node != NULL) {
504 node_p node = pcbp->sockdata->node;
506 if (NG_NODE_HAS_NAME(node))
507 bcopy(NG_NODE_NAME(node), sg->sg_data,
508 strlen(NG_NODE_NAME(node)));
509 mtx_unlock(&pcbp->sockdata->mtx);
512 sg->sg_family = AF_NETGRAPH;
513 *addr = (struct sockaddr *)sg;
515 mtx_unlock(&pcbp->sockdata->mtx);
524 * Attach a socket to it's protocol specific partner.
525 * For a control socket, actually create a netgraph node and attach
530 ng_attach_cntl(struct socket *so)
537 /* Setup protocol control block */
538 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
540 pcbp = sotongpcb(so);
542 /* Make the generic node components */
543 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
544 ng_detach_common(pcbp, NG_CONTROL);
549 * Allocate node private info and hash. We start
550 * with 16 hash entries, however we may grow later
551 * in ngs_newhook(). We can't predict how much hooks
552 * does this node plan to have.
554 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
555 priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
557 /* Initialize mutex. */
558 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
560 /* Link the pcb the private data. */
561 priv->ctlsock = pcbp;
562 pcbp->sockdata = priv;
565 pcbp->node_id = node->nd_ID; /* hint for netstat(1) */
567 /* Link the node and the private data. */
568 NG_NODE_SET_PRIVATE(priv->node, priv);
569 NG_NODE_REF(priv->node);
576 ng_attach_data(struct socket *so)
578 return (ng_attach_common(so, NG_DATA));
582 * Set up a socket protocol control block.
583 * This code is shared between control and data sockets.
586 ng_attach_common(struct socket *so, int type)
591 /* Standard socket setup stuff. */
592 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
596 /* Allocate the pcb. */
597 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
600 /* Link the pcb and the socket. */
601 so->so_pcb = (caddr_t)pcbp;
602 pcbp->ng_socket = so;
604 /* Add the socket to linked list */
605 mtx_lock(&ngsocketlist_mtx);
606 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
607 mtx_unlock(&ngsocketlist_mtx);
612 * Disassociate the socket from it's protocol specific
613 * partner. If it's attached to a node's private data structure,
614 * then unlink from that too. If we were the last socket attached to it,
615 * then shut down the entire node. Shared code for control and data sockets.
618 ng_detach_common(struct ngpcb *pcbp, int which)
620 struct ngsock *priv = pcbp->sockdata;
623 mtx_lock(&priv->mtx);
627 priv->ctlsock = NULL;
630 priv->datasock = NULL;
633 panic("%s", __func__);
635 pcbp->sockdata = NULL;
638 ng_socket_free_priv(priv);
641 pcbp->ng_socket->so_pcb = NULL;
642 mtx_lock(&ngsocketlist_mtx);
643 LIST_REMOVE(pcbp, socks);
644 mtx_unlock(&ngsocketlist_mtx);
649 * Remove a reference from node private data.
652 ng_socket_free_priv(struct ngsock *priv)
654 mtx_assert(&priv->mtx, MA_OWNED);
658 if (priv->refs == 0) {
659 mtx_destroy(&priv->mtx);
660 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
661 free(priv, M_NETGRAPH_SOCK);
665 if ((priv->refs == 1) && (priv->node != NULL)) {
666 node_p node = priv->node;
669 mtx_unlock(&priv->mtx);
671 ng_rmnode_self(node);
673 mtx_unlock(&priv->mtx);
677 * Connect the data socket to a named control socket node.
680 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
682 struct sockaddr_ng *sap;
688 /* If we are already connected, don't do it again. */
689 if (pcbp->sockdata != NULL)
693 * Find the target (victim) and check it doesn't already have
694 * a data socket. Also check it is a 'socket' type node.
695 * Use ng_package_data() and ng_address_path() to do this.
698 sap = (struct sockaddr_ng *) nam;
699 /* The item will hold the node reference. */
700 item = ng_package_data(NULL, NG_WAITOK);
702 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
703 return (error); /* item is freed on failure */
706 * Extract node from item and free item. Remember we now have
707 * a reference on the node. The item holds it for us.
708 * when we free the item we release the reference.
710 farnode = item->el_dest; /* shortcut */
711 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
712 NG_FREE_ITEM(item); /* drop the reference to the node */
715 priv = NG_NODE_PRIVATE(farnode);
716 if (priv->datasock != NULL) {
717 NG_FREE_ITEM(item); /* drop the reference to the node */
722 * Link the PCB and the private data struct. and note the extra
723 * reference. Drop the extra reference on the node.
725 mtx_lock(&priv->mtx);
726 priv->datasock = pcbp;
727 pcbp->sockdata = priv;
728 pcbp->node_id = priv->node->nd_ID; /* hint for netstat(1) */
730 mtx_unlock(&priv->mtx);
731 NG_FREE_ITEM(item); /* drop the reference to the node */
736 * Binding a socket means giving the corresponding node a name
739 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
741 struct ngsock *const priv = pcbp->sockdata;
742 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
748 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
749 (sap->sg_data[0] == '\0') ||
750 (sap->sg_data[sap->sg_len - 3] != '\0')) {
754 return (ng_name_node(priv->node, sap->sg_data));
757 /***************************************************************
759 ***************************************************************/
762 * You can only create new nodes from the socket end of things.
765 ngs_constructor(node_p nodep)
771 ngs_rehash(node_p node)
773 struct ngsock *priv = NG_NODE_PRIVATE(node);
780 new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
785 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
786 hp = NG_HOOK_PRIVATE(hook);
788 LIST_REMOVE(hp, next);
790 h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
791 LIST_INSERT_HEAD(&new[h], hp, next);
794 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
800 * We allow any hook to be connected to the node.
801 * There is no per-hook private information though.
804 ngs_newhook(node_p node, hook_p hook, const char *name)
806 struct ngsock *const priv = NG_NODE_PRIVATE(node);
810 hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
813 if (node->nd_numhooks * 2 > priv->hmask)
816 h = hash32_str(name, HASHINIT) & priv->hmask;
817 LIST_INSERT_HEAD(&priv->hash[h], hp, next);
818 NG_HOOK_SET_PRIVATE(hook, hp);
824 * If only one hook, allow read(2) and write(2) to work.
827 ngs_connect(hook_p hook)
829 node_p node = NG_HOOK_NODE(hook);
830 struct ngsock *priv = NG_NODE_PRIVATE(node);
832 if ((priv->datasock) && (priv->datasock->ng_socket)) {
833 if (NG_NODE_NUMHOOKS(node) == 1)
834 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
836 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
841 /* Look up hook by name */
843 ngs_findhook(node_p node, const char *name)
845 struct ngsock *priv = NG_NODE_PRIVATE(node);
850 * Microoptimisation for an ng_socket with
851 * a single hook, which is a common case.
853 if (node->nd_numhooks == 1) {
856 hook = LIST_FIRST(&node->nd_hooks);
858 if (strcmp(NG_HOOK_NAME(hook), name) == 0)
864 h = hash32_str(name, HASHINIT) & priv->hmask;
866 LIST_FOREACH(hp, &priv->hash[h], next)
867 if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
874 * Incoming messages get passed up to the control socket.
875 * Unless they are for us specifically (socket_type)
878 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
880 struct ngsock *const priv = NG_NODE_PRIVATE(node);
883 struct sockaddr_ng addr;
886 ng_ID_t retaddr = NGI_RETADDR(item);
890 NGI_GET_MSG(item, msg);
894 * Grab priv->mtx here to prevent destroying of control socket
895 * after checking that priv->ctlsock is not NULL.
897 mtx_lock(&priv->mtx);
898 pcbp = priv->ctlsock;
901 * Only allow mesgs to be passed if we have the control socket.
902 * Data sockets can only support the generic messages.
905 mtx_unlock(&priv->mtx);
910 so = pcbp->ng_socket;
911 SOCKBUF_LOCK(&so->so_rcv);
913 /* As long as the race is handled, priv->mtx may be unlocked now. */
914 mtx_unlock(&priv->mtx);
916 #ifdef TRACE_MESSAGES
917 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
919 msg->header.typecookie,
926 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
927 switch (msg->header.cmd) {
928 case NGM_SOCK_CMD_NOLINGER:
929 priv->flags |= NGS_FLAG_NOLINGER;
931 case NGM_SOCK_CMD_LINGER:
932 priv->flags &= ~NGS_FLAG_NOLINGER;
935 error = EINVAL; /* unknown command */
937 SOCKBUF_UNLOCK(&so->so_rcv);
939 /* Free the message and return. */
944 /* Get the return address into a sockaddr. */
945 bzero(&addr, sizeof(addr));
946 addr.sg_len = sizeof(addr);
947 addr.sg_family = AF_NETGRAPH;
948 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
950 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
951 SOCKBUF_UNLOCK(&so->so_rcv);
952 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
958 /* Copy the message itself into an mbuf chain. */
959 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
963 * Here we free the message. We need to do that
964 * regardless of whether we got mbufs.
969 SOCKBUF_UNLOCK(&so->so_rcv);
974 /* Send it up to the socket. */
975 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
977 SOCKBUF_UNLOCK(&so->so_rcv);
982 sorwakeup_locked(so);
988 * Receive data on a hook
991 ngs_rcvdata(hook_p hook, item_p item)
993 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
994 struct ngpcb *const pcbp = priv->datasock;
996 struct sockaddr_ng *addr;
997 char *addrbuf[NG_HOOKSIZ + 4];
1004 /* If there is no data socket, black-hole it. */
1009 so = pcbp->ng_socket;
1011 /* Get the return address into a sockaddr. */
1012 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1013 addr = (struct sockaddr_ng *) addrbuf;
1014 addr->sg_len = addrlen + 3;
1015 addr->sg_family = AF_NETGRAPH;
1016 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1017 addr->sg_data[addrlen] = '\0';
1019 /* Try to tell the socket which hook it came in on. */
1020 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
1030 * Hook disconnection
1032 * For this type, removal of the last link destroys the node
1033 * if the NOLINGER flag is set.
1036 ngs_disconnect(hook_p hook)
1038 node_p node = NG_HOOK_NODE(hook);
1039 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1040 struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
1042 LIST_REMOVE(hp, next);
1043 free(hp, M_NETGRAPH_SOCK);
1045 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1046 if (NG_NODE_NUMHOOKS(node) == 1)
1047 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1049 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1052 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1053 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1054 ng_rmnode_self(node);
1060 * Do local shutdown processing.
1061 * In this case, that involves making sure the socket
1062 * knows we should be shutting down.
1065 ngs_shutdown(node_p node)
1067 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1068 struct ngpcb *dpcbp, *pcbp;
1070 mtx_lock(&priv->mtx);
1071 dpcbp = priv->datasock;
1072 pcbp = priv->ctlsock;
1075 soisdisconnected(dpcbp->ng_socket);
1078 soisdisconnected(pcbp->ng_socket);
1081 NG_NODE_SET_PRIVATE(node, NULL);
1082 ng_socket_free_priv(priv);
1084 NG_NODE_UNREF(node);
1089 ng_socket_item_applied(void *context, int error)
1091 struct ngsock *const priv = (struct ngsock *)context;
1093 mtx_lock(&priv->mtx);
1094 priv->error = error;
1096 mtx_unlock(&priv->mtx);
1101 dummy_disconnect(struct socket *so)
1106 * Control and data socket type descriptors
1108 * XXXRW: Perhaps _close should do something?
1111 static struct pr_usrreqs ngc_usrreqs = {
1113 .pru_attach = ngc_attach,
1114 .pru_bind = ngc_bind,
1115 .pru_connect = ngc_connect,
1116 .pru_detach = ngc_detach,
1117 .pru_disconnect = dummy_disconnect,
1118 .pru_peeraddr = NULL,
1119 .pru_send = ngc_send,
1120 .pru_shutdown = NULL,
1121 .pru_sockaddr = ng_getsockaddr,
1125 static struct pr_usrreqs ngd_usrreqs = {
1127 .pru_attach = ngd_attach,
1129 .pru_connect = ngd_connect,
1130 .pru_detach = ngd_detach,
1131 .pru_disconnect = dummy_disconnect,
1132 .pru_peeraddr = NULL,
1133 .pru_send = ngd_send,
1134 .pru_shutdown = NULL,
1135 .pru_sockaddr = ng_getsockaddr,
1140 * Definitions of protocols supported in the NETGRAPH domain.
1143 extern struct domain ngdomain; /* stop compiler warnings */
1145 static struct protosw ngsw[] = {
1147 .pr_type = SOCK_DGRAM,
1148 .pr_domain = &ngdomain,
1149 .pr_protocol = NG_CONTROL,
1150 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1151 .pr_usrreqs = &ngc_usrreqs
1154 .pr_type = SOCK_DGRAM,
1155 .pr_domain = &ngdomain,
1156 .pr_protocol = NG_DATA,
1157 .pr_flags = PR_ATOMIC | PR_ADDR,
1158 .pr_usrreqs = &ngd_usrreqs
1162 struct domain ngdomain = {
1163 .dom_family = AF_NETGRAPH,
1164 .dom_name = "netgraph",
1165 .dom_protosw = ngsw,
1166 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1170 * Handle loading and unloading for this node type.
1171 * This is to handle auxiliary linkages (e.g protocol domain addition).
1174 ngs_mod_event(module_t mod, int event, void *data)
1180 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1183 /* Ensure there are no open netgraph sockets. */
1184 if (!LIST_EMPTY(&ngsocklist)) {
1189 /* Unregister protocol domain XXX can't do this yet.. */
1200 VNET_DOMAIN_SET(ng);
1202 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1203 static SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1204 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1205 static SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1206 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");