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