xref: /freebsd/sys/netgraph/ng_ksocket.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*
2  * ng_ksocket.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: Archie Cobbs <archie@freebsd.org>
39  * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $
40  */
41 
42 /*
43  * Kernel socket node type.  This node type is basically a kernel-mode
44  * version of a socket... kindof like the reverse of the socket node type.
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/mbuf.h>
51 #include <sys/proc.h>
52 #include <sys/malloc.h>
53 #include <sys/ctype.h>
54 #include <sys/protosw.h>
55 #include <sys/errno.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/uio.h>
59 #include <sys/un.h>
60 
61 #include <netgraph/ng_message.h>
62 #include <netgraph/netgraph.h>
63 #include <netgraph/ng_parse.h>
64 #include <netgraph/ng_ksocket.h>
65 
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 
69 #ifdef NG_SEPARATE_MALLOC
70 static MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock",
71     "netgraph ksock node");
72 #else
73 #define M_NETGRAPH_KSOCKET M_NETGRAPH
74 #endif
75 
76 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
77 #define SADATA_OFFSET	(OFFSETOF(struct sockaddr, sa_data))
78 
79 /* Node private data */
80 struct ng_ksocket_private {
81 	node_p		node;
82 	hook_p		hook;
83 	struct socket	*so;
84 	int		fn_sent;	/* FN call on incoming event was sent */
85 	LIST_HEAD(, ng_ksocket_private)	embryos;
86 	LIST_ENTRY(ng_ksocket_private)	siblings;
87 	u_int32_t	flags;
88 	u_int32_t	response_token;
89 	ng_ID_t		response_addr;
90 };
91 typedef struct ng_ksocket_private *priv_p;
92 
93 /* Flags for priv_p */
94 #define	KSF_CONNECTING	0x00000001	/* Waiting for connection complete */
95 #define	KSF_ACCEPTING	0x00000002	/* Waiting for accept complete */
96 #define	KSF_EOFSEEN	0x00000004	/* Have sent 0-length EOF mbuf */
97 #define	KSF_CLONED	0x00000008	/* Cloned from an accepting socket */
98 #define	KSF_EMBRYONIC	0x00000010	/* Cloned node with no hooks yet */
99 
100 /* Netgraph node methods */
101 static ng_constructor_t	ng_ksocket_constructor;
102 static ng_rcvmsg_t	ng_ksocket_rcvmsg;
103 static ng_shutdown_t	ng_ksocket_shutdown;
104 static ng_newhook_t	ng_ksocket_newhook;
105 static ng_rcvdata_t	ng_ksocket_rcvdata;
106 static ng_connect_t	ng_ksocket_connect;
107 static ng_disconnect_t	ng_ksocket_disconnect;
108 
109 /* Alias structure */
110 struct ng_ksocket_alias {
111 	const char	*name;
112 	const int	value;
113 	const int	family;
114 };
115 
116 /* Protocol family aliases */
117 static const struct ng_ksocket_alias ng_ksocket_families[] = {
118 	{ "local",	PF_LOCAL	},
119 	{ "inet",	PF_INET		},
120 	{ "inet6",	PF_INET6	},
121 	{ "atm",	PF_ATM		},
122 	{ "divert",	PF_DIVERT	},
123 	{ NULL,		-1		},
124 };
125 
126 /* Socket type aliases */
127 static const struct ng_ksocket_alias ng_ksocket_types[] = {
128 	{ "stream",	SOCK_STREAM	},
129 	{ "dgram",	SOCK_DGRAM	},
130 	{ "raw",	SOCK_RAW	},
131 	{ "rdm",	SOCK_RDM	},
132 	{ "seqpacket",	SOCK_SEQPACKET	},
133 	{ NULL,		-1		},
134 };
135 
136 /* Protocol aliases */
137 static const struct ng_ksocket_alias ng_ksocket_protos[] = {
138 	{ "ip",		IPPROTO_IP,		PF_INET		},
139 	{ "raw",	IPPROTO_RAW,		PF_INET		},
140 	{ "icmp",	IPPROTO_ICMP,		PF_INET		},
141 	{ "igmp",	IPPROTO_IGMP,		PF_INET		},
142 	{ "tcp",	IPPROTO_TCP,		PF_INET		},
143 	{ "udp",	IPPROTO_UDP,		PF_INET		},
144 	{ "gre",	IPPROTO_GRE,		PF_INET		},
145 	{ "esp",	IPPROTO_ESP,		PF_INET		},
146 	{ "ah",		IPPROTO_AH,		PF_INET		},
147 	{ "swipe",	IPPROTO_SWIPE,		PF_INET		},
148 	{ "encap",	IPPROTO_ENCAP,		PF_INET		},
149 	{ "pim",	IPPROTO_PIM,		PF_INET		},
150 	{ NULL,		-1					},
151 };
152 
153 /* Helper functions */
154 static int	ng_ksocket_accept(priv_p);
155 static int	ng_ksocket_listen_upcall(struct socket *so, void *arg,
156     int waitflag);
157 static void	ng_ksocket_listen_upcall2(node_p node, hook_p hook,
158     void *arg1, int arg2);
159 static int	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
160 static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
161 			const char *s, int family);
162 static void	ng_ksocket_incoming2(node_p node, hook_p hook,
163 			void *arg1, int arg2);
164 
165 /************************************************************************
166 			STRUCT SOCKADDR PARSE TYPE
167  ************************************************************************/
168 
169 /* Get the length of the data portion of a generic struct sockaddr */
170 static int
171 ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
172 	const u_char *start, const u_char *buf)
173 {
174 	const struct sockaddr *sa;
175 
176 	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
177 	return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
178 }
179 
180 /* Type for the variable length data portion of a generic struct sockaddr */
181 static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
182 	&ng_parse_bytearray_type,
183 	&ng_parse_generic_sockdata_getLength
184 };
185 
186 /* Type for a generic struct sockaddr */
187 static const struct ng_parse_struct_field
188     ng_parse_generic_sockaddr_type_fields[] = {
189 	  { "len",	&ng_parse_uint8_type			},
190 	  { "family",	&ng_parse_uint8_type			},
191 	  { "data",	&ng_ksocket_generic_sockdata_type	},
192 	  { NULL }
193 };
194 static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
195 	&ng_parse_struct_type,
196 	&ng_parse_generic_sockaddr_type_fields
197 };
198 
199 /* Convert a struct sockaddr from ASCII to binary.  If its a protocol
200    family that we specially handle, do that, otherwise defer to the
201    generic parse type ng_ksocket_generic_sockaddr_type. */
202 static int
203 ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
204 	const char *s, int *off, const u_char *const start,
205 	u_char *const buf, int *buflen)
206 {
207 	struct sockaddr *const sa = (struct sockaddr *)buf;
208 	enum ng_parse_token tok;
209 	char fambuf[32];
210 	int family, len;
211 	char *t;
212 
213 	/* If next token is a left curly brace, use generic parse type */
214 	if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
215 		return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
216 		    (&ng_ksocket_generic_sockaddr_type,
217 		    s, off, start, buf, buflen);
218 	}
219 
220 	/* Get socket address family followed by a slash */
221 	while (isspace(s[*off]))
222 		(*off)++;
223 	if ((t = strchr(s + *off, '/')) == NULL)
224 		return (EINVAL);
225 	if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
226 		return (EINVAL);
227 	strncpy(fambuf, s + *off, len);
228 	fambuf[len] = '\0';
229 	*off += len + 1;
230 	if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
231 		return (EINVAL);
232 
233 	/* Set family */
234 	if (*buflen < SADATA_OFFSET)
235 		return (ERANGE);
236 	sa->sa_family = family;
237 
238 	/* Set family-specific data and length */
239 	switch (sa->sa_family) {
240 	case PF_LOCAL:		/* Get pathname */
241 	    {
242 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
243 		struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
244 		int toklen, pathlen;
245 		char *path;
246 
247 		if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL)
248 			return (EINVAL);
249 		pathlen = strlen(path);
250 		if (pathlen > SOCK_MAXADDRLEN) {
251 			free(path, M_NETGRAPH_KSOCKET);
252 			return (E2BIG);
253 		}
254 		if (*buflen < pathoff + pathlen) {
255 			free(path, M_NETGRAPH_KSOCKET);
256 			return (ERANGE);
257 		}
258 		*off += toklen;
259 		bcopy(path, sun->sun_path, pathlen);
260 		sun->sun_len = pathoff + pathlen;
261 		free(path, M_NETGRAPH_KSOCKET);
262 		break;
263 	    }
264 
265 	case PF_INET:		/* Get an IP address with optional port */
266 	    {
267 		struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
268 		int i;
269 
270 		/* Parse this: <ipaddress>[:port] */
271 		for (i = 0; i < 4; i++) {
272 			u_long val;
273 			char *eptr;
274 
275 			val = strtoul(s + *off, &eptr, 10);
276 			if (val > 0xff || eptr == s + *off)
277 				return (EINVAL);
278 			*off += (eptr - (s + *off));
279 			((u_char *)&sin->sin_addr)[i] = (u_char)val;
280 			if (i < 3) {
281 				if (s[*off] != '.')
282 					return (EINVAL);
283 				(*off)++;
284 			} else if (s[*off] == ':') {
285 				(*off)++;
286 				val = strtoul(s + *off, &eptr, 10);
287 				if (val > 0xffff || eptr == s + *off)
288 					return (EINVAL);
289 				*off += (eptr - (s + *off));
290 				sin->sin_port = htons(val);
291 			} else
292 				sin->sin_port = 0;
293 		}
294 		bzero(&sin->sin_zero, sizeof(sin->sin_zero));
295 		sin->sin_len = sizeof(*sin);
296 		break;
297 	    }
298 
299 #if 0
300 	case PF_INET6:	/* XXX implement this someday */
301 #endif
302 
303 	default:
304 		return (EINVAL);
305 	}
306 
307 	/* Done */
308 	*buflen = sa->sa_len;
309 	return (0);
310 }
311 
312 /* Convert a struct sockaddr from binary to ASCII */
313 static int
314 ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
315 	const u_char *data, int *off, char *cbuf, int cbuflen)
316 {
317 	const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
318 	int slen = 0;
319 
320 	/* Output socket address, either in special or generic format */
321 	switch (sa->sa_family) {
322 	case PF_LOCAL:
323 	    {
324 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
325 		const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
326 		const int pathlen = sun->sun_len - pathoff;
327 		char pathbuf[SOCK_MAXADDRLEN + 1];
328 		char *pathtoken;
329 
330 		bcopy(sun->sun_path, pathbuf, pathlen);
331 		if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL)
332 			return (ENOMEM);
333 		slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken);
334 		free(pathtoken, M_NETGRAPH_KSOCKET);
335 		if (slen >= cbuflen)
336 			return (ERANGE);
337 		*off += sun->sun_len;
338 		return (0);
339 	    }
340 
341 	case PF_INET:
342 	    {
343 		const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
344 
345 		slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
346 		  ((const u_char *)&sin->sin_addr)[0],
347 		  ((const u_char *)&sin->sin_addr)[1],
348 		  ((const u_char *)&sin->sin_addr)[2],
349 		  ((const u_char *)&sin->sin_addr)[3]);
350 		if (sin->sin_port != 0) {
351 			slen += snprintf(cbuf + strlen(cbuf),
352 			    cbuflen - strlen(cbuf), ":%d",
353 			    (u_int)ntohs(sin->sin_port));
354 		}
355 		if (slen >= cbuflen)
356 			return (ERANGE);
357 		*off += sizeof(*sin);
358 		return(0);
359 	    }
360 
361 #if 0
362 	case PF_INET6:	/* XXX implement this someday */
363 #endif
364 
365 	default:
366 		return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
367 		    (&ng_ksocket_generic_sockaddr_type,
368 		    data, off, cbuf, cbuflen);
369 	}
370 }
371 
372 /* Parse type for struct sockaddr */
373 static const struct ng_parse_type ng_ksocket_sockaddr_type = {
374 	NULL,
375 	NULL,
376 	NULL,
377 	&ng_ksocket_sockaddr_parse,
378 	&ng_ksocket_sockaddr_unparse,
379 	NULL		/* no such thing as a default struct sockaddr */
380 };
381 
382 /************************************************************************
383 		STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
384  ************************************************************************/
385 
386 /* Get length of the struct ng_ksocket_sockopt value field, which is the
387    just the excess of the message argument portion over the length of
388    the struct ng_ksocket_sockopt. */
389 static int
390 ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
391 	const u_char *start, const u_char *buf)
392 {
393 	static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
394 	const struct ng_ksocket_sockopt *sopt;
395 	const struct ng_mesg *msg;
396 
397 	sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
398 	msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
399 	return msg->header.arglen - sizeof(*sopt);
400 }
401 
402 /* Parse type for the option value part of a struct ng_ksocket_sockopt
403    XXX Eventually, we should handle the different socket options specially.
404    XXX This would avoid byte order problems, eg an integer value of 1 is
405    XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
406 static const struct ng_parse_type ng_ksocket_sockoptval_type = {
407 	&ng_parse_bytearray_type,
408 	&ng_parse_sockoptval_getLength
409 };
410 
411 /* Parse type for struct ng_ksocket_sockopt */
412 static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[]
413 	= NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
414 static const struct ng_parse_type ng_ksocket_sockopt_type = {
415 	&ng_parse_struct_type,
416 	&ng_ksocket_sockopt_type_fields
417 };
418 
419 /* Parse type for struct ng_ksocket_accept */
420 static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[]
421 	= NGM_KSOCKET_ACCEPT_INFO;
422 static const struct ng_parse_type ng_ksocket_accept_type = {
423 	&ng_parse_struct_type,
424 	&ng_ksocket_accept_type_fields
425 };
426 
427 /* List of commands and how to convert arguments to/from ASCII */
428 static const struct ng_cmdlist ng_ksocket_cmds[] = {
429 	{
430 	  NGM_KSOCKET_COOKIE,
431 	  NGM_KSOCKET_BIND,
432 	  "bind",
433 	  &ng_ksocket_sockaddr_type,
434 	  NULL
435 	},
436 	{
437 	  NGM_KSOCKET_COOKIE,
438 	  NGM_KSOCKET_LISTEN,
439 	  "listen",
440 	  &ng_parse_int32_type,
441 	  NULL
442 	},
443 	{
444 	  NGM_KSOCKET_COOKIE,
445 	  NGM_KSOCKET_ACCEPT,
446 	  "accept",
447 	  NULL,
448 	  &ng_ksocket_accept_type
449 	},
450 	{
451 	  NGM_KSOCKET_COOKIE,
452 	  NGM_KSOCKET_CONNECT,
453 	  "connect",
454 	  &ng_ksocket_sockaddr_type,
455 	  &ng_parse_int32_type
456 	},
457 	{
458 	  NGM_KSOCKET_COOKIE,
459 	  NGM_KSOCKET_GETNAME,
460 	  "getname",
461 	  NULL,
462 	  &ng_ksocket_sockaddr_type
463 	},
464 	{
465 	  NGM_KSOCKET_COOKIE,
466 	  NGM_KSOCKET_GETPEERNAME,
467 	  "getpeername",
468 	  NULL,
469 	  &ng_ksocket_sockaddr_type
470 	},
471 	{
472 	  NGM_KSOCKET_COOKIE,
473 	  NGM_KSOCKET_SETOPT,
474 	  "setopt",
475 	  &ng_ksocket_sockopt_type,
476 	  NULL
477 	},
478 	{
479 	  NGM_KSOCKET_COOKIE,
480 	  NGM_KSOCKET_GETOPT,
481 	  "getopt",
482 	  &ng_ksocket_sockopt_type,
483 	  &ng_ksocket_sockopt_type
484 	},
485 	{ 0 }
486 };
487 
488 /* Node type descriptor */
489 static struct ng_type ng_ksocket_typestruct = {
490 	.version =	NG_ABI_VERSION,
491 	.name =		NG_KSOCKET_NODE_TYPE,
492 	.constructor =	ng_ksocket_constructor,
493 	.rcvmsg =	ng_ksocket_rcvmsg,
494 	.shutdown =	ng_ksocket_shutdown,
495 	.newhook =	ng_ksocket_newhook,
496 	.connect =	ng_ksocket_connect,
497 	.rcvdata =	ng_ksocket_rcvdata,
498 	.disconnect =	ng_ksocket_disconnect,
499 	.cmdlist =	ng_ksocket_cmds,
500 };
501 NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
502 
503 #define ERROUT(x)	do { error = (x); goto done; } while (0)
504 
505 /************************************************************************
506 			NETGRAPH NODE STUFF
507  ************************************************************************/
508 
509 /*
510  * Node type constructor
511  * The NODE part is assumed to be all set up.
512  * There is already a reference to the node for us.
513  */
514 static int
515 ng_ksocket_constructor(node_p node)
516 {
517 	priv_p priv;
518 
519 	/* Allocate private structure */
520 	priv = malloc(sizeof(*priv), M_NETGRAPH_KSOCKET, M_NOWAIT | M_ZERO);
521 	if (priv == NULL)
522 		return (ENOMEM);
523 
524 	LIST_INIT(&priv->embryos);
525 	/* cross link them */
526 	priv->node = node;
527 	NG_NODE_SET_PRIVATE(node, priv);
528 
529 	/* Done */
530 	return (0);
531 }
532 
533 /*
534  * Give our OK for a hook to be added. The hook name is of the
535  * form "<family>/<type>/<proto>" where the three components may
536  * be decimal numbers or else aliases from the above lists.
537  *
538  * Connecting a hook amounts to opening the socket.  Disconnecting
539  * the hook closes the socket and destroys the node as well.
540  */
541 static int
542 ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
543 {
544 	struct thread *td = curthread;	/* XXX broken */
545 	const priv_p priv = NG_NODE_PRIVATE(node);
546 	char *s1, *s2, name[NG_HOOKSIZ];
547 	int family, type, protocol, error;
548 
549 	/* Check if we're already connected */
550 	if (priv->hook != NULL)
551 		return (EISCONN);
552 
553 	if (priv->flags & KSF_CLONED) {
554 		if (priv->flags & KSF_EMBRYONIC) {
555 			/* Remove ourselves from our parent's embryo list */
556 			LIST_REMOVE(priv, siblings);
557 			priv->flags &= ~KSF_EMBRYONIC;
558 		}
559 	} else {
560 		/* Extract family, type, and protocol from hook name */
561 		snprintf(name, sizeof(name), "%s", name0);
562 		s1 = name;
563 		if ((s2 = strchr(s1, '/')) == NULL)
564 			return (EINVAL);
565 		*s2++ = '\0';
566 		family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
567 		if (family == -1)
568 			return (EINVAL);
569 		s1 = s2;
570 		if ((s2 = strchr(s1, '/')) == NULL)
571 			return (EINVAL);
572 		*s2++ = '\0';
573 		type = ng_ksocket_parse(ng_ksocket_types, s1, 0);
574 		if (type == -1)
575 			return (EINVAL);
576 		s1 = s2;
577 		protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family);
578 		if (protocol == -1)
579 			return (EINVAL);
580 
581 		/* Create the socket */
582 		error = socreate(family, &priv->so, type, protocol,
583 		   td->td_ucred, td);
584 		if (error != 0)
585 			return (error);
586 
587 		/* XXX call soreserve() ? */
588 	}
589 
590 	/* OK */
591 	priv->hook = hook;
592 
593 	/*
594 	 * In case of misconfigured routing a packet may reenter
595 	 * ksocket node recursively. Decouple stack to avoid possible
596 	 * panics about sleeping with locks held.
597 	 */
598 	NG_HOOK_FORCE_QUEUE(hook);
599 
600 	return(0);
601 }
602 
603 static int
604 ng_ksocket_connect(hook_p hook)
605 {
606 	node_p node = NG_HOOK_NODE(hook);
607 	const priv_p priv = NG_NODE_PRIVATE(node);
608 	struct socket *const so = priv->so;
609 
610 	/* Add our hook for incoming data and other events */
611 	SOCKBUF_LOCK(&priv->so->so_rcv);
612 	soupcall_set(priv->so, SO_RCV, ng_ksocket_incoming, node);
613 	SOCKBUF_UNLOCK(&priv->so->so_rcv);
614 	SOCKBUF_LOCK(&priv->so->so_snd);
615 	soupcall_set(priv->so, SO_SND, ng_ksocket_incoming, node);
616 	SOCKBUF_UNLOCK(&priv->so->so_snd);
617 	SOCK_LOCK(priv->so);
618 	priv->so->so_state |= SS_NBIO;
619 	SOCK_UNLOCK(priv->so);
620 	/*
621 	 * --Original comment--
622 	 * On a cloned socket we may have already received one or more
623 	 * upcalls which we couldn't handle without a hook.  Handle
624 	 * those now.
625 	 * We cannot call the upcall function directly
626 	 * from here, because until this function has returned our
627 	 * hook isn't connected.
628 	 *
629 	 * ---meta comment for -current ---
630 	 * XXX This is dubius.
631 	 * Upcalls between the time that the hook was
632 	 * first created and now (on another processesor) will
633 	 * be earlier on the queue than the request to finalise the hook.
634 	 * By the time the hook is finalised,
635 	 * The queued upcalls will have happened and the code
636 	 * will have discarded them because of a lack of a hook.
637 	 * (socket not open).
638 	 *
639 	 * This is a bad byproduct of the complicated way in which hooks
640 	 * are now created (3 daisy chained async events).
641 	 *
642 	 * Since we are a netgraph operation
643 	 * We know that we hold a lock on this node. This forces the
644 	 * request we make below to be queued rather than implemented
645 	 * immediately which will cause the upcall function to be called a bit
646 	 * later.
647 	 * However, as we will run any waiting queued operations immediately
648 	 * after doing this one, if we have not finalised the other end
649 	 * of the hook, those queued operations will fail.
650 	 */
651 	if (priv->flags & KSF_CLONED) {
652 		ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, M_NOWAIT);
653 	}
654 
655 	return (0);
656 }
657 
658 /*
659  * Receive a control message
660  */
661 static int
662 ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook)
663 {
664 	struct thread *td = curthread;	/* XXX broken */
665 	const priv_p priv = NG_NODE_PRIVATE(node);
666 	struct socket *const so = priv->so;
667 	struct ng_mesg *resp = NULL;
668 	int error = 0;
669 	struct ng_mesg *msg;
670 
671 	NGI_GET_MSG(item, msg);
672 	switch (msg->header.typecookie) {
673 	case NGM_KSOCKET_COOKIE:
674 		switch (msg->header.cmd) {
675 		case NGM_KSOCKET_BIND:
676 		    {
677 			struct sockaddr *const sa
678 			    = (struct sockaddr *)msg->data;
679 
680 			/* Sanity check */
681 			if (msg->header.arglen < SADATA_OFFSET
682 			    || msg->header.arglen < sa->sa_len)
683 				ERROUT(EINVAL);
684 			if (so == NULL)
685 				ERROUT(ENXIO);
686 
687 			/* Bind */
688 			error = sobind(so, sa, td);
689 			break;
690 		    }
691 		case NGM_KSOCKET_LISTEN:
692 		    {
693 			/* Sanity check */
694 			if (msg->header.arglen != sizeof(int32_t))
695 				ERROUT(EINVAL);
696 			if (so == NULL)
697 				ERROUT(ENXIO);
698 
699 			/* Listen */
700 			so->so_state |= SS_NBIO;
701 			error = solisten(so, *((int32_t *)msg->data), td);
702 			if (error == 0) {
703 				SOLISTEN_LOCK(so);
704 				solisten_upcall_set(so,
705 				    ng_ksocket_listen_upcall, priv);
706 				SOLISTEN_UNLOCK(so);
707 			}
708 			break;
709 		    }
710 
711 		case NGM_KSOCKET_ACCEPT:
712 		    {
713 			/* Sanity check */
714 			if (msg->header.arglen != 0)
715 				ERROUT(EINVAL);
716 			if (so == NULL)
717 				ERROUT(ENXIO);
718 
719 			/* Make sure the socket is capable of accepting */
720 			if (!(so->so_options & SO_ACCEPTCONN))
721 				ERROUT(EINVAL);
722 			if (priv->flags & KSF_ACCEPTING)
723 				ERROUT(EALREADY);
724 
725 			/*
726 			 * If a connection is already complete, take it.
727 			 * Otherwise let the upcall function deal with
728 			 * the connection when it comes in.  Don't return
729 			 * EWOULDBLOCK, per ng_ksocket(4) documentation.
730 			 */
731 			error = ng_ksocket_accept(priv);
732 			if (error == EWOULDBLOCK)
733 				error = 0;
734 			if (error != 0)
735 				ERROUT(error);
736 
737 			priv->response_token = msg->header.token;
738 			priv->response_addr = NGI_RETADDR(item);
739 			break;
740 		    }
741 
742 		case NGM_KSOCKET_CONNECT:
743 		    {
744 			struct sockaddr *const sa
745 			    = (struct sockaddr *)msg->data;
746 
747 			/* Sanity check */
748 			if (msg->header.arglen < SADATA_OFFSET
749 			    || msg->header.arglen < sa->sa_len)
750 				ERROUT(EINVAL);
751 			if (so == NULL)
752 				ERROUT(ENXIO);
753 
754 			/* Do connect */
755 			if ((so->so_state & SS_ISCONNECTING) != 0)
756 				ERROUT(EALREADY);
757 			if ((error = soconnect(so, sa, td)) != 0) {
758 				so->so_state &= ~SS_ISCONNECTING;
759 				ERROUT(error);
760 			}
761 			if ((so->so_state & SS_ISCONNECTING) != 0) {
762 				/* We will notify the sender when we connect */
763 				priv->response_token = msg->header.token;
764 				priv->response_addr = NGI_RETADDR(item);
765 				priv->flags |= KSF_CONNECTING;
766 				ERROUT(EINPROGRESS);
767 			}
768 			break;
769 		    }
770 
771 		case NGM_KSOCKET_GETNAME:
772 		case NGM_KSOCKET_GETPEERNAME:
773 		    {
774 			int (*func)(struct socket *so, struct sockaddr *sa);
775 			struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
776 
777 			/* Sanity check */
778 			if (msg->header.arglen != 0)
779 				ERROUT(EINVAL);
780 			if (so == NULL)
781 				ERROUT(ENXIO);
782 
783 			/* Get function */
784 			if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
785 				if ((so->so_state
786 				    & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
787 					ERROUT(ENOTCONN);
788 				func = sopeeraddr;
789 			} else
790 				func = sosockaddr;
791 
792 			/* Get local or peer address */
793 			error = (*func)(so, (struct sockaddr *)&ss);
794 			if (error)
795 				break;
796 
797 			/* Send it back in a response */
798 			NG_MKRESPONSE(resp, msg, ss.ss_len, M_NOWAIT);
799 			if (resp != NULL)
800 				bcopy(&ss, resp->data, ss.ss_len);
801 			else
802 				error = ENOMEM;
803 
804 			break;
805 		    }
806 
807 		case NGM_KSOCKET_GETOPT:
808 		    {
809 			struct ng_ksocket_sockopt *ksopt =
810 			    (struct ng_ksocket_sockopt *)msg->data;
811 			struct sockopt sopt;
812 
813 			/* Sanity check */
814 			if (msg->header.arglen != sizeof(*ksopt))
815 				ERROUT(EINVAL);
816 			if (so == NULL)
817 				ERROUT(ENXIO);
818 
819 			/* Get response with room for option value */
820 			NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
821 			    + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
822 			if (resp == NULL)
823 				ERROUT(ENOMEM);
824 
825 			/* Get socket option, and put value in the response */
826 			sopt.sopt_dir = SOPT_GET;
827 			sopt.sopt_level = ksopt->level;
828 			sopt.sopt_name = ksopt->name;
829 			sopt.sopt_td = NULL;
830 			sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
831 			ksopt = (struct ng_ksocket_sockopt *)resp->data;
832 			sopt.sopt_val = ksopt->value;
833 			if ((error = sogetopt(so, &sopt)) != 0) {
834 				NG_FREE_MSG(resp);
835 				break;
836 			}
837 
838 			/* Set actual value length */
839 			resp->header.arglen = sizeof(*ksopt)
840 			    + sopt.sopt_valsize;
841 			break;
842 		    }
843 
844 		case NGM_KSOCKET_SETOPT:
845 		    {
846 			struct ng_ksocket_sockopt *const ksopt =
847 			    (struct ng_ksocket_sockopt *)msg->data;
848 			const int valsize = msg->header.arglen - sizeof(*ksopt);
849 			struct sockopt sopt;
850 
851 			/* Sanity check */
852 			if (valsize < 0)
853 				ERROUT(EINVAL);
854 			if (so == NULL)
855 				ERROUT(ENXIO);
856 
857 			/* Set socket option */
858 			sopt.sopt_dir = SOPT_SET;
859 			sopt.sopt_level = ksopt->level;
860 			sopt.sopt_name = ksopt->name;
861 			sopt.sopt_val = ksopt->value;
862 			sopt.sopt_valsize = valsize;
863 			sopt.sopt_td = NULL;
864 			error = sosetopt(so, &sopt);
865 			break;
866 		    }
867 
868 		default:
869 			error = EINVAL;
870 			break;
871 		}
872 		break;
873 	default:
874 		error = EINVAL;
875 		break;
876 	}
877 done:
878 	NG_RESPOND_MSG(error, node, item, resp);
879 	NG_FREE_MSG(msg);
880 	return (error);
881 }
882 
883 /*
884  * Receive incoming data on our hook.  Send it out the socket.
885  */
886 static int
887 ng_ksocket_rcvdata(hook_p hook, item_p item)
888 {
889 	struct thread *td = curthread;	/* XXX broken */
890 	const node_p node = NG_HOOK_NODE(hook);
891 	const priv_p priv = NG_NODE_PRIVATE(node);
892 	struct socket *const so = priv->so;
893 	struct sockaddr *sa = NULL;
894 	int error;
895 	struct mbuf *m;
896 #ifdef ALIGNED_POINTER
897 	struct mbuf *n;
898 #endif /* ALIGNED_POINTER */
899 	struct sa_tag *stag;
900 
901 	/* Extract data */
902 	NGI_GET_M(item, m);
903 	NG_FREE_ITEM(item);
904 #ifdef ALIGNED_POINTER
905 	if (!ALIGNED_POINTER(mtod(m, caddr_t), uint32_t)) {
906 		n = m_defrag(m, M_NOWAIT);
907 		if (n == NULL) {
908 			m_freem(m);
909 			return (ENOBUFS);
910 		}
911 		m = n;
912 	}
913 #endif /* ALIGNED_POINTER */
914 	/*
915 	 * Look if socket address is stored in packet tags.
916 	 * If sockaddr is ours, or provided by a third party (zero id),
917 	 * then we accept it.
918 	 */
919 	if (((stag = (struct sa_tag *)m_tag_locate(m, NGM_KSOCKET_COOKIE,
920 	    NG_KSOCKET_TAG_SOCKADDR, NULL)) != NULL) &&
921 	    (stag->id == NG_NODE_ID(node) || stag->id == 0))
922 		sa = &stag->sa;
923 
924 	/* Reset specific mbuf flags to prevent addressing problems. */
925 	m->m_flags &= ~(M_BCAST|M_MCAST);
926 
927 	/* Send packet */
928 	error = sosend(so, sa, 0, m, 0, 0, td);
929 
930 	return (error);
931 }
932 
933 /*
934  * Destroy node
935  */
936 static int
937 ng_ksocket_shutdown(node_p node)
938 {
939 	const priv_p priv = NG_NODE_PRIVATE(node);
940 	struct socket *so = priv->so;
941 	priv_p embryo;
942 
943 	/* Close our socket (if any) */
944 	if (priv->so != NULL) {
945 		if (SOLISTENING(so)) {
946 			SOLISTEN_LOCK(so);
947 			solisten_upcall_set(so, NULL, NULL);
948 			SOLISTEN_UNLOCK(so);
949 		} else {
950 			SOCK_RECVBUF_LOCK(so);
951 			soupcall_clear(so, SO_RCV);
952 			SOCK_RECVBUF_UNLOCK(so);
953 			SOCK_SENDBUF_LOCK(so);
954 			soupcall_clear(so, SO_SND);
955 			SOCK_SENDBUF_UNLOCK(so);
956 		}
957 		soclose(so);
958 		priv->so = NULL;
959 	}
960 
961 	/* If we are an embryo, take ourselves out of the parent's list */
962 	if (priv->flags & KSF_EMBRYONIC) {
963 		LIST_REMOVE(priv, siblings);
964 		priv->flags &= ~KSF_EMBRYONIC;
965 	}
966 
967 	/* Remove any embryonic children we have */
968 	while (!LIST_EMPTY(&priv->embryos)) {
969 		embryo = LIST_FIRST(&priv->embryos);
970 		ng_rmnode_self(embryo->node);
971 	}
972 
973 	/* Take down netgraph node */
974 	bzero(priv, sizeof(*priv));
975 	free(priv, M_NETGRAPH_KSOCKET);
976 	NG_NODE_SET_PRIVATE(node, NULL);
977 	NG_NODE_UNREF(node);		/* let the node escape */
978 	return (0);
979 }
980 
981 /*
982  * Hook disconnection
983  */
984 static int
985 ng_ksocket_disconnect(hook_p hook)
986 {
987 	KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0,
988 	    ("%s: numhooks=%d?", __func__,
989 	    NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook))));
990 	if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
991 		ng_rmnode_self(NG_HOOK_NODE(hook));
992 	return (0);
993 }
994 
995 /************************************************************************
996 			HELPER STUFF
997  ************************************************************************/
998 /*
999  * You should not "just call" a netgraph node function from an external
1000  * asynchronous event. This is because in doing so you are ignoring the
1001  * locking on the netgraph nodes. Instead call your function via ng_send_fn().
1002  * This will call the function you chose, but will first do all the
1003  * locking rigmarole. Your function MAY only be called at some distant future
1004  * time (several millisecs away) so don't give it any arguments
1005  * that may be revoked soon (e.g. on your stack).
1006  *
1007  * To decouple stack, we use queue version of ng_send_fn().
1008  */
1009 
1010 static int
1011 ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
1012 {
1013 	const node_p node = arg;
1014 	const priv_p priv = NG_NODE_PRIVATE(node);
1015 	int wait = ((waitflag & M_WAITOK) ? NG_WAITOK : 0) | NG_QUEUE;
1016 
1017 	/*
1018 	 * Even if node is not locked, as soon as we are called, we assume
1019 	 * it exist and it's private area is valid. With some care we can
1020 	 * access it. Mark node that incoming event for it was sent to
1021 	 * avoid unneded queue trashing.
1022 	 */
1023 	if (atomic_cmpset_int(&priv->fn_sent, 0, 1) &&
1024 	    ng_send_fn1(node, NULL, &ng_ksocket_incoming2, so, 0, wait)) {
1025 		atomic_store_rel_int(&priv->fn_sent, 0);
1026 	}
1027 	return (SU_OK);
1028 }
1029 
1030 /*
1031  * When incoming data is appended to the socket, we get notified here.
1032  * This is also called whenever a significant event occurs for the socket.
1033  * Our original caller may have queued this even some time ago and
1034  * we cannot trust that he even still exists. The node however is being
1035  * held with a reference by the queueing code and guarantied to be valid.
1036  */
1037 static void
1038 ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int arg2)
1039 {
1040 	struct socket *so = arg1;
1041 	const priv_p priv = NG_NODE_PRIVATE(node);
1042 	struct ng_mesg *response;
1043 	int error;
1044 
1045 	KASSERT(so == priv->so, ("%s: wrong socket", __func__));
1046 
1047 	/* Allow next incoming event to be queued. */
1048 	atomic_store_rel_int(&priv->fn_sent, 0);
1049 
1050 	/* Check whether a pending connect operation has completed */
1051 	if (priv->flags & KSF_CONNECTING) {
1052 		if ((error = so->so_error) != 0) {
1053 			so->so_error = 0;
1054 			so->so_state &= ~SS_ISCONNECTING;
1055 		}
1056 		if (!(so->so_state & SS_ISCONNECTING)) {
1057 			NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
1058 			    NGM_KSOCKET_CONNECT, sizeof(int32_t), M_NOWAIT);
1059 			if (response != NULL) {
1060 				response->header.flags |= NGF_RESP;
1061 				response->header.token = priv->response_token;
1062 				*(int32_t *)response->data = error;
1063 				/*
1064 				 * send an async "response" message
1065 				 * to the node that set us up
1066 				 * (if it still exists)
1067 				 */
1068 				NG_SEND_MSG_ID(error, node,
1069 				    response, priv->response_addr, 0);
1070 			}
1071 			priv->flags &= ~KSF_CONNECTING;
1072 		}
1073 	}
1074 
1075 	/*
1076 	 * If we don't have a hook, we must handle data events later.  When
1077 	 * the hook gets created and is connected, this upcall function
1078 	 * will be called again.
1079 	 */
1080 	if (priv->hook == NULL)
1081 		return;
1082 
1083 	/* Read and forward available mbufs. */
1084 	while (1) {
1085 		struct uio uio;
1086 		struct sockaddr *sa;
1087 		struct mbuf *m;
1088 		int flags;
1089 
1090 		/* Try to get next packet from socket. */
1091 		uio.uio_td = NULL;
1092 		uio.uio_resid = IP_MAXPACKET;
1093 		flags = MSG_DONTWAIT;
1094 		sa = NULL;
1095 		if ((error = soreceive(so, (so->so_state & SS_ISCONNECTED) ?
1096 		    NULL : &sa, &uio, &m, NULL, &flags)) != 0)
1097 			break;
1098 
1099 		/* See if we got anything. */
1100 		if (flags & MSG_TRUNC) {
1101 			m_freem(m);
1102 			m = NULL;
1103 		}
1104 		if (m == NULL) {
1105 			if (sa != NULL)
1106 				free(sa, M_SONAME);
1107 			break;
1108 		}
1109 
1110 		KASSERT(m->m_nextpkt == NULL, ("%s: nextpkt", __func__));
1111 
1112 		/*
1113 		 * Stream sockets do not have packet boundaries, so
1114 		 * we have to allocate a header mbuf and attach the
1115 		 * stream of data to it.
1116 		 */
1117 		if (so->so_type == SOCK_STREAM) {
1118 			struct mbuf *mh;
1119 
1120 			mh = m_gethdr(M_NOWAIT, MT_DATA);
1121 			if (mh == NULL) {
1122 				m_freem(m);
1123 				if (sa != NULL)
1124 					free(sa, M_SONAME);
1125 				break;
1126 			}
1127 
1128 			mh->m_next = m;
1129 			for (; m; m = m->m_next)
1130 				mh->m_pkthdr.len += m->m_len;
1131 			m = mh;
1132 		}
1133 
1134 		/* Put peer's socket address (if any) into a tag */
1135 		if (sa != NULL) {
1136 			struct sa_tag	*stag;
1137 
1138 			stag = (struct sa_tag *)m_tag_alloc(NGM_KSOCKET_COOKIE,
1139 			    NG_KSOCKET_TAG_SOCKADDR, sizeof(ng_ID_t) +
1140 			    sa->sa_len, M_NOWAIT);
1141 			if (stag == NULL) {
1142 				free(sa, M_SONAME);
1143 				goto sendit;
1144 			}
1145 			bcopy(sa, &stag->sa, sa->sa_len);
1146 			free(sa, M_SONAME);
1147 			stag->id = NG_NODE_ID(node);
1148 			m_tag_prepend(m, &stag->tag);
1149 		}
1150 
1151 sendit:		/* Forward data with optional peer sockaddr as packet tag */
1152 		NG_SEND_DATA_ONLY(error, priv->hook, m);
1153 	}
1154 
1155 	/*
1156 	 * If the peer has closed the connection, forward a 0-length mbuf
1157 	 * to indicate end-of-file.
1158 	 */
1159 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE &&
1160 	    !(priv->flags & KSF_EOFSEEN)) {
1161 		struct mbuf *m;
1162 
1163 		m = m_gethdr(M_NOWAIT, MT_DATA);
1164 		if (m != NULL)
1165 			NG_SEND_DATA_ONLY(error, priv->hook, m);
1166 		priv->flags |= KSF_EOFSEEN;
1167 	}
1168 }
1169 
1170 static int
1171 ng_ksocket_accept(priv_p priv)
1172 {
1173 	struct socket *const head = priv->so;
1174 	struct socket *so;
1175 	struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1176 	struct ng_mesg *resp;
1177 	struct ng_ksocket_accept *resp_data;
1178 	node_p node;
1179 	priv_p priv2;
1180 	int len;
1181 	int error;
1182 
1183 	SOLISTEN_LOCK(head);
1184 	error = solisten_dequeue(head, &so, SOCK_NONBLOCK);
1185 	if (error == EWOULDBLOCK) {
1186 		priv->flags |= KSF_ACCEPTING;
1187 		return (error);
1188 	}
1189 	priv->flags &= ~KSF_ACCEPTING;
1190 	if (error)
1191 		return (error);
1192 
1193 	if ((error = soaccept(so, (struct sockaddr *)&ss)) != 0)
1194 		return (error);
1195 
1196 	len = OFFSETOF(struct ng_ksocket_accept, addr);
1197 	len += ss.ss_len;
1198 
1199 	NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1200 	    M_NOWAIT);
1201 	if (resp == NULL) {
1202 		soclose(so);
1203 		goto out;
1204 	}
1205 	resp->header.flags |= NGF_RESP;
1206 	resp->header.token = priv->response_token;
1207 
1208 	/* Clone a ksocket node to wrap the new socket */
1209 	error = ng_make_node_common(&ng_ksocket_typestruct, &node);
1210 	if (error) {
1211 		free(resp, M_NETGRAPH);
1212 		soclose(so);
1213 		goto out;
1214 	}
1215 
1216 	if (ng_ksocket_constructor(node) != 0) {
1217 		NG_NODE_UNREF(node);
1218 		free(resp, M_NETGRAPH);
1219 		soclose(so);
1220 		goto out;
1221 	}
1222 
1223 	priv2 = NG_NODE_PRIVATE(node);
1224 	priv2->so = so;
1225 	priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1226 
1227 	/*
1228 	 * Insert the cloned node into a list of embryonic children
1229 	 * on the parent node.  When a hook is created on the cloned
1230 	 * node it will be removed from this list.  When the parent
1231 	 * is destroyed it will destroy any embryonic children it has.
1232 	 */
1233 	LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1234 
1235 	SOCKBUF_LOCK(&so->so_rcv);
1236 	soupcall_set(so, SO_RCV, ng_ksocket_incoming, node);
1237 	SOCKBUF_UNLOCK(&so->so_rcv);
1238 	SOCKBUF_LOCK(&so->so_snd);
1239 	soupcall_set(so, SO_SND, ng_ksocket_incoming, node);
1240 	SOCKBUF_UNLOCK(&so->so_snd);
1241 
1242 	/* Fill in the response data and send it or return it to the caller */
1243 	resp_data = (struct ng_ksocket_accept *)resp->data;
1244 	resp_data->nodeid = NG_NODE_ID(node);
1245 	bcopy(&ss, &resp_data->addr, ss.ss_len);
1246 	NG_SEND_MSG_ID(error, node, resp, priv->response_addr, 0);
1247 
1248 out:
1249 
1250 	return (0);
1251 }
1252 
1253 static int
1254 ng_ksocket_listen_upcall(struct socket *so, void *arg, int waitflag)
1255 {
1256 	priv_p priv = arg;
1257 	int wait = ((waitflag & M_WAITOK) ? NG_WAITOK : 0) | NG_QUEUE;
1258 
1259 	ng_send_fn1(priv->node, NULL, &ng_ksocket_listen_upcall2, priv, 0,
1260 	    wait);
1261 	return (SU_OK);
1262 }
1263 
1264 static void
1265 ng_ksocket_listen_upcall2(node_p node, hook_p hook, void *arg1, int arg2)
1266 {
1267 	const priv_p priv = NG_NODE_PRIVATE(node);
1268 
1269 	(void )ng_ksocket_accept(priv);
1270 }
1271 
1272 /*
1273  * Parse out either an integer value or an alias.
1274  */
1275 static int
1276 ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1277 	const char *s, int family)
1278 {
1279 	int k, val;
1280 	char *eptr;
1281 
1282 	/* Try aliases */
1283 	for (k = 0; aliases[k].name != NULL; k++) {
1284 		if (strcmp(s, aliases[k].name) == 0
1285 		    && aliases[k].family == family)
1286 			return aliases[k].value;
1287 	}
1288 
1289 	/* Try parsing as a number */
1290 	val = (int)strtoul(s, &eptr, 10);
1291 	if (val < 0 || *eptr != '\0')
1292 		return (-1);
1293 	return (val);
1294 }
1295