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@whistle.com>
40 * $Whistle: ng_socket.c,v 1.25 1999/01/28 23:54:54 julian Exp $
44 * Netgraph socket nodes
46 * There are two types of netgraph sockets, control and data.
47 * Control sockets have a netgraph node, but data sockets are
48 * parasitic on control sockets, and have no node of their own.
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/domain.h>
55 #include <sys/errno.h>
56 #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 <netgraph/ng_socket.h>
72 #include <netgraph/ng_socketvar.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 int ngs_constructor(node_p *nodep);
102 static int ngs_rcvmsg(node_p node, struct ng_mesg *msg,
103 const char *retaddr, struct ng_mesg **resp);
104 static int ngs_rmnode(node_p node);
105 static int ngs_newhook(node_p node, hook_p hook, const char *name);
106 static int ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta);
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 proc *p); */
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 = {
138 NETGRAPH_INIT(socket, &typestruct);
141 static u_long ngpdg_sendspace = 2 * 1024; /* really max datagram size */
142 static u_long ngpdg_recvspace = 20 * 1024;
144 /* List of all sockets */
145 LIST_HEAD(, ngpcb) ngsocklist;
147 #define sotongpcb(so) ((struct ngpcb *)so->so_pcb)
149 /* If getting unexplained errors returned, set this to "Debugger("X"); */
154 /***************************************************************
156 ***************************************************************/
159 ngc_attach(struct socket *so, int proto, struct proc *p)
161 struct ngpcb *const pcbp = sotongpcb(so);
165 return (ng_attach_cntl(so));
169 ngc_detach(struct socket *so)
171 struct ngpcb *const pcbp = sotongpcb(so);
175 ng_detach_common(pcbp, NG_CONTROL);
180 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
181 struct mbuf *control, struct proc *p)
183 struct ngpcb *const pcbp = sotongpcb(so);
184 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
185 struct ng_mesg *resp;
187 char *msg, *path = NULL;
195 if (control && (error = ng_internalize(control, p))) {
196 if (pcbp->sockdata == NULL) {
208 /* Require destination as there may be >= 1 hooks on this node */
210 error = EDESTADDRREQ;
214 /* Allocate an expendable buffer for the path, chop off
215 * the sockaddr header, and make sure it's NUL terminated */
216 len = sap->sg_len - 2;
217 MALLOC(path, char *, len + 1, M_NETGRAPH, M_WAITOK);
222 bcopy(sap->sg_data, path, len);
225 /* Move the actual message out of mbufs into a linear buffer.
226 * Start by adding up the size of the data. (could use mh_len?) */
227 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
230 /* Move the data into a linear buffer as well. Messages are not
231 * delivered in mbufs. */
232 MALLOC(msg, char *, len + 1, M_NETGRAPH, M_WAITOK);
237 m_copydata(m, 0, len, msg);
239 /* The callee will free the msg when done. The addr is our business. */
240 error = ng_send_msg(pcbp->sockdata->node,
241 (struct ng_mesg *) msg, path, &resp);
243 /* If the callee responded with a synchronous response, then put it
244 * back on the receive side of the socket; sap is source address. */
245 if (error == 0 && resp != NULL)
246 error = ship_msg(pcbp, resp, sap);
250 FREE(path, M_NETGRAPH);
259 ngc_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
261 struct ngpcb *const pcbp = sotongpcb(so);
265 return (ng_bind(nam, pcbp));
269 ngc_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
271 struct ngpcb *const pcbp = sotongpcb(so);
275 return (ng_connect_cntl(nam, pcbp));
278 /***************************************************************
280 ***************************************************************/
283 ngd_attach(struct socket *so, int proto, struct proc *p)
285 struct ngpcb *const pcbp = sotongpcb(so);
289 return (ng_attach_data(so));
293 ngd_detach(struct socket *so)
295 struct ngpcb *const pcbp = sotongpcb(so);
299 ng_detach_common(pcbp, NG_DATA);
304 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
305 struct mbuf *control, struct proc *p)
307 struct ngpcb *const pcbp = sotongpcb(so);
308 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
309 char *hookname = NULL;
314 if ((pcbp == NULL) || (control != NULL)) {
318 if (pcbp->sockdata == NULL) {
323 error = EDESTADDRREQ;
327 /* Allocate an expendable buffer for the hook name, chop off
328 * the sockaddr header, and make sure it's NUL terminated */
329 len = sap->sg_len - 2;
330 MALLOC(hookname, char *, len + 1, M_NETGRAPH, M_WAITOK);
331 if (hookname == NULL) {
335 bcopy(sap->sg_data, hookname, len);
336 hookname[len] = '\0';
338 /* Find the correct hook from 'hookname' */
339 LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
340 if (strcmp(hookname, hook->name) == 0)
344 /* Send data (OK if hook is NULL) */
345 NG_SEND_DATA(error, hook, m, mp); /* makes m NULL */
348 if (hookname != NULL)
349 FREE(hookname, M_NETGRAPH);
358 ngd_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
360 struct ngpcb *const pcbp = sotongpcb(so);
364 return (ng_connect_data(nam, pcbp));
368 * Used for both data and control sockets
371 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
373 struct ngpcb *const pcbp = sotongpcb(so);
379 if (pcbp->sockdata->node->name != NULL) {
380 namelen = strlen(pcbp->sockdata->node->name) + 3;
381 MALLOC(sa, struct sockaddr *, namelen, M_SONAME, M_WAITOK);
384 sa->sa_family = AF_NETGRAPH;
385 sa->sa_len = namelen;
386 strcpy(sa->sa_data, pcbp->sockdata->node->name);
389 *addr = NULL; /* XXX check this makes sense */
394 * Attach a socket to it's protocol specific partner.
395 * For a control socket, actually create a netgraph node and attach
400 ng_attach_cntl(struct socket *so)
402 struct ngsock *privdata;
406 /* Setup protocol control block */
407 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
409 pcbp = (struct ngpcb *) so->so_pcb;
411 /* Allocate node private info */
412 MALLOC(privdata, struct ngsock *,
413 sizeof(*privdata), M_NETGRAPH, M_WAITOK);
414 if (privdata == NULL) {
415 ng_detach_common(pcbp, NG_CONTROL);
418 bzero(privdata, sizeof(*privdata));
420 /* Make the generic node components */
421 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
422 FREE(privdata, M_NETGRAPH);
423 ng_detach_common(pcbp, NG_CONTROL);
426 privdata->node->private = privdata;
428 /* Link the pcb and the node private data */
429 privdata->ctlsock = pcbp;
430 pcbp->sockdata = privdata;
436 ng_attach_data(struct socket *so)
438 return(ng_attach_common(so, NG_DATA));
442 * Set up a socket protocol control block.
443 * This code is shared between control and data sockets.
446 ng_attach_common(struct socket *so, int type)
451 /* Standard socket setup stuff */
452 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
456 /* Allocate the pcb */
457 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK);
460 bzero(pcbp, sizeof(*pcbp));
463 /* Link the pcb and the socket */
464 so->so_pcb = (caddr_t) pcbp;
465 pcbp->ng_socket = so;
467 /* Add the socket to linked list */
468 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
473 * Disassociate the socket from it's protocol specific
474 * partner. If it's attached to a node's private data structure,
475 * then unlink from that too. If we were the last socket attached to it,
476 * then shut down the entire node. Shared code for control and data sockets.
479 ng_detach_common(struct ngpcb *pcbp, int which)
481 struct ngsock *sockdata;
483 if (pcbp->sockdata) {
484 sockdata = pcbp->sockdata;
485 pcbp->sockdata = NULL;
488 sockdata->ctlsock = NULL;
491 sockdata->datasock = NULL;
496 if ((--sockdata->refs == 0) && (sockdata->node != NULL))
497 ng_rmnode(sockdata->node);
499 pcbp->ng_socket->so_pcb = NULL;
500 pcbp->ng_socket = NULL;
501 LIST_REMOVE(pcbp, socks);
507 * File descriptors can be passed into a AF_NETGRAPH socket.
508 * Note, that file descriptors cannot be passed OUT.
509 * Only character device descriptors are accepted.
510 * Character devices are useful to connect a graph to a device,
511 * which after all is the purpose of this whole system.
514 ng_internalize(struct mbuf *control, struct proc *p)
516 struct filedesc *fdp = p->p_fd;
517 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
523 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
524 cm->cmsg_len != control->m_len) {
529 /* Check there is only one FD. XXX what would more than one signify? */
530 oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
536 /* Check that the FD given is legit. and change it to a pointer to a
538 fd = *(int *) (cm + 1);
539 if ((unsigned) fd >= fdp->fd_nfiles
540 || (fp = fdp->fd_ofiles[fd]) == NULL) {
544 /* Depending on what kind of resource it is, act differently. For
545 * devices, we treat it as a file. For a AF_NETGRAPH socket,
546 * shortcut straight to the node. */
547 switch (fp->f_type) {
549 vn = (struct vnode *) fp->f_data;
550 if (vn && (vn->v_type == VCHR)) {
551 /* for a VCHR, actually reference the FILE */
553 /* XXX then what :) */
554 /* how to pass on to other modules? */
569 * Connect the data socket to a named control socket node.
572 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
574 struct sockaddr_ng *sap;
576 struct ngsock *sockdata;
579 /* If we are already connected, don't do it again */
580 if (pcbp->sockdata != NULL)
583 /* Find the target (victim) and check it doesn't already have a data
584 * socket. Also check it is a 'socket' type node. */
585 sap = (struct sockaddr_ng *) nam;
586 if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
589 if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
591 sockdata = farnode->private;
592 if (sockdata->datasock != NULL)
595 /* Link the PCB and the private data struct. and note the extra
597 sockdata->datasock = pcbp;
598 pcbp->sockdata = sockdata;
604 * Connect the existing control socket node to a named node:hook.
605 * The hook we use on this end is the same name as the remote node name.
608 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
610 struct ngsock *const sockdata = pcbp->sockdata;
611 struct sockaddr_ng *sap;
616 sap = (struct sockaddr_ng *) nam;
617 rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
618 if (rtn < 0 || node == NULL || hook == NULL) {
622 farnode = ng_findname(sockdata->node, node);
623 if (farnode == NULL) {
625 return (EADDRNOTAVAIL);
628 /* Connect, using a hook name the same as the far node name. */
629 error = ng_con_nodes(sockdata->node, node, farnode, hook);
634 * Binding a socket means giving the corresponding node a name
637 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
639 struct ngsock *const sockdata = pcbp->sockdata;
640 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
642 if (sockdata == NULL) {
646 if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
650 return (ng_name_node(sockdata->node, sap->sg_data));
654 * Take a message and pass it up to the control socket associated
658 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
660 struct socket *const so = pcbp->ng_socket;
664 /* Copy the message itself into an mbuf chain */
665 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
666 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
668 /* Here we free the message, as we are the end of the line.
669 * We need to do that regardless of whether we got mbufs. */
670 FREE(msg, M_NETGRAPH);
677 /* Send it up to the socket */
678 if (sbappendaddr(&so->so_rcv,
679 (struct sockaddr *) addr, mdata, NULL) == 0) {
689 * You can only create new nodes from the socket end of things.
692 ngs_constructor(node_p *nodep)
698 * We allow any hook to be connected to the node.
699 * There is no per-hook private information though.
702 ngs_newhook(node_p node, hook_p hook, const char *name)
704 hook->private = node->private;
709 * Incoming messages get passed up to the control socket.
712 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
713 struct ng_mesg **resp)
715 struct ngsock *const sockdata = node->private;
716 struct ngpcb *const pcbp = sockdata->ctlsock;
717 struct sockaddr_ng *addr;
721 /* Only allow mesgs to be passed if we have the control socket.
722 * Data sockets can only support the generic messages. */
728 /* Get the return address into a sockaddr */
729 if ((retaddr == NULL) || (*retaddr == '\0'))
731 addrlen = strlen(retaddr);
732 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH, M_NOWAIT);
737 addr->sg_len = addrlen + 3;
738 addr->sg_family = AF_NETGRAPH;
739 bcopy(retaddr, addr->sg_data, addrlen);
740 addr->sg_data[addrlen] = '\0';
743 error = ship_msg(pcbp, msg, addr);
744 FREE(addr, M_NETGRAPH);
749 * Receive data on a hook
752 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
754 struct ngsock *const sockdata = hook->node->private;
755 struct ngpcb *const pcbp = sockdata->datasock;
757 struct sockaddr_ng *addr;
758 char *addrbuf[NG_HOOKLEN + 1 + 4];
761 /* If there is no data socket, black-hole it */
763 NG_FREE_DATA(m, meta);
766 so = pcbp->ng_socket;
768 /* Get the return address into a sockaddr. */
769 addrlen = strlen(hook->name); /* <= NG_HOOKLEN */
770 addr = (struct sockaddr_ng *) addrbuf;
771 addr->sg_len = addrlen + 3;
772 addr->sg_family = AF_NETGRAPH;
773 bcopy(hook->name, addr->sg_data, addrlen);
774 addr->sg_data[addrlen] = '\0';
776 /* We have no use for the meta data, free/clear it now. */
779 /* Try to tell the socket which hook it came in on */
780 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
790 * Do local shutdown processing.
791 * In this case, that involves making sure the socket
792 * knows we should be shutting down.
795 ngs_rmnode(node_p node)
797 struct ngsock *const sockdata = node->private;
798 struct ngpcb *const dpcbp = sockdata->datasock;
799 struct ngpcb *const pcbp = sockdata->ctlsock;
805 soisdisconnected(dpcbp->ng_socket);
806 dpcbp->sockdata = NULL;
807 sockdata->datasock = NULL;
811 soisdisconnected(pcbp->ng_socket);
812 pcbp->sockdata = NULL;
813 sockdata->ctlsock = NULL;
816 node->private = NULL;
818 FREE(sockdata, M_NETGRAPH);
823 * Control and data socket type descriptors
826 static struct pr_usrreqs ngc_usrreqs = {
832 pru_connect2_notsupp,
835 NULL, /* disconnect */
837 NULL, /* setpeeraddr */
849 static struct pr_usrreqs ngd_usrreqs = {
855 pru_connect2_notsupp,
858 NULL, /* disconnect */
860 NULL, /* setpeeraddr */
873 * Definitions of protocols supported in the NETGRAPH domain.
876 extern struct domain ngdomain; /* stop compiler warnings */
878 static struct protosw ngsw[] = {
883 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
901 struct domain ngdomain = {
908 &ngsw[sizeof(ngsw) / sizeof(ngsw[0])],
916 * Handle loading and unloading for this node type
917 * This is to handle auxiliary linkages (e.g protocol domain addition).
920 ngs_mod_event(module_t mod, int event, void *data)
926 /* Register protocol domain */
927 net_add_domain(&ngdomain);
930 /* Insure there are no open netgraph sockets */
931 if (!LIST_EMPTY(&ngsocklist)) {
937 /* Unregister protocol domain XXX can't do this yet.. */
938 if ((error = net_rm_domain(&ngdomain)) != 0)
951 SYSCTL_NODE(_net, AF_NETGRAPH, graph, CTLFLAG_RW, 0, "netgraph Family");
952 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
953 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
954 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
955 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
956 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");