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 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 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>
57 #include <sys/filedesc.h>
58 #include <sys/malloc.h>
59 #include <sys/queue.h>
61 #include <sys/protosw.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
68 #include <netgraph/ng_message.h>
69 #include <netgraph/netgraph.h>
70 #include <netgraph/ng_socketvar.h>
71 #include <netgraph/ng_socket.h>
74 * It's Ascii-art time!
75 * +-------------+ +-------------+
76 * |socket (ctl)| |socket (data)|
77 * +-------------+ +-------------+
81 * +-----------+ +-----------+
82 * |pcb (ctl)| |pcb (data)|
83 * +-----------+ +-----------+
87 * +--------------------------+
88 * | Socket type private |
90 * +--------------------------+
99 /* Netgraph node methods */
100 static ng_constructor_t ngs_constructor;
101 static ng_rcvmsg_t ngs_rcvmsg;
102 static ng_shutdown_t ngs_rmnode;
103 static ng_newhook_t ngs_newhook;
104 static ng_rcvdata_t ngs_rcvdata;
105 static ng_disconnect_t ngs_disconnect;
107 /* Internal methods */
108 static int ng_attach_data(struct socket *so);
109 static int ng_attach_cntl(struct socket *so);
110 static int ng_attach_common(struct socket *so, int type);
111 static void ng_detach_common(struct ngpcb *pcbp, int type);
112 /*static int ng_internalize(struct mbuf *m, struct proc *p); */
114 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
115 static int ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp);
116 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
118 static int ngs_mod_event(module_t mod, int event, void *data);
119 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
120 struct sockaddr_ng *addr);
122 /* Netgraph type descriptor */
123 static struct ng_type typestruct = {
138 NETGRAPH_INIT(socket, &typestruct);
141 static u_long ngpdg_sendspace = 20 * 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);
167 return (ng_attach_cntl(so));
171 ngc_detach(struct socket *so)
173 struct ngpcb *const pcbp = sotongpcb(so);
177 ng_detach_common(pcbp, NG_CONTROL);
182 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
183 struct mbuf *control, struct proc *p)
185 struct ngpcb *const pcbp = sotongpcb(so);
186 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
187 struct ng_mesg *resp;
189 char *msg, *path = NULL;
197 if (control && (error = ng_internalize(control, p))) {
198 if (pcbp->sockdata == NULL) {
210 /* Require destination as there may be >= 1 hooks on this node */
212 error = EDESTADDRREQ;
216 /* Allocate an expendable buffer for the path, chop off
217 * the sockaddr header, and make sure it's NUL terminated */
218 len = sap->sg_len - 2;
219 MALLOC(path, char *, len + 1, M_NETGRAPH, M_WAITOK);
224 bcopy(sap->sg_data, path, len);
227 /* Move the actual message out of mbufs into a linear buffer.
228 * Start by adding up the size of the data. (could use mh_len?) */
229 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
232 /* Move the data into a linear buffer as well. Messages are not
233 * delivered in mbufs. */
234 MALLOC(msg, char *, len + 1, M_NETGRAPH, M_WAITOK);
239 m_copydata(m, 0, len, msg);
241 /* The callee will free the msg when done. The addr is our business. */
242 error = ng_send_msg(pcbp->sockdata->node,
243 (struct ng_mesg *) msg, path, &resp);
245 /* If the callee responded with a synchronous response, then put it
246 * back on the receive side of the socket; sap is source address. */
247 if (error == 0 && resp != NULL)
248 error = ship_msg(pcbp, resp, sap);
252 FREE(path, M_NETGRAPH);
261 ngc_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
263 struct ngpcb *const pcbp = sotongpcb(so);
267 return (ng_bind(nam, pcbp));
271 ngc_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
273 struct ngpcb *const pcbp = sotongpcb(so);
277 return (ng_connect_cntl(nam, pcbp));
280 /***************************************************************
282 ***************************************************************/
285 ngd_attach(struct socket *so, int proto, struct proc *p)
287 struct ngpcb *const pcbp = sotongpcb(so);
291 return (ng_attach_data(so));
295 ngd_detach(struct socket *so)
297 struct ngpcb *const pcbp = sotongpcb(so);
301 ng_detach_common(pcbp, NG_DATA);
306 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
307 struct mbuf *control, struct proc *p)
309 struct ngpcb *const pcbp = sotongpcb(so);
310 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
314 char hookname[NG_HOOKLEN + 1];
316 if ((pcbp == NULL) || (control != NULL)) {
320 if (pcbp->sockdata == NULL) {
325 * If the user used any of these ways to not specify an address
326 * then handle specially.
329 || ((len = sap->sg_len - 2) <= 0)
330 || (*sap->sg_data == '\0')) {
331 if (pcbp->sockdata->node->numhooks != 1) {
332 error = EDESTADDRREQ;
336 * if exactly one hook exists, just use it.
337 * Special case to allow write(2) to work on an ng_socket.
339 hook = LIST_FIRST(&pcbp->sockdata->node->hooks);
341 if (len > NG_HOOKLEN) {
347 * chop off the sockaddr header, and make sure it's NUL
350 bcopy(sap->sg_data, hookname, len);
351 hookname[len] = '\0';
353 /* Find the correct hook from 'hookname' */
354 LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
355 if (strcmp(hookname, hook->name) == 0)
359 error = EHOSTUNREACH;
362 /* Send data (OK if hook is NULL) */
363 NG_SEND_DATA(error, hook, m, mp); /* makes m NULL */
374 ngd_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
376 struct ngpcb *const pcbp = sotongpcb(so);
380 return (ng_connect_data(nam, pcbp));
384 * Used for both data and control sockets
387 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
390 struct sockaddr_ng *sg;
391 int sg_len, namelen, s;
393 /* Why isn't sg_data a `char[1]' ? :-( */
394 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
397 pcbp = sotongpcb(so);
398 if ((pcbp == 0) || (pcbp->sockdata == NULL)) {
403 namelen = 0; /* silence compiler ! */
405 if (pcbp->sockdata->node->name != NULL)
406 sg_len += namelen = strlen(pcbp->sockdata->node->name);
408 MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK);
411 if (pcbp->sockdata->node->name != NULL)
412 bcopy(pcbp->sockdata->node->name, sg->sg_data, namelen);
416 sg->sg_family = AF_NETGRAPH;
417 *addr = (struct sockaddr *)sg;
423 * Attach a socket to it's protocol specific partner.
424 * For a control socket, actually create a netgraph node and attach
429 ng_attach_cntl(struct socket *so)
431 struct ngsock *privdata;
435 /* Setup protocol control block */
436 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
438 pcbp = sotongpcb(so);
440 /* Allocate node private info */
441 MALLOC(privdata, struct ngsock *,
442 sizeof(*privdata), M_NETGRAPH, M_WAITOK);
443 if (privdata == NULL) {
444 ng_detach_common(pcbp, NG_CONTROL);
447 bzero(privdata, sizeof(*privdata));
449 /* Make the generic node components */
450 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
451 FREE(privdata, M_NETGRAPH);
452 ng_detach_common(pcbp, NG_CONTROL);
455 privdata->node->private = privdata;
457 /* Link the pcb and the node private data */
458 privdata->ctlsock = pcbp;
459 pcbp->sockdata = privdata;
465 ng_attach_data(struct socket *so)
467 return(ng_attach_common(so, NG_DATA));
471 * Set up a socket protocol control block.
472 * This code is shared between control and data sockets.
475 ng_attach_common(struct socket *so, int type)
480 /* Standard socket setup stuff */
481 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
485 /* Allocate the pcb */
486 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK);
489 bzero(pcbp, sizeof(*pcbp));
492 /* Link the pcb and the socket */
493 so->so_pcb = (caddr_t) pcbp;
494 pcbp->ng_socket = so;
496 /* Add the socket to linked list */
497 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
502 * Disassociate the socket from it's protocol specific
503 * partner. If it's attached to a node's private data structure,
504 * then unlink from that too. If we were the last socket attached to it,
505 * then shut down the entire node. Shared code for control and data sockets.
508 ng_detach_common(struct ngpcb *pcbp, int which)
510 struct ngsock *sockdata;
512 if (pcbp->sockdata) {
513 sockdata = pcbp->sockdata;
514 pcbp->sockdata = NULL;
517 sockdata->ctlsock = NULL;
520 sockdata->datasock = NULL;
525 if ((--sockdata->refs == 0) && (sockdata->node != NULL))
526 ng_rmnode(sockdata->node);
528 pcbp->ng_socket->so_pcb = NULL;
529 pcbp->ng_socket = NULL;
530 LIST_REMOVE(pcbp, socks);
536 * File descriptors can be passed into a AF_NETGRAPH socket.
537 * Note, that file descriptors cannot be passed OUT.
538 * Only character device descriptors are accepted.
539 * Character devices are useful to connect a graph to a device,
540 * which after all is the purpose of this whole system.
543 ng_internalize(struct mbuf *control, struct proc *p)
545 struct filedesc *fdp = p->p_fd;
546 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
552 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
553 cm->cmsg_len != control->m_len) {
558 /* Check there is only one FD. XXX what would more than one signify? */
559 oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
565 /* Check that the FD given is legit. and change it to a pointer to a
567 fd = *(int *) (cm + 1);
568 if ((unsigned) fd >= fdp->fd_nfiles
569 || (fp = fdp->fd_ofiles[fd]) == NULL) {
573 /* Depending on what kind of resource it is, act differently. For
574 * devices, we treat it as a file. For a AF_NETGRAPH socket,
575 * shortcut straight to the node. */
576 switch (fp->f_type) {
578 vn = (struct vnode *) fp->f_data;
579 if (vn && (vn->v_type == VCHR)) {
580 /* for a VCHR, actually reference the FILE */
582 /* XXX then what :) */
583 /* how to pass on to other modules? */
598 * Connect the data socket to a named control socket node.
601 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
603 struct sockaddr_ng *sap;
605 struct ngsock *sockdata;
608 /* If we are already connected, don't do it again */
609 if (pcbp->sockdata != NULL)
612 /* Find the target (victim) and check it doesn't already have a data
613 * socket. Also check it is a 'socket' type node. */
614 sap = (struct sockaddr_ng *) nam;
615 if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
618 if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
620 sockdata = farnode->private;
621 if (sockdata->datasock != NULL)
624 /* Link the PCB and the private data struct. and note the extra
626 sockdata->datasock = pcbp;
627 pcbp->sockdata = sockdata;
633 * Connect the existing control socket node to a named node:hook.
634 * The hook we use on this end is the same name as the remote node name.
637 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
639 struct ngsock *const sockdata = pcbp->sockdata;
640 struct sockaddr_ng *sap;
645 sap = (struct sockaddr_ng *) nam;
646 rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
647 if (rtn < 0 || node == NULL || hook == NULL) {
651 farnode = ng_findname(sockdata->node, node);
652 if (farnode == NULL) {
654 return (EADDRNOTAVAIL);
657 /* Connect, using a hook name the same as the far node name. */
658 error = ng_con_nodes(sockdata->node, node, farnode, hook);
663 * Binding a socket means giving the corresponding node a name
666 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
668 struct ngsock *const sockdata = pcbp->sockdata;
669 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
671 if (sockdata == NULL) {
675 if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
679 return (ng_name_node(sockdata->node, sap->sg_data));
683 * Take a message and pass it up to the control socket associated
687 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
689 struct socket *const so = pcbp->ng_socket;
693 /* Copy the message itself into an mbuf chain */
694 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
695 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
697 /* Here we free the message, as we are the end of the line.
698 * We need to do that regardless of whether we got mbufs. */
699 FREE(msg, M_NETGRAPH);
706 /* Send it up to the socket */
707 if (sbappendaddr(&so->so_rcv,
708 (struct sockaddr *) addr, mdata, NULL) == 0) {
718 * You can only create new nodes from the socket end of things.
721 ngs_constructor(node_p *nodep)
727 * We allow any hook to be connected to the node.
728 * There is no per-hook private information though.
731 ngs_newhook(node_p node, hook_p hook, const char *name)
733 hook->private = node->private;
738 * Incoming messages get passed up to the control socket.
739 * Unless they are for us specifically (socket_type)
742 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
743 struct ng_mesg **resp)
745 struct ngsock *const sockdata = node->private;
746 struct ngpcb *const pcbp = sockdata->ctlsock;
747 struct sockaddr_ng *addr;
751 /* Only allow mesgs to be passed if we have the control socket.
752 * Data sockets can only support the generic messages. */
758 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
759 switch (msg->header.cmd) {
760 case NGM_SOCK_CMD_NOLINGER:
761 sockdata->flags |= NGS_FLAG_NOLINGER;
763 case NGM_SOCK_CMD_LINGER:
764 sockdata->flags &= ~NGS_FLAG_NOLINGER;
767 error = EINVAL; /* unknown command */
769 /* Free the message and return */
770 FREE(msg, M_NETGRAPH);
774 /* Get the return address into a sockaddr */
775 if ((retaddr == NULL) || (*retaddr == '\0'))
777 addrlen = strlen(retaddr);
778 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH, M_NOWAIT);
783 addr->sg_len = addrlen + 3;
784 addr->sg_family = AF_NETGRAPH;
785 bcopy(retaddr, addr->sg_data, addrlen);
786 addr->sg_data[addrlen] = '\0';
789 error = ship_msg(pcbp, msg, addr);
790 FREE(addr, M_NETGRAPH);
795 * Receive data on a hook
798 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
800 struct ngsock *const sockdata = hook->node->private;
801 struct ngpcb *const pcbp = sockdata->datasock;
803 struct sockaddr_ng *addr;
804 char *addrbuf[NG_HOOKLEN + 1 + 4];
807 /* If there is no data socket, black-hole it */
809 NG_FREE_DATA(m, meta);
812 so = pcbp->ng_socket;
814 /* Get the return address into a sockaddr. */
815 addrlen = strlen(hook->name); /* <= NG_HOOKLEN */
816 addr = (struct sockaddr_ng *) addrbuf;
817 addr->sg_len = addrlen + 3;
818 addr->sg_family = AF_NETGRAPH;
819 bcopy(hook->name, addr->sg_data, addrlen);
820 addr->sg_data[addrlen] = '\0';
822 /* We have no use for the meta data, free/clear it now. */
825 /* Try to tell the socket which hook it came in on */
826 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
838 * For this type, removal of the last link destroys the node
839 * if the NOLINGER flag is set.
842 ngs_disconnect(hook_p hook)
844 struct ngsock *const sockdata = hook->node->private;
846 if ((sockdata->flags & NGS_FLAG_NOLINGER )
847 && (hook->node->numhooks == 0)) {
848 ng_rmnode(hook->node);
854 * Do local shutdown processing.
855 * In this case, that involves making sure the socket
856 * knows we should be shutting down.
859 ngs_rmnode(node_p node)
861 struct ngsock *const sockdata = node->private;
862 struct ngpcb *const dpcbp = sockdata->datasock;
863 struct ngpcb *const pcbp = sockdata->ctlsock;
869 soisdisconnected(dpcbp->ng_socket);
870 dpcbp->sockdata = NULL;
871 sockdata->datasock = NULL;
875 soisdisconnected(pcbp->ng_socket);
876 pcbp->sockdata = NULL;
877 sockdata->ctlsock = NULL;
880 node->private = NULL;
882 FREE(sockdata, M_NETGRAPH);
887 * Control and data socket type descriptors
890 static struct pr_usrreqs ngc_usrreqs = {
896 pru_connect2_notsupp,
899 NULL, /* disconnect */
901 NULL, /* setpeeraddr */
913 static struct pr_usrreqs ngd_usrreqs = {
919 pru_connect2_notsupp,
922 NULL, /* disconnect */
924 NULL, /* setpeeraddr */
937 * Definitions of protocols supported in the NETGRAPH domain.
940 extern struct domain ngdomain; /* stop compiler warnings */
942 static struct protosw ngsw[] = {
947 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
965 struct domain ngdomain = {
972 &ngsw[sizeof(ngsw) / sizeof(ngsw[0])],
980 * Handle loading and unloading for this node type
981 * This is to handle auxiliary linkages (e.g protocol domain addition).
984 ngs_mod_event(module_t mod, int event, void *data)
990 /* Register protocol domain */
991 net_add_domain(&ngdomain);
994 /* Insure there are no open netgraph sockets */
995 if (!LIST_EMPTY(&ngsocklist)) {
1001 /* Unregister protocol domain XXX can't do this yet.. */
1002 if ((error = net_rm_domain(&ngdomain)) != 0)
1015 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1016 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1017 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1018 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1019 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");