6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Julian Elischer <julian@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_socket.c,v 1.85 2008/03/11 21:58:48 mav Exp $
41 * $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/domain.h>
53 #include <sys/kernel.h>
54 #include <sys/linker.h>
56 #include <sys/malloc.h>
58 #include <sys/msgport2.h>
60 #include <sys/mutex.h>
62 #include <sys/param.h>
65 #include <sys/protosw.h>
66 #include <sys/queue.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/socketvar2.h>
71 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74 #include <sys/thread2.h>
75 #include <sys/vnode.h>
77 #include <netgraph7/ng_message.h>
78 #include <netgraph7/netgraph.h>
79 #include "ng_socketvar.h"
80 #include "ng_socket.h"
82 #ifdef NG_SEPARATE_MALLOC
83 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
84 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
86 #define M_NETGRAPH_PATH M_NETGRAPH
87 #define M_NETGRAPH_SOCK M_NETGRAPH
91 * It's Ascii-art time!
92 * +-------------+ +-------------+
93 * |socket (ctl)| |socket (data)|
94 * +-------------+ +-------------+
98 * +-----------+ +-----------+
99 * |pcb (ctl)| |pcb (data)|
100 * +-----------+ +-----------+
104 * +--------------------------+
105 * | Socket type private |
107 * +--------------------------+
116 /* Netgraph node methods */
117 static ng_constructor_t ngs_constructor;
118 static ng_rcvmsg_t ngs_rcvmsg;
119 static ng_shutdown_t ngs_shutdown;
120 static ng_newhook_t ngs_newhook;
121 static ng_connect_t ngs_connect;
122 static ng_rcvdata_t ngs_rcvdata;
123 static ng_disconnect_t ngs_disconnect;
125 /* Internal methods */
126 static int ng_attach_data(struct socket *so);
127 static int ng_attach_cntl(struct socket *so);
128 static int ng_attach_common(struct socket *so, int type);
129 static void ng_detach_common(struct ngpcb *pcbp, int type);
130 static void ng_socket_free_priv(struct ngsock *priv);
132 static int ng_internalize(struct mbuf *m, struct thread *p);
134 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
135 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
137 static int ngs_mod_event(module_t mod, int event, void *data);
138 static void ng_socket_item_applied(void *context, int error);
139 static int linker_api_available(void);
141 /* Netgraph type descriptor */
142 static struct ng_type typestruct = {
143 .version = NG_ABI_VERSION,
144 .name = NG_SOCKET_NODE_TYPE,
145 .mod_event = ngs_mod_event,
146 .constructor = ngs_constructor,
147 .rcvmsg = ngs_rcvmsg,
148 .shutdown = ngs_shutdown,
149 .newhook = ngs_newhook,
150 .connect = ngs_connect,
151 .rcvdata = ngs_rcvdata,
152 .disconnect = ngs_disconnect,
154 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
157 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
158 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
159 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
160 static u_long ngpdg_recvspace = 20 * 1024;
161 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
162 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
164 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
166 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
171 /***************************************************************
173 ***************************************************************/
176 ngc_attach(netmsg_t msg)
178 struct socket *so = msg->attach.base.nm_so;
179 struct pru_attach_info *ai = msg->attach.nm_ai;
180 struct ngpcb *const pcbp = sotongpcb(so);
183 if (priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY) != 0)
185 else if (pcbp != NULL)
188 error = ng_attach_cntl(so);
189 lwkt_replymsg(&msg->attach.base.lmsg, error);
193 ngc_detach(netmsg_t msg)
195 struct socket *so = msg->detach.base.nm_so;
196 struct ngpcb *const pcbp = sotongpcb(so);
198 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
199 ng_detach_common(pcbp, NG_CONTROL);
200 lwkt_replymsg(&msg->detach.base.lmsg, 0);
204 ngc_send(netmsg_t netmsg)
206 struct socket *so = netmsg->send.base.nm_so;
207 struct mbuf *m = netmsg->send.nm_m;
208 struct sockaddr *addr = netmsg->send.nm_addr;
209 struct mbuf *control = netmsg->send.nm_control;
210 struct ngpcb *const pcbp = sotongpcb(so);
211 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
212 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
218 struct ng_apply_info apply;
221 if (control && (error = ng_internalize(control, td))) {
222 if (pcbp->sockdata == NULL) {
234 /* Require destination as there may be >= 1 hooks on this node. */
236 error = EDESTADDRREQ;
241 * Allocate an expendable buffer for the path, chop off
242 * the sockaddr header, and make sure it's NUL terminated.
244 len = sap->sg_len - 2;
245 path = kmalloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
246 bcopy(sap->sg_data, path, len);
250 * Move the actual message out of mbufs into a linear buffer.
251 * Start by adding up the size of the data. (could use mh_len?)
253 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
257 * Move the data into a linear buffer as well.
258 * Messages are not delivered in mbufs.
260 msg = kmalloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
261 m_copydata(m, 0, len, (char *)msg);
263 if (msg->header.version != NG_VERSION) {
264 kfree(msg, M_NETGRAPH_MSG);
271 * We look into the message and if it mkpeers a node of unknown type, we
272 * try to load it. We need to do this now, in syscall thread, because if
273 * message gets queued and applied later we will get panic.
275 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
276 msg->header.cmd == NGM_MKPEER) {
277 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
278 struct ng_type *type;
280 if ((type = ng_findtype(mkp->type)) == NULL) {
281 char filename[NG_TYPESIZ + 3];
282 linker_file_t fileid;
284 if (!linker_api_available()) {
289 /* Not found, try to load it as a loadable module. */
290 snprintf(filename, sizeof(filename), "ng_%s.ko",
292 error = linker_load_file(filename, &fileid);
294 kfree(msg, M_NETGRAPH_MSG);
298 /* See if type has been loaded successfully. */
299 if ((type = ng_findtype(mkp->type)) == NULL) {
300 kfree(msg, M_NETGRAPH_MSG);
301 (void)linker_file_unload(fileid);
308 item = ng_package_msg(msg, NG_WAITOK);
309 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
311 #ifdef TRACE_MESSAGES
312 printf("ng_address_path: errx=%d\n", error);
317 #ifdef TRACE_MESSAGES
318 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
319 item->el_dest->nd_ID,
320 msg->header.typecookie,
325 item->el_dest->nd_type->name);
329 * We do not want to return from syscall until the item
330 * is processed by destination node. We register callback
331 * on the item, which will update priv->error when item
333 * If ng_snd_item() has queued item, we sleep until
334 * callback wakes us up.
336 bzero(&apply, sizeof(apply));
337 apply.apply = ng_socket_item_applied;
338 apply.context = priv;
339 item->apply = &apply;
342 error = ng_snd_item(item, 0);
344 mtx_lock(&priv->mtx);
345 if (priv->error == -1)
346 mtxsleep(priv, &priv->mtx, 0, "ngsock", 0);
347 mtx_unlock(&priv->mtx);
348 KASSERT(priv->error != -1,
349 ("ng_socket: priv->error wasn't updated"));
354 kfree(path, M_NETGRAPH_PATH);
360 lwkt_replymsg(&netmsg->send.base.lmsg, error);
364 ngc_bind(netmsg_t msg)
366 struct socket *so = msg->connect.base.nm_so;
367 struct sockaddr *nam = msg->connect.nm_nam;
368 struct ngpcb *const pcbp = sotongpcb(so);
374 error = ng_bind(nam, pcbp);
375 lwkt_replymsg(&msg->connect.base.lmsg, error);
379 ngc_connect(netmsg_t msg)
382 * At this time refuse to do this.. it used to
383 * do something but it was undocumented and not used.
385 printf("program tried to connect control socket to remote node\n");
386 lwkt_replymsg(&msg->connect.base.lmsg, EINVAL);
389 /***************************************************************
391 ***************************************************************/
394 ngd_attach(netmsg_t msg)
396 struct socket *so = msg->attach.base.nm_so;
397 struct ngpcb *const pcbp = sotongpcb(so);
403 error = ng_attach_data(so);
404 lwkt_replymsg(&msg->connect.base.lmsg, error);
408 ngd_detach(netmsg_t msg)
410 struct socket *so = msg->detach.base.nm_so;
411 struct ngpcb *const pcbp = sotongpcb(so);
413 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
414 ng_detach_common(pcbp, NG_DATA);
415 lwkt_replymsg(&msg->detach.base.lmsg, 0);
419 ngd_send(netmsg_t msg)
421 struct socket *so = msg->send.base.nm_so;
422 struct mbuf *m = msg->send.nm_m;
423 struct sockaddr *addr = msg->send.nm_addr;
424 struct mbuf *control = msg->send.nm_control;
425 struct ngpcb *const pcbp = sotongpcb(so);
426 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
429 char hookname[NG_HOOKSIZ];
431 if ((pcbp == NULL) || (control != NULL)) {
435 if (pcbp->sockdata == NULL) {
441 len = 0; /* Make compiler happy. */
443 len = sap->sg_len - 2;
446 * If the user used any of these ways to not specify an address
447 * then handle specially.
449 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
450 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
451 error = EDESTADDRREQ;
455 * If exactly one hook exists, just use it.
456 * Special case to allow write(2) to work on an ng_socket.
458 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
460 if (len >= NG_HOOKSIZ) {
466 * chop off the sockaddr header, and make sure it's NUL
469 bcopy(sap->sg_data, hookname, len);
470 hookname[len] = '\0';
472 /* Find the correct hook from 'hookname' */
473 hook = ng_findhook(pcbp->sockdata->node, hookname);
475 error = EHOSTUNREACH;
481 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
488 lwkt_replymsg(&msg->send.base.lmsg, error);
492 ngd_connect(netmsg_t msg)
494 struct socket *so = msg->connect.base.nm_so;
495 struct sockaddr *nam = msg->connect.nm_nam;
496 struct ngpcb *const pcbp = sotongpcb(so);
502 error = ng_connect_data(nam, pcbp);
503 lwkt_replymsg(&msg->connect.base.lmsg, error);
507 * Used for both data and control sockets
510 ng_getsockaddr(netmsg_t msg)
512 struct socket *so = msg->sockaddr.base.nm_so;
513 struct sockaddr **addr = msg->sockaddr.nm_nam;
515 struct sockaddr_ng *sg;
519 /* Why isn't sg_data a `char[1]' ? :-( */
520 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
522 pcbp = sotongpcb(so);
523 if ((pcbp == NULL) || (pcbp->sockdata == NULL)) {
524 /* XXXGL: can this still happen? */
529 mtx_lock(&pcbp->sockdata->mtx);
530 if (pcbp->sockdata->node != NULL) {
531 node_p node = pcbp->sockdata->node;
532 int namelen = 0; /* silence compiler! */
534 if (NG_NODE_HAS_NAME(node))
535 sg_len += namelen = strlen(NG_NODE_NAME(node));
537 sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
539 if (NG_NODE_HAS_NAME(node))
540 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
543 sg->sg_family = AF_NETGRAPH;
544 *addr = (struct sockaddr *)sg;
545 mtx_unlock(&pcbp->sockdata->mtx);
547 mtx_unlock(&pcbp->sockdata->mtx);
552 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
556 * Attach a socket to it's protocol specific partner.
557 * For a control socket, actually create a netgraph node and attach
562 ng_attach_cntl(struct socket *so)
568 /* Allocate node private info */
569 priv = kmalloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
571 /* Setup protocol control block */
572 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
573 kfree(priv, M_NETGRAPH_SOCK);
576 pcbp = sotongpcb(so);
578 /* Link the pcb the private data. */
579 priv->ctlsock = pcbp;
580 pcbp->sockdata = priv;
583 /* Initialize mutex. */
584 mtx_init(&priv->mtx);
586 /* Make the generic node components */
587 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
588 kfree(priv, M_NETGRAPH_SOCK);
589 ng_detach_common(pcbp, NG_CONTROL);
593 /* Link the node and the private data. */
594 NG_NODE_SET_PRIVATE(priv->node, priv);
595 NG_NODE_REF(priv->node);
602 ng_attach_data(struct socket *so)
604 return (ng_attach_common(so, NG_DATA));
608 * Set up a socket protocol control block.
609 * This code is shared between control and data sockets.
612 ng_attach_common(struct socket *so, int type)
617 /* Standard socket setup stuff. */
618 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL);
622 /* Allocate the pcb. */
623 pcbp = kmalloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
626 /* Link the pcb and the socket. */
627 so->so_pcb = (caddr_t)pcbp;
628 pcbp->ng_socket = so;
634 * Disassociate the socket from it's protocol specific
635 * partner. If it's attached to a node's private data structure,
636 * then unlink from that too. If we were the last socket attached to it,
637 * then shut down the entire node. Shared code for control and data sockets.
640 ng_detach_common(struct ngpcb *pcbp, int which)
642 struct ngsock *priv = pcbp->sockdata;
645 mtx_lock(&priv->mtx);
649 priv->ctlsock = NULL;
652 priv->datasock = NULL;
657 pcbp->sockdata = NULL;
659 ng_socket_free_priv(priv);
662 pcbp->ng_socket->so_pcb = NULL;
667 * Remove a reference from node private data.
670 ng_socket_free_priv(struct ngsock *priv)
672 KKASSERT(mtx_owned(&priv->mtx));
676 if (priv->refs == 0) {
677 mtx_uninit(&priv->mtx);
678 kfree(priv, M_NETGRAPH_SOCK);
682 if ((priv->refs == 1) && (priv->node != NULL)) {
683 node_p node = priv->node;
686 mtx_unlock(&priv->mtx);
688 ng_rmnode_self(node);
690 mtx_unlock(&priv->mtx);
695 * File descriptors can be passed into an AF_NETGRAPH socket.
696 * Note, that file descriptors cannot be passed OUT.
697 * Only character device descriptors are accepted.
698 * Character devices are useful to connect a graph to a device,
699 * which after all is the purpose of this whole system.
702 ng_internalize(struct mbuf *control, struct thread *td)
704 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
710 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
711 cm->cmsg_len != control->m_len) {
716 /* Check there is only one FD. XXX what would more than one signify? */
717 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
723 /* Check that the FD given is legit. and change it to a pointer to a
726 if ((error = fget(td, fd, &fp)) != 0)
729 /* Depending on what kind of resource it is, act differently. For
730 * devices, we treat it as a file. For an AF_NETGRAPH socket,
731 * shortcut straight to the node. */
732 switch (fp->f_type) {
735 if (vn && (vn->v_type == VCHR)) {
736 /* for a VCHR, actually reference the FILE */
738 /* XXX then what :) */
739 /* how to pass on to other modules? */
757 * Connect the data socket to a named control socket node.
760 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
762 struct sockaddr_ng *sap;
768 /* If we are already connected, don't do it again. */
769 if (pcbp->sockdata != NULL)
773 * Find the target (victim) and check it doesn't already have
774 * a data socket. Also check it is a 'socket' type node.
775 * Use ng_package_data() and ng_address_path() to do this.
778 sap = (struct sockaddr_ng *) nam;
779 /* The item will hold the node reference. */
780 item = ng_package_data(NULL, NG_WAITOK);
782 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
783 return (error); /* item is freed on failure */
786 * Extract node from item and free item. Remember we now have
787 * a reference on the node. The item holds it for us.
788 * when we free the item we release the reference.
790 farnode = item->el_dest; /* shortcut */
791 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
792 NG_FREE_ITEM(item); /* drop the reference to the node */
795 priv = NG_NODE_PRIVATE(farnode);
796 if (priv->datasock != NULL) {
797 NG_FREE_ITEM(item); /* drop the reference to the node */
802 * Link the PCB and the private data struct. and note the extra
803 * reference. Drop the extra reference on the node.
805 mtx_lock(&priv->mtx);
806 priv->datasock = pcbp;
807 pcbp->sockdata = priv;
809 mtx_unlock(&priv->mtx);
810 NG_FREE_ITEM(item); /* drop the reference to the node */
815 * Binding a socket means giving the corresponding node a name
818 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
820 struct ngsock *const priv = pcbp->sockdata;
821 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
827 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
828 (sap->sg_data[0] == '\0') ||
829 (sap->sg_data[sap->sg_len - 3] != '\0')) {
833 return (ng_name_node(priv->node, sap->sg_data));
836 /***************************************************************
838 ***************************************************************/
841 * You can only create new nodes from the socket end of things.
844 ngs_constructor(node_p nodep)
850 * We allow any hook to be connected to the node.
851 * There is no per-hook private information though.
854 ngs_newhook(node_p node, hook_p hook, const char *name)
856 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
861 * If only one hook, allow read(2) and write(2) to work.
864 ngs_connect(hook_p hook)
866 node_p node = NG_HOOK_NODE(hook);
867 struct ngsock *priv = NG_NODE_PRIVATE(node);
869 if ((priv->datasock) && (priv->datasock->ng_socket)) {
870 if (NG_NODE_NUMHOOKS(node) == 1)
871 sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
873 soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
879 * Incoming messages get passed up to the control socket.
880 * Unless they are for us specifically (socket_type)
883 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
885 struct ngsock *const priv = NG_NODE_PRIVATE(node);
886 struct ngpcb *const pcbp = priv->ctlsock;
888 struct sockaddr_ng addr;
891 ng_ID_t retaddr = NGI_RETADDR(item);
895 NGI_GET_MSG(item, msg);
899 * Only allow mesgs to be passed if we have the control socket.
900 * Data sockets can only support the generic messages.
907 so = pcbp->ng_socket;
909 #ifdef TRACE_MESSAGES
910 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
912 msg->header.typecookie,
919 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
920 switch (msg->header.cmd) {
921 case NGM_SOCK_CMD_NOLINGER:
922 priv->flags |= NGS_FLAG_NOLINGER;
924 case NGM_SOCK_CMD_LINGER:
925 priv->flags &= ~NGS_FLAG_NOLINGER;
928 error = EINVAL; /* unknown command */
930 /* Free the message and return. */
935 /* Get the return address into a sockaddr. */
936 bzero(&addr, sizeof(addr));
937 addr.sg_len = sizeof(addr);
938 addr.sg_family = AF_NETGRAPH;
939 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
941 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
942 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
948 /* Copy the message itself into an mbuf chain. */
949 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
953 * Here we free the message. We need to do that
954 * regardless of whether we got mbufs.
963 /* Send it up to the socket. */
964 if (sbappendaddr((struct sockbuf *)&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
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];
991 /* If there is no data socket, black-hole it. */
996 so = pcbp->ng_socket;
998 /* Get the return address into a sockaddr. */
999 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1000 addr = (struct sockaddr_ng *) addrbuf;
1001 addr->sg_len = addrlen + 3;
1002 addr->sg_family = AF_NETGRAPH;
1003 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1004 addr->sg_data[addrlen] = '\0';
1006 /* Try to tell the socket which hook it came in on. */
1007 if (sbappendaddr((struct sockbuf *)&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
1017 * Hook disconnection
1019 * For this type, removal of the last link destroys the node
1020 * if the NOLINGER flag is set.
1023 ngs_disconnect(hook_p hook)
1025 node_p node = NG_HOOK_NODE(hook);
1026 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1028 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1029 if (NG_NODE_NUMHOOKS(node) == 1)
1030 sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
1032 soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
1035 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1036 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1037 ng_rmnode_self(node);
1043 * Do local shutdown processing.
1044 * In this case, that involves making sure the socket
1045 * knows we should be shutting down.
1048 ngs_shutdown(node_p node)
1050 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1051 struct ngpcb *const dpcbp = priv->datasock;
1052 struct ngpcb *const pcbp = priv->ctlsock;
1055 soisdisconnected(dpcbp->ng_socket);
1058 soisdisconnected(pcbp->ng_socket);
1060 mtx_lock(&priv->mtx);
1062 NG_NODE_SET_PRIVATE(node, NULL);
1063 ng_socket_free_priv(priv);
1065 NG_NODE_UNREF(node);
1070 ng_socket_item_applied(void *context, int error)
1072 struct ngsock *const priv = (struct ngsock *)context;
1074 mtx_lock(&priv->mtx);
1075 priv->error = error;
1077 mtx_unlock(&priv->mtx);
1082 * Control and data socket type descriptors
1084 * XXXRW: Perhaps _close should do something?
1087 static struct pr_usrreqs ngc_usrreqs = {
1089 .pru_attach = ngc_attach,
1090 .pru_bind = ngc_bind,
1091 .pru_connect = ngc_connect,
1092 .pru_detach = ngc_detach,
1093 .pru_disconnect = NULL,
1094 .pru_peeraddr = NULL,
1095 .pru_send = ngc_send,
1096 .pru_shutdown = NULL,
1097 .pru_sockaddr = ng_getsockaddr,
1098 .pru_sosend = sosend,
1099 .pru_soreceive = soreceive,
1100 /* .pru_close = NULL, */
1103 static struct pr_usrreqs ngd_usrreqs = {
1105 .pru_attach = ngd_attach,
1107 .pru_connect = ngd_connect,
1108 .pru_detach = ngd_detach,
1109 .pru_disconnect = NULL,
1110 .pru_peeraddr = NULL,
1111 .pru_send = ngd_send,
1112 .pru_shutdown = NULL,
1113 .pru_sockaddr = ng_getsockaddr,
1114 .pru_sosend = sosend,
1115 .pru_soreceive = soreceive,
1116 /* .pru_close = NULL, */
1120 * Definitions of protocols supported in the NETGRAPH domain.
1123 extern struct domain ngdomain; /* stop compiler warnings */
1125 static struct protosw ngsw[] = {
1127 .pr_type = SOCK_DGRAM,
1128 .pr_domain = &ngdomain,
1129 .pr_protocol = NG_CONTROL,
1130 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1131 .pr_usrreqs = &ngc_usrreqs
1134 .pr_type = SOCK_DGRAM,
1135 .pr_domain = &ngdomain,
1136 .pr_protocol = NG_DATA,
1137 .pr_flags = PR_ATOMIC | PR_ADDR,
1138 .pr_usrreqs = &ngd_usrreqs
1142 struct domain ngdomain = {
1143 .dom_family = AF_NETGRAPH,
1144 .dom_name = "netgraph",
1145 .dom_protosw = ngsw,
1146 .dom_protoswNPROTOSW = &ngsw[NELEM(ngsw)]
1150 * Handle loading and unloading for this node type.
1151 * This is to handle auxiliary linkages (e.g protocol domain addition).
1154 ngs_mod_event(module_t mod, int event, void *data)
1160 /* Register protocol domain. */
1161 net_add_domain(&ngdomain);
1165 /* Unregister protocol domain XXX can't do this yet.. */
1166 if ((error = net_rm_domain(&ngdomain)) != 0)
1180 linker_api_available(void)
1182 /* linker_* API won't work without a process context */
1183 if (curproc == NULL)
1186 * nlookup_init() relies on namei_oc to be initialized,
1187 * but it's not when the netgraph module is loaded during boot.
1189 if (namei_oc == NULL)
1194 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1195 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1196 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1197 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1198 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");