ng_ksocket.c revision 83366
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: head/sys/netgraph/ng_ksocket.c 83366 2001-09-12 08:38:13Z julian $
40 * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $
41 */
42
43/*
44 * Kernel socket node type.  This node type is basically a kernel-mode
45 * version of a socket... kindof like the reverse of the socket node type.
46 */
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/mbuf.h>
52#include <sys/proc.h>
53#include <sys/malloc.h>
54#include <sys/ctype.h>
55#include <sys/protosw.h>
56#include <sys/errno.h>
57#include <sys/socket.h>
58#include <sys/socketvar.h>
59#include <sys/uio.h>
60#include <sys/un.h>
61
62#include <netgraph/ng_message.h>
63#include <netgraph/netgraph.h>
64#include <netgraph/ng_parse.h>
65#include <netgraph/ng_ksocket.h>
66
67#include <netinet/in.h>
68#include <netatalk/at.h>
69
70#ifdef NG_SEPARATE_MALLOC
71MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock", "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 */
80struct ng_ksocket_private {
81	node_p		node;
82	hook_p		hook;
83	struct socket	*so;
84	LIST_HEAD(, ng_ksocket_private)	embryos;
85	LIST_ENTRY(ng_ksocket_private)	siblings;
86	u_int32_t	flags;
87	u_int32_t	response_token;
88	ng_ID_t		response_addr;
89};
90typedef struct ng_ksocket_private *priv_p;
91
92/* Flags for priv_p */
93#define	KSF_CONNECTING	0x00000001	/* Waiting for connection complete */
94#define	KSF_ACCEPTING	0x00000002	/* Waiting for accept complete */
95#define	KSF_EOFSEEN	0x00000004	/* Have sent 0-length EOF mbuf */
96#define	KSF_CLONED	0x00000008	/* Cloned from an accepting socket */
97#define	KSF_EMBRYONIC	0x00000010	/* Cloned node with no hooks yet */
98
99/* Netgraph node methods */
100static ng_constructor_t	ng_ksocket_constructor;
101static ng_rcvmsg_t	ng_ksocket_rcvmsg;
102static ng_shutdown_t	ng_ksocket_shutdown;
103static ng_newhook_t	ng_ksocket_newhook;
104static ng_rcvdata_t	ng_ksocket_rcvdata;
105static ng_connect_t	ng_ksocket_connect;
106static ng_disconnect_t	ng_ksocket_disconnect;
107
108/* Alias structure */
109struct ng_ksocket_alias {
110	const char	*name;
111	const int	value;
112	const int	family;
113};
114
115/* Protocol family aliases */
116static const struct ng_ksocket_alias ng_ksocket_families[] = {
117	{ "local",	PF_LOCAL	},
118	{ "inet",	PF_INET		},
119	{ "inet6",	PF_INET6	},
120	{ "atalk",	PF_APPLETALK	},
121	{ "ipx",	PF_IPX		},
122	{ "atm",	PF_ATM		},
123	{ NULL,		-1		},
124};
125
126/* Socket type aliases */
127static 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 */
137static const struct ng_ksocket_alias ng_ksocket_protos[] = {
138	{ "ip",		IPPROTO_IP,		PF_INET		},
139	{ "raw",	IPPROTO_IP,		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	{ "divert",	IPPROTO_DIVERT,		PF_INET		},
150	{ "ddp",	ATPROTO_DDP,		PF_APPLETALK	},
151	{ "aarp",	ATPROTO_AARP,		PF_APPLETALK	},
152	{ NULL,		-1					},
153};
154
155/* Helper functions */
156static int	ng_ksocket_check_accept(priv_p);
157static void	ng_ksocket_finish_accept(priv_p);
158static void	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
159static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
160			const char *s, int family);
161static void	ng_ksocket_incoming2(node_p node, hook_p hook,
162			void *arg1, int waitflag);
163
164/************************************************************************
165			STRUCT SOCKADDR PARSE TYPE
166 ************************************************************************/
167
168/* Get the length of the data portion of a generic struct sockaddr */
169static int
170ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
171	const u_char *start, const u_char *buf)
172{
173	const struct sockaddr *sa;
174
175	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
176	return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
177}
178
179/* Type for the variable length data portion of a generic struct sockaddr */
180static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
181	&ng_parse_bytearray_type,
182	&ng_parse_generic_sockdata_getLength
183};
184
185/* Type for a generic struct sockaddr */
186static const struct ng_parse_struct_info ng_parse_generic_sockaddr_type_info = {
187	{
188	  { "len",	&ng_parse_uint8_type			},
189	  { "family",	&ng_parse_uint8_type			},
190	  { "data",	&ng_ksocket_generic_sockdata_type	},
191	  { NULL }
192	}
193};
194static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
195	&ng_parse_struct_type,
196	&ng_parse_generic_sockaddr_type_info
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. */
202static int
203ng_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 = index(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_APPLETALK:	/* XXX implement these someday */
301	case PF_INET6:
302	case PF_IPX:
303#endif
304
305	default:
306		return (EINVAL);
307	}
308
309	/* Done */
310	*buflen = sa->sa_len;
311	return (0);
312}
313
314/* Convert a struct sockaddr from binary to ASCII */
315static int
316ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
317	const u_char *data, int *off, char *cbuf, int cbuflen)
318{
319	const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
320	int slen = 0;
321
322	/* Output socket address, either in special or generic format */
323	switch (sa->sa_family) {
324	case PF_LOCAL:
325	    {
326		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
327		const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
328		const int pathlen = sun->sun_len - pathoff;
329		char pathbuf[SOCK_MAXADDRLEN + 1];
330		char *pathtoken;
331
332		bcopy(sun->sun_path, pathbuf, pathlen);
333		if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL)
334			return (ENOMEM);
335		slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken);
336		FREE(pathtoken, M_NETGRAPH_KSOCKET);
337		if (slen >= cbuflen)
338			return (ERANGE);
339		*off += sun->sun_len;
340		return (0);
341	    }
342
343	case PF_INET:
344	    {
345		const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
346
347		slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
348		  ((const u_char *)&sin->sin_addr)[0],
349		  ((const u_char *)&sin->sin_addr)[1],
350		  ((const u_char *)&sin->sin_addr)[2],
351		  ((const u_char *)&sin->sin_addr)[3]);
352		if (sin->sin_port != 0) {
353			slen += snprintf(cbuf + strlen(cbuf),
354			    cbuflen - strlen(cbuf), ":%d",
355			    (u_int)ntohs(sin->sin_port));
356		}
357		if (slen >= cbuflen)
358			return (ERANGE);
359		*off += sizeof(*sin);
360		return(0);
361	    }
362
363#if 0
364	case PF_APPLETALK:	/* XXX implement these someday */
365	case PF_INET6:
366	case PF_IPX:
367#endif
368
369	default:
370		return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
371		    (&ng_ksocket_generic_sockaddr_type,
372		    data, off, cbuf, cbuflen);
373	}
374}
375
376/* Parse type for struct sockaddr */
377static const struct ng_parse_type ng_ksocket_sockaddr_type = {
378	NULL,
379	NULL,
380	NULL,
381	&ng_ksocket_sockaddr_parse,
382	&ng_ksocket_sockaddr_unparse,
383	NULL		/* no such thing as a default struct sockaddr */
384};
385
386/************************************************************************
387		STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
388 ************************************************************************/
389
390/* Get length of the struct ng_ksocket_sockopt value field, which is the
391   just the excess of the message argument portion over the length of
392   the struct ng_ksocket_sockopt. */
393static int
394ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
395	const u_char *start, const u_char *buf)
396{
397	static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
398	const struct ng_ksocket_sockopt *sopt;
399	const struct ng_mesg *msg;
400
401	sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
402	msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
403	return msg->header.arglen - sizeof(*sopt);
404}
405
406/* Parse type for the option value part of a struct ng_ksocket_sockopt
407   XXX Eventually, we should handle the different socket options specially.
408   XXX This would avoid byte order problems, eg an integer value of 1 is
409   XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
410static const struct ng_parse_type ng_ksocket_sockoptval_type = {
411	&ng_parse_bytearray_type,
412	&ng_parse_sockoptval_getLength
413};
414
415/* Parse type for struct ng_ksocket_sockopt */
416static const struct ng_parse_struct_info ng_ksocket_sockopt_type_info
417	= NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
418static const struct ng_parse_type ng_ksocket_sockopt_type = {
419	&ng_parse_struct_type,
420	&ng_ksocket_sockopt_type_info,
421};
422
423/* Parse type for struct ng_ksocket_accept */
424static const struct ng_parse_struct_info ng_ksocket_accept_type_info
425	= NGM_KSOCKET_ACCEPT_INFO;
426static const struct ng_parse_type ng_ksocket_accept_type = {
427	&ng_parse_struct_type,
428	&ng_ksocket_accept_type_info
429};
430
431/* List of commands and how to convert arguments to/from ASCII */
432static const struct ng_cmdlist ng_ksocket_cmds[] = {
433	{
434	  NGM_KSOCKET_COOKIE,
435	  NGM_KSOCKET_BIND,
436	  "bind",
437	  &ng_ksocket_sockaddr_type,
438	  NULL
439	},
440	{
441	  NGM_KSOCKET_COOKIE,
442	  NGM_KSOCKET_LISTEN,
443	  "listen",
444	  &ng_parse_int32_type,
445	  NULL
446	},
447	{
448	  NGM_KSOCKET_COOKIE,
449	  NGM_KSOCKET_ACCEPT,
450	  "accept",
451	  NULL,
452	  &ng_ksocket_accept_type
453	},
454	{
455	  NGM_KSOCKET_COOKIE,
456	  NGM_KSOCKET_CONNECT,
457	  "connect",
458	  &ng_ksocket_sockaddr_type,
459	  &ng_parse_int32_type
460	},
461	{
462	  NGM_KSOCKET_COOKIE,
463	  NGM_KSOCKET_GETNAME,
464	  "getname",
465	  NULL,
466	  &ng_ksocket_sockaddr_type
467	},
468	{
469	  NGM_KSOCKET_COOKIE,
470	  NGM_KSOCKET_GETPEERNAME,
471	  "getpeername",
472	  NULL,
473	  &ng_ksocket_sockaddr_type
474	},
475	{
476	  NGM_KSOCKET_COOKIE,
477	  NGM_KSOCKET_SETOPT,
478	  "setopt",
479	  &ng_ksocket_sockopt_type,
480	  NULL
481	},
482	{
483	  NGM_KSOCKET_COOKIE,
484	  NGM_KSOCKET_GETOPT,
485	  "getopt",
486	  &ng_ksocket_sockopt_type,
487	  &ng_ksocket_sockopt_type
488	},
489	{ 0 }
490};
491
492/* Node type descriptor */
493static struct ng_type ng_ksocket_typestruct = {
494	NG_ABI_VERSION,
495	NG_KSOCKET_NODE_TYPE,
496	NULL,
497	ng_ksocket_constructor,
498	ng_ksocket_rcvmsg,
499	ng_ksocket_shutdown,
500	ng_ksocket_newhook,
501	NULL,
502	ng_ksocket_connect,
503	ng_ksocket_rcvdata,
504	ng_ksocket_disconnect,
505	ng_ksocket_cmds
506};
507NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
508
509#define ERROUT(x)	do { error = (x); goto done; } while (0)
510
511/************************************************************************
512			NETGRAPH NODE STUFF
513 ************************************************************************/
514
515/*
516 * Node type constructor
517 * The NODE part is assumed to be all set up.
518 * There is already a reference to the node for us.
519 */
520static int
521ng_ksocket_constructor(node_p node)
522{
523	priv_p priv;
524
525	/* Allocate private structure */
526	MALLOC(priv, priv_p, sizeof(*priv),
527	    M_NETGRAPH_KSOCKET, M_NOWAIT | M_ZERO);
528	if (priv == NULL)
529		return (ENOMEM);
530
531	LIST_INIT(&priv->embryos);
532	/* cross link them */
533	priv->node = node;
534	NG_NODE_SET_PRIVATE(node, priv);
535
536	/* Done */
537	return (0);
538}
539
540/*
541 * Give our OK for a hook to be added. The hook name is of the
542 * form "<family>/<type>/<proto>" where the three components may
543 * be decimal numbers or else aliases from the above lists.
544 *
545 * Connecting a hook amounts to opening the socket.  Disconnecting
546 * the hook closes the socket and destroys the node as well.
547 */
548static int
549ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
550{
551	struct thread *td = curthread ? curthread : thread0;	/* XXX broken */
552	const priv_p priv = NG_NODE_PRIVATE(node);
553	char *s1, *s2, name[NG_HOOKLEN+1];
554	int family, type, protocol, error;
555
556	/* Check if we're already connected */
557	if (priv->hook != NULL)
558		return (EISCONN);
559
560	if (priv->flags & KSF_CLONED) {
561		if (priv->flags & KSF_EMBRYONIC) {
562			/* Remove ourselves from our parent's embryo list */
563			LIST_REMOVE(priv, siblings);
564			priv->flags &= ~KSF_EMBRYONIC;
565		}
566	} else {
567		/* Extract family, type, and protocol from hook name */
568		snprintf(name, sizeof(name), "%s", name0);
569		s1 = name;
570		if ((s2 = index(s1, '/')) == NULL)
571			return (EINVAL);
572		*s2++ = '\0';
573		family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
574		if (family == -1)
575			return (EINVAL);
576		s1 = s2;
577		if ((s2 = index(s1, '/')) == NULL)
578			return (EINVAL);
579		*s2++ = '\0';
580		type = ng_ksocket_parse(ng_ksocket_types, s1, 0);
581		if (type == -1)
582			return (EINVAL);
583		s1 = s2;
584		protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family);
585		if (protocol == -1)
586			return (EINVAL);
587
588		/* Create the socket */
589		error = socreate(family, &priv->so, type, protocol, td);
590		if (error != 0)
591			return (error);
592
593		/* XXX call soreserve() ? */
594
595	}
596
597	/* OK */
598	priv->hook = hook;
599	return(0);
600}
601
602static int
603ng_ksocket_connect(hook_p hook)
604{
605	node_p node = NG_HOOK_NODE(hook);
606	const priv_p priv = NG_NODE_PRIVATE(node);
607	struct socket *const so = priv->so;
608
609	/* Add our hook for incoming data and other events */
610	priv->so->so_upcallarg = (caddr_t)node;
611	priv->so->so_upcall = ng_ksocket_incoming;
612	priv->so->so_rcv.sb_flags |= SB_UPCALL;
613	priv->so->so_snd.sb_flags |= SB_UPCALL;
614	priv->so->so_state |= SS_NBIO;
615	/*
616	 * --Original comment--
617	 * On a cloned socket we may have already received one or more
618	 * upcalls which we couldn't handle without a hook.  Handle
619	 * those now.
620	 * We cannot call the upcall function directly
621	 * from here, because until this function has returned our
622	 * hook isn't connected.
623	 *
624	 * ---meta comment for -current ---
625	 * XXX This is dubius.
626	 * Upcalls between the time that the hook was
627	 * first created and now (on another processesor) will
628	 * be earlier on the queue than the request to finalise the hook.
629	 * By the time the hook is finalised,
630	 * The queued upcalls will have happenned and the code
631	 * will have discarded them because of a lack of a hook.
632	 * (socket not open).
633	 *
634	 * This is a bad byproduct of the complicated way in which hooks
635	 * are now created (3 daisy chained async events).
636	 *
637	 * Since we are a netgraph operation
638	 * We know that we hold a lock on this node. This forces the
639	 * request we make below to be queued rather than implemented
640	 * immediatly which will cause the upcall function to be called a bit
641	 * later.
642	 * However, as we will run any waiting queued operations immediatly
643	 * after doing this one, if we have not finalised the other end
644	 * of the hook, those queued operations will fail.
645	 */
646	if (priv->flags & KSF_CLONED) {
647		ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, M_NOWAIT);
648	}
649
650	return (0);
651}
652
653/*
654 * Receive a control message
655 */
656static int
657ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook)
658{
659	struct thread *td = curthread ? curthread : thread0;	/* XXX broken */
660	const priv_p priv = NG_NODE_PRIVATE(node);
661	struct socket *const so = priv->so;
662	struct ng_mesg *resp = NULL;
663	int error = 0;
664	struct ng_mesg *msg;
665	ng_ID_t raddr;
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			error = solisten(so, *((int32_t *)msg->data), td);
697			break;
698		    }
699
700		case NGM_KSOCKET_ACCEPT:
701		    {
702			/* Sanity check */
703			if (msg->header.arglen != 0)
704				ERROUT(EINVAL);
705			if (so == NULL)
706				ERROUT(ENXIO);
707
708			/* Make sure the socket is capable of accepting */
709			if (!(so->so_options & SO_ACCEPTCONN))
710				ERROUT(EINVAL);
711			if (priv->flags & KSF_ACCEPTING)
712				ERROUT(EALREADY);
713
714			error = ng_ksocket_check_accept(priv);
715			if (error != 0 && error != EWOULDBLOCK)
716				ERROUT(error);
717
718			/*
719			 * If a connection is already complete, take it.
720			 * Otherwise let the upcall function deal with
721			 * the connection when it comes in.
722			 */
723			priv->response_token = msg->header.token;
724			raddr = priv->response_addr;
725			if (error == 0) {
726				ng_ksocket_finish_accept(priv);
727			} else
728				priv->flags |= KSF_ACCEPTING;
729			break;
730		    }
731
732		case NGM_KSOCKET_CONNECT:
733		    {
734			struct sockaddr *const sa
735			    = (struct sockaddr *)msg->data;
736
737			/* Sanity check */
738			if (msg->header.arglen < SADATA_OFFSET
739			    || msg->header.arglen < sa->sa_len)
740				ERROUT(EINVAL);
741			if (so == NULL)
742				ERROUT(ENXIO);
743
744			/* Do connect */
745			if ((so->so_state & SS_ISCONNECTING) != 0)
746				ERROUT(EALREADY);
747			if ((error = soconnect(so, sa, td)) != 0) {
748				so->so_state &= ~SS_ISCONNECTING;
749				ERROUT(error);
750			}
751			if ((so->so_state & SS_ISCONNECTING) != 0)
752				/* We will notify the sender when we connect */
753				priv->response_token = msg->header.token;
754				raddr = priv->response_addr;
755				priv->flags |= KSF_CONNECTING;
756				ERROUT(EINPROGRESS);
757			break;
758		    }
759
760		case NGM_KSOCKET_GETNAME:
761		case NGM_KSOCKET_GETPEERNAME:
762		    {
763			int (*func)(struct socket *so, struct sockaddr **nam);
764			struct sockaddr *sa = NULL;
765			int len;
766
767			/* Sanity check */
768			if (msg->header.arglen != 0)
769				ERROUT(EINVAL);
770			if (so == NULL)
771				ERROUT(ENXIO);
772
773			/* Get function */
774			if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
775				if ((so->so_state
776				    & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
777					ERROUT(ENOTCONN);
778				func = so->so_proto->pr_usrreqs->pru_peeraddr;
779			} else
780				func = so->so_proto->pr_usrreqs->pru_sockaddr;
781
782			/* Get local or peer address */
783			if ((error = (*func)(so, &sa)) != 0)
784				goto bail;
785			len = (sa == NULL) ? 0 : sa->sa_len;
786
787			/* Send it back in a response */
788			NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
789			if (resp == NULL) {
790				error = ENOMEM;
791				goto bail;
792			}
793			bcopy(sa, resp->data, len);
794
795		bail:
796			/* Cleanup */
797			if (sa != NULL)
798				FREE(sa, M_SONAME);
799			break;
800		    }
801
802		case NGM_KSOCKET_GETOPT:
803		    {
804			struct ng_ksocket_sockopt *ksopt =
805			    (struct ng_ksocket_sockopt *)msg->data;
806			struct sockopt sopt;
807
808			/* Sanity check */
809			if (msg->header.arglen != sizeof(*ksopt))
810				ERROUT(EINVAL);
811			if (so == NULL)
812				ERROUT(ENXIO);
813
814			/* Get response with room for option value */
815			NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
816			    + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
817			if (resp == NULL)
818				ERROUT(ENOMEM);
819
820			/* Get socket option, and put value in the response */
821			sopt.sopt_dir = SOPT_GET;
822			sopt.sopt_level = ksopt->level;
823			sopt.sopt_name = ksopt->name;
824			sopt.sopt_td = NULL;
825			sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
826			ksopt = (struct ng_ksocket_sockopt *)resp->data;
827			sopt.sopt_val = ksopt->value;
828			if ((error = sogetopt(so, &sopt)) != 0) {
829				NG_FREE_MSG(resp);
830				break;
831			}
832
833			/* Set actual value length */
834			resp->header.arglen = sizeof(*ksopt)
835			    + sopt.sopt_valsize;
836			break;
837		    }
838
839		case NGM_KSOCKET_SETOPT:
840		    {
841			struct ng_ksocket_sockopt *const ksopt =
842			    (struct ng_ksocket_sockopt *)msg->data;
843			const int valsize = msg->header.arglen - sizeof(*ksopt);
844			struct sockopt sopt;
845
846			/* Sanity check */
847			if (valsize < 0)
848				ERROUT(EINVAL);
849			if (so == NULL)
850				ERROUT(ENXIO);
851
852			/* Set socket option */
853			sopt.sopt_dir = SOPT_SET;
854			sopt.sopt_level = ksopt->level;
855			sopt.sopt_name = ksopt->name;
856			sopt.sopt_val = ksopt->value;
857			sopt.sopt_valsize = valsize;
858			sopt.sopt_td = NULL;
859			error = sosetopt(so, &sopt);
860			break;
861		    }
862
863		default:
864			error = EINVAL;
865			break;
866		}
867		break;
868	default:
869		error = EINVAL;
870		break;
871	}
872done:
873	NG_RESPOND_MSG(error, node, item, resp);
874	NG_FREE_MSG(msg);
875	return (error);
876}
877
878/*
879 * Receive incoming data on our hook.  Send it out the socket.
880 */
881static int
882ng_ksocket_rcvdata(hook_p hook, item_p item)
883{
884	struct thread *td = curthread ? curthread : thread0;	/* XXX broken */
885	const node_p node = NG_HOOK_NODE(hook);
886	const priv_p priv = NG_NODE_PRIVATE(node);
887	struct socket *const so = priv->so;
888	int error;
889	struct mbuf *m;
890
891	NGI_GET_M(item, m);
892	NG_FREE_ITEM(item);
893	error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, 0, 0, m, 0, 0, td);
894	return (error);
895}
896
897/*
898 * Destroy node
899 */
900static int
901ng_ksocket_shutdown(node_p node)
902{
903	const priv_p priv = NG_NODE_PRIVATE(node);
904	priv_p embryo;
905
906	/* Close our socket (if any) */
907	if (priv->so != NULL) {
908		priv->so->so_upcall = NULL;
909		priv->so->so_rcv.sb_flags &= ~SB_UPCALL;
910		priv->so->so_snd.sb_flags &= ~SB_UPCALL;
911		soclose(priv->so);
912		priv->so = NULL;
913	}
914
915	/* If we are an embryo, take ourselves out of the parent's list */
916	if (priv->flags & KSF_EMBRYONIC) {
917		LIST_REMOVE(priv, siblings);
918		priv->flags &= ~KSF_EMBRYONIC;
919	}
920
921	/* Remove any embryonic children we have */
922	while (!LIST_EMPTY(&priv->embryos)) {
923		embryo = LIST_FIRST(&priv->embryos);
924		ng_rmnode_self(embryo->node);
925	}
926
927	/* Take down netgraph node */
928	bzero(priv, sizeof(*priv));
929	FREE(priv, M_NETGRAPH_KSOCKET);
930	NG_NODE_SET_PRIVATE(node, NULL);
931	NG_NODE_UNREF(node);		/* let the node escape */
932	return (0);
933}
934
935/*
936 * Hook disconnection
937 */
938static int
939ng_ksocket_disconnect(hook_p hook)
940{
941	KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0,
942	    ("%s: numhooks=%d?", __FUNCTION__,
943	    NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook))));
944	if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
945		ng_rmnode_self(NG_HOOK_NODE(hook));
946	return (0);
947}
948
949/************************************************************************
950			HELPER STUFF
951 ************************************************************************/
952/*
953 * You should no-longer "just call" a netgraph node function
954 * from an external asynchronous event.
955 * This is because in doing so you are ignoring the locking on the netgraph
956 * nodes. Instead call your function via
957 * "int ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn,
958 *	 void *arg1, int arg2);"
959 * this will call the function you chose, but will first do all the
960 * locking rigmarole. Your function MAY only be called at some distant future
961 * time (several millisecs away) so don't give it any arguments
962 * that may be revoked soon (e.g. on your stack).
963 * In this case even the 'so' argument is doubtful.
964 * While the function request is being processed the node
965 * has an extra reference and as such will not disappear until
966 * the request has at least been done, but the 'so' may not be so lucky.
967 * handle this by checking the validity of the node in the target function
968 * before dereferencing the socket pointer.
969 */
970
971static void
972ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
973{
974	const node_p node = arg;
975
976	ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, waitflag);
977}
978
979
980/*
981 * When incoming data is appended to the socket, we get notified here.
982 * This is also called whenever a significant event occurs for the socket.
983 * We know that HOOK is NULL. Because of how we were called we know we have a
984 * lock on this node an are participating inthe netgraph locking.
985 * Our original caller may have queued this even some time ago and
986 * we cannot trust that he even still exists. The node however is being
987 * held with a reference by the queueing code, at least until we finish,
988 * even if it has been zapped, so first check it's validiy
989 * before we trust the socket (which was derived from it).
990 */
991static void
992ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int waitflag)
993{
994	struct socket *so = arg1;
995	const priv_p priv = NG_NODE_PRIVATE(node);
996	struct mbuf *m;
997	struct ng_mesg *response;
998	struct uio auio;
999	int s, flags, error;
1000
1001	s = splnet();
1002
1003	/* Sanity check */
1004	if (NG_NODE_NOT_VALID(node)) {
1005		splx(s);
1006		return;
1007	}
1008	/* so = priv->so; *//* XXX could have derived this like so */
1009	KASSERT(so == priv->so, ("%s: wrong socket", __FUNCTION__));
1010
1011	/* Check whether a pending connect operation has completed */
1012	if (priv->flags & KSF_CONNECTING) {
1013		if ((error = so->so_error) != 0) {
1014			so->so_error = 0;
1015			so->so_state &= ~SS_ISCONNECTING;
1016		}
1017		if (!(so->so_state & SS_ISCONNECTING)) {
1018			NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
1019			    NGM_KSOCKET_CONNECT, sizeof(int32_t), waitflag);
1020			if (response != NULL) {
1021				response->header.flags |= NGF_RESP;
1022				response->header.token = priv->response_token;
1023				*(int32_t *)response->data = error;
1024				/*
1025				 * send an async "response" message
1026				 * to the node that set us up
1027				 * (if it still exists)
1028				 */
1029				NG_SEND_MSG_ID(error, node, response,
1030						priv->response_addr, NULL);
1031			}
1032			priv->flags &= ~KSF_CONNECTING;
1033		}
1034	}
1035
1036	/* Check whether a pending accept operation has completed */
1037	if (priv->flags & KSF_ACCEPTING) {
1038		error = ng_ksocket_check_accept(priv);
1039		if (error != EWOULDBLOCK)
1040			priv->flags &= ~KSF_ACCEPTING;
1041		if (error == 0)
1042			ng_ksocket_finish_accept(priv);
1043	}
1044
1045	/*
1046	 * If we don't have a hook, we must handle data events later.  When
1047	 * the hook gets created and is connected, this upcall function
1048	 * will be called again.
1049	 */
1050	if (priv->hook == NULL) {
1051		splx(s);
1052		return;
1053	}
1054
1055	/* Read and forward available mbuf's */
1056	auio.uio_td = NULL;
1057	auio.uio_resid = 1000000000;
1058	flags = MSG_DONTWAIT;
1059	do {
1060		if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive)
1061		      (so, (struct sockaddr **)0, &auio, &m,
1062		      (struct mbuf **)0, &flags)) == 0
1063		    && m != NULL) {
1064			struct mbuf *n;
1065
1066			/* Don't trust the various socket layers to get the
1067			   packet header and length correct (eg. kern/15175) */
1068			for (n = m, m->m_pkthdr.len = 0; n; n = n->m_next)
1069				m->m_pkthdr.len += n->m_len;
1070			NG_SEND_DATA_ONLY(error, priv->hook, m);
1071		}
1072	} while (error == 0 && m != NULL);
1073
1074	/*
1075	 * If the peer has closed the connection, forward a 0-length mbuf
1076	 * to indicate end-of-file.
1077	 */
1078	if (so->so_state & SS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) {
1079		MGETHDR(m, waitflag, MT_DATA);
1080		if (m != NULL) {
1081			m->m_len = m->m_pkthdr.len = 0;
1082			NG_SEND_DATA_ONLY(error, priv->hook, m);
1083		}
1084		priv->flags |= KSF_EOFSEEN;
1085	}
1086	splx(s);
1087}
1088
1089/*
1090 * Check for a completed incoming connection and return 0 if one is found.
1091 * Otherwise return the appropriate error code.
1092 */
1093static int
1094ng_ksocket_check_accept(priv_p priv)
1095{
1096	struct socket *const head = priv->so;
1097	int error;
1098
1099	if ((error = head->so_error) != 0) {
1100		head->so_error = 0;
1101		return error;
1102	}
1103	if (TAILQ_EMPTY(&head->so_comp)) {
1104		if (head->so_state & SS_CANTRCVMORE)
1105			return ECONNABORTED;
1106		return EWOULDBLOCK;
1107	}
1108	return 0;
1109}
1110
1111/*
1112 * Handle the first completed incoming connection, assumed to be already
1113 * on the socket's so_comp queue.
1114 */
1115static void
1116ng_ksocket_finish_accept(priv_p priv)
1117{
1118	struct socket *const head = priv->so;
1119	struct socket *so;
1120	struct sockaddr *sa = NULL;
1121	struct ng_mesg *resp;
1122	struct ng_ksocket_accept *resp_data;
1123	node_p node;
1124	priv_p priv2;
1125	int len;
1126	int error;
1127
1128	so = TAILQ_FIRST(&head->so_comp);
1129	if (so == NULL)		/* Should never happen */
1130		return;
1131	TAILQ_REMOVE(&head->so_comp, so, so_list);
1132	head->so_qlen--;
1133
1134	/* XXX KNOTE(&head->so_rcv.sb_sel.si_note, 0); */
1135
1136	so->so_state &= ~SS_COMP;
1137	so->so_state |= SS_NBIO;
1138	so->so_head = NULL;
1139
1140	soaccept(so, &sa);
1141
1142	len = OFFSETOF(struct ng_ksocket_accept, addr);
1143	if (sa != NULL)
1144		len += sa->sa_len;
1145
1146	NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1147	    M_NOWAIT);
1148	if (resp == NULL) {
1149		soclose(so);
1150		goto out;
1151	}
1152	resp->header.flags |= NGF_RESP;
1153	resp->header.token = priv->response_token;
1154
1155	/* Clone a ksocket node to wrap the new socket */
1156        error = ng_make_node_common(&ng_ksocket_typestruct, &node);
1157        if (error) {
1158		FREE(resp, M_NETGRAPH);
1159		soclose(so);
1160		goto out;
1161	}
1162
1163	if (ng_ksocket_constructor(node) != 0) {
1164		NG_NODE_UNREF(node);
1165		FREE(resp, M_NETGRAPH);
1166		soclose(so);
1167		goto out;
1168	}
1169
1170	priv2 = NG_NODE_PRIVATE(node);
1171	priv2->so = so;
1172	priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1173
1174	/*
1175	 * Insert the cloned node into a list of embryonic children
1176	 * on the parent node.  When a hook is created on the cloned
1177	 * node it will be removed from this list.  When the parent
1178	 * is destroyed it will destroy any embryonic children it has.
1179	 */
1180	LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1181
1182	so->so_upcallarg = (caddr_t)node;
1183	so->so_upcall = ng_ksocket_incoming;
1184	so->so_rcv.sb_flags |= SB_UPCALL;
1185	so->so_snd.sb_flags |= SB_UPCALL;
1186
1187	/* Fill in the response data and send it or return it to the caller */
1188	resp_data = (struct ng_ksocket_accept *)resp->data;
1189	resp_data->nodeid = NG_NODE_ID(node);
1190	if (sa != NULL)
1191		bcopy(sa, &resp_data->addr, sa->sa_len);
1192	NG_SEND_MSG_ID(error, node, resp, priv->response_addr, NULL);
1193
1194out:
1195	if (sa != NULL)
1196		FREE(sa, M_SONAME);
1197}
1198
1199/*
1200 * Parse out either an integer value or an alias.
1201 */
1202static int
1203ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1204	const char *s, int family)
1205{
1206	int k, val;
1207	char *eptr;
1208
1209	/* Try aliases */
1210	for (k = 0; aliases[k].name != NULL; k++) {
1211		if (strcmp(s, aliases[k].name) == 0
1212		    && aliases[k].family == family)
1213			return aliases[k].value;
1214	}
1215
1216	/* Try parsing as a number */
1217	val = (int)strtoul(s, &eptr, 10);
1218	if (val < 0 || *eptr != '\0')
1219		return (-1);
1220	return (val);
1221}
1222
1223