Remove bogus checks after kmalloc(M_WAITOK) which never returns NULL.
[dragonfly.git] / sys / netgraph / socket / ng_socket.c
blobd4826e57da4abada73bf927576104eeb07f4e0bc
2 /*
3 * ng_socket.c
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
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
35 * OF SUCH DAMAGE.
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.16 2008/01/06 16:55:52 swildner 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>
54 #include <sys/proc.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>
61 #include <sys/mbuf.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
66 #include <sys/thread2.h>
67 #ifdef NOTYET
68 #include <sys/vnode.h>
69 #endif
70 #include <netgraph/ng_message.h>
71 #include <netgraph/netgraph.h>
72 #include "ng_socketvar.h"
73 #include "ng_socket.h"
76 * It's Ascii-art time!
77 * +-------------+ +-------------+
78 * |socket (ctl)| |socket (data)|
79 * +-------------+ +-------------+
80 * ^ ^
81 * | |
82 * v v
83 * +-----------+ +-----------+
84 * |pcb (ctl)| |pcb (data)|
85 * +-----------+ +-----------+
86 * ^ ^
87 * | |
88 * v v
89 * +--------------------------+
90 * | Socket type private |
91 * | data |
92 * +--------------------------+
93 * ^
94 * |
95 * v
96 * +----------------+
97 * | struct ng_node |
98 * +----------------+
101 /* Netgraph node methods */
102 static ng_constructor_t ngs_constructor;
103 static ng_rcvmsg_t ngs_rcvmsg;
104 static ng_shutdown_t ngs_rmnode;
105 static ng_newhook_t ngs_newhook;
106 static ng_rcvdata_t ngs_rcvdata;
107 static ng_disconnect_t ngs_disconnect;
109 /* Internal methods */
110 static int ng_attach_data(struct socket *so);
111 static int ng_attach_cntl(struct socket *so);
112 static int ng_attach_common(struct socket *so, int type);
113 static void ng_detach_common(struct ngpcb *pcbp, int type);
114 /*static int ng_internalize(struct mbuf *m, struct thread *td); */
116 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
117 static int ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp);
118 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
120 static int ngs_mod_event(module_t mod, int event, void *data);
121 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
122 struct sockaddr_ng *addr);
124 /* Netgraph type descriptor */
125 static struct ng_type typestruct = {
126 NG_VERSION,
127 NG_SOCKET_NODE_TYPE,
128 ngs_mod_event,
129 ngs_constructor,
130 ngs_rcvmsg,
131 ngs_rmnode,
132 ngs_newhook,
133 NULL,
134 NULL,
135 ngs_rcvdata,
136 ngs_rcvdata,
137 ngs_disconnect,
138 NULL
140 NETGRAPH_INIT(socket, &typestruct);
142 /* Buffer space */
143 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
144 static u_long ngpdg_recvspace = 20 * 1024;
146 /* List of all sockets */
147 LIST_HEAD(, ngpcb) ngsocklist;
149 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
151 /* If getting unexplained errors returned, set this to "Debugger("X"); */
152 #ifndef TRAP_ERROR
153 #define TRAP_ERROR
154 #endif
156 /***************************************************************
157 Control sockets
158 ***************************************************************/
160 static int
161 ngc_attach(struct socket *so, int proto, struct pru_attach_info *ai)
163 struct ngpcb *const pcbp = sotongpcb(so);
165 if (suser_cred(ai->p_ucred, NULL_CRED_OKAY) != 0)
166 return (EPERM);
167 if (pcbp != NULL)
168 return (EISCONN);
169 return (ng_attach_cntl(so));
172 static int
173 ngc_detach(struct socket *so)
175 struct ngpcb *const pcbp = sotongpcb(so);
177 if (pcbp == NULL)
178 return (EINVAL);
179 ng_detach_common(pcbp, NG_CONTROL);
180 return (0);
183 static int
184 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
185 struct mbuf *control, struct thread *td)
187 struct ngpcb *const pcbp = sotongpcb(so);
188 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
189 struct ng_mesg *resp;
190 struct mbuf *m0;
191 char *msg, *path = NULL;
192 int len, error = 0;
194 if (pcbp == NULL) {
195 error = EINVAL;
196 goto release;
198 #ifdef NOTYET
199 if (control && (error = ng_internalize(control, p))) {
200 if (pcbp->sockdata == NULL) {
201 error = ENOTCONN;
202 goto release;
205 #else /* NOTYET */
206 if (control) {
207 error = EINVAL;
208 goto release;
210 #endif /* NOTYET */
212 /* Require destination as there may be >= 1 hooks on this node */
213 if (addr == NULL) {
214 error = EDESTADDRREQ;
215 goto release;
218 /* Allocate an expendable buffer for the path, chop off
219 * the sockaddr header, and make sure it's NUL terminated */
220 len = sap->sg_len - 2;
221 MALLOC(path, char *, len + 1, M_NETGRAPH, M_WAITOK);
222 bcopy(sap->sg_data, path, len);
223 path[len] = '\0';
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)
228 len += m0->m_len;
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);
233 m_copydata(m, 0, len, msg);
235 /* The callee will free the msg when done. The addr is our business. */
236 error = ng_send_msg(pcbp->sockdata->node,
237 (struct ng_mesg *) msg, path, &resp);
239 /* If the callee responded with a synchronous response, then put it
240 * back on the receive side of the socket; sap is source address. */
241 if (error == 0 && resp != NULL)
242 error = ship_msg(pcbp, resp, sap);
244 release:
245 if (path != NULL)
246 FREE(path, M_NETGRAPH);
247 if (control != NULL)
248 m_freem(control);
249 if (m != NULL)
250 m_freem(m);
251 return (error);
254 static int
255 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
257 struct ngpcb *const pcbp = sotongpcb(so);
259 if (pcbp == 0)
260 return (EINVAL);
261 return (ng_bind(nam, pcbp));
264 static int
265 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
267 struct ngpcb *const pcbp = sotongpcb(so);
269 if (pcbp == 0)
270 return (EINVAL);
271 return (ng_connect_cntl(nam, pcbp));
274 /***************************************************************
275 Data sockets
276 ***************************************************************/
278 static int
279 ngd_attach(struct socket *so, int proto, struct pru_attach_info *ai)
281 struct ngpcb *const pcbp = sotongpcb(so);
283 if (pcbp != NULL)
284 return (EISCONN);
285 return (ng_attach_data(so));
288 static int
289 ngd_detach(struct socket *so)
291 struct ngpcb *const pcbp = sotongpcb(so);
293 if (pcbp == NULL)
294 return (EINVAL);
295 ng_detach_common(pcbp, NG_DATA);
296 return (0);
299 static int
300 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
301 struct mbuf *control, struct thread *td)
303 struct ngpcb *const pcbp = sotongpcb(so);
304 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
305 meta_p mp = NULL;
306 int len, error;
307 hook_p hook = NULL;
308 char hookname[NG_HOOKSIZ];
310 if ((pcbp == NULL) || (control != NULL)) {
311 error = EINVAL;
312 goto release;
314 if (pcbp->sockdata == NULL) {
315 error = ENOTCONN;
316 goto release;
319 * If the user used any of these ways to not specify an address
320 * then handle specially.
322 if ((sap == NULL)
323 || ((len = sap->sg_len - 2) <= 0)
324 || (*sap->sg_data == '\0')) {
325 if (pcbp->sockdata->node->numhooks != 1) {
326 error = EDESTADDRREQ;
327 goto release;
330 * if exactly one hook exists, just use it.
331 * Special case to allow write(2) to work on an ng_socket.
333 hook = LIST_FIRST(&pcbp->sockdata->node->hooks);
334 } else {
335 if (len >= NG_HOOKSIZ) {
336 error = EINVAL;
337 goto release;
341 * chop off the sockaddr header, and make sure it's NUL
342 * terminated
344 bcopy(sap->sg_data, hookname, len);
345 hookname[len] = '\0';
347 /* Find the correct hook from 'hookname' */
348 LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
349 if (strcmp(hookname, hook->name) == 0)
350 break;
352 if (hook == NULL)
353 error = EHOSTUNREACH;
356 /* Send data (OK if hook is NULL) */
357 NG_SEND_DATA(error, hook, m, mp); /* makes m NULL */
359 release:
360 if (control != NULL)
361 m_freem(control);
362 if (m != NULL)
363 m_freem(m);
364 return (error);
367 static int
368 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
370 struct ngpcb *const pcbp = sotongpcb(so);
372 if (pcbp == 0)
373 return (EINVAL);
374 return (ng_connect_data(nam, pcbp));
378 * Used for both data and control sockets
380 static int
381 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
383 struct ngpcb *pcbp;
384 struct sockaddr_ng *sg;
385 int sg_len, namelen;
387 /* Why isn't sg_data a `char[1]' ? :-( */
388 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
390 crit_enter();
391 pcbp = sotongpcb(so);
392 if ((pcbp == 0) || (pcbp->sockdata == NULL)) {
393 crit_exit();
394 return (EINVAL);
397 namelen = 0; /* silence compiler ! */
399 if (pcbp->sockdata->node->name != NULL)
400 sg_len += namelen = strlen(pcbp->sockdata->node->name);
402 MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK | M_ZERO);
404 if (pcbp->sockdata->node->name != NULL)
405 bcopy(pcbp->sockdata->node->name, sg->sg_data, namelen);
406 crit_exit();
408 sg->sg_len = sg_len;
409 sg->sg_family = AF_NETGRAPH;
410 *addr = (struct sockaddr *)sg;
412 return (0);
416 * Attach a socket to it's protocol specific partner.
417 * For a control socket, actually create a netgraph node and attach
418 * to it as well.
421 static int
422 ng_attach_cntl(struct socket *so)
424 struct ngsock *privdata;
425 struct ngpcb *pcbp;
426 int error;
428 /* Setup protocol control block */
429 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
430 return (error);
431 pcbp = sotongpcb(so);
433 /* Allocate node private info */
434 MALLOC(privdata, struct ngsock *,
435 sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
437 /* Make the generic node components */
438 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
439 FREE(privdata, M_NETGRAPH);
440 ng_detach_common(pcbp, NG_CONTROL);
441 return (error);
443 privdata->node->private = privdata;
445 /* Link the pcb and the node private data */
446 privdata->ctlsock = pcbp;
447 pcbp->sockdata = privdata;
448 privdata->refs++;
449 return (0);
452 static int
453 ng_attach_data(struct socket *so)
455 return(ng_attach_common(so, NG_DATA));
459 * Set up a socket protocol control block.
460 * This code is shared between control and data sockets.
462 static int
463 ng_attach_common(struct socket *so, int type)
465 struct ngpcb *pcbp;
466 int error;
468 /* Standard socket setup stuff */
469 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL);
470 if (error)
471 return (error);
473 /* Allocate the pcb */
474 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
475 pcbp->type = type;
477 /* Link the pcb and the socket */
478 so->so_pcb = (caddr_t) pcbp;
479 pcbp->ng_socket = so;
481 /* Add the socket to linked list */
482 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
483 return (0);
487 * Disassociate the socket from it's protocol specific
488 * partner. If it's attached to a node's private data structure,
489 * then unlink from that too. If we were the last socket attached to it,
490 * then shut down the entire node. Shared code for control and data sockets.
492 static void
493 ng_detach_common(struct ngpcb *pcbp, int which)
495 struct ngsock *sockdata;
497 if (pcbp->sockdata) {
498 sockdata = pcbp->sockdata;
499 pcbp->sockdata = NULL;
500 switch (which) {
501 case NG_CONTROL:
502 sockdata->ctlsock = NULL;
503 break;
504 case NG_DATA:
505 sockdata->datasock = NULL;
506 break;
507 default:
508 panic(__func__);
510 if ((--sockdata->refs == 0) && (sockdata->node != NULL))
511 ng_rmnode(sockdata->node);
513 pcbp->ng_socket->so_pcb = NULL;
514 pcbp->ng_socket = NULL;
515 LIST_REMOVE(pcbp, socks);
516 FREE(pcbp, M_PCB);
519 #ifdef NOTYET
521 * File descriptors can be passed into a AF_NETGRAPH socket.
522 * Note, that file descriptors cannot be passed OUT.
523 * Only character device descriptors are accepted.
524 * Character devices are useful to connect a graph to a device,
525 * which after all is the purpose of this whole system.
527 static int
528 ng_internalize(struct mbuf *control, struct thread *td)
530 struct filedesc *fdp = p->p_fd;
531 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
532 struct file *fp;
533 struct vnode *vn;
534 int oldfds;
535 int fd;
537 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
538 cm->cmsg_len != control->m_len) {
539 TRAP_ERROR;
540 return (EINVAL);
543 /* Check there is only one FD. XXX what would more than one signify? */
544 oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
545 if (oldfds != 1) {
546 TRAP_ERROR;
547 return (EINVAL);
550 /* Check that the FD given is legit. and change it to a pointer to a
551 * struct file. */
552 fd = *(int *) (cm + 1);
553 if ((unsigned) fd >= fdp->fd_nfiles
554 || (fp = fdp->fd_files[fd].fp) == NULL) {
555 return (EBADF);
558 /* Depending on what kind of resource it is, act differently. For
559 * devices, we treat it as a file. For a AF_NETGRAPH socket,
560 * shortcut straight to the node. */
561 switch (fp->f_type) {
562 case DTYPE_VNODE:
563 vn = (struct vnode *) fp->f_data;
564 if (vn && (vn->v_type == VCHR)) {
565 /* for a VCHR, actually reference the FILE */
566 fp->f_count++;
567 /* XXX then what :) */
568 /* how to pass on to other modules? */
569 } else {
570 TRAP_ERROR;
571 return (EINVAL);
573 break;
574 default:
575 TRAP_ERROR;
576 return (EINVAL);
578 return (0);
580 #endif /* NOTYET */
583 * Connect the data socket to a named control socket node.
585 static int
586 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
588 struct sockaddr_ng *sap;
589 node_p farnode;
590 struct ngsock *sockdata;
591 int error;
593 /* If we are already connected, don't do it again */
594 if (pcbp->sockdata != NULL)
595 return (EISCONN);
597 /* Find the target (victim) and check it doesn't already have a data
598 * socket. Also check it is a 'socket' type node. */
599 sap = (struct sockaddr_ng *) nam;
600 if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
601 return (error);
603 if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
604 return (EINVAL);
605 sockdata = farnode->private;
606 if (sockdata->datasock != NULL)
607 return (EADDRINUSE);
609 /* Link the PCB and the private data struct. and note the extra
610 * reference */
611 sockdata->datasock = pcbp;
612 pcbp->sockdata = sockdata;
613 sockdata->refs++;
614 return (0);
618 * Connect the existing control socket node to a named node:hook.
619 * The hook we use on this end is the same name as the remote node name.
621 static int
622 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
624 struct ngsock *const sockdata = pcbp->sockdata;
625 struct sockaddr_ng *sap;
626 char *node, *hook;
627 node_p farnode;
628 int rtn, error;
630 sap = (struct sockaddr_ng *) nam;
631 rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
632 if (rtn < 0 || node == NULL || hook == NULL) {
633 TRAP_ERROR;
634 return (EINVAL);
636 farnode = ng_findname(sockdata->node, node);
637 if (farnode == NULL) {
638 TRAP_ERROR;
639 return (EADDRNOTAVAIL);
642 /* Connect, using a hook name the same as the far node name. */
643 error = ng_con_nodes(sockdata->node, node, farnode, hook);
644 return error;
648 * Binding a socket means giving the corresponding node a name
650 static int
651 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
653 struct ngsock *const sockdata = pcbp->sockdata;
654 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
656 if (sockdata == NULL) {
657 TRAP_ERROR;
658 return (EINVAL);
660 if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
661 TRAP_ERROR;
662 return (EINVAL);
664 return (ng_name_node(sockdata->node, sap->sg_data));
668 * Take a message and pass it up to the control socket associated
669 * with the node.
671 static int
672 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
674 struct socket *const so = pcbp->ng_socket;
675 struct mbuf *mdata;
676 int msglen;
678 /* Copy the message itself into an mbuf chain */
679 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
680 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
682 /* Here we free the message, as we are the end of the line.
683 * We need to do that regardless of whether we got mbufs. */
684 FREE(msg, M_NETGRAPH);
686 if (mdata == NULL) {
687 TRAP_ERROR;
688 return (ENOBUFS);
691 /* Send it up to the socket */
692 if (ssb_appendaddr(&so->so_rcv,
693 (struct sockaddr *) addr, mdata, NULL) == 0) {
694 TRAP_ERROR;
695 m_freem(mdata);
696 return (ENOBUFS);
698 sorwakeup(so);
699 return (0);
703 * You can only create new nodes from the socket end of things.
705 static int
706 ngs_constructor(node_p *nodep)
708 return (EINVAL);
712 * We allow any hook to be connected to the node.
713 * There is no per-hook private information though.
715 static int
716 ngs_newhook(node_p node, hook_p hook, const char *name)
718 hook->private = node->private;
719 return (0);
723 * Incoming messages get passed up to the control socket.
724 * Unless they are for us specifically (socket_type)
726 static int
727 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
728 struct ng_mesg **resp)
730 struct ngsock *const sockdata = node->private;
731 struct ngpcb *const pcbp = sockdata->ctlsock;
732 struct sockaddr_ng *addr;
733 int addrlen;
734 int error = 0;
736 /* Only allow mesgs to be passed if we have the control socket.
737 * Data sockets can only support the generic messages. */
738 if (pcbp == NULL) {
739 TRAP_ERROR;
740 return (EINVAL);
743 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
744 switch (msg->header.cmd) {
745 case NGM_SOCK_CMD_NOLINGER:
746 sockdata->flags |= NGS_FLAG_NOLINGER;
747 break;
748 case NGM_SOCK_CMD_LINGER:
749 sockdata->flags &= ~NGS_FLAG_NOLINGER;
750 break;
751 default:
752 error = EINVAL; /* unknown command */
754 /* Free the message and return */
755 FREE(msg, M_NETGRAPH);
756 return(error);
759 /* Get the return address into a sockaddr */
760 if ((retaddr == NULL) || (*retaddr == '\0'))
761 retaddr = "";
762 addrlen = strlen(retaddr);
763 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH, M_NOWAIT);
764 if (addr == NULL) {
765 TRAP_ERROR;
766 return (ENOMEM);
768 addr->sg_len = addrlen + 3;
769 addr->sg_family = AF_NETGRAPH;
770 bcopy(retaddr, addr->sg_data, addrlen);
771 addr->sg_data[addrlen] = '\0';
773 /* Send it up */
774 error = ship_msg(pcbp, msg, addr);
775 FREE(addr, M_NETGRAPH);
776 return (error);
780 * Receive data on a hook
782 static int
783 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
785 struct ngsock *const sockdata = hook->node->private;
786 struct ngpcb *const pcbp = sockdata->datasock;
787 struct socket *so;
788 struct sockaddr_ng *addr;
789 char *addrbuf[NG_HOOKSIZ + 4];
790 int addrlen;
792 /* If there is no data socket, black-hole it */
793 if (pcbp == NULL) {
794 NG_FREE_DATA(m, meta);
795 return (0);
797 so = pcbp->ng_socket;
799 /* Get the return address into a sockaddr. */
800 addrlen = strlen(hook->name); /* <= NG_HOOKSIZ - 1 */
801 addr = (struct sockaddr_ng *) addrbuf;
802 addr->sg_len = addrlen + 3;
803 addr->sg_family = AF_NETGRAPH;
804 bcopy(hook->name, addr->sg_data, addrlen);
805 addr->sg_data[addrlen] = '\0';
807 /* We have no use for the meta data, free/clear it now. */
808 NG_FREE_META(meta);
810 /* Try to tell the socket which hook it came in on */
811 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
812 m_freem(m);
813 TRAP_ERROR;
814 return (ENOBUFS);
816 sorwakeup(so);
817 return (0);
821 * Hook disconnection
823 * For this type, removal of the last link destroys the node
824 * if the NOLINGER flag is set.
826 static int
827 ngs_disconnect(hook_p hook)
829 struct ngsock *const sockdata = hook->node->private;
831 if ((sockdata->flags & NGS_FLAG_NOLINGER )
832 && (hook->node->numhooks == 0)) {
833 ng_rmnode(hook->node);
835 return (0);
839 * Do local shutdown processing.
840 * In this case, that involves making sure the socket
841 * knows we should be shutting down.
843 static int
844 ngs_rmnode(node_p node)
846 struct ngsock *const sockdata = node->private;
847 struct ngpcb *const dpcbp = sockdata->datasock;
848 struct ngpcb *const pcbp = sockdata->ctlsock;
850 ng_cutlinks(node);
851 ng_unname(node);
853 if (dpcbp != NULL) {
854 soisdisconnected(dpcbp->ng_socket);
855 dpcbp->sockdata = NULL;
856 sockdata->datasock = NULL;
857 sockdata->refs--;
859 if (pcbp != NULL) {
860 soisdisconnected(pcbp->ng_socket);
861 pcbp->sockdata = NULL;
862 sockdata->ctlsock = NULL;
863 sockdata->refs--;
865 node->private = NULL;
866 ng_unref(node);
867 FREE(sockdata, M_NETGRAPH);
868 return (0);
872 * Control and data socket type descriptors
875 static struct pr_usrreqs ngc_usrreqs = {
876 .pru_abort = NULL,
877 .pru_accept = pru_accept_notsupp,
878 .pru_attach = ngc_attach,
879 .pru_bind = ngc_bind,
880 .pru_connect = ngc_connect,
881 .pru_connect2 = pru_connect2_notsupp,
882 .pru_control = pru_control_notsupp,
883 .pru_detach = ngc_detach,
884 .pru_disconnect = NULL,
885 .pru_listen = pru_listen_notsupp,
886 .pru_peeraddr = NULL,
887 .pru_rcvd = pru_rcvd_notsupp,
888 .pru_rcvoob = pru_rcvoob_notsupp,
889 .pru_send = ngc_send,
890 .pru_sense = pru_sense_null,
891 .pru_shutdown = NULL,
892 .pru_sockaddr = ng_setsockaddr,
893 .pru_sosend = sosend,
894 .pru_soreceive = soreceive,
895 .pru_sopoll = sopoll
898 static struct pr_usrreqs ngd_usrreqs = {
899 .pru_abort = NULL,
900 .pru_accept = pru_accept_notsupp,
901 .pru_attach = ngd_attach,
902 .pru_bind = NULL,
903 .pru_connect = ngd_connect,
904 .pru_connect2 = pru_connect2_notsupp,
905 .pru_control = pru_control_notsupp,
906 .pru_detach = ngd_detach,
907 .pru_disconnect = NULL,
908 .pru_listen = pru_listen_notsupp,
909 .pru_peeraddr = NULL,
910 .pru_rcvd = pru_rcvd_notsupp,
911 .pru_rcvoob = pru_rcvoob_notsupp,
912 .pru_send = ngd_send,
913 .pru_sense = pru_sense_null,
914 .pru_shutdown = NULL,
915 .pru_sockaddr = ng_setsockaddr,
916 .pru_sosend = sosend,
917 .pru_soreceive = soreceive,
918 .pru_sopoll = sopoll
922 * Definitions of protocols supported in the NETGRAPH domain.
925 extern struct domain ngdomain; /* stop compiler warnings */
927 static struct protosw ngsw[] = {
929 SOCK_DGRAM,
930 &ngdomain,
931 NG_CONTROL,
932 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
933 0, 0, 0, 0,
934 cpu0_soport,
935 0, 0, 0, 0,
936 &ngc_usrreqs
939 SOCK_DGRAM,
940 &ngdomain,
941 NG_DATA,
942 PR_ATOMIC | PR_ADDR,
943 0, 0, 0, 0,
944 cpu0_soport,
945 0, 0, 0, 0,
946 &ngd_usrreqs
950 struct domain ngdomain = {
951 AF_NETGRAPH, "netgraph", NULL, NULL, NULL,
952 ngsw, &ngsw[sizeof(ngsw) / sizeof(ngsw[0])],
956 * Handle loading and unloading for this node type
957 * This is to handle auxiliary linkages (e.g protocol domain addition).
959 static int
960 ngs_mod_event(module_t mod, int event, void *data)
962 int error = 0;
964 switch (event) {
965 case MOD_LOAD:
966 /* Register protocol domain */
967 net_add_domain(&ngdomain);
968 break;
969 case MOD_UNLOAD:
970 /* Insure there are no open netgraph sockets */
971 if (!LIST_EMPTY(&ngsocklist)) {
972 error = EBUSY;
973 break;
976 #ifdef NOTYET
977 /* Unregister protocol domain XXX can't do this yet.. */
978 if ((error = net_rm_domain(&ngdomain)) != 0)
979 break;
980 #else
981 error = EBUSY;
982 #endif
983 break;
984 default:
985 error = EOPNOTSUPP;
986 break;
988 return (error);
991 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
992 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
993 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
994 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
995 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");