5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Julian Elischer <julian@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_socket.c,v 1.11.2.6 2002/07/02 22:17:18 archie Exp $
40 * $DragonFly: src/sys/netgraph/socket/ng_socket.c,v 1.7 2004/03/06 01:58:55 hsu 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/param.h>
53 #include <sys/systm.h>
55 #include <sys/domain.h>
56 #include <sys/errno.h>
57 #include <sys/kernel.h>
58 #include <sys/filedesc.h>
59 #include <sys/malloc.h>
60 #include <sys/queue.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
69 #include <netgraph/ng_message.h>
70 #include <netgraph/netgraph.h>
71 #include "ng_socketvar.h"
72 #include "ng_socket.h"
75 * It's Ascii-art time!
76 * +-------------+ +-------------+
77 * |socket (ctl)| |socket (data)|
78 * +-------------+ +-------------+
82 * +-----------+ +-----------+
83 * |pcb (ctl)| |pcb (data)|
84 * +-----------+ +-----------+
88 * +--------------------------+
89 * | Socket type private |
91 * +--------------------------+
100 /* Netgraph node methods */
101 static ng_constructor_t ngs_constructor;
102 static ng_rcvmsg_t ngs_rcvmsg;
103 static ng_shutdown_t ngs_rmnode;
104 static ng_newhook_t ngs_newhook;
105 static ng_rcvdata_t ngs_rcvdata;
106 static ng_disconnect_t ngs_disconnect;
108 /* Internal methods */
109 static int ng_attach_data(struct socket *so);
110 static int ng_attach_cntl(struct socket *so);
111 static int ng_attach_common(struct socket *so, int type);
112 static void ng_detach_common(struct ngpcb *pcbp, int type);
113 /*static int ng_internalize(struct mbuf *m, struct thread *td); */
115 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
116 static int ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp);
117 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
119 static int ngs_mod_event(module_t mod, int event, void *data);
120 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
121 struct sockaddr_ng *addr);
123 /* Netgraph type descriptor */
124 static struct ng_type typestruct = {
139 NETGRAPH_INIT(socket, &typestruct);
142 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
143 static u_long ngpdg_recvspace = 20 * 1024;
145 /* List of all sockets */
146 LIST_HEAD(, ngpcb) ngsocklist;
148 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
150 /* If getting unexplained errors returned, set this to "Debugger("X"); */
155 /***************************************************************
157 ***************************************************************/
160 ngc_attach(struct socket *so, int proto, struct pru_attach_info *ai)
162 struct ngpcb *const pcbp = sotongpcb(so);
164 if (suser_cred(ai->p_ucred, NULL_CRED_OKAY) != 0)
168 return (ng_attach_cntl(so));
172 ngc_detach(struct socket *so)
174 struct ngpcb *const pcbp = sotongpcb(so);
178 ng_detach_common(pcbp, NG_CONTROL);
183 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
184 struct mbuf *control, struct thread *td)
186 struct ngpcb *const pcbp = sotongpcb(so);
187 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
188 struct ng_mesg *resp;
190 char *msg, *path = NULL;
198 if (control && (error = ng_internalize(control, p))) {
199 if (pcbp->sockdata == NULL) {
211 /* Require destination as there may be >= 1 hooks on this node */
213 error = EDESTADDRREQ;
217 /* Allocate an expendable buffer for the path, chop off
218 * the sockaddr header, and make sure it's NUL terminated */
219 len = sap->sg_len - 2;
220 MALLOC(path, char *, len + 1, M_NETGRAPH, M_WAITOK);
225 bcopy(sap->sg_data, path, len);
228 /* Move the actual message out of mbufs into a linear buffer.
229 * Start by adding up the size of the data. (could use mh_len?) */
230 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
233 /* Move the data into a linear buffer as well. Messages are not
234 * delivered in mbufs. */
235 MALLOC(msg, char *, len + 1, M_NETGRAPH, M_WAITOK);
240 m_copydata(m, 0, len, msg);
242 /* The callee will free the msg when done. The addr is our business. */
243 error = ng_send_msg(pcbp->sockdata->node,
244 (struct ng_mesg *) msg, path, &resp);
246 /* If the callee responded with a synchronous response, then put it
247 * back on the receive side of the socket; sap is source address. */
248 if (error == 0 && resp != NULL)
249 error = ship_msg(pcbp, resp, sap);
253 FREE(path, M_NETGRAPH);
262 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
264 struct ngpcb *const pcbp = sotongpcb(so);
268 return (ng_bind(nam, pcbp));
272 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
274 struct ngpcb *const pcbp = sotongpcb(so);
278 return (ng_connect_cntl(nam, pcbp));
281 /***************************************************************
283 ***************************************************************/
286 ngd_attach(struct socket *so, int proto, struct pru_attach_info *ai)
288 struct ngpcb *const pcbp = sotongpcb(so);
292 return (ng_attach_data(so));
296 ngd_detach(struct socket *so)
298 struct ngpcb *const pcbp = sotongpcb(so);
302 ng_detach_common(pcbp, NG_DATA);
307 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
308 struct mbuf *control, struct thread *td)
310 struct ngpcb *const pcbp = sotongpcb(so);
311 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
315 char hookname[NG_HOOKLEN + 1];
317 if ((pcbp == NULL) || (control != NULL)) {
321 if (pcbp->sockdata == NULL) {
326 * If the user used any of these ways to not specify an address
327 * then handle specially.
330 || ((len = sap->sg_len - 2) <= 0)
331 || (*sap->sg_data == '\0')) {
332 if (pcbp->sockdata->node->numhooks != 1) {
333 error = EDESTADDRREQ;
337 * if exactly one hook exists, just use it.
338 * Special case to allow write(2) to work on an ng_socket.
340 hook = LIST_FIRST(&pcbp->sockdata->node->hooks);
342 if (len > NG_HOOKLEN) {
348 * chop off the sockaddr header, and make sure it's NUL
351 bcopy(sap->sg_data, hookname, len);
352 hookname[len] = '\0';
354 /* Find the correct hook from 'hookname' */
355 LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
356 if (strcmp(hookname, hook->name) == 0)
360 error = EHOSTUNREACH;
363 /* Send data (OK if hook is NULL) */
364 NG_SEND_DATA(error, hook, m, mp); /* makes m NULL */
375 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
377 struct ngpcb *const pcbp = sotongpcb(so);
381 return (ng_connect_data(nam, pcbp));
385 * Used for both data and control sockets
388 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
391 struct sockaddr_ng *sg;
392 int sg_len, namelen, s;
394 /* Why isn't sg_data a `char[1]' ? :-( */
395 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
398 pcbp = sotongpcb(so);
399 if ((pcbp == 0) || (pcbp->sockdata == NULL)) {
404 namelen = 0; /* silence compiler ! */
406 if (pcbp->sockdata->node->name != NULL)
407 sg_len += namelen = strlen(pcbp->sockdata->node->name);
409 MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK);
412 if (pcbp->sockdata->node->name != NULL)
413 bcopy(pcbp->sockdata->node->name, sg->sg_data, namelen);
417 sg->sg_family = AF_NETGRAPH;
418 *addr = (struct sockaddr *)sg;
424 * Attach a socket to it's protocol specific partner.
425 * For a control socket, actually create a netgraph node and attach
430 ng_attach_cntl(struct socket *so)
432 struct ngsock *privdata;
436 /* Setup protocol control block */
437 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
439 pcbp = sotongpcb(so);
441 /* Allocate node private info */
442 MALLOC(privdata, struct ngsock *,
443 sizeof(*privdata), M_NETGRAPH, M_WAITOK);
444 if (privdata == NULL) {
445 ng_detach_common(pcbp, NG_CONTROL);
448 bzero(privdata, sizeof(*privdata));
450 /* Make the generic node components */
451 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
452 FREE(privdata, M_NETGRAPH);
453 ng_detach_common(pcbp, NG_CONTROL);
456 privdata->node->private = privdata;
458 /* Link the pcb and the node private data */
459 privdata->ctlsock = pcbp;
460 pcbp->sockdata = privdata;
466 ng_attach_data(struct socket *so)
468 return(ng_attach_common(so, NG_DATA));
472 * Set up a socket protocol control block.
473 * This code is shared between control and data sockets.
476 ng_attach_common(struct socket *so, int type)
481 /* Standard socket setup stuff */
482 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL);
486 /* Allocate the pcb */
487 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK);
490 bzero(pcbp, sizeof(*pcbp));
493 /* Link the pcb and the socket */
494 so->so_pcb = (caddr_t) pcbp;
495 pcbp->ng_socket = so;
497 /* Add the socket to linked list */
498 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
503 * Disassociate the socket from it's protocol specific
504 * partner. If it's attached to a node's private data structure,
505 * then unlink from that too. If we were the last socket attached to it,
506 * then shut down the entire node. Shared code for control and data sockets.
509 ng_detach_common(struct ngpcb *pcbp, int which)
511 struct ngsock *sockdata;
513 if (pcbp->sockdata) {
514 sockdata = pcbp->sockdata;
515 pcbp->sockdata = NULL;
518 sockdata->ctlsock = NULL;
521 sockdata->datasock = NULL;
526 if ((--sockdata->refs == 0) && (sockdata->node != NULL))
527 ng_rmnode(sockdata->node);
529 pcbp->ng_socket->so_pcb = NULL;
530 pcbp->ng_socket = NULL;
531 LIST_REMOVE(pcbp, socks);
537 * File descriptors can be passed into a AF_NETGRAPH socket.
538 * Note, that file descriptors cannot be passed OUT.
539 * Only character device descriptors are accepted.
540 * Character devices are useful to connect a graph to a device,
541 * which after all is the purpose of this whole system.
544 ng_internalize(struct mbuf *control, struct thread *td)
546 struct filedesc *fdp = p->p_fd;
547 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
553 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
554 cm->cmsg_len != control->m_len) {
559 /* Check there is only one FD. XXX what would more than one signify? */
560 oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
566 /* Check that the FD given is legit. and change it to a pointer to a
568 fd = *(int *) (cm + 1);
569 if ((unsigned) fd >= fdp->fd_nfiles
570 || (fp = fdp->fd_ofiles[fd]) == NULL) {
574 /* Depending on what kind of resource it is, act differently. For
575 * devices, we treat it as a file. For a AF_NETGRAPH socket,
576 * shortcut straight to the node. */
577 switch (fp->f_type) {
579 vn = (struct vnode *) fp->f_data;
580 if (vn && (vn->v_type == VCHR)) {
581 /* for a VCHR, actually reference the FILE */
583 /* XXX then what :) */
584 /* how to pass on to other modules? */
599 * Connect the data socket to a named control socket node.
602 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
604 struct sockaddr_ng *sap;
606 struct ngsock *sockdata;
609 /* If we are already connected, don't do it again */
610 if (pcbp->sockdata != NULL)
613 /* Find the target (victim) and check it doesn't already have a data
614 * socket. Also check it is a 'socket' type node. */
615 sap = (struct sockaddr_ng *) nam;
616 if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
619 if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
621 sockdata = farnode->private;
622 if (sockdata->datasock != NULL)
625 /* Link the PCB and the private data struct. and note the extra
627 sockdata->datasock = pcbp;
628 pcbp->sockdata = sockdata;
634 * Connect the existing control socket node to a named node:hook.
635 * The hook we use on this end is the same name as the remote node name.
638 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
640 struct ngsock *const sockdata = pcbp->sockdata;
641 struct sockaddr_ng *sap;
646 sap = (struct sockaddr_ng *) nam;
647 rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
648 if (rtn < 0 || node == NULL || hook == NULL) {
652 farnode = ng_findname(sockdata->node, node);
653 if (farnode == NULL) {
655 return (EADDRNOTAVAIL);
658 /* Connect, using a hook name the same as the far node name. */
659 error = ng_con_nodes(sockdata->node, node, farnode, hook);
664 * Binding a socket means giving the corresponding node a name
667 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
669 struct ngsock *const sockdata = pcbp->sockdata;
670 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
672 if (sockdata == NULL) {
676 if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
680 return (ng_name_node(sockdata->node, sap->sg_data));
684 * Take a message and pass it up to the control socket associated
688 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
690 struct socket *const so = pcbp->ng_socket;
694 /* Copy the message itself into an mbuf chain */
695 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
696 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
698 /* Here we free the message, as we are the end of the line.
699 * We need to do that regardless of whether we got mbufs. */
700 FREE(msg, M_NETGRAPH);
707 /* Send it up to the socket */
708 if (sbappendaddr(&so->so_rcv,
709 (struct sockaddr *) addr, mdata, NULL) == 0) {
719 * You can only create new nodes from the socket end of things.
722 ngs_constructor(node_p *nodep)
728 * We allow any hook to be connected to the node.
729 * There is no per-hook private information though.
732 ngs_newhook(node_p node, hook_p hook, const char *name)
734 hook->private = node->private;
739 * Incoming messages get passed up to the control socket.
740 * Unless they are for us specifically (socket_type)
743 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
744 struct ng_mesg **resp)
746 struct ngsock *const sockdata = node->private;
747 struct ngpcb *const pcbp = sockdata->ctlsock;
748 struct sockaddr_ng *addr;
752 /* Only allow mesgs to be passed if we have the control socket.
753 * Data sockets can only support the generic messages. */
759 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
760 switch (msg->header.cmd) {
761 case NGM_SOCK_CMD_NOLINGER:
762 sockdata->flags |= NGS_FLAG_NOLINGER;
764 case NGM_SOCK_CMD_LINGER:
765 sockdata->flags &= ~NGS_FLAG_NOLINGER;
768 error = EINVAL; /* unknown command */
770 /* Free the message and return */
771 FREE(msg, M_NETGRAPH);
775 /* Get the return address into a sockaddr */
776 if ((retaddr == NULL) || (*retaddr == '\0'))
778 addrlen = strlen(retaddr);
779 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH, M_NOWAIT);
784 addr->sg_len = addrlen + 3;
785 addr->sg_family = AF_NETGRAPH;
786 bcopy(retaddr, addr->sg_data, addrlen);
787 addr->sg_data[addrlen] = '\0';
790 error = ship_msg(pcbp, msg, addr);
791 FREE(addr, M_NETGRAPH);
796 * Receive data on a hook
799 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
801 struct ngsock *const sockdata = hook->node->private;
802 struct ngpcb *const pcbp = sockdata->datasock;
804 struct sockaddr_ng *addr;
805 char *addrbuf[NG_HOOKLEN + 1 + 4];
808 /* If there is no data socket, black-hole it */
810 NG_FREE_DATA(m, meta);
813 so = pcbp->ng_socket;
815 /* Get the return address into a sockaddr. */
816 addrlen = strlen(hook->name); /* <= NG_HOOKLEN */
817 addr = (struct sockaddr_ng *) addrbuf;
818 addr->sg_len = addrlen + 3;
819 addr->sg_family = AF_NETGRAPH;
820 bcopy(hook->name, addr->sg_data, addrlen);
821 addr->sg_data[addrlen] = '\0';
823 /* We have no use for the meta data, free/clear it now. */
826 /* Try to tell the socket which hook it came in on */
827 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
839 * For this type, removal of the last link destroys the node
840 * if the NOLINGER flag is set.
843 ngs_disconnect(hook_p hook)
845 struct ngsock *const sockdata = hook->node->private;
847 if ((sockdata->flags & NGS_FLAG_NOLINGER )
848 && (hook->node->numhooks == 0)) {
849 ng_rmnode(hook->node);
855 * Do local shutdown processing.
856 * In this case, that involves making sure the socket
857 * knows we should be shutting down.
860 ngs_rmnode(node_p node)
862 struct ngsock *const sockdata = node->private;
863 struct ngpcb *const dpcbp = sockdata->datasock;
864 struct ngpcb *const pcbp = sockdata->ctlsock;
870 soisdisconnected(dpcbp->ng_socket);
871 dpcbp->sockdata = NULL;
872 sockdata->datasock = NULL;
876 soisdisconnected(pcbp->ng_socket);
877 pcbp->sockdata = NULL;
878 sockdata->ctlsock = NULL;
881 node->private = NULL;
883 FREE(sockdata, M_NETGRAPH);
888 * Control and data socket type descriptors
891 static struct pr_usrreqs ngc_usrreqs = {
897 pru_connect2_notsupp,
900 NULL, /* disconnect */
902 NULL, /* setpeeraddr */
914 static struct pr_usrreqs ngd_usrreqs = {
920 pru_connect2_notsupp,
923 NULL, /* disconnect */
925 NULL, /* setpeeraddr */
938 * Definitions of protocols supported in the NETGRAPH domain.
941 extern struct domain ngdomain; /* stop compiler warnings */
943 static struct protosw ngsw[] = {
948 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
966 struct domain ngdomain = {
973 &ngsw[sizeof(ngsw) / sizeof(ngsw[0])],
981 * Handle loading and unloading for this node type
982 * This is to handle auxiliary linkages (e.g protocol domain addition).
985 ngs_mod_event(module_t mod, int event, void *data)
991 /* Register protocol domain */
992 net_add_domain(&ngdomain);
995 /* Insure there are no open netgraph sockets */
996 if (!LIST_EMPTY(&ngsocklist)) {
1002 /* Unregister protocol domain XXX can't do this yet.. */
1003 if ((error = net_rm_domain(&ngdomain)) != 0)
1016 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1017 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1018 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1019 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1020 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");