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(struct ngpcb *pcbp, int type);
126 static void ng_socket_free_priv(struct ngsock *priv);
127 /*static int ng_internalize(struct mbuf *m, struct thread *p); */
129 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
130 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
132 static int ngs_mod_event(module_t mod, int event, void *data);
133 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
134 struct sockaddr_ng *addr);
135 static void ng_socket_item_applied(void *context, int error);
137 /* Netgraph type descriptor */
138 static struct ng_type typestruct = {
139 .version = NG_ABI_VERSION,
140 .name = NG_SOCKET_NODE_TYPE,
141 .mod_event = ngs_mod_event,
142 .constructor = ngs_constructor,
143 .rcvmsg = ngs_rcvmsg,
144 .shutdown = ngs_shutdown,
145 .newhook = ngs_newhook,
146 .connect = ngs_connect,
147 .rcvdata = ngs_rcvdata,
148 .disconnect = ngs_disconnect,
150 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
153 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
154 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
155 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
156 static u_long ngpdg_recvspace = 20 * 1024;
157 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
158 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
160 /* List of all sockets */
161 static LIST_HEAD(, ngpcb) ngsocklist;
163 static struct mtx ngsocketlist_mtx;
165 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
167 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
172 /***************************************************************
174 ***************************************************************/
177 ngc_attach(struct socket *so, int proto, struct thread *td)
179 struct ngpcb *const pcbp = sotongpcb(so);
185 return (ng_attach_cntl(so));
189 ngc_detach(struct socket *so)
191 struct ngpcb *const pcbp = sotongpcb(so);
195 ng_detach_common(pcbp, NG_CONTROL);
200 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
201 struct mbuf *control, struct thread *td)
203 struct ngpcb *const pcbp = sotongpcb(so);
204 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
205 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
217 if (control && (error = ng_internalize(control, td))) {
218 if (pcbp->sockdata == NULL) {
230 /* Require destination as there may be >= 1 hooks on this node */
232 error = EDESTADDRREQ;
236 /* Allocate an expendable buffer for the path, chop off
237 * the sockaddr header, and make sure it's NUL terminated */
238 len = sap->sg_len - 2;
239 MALLOC(path, char *, len + 1, M_NETGRAPH_PATH, M_WAITOK);
244 bcopy(sap->sg_data, path, len);
247 /* Move the actual message out of mbufs into a linear buffer.
248 * Start by adding up the size of the data. (could use mh_len?) */
249 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
252 /* Move the data into a linear buffer as well. Messages are not
253 * delivered in mbufs. */
254 MALLOC(msg, struct ng_mesg *, len + 1, M_NETGRAPH_MSG, M_WAITOK);
259 m_copydata(m, 0, len, (char *)msg);
261 if (msg->header.version != NG_VERSION) {
268 * We look into the message and if it mkpeers a node of unknown type, we
269 * try to load it. We need to do this now, in syscall thread, because if
270 * message gets queued and applied later we will get panic.
272 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
273 msg->header.cmd == NGM_MKPEER) {
274 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
275 struct ng_type *type;
277 if ((type = ng_findtype(mkp->type)) == NULL) {
278 char filename[NG_TYPESIZ + 3];
281 /* Not found, try to load it as a loadable module. */
282 snprintf(filename, sizeof(filename), "ng_%s",
285 error = linker_load_module(NULL, filename, NULL, NULL,
289 FREE(msg, M_NETGRAPH_MSG);
294 /* See if type has been loaded successfully. */
295 if ((type = ng_findtype(mkp->type)) == NULL) {
296 FREE(msg, M_NETGRAPH_MSG);
303 if ((item = ng_package_msg(msg, M_WAITOK)) == NULL) {
305 #ifdef TRACE_MESSAGES
306 printf("ng_package_msg: err=%d\n", error);
310 if ((error = ng_address_path((pcbp->sockdata->node), item,
312 #ifdef TRACE_MESSAGES
313 printf("ng_address_path: errx=%d\n", error);
318 #ifdef TRACE_MESSAGES
319 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
320 item->el_dest->nd_ID,
321 msg->header.typecookie,
326 item->el_dest->nd_type->name);
330 * We do not want to return from syscall until the item
331 * is processed by destination node. We register callback
332 * on the item, which will update priv->error when item
334 * If ng_snd_item() has queued item, we sleep until
335 * callback wakes us up.
337 item->apply = ng_socket_item_applied;
338 item->context = priv;
341 error = ng_snd_item(item, NG_PROGRESS);
343 if (error == EINPROGRESS) {
344 mtx_lock(&priv->mtx);
345 if (priv->error == -1)
346 msleep(priv, &priv->mtx, 0, "ngsock", 0);
347 mtx_unlock(&priv->mtx);
348 KASSERT(priv->error != -1,
349 ("ng_socket: priv->error wasn't updated"));
355 FREE(path, M_NETGRAPH_PATH);
364 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
366 struct ngpcb *const pcbp = sotongpcb(so);
370 return (ng_bind(nam, pcbp));
374 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
376 printf(" program tried to connect control socket to remote node\n ");
378 * At this time refuse to do this.. it used to
379 * do something but it was undocumented and not used.
384 /***************************************************************
386 ***************************************************************/
389 ngd_attach(struct socket *so, int proto, struct thread *td)
391 struct ngpcb *const pcbp = sotongpcb(so);
395 return (ng_attach_data(so));
399 ngd_detach(struct socket *so)
401 struct ngpcb *const pcbp = sotongpcb(so);
405 ng_detach_common(pcbp, NG_DATA);
410 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
411 struct mbuf *control, struct thread *td)
413 struct ngpcb *const pcbp = sotongpcb(so);
414 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
417 char hookname[NG_HOOKSIZ];
419 if ((pcbp == NULL) || (control != NULL)) {
423 if (pcbp->sockdata == NULL) {
429 len = 0; /* Make compiler happy. */
431 len = sap->sg_len - 2;
434 * If the user used any of these ways to not specify an address
435 * then handle specially.
437 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
438 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
439 error = EDESTADDRREQ;
443 * if exactly one hook exists, just use it.
444 * Special case to allow write(2) to work on an ng_socket.
446 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
448 if (len >= NG_HOOKSIZ) {
454 * chop off the sockaddr header, and make sure it's NUL
457 bcopy(sap->sg_data, hookname, len);
458 hookname[len] = '\0';
460 /* Find the correct hook from 'hookname' */
461 LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) {
462 if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) {
467 error = EHOSTUNREACH;
471 /* Send data (OK if hook is NULL) */
472 NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */
483 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
485 struct ngpcb *const pcbp = sotongpcb(so);
489 return (ng_connect_data(nam, pcbp));
493 * Used for both data and control sockets
496 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
499 struct sockaddr_ng *sg;
503 /* Why isn't sg_data a `char[1]' ? :-( */
504 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
506 pcbp = sotongpcb(so);
507 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
508 /* XXXGL: can this still happen? */
511 mtx_lock(&pcbp->sockdata->mtx);
512 if (pcbp->sockdata->node != NULL) {
513 node_p node = pcbp->sockdata->node;
514 int namelen = 0; /* silence compiler! */
516 if (NG_NODE_HAS_NAME(node))
517 sg_len += namelen = strlen(NG_NODE_NAME(node));
519 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
521 if (NG_NODE_HAS_NAME(node))
522 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
525 sg->sg_family = AF_NETGRAPH;
526 *addr = (struct sockaddr *)sg;
527 mtx_unlock(&pcbp->sockdata->mtx);
529 mtx_unlock(&pcbp->sockdata->mtx);
537 * Attach a socket to it's protocol specific partner.
538 * For a control socket, actually create a netgraph node and attach
543 ng_attach_cntl(struct socket *so)
549 /* Allocate node private info */
550 MALLOC(priv, struct ngsock *,
551 sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
555 /* Setup protocol control block */
556 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
557 FREE(priv, M_NETGRAPH_SOCK);
560 pcbp = sotongpcb(so);
562 /* Link the pcb the private data. */
563 priv->ctlsock = pcbp;
564 pcbp->sockdata = priv;
567 /* Initialize mutex. */
568 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
570 /* Make the generic node components */
571 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
572 FREE(priv, M_NETGRAPH_SOCK);
573 ng_detach_common(pcbp, NG_CONTROL);
577 /* Link the node and the private data. */
578 NG_NODE_SET_PRIVATE(priv->node, priv);
579 NG_NODE_REF(priv->node);
586 ng_attach_data(struct socket *so)
588 return(ng_attach_common(so, NG_DATA));
592 * Set up a socket protocol control block.
593 * This code is shared between control and data sockets.
596 ng_attach_common(struct socket *so, int type)
601 /* Standard socket setup stuff */
602 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
606 /* Allocate the pcb */
607 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
612 /* Link the pcb and the socket */
613 so->so_pcb = (caddr_t) pcbp;
614 pcbp->ng_socket = so;
616 /* Add the socket to linked list */
617 mtx_lock(&ngsocketlist_mtx);
618 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
619 mtx_unlock(&ngsocketlist_mtx);
624 * Disassociate the socket from it's protocol specific
625 * partner. If it's attached to a node's private data structure,
626 * then unlink from that too. If we were the last socket attached to it,
627 * then shut down the entire node. Shared code for control and data sockets.
630 ng_detach_common(struct ngpcb *pcbp, int which)
632 struct ngsock *priv = pcbp->sockdata;
635 mtx_lock(&priv->mtx);
639 priv->ctlsock = NULL;
642 priv->datasock = NULL;
647 pcbp->sockdata = NULL;
649 ng_socket_free_priv(priv);
652 pcbp->ng_socket->so_pcb = NULL;
653 mtx_lock(&ngsocketlist_mtx);
654 LIST_REMOVE(pcbp, socks);
655 mtx_unlock(&ngsocketlist_mtx);
660 * Remove a reference from node private data.
663 ng_socket_free_priv(struct ngsock *priv)
665 mtx_assert(&priv->mtx, MA_OWNED);
669 if (priv->refs == 0) {
670 mtx_destroy(&priv->mtx);
671 FREE(priv, M_NETGRAPH_SOCK);
675 if ((priv->refs == 1) && (priv->node != NULL)) {
676 node_p node = priv->node;
679 mtx_unlock(&priv->mtx);
681 ng_rmnode_self(node);
683 mtx_unlock(&priv->mtx);
688 * File descriptors can be passed into an AF_NETGRAPH socket.
689 * Note, that file descriptors cannot be passed OUT.
690 * Only character device descriptors are accepted.
691 * Character devices are useful to connect a graph to a device,
692 * which after all is the purpose of this whole system.
695 ng_internalize(struct mbuf *control, struct thread *td)
697 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
703 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
704 cm->cmsg_len != control->m_len) {
709 /* Check there is only one FD. XXX what would more than one signify? */
710 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
716 /* Check that the FD given is legit. and change it to a pointer to a
719 if ((error = fget(td, fd, &fp)) != 0)
722 /* Depending on what kind of resource it is, act differently. For
723 * devices, we treat it as a file. For an AF_NETGRAPH socket,
724 * shortcut straight to the node. */
725 switch (fp->f_type) {
728 if (vn && (vn->v_type == VCHR)) {
729 /* for a VCHR, actually reference the FILE */
731 /* XXX then what :) */
732 /* how to pass on to other modules? */
750 * Connect the data socket to a named control socket node.
753 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
755 struct sockaddr_ng *sap;
761 /* If we are already connected, don't do it again */
762 if (pcbp->sockdata != NULL)
765 /* Find the target (victim) and check it doesn't already have a data
766 * socket. Also check it is a 'socket' type node.
767 * Use ng_package_data() and address_path() to do this.
770 sap = (struct sockaddr_ng *) nam;
771 /* The item will hold the node reference */
772 item = ng_package_data(NULL, NG_WAITOK);
776 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
777 return (error); /* item is freed on failure */
780 * Extract node from item and free item. Remember we now have
781 * a reference on the node. The item holds it for us.
782 * when we free the item we release the reference.
784 farnode = item->el_dest; /* shortcut */
785 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
786 NG_FREE_ITEM(item); /* drop the reference to the node */
789 priv = NG_NODE_PRIVATE(farnode);
790 if (priv->datasock != NULL) {
791 NG_FREE_ITEM(item); /* drop the reference to the node */
796 * Link the PCB and the private data struct. and note the extra
797 * reference. Drop the extra reference on the node.
799 mtx_lock(&priv->mtx);
800 priv->datasock = pcbp;
801 pcbp->sockdata = priv;
803 mtx_unlock(&priv->mtx);
804 NG_FREE_ITEM(item); /* drop the reference to the node */
809 * Binding a socket means giving the corresponding node a name
812 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
814 struct ngsock *const priv = pcbp->sockdata;
815 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
821 if ((sap->sg_len < 4)
822 || (sap->sg_len > (NG_NODESIZ + 2))
823 || (sap->sg_data[0] == '\0')
824 || (sap->sg_data[sap->sg_len - 3] != '\0')) {
828 return (ng_name_node(priv->node, sap->sg_data));
832 * Take a message and pass it up to the control socket associated
836 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
838 struct socket *const so = pcbp->ng_socket;
843 /* Copy the message itself into an mbuf chain */
844 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
845 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
847 /* Here we free the message, as we are the end of the line.
848 * We need to do that regardless of whether we got mbufs. */
856 /* Send it up to the socket */
857 if (sbappendaddr(&so->so_rcv,
858 (struct sockaddr *) addr, mdata, NULL) == 0) {
861 error = so->so_error = ENOBUFS;
867 /***************************************************************
869 ***************************************************************/
872 * You can only create new nodes from the socket end of things.
875 ngs_constructor(node_p nodep)
881 * We allow any hook to be connected to the node.
882 * There is no per-hook private information though.
885 ngs_newhook(node_p node, hook_p hook, const char *name)
887 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
892 * if only one hook, allow read(2) and write(2) to work.
895 ngs_connect(hook_p hook)
897 node_p node = NG_HOOK_NODE(hook);
898 struct ngsock *priv = NG_NODE_PRIVATE(node);
901 && (priv->datasock->ng_socket)) {
902 if (NG_NODE_NUMHOOKS(node) == 1) {
903 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
905 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
912 * Incoming messages get passed up to the control socket.
913 * Unless they are for us specifically (socket_type)
916 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
918 struct ngsock *const priv = NG_NODE_PRIVATE(node);
919 struct ngpcb *const pcbp = priv->ctlsock;
920 struct sockaddr_ng *addr;
924 ng_ID_t retaddr = NGI_RETADDR(item);
927 NGI_GET_MSG(item, msg);
928 NG_FREE_ITEM(item); /* we have all we need */
930 /* Only allow mesgs to be passed if we have the control socket.
931 * Data sockets can only support the generic messages. */
937 #ifdef TRACE_MESSAGES
938 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
940 msg->header.typecookie,
947 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
948 switch (msg->header.cmd) {
949 case NGM_SOCK_CMD_NOLINGER:
950 priv->flags |= NGS_FLAG_NOLINGER;
952 case NGM_SOCK_CMD_LINGER:
953 priv->flags &= ~NGS_FLAG_NOLINGER;
956 error = EINVAL; /* unknown command */
958 /* Free the message and return */
963 /* Get the return address into a sockaddr */
964 sprintf(retabuf,"[%x]:", retaddr);
965 addrlen = strlen(retabuf);
966 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT);
971 addr->sg_len = addrlen + 3;
972 addr->sg_family = AF_NETGRAPH;
973 bcopy(retabuf, addr->sg_data, addrlen);
974 addr->sg_data[addrlen] = '\0';
977 error = ship_msg(pcbp, msg, addr);
978 FREE(addr, M_NETGRAPH_PATH);
983 * Receive data on a hook
986 ngs_rcvdata(hook_p hook, item_p item)
988 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
989 struct ngpcb *const pcbp = priv->datasock;
991 struct sockaddr_ng *addr;
992 char *addrbuf[NG_HOOKSIZ + 4];
998 /* If there is no data socket, black-hole it */
1003 so = pcbp->ng_socket;
1005 /* Get the return address into a sockaddr. */
1006 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1007 addr = (struct sockaddr_ng *) addrbuf;
1008 addr->sg_len = addrlen + 3;
1009 addr->sg_family = AF_NETGRAPH;
1010 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1011 addr->sg_data[addrlen] = '\0';
1013 /* Try to tell the socket which hook it came in on */
1014 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
1024 * Hook disconnection
1026 * For this type, removal of the last link destroys the node
1027 * if the NOLINGER flag is set.
1030 ngs_disconnect(hook_p hook)
1032 node_p node = NG_HOOK_NODE(hook);
1033 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1035 if ((priv->datasock)
1036 && (priv->datasock->ng_socket)) {
1037 if (NG_NODE_NUMHOOKS(node) == 1) {
1038 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1040 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1044 if ((priv->flags & NGS_FLAG_NOLINGER )
1045 && (NG_NODE_NUMHOOKS(node) == 0)
1046 && (NG_NODE_IS_VALID(node))) {
1047 ng_rmnode_self(node);
1053 * Do local shutdown processing.
1054 * In this case, that involves making sure the socket
1055 * knows we should be shutting down.
1058 ngs_shutdown(node_p node)
1060 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1061 struct ngpcb *const dpcbp = priv->datasock;
1062 struct ngpcb *const pcbp = priv->ctlsock;
1065 soisdisconnected(dpcbp->ng_socket);
1068 soisdisconnected(pcbp->ng_socket);
1070 mtx_lock(&priv->mtx);
1072 NG_NODE_SET_PRIVATE(node, NULL);
1073 ng_socket_free_priv(priv);
1075 NG_NODE_UNREF(node);
1080 ng_socket_item_applied(void *context, int error)
1082 struct ngsock *const priv = (struct ngsock *)context;
1084 mtx_lock(&priv->mtx);
1085 priv->error = error;
1087 mtx_unlock(&priv->mtx);
1092 dummy_disconnect(struct socket *so)
1097 * Control and data socket type descriptors
1100 static struct pr_usrreqs ngc_usrreqs = {
1102 .pru_attach = ngc_attach,
1103 .pru_bind = ngc_bind,
1104 .pru_connect = ngc_connect,
1105 .pru_detach = ngc_detach,
1106 .pru_disconnect = dummy_disconnect,
1107 .pru_peeraddr = NULL,
1108 .pru_send = ngc_send,
1109 .pru_shutdown = NULL,
1110 .pru_sockaddr = ng_setsockaddr,
1113 static struct pr_usrreqs ngd_usrreqs = {
1115 .pru_attach = ngd_attach,
1117 .pru_connect = ngd_connect,
1118 .pru_detach = ngd_detach,
1119 .pru_disconnect = dummy_disconnect,
1120 .pru_peeraddr = NULL,
1121 .pru_send = ngd_send,
1122 .pru_shutdown = NULL,
1123 .pru_sockaddr = ng_setsockaddr,
1127 * Definitions of protocols supported in the NETGRAPH domain.
1130 extern struct domain ngdomain; /* stop compiler warnings */
1132 static struct protosw ngsw[] = {
1134 .pr_type = SOCK_DGRAM,
1135 .pr_domain = &ngdomain,
1136 .pr_protocol = NG_CONTROL,
1137 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1138 .pr_usrreqs = &ngc_usrreqs
1141 .pr_type = SOCK_DGRAM,
1142 .pr_domain = &ngdomain,
1143 .pr_protocol = NG_DATA,
1144 .pr_flags = PR_ATOMIC | PR_ADDR,
1145 .pr_usrreqs = &ngd_usrreqs
1149 struct domain ngdomain = {
1150 .dom_family = AF_NETGRAPH,
1151 .dom_name = "netgraph",
1152 .dom_protosw = ngsw,
1153 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1157 * Handle loading and unloading for this node type
1158 * This is to handle auxiliary linkages (e.g protocol domain addition).
1161 ngs_mod_event(module_t mod, int event, void *data)
1167 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1168 /* Register protocol domain */
1169 net_add_domain(&ngdomain);
1172 /* Insure there are no open netgraph sockets */
1173 if (!LIST_EMPTY(&ngsocklist)) {
1179 if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) {
1180 /* Unregister protocol domain XXX can't do this yet.. */
1181 if ((error = net_rm_domain(&ngdomain)) != 0)
1183 mtx_destroy(&ngsocketlist_mtx);
1195 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1196 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1197 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1198 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1199 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");