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/errno.h>
56 #include <sys/kernel.h>
57 #include <sys/linker.h>
59 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/protosw.h>
63 #include <sys/queue.h>
64 #include <sys/signalvar.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
68 #include <sys/sysctl.h>
69 #include <sys/systm.h>
71 #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(node_p node, hook_p hook, void *arg1, int which);
126 /*static int ng_internalize(struct mbuf *m, struct thread *p); */
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 int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
133 struct sockaddr_ng *addr);
135 /* Netgraph type descriptor */
136 static struct ng_type typestruct = {
137 .version = NG_ABI_VERSION,
138 .name = NG_SOCKET_NODE_TYPE,
139 .mod_event = ngs_mod_event,
140 .constructor = ngs_constructor,
141 .rcvmsg = ngs_rcvmsg,
142 .shutdown = ngs_shutdown,
143 .newhook = ngs_newhook,
144 .connect = ngs_connect,
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_INT(_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_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
156 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
158 /* List of all sockets */
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"); */
170 /***************************************************************
172 ***************************************************************/
175 ngc_attach(struct socket *so, int proto, struct thread *td)
177 struct ngpcb *const pcbp = sotongpcb(so);
183 return (ng_attach_cntl(so));
187 ngc_detach(struct socket *so)
189 struct ngpcb *const pcbp = sotongpcb(so);
195 * If there is a node, then obtain netgraph locking first.
197 if (pcbp->sockdata != NULL)
198 ng_send_fn1(pcbp->sockdata->node, NULL, &ng_detach_common,
199 pcbp, NG_CONTROL, NG_WAITOK);
201 ng_detach_common(NULL, NULL, pcbp, NG_CONTROL);
207 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
208 struct mbuf *control, struct thread *td)
210 struct ngpcb *const pcbp = sotongpcb(so);
211 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
223 if (control && (error = ng_internalize(control, td))) {
224 if (pcbp->sockdata == NULL) {
236 /* Require destination as there may be >= 1 hooks on this node */
238 error = EDESTADDRREQ;
242 /* Allocate an expendable buffer for the path, chop off
243 * the sockaddr header, and make sure it's NUL terminated */
244 len = sap->sg_len - 2;
245 MALLOC(path, char *, len + 1, M_NETGRAPH_PATH, M_WAITOK);
250 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?) */
255 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
258 /* Move the data into a linear buffer as well. Messages are not
259 * delivered in mbufs. */
260 MALLOC(msg, struct ng_mesg *, len + 1, M_NETGRAPH_MSG, M_WAITOK);
265 m_copydata(m, 0, len, (char *)msg);
267 if (msg->header.version != NG_VERSION) {
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;
281 struct ng_type *type;
283 if ((type = ng_findtype(mkp->type)) == NULL) {
284 char filename[NG_TYPESIZ + 3];
288 /* Not found, try to load it as a loadable module. */
289 snprintf(filename, sizeof(filename), "ng_%s",
292 error = linker_load_module(NULL, filename, NULL, NULL,
296 FREE(msg, M_NETGRAPH_MSG);
301 /* See if type has been loaded successfully. */
302 if ((type = ng_findtype(mkp->type)) == NULL) {
303 FREE(msg, M_NETGRAPH_MSG);
310 if ((item = ng_package_msg(msg, M_WAITOK)) == NULL) {
312 #ifdef TRACE_MESSAGES
313 printf("ng_package_msg: err=%d\n", error);
317 if ((error = ng_address_path((pcbp->sockdata->node), item,
319 #ifdef TRACE_MESSAGES
320 printf("ng_address_path: errx=%d\n", error);
325 #ifdef TRACE_MESSAGES
326 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
327 item->el_dest->nd_ID,
328 msg->header.typecookie,
333 item->el_dest->nd_type->name);
336 error = ng_snd_item(item, NG_NOFLAGS);
340 FREE(path, M_NETGRAPH_PATH);
349 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
351 struct ngpcb *const pcbp = sotongpcb(so);
355 return (ng_bind(nam, pcbp));
359 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
361 printf(" program tried to connect control socket to remote node\n ");
363 * At this time refuse to do this.. it used to
364 * do something but it was undocumented and not used.
369 /***************************************************************
371 ***************************************************************/
374 ngd_attach(struct socket *so, int proto, struct thread *td)
376 struct ngpcb *const pcbp = sotongpcb(so);
380 return (ng_attach_data(so));
384 ngd_detach(struct socket *so)
386 struct ngpcb *const pcbp = sotongpcb(so);
392 * If there is a node, then obtain netgraph locking first.
394 if (pcbp->sockdata != NULL)
395 ng_send_fn1(pcbp->sockdata->node, NULL, &ng_detach_common,
396 pcbp, NG_DATA, NG_WAITOK);
398 ng_detach_common(NULL, NULL, pcbp, NG_DATA);
404 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
405 struct mbuf *control, struct thread *td)
407 struct ngpcb *const pcbp = sotongpcb(so);
408 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
411 char hookname[NG_HOOKSIZ];
413 if ((pcbp == NULL) || (control != NULL)) {
417 if (pcbp->sockdata == NULL) {
422 * If the user used any of these ways to not specify an address
423 * then handle specially.
426 || ((len = sap->sg_len - 2) <= 0)
427 || (*sap->sg_data == '\0')) {
428 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
429 error = EDESTADDRREQ;
433 * if exactly one hook exists, just use it.
434 * Special case to allow write(2) to work on an ng_socket.
436 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
438 if (len >= NG_HOOKSIZ) {
444 * chop off the sockaddr header, and make sure it's NUL
447 bcopy(sap->sg_data, hookname, len);
448 hookname[len] = '\0';
450 /* Find the correct hook from 'hookname' */
451 LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) {
452 if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) {
457 error = EHOSTUNREACH;
461 /* Send data (OK if hook is NULL) */
462 NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */
473 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
475 struct ngpcb *const pcbp = sotongpcb(so);
479 return (ng_connect_data(nam, pcbp));
483 * Used for both data and control sockets
486 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
489 struct sockaddr_ng *sg;
490 int sg_len, namelen, s;
492 /* Why isn't sg_data a `char[1]' ? :-( */
493 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
496 pcbp = sotongpcb(so);
497 if ((pcbp == NULL) || (pcbp->sockdata == NULL)) {
502 namelen = 0; /* silence compiler ! */
503 if ( NG_NODE_HAS_NAME(pcbp->sockdata->node))
504 sg_len += namelen = strlen(NG_NODE_NAME(pcbp->sockdata->node));
506 MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK | M_ZERO);
508 if (NG_NODE_HAS_NAME(pcbp->sockdata->node))
509 bcopy(NG_NODE_NAME(pcbp->sockdata->node), sg->sg_data, namelen);
513 sg->sg_family = AF_NETGRAPH;
514 *addr = (struct sockaddr *)sg;
520 * Attach a socket to it's protocol specific partner.
521 * For a control socket, actually create a netgraph node and attach
526 ng_attach_cntl(struct socket *so)
528 struct ngsock *privdata;
532 /* Setup protocol control block */
533 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
535 pcbp = sotongpcb(so);
537 /* Allocate node private info */
538 MALLOC(privdata, struct ngsock *,
539 sizeof(*privdata), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
540 if (privdata == NULL) {
541 ng_detach_common(NULL, NULL, pcbp, NG_CONTROL);
545 /* Make the generic node components */
546 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
547 FREE(privdata, M_NETGRAPH_SOCK);
548 ng_detach_common(NULL, NULL, pcbp, NG_CONTROL);
551 NG_NODE_SET_PRIVATE(privdata->node, privdata);
553 /* Link the pcb and the node private data */
554 privdata->ctlsock = pcbp;
555 pcbp->sockdata = privdata;
561 ng_attach_data(struct socket *so)
563 return(ng_attach_common(so, NG_DATA));
567 * Set up a socket protocol control block.
568 * This code is shared between control and data sockets.
571 ng_attach_common(struct socket *so, int type)
576 /* Standard socket setup stuff */
577 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
581 /* Allocate the pcb */
582 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
587 /* Link the pcb and the socket */
588 so->so_pcb = (caddr_t) pcbp;
589 pcbp->ng_socket = so;
591 /* Add the socket to linked list */
592 mtx_lock(&ngsocketlist_mtx);
593 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
594 mtx_unlock(&ngsocketlist_mtx);
599 * Disassociate the socket from it's protocol specific
600 * partner. If it's attached to a node's private data structure,
601 * then unlink from that too. If we were the last socket attached to it,
602 * then shut down the entire node. Shared code for control and data sockets.
605 ng_detach_common(node_p node, hook_p hook, void *arg1, int which)
607 struct ngpcb *pcbp = arg1;
609 if (pcbp->sockdata) {
612 priv = pcbp->sockdata;
613 pcbp->sockdata = NULL;
616 priv->ctlsock = NULL;
619 priv->datasock = NULL;
624 if ((--priv->refs == 0) && (priv->node != NULL))
625 ng_rmnode_self(priv->node);
627 pcbp->ng_socket->so_pcb = NULL;
628 pcbp->ng_socket = NULL;
629 mtx_lock(&ngsocketlist_mtx);
630 LIST_REMOVE(pcbp, socks);
631 mtx_unlock(&ngsocketlist_mtx);
637 * File descriptors can be passed into an AF_NETGRAPH socket.
638 * Note, that file descriptors cannot be passed OUT.
639 * Only character device descriptors are accepted.
640 * Character devices are useful to connect a graph to a device,
641 * which after all is the purpose of this whole system.
644 ng_internalize(struct mbuf *control, struct thread *td)
646 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
652 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
653 cm->cmsg_len != control->m_len) {
658 /* Check there is only one FD. XXX what would more than one signify? */
659 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
665 /* Check that the FD given is legit. and change it to a pointer to a
668 if ((error = fget(td, fd, &fp)) != 0)
671 /* Depending on what kind of resource it is, act differently. For
672 * devices, we treat it as a file. For an AF_NETGRAPH socket,
673 * shortcut straight to the node. */
674 switch (fp->f_type) {
677 if (vn && (vn->v_type == VCHR)) {
678 /* for a VCHR, actually reference the FILE */
680 /* XXX then what :) */
681 /* how to pass on to other modules? */
699 * Connect the data socket to a named control socket node.
702 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
704 struct sockaddr_ng *sap;
710 /* If we are already connected, don't do it again */
711 if (pcbp->sockdata != NULL)
714 /* Find the target (victim) and check it doesn't already have a data
715 * socket. Also check it is a 'socket' type node.
716 * Use ng_package_data() and address_path() to do this.
719 sap = (struct sockaddr_ng *) nam;
720 /* The item will hold the node reference */
721 item = ng_package_data(NULL, NG_WAITOK);
725 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
726 return (error); /* item is freed on failure */
729 * Extract node from item and free item. Remember we now have
730 * a reference on the node. The item holds it for us.
731 * when we free the item we release the reference.
733 farnode = item->el_dest; /* shortcut */
734 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
735 NG_FREE_ITEM(item); /* drop the reference to the node */
738 priv = NG_NODE_PRIVATE(farnode);
739 if (priv->datasock != NULL) {
740 NG_FREE_ITEM(item); /* drop the reference to the node */
745 * Link the PCB and the private data struct. and note the extra
746 * reference. Drop the extra reference on the node.
748 priv->datasock = pcbp;
749 pcbp->sockdata = priv;
750 priv->refs++; /* XXX possible race if it's being freed */
751 NG_FREE_ITEM(item); /* drop the reference to the node */
756 * Binding a socket means giving the corresponding node a name
759 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
761 struct ngsock *const priv = pcbp->sockdata;
762 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
768 if ((sap->sg_len < 4)
769 || (sap->sg_len > (NG_NODESIZ + 2))
770 || (sap->sg_data[0] == '\0')
771 || (sap->sg_data[sap->sg_len - 3] != '\0')) {
775 return (ng_name_node(priv->node, sap->sg_data));
779 * Take a message and pass it up to the control socket associated
783 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
785 struct socket *const so = pcbp->ng_socket;
789 /* Copy the message itself into an mbuf chain */
790 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
791 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
793 /* Here we free the message, as we are the end of the line.
794 * We need to do that regardless of whether we got mbufs. */
802 /* Send it up to the socket */
803 if (sbappendaddr(&so->so_rcv,
804 (struct sockaddr *) addr, mdata, NULL) == 0) {
814 * You can only create new nodes from the socket end of things.
817 ngs_constructor(node_p nodep)
823 * We allow any hook to be connected to the node.
824 * There is no per-hook private information though.
827 ngs_newhook(node_p node, hook_p hook, const char *name)
829 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
834 * if only one hook, allow read(2) and write(2) to work.
837 ngs_connect(hook_p hook)
839 node_p node = NG_HOOK_NODE(hook);
840 struct ngsock *priv = NG_NODE_PRIVATE(node);
843 && (priv->datasock->ng_socket)) {
844 if (NG_NODE_NUMHOOKS(node) == 1) {
845 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
847 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
854 * Incoming messages get passed up to the control socket.
855 * Unless they are for us specifically (socket_type)
858 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
860 struct ngsock *const priv = NG_NODE_PRIVATE(node);
861 struct ngpcb *const pcbp = priv->ctlsock;
862 struct sockaddr_ng *addr;
866 ng_ID_t retaddr = NGI_RETADDR(item);
869 NGI_GET_MSG(item, msg);
870 NG_FREE_ITEM(item); /* we have all we need */
872 /* Only allow mesgs to be passed if we have the control socket.
873 * Data sockets can only support the generic messages. */
879 #ifdef TRACE_MESSAGES
880 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
882 msg->header.typecookie,
889 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
890 switch (msg->header.cmd) {
891 case NGM_SOCK_CMD_NOLINGER:
892 priv->flags |= NGS_FLAG_NOLINGER;
894 case NGM_SOCK_CMD_LINGER:
895 priv->flags &= ~NGS_FLAG_NOLINGER;
898 error = EINVAL; /* unknown command */
900 /* Free the message and return */
905 /* Get the return address into a sockaddr */
906 sprintf(retabuf,"[%x]:", retaddr);
907 addrlen = strlen(retabuf);
908 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT);
913 addr->sg_len = addrlen + 3;
914 addr->sg_family = AF_NETGRAPH;
915 bcopy(retabuf, addr->sg_data, addrlen);
916 addr->sg_data[addrlen] = '\0';
919 error = ship_msg(pcbp, msg, addr);
920 FREE(addr, M_NETGRAPH_PATH);
925 * Receive data on a hook
928 ngs_rcvdata(hook_p hook, item_p item)
930 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
931 struct ngpcb *const pcbp = priv->datasock;
933 struct sockaddr_ng *addr;
934 char *addrbuf[NG_HOOKSIZ + 4];
940 /* If there is no data socket, black-hole it */
945 so = pcbp->ng_socket;
947 /* Get the return address into a sockaddr. */
948 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
949 addr = (struct sockaddr_ng *) addrbuf;
950 addr->sg_len = addrlen + 3;
951 addr->sg_family = AF_NETGRAPH;
952 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
953 addr->sg_data[addrlen] = '\0';
955 /* Try to tell the socket which hook it came in on */
956 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
968 * For this type, removal of the last link destroys the node
969 * if the NOLINGER flag is set.
972 ngs_disconnect(hook_p hook)
974 node_p node = NG_HOOK_NODE(hook);
975 struct ngsock *const priv = NG_NODE_PRIVATE(node);
978 && (priv->datasock->ng_socket)) {
979 if (NG_NODE_NUMHOOKS(node) == 1) {
980 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
982 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
986 if ((priv->flags & NGS_FLAG_NOLINGER )
987 && (NG_NODE_NUMHOOKS(node) == 0)
988 && (NG_NODE_IS_VALID(node))) {
989 ng_rmnode_self(node);
995 * Do local shutdown processing.
996 * In this case, that involves making sure the socket
997 * knows we should be shutting down.
1000 ngs_shutdown(node_p node)
1002 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1003 struct ngpcb *const dpcbp = priv->datasock;
1004 struct ngpcb *const pcbp = priv->ctlsock;
1006 if (dpcbp != NULL) {
1007 soisdisconnected(dpcbp->ng_socket);
1008 dpcbp->sockdata = NULL;
1009 priv->datasock = NULL;
1013 soisdisconnected(pcbp->ng_socket);
1014 pcbp->sockdata = NULL;
1015 priv->ctlsock = NULL;
1018 NG_NODE_SET_PRIVATE(node, NULL);
1019 NG_NODE_UNREF(node);
1020 FREE(priv, M_NETGRAPH_SOCK);
1025 dummy_disconnect(struct socket *so)
1030 * Control and data socket type descriptors
1033 static struct pr_usrreqs ngc_usrreqs = {
1035 .pru_attach = ngc_attach,
1036 .pru_bind = ngc_bind,
1037 .pru_connect = ngc_connect,
1038 .pru_detach = ngc_detach,
1039 .pru_disconnect = dummy_disconnect,
1040 .pru_peeraddr = NULL,
1041 .pru_send = ngc_send,
1042 .pru_shutdown = NULL,
1043 .pru_sockaddr = ng_setsockaddr,
1046 static struct pr_usrreqs ngd_usrreqs = {
1048 .pru_attach = ngd_attach,
1050 .pru_connect = ngd_connect,
1051 .pru_detach = ngd_detach,
1052 .pru_disconnect = dummy_disconnect,
1053 .pru_peeraddr = NULL,
1054 .pru_send = ngd_send,
1055 .pru_shutdown = NULL,
1056 .pru_sockaddr ng_setsockaddr,
1060 * Definitions of protocols supported in the NETGRAPH domain.
1063 extern struct domain ngdomain; /* stop compiler warnings */
1065 static struct protosw ngsw[] = {
1067 SOCK_DGRAM, /* protocol type */
1068 &ngdomain, /* backpointer to domain */
1070 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */, /* flags */
1071 0, 0, 0, 0, /* input, output, ctlinput, ctloutput */
1073 0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */
1074 &ngc_usrreqs, /* usrreq table (above) */
1075 /*{NULL}*/ /* pffh (protocol filter head?) */
1078 SOCK_DGRAM, /* protocol type */
1079 &ngdomain, /* backpointer to domain */
1081 PR_ATOMIC | PR_ADDR, /* flags */
1082 0, 0, 0, 0, /* input, output, ctlinput, ctloutput */
1083 NULL, /* ousrreq() */
1084 0, 0, 0, 0, /* init, fasttimeo, slowtimo, drain */
1085 &ngd_usrreqs, /* usrreq table (above) */
1086 /*{NULL}*/ /* pffh (protocol filter head?) */
1090 struct domain ngdomain = {
1094 NULL, /* externalise() */
1095 NULL, /* dispose() */
1096 ngsw, /* protosw entry */
1097 &ngsw[sizeof(ngsw) / sizeof(ngsw[0])], /* Number of protosw entries */
1098 NULL, /* next domain in list */
1099 NULL, /* rtattach() */
1100 0, /* arg to rtattach in bits */
1105 * Handle loading and unloading for this node type
1106 * This is to handle auxiliary linkages (e.g protocol domain addition).
1109 ngs_mod_event(module_t mod, int event, void *data)
1115 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1116 /* Register protocol domain */
1117 net_add_domain(&ngdomain);
1120 /* Insure there are no open netgraph sockets */
1121 if (!LIST_EMPTY(&ngsocklist)) {
1127 if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) {
1128 /* Unregister protocol domain XXX can't do this yet.. */
1129 if ((error = net_rm_domain(&ngdomain)) != 0)
1131 mtx_destroy(&ngsocketlist_mtx);
1143 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1144 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1145 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1146 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1147 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");