AHCI - fix minor bug in last commit.
[dragonfly.git] / sys / netgraph7 / ng_socket.c
blob237f97a50a92c7deb5f67957cac80bcdb4495073
1 /*
2 * ng_socket.c
3 */
5 /*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
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
36 * OF SUCH DAMAGE.
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 * $DragonFly: src/sys/netgraph7/ng_socket.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
46 * Netgraph socket nodes
48 * There are two types of netgraph sockets, control and data.
49 * Control sockets have a netgraph node, but data sockets are
50 * parasitic on control sockets, and have no node of their own.
53 #include <sys/param.h>
54 #include <sys/domain.h>
55 #include <sys/kernel.h>
56 #include <sys/linker.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/mutex.h>
61 #include <sys/priv.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>
68 #ifdef NOTYET
69 #include <sys/vnode.h>
70 #endif
71 #include "ng_message.h"
72 #include "netgraph.h"
73 #include "ng_socketvar.h"
74 #include "ng_socket.h"
76 #ifdef NG_SEPARATE_MALLOC
77 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
78 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
79 #else
80 #define M_NETGRAPH_PATH M_NETGRAPH
81 #define M_NETGRAPH_SOCK M_NETGRAPH
82 #endif
85 * It's Ascii-art time!
86 * +-------------+ +-------------+
87 * |socket (ctl)| |socket (data)|
88 * +-------------+ +-------------+
89 * ^ ^
90 * | |
91 * v v
92 * +-----------+ +-----------+
93 * |pcb (ctl)| |pcb (data)|
94 * +-----------+ +-----------+
95 * ^ ^
96 * | |
97 * v v
98 * +--------------------------+
99 * | Socket type private |
100 * | data |
101 * +--------------------------+
105 * +----------------+
106 * | struct ng_node |
107 * +----------------+
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_rcvdata_t ngs_rcvdata;
117 static ng_disconnect_t ngs_disconnect;
119 /* Internal methods */
120 static int ng_attach_data(struct socket *so);
121 static int ng_attach_cntl(struct socket *so);
122 static int ng_attach_common(struct socket *so, int type);
123 static void ng_detach_common(struct ngpcb *pcbp, int type);
124 static void ng_socket_free_priv(struct ngsock *priv);
125 #ifdef NOTYET
126 static int ng_internalize(struct mbuf *m, struct thread *p);
127 #endif
128 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
129 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
131 static int ngs_mod_event(module_t mod, int event, void *data);
132 static void ng_socket_item_applied(void *context, int error);
134 /* Netgraph type descriptor */
135 static struct ng_type typestruct = {
136 .version = NG_ABI_VERSION,
137 .name = NG_SOCKET_NODE_TYPE,
138 .mod_event = ngs_mod_event,
139 .constructor = ngs_constructor,
140 .rcvmsg = ngs_rcvmsg,
141 .shutdown = ngs_shutdown,
142 .newhook = ngs_newhook,
143 .connect = ngs_connect,
144 .rcvdata = ngs_rcvdata,
145 .disconnect = ngs_disconnect,
147 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
149 /* Buffer space */
150 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
151 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
152 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
153 static u_long ngpdg_recvspace = 20 * 1024;
154 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
155 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
157 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
159 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
160 #ifndef TRAP_ERROR
161 #define TRAP_ERROR
162 #endif
164 /***************************************************************
165 Control sockets
166 ***************************************************************/
168 static int
169 ngc_attach(struct socket *so, int proto, struct thread *td)
171 struct ngpcb *const pcbp = sotongpcb(so);
172 int error;
174 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
175 if (error)
176 return (error);
177 if (pcbp != NULL)
178 return (EISCONN);
179 return (ng_attach_cntl(so));
182 static void
183 ngc_detach(struct socket *so)
185 struct ngpcb *const pcbp = sotongpcb(so);
187 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
188 ng_detach_common(pcbp, NG_CONTROL);
191 static int
192 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
193 struct mbuf *control, struct thread *td)
195 struct ngpcb *const pcbp = sotongpcb(so);
196 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
197 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
198 struct ng_mesg *msg;
199 struct mbuf *m0;
200 item_p item;
201 char *path = NULL;
202 int len, error = 0;
203 struct ng_apply_info apply;
205 #ifdef NOTYET
206 if (control && (error = ng_internalize(control, td))) {
207 if (pcbp->sockdata == NULL) {
208 error = ENOTCONN;
209 goto release;
212 #else /* NOTYET */
213 if (control) {
214 error = EINVAL;
215 goto release;
217 #endif /* NOTYET */
219 /* Require destination as there may be >= 1 hooks on this node. */
220 if (addr == NULL) {
221 error = EDESTADDRREQ;
222 goto release;
226 * Allocate an expendable buffer for the path, chop off
227 * the sockaddr header, and make sure it's NUL terminated.
229 len = sap->sg_len - 2;
230 path = kmalloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
231 bcopy(sap->sg_data, path, len);
232 path[len] = '\0';
235 * Move the actual message out of mbufs into a linear buffer.
236 * Start by adding up the size of the data. (could use mh_len?)
238 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
239 len += m0->m_len;
242 * Move the data into a linear buffer as well.
243 * Messages are not delivered in mbufs.
245 msg = kmalloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
246 m_copydata(m, 0, len, (char *)msg);
248 if (msg->header.version != NG_VERSION) {
249 kfree(msg, M_NETGRAPH_MSG);
250 error = EINVAL;
251 goto release;
255 * Hack alert!
256 * We look into the message and if it mkpeers a node of unknown type, we
257 * try to load it. We need to do this now, in syscall thread, because if
258 * message gets queued and applied later we will get panic.
260 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
261 msg->header.cmd == NGM_MKPEER) {
262 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
263 struct ng_type *type;
265 if ((type = ng_findtype(mkp->type)) == NULL) {
266 char filename[NG_TYPESIZ + 3];
267 int fileid;
269 /* Not found, try to load it as a loadable module. */
270 snprintf(filename, sizeof(filename), "ng_%s",
271 mkp->type);
272 error = kern_kldload(curthread, filename, &fileid);
273 if (error != 0) {
274 kfree(msg, M_NETGRAPH_MSG);
275 goto release;
278 /* See if type has been loaded successfully. */
279 if ((type = ng_findtype(mkp->type)) == NULL) {
280 kfree(msg, M_NETGRAPH_MSG);
281 (void)kern_kldunload(curthread, fileid,
282 LINKER_UNLOAD_NORMAL);
283 error = ENXIO;
284 goto release;
289 item = ng_package_msg(msg, NG_WAITOK);
290 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
291 != 0) {
292 #ifdef TRACE_MESSAGES
293 printf("ng_address_path: errx=%d\n", error);
294 #endif
295 goto release;
298 #ifdef TRACE_MESSAGES
299 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
300 item->el_dest->nd_ID,
301 msg->header.typecookie,
302 msg->header.cmd,
303 msg->header.cmdstr,
304 msg->header.flags,
305 msg->header.token,
306 item->el_dest->nd_type->name);
307 #endif
308 SAVE_LINE(item);
310 * We do not want to return from syscall until the item
311 * is processed by destination node. We register callback
312 * on the item, which will update priv->error when item
313 * was applied.
314 * If ng_snd_item() has queued item, we sleep until
315 * callback wakes us up.
317 bzero(&apply, sizeof(apply));
318 apply.apply = ng_socket_item_applied;
319 apply.context = priv;
320 item->apply = &apply;
321 priv->error = -1;
323 error = ng_snd_item(item, 0);
325 mtx_lock(&priv->mtx);
326 if (priv->error == -1)
327 msleep(priv, &priv->mtx, 0, "ngsock", 0);
328 mtx_unlock(&priv->mtx);
329 KASSERT(priv->error != -1,
330 ("ng_socket: priv->error wasn't updated"));
331 error = priv->error;
333 release:
334 if (path != NULL)
335 kfree(path, M_NETGRAPH_PATH);
336 if (control != NULL)
337 m_freem(control);
338 if (m != NULL)
339 m_freem(m);
340 return (error);
343 static int
344 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
346 struct ngpcb *const pcbp = sotongpcb(so);
348 if (pcbp == 0)
349 return (EINVAL);
350 return (ng_bind(nam, pcbp));
353 static int
354 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
357 * At this time refuse to do this.. it used to
358 * do something but it was undocumented and not used.
360 printf("program tried to connect control socket to remote node\n");
361 return (EINVAL);
364 /***************************************************************
365 Data sockets
366 ***************************************************************/
368 static int
369 ngd_attach(struct socket *so, int proto, struct thread *td)
371 struct ngpcb *const pcbp = sotongpcb(so);
373 if (pcbp != NULL)
374 return (EISCONN);
375 return (ng_attach_data(so));
378 static void
379 ngd_detach(struct socket *so)
381 struct ngpcb *const pcbp = sotongpcb(so);
383 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
384 ng_detach_common(pcbp, NG_DATA);
387 static int
388 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
389 struct mbuf *control, struct thread *td)
391 struct ngpcb *const pcbp = sotongpcb(so);
392 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
393 int len, error;
394 hook_p hook = NULL;
395 char hookname[NG_HOOKSIZ];
397 if ((pcbp == NULL) || (control != NULL)) {
398 error = EINVAL;
399 goto release;
401 if (pcbp->sockdata == NULL) {
402 error = ENOTCONN;
403 goto release;
406 if (sap == NULL)
407 len = 0; /* Make compiler happy. */
408 else
409 len = sap->sg_len - 2;
412 * If the user used any of these ways to not specify an address
413 * then handle specially.
415 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
416 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
417 error = EDESTADDRREQ;
418 goto release;
421 * If exactly one hook exists, just use it.
422 * Special case to allow write(2) to work on an ng_socket.
424 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
425 } else {
426 if (len >= NG_HOOKSIZ) {
427 error = EINVAL;
428 goto release;
432 * chop off the sockaddr header, and make sure it's NUL
433 * terminated
435 bcopy(sap->sg_data, hookname, len);
436 hookname[len] = '\0';
438 /* Find the correct hook from 'hookname' */
439 hook = ng_findhook(pcbp->sockdata->node, hookname);
440 if (hook == NULL) {
441 error = EHOSTUNREACH;
442 goto release;
446 /* Send data. */
447 NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
449 release:
450 if (control != NULL)
451 m_freem(control);
452 if (m != NULL)
453 m_freem(m);
454 return (error);
457 static int
458 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
460 struct ngpcb *const pcbp = sotongpcb(so);
462 if (pcbp == 0)
463 return (EINVAL);
464 return (ng_connect_data(nam, pcbp));
468 * Used for both data and control sockets
470 static int
471 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
473 struct ngpcb *pcbp;
474 struct sockaddr_ng *sg;
475 int sg_len;
476 int error = 0;
478 /* Why isn't sg_data a `char[1]' ? :-( */
479 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
481 pcbp = sotongpcb(so);
482 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
483 /* XXXGL: can this still happen? */
484 return (EINVAL);
486 mtx_lock(&pcbp->sockdata->mtx);
487 if (pcbp->sockdata->node != NULL) {
488 node_p node = pcbp->sockdata->node;
489 int namelen = 0; /* silence compiler! */
491 if (NG_NODE_HAS_NAME(node))
492 sg_len += namelen = strlen(NG_NODE_NAME(node));
494 sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
496 if (NG_NODE_HAS_NAME(node))
497 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
499 sg->sg_len = sg_len;
500 sg->sg_family = AF_NETGRAPH;
501 *addr = (struct sockaddr *)sg;
502 mtx_unlock(&pcbp->sockdata->mtx);
503 } else {
504 mtx_unlock(&pcbp->sockdata->mtx);
505 error = EINVAL;
508 return (error);
512 * Attach a socket to it's protocol specific partner.
513 * For a control socket, actually create a netgraph node and attach
514 * to it as well.
517 static int
518 ng_attach_cntl(struct socket *so)
520 struct ngsock *priv;
521 struct ngpcb *pcbp;
522 int error;
524 /* Allocate node private info */
525 priv = kmalloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
527 /* Setup protocol control block */
528 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
529 kfree(priv, M_NETGRAPH_SOCK);
530 return (error);
532 pcbp = sotongpcb(so);
534 /* Link the pcb the private data. */
535 priv->ctlsock = pcbp;
536 pcbp->sockdata = priv;
537 priv->refs++;
539 /* Initialize mutex. */
540 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
542 /* Make the generic node components */
543 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
544 kfree(priv, M_NETGRAPH_SOCK);
545 ng_detach_common(pcbp, NG_CONTROL);
546 return (error);
549 /* Link the node and the private data. */
550 NG_NODE_SET_PRIVATE(priv->node, priv);
551 NG_NODE_REF(priv->node);
552 priv->refs++;
554 return (0);
557 static int
558 ng_attach_data(struct socket *so)
560 return (ng_attach_common(so, NG_DATA));
564 * Set up a socket protocol control block.
565 * This code is shared between control and data sockets.
567 static int
568 ng_attach_common(struct socket *so, int type)
570 struct ngpcb *pcbp;
571 int error;
573 /* Standard socket setup stuff. */
574 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
575 if (error)
576 return (error);
578 /* Allocate the pcb. */
579 pcbp = kmalloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
580 pcbp->type = type;
582 /* Link the pcb and the socket. */
583 so->so_pcb = (caddr_t)pcbp;
584 pcbp->ng_socket = so;
586 return (0);
590 * Disassociate the socket from it's protocol specific
591 * partner. If it's attached to a node's private data structure,
592 * then unlink from that too. If we were the last socket attached to it,
593 * then shut down the entire node. Shared code for control and data sockets.
595 static void
596 ng_detach_common(struct ngpcb *pcbp, int which)
598 struct ngsock *priv = pcbp->sockdata;
600 if (priv != NULL) {
601 mtx_lock(&priv->mtx);
603 switch (which) {
604 case NG_CONTROL:
605 priv->ctlsock = NULL;
606 break;
607 case NG_DATA:
608 priv->datasock = NULL;
609 break;
610 default:
611 panic(__func__);
613 pcbp->sockdata = NULL;
615 ng_socket_free_priv(priv);
618 pcbp->ng_socket->so_pcb = NULL;
619 kfree(pcbp, M_PCB);
623 * Remove a reference from node private data.
625 static void
626 ng_socket_free_priv(struct ngsock *priv)
628 mtx_assert(&priv->mtx, MA_OWNED);
630 priv->refs--;
632 if (priv->refs == 0) {
633 mtx_destroy(&priv->mtx);
634 kfree(priv, M_NETGRAPH_SOCK);
635 return;
638 if ((priv->refs == 1) && (priv->node != NULL)) {
639 node_p node = priv->node;
641 priv->node = NULL;
642 mtx_unlock(&priv->mtx);
643 NG_NODE_UNREF(node);
644 ng_rmnode_self(node);
645 } else
646 mtx_unlock(&priv->mtx);
649 #ifdef NOTYET
651 * File descriptors can be passed into an AF_NETGRAPH socket.
652 * Note, that file descriptors cannot be passed OUT.
653 * Only character device descriptors are accepted.
654 * Character devices are useful to connect a graph to a device,
655 * which after all is the purpose of this whole system.
657 static int
658 ng_internalize(struct mbuf *control, struct thread *td)
660 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
661 struct file *fp;
662 struct vnode *vn;
663 int oldfds;
664 int fd;
666 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
667 cm->cmsg_len != control->m_len) {
668 TRAP_ERROR;
669 return (EINVAL);
672 /* Check there is only one FD. XXX what would more than one signify? */
673 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
674 if (oldfds != 1) {
675 TRAP_ERROR;
676 return (EINVAL);
679 /* Check that the FD given is legit. and change it to a pointer to a
680 * struct file. */
681 fd = CMSG_DATA(cm);
682 if ((error = fget(td, fd, &fp)) != 0)
683 return (error);
685 /* Depending on what kind of resource it is, act differently. For
686 * devices, we treat it as a file. For an AF_NETGRAPH socket,
687 * shortcut straight to the node. */
688 switch (fp->f_type) {
689 case DTYPE_VNODE:
690 vn = fp->f_data;
691 if (vn && (vn->v_type == VCHR)) {
692 /* for a VCHR, actually reference the FILE */
693 fhold(fp);
694 /* XXX then what :) */
695 /* how to pass on to other modules? */
696 } else {
697 fdrop(fp, td);
698 TRAP_ERROR;
699 return (EINVAL);
701 break;
702 default:
703 fdrop(fp, td);
704 TRAP_ERROR;
705 return (EINVAL);
707 fdrop(fp, td);
708 return (0);
710 #endif /* NOTYET */
713 * Connect the data socket to a named control socket node.
715 static int
716 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
718 struct sockaddr_ng *sap;
719 node_p farnode;
720 struct ngsock *priv;
721 int error;
722 item_p item;
724 /* If we are already connected, don't do it again. */
725 if (pcbp->sockdata != NULL)
726 return (EISCONN);
729 * Find the target (victim) and check it doesn't already have
730 * a data socket. Also check it is a 'socket' type node.
731 * Use ng_package_data() and ng_address_path() to do this.
734 sap = (struct sockaddr_ng *) nam;
735 /* The item will hold the node reference. */
736 item = ng_package_data(NULL, NG_WAITOK);
738 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
739 return (error); /* item is freed on failure */
742 * Extract node from item and free item. Remember we now have
743 * a reference on the node. The item holds it for us.
744 * when we free the item we release the reference.
746 farnode = item->el_dest; /* shortcut */
747 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
748 NG_FREE_ITEM(item); /* drop the reference to the node */
749 return (EINVAL);
751 priv = NG_NODE_PRIVATE(farnode);
752 if (priv->datasock != NULL) {
753 NG_FREE_ITEM(item); /* drop the reference to the node */
754 return (EADDRINUSE);
758 * Link the PCB and the private data struct. and note the extra
759 * reference. Drop the extra reference on the node.
761 mtx_lock(&priv->mtx);
762 priv->datasock = pcbp;
763 pcbp->sockdata = priv;
764 priv->refs++;
765 mtx_unlock(&priv->mtx);
766 NG_FREE_ITEM(item); /* drop the reference to the node */
767 return (0);
771 * Binding a socket means giving the corresponding node a name
773 static int
774 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
776 struct ngsock *const priv = pcbp->sockdata;
777 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
779 if (priv == NULL) {
780 TRAP_ERROR;
781 return (EINVAL);
783 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
784 (sap->sg_data[0] == '\0') ||
785 (sap->sg_data[sap->sg_len - 3] != '\0')) {
786 TRAP_ERROR;
787 return (EINVAL);
789 return (ng_name_node(priv->node, sap->sg_data));
792 /***************************************************************
793 Netgraph node
794 ***************************************************************/
797 * You can only create new nodes from the socket end of things.
799 static int
800 ngs_constructor(node_p nodep)
802 return (EINVAL);
806 * We allow any hook to be connected to the node.
807 * There is no per-hook private information though.
809 static int
810 ngs_newhook(node_p node, hook_p hook, const char *name)
812 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
813 return (0);
817 * If only one hook, allow read(2) and write(2) to work.
819 static int
820 ngs_connect(hook_p hook)
822 node_p node = NG_HOOK_NODE(hook);
823 struct ngsock *priv = NG_NODE_PRIVATE(node);
825 if ((priv->datasock) && (priv->datasock->ng_socket)) {
826 if (NG_NODE_NUMHOOKS(node) == 1)
827 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
828 else
829 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
831 return (0);
835 * Incoming messages get passed up to the control socket.
836 * Unless they are for us specifically (socket_type)
838 static int
839 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
841 struct ngsock *const priv = NG_NODE_PRIVATE(node);
842 struct ngpcb *const pcbp = priv->ctlsock;
843 struct socket *so;
844 struct sockaddr_ng addr;
845 struct ng_mesg *msg;
846 struct mbuf *m;
847 ng_ID_t retaddr = NGI_RETADDR(item);
848 int addrlen;
849 int error = 0;
851 NGI_GET_MSG(item, msg);
852 NG_FREE_ITEM(item);
855 * Only allow mesgs to be passed if we have the control socket.
856 * Data sockets can only support the generic messages.
858 if (pcbp == NULL) {
859 TRAP_ERROR;
860 NG_FREE_MSG(msg);
861 return (EINVAL);
863 so = pcbp->ng_socket;
865 #ifdef TRACE_MESSAGES
866 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
867 retaddr,
868 msg->header.typecookie,
869 msg->header.cmd,
870 msg->header.cmdstr,
871 msg->header.flags,
872 msg->header.token);
873 #endif
875 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
876 switch (msg->header.cmd) {
877 case NGM_SOCK_CMD_NOLINGER:
878 priv->flags |= NGS_FLAG_NOLINGER;
879 break;
880 case NGM_SOCK_CMD_LINGER:
881 priv->flags &= ~NGS_FLAG_NOLINGER;
882 break;
883 default:
884 error = EINVAL; /* unknown command */
886 /* Free the message and return. */
887 NG_FREE_MSG(msg);
888 return (error);
891 /* Get the return address into a sockaddr. */
892 bzero(&addr, sizeof(addr));
893 addr.sg_len = sizeof(addr);
894 addr.sg_family = AF_NETGRAPH;
895 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
896 "[%x]:", retaddr);
897 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
898 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
899 addrlen);
900 NG_FREE_MSG(msg);
901 return (EINVAL);
904 /* Copy the message itself into an mbuf chain. */
905 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
906 0, NULL, NULL);
909 * Here we free the message. We need to do that
910 * regardless of whether we got mbufs.
912 NG_FREE_MSG(msg);
914 if (m == NULL) {
915 TRAP_ERROR;
916 return (ENOBUFS);
919 /* Send it up to the socket. */
920 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
921 TRAP_ERROR;
922 m_freem(m);
923 return (ENOBUFS);
925 sorwakeup(so);
927 return (error);
931 * Receive data on a hook
933 static int
934 ngs_rcvdata(hook_p hook, item_p item)
936 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
937 struct ngpcb *const pcbp = priv->datasock;
938 struct socket *so;
939 struct sockaddr_ng *addr;
940 char *addrbuf[NG_HOOKSIZ + 4];
941 int addrlen;
942 struct mbuf *m;
944 NGI_GET_M(item, m);
945 NG_FREE_ITEM(item);
947 /* If there is no data socket, black-hole it. */
948 if (pcbp == NULL) {
949 NG_FREE_M(m);
950 return (0);
952 so = pcbp->ng_socket;
954 /* Get the return address into a sockaddr. */
955 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
956 addr = (struct sockaddr_ng *) addrbuf;
957 addr->sg_len = addrlen + 3;
958 addr->sg_family = AF_NETGRAPH;
959 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
960 addr->sg_data[addrlen] = '\0';
962 /* Try to tell the socket which hook it came in on. */
963 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
964 m_freem(m);
965 TRAP_ERROR;
966 return (ENOBUFS);
968 sorwakeup(so);
969 return (0);
973 * Hook disconnection
975 * For this type, removal of the last link destroys the node
976 * if the NOLINGER flag is set.
978 static int
979 ngs_disconnect(hook_p hook)
981 node_p node = NG_HOOK_NODE(hook);
982 struct ngsock *const priv = NG_NODE_PRIVATE(node);
984 if ((priv->datasock) && (priv->datasock->ng_socket)) {
985 if (NG_NODE_NUMHOOKS(node) == 1)
986 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
987 else
988 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
991 if ((priv->flags & NGS_FLAG_NOLINGER) &&
992 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
993 ng_rmnode_self(node);
995 return (0);
999 * Do local shutdown processing.
1000 * In this case, that involves making sure the socket
1001 * knows we should be shutting down.
1003 static int
1004 ngs_shutdown(node_p node)
1006 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1007 struct ngpcb *const dpcbp = priv->datasock;
1008 struct ngpcb *const pcbp = priv->ctlsock;
1010 if (dpcbp != NULL)
1011 soisdisconnected(dpcbp->ng_socket);
1013 if (pcbp != NULL)
1014 soisdisconnected(pcbp->ng_socket);
1016 mtx_lock(&priv->mtx);
1017 priv->node = NULL;
1018 NG_NODE_SET_PRIVATE(node, NULL);
1019 ng_socket_free_priv(priv);
1021 NG_NODE_UNREF(node);
1022 return (0);
1025 static void
1026 ng_socket_item_applied(void *context, int error)
1028 struct ngsock *const priv = (struct ngsock *)context;
1030 mtx_lock(&priv->mtx);
1031 priv->error = error;
1032 wakeup(priv);
1033 mtx_unlock(&priv->mtx);
1037 static int
1038 dummy_disconnect(struct socket *so)
1040 return (0);
1043 * Control and data socket type descriptors
1045 * XXXRW: Perhaps _close should do something?
1048 static struct pr_usrreqs ngc_usrreqs = {
1049 .pru_abort = NULL,
1050 .pru_attach = ngc_attach,
1051 .pru_bind = ngc_bind,
1052 .pru_connect = ngc_connect,
1053 .pru_detach = ngc_detach,
1054 .pru_disconnect = dummy_disconnect,
1055 .pru_peeraddr = NULL,
1056 .pru_send = ngc_send,
1057 .pru_shutdown = NULL,
1058 .pru_sockaddr = ng_getsockaddr,
1059 .pru_close = NULL,
1062 static struct pr_usrreqs ngd_usrreqs = {
1063 .pru_abort = NULL,
1064 .pru_attach = ngd_attach,
1065 .pru_bind = NULL,
1066 .pru_connect = ngd_connect,
1067 .pru_detach = ngd_detach,
1068 .pru_disconnect = dummy_disconnect,
1069 .pru_peeraddr = NULL,
1070 .pru_send = ngd_send,
1071 .pru_shutdown = NULL,
1072 .pru_sockaddr = ng_getsockaddr,
1073 .pru_close = NULL,
1077 * Definitions of protocols supported in the NETGRAPH domain.
1080 extern struct domain ngdomain; /* stop compiler warnings */
1082 static struct protosw ngsw[] = {
1084 .pr_type = SOCK_DGRAM,
1085 .pr_domain = &ngdomain,
1086 .pr_protocol = NG_CONTROL,
1087 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1088 .pr_usrreqs = &ngc_usrreqs
1091 .pr_type = SOCK_DGRAM,
1092 .pr_domain = &ngdomain,
1093 .pr_protocol = NG_DATA,
1094 .pr_flags = PR_ATOMIC | PR_ADDR,
1095 .pr_usrreqs = &ngd_usrreqs
1099 struct domain ngdomain = {
1100 .dom_family = AF_NETGRAPH,
1101 .dom_name = "netgraph",
1102 .dom_protosw = ngsw,
1103 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1107 * Handle loading and unloading for this node type.
1108 * This is to handle auxiliary linkages (e.g protocol domain addition).
1110 static int
1111 ngs_mod_event(module_t mod, int event, void *data)
1113 int error = 0;
1115 switch (event) {
1116 case MOD_LOAD:
1117 /* Register protocol domain. */
1118 net_add_domain(&ngdomain);
1119 break;
1120 case MOD_UNLOAD:
1121 #ifdef NOTYET
1122 /* Unregister protocol domain XXX can't do this yet.. */
1123 if ((error = net_rm_domain(&ngdomain)) != 0)
1124 break;
1125 else
1126 #endif
1127 error = EBUSY;
1128 break;
1129 default:
1130 error = EOPNOTSUPP;
1131 break;
1133 return (error);
1136 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1137 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1138 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1139 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1140 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");