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/syscallsubr.h>
69 #include <sys/sysctl.h>
70 #include <sys/systm.h>
72 #include <sys/vnode.h>
74 #include <netgraph/ng_message.h>
75 #include <netgraph/netgraph.h>
76 #include <netgraph/ng_socketvar.h>
77 #include <netgraph/ng_socket.h>
79 #ifdef NG_SEPARATE_MALLOC
80 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
81 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
83 #define M_NETGRAPH_PATH M_NETGRAPH
84 #define M_NETGRAPH_SOCK M_NETGRAPH
88 * It's Ascii-art time!
89 * +-------------+ +-------------+
90 * |socket (ctl)| |socket (data)|
91 * +-------------+ +-------------+
95 * +-----------+ +-----------+
96 * |pcb (ctl)| |pcb (data)|
97 * +-----------+ +-----------+
101 * +--------------------------+
102 * | Socket type private |
104 * +--------------------------+
113 /* Netgraph node methods */
114 static ng_constructor_t ngs_constructor;
115 static ng_rcvmsg_t ngs_rcvmsg;
116 static ng_shutdown_t ngs_shutdown;
117 static ng_newhook_t ngs_newhook;
118 static ng_connect_t ngs_connect;
119 static ng_rcvdata_t ngs_rcvdata;
120 static ng_disconnect_t ngs_disconnect;
122 /* Internal methods */
123 static int ng_attach_data(struct socket *so);
124 static int ng_attach_cntl(struct socket *so);
125 static int ng_attach_common(struct socket *so, int type);
126 static void ng_detach_common(struct ngpcb *pcbp, int type);
127 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 int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
135 struct sockaddr_ng *addr);
136 static void ng_socket_item_applied(void *context, int error);
138 /* Netgraph type descriptor */
139 static struct ng_type typestruct = {
140 .version = NG_ABI_VERSION,
141 .name = NG_SOCKET_NODE_TYPE,
142 .mod_event = ngs_mod_event,
143 .constructor = ngs_constructor,
144 .rcvmsg = ngs_rcvmsg,
145 .shutdown = ngs_shutdown,
146 .newhook = ngs_newhook,
147 .connect = ngs_connect,
148 .rcvdata = ngs_rcvdata,
149 .disconnect = ngs_disconnect,
151 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
154 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
155 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
156 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
157 static u_long ngpdg_recvspace = 20 * 1024;
158 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
159 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
161 /* List of all sockets */
162 static LIST_HEAD(, ngpcb) ngsocklist;
164 static struct mtx ngsocketlist_mtx;
166 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
168 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
173 /***************************************************************
175 ***************************************************************/
178 ngc_attach(struct socket *so, int proto, struct thread *td)
180 struct ngpcb *const pcbp = sotongpcb(so);
186 return (ng_attach_cntl(so));
190 ngc_detach(struct socket *so)
192 struct ngpcb *const pcbp = sotongpcb(so);
194 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
195 ng_detach_common(pcbp, NG_CONTROL);
199 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
200 struct mbuf *control, struct thread *td)
202 struct ngpcb *const pcbp = sotongpcb(so);
203 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
204 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
212 if (control && (error = ng_internalize(control, td))) {
213 if (pcbp->sockdata == NULL) {
225 /* Require destination as there may be >= 1 hooks on this node */
227 error = EDESTADDRREQ;
231 /* Allocate an expendable buffer for the path, chop off
232 * the sockaddr header, and make sure it's NUL terminated */
233 len = sap->sg_len - 2;
234 MALLOC(path, char *, len + 1, M_NETGRAPH_PATH, M_WAITOK);
239 bcopy(sap->sg_data, path, len);
242 /* Move the actual message out of mbufs into a linear buffer.
243 * Start by adding up the size of the data. (could use mh_len?) */
244 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
247 /* Move the data into a linear buffer as well. Messages are not
248 * delivered in mbufs. */
249 MALLOC(msg, struct ng_mesg *, len + 1, M_NETGRAPH_MSG, M_WAITOK);
254 m_copydata(m, 0, len, (char *)msg);
256 if (msg->header.version != NG_VERSION) {
257 FREE(msg, M_NETGRAPH_MSG);
264 * We look into the message and if it mkpeers a node of unknown type, we
265 * try to load it. We need to do this now, in syscall thread, because if
266 * message gets queued and applied later we will get panic.
268 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
269 msg->header.cmd == NGM_MKPEER) {
270 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
271 struct ng_type *type;
273 if ((type = ng_findtype(mkp->type)) == NULL) {
274 char filename[NG_TYPESIZ + 3];
277 /* Not found, try to load it as a loadable module. */
278 snprintf(filename, sizeof(filename), "ng_%s",
280 error = kern_kldload(curthread, filename, &fileid);
282 FREE(msg, M_NETGRAPH_MSG);
286 /* See if type has been loaded successfully. */
287 if ((type = ng_findtype(mkp->type)) == NULL) {
288 FREE(msg, M_NETGRAPH_MSG);
289 (void)kern_kldunload(curthread, fileid,
290 LINKER_UNLOAD_NORMAL);
297 if ((item = ng_package_msg(msg, M_WAITOK)) == NULL) {
299 #ifdef TRACE_MESSAGES
300 printf("ng_package_msg: err=%d\n", error);
304 if ((error = ng_address_path((pcbp->sockdata->node), item,
306 #ifdef TRACE_MESSAGES
307 printf("ng_address_path: errx=%d\n", error);
312 #ifdef TRACE_MESSAGES
313 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
314 item->el_dest->nd_ID,
315 msg->header.typecookie,
320 item->el_dest->nd_type->name);
324 * We do not want to return from syscall until the item
325 * is processed by destination node. We register callback
326 * on the item, which will update priv->error when item
328 * If ng_snd_item() has queued item, we sleep until
329 * callback wakes us up.
331 item->apply = ng_socket_item_applied;
332 item->context = priv;
335 error = ng_snd_item(item, NG_PROGRESS);
337 if (error == EINPROGRESS) {
338 mtx_lock(&priv->mtx);
339 if (priv->error == -1)
340 msleep(priv, &priv->mtx, 0, "ngsock", 0);
341 mtx_unlock(&priv->mtx);
342 KASSERT(priv->error != -1,
343 ("ng_socket: priv->error wasn't updated"));
349 FREE(path, M_NETGRAPH_PATH);
358 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
360 struct ngpcb *const pcbp = sotongpcb(so);
364 return (ng_bind(nam, pcbp));
368 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
370 printf(" program tried to connect control socket to remote node\n ");
372 * At this time refuse to do this.. it used to
373 * do something but it was undocumented and not used.
378 /***************************************************************
380 ***************************************************************/
383 ngd_attach(struct socket *so, int proto, struct thread *td)
385 struct ngpcb *const pcbp = sotongpcb(so);
389 return (ng_attach_data(so));
393 ngd_detach(struct socket *so)
395 struct ngpcb *const pcbp = sotongpcb(so);
397 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
398 ng_detach_common(pcbp, NG_DATA);
402 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
403 struct mbuf *control, struct thread *td)
405 struct ngpcb *const pcbp = sotongpcb(so);
406 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
409 char hookname[NG_HOOKSIZ];
411 if ((pcbp == NULL) || (control != NULL)) {
415 if (pcbp->sockdata == NULL) {
421 len = 0; /* Make compiler happy. */
423 len = sap->sg_len - 2;
426 * If the user used any of these ways to not specify an address
427 * then handle specially.
429 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
430 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
431 error = EDESTADDRREQ;
435 * if exactly one hook exists, just use it.
436 * Special case to allow write(2) to work on an ng_socket.
438 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
440 if (len >= NG_HOOKSIZ) {
446 * chop off the sockaddr header, and make sure it's NUL
449 bcopy(sap->sg_data, hookname, len);
450 hookname[len] = '\0';
452 /* Find the correct hook from 'hookname' */
453 LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) {
454 if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) {
459 error = EHOSTUNREACH;
463 /* Send data (OK if hook is NULL) */
464 NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */
475 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
477 struct ngpcb *const pcbp = sotongpcb(so);
481 return (ng_connect_data(nam, pcbp));
485 * Used for both data and control sockets
488 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
491 struct sockaddr_ng *sg;
495 /* Why isn't sg_data a `char[1]' ? :-( */
496 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
498 pcbp = sotongpcb(so);
499 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
500 /* XXXGL: can this still happen? */
503 mtx_lock(&pcbp->sockdata->mtx);
504 if (pcbp->sockdata->node != NULL) {
505 node_p node = pcbp->sockdata->node;
506 int namelen = 0; /* silence compiler! */
508 if (NG_NODE_HAS_NAME(node))
509 sg_len += namelen = strlen(NG_NODE_NAME(node));
511 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
513 if (NG_NODE_HAS_NAME(node))
514 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
517 sg->sg_family = AF_NETGRAPH;
518 *addr = (struct sockaddr *)sg;
519 mtx_unlock(&pcbp->sockdata->mtx);
521 mtx_unlock(&pcbp->sockdata->mtx);
529 * Attach a socket to it's protocol specific partner.
530 * For a control socket, actually create a netgraph node and attach
535 ng_attach_cntl(struct socket *so)
541 /* Allocate node private info */
542 MALLOC(priv, struct ngsock *,
543 sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
547 /* Setup protocol control block */
548 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
549 FREE(priv, M_NETGRAPH_SOCK);
552 pcbp = sotongpcb(so);
554 /* Link the pcb the private data. */
555 priv->ctlsock = pcbp;
556 pcbp->sockdata = priv;
559 /* Initialize mutex. */
560 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
562 /* Make the generic node components */
563 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
564 FREE(priv, M_NETGRAPH_SOCK);
565 ng_detach_common(pcbp, NG_CONTROL);
569 /* Link the node and the private data. */
570 NG_NODE_SET_PRIVATE(priv->node, priv);
571 NG_NODE_REF(priv->node);
578 ng_attach_data(struct socket *so)
580 return(ng_attach_common(so, NG_DATA));
584 * Set up a socket protocol control block.
585 * This code is shared between control and data sockets.
588 ng_attach_common(struct socket *so, int type)
593 /* Standard socket setup stuff */
594 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
598 /* Allocate the pcb */
599 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
604 /* Link the pcb and the socket */
605 so->so_pcb = (caddr_t) pcbp;
606 pcbp->ng_socket = so;
608 /* Add the socket to linked list */
609 mtx_lock(&ngsocketlist_mtx);
610 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
611 mtx_unlock(&ngsocketlist_mtx);
616 * Disassociate the socket from it's protocol specific
617 * partner. If it's attached to a node's private data structure,
618 * then unlink from that too. If we were the last socket attached to it,
619 * then shut down the entire node. Shared code for control and data sockets.
622 ng_detach_common(struct ngpcb *pcbp, int which)
624 struct ngsock *priv = pcbp->sockdata;
627 mtx_lock(&priv->mtx);
631 priv->ctlsock = NULL;
634 priv->datasock = NULL;
639 pcbp->sockdata = NULL;
641 ng_socket_free_priv(priv);
644 pcbp->ng_socket->so_pcb = NULL;
645 mtx_lock(&ngsocketlist_mtx);
646 LIST_REMOVE(pcbp, socks);
647 mtx_unlock(&ngsocketlist_mtx);
652 * Remove a reference from node private data.
655 ng_socket_free_priv(struct ngsock *priv)
657 mtx_assert(&priv->mtx, MA_OWNED);
661 if (priv->refs == 0) {
662 mtx_destroy(&priv->mtx);
663 FREE(priv, M_NETGRAPH_SOCK);
667 if ((priv->refs == 1) && (priv->node != NULL)) {
668 node_p node = priv->node;
671 mtx_unlock(&priv->mtx);
673 ng_rmnode_self(node);
675 mtx_unlock(&priv->mtx);
680 * File descriptors can be passed into an AF_NETGRAPH socket.
681 * Note, that file descriptors cannot be passed OUT.
682 * Only character device descriptors are accepted.
683 * Character devices are useful to connect a graph to a device,
684 * which after all is the purpose of this whole system.
687 ng_internalize(struct mbuf *control, struct thread *td)
689 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
695 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
696 cm->cmsg_len != control->m_len) {
701 /* Check there is only one FD. XXX what would more than one signify? */
702 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
708 /* Check that the FD given is legit. and change it to a pointer to a
711 if ((error = fget(td, fd, &fp)) != 0)
714 /* Depending on what kind of resource it is, act differently. For
715 * devices, we treat it as a file. For an AF_NETGRAPH socket,
716 * shortcut straight to the node. */
717 switch (fp->f_type) {
720 if (vn && (vn->v_type == VCHR)) {
721 /* for a VCHR, actually reference the FILE */
723 /* XXX then what :) */
724 /* how to pass on to other modules? */
742 * Connect the data socket to a named control socket node.
745 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
747 struct sockaddr_ng *sap;
753 /* If we are already connected, don't do it again */
754 if (pcbp->sockdata != NULL)
757 /* Find the target (victim) and check it doesn't already have a data
758 * socket. Also check it is a 'socket' type node.
759 * Use ng_package_data() and address_path() to do this.
762 sap = (struct sockaddr_ng *) nam;
763 /* The item will hold the node reference */
764 item = ng_package_data(NULL, NG_WAITOK);
768 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
769 return (error); /* item is freed on failure */
772 * Extract node from item and free item. Remember we now have
773 * a reference on the node. The item holds it for us.
774 * when we free the item we release the reference.
776 farnode = item->el_dest; /* shortcut */
777 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
778 NG_FREE_ITEM(item); /* drop the reference to the node */
781 priv = NG_NODE_PRIVATE(farnode);
782 if (priv->datasock != NULL) {
783 NG_FREE_ITEM(item); /* drop the reference to the node */
788 * Link the PCB and the private data struct. and note the extra
789 * reference. Drop the extra reference on the node.
791 mtx_lock(&priv->mtx);
792 priv->datasock = pcbp;
793 pcbp->sockdata = priv;
795 mtx_unlock(&priv->mtx);
796 NG_FREE_ITEM(item); /* drop the reference to the node */
801 * Binding a socket means giving the corresponding node a name
804 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
806 struct ngsock *const priv = pcbp->sockdata;
807 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
813 if ((sap->sg_len < 4)
814 || (sap->sg_len > (NG_NODESIZ + 2))
815 || (sap->sg_data[0] == '\0')
816 || (sap->sg_data[sap->sg_len - 3] != '\0')) {
820 return (ng_name_node(priv->node, sap->sg_data));
824 * Take a message and pass it up to the control socket associated
828 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
830 struct socket *const so = pcbp->ng_socket;
835 /* Copy the message itself into an mbuf chain */
836 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
837 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
839 /* Here we free the message, as we are the end of the line.
840 * We need to do that regardless of whether we got mbufs. */
848 /* Send it up to the socket */
849 if (sbappendaddr(&so->so_rcv,
850 (struct sockaddr *) addr, mdata, NULL) == 0) {
853 error = so->so_error = ENOBUFS;
859 /***************************************************************
861 ***************************************************************/
864 * You can only create new nodes from the socket end of things.
867 ngs_constructor(node_p nodep)
873 * We allow any hook to be connected to the node.
874 * There is no per-hook private information though.
877 ngs_newhook(node_p node, hook_p hook, const char *name)
879 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
884 * if only one hook, allow read(2) and write(2) to work.
887 ngs_connect(hook_p hook)
889 node_p node = NG_HOOK_NODE(hook);
890 struct ngsock *priv = NG_NODE_PRIVATE(node);
893 && (priv->datasock->ng_socket)) {
894 if (NG_NODE_NUMHOOKS(node) == 1) {
895 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
897 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
904 * Incoming messages get passed up to the control socket.
905 * Unless they are for us specifically (socket_type)
908 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
910 struct ngsock *const priv = NG_NODE_PRIVATE(node);
911 struct ngpcb *const pcbp = priv->ctlsock;
912 struct sockaddr_ng *addr;
916 ng_ID_t retaddr = NGI_RETADDR(item);
919 NGI_GET_MSG(item, msg);
920 NG_FREE_ITEM(item); /* we have all we need */
922 /* Only allow mesgs to be passed if we have the control socket.
923 * Data sockets can only support the generic messages. */
929 #ifdef TRACE_MESSAGES
930 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
932 msg->header.typecookie,
939 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
940 switch (msg->header.cmd) {
941 case NGM_SOCK_CMD_NOLINGER:
942 priv->flags |= NGS_FLAG_NOLINGER;
944 case NGM_SOCK_CMD_LINGER:
945 priv->flags &= ~NGS_FLAG_NOLINGER;
948 error = EINVAL; /* unknown command */
950 /* Free the message and return */
955 /* Get the return address into a sockaddr */
956 sprintf(retabuf,"[%x]:", retaddr);
957 addrlen = strlen(retabuf);
958 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT);
963 addr->sg_len = addrlen + 3;
964 addr->sg_family = AF_NETGRAPH;
965 bcopy(retabuf, addr->sg_data, addrlen);
966 addr->sg_data[addrlen] = '\0';
969 error = ship_msg(pcbp, msg, addr);
970 FREE(addr, M_NETGRAPH_PATH);
975 * Receive data on a hook
978 ngs_rcvdata(hook_p hook, item_p item)
980 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
981 struct ngpcb *const pcbp = priv->datasock;
983 struct sockaddr_ng *addr;
984 char *addrbuf[NG_HOOKSIZ + 4];
990 /* If there is no data socket, black-hole it */
995 so = pcbp->ng_socket;
997 /* Get the return address into a sockaddr. */
998 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
999 addr = (struct sockaddr_ng *) addrbuf;
1000 addr->sg_len = addrlen + 3;
1001 addr->sg_family = AF_NETGRAPH;
1002 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1003 addr->sg_data[addrlen] = '\0';
1005 /* Try to tell the socket which hook it came in on */
1006 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
1016 * Hook disconnection
1018 * For this type, removal of the last link destroys the node
1019 * if the NOLINGER flag is set.
1022 ngs_disconnect(hook_p hook)
1024 node_p node = NG_HOOK_NODE(hook);
1025 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1027 if ((priv->datasock)
1028 && (priv->datasock->ng_socket)) {
1029 if (NG_NODE_NUMHOOKS(node) == 1) {
1030 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1032 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1036 if ((priv->flags & NGS_FLAG_NOLINGER )
1037 && (NG_NODE_NUMHOOKS(node) == 0)
1038 && (NG_NODE_IS_VALID(node))) {
1039 ng_rmnode_self(node);
1045 * Do local shutdown processing.
1046 * In this case, that involves making sure the socket
1047 * knows we should be shutting down.
1050 ngs_shutdown(node_p node)
1052 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1053 struct ngpcb *const dpcbp = priv->datasock;
1054 struct ngpcb *const pcbp = priv->ctlsock;
1057 soisdisconnected(dpcbp->ng_socket);
1060 soisdisconnected(pcbp->ng_socket);
1062 mtx_lock(&priv->mtx);
1064 NG_NODE_SET_PRIVATE(node, NULL);
1065 ng_socket_free_priv(priv);
1067 NG_NODE_UNREF(node);
1072 ng_socket_item_applied(void *context, int error)
1074 struct ngsock *const priv = (struct ngsock *)context;
1076 mtx_lock(&priv->mtx);
1077 priv->error = error;
1079 mtx_unlock(&priv->mtx);
1084 dummy_disconnect(struct socket *so)
1089 * Control and data socket type descriptors
1091 * XXXRW: Perhaps _close should do something?
1094 static struct pr_usrreqs ngc_usrreqs = {
1096 .pru_attach = ngc_attach,
1097 .pru_bind = ngc_bind,
1098 .pru_connect = ngc_connect,
1099 .pru_detach = ngc_detach,
1100 .pru_disconnect = dummy_disconnect,
1101 .pru_peeraddr = NULL,
1102 .pru_send = ngc_send,
1103 .pru_shutdown = NULL,
1104 .pru_sockaddr = ng_setsockaddr,
1108 static struct pr_usrreqs ngd_usrreqs = {
1110 .pru_attach = ngd_attach,
1112 .pru_connect = ngd_connect,
1113 .pru_detach = ngd_detach,
1114 .pru_disconnect = dummy_disconnect,
1115 .pru_peeraddr = NULL,
1116 .pru_send = ngd_send,
1117 .pru_shutdown = NULL,
1118 .pru_sockaddr = ng_setsockaddr,
1123 * Definitions of protocols supported in the NETGRAPH domain.
1126 extern struct domain ngdomain; /* stop compiler warnings */
1128 static struct protosw ngsw[] = {
1130 .pr_type = SOCK_DGRAM,
1131 .pr_domain = &ngdomain,
1132 .pr_protocol = NG_CONTROL,
1133 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1134 .pr_usrreqs = &ngc_usrreqs
1137 .pr_type = SOCK_DGRAM,
1138 .pr_domain = &ngdomain,
1139 .pr_protocol = NG_DATA,
1140 .pr_flags = PR_ATOMIC | PR_ADDR,
1141 .pr_usrreqs = &ngd_usrreqs
1145 struct domain ngdomain = {
1146 .dom_family = AF_NETGRAPH,
1147 .dom_name = "netgraph",
1148 .dom_protosw = ngsw,
1149 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1153 * Handle loading and unloading for this node type
1154 * This is to handle auxiliary linkages (e.g protocol domain addition).
1157 ngs_mod_event(module_t mod, int event, void *data)
1163 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1164 /* Register protocol domain */
1165 net_add_domain(&ngdomain);
1168 /* Insure there are no open netgraph sockets */
1169 if (!LIST_EMPTY(&ngsocklist)) {
1175 if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) {
1176 /* Unregister protocol domain XXX can't do this yet.. */
1177 if ((error = net_rm_domain(&ngdomain)) != 0)
1179 mtx_destroy(&ngsocketlist_mtx);
1191 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1192 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1193 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1194 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1195 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");