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>
39 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
43 * Netgraph socket nodes
45 * There are two types of netgraph sockets, control and data.
46 * Control sockets have a netgraph node, but data sockets are
47 * parasitic on control sockets, and have no node of their own.
50 #include <sys/param.h>
51 #include <sys/domain.h>
53 #include <sys/kernel.h>
54 #include <sys/linker.h>
56 #include <sys/malloc.h>
58 #include <sys/mutex.h>
60 #include <sys/epoch.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;
241 if (sap->sg_len > NG_NODESIZ + offsetof(struct sockaddr_ng, sg_data)) {
247 * Allocate an expendable buffer for the path, chop off
248 * the sockaddr header, and make sure it's NUL terminated.
250 len = sap->sg_len - offsetof(struct sockaddr_ng, sg_data);
251 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
252 bcopy(sap->sg_data, path, len);
256 * Move the actual message out of mbufs into a linear buffer.
257 * Start by adding up the size of the data. (could use mh_len?)
259 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
263 * Move the data into a linear buffer as well.
264 * Messages are not delivered in mbufs.
266 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
267 m_copydata(m, 0, len, (char *)msg);
269 if (msg->header.version != NG_VERSION) {
270 free(msg, M_NETGRAPH_MSG);
277 * We look into the message and if it mkpeers a node of unknown type, we
278 * try to load it. We need to do this now, in syscall thread, because if
279 * message gets queued and applied later we will get panic.
281 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
282 msg->header.cmd == NGM_MKPEER) {
283 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
285 if (ng_findtype(mkp->type) == NULL) {
286 char filename[NG_TYPESIZ + 3];
289 /* Not found, try to load it as a loadable module. */
290 snprintf(filename, sizeof(filename), "ng_%s",
292 error = kern_kldload(curthread, filename, &fileid);
294 free(msg, M_NETGRAPH_MSG);
298 /* See if type has been loaded successfully. */
299 if (ng_findtype(mkp->type) == NULL) {
300 free(msg, M_NETGRAPH_MSG);
301 (void)kern_kldunload(curthread, fileid,
302 LINKER_UNLOAD_NORMAL);
309 item = ng_package_msg(msg, NG_WAITOK);
310 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
312 #ifdef TRACE_MESSAGES
313 printf("ng_address_path: errx=%d\n", error);
318 #ifdef TRACE_MESSAGES
319 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
320 item->el_dest->nd_ID,
321 msg->header.typecookie,
326 item->el_dest->nd_type->name);
330 * We do not want to return from syscall until the item
331 * is processed by destination node. We register callback
332 * on the item, which will update priv->error when item
334 * If ng_snd_item() has queued item, we sleep until
335 * callback wakes us up.
337 bzero(&apply, sizeof(apply));
338 apply.apply = ng_socket_item_applied;
339 apply.context = priv;
340 item->apply = &apply;
343 error = ng_snd_item(item, 0);
345 mtx_lock(&priv->mtx);
346 if (priv->error == -1)
347 msleep(priv, &priv->mtx, 0, "ngsock", 0);
348 mtx_unlock(&priv->mtx);
349 KASSERT(priv->error != -1,
350 ("ng_socket: priv->error wasn't updated"));
355 free(path, M_NETGRAPH_PATH);
364 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
366 struct ngpcb *const pcbp = sotongpcb(so);
370 return (ng_bind(nam, pcbp));
374 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
377 * At this time refuse to do this.. it used to
378 * do something but it was undocumented and not used.
380 printf("program tried to connect control socket to remote node\n");
384 /***************************************************************
386 ***************************************************************/
389 ngd_attach(struct socket *so, int proto, struct thread *td)
391 struct ngpcb *const pcbp = sotongpcb(so);
395 return (ng_attach_data(so));
399 ngd_detach(struct socket *so)
401 struct ngpcb *const pcbp = sotongpcb(so);
403 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
404 ng_detach_common(pcbp, NG_DATA);
408 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
409 struct mbuf *control, struct thread *td)
411 struct epoch_tracker et;
412 struct ngpcb *const pcbp = sotongpcb(so);
413 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
417 char hookname[NG_HOOKSIZ];
419 if ((pcbp == NULL) || (control != NULL)) {
423 if (pcbp->sockdata == NULL) {
429 len = 0; /* Make compiler happy. */
431 if (sap->sg_len > NG_NODESIZ +
432 offsetof(struct sockaddr_ng, sg_data)) {
436 len = sap->sg_len - offsetof(struct sockaddr_ng, sg_data);
440 * If the user used any of these ways to not specify an address
441 * then handle specially.
443 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
444 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
445 error = EDESTADDRREQ;
449 * If exactly one hook exists, just use it.
450 * Special case to allow write(2) to work on an ng_socket.
452 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
454 if (len >= NG_HOOKSIZ) {
460 * chop off the sockaddr header, and make sure it's NUL
463 bcopy(sap->sg_data, hookname, len);
464 hookname[len] = '\0';
466 /* Find the correct hook from 'hookname' */
467 hook = ng_findhook(pcbp->sockdata->node, hookname);
469 error = EHOSTUNREACH;
475 item = ng_package_data(m, NG_WAITOK);
478 NG_FWD_ITEM_HOOK(error, item, hook);
490 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
492 struct ngpcb *const pcbp = sotongpcb(so);
496 return (ng_connect_data(nam, pcbp));
500 * Used for both data and control sockets
503 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
506 struct sockaddr_ng *sg;
510 pcbp = sotongpcb(so);
511 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
512 /* XXXGL: can this still happen? */
515 sg_len = sizeof(struct sockaddr_ng) + NG_NODESIZ -
517 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
519 mtx_lock(&pcbp->sockdata->mtx);
520 if (pcbp->sockdata->node != NULL) {
521 node_p node = pcbp->sockdata->node;
523 if (NG_NODE_HAS_NAME(node))
524 bcopy(NG_NODE_NAME(node), sg->sg_data,
525 strlen(NG_NODE_NAME(node)));
526 mtx_unlock(&pcbp->sockdata->mtx);
529 sg->sg_family = AF_NETGRAPH;
530 *addr = (struct sockaddr *)sg;
532 mtx_unlock(&pcbp->sockdata->mtx);
541 * Attach a socket to it's protocol specific partner.
542 * For a control socket, actually create a netgraph node and attach
547 ng_attach_cntl(struct socket *so)
554 /* Setup protocol control block */
555 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
557 pcbp = sotongpcb(so);
559 /* Make the generic node components */
560 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
561 ng_detach_common(pcbp, NG_CONTROL);
566 * Allocate node private info and hash. We start
567 * with 16 hash entries, however we may grow later
568 * in ngs_newhook(). We can't predict how much hooks
569 * does this node plan to have.
571 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
572 priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
574 /* Initialize mutex. */
575 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
577 /* Link the pcb the private data. */
578 priv->ctlsock = pcbp;
579 pcbp->sockdata = priv;
582 pcbp->node_id = node->nd_ID; /* hint for netstat(1) */
584 /* Link the node and the private data. */
585 NG_NODE_SET_PRIVATE(priv->node, priv);
586 NG_NODE_REF(priv->node);
593 ng_attach_data(struct socket *so)
595 return (ng_attach_common(so, NG_DATA));
599 * Set up a socket protocol control block.
600 * This code is shared between control and data sockets.
603 ng_attach_common(struct socket *so, int type)
608 /* Standard socket setup stuff. */
609 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
613 /* Allocate the pcb. */
614 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
617 /* Link the pcb and the socket. */
618 so->so_pcb = (caddr_t)pcbp;
619 pcbp->ng_socket = so;
621 /* Add the socket to linked list */
622 mtx_lock(&ngsocketlist_mtx);
623 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
624 mtx_unlock(&ngsocketlist_mtx);
629 * Disassociate the socket from it's protocol specific
630 * partner. If it's attached to a node's private data structure,
631 * then unlink from that too. If we were the last socket attached to it,
632 * then shut down the entire node. Shared code for control and data sockets.
635 ng_detach_common(struct ngpcb *pcbp, int which)
637 struct ngsock *priv = pcbp->sockdata;
640 mtx_lock(&priv->mtx);
644 priv->ctlsock = NULL;
647 priv->datasock = NULL;
650 panic("%s", __func__);
652 pcbp->sockdata = NULL;
655 ng_socket_free_priv(priv);
658 pcbp->ng_socket->so_pcb = NULL;
659 mtx_lock(&ngsocketlist_mtx);
660 LIST_REMOVE(pcbp, socks);
661 mtx_unlock(&ngsocketlist_mtx);
666 * Remove a reference from node private data.
669 ng_socket_free_priv(struct ngsock *priv)
671 mtx_assert(&priv->mtx, MA_OWNED);
675 if (priv->refs == 0) {
676 mtx_destroy(&priv->mtx);
677 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
678 free(priv, M_NETGRAPH_SOCK);
682 if ((priv->refs == 1) && (priv->node != NULL)) {
683 node_p node = priv->node;
686 mtx_unlock(&priv->mtx);
688 ng_rmnode_self(node);
690 mtx_unlock(&priv->mtx);
694 * Connect the data socket to a named control socket node.
697 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
699 struct sockaddr_ng *sap;
705 /* If we are already connected, don't do it again. */
706 if (pcbp->sockdata != NULL)
710 * Find the target (victim) and check it doesn't already have
711 * a data socket. Also check it is a 'socket' type node.
712 * Use ng_package_data() and ng_address_path() to do this.
715 sap = (struct sockaddr_ng *) nam;
716 /* The item will hold the node reference. */
717 item = ng_package_data(NULL, NG_WAITOK);
719 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
720 return (error); /* item is freed on failure */
723 * Extract node from item and free item. Remember we now have
724 * a reference on the node. The item holds it for us.
725 * when we free the item we release the reference.
727 farnode = item->el_dest; /* shortcut */
728 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
729 NG_FREE_ITEM(item); /* drop the reference to the node */
732 priv = NG_NODE_PRIVATE(farnode);
733 if (priv->datasock != NULL) {
734 NG_FREE_ITEM(item); /* drop the reference to the node */
739 * Link the PCB and the private data struct. and note the extra
740 * reference. Drop the extra reference on the node.
742 mtx_lock(&priv->mtx);
743 priv->datasock = pcbp;
744 pcbp->sockdata = priv;
745 pcbp->node_id = priv->node->nd_ID; /* hint for netstat(1) */
747 mtx_unlock(&priv->mtx);
748 NG_FREE_ITEM(item); /* drop the reference to the node */
753 * Binding a socket means giving the corresponding node a name
756 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
758 struct ngsock *const priv = pcbp->sockdata;
759 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
765 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
766 (sap->sg_data[0] == '\0') ||
767 (sap->sg_data[sap->sg_len - 3] != '\0')) {
771 return (ng_name_node(priv->node, sap->sg_data));
774 /***************************************************************
776 ***************************************************************/
779 * You can only create new nodes from the socket end of things.
782 ngs_constructor(node_p nodep)
788 ngs_rehash(node_p node)
790 struct ngsock *priv = NG_NODE_PRIVATE(node);
797 new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
802 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
803 hp = NG_HOOK_PRIVATE(hook);
805 LIST_REMOVE(hp, next);
807 h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
808 LIST_INSERT_HEAD(&new[h], hp, next);
811 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
817 * We allow any hook to be connected to the node.
818 * There is no per-hook private information though.
821 ngs_newhook(node_p node, hook_p hook, const char *name)
823 struct ngsock *const priv = NG_NODE_PRIVATE(node);
827 hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
830 if (node->nd_numhooks * 2 > priv->hmask)
833 h = hash32_str(name, HASHINIT) & priv->hmask;
834 LIST_INSERT_HEAD(&priv->hash[h], hp, next);
835 NG_HOOK_SET_PRIVATE(hook, hp);
841 * If only one hook, allow read(2) and write(2) to work.
844 ngs_connect(hook_p hook)
846 node_p node = NG_HOOK_NODE(hook);
847 struct ngsock *priv = NG_NODE_PRIVATE(node);
849 if ((priv->datasock) && (priv->datasock->ng_socket)) {
850 if (NG_NODE_NUMHOOKS(node) == 1)
851 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
853 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
858 /* Look up hook by name */
860 ngs_findhook(node_p node, const char *name)
862 struct ngsock *priv = NG_NODE_PRIVATE(node);
867 * Microoptimisation for an ng_socket with
868 * a single hook, which is a common case.
870 if (node->nd_numhooks == 1) {
873 hook = LIST_FIRST(&node->nd_hooks);
875 if (strcmp(NG_HOOK_NAME(hook), name) == 0)
881 h = hash32_str(name, HASHINIT) & priv->hmask;
883 LIST_FOREACH(hp, &priv->hash[h], next)
884 if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
891 * Incoming messages get passed up to the control socket.
892 * Unless they are for us specifically (socket_type)
895 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
897 struct ngsock *const priv = NG_NODE_PRIVATE(node);
900 struct sockaddr_ng addr;
903 ng_ID_t retaddr = NGI_RETADDR(item);
907 NGI_GET_MSG(item, msg);
911 * Grab priv->mtx here to prevent destroying of control socket
912 * after checking that priv->ctlsock is not NULL.
914 mtx_lock(&priv->mtx);
915 pcbp = priv->ctlsock;
918 * Only allow mesgs to be passed if we have the control socket.
919 * Data sockets can only support the generic messages.
922 mtx_unlock(&priv->mtx);
927 so = pcbp->ng_socket;
928 SOCKBUF_LOCK(&so->so_rcv);
930 /* As long as the race is handled, priv->mtx may be unlocked now. */
931 mtx_unlock(&priv->mtx);
933 #ifdef TRACE_MESSAGES
934 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
936 msg->header.typecookie,
943 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
944 switch (msg->header.cmd) {
945 case NGM_SOCK_CMD_NOLINGER:
946 priv->flags |= NGS_FLAG_NOLINGER;
948 case NGM_SOCK_CMD_LINGER:
949 priv->flags &= ~NGS_FLAG_NOLINGER;
952 error = EINVAL; /* unknown command */
954 SOCKBUF_UNLOCK(&so->so_rcv);
956 /* Free the message and return. */
961 /* Get the return address into a sockaddr. */
962 bzero(&addr, sizeof(addr));
963 addr.sg_len = sizeof(addr);
964 addr.sg_family = AF_NETGRAPH;
965 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
967 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
968 SOCKBUF_UNLOCK(&so->so_rcv);
969 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
975 /* Copy the message itself into an mbuf chain. */
976 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
980 * Here we free the message. We need to do that
981 * regardless of whether we got mbufs.
986 SOCKBUF_UNLOCK(&so->so_rcv);
991 /* Send it up to the socket. */
992 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
994 soroverflow_locked(so);
1000 /* sorwakeup_locked () releases the lock internally. */
1001 sorwakeup_locked(so);
1007 * Receive data on a hook
1010 ngs_rcvdata(hook_p hook, item_p item)
1012 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1013 struct ngpcb *const pcbp = priv->datasock;
1015 struct sockaddr_ng *addr;
1016 char *addrbuf[NG_HOOKSIZ + 4];
1023 /* If there is no data socket, black-hole it. */
1028 so = pcbp->ng_socket;
1030 /* Get the return address into a sockaddr. */
1031 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1032 addr = (struct sockaddr_ng *) addrbuf;
1033 addr->sg_len = addrlen + 3;
1034 addr->sg_family = AF_NETGRAPH;
1035 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1036 addr->sg_data[addrlen] = '\0';
1038 /* Try to tell the socket which hook it came in on. */
1039 SOCKBUF_LOCK(&so->so_rcv);
1040 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)addr, m,
1042 SOCKBUF_UNLOCK(&so->so_rcv);
1048 /* sorwakeup_locked () releases the lock internally. */
1049 sorwakeup_locked(so);
1054 * Hook disconnection
1056 * For this type, removal of the last link destroys the node
1057 * if the NOLINGER flag is set.
1060 ngs_disconnect(hook_p hook)
1062 node_p node = NG_HOOK_NODE(hook);
1063 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1064 struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
1066 LIST_REMOVE(hp, next);
1067 free(hp, M_NETGRAPH_SOCK);
1069 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1070 if (NG_NODE_NUMHOOKS(node) == 1)
1071 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1073 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1076 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1077 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1078 ng_rmnode_self(node);
1084 * Do local shutdown processing.
1085 * In this case, that involves making sure the socket
1086 * knows we should be shutting down.
1089 ngs_shutdown(node_p node)
1091 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1092 struct ngpcb *dpcbp, *pcbp;
1094 mtx_lock(&priv->mtx);
1095 dpcbp = priv->datasock;
1096 pcbp = priv->ctlsock;
1099 soisdisconnected(dpcbp->ng_socket);
1102 soisdisconnected(pcbp->ng_socket);
1105 NG_NODE_SET_PRIVATE(node, NULL);
1106 ng_socket_free_priv(priv);
1108 NG_NODE_UNREF(node);
1113 ng_socket_item_applied(void *context, int error)
1115 struct ngsock *const priv = (struct ngsock *)context;
1117 mtx_lock(&priv->mtx);
1118 priv->error = error;
1120 mtx_unlock(&priv->mtx);
1125 dummy_disconnect(struct socket *so)
1131 * Definitions of protocols supported in the NETGRAPH domain.
1132 * Control and data socket type descriptors
1134 * XXXRW: Perhaps _close should do something?
1136 static struct protosw ngcontrol_protosw = {
1137 .pr_type = SOCK_DGRAM,
1138 .pr_protocol = NG_CONTROL,
1139 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1140 .pr_attach = ngc_attach,
1141 .pr_bind = ngc_bind,
1142 .pr_connect = ngc_connect,
1143 .pr_detach = ngc_detach,
1144 .pr_disconnect = dummy_disconnect,
1145 .pr_send = ngc_send,
1146 .pr_sockaddr = ng_getsockaddr,
1148 static struct protosw ngdata_protosw = {
1149 .pr_type = SOCK_DGRAM,
1150 .pr_protocol = NG_DATA,
1151 .pr_flags = PR_ATOMIC | PR_ADDR,
1152 .pr_attach = ngd_attach,
1153 .pr_connect = ngd_connect,
1154 .pr_detach = ngd_detach,
1155 .pr_disconnect = dummy_disconnect,
1156 .pr_send = ngd_send,
1157 .pr_sockaddr = ng_getsockaddr,
1160 static struct domain ngdomain = {
1161 .dom_family = AF_NETGRAPH,
1162 .dom_name = "netgraph",
1164 .dom_protosw = { &ngcontrol_protosw, &ngdata_protosw },
1168 * Handle loading and unloading for this node type.
1169 * This is to handle auxiliary linkages (e.g protocol domain addition).
1172 ngs_mod_event(module_t mod, int event, void *data)
1178 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1181 /* Ensure there are no open netgraph sockets. */
1182 if (!LIST_EMPTY(&ngsocklist)) {
1187 /* Unregister protocol domain XXX can't do this yet.. */
1200 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, AF_NETGRAPH, "");
1201 static SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1203 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_DATA, "");
1204 static SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1206 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_CONTROL, "");