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>
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);
128 static int ng_internalize(struct mbuf *m, struct thread *p);
130 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
131 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
133 static int ngs_mod_event(module_t mod, int event, void *data);
134 static void ng_socket_item_applied(void *context, int error);
136 /* Netgraph type descriptor */
137 static struct ng_type typestruct = {
138 .version = NG_ABI_VERSION,
139 .name = NG_SOCKET_NODE_TYPE,
140 .mod_event = ngs_mod_event,
141 .constructor = ngs_constructor,
142 .rcvmsg = ngs_rcvmsg,
143 .shutdown = ngs_shutdown,
144 .newhook = ngs_newhook,
145 .connect = ngs_connect,
146 .rcvdata = ngs_rcvdata,
147 .disconnect = ngs_disconnect,
149 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
152 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
153 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
154 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
155 static u_long ngpdg_recvspace = 20 * 1024;
156 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
157 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
159 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
161 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
166 /***************************************************************
168 ***************************************************************/
171 ngc_attach(struct socket *so, int proto, struct thread *td)
173 struct ngpcb *const pcbp = sotongpcb(so);
176 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
181 return (ng_attach_cntl(so));
185 ngc_detach(struct socket *so)
187 struct ngpcb *const pcbp = sotongpcb(so);
189 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
190 ng_detach_common(pcbp, NG_CONTROL);
194 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
195 struct mbuf *control, struct thread *td)
197 struct ngpcb *const pcbp = sotongpcb(so);
198 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
199 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
205 struct ng_apply_info apply;
208 if (control && (error = ng_internalize(control, td))) {
209 if (pcbp->sockdata == NULL) {
221 /* Require destination as there may be >= 1 hooks on this node. */
223 error = EDESTADDRREQ;
228 * Allocate an expendable buffer for the path, chop off
229 * the sockaddr header, and make sure it's NUL terminated.
231 len = sap->sg_len - 2;
232 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
233 bcopy(sap->sg_data, path, len);
237 * Move the actual message out of mbufs into a linear buffer.
238 * Start by adding up the size of the data. (could use mh_len?)
240 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
244 * Move the data into a linear buffer as well.
245 * Messages are not delivered in mbufs.
247 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
248 m_copydata(m, 0, len, (char *)msg);
250 if (msg->header.version != NG_VERSION) {
251 free(msg, M_NETGRAPH_MSG);
258 * We look into the message and if it mkpeers a node of unknown type, we
259 * try to load it. We need to do this now, in syscall thread, because if
260 * message gets queued and applied later we will get panic.
262 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
263 msg->header.cmd == NGM_MKPEER) {
264 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
266 if (ng_findtype(mkp->type) == NULL) {
267 char filename[NG_TYPESIZ + 3];
270 /* Not found, try to load it as a loadable module. */
271 snprintf(filename, sizeof(filename), "ng_%s",
273 error = kern_kldload(curthread, filename, &fileid);
275 free(msg, M_NETGRAPH_MSG);
279 /* See if type has been loaded successfully. */
280 if (ng_findtype(mkp->type) == NULL) {
281 free(msg, M_NETGRAPH_MSG);
282 (void)kern_kldunload(curthread, fileid,
283 LINKER_UNLOAD_NORMAL);
290 item = ng_package_msg(msg, M_WAITOK);
291 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
293 #ifdef TRACE_MESSAGES
294 printf("ng_address_path: errx=%d\n", error);
299 #ifdef TRACE_MESSAGES
300 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
301 item->el_dest->nd_ID,
302 msg->header.typecookie,
307 item->el_dest->nd_type->name);
311 * We do not want to return from syscall until the item
312 * is processed by destination node. We register callback
313 * on the item, which will update priv->error when item
315 * If ng_snd_item() has queued item, we sleep until
316 * callback wakes us up.
318 bzero(&apply, sizeof(apply));
319 apply.apply = ng_socket_item_applied;
320 apply.context = priv;
321 item->apply = &apply;
324 error = ng_snd_item(item, 0);
326 mtx_lock(&priv->mtx);
327 if (priv->error == -1)
328 msleep(priv, &priv->mtx, 0, "ngsock", 0);
329 mtx_unlock(&priv->mtx);
330 KASSERT(priv->error != -1,
331 ("ng_socket: priv->error wasn't updated"));
336 free(path, M_NETGRAPH_PATH);
345 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
347 struct ngpcb *const pcbp = sotongpcb(so);
351 return (ng_bind(nam, pcbp));
355 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
358 * At this time refuse to do this.. it used to
359 * do something but it was undocumented and not used.
361 printf("program tried to connect control socket to remote node\n");
365 /***************************************************************
367 ***************************************************************/
370 ngd_attach(struct socket *so, int proto, struct thread *td)
372 struct ngpcb *const pcbp = sotongpcb(so);
376 return (ng_attach_data(so));
380 ngd_detach(struct socket *so)
382 struct ngpcb *const pcbp = sotongpcb(so);
384 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
385 ng_detach_common(pcbp, NG_DATA);
389 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
390 struct mbuf *control, struct thread *td)
392 struct ngpcb *const pcbp = sotongpcb(so);
393 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
396 char hookname[NG_HOOKSIZ];
398 if ((pcbp == NULL) || (control != NULL)) {
402 if (pcbp->sockdata == NULL) {
408 len = 0; /* Make compiler happy. */
410 len = sap->sg_len - 2;
413 * If the user used any of these ways to not specify an address
414 * then handle specially.
416 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
417 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
418 error = EDESTADDRREQ;
422 * If exactly one hook exists, just use it.
423 * Special case to allow write(2) to work on an ng_socket.
425 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
427 if (len >= NG_HOOKSIZ) {
433 * chop off the sockaddr header, and make sure it's NUL
436 bcopy(sap->sg_data, hookname, len);
437 hookname[len] = '\0';
439 /* Find the correct hook from 'hookname' */
440 hook = ng_findhook(pcbp->sockdata->node, hookname);
442 error = EHOSTUNREACH;
448 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
459 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
461 struct ngpcb *const pcbp = sotongpcb(so);
465 return (ng_connect_data(nam, pcbp));
469 * Used for both data and control sockets
472 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
475 struct sockaddr_ng *sg;
479 /* Why isn't sg_data a `char[1]' ? :-( */
480 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
482 pcbp = sotongpcb(so);
483 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
484 /* XXXGL: can this still happen? */
487 mtx_lock(&pcbp->sockdata->mtx);
488 if (pcbp->sockdata->node != NULL) {
489 node_p node = pcbp->sockdata->node;
490 int namelen = 0; /* silence compiler! */
492 if (NG_NODE_HAS_NAME(node))
493 sg_len += namelen = strlen(NG_NODE_NAME(node));
495 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
497 if (NG_NODE_HAS_NAME(node))
498 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
501 sg->sg_family = AF_NETGRAPH;
502 *addr = (struct sockaddr *)sg;
503 mtx_unlock(&pcbp->sockdata->mtx);
505 mtx_unlock(&pcbp->sockdata->mtx);
513 * Attach a socket to it's protocol specific partner.
514 * For a control socket, actually create a netgraph node and attach
519 ng_attach_cntl(struct socket *so)
525 /* Allocate node private info */
526 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
528 /* Setup protocol control block */
529 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
530 free(priv, M_NETGRAPH_SOCK);
533 pcbp = sotongpcb(so);
535 /* Link the pcb the private data. */
536 priv->ctlsock = pcbp;
537 pcbp->sockdata = priv;
540 /* Initialize mutex. */
541 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
543 /* Make the generic node components */
544 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
545 free(priv, M_NETGRAPH_SOCK);
546 ng_detach_common(pcbp, NG_CONTROL);
550 /* Link the node and the private data. */
551 NG_NODE_SET_PRIVATE(priv->node, priv);
552 NG_NODE_REF(priv->node);
559 ng_attach_data(struct socket *so)
561 return (ng_attach_common(so, NG_DATA));
565 * Set up a socket protocol control block.
566 * This code is shared between control and data sockets.
569 ng_attach_common(struct socket *so, int type)
574 /* Standard socket setup stuff. */
575 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
579 /* Allocate the pcb. */
580 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
583 /* Link the pcb and the socket. */
584 so->so_pcb = (caddr_t)pcbp;
585 pcbp->ng_socket = so;
591 * Disassociate the socket from it's protocol specific
592 * partner. If it's attached to a node's private data structure,
593 * then unlink from that too. If we were the last socket attached to it,
594 * then shut down the entire node. Shared code for control and data sockets.
597 ng_detach_common(struct ngpcb *pcbp, int which)
599 struct ngsock *priv = pcbp->sockdata;
602 mtx_lock(&priv->mtx);
606 priv->ctlsock = NULL;
609 priv->datasock = NULL;
614 pcbp->sockdata = NULL;
616 ng_socket_free_priv(priv);
619 pcbp->ng_socket->so_pcb = NULL;
624 * Remove a reference from node private data.
627 ng_socket_free_priv(struct ngsock *priv)
629 mtx_assert(&priv->mtx, MA_OWNED);
633 if (priv->refs == 0) {
634 mtx_destroy(&priv->mtx);
635 free(priv, M_NETGRAPH_SOCK);
639 if ((priv->refs == 1) && (priv->node != NULL)) {
640 node_p node = priv->node;
643 mtx_unlock(&priv->mtx);
645 ng_rmnode_self(node);
647 mtx_unlock(&priv->mtx);
652 * File descriptors can be passed into an AF_NETGRAPH socket.
653 * Note, that file descriptors cannot be passed OUT.
654 * Only character device descriptors are accepted.
655 * Character devices are useful to connect a graph to a device,
656 * which after all is the purpose of this whole system.
659 ng_internalize(struct mbuf *control, struct thread *td)
661 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
667 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
668 cm->cmsg_len != control->m_len) {
673 /* Check there is only one FD. XXX what would more than one signify? */
674 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
680 /* Check that the FD given is legit. and change it to a pointer to a
683 if ((error = fget(td, fd, &fp)) != 0)
686 /* Depending on what kind of resource it is, act differently. For
687 * devices, we treat it as a file. For an AF_NETGRAPH socket,
688 * shortcut straight to the node. */
689 switch (fp->f_type) {
692 if (vn && (vn->v_type == VCHR)) {
693 /* for a VCHR, actually reference the FILE */
695 /* XXX then what :) */
696 /* how to pass on to other modules? */
714 * Connect the data socket to a named control socket node.
717 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
719 struct sockaddr_ng *sap;
725 /* If we are already connected, don't do it again. */
726 if (pcbp->sockdata != NULL)
730 * Find the target (victim) and check it doesn't already have
731 * a data socket. Also check it is a 'socket' type node.
732 * Use ng_package_data() and ng_address_path() to do this.
735 sap = (struct sockaddr_ng *) nam;
736 /* The item will hold the node reference. */
737 item = ng_package_data(NULL, NG_WAITOK);
739 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
740 return (error); /* item is freed on failure */
743 * Extract node from item and free item. Remember we now have
744 * a reference on the node. The item holds it for us.
745 * when we free the item we release the reference.
747 farnode = item->el_dest; /* shortcut */
748 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
749 NG_FREE_ITEM(item); /* drop the reference to the node */
752 priv = NG_NODE_PRIVATE(farnode);
753 if (priv->datasock != NULL) {
754 NG_FREE_ITEM(item); /* drop the reference to the node */
759 * Link the PCB and the private data struct. and note the extra
760 * reference. Drop the extra reference on the node.
762 mtx_lock(&priv->mtx);
763 priv->datasock = pcbp;
764 pcbp->sockdata = priv;
766 mtx_unlock(&priv->mtx);
767 NG_FREE_ITEM(item); /* drop the reference to the node */
772 * Binding a socket means giving the corresponding node a name
775 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
777 struct ngsock *const priv = pcbp->sockdata;
778 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
784 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
785 (sap->sg_data[0] == '\0') ||
786 (sap->sg_data[sap->sg_len - 3] != '\0')) {
790 return (ng_name_node(priv->node, sap->sg_data));
793 /***************************************************************
795 ***************************************************************/
798 * You can only create new nodes from the socket end of things.
801 ngs_constructor(node_p nodep)
807 * We allow any hook to be connected to the node.
808 * There is no per-hook private information though.
811 ngs_newhook(node_p node, hook_p hook, const char *name)
813 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
818 * If only one hook, allow read(2) and write(2) to work.
821 ngs_connect(hook_p hook)
823 node_p node = NG_HOOK_NODE(hook);
824 struct ngsock *priv = NG_NODE_PRIVATE(node);
826 if ((priv->datasock) && (priv->datasock->ng_socket)) {
827 if (NG_NODE_NUMHOOKS(node) == 1)
828 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
830 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
836 * Incoming messages get passed up to the control socket.
837 * Unless they are for us specifically (socket_type)
840 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
842 struct ngsock *const priv = NG_NODE_PRIVATE(node);
843 struct ngpcb *const pcbp = priv->ctlsock;
845 struct sockaddr_ng addr;
848 ng_ID_t retaddr = NGI_RETADDR(item);
852 NGI_GET_MSG(item, msg);
856 * Only allow mesgs to be passed if we have the control socket.
857 * Data sockets can only support the generic messages.
864 so = pcbp->ng_socket;
866 #ifdef TRACE_MESSAGES
867 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
869 msg->header.typecookie,
876 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
877 switch (msg->header.cmd) {
878 case NGM_SOCK_CMD_NOLINGER:
879 priv->flags |= NGS_FLAG_NOLINGER;
881 case NGM_SOCK_CMD_LINGER:
882 priv->flags &= ~NGS_FLAG_NOLINGER;
885 error = EINVAL; /* unknown command */
887 /* Free the message and return. */
892 /* Get the return address into a sockaddr. */
893 bzero(&addr, sizeof(addr));
894 addr.sg_len = sizeof(addr);
895 addr.sg_family = AF_NETGRAPH;
896 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
898 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
899 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
905 /* Copy the message itself into an mbuf chain. */
906 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
910 * Here we free the message. We need to do that
911 * regardless of whether we got mbufs.
920 /* Send it up to the socket. */
921 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
932 * Receive data on a hook
935 ngs_rcvdata(hook_p hook, item_p item)
937 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
938 struct ngpcb *const pcbp = priv->datasock;
940 struct sockaddr_ng *addr;
941 char *addrbuf[NG_HOOKSIZ + 4];
948 /* If there is no data socket, black-hole it. */
953 so = pcbp->ng_socket;
955 /* Get the return address into a sockaddr. */
956 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
957 addr = (struct sockaddr_ng *) addrbuf;
958 addr->sg_len = addrlen + 3;
959 addr->sg_family = AF_NETGRAPH;
960 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
961 addr->sg_data[addrlen] = '\0';
963 /* Try to tell the socket which hook it came in on. */
964 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
976 * For this type, removal of the last link destroys the node
977 * if the NOLINGER flag is set.
980 ngs_disconnect(hook_p hook)
982 node_p node = NG_HOOK_NODE(hook);
983 struct ngsock *const priv = NG_NODE_PRIVATE(node);
985 if ((priv->datasock) && (priv->datasock->ng_socket)) {
986 if (NG_NODE_NUMHOOKS(node) == 1)
987 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
989 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
992 if ((priv->flags & NGS_FLAG_NOLINGER) &&
993 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
994 ng_rmnode_self(node);
1000 * Do local shutdown processing.
1001 * In this case, that involves making sure the socket
1002 * knows we should be shutting down.
1005 ngs_shutdown(node_p node)
1007 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1008 struct ngpcb *const dpcbp = priv->datasock;
1009 struct ngpcb *const pcbp = priv->ctlsock;
1012 soisdisconnected(dpcbp->ng_socket);
1015 soisdisconnected(pcbp->ng_socket);
1017 mtx_lock(&priv->mtx);
1019 NG_NODE_SET_PRIVATE(node, NULL);
1020 ng_socket_free_priv(priv);
1022 NG_NODE_UNREF(node);
1027 ng_socket_item_applied(void *context, int error)
1029 struct ngsock *const priv = (struct ngsock *)context;
1031 mtx_lock(&priv->mtx);
1032 priv->error = error;
1034 mtx_unlock(&priv->mtx);
1039 dummy_disconnect(struct socket *so)
1044 * Control and data socket type descriptors
1046 * XXXRW: Perhaps _close should do something?
1049 static struct pr_usrreqs ngc_usrreqs = {
1051 .pru_attach = ngc_attach,
1052 .pru_bind = ngc_bind,
1053 .pru_connect = ngc_connect,
1054 .pru_detach = ngc_detach,
1055 .pru_disconnect = dummy_disconnect,
1056 .pru_peeraddr = NULL,
1057 .pru_send = ngc_send,
1058 .pru_shutdown = NULL,
1059 .pru_sockaddr = ng_getsockaddr,
1063 static struct pr_usrreqs ngd_usrreqs = {
1065 .pru_attach = ngd_attach,
1067 .pru_connect = ngd_connect,
1068 .pru_detach = ngd_detach,
1069 .pru_disconnect = dummy_disconnect,
1070 .pru_peeraddr = NULL,
1071 .pru_send = ngd_send,
1072 .pru_shutdown = NULL,
1073 .pru_sockaddr = ng_getsockaddr,
1078 * Definitions of protocols supported in the NETGRAPH domain.
1081 extern struct domain ngdomain; /* stop compiler warnings */
1083 static struct protosw ngsw[] = {
1085 .pr_type = SOCK_DGRAM,
1086 .pr_domain = &ngdomain,
1087 .pr_protocol = NG_CONTROL,
1088 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1089 .pr_usrreqs = &ngc_usrreqs
1092 .pr_type = SOCK_DGRAM,
1093 .pr_domain = &ngdomain,
1094 .pr_protocol = NG_DATA,
1095 .pr_flags = PR_ATOMIC | PR_ADDR,
1096 .pr_usrreqs = &ngd_usrreqs
1100 struct domain ngdomain = {
1101 .dom_family = AF_NETGRAPH,
1102 .dom_name = "netgraph",
1103 .dom_protosw = ngsw,
1104 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1108 * Handle loading and unloading for this node type.
1109 * This is to handle auxiliary linkages (e.g protocol domain addition).
1112 ngs_mod_event(module_t mod, int event, void *data)
1121 /* Unregister protocol domain XXX can't do this yet.. */
1132 VNET_DOMAIN_SET(ng);
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, "");