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