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>
54 #include <sys/kernel.h>
55 #include <sys/linker.h>
57 #include <sys/malloc.h>
59 #include <sys/mutex.h>
61 #include <sys/protosw.h>
62 #include <sys/queue.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/syscallsubr.h>
66 #include <sys/sysctl.h>
68 #include <sys/vnode.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 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
80 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_rcvdata_t ngs_rcvdata;
119 static ng_disconnect_t ngs_disconnect;
121 /* Internal methods */
122 static int ng_attach_data(struct socket *so);
123 static int ng_attach_cntl(struct socket *so);
124 static int ng_attach_common(struct socket *so, int type);
125 static void ng_detach_common(struct ngpcb *pcbp, int type);
126 static void ng_socket_free_priv(struct ngsock *priv);
128 static int ng_internalize(struct mbuf *m, struct thread *p);
130 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
131 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
133 static int ngs_mod_event(module_t mod, int event, void *data);
134 static void ng_socket_item_applied(void *context, int error);
136 /* Netgraph type descriptor */
137 static struct ng_type typestruct = {
138 .version = NG_ABI_VERSION,
139 .name = NG_SOCKET_NODE_TYPE,
140 .mod_event = ngs_mod_event,
141 .constructor = ngs_constructor,
142 .rcvmsg = ngs_rcvmsg,
143 .shutdown = ngs_shutdown,
144 .newhook = ngs_newhook,
145 .connect = ngs_connect,
146 .rcvdata = ngs_rcvdata,
147 .disconnect = ngs_disconnect,
149 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
152 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
153 SYSCTL_ULONG(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
154 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
155 static u_long ngpdg_recvspace = 20 * 1024;
156 SYSCTL_ULONG(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
157 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
159 /* List of all sockets (for netstat -f netgraph) */
160 static LIST_HEAD(, ngpcb) ngsocklist;
162 static struct mtx ngsocketlist_mtx;
164 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
166 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
171 /***************************************************************
173 ***************************************************************/
176 ngc_attach(struct socket *so, int proto, struct thread *td)
178 struct ngpcb *const pcbp = sotongpcb(so);
181 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
186 return (ng_attach_cntl(so));
190 ngc_detach(struct socket *so)
192 struct ngpcb *const pcbp = sotongpcb(so);
194 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
195 ng_detach_common(pcbp, NG_CONTROL);
199 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
200 struct mbuf *control, struct thread *td)
202 struct ngpcb *const pcbp = sotongpcb(so);
203 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
204 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
210 struct ng_apply_info apply;
213 if (control && (error = ng_internalize(control, td))) {
214 if (pcbp->sockdata == NULL) {
226 /* Require destination as there may be >= 1 hooks on this node. */
228 error = EDESTADDRREQ;
233 * Allocate an expendable buffer for the path, chop off
234 * the sockaddr header, and make sure it's NUL terminated.
236 len = sap->sg_len - 2;
237 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
238 bcopy(sap->sg_data, path, len);
242 * Move the actual message out of mbufs into a linear buffer.
243 * Start by adding up the size of the data. (could use mh_len?)
245 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
249 * Move the data into a linear buffer as well.
250 * Messages are not delivered in mbufs.
252 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
253 m_copydata(m, 0, len, (char *)msg);
255 if (msg->header.version != NG_VERSION) {
256 free(msg, M_NETGRAPH_MSG);
263 * We look into the message and if it mkpeers a node of unknown type, we
264 * try to load it. We need to do this now, in syscall thread, because if
265 * message gets queued and applied later we will get panic.
267 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
268 msg->header.cmd == NGM_MKPEER) {
269 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
271 if (ng_findtype(mkp->type) == NULL) {
272 char filename[NG_TYPESIZ + 3];
275 /* Not found, try to load it as a loadable module. */
276 snprintf(filename, sizeof(filename), "ng_%s",
278 error = kern_kldload(curthread, filename, &fileid);
280 free(msg, M_NETGRAPH_MSG);
284 /* See if type has been loaded successfully. */
285 if (ng_findtype(mkp->type) == NULL) {
286 free(msg, M_NETGRAPH_MSG);
287 (void)kern_kldunload(curthread, fileid,
288 LINKER_UNLOAD_NORMAL);
295 item = ng_package_msg(msg, M_WAITOK);
296 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
298 #ifdef TRACE_MESSAGES
299 printf("ng_address_path: errx=%d\n", error);
304 #ifdef TRACE_MESSAGES
305 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
306 item->el_dest->nd_ID,
307 msg->header.typecookie,
312 item->el_dest->nd_type->name);
316 * We do not want to return from syscall until the item
317 * is processed by destination node. We register callback
318 * on the item, which will update priv->error when item
320 * If ng_snd_item() has queued item, we sleep until
321 * callback wakes us up.
323 bzero(&apply, sizeof(apply));
324 apply.apply = ng_socket_item_applied;
325 apply.context = priv;
326 item->apply = &apply;
329 error = ng_snd_item(item, 0);
331 mtx_lock(&priv->mtx);
332 if (priv->error == -1)
333 msleep(priv, &priv->mtx, 0, "ngsock", 0);
334 mtx_unlock(&priv->mtx);
335 KASSERT(priv->error != -1,
336 ("ng_socket: priv->error wasn't updated"));
341 free(path, M_NETGRAPH_PATH);
350 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
352 struct ngpcb *const pcbp = sotongpcb(so);
356 return (ng_bind(nam, pcbp));
360 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
363 * At this time refuse to do this.. it used to
364 * do something but it was undocumented and not used.
366 printf("program tried to connect control socket to remote node\n");
370 /***************************************************************
372 ***************************************************************/
375 ngd_attach(struct socket *so, int proto, struct thread *td)
377 struct ngpcb *const pcbp = sotongpcb(so);
381 return (ng_attach_data(so));
385 ngd_detach(struct socket *so)
387 struct ngpcb *const pcbp = sotongpcb(so);
389 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
390 ng_detach_common(pcbp, NG_DATA);
394 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
395 struct mbuf *control, struct thread *td)
397 struct ngpcb *const pcbp = sotongpcb(so);
398 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
401 char hookname[NG_HOOKSIZ];
403 if ((pcbp == NULL) || (control != NULL)) {
407 if (pcbp->sockdata == NULL) {
413 len = 0; /* Make compiler happy. */
415 len = sap->sg_len - 2;
418 * If the user used any of these ways to not specify an address
419 * then handle specially.
421 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
422 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
423 error = EDESTADDRREQ;
427 * If exactly one hook exists, just use it.
428 * Special case to allow write(2) to work on an ng_socket.
430 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
432 if (len >= NG_HOOKSIZ) {
438 * chop off the sockaddr header, and make sure it's NUL
441 bcopy(sap->sg_data, hookname, len);
442 hookname[len] = '\0';
444 /* Find the correct hook from 'hookname' */
445 hook = ng_findhook(pcbp->sockdata->node, hookname);
447 error = EHOSTUNREACH;
453 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
464 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
466 struct ngpcb *const pcbp = sotongpcb(so);
470 return (ng_connect_data(nam, pcbp));
474 * Used for both data and control sockets
477 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
480 struct sockaddr_ng *sg;
484 /* Why isn't sg_data a `char[1]' ? :-( */
485 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
487 pcbp = sotongpcb(so);
488 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
489 /* XXXGL: can this still happen? */
492 mtx_lock(&pcbp->sockdata->mtx);
493 if (pcbp->sockdata->node != NULL) {
494 node_p node = pcbp->sockdata->node;
495 int namelen = 0; /* silence compiler! */
497 if (NG_NODE_HAS_NAME(node))
498 sg_len += namelen = strlen(NG_NODE_NAME(node));
500 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
502 if (NG_NODE_HAS_NAME(node))
503 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
506 sg->sg_family = AF_NETGRAPH;
507 *addr = (struct sockaddr *)sg;
508 mtx_unlock(&pcbp->sockdata->mtx);
510 mtx_unlock(&pcbp->sockdata->mtx);
518 * Attach a socket to it's protocol specific partner.
519 * For a control socket, actually create a netgraph node and attach
524 ng_attach_cntl(struct socket *so)
531 /* Setup protocol control block */
532 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
534 pcbp = sotongpcb(so);
536 /* Make the generic node components */
537 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
538 ng_detach_common(pcbp, NG_CONTROL);
542 /* Allocate node private info */
543 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
545 /* Initialize mutex. */
546 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
548 /* Link the pcb the private data. */
549 priv->ctlsock = pcbp;
550 pcbp->sockdata = priv;
554 /* Store a hint for netstat(1). */
555 priv->node_id = priv->node->nd_ID;
557 /* Link the node and the private data. */
558 NG_NODE_SET_PRIVATE(priv->node, priv);
559 NG_NODE_REF(priv->node);
566 ng_attach_data(struct socket *so)
568 return (ng_attach_common(so, NG_DATA));
572 * Set up a socket protocol control block.
573 * This code is shared between control and data sockets.
576 ng_attach_common(struct socket *so, int type)
581 /* Standard socket setup stuff. */
582 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
586 /* Allocate the pcb. */
587 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
590 /* Link the pcb and the socket. */
591 so->so_pcb = (caddr_t)pcbp;
592 pcbp->ng_socket = so;
594 /* Add the socket to linked list */
595 mtx_lock(&ngsocketlist_mtx);
596 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
597 mtx_unlock(&ngsocketlist_mtx);
602 * Disassociate the socket from it's protocol specific
603 * partner. If it's attached to a node's private data structure,
604 * then unlink from that too. If we were the last socket attached to it,
605 * then shut down the entire node. Shared code for control and data sockets.
608 ng_detach_common(struct ngpcb *pcbp, int which)
610 struct ngsock *priv = pcbp->sockdata;
613 mtx_lock(&priv->mtx);
617 priv->ctlsock = NULL;
620 priv->datasock = NULL;
623 panic("%s", __func__);
625 pcbp->sockdata = NULL;
627 ng_socket_free_priv(priv);
630 pcbp->ng_socket->so_pcb = NULL;
631 mtx_lock(&ngsocketlist_mtx);
632 LIST_REMOVE(pcbp, socks);
633 mtx_unlock(&ngsocketlist_mtx);
638 * Remove a reference from node private data.
641 ng_socket_free_priv(struct ngsock *priv)
643 mtx_assert(&priv->mtx, MA_OWNED);
647 if (priv->refs == 0) {
648 mtx_destroy(&priv->mtx);
649 free(priv, M_NETGRAPH_SOCK);
653 if ((priv->refs == 1) && (priv->node != NULL)) {
654 node_p node = priv->node;
657 mtx_unlock(&priv->mtx);
659 ng_rmnode_self(node);
661 mtx_unlock(&priv->mtx);
666 * File descriptors can be passed into an AF_NETGRAPH socket.
667 * Note, that file descriptors cannot be passed OUT.
668 * Only character device descriptors are accepted.
669 * Character devices are useful to connect a graph to a device,
670 * which after all is the purpose of this whole system.
673 ng_internalize(struct mbuf *control, struct thread *td)
675 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
681 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
682 cm->cmsg_len != control->m_len) {
687 /* Check there is only one FD. XXX what would more than one signify? */
688 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
694 /* Check that the FD given is legit. and change it to a pointer to a
697 if ((error = fget(td, fd, 0, &fp)) != 0)
700 /* Depending on what kind of resource it is, act differently. For
701 * devices, we treat it as a file. For an AF_NETGRAPH socket,
702 * shortcut straight to the node. */
703 switch (fp->f_type) {
706 if (vn && (vn->v_type == VCHR)) {
707 /* for a VCHR, actually reference the FILE */
709 /* XXX then what :) */
710 /* how to pass on to other modules? */
728 * Connect the data socket to a named control socket node.
731 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
733 struct sockaddr_ng *sap;
739 /* If we are already connected, don't do it again. */
740 if (pcbp->sockdata != NULL)
744 * Find the target (victim) and check it doesn't already have
745 * a data socket. Also check it is a 'socket' type node.
746 * Use ng_package_data() and ng_address_path() to do this.
749 sap = (struct sockaddr_ng *) nam;
750 /* The item will hold the node reference. */
751 item = ng_package_data(NULL, NG_WAITOK);
753 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
754 return (error); /* item is freed on failure */
757 * Extract node from item and free item. Remember we now have
758 * a reference on the node. The item holds it for us.
759 * when we free the item we release the reference.
761 farnode = item->el_dest; /* shortcut */
762 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
763 NG_FREE_ITEM(item); /* drop the reference to the node */
766 priv = NG_NODE_PRIVATE(farnode);
767 if (priv->datasock != NULL) {
768 NG_FREE_ITEM(item); /* drop the reference to the node */
773 * Link the PCB and the private data struct. and note the extra
774 * reference. Drop the extra reference on the node.
776 mtx_lock(&priv->mtx);
777 priv->datasock = pcbp;
778 pcbp->sockdata = priv;
780 mtx_unlock(&priv->mtx);
781 NG_FREE_ITEM(item); /* drop the reference to the node */
786 * Binding a socket means giving the corresponding node a name
789 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
791 struct ngsock *const priv = pcbp->sockdata;
792 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
798 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
799 (sap->sg_data[0] == '\0') ||
800 (sap->sg_data[sap->sg_len - 3] != '\0')) {
804 return (ng_name_node(priv->node, sap->sg_data));
807 /***************************************************************
809 ***************************************************************/
812 * You can only create new nodes from the socket end of things.
815 ngs_constructor(node_p nodep)
821 * We allow any hook to be connected to the node.
822 * There is no per-hook private information though.
825 ngs_newhook(node_p node, hook_p hook, const char *name)
827 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
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;
850 * Incoming messages get passed up to the control socket.
851 * Unless they are for us specifically (socket_type)
854 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
856 struct ngsock *const priv = NG_NODE_PRIVATE(node);
859 struct sockaddr_ng addr;
862 ng_ID_t retaddr = NGI_RETADDR(item);
866 NGI_GET_MSG(item, msg);
870 * Grab priv->mtx here to prevent destroying of control socket
871 * after checking that priv->ctlsock is not NULL.
873 mtx_lock(&priv->mtx);
874 pcbp = priv->ctlsock;
877 * Only allow mesgs to be passed if we have the control socket.
878 * Data sockets can only support the generic messages.
881 mtx_unlock(&priv->mtx);
886 so = pcbp->ng_socket;
887 SOCKBUF_LOCK(&so->so_rcv);
889 /* As long as the race is handled, priv->mtx may be unlocked now. */
890 mtx_unlock(&priv->mtx);
892 #ifdef TRACE_MESSAGES
893 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
895 msg->header.typecookie,
902 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
903 switch (msg->header.cmd) {
904 case NGM_SOCK_CMD_NOLINGER:
905 priv->flags |= NGS_FLAG_NOLINGER;
907 case NGM_SOCK_CMD_LINGER:
908 priv->flags &= ~NGS_FLAG_NOLINGER;
911 error = EINVAL; /* unknown command */
913 SOCKBUF_UNLOCK(&so->so_rcv);
915 /* Free the message and return. */
920 /* Get the return address into a sockaddr. */
921 bzero(&addr, sizeof(addr));
922 addr.sg_len = sizeof(addr);
923 addr.sg_family = AF_NETGRAPH;
924 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
926 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
927 SOCKBUF_UNLOCK(&so->so_rcv);
928 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
934 /* Copy the message itself into an mbuf chain. */
935 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
939 * Here we free the message. We need to do that
940 * regardless of whether we got mbufs.
945 SOCKBUF_UNLOCK(&so->so_rcv);
950 /* Send it up to the socket. */
951 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
953 SOCKBUF_UNLOCK(&so->so_rcv);
958 sorwakeup_locked(so);
964 * Receive data on a hook
967 ngs_rcvdata(hook_p hook, item_p item)
969 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
970 struct ngpcb *const pcbp = priv->datasock;
972 struct sockaddr_ng *addr;
973 char *addrbuf[NG_HOOKSIZ + 4];
980 /* If there is no data socket, black-hole it. */
985 so = pcbp->ng_socket;
987 /* Get the return address into a sockaddr. */
988 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
989 addr = (struct sockaddr_ng *) addrbuf;
990 addr->sg_len = addrlen + 3;
991 addr->sg_family = AF_NETGRAPH;
992 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
993 addr->sg_data[addrlen] = '\0';
995 /* Try to tell the socket which hook it came in on. */
996 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
1006 * Hook disconnection
1008 * For this type, removal of the last link destroys the node
1009 * if the NOLINGER flag is set.
1012 ngs_disconnect(hook_p hook)
1014 node_p node = NG_HOOK_NODE(hook);
1015 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1017 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1018 if (NG_NODE_NUMHOOKS(node) == 1)
1019 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1021 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1024 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1025 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1026 ng_rmnode_self(node);
1032 * Do local shutdown processing.
1033 * In this case, that involves making sure the socket
1034 * knows we should be shutting down.
1037 ngs_shutdown(node_p node)
1039 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1040 struct ngpcb *dpcbp, *pcbp;
1042 mtx_lock(&priv->mtx);
1043 dpcbp = priv->datasock;
1044 pcbp = priv->ctlsock;
1047 soisdisconnected(dpcbp->ng_socket);
1050 soisdisconnected(pcbp->ng_socket);
1053 NG_NODE_SET_PRIVATE(node, NULL);
1054 ng_socket_free_priv(priv);
1056 NG_NODE_UNREF(node);
1061 ng_socket_item_applied(void *context, int error)
1063 struct ngsock *const priv = (struct ngsock *)context;
1065 mtx_lock(&priv->mtx);
1066 priv->error = error;
1068 mtx_unlock(&priv->mtx);
1073 dummy_disconnect(struct socket *so)
1078 * Control and data socket type descriptors
1080 * XXXRW: Perhaps _close should do something?
1083 static struct pr_usrreqs ngc_usrreqs = {
1085 .pru_attach = ngc_attach,
1086 .pru_bind = ngc_bind,
1087 .pru_connect = ngc_connect,
1088 .pru_detach = ngc_detach,
1089 .pru_disconnect = dummy_disconnect,
1090 .pru_peeraddr = NULL,
1091 .pru_send = ngc_send,
1092 .pru_shutdown = NULL,
1093 .pru_sockaddr = ng_getsockaddr,
1097 static struct pr_usrreqs ngd_usrreqs = {
1099 .pru_attach = ngd_attach,
1101 .pru_connect = ngd_connect,
1102 .pru_detach = ngd_detach,
1103 .pru_disconnect = dummy_disconnect,
1104 .pru_peeraddr = NULL,
1105 .pru_send = ngd_send,
1106 .pru_shutdown = NULL,
1107 .pru_sockaddr = ng_getsockaddr,
1112 * Definitions of protocols supported in the NETGRAPH domain.
1115 extern struct domain ngdomain; /* stop compiler warnings */
1117 static struct protosw ngsw[] = {
1119 .pr_type = SOCK_DGRAM,
1120 .pr_domain = &ngdomain,
1121 .pr_protocol = NG_CONTROL,
1122 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1123 .pr_usrreqs = &ngc_usrreqs
1126 .pr_type = SOCK_DGRAM,
1127 .pr_domain = &ngdomain,
1128 .pr_protocol = NG_DATA,
1129 .pr_flags = PR_ATOMIC | PR_ADDR,
1130 .pr_usrreqs = &ngd_usrreqs
1134 struct domain ngdomain = {
1135 .dom_family = AF_NETGRAPH,
1136 .dom_name = "netgraph",
1137 .dom_protosw = ngsw,
1138 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1142 * Handle loading and unloading for this node type.
1143 * This is to handle auxiliary linkages (e.g protocol domain addition).
1146 ngs_mod_event(module_t mod, int event, void *data)
1152 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1155 /* Ensure there are no open netgraph sockets. */
1156 if (!LIST_EMPTY(&ngsocklist)) {
1161 /* Unregister protocol domain XXX can't do this yet.. */
1172 VNET_DOMAIN_SET(ng);
1174 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1175 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1176 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1177 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1178 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");