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