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