Fix UTIME_OMIT handling
[dragonfly.git] / sys / netgraph / socket / ng_socket.c
blob52cb0763f374c6e836174479f2a40307859b3e6b
2 /*
3 * ng_socket.c
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
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 * $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>
53 #include <sys/proc.h>
54 #include <sys/caps.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 #ifdef NOTYET
67 #include <sys/vnode.h>
68 #endif
70 #include <sys/thread2.h>
71 #include <sys/socketvar2.h>
72 #include <sys/msgport2.h>
74 #include <netgraph/ng_message.h>
75 #include <netgraph/netgraph.h>
76 #include "ng_socketvar.h"
77 #include "ng_socket.h"
80 * It's Ascii-art time!
81 * +-------------+ +-------------+
82 * |socket (ctl)| |socket (data)|
83 * +-------------+ +-------------+
84 * ^ ^
85 * | |
86 * v v
87 * +-----------+ +-----------+
88 * |pcb (ctl)| |pcb (data)|
89 * +-----------+ +-----------+
90 * ^ ^
91 * | |
92 * v v
93 * +--------------------------+
94 * | Socket type private |
95 * | data |
96 * +--------------------------+
97 * ^
98 * |
99 * v
100 * +----------------+
101 * | struct ng_node |
102 * +----------------+
105 /* Netgraph node methods */
106 static ng_constructor_t ngs_constructor;
107 static ng_rcvmsg_t ngs_rcvmsg;
108 static ng_shutdown_t ngs_rmnode;
109 static ng_newhook_t ngs_newhook;
110 static ng_rcvdata_t ngs_rcvdata;
111 static ng_disconnect_t ngs_disconnect;
113 /* Internal methods */
114 static int ng_attach_data(struct socket *so);
115 static int ng_attach_cntl(struct socket *so);
116 static int ng_attach_common(struct socket *so, int type);
117 static void ng_detach_common(struct ngpcb *pcbp, int type);
118 /*static int ng_internalize(struct mbuf *m, struct thread *td); */
120 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
121 static int ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp);
122 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
124 static int ngs_mod_event(module_t mod, int event, void *data);
125 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
126 struct sockaddr_ng *addr);
128 /* Netgraph type descriptor */
129 static struct ng_type typestruct = {
130 NG_VERSION,
131 NG_SOCKET_NODE_TYPE,
132 ngs_mod_event,
133 ngs_constructor,
134 ngs_rcvmsg,
135 ngs_rmnode,
136 ngs_newhook,
137 NULL,
138 NULL,
139 ngs_rcvdata,
140 ngs_rcvdata,
141 ngs_disconnect,
142 NULL
144 NETGRAPH_INIT(socket, &typestruct);
146 /* Buffer space */
147 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
148 static u_long ngpdg_recvspace = 20 * 1024;
150 /* List of all sockets */
151 LIST_HEAD(, ngpcb) ngsocklist;
153 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
155 /* If getting unexplained errors returned, set this to "Debugger("X"); */
156 #ifndef TRAP_ERROR
157 #define TRAP_ERROR
158 #endif
160 /***************************************************************
161 Control sockets
162 ***************************************************************/
164 static void
165 ngc_attach(netmsg_t msg)
167 struct socket *so = msg->attach.base.nm_so;
168 struct pru_attach_info *ai = msg->attach.nm_ai;
169 struct ngpcb *const pcbp = sotongpcb(so);
170 int error;
172 if (caps_priv_check(ai->p_ucred,
173 SYSCAP_RESTRICTEDROOT | __SYSCAP_NULLCRED) != 0)
175 error = EPERM;
177 else if (pcbp != NULL)
178 error = EISCONN;
179 else
180 error = ng_attach_cntl(so);
181 lwkt_replymsg(&msg->attach.base.lmsg, error);
184 static void
185 ngc_detach(netmsg_t msg)
187 struct socket *so = msg->detach.base.nm_so;
188 struct ngpcb *const pcbp = sotongpcb(so);
189 int error;
191 if (pcbp) {
192 ng_detach_common(pcbp, NG_CONTROL);
193 error = 0;
194 } else {
195 error = EINVAL;
197 lwkt_replymsg(&msg->detach.base.lmsg, error);
200 static void
201 ngc_send(netmsg_t msg)
203 struct socket *so = msg->send.base.nm_so;
204 struct mbuf *m = msg->send.nm_m;
205 struct sockaddr *addr = msg->send.nm_addr;
206 struct mbuf *control = msg->send.nm_control;
207 struct ngpcb *const pcbp = sotongpcb(so);
208 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
209 struct ng_mesg *resp;
210 struct mbuf *m0;
211 char *xmsg, *path = NULL;
212 int len;
213 int error = 0;
215 if (pcbp == NULL) {
216 error = EINVAL;
217 goto release;
219 #ifdef NOTYET
220 if (control && (error = ng_internalize(control, p))) {
221 if (pcbp->sockdata == NULL) {
222 error = ENOTCONN;
223 goto release;
226 #else /* NOTYET */
227 if (control) {
228 error = EINVAL;
229 goto release;
231 #endif /* NOTYET */
233 /* Require destination as there may be >= 1 hooks on this node */
234 if (addr == NULL) {
235 error = EDESTADDRREQ;
236 goto release;
239 /* Allocate an expendable buffer for the path, chop off
240 * the sockaddr header, and make sure it's NUL terminated */
241 len = sap->sg_len - 2;
242 path = kmalloc(len + 1, M_NETGRAPH, M_WAITOK);
243 bcopy(sap->sg_data, path, len);
244 path[len] = '\0';
246 /* Move the actual message out of mbufs into a linear buffer.
247 * Start by adding up the size of the data. (could use mh_len?) */
248 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
249 len += m0->m_len;
251 /* Move the data into a linear buffer as well. Messages are not
252 * delivered in mbufs. */
253 xmsg = kmalloc(len + 1, M_NETGRAPH, M_WAITOK);
254 m_copydata(m, 0, len, xmsg);
256 /* The callee will free the xmsg when done. The addr is our business. */
257 error = ng_send_msg(pcbp->sockdata->node,
258 (struct ng_mesg *) xmsg, path, &resp);
260 /* If the callee responded with a synchronous response, then put it
261 * back on the receive side of the socket; sap is source address. */
262 if (error == 0 && resp != NULL)
263 error = ship_msg(pcbp, resp, sap);
265 release:
266 if (path != NULL)
267 kfree(path, M_NETGRAPH);
268 if (control != NULL)
269 m_freem(control);
270 if (m != NULL)
271 m_freem(m);
272 lwkt_replymsg(&msg->send.base.lmsg, error);
275 static void
276 ngc_bind(netmsg_t msg)
278 struct socket *so = msg->bind.base.nm_so;
279 struct sockaddr *nam = msg->bind.nm_nam;
280 struct ngpcb *const pcbp = sotongpcb(so);
281 int error;
283 if (pcbp)
284 error = ng_bind(nam, pcbp);
285 else
286 error = EINVAL;
287 lwkt_replymsg(&msg->bind.base.lmsg, error);
290 static void
291 ngc_connect(netmsg_t msg)
293 struct socket *so = msg->connect.base.nm_so;
294 struct sockaddr *nam = msg->connect.nm_nam;
295 struct ngpcb *const pcbp = sotongpcb(so);
296 int error;
298 if (pcbp)
299 error = ng_connect_cntl(nam, pcbp);
300 else
301 error = EINVAL;
302 lwkt_replymsg(&msg->connect.base.lmsg, error);
305 /***************************************************************
306 Data sockets
307 ***************************************************************/
309 static void
310 ngd_attach(netmsg_t msg)
312 struct socket *so = msg->attach.base.nm_so;
313 struct ngpcb *const pcbp = sotongpcb(so);
314 int error;
316 if (pcbp)
317 error = EISCONN;
318 else
319 error = ng_attach_data(so);
320 lwkt_replymsg(&msg->connect.base.lmsg, error);
323 static void
324 ngd_detach(netmsg_t msg)
326 struct socket *so = msg->detach.base.nm_so;
327 struct ngpcb *const pcbp = sotongpcb(so);
328 int error;
330 if (pcbp) {
331 ng_detach_common(pcbp, NG_DATA);
332 error = 0;
333 } else {
334 error = EINVAL;
336 lwkt_replymsg(&msg->detach.base.lmsg, error);
339 static void
340 ngd_send(netmsg_t msg)
342 struct socket *so = msg->send.base.nm_so;
343 struct mbuf *m = msg->send.nm_m;
344 struct sockaddr *addr = msg->send.nm_addr;
345 struct mbuf *control = msg->send.nm_control;
346 struct ngpcb *const pcbp = sotongpcb(so);
347 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
348 meta_p mp = NULL;
349 int len, error;
350 hook_p hook = NULL;
351 char hookname[NG_HOOKSIZ];
353 if ((pcbp == NULL) || (control != NULL)) {
354 error = EINVAL;
355 goto release;
357 if (pcbp->sockdata == NULL) {
358 error = ENOTCONN;
359 goto release;
362 * If the user used any of these ways to not specify an address
363 * then handle specially.
365 if ((sap == NULL)
366 || ((len = sap->sg_len - 2) <= 0)
367 || (*sap->sg_data == '\0')) {
368 if (pcbp->sockdata->node->numhooks != 1) {
369 error = EDESTADDRREQ;
370 goto release;
373 * if exactly one hook exists, just use it.
374 * Special case to allow write(2) to work on an ng_socket.
376 hook = LIST_FIRST(&pcbp->sockdata->node->hooks);
377 } else {
378 if (len >= NG_HOOKSIZ) {
379 error = EINVAL;
380 goto release;
384 * chop off the sockaddr header, and make sure it's NUL
385 * terminated
387 bcopy(sap->sg_data, hookname, len);
388 hookname[len] = '\0';
390 /* Find the correct hook from 'hookname' */
391 LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
392 if (strcmp(hookname, hook->name) == 0)
393 break;
395 if (hook == NULL)
396 error = EHOSTUNREACH;
399 /* Send data (OK if hook is NULL) */
400 NG_SEND_DATA(error, hook, m, mp); /* makes m NULL */
402 release:
403 if (control != NULL)
404 m_freem(control);
405 if (m != NULL)
406 m_freem(m);
407 lwkt_replymsg(&msg->send.base.lmsg, error);
410 static void
411 ngd_connect(netmsg_t msg)
413 struct socket *so = msg->connect.base.nm_so;
414 struct sockaddr *nam = msg->connect.nm_nam;
415 struct ngpcb *const pcbp = sotongpcb(so);
416 int error;
418 if (pcbp) {
419 error = ng_connect_data(nam, pcbp);
420 } else {
421 error = EINVAL;
423 lwkt_replymsg(&msg->connect.base.lmsg, error);
427 * Used for both data and control sockets
429 static void
430 ng_setsockaddr(netmsg_t msg)
432 struct socket *so = msg->sockaddr.base.nm_so;
433 struct sockaddr **addr = msg->sockaddr.nm_nam;
434 struct ngpcb *pcbp;
435 struct sockaddr_ng *sg;
436 int sg_len, namelen;
437 int error;
439 /* Why isn't sg_data a `char[1]' ? :-( */
440 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
442 crit_enter();
443 pcbp = sotongpcb(so);
444 if ((pcbp == NULL) || (pcbp->sockdata == NULL)) {
445 crit_exit();
446 error = EINVAL;
447 goto out;
450 namelen = 0; /* silence compiler ! */
452 if (pcbp->sockdata->node->name != NULL)
453 sg_len += namelen = strlen(pcbp->sockdata->node->name);
455 sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
457 if (pcbp->sockdata->node->name != NULL)
458 bcopy(pcbp->sockdata->node->name, sg->sg_data, namelen);
459 crit_exit();
461 sg->sg_len = sg_len;
462 sg->sg_family = AF_NETGRAPH;
463 *addr = (struct sockaddr *)sg;
464 error = 0;
465 out:
466 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
470 * Attach a socket to it's protocol specific partner.
471 * For a control socket, actually create a netgraph node and attach
472 * to it as well.
475 static int
476 ng_attach_cntl(struct socket *so)
478 struct ngsock *privdata;
479 struct ngpcb *pcbp;
480 int error;
482 /* Setup protocol control block */
483 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
484 return (error);
485 pcbp = sotongpcb(so);
487 /* Allocate node private info */
488 privdata = kmalloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
490 /* Make the generic node components */
491 if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
492 kfree(privdata, M_NETGRAPH);
493 ng_detach_common(pcbp, NG_CONTROL);
494 return (error);
496 privdata->node->private = privdata;
498 /* Link the pcb and the node private data */
499 privdata->ctlsock = pcbp;
500 pcbp->sockdata = privdata;
501 privdata->refs++;
502 return (0);
505 static int
506 ng_attach_data(struct socket *so)
508 return(ng_attach_common(so, NG_DATA));
512 * Set up a socket protocol control block.
513 * This code is shared between control and data sockets.
515 static int
516 ng_attach_common(struct socket *so, int type)
518 struct ngpcb *pcbp;
519 int error;
521 /* Standard socket setup stuff */
522 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL);
523 if (error)
524 return (error);
526 /* Allocate the pcb */
527 pcbp = kmalloc(sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
528 pcbp->type = type;
530 /* Link the pcb and the socket */
531 soreference(so);
532 so->so_pcb = (caddr_t) pcbp;
533 pcbp->ng_socket = so;
535 /* Add the socket to linked list */
536 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
537 return (0);
541 * Disassociate the socket from it's protocol specific
542 * partner. If it's attached to a node's private data structure,
543 * then unlink from that too. If we were the last socket attached to it,
544 * then shut down the entire node. Shared code for control and data sockets.
546 static void
547 ng_detach_common(struct ngpcb *pcbp, int which)
549 struct ngsock *sockdata;
550 struct socket *so;
552 if (pcbp->sockdata) {
553 sockdata = pcbp->sockdata;
554 pcbp->sockdata = NULL;
555 switch (which) {
556 case NG_CONTROL:
557 sockdata->ctlsock = NULL;
558 break;
559 case NG_DATA:
560 sockdata->datasock = NULL;
561 break;
562 default:
563 panic(__func__);
565 if ((--sockdata->refs == 0) && (sockdata->node != NULL))
566 ng_rmnode(sockdata->node);
568 so = pcbp->ng_socket;
569 so->so_pcb = NULL;
570 pcbp->ng_socket = NULL;
571 sofree(so); /* remove pcb ref */
573 LIST_REMOVE(pcbp, socks);
574 kfree(pcbp, M_PCB);
577 #ifdef NOTYET
579 * File descriptors can be passed into a AF_NETGRAPH socket.
580 * Note, that file descriptors cannot be passed OUT.
581 * Only character device descriptors are accepted.
582 * Character devices are useful to connect a graph to a device,
583 * which after all is the purpose of this whole system.
585 static int
586 ng_internalize(struct mbuf *control, struct thread *td)
588 struct filedesc *fdp = p->p_fd;
589 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
590 struct file *fp;
591 struct vnode *vn;
592 int oldfds;
593 int fd;
595 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
596 cm->cmsg_len != control->m_len) {
597 TRAP_ERROR;
598 return (EINVAL);
601 /* Check there is only one FD. XXX what would more than one signify? */
602 oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
603 if (oldfds != 1) {
604 TRAP_ERROR;
605 return (EINVAL);
608 /* Check that the FD given is legit. and change it to a pointer to a
609 * struct file. */
610 fd = *(int *) (cm + 1);
611 if ((unsigned) fd >= fdp->fd_nfiles
612 || (fp = fdp->fd_files[fd].fp) == NULL) {
613 return (EBADF);
616 /* Depending on what kind of resource it is, act differently. For
617 * devices, we treat it as a file. For a AF_NETGRAPH socket,
618 * shortcut straight to the node. */
619 switch (fp->f_type) {
620 case DTYPE_VNODE:
621 vn = (struct vnode *) fp->f_data;
622 if (vn && (vn->v_type == VCHR)) {
623 /* for a VCHR, actually reference the FILE */
624 fhold(fp);
625 /* XXX then what :) */
626 /* how to pass on to other modules? */
627 } else {
628 TRAP_ERROR;
629 return (EINVAL);
631 break;
632 default:
633 TRAP_ERROR;
634 return (EINVAL);
636 return (0);
638 #endif /* NOTYET */
641 * Connect the data socket to a named control socket node.
643 static int
644 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
646 struct sockaddr_ng *sap;
647 node_p farnode;
648 struct ngsock *sockdata;
649 int error;
651 /* If we are already connected, don't do it again */
652 if (pcbp->sockdata != NULL)
653 return (EISCONN);
655 /* Find the target (victim) and check it doesn't already have a data
656 * socket. Also check it is a 'socket' type node. */
657 sap = (struct sockaddr_ng *) nam;
658 if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
659 return (error);
661 if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
662 return (EINVAL);
663 sockdata = farnode->private;
664 if (sockdata->datasock != NULL)
665 return (EADDRINUSE);
667 /* Link the PCB and the private data struct. and note the extra
668 * reference */
669 sockdata->datasock = pcbp;
670 pcbp->sockdata = sockdata;
671 sockdata->refs++;
672 return (0);
676 * Connect the existing control socket node to a named node:hook.
677 * The hook we use on this end is the same name as the remote node name.
679 static int
680 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
682 struct ngsock *const sockdata = pcbp->sockdata;
683 struct sockaddr_ng *sap;
684 char *node, *hook;
685 node_p farnode;
686 int rtn, error;
688 sap = (struct sockaddr_ng *) nam;
689 rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
690 if (rtn < 0 || node == NULL || hook == NULL) {
691 TRAP_ERROR;
692 return (EINVAL);
694 farnode = ng_findname(sockdata->node, node);
695 if (farnode == NULL) {
696 TRAP_ERROR;
697 return (EADDRNOTAVAIL);
700 /* Connect, using a hook name the same as the far node name. */
701 error = ng_con_nodes(sockdata->node, node, farnode, hook);
702 return error;
706 * Binding a socket means giving the corresponding node a name
708 static int
709 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
711 struct ngsock *const sockdata = pcbp->sockdata;
712 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
714 if (sockdata == NULL) {
715 TRAP_ERROR;
716 return (EINVAL);
718 if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
719 TRAP_ERROR;
720 return (EINVAL);
722 return (ng_name_node(sockdata->node, sap->sg_data));
726 * Take a message and pass it up to the control socket associated
727 * with the node.
729 static int
730 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
732 struct socket *const so = pcbp->ng_socket;
733 struct mbuf *mdata;
734 int msglen;
736 /* Copy the message itself into an mbuf chain */
737 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
738 mdata = m_devget(msg, msglen, 0, NULL);
740 /* Here we free the message, as we are the end of the line.
741 * We need to do that regardless of whether we got mbufs. */
742 kfree(msg, M_NETGRAPH);
744 if (mdata == NULL) {
745 TRAP_ERROR;
746 return (ENOBUFS);
749 /* Send it up to the socket */
750 lwkt_gettoken(&so->so_rcv.ssb_token);
751 if (ssb_appendaddr(&so->so_rcv,
752 (struct sockaddr *) addr, mdata, NULL) == 0) {
753 soroverflow(so);
754 lwkt_reltoken(&so->so_rcv.ssb_token);
755 TRAP_ERROR;
756 m_freem(mdata);
757 return (ENOBUFS);
759 sorwakeup(so);
760 lwkt_reltoken(&so->so_rcv.ssb_token);
761 return (0);
765 * You can only create new nodes from the socket end of things.
767 static int
768 ngs_constructor(node_p *nodep)
770 return (EINVAL);
774 * We allow any hook to be connected to the node.
775 * There is no per-hook private information though.
777 static int
778 ngs_newhook(node_p node, hook_p hook, const char *name)
780 hook->private = node->private;
781 return (0);
785 * Incoming messages get passed up to the control socket.
786 * Unless they are for us specifically (socket_type)
788 static int
789 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
790 struct ng_mesg **resp)
792 struct ngsock *const sockdata = node->private;
793 struct ngpcb *const pcbp = sockdata->ctlsock;
794 struct sockaddr_ng *addr;
795 int addrlen;
796 int error = 0;
798 /* Only allow mesgs to be passed if we have the control socket.
799 * Data sockets can only support the generic messages. */
800 if (pcbp == NULL) {
801 TRAP_ERROR;
802 return (EINVAL);
805 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
806 switch (msg->header.cmd) {
807 case NGM_SOCK_CMD_NOLINGER:
808 sockdata->flags |= NGS_FLAG_NOLINGER;
809 break;
810 case NGM_SOCK_CMD_LINGER:
811 sockdata->flags &= ~NGS_FLAG_NOLINGER;
812 break;
813 default:
814 error = EINVAL; /* unknown command */
816 /* Free the message and return */
817 kfree(msg, M_NETGRAPH);
818 return(error);
821 /* Get the return address into a sockaddr */
822 if ((retaddr == NULL) || (*retaddr == '\0'))
823 retaddr = "";
824 addrlen = strlen(retaddr);
825 addr = kmalloc(addrlen + 4, M_NETGRAPH, M_NOWAIT);
826 if (addr == NULL) {
827 TRAP_ERROR;
828 return (ENOMEM);
830 addr->sg_len = addrlen + 3;
831 addr->sg_family = AF_NETGRAPH;
832 bcopy(retaddr, addr->sg_data, addrlen);
833 addr->sg_data[addrlen] = '\0';
835 /* Send it up */
836 error = ship_msg(pcbp, msg, addr);
837 kfree(addr, M_NETGRAPH);
838 return (error);
842 * Receive data on a hook
844 static int
845 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
847 struct ngsock *const sockdata = hook->node->private;
848 struct ngpcb *const pcbp = sockdata->datasock;
849 struct socket *so;
850 struct sockaddr_ng *addr;
851 char *addrbuf[NG_HOOKSIZ + 4];
852 int addrlen;
854 /* If there is no data socket, black-hole it */
855 if (pcbp == NULL) {
856 NG_FREE_DATA(m, meta);
857 return (0);
859 so = pcbp->ng_socket;
861 /* Get the return address into a sockaddr. */
862 addrlen = strlen(hook->name); /* <= NG_HOOKSIZ - 1 */
863 addr = (struct sockaddr_ng *) addrbuf;
864 addr->sg_len = addrlen + 3;
865 addr->sg_family = AF_NETGRAPH;
866 bcopy(hook->name, addr->sg_data, addrlen);
867 addr->sg_data[addrlen] = '\0';
869 /* We have no use for the meta data, free/clear it now. */
870 NG_FREE_META(meta);
872 /* Try to tell the socket which hook it came in on */
873 lwkt_gettoken(&so->so_rcv.ssb_token);
874 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
875 soroverflow(so);
876 lwkt_reltoken(&so->so_rcv.ssb_token);
877 m_freem(m);
878 TRAP_ERROR;
879 return (ENOBUFS);
881 sorwakeup(so);
882 lwkt_reltoken(&so->so_rcv.ssb_token);
883 return (0);
887 * Hook disconnection
889 * For this type, removal of the last link destroys the node
890 * if the NOLINGER flag is set.
892 static int
893 ngs_disconnect(hook_p hook)
895 struct ngsock *const sockdata = hook->node->private;
897 if ((sockdata->flags & NGS_FLAG_NOLINGER )
898 && (hook->node->numhooks == 0)) {
899 ng_rmnode(hook->node);
901 return (0);
905 * Do local shutdown processing.
906 * In this case, that involves making sure the socket
907 * knows we should be shutting down.
909 static int
910 ngs_rmnode(node_p node)
912 struct ngsock *const sockdata = node->private;
913 struct ngpcb *const dpcbp = sockdata->datasock;
914 struct ngpcb *const pcbp = sockdata->ctlsock;
916 ng_cutlinks(node);
917 ng_unname(node);
919 if (dpcbp != NULL) {
920 soisdisconnected(dpcbp->ng_socket);
921 dpcbp->sockdata = NULL;
922 sockdata->datasock = NULL;
923 sockdata->refs--;
925 if (pcbp != NULL) {
926 soisdisconnected(pcbp->ng_socket);
927 pcbp->sockdata = NULL;
928 sockdata->ctlsock = NULL;
929 sockdata->refs--;
931 node->private = NULL;
932 ng_unref(node);
933 kfree(sockdata, M_NETGRAPH);
934 return (0);
938 * Control and data socket type descriptors
941 static struct pr_usrreqs ngc_usrreqs = {
942 .pru_abort = NULL,
943 .pru_accept = pr_generic_notsupp,
944 .pru_attach = ngc_attach,
945 .pru_bind = ngc_bind,
946 .pru_connect = ngc_connect,
947 .pru_connect2 = pr_generic_notsupp,
948 .pru_control = pr_generic_notsupp,
949 .pru_detach = ngc_detach,
950 .pru_disconnect = NULL,
951 .pru_listen = pr_generic_notsupp,
952 .pru_peeraddr = NULL,
953 .pru_rcvd = pr_generic_notsupp,
954 .pru_rcvoob = pr_generic_notsupp,
955 .pru_send = ngc_send,
956 .pru_sense = pru_sense_null,
957 .pru_shutdown = NULL,
958 .pru_sockaddr = ng_setsockaddr,
959 .pru_sosend = sosend,
960 .pru_soreceive = soreceive
963 static struct pr_usrreqs ngd_usrreqs = {
964 .pru_abort = NULL,
965 .pru_accept = pr_generic_notsupp,
966 .pru_attach = ngd_attach,
967 .pru_bind = NULL,
968 .pru_connect = ngd_connect,
969 .pru_connect2 = pr_generic_notsupp,
970 .pru_control = pr_generic_notsupp,
971 .pru_detach = ngd_detach,
972 .pru_disconnect = NULL,
973 .pru_listen = pr_generic_notsupp,
974 .pru_peeraddr = NULL,
975 .pru_rcvd = pr_generic_notsupp,
976 .pru_rcvoob = pr_generic_notsupp,
977 .pru_send = ngd_send,
978 .pru_sense = pru_sense_null,
979 .pru_shutdown = NULL,
980 .pru_sockaddr = ng_setsockaddr,
981 .pru_sosend = sosend,
982 .pru_soreceive = soreceive
986 * Definitions of protocols supported in the NETGRAPH domain.
989 extern struct domain ngdomain; /* stop compiler warnings */
991 static struct protosw ngsw[] = {
993 .pr_type = SOCK_DGRAM,
994 .pr_domain = &ngdomain,
995 .pr_protocol = NG_CONTROL,
996 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
997 .pr_usrreqs = &ngc_usrreqs
1000 .pr_type = SOCK_DGRAM,
1001 .pr_domain = &ngdomain,
1002 .pr_protocol = NG_DATA,
1003 .pr_flags = PR_ATOMIC | PR_ADDR,
1004 .pr_usrreqs = &ngd_usrreqs
1008 struct domain ngdomain = {
1009 .dom_family = AF_NETGRAPH,
1010 .dom_name = "netgraph",
1011 .dom_protosw = ngsw,
1012 .dom_protoswNPROTOSW = &ngsw[NELEM(ngsw)],
1016 * Handle loading and unloading for this node type
1017 * This is to handle auxiliary linkages (e.g protocol domain addition).
1019 static int
1020 ngs_mod_event(module_t mod, int event, void *data)
1022 int error = 0;
1024 switch (event) {
1025 case MOD_LOAD:
1026 /* Register protocol domain */
1027 net_add_domain(&ngdomain);
1028 break;
1029 case MOD_UNLOAD:
1030 /* Insure there are no open netgraph sockets */
1031 if (!LIST_EMPTY(&ngsocklist)) {
1032 error = EBUSY;
1033 break;
1036 #ifdef NOTYET
1037 /* Unregister protocol domain XXX can't do this yet.. */
1038 if ((error = net_rm_domain(&ngdomain)) != 0)
1039 break;
1040 #else
1041 error = EBUSY;
1042 #endif
1043 break;
1044 default:
1045 error = EOPNOTSUPP;
1046 break;
1048 return (error);
1051 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1052 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1053 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1054 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1055 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");