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/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 <netgraph/ng_message.h>
71 #include <netgraph/netgraph.h>
72 #include <netgraph/ng_socketvar.h>
73 #include <netgraph/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 = malloc(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 = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
245 m_copydata(m, 0, len, (char *)msg);
247 if (msg->header.version != NG_VERSION) {
248 free(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;
263 if (ng_findtype(mkp->type) == NULL) {
264 char filename[NG_TYPESIZ + 3];
267 /* Not found, try to load it as a loadable module. */
268 snprintf(filename, sizeof(filename), "ng_%s",
270 error = kern_kldload(curthread, filename, &fileid);
272 free(msg, M_NETGRAPH_MSG);
276 /* See if type has been loaded successfully. */
277 if (ng_findtype(mkp->type) == NULL) {
278 free(msg, M_NETGRAPH_MSG);
279 (void)kern_kldunload(curthread, fileid,
280 LINKER_UNLOAD_NORMAL);
287 item = ng_package_msg(msg, M_WAITOK);
288 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
290 #ifdef TRACE_MESSAGES
291 printf("ng_address_path: errx=%d\n", error);
296 #ifdef TRACE_MESSAGES
297 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
298 item->el_dest->nd_ID,
299 msg->header.typecookie,
304 item->el_dest->nd_type->name);
308 * We do not want to return from syscall until the item
309 * is processed by destination node. We register callback
310 * on the item, which will update priv->error when item
312 * If ng_snd_item() has queued item, we sleep until
313 * callback wakes us up.
315 bzero(&apply, sizeof(apply));
316 apply.apply = ng_socket_item_applied;
317 apply.context = priv;
318 item->apply = &apply;
321 error = ng_snd_item(item, 0);
323 mtx_lock(&priv->mtx);
324 if (priv->error == -1)
325 msleep(priv, &priv->mtx, 0, "ngsock", 0);
326 mtx_unlock(&priv->mtx);
327 KASSERT(priv->error != -1,
328 ("ng_socket: priv->error wasn't updated"));
333 free(path, M_NETGRAPH_PATH);
342 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
344 struct ngpcb *const pcbp = sotongpcb(so);
348 return (ng_bind(nam, pcbp));
352 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
355 * At this time refuse to do this.. it used to
356 * do something but it was undocumented and not used.
358 printf("program tried to connect control socket to remote node\n");
362 /***************************************************************
364 ***************************************************************/
367 ngd_attach(struct socket *so, int proto, struct thread *td)
369 struct ngpcb *const pcbp = sotongpcb(so);
373 return (ng_attach_data(so));
377 ngd_detach(struct socket *so)
379 struct ngpcb *const pcbp = sotongpcb(so);
381 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
382 ng_detach_common(pcbp, NG_DATA);
386 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
387 struct mbuf *control, struct thread *td)
389 struct ngpcb *const pcbp = sotongpcb(so);
390 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
393 char hookname[NG_HOOKSIZ];
395 if ((pcbp == NULL) || (control != NULL)) {
399 if (pcbp->sockdata == NULL) {
405 len = 0; /* Make compiler happy. */
407 len = sap->sg_len - 2;
410 * If the user used any of these ways to not specify an address
411 * then handle specially.
413 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
414 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
415 error = EDESTADDRREQ;
419 * If exactly one hook exists, just use it.
420 * Special case to allow write(2) to work on an ng_socket.
422 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
424 if (len >= NG_HOOKSIZ) {
430 * chop off the sockaddr header, and make sure it's NUL
433 bcopy(sap->sg_data, hookname, len);
434 hookname[len] = '\0';
436 /* Find the correct hook from 'hookname' */
437 hook = ng_findhook(pcbp->sockdata->node, hookname);
439 error = EHOSTUNREACH;
445 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
456 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
458 struct ngpcb *const pcbp = sotongpcb(so);
462 return (ng_connect_data(nam, pcbp));
466 * Used for both data and control sockets
469 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
472 struct sockaddr_ng *sg;
476 /* Why isn't sg_data a `char[1]' ? :-( */
477 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
479 pcbp = sotongpcb(so);
480 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
481 /* XXXGL: can this still happen? */
484 mtx_lock(&pcbp->sockdata->mtx);
485 if (pcbp->sockdata->node != NULL) {
486 node_p node = pcbp->sockdata->node;
487 int namelen = 0; /* silence compiler! */
489 if (NG_NODE_HAS_NAME(node))
490 sg_len += namelen = strlen(NG_NODE_NAME(node));
492 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
494 if (NG_NODE_HAS_NAME(node))
495 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
498 sg->sg_family = AF_NETGRAPH;
499 *addr = (struct sockaddr *)sg;
500 mtx_unlock(&pcbp->sockdata->mtx);
502 mtx_unlock(&pcbp->sockdata->mtx);
510 * Attach a socket to it's protocol specific partner.
511 * For a control socket, actually create a netgraph node and attach
516 ng_attach_cntl(struct socket *so)
522 /* Allocate node private info */
523 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
525 /* Setup protocol control block */
526 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
527 free(priv, M_NETGRAPH_SOCK);
530 pcbp = sotongpcb(so);
532 /* Link the pcb the private data. */
533 priv->ctlsock = pcbp;
534 pcbp->sockdata = priv;
537 /* Initialize mutex. */
538 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
540 /* Make the generic node components */
541 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
542 free(priv, M_NETGRAPH_SOCK);
543 ng_detach_common(pcbp, NG_CONTROL);
547 /* Link the node and the private data. */
548 NG_NODE_SET_PRIVATE(priv->node, priv);
549 NG_NODE_REF(priv->node);
556 ng_attach_data(struct socket *so)
558 return (ng_attach_common(so, NG_DATA));
562 * Set up a socket protocol control block.
563 * This code is shared between control and data sockets.
566 ng_attach_common(struct socket *so, int type)
571 /* Standard socket setup stuff. */
572 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
576 /* Allocate the pcb. */
577 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
580 /* Link the pcb and the socket. */
581 so->so_pcb = (caddr_t)pcbp;
582 pcbp->ng_socket = so;
588 * Disassociate the socket from it's protocol specific
589 * partner. If it's attached to a node's private data structure,
590 * then unlink from that too. If we were the last socket attached to it,
591 * then shut down the entire node. Shared code for control and data sockets.
594 ng_detach_common(struct ngpcb *pcbp, int which)
596 struct ngsock *priv = pcbp->sockdata;
599 mtx_lock(&priv->mtx);
603 priv->ctlsock = NULL;
606 priv->datasock = NULL;
611 pcbp->sockdata = NULL;
613 ng_socket_free_priv(priv);
616 pcbp->ng_socket->so_pcb = NULL;
621 * Remove a reference from node private data.
624 ng_socket_free_priv(struct ngsock *priv)
626 mtx_assert(&priv->mtx, MA_OWNED);
630 if (priv->refs == 0) {
631 mtx_destroy(&priv->mtx);
632 free(priv, M_NETGRAPH_SOCK);
636 if ((priv->refs == 1) && (priv->node != NULL)) {
637 node_p node = priv->node;
640 mtx_unlock(&priv->mtx);
642 ng_rmnode_self(node);
644 mtx_unlock(&priv->mtx);
649 * File descriptors can be passed into an AF_NETGRAPH socket.
650 * Note, that file descriptors cannot be passed OUT.
651 * Only character device descriptors are accepted.
652 * Character devices are useful to connect a graph to a device,
653 * which after all is the purpose of this whole system.
656 ng_internalize(struct mbuf *control, struct thread *td)
658 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
664 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
665 cm->cmsg_len != control->m_len) {
670 /* Check there is only one FD. XXX what would more than one signify? */
671 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
677 /* Check that the FD given is legit. and change it to a pointer to a
680 if ((error = fget(td, fd, &fp)) != 0)
683 /* Depending on what kind of resource it is, act differently. For
684 * devices, we treat it as a file. For an AF_NETGRAPH socket,
685 * shortcut straight to the node. */
686 switch (fp->f_type) {
689 if (vn && (vn->v_type == VCHR)) {
690 /* for a VCHR, actually reference the FILE */
692 /* XXX then what :) */
693 /* how to pass on to other modules? */
711 * Connect the data socket to a named control socket node.
714 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
716 struct sockaddr_ng *sap;
722 /* If we are already connected, don't do it again. */
723 if (pcbp->sockdata != NULL)
727 * Find the target (victim) and check it doesn't already have
728 * a data socket. Also check it is a 'socket' type node.
729 * Use ng_package_data() and ng_address_path() to do this.
732 sap = (struct sockaddr_ng *) nam;
733 /* The item will hold the node reference. */
734 item = ng_package_data(NULL, NG_WAITOK);
736 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
737 return (error); /* item is freed on failure */
740 * Extract node from item and free item. Remember we now have
741 * a reference on the node. The item holds it for us.
742 * when we free the item we release the reference.
744 farnode = item->el_dest; /* shortcut */
745 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
746 NG_FREE_ITEM(item); /* drop the reference to the node */
749 priv = NG_NODE_PRIVATE(farnode);
750 if (priv->datasock != NULL) {
751 NG_FREE_ITEM(item); /* drop the reference to the node */
756 * Link the PCB and the private data struct. and note the extra
757 * reference. Drop the extra reference on the node.
759 mtx_lock(&priv->mtx);
760 priv->datasock = pcbp;
761 pcbp->sockdata = priv;
763 mtx_unlock(&priv->mtx);
764 NG_FREE_ITEM(item); /* drop the reference to the node */
769 * Binding a socket means giving the corresponding node a name
772 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
774 struct ngsock *const priv = pcbp->sockdata;
775 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
781 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
782 (sap->sg_data[0] == '\0') ||
783 (sap->sg_data[sap->sg_len - 3] != '\0')) {
787 return (ng_name_node(priv->node, sap->sg_data));
790 /***************************************************************
792 ***************************************************************/
795 * You can only create new nodes from the socket end of things.
798 ngs_constructor(node_p nodep)
804 * We allow any hook to be connected to the node.
805 * There is no per-hook private information though.
808 ngs_newhook(node_p node, hook_p hook, const char *name)
810 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
815 * If only one hook, allow read(2) and write(2) to work.
818 ngs_connect(hook_p hook)
820 node_p node = NG_HOOK_NODE(hook);
821 struct ngsock *priv = NG_NODE_PRIVATE(node);
823 if ((priv->datasock) && (priv->datasock->ng_socket)) {
824 if (NG_NODE_NUMHOOKS(node) == 1)
825 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
827 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
833 * Incoming messages get passed up to the control socket.
834 * Unless they are for us specifically (socket_type)
837 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
839 struct ngsock *const priv = NG_NODE_PRIVATE(node);
840 struct ngpcb *const pcbp = priv->ctlsock;
842 struct sockaddr_ng addr;
845 ng_ID_t retaddr = NGI_RETADDR(item);
849 NGI_GET_MSG(item, msg);
853 * Only allow mesgs to be passed if we have the control socket.
854 * Data sockets can only support the generic messages.
861 so = pcbp->ng_socket;
863 #ifdef TRACE_MESSAGES
864 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
866 msg->header.typecookie,
873 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
874 switch (msg->header.cmd) {
875 case NGM_SOCK_CMD_NOLINGER:
876 priv->flags |= NGS_FLAG_NOLINGER;
878 case NGM_SOCK_CMD_LINGER:
879 priv->flags &= ~NGS_FLAG_NOLINGER;
882 error = EINVAL; /* unknown command */
884 /* Free the message and return. */
889 /* Get the return address into a sockaddr. */
890 bzero(&addr, sizeof(addr));
891 addr.sg_len = sizeof(addr);
892 addr.sg_family = AF_NETGRAPH;
893 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
895 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
896 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
902 /* Copy the message itself into an mbuf chain. */
903 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
907 * Here we free the message. We need to do that
908 * regardless of whether we got mbufs.
917 /* Send it up to the socket. */
918 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
929 * Receive data on a hook
932 ngs_rcvdata(hook_p hook, item_p item)
934 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
935 struct ngpcb *const pcbp = priv->datasock;
937 struct sockaddr_ng *addr;
938 char *addrbuf[NG_HOOKSIZ + 4];
945 /* If there is no data socket, black-hole it. */
950 so = pcbp->ng_socket;
952 /* Get the return address into a sockaddr. */
953 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
954 addr = (struct sockaddr_ng *) addrbuf;
955 addr->sg_len = addrlen + 3;
956 addr->sg_family = AF_NETGRAPH;
957 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
958 addr->sg_data[addrlen] = '\0';
960 /* Try to tell the socket which hook it came in on. */
961 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
973 * For this type, removal of the last link destroys the node
974 * if the NOLINGER flag is set.
977 ngs_disconnect(hook_p hook)
979 node_p node = NG_HOOK_NODE(hook);
980 struct ngsock *const priv = NG_NODE_PRIVATE(node);
982 if ((priv->datasock) && (priv->datasock->ng_socket)) {
983 if (NG_NODE_NUMHOOKS(node) == 1)
984 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
986 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
989 if ((priv->flags & NGS_FLAG_NOLINGER) &&
990 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
991 ng_rmnode_self(node);
997 * Do local shutdown processing.
998 * In this case, that involves making sure the socket
999 * knows we should be shutting down.
1002 ngs_shutdown(node_p node)
1004 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1005 struct ngpcb *const dpcbp = priv->datasock;
1006 struct ngpcb *const pcbp = priv->ctlsock;
1009 soisdisconnected(dpcbp->ng_socket);
1012 soisdisconnected(pcbp->ng_socket);
1014 mtx_lock(&priv->mtx);
1016 NG_NODE_SET_PRIVATE(node, NULL);
1017 ng_socket_free_priv(priv);
1019 NG_NODE_UNREF(node);
1024 ng_socket_item_applied(void *context, int error)
1026 struct ngsock *const priv = (struct ngsock *)context;
1028 mtx_lock(&priv->mtx);
1029 priv->error = error;
1031 mtx_unlock(&priv->mtx);
1036 dummy_disconnect(struct socket *so)
1041 * Control and data socket type descriptors
1043 * XXXRW: Perhaps _close should do something?
1046 static struct pr_usrreqs ngc_usrreqs = {
1048 .pru_attach = ngc_attach,
1049 .pru_bind = ngc_bind,
1050 .pru_connect = ngc_connect,
1051 .pru_detach = ngc_detach,
1052 .pru_disconnect = dummy_disconnect,
1053 .pru_peeraddr = NULL,
1054 .pru_send = ngc_send,
1055 .pru_shutdown = NULL,
1056 .pru_sockaddr = ng_getsockaddr,
1060 static struct pr_usrreqs ngd_usrreqs = {
1062 .pru_attach = ngd_attach,
1064 .pru_connect = ngd_connect,
1065 .pru_detach = ngd_detach,
1066 .pru_disconnect = dummy_disconnect,
1067 .pru_peeraddr = NULL,
1068 .pru_send = ngd_send,
1069 .pru_shutdown = NULL,
1070 .pru_sockaddr = ng_getsockaddr,
1075 * Definitions of protocols supported in the NETGRAPH domain.
1078 extern struct domain ngdomain; /* stop compiler warnings */
1080 static struct protosw ngsw[] = {
1082 .pr_type = SOCK_DGRAM,
1083 .pr_domain = &ngdomain,
1084 .pr_protocol = NG_CONTROL,
1085 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1086 .pr_usrreqs = &ngc_usrreqs
1089 .pr_type = SOCK_DGRAM,
1090 .pr_domain = &ngdomain,
1091 .pr_protocol = NG_DATA,
1092 .pr_flags = PR_ATOMIC | PR_ADDR,
1093 .pr_usrreqs = &ngd_usrreqs
1097 struct domain ngdomain = {
1098 .dom_family = AF_NETGRAPH,
1099 .dom_name = "netgraph",
1100 .dom_protosw = ngsw,
1101 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
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 /* Register protocol domain. */
1116 net_add_domain(&ngdomain);
1120 /* Unregister protocol domain XXX can't do this yet.. */
1121 if ((error = net_rm_domain(&ngdomain)) != 0)
1134 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1135 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1136 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1137 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1138 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");