Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / netgraph7 / socket / ng_socket.c
1 /*
2  * ng_socket.c
3  */
4
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  *
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD: src/sys/netgraph/ng_socket.c,v 1.85 2008/03/11 21:58:48 mav 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/domain.h>
53 #include <sys/kernel.h>
54 #include <sys/linker.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/msgport2.h>
59 /*
60 #include <sys/mutex.h>
61 */
62 #include <sys/param.h>
63 #include <sys/priv.h>
64 #include <sys/proc.h>
65 #include <sys/protosw.h>
66 #include <sys/queue.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/socketvar2.h>
70 /*
71 #include <sys/syscallsubr.h>
72 */
73 #include <sys/sysctl.h>
74 #include <sys/thread2.h>
75 #include <sys/vnode.h>
76
77 #include <netgraph7/ng_message.h>
78 #include <netgraph7/netgraph.h>
79 #include "ng_socketvar.h"
80 #include "ng_socket.h"
81
82 #ifdef NG_SEPARATE_MALLOC
83 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
84 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
85 #else
86 #define M_NETGRAPH_PATH M_NETGRAPH
87 #define M_NETGRAPH_SOCK M_NETGRAPH
88 #endif
89
90 /*
91  * It's Ascii-art time!
92  *   +-------------+   +-------------+
93  *   |socket  (ctl)|   |socket (data)|
94  *   +-------------+   +-------------+
95  *          ^                 ^
96  *          |                 |
97  *          v                 v
98  *    +-----------+     +-----------+
99  *    |pcb   (ctl)|     |pcb  (data)|
100  *    +-----------+     +-----------+
101  *          ^                 ^
102  *          |                 |
103  *          v                 v
104  *      +--------------------------+
105  *      |   Socket type private    |
106  *      |       data               |
107  *      +--------------------------+
108  *                   ^
109  *                   |
110  *                   v
111  *           +----------------+
112  *           | struct ng_node |
113  *           +----------------+
114  */
115
116 /* Netgraph node methods */
117 static ng_constructor_t ngs_constructor;
118 static ng_rcvmsg_t      ngs_rcvmsg;
119 static ng_shutdown_t    ngs_shutdown;
120 static ng_newhook_t     ngs_newhook;
121 static ng_connect_t     ngs_connect;
122 static ng_rcvdata_t     ngs_rcvdata;
123 static ng_disconnect_t  ngs_disconnect;
124
125 /* Internal methods */
126 static int      ng_attach_data(struct socket *so);
127 static int      ng_attach_cntl(struct socket *so);
128 static int      ng_attach_common(struct socket *so, int type);
129 static void     ng_detach_common(struct ngpcb *pcbp, int type);
130 static void     ng_socket_free_priv(struct ngsock *priv);
131 #ifdef NOTYET
132 static int      ng_internalize(struct mbuf *m, struct thread *p);
133 #endif
134 static int      ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
135 static int      ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
136
137 static int      ngs_mod_event(module_t mod, int event, void *data);
138 static void     ng_socket_item_applied(void *context, int error);
139 static int      linker_api_available(void);
140
141 /* Netgraph type descriptor */
142 static struct ng_type typestruct = {
143         .version =      NG_ABI_VERSION,
144         .name =         NG_SOCKET_NODE_TYPE,
145         .mod_event =    ngs_mod_event,
146         .constructor =  ngs_constructor,
147         .rcvmsg =       ngs_rcvmsg,
148         .shutdown =     ngs_shutdown,
149         .newhook =      ngs_newhook,
150         .connect =      ngs_connect,
151         .rcvdata =      ngs_rcvdata,
152         .disconnect =   ngs_disconnect,
153 };
154 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
155
156 /* Buffer space */
157 static u_long ngpdg_sendspace = 20 * 1024;      /* really max datagram size */
158 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
159     &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
160 static u_long ngpdg_recvspace = 20 * 1024;
161 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
162     &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
163
164 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
165
166 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
167 #ifndef TRAP_ERROR
168 #define TRAP_ERROR
169 #endif
170
171 /***************************************************************
172         Control sockets
173 ***************************************************************/
174
175 static void
176 ngc_attach(netmsg_t msg)
177 {
178         struct socket *so = msg->attach.base.nm_so;
179         struct pru_attach_info *ai = msg->attach.nm_ai;
180         struct ngpcb *const pcbp = sotongpcb(so);
181         int error;
182
183         if (priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY) != 0)
184                 error = EPERM;
185         else if (pcbp != NULL)
186                 error = EISCONN;
187         else
188                 error = ng_attach_cntl(so);
189         lwkt_replymsg(&msg->attach.base.lmsg, error);
190 }
191
192 static void
193 ngc_detach(netmsg_t msg)
194 {
195         struct socket *so = msg->detach.base.nm_so;
196         struct ngpcb *const pcbp = sotongpcb(so);
197
198         KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
199         ng_detach_common(pcbp, NG_CONTROL);
200         lwkt_replymsg(&msg->detach.base.lmsg, 0);
201 }
202
203 static void
204 ngc_send(netmsg_t netmsg)
205 {
206         struct socket *so = netmsg->send.base.nm_so;
207         struct mbuf *m = netmsg->send.nm_m;
208         struct sockaddr *addr = netmsg->send.nm_addr;
209         struct mbuf *control = netmsg->send.nm_control;
210         struct ngpcb *const pcbp = sotongpcb(so);
211         struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
212         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
213         struct ng_mesg *msg;
214         struct mbuf *m0;
215         item_p item;
216         char *path = NULL;
217         int len, error = 0;
218         struct ng_apply_info apply;
219
220 #ifdef  NOTYET
221         if (control && (error = ng_internalize(control, td))) {
222                 if (pcbp->sockdata == NULL) {
223                         error = ENOTCONN;
224                         goto release;
225                 }
226         }
227 #else   /* NOTYET */
228         if (control) {
229                 error = EINVAL;
230                 goto release;
231         }
232 #endif  /* NOTYET */
233
234         /* Require destination as there may be >= 1 hooks on this node. */
235         if (addr == NULL) {
236                 error = EDESTADDRREQ;
237                 goto release;
238         }
239
240         /*
241          * Allocate an expendable buffer for the path, chop off
242          * the sockaddr header, and make sure it's NUL terminated.
243          */
244         len = sap->sg_len - 2;
245         path = kmalloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
246         bcopy(sap->sg_data, path, len);
247         path[len] = '\0';
248
249         /*
250          * Move the actual message out of mbufs into a linear buffer.
251          * Start by adding up the size of the data. (could use mh_len?)
252          */
253         for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
254                 len += m0->m_len;
255
256         /*
257          * Move the data into a linear buffer as well.
258          * Messages are not delivered in mbufs.
259          */
260         msg = kmalloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
261         m_copydata(m, 0, len, (char *)msg);
262
263         if (msg->header.version != NG_VERSION) {
264                 kfree(msg, M_NETGRAPH_MSG);
265                 error = EINVAL;
266                 goto release;
267         }
268
269         /*
270          * Hack alert!
271          * We look into the message and if it mkpeers a node of unknown type, we
272          * try to load it. We need to do this now, in syscall thread, because if
273          * message gets queued and applied later we will get panic.
274          */
275         if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
276             msg->header.cmd == NGM_MKPEER) {
277                 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
278                 struct ng_type *type;
279
280                 if ((type = ng_findtype(mkp->type)) == NULL) {
281                         char filename[NG_TYPESIZ + 3];
282                         linker_file_t fileid;
283
284                         if (!linker_api_available()) {
285                                 error = ENXIO;
286                                 goto done;
287                         }
288
289                         /* Not found, try to load it as a loadable module. */
290                         snprintf(filename, sizeof(filename), "ng_%s.ko",
291                             mkp->type);
292                         error = linker_load_file(filename, &fileid);
293                         if (error != 0) {
294                                 kfree(msg, M_NETGRAPH_MSG);
295                                 goto release;
296                         }
297
298                         /* See if type has been loaded successfully. */
299                         if ((type = ng_findtype(mkp->type)) == NULL) {
300                                 kfree(msg, M_NETGRAPH_MSG);
301                                 (void)linker_file_unload(fileid);
302                                 error =  ENXIO;
303                                 goto release;
304                         }
305                 }
306         }
307
308         item = ng_package_msg(msg, NG_WAITOK);
309         if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
310             != 0) {
311 #ifdef TRACE_MESSAGES
312                 printf("ng_address_path: errx=%d\n", error);
313 #endif
314                 goto release;
315         }
316
317 #ifdef TRACE_MESSAGES
318         printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
319                 item->el_dest->nd_ID,
320                 msg->header.typecookie,
321                 msg->header.cmd,
322                 msg->header.cmdstr,
323                 msg->header.flags,
324                 msg->header.token,
325                 item->el_dest->nd_type->name);
326 #endif
327         SAVE_LINE(item);
328         /*
329          * We do not want to return from syscall until the item
330          * is processed by destination node. We register callback
331          * on the item, which will update priv->error when item
332          * was applied.
333          * If ng_snd_item() has queued item, we sleep until
334          * callback wakes us up.
335          */
336         bzero(&apply, sizeof(apply));
337         apply.apply = ng_socket_item_applied;
338         apply.context = priv;
339         item->apply = &apply;
340         priv->error = -1;
341
342         error = ng_snd_item(item, 0);
343
344         mtx_lock(&priv->mtx);
345         if (priv->error == -1)
346                 mtxsleep(priv, &priv->mtx, 0, "ngsock", 0);
347         mtx_unlock(&priv->mtx);
348         KASSERT(priv->error != -1,
349             ("ng_socket: priv->error wasn't updated"));
350         error = priv->error;
351
352 release:
353         if (path != NULL)
354                 kfree(path, M_NETGRAPH_PATH);
355         if (control != NULL)
356                 m_freem(control);
357         if (m != NULL)
358                 m_freem(m);
359 done:
360         lwkt_replymsg(&netmsg->send.base.lmsg, error);
361 }
362
363 static void
364 ngc_bind(netmsg_t msg)
365 {
366         struct socket *so = msg->connect.base.nm_so;
367         struct sockaddr *nam = msg->connect.nm_nam;
368         struct ngpcb *const pcbp = sotongpcb(so);
369         int error;
370
371         if (pcbp == NULL)
372                 error = EINVAL;
373         else
374                 error = ng_bind(nam, pcbp);
375         lwkt_replymsg(&msg->connect.base.lmsg, error);
376 }
377
378 static void
379 ngc_connect(netmsg_t msg)
380 {
381         /*
382          * At this time refuse to do this.. it used to
383          * do something but it was undocumented and not used.
384          */
385         printf("program tried to connect control socket to remote node\n");
386         lwkt_replymsg(&msg->connect.base.lmsg, EINVAL);
387 }
388
389 /***************************************************************
390         Data sockets
391 ***************************************************************/
392
393 static void
394 ngd_attach(netmsg_t msg)
395 {
396         struct socket *so = msg->attach.base.nm_so;
397         struct ngpcb *const pcbp = sotongpcb(so);
398         int error;
399
400         if (pcbp != NULL)
401                 error =  EISCONN;
402         else
403                 error = ng_attach_data(so);
404         lwkt_replymsg(&msg->connect.base.lmsg, error);
405 }
406
407 static void
408 ngd_detach(netmsg_t msg)
409 {
410         struct socket *so = msg->detach.base.nm_so;
411         struct ngpcb *const pcbp = sotongpcb(so);
412
413         KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
414         ng_detach_common(pcbp, NG_DATA);
415         lwkt_replymsg(&msg->detach.base.lmsg, 0);
416 }
417
418 static void
419 ngd_send(netmsg_t msg)
420 {
421         struct socket *so = msg->send.base.nm_so;
422         struct mbuf *m = msg->send.nm_m;
423         struct sockaddr *addr = msg->send.nm_addr;
424         struct mbuf *control = msg->send.nm_control;
425         struct ngpcb *const pcbp = sotongpcb(so);
426         struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
427         int     len, error;
428         hook_p  hook = NULL;
429         char    hookname[NG_HOOKSIZ];
430
431         if ((pcbp == NULL) || (control != NULL)) {
432                 error = EINVAL;
433                 goto release;
434         }
435         if (pcbp->sockdata == NULL) {
436                 error = ENOTCONN;
437                 goto release;
438         }
439
440         if (sap == NULL)
441                 len = 0;                /* Make compiler happy. */
442         else
443                 len = sap->sg_len - 2;
444
445         /*
446          * If the user used any of these ways to not specify an address
447          * then handle specially.
448          */
449         if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
450                 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
451                         error = EDESTADDRREQ;
452                         goto release;
453                 }
454                 /*
455                  * If exactly one hook exists, just use it.
456                  * Special case to allow write(2) to work on an ng_socket.
457                  */
458                 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
459         } else {
460                 if (len >= NG_HOOKSIZ) {
461                         error = EINVAL;
462                         goto release;
463                 }
464
465                 /*
466                  * chop off the sockaddr header, and make sure it's NUL
467                  * terminated
468                  */
469                 bcopy(sap->sg_data, hookname, len);
470                 hookname[len] = '\0';
471
472                 /* Find the correct hook from 'hookname' */
473                 hook = ng_findhook(pcbp->sockdata->node, hookname);
474                 if (hook == NULL) {
475                         error = EHOSTUNREACH;
476                         goto release;
477                 }
478         }
479
480         /* Send data. */
481         NG_SEND_DATA_FLAGS(error, hook, m, NG_WAITOK);
482
483 release:
484         if (control != NULL)
485                 m_freem(control);
486         if (m != NULL)
487                 m_freem(m);
488         lwkt_replymsg(&msg->send.base.lmsg, error);
489 }
490
491 static void
492 ngd_connect(netmsg_t msg)
493 {
494         struct socket *so = msg->connect.base.nm_so;
495         struct sockaddr *nam = msg->connect.nm_nam;
496         struct ngpcb *const pcbp = sotongpcb(so);
497         int error;
498
499         if (pcbp == NULL)
500                 error = EINVAL;
501         else
502                 error = ng_connect_data(nam, pcbp);
503         lwkt_replymsg(&msg->connect.base.lmsg, error);
504 }
505
506 /*
507  * Used for both data and control sockets
508  */
509 static void
510 ng_getsockaddr(netmsg_t msg)
511 {
512         struct socket *so = msg->sockaddr.base.nm_so;
513         struct sockaddr **addr = msg->sockaddr.nm_nam;
514         struct ngpcb *pcbp;
515         struct sockaddr_ng *sg;
516         int sg_len;
517         int error = 0;
518
519         /* Why isn't sg_data a `char[1]' ? :-( */
520         sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
521
522         pcbp = sotongpcb(so);
523         if ((pcbp == NULL) || (pcbp->sockdata == NULL)) {
524                 /* XXXGL: can this still happen? */
525                 error = EINVAL;
526                 goto replymsg;
527         }
528
529         mtx_lock(&pcbp->sockdata->mtx);
530         if (pcbp->sockdata->node != NULL) {
531                 node_p node = pcbp->sockdata->node;
532                 int namelen = 0;        /* silence compiler! */
533
534                 if (NG_NODE_HAS_NAME(node))
535                         sg_len += namelen = strlen(NG_NODE_NAME(node));
536
537                 sg = kmalloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
538
539                 if (NG_NODE_HAS_NAME(node))
540                         bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
541
542                 sg->sg_len = sg_len;
543                 sg->sg_family = AF_NETGRAPH;
544                 *addr = (struct sockaddr *)sg;
545                 mtx_unlock(&pcbp->sockdata->mtx);
546         } else {
547                 mtx_unlock(&pcbp->sockdata->mtx);
548                 error = EINVAL;
549         }
550
551 replymsg:
552         lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
553 }
554
555 /*
556  * Attach a socket to it's protocol specific partner.
557  * For a control socket, actually create a netgraph node and attach
558  * to it as well.
559  */
560
561 static int
562 ng_attach_cntl(struct socket *so)
563 {
564         struct ngsock *priv;
565         struct ngpcb *pcbp;
566         int error;
567
568         /* Allocate node private info */
569         priv = kmalloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
570
571         /* Setup protocol control block */
572         if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
573                 kfree(priv, M_NETGRAPH_SOCK);
574                 return (error);
575         }
576         pcbp = sotongpcb(so);
577
578         /* Link the pcb the private data. */
579         priv->ctlsock = pcbp;
580         pcbp->sockdata = priv;
581         priv->refs++;
582
583         /* Initialize mutex. */
584         mtx_init(&priv->mtx);
585
586         /* Make the generic node components */
587         if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
588                 kfree(priv, M_NETGRAPH_SOCK);
589                 ng_detach_common(pcbp, NG_CONTROL);
590                 return (error);
591         }
592
593         /* Link the node and the private data. */
594         NG_NODE_SET_PRIVATE(priv->node, priv);
595         NG_NODE_REF(priv->node);
596         priv->refs++;
597
598         return (0);
599 }
600
601 static int
602 ng_attach_data(struct socket *so)
603 {
604         return (ng_attach_common(so, NG_DATA));
605 }
606
607 /*
608  * Set up a socket protocol control block.
609  * This code is shared between control and data sockets.
610  */
611 static int
612 ng_attach_common(struct socket *so, int type)
613 {
614         struct ngpcb *pcbp;
615         int error;
616
617         /* Standard socket setup stuff. */
618         error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace, NULL);
619         if (error)
620                 return (error);
621
622         /* Allocate the pcb. */
623         pcbp = kmalloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
624         pcbp->type = type;
625
626         /* Link the pcb and the socket. */
627         so->so_pcb = (caddr_t)pcbp;
628         pcbp->ng_socket = so;
629
630         return (0);
631 }
632
633 /*
634  * Disassociate the socket from it's protocol specific
635  * partner. If it's attached to a node's private data structure,
636  * then unlink from that too. If we were the last socket attached to it,
637  * then shut down the entire node. Shared code for control and data sockets.
638  */
639 static void
640 ng_detach_common(struct ngpcb *pcbp, int which)
641 {
642         struct ngsock *priv = pcbp->sockdata;
643
644         if (priv != NULL) {
645                 mtx_lock(&priv->mtx);
646
647                 switch (which) {
648                 case NG_CONTROL:
649                         priv->ctlsock = NULL;
650                         break;
651                 case NG_DATA:
652                         priv->datasock = NULL;
653                         break;
654                 default:
655                         panic(__func__);
656                 }
657                 pcbp->sockdata = NULL;
658
659                 ng_socket_free_priv(priv);
660         }
661
662         pcbp->ng_socket->so_pcb = NULL;
663         kfree(pcbp, M_PCB);
664 }
665
666 /*
667  * Remove a reference from node private data.
668  */
669 static void
670 ng_socket_free_priv(struct ngsock *priv)
671 {
672         KKASSERT(mtx_owned(&priv->mtx));
673
674         priv->refs--;
675
676         if (priv->refs == 0) {
677                 mtx_uninit(&priv->mtx);
678                 kfree(priv, M_NETGRAPH_SOCK);
679                 return;
680         }
681
682         if ((priv->refs == 1) && (priv->node != NULL)) {
683                 node_p node = priv->node;
684
685                 priv->node = NULL;
686                 mtx_unlock(&priv->mtx);
687                 NG_NODE_UNREF(node);
688                 ng_rmnode_self(node);
689         } else
690                 mtx_unlock(&priv->mtx);
691 }
692
693 #ifdef NOTYET
694 /*
695  * File descriptors can be passed into an AF_NETGRAPH socket.
696  * Note, that file descriptors cannot be passed OUT.
697  * Only character device descriptors are accepted.
698  * Character devices are useful to connect a graph to a device,
699  * which after all is the purpose of this whole system.
700  */
701 static int
702 ng_internalize(struct mbuf *control, struct thread *td)
703 {
704         const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
705         struct file *fp;
706         struct vnode *vn;
707         int oldfds;
708         int fd;
709
710         if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
711             cm->cmsg_len != control->m_len) {
712                 TRAP_ERROR;
713                 return (EINVAL);
714         }
715
716         /* Check there is only one FD. XXX what would more than one signify? */
717         oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
718         if (oldfds != 1) {
719                 TRAP_ERROR;
720                 return (EINVAL);
721         }
722
723         /* Check that the FD given is legit. and change it to a pointer to a
724          * struct file. */
725         fd = CMSG_DATA(cm);
726         if ((error = fget(td, fd, &fp)) != 0)
727                 return (error);
728
729         /* Depending on what kind of resource it is, act differently. For
730          * devices, we treat it as a file. For an AF_NETGRAPH socket,
731          * shortcut straight to the node. */
732         switch (fp->f_type) {
733         case DTYPE_VNODE:
734                 vn = fp->f_data;
735                 if (vn && (vn->v_type == VCHR)) {
736                         /* for a VCHR, actually reference the FILE */
737                         fhold(fp);
738                         /* XXX then what :) */
739                         /* how to pass on to other modules? */
740                 } else {
741                         fdrop(fp, td);
742                         TRAP_ERROR;
743                         return (EINVAL);
744                 }
745                 break;
746         default:
747                 fdrop(fp, td);
748                 TRAP_ERROR;
749                 return (EINVAL);
750         }
751         fdrop(fp, td);
752         return (0);
753 }
754 #endif  /* NOTYET */
755
756 /*
757  * Connect the data socket to a named control socket node.
758  */
759 static int
760 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
761 {
762         struct sockaddr_ng *sap;
763         node_p farnode;
764         struct ngsock *priv;
765         int error;
766         item_p item;
767
768         /* If we are already connected, don't do it again. */
769         if (pcbp->sockdata != NULL)
770                 return (EISCONN);
771
772         /*
773          * Find the target (victim) and check it doesn't already have
774          * a data socket. Also check it is a 'socket' type node.
775          * Use ng_package_data() and ng_address_path() to do this.
776          */
777
778         sap = (struct sockaddr_ng *) nam;
779         /* The item will hold the node reference. */
780         item = ng_package_data(NULL, NG_WAITOK);
781
782         if ((error = ng_address_path(NULL, item,  sap->sg_data, 0)))
783                 return (error); /* item is freed on failure */
784
785         /*
786          * Extract node from item and free item. Remember we now have
787          * a reference on the node. The item holds it for us.
788          * when we free the item we release the reference.
789          */
790         farnode = item->el_dest; /* shortcut */
791         if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
792                 NG_FREE_ITEM(item); /* drop the reference to the node */
793                 return (EINVAL);
794         }
795         priv = NG_NODE_PRIVATE(farnode);
796         if (priv->datasock != NULL) {
797                 NG_FREE_ITEM(item);     /* drop the reference to the node */
798                 return (EADDRINUSE);
799         }
800
801         /*
802          * Link the PCB and the private data struct. and note the extra
803          * reference. Drop the extra reference on the node.
804          */
805         mtx_lock(&priv->mtx);
806         priv->datasock = pcbp;
807         pcbp->sockdata = priv;
808         priv->refs++;
809         mtx_unlock(&priv->mtx);
810         NG_FREE_ITEM(item);     /* drop the reference to the node */
811         return (0);
812 }
813
814 /*
815  * Binding a socket means giving the corresponding node a name
816  */
817 static int
818 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
819 {
820         struct ngsock *const priv = pcbp->sockdata;
821         struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
822
823         if (priv == NULL) {
824                 TRAP_ERROR;
825                 return (EINVAL);
826         }
827         if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
828             (sap->sg_data[0] == '\0') ||
829             (sap->sg_data[sap->sg_len - 3] != '\0')) {
830                 TRAP_ERROR;
831                 return (EINVAL);
832         }
833         return (ng_name_node(priv->node, sap->sg_data));
834 }
835
836 /***************************************************************
837         Netgraph node
838 ***************************************************************/
839
840 /*
841  * You can only create new nodes from the socket end of things.
842  */
843 static int
844 ngs_constructor(node_p nodep)
845 {
846         return (EINVAL);
847 }
848
849 /*
850  * We allow any hook to be connected to the node.
851  * There is no per-hook private information though.
852  */
853 static int
854 ngs_newhook(node_p node, hook_p hook, const char *name)
855 {
856         NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
857         return (0);
858 }
859
860 /*
861  * If only one hook, allow read(2) and write(2) to work.
862  */
863 static int
864 ngs_connect(hook_p hook)
865 {
866         node_p node = NG_HOOK_NODE(hook);
867         struct ngsock *priv = NG_NODE_PRIVATE(node);
868
869         if ((priv->datasock) && (priv->datasock->ng_socket)) {
870                 if (NG_NODE_NUMHOOKS(node) == 1)
871                         sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
872                 else
873                         soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
874         }
875         return (0);
876 }
877
878 /*
879  * Incoming messages get passed up to the control socket.
880  * Unless they are for us specifically (socket_type)
881  */
882 static int
883 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
884 {
885         struct ngsock *const priv = NG_NODE_PRIVATE(node);
886         struct ngpcb *const pcbp = priv->ctlsock;
887         struct socket *so;
888         struct sockaddr_ng addr;
889         struct ng_mesg *msg;
890         struct mbuf *m;
891         ng_ID_t retaddr = NGI_RETADDR(item);
892         int addrlen;
893         int error = 0;
894
895         NGI_GET_MSG(item, msg);
896         NG_FREE_ITEM(item);
897
898         /*
899          * Only allow mesgs to be passed if we have the control socket.
900          * Data sockets can only support the generic messages.
901          */
902         if (pcbp == NULL) {
903                 TRAP_ERROR;
904                 NG_FREE_MSG(msg);
905                 return (EINVAL);
906         }
907         so = pcbp->ng_socket;
908
909 #ifdef TRACE_MESSAGES
910         printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
911                 retaddr,
912                 msg->header.typecookie,
913                 msg->header.cmd,
914                 msg->header.cmdstr,
915                 msg->header.flags,
916                 msg->header.token);
917 #endif
918
919         if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
920                 switch (msg->header.cmd) {
921                 case NGM_SOCK_CMD_NOLINGER:
922                         priv->flags |= NGS_FLAG_NOLINGER;
923                         break;
924                 case NGM_SOCK_CMD_LINGER:
925                         priv->flags &= ~NGS_FLAG_NOLINGER;
926                         break;
927                 default:
928                         error = EINVAL;         /* unknown command */
929                 }
930                 /* Free the message and return. */
931                 NG_FREE_MSG(msg);
932                 return (error);
933         }
934
935         /* Get the return address into a sockaddr. */
936         bzero(&addr, sizeof(addr));
937         addr.sg_len = sizeof(addr);
938         addr.sg_family = AF_NETGRAPH;
939         addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
940             "[%x]:", retaddr);
941         if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
942                 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
943                     addrlen);
944                 NG_FREE_MSG(msg);
945                 return (EINVAL);
946         }
947
948         /* Copy the message itself into an mbuf chain. */
949         m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
950             0, NULL, NULL);
951
952         /*
953          * Here we free the message. We need to do that
954          * regardless of whether we got mbufs.
955          */
956         NG_FREE_MSG(msg);
957
958         if (m == NULL) {
959                 TRAP_ERROR;
960                 return (ENOBUFS);
961         }
962
963         /* Send it up to the socket. */
964         if (sbappendaddr((struct sockbuf *)&so->so_rcv, (struct sockaddr *)&addr, m, NULL) == 0) {
965                 TRAP_ERROR;
966                 m_freem(m);
967                 return (ENOBUFS);
968         }
969         sorwakeup(so);
970         
971         return (error);
972 }
973
974 /*
975  * Receive data on a hook
976  */
977 static int
978 ngs_rcvdata(hook_p hook, item_p item)
979 {
980         struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
981         struct ngpcb *const pcbp = priv->datasock;
982         struct socket *so;
983         struct sockaddr_ng *addr;
984         char *addrbuf[NG_HOOKSIZ + 4];
985         int addrlen;
986         struct mbuf *m;
987
988         NGI_GET_M(item, m);
989         NG_FREE_ITEM(item);
990
991         /* If there is no data socket, black-hole it. */
992         if (pcbp == NULL) {
993                 NG_FREE_M(m);
994                 return (0);
995         }
996         so = pcbp->ng_socket;
997
998         /* Get the return address into a sockaddr. */
999         addrlen = strlen(NG_HOOK_NAME(hook));   /* <= NG_HOOKSIZ - 1 */
1000         addr = (struct sockaddr_ng *) addrbuf;
1001         addr->sg_len = addrlen + 3;
1002         addr->sg_family = AF_NETGRAPH;
1003         bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1004         addr->sg_data[addrlen] = '\0';
1005
1006         /* Try to tell the socket which hook it came in on. */
1007         if (sbappendaddr((struct sockbuf *)&so->so_rcv, (struct sockaddr *)addr, m, NULL) == 0) {
1008                 m_freem(m);
1009                 TRAP_ERROR;
1010                 return (ENOBUFS);
1011         }
1012         sorwakeup(so);
1013         return (0);
1014 }
1015
1016 /*
1017  * Hook disconnection
1018  *
1019  * For this type, removal of the last link destroys the node
1020  * if the NOLINGER flag is set.
1021  */
1022 static int
1023 ngs_disconnect(hook_p hook)
1024 {
1025         node_p node = NG_HOOK_NODE(hook);
1026         struct ngsock *const priv = NG_NODE_PRIVATE(node);
1027
1028         if ((priv->datasock) && (priv->datasock->ng_socket)) {
1029                 if (NG_NODE_NUMHOOKS(node) == 1)
1030                         sosetstate(priv->datasock->ng_socket, SS_ISCONNECTED);
1031                 else
1032                         soclrstate(priv->datasock->ng_socket, SS_ISCONNECTED);
1033         }
1034
1035         if ((priv->flags & NGS_FLAG_NOLINGER) &&
1036             (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1037                 ng_rmnode_self(node);
1038
1039         return (0);
1040 }
1041
1042 /*
1043  * Do local shutdown processing.
1044  * In this case, that involves making sure the socket
1045  * knows we should be shutting down.
1046  */
1047 static int
1048 ngs_shutdown(node_p node)
1049 {
1050         struct ngsock *const priv = NG_NODE_PRIVATE(node);
1051         struct ngpcb *const dpcbp = priv->datasock;
1052         struct ngpcb *const pcbp = priv->ctlsock;
1053
1054         if (dpcbp != NULL)
1055                 soisdisconnected(dpcbp->ng_socket);
1056
1057         if (pcbp != NULL)
1058                 soisdisconnected(pcbp->ng_socket);
1059
1060         mtx_lock(&priv->mtx);
1061         priv->node = NULL;
1062         NG_NODE_SET_PRIVATE(node, NULL);
1063         ng_socket_free_priv(priv);
1064
1065         NG_NODE_UNREF(node);
1066         return (0);
1067 }
1068
1069 static void
1070 ng_socket_item_applied(void *context, int error)
1071 {
1072         struct ngsock *const priv = (struct ngsock *)context;
1073
1074         mtx_lock(&priv->mtx);
1075         priv->error = error;
1076         wakeup(priv);
1077         mtx_unlock(&priv->mtx);
1078
1079 }
1080
1081 /*
1082  * Control and data socket type descriptors
1083  *
1084  * XXXRW: Perhaps _close should do something?
1085  */
1086
1087 static struct pr_usrreqs ngc_usrreqs = {
1088         .pru_abort =            NULL,
1089         .pru_attach =           ngc_attach,
1090         .pru_bind =             ngc_bind,
1091         .pru_connect =          ngc_connect,
1092         .pru_detach =           ngc_detach,
1093         .pru_disconnect =       NULL,
1094         .pru_peeraddr =         NULL,
1095         .pru_send =             ngc_send,
1096         .pru_shutdown =         NULL,
1097         .pru_sockaddr =         ng_getsockaddr,
1098         .pru_sosend =           sosend,
1099         .pru_soreceive =        soreceive,
1100         /* .pru_close =         NULL, */
1101 };
1102
1103 static struct pr_usrreqs ngd_usrreqs = {
1104         .pru_abort =            NULL,
1105         .pru_attach =           ngd_attach,
1106         .pru_bind =             NULL,
1107         .pru_connect =          ngd_connect,
1108         .pru_detach =           ngd_detach,
1109         .pru_disconnect =       NULL,
1110         .pru_peeraddr =         NULL,
1111         .pru_send =             ngd_send,
1112         .pru_shutdown =         NULL,
1113         .pru_sockaddr =         ng_getsockaddr,
1114         .pru_sosend =           sosend,
1115         .pru_soreceive =        soreceive,
1116         /* .pru_close =         NULL, */
1117 };
1118
1119 /*
1120  * Definitions of protocols supported in the NETGRAPH domain.
1121  */
1122
1123 extern struct domain ngdomain;          /* stop compiler warnings */
1124
1125 static struct protosw ngsw[] = {
1126 {
1127         .pr_type =              SOCK_DGRAM,
1128         .pr_domain =            &ngdomain,
1129         .pr_protocol =          NG_CONTROL,
1130         .pr_flags =             PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1131         .pr_usrreqs =           &ngc_usrreqs
1132 },
1133 {
1134         .pr_type =              SOCK_DGRAM,
1135         .pr_domain =            &ngdomain,
1136         .pr_protocol =          NG_DATA,
1137         .pr_flags =             PR_ATOMIC | PR_ADDR,
1138         .pr_usrreqs =           &ngd_usrreqs
1139 }
1140 };
1141
1142 struct domain ngdomain = {
1143         .dom_family =           AF_NETGRAPH,
1144         .dom_name =             "netgraph",
1145         .dom_protosw =          ngsw,
1146         .dom_protoswNPROTOSW =  &ngsw[NELEM(ngsw)]
1147 };
1148
1149 /*
1150  * Handle loading and unloading for this node type.
1151  * This is to handle auxiliary linkages (e.g protocol domain addition).
1152  */
1153 static int
1154 ngs_mod_event(module_t mod, int event, void *data)
1155 {
1156         int error = 0;
1157
1158         switch (event) {
1159         case MOD_LOAD:
1160                 /* Register protocol domain. */
1161                 net_add_domain(&ngdomain);
1162                 break;
1163         case MOD_UNLOAD:
1164 #ifdef NOTYET
1165                 /* Unregister protocol domain XXX can't do this yet.. */
1166                 if ((error = net_rm_domain(&ngdomain)) != 0)
1167                         break;
1168                 else
1169 #endif
1170                         error = EBUSY;
1171                 break;
1172         default:
1173                 error = EOPNOTSUPP;
1174                 break;
1175         }
1176         return (error);
1177 }
1178
1179 static int
1180 linker_api_available(void)
1181 {
1182         /* linker_* API won't work without a process context */
1183         if (curproc == NULL)
1184                 return 0;
1185         /*
1186          * nlookup_init() relies on namei_oc to be initialized,
1187          * but it's not when the netgraph module is loaded during boot.
1188          */
1189         if (namei_oc == NULL)
1190                 return 0;
1191         return 1;
1192 }
1193
1194 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1195 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1196 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1197 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1198 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");
1199