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