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>
40 * $FreeBSD: src/sys/netgraph/ng_socket.c,v 1.85 2008/03/11 21:58:48 mav Exp $
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>
60 #include <sys/mutex.h>
64 #include <sys/protosw.h>
65 #include <sys/queue.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
69 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
73 #include <sys/vnode.h>
75 #include <netgraph7/ng_message.h>
76 #include <netgraph7/netgraph.h>
77 #include "ng_socketvar.h"
78 #include "ng_socket.h"
80 #ifdef NG_SEPARATE_MALLOC
81 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
82 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
84 #define M_NETGRAPH_PATH M_NETGRAPH
85 #define M_NETGRAPH_SOCK M_NETGRAPH
89 * It's Ascii-art time!
90 * +-------------+ +-------------+
91 * |socket (ctl)| |socket (data)|
92 * +-------------+ +-------------+
96 * +-----------+ +-----------+
97 * |pcb (ctl)| |pcb (data)|
98 * +-----------+ +-----------+
102 * +--------------------------+
103 * | Socket type private |
105 * +--------------------------+
114 /* Netgraph node methods */
115 static ng_constructor_t ngs_constructor;
116 static ng_rcvmsg_t ngs_rcvmsg;
117 static ng_shutdown_t ngs_shutdown;
118 static ng_newhook_t ngs_newhook;
119 static ng_connect_t ngs_connect;
120 static ng_rcvdata_t ngs_rcvdata;
121 static ng_disconnect_t ngs_disconnect;
123 /* Internal methods */
124 static int ng_attach_data(struct socket *so);
125 static int ng_attach_cntl(struct socket *so);
126 static int ng_attach_common(struct socket *so, int type);
127 static void ng_detach_common(struct ngpcb *pcbp, int type);
128 static void ng_socket_free_priv(struct ngsock *priv);
130 static int ng_internalize(struct mbuf *m, struct thread *p);
132 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
133 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
135 static int ngs_mod_event(module_t mod, int event, void *data);
136 static void ng_socket_item_applied(void *context, int error);
138 /* Netgraph type descriptor */
139 static struct ng_type typestruct = {
140 .version = NG_ABI_VERSION,
141 .name = NG_SOCKET_NODE_TYPE,
142 .mod_event = ngs_mod_event,
143 .constructor = ngs_constructor,
144 .rcvmsg = ngs_rcvmsg,
145 .shutdown = ngs_shutdown,
146 .newhook = ngs_newhook,
147 .connect = ngs_connect,
148 .rcvdata = ngs_rcvdata,
149 .disconnect = ngs_disconnect,
151 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
154 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
155 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
156 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
157 static u_long ngpdg_recvspace = 20 * 1024;
158 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
159 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
161 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
163 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
168 /***************************************************************
170 ***************************************************************/
173 ngc_attach(struct socket *so, int proto, struct pru_attach_info *ai)
175 struct ngpcb *const pcbp = sotongpcb(so);
177 if (priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY) != 0)
181 return (ng_attach_cntl(so));
185 ngc_detach(struct socket *so)
187 struct ngpcb *const pcbp = sotongpcb(so);
189 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
190 ng_detach_common(pcbp, NG_CONTROL);
195 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
196 struct mbuf *control, struct thread *td)
198 struct ngpcb *const pcbp = sotongpcb(so);
199 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
200 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
206 struct ng_apply_info apply;
209 if (control && (error = ng_internalize(control, td))) {
210 if (pcbp->sockdata == NULL) {
222 /* Require destination as there may be >= 1 hooks on this node. */
224 error = EDESTADDRREQ;
229 * Allocate an expendable buffer for the path, chop off
230 * the sockaddr header, and make sure it's NUL terminated.
232 len = sap->sg_len - 2;
233 path = kmalloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
234 bcopy(sap->sg_data, path, len);
238 * Move the actual message out of mbufs into a linear buffer.
239 * Start by adding up the size of the data. (could use mh_len?)
241 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
245 * Move the data into a linear buffer as well.
246 * Messages are not delivered in mbufs.
248 msg = kmalloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
249 m_copydata(m, 0, len, (char *)msg);
251 if (msg->header.version != NG_VERSION) {
252 kfree(msg, M_NETGRAPH_MSG);
259 * We look into the message and if it mkpeers a node of unknown type, we
260 * try to load it. We need to do this now, in syscall thread, because if
261 * message gets queued and applied later we will get panic.
263 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
264 msg->header.cmd == NGM_MKPEER) {
265 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
266 struct ng_type *type;
268 if ((type = ng_findtype(mkp->type)) == NULL) {
269 char filename[NG_TYPESIZ + 3];
270 linker_file_t fileid;
272 if (!linker_api_available())
275 /* Not found, try to load it as a loadable module. */
276 snprintf(filename, sizeof(filename), "ng_%s.ko",
278 error = linker_load_file(filename, &fileid);
280 kfree(msg, M_NETGRAPH_MSG);
284 /* See if type has been loaded successfully. */
285 if ((type = ng_findtype(mkp->type)) == NULL) {
286 kfree(msg, M_NETGRAPH_MSG);
287 (void)linker_file_unload(fileid);
294 item = ng_package_msg(msg, NG_WAITOK);
295 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
297 #ifdef TRACE_MESSAGES
298 printf("ng_address_path: errx=%d\n", error);
303 #ifdef TRACE_MESSAGES
304 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
305 item->el_dest->nd_ID,
306 msg->header.typecookie,
311 item->el_dest->nd_type->name);
315 * We do not want to return from syscall until the item
316 * is processed by destination node. We register callback
317 * on the item, which will update priv->error when item
319 * If ng_snd_item() has queued item, we sleep until
320 * callback wakes us up.
322 bzero(&apply, sizeof(apply));
323 apply.apply = ng_socket_item_applied;
324 apply.context = priv;
325 item->apply = &apply;
328 error = ng_snd_item(item, 0);
330 mtx_lock(&priv->mtx);
331 if (priv->error == -1)
332 lock_sleep(priv, 0, "ngsock", 0,
333 (struct lock *)&priv->mtx);
334 mtx_unlock(&priv->mtx);
335 KASSERT(priv->error != -1,
336 ("ng_socket: priv->error wasn't updated"));
341 kfree(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 pru_attach_info *ai)
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);
395 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
396 struct mbuf *control, struct thread *td)
398 struct ngpcb *const pcbp = sotongpcb(so);
399 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
402 char hookname[NG_HOOKSIZ];
404 if ((pcbp == NULL) || (control != NULL)) {
408 if (pcbp->sockdata == NULL) {
414 len = 0; /* Make compiler happy. */
416 len = sap->sg_len - 2;
419 * If the user used any of these ways to not specify an address
420 * then handle specially.
422 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
423 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
424 error = EDESTADDRREQ;
428 * If exactly one hook exists, just use it.
429 * Special case to allow write(2) to work on an ng_socket.
431 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
433 if (len >= NG_HOOKSIZ) {
439 * chop off the sockaddr header, and make sure it's NUL
442 bcopy(sap->sg_data, hookname, len);
443 hookname[len] = '\0';
445 /* Find the correct hook from 'hookname' */
446 hook = ng_findhook(pcbp->sockdata->node, hookname);
448 error = EHOSTUNREACH;
454 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
465 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
467 struct ngpcb *const pcbp = sotongpcb(so);
471 return (ng_connect_data(nam, pcbp));
475 * Used for both data and control sockets
478 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
481 struct sockaddr_ng *sg;
485 /* Why isn't sg_data a `char[1]' ? :-( */
486 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
488 pcbp = sotongpcb(so);
489 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
490 /* XXXGL: can this still happen? */
493 mtx_lock(&pcbp->sockdata->mtx);
494 if (pcbp->sockdata->node != NULL) {
495 node_p node = pcbp->sockdata->node;
496 int namelen = 0; /* silence compiler! */
498 if (NG_NODE_HAS_NAME(node))
499 sg_len += namelen = strlen(NG_NODE_NAME(node));
501 sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
503 if (NG_NODE_HAS_NAME(node))
504 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
507 sg->sg_family = AF_NETGRAPH;
508 *addr = (struct sockaddr *)sg;
509 mtx_unlock(&pcbp->sockdata->mtx);
511 mtx_unlock(&pcbp->sockdata->mtx);
519 * Attach a socket to it's protocol specific partner.
520 * For a control socket, actually create a netgraph node and attach
525 ng_attach_cntl(struct socket *so)
531 /* Allocate node private info */
532 priv = kmalloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
534 /* Setup protocol control block */
535 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
536 kfree(priv, M_NETGRAPH_SOCK);
539 pcbp = sotongpcb(so);
541 /* Link the pcb the private data. */
542 priv->ctlsock = pcbp;
543 pcbp->sockdata = priv;
546 /* Initialize mutex. */
547 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
549 /* Make the generic node components */
550 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
551 kfree(priv, M_NETGRAPH_SOCK);
552 ng_detach_common(pcbp, NG_CONTROL);
556 /* Link the node and the private data. */
557 NG_NODE_SET_PRIVATE(priv->node, priv);
558 NG_NODE_REF(priv->node);
565 ng_attach_data(struct socket *so)
567 return (ng_attach_common(so, NG_DATA));
571 * Set up a socket protocol control block.
572 * This code is shared between control and data sockets.
575 ng_attach_common(struct socket *so, int type)
580 /* Standard socket setup stuff. */
581 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL);
585 /* Allocate the pcb. */
586 pcbp = kmalloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
589 /* Link the pcb and the socket. */
590 so->so_pcb = (caddr_t)pcbp;
591 pcbp->ng_socket = so;
597 * Disassociate the socket from it's protocol specific
598 * partner. If it's attached to a node's private data structure,
599 * then unlink from that too. If we were the last socket attached to it,
600 * then shut down the entire node. Shared code for control and data sockets.
603 ng_detach_common(struct ngpcb *pcbp, int which)
605 struct ngsock *priv = pcbp->sockdata;
608 mtx_lock(&priv->mtx);
612 priv->ctlsock = NULL;
615 priv->datasock = NULL;
620 pcbp->sockdata = NULL;
622 ng_socket_free_priv(priv);
625 pcbp->ng_socket->so_pcb = NULL;
630 * Remove a reference from node private data.
633 ng_socket_free_priv(struct ngsock *priv)
635 mtx_assert(&priv->mtx, MA_OWNED);
639 if (priv->refs == 0) {
640 mtx_destroy(&priv->mtx);
641 kfree(priv, M_NETGRAPH_SOCK);
645 if ((priv->refs == 1) && (priv->node != NULL)) {
646 node_p node = priv->node;
649 mtx_unlock(&priv->mtx);
651 ng_rmnode_self(node);
653 mtx_unlock(&priv->mtx);
658 * File descriptors can be passed into an AF_NETGRAPH socket.
659 * Note, that file descriptors cannot be passed OUT.
660 * Only character device descriptors are accepted.
661 * Character devices are useful to connect a graph to a device,
662 * which after all is the purpose of this whole system.
665 ng_internalize(struct mbuf *control, struct thread *td)
667 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
673 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
674 cm->cmsg_len != control->m_len) {
679 /* Check there is only one FD. XXX what would more than one signify? */
680 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
686 /* Check that the FD given is legit. and change it to a pointer to a
689 if ((error = fget(td, fd, &fp)) != 0)
692 /* Depending on what kind of resource it is, act differently. For
693 * devices, we treat it as a file. For an AF_NETGRAPH socket,
694 * shortcut straight to the node. */
695 switch (fp->f_type) {
698 if (vn && (vn->v_type == VCHR)) {
699 /* for a VCHR, actually reference the FILE */
701 /* XXX then what :) */
702 /* how to pass on to other modules? */
720 * Connect the data socket to a named control socket node.
723 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
725 struct sockaddr_ng *sap;
731 /* If we are already connected, don't do it again. */
732 if (pcbp->sockdata != NULL)
736 * Find the target (victim) and check it doesn't already have
737 * a data socket. Also check it is a 'socket' type node.
738 * Use ng_package_data() and ng_address_path() to do this.
741 sap = (struct sockaddr_ng *) nam;
742 /* The item will hold the node reference. */
743 item = ng_package_data(NULL, NG_WAITOK);
745 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
746 return (error); /* item is freed on failure */
749 * Extract node from item and free item. Remember we now have
750 * a reference on the node. The item holds it for us.
751 * when we free the item we release the reference.
753 farnode = item->el_dest; /* shortcut */
754 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
755 NG_FREE_ITEM(item); /* drop the reference to the node */
758 priv = NG_NODE_PRIVATE(farnode);
759 if (priv->datasock != NULL) {
760 NG_FREE_ITEM(item); /* drop the reference to the node */
765 * Link the PCB and the private data struct. and note the extra
766 * reference. Drop the extra reference on the node.
768 mtx_lock(&priv->mtx);
769 priv->datasock = pcbp;
770 pcbp->sockdata = priv;
772 mtx_unlock(&priv->mtx);
773 NG_FREE_ITEM(item); /* drop the reference to the node */
778 * Binding a socket means giving the corresponding node a name
781 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
783 struct ngsock *const priv = pcbp->sockdata;
784 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
790 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
791 (sap->sg_data[0] == '\0') ||
792 (sap->sg_data[sap->sg_len - 3] != '\0')) {
796 return (ng_name_node(priv->node, sap->sg_data));
799 /***************************************************************
801 ***************************************************************/
804 * You can only create new nodes from the socket end of things.
807 ngs_constructor(node_p nodep)
813 * We allow any hook to be connected to the node.
814 * There is no per-hook private information though.
817 ngs_newhook(node_p node, hook_p hook, const char *name)
819 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
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 sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
836 soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
842 * Incoming messages get passed up to the control socket.
843 * Unless they are for us specifically (socket_type)
846 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
848 struct ngsock *const priv = NG_NODE_PRIVATE(node);
849 struct ngpcb *const pcbp = priv->ctlsock;
851 struct sockaddr_ng addr;
854 ng_ID_t retaddr = NGI_RETADDR(item);
858 NGI_GET_MSG(item, msg);
862 * Only allow mesgs to be passed if we have the control socket.
863 * Data sockets can only support the generic messages.
870 so = pcbp->ng_socket;
872 #ifdef TRACE_MESSAGES
873 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
875 msg->header.typecookie,
882 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
883 switch (msg->header.cmd) {
884 case NGM_SOCK_CMD_NOLINGER:
885 priv->flags |= NGS_FLAG_NOLINGER;
887 case NGM_SOCK_CMD_LINGER:
888 priv->flags &= ~NGS_FLAG_NOLINGER;
891 error = EINVAL; /* unknown command */
893 /* Free the message and return. */
898 /* Get the return address into a sockaddr. */
899 bzero(&addr, sizeof(addr));
900 addr.sg_len = sizeof(addr);
901 addr.sg_family = AF_NETGRAPH;
902 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
904 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
905 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
911 /* Copy the message itself into an mbuf chain. */
912 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
916 * Here we free the message. We need to do that
917 * regardless of whether we got mbufs.
926 /* Send it up to the socket. */
927 if (sbappendaddr((struct sockbuf *)&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
938 * Receive data on a hook
941 ngs_rcvdata(hook_p hook, item_p item)
943 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
944 struct ngpcb *const pcbp = priv->datasock;
946 struct sockaddr_ng *addr;
947 char *addrbuf[NG_HOOKSIZ + 4];
954 /* If there is no data socket, black-hole it. */
959 so = pcbp->ng_socket;
961 /* Get the return address into a sockaddr. */
962 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
963 addr = (struct sockaddr_ng *) addrbuf;
964 addr->sg_len = addrlen + 3;
965 addr->sg_family = AF_NETGRAPH;
966 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
967 addr->sg_data[addrlen] = '\0';
969 /* Try to tell the socket which hook it came in on. */
970 if (sbappendaddr((struct sockbuf *)&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
982 * For this type, removal of the last link destroys the node
983 * if the NOLINGER flag is set.
986 ngs_disconnect(hook_p hook)
988 node_p node = NG_HOOK_NODE(hook);
989 struct ngsock *const priv = NG_NODE_PRIVATE(node);
991 if ((priv->datasock) && (priv->datasock->ng_socket)) {
992 if (NG_NODE_NUMHOOKS(node) == 1)
993 sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
995 soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
998 if ((priv->flags & NGS_FLAG_NOLINGER) &&
999 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1000 ng_rmnode_self(node);
1006 * Do local shutdown processing.
1007 * In this case, that involves making sure the socket
1008 * knows we should be shutting down.
1011 ngs_shutdown(node_p node)
1013 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1014 struct ngpcb *const dpcbp = priv->datasock;
1015 struct ngpcb *const pcbp = priv->ctlsock;
1018 soisdisconnected(dpcbp->ng_socket);
1021 soisdisconnected(pcbp->ng_socket);
1023 mtx_lock(&priv->mtx);
1025 NG_NODE_SET_PRIVATE(node, NULL);
1026 ng_socket_free_priv(priv);
1028 NG_NODE_UNREF(node);
1033 ng_socket_item_applied(void *context, int error)
1035 struct ngsock *const priv = (struct ngsock *)context;
1037 mtx_lock(&priv->mtx);
1038 priv->error = error;
1040 mtx_unlock(&priv->mtx);
1045 dummy_disconnect(struct socket *so)
1050 * Control and data socket type descriptors
1052 * XXXRW: Perhaps _close should do something?
1055 static struct pr_usrreqs ngc_usrreqs = {
1057 .pru_attach = ngc_attach,
1058 .pru_bind = ngc_bind,
1059 .pru_connect = ngc_connect,
1060 .pru_detach = ngc_detach,
1061 .pru_disconnect = dummy_disconnect,
1062 .pru_peeraddr = NULL,
1063 .pru_send = ngc_send,
1064 .pru_shutdown = NULL,
1065 .pru_sockaddr = ng_getsockaddr,
1066 .pru_sopoll = sopoll,
1067 .pru_sosend = sosend,
1068 .pru_soreceive = soreceive,
1069 /* .pru_close = NULL, */
1072 static struct pr_usrreqs ngd_usrreqs = {
1074 .pru_attach = ngd_attach,
1076 .pru_connect = ngd_connect,
1077 .pru_detach = ngd_detach,
1078 .pru_disconnect = dummy_disconnect,
1079 .pru_peeraddr = NULL,
1080 .pru_send = ngd_send,
1081 .pru_shutdown = NULL,
1082 .pru_sockaddr = ng_getsockaddr,
1083 .pru_sopoll = sopoll,
1084 .pru_sosend = sosend,
1085 .pru_soreceive = soreceive,
1086 /* .pru_close = NULL, */
1090 * Definitions of protocols supported in the NETGRAPH domain.
1093 extern struct domain ngdomain; /* stop compiler warnings */
1095 static struct protosw ngsw[] = {
1097 .pr_type = SOCK_DGRAM,
1098 .pr_domain = &ngdomain,
1099 .pr_protocol = NG_CONTROL,
1100 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1101 .pr_mport = cpu0_soport,
1102 .pr_usrreqs = &ngc_usrreqs
1105 .pr_type = SOCK_DGRAM,
1106 .pr_domain = &ngdomain,
1107 .pr_protocol = NG_DATA,
1108 .pr_flags = PR_ATOMIC | PR_ADDR,
1109 .pr_mport = cpu0_soport,
1110 .pr_usrreqs = &ngd_usrreqs
1114 struct domain ngdomain = {
1115 .dom_family = AF_NETGRAPH,
1116 .dom_name = "netgraph",
1117 .dom_protosw = ngsw,
1118 .dom_protoswNPROTOSW = &ngsw[NELEM(ngsw)]
1122 * Handle loading and unloading for this node type.
1123 * This is to handle auxiliary linkages (e.g protocol domain addition).
1126 ngs_mod_event(module_t mod, int event, void *data)
1132 /* Register protocol domain. */
1133 net_add_domain(&ngdomain);
1137 /* Unregister protocol domain XXX can't do this yet.. */
1138 if ((error = net_rm_domain(&ngdomain)) != 0)
1151 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1152 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1153 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1154 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1155 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");