]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_socket.c
This commit was generated by cvs2svn to compensate for changes in r52744,
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_socket.c
1
2 /*
3  * ng_socket.c
4  *
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.
18  * 
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.
36  *
37  * Author: Julian Elischer <julian@whistle.com>
38  *
39  * $FreeBSD$
40  * $Whistle: ng_socket.c,v 1.25 1999/01/28 23:54:54 julian Exp $
41  */
42
43 /*
44  * Netgraph socket nodes
45  *
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.
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/domain.h>
55 #include <sys/errno.h>
56 #include <sys/kernel.h>
57 #include <sys/file.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
69 #include <netgraph/ng_message.h>
70 #include <netgraph/netgraph.h>
71 #include <netgraph/ng_socket.h>
72 #include <netgraph/ng_socketvar.h>
73
74 /*
75  * It's Ascii-art time!
76  *   +-------------+   +-------------+
77  *   |socket  (ctl)|   |socket (data)|
78  *   +-------------+   +-------------+
79  *          ^                 ^
80  *          |                 |
81  *          v                 v
82  *    +-----------+     +-----------+
83  *    |pcb   (ctl)|     |pcb  (data)|
84  *    +-----------+     +-----------+
85  *          ^                 ^
86  *          |                 |
87  *          v                 v
88  *      +--------------------------+
89  *      |   Socket type private    |
90  *      |       data               |
91  *      +--------------------------+
92  *                   ^
93  *                   |
94  *                   v
95  *           +----------------+
96  *           | struct ng_node |
97  *           +----------------+
98  */
99
100 /* Netgraph node methods */
101 static int      ngs_constructor(node_p *nodep);
102 static int      ngs_rcvmsg(node_p node, struct ng_mesg *msg,
103                         const char *retaddr, struct ng_mesg **resp);
104 static int      ngs_rmnode(node_p node);
105 static int      ngs_newhook(node_p node, hook_p hook, const char *name);
106 static int      ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta);
107
108 /* Internal methods */
109 static int      ng_attach_data(struct socket *so);
110 static int      ng_attach_cntl(struct socket *so);
111 static int      ng_attach_common(struct socket *so, int type);
112 static void     ng_detach_common(struct ngpcb *pcbp, int type);
113 /*static int    ng_internalize(struct mbuf *m, struct proc *p); */
114
115 static int      ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
116 static int      ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp);
117 static int      ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
118
119 static int      ngs_mod_event(module_t mod, int event, void *data);
120 static int      ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
121                         struct sockaddr_ng *addr);
122
123 /* Netgraph type descriptor */
124 static struct ng_type typestruct = {
125         NG_VERSION,
126         NG_SOCKET_NODE_TYPE,
127         ngs_mod_event,
128         ngs_constructor,
129         ngs_rcvmsg,
130         ngs_rmnode,
131         ngs_newhook,
132         NULL,
133         NULL,
134         ngs_rcvdata,
135         ngs_rcvdata,
136         NULL,
137 };
138 NETGRAPH_INIT(socket, &typestruct);
139
140 /* Buffer space */
141 static u_long ngpdg_sendspace = 2 * 1024;       /* really max datagram size */
142 static u_long ngpdg_recvspace = 20 * 1024;
143
144 /* List of all sockets */
145 LIST_HEAD(, ngpcb) ngsocklist;
146
147 #define sotongpcb(so) ((struct ngpcb *)so->so_pcb)
148
149 /* If getting unexplained errors returned, set this to "Debugger("X"); */
150 #ifndef TRAP_ERROR
151 #define TRAP_ERROR
152 #endif
153
154 /***************************************************************
155         Control sockets
156 ***************************************************************/
157
158 static int
159 ngc_attach(struct socket *so, int proto, struct proc *p)
160 {
161         struct ngpcb *const pcbp = sotongpcb(so);
162
163         if (pcbp != NULL)
164                 return (EISCONN);
165         return (ng_attach_cntl(so));
166 }
167
168 static int
169 ngc_detach(struct socket *so)
170 {
171         struct ngpcb *const pcbp = sotongpcb(so);
172
173         if (pcbp == NULL)
174                 return (EINVAL);
175         ng_detach_common(pcbp, NG_CONTROL);
176         return (0);
177 }
178
179 static int
180 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
181          struct mbuf *control, struct proc *p)
182 {
183         struct ngpcb *const pcbp = sotongpcb(so);
184         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
185         struct ng_mesg *resp;
186         struct mbuf *m0;
187         char *msg, *path = NULL;
188         int len, error = 0;
189
190         if (pcbp == NULL) {
191                 error = EINVAL;
192                 goto release;
193         }
194 #ifdef  NOTYET
195         if (control && (error = ng_internalize(control, p))) {
196                 if (pcbp->sockdata == NULL) {
197                         error = ENOTCONN;
198                         goto release;
199                 }
200         }
201 #else   /* NOTYET */
202         if (control) {
203                 error = EINVAL;
204                 goto release;
205         }
206 #endif  /* NOTYET */
207
208         /* Require destination as there may be >= 1 hooks on this node */
209         if (addr == NULL) {
210                 error = EDESTADDRREQ;
211                 goto release;
212         }
213
214         /* Allocate an expendable buffer for the path, chop off
215          * the sockaddr header, and make sure it's NUL terminated */
216         len = sap->sg_len - 2;
217         MALLOC(path, char *, len + 1, M_NETGRAPH, M_WAITOK);
218         if (path == NULL) {
219                 error = ENOMEM;
220                 goto release;
221         }
222         bcopy(sap->sg_data, path, len);
223         path[len] = '\0';
224
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;
229
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         if (msg == NULL) {
234                 error = ENOMEM;
235                 goto release;
236         }
237         m_copydata(m, 0, len, msg);
238
239         /* The callee will free the msg when done. The addr is our business. */
240         error = ng_send_msg(pcbp->sockdata->node,
241                             (struct ng_mesg *) msg, path, &resp);
242
243         /* If the callee responded with a synchronous response, then put it
244          * back on the receive side of the socket; sap is source address. */
245         if (error == 0 && resp != NULL)
246                 error = ship_msg(pcbp, resp, sap);
247
248 release:
249         if (path != NULL)
250                 FREE(path, M_NETGRAPH);
251         if (control != NULL)
252                 m_freem(control);
253         if (m != NULL)
254                 m_freem(m);
255         return (error);
256 }
257
258 static int
259 ngc_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
260 {
261         struct ngpcb *const pcbp = sotongpcb(so);
262
263         if (pcbp == 0)
264                 return (EINVAL);
265         return (ng_bind(nam, pcbp));
266 }
267
268 static int
269 ngc_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
270 {
271         struct ngpcb *const pcbp = sotongpcb(so);
272
273         if (pcbp == 0)
274                 return (EINVAL);
275         return (ng_connect_cntl(nam, pcbp));
276 }
277
278 /***************************************************************
279         Data sockets
280 ***************************************************************/
281
282 static int
283 ngd_attach(struct socket *so, int proto, struct proc *p)
284 {
285         struct ngpcb *const pcbp = sotongpcb(so);
286
287         if (pcbp != NULL)
288                 return (EISCONN);
289         return (ng_attach_data(so));
290 }
291
292 static int
293 ngd_detach(struct socket *so)
294 {
295         struct ngpcb *const pcbp = sotongpcb(so);
296
297         if (pcbp == NULL)
298                 return (EINVAL);
299         ng_detach_common(pcbp, NG_DATA);
300         return (0);
301 }
302
303 static int
304 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
305          struct mbuf *control, struct proc *p)
306 {
307         struct ngpcb *const pcbp = sotongpcb(so);
308         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
309         char   *hookname = NULL;
310         meta_p  mp = NULL;
311         int     len, error;
312         hook_p  hook;
313
314         if ((pcbp == NULL) || (control != NULL)) {
315                 error = EINVAL;
316                 goto release;
317         }
318         if (pcbp->sockdata == NULL) {
319                 error = ENOTCONN;
320                 goto release;
321         }
322         if (addr == NULL) {
323                 error = EDESTADDRREQ;
324                 goto release;
325         }
326
327         /* Allocate an expendable buffer for the hook name, chop off
328          * the sockaddr header, and make sure it's NUL terminated */
329         len = sap->sg_len - 2;
330         MALLOC(hookname, char *, len + 1, M_NETGRAPH, M_WAITOK);
331         if (hookname == NULL) {
332                 error = ENOMEM;
333                 goto release;
334         }
335         bcopy(sap->sg_data, hookname, len);
336         hookname[len] = '\0';
337
338         /* Find the correct hook from 'hookname' */
339         LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
340                 if (strcmp(hookname, hook->name) == 0)
341                         break;
342         }
343
344         /* Send data (OK if hook is NULL) */
345         NG_SEND_DATA(error, hook, m, mp);       /* makes m NULL */
346
347 release:
348         if (hookname != NULL)
349                 FREE(hookname, M_NETGRAPH);
350         if (control != NULL)
351                 m_freem(control);
352         if (m != NULL)
353                 m_freem(m);
354         return (error);
355 }
356
357 static int
358 ngd_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
359 {
360         struct ngpcb *const pcbp = sotongpcb(so);
361
362         if (pcbp == 0)
363                 return (EINVAL);
364         return (ng_connect_data(nam, pcbp));
365 }
366
367 /*
368  * Used for both data and control sockets
369  */
370 static int
371 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
372 {
373         struct ngpcb *const pcbp = sotongpcb(so);
374         struct sockaddr *sa;
375         int namelen;
376
377         if (pcbp == 0)
378                 return (EINVAL);
379         if (pcbp->sockdata->node->name != NULL) {
380                 namelen = strlen(pcbp->sockdata->node->name) + 3;
381                 MALLOC(sa, struct sockaddr *, namelen, M_SONAME, M_WAITOK);
382                 if (sa == NULL)
383                         return (ENOMEM);
384                 sa->sa_family = AF_NETGRAPH;
385                 sa->sa_len = namelen;
386                 strcpy(sa->sa_data, pcbp->sockdata->node->name);
387                 *addr = sa;
388         } else
389                 *addr = NULL;           /* XXX check this makes sense */
390         return (0);
391 }
392
393 /*
394  * Attach a socket to it's protocol specific partner.
395  * For a control socket, actually create a netgraph node and attach
396  * to it as well.
397  */
398
399 static int
400 ng_attach_cntl(struct socket *so)
401 {
402         struct ngsock *privdata;
403         struct ngpcb *pcbp;
404         int error;
405
406         /* Setup protocol control block */
407         if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
408                 return (error);
409         pcbp = (struct ngpcb *) so->so_pcb;
410
411         /* Allocate node private info */
412         MALLOC(privdata, struct ngsock *,
413             sizeof(*privdata), M_NETGRAPH, M_WAITOK);
414         if (privdata == NULL) {
415                 ng_detach_common(pcbp, NG_CONTROL);
416                 return (ENOMEM);
417         }
418         bzero(privdata, sizeof(*privdata));
419
420         /* Make the generic node components */
421         if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
422                 FREE(privdata, M_NETGRAPH);
423                 ng_detach_common(pcbp, NG_CONTROL);
424                 return (error);
425         }
426         privdata->node->private = privdata;
427
428         /* Link the pcb and the node private data */
429         privdata->ctlsock = pcbp;
430         pcbp->sockdata = privdata;
431         privdata->refs++;
432         return (0);
433 }
434
435 static int
436 ng_attach_data(struct socket *so)
437 {
438         return(ng_attach_common(so, NG_DATA));
439 }
440
441 /*
442  * Set up a socket protocol control block.
443  * This code is shared between control and data sockets.
444  */
445 static int
446 ng_attach_common(struct socket *so, int type)
447 {
448         struct ngpcb *pcbp;
449         int error;
450
451         /* Standard socket setup stuff */
452         error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
453         if (error)
454                 return (error);
455
456         /* Allocate the pcb */
457         MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK);
458         if (pcbp == NULL)
459                 return (ENOMEM);
460         bzero(pcbp, sizeof(*pcbp));
461         pcbp->type = type;
462
463         /* Link the pcb and the socket */
464         so->so_pcb = (caddr_t) pcbp;
465         pcbp->ng_socket = so;
466
467         /* Add the socket to linked list */
468         LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
469         return (0);
470 }
471
472 /*
473  * Disassociate the socket from it's protocol specific
474  * partner. If it's attached to a node's private data structure,
475  * then unlink from that too. If we were the last socket attached to it,
476  * then shut down the entire node. Shared code for control and data sockets.
477  */
478 static void
479 ng_detach_common(struct ngpcb *pcbp, int which)
480 {
481         struct ngsock *sockdata;
482
483         if (pcbp->sockdata) {
484                 sockdata = pcbp->sockdata;
485                 pcbp->sockdata = NULL;
486                 switch (which) {
487                 case NG_CONTROL:
488                         sockdata->ctlsock = NULL;
489                         break;
490                 case NG_DATA:
491                         sockdata->datasock = NULL;
492                         break;
493                 default:
494                         panic(__FUNCTION__);
495                 }
496                 if ((--sockdata->refs == 0) && (sockdata->node != NULL))
497                         ng_rmnode(sockdata->node);
498         }
499         pcbp->ng_socket->so_pcb = NULL;
500         pcbp->ng_socket = NULL;
501         LIST_REMOVE(pcbp, socks);
502         FREE(pcbp, M_PCB);
503 }
504
505 #ifdef NOTYET
506 /*
507  * File descriptors can be passed into a AF_NETGRAPH socket.
508  * Note, that file descriptors cannot be passed OUT.
509  * Only character device descriptors are accepted.
510  * Character devices are useful to connect a graph to a device,
511  * which after all is the purpose of this whole system.
512  */
513 static int
514 ng_internalize(struct mbuf *control, struct proc *p)
515 {
516         struct filedesc *fdp = p->p_fd;
517         struct cmsghdr *cm = mtod(control, struct cmsghdr *);
518         struct file *fp;
519         struct vnode *vn;
520         int oldfds;
521         int fd;
522
523         if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
524             cm->cmsg_len != control->m_len) {
525                 TRAP_ERROR;
526                 return (EINVAL);
527         }
528
529         /* Check there is only one FD. XXX what would more than one signify? */
530         oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
531         if (oldfds != 1) {
532                 TRAP_ERROR;
533                 return (EINVAL);
534         }
535
536         /* Check that the FD given is legit. and change it to a pointer to a
537          * struct file. */
538         fd = *(int *) (cm + 1);
539         if ((unsigned) fd >= fdp->fd_nfiles
540             || (fp = fdp->fd_ofiles[fd]) == NULL) {
541                 return (EBADF);
542         }
543
544         /* Depending on what kind of resource it is, act differently. For
545          * devices, we treat it as a file. For a AF_NETGRAPH socket,
546          * shortcut straight to the node. */
547         switch (fp->f_type) {
548         case DTYPE_VNODE:
549                 vn = (struct vnode *) fp->f_data;
550                 if (vn && (vn->v_type == VCHR)) {
551                         /* for a VCHR, actually reference the FILE */
552                         fp->f_count++;
553                         /* XXX then what :) */
554                         /* how to pass on to other modules? */
555                 } else {
556                         TRAP_ERROR;
557                         return (EINVAL);
558                 }
559                 break;
560         default:
561                 TRAP_ERROR;
562                 return (EINVAL);
563         }
564         return (0);
565 }
566 #endif  /* NOTYET */
567
568 /*
569  * Connect the data socket to a named control socket node.
570  */
571 static int
572 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
573 {
574         struct sockaddr_ng *sap;
575         node_p farnode;
576         struct ngsock *sockdata;
577         int error;
578
579         /* If we are already connected, don't do it again */
580         if (pcbp->sockdata != NULL)
581                 return (EISCONN);
582
583         /* Find the target (victim) and check it doesn't already have a data
584          * socket. Also check it is a 'socket' type node. */
585         sap = (struct sockaddr_ng *) nam;
586         if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
587                 return (error);
588
589         if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
590                 return (EINVAL);
591         sockdata = farnode->private;
592         if (sockdata->datasock != NULL)
593                 return (EADDRINUSE);
594
595         /* Link the PCB and the private data struct. and note the extra
596          * reference */
597         sockdata->datasock = pcbp;
598         pcbp->sockdata = sockdata;
599         sockdata->refs++;
600         return (0);
601 }
602
603 /*
604  * Connect the existing control socket node to a named node:hook.
605  * The hook we use on this end is the same name as the remote node name.
606  */
607 static int
608 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
609 {
610         struct ngsock *const sockdata = pcbp->sockdata;
611         struct sockaddr_ng *sap;
612         char *node, *hook;
613         node_p farnode;
614         int rtn, error;
615
616         sap = (struct sockaddr_ng *) nam;
617         rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
618         if (rtn < 0 || node == NULL || hook == NULL) {
619                 TRAP_ERROR;
620                 return (EINVAL);
621         }
622         farnode = ng_findname(sockdata->node, node);
623         if (farnode == NULL) {
624                 TRAP_ERROR;
625                 return (EADDRNOTAVAIL);
626         }
627
628         /* Connect, using a hook name the same as the far node name. */
629         error = ng_con_nodes(sockdata->node, node, farnode, hook);
630         return error;
631 }
632
633 /*
634  * Binding a socket means giving the corresponding node a name
635  */
636 static int
637 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
638 {
639         struct ngsock *const sockdata = pcbp->sockdata;
640         struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
641
642         if (sockdata == NULL) {
643                 TRAP_ERROR;
644                 return (EINVAL);
645         }
646         if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
647                 TRAP_ERROR;
648                 return (EINVAL);
649         }
650         return (ng_name_node(sockdata->node, sap->sg_data));
651 }
652
653 /*
654  * Take a message and pass it up to the control socket associated
655  * with the node.
656  */
657 static int
658 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
659 {
660         struct socket *const so = pcbp->ng_socket;
661         struct mbuf *mdata;
662         int msglen;
663
664         /* Copy the message itself into an mbuf chain */
665         msglen = sizeof(struct ng_mesg) + msg->header.arglen;
666         mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
667
668         /* Here we free the message, as we are the end of the line.
669          * We need to do that regardless of whether we got mbufs. */
670         FREE(msg, M_NETGRAPH);
671
672         if (mdata == NULL) {
673                 TRAP_ERROR;
674                 return (ENOBUFS);
675         }
676
677         /* Send it up to the socket */
678         if (sbappendaddr(&so->so_rcv,
679             (struct sockaddr *) addr, mdata, NULL) == 0) {
680                 TRAP_ERROR;
681                 m_freem(mdata);
682                 return (ENOBUFS);
683         }
684         sorwakeup(so);
685         return (0);
686 }
687
688 /*
689  * You can only create new nodes from the socket end of things.
690  */
691 static int
692 ngs_constructor(node_p *nodep)
693 {
694         return (EINVAL);
695 }
696
697 /*
698  * We allow any hook to be connected to the node.
699  * There is no per-hook private information though.
700  */
701 static int
702 ngs_newhook(node_p node, hook_p hook, const char *name)
703 {
704         hook->private = node->private;
705         return (0);
706 }
707
708 /*
709  * Incoming messages get passed up to the control socket.
710  */
711 static int
712 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
713            struct ng_mesg **resp)
714 {
715         struct ngsock *const sockdata = node->private;
716         struct ngpcb *const pcbp = sockdata->ctlsock;
717         struct sockaddr_ng *addr;
718         int addrlen;
719         int error = 0;
720
721         /* Only allow mesgs to be passed if we have the control socket.
722          * Data sockets can only support the generic messages. */
723         if (pcbp == NULL) {
724                 TRAP_ERROR;
725                 return (EINVAL);
726         }
727
728         /* Get the return address into a sockaddr */
729         if ((retaddr == NULL) || (*retaddr == '\0'))
730                 retaddr = "";
731         addrlen = strlen(retaddr);
732         MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH, M_NOWAIT);
733         if (addr == NULL) {
734                 TRAP_ERROR;
735                 return (ENOMEM);
736         }
737         addr->sg_len = addrlen + 3;
738         addr->sg_family = AF_NETGRAPH;
739         bcopy(retaddr, addr->sg_data, addrlen);
740         addr->sg_data[addrlen] = '\0';
741
742         /* Send it up */
743         error = ship_msg(pcbp, msg, addr);
744         FREE(addr, M_NETGRAPH);
745         return (error);
746 }
747
748 /*
749  * Receive data on a hook
750  */
751 static int
752 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
753 {
754         struct ngsock *const sockdata = hook->node->private;
755         struct ngpcb *const pcbp = sockdata->datasock;
756         struct socket *so;
757         struct sockaddr_ng *addr;
758         char *addrbuf[NG_HOOKLEN + 1 + 4];
759         int addrlen;
760
761         /* If there is no data socket, black-hole it */
762         if (pcbp == NULL) {
763                 NG_FREE_DATA(m, meta);
764                 return (0);
765         }
766         so = pcbp->ng_socket;
767
768         /* Get the return address into a sockaddr. */
769         addrlen = strlen(hook->name);   /* <= NG_HOOKLEN */
770         addr = (struct sockaddr_ng *) addrbuf;
771         addr->sg_len = addrlen + 3;
772         addr->sg_family = AF_NETGRAPH;
773         bcopy(hook->name, addr->sg_data, addrlen);
774         addr->sg_data[addrlen] = '\0';
775
776         /* We have no use for the meta data, free/clear it now. */
777         NG_FREE_META(meta);
778
779         /* Try to tell the socket which hook it came in on */
780         if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
781                 m_freem(m);
782                 TRAP_ERROR;
783                 return (ENOBUFS);
784         }
785         sorwakeup(so);
786         return (0);
787 }
788
789 /*
790  * Do local shutdown processing.
791  * In this case, that involves making sure the socket
792  * knows we should be shutting down.
793  */
794 static int
795 ngs_rmnode(node_p node)
796 {
797         struct ngsock *const sockdata = node->private;
798         struct ngpcb *const dpcbp = sockdata->datasock;
799         struct ngpcb *const pcbp = sockdata->ctlsock;
800
801         ng_cutlinks(node);
802         ng_unname(node);
803
804         if (dpcbp != NULL) {
805                 soisdisconnected(dpcbp->ng_socket);
806                 dpcbp->sockdata = NULL;
807                 sockdata->datasock = NULL;
808                 sockdata->refs--;
809         }
810         if (pcbp != NULL) {
811                 soisdisconnected(pcbp->ng_socket);
812                 pcbp->sockdata = NULL;
813                 sockdata->ctlsock = NULL;
814                 sockdata->refs--;
815         }
816         node->private = NULL;
817         ng_unref(node);
818         FREE(sockdata, M_NETGRAPH);
819         return (0);
820 }
821
822 /*
823  * Control and data socket type descriptors
824  */
825
826 static struct pr_usrreqs ngc_usrreqs = {
827         NULL,                   /* abort */
828         pru_accept_notsupp,
829         ngc_attach,
830         ngc_bind,
831         ngc_connect,
832         pru_connect2_notsupp,
833         pru_control_notsupp,
834         ngc_detach,
835         NULL,                   /* disconnect */
836         pru_listen_notsupp,
837         NULL,                   /* setpeeraddr */
838         pru_rcvd_notsupp,
839         pru_rcvoob_notsupp,
840         ngc_send,
841         pru_sense_null,
842         NULL,                   /* shutdown */
843         ng_setsockaddr,
844         sosend,
845         soreceive,
846         sopoll
847 };
848
849 static struct pr_usrreqs ngd_usrreqs = {
850         NULL,                   /* abort */
851         pru_accept_notsupp,
852         ngd_attach,
853         NULL,                   /* bind */
854         ngd_connect,
855         pru_connect2_notsupp,
856         pru_control_notsupp,
857         ngd_detach,
858         NULL,                   /* disconnect */
859         pru_listen_notsupp,
860         NULL,                   /* setpeeraddr */
861         pru_rcvd_notsupp,
862         pru_rcvoob_notsupp,
863         ngd_send,
864         pru_sense_null,
865         NULL,                   /* shutdown */
866         ng_setsockaddr,
867         sosend,
868         soreceive,
869         sopoll
870 };
871
872 /*
873  * Definitions of protocols supported in the NETGRAPH domain.
874  */
875
876 extern struct domain ngdomain;          /* stop compiler warnings */
877
878 static struct protosw ngsw[] = {
879         {
880                 SOCK_DGRAM,
881                 &ngdomain,
882                 NG_CONTROL,
883                 PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
884                 0, 0, 0, 0,
885                 NULL,
886                 0, 0, 0, 0,
887                 &ngc_usrreqs
888         },
889         {
890                 SOCK_DGRAM,
891                 &ngdomain,
892                 NG_DATA,
893                 PR_ATOMIC | PR_ADDR,
894                 0, 0, 0, 0,
895                 NULL,
896                 0, 0, 0, 0,
897                 &ngd_usrreqs
898         }
899 };
900
901 struct domain ngdomain = {
902         AF_NETGRAPH,
903         "netgraph",
904         0,
905         NULL,
906         NULL,
907         ngsw,
908         &ngsw[sizeof(ngsw) / sizeof(ngsw[0])],
909         0,
910         NULL,
911         0,
912         0
913 };
914
915 /*
916  * Handle loading and unloading for this node type
917  * This is to handle auxiliary linkages (e.g protocol domain addition).
918  */
919 static int
920 ngs_mod_event(module_t mod, int event, void *data)
921 {
922         int error = 0;
923
924         switch (event) {
925         case MOD_LOAD:
926                 /* Register protocol domain */
927                 net_add_domain(&ngdomain);
928                 break;
929         case MOD_UNLOAD:
930                 /* Insure there are no open netgraph sockets */
931                 if (!LIST_EMPTY(&ngsocklist)) {
932                         error = EBUSY;
933                         break;
934                 }
935
936 #ifdef NOTYET
937                 /* Unregister protocol domain XXX can't do this yet.. */
938                 if ((error = net_rm_domain(&ngdomain)) != 0)
939                         break;
940 #else
941                 error = EBUSY;
942 #endif
943                 break;
944         default:
945                 error = EOPNOTSUPP;
946                 break;
947         }
948         return (error);
949 }
950
951 SYSCTL_NODE(_net, AF_NETGRAPH, graph, CTLFLAG_RW, 0, "netgraph Family");
952 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
953 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
954 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
955 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
956 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");
957