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 * $DragonFly: src/sys/netgraph7/ng_socket.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
46 * Netgraph socket nodes
48 * There are two types of netgraph sockets, control and data.
49 * Control sockets have a netgraph node, but data sockets are
50 * parasitic on control sockets, and have no node of their own.
53 #include <sys/param.h>
54 #include <sys/domain.h>
55 #include <sys/kernel.h>
56 #include <sys/linker.h>
58 #include <sys/malloc.h>
60 #include <sys/mutex.h>
62 #include <sys/protosw.h>
63 #include <sys/queue.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysctl.h>
69 #include <sys/vnode.h>
71 #include "ng_message.h"
73 #include "ng_socketvar.h"
74 #include "ng_socket.h"
76 #ifdef NG_SEPARATE_MALLOC
77 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
78 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
80 #define M_NETGRAPH_PATH M_NETGRAPH
81 #define M_NETGRAPH_SOCK M_NETGRAPH
85 * It's Ascii-art time!
86 * +-------------+ +-------------+
87 * |socket (ctl)| |socket (data)|
88 * +-------------+ +-------------+
92 * +-----------+ +-----------+
93 * |pcb (ctl)| |pcb (data)|
94 * +-----------+ +-----------+
98 * +--------------------------+
99 * | Socket type private |
101 * +--------------------------+
110 /* Netgraph node methods */
111 static ng_constructor_t ngs_constructor;
112 static ng_rcvmsg_t ngs_rcvmsg;
113 static ng_shutdown_t ngs_shutdown;
114 static ng_newhook_t ngs_newhook;
115 static ng_connect_t ngs_connect;
116 static ng_rcvdata_t ngs_rcvdata;
117 static ng_disconnect_t ngs_disconnect;
119 /* Internal methods */
120 static int ng_attach_data(struct socket *so);
121 static int ng_attach_cntl(struct socket *so);
122 static int ng_attach_common(struct socket *so, int type);
123 static void ng_detach_common(struct ngpcb *pcbp, int type);
124 static void ng_socket_free_priv(struct ngsock *priv);
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 void ng_socket_item_applied(void *context, int error);
134 /* Netgraph type descriptor */
135 static struct ng_type typestruct = {
136 .version = NG_ABI_VERSION,
137 .name = NG_SOCKET_NODE_TYPE,
138 .mod_event = ngs_mod_event,
139 .constructor = ngs_constructor,
140 .rcvmsg = ngs_rcvmsg,
141 .shutdown = ngs_shutdown,
142 .newhook = ngs_newhook,
143 .connect = ngs_connect,
144 .rcvdata = ngs_rcvdata,
145 .disconnect = ngs_disconnect,
147 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
150 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
151 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
152 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
153 static u_long ngpdg_recvspace = 20 * 1024;
154 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
155 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
157 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
159 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
164 /***************************************************************
166 ***************************************************************/
169 ngc_attach(struct socket *so, int proto, struct thread *td)
171 struct ngpcb *const pcbp = sotongpcb(so);
174 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
179 return (ng_attach_cntl(so));
183 ngc_detach(struct socket *so)
185 struct ngpcb *const pcbp = sotongpcb(so);
187 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
188 ng_detach_common(pcbp, NG_CONTROL);
192 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
193 struct mbuf *control, struct thread *td)
195 struct ngpcb *const pcbp = sotongpcb(so);
196 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
197 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
203 struct ng_apply_info apply;
206 if (control && (error = ng_internalize(control, td))) {
207 if (pcbp->sockdata == NULL) {
219 /* Require destination as there may be >= 1 hooks on this node. */
221 error = EDESTADDRREQ;
226 * Allocate an expendable buffer for the path, chop off
227 * the sockaddr header, and make sure it's NUL terminated.
229 len = sap->sg_len - 2;
230 path = kmalloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
231 bcopy(sap->sg_data, path, len);
235 * Move the actual message out of mbufs into a linear buffer.
236 * Start by adding up the size of the data. (could use mh_len?)
238 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
242 * Move the data into a linear buffer as well.
243 * Messages are not delivered in mbufs.
245 msg = kmalloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
246 m_copydata(m, 0, len, (char *)msg);
248 if (msg->header.version != NG_VERSION) {
249 kfree(msg, M_NETGRAPH_MSG);
256 * We look into the message and if it mkpeers a node of unknown type, we
257 * try to load it. We need to do this now, in syscall thread, because if
258 * message gets queued and applied later we will get panic.
260 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
261 msg->header.cmd == NGM_MKPEER) {
262 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
263 struct ng_type *type;
265 if ((type = ng_findtype(mkp->type)) == NULL) {
266 char filename[NG_TYPESIZ + 3];
269 /* Not found, try to load it as a loadable module. */
270 snprintf(filename, sizeof(filename), "ng_%s",
272 error = kern_kldload(curthread, filename, &fileid);
274 kfree(msg, M_NETGRAPH_MSG);
278 /* See if type has been loaded successfully. */
279 if ((type = ng_findtype(mkp->type)) == NULL) {
280 kfree(msg, M_NETGRAPH_MSG);
281 (void)kern_kldunload(curthread, fileid,
282 LINKER_UNLOAD_NORMAL);
289 item = ng_package_msg(msg, NG_WAITOK);
290 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
292 #ifdef TRACE_MESSAGES
293 printf("ng_address_path: errx=%d\n", error);
298 #ifdef TRACE_MESSAGES
299 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
300 item->el_dest->nd_ID,
301 msg->header.typecookie,
306 item->el_dest->nd_type->name);
310 * We do not want to return from syscall until the item
311 * is processed by destination node. We register callback
312 * on the item, which will update priv->error when item
314 * If ng_snd_item() has queued item, we sleep until
315 * callback wakes us up.
317 bzero(&apply, sizeof(apply));
318 apply.apply = ng_socket_item_applied;
319 apply.context = priv;
320 item->apply = &apply;
323 error = ng_snd_item(item, 0);
325 mtx_lock(&priv->mtx);
326 if (priv->error == -1)
327 msleep(priv, &priv->mtx, 0, "ngsock", 0);
328 mtx_unlock(&priv->mtx);
329 KASSERT(priv->error != -1,
330 ("ng_socket: priv->error wasn't updated"));
335 kfree(path, M_NETGRAPH_PATH);
344 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
346 struct ngpcb *const pcbp = sotongpcb(so);
350 return (ng_bind(nam, pcbp));
354 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
357 * At this time refuse to do this.. it used to
358 * do something but it was undocumented and not used.
360 printf("program tried to connect control socket to remote node\n");
364 /***************************************************************
366 ***************************************************************/
369 ngd_attach(struct socket *so, int proto, struct thread *td)
371 struct ngpcb *const pcbp = sotongpcb(so);
375 return (ng_attach_data(so));
379 ngd_detach(struct socket *so)
381 struct ngpcb *const pcbp = sotongpcb(so);
383 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
384 ng_detach_common(pcbp, NG_DATA);
388 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
389 struct mbuf *control, struct thread *td)
391 struct ngpcb *const pcbp = sotongpcb(so);
392 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
395 char hookname[NG_HOOKSIZ];
397 if ((pcbp == NULL) || (control != NULL)) {
401 if (pcbp->sockdata == NULL) {
407 len = 0; /* Make compiler happy. */
409 len = sap->sg_len - 2;
412 * If the user used any of these ways to not specify an address
413 * then handle specially.
415 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
416 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
417 error = EDESTADDRREQ;
421 * If exactly one hook exists, just use it.
422 * Special case to allow write(2) to work on an ng_socket.
424 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
426 if (len >= NG_HOOKSIZ) {
432 * chop off the sockaddr header, and make sure it's NUL
435 bcopy(sap->sg_data, hookname, len);
436 hookname[len] = '\0';
438 /* Find the correct hook from 'hookname' */
439 hook = ng_findhook(pcbp->sockdata->node, hookname);
441 error = EHOSTUNREACH;
447 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
458 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
460 struct ngpcb *const pcbp = sotongpcb(so);
464 return (ng_connect_data(nam, pcbp));
468 * Used for both data and control sockets
471 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
474 struct sockaddr_ng *sg;
478 /* Why isn't sg_data a `char[1]' ? :-( */
479 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
481 pcbp = sotongpcb(so);
482 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
483 /* XXXGL: can this still happen? */
486 mtx_lock(&pcbp->sockdata->mtx);
487 if (pcbp->sockdata->node != NULL) {
488 node_p node = pcbp->sockdata->node;
489 int namelen = 0; /* silence compiler! */
491 if (NG_NODE_HAS_NAME(node))
492 sg_len += namelen = strlen(NG_NODE_NAME(node));
494 sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
496 if (NG_NODE_HAS_NAME(node))
497 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
500 sg->sg_family = AF_NETGRAPH;
501 *addr = (struct sockaddr *)sg;
502 mtx_unlock(&pcbp->sockdata->mtx);
504 mtx_unlock(&pcbp->sockdata->mtx);
512 * Attach a socket to it's protocol specific partner.
513 * For a control socket, actually create a netgraph node and attach
518 ng_attach_cntl(struct socket *so)
524 /* Allocate node private info */
525 priv = kmalloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
527 /* Setup protocol control block */
528 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
529 kfree(priv, M_NETGRAPH_SOCK);
532 pcbp = sotongpcb(so);
534 /* Link the pcb the private data. */
535 priv->ctlsock = pcbp;
536 pcbp->sockdata = priv;
539 /* Initialize mutex. */
540 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
542 /* Make the generic node components */
543 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
544 kfree(priv, M_NETGRAPH_SOCK);
545 ng_detach_common(pcbp, NG_CONTROL);
549 /* Link the node and the private data. */
550 NG_NODE_SET_PRIVATE(priv->node, priv);
551 NG_NODE_REF(priv->node);
558 ng_attach_data(struct socket *so)
560 return (ng_attach_common(so, NG_DATA));
564 * Set up a socket protocol control block.
565 * This code is shared between control and data sockets.
568 ng_attach_common(struct socket *so, int type)
573 /* Standard socket setup stuff. */
574 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
578 /* Allocate the pcb. */
579 pcbp = kmalloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
582 /* Link the pcb and the socket. */
583 so->so_pcb = (caddr_t)pcbp;
584 pcbp->ng_socket = so;
590 * Disassociate the socket from it's protocol specific
591 * partner. If it's attached to a node's private data structure,
592 * then unlink from that too. If we were the last socket attached to it,
593 * then shut down the entire node. Shared code for control and data sockets.
596 ng_detach_common(struct ngpcb *pcbp, int which)
598 struct ngsock *priv = pcbp->sockdata;
601 mtx_lock(&priv->mtx);
605 priv->ctlsock = NULL;
608 priv->datasock = NULL;
613 pcbp->sockdata = NULL;
615 ng_socket_free_priv(priv);
618 pcbp->ng_socket->so_pcb = NULL;
623 * Remove a reference from node private data.
626 ng_socket_free_priv(struct ngsock *priv)
628 mtx_assert(&priv->mtx, MA_OWNED);
632 if (priv->refs == 0) {
633 mtx_destroy(&priv->mtx);
634 kfree(priv, M_NETGRAPH_SOCK);
638 if ((priv->refs == 1) && (priv->node != NULL)) {
639 node_p node = priv->node;
642 mtx_unlock(&priv->mtx);
644 ng_rmnode_self(node);
646 mtx_unlock(&priv->mtx);
651 * File descriptors can be passed into an AF_NETGRAPH socket.
652 * Note, that file descriptors cannot be passed OUT.
653 * Only character device descriptors are accepted.
654 * Character devices are useful to connect a graph to a device,
655 * which after all is the purpose of this whole system.
658 ng_internalize(struct mbuf *control, struct thread *td)
660 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
666 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
667 cm->cmsg_len != control->m_len) {
672 /* Check there is only one FD. XXX what would more than one signify? */
673 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
679 /* Check that the FD given is legit. and change it to a pointer to a
682 if ((error = fget(td, fd, &fp)) != 0)
685 /* Depending on what kind of resource it is, act differently. For
686 * devices, we treat it as a file. For an AF_NETGRAPH socket,
687 * shortcut straight to the node. */
688 switch (fp->f_type) {
691 if (vn && (vn->v_type == VCHR)) {
692 /* for a VCHR, actually reference the FILE */
694 /* XXX then what :) */
695 /* how to pass on to other modules? */
713 * Connect the data socket to a named control socket node.
716 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
718 struct sockaddr_ng *sap;
724 /* If we are already connected, don't do it again. */
725 if (pcbp->sockdata != NULL)
729 * Find the target (victim) and check it doesn't already have
730 * a data socket. Also check it is a 'socket' type node.
731 * Use ng_package_data() and ng_address_path() to do this.
734 sap = (struct sockaddr_ng *) nam;
735 /* The item will hold the node reference. */
736 item = ng_package_data(NULL, NG_WAITOK);
738 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
739 return (error); /* item is freed on failure */
742 * Extract node from item and free item. Remember we now have
743 * a reference on the node. The item holds it for us.
744 * when we free the item we release the reference.
746 farnode = item->el_dest; /* shortcut */
747 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
748 NG_FREE_ITEM(item); /* drop the reference to the node */
751 priv = NG_NODE_PRIVATE(farnode);
752 if (priv->datasock != NULL) {
753 NG_FREE_ITEM(item); /* drop the reference to the node */
758 * Link the PCB and the private data struct. and note the extra
759 * reference. Drop the extra reference on the node.
761 mtx_lock(&priv->mtx);
762 priv->datasock = pcbp;
763 pcbp->sockdata = priv;
765 mtx_unlock(&priv->mtx);
766 NG_FREE_ITEM(item); /* drop the reference to the node */
771 * Binding a socket means giving the corresponding node a name
774 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
776 struct ngsock *const priv = pcbp->sockdata;
777 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
783 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
784 (sap->sg_data[0] == '\0') ||
785 (sap->sg_data[sap->sg_len - 3] != '\0')) {
789 return (ng_name_node(priv->node, sap->sg_data));
792 /***************************************************************
794 ***************************************************************/
797 * You can only create new nodes from the socket end of things.
800 ngs_constructor(node_p nodep)
806 * We allow any hook to be connected to the node.
807 * There is no per-hook private information though.
810 ngs_newhook(node_p node, hook_p hook, const char *name)
812 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
817 * If only one hook, allow read(2) and write(2) to work.
820 ngs_connect(hook_p hook)
822 node_p node = NG_HOOK_NODE(hook);
823 struct ngsock *priv = NG_NODE_PRIVATE(node);
825 if ((priv->datasock) && (priv->datasock->ng_socket)) {
826 if (NG_NODE_NUMHOOKS(node) == 1)
827 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
829 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
835 * Incoming messages get passed up to the control socket.
836 * Unless they are for us specifically (socket_type)
839 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
841 struct ngsock *const priv = NG_NODE_PRIVATE(node);
842 struct ngpcb *const pcbp = priv->ctlsock;
844 struct sockaddr_ng addr;
847 ng_ID_t retaddr = NGI_RETADDR(item);
851 NGI_GET_MSG(item, msg);
855 * Only allow mesgs to be passed if we have the control socket.
856 * Data sockets can only support the generic messages.
863 so = pcbp->ng_socket;
865 #ifdef TRACE_MESSAGES
866 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
868 msg->header.typecookie,
875 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
876 switch (msg->header.cmd) {
877 case NGM_SOCK_CMD_NOLINGER:
878 priv->flags |= NGS_FLAG_NOLINGER;
880 case NGM_SOCK_CMD_LINGER:
881 priv->flags &= ~NGS_FLAG_NOLINGER;
884 error = EINVAL; /* unknown command */
886 /* Free the message and return. */
891 /* Get the return address into a sockaddr. */
892 bzero(&addr, sizeof(addr));
893 addr.sg_len = sizeof(addr);
894 addr.sg_family = AF_NETGRAPH;
895 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
897 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
898 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
904 /* Copy the message itself into an mbuf chain. */
905 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
909 * Here we free the message. We need to do that
910 * regardless of whether we got mbufs.
919 /* Send it up to the socket. */
920 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
931 * Receive data on a hook
934 ngs_rcvdata(hook_p hook, item_p item)
936 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
937 struct ngpcb *const pcbp = priv->datasock;
939 struct sockaddr_ng *addr;
940 char *addrbuf[NG_HOOKSIZ + 4];
947 /* If there is no data socket, black-hole it. */
952 so = pcbp->ng_socket;
954 /* Get the return address into a sockaddr. */
955 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
956 addr = (struct sockaddr_ng *) addrbuf;
957 addr->sg_len = addrlen + 3;
958 addr->sg_family = AF_NETGRAPH;
959 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
960 addr->sg_data[addrlen] = '\0';
962 /* Try to tell the socket which hook it came in on. */
963 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
975 * For this type, removal of the last link destroys the node
976 * if the NOLINGER flag is set.
979 ngs_disconnect(hook_p hook)
981 node_p node = NG_HOOK_NODE(hook);
982 struct ngsock *const priv = NG_NODE_PRIVATE(node);
984 if ((priv->datasock) && (priv->datasock->ng_socket)) {
985 if (NG_NODE_NUMHOOKS(node) == 1)
986 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
988 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
991 if ((priv->flags & NGS_FLAG_NOLINGER) &&
992 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
993 ng_rmnode_self(node);
999 * Do local shutdown processing.
1000 * In this case, that involves making sure the socket
1001 * knows we should be shutting down.
1004 ngs_shutdown(node_p node)
1006 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1007 struct ngpcb *const dpcbp = priv->datasock;
1008 struct ngpcb *const pcbp = priv->ctlsock;
1011 soisdisconnected(dpcbp->ng_socket);
1014 soisdisconnected(pcbp->ng_socket);
1016 mtx_lock(&priv->mtx);
1018 NG_NODE_SET_PRIVATE(node, NULL);
1019 ng_socket_free_priv(priv);
1021 NG_NODE_UNREF(node);
1026 ng_socket_item_applied(void *context, int error)
1028 struct ngsock *const priv = (struct ngsock *)context;
1030 mtx_lock(&priv->mtx);
1031 priv->error = error;
1033 mtx_unlock(&priv->mtx);
1038 dummy_disconnect(struct socket *so)
1043 * Control and data socket type descriptors
1045 * XXXRW: Perhaps _close should do something?
1048 static struct pr_usrreqs ngc_usrreqs = {
1050 .pru_attach = ngc_attach,
1051 .pru_bind = ngc_bind,
1052 .pru_connect = ngc_connect,
1053 .pru_detach = ngc_detach,
1054 .pru_disconnect = dummy_disconnect,
1055 .pru_peeraddr = NULL,
1056 .pru_send = ngc_send,
1057 .pru_shutdown = NULL,
1058 .pru_sockaddr = ng_getsockaddr,
1062 static struct pr_usrreqs ngd_usrreqs = {
1064 .pru_attach = ngd_attach,
1066 .pru_connect = ngd_connect,
1067 .pru_detach = ngd_detach,
1068 .pru_disconnect = dummy_disconnect,
1069 .pru_peeraddr = NULL,
1070 .pru_send = ngd_send,
1071 .pru_shutdown = NULL,
1072 .pru_sockaddr = ng_getsockaddr,
1077 * Definitions of protocols supported in the NETGRAPH domain.
1080 extern struct domain ngdomain; /* stop compiler warnings */
1082 static struct protosw ngsw[] = {
1084 .pr_type = SOCK_DGRAM,
1085 .pr_domain = &ngdomain,
1086 .pr_protocol = NG_CONTROL,
1087 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1088 .pr_usrreqs = &ngc_usrreqs
1091 .pr_type = SOCK_DGRAM,
1092 .pr_domain = &ngdomain,
1093 .pr_protocol = NG_DATA,
1094 .pr_flags = PR_ATOMIC | PR_ADDR,
1095 .pr_usrreqs = &ngd_usrreqs
1099 struct domain ngdomain = {
1100 .dom_family = AF_NETGRAPH,
1101 .dom_name = "netgraph",
1102 .dom_protosw = ngsw,
1103 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1107 * Handle loading and unloading for this node type.
1108 * This is to handle auxiliary linkages (e.g protocol domain addition).
1111 ngs_mod_event(module_t mod, int event, void *data)
1117 /* Register protocol domain. */
1118 net_add_domain(&ngdomain);
1122 /* Unregister protocol domain XXX can't do this yet.. */
1123 if ((error = net_rm_domain(&ngdomain)) != 0)
1136 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1137 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1138 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1139 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1140 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");