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