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;
244 * Allocate an expendable buffer for the path, chop off
245 * the sockaddr header, and make sure it's NUL terminated.
247 len = sap->sg_len - 2;
248 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
249 bcopy(sap->sg_data, path, len);
253 * Move the actual message out of mbufs into a linear buffer.
254 * Start by adding up the size of the data. (could use mh_len?)
256 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
260 * Move the data into a linear buffer as well.
261 * Messages are not delivered in mbufs.
263 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
264 m_copydata(m, 0, len, (char *)msg);
266 if (msg->header.version != NG_VERSION) {
267 free(msg, M_NETGRAPH_MSG);
274 * We look into the message and if it mkpeers a node of unknown type, we
275 * try to load it. We need to do this now, in syscall thread, because if
276 * message gets queued and applied later we will get panic.
278 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
279 msg->header.cmd == NGM_MKPEER) {
280 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
282 if (ng_findtype(mkp->type) == NULL) {
283 char filename[NG_TYPESIZ + 3];
286 /* Not found, try to load it as a loadable module. */
287 snprintf(filename, sizeof(filename), "ng_%s",
289 error = kern_kldload(curthread, filename, &fileid);
291 free(msg, M_NETGRAPH_MSG);
295 /* See if type has been loaded successfully. */
296 if (ng_findtype(mkp->type) == NULL) {
297 free(msg, M_NETGRAPH_MSG);
298 (void)kern_kldunload(curthread, fileid,
299 LINKER_UNLOAD_NORMAL);
306 item = ng_package_msg(msg, NG_WAITOK);
307 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
309 #ifdef TRACE_MESSAGES
310 printf("ng_address_path: errx=%d\n", error);
315 #ifdef TRACE_MESSAGES
316 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
317 item->el_dest->nd_ID,
318 msg->header.typecookie,
323 item->el_dest->nd_type->name);
327 * We do not want to return from syscall until the item
328 * is processed by destination node. We register callback
329 * on the item, which will update priv->error when item
331 * If ng_snd_item() has queued item, we sleep until
332 * callback wakes us up.
334 bzero(&apply, sizeof(apply));
335 apply.apply = ng_socket_item_applied;
336 apply.context = priv;
337 item->apply = &apply;
340 error = ng_snd_item(item, 0);
342 mtx_lock(&priv->mtx);
343 if (priv->error == -1)
344 msleep(priv, &priv->mtx, 0, "ngsock", 0);
345 mtx_unlock(&priv->mtx);
346 KASSERT(priv->error != -1,
347 ("ng_socket: priv->error wasn't updated"));
352 free(path, M_NETGRAPH_PATH);
361 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
363 struct ngpcb *const pcbp = sotongpcb(so);
367 return (ng_bind(nam, pcbp));
371 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
374 * At this time refuse to do this.. it used to
375 * do something but it was undocumented and not used.
377 printf("program tried to connect control socket to remote node\n");
381 /***************************************************************
383 ***************************************************************/
386 ngd_attach(struct socket *so, int proto, struct thread *td)
388 struct ngpcb *const pcbp = sotongpcb(so);
392 return (ng_attach_data(so));
396 ngd_detach(struct socket *so)
398 struct ngpcb *const pcbp = sotongpcb(so);
400 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
401 ng_detach_common(pcbp, NG_DATA);
405 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
406 struct mbuf *control, struct thread *td)
408 struct epoch_tracker et;
409 struct ngpcb *const pcbp = sotongpcb(so);
410 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
414 char hookname[NG_HOOKSIZ];
416 if ((pcbp == NULL) || (control != NULL)) {
420 if (pcbp->sockdata == NULL) {
426 len = 0; /* Make compiler happy. */
428 len = sap->sg_len - 2;
431 * If the user used any of these ways to not specify an address
432 * then handle specially.
434 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
435 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
436 error = EDESTADDRREQ;
440 * If exactly one hook exists, just use it.
441 * Special case to allow write(2) to work on an ng_socket.
443 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
445 if (len >= NG_HOOKSIZ) {
451 * chop off the sockaddr header, and make sure it's NUL
454 bcopy(sap->sg_data, hookname, len);
455 hookname[len] = '\0';
457 /* Find the correct hook from 'hookname' */
458 hook = ng_findhook(pcbp->sockdata->node, hookname);
460 error = EHOSTUNREACH;
466 item = ng_package_data(m, NG_WAITOK);
469 NG_FWD_ITEM_HOOK(error, item, hook);
481 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
483 struct ngpcb *const pcbp = sotongpcb(so);
487 return (ng_connect_data(nam, pcbp));
491 * Used for both data and control sockets
494 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
497 struct sockaddr_ng *sg;
501 pcbp = sotongpcb(so);
502 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
503 /* XXXGL: can this still happen? */
506 sg_len = sizeof(struct sockaddr_ng) + NG_NODESIZ -
508 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
510 mtx_lock(&pcbp->sockdata->mtx);
511 if (pcbp->sockdata->node != NULL) {
512 node_p node = pcbp->sockdata->node;
514 if (NG_NODE_HAS_NAME(node))
515 bcopy(NG_NODE_NAME(node), sg->sg_data,
516 strlen(NG_NODE_NAME(node)));
517 mtx_unlock(&pcbp->sockdata->mtx);
520 sg->sg_family = AF_NETGRAPH;
521 *addr = (struct sockaddr *)sg;
523 mtx_unlock(&pcbp->sockdata->mtx);
532 * Attach a socket to it's protocol specific partner.
533 * For a control socket, actually create a netgraph node and attach
538 ng_attach_cntl(struct socket *so)
545 /* Setup protocol control block */
546 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
548 pcbp = sotongpcb(so);
550 /* Make the generic node components */
551 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
552 ng_detach_common(pcbp, NG_CONTROL);
557 * Allocate node private info and hash. We start
558 * with 16 hash entries, however we may grow later
559 * in ngs_newhook(). We can't predict how much hooks
560 * does this node plan to have.
562 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
563 priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
565 /* Initialize mutex. */
566 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
568 /* Link the pcb the private data. */
569 priv->ctlsock = pcbp;
570 pcbp->sockdata = priv;
573 pcbp->node_id = node->nd_ID; /* hint for netstat(1) */
575 /* Link the node and the private data. */
576 NG_NODE_SET_PRIVATE(priv->node, priv);
577 NG_NODE_REF(priv->node);
584 ng_attach_data(struct socket *so)
586 return (ng_attach_common(so, NG_DATA));
590 * Set up a socket protocol control block.
591 * This code is shared between control and data sockets.
594 ng_attach_common(struct socket *so, int type)
599 /* Standard socket setup stuff. */
600 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
604 /* Allocate the pcb. */
605 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
608 /* Link the pcb and the socket. */
609 so->so_pcb = (caddr_t)pcbp;
610 pcbp->ng_socket = so;
612 /* Add the socket to linked list */
613 mtx_lock(&ngsocketlist_mtx);
614 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
615 mtx_unlock(&ngsocketlist_mtx);
620 * Disassociate the socket from it's protocol specific
621 * partner. If it's attached to a node's private data structure,
622 * then unlink from that too. If we were the last socket attached to it,
623 * then shut down the entire node. Shared code for control and data sockets.
626 ng_detach_common(struct ngpcb *pcbp, int which)
628 struct ngsock *priv = pcbp->sockdata;
631 mtx_lock(&priv->mtx);
635 priv->ctlsock = NULL;
638 priv->datasock = NULL;
641 panic("%s", __func__);
643 pcbp->sockdata = NULL;
646 ng_socket_free_priv(priv);
649 pcbp->ng_socket->so_pcb = NULL;
650 mtx_lock(&ngsocketlist_mtx);
651 LIST_REMOVE(pcbp, socks);
652 mtx_unlock(&ngsocketlist_mtx);
657 * Remove a reference from node private data.
660 ng_socket_free_priv(struct ngsock *priv)
662 mtx_assert(&priv->mtx, MA_OWNED);
666 if (priv->refs == 0) {
667 mtx_destroy(&priv->mtx);
668 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
669 free(priv, M_NETGRAPH_SOCK);
673 if ((priv->refs == 1) && (priv->node != NULL)) {
674 node_p node = priv->node;
677 mtx_unlock(&priv->mtx);
679 ng_rmnode_self(node);
681 mtx_unlock(&priv->mtx);
685 * Connect the data socket to a named control socket node.
688 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
690 struct sockaddr_ng *sap;
696 /* If we are already connected, don't do it again. */
697 if (pcbp->sockdata != NULL)
701 * Find the target (victim) and check it doesn't already have
702 * a data socket. Also check it is a 'socket' type node.
703 * Use ng_package_data() and ng_address_path() to do this.
706 sap = (struct sockaddr_ng *) nam;
707 /* The item will hold the node reference. */
708 item = ng_package_data(NULL, NG_WAITOK);
710 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
711 return (error); /* item is freed on failure */
714 * Extract node from item and free item. Remember we now have
715 * a reference on the node. The item holds it for us.
716 * when we free the item we release the reference.
718 farnode = item->el_dest; /* shortcut */
719 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
720 NG_FREE_ITEM(item); /* drop the reference to the node */
723 priv = NG_NODE_PRIVATE(farnode);
724 if (priv->datasock != NULL) {
725 NG_FREE_ITEM(item); /* drop the reference to the node */
730 * Link the PCB and the private data struct. and note the extra
731 * reference. Drop the extra reference on the node.
733 mtx_lock(&priv->mtx);
734 priv->datasock = pcbp;
735 pcbp->sockdata = priv;
736 pcbp->node_id = priv->node->nd_ID; /* hint for netstat(1) */
738 mtx_unlock(&priv->mtx);
739 NG_FREE_ITEM(item); /* drop the reference to the node */
744 * Binding a socket means giving the corresponding node a name
747 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
749 struct ngsock *const priv = pcbp->sockdata;
750 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
756 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
757 (sap->sg_data[0] == '\0') ||
758 (sap->sg_data[sap->sg_len - 3] != '\0')) {
762 return (ng_name_node(priv->node, sap->sg_data));
765 /***************************************************************
767 ***************************************************************/
770 * You can only create new nodes from the socket end of things.
773 ngs_constructor(node_p nodep)
779 ngs_rehash(node_p node)
781 struct ngsock *priv = NG_NODE_PRIVATE(node);
788 new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
793 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
794 hp = NG_HOOK_PRIVATE(hook);
796 LIST_REMOVE(hp, next);
798 h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
799 LIST_INSERT_HEAD(&new[h], hp, next);
802 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
808 * We allow any hook to be connected to the node.
809 * There is no per-hook private information though.
812 ngs_newhook(node_p node, hook_p hook, const char *name)
814 struct ngsock *const priv = NG_NODE_PRIVATE(node);
818 hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
821 if (node->nd_numhooks * 2 > priv->hmask)
824 h = hash32_str(name, HASHINIT) & priv->hmask;
825 LIST_INSERT_HEAD(&priv->hash[h], hp, next);
826 NG_HOOK_SET_PRIVATE(hook, hp);
832 * If only one hook, allow read(2) and write(2) to work.
835 ngs_connect(hook_p hook)
837 node_p node = NG_HOOK_NODE(hook);
838 struct ngsock *priv = NG_NODE_PRIVATE(node);
840 if ((priv->datasock) && (priv->datasock->ng_socket)) {
841 if (NG_NODE_NUMHOOKS(node) == 1)
842 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
844 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
849 /* Look up hook by name */
851 ngs_findhook(node_p node, const char *name)
853 struct ngsock *priv = NG_NODE_PRIVATE(node);
858 * Microoptimisation for an ng_socket with
859 * a single hook, which is a common case.
861 if (node->nd_numhooks == 1) {
864 hook = LIST_FIRST(&node->nd_hooks);
866 if (strcmp(NG_HOOK_NAME(hook), name) == 0)
872 h = hash32_str(name, HASHINIT) & priv->hmask;
874 LIST_FOREACH(hp, &priv->hash[h], next)
875 if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
882 * Incoming messages get passed up to the control socket.
883 * Unless they are for us specifically (socket_type)
886 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
888 struct ngsock *const priv = NG_NODE_PRIVATE(node);
891 struct sockaddr_ng addr;
894 ng_ID_t retaddr = NGI_RETADDR(item);
898 NGI_GET_MSG(item, msg);
902 * Grab priv->mtx here to prevent destroying of control socket
903 * after checking that priv->ctlsock is not NULL.
905 mtx_lock(&priv->mtx);
906 pcbp = priv->ctlsock;
909 * Only allow mesgs to be passed if we have the control socket.
910 * Data sockets can only support the generic messages.
913 mtx_unlock(&priv->mtx);
918 so = pcbp->ng_socket;
919 SOCKBUF_LOCK(&so->so_rcv);
921 /* As long as the race is handled, priv->mtx may be unlocked now. */
922 mtx_unlock(&priv->mtx);
924 #ifdef TRACE_MESSAGES
925 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
927 msg->header.typecookie,
934 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
935 switch (msg->header.cmd) {
936 case NGM_SOCK_CMD_NOLINGER:
937 priv->flags |= NGS_FLAG_NOLINGER;
939 case NGM_SOCK_CMD_LINGER:
940 priv->flags &= ~NGS_FLAG_NOLINGER;
943 error = EINVAL; /* unknown command */
945 SOCKBUF_UNLOCK(&so->so_rcv);
947 /* Free the message and return. */
952 /* Get the return address into a sockaddr. */
953 bzero(&addr, sizeof(addr));
954 addr.sg_len = sizeof(addr);
955 addr.sg_family = AF_NETGRAPH;
956 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
958 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
959 SOCKBUF_UNLOCK(&so->so_rcv);
960 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
966 /* Copy the message itself into an mbuf chain. */
967 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
971 * Here we free the message. We need to do that
972 * regardless of whether we got mbufs.
977 SOCKBUF_UNLOCK(&so->so_rcv);
982 /* Send it up to the socket. */
983 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
985 SOCKBUF_UNLOCK(&so->so_rcv);
991 /* sorwakeup_locked () releases the lock internally. */
992 sorwakeup_locked(so);
998 * Receive data on a hook
1001 ngs_rcvdata(hook_p hook, item_p item)
1003 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1004 struct ngpcb *const pcbp = priv->datasock;
1006 struct sockaddr_ng *addr;
1007 char *addrbuf[NG_HOOKSIZ + 4];
1014 /* If there is no data socket, black-hole it. */
1019 so = pcbp->ng_socket;
1021 /* Get the return address into a sockaddr. */
1022 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1023 addr = (struct sockaddr_ng *) addrbuf;
1024 addr->sg_len = addrlen + 3;
1025 addr->sg_family = AF_NETGRAPH;
1026 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1027 addr->sg_data[addrlen] = '\0';
1029 /* Try to tell the socket which hook it came in on. */
1030 SOCKBUF_LOCK(&so->so_rcv);
1031 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)addr, m,
1033 SOCKBUF_UNLOCK(&so->so_rcv);
1039 /* sorwakeup_locked () releases the lock internally. */
1040 sorwakeup_locked(so);
1045 * Hook disconnection
1047 * For this type, removal of the last link destroys the node
1048 * if the NOLINGER flag is set.
1051 ngs_disconnect(hook_p hook)
1053 node_p node = NG_HOOK_NODE(hook);
1054 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1055 struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
1057 LIST_REMOVE(hp, next);
1058 free(hp, M_NETGRAPH_SOCK);
1060 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1061 if (NG_NODE_NUMHOOKS(node) == 1)
1062 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1064 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1067 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1068 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1069 ng_rmnode_self(node);
1075 * Do local shutdown processing.
1076 * In this case, that involves making sure the socket
1077 * knows we should be shutting down.
1080 ngs_shutdown(node_p node)
1082 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1083 struct ngpcb *dpcbp, *pcbp;
1085 mtx_lock(&priv->mtx);
1086 dpcbp = priv->datasock;
1087 pcbp = priv->ctlsock;
1090 soisdisconnected(dpcbp->ng_socket);
1093 soisdisconnected(pcbp->ng_socket);
1096 NG_NODE_SET_PRIVATE(node, NULL);
1097 ng_socket_free_priv(priv);
1099 NG_NODE_UNREF(node);
1104 ng_socket_item_applied(void *context, int error)
1106 struct ngsock *const priv = (struct ngsock *)context;
1108 mtx_lock(&priv->mtx);
1109 priv->error = error;
1111 mtx_unlock(&priv->mtx);
1116 dummy_disconnect(struct socket *so)
1121 * Control and data socket type descriptors
1123 * XXXRW: Perhaps _close should do something?
1126 static struct pr_usrreqs ngc_usrreqs = {
1128 .pru_attach = ngc_attach,
1129 .pru_bind = ngc_bind,
1130 .pru_connect = ngc_connect,
1131 .pru_detach = ngc_detach,
1132 .pru_disconnect = dummy_disconnect,
1133 .pru_peeraddr = NULL,
1134 .pru_send = ngc_send,
1135 .pru_shutdown = NULL,
1136 .pru_sockaddr = ng_getsockaddr,
1140 static struct pr_usrreqs ngd_usrreqs = {
1142 .pru_attach = ngd_attach,
1144 .pru_connect = ngd_connect,
1145 .pru_detach = ngd_detach,
1146 .pru_disconnect = dummy_disconnect,
1147 .pru_peeraddr = NULL,
1148 .pru_send = ngd_send,
1149 .pru_shutdown = NULL,
1150 .pru_sockaddr = ng_getsockaddr,
1155 * Definitions of protocols supported in the NETGRAPH domain.
1158 extern struct domain ngdomain; /* stop compiler warnings */
1160 static struct protosw ngsw[] = {
1162 .pr_type = SOCK_DGRAM,
1163 .pr_domain = &ngdomain,
1164 .pr_protocol = NG_CONTROL,
1165 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1166 .pr_usrreqs = &ngc_usrreqs
1169 .pr_type = SOCK_DGRAM,
1170 .pr_domain = &ngdomain,
1171 .pr_protocol = NG_DATA,
1172 .pr_flags = PR_ATOMIC | PR_ADDR,
1173 .pr_usrreqs = &ngd_usrreqs
1177 struct domain ngdomain = {
1178 .dom_family = AF_NETGRAPH,
1179 .dom_name = "netgraph",
1180 .dom_protosw = ngsw,
1181 .dom_protoswNPROTOSW = &ngsw[nitems(ngsw)]
1185 * Handle loading and unloading for this node type.
1186 * This is to handle auxiliary linkages (e.g protocol domain addition).
1189 ngs_mod_event(module_t mod, int event, void *data)
1195 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1198 /* Ensure there are no open netgraph sockets. */
1199 if (!LIST_EMPTY(&ngsocklist)) {
1204 /* Unregister protocol domain XXX can't do this yet.. */
1215 VNET_DOMAIN_SET(ng);
1217 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, AF_NETGRAPH, "");
1218 static SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1220 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_DATA, "");
1221 static SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1223 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_CONTROL, "");