xref: /dragonfly/sys/netgraph/ksocket/ng_ksocket.c (revision 8e1c6f81)
1 
2 /*
3  * ng_ksocket.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  *
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  *
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_ksocket.c,v 1.5.2.14 2003/08/24 08:24:38 hsu Exp $
40  * $DragonFly: src/sys/netgraph/ksocket/ng_ksocket.c,v 1.16 2008/01/05 14:02:39 swildner Exp $
41  * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $
42  */
43 
44 /*
45  * Kernel socket node type.  This node type is basically a kernel-mode
46  * version of a socket... kindof like the reverse of the socket node type.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/mbuf.h>
53 #include <sys/proc.h>
54 #include <sys/malloc.h>
55 #include <sys/ctype.h>
56 #include <sys/protosw.h>
57 #include <sys/errno.h>
58 #include <sys/fcntl.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/socketops.h>
62 #include <sys/thread2.h>
63 #include <sys/uio.h>
64 #include <sys/un.h>
65 
66 #include <netgraph/ng_message.h>
67 #include <netgraph/netgraph.h>
68 #include <netgraph/ng_parse.h>
69 #include "ng_ksocket.h"
70 
71 #include <netinet/in.h>
72 #include <netproto/atalk/at.h>
73 
74 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
75 #define SADATA_OFFSET	(OFFSETOF(struct sockaddr, sa_data))
76 
77 /* Node private data */
78 struct ng_ksocket_private {
79 	node_p		node;
80 	hook_p		hook;
81 	struct socket	*so;
82 	LIST_HEAD(, ng_ksocket_private)	embryos;
83 	LIST_ENTRY(ng_ksocket_private)	siblings;
84 	u_int32_t	flags;
85 	u_int32_t	response_token;
86 	char		response_addr[NG_PATHSIZ];
87 };
88 typedef struct ng_ksocket_private *priv_p;
89 
90 /* Flags for priv_p */
91 #define	KSF_CONNECTING	0x00000001	/* Waiting for connection complete */
92 #define	KSF_ACCEPTING	0x00000002	/* Waiting for accept complete */
93 #define	KSF_EOFSEEN	0x00000004	/* Have sent 0-length EOF mbuf */
94 #define	KSF_CLONED	0x00000008	/* Cloned from an accepting socket */
95 #define	KSF_EMBRYONIC	0x00000010	/* Cloned node with no hooks yet */
96 #define	KSF_SENDING	0x00000020	/* Sending on socket */
97 
98 /* Internal commands which we send to ourselves */
99 #define	NGM_KSOCKET_INTERNAL_COOKIE	(NGM_KSOCKET_COOKIE + 1)
100 
101 enum {
102 	NGM_KSOCKET_INTERNAL_UPCALL = 1
103 };
104 
105 /* Netgraph node methods */
106 static ng_constructor_t	ng_ksocket_constructor;
107 static ng_rcvmsg_t	ng_ksocket_rcvmsg;
108 static ng_shutdown_t	ng_ksocket_rmnode;
109 static ng_newhook_t	ng_ksocket_newhook;
110 static ng_rcvdata_t	ng_ksocket_rcvdata;
111 static ng_disconnect_t	ng_ksocket_disconnect;
112 
113 /* Alias structure */
114 struct ng_ksocket_alias {
115 	const char	*name;
116 	const int	value;
117 	const int	family;
118 };
119 
120 /* Protocol family aliases */
121 static const struct ng_ksocket_alias ng_ksocket_families[] = {
122 	{ "local",	PF_LOCAL	},
123 	{ "inet",	PF_INET		},
124 	{ "inet6",	PF_INET6	},
125 	{ "atalk",	PF_APPLETALK	},
126 	{ "ipx",	PF_IPX		},
127 	{ "atm",	PF_ATM		},
128 	{ NULL,		-1		},
129 };
130 
131 /* Socket type aliases */
132 static const struct ng_ksocket_alias ng_ksocket_types[] = {
133 	{ "stream",	SOCK_STREAM	},
134 	{ "dgram",	SOCK_DGRAM	},
135 	{ "raw",	SOCK_RAW	},
136 	{ "rdm",	SOCK_RDM	},
137 	{ "seqpacket",	SOCK_SEQPACKET	},
138 	{ NULL,		-1		},
139 };
140 
141 /* Protocol aliases */
142 static const struct ng_ksocket_alias ng_ksocket_protos[] = {
143 	{ "ip",		IPPROTO_IP,		PF_INET		},
144 	{ "raw",	IPPROTO_RAW,		PF_INET		},
145 	{ "icmp",	IPPROTO_ICMP,		PF_INET		},
146 	{ "igmp",	IPPROTO_IGMP,		PF_INET		},
147 	{ "tcp",	IPPROTO_TCP,		PF_INET		},
148 	{ "udp",	IPPROTO_UDP,		PF_INET		},
149 	{ "gre",	IPPROTO_GRE,		PF_INET		},
150 	{ "esp",	IPPROTO_ESP,		PF_INET		},
151 	{ "ah",		IPPROTO_AH,		PF_INET		},
152 	{ "swipe",	IPPROTO_SWIPE,		PF_INET		},
153 	{ "encap",	IPPROTO_ENCAP,		PF_INET		},
154 	{ "divert",	IPPROTO_DIVERT,		PF_INET		},
155 	{ "pim",	IPPROTO_PIM,		PF_INET		},
156 	{ "ddp",	ATPROTO_DDP,		PF_APPLETALK	},
157 	{ "aarp",	ATPROTO_AARP,		PF_APPLETALK	},
158 	{ NULL,		-1					},
159 };
160 
161 /* Helper functions */
162 static int	ng_ksocket_check_accept(priv_p);
163 static void	ng_ksocket_finish_accept(priv_p, struct ng_mesg **);
164 static void	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
165 static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
166 			const char *s, int family);
167 
168 /************************************************************************
169 			STRUCT SOCKADDR PARSE TYPE
170  ************************************************************************/
171 
172 /* Get the length of the data portion of a generic struct sockaddr */
173 static int
174 ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
175 	const u_char *start, const u_char *buf)
176 {
177 	const struct sockaddr *sa;
178 
179 	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
180 	return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
181 }
182 
183 /* Type for the variable length data portion of a generic struct sockaddr */
184 static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
185 	&ng_parse_bytearray_type,
186 	&ng_parse_generic_sockdata_getLength
187 };
188 
189 /* Type for a generic struct sockaddr */
190 static const struct ng_parse_struct_field
191     ng_parse_generic_sockaddr_type_fields[] = {
192 	  { "len",	&ng_parse_uint8_type			},
193 	  { "family",	&ng_parse_uint8_type			},
194 	  { "data",	&ng_ksocket_generic_sockdata_type	},
195 	  { NULL }
196 };
197 static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
198 	&ng_parse_struct_type,
199 	&ng_parse_generic_sockaddr_type_fields
200 };
201 
202 /* Convert a struct sockaddr from ASCII to binary.  If its a protocol
203    family that we specially handle, do that, otherwise defer to the
204    generic parse type ng_ksocket_generic_sockaddr_type. */
205 static int
206 ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
207 	const char *s, int *off, const u_char *const start,
208 	u_char *const buf, int *buflen)
209 {
210 	struct sockaddr *const sa = (struct sockaddr *)buf;
211 	enum ng_parse_token tok;
212 	char fambuf[32];
213 	int family, len;
214 	char *t;
215 
216 	/* If next token is a left curly brace, use generic parse type */
217 	if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
218 		return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
219 		    (&ng_ksocket_generic_sockaddr_type,
220 		    s, off, start, buf, buflen);
221 	}
222 
223 	/* Get socket address family followed by a slash */
224 	while (isspace(s[*off]))
225 		(*off)++;
226 	if ((t = index(s + *off, '/')) == NULL)
227 		return (EINVAL);
228 	if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
229 		return (EINVAL);
230 	strncpy(fambuf, s + *off, len);
231 	fambuf[len] = '\0';
232 	*off += len + 1;
233 	if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
234 		return (EINVAL);
235 
236 	/* Set family */
237 	if (*buflen < SADATA_OFFSET)
238 		return (ERANGE);
239 	sa->sa_family = family;
240 
241 	/* Set family-specific data and length */
242 	switch (sa->sa_family) {
243 	case PF_LOCAL:		/* Get pathname */
244 	    {
245 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
246 		struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
247 		int toklen, pathlen;
248 		char *path;
249 
250 		if ((path = ng_get_string_token(s, off, &toklen)) == NULL)
251 			return (EINVAL);
252 		pathlen = strlen(path);
253 		if (pathlen > SOCK_MAXADDRLEN) {
254 			FREE(path, M_NETGRAPH);
255 			return (E2BIG);
256 		}
257 		if (*buflen < pathoff + pathlen) {
258 			FREE(path, M_NETGRAPH);
259 			return (ERANGE);
260 		}
261 		*off += toklen;
262 		bcopy(path, sun->sun_path, pathlen);
263 		sun->sun_len = pathoff + pathlen;
264 		FREE(path, M_NETGRAPH);
265 		break;
266 	    }
267 
268 	case PF_INET:		/* Get an IP address with optional port */
269 	    {
270 		struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
271 		int i;
272 
273 		/* Parse this: <ipaddress>[:port] */
274 		for (i = 0; i < 4; i++) {
275 			u_long val;
276 			char *eptr;
277 
278 			val = strtoul(s + *off, &eptr, 10);
279 			if (val > 0xff || eptr == s + *off)
280 				return (EINVAL);
281 			*off += (eptr - (s + *off));
282 			((u_char *)&sin->sin_addr)[i] = (u_char)val;
283 			if (i < 3) {
284 				if (s[*off] != '.')
285 					return (EINVAL);
286 				(*off)++;
287 			} else if (s[*off] == ':') {
288 				(*off)++;
289 				val = strtoul(s + *off, &eptr, 10);
290 				if (val > 0xffff || eptr == s + *off)
291 					return (EINVAL);
292 				*off += (eptr - (s + *off));
293 				sin->sin_port = htons(val);
294 			} else
295 				sin->sin_port = 0;
296 		}
297 		bzero(&sin->sin_zero, sizeof(sin->sin_zero));
298 		sin->sin_len = sizeof(*sin);
299 		break;
300 	    }
301 
302 #if 0
303 	case PF_APPLETALK:	/* XXX implement these someday */
304 	case PF_INET6:
305 	case PF_IPX:
306 #endif
307 
308 	default:
309 		return (EINVAL);
310 	}
311 
312 	/* Done */
313 	*buflen = sa->sa_len;
314 	return (0);
315 }
316 
317 /* Convert a struct sockaddr from binary to ASCII */
318 static int
319 ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
320 	const u_char *data, int *off, char *cbuf, int cbuflen)
321 {
322 	const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
323 	int slen = 0;
324 
325 	/* Output socket address, either in special or generic format */
326 	switch (sa->sa_family) {
327 	case PF_LOCAL:
328 	    {
329 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
330 		const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
331 		const int pathlen = sun->sun_len - pathoff;
332 		char pathbuf[SOCK_MAXADDRLEN + 1];
333 		char *pathtoken;
334 
335 		bcopy(sun->sun_path, pathbuf, pathlen);
336 		pathbuf[pathlen] = '\0';
337 		if ((pathtoken = ng_encode_string(pathbuf)) == NULL)
338 			return (ENOMEM);
339 		slen += ksnprintf(cbuf, cbuflen, "local/%s", pathtoken);
340 		FREE(pathtoken, M_NETGRAPH);
341 		if (slen >= cbuflen)
342 			return (ERANGE);
343 		*off += sun->sun_len;
344 		return (0);
345 	    }
346 
347 	case PF_INET:
348 	    {
349 		const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
350 
351 		slen += ksnprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
352 		  ((const u_char *)&sin->sin_addr)[0],
353 		  ((const u_char *)&sin->sin_addr)[1],
354 		  ((const u_char *)&sin->sin_addr)[2],
355 		  ((const u_char *)&sin->sin_addr)[3]);
356 		if (sin->sin_port != 0) {
357 			slen += ksnprintf(cbuf + strlen(cbuf),
358 			    cbuflen - strlen(cbuf), ":%d",
359 			    (u_int)ntohs(sin->sin_port));
360 		}
361 		if (slen >= cbuflen)
362 			return (ERANGE);
363 		*off += sizeof(*sin);
364 		return(0);
365 	    }
366 
367 #if 0
368 	case PF_APPLETALK:	/* XXX implement these someday */
369 	case PF_INET6:
370 	case PF_IPX:
371 #endif
372 
373 	default:
374 		return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
375 		    (&ng_ksocket_generic_sockaddr_type,
376 		    data, off, cbuf, cbuflen);
377 	}
378 }
379 
380 /* Parse type for struct sockaddr */
381 static const struct ng_parse_type ng_ksocket_sockaddr_type = {
382 	NULL,
383 	NULL,
384 	NULL,
385 	&ng_ksocket_sockaddr_parse,
386 	&ng_ksocket_sockaddr_unparse,
387 	NULL		/* no such thing as a default struct sockaddr */
388 };
389 
390 /************************************************************************
391 		STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
392  ************************************************************************/
393 
394 /* Get length of the struct ng_ksocket_sockopt value field, which is the
395    just the excess of the message argument portion over the length of
396    the struct ng_ksocket_sockopt. */
397 static int
398 ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
399 	const u_char *start, const u_char *buf)
400 {
401 	static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
402 	const struct ng_ksocket_sockopt *sopt;
403 	const struct ng_mesg *msg;
404 
405 	sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
406 	msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
407 	return msg->header.arglen - sizeof(*sopt);
408 }
409 
410 /* Parse type for the option value part of a struct ng_ksocket_sockopt
411    XXX Eventually, we should handle the different socket options specially.
412    XXX This would avoid byte order problems, eg an integer value of 1 is
413    XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
414 static const struct ng_parse_type ng_ksocket_sockoptval_type = {
415 	&ng_parse_bytearray_type,
416 	&ng_parse_sockoptval_getLength
417 };
418 
419 /* Parse type for struct ng_ksocket_sockopt */
420 static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[]
421 	= NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
422 static const struct ng_parse_type ng_ksocket_sockopt_type = {
423 	&ng_parse_struct_type,
424 	&ng_ksocket_sockopt_type_fields
425 };
426 
427 /* Parse type for struct ng_ksocket_accept */
428 static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[]
429 	= NGM_KSOCKET_ACCEPT_INFO;
430 static const struct ng_parse_type ng_ksocket_accept_type = {
431 	&ng_parse_struct_type,
432 	&ng_ksocket_accept_type_fields
433 };
434 
435 /* List of commands and how to convert arguments to/from ASCII */
436 static const struct ng_cmdlist ng_ksocket_cmds[] = {
437 	{
438 	  NGM_KSOCKET_COOKIE,
439 	  NGM_KSOCKET_BIND,
440 	  "bind",
441 	  &ng_ksocket_sockaddr_type,
442 	  NULL
443 	},
444 	{
445 	  NGM_KSOCKET_COOKIE,
446 	  NGM_KSOCKET_LISTEN,
447 	  "listen",
448 	  &ng_parse_int32_type,
449 	  NULL
450 	},
451 	{
452 	  NGM_KSOCKET_COOKIE,
453 	  NGM_KSOCKET_ACCEPT,
454 	  "accept",
455 	  NULL,
456 	  &ng_ksocket_accept_type
457 	},
458 	{
459 	  NGM_KSOCKET_COOKIE,
460 	  NGM_KSOCKET_CONNECT,
461 	  "connect",
462 	  &ng_ksocket_sockaddr_type,
463 	  &ng_parse_int32_type
464 	},
465 	{
466 	  NGM_KSOCKET_COOKIE,
467 	  NGM_KSOCKET_GETNAME,
468 	  "getname",
469 	  NULL,
470 	  &ng_ksocket_sockaddr_type
471 	},
472 	{
473 	  NGM_KSOCKET_COOKIE,
474 	  NGM_KSOCKET_GETPEERNAME,
475 	  "getpeername",
476 	  NULL,
477 	  &ng_ksocket_sockaddr_type
478 	},
479 	{
480 	  NGM_KSOCKET_COOKIE,
481 	  NGM_KSOCKET_SETOPT,
482 	  "setopt",
483 	  &ng_ksocket_sockopt_type,
484 	  NULL
485 	},
486 	{
487 	  NGM_KSOCKET_COOKIE,
488 	  NGM_KSOCKET_GETOPT,
489 	  "getopt",
490 	  &ng_ksocket_sockopt_type,
491 	  &ng_ksocket_sockopt_type
492 	},
493 
494 	/* Internal commands */
495 	{
496 	  NGM_KSOCKET_INTERNAL_COOKIE,
497 	  NGM_KSOCKET_INTERNAL_UPCALL,
498 	  "upcall",
499 	  NULL,
500 	  NULL
501 	},
502 	{ 0 }
503 };
504 
505 /* Node type descriptor */
506 static struct ng_type ng_ksocket_typestruct = {
507 	NG_VERSION,
508 	NG_KSOCKET_NODE_TYPE,
509 	NULL,
510 	ng_ksocket_constructor,
511 	ng_ksocket_rcvmsg,
512 	ng_ksocket_rmnode,
513 	ng_ksocket_newhook,
514 	NULL,
515 	NULL,
516 	ng_ksocket_rcvdata,
517 	ng_ksocket_rcvdata,
518 	ng_ksocket_disconnect,
519 	ng_ksocket_cmds
520 };
521 NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
522 
523 #define ERROUT(x)	do { error = (x); goto done; } while (0)
524 
525 /************************************************************************
526 			NETGRAPH NODE STUFF
527  ************************************************************************/
528 
529 /*
530  * Node type constructor
531  */
532 static int
533 ng_ksocket_constructor(node_p *nodep)
534 {
535 	priv_p priv;
536 	int error;
537 
538 	/* Allocate private structure */
539 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
540 	if (priv == NULL)
541 		return (ENOMEM);
542 
543 	/* Call generic node constructor */
544 	if ((error = ng_make_node_common(&ng_ksocket_typestruct, nodep))) {
545 		FREE(priv, M_NETGRAPH);
546 		return (error);
547 	}
548 	(*nodep)->private = priv;
549 	priv->node = *nodep;
550 	LIST_INIT(&priv->embryos);
551 
552 	/* Done */
553 	return (0);
554 }
555 
556 /*
557  * Give our OK for a hook to be added. The hook name is of the
558  * form "<family>/<type>/<proto>" where the three components may
559  * be decimal numbers or else aliases from the above lists.
560  *
561  * Connecting a hook amounts to opening the socket.  Disconnecting
562  * the hook closes the socket and destroys the node as well.
563  */
564 static int
565 ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
566 {
567 	struct thread *td = curthread->td_proc ? curthread : &thread0;	/* XXX broken */
568 	const priv_p priv = node->private;
569 	struct ng_mesg *msg;
570 	char *s1, *s2, name[NG_HOOKSIZ];
571 	int family, type, protocol, error;
572 
573 	/* Check if we're already connected */
574 	if (priv->hook != NULL)
575 		return (EISCONN);
576 
577 	if (priv->flags & KSF_CLONED) {
578 		if (priv->flags & KSF_EMBRYONIC) {
579 			/* Remove ourselves from our parent's embryo list */
580 			LIST_REMOVE(priv, siblings);
581 			priv->flags &= ~KSF_EMBRYONIC;
582 		}
583 	} else {
584 		/* Extract family, type, and protocol from hook name */
585 		ksnprintf(name, sizeof(name), "%s", name0);
586 		s1 = name;
587 		if ((s2 = index(s1, '/')) == NULL)
588 			return (EINVAL);
589 		*s2++ = '\0';
590 		family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
591 		if (family == -1)
592 			return (EINVAL);
593 		s1 = s2;
594 		if ((s2 = index(s1, '/')) == NULL)
595 			return (EINVAL);
596 		*s2++ = '\0';
597 		type = ng_ksocket_parse(ng_ksocket_types, s1, 0);
598 		if (type == -1)
599 			return (EINVAL);
600 		s1 = s2;
601 		protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family);
602 		if (protocol == -1)
603 			return (EINVAL);
604 
605 		/* Create the socket */
606 		error = socreate(family, &priv->so, type, protocol, td);
607 		if (error != 0)
608 			return (error);
609 
610 		/* XXX call soreserve() ? */
611 
612 		/* Add our hook for incoming data and other events */
613 		priv->so->so_upcallarg = (caddr_t)node;
614 		priv->so->so_upcall = ng_ksocket_incoming;
615 		priv->so->so_rcv.ssb_flags |= SSB_UPCALL;
616 		priv->so->so_snd.ssb_flags |= SSB_UPCALL;
617 	}
618 
619 	/* OK */
620 	priv->hook = hook;
621 
622 	/*
623 	 * On a cloned socket we may have already received one or more
624 	 * upcalls which we couldn't handle without a hook.  Handle
625 	 * those now.  We cannot call the upcall function directly
626 	 * from here, because until this function has returned our
627 	 * hook isn't connected.  So we queue a message to ourselves
628 	 * which will cause the upcall function to be called a bit
629 	 * later.
630 	 */
631 	if (priv->flags & KSF_CLONED) {
632 		NG_MKMESSAGE(msg, NGM_KSOCKET_INTERNAL_COOKIE,
633 		    NGM_KSOCKET_INTERNAL_UPCALL, 0, M_NOWAIT);
634 		if (msg != NULL)
635 			ng_queue_msg(node, msg, ".:");
636 	}
637 
638 	return (0);
639 }
640 
641 /*
642  * Receive a control message
643  */
644 static int
645 ng_ksocket_rcvmsg(node_p node, struct ng_mesg *msg,
646 	      const char *raddr, struct ng_mesg **rptr)
647 {
648 	struct thread *td = curthread->td_proc ? curthread : &thread0;	/* XXX broken */
649 	const priv_p priv = node->private;
650 	struct socket *const so = priv->so;
651 	struct ng_mesg *resp = NULL;
652 	int error = 0;
653 
654 	switch (msg->header.typecookie) {
655 	case NGM_KSOCKET_COOKIE:
656 		switch (msg->header.cmd) {
657 		case NGM_KSOCKET_BIND:
658 		    {
659 			struct sockaddr *const sa
660 			    = (struct sockaddr *)msg->data;
661 
662 			/* Sanity check */
663 			if (msg->header.arglen < SADATA_OFFSET
664 			    || msg->header.arglen < sa->sa_len)
665 				ERROUT(EINVAL);
666 			if (so == NULL)
667 				ERROUT(ENXIO);
668 
669 			/* Bind */
670 			error = sobind(so, sa, td);
671 			break;
672 		    }
673 		case NGM_KSOCKET_LISTEN:
674 		    {
675 			/* Sanity check */
676 			if (msg->header.arglen != sizeof(int32_t))
677 				ERROUT(EINVAL);
678 			if (so == NULL)
679 				ERROUT(ENXIO);
680 
681 			/* Listen */
682 			error = solisten(so, *((int32_t *)msg->data), td);
683 			break;
684 		    }
685 
686 		case NGM_KSOCKET_ACCEPT:
687 		    {
688 			/* Sanity check */
689 			if (msg->header.arglen != 0)
690 				ERROUT(EINVAL);
691 			if (so == NULL)
692 				ERROUT(ENXIO);
693 
694 			/* Make sure the socket is capable of accepting */
695 			if (!(so->so_options & SO_ACCEPTCONN))
696 				ERROUT(EINVAL);
697 			if (priv->flags & KSF_ACCEPTING)
698 				ERROUT(EALREADY);
699 
700 			error = ng_ksocket_check_accept(priv);
701 			if (error != 0 && error != EWOULDBLOCK)
702 				ERROUT(error);
703 
704 			/*
705 			 * If a connection is already complete, take it.
706 			 * Otherwise let the upcall function deal with
707 			 * the connection when it comes in.
708 			 */
709 			priv->response_token = msg->header.token;
710 			strcpy(priv->response_addr, raddr);
711 			if (error == 0) {
712 				ng_ksocket_finish_accept(priv,
713 				    rptr != NULL ? &resp : NULL);
714 			} else
715 				priv->flags |= KSF_ACCEPTING;
716 			break;
717 		    }
718 
719 		case NGM_KSOCKET_CONNECT:
720 		    {
721 			struct sockaddr *const sa
722 			    = (struct sockaddr *)msg->data;
723 
724 			/* Sanity check */
725 			if (msg->header.arglen < SADATA_OFFSET
726 			    || msg->header.arglen < sa->sa_len)
727 				ERROUT(EINVAL);
728 			if (so == NULL)
729 				ERROUT(ENXIO);
730 
731 			/* Do connect */
732 			if ((so->so_state & SS_ISCONNECTING) != 0)
733 				ERROUT(EALREADY);
734 			if ((error = soconnect(so, sa, td)) != 0) {
735 				so->so_state &= ~SS_ISCONNECTING;
736 				ERROUT(error);
737 			}
738 			if ((so->so_state & SS_ISCONNECTING) != 0) {
739 				/* We will notify the sender when we connect */
740 				priv->response_token = msg->header.token;
741 				strcpy(priv->response_addr, raddr);
742 				priv->flags |= KSF_CONNECTING;
743 				ERROUT(EINPROGRESS);
744 			}
745 			break;
746 		    }
747 
748 		case NGM_KSOCKET_GETNAME:
749 		case NGM_KSOCKET_GETPEERNAME:
750 		    {
751 			struct sockaddr *sa = NULL;
752 			int len;
753 
754 			/* Sanity check */
755 			if (msg->header.arglen != 0)
756 				ERROUT(EINVAL);
757 			if (so == NULL)
758 				ERROUT(ENXIO);
759 
760 			/* Get function */
761 			if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
762 				if ((so->so_state
763 				    & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
764 					ERROUT(ENOTCONN);
765 				error = so_pru_peeraddr(so, &sa);
766 			} else
767 				error = so_pru_sockaddr(so, &sa);
768 
769 			/* Get local or peer address */
770 			if (error != 0)
771 				goto bail;
772 			len = (sa == NULL) ? 0 : sa->sa_len;
773 
774 			/* Send it back in a response */
775 			NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
776 			if (resp == NULL) {
777 				error = ENOMEM;
778 				goto bail;
779 			}
780 			bcopy(sa, resp->data, len);
781 
782 		bail:
783 			/* Cleanup */
784 			if (sa != NULL)
785 				FREE(sa, M_SONAME);
786 			break;
787 		    }
788 
789 		case NGM_KSOCKET_GETOPT:
790 		    {
791 			struct ng_ksocket_sockopt *ksopt =
792 			    (struct ng_ksocket_sockopt *)msg->data;
793 			struct sockopt sopt;
794 
795 			/* Sanity check */
796 			if (msg->header.arglen != sizeof(*ksopt))
797 				ERROUT(EINVAL);
798 			if (so == NULL)
799 				ERROUT(ENXIO);
800 
801 			/* Get response with room for option value */
802 			NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
803 			    + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
804 			if (resp == NULL)
805 				ERROUT(ENOMEM);
806 
807 			/* Get socket option, and put value in the response */
808 			sopt.sopt_dir = SOPT_GET;
809 			sopt.sopt_level = ksopt->level;
810 			sopt.sopt_name = ksopt->name;
811 			sopt.sopt_td = NULL;
812 			sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
813 			ksopt = (struct ng_ksocket_sockopt *)resp->data;
814 			sopt.sopt_val = ksopt->value;
815 			if ((error = sogetopt(so, &sopt)) != 0) {
816 				FREE(resp, M_NETGRAPH);
817 				break;
818 			}
819 
820 			/* Set actual value length */
821 			resp->header.arglen = sizeof(*ksopt)
822 			    + sopt.sopt_valsize;
823 			break;
824 		    }
825 
826 		case NGM_KSOCKET_SETOPT:
827 		    {
828 			struct ng_ksocket_sockopt *const ksopt =
829 			    (struct ng_ksocket_sockopt *)msg->data;
830 			const int valsize = msg->header.arglen - sizeof(*ksopt);
831 			struct sockopt sopt;
832 
833 			/* Sanity check */
834 			if (valsize < 0)
835 				ERROUT(EINVAL);
836 			if (so == NULL)
837 				ERROUT(ENXIO);
838 
839 			/* Set socket option */
840 			sopt.sopt_dir = SOPT_SET;
841 			sopt.sopt_level = ksopt->level;
842 			sopt.sopt_name = ksopt->name;
843 			sopt.sopt_val = ksopt->value;
844 			sopt.sopt_valsize = valsize;
845 			sopt.sopt_td = NULL;
846 			error = sosetopt(so, &sopt);
847 			break;
848 		    }
849 
850 		default:
851 			error = EINVAL;
852 			break;
853 		}
854 		break;
855 	case NGM_KSOCKET_INTERNAL_COOKIE:
856 		switch (msg->header.cmd) {
857 		case NGM_KSOCKET_INTERNAL_UPCALL:
858 			if (so == NULL)
859 				ERROUT(ENXIO);
860 			(*priv->so->so_upcall)(so, so->so_upcallarg, M_NOWAIT);
861 			break;
862 		default:
863 			error = EINVAL;
864 			break;
865 		}
866 		break;
867 	default:
868 		error = EINVAL;
869 		break;
870 	}
871 	if (rptr)
872 		*rptr = resp;
873 	else if (resp)
874 		FREE(resp, M_NETGRAPH);
875 
876 done:
877 	FREE(msg, M_NETGRAPH);
878 	return (error);
879 }
880 
881 /*
882  * Receive incoming data on our hook.  Send it out the socket.
883  */
884 static int
885 ng_ksocket_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
886 {
887 	struct thread *td = curthread->td_proc ? curthread : &thread0;	/* XXX broken */
888 	const node_p node = hook->node;
889 	const priv_p priv = node->private;
890 	struct socket *const so = priv->so;
891 	struct sockaddr *sa = NULL;
892 	int error;
893 
894 	/* Avoid reentrantly sending on the socket */
895 	if ((priv->flags & KSF_SENDING) != 0) {
896 		NG_FREE_DATA(m, meta);
897 		return (EDEADLK);
898 	}
899 
900 	/* If any meta info, look for peer socket address */
901 	if (meta != NULL) {
902 		struct meta_field_header *field;
903 
904 		/* Look for peer socket address */
905 		for (field = &meta->options[0];
906 		    (caddr_t)field < (caddr_t)meta + meta->used_len;
907 		    field = (struct meta_field_header *)
908 		      ((caddr_t)field + field->len)) {
909 			if (field->cookie != NGM_KSOCKET_COOKIE
910 			    || field->type != NG_KSOCKET_META_SOCKADDR)
911 				continue;
912 			sa = (struct sockaddr *)field->data;
913 			break;
914 		}
915 	}
916 
917 	/* Send packet */
918 	priv->flags |= KSF_SENDING;
919 	error = so_pru_sosend(so, sa, NULL, m, NULL, 0, td);
920 	priv->flags &= ~KSF_SENDING;
921 
922 	/* Clean up and exit */
923 	NG_FREE_META(meta);
924 	return (error);
925 }
926 
927 /*
928  * Destroy node
929  */
930 static int
931 ng_ksocket_rmnode(node_p node)
932 {
933 	const priv_p priv = node->private;
934 	priv_p embryo;
935 
936 	/* Close our socket (if any) */
937 	if (priv->so != NULL) {
938 		priv->so->so_upcall = NULL;
939 		priv->so->so_rcv.ssb_flags &= ~SSB_UPCALL;
940 		priv->so->so_snd.ssb_flags &= ~SSB_UPCALL;
941 		soclose(priv->so, FNONBLOCK);
942 		priv->so = NULL;
943 	}
944 
945 	/* If we are an embryo, take ourselves out of the parent's list */
946 	if (priv->flags & KSF_EMBRYONIC) {
947 		LIST_REMOVE(priv, siblings);
948 		priv->flags &= ~KSF_EMBRYONIC;
949 	}
950 
951 	/* Remove any embryonic children we have */
952 	while (!LIST_EMPTY(&priv->embryos)) {
953 		embryo = LIST_FIRST(&priv->embryos);
954 		ng_rmnode(embryo->node);
955 	}
956 
957 	/* Take down netgraph node */
958 	node->flags |= NG_INVALID;
959 	ng_cutlinks(node);
960 	ng_unname(node);
961 	bzero(priv, sizeof(*priv));
962 	FREE(priv, M_NETGRAPH);
963 	node->private = NULL;
964 	ng_unref(node);		/* let the node escape */
965 	return (0);
966 }
967 
968 /*
969  * Hook disconnection
970  */
971 static int
972 ng_ksocket_disconnect(hook_p hook)
973 {
974 	KASSERT(hook->node->numhooks == 0,
975 	    ("%s: numhooks=%d?", __func__, hook->node->numhooks));
976 	ng_rmnode(hook->node);
977 	return (0);
978 }
979 
980 /************************************************************************
981 			HELPER STUFF
982  ************************************************************************/
983 
984 /*
985  * When incoming data is appended to the socket, we get notified here.
986  * This is also called whenever a significant event occurs for the socket.
987  */
988 static void
989 ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
990 {
991 	const node_p node = arg;
992 	const priv_p priv = node->private;
993 	struct ng_mesg *response;
994 	int error;
995 
996 	crit_enter();
997 
998 	/* Sanity check */
999 	if ((node->flags & NG_INVALID) != 0) {
1000 		crit_exit();
1001 		return;
1002 	}
1003 	KASSERT(so == priv->so, ("%s: wrong socket", __func__));
1004 
1005 	/* Check whether a pending connect operation has completed */
1006 	if (priv->flags & KSF_CONNECTING) {
1007 		if ((error = so->so_error) != 0) {
1008 			so->so_error = 0;
1009 			so->so_state &= ~SS_ISCONNECTING;
1010 		}
1011 		if (!(so->so_state & SS_ISCONNECTING)) {
1012 			NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
1013 			    NGM_KSOCKET_CONNECT, sizeof(int32_t), waitflag);
1014 			if (response != NULL) {
1015 				response->header.flags |= NGF_RESP;
1016 				response->header.token = priv->response_token;
1017 				*(int32_t *)response->data = error;
1018 				/*
1019 				 * XXX We use ng_queue_msg here because we are
1020 				 * being called from deep in the bowels of the TCP
1021 				 * stack.  Is this right, or should we let the
1022 				 * receiver of the message worry about that?
1023 				 */
1024 				ng_queue_msg(node, response,
1025 				    priv->response_addr);
1026 			}
1027 			priv->flags &= ~KSF_CONNECTING;
1028 		}
1029 	}
1030 
1031 	/* Check whether a pending accept operation has completed */
1032 	if (priv->flags & KSF_ACCEPTING) {
1033 		error = ng_ksocket_check_accept(priv);
1034 		if (error != EWOULDBLOCK)
1035 			priv->flags &= ~KSF_ACCEPTING;
1036 		if (error == 0)
1037 			ng_ksocket_finish_accept(priv, NULL);
1038 	}
1039 
1040 	/*
1041 	 * If we don't have a hook, we must handle data events later.  When
1042 	 * the hook gets created and is connected, this upcall function
1043 	 * will be called again.
1044 	 */
1045 	if (priv->hook == NULL) {
1046 		crit_exit();
1047 		return;
1048 	}
1049 
1050 	/* Read and forward available mbuf's */
1051 	while (1) {
1052 		struct sockaddr *sa = NULL;
1053 		struct sockbuf sio;
1054 		meta_p meta = NULL;
1055 		struct mbuf *n;
1056 		int flags;
1057 
1058 		sbinit(&sio, 1000000000);
1059 		flags = MSG_DONTWAIT;
1060 
1061 		/* Try to get next packet from socket */
1062 		error = so_pru_soreceive(so,
1063 				((so->so_state & SS_ISCONNECTED) ? NULL : &sa),
1064 				NULL, &sio, NULL, &flags);
1065 		if (error)
1066 			break;
1067 
1068 		/* See if we got anything */
1069 		if (sio.sb_mb == NULL) {
1070 			if (sa != NULL)
1071 				FREE(sa, M_SONAME);
1072 			break;
1073 		}
1074 
1075 		/* Don't trust the various socket layers to get the
1076 		   packet header and length correct (eg. kern/15175) */
1077 		sio.sb_mb->m_pkthdr.len = 0;
1078 		for (n = sio.sb_mb; n != NULL; n = n->m_next)
1079 			sio.sb_mb->m_pkthdr.len += n->m_len;
1080 
1081 		/* Put peer's socket address (if any) into a meta info blob */
1082 		if (sa != NULL) {
1083 			struct meta_field_header *mhead;
1084 			u_int len;
1085 
1086 			len = sizeof(*meta) + sizeof(*mhead) + sa->sa_len;
1087 			MALLOC(meta, meta_p, len, M_NETGRAPH, M_NOWAIT);
1088 			if (meta == NULL) {
1089 				FREE(sa, M_SONAME);
1090 				goto sendit;
1091 			}
1092 			mhead = &meta->options[0];
1093 			bzero(meta, sizeof(*meta));
1094 			bzero(mhead, sizeof(*mhead));
1095 			meta->allocated_len = len;
1096 			meta->used_len = len;
1097 			mhead->cookie = NGM_KSOCKET_COOKIE;
1098 			mhead->type = NG_KSOCKET_META_SOCKADDR;
1099 			mhead->len = sizeof(*mhead) + sa->sa_len;
1100 			bcopy(sa, mhead->data, sa->sa_len);
1101 			FREE(sa, M_SONAME);
1102 		}
1103 sendit:		/* Forward data with optional peer sockaddr as meta info */
1104 		NG_SEND_DATA(error, priv->hook, sio.sb_mb, meta);
1105 	}
1106 
1107 	/*
1108 	 * If the peer has closed the connection, forward a 0-length mbuf
1109 	 * to indicate end-of-file.
1110 	 */
1111 	if (so->so_state & SS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) {
1112 		struct mbuf *m;
1113 
1114 		MGETHDR(m, waitflag, MT_DATA);
1115 		if (m != NULL) {
1116 			m->m_len = m->m_pkthdr.len = 0;
1117 			NG_SEND_DATA_ONLY(error, priv->hook, m);
1118 		}
1119 		priv->flags |= KSF_EOFSEEN;
1120 	}
1121 
1122 	crit_exit();
1123 }
1124 
1125 /*
1126  * Check for a completed incoming connection and return 0 if one is found.
1127  * Otherwise return the appropriate error code.
1128  */
1129 static int
1130 ng_ksocket_check_accept(priv_p priv)
1131 {
1132 	struct socket *const head = priv->so;
1133 	int error;
1134 
1135 	if ((error = head->so_error) != 0) {
1136 		head->so_error = 0;
1137 		return error;
1138 	}
1139 	if (TAILQ_EMPTY(&head->so_comp)) {
1140 		if (head->so_state & SS_CANTRCVMORE)
1141 			return ECONNABORTED;
1142 		return EWOULDBLOCK;
1143 	}
1144 	return 0;
1145 }
1146 
1147 /*
1148  * Handle the first completed incoming connection, assumed to be already
1149  * on the socket's so_comp queue.
1150  */
1151 static void
1152 ng_ksocket_finish_accept(priv_p priv, struct ng_mesg **rptr)
1153 {
1154 	struct socket *const head = priv->so;
1155 	struct socket *so;
1156 	struct sockaddr *sa = NULL;
1157 	struct ng_mesg *resp;
1158 	struct ng_ksocket_accept *resp_data;
1159 	node_p node2;
1160 	priv_p priv2;
1161 	int len;
1162 
1163 	so = TAILQ_FIRST(&head->so_comp);
1164 	if (so == NULL)		/* Should never happen */
1165 		return;
1166 	TAILQ_REMOVE(&head->so_comp, so, so_list);
1167 	head->so_qlen--;
1168 
1169 	/* XXX KNOTE(&head->so_rcv.ssb_sel.si_note, 0); */
1170 
1171 	so->so_state &= ~SS_COMP;
1172 	so->so_head = NULL;
1173 
1174 	soaccept(so, &sa);
1175 
1176 	len = OFFSETOF(struct ng_ksocket_accept, addr);
1177 	if (sa != NULL)
1178 		len += sa->sa_len;
1179 
1180 	NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1181 	    M_NOWAIT);
1182 	if (resp == NULL) {
1183 		soclose(so, FNONBLOCK);
1184 		goto out;
1185 	}
1186 	resp->header.flags |= NGF_RESP;
1187 	resp->header.token = priv->response_token;
1188 
1189 	/* Clone a ksocket node to wrap the new socket */
1190 	if (ng_ksocket_constructor(&node2) != 0) {
1191 		FREE(resp, M_NETGRAPH);
1192 		soclose(so, FNONBLOCK);
1193 		goto out;
1194 	}
1195 	priv2 = (priv_p)node2->private;
1196 	priv2->so = so;
1197 	priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1198 
1199 	/*
1200 	 * Insert the cloned node into a list of embryonic children
1201 	 * on the parent node.  When a hook is created on the cloned
1202 	 * node it will be removed from this list.  When the parent
1203 	 * is destroyed it will destroy any embryonic children it has.
1204 	 */
1205 	LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1206 
1207 	so->so_upcallarg = (caddr_t)node2;
1208 	so->so_upcall = ng_ksocket_incoming;
1209 	so->so_rcv.ssb_flags |= SSB_UPCALL;
1210 	so->so_snd.ssb_flags |= SSB_UPCALL;
1211 
1212 	/* Fill in the response data and send it or return it to the caller */
1213 	resp_data = (struct ng_ksocket_accept *)resp->data;
1214 	resp_data->nodeid = node2->ID;
1215 	if (sa != NULL)
1216 		bcopy(sa, &resp_data->addr, sa->sa_len);
1217 	if (rptr != NULL)
1218 		*rptr = resp;
1219 	else
1220 		ng_queue_msg(priv->node, resp, priv->response_addr);
1221 
1222 out:
1223 	if (sa != NULL)
1224 		FREE(sa, M_SONAME);
1225 }
1226 
1227 /*
1228  * Parse out either an integer value or an alias.
1229  */
1230 static int
1231 ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1232 	const char *s, int family)
1233 {
1234 	int k, val;
1235 	char *eptr;
1236 
1237 	/* Try aliases */
1238 	for (k = 0; aliases[k].name != NULL; k++) {
1239 		if (strcmp(s, aliases[k].name) == 0
1240 		    && aliases[k].family == family)
1241 			return aliases[k].value;
1242 	}
1243 
1244 	/* Try parsing as a number */
1245 	val = (int)strtoul(s, &eptr, 10);
1246 	if (val < 0 || *eptr != '\0')
1247 		return (-1);
1248 	return (val);
1249 }
1250 
1251