ng_ksocket.c revision 103205
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 103205 2002-09-11 00:52:50Z benno $
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#define	KSF_SENDING	0x00000020	/* Sending on socket */
99
100/* Netgraph node methods */
101static ng_constructor_t	ng_ksocket_constructor;
102static ng_rcvmsg_t	ng_ksocket_rcvmsg;
103static ng_shutdown_t	ng_ksocket_shutdown;
104static ng_newhook_t	ng_ksocket_newhook;
105static ng_rcvdata_t	ng_ksocket_rcvdata;
106static ng_connect_t	ng_ksocket_connect;
107static ng_disconnect_t	ng_ksocket_disconnect;
108
109/* Alias structure */
110struct ng_ksocket_alias {
111	const char	*name;
112	const int	value;
113	const int	family;
114};
115
116/* Protocol family aliases */
117static const struct ng_ksocket_alias ng_ksocket_families[] = {
118	{ "local",	PF_LOCAL	},
119	{ "inet",	PF_INET		},
120	{ "inet6",	PF_INET6	},
121	{ "atalk",	PF_APPLETALK	},
122	{ "ipx",	PF_IPX		},
123	{ "atm",	PF_ATM		},
124	{ NULL,		-1		},
125};
126
127/* Socket type aliases */
128static const struct ng_ksocket_alias ng_ksocket_types[] = {
129	{ "stream",	SOCK_STREAM	},
130	{ "dgram",	SOCK_DGRAM	},
131	{ "raw",	SOCK_RAW	},
132	{ "rdm",	SOCK_RDM	},
133	{ "seqpacket",	SOCK_SEQPACKET	},
134	{ NULL,		-1		},
135};
136
137/* Protocol aliases */
138static const struct ng_ksocket_alias ng_ksocket_protos[] = {
139	{ "ip",		IPPROTO_IP,		PF_INET		},
140	{ "raw",	IPPROTO_RAW,		PF_INET		},
141	{ "icmp",	IPPROTO_ICMP,		PF_INET		},
142	{ "igmp",	IPPROTO_IGMP,		PF_INET		},
143	{ "tcp",	IPPROTO_TCP,		PF_INET		},
144	{ "udp",	IPPROTO_UDP,		PF_INET		},
145	{ "gre",	IPPROTO_GRE,		PF_INET		},
146	{ "esp",	IPPROTO_ESP,		PF_INET		},
147	{ "ah",		IPPROTO_AH,		PF_INET		},
148	{ "swipe",	IPPROTO_SWIPE,		PF_INET		},
149	{ "encap",	IPPROTO_ENCAP,		PF_INET		},
150	{ "divert",	IPPROTO_DIVERT,		PF_INET		},
151	{ "ddp",	ATPROTO_DDP,		PF_APPLETALK	},
152	{ "aarp",	ATPROTO_AARP,		PF_APPLETALK	},
153	{ NULL,		-1					},
154};
155
156/* Helper functions */
157static int	ng_ksocket_check_accept(priv_p);
158static void	ng_ksocket_finish_accept(priv_p);
159static void	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
160static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
161			const char *s, int family);
162static void	ng_ksocket_incoming2(node_p node, hook_p hook,
163			void *arg1, int waitflag);
164
165/************************************************************************
166			STRUCT SOCKADDR PARSE TYPE
167 ************************************************************************/
168
169/* Get the length of the data portion of a generic struct sockaddr */
170static int
171ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
172	const u_char *start, const u_char *buf)
173{
174	const struct sockaddr *sa;
175
176	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
177	return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
178}
179
180/* Type for the variable length data portion of a generic struct sockaddr */
181static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
182	&ng_parse_bytearray_type,
183	&ng_parse_generic_sockdata_getLength
184};
185
186/* Type for a generic struct sockaddr */
187static const struct ng_parse_struct_field
188    ng_parse_generic_sockaddr_type_fields[] = {
189	  { "len",	&ng_parse_uint8_type			},
190	  { "family",	&ng_parse_uint8_type			},
191	  { "data",	&ng_ksocket_generic_sockdata_type	},
192	  { NULL }
193};
194static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
195	&ng_parse_struct_type,
196	&ng_parse_generic_sockaddr_type_fields
197};
198
199/* Convert a struct sockaddr from ASCII to binary.  If its a protocol
200   family that we specially handle, do that, otherwise defer to the
201   generic parse type ng_ksocket_generic_sockaddr_type. */
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_field ng_ksocket_sockopt_type_fields[]
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_fields
421};
422
423/* Parse type for struct ng_ksocket_accept */
424static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[]
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_fields
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,
590		   td->td_ucred, td);
591		if (error != 0)
592			return (error);
593
594		/* XXX call soreserve() ? */
595
596	}
597
598	/* OK */
599	priv->hook = hook;
600	return(0);
601}
602
603static int
604ng_ksocket_connect(hook_p hook)
605{
606	node_p node = NG_HOOK_NODE(hook);
607	const priv_p priv = NG_NODE_PRIVATE(node);
608	struct socket *const so = priv->so;
609
610	/* Add our hook for incoming data and other events */
611	priv->so->so_upcallarg = (caddr_t)node;
612	priv->so->so_upcall = ng_ksocket_incoming;
613	priv->so->so_rcv.sb_flags |= SB_UPCALL;
614	priv->so->so_snd.sb_flags |= SB_UPCALL;
615	priv->so->so_state |= SS_NBIO;
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 happenned 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	 * immediatly which will cause the upcall function to be called a bit
642	 * later.
643	 * However, as we will run any waiting queued operations immediatly
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 */
657static int
658ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook)
659{
660	struct thread *td = curthread ? curthread : &thread0;	/* 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	ng_ID_t raddr;
667
668	NGI_GET_MSG(item, msg);
669	switch (msg->header.typecookie) {
670	case NGM_KSOCKET_COOKIE:
671		switch (msg->header.cmd) {
672		case NGM_KSOCKET_BIND:
673		    {
674			struct sockaddr *const sa
675			    = (struct sockaddr *)msg->data;
676
677			/* Sanity check */
678			if (msg->header.arglen < SADATA_OFFSET
679			    || msg->header.arglen < sa->sa_len)
680				ERROUT(EINVAL);
681			if (so == NULL)
682				ERROUT(ENXIO);
683
684			/* Bind */
685			error = sobind(so, sa, td);
686			break;
687		    }
688		case NGM_KSOCKET_LISTEN:
689		    {
690			/* Sanity check */
691			if (msg->header.arglen != sizeof(int32_t))
692				ERROUT(EINVAL);
693			if (so == NULL)
694				ERROUT(ENXIO);
695
696			/* Listen */
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			error = ng_ksocket_check_accept(priv);
716			if (error != 0 && error != EWOULDBLOCK)
717				ERROUT(error);
718
719			/*
720			 * If a connection is already complete, take it.
721			 * Otherwise let the upcall function deal with
722			 * the connection when it comes in.
723			 */
724			priv->response_token = msg->header.token;
725			raddr = priv->response_addr = NGI_RETADDR(item);
726			if (error == 0) {
727				ng_ksocket_finish_accept(priv);
728			} else
729				priv->flags |= KSF_ACCEPTING;
730			break;
731		    }
732
733		case NGM_KSOCKET_CONNECT:
734		    {
735			struct sockaddr *const sa
736			    = (struct sockaddr *)msg->data;
737
738			/* Sanity check */
739			if (msg->header.arglen < SADATA_OFFSET
740			    || msg->header.arglen < sa->sa_len)
741				ERROUT(EINVAL);
742			if (so == NULL)
743				ERROUT(ENXIO);
744
745			/* Do connect */
746			if ((so->so_state & SS_ISCONNECTING) != 0)
747				ERROUT(EALREADY);
748			if ((error = soconnect(so, sa, td)) != 0) {
749				so->so_state &= ~SS_ISCONNECTING;
750				ERROUT(error);
751			}
752			if ((so->so_state & SS_ISCONNECTING) != 0)
753				/* We will notify the sender when we connect */
754				priv->response_token = msg->header.token;
755				raddr = priv->response_addr = NGI_RETADDR(item);
756				priv->flags |= KSF_CONNECTING;
757				ERROUT(EINPROGRESS);
758			break;
759		    }
760
761		case NGM_KSOCKET_GETNAME:
762		case NGM_KSOCKET_GETPEERNAME:
763		    {
764			int (*func)(struct socket *so, struct sockaddr **nam);
765			struct sockaddr *sa = NULL;
766			int len;
767
768			/* Sanity check */
769			if (msg->header.arglen != 0)
770				ERROUT(EINVAL);
771			if (so == NULL)
772				ERROUT(ENXIO);
773
774			/* Get function */
775			if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
776				if ((so->so_state
777				    & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
778					ERROUT(ENOTCONN);
779				func = so->so_proto->pr_usrreqs->pru_peeraddr;
780			} else
781				func = so->so_proto->pr_usrreqs->pru_sockaddr;
782
783			/* Get local or peer address */
784			if ((error = (*func)(so, &sa)) != 0)
785				goto bail;
786			len = (sa == NULL) ? 0 : sa->sa_len;
787
788			/* Send it back in a response */
789			NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
790			if (resp == NULL) {
791				error = ENOMEM;
792				goto bail;
793			}
794			bcopy(sa, resp->data, len);
795
796		bail:
797			/* Cleanup */
798			if (sa != NULL)
799				FREE(sa, M_SONAME);
800			break;
801		    }
802
803		case NGM_KSOCKET_GETOPT:
804		    {
805			struct ng_ksocket_sockopt *ksopt =
806			    (struct ng_ksocket_sockopt *)msg->data;
807			struct sockopt sopt;
808
809			/* Sanity check */
810			if (msg->header.arglen != sizeof(*ksopt))
811				ERROUT(EINVAL);
812			if (so == NULL)
813				ERROUT(ENXIO);
814
815			/* Get response with room for option value */
816			NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
817			    + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
818			if (resp == NULL)
819				ERROUT(ENOMEM);
820
821			/* Get socket option, and put value in the response */
822			sopt.sopt_dir = SOPT_GET;
823			sopt.sopt_level = ksopt->level;
824			sopt.sopt_name = ksopt->name;
825			sopt.sopt_td = NULL;
826			sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
827			ksopt = (struct ng_ksocket_sockopt *)resp->data;
828			sopt.sopt_val = ksopt->value;
829			if ((error = sogetopt(so, &sopt)) != 0) {
830				NG_FREE_MSG(resp);
831				break;
832			}
833
834			/* Set actual value length */
835			resp->header.arglen = sizeof(*ksopt)
836			    + sopt.sopt_valsize;
837			break;
838		    }
839
840		case NGM_KSOCKET_SETOPT:
841		    {
842			struct ng_ksocket_sockopt *const ksopt =
843			    (struct ng_ksocket_sockopt *)msg->data;
844			const int valsize = msg->header.arglen - sizeof(*ksopt);
845			struct sockopt sopt;
846
847			/* Sanity check */
848			if (valsize < 0)
849				ERROUT(EINVAL);
850			if (so == NULL)
851				ERROUT(ENXIO);
852
853			/* Set socket option */
854			sopt.sopt_dir = SOPT_SET;
855			sopt.sopt_level = ksopt->level;
856			sopt.sopt_name = ksopt->name;
857			sopt.sopt_val = ksopt->value;
858			sopt.sopt_valsize = valsize;
859			sopt.sopt_td = NULL;
860			error = sosetopt(so, &sopt);
861			break;
862		    }
863
864		default:
865			error = EINVAL;
866			break;
867		}
868		break;
869	default:
870		error = EINVAL;
871		break;
872	}
873done:
874	NG_RESPOND_MSG(error, node, item, resp);
875	NG_FREE_MSG(msg);
876	return (error);
877}
878
879/*
880 * Receive incoming data on our hook.  Send it out the socket.
881 */
882static int
883ng_ksocket_rcvdata(hook_p hook, item_p item)
884{
885	struct thread *td = curthread ? curthread : &thread0;	/* XXX broken */
886	const node_p node = NG_HOOK_NODE(hook);
887	const priv_p priv = NG_NODE_PRIVATE(node);
888	struct socket *const so = priv->so;
889	struct sockaddr *sa = NULL;
890	meta_p meta;
891	int error;
892	struct mbuf *m;
893
894	/* Avoid reentrantly sending on the socket */
895	if ((priv->flags & KSF_SENDING) != 0) {
896		NG_FREE_ITEM(item);
897		return (EDEADLK);
898	}
899
900	/* Extract data and meta information */
901	NGI_GET_M(item, m);
902	NGI_GET_META(item, meta);
903	NG_FREE_ITEM(item);
904
905	/* If any meta info, look for peer socket address */
906	if (meta != NULL) {
907		struct meta_field_header *field;
908
909		/* Look for peer socket address */
910		for (field = &meta->options[0];
911		    (caddr_t)field < (caddr_t)meta + meta->used_len;
912		    field = (struct meta_field_header *)
913		      ((caddr_t)field + field->len)) {
914			if (field->cookie != NGM_KSOCKET_COOKIE
915			    || field->type != NG_KSOCKET_META_SOCKADDR)
916				continue;
917			sa = (struct sockaddr *)field->data;
918			break;
919		}
920	}
921
922	/* Send packet */
923	priv->flags |= KSF_SENDING;
924	error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, sa, 0, m, 0, 0, td);
925	priv->flags &= ~KSF_SENDING;
926
927	/* Clean up and exit */
928	NG_FREE_META(meta);
929	return (error);
930}
931
932/*
933 * Destroy node
934 */
935static int
936ng_ksocket_shutdown(node_p node)
937{
938	const priv_p priv = NG_NODE_PRIVATE(node);
939	priv_p embryo;
940
941	/* Close our socket (if any) */
942	if (priv->so != NULL) {
943		priv->so->so_upcall = NULL;
944		priv->so->so_rcv.sb_flags &= ~SB_UPCALL;
945		priv->so->so_snd.sb_flags &= ~SB_UPCALL;
946		soclose(priv->so);
947		priv->so = NULL;
948	}
949
950	/* If we are an embryo, take ourselves out of the parent's list */
951	if (priv->flags & KSF_EMBRYONIC) {
952		LIST_REMOVE(priv, siblings);
953		priv->flags &= ~KSF_EMBRYONIC;
954	}
955
956	/* Remove any embryonic children we have */
957	while (!LIST_EMPTY(&priv->embryos)) {
958		embryo = LIST_FIRST(&priv->embryos);
959		ng_rmnode_self(embryo->node);
960	}
961
962	/* Take down netgraph node */
963	bzero(priv, sizeof(*priv));
964	FREE(priv, M_NETGRAPH_KSOCKET);
965	NG_NODE_SET_PRIVATE(node, NULL);
966	NG_NODE_UNREF(node);		/* let the node escape */
967	return (0);
968}
969
970/*
971 * Hook disconnection
972 */
973static int
974ng_ksocket_disconnect(hook_p hook)
975{
976	KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0,
977	    ("%s: numhooks=%d?", __func__,
978	    NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook))));
979	if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
980		ng_rmnode_self(NG_HOOK_NODE(hook));
981	return (0);
982}
983
984/************************************************************************
985			HELPER STUFF
986 ************************************************************************/
987/*
988 * You should no-longer "just call" a netgraph node function
989 * from an external asynchronous event.
990 * This is because in doing so you are ignoring the locking on the netgraph
991 * nodes. Instead call your function via
992 * "int ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn,
993 *	 void *arg1, int arg2);"
994 * this will call the function you chose, but will first do all the
995 * locking rigmarole. Your function MAY only be called at some distant future
996 * time (several millisecs away) so don't give it any arguments
997 * that may be revoked soon (e.g. on your stack).
998 * In this case even the 'so' argument is doubtful.
999 * While the function request is being processed the node
1000 * has an extra reference and as such will not disappear until
1001 * the request has at least been done, but the 'so' may not be so lucky.
1002 * handle this by checking the validity of the node in the target function
1003 * before dereferencing the socket pointer.
1004 */
1005
1006static void
1007ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
1008{
1009	const node_p node = arg;
1010
1011	ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, waitflag);
1012}
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 * We know that HOOK is NULL. Because of how we were called we know we have a
1019 * lock on this node an are participating inthe netgraph locking.
1020 * Our original caller may have queued this even some time ago and
1021 * we cannot trust that he even still exists. The node however is being
1022 * held with a reference by the queueing code, at least until we finish,
1023 * even if it has been zapped, so first check it's validiy
1024 * before we trust the socket (which was derived from it).
1025 */
1026static void
1027ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int waitflag)
1028{
1029	struct socket *so = arg1;
1030	const priv_p priv = NG_NODE_PRIVATE(node);
1031	struct mbuf *m;
1032	struct ng_mesg *response;
1033	struct uio auio;
1034	int s, flags, error;
1035
1036	s = splnet();
1037
1038	/* Sanity check */
1039	if (NG_NODE_NOT_VALID(node)) {
1040		splx(s);
1041		return;
1042	}
1043	/* so = priv->so; *//* XXX could have derived this like so */
1044	KASSERT(so == priv->so, ("%s: wrong socket", __func__));
1045
1046	/* Check whether a pending connect operation has completed */
1047	if (priv->flags & KSF_CONNECTING) {
1048		if ((error = so->so_error) != 0) {
1049			so->so_error = 0;
1050			so->so_state &= ~SS_ISCONNECTING;
1051		}
1052		if (!(so->so_state & SS_ISCONNECTING)) {
1053			NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
1054			    NGM_KSOCKET_CONNECT, sizeof(int32_t), waitflag);
1055			if (response != NULL) {
1056				response->header.flags |= NGF_RESP;
1057				response->header.token = priv->response_token;
1058				*(int32_t *)response->data = error;
1059				/*
1060				 * send an async "response" message
1061				 * to the node that set us up
1062				 * (if it still exists)
1063				 */
1064				NG_SEND_MSG_ID(error, node,
1065				    response, priv->response_addr, 0);
1066			}
1067			priv->flags &= ~KSF_CONNECTING;
1068		}
1069	}
1070
1071	/* Check whether a pending accept operation has completed */
1072	if (priv->flags & KSF_ACCEPTING) {
1073		error = ng_ksocket_check_accept(priv);
1074		if (error != EWOULDBLOCK)
1075			priv->flags &= ~KSF_ACCEPTING;
1076		if (error == 0)
1077			ng_ksocket_finish_accept(priv);
1078	}
1079
1080	/*
1081	 * If we don't have a hook, we must handle data events later.  When
1082	 * the hook gets created and is connected, this upcall function
1083	 * will be called again.
1084	 */
1085	if (priv->hook == NULL) {
1086		splx(s);
1087		return;
1088	}
1089
1090	/* Read and forward available mbuf's */
1091	auio.uio_td = NULL;
1092	auio.uio_resid = 1000000000;
1093	flags = MSG_DONTWAIT;
1094	while (1) {
1095		struct sockaddr *sa = NULL;
1096		meta_p meta = NULL;
1097		struct mbuf *n;
1098
1099		/* Try to get next packet from socket */
1100		if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive)
1101		    (so, (so->so_state & SS_ISCONNECTED) ? NULL : &sa,
1102		    &auio, &m, (struct mbuf **)0, &flags)) != 0)
1103			break;
1104
1105		/* See if we got anything */
1106		if (m == NULL) {
1107			if (sa != NULL)
1108				FREE(sa, M_SONAME);
1109			break;
1110		}
1111
1112		/* Don't trust the various socket layers to get the
1113		   packet header and length correct (eg. kern/15175) */
1114		for (n = m, m->m_pkthdr.len = 0; n != NULL; n = n->m_next)
1115			m->m_pkthdr.len += n->m_len;
1116
1117		/* Put peer's socket address (if any) into a meta info blob */
1118		if (sa != NULL) {
1119			struct meta_field_header *mhead;
1120			u_int len;
1121
1122			len = sizeof(*meta) + sizeof(*mhead) + sa->sa_len;
1123			MALLOC(meta, meta_p, len, M_NETGRAPH_META, M_NOWAIT);
1124			if (meta == NULL) {
1125				FREE(sa, M_SONAME);
1126				goto sendit;
1127			}
1128			mhead = &meta->options[0];
1129			bzero(meta, sizeof(*meta));
1130			bzero(mhead, sizeof(*mhead));
1131			meta->allocated_len = len;
1132			meta->used_len = len;
1133			mhead->cookie = NGM_KSOCKET_COOKIE;
1134			mhead->type = NG_KSOCKET_META_SOCKADDR;
1135			mhead->len = sizeof(*mhead) + sa->sa_len;
1136			bcopy(sa, mhead->data, sa->sa_len);
1137			FREE(sa, M_SONAME);
1138		}
1139
1140sendit:		/* Forward data with optional peer sockaddr as meta info */
1141		NG_SEND_DATA(error, priv->hook, m, meta);
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_state & SS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) {
1149		MGETHDR(m, waitflag, MT_DATA);
1150		if (m != NULL) {
1151			m->m_len = m->m_pkthdr.len = 0;
1152			NG_SEND_DATA_ONLY(error, priv->hook, m);
1153		}
1154		priv->flags |= KSF_EOFSEEN;
1155	}
1156	splx(s);
1157}
1158
1159/*
1160 * Check for a completed incoming connection and return 0 if one is found.
1161 * Otherwise return the appropriate error code.
1162 */
1163static int
1164ng_ksocket_check_accept(priv_p priv)
1165{
1166	struct socket *const head = priv->so;
1167	int error;
1168
1169	if ((error = head->so_error) != 0) {
1170		head->so_error = 0;
1171		return error;
1172	}
1173	if (TAILQ_EMPTY(&head->so_comp)) {
1174		if (head->so_state & SS_CANTRCVMORE)
1175			return ECONNABORTED;
1176		return EWOULDBLOCK;
1177	}
1178	return 0;
1179}
1180
1181/*
1182 * Handle the first completed incoming connection, assumed to be already
1183 * on the socket's so_comp queue.
1184 */
1185static void
1186ng_ksocket_finish_accept(priv_p priv)
1187{
1188	struct socket *const head = priv->so;
1189	struct socket *so;
1190	struct sockaddr *sa = NULL;
1191	struct ng_mesg *resp;
1192	struct ng_ksocket_accept *resp_data;
1193	node_p node;
1194	priv_p priv2;
1195	int len;
1196	int error;
1197
1198	so = TAILQ_FIRST(&head->so_comp);
1199	if (so == NULL)		/* Should never happen */
1200		return;
1201	TAILQ_REMOVE(&head->so_comp, so, so_list);
1202	head->so_qlen--;
1203
1204	/* XXX KNOTE(&head->so_rcv.sb_sel.si_note, 0); */
1205
1206	so->so_state &= ~SS_COMP;
1207	so->so_state |= SS_NBIO;
1208	so->so_head = NULL;
1209
1210	soaccept(so, &sa);
1211
1212	len = OFFSETOF(struct ng_ksocket_accept, addr);
1213	if (sa != NULL)
1214		len += sa->sa_len;
1215
1216	NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1217	    M_NOWAIT);
1218	if (resp == NULL) {
1219		soclose(so);
1220		goto out;
1221	}
1222	resp->header.flags |= NGF_RESP;
1223	resp->header.token = priv->response_token;
1224
1225	/* Clone a ksocket node to wrap the new socket */
1226        error = ng_make_node_common(&ng_ksocket_typestruct, &node);
1227        if (error) {
1228		FREE(resp, M_NETGRAPH);
1229		soclose(so);
1230		goto out;
1231	}
1232
1233	if (ng_ksocket_constructor(node) != 0) {
1234		NG_NODE_UNREF(node);
1235		FREE(resp, M_NETGRAPH);
1236		soclose(so);
1237		goto out;
1238	}
1239
1240	priv2 = NG_NODE_PRIVATE(node);
1241	priv2->so = so;
1242	priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1243
1244	/*
1245	 * Insert the cloned node into a list of embryonic children
1246	 * on the parent node.  When a hook is created on the cloned
1247	 * node it will be removed from this list.  When the parent
1248	 * is destroyed it will destroy any embryonic children it has.
1249	 */
1250	LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1251
1252	so->so_upcallarg = (caddr_t)node;
1253	so->so_upcall = ng_ksocket_incoming;
1254	so->so_rcv.sb_flags |= SB_UPCALL;
1255	so->so_snd.sb_flags |= SB_UPCALL;
1256
1257	/* Fill in the response data and send it or return it to the caller */
1258	resp_data = (struct ng_ksocket_accept *)resp->data;
1259	resp_data->nodeid = NG_NODE_ID(node);
1260	if (sa != NULL)
1261		bcopy(sa, &resp_data->addr, sa->sa_len);
1262	NG_SEND_MSG_ID(error, node, resp, priv->response_addr, 0);
1263
1264out:
1265	if (sa != NULL)
1266		FREE(sa, M_SONAME);
1267}
1268
1269/*
1270 * Parse out either an integer value or an alias.
1271 */
1272static int
1273ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1274	const char *s, int family)
1275{
1276	int k, val;
1277	char *eptr;
1278
1279	/* Try aliases */
1280	for (k = 0; aliases[k].name != NULL; k++) {
1281		if (strcmp(s, aliases[k].name) == 0
1282		    && aliases[k].family == family)
1283			return aliases[k].value;
1284	}
1285
1286	/* Try parsing as a number */
1287	val = (int)strtoul(s, &eptr, 10);
1288	if (val < 0 || *eptr != '\0')
1289		return (-1);
1290	return (val);
1291}
1292
1293