Merge remote-tracking branch 'origin/vendor/LIBARCHIVE'
[dragonfly.git] / sys / netgraph / socket / 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@freebsd.org>
38  *
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 $
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/priv.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
69
70 #include <sys/thread2.h>
71 #include <sys/socketvar2.h>
72 #include <sys/msgport2.h>
73
74 #include <netgraph/ng_message.h>
75 #include <netgraph/netgraph.h>
76 #include "ng_socketvar.h"
77 #include "ng_socket.h"
78
79 /*
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  *           +----------------+
103  */
104
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;
112
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); */
119
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);
123
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);
127
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
143 };
144 NETGRAPH_INIT(socket, &typestruct);
145
146 /* Buffer space */
147 static u_long ngpdg_sendspace = 20 * 1024;      /* really max datagram size */
148 static u_long ngpdg_recvspace = 20 * 1024;
149
150 /* List of all sockets */
151 LIST_HEAD(, ngpcb) ngsocklist;
152
153 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
154
155 /* If getting unexplained errors returned, set this to "Debugger("X"); */
156 #ifndef TRAP_ERROR
157 #define TRAP_ERROR
158 #endif
159
160 /***************************************************************
161         Control sockets
162 ***************************************************************/
163
164 static void
165 ngc_attach(netmsg_t msg)
166 {
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;
171
172         if (priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY) != 0)
173                 error = EPERM;
174         else if (pcbp != NULL)
175                 error = EISCONN;
176         else
177                 error = ng_attach_cntl(so);
178         lwkt_replymsg(&msg->attach.base.lmsg, error);
179 }
180
181 static void
182 ngc_detach(netmsg_t msg)
183 {
184         struct socket *so = msg->detach.base.nm_so;
185         struct ngpcb *const pcbp = sotongpcb(so);
186         int error;
187
188         if (pcbp) {
189                 ng_detach_common(pcbp, NG_CONTROL);
190                 error = 0;
191         } else {
192                 error = EINVAL;
193         }
194         lwkt_replymsg(&msg->detach.base.lmsg, error);
195 }
196
197 static void
198 ngc_send(netmsg_t msg)
199 {
200         struct socket *so = msg->send.base.nm_so;
201         struct mbuf *m = msg->send.nm_m;
202         struct sockaddr *addr = msg->send.nm_addr;
203         struct mbuf *control = msg->send.nm_control;
204         struct ngpcb *const pcbp = sotongpcb(so);
205         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
206         struct ng_mesg *resp;
207         struct mbuf *m0;
208         char *xmsg, *path = NULL;
209         int len;
210         int error = 0;
211
212         if (pcbp == NULL) {
213                 error = EINVAL;
214                 goto release;
215         }
216 #ifdef  NOTYET
217         if (control && (error = ng_internalize(control, p))) {
218                 if (pcbp->sockdata == NULL) {
219                         error = ENOTCONN;
220                         goto release;
221                 }
222         }
223 #else   /* NOTYET */
224         if (control) {
225                 error = EINVAL;
226                 goto release;
227         }
228 #endif  /* NOTYET */
229
230         /* Require destination as there may be >= 1 hooks on this node */
231         if (addr == NULL) {
232                 error = EDESTADDRREQ;
233                 goto release;
234         }
235
236         /* Allocate an expendable buffer for the path, chop off
237          * the sockaddr header, and make sure it's NUL terminated */
238         len = sap->sg_len - 2;
239         path = kmalloc(len + 1, M_NETGRAPH, M_WAITOK);
240         bcopy(sap->sg_data, path, len);
241         path[len] = '\0';
242
243         /* Move the actual message out of mbufs into a linear buffer.
244          * Start by adding up the size of the data. (could use mh_len?) */
245         for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
246                 len += m0->m_len;
247
248         /* Move the data into a linear buffer as well. Messages are not
249          * delivered in mbufs. */
250         xmsg = kmalloc(len + 1, M_NETGRAPH, M_WAITOK);
251         m_copydata(m, 0, len, xmsg);
252
253         /* The callee will free the xmsg when done. The addr is our business. */
254         error = ng_send_msg(pcbp->sockdata->node,
255                             (struct ng_mesg *) xmsg, path, &resp);
256
257         /* If the callee responded with a synchronous response, then put it
258          * back on the receive side of the socket; sap is source address. */
259         if (error == 0 && resp != NULL)
260                 error = ship_msg(pcbp, resp, sap);
261
262 release:
263         if (path != NULL)
264                 kfree(path, M_NETGRAPH);
265         if (control != NULL)
266                 m_freem(control);
267         if (m != NULL)
268                 m_freem(m);
269         lwkt_replymsg(&msg->send.base.lmsg, error);
270 }
271
272 static void
273 ngc_bind(netmsg_t msg)
274 {
275         struct socket *so = msg->bind.base.nm_so;
276         struct sockaddr *nam = msg->bind.nm_nam;
277         struct ngpcb *const pcbp = sotongpcb(so);
278         int error;
279
280         if (pcbp)
281                 error = ng_bind(nam, pcbp);
282         else
283                 error = EINVAL;
284         lwkt_replymsg(&msg->bind.base.lmsg, error);
285 }
286
287 static void
288 ngc_connect(netmsg_t msg)
289 {
290         struct socket *so = msg->connect.base.nm_so;
291         struct sockaddr *nam = msg->connect.nm_nam;
292         struct ngpcb *const pcbp = sotongpcb(so);
293         int error;
294
295         if (pcbp)
296                 error = ng_connect_cntl(nam, pcbp);
297         else
298                 error = EINVAL;
299         lwkt_replymsg(&msg->connect.base.lmsg, error);
300 }
301
302 /***************************************************************
303         Data sockets
304 ***************************************************************/
305
306 static void
307 ngd_attach(netmsg_t msg)
308 {
309         struct socket *so = msg->attach.base.nm_so;
310         struct ngpcb *const pcbp = sotongpcb(so);
311         int error;
312
313         if (pcbp)
314                 error = EISCONN;
315         else
316                 error = ng_attach_data(so);
317         lwkt_replymsg(&msg->connect.base.lmsg, error);
318 }
319
320 static void
321 ngd_detach(netmsg_t msg)
322 {
323         struct socket *so = msg->detach.base.nm_so;
324         struct ngpcb *const pcbp = sotongpcb(so);
325         int error;
326
327         if (pcbp) {
328                 ng_detach_common(pcbp, NG_DATA);
329                 error = 0;
330         } else {
331                 error = EINVAL;
332         }
333         lwkt_replymsg(&msg->detach.base.lmsg, error);
334 }
335
336 static void
337 ngd_send(netmsg_t msg)
338 {
339         struct socket *so = msg->send.base.nm_so;
340         struct mbuf *m = msg->send.nm_m;
341         struct sockaddr *addr = msg->send.nm_addr;
342         struct mbuf *control = msg->send.nm_control;
343         struct ngpcb *const pcbp = sotongpcb(so);
344         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
345         meta_p  mp = NULL;
346         int     len, error;
347         hook_p  hook = NULL;
348         char    hookname[NG_HOOKSIZ];
349
350         if ((pcbp == NULL) || (control != NULL)) {
351                 error = EINVAL;
352                 goto release;
353         }
354         if (pcbp->sockdata == NULL) {
355                 error = ENOTCONN;
356                 goto release;
357         }
358         /*
359          * If the user used any of these ways to not specify an address
360          * then handle specially.
361          */
362         if ((sap == NULL)
363             || ((len = sap->sg_len - 2) <= 0)
364             || (*sap->sg_data == '\0')) {
365                 if (pcbp->sockdata->node->numhooks != 1) {
366                         error = EDESTADDRREQ;
367                         goto release;
368                 }
369                 /*
370                  * if exactly one hook exists, just use it.
371                  * Special case to allow write(2) to work on an ng_socket.
372                  */
373                 hook = LIST_FIRST(&pcbp->sockdata->node->hooks);
374         } else {
375                 if (len >= NG_HOOKSIZ) {
376                         error = EINVAL;
377                         goto release;
378                 }
379
380                 /*
381                  * chop off the sockaddr header, and make sure it's NUL
382                  * terminated
383                  */
384                 bcopy(sap->sg_data, hookname, len);
385                 hookname[len] = '\0';
386
387                 /* Find the correct hook from 'hookname' */
388                 LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
389                         if (strcmp(hookname, hook->name) == 0)
390                                 break;
391                 }
392                 if (hook == NULL)
393                         error = EHOSTUNREACH;
394         }
395
396         /* Send data (OK if hook is NULL) */
397         NG_SEND_DATA(error, hook, m, mp);       /* makes m NULL */
398
399 release:
400         if (control != NULL)
401                 m_freem(control);
402         if (m != NULL)
403                 m_freem(m);
404         lwkt_replymsg(&msg->send.base.lmsg, error);
405 }
406
407 static void
408 ngd_connect(netmsg_t msg)
409 {
410         struct socket *so = msg->connect.base.nm_so;
411         struct sockaddr *nam = msg->connect.nm_nam;
412         struct ngpcb *const pcbp = sotongpcb(so);
413         int error;
414
415         if (pcbp) {
416                 error = ng_connect_data(nam, pcbp);
417         } else {
418                 error = EINVAL;
419         }
420         lwkt_replymsg(&msg->connect.base.lmsg, error);
421 }
422
423 /*
424  * Used for both data and control sockets
425  */
426 static void
427 ng_setsockaddr(netmsg_t msg)
428 {
429         struct socket *so = msg->sockaddr.base.nm_so;
430         struct sockaddr **addr = msg->sockaddr.nm_nam;
431         struct ngpcb *pcbp;
432         struct sockaddr_ng *sg;
433         int sg_len, namelen;
434         int error;
435
436         /* Why isn't sg_data a `char[1]' ? :-( */
437         sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
438
439         crit_enter();
440         pcbp = sotongpcb(so);
441         if ((pcbp == NULL) || (pcbp->sockdata == NULL)) {
442                 crit_exit();
443                 error = EINVAL;
444                 goto out;
445         }
446
447         namelen = 0;            /* silence compiler ! */
448
449         if (pcbp->sockdata->node->name != NULL)
450                 sg_len += namelen = strlen(pcbp->sockdata->node->name);
451
452         sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
453
454         if (pcbp->sockdata->node->name != NULL)
455                 bcopy(pcbp->sockdata->node->name, sg->sg_data, namelen);
456         crit_exit();
457
458         sg->sg_len = sg_len;
459         sg->sg_family = AF_NETGRAPH;
460         *addr = (struct sockaddr *)sg;
461         error = 0;
462 out:
463         lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
464 }
465
466 /*
467  * Attach a socket to it's protocol specific partner.
468  * For a control socket, actually create a netgraph node and attach
469  * to it as well.
470  */
471
472 static int
473 ng_attach_cntl(struct socket *so)
474 {
475         struct ngsock *privdata;
476         struct ngpcb *pcbp;
477         int error;
478
479         /* Setup protocol control block */
480         if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
481                 return (error);
482         pcbp = sotongpcb(so);
483
484         /* Allocate node private info */
485         privdata = kmalloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
486
487         /* Make the generic node components */
488         if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
489                 kfree(privdata, M_NETGRAPH);
490                 ng_detach_common(pcbp, NG_CONTROL);
491                 return (error);
492         }
493         privdata->node->private = privdata;
494
495         /* Link the pcb and the node private data */
496         privdata->ctlsock = pcbp;
497         pcbp->sockdata = privdata;
498         privdata->refs++;
499         return (0);
500 }
501
502 static int
503 ng_attach_data(struct socket *so)
504 {
505         return(ng_attach_common(so, NG_DATA));
506 }
507
508 /*
509  * Set up a socket protocol control block.
510  * This code is shared between control and data sockets.
511  */
512 static int
513 ng_attach_common(struct socket *so, int type)
514 {
515         struct ngpcb *pcbp;
516         int error;
517
518         /* Standard socket setup stuff */
519         error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL);
520         if (error)
521                 return (error);
522
523         /* Allocate the pcb */
524         pcbp = kmalloc(sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
525         pcbp->type = type;
526
527         /* Link the pcb and the socket */
528         soreference(so);
529         so->so_pcb = (caddr_t) pcbp;
530         pcbp->ng_socket = so;
531
532         /* Add the socket to linked list */
533         LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
534         return (0);
535 }
536
537 /*
538  * Disassociate the socket from it's protocol specific
539  * partner. If it's attached to a node's private data structure,
540  * then unlink from that too. If we were the last socket attached to it,
541  * then shut down the entire node. Shared code for control and data sockets.
542  */
543 static void
544 ng_detach_common(struct ngpcb *pcbp, int which)
545 {
546         struct ngsock *sockdata;
547         struct socket *so;
548
549         if (pcbp->sockdata) {
550                 sockdata = pcbp->sockdata;
551                 pcbp->sockdata = NULL;
552                 switch (which) {
553                 case NG_CONTROL:
554                         sockdata->ctlsock = NULL;
555                         break;
556                 case NG_DATA:
557                         sockdata->datasock = NULL;
558                         break;
559                 default:
560                         panic(__func__);
561                 }
562                 if ((--sockdata->refs == 0) && (sockdata->node != NULL))
563                         ng_rmnode(sockdata->node);
564         }
565         so = pcbp->ng_socket;
566         so->so_pcb = NULL;
567         pcbp->ng_socket = NULL;
568         sofree(so);             /* remove pcb ref */
569
570         LIST_REMOVE(pcbp, socks);
571         kfree(pcbp, M_PCB);
572 }
573
574 #ifdef NOTYET
575 /*
576  * File descriptors can be passed into a AF_NETGRAPH socket.
577  * Note, that file descriptors cannot be passed OUT.
578  * Only character device descriptors are accepted.
579  * Character devices are useful to connect a graph to a device,
580  * which after all is the purpose of this whole system.
581  */
582 static int
583 ng_internalize(struct mbuf *control, struct thread *td)
584 {
585         struct filedesc *fdp = p->p_fd;
586         const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
587         struct file *fp;
588         struct vnode *vn;
589         int oldfds;
590         int fd;
591
592         if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
593             cm->cmsg_len != control->m_len) {
594                 TRAP_ERROR;
595                 return (EINVAL);
596         }
597
598         /* Check there is only one FD. XXX what would more than one signify? */
599         oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
600         if (oldfds != 1) {
601                 TRAP_ERROR;
602                 return (EINVAL);
603         }
604
605         /* Check that the FD given is legit. and change it to a pointer to a
606          * struct file. */
607         fd = *(int *) (cm + 1);
608         if ((unsigned) fd >= fdp->fd_nfiles
609             || (fp = fdp->fd_files[fd].fp) == NULL) {
610                 return (EBADF);
611         }
612
613         /* Depending on what kind of resource it is, act differently. For
614          * devices, we treat it as a file. For a AF_NETGRAPH socket,
615          * shortcut straight to the node. */
616         switch (fp->f_type) {
617         case DTYPE_VNODE:
618                 vn = (struct vnode *) fp->f_data;
619                 if (vn && (vn->v_type == VCHR)) {
620                         /* for a VCHR, actually reference the FILE */
621                         fhold(fp);
622                         /* XXX then what :) */
623                         /* how to pass on to other modules? */
624                 } else {
625                         TRAP_ERROR;
626                         return (EINVAL);
627                 }
628                 break;
629         default:
630                 TRAP_ERROR;
631                 return (EINVAL);
632         }
633         return (0);
634 }
635 #endif  /* NOTYET */
636
637 /*
638  * Connect the data socket to a named control socket node.
639  */
640 static int
641 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
642 {
643         struct sockaddr_ng *sap;
644         node_p farnode;
645         struct ngsock *sockdata;
646         int error;
647
648         /* If we are already connected, don't do it again */
649         if (pcbp->sockdata != NULL)
650                 return (EISCONN);
651
652         /* Find the target (victim) and check it doesn't already have a data
653          * socket. Also check it is a 'socket' type node. */
654         sap = (struct sockaddr_ng *) nam;
655         if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
656                 return (error);
657
658         if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
659                 return (EINVAL);
660         sockdata = farnode->private;
661         if (sockdata->datasock != NULL)
662                 return (EADDRINUSE);
663
664         /* Link the PCB and the private data struct. and note the extra
665          * reference */
666         sockdata->datasock = pcbp;
667         pcbp->sockdata = sockdata;
668         sockdata->refs++;
669         return (0);
670 }
671
672 /*
673  * Connect the existing control socket node to a named node:hook.
674  * The hook we use on this end is the same name as the remote node name.
675  */
676 static int
677 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
678 {
679         struct ngsock *const sockdata = pcbp->sockdata;
680         struct sockaddr_ng *sap;
681         char *node, *hook;
682         node_p farnode;
683         int rtn, error;
684
685         sap = (struct sockaddr_ng *) nam;
686         rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
687         if (rtn < 0 || node == NULL || hook == NULL) {
688                 TRAP_ERROR;
689                 return (EINVAL);
690         }
691         farnode = ng_findname(sockdata->node, node);
692         if (farnode == NULL) {
693                 TRAP_ERROR;
694                 return (EADDRNOTAVAIL);
695         }
696
697         /* Connect, using a hook name the same as the far node name. */
698         error = ng_con_nodes(sockdata->node, node, farnode, hook);
699         return error;
700 }
701
702 /*
703  * Binding a socket means giving the corresponding node a name
704  */
705 static int
706 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
707 {
708         struct ngsock *const sockdata = pcbp->sockdata;
709         struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
710
711         if (sockdata == NULL) {
712                 TRAP_ERROR;
713                 return (EINVAL);
714         }
715         if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
716                 TRAP_ERROR;
717                 return (EINVAL);
718         }
719         return (ng_name_node(sockdata->node, sap->sg_data));
720 }
721
722 /*
723  * Take a message and pass it up to the control socket associated
724  * with the node.
725  */
726 static int
727 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
728 {
729         struct socket *const so = pcbp->ng_socket;
730         struct mbuf *mdata;
731         int msglen;
732
733         /* Copy the message itself into an mbuf chain */
734         msglen = sizeof(struct ng_mesg) + msg->header.arglen;
735         mdata = m_devget((caddr_t) msg, msglen, 0, NULL);
736
737         /* Here we free the message, as we are the end of the line.
738          * We need to do that regardless of whether we got mbufs. */
739         kfree(msg, M_NETGRAPH);
740
741         if (mdata == NULL) {
742                 TRAP_ERROR;
743                 return (ENOBUFS);
744         }
745
746         /* Send it up to the socket */
747         lwkt_gettoken(&so->so_rcv.ssb_token);
748         if (ssb_appendaddr(&so->so_rcv,
749             (struct sockaddr *) addr, mdata, NULL) == 0) {
750                 lwkt_reltoken(&so->so_rcv.ssb_token);
751                 TRAP_ERROR;
752                 m_freem(mdata);
753                 return (ENOBUFS);
754         }
755         sorwakeup(so);
756         lwkt_reltoken(&so->so_rcv.ssb_token);
757         return (0);
758 }
759
760 /*
761  * You can only create new nodes from the socket end of things.
762  */
763 static int
764 ngs_constructor(node_p *nodep)
765 {
766         return (EINVAL);
767 }
768
769 /*
770  * We allow any hook to be connected to the node.
771  * There is no per-hook private information though.
772  */
773 static int
774 ngs_newhook(node_p node, hook_p hook, const char *name)
775 {
776         hook->private = node->private;
777         return (0);
778 }
779
780 /*
781  * Incoming messages get passed up to the control socket.
782  * Unless they are for us specifically (socket_type)
783  */
784 static int
785 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
786            struct ng_mesg **resp)
787 {
788         struct ngsock *const sockdata = node->private;
789         struct ngpcb *const pcbp = sockdata->ctlsock;
790         struct sockaddr_ng *addr;
791         int addrlen;
792         int error = 0;
793
794         /* Only allow mesgs to be passed if we have the control socket.
795          * Data sockets can only support the generic messages. */
796         if (pcbp == NULL) {
797                 TRAP_ERROR;
798                 return (EINVAL);
799         }
800
801         if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
802                 switch (msg->header.cmd) {
803                 case NGM_SOCK_CMD_NOLINGER:
804                         sockdata->flags |= NGS_FLAG_NOLINGER;
805                         break;
806                 case NGM_SOCK_CMD_LINGER:
807                         sockdata->flags &= ~NGS_FLAG_NOLINGER;
808                         break;
809                 default:
810                         error = EINVAL;         /* unknown command */
811                 }
812                 /* Free the message and return */
813                 kfree(msg, M_NETGRAPH);
814                 return(error);
815
816         }
817         /* Get the return address into a sockaddr */
818         if ((retaddr == NULL) || (*retaddr == '\0'))
819                 retaddr = "";
820         addrlen = strlen(retaddr);
821         addr = kmalloc(addrlen + 4, M_NETGRAPH, M_NOWAIT);
822         if (addr == NULL) {
823                 TRAP_ERROR;
824                 return (ENOMEM);
825         }
826         addr->sg_len = addrlen + 3;
827         addr->sg_family = AF_NETGRAPH;
828         bcopy(retaddr, addr->sg_data, addrlen);
829         addr->sg_data[addrlen] = '\0';
830
831         /* Send it up */
832         error = ship_msg(pcbp, msg, addr);
833         kfree(addr, M_NETGRAPH);
834         return (error);
835 }
836
837 /*
838  * Receive data on a hook
839  */
840 static int
841 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
842 {
843         struct ngsock *const sockdata = hook->node->private;
844         struct ngpcb *const pcbp = sockdata->datasock;
845         struct socket *so;
846         struct sockaddr_ng *addr;
847         char *addrbuf[NG_HOOKSIZ + 4];
848         int addrlen;
849
850         /* If there is no data socket, black-hole it */
851         if (pcbp == NULL) {
852                 NG_FREE_DATA(m, meta);
853                 return (0);
854         }
855         so = pcbp->ng_socket;
856
857         /* Get the return address into a sockaddr. */
858         addrlen = strlen(hook->name);   /* <= NG_HOOKSIZ - 1 */
859         addr = (struct sockaddr_ng *) addrbuf;
860         addr->sg_len = addrlen + 3;
861         addr->sg_family = AF_NETGRAPH;
862         bcopy(hook->name, addr->sg_data, addrlen);
863         addr->sg_data[addrlen] = '\0';
864
865         /* We have no use for the meta data, free/clear it now. */
866         NG_FREE_META(meta);
867
868         /* Try to tell the socket which hook it came in on */
869         lwkt_gettoken(&so->so_rcv.ssb_token);
870         if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
871                 lwkt_reltoken(&so->so_rcv.ssb_token);
872                 m_freem(m);
873                 TRAP_ERROR;
874                 return (ENOBUFS);
875         }
876         sorwakeup(so);
877         lwkt_reltoken(&so->so_rcv.ssb_token);
878         return (0);
879 }
880
881 /*
882  * Hook disconnection
883  *
884  * For this type, removal of the last link destroys the node
885  * if the NOLINGER flag is set.
886  */
887 static int
888 ngs_disconnect(hook_p hook)
889 {
890         struct ngsock *const sockdata = hook->node->private;
891
892         if ((sockdata->flags & NGS_FLAG_NOLINGER )
893             && (hook->node->numhooks == 0)) {
894                 ng_rmnode(hook->node);
895         }
896         return (0);
897 }
898
899 /*
900  * Do local shutdown processing.
901  * In this case, that involves making sure the socket
902  * knows we should be shutting down.
903  */
904 static int
905 ngs_rmnode(node_p node)
906 {
907         struct ngsock *const sockdata = node->private;
908         struct ngpcb *const dpcbp = sockdata->datasock;
909         struct ngpcb *const pcbp = sockdata->ctlsock;
910
911         ng_cutlinks(node);
912         ng_unname(node);
913
914         if (dpcbp != NULL) {
915                 soisdisconnected(dpcbp->ng_socket);
916                 dpcbp->sockdata = NULL;
917                 sockdata->datasock = NULL;
918                 sockdata->refs--;
919         }
920         if (pcbp != NULL) {
921                 soisdisconnected(pcbp->ng_socket);
922                 pcbp->sockdata = NULL;
923                 sockdata->ctlsock = NULL;
924                 sockdata->refs--;
925         }
926         node->private = NULL;
927         ng_unref(node);
928         kfree(sockdata, M_NETGRAPH);
929         return (0);
930 }
931
932 /*
933  * Control and data socket type descriptors
934  */
935
936 static struct pr_usrreqs ngc_usrreqs = {
937         .pru_abort = NULL,
938         .pru_accept = pr_generic_notsupp,
939         .pru_attach = ngc_attach,
940         .pru_bind = ngc_bind,
941         .pru_connect = ngc_connect,
942         .pru_connect2 = pr_generic_notsupp,
943         .pru_control = pr_generic_notsupp,
944         .pru_detach = ngc_detach,
945         .pru_disconnect = NULL,
946         .pru_listen = pr_generic_notsupp,
947         .pru_peeraddr = NULL,
948         .pru_rcvd = pr_generic_notsupp,
949         .pru_rcvoob = pr_generic_notsupp,
950         .pru_send = ngc_send,
951         .pru_sense = pru_sense_null,
952         .pru_shutdown = NULL,
953         .pru_sockaddr = ng_setsockaddr,
954         .pru_sosend = sosend,
955         .pru_soreceive = soreceive
956 };
957
958 static struct pr_usrreqs ngd_usrreqs = {
959         .pru_abort = NULL,
960         .pru_accept = pr_generic_notsupp,
961         .pru_attach = ngd_attach,
962         .pru_bind = NULL,
963         .pru_connect = ngd_connect,
964         .pru_connect2 = pr_generic_notsupp,
965         .pru_control = pr_generic_notsupp,
966         .pru_detach = ngd_detach,
967         .pru_disconnect = NULL,
968         .pru_listen = pr_generic_notsupp,
969         .pru_peeraddr = NULL,
970         .pru_rcvd = pr_generic_notsupp,
971         .pru_rcvoob = pr_generic_notsupp,
972         .pru_send = ngd_send,
973         .pru_sense = pru_sense_null,
974         .pru_shutdown = NULL,
975         .pru_sockaddr = ng_setsockaddr,
976         .pru_sosend = sosend,
977         .pru_soreceive = soreceive
978 };
979
980 /*
981  * Definitions of protocols supported in the NETGRAPH domain.
982  */
983
984 extern struct domain ngdomain;          /* stop compiler warnings */
985
986 static struct protosw ngsw[] = {
987     {
988         .pr_type = SOCK_DGRAM,
989         .pr_domain = &ngdomain,
990         .pr_protocol = NG_CONTROL,
991         .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
992         .pr_usrreqs = &ngc_usrreqs
993     },
994     {
995         .pr_type = SOCK_DGRAM,
996         .pr_domain = &ngdomain,
997         .pr_protocol = NG_DATA,
998         .pr_flags = PR_ATOMIC | PR_ADDR,
999         .pr_usrreqs = &ngd_usrreqs
1000     }
1001 };
1002
1003 struct domain ngdomain = {
1004         AF_NETGRAPH, "netgraph", NULL, NULL, NULL,
1005         ngsw, &ngsw[NELEM(ngsw)],
1006 };
1007
1008 /*
1009  * Handle loading and unloading for this node type
1010  * This is to handle auxiliary linkages (e.g protocol domain addition).
1011  */
1012 static int
1013 ngs_mod_event(module_t mod, int event, void *data)
1014 {
1015         int error = 0;
1016
1017         switch (event) {
1018         case MOD_LOAD:
1019                 /* Register protocol domain */
1020                 net_add_domain(&ngdomain);
1021                 break;
1022         case MOD_UNLOAD:
1023                 /* Insure there are no open netgraph sockets */
1024                 if (!LIST_EMPTY(&ngsocklist)) {
1025                         error = EBUSY;
1026                         break;
1027                 }
1028
1029 #ifdef NOTYET
1030                 /* Unregister protocol domain XXX can't do this yet.. */
1031                 if ((error = net_rm_domain(&ngdomain)) != 0)
1032                         break;
1033 #else
1034                 error = EBUSY;
1035 #endif
1036                 break;
1037         default:
1038                 error = EOPNOTSUPP;
1039                 break;
1040         }
1041         return (error);
1042 }
1043
1044 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1045 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1046 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1047 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1048 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");
1049