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>
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>
71 #include <netgraph/ng_message.h>
72 #include <netgraph/netgraph.h>
73 #include <netgraph/ng_socketvar.h>
74 #include <netgraph/ng_socket.h>
76 #ifdef NG_SEPARATE_MALLOC
77 static MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info");
78 static 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_findhook_t ngs_findhook;
117 static ng_rcvdata_t ngs_rcvdata;
118 static ng_disconnect_t ngs_disconnect;
120 /* Internal methods */
121 static int ng_attach_data(struct socket *so);
122 static int ng_attach_cntl(struct socket *so);
123 static int ng_attach_common(struct socket *so, int type);
124 static void ng_detach_common(struct ngpcb *pcbp, int type);
125 static void ng_socket_free_priv(struct ngsock *priv);
126 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
127 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
129 static int ngs_mod_event(module_t mod, int event, void *data);
130 static void ng_socket_item_applied(void *context, int error);
132 /* Netgraph type descriptor */
133 static struct ng_type typestruct = {
134 .version = NG_ABI_VERSION,
135 .name = NG_SOCKET_NODE_TYPE,
136 .mod_event = ngs_mod_event,
137 .constructor = ngs_constructor,
138 .rcvmsg = ngs_rcvmsg,
139 .shutdown = ngs_shutdown,
140 .newhook = ngs_newhook,
141 .connect = ngs_connect,
142 .findhook = ngs_findhook,
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_ULONG(_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_ULONG(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
154 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
156 /* List of all sockets (for netstat -f netgraph) */
157 static LIST_HEAD(, ngpcb) ngsocklist;
159 static struct mtx ngsocketlist_mtx;
161 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
163 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
168 /***************************************************************
170 ***************************************************************/
173 ngc_attach(struct socket *so, int proto, struct thread *td)
175 struct ngpcb *const pcbp = sotongpcb(so);
178 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
183 return (ng_attach_cntl(so));
187 ngc_detach(struct socket *so)
189 struct ngpcb *const pcbp = sotongpcb(so);
191 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
192 ng_detach_common(pcbp, NG_CONTROL);
196 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
197 struct mbuf *control, struct thread *td)
199 struct ngpcb *const pcbp = sotongpcb(so);
200 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
201 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
207 struct ng_apply_info apply;
214 /* Require destination as there may be >= 1 hooks on this node. */
216 error = EDESTADDRREQ;
221 * Allocate an expendable buffer for the path, chop off
222 * the sockaddr header, and make sure it's NUL terminated.
224 len = sap->sg_len - 2;
225 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
226 bcopy(sap->sg_data, path, len);
230 * Move the actual message out of mbufs into a linear buffer.
231 * Start by adding up the size of the data. (could use mh_len?)
233 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
237 * Move the data into a linear buffer as well.
238 * Messages are not delivered in mbufs.
240 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
241 m_copydata(m, 0, len, (char *)msg);
243 if (msg->header.version != NG_VERSION) {
244 free(msg, M_NETGRAPH_MSG);
251 * We look into the message and if it mkpeers a node of unknown type, we
252 * try to load it. We need to do this now, in syscall thread, because if
253 * message gets queued and applied later we will get panic.
255 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
256 msg->header.cmd == NGM_MKPEER) {
257 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
259 if (ng_findtype(mkp->type) == NULL) {
260 char filename[NG_TYPESIZ + 3];
263 /* Not found, try to load it as a loadable module. */
264 snprintf(filename, sizeof(filename), "ng_%s",
266 error = kern_kldload(curthread, filename, &fileid);
268 free(msg, M_NETGRAPH_MSG);
272 /* See if type has been loaded successfully. */
273 if (ng_findtype(mkp->type) == NULL) {
274 free(msg, M_NETGRAPH_MSG);
275 (void)kern_kldunload(curthread, fileid,
276 LINKER_UNLOAD_NORMAL);
283 item = ng_package_msg(msg, M_WAITOK);
284 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
286 #ifdef TRACE_MESSAGES
287 printf("ng_address_path: errx=%d\n", error);
292 #ifdef TRACE_MESSAGES
293 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
294 item->el_dest->nd_ID,
295 msg->header.typecookie,
300 item->el_dest->nd_type->name);
304 * We do not want to return from syscall until the item
305 * is processed by destination node. We register callback
306 * on the item, which will update priv->error when item
308 * If ng_snd_item() has queued item, we sleep until
309 * callback wakes us up.
311 bzero(&apply, sizeof(apply));
312 apply.apply = ng_socket_item_applied;
313 apply.context = priv;
314 item->apply = &apply;
317 error = ng_snd_item(item, 0);
319 mtx_lock(&priv->mtx);
320 if (priv->error == -1)
321 msleep(priv, &priv->mtx, 0, "ngsock", 0);
322 mtx_unlock(&priv->mtx);
323 KASSERT(priv->error != -1,
324 ("ng_socket: priv->error wasn't updated"));
329 free(path, M_NETGRAPH_PATH);
338 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
340 struct ngpcb *const pcbp = sotongpcb(so);
344 return (ng_bind(nam, pcbp));
348 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
351 * At this time refuse to do this.. it used to
352 * do something but it was undocumented and not used.
354 printf("program tried to connect control socket to remote node\n");
358 /***************************************************************
360 ***************************************************************/
363 ngd_attach(struct socket *so, int proto, struct thread *td)
365 struct ngpcb *const pcbp = sotongpcb(so);
369 return (ng_attach_data(so));
373 ngd_detach(struct socket *so)
375 struct ngpcb *const pcbp = sotongpcb(so);
377 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
378 ng_detach_common(pcbp, NG_DATA);
382 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
383 struct mbuf *control, struct thread *td)
385 struct ngpcb *const pcbp = sotongpcb(so);
386 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
389 char hookname[NG_HOOKSIZ];
391 if ((pcbp == NULL) || (control != NULL)) {
395 if (pcbp->sockdata == NULL) {
401 len = 0; /* Make compiler happy. */
403 len = sap->sg_len - 2;
406 * If the user used any of these ways to not specify an address
407 * then handle specially.
409 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
410 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
411 error = EDESTADDRREQ;
415 * If exactly one hook exists, just use it.
416 * Special case to allow write(2) to work on an ng_socket.
418 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
420 if (len >= NG_HOOKSIZ) {
426 * chop off the sockaddr header, and make sure it's NUL
429 bcopy(sap->sg_data, hookname, len);
430 hookname[len] = '\0';
432 /* Find the correct hook from 'hookname' */
433 hook = ng_findhook(pcbp->sockdata->node, hookname);
435 error = EHOSTUNREACH;
441 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
452 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
454 struct ngpcb *const pcbp = sotongpcb(so);
458 return (ng_connect_data(nam, pcbp));
462 * Used for both data and control sockets
465 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
468 struct sockaddr_ng *sg;
472 pcbp = sotongpcb(so);
473 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
474 /* XXXGL: can this still happen? */
477 sg_len = sizeof(struct sockaddr_ng) + NG_NODESIZ -
479 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
481 mtx_lock(&pcbp->sockdata->mtx);
482 if (pcbp->sockdata->node != NULL) {
483 node_p node = pcbp->sockdata->node;
485 if (NG_NODE_HAS_NAME(node))
486 bcopy(NG_NODE_NAME(node), sg->sg_data,
487 strlen(NG_NODE_NAME(node)));
488 mtx_unlock(&pcbp->sockdata->mtx);
491 sg->sg_family = AF_NETGRAPH;
492 *addr = (struct sockaddr *)sg;
494 mtx_unlock(&pcbp->sockdata->mtx);
503 * Attach a socket to it's protocol specific partner.
504 * For a control socket, actually create a netgraph node and attach
509 ng_attach_cntl(struct socket *so)
516 /* Setup protocol control block */
517 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
519 pcbp = sotongpcb(so);
521 /* Make the generic node components */
522 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
523 ng_detach_common(pcbp, NG_CONTROL);
528 * Allocate node private info and hash. We start
529 * with 16 hash entries, however we may grow later
530 * in ngs_newhook(). We can't predict how much hooks
531 * does this node plan to have.
533 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
534 priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
536 /* Initialize mutex. */
537 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
539 /* Link the pcb the private data. */
540 priv->ctlsock = pcbp;
541 pcbp->sockdata = priv;
545 /* Store a hint for netstat(1). */
546 priv->node_id = priv->node->nd_ID;
548 /* Link the node and the private data. */
549 NG_NODE_SET_PRIVATE(priv->node, priv);
550 NG_NODE_REF(priv->node);
557 ng_attach_data(struct socket *so)
559 return (ng_attach_common(so, NG_DATA));
563 * Set up a socket protocol control block.
564 * This code is shared between control and data sockets.
567 ng_attach_common(struct socket *so, int type)
572 /* Standard socket setup stuff. */
573 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
577 /* Allocate the pcb. */
578 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
581 /* Link the pcb and the socket. */
582 so->so_pcb = (caddr_t)pcbp;
583 pcbp->ng_socket = so;
585 /* Add the socket to linked list */
586 mtx_lock(&ngsocketlist_mtx);
587 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
588 mtx_unlock(&ngsocketlist_mtx);
593 * Disassociate the socket from it's protocol specific
594 * partner. If it's attached to a node's private data structure,
595 * then unlink from that too. If we were the last socket attached to it,
596 * then shut down the entire node. Shared code for control and data sockets.
599 ng_detach_common(struct ngpcb *pcbp, int which)
601 struct ngsock *priv = pcbp->sockdata;
604 mtx_lock(&priv->mtx);
608 priv->ctlsock = NULL;
611 priv->datasock = NULL;
614 panic("%s", __func__);
616 pcbp->sockdata = NULL;
618 ng_socket_free_priv(priv);
621 pcbp->ng_socket->so_pcb = NULL;
622 mtx_lock(&ngsocketlist_mtx);
623 LIST_REMOVE(pcbp, socks);
624 mtx_unlock(&ngsocketlist_mtx);
629 * Remove a reference from node private data.
632 ng_socket_free_priv(struct ngsock *priv)
634 mtx_assert(&priv->mtx, MA_OWNED);
638 if (priv->refs == 0) {
639 mtx_destroy(&priv->mtx);
640 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
641 free(priv, M_NETGRAPH_SOCK);
645 if ((priv->refs == 1) && (priv->node != NULL)) {
646 node_p node = priv->node;
649 mtx_unlock(&priv->mtx);
651 ng_rmnode_self(node);
653 mtx_unlock(&priv->mtx);
657 * Connect the data socket to a named control socket node.
660 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
662 struct sockaddr_ng *sap;
668 /* If we are already connected, don't do it again. */
669 if (pcbp->sockdata != NULL)
673 * Find the target (victim) and check it doesn't already have
674 * a data socket. Also check it is a 'socket' type node.
675 * Use ng_package_data() and ng_address_path() to do this.
678 sap = (struct sockaddr_ng *) nam;
679 /* The item will hold the node reference. */
680 item = ng_package_data(NULL, NG_WAITOK);
682 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
683 return (error); /* item is freed on failure */
686 * Extract node from item and free item. Remember we now have
687 * a reference on the node. The item holds it for us.
688 * when we free the item we release the reference.
690 farnode = item->el_dest; /* shortcut */
691 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
692 NG_FREE_ITEM(item); /* drop the reference to the node */
695 priv = NG_NODE_PRIVATE(farnode);
696 if (priv->datasock != NULL) {
697 NG_FREE_ITEM(item); /* drop the reference to the node */
702 * Link the PCB and the private data struct. and note the extra
703 * reference. Drop the extra reference on the node.
705 mtx_lock(&priv->mtx);
706 priv->datasock = pcbp;
707 pcbp->sockdata = priv;
709 mtx_unlock(&priv->mtx);
710 NG_FREE_ITEM(item); /* drop the reference to the node */
715 * Binding a socket means giving the corresponding node a name
718 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
720 struct ngsock *const priv = pcbp->sockdata;
721 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
727 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
728 (sap->sg_data[0] == '\0') ||
729 (sap->sg_data[sap->sg_len - 3] != '\0')) {
733 return (ng_name_node(priv->node, sap->sg_data));
736 /***************************************************************
738 ***************************************************************/
741 * You can only create new nodes from the socket end of things.
744 ngs_constructor(node_p nodep)
750 ngs_rehash(node_p node)
752 struct ngsock *priv = NG_NODE_PRIVATE(node);
759 new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
764 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
765 hp = NG_HOOK_PRIVATE(hook);
767 LIST_REMOVE(hp, next);
769 h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
770 LIST_INSERT_HEAD(&new[h], hp, next);
773 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
779 * We allow any hook to be connected to the node.
780 * There is no per-hook private information though.
783 ngs_newhook(node_p node, hook_p hook, const char *name)
785 struct ngsock *const priv = NG_NODE_PRIVATE(node);
789 hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
792 if (node->nd_numhooks * 2 > priv->hmask)
795 h = hash32_str(name, HASHINIT) & priv->hmask;
796 LIST_INSERT_HEAD(&priv->hash[h], hp, next);
797 NG_HOOK_SET_PRIVATE(hook, hp);
803 * If only one hook, allow read(2) and write(2) to work.
806 ngs_connect(hook_p hook)
808 node_p node = NG_HOOK_NODE(hook);
809 struct ngsock *priv = NG_NODE_PRIVATE(node);
811 if ((priv->datasock) && (priv->datasock->ng_socket)) {
812 if (NG_NODE_NUMHOOKS(node) == 1)
813 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
815 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
820 /* Look up hook by name */
822 ngs_findhook(node_p node, const char *name)
824 struct ngsock *priv = NG_NODE_PRIVATE(node);
829 * Microoptimisation for an ng_socket with
830 * a single hook, which is a common case.
832 if (node->nd_numhooks == 1) {
835 hook = LIST_FIRST(&node->nd_hooks);
837 if (strcmp(NG_HOOK_NAME(hook), name) == 0)
843 h = hash32_str(name, HASHINIT) & priv->hmask;
845 LIST_FOREACH(hp, &priv->hash[h], next)
846 if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
853 * Incoming messages get passed up to the control socket.
854 * Unless they are for us specifically (socket_type)
857 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
859 struct ngsock *const priv = NG_NODE_PRIVATE(node);
862 struct sockaddr_ng addr;
865 ng_ID_t retaddr = NGI_RETADDR(item);
869 NGI_GET_MSG(item, msg);
873 * Grab priv->mtx here to prevent destroying of control socket
874 * after checking that priv->ctlsock is not NULL.
876 mtx_lock(&priv->mtx);
877 pcbp = priv->ctlsock;
880 * Only allow mesgs to be passed if we have the control socket.
881 * Data sockets can only support the generic messages.
884 mtx_unlock(&priv->mtx);
889 so = pcbp->ng_socket;
890 SOCKBUF_LOCK(&so->so_rcv);
892 /* As long as the race is handled, priv->mtx may be unlocked now. */
893 mtx_unlock(&priv->mtx);
895 #ifdef TRACE_MESSAGES
896 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
898 msg->header.typecookie,
905 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
906 switch (msg->header.cmd) {
907 case NGM_SOCK_CMD_NOLINGER:
908 priv->flags |= NGS_FLAG_NOLINGER;
910 case NGM_SOCK_CMD_LINGER:
911 priv->flags &= ~NGS_FLAG_NOLINGER;
914 error = EINVAL; /* unknown command */
916 SOCKBUF_UNLOCK(&so->so_rcv);
918 /* Free the message and return. */
923 /* Get the return address into a sockaddr. */
924 bzero(&addr, sizeof(addr));
925 addr.sg_len = sizeof(addr);
926 addr.sg_family = AF_NETGRAPH;
927 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
929 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
930 SOCKBUF_UNLOCK(&so->so_rcv);
931 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
937 /* Copy the message itself into an mbuf chain. */
938 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
942 * Here we free the message. We need to do that
943 * regardless of whether we got mbufs.
948 SOCKBUF_UNLOCK(&so->so_rcv);
953 /* Send it up to the socket. */
954 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
956 SOCKBUF_UNLOCK(&so->so_rcv);
961 sorwakeup_locked(so);
967 * Receive data on a hook
970 ngs_rcvdata(hook_p hook, item_p item)
972 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
973 struct ngpcb *const pcbp = priv->datasock;
975 struct sockaddr_ng *addr;
976 char *addrbuf[NG_HOOKSIZ + 4];
983 /* If there is no data socket, black-hole it. */
988 so = pcbp->ng_socket;
990 /* Get the return address into a sockaddr. */
991 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
992 addr = (struct sockaddr_ng *) addrbuf;
993 addr->sg_len = addrlen + 3;
994 addr->sg_family = AF_NETGRAPH;
995 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
996 addr->sg_data[addrlen] = '\0';
998 /* Try to tell the socket which hook it came in on. */
999 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
1009 * Hook disconnection
1011 * For this type, removal of the last link destroys the node
1012 * if the NOLINGER flag is set.
1015 ngs_disconnect(hook_p hook)
1017 node_p node = NG_HOOK_NODE(hook);
1018 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1019 struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
1021 LIST_REMOVE(hp, next);
1022 free(hp, M_NETGRAPH_SOCK);
1024 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1025 if (NG_NODE_NUMHOOKS(node) == 1)
1026 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1028 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1031 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1032 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1033 ng_rmnode_self(node);
1039 * Do local shutdown processing.
1040 * In this case, that involves making sure the socket
1041 * knows we should be shutting down.
1044 ngs_shutdown(node_p node)
1046 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1047 struct ngpcb *dpcbp, *pcbp;
1049 mtx_lock(&priv->mtx);
1050 dpcbp = priv->datasock;
1051 pcbp = priv->ctlsock;
1054 soisdisconnected(dpcbp->ng_socket);
1057 soisdisconnected(pcbp->ng_socket);
1060 NG_NODE_SET_PRIVATE(node, NULL);
1061 ng_socket_free_priv(priv);
1063 NG_NODE_UNREF(node);
1068 ng_socket_item_applied(void *context, int error)
1070 struct ngsock *const priv = (struct ngsock *)context;
1072 mtx_lock(&priv->mtx);
1073 priv->error = error;
1075 mtx_unlock(&priv->mtx);
1080 dummy_disconnect(struct socket *so)
1085 * Control and data socket type descriptors
1087 * XXXRW: Perhaps _close should do something?
1090 static struct pr_usrreqs ngc_usrreqs = {
1092 .pru_attach = ngc_attach,
1093 .pru_bind = ngc_bind,
1094 .pru_connect = ngc_connect,
1095 .pru_detach = ngc_detach,
1096 .pru_disconnect = dummy_disconnect,
1097 .pru_peeraddr = NULL,
1098 .pru_send = ngc_send,
1099 .pru_shutdown = NULL,
1100 .pru_sockaddr = ng_getsockaddr,
1104 static struct pr_usrreqs ngd_usrreqs = {
1106 .pru_attach = ngd_attach,
1108 .pru_connect = ngd_connect,
1109 .pru_detach = ngd_detach,
1110 .pru_disconnect = dummy_disconnect,
1111 .pru_peeraddr = NULL,
1112 .pru_send = ngd_send,
1113 .pru_shutdown = NULL,
1114 .pru_sockaddr = ng_getsockaddr,
1119 * Definitions of protocols supported in the NETGRAPH domain.
1122 extern struct domain ngdomain; /* stop compiler warnings */
1124 static struct protosw ngsw[] = {
1126 .pr_type = SOCK_DGRAM,
1127 .pr_domain = &ngdomain,
1128 .pr_protocol = NG_CONTROL,
1129 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1130 .pr_usrreqs = &ngc_usrreqs
1133 .pr_type = SOCK_DGRAM,
1134 .pr_domain = &ngdomain,
1135 .pr_protocol = NG_DATA,
1136 .pr_flags = PR_ATOMIC | PR_ADDR,
1137 .pr_usrreqs = &ngd_usrreqs
1141 struct domain ngdomain = {
1142 .dom_family = AF_NETGRAPH,
1143 .dom_name = "netgraph",
1144 .dom_protosw = ngsw,
1145 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1149 * Handle loading and unloading for this node type.
1150 * This is to handle auxiliary linkages (e.g protocol domain addition).
1153 ngs_mod_event(module_t mod, int event, void *data)
1159 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1162 /* Ensure there are no open netgraph sockets. */
1163 if (!LIST_EMPTY(&ngsocklist)) {
1168 /* Unregister protocol domain XXX can't do this yet.. */
1179 VNET_DOMAIN_SET(ng);
1181 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1182 static SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1183 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1184 static SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1185 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");