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