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>
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>
70 #include "ng_message.h"
72 #include "ng_socketvar.h"
73 #include "ng_socket.h"
75 #ifdef NG_SEPARATE_MALLOC
76 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
77 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
79 #define M_NETGRAPH_PATH M_NETGRAPH
80 #define M_NETGRAPH_SOCK M_NETGRAPH
84 * It's Ascii-art time!
85 * +-------------+ +-------------+
86 * |socket (ctl)| |socket (data)|
87 * +-------------+ +-------------+
91 * +-----------+ +-----------+
92 * |pcb (ctl)| |pcb (data)|
93 * +-----------+ +-----------+
97 * +--------------------------+
98 * | Socket type private |
100 * +--------------------------+
109 /* Netgraph node methods */
110 static ng_constructor_t ngs_constructor;
111 static ng_rcvmsg_t ngs_rcvmsg;
112 static ng_shutdown_t ngs_shutdown;
113 static ng_newhook_t ngs_newhook;
114 static ng_connect_t ngs_connect;
115 static ng_rcvdata_t ngs_rcvdata;
116 static ng_disconnect_t ngs_disconnect;
118 /* Internal methods */
119 static int ng_attach_data(struct socket *so);
120 static int ng_attach_cntl(struct socket *so);
121 static int ng_attach_common(struct socket *so, int type);
122 static void ng_detach_common(struct ngpcb *pcbp, int type);
123 static void ng_socket_free_priv(struct ngsock *priv);
125 static int ng_internalize(struct mbuf *m, struct thread *p);
127 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
128 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
130 static int ngs_mod_event(module_t mod, int event, void *data);
131 static void ng_socket_item_applied(void *context, int error);
133 /* Netgraph type descriptor */
134 static struct ng_type typestruct = {
135 .version = NG_ABI_VERSION,
136 .name = NG_SOCKET_NODE_TYPE,
137 .mod_event = ngs_mod_event,
138 .constructor = ngs_constructor,
139 .rcvmsg = ngs_rcvmsg,
140 .shutdown = ngs_shutdown,
141 .newhook = ngs_newhook,
142 .connect = ngs_connect,
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_INT(_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_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
154 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
156 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
158 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
163 /***************************************************************
165 ***************************************************************/
168 ngc_attach(struct socket *so, int proto, struct thread *td)
170 struct ngpcb *const pcbp = sotongpcb(so);
173 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
178 return (ng_attach_cntl(so));
182 ngc_detach(struct socket *so)
184 struct ngpcb *const pcbp = sotongpcb(so);
186 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
187 ng_detach_common(pcbp, NG_CONTROL);
191 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
192 struct mbuf *control, struct thread *td)
194 struct ngpcb *const pcbp = sotongpcb(so);
195 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
196 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
202 struct ng_apply_info apply;
205 if (control && (error = ng_internalize(control, td))) {
206 if (pcbp->sockdata == NULL) {
218 /* Require destination as there may be >= 1 hooks on this node. */
220 error = EDESTADDRREQ;
225 * Allocate an expendable buffer for the path, chop off
226 * the sockaddr header, and make sure it's NUL terminated.
228 len = sap->sg_len - 2;
229 path = kmalloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
230 bcopy(sap->sg_data, path, len);
234 * Move the actual message out of mbufs into a linear buffer.
235 * Start by adding up the size of the data. (could use mh_len?)
237 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
241 * Move the data into a linear buffer as well.
242 * Messages are not delivered in mbufs.
244 msg = kmalloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
245 m_copydata(m, 0, len, (char *)msg);
247 if (msg->header.version != NG_VERSION) {
248 kfree(msg, M_NETGRAPH_MSG);
255 * We look into the message and if it mkpeers a node of unknown type, we
256 * try to load it. We need to do this now, in syscall thread, because if
257 * message gets queued and applied later we will get panic.
259 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
260 msg->header.cmd == NGM_MKPEER) {
261 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
262 struct ng_type *type;
264 if ((type = ng_findtype(mkp->type)) == NULL) {
265 char filename[NG_TYPESIZ + 3];
268 /* Not found, try to load it as a loadable module. */
269 snprintf(filename, sizeof(filename), "ng_%s",
271 error = kern_kldload(curthread, filename, &fileid);
273 kfree(msg, M_NETGRAPH_MSG);
277 /* See if type has been loaded successfully. */
278 if ((type = ng_findtype(mkp->type)) == NULL) {
279 kfree(msg, M_NETGRAPH_MSG);
280 (void)kern_kldunload(curthread, fileid,
281 LINKER_UNLOAD_NORMAL);
288 item = ng_package_msg(msg, NG_WAITOK);
289 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
291 #ifdef TRACE_MESSAGES
292 printf("ng_address_path: errx=%d\n", error);
297 #ifdef TRACE_MESSAGES
298 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
299 item->el_dest->nd_ID,
300 msg->header.typecookie,
305 item->el_dest->nd_type->name);
309 * We do not want to return from syscall until the item
310 * is processed by destination node. We register callback
311 * on the item, which will update priv->error when item
313 * If ng_snd_item() has queued item, we sleep until
314 * callback wakes us up.
316 bzero(&apply, sizeof(apply));
317 apply.apply = ng_socket_item_applied;
318 apply.context = priv;
319 item->apply = &apply;
322 error = ng_snd_item(item, 0);
324 mtx_lock(&priv->mtx);
325 if (priv->error == -1)
326 msleep(priv, &priv->mtx, 0, "ngsock", 0);
327 mtx_unlock(&priv->mtx);
328 KASSERT(priv->error != -1,
329 ("ng_socket: priv->error wasn't updated"));
334 kfree(path, M_NETGRAPH_PATH);
343 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
345 struct ngpcb *const pcbp = sotongpcb(so);
349 return (ng_bind(nam, pcbp));
353 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
356 * At this time refuse to do this.. it used to
357 * do something but it was undocumented and not used.
359 printf("program tried to connect control socket to remote node\n");
363 /***************************************************************
365 ***************************************************************/
368 ngd_attach(struct socket *so, int proto, struct thread *td)
370 struct ngpcb *const pcbp = sotongpcb(so);
374 return (ng_attach_data(so));
378 ngd_detach(struct socket *so)
380 struct ngpcb *const pcbp = sotongpcb(so);
382 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
383 ng_detach_common(pcbp, NG_DATA);
387 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
388 struct mbuf *control, struct thread *td)
390 struct ngpcb *const pcbp = sotongpcb(so);
391 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
394 char hookname[NG_HOOKSIZ];
396 if ((pcbp == NULL) || (control != NULL)) {
400 if (pcbp->sockdata == NULL) {
406 len = 0; /* Make compiler happy. */
408 len = sap->sg_len - 2;
411 * If the user used any of these ways to not specify an address
412 * then handle specially.
414 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
415 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
416 error = EDESTADDRREQ;
420 * If exactly one hook exists, just use it.
421 * Special case to allow write(2) to work on an ng_socket.
423 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
425 if (len >= NG_HOOKSIZ) {
431 * chop off the sockaddr header, and make sure it's NUL
434 bcopy(sap->sg_data, hookname, len);
435 hookname[len] = '\0';
437 /* Find the correct hook from 'hookname' */
438 hook = ng_findhook(pcbp->sockdata->node, hookname);
440 error = EHOSTUNREACH;
446 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
457 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
459 struct ngpcb *const pcbp = sotongpcb(so);
463 return (ng_connect_data(nam, pcbp));
467 * Used for both data and control sockets
470 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
473 struct sockaddr_ng *sg;
477 /* Why isn't sg_data a `char[1]' ? :-( */
478 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
480 pcbp = sotongpcb(so);
481 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
482 /* XXXGL: can this still happen? */
485 mtx_lock(&pcbp->sockdata->mtx);
486 if (pcbp->sockdata->node != NULL) {
487 node_p node = pcbp->sockdata->node;
488 int namelen = 0; /* silence compiler! */
490 if (NG_NODE_HAS_NAME(node))
491 sg_len += namelen = strlen(NG_NODE_NAME(node));
493 sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
495 if (NG_NODE_HAS_NAME(node))
496 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
499 sg->sg_family = AF_NETGRAPH;
500 *addr = (struct sockaddr *)sg;
501 mtx_unlock(&pcbp->sockdata->mtx);
503 mtx_unlock(&pcbp->sockdata->mtx);
511 * Attach a socket to it's protocol specific partner.
512 * For a control socket, actually create a netgraph node and attach
517 ng_attach_cntl(struct socket *so)
523 /* Allocate node private info */
524 priv = kmalloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
526 /* Setup protocol control block */
527 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
528 kfree(priv, M_NETGRAPH_SOCK);
531 pcbp = sotongpcb(so);
533 /* Link the pcb the private data. */
534 priv->ctlsock = pcbp;
535 pcbp->sockdata = priv;
538 /* Initialize mutex. */
539 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
541 /* Make the generic node components */
542 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
543 kfree(priv, M_NETGRAPH_SOCK);
544 ng_detach_common(pcbp, NG_CONTROL);
548 /* Link the node and the private data. */
549 NG_NODE_SET_PRIVATE(priv->node, priv);
550 NG_NODE_REF(priv->node);
557 ng_attach_data(struct socket *so)
559 return (ng_attach_common(so, NG_DATA));
563 * Set up a socket protocol control block.
564 * This code is shared between control and data sockets.
567 ng_attach_common(struct socket *so, int type)
572 /* Standard socket setup stuff. */
573 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
577 /* Allocate the pcb. */
578 pcbp = kmalloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
581 /* Link the pcb and the socket. */
582 so->so_pcb = (caddr_t)pcbp;
583 pcbp->ng_socket = so;
589 * Disassociate the socket from it's protocol specific
590 * partner. If it's attached to a node's private data structure,
591 * then unlink from that too. If we were the last socket attached to it,
592 * then shut down the entire node. Shared code for control and data sockets.
595 ng_detach_common(struct ngpcb *pcbp, int which)
597 struct ngsock *priv = pcbp->sockdata;
600 mtx_lock(&priv->mtx);
604 priv->ctlsock = NULL;
607 priv->datasock = NULL;
612 pcbp->sockdata = NULL;
614 ng_socket_free_priv(priv);
617 pcbp->ng_socket->so_pcb = NULL;
622 * Remove a reference from node private data.
625 ng_socket_free_priv(struct ngsock *priv)
627 mtx_assert(&priv->mtx, MA_OWNED);
631 if (priv->refs == 0) {
632 mtx_destroy(&priv->mtx);
633 kfree(priv, M_NETGRAPH_SOCK);
637 if ((priv->refs == 1) && (priv->node != NULL)) {
638 node_p node = priv->node;
641 mtx_unlock(&priv->mtx);
643 ng_rmnode_self(node);
645 mtx_unlock(&priv->mtx);
650 * File descriptors can be passed into an AF_NETGRAPH socket.
651 * Note, that file descriptors cannot be passed OUT.
652 * Only character device descriptors are accepted.
653 * Character devices are useful to connect a graph to a device,
654 * which after all is the purpose of this whole system.
657 ng_internalize(struct mbuf *control, struct thread *td)
659 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
665 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
666 cm->cmsg_len != control->m_len) {
671 /* Check there is only one FD. XXX what would more than one signify? */
672 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
678 /* Check that the FD given is legit. and change it to a pointer to a
681 if ((error = fget(td, fd, &fp)) != 0)
684 /* Depending on what kind of resource it is, act differently. For
685 * devices, we treat it as a file. For an AF_NETGRAPH socket,
686 * shortcut straight to the node. */
687 switch (fp->f_type) {
690 if (vn && (vn->v_type == VCHR)) {
691 /* for a VCHR, actually reference the FILE */
693 /* XXX then what :) */
694 /* how to pass on to other modules? */
712 * Connect the data socket to a named control socket node.
715 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
717 struct sockaddr_ng *sap;
723 /* If we are already connected, don't do it again. */
724 if (pcbp->sockdata != NULL)
728 * Find the target (victim) and check it doesn't already have
729 * a data socket. Also check it is a 'socket' type node.
730 * Use ng_package_data() and ng_address_path() to do this.
733 sap = (struct sockaddr_ng *) nam;
734 /* The item will hold the node reference. */
735 item = ng_package_data(NULL, NG_WAITOK);
737 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
738 return (error); /* item is freed on failure */
741 * Extract node from item and free item. Remember we now have
742 * a reference on the node. The item holds it for us.
743 * when we free the item we release the reference.
745 farnode = item->el_dest; /* shortcut */
746 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
747 NG_FREE_ITEM(item); /* drop the reference to the node */
750 priv = NG_NODE_PRIVATE(farnode);
751 if (priv->datasock != NULL) {
752 NG_FREE_ITEM(item); /* drop the reference to the node */
757 * Link the PCB and the private data struct. and note the extra
758 * reference. Drop the extra reference on the node.
760 mtx_lock(&priv->mtx);
761 priv->datasock = pcbp;
762 pcbp->sockdata = priv;
764 mtx_unlock(&priv->mtx);
765 NG_FREE_ITEM(item); /* drop the reference to the node */
770 * Binding a socket means giving the corresponding node a name
773 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
775 struct ngsock *const priv = pcbp->sockdata;
776 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
782 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
783 (sap->sg_data[0] == '\0') ||
784 (sap->sg_data[sap->sg_len - 3] != '\0')) {
788 return (ng_name_node(priv->node, sap->sg_data));
791 /***************************************************************
793 ***************************************************************/
796 * You can only create new nodes from the socket end of things.
799 ngs_constructor(node_p nodep)
805 * We allow any hook to be connected to the node.
806 * There is no per-hook private information though.
809 ngs_newhook(node_p node, hook_p hook, const char *name)
811 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
816 * If only one hook, allow read(2) and write(2) to work.
819 ngs_connect(hook_p hook)
821 node_p node = NG_HOOK_NODE(hook);
822 struct ngsock *priv = NG_NODE_PRIVATE(node);
824 if ((priv->datasock) && (priv->datasock->ng_socket)) {
825 if (NG_NODE_NUMHOOKS(node) == 1)
826 sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
828 soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
834 * Incoming messages get passed up to the control socket.
835 * Unless they are for us specifically (socket_type)
838 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
840 struct ngsock *const priv = NG_NODE_PRIVATE(node);
841 struct ngpcb *const pcbp = priv->ctlsock;
843 struct sockaddr_ng addr;
846 ng_ID_t retaddr = NGI_RETADDR(item);
850 NGI_GET_MSG(item, msg);
854 * Only allow mesgs to be passed if we have the control socket.
855 * Data sockets can only support the generic messages.
862 so = pcbp->ng_socket;
864 #ifdef TRACE_MESSAGES
865 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
867 msg->header.typecookie,
874 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
875 switch (msg->header.cmd) {
876 case NGM_SOCK_CMD_NOLINGER:
877 priv->flags |= NGS_FLAG_NOLINGER;
879 case NGM_SOCK_CMD_LINGER:
880 priv->flags &= ~NGS_FLAG_NOLINGER;
883 error = EINVAL; /* unknown command */
885 /* Free the message and return. */
890 /* Get the return address into a sockaddr. */
891 bzero(&addr, sizeof(addr));
892 addr.sg_len = sizeof(addr);
893 addr.sg_family = AF_NETGRAPH;
894 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
896 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
897 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
903 /* Copy the message itself into an mbuf chain. */
904 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
908 * Here we free the message. We need to do that
909 * regardless of whether we got mbufs.
918 /* Send it up to the socket. */
919 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
930 * Receive data on a hook
933 ngs_rcvdata(hook_p hook, item_p item)
935 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
936 struct ngpcb *const pcbp = priv->datasock;
938 struct sockaddr_ng *addr;
939 char *addrbuf[NG_HOOKSIZ + 4];
946 /* If there is no data socket, black-hole it. */
951 so = pcbp->ng_socket;
953 /* Get the return address into a sockaddr. */
954 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
955 addr = (struct sockaddr_ng *) addrbuf;
956 addr->sg_len = addrlen + 3;
957 addr->sg_family = AF_NETGRAPH;
958 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
959 addr->sg_data[addrlen] = '\0';
961 /* Try to tell the socket which hook it came in on. */
962 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
974 * For this type, removal of the last link destroys the node
975 * if the NOLINGER flag is set.
978 ngs_disconnect(hook_p hook)
980 node_p node = NG_HOOK_NODE(hook);
981 struct ngsock *const priv = NG_NODE_PRIVATE(node);
983 if ((priv->datasock) && (priv->datasock->ng_socket)) {
984 if (NG_NODE_NUMHOOKS(node) == 1)
985 sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
987 soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
990 if ((priv->flags & NGS_FLAG_NOLINGER) &&
991 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
992 ng_rmnode_self(node);
998 * Do local shutdown processing.
999 * In this case, that involves making sure the socket
1000 * knows we should be shutting down.
1003 ngs_shutdown(node_p node)
1005 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1006 struct ngpcb *const dpcbp = priv->datasock;
1007 struct ngpcb *const pcbp = priv->ctlsock;
1010 soisdisconnected(dpcbp->ng_socket);
1013 soisdisconnected(pcbp->ng_socket);
1015 mtx_lock(&priv->mtx);
1017 NG_NODE_SET_PRIVATE(node, NULL);
1018 ng_socket_free_priv(priv);
1020 NG_NODE_UNREF(node);
1025 ng_socket_item_applied(void *context, int error)
1027 struct ngsock *const priv = (struct ngsock *)context;
1029 mtx_lock(&priv->mtx);
1030 priv->error = error;
1032 mtx_unlock(&priv->mtx);
1037 dummy_disconnect(struct socket *so)
1042 * Control and data socket type descriptors
1044 * XXXRW: Perhaps _close should do something?
1047 static struct pr_usrreqs ngc_usrreqs = {
1049 .pru_attach = ngc_attach,
1050 .pru_bind = ngc_bind,
1051 .pru_connect = ngc_connect,
1052 .pru_detach = ngc_detach,
1053 .pru_disconnect = dummy_disconnect,
1054 .pru_peeraddr = NULL,
1055 .pru_send = ngc_send,
1056 .pru_shutdown = NULL,
1057 .pru_sockaddr = ng_getsockaddr,
1061 static struct pr_usrreqs ngd_usrreqs = {
1063 .pru_attach = ngd_attach,
1065 .pru_connect = ngd_connect,
1066 .pru_detach = ngd_detach,
1067 .pru_disconnect = dummy_disconnect,
1068 .pru_peeraddr = NULL,
1069 .pru_send = ngd_send,
1070 .pru_shutdown = NULL,
1071 .pru_sockaddr = ng_getsockaddr,
1076 * Definitions of protocols supported in the NETGRAPH domain.
1079 extern struct domain ngdomain; /* stop compiler warnings */
1081 static struct protosw ngsw[] = {
1083 .pr_type = SOCK_DGRAM,
1084 .pr_domain = &ngdomain,
1085 .pr_protocol = NG_CONTROL,
1086 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1087 .pr_usrreqs = &ngc_usrreqs
1090 .pr_type = SOCK_DGRAM,
1091 .pr_domain = &ngdomain,
1092 .pr_protocol = NG_DATA,
1093 .pr_flags = PR_ATOMIC | PR_ADDR,
1094 .pr_usrreqs = &ngd_usrreqs
1098 struct domain ngdomain = {
1099 .dom_family = AF_NETGRAPH,
1100 .dom_name = "netgraph",
1101 .dom_protosw = ngsw,
1102 .dom_protoswNPROTOSW = &ngsw[NELEM(ngsw)]
1106 * Handle loading and unloading for this node type.
1107 * This is to handle auxiliary linkages (e.g protocol domain addition).
1110 ngs_mod_event(module_t mod, int event, void *data)
1116 /* Register protocol domain. */
1117 net_add_domain(&ngdomain);
1121 /* Unregister protocol domain XXX can't do this yet.. */
1122 if ((error = net_rm_domain(&ngdomain)) != 0)
1135 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1136 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1137 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1138 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1139 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");