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