1/*
2 * ng_ksocket.c
3 */
4
5/*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 *    copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 *    Communications, Inc. trademarks, including the mark "WHISTLE
17 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 *    such appears in the above copyright notice or in the software.
19 *
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * Author: Archie Cobbs <archie@freebsd.org>
39 *
40 * $FreeBSD$
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/socket.h>
59#include <sys/socketvar.h>
60#include <sys/uio.h>
61#include <sys/un.h>
62
63#include <netgraph/ng_message.h>
64#include <netgraph/netgraph.h>
65#include <netgraph/ng_parse.h>
66#include <netgraph/ng_ksocket.h>
67
68#include <netinet/in.h>
69#include <netinet/ip.h>
70
71#ifdef NG_SEPARATE_MALLOC
72static MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock",
73    "netgraph ksock node");
74#else
75#define M_NETGRAPH_KSOCKET M_NETGRAPH
76#endif
77
78#define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
79#define SADATA_OFFSET	(OFFSETOF(struct sockaddr, sa_data))
80
81/* Node private data */
82struct ng_ksocket_private {
83	node_p		node;
84	hook_p		hook;
85	struct socket	*so;
86	int		fn_sent;	/* FN call on incoming event was sent */
87	LIST_HEAD(, ng_ksocket_private)	embryos;
88	LIST_ENTRY(ng_ksocket_private)	siblings;
89	u_int32_t	flags;
90	u_int32_t	response_token;
91	ng_ID_t		response_addr;
92};
93typedef struct ng_ksocket_private *priv_p;
94
95/* Flags for priv_p */
96#define	KSF_CONNECTING	0x00000001	/* Waiting for connection complete */
97#define	KSF_ACCEPTING	0x00000002	/* Waiting for accept complete */
98#define	KSF_EOFSEEN	0x00000004	/* Have sent 0-length EOF mbuf */
99#define	KSF_CLONED	0x00000008	/* Cloned from an accepting socket */
100#define	KSF_EMBRYONIC	0x00000010	/* Cloned node with no hooks yet */
101
102/* Netgraph node methods */
103static ng_constructor_t	ng_ksocket_constructor;
104static ng_rcvmsg_t	ng_ksocket_rcvmsg;
105static ng_shutdown_t	ng_ksocket_shutdown;
106static ng_newhook_t	ng_ksocket_newhook;
107static ng_rcvdata_t	ng_ksocket_rcvdata;
108static ng_connect_t	ng_ksocket_connect;
109static ng_disconnect_t	ng_ksocket_disconnect;
110
111/* Alias structure */
112struct ng_ksocket_alias {
113	const char	*name;
114	const int	value;
115	const int	family;
116};
117
118/* Protocol family aliases */
119static const struct ng_ksocket_alias ng_ksocket_families[] = {
120	{ "local",	PF_LOCAL	},
121	{ "inet",	PF_INET		},
122	{ "inet6",	PF_INET6	},
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	{ "pim",	IPPROTO_PIM,		PF_INET		},
152	{ NULL,		-1					},
153};
154
155/* Helper functions */
156static int	ng_ksocket_accept(priv_p);
157static int	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
158static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
159			const char *s, int family);
160static void	ng_ksocket_incoming2(node_p node, hook_p hook,
161			void *arg1, int arg2);
162
163/************************************************************************
164			STRUCT SOCKADDR PARSE TYPE
165 ************************************************************************/
166
167/* Get the length of the data portion of a generic struct sockaddr */
168static int
169ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
170	const u_char *start, const u_char *buf)
171{
172	const struct sockaddr *sa;
173
174	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
175	return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
176}
177
178/* Type for the variable length data portion of a generic struct sockaddr */
179static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
180	&ng_parse_bytearray_type,
181	&ng_parse_generic_sockdata_getLength
182};
183
184/* Type for a generic struct sockaddr */
185static const struct ng_parse_struct_field
186    ng_parse_generic_sockaddr_type_fields[] = {
187	  { "len",	&ng_parse_uint8_type			},
188	  { "family",	&ng_parse_uint8_type			},
189	  { "data",	&ng_ksocket_generic_sockdata_type	},
190	  { NULL }
191};
192static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
193	&ng_parse_struct_type,
194	&ng_parse_generic_sockaddr_type_fields
195};
196
197/* Convert a struct sockaddr from ASCII to binary.  If its a protocol
198   family that we specially handle, do that, otherwise defer to the
199   generic parse type ng_ksocket_generic_sockaddr_type. */
200static int
201ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
202	const char *s, int *off, const u_char *const start,
203	u_char *const buf, int *buflen)
204{
205	struct sockaddr *const sa = (struct sockaddr *)buf;
206	enum ng_parse_token tok;
207	char fambuf[32];
208	int family, len;
209	char *t;
210
211	/* If next token is a left curly brace, use generic parse type */
212	if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
213		return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
214		    (&ng_ksocket_generic_sockaddr_type,
215		    s, off, start, buf, buflen);
216	}
217
218	/* Get socket address family followed by a slash */
219	while (isspace(s[*off]))
220		(*off)++;
221	if ((t = strchr(s + *off, '/')) == NULL)
222		return (EINVAL);
223	if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
224		return (EINVAL);
225	strncpy(fambuf, s + *off, len);
226	fambuf[len] = '\0';
227	*off += len + 1;
228	if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
229		return (EINVAL);
230
231	/* Set family */
232	if (*buflen < SADATA_OFFSET)
233		return (ERANGE);
234	sa->sa_family = family;
235
236	/* Set family-specific data and length */
237	switch (sa->sa_family) {
238	case PF_LOCAL:		/* Get pathname */
239	    {
240		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
241		struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
242		int toklen, pathlen;
243		char *path;
244
245		if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL)
246			return (EINVAL);
247		pathlen = strlen(path);
248		if (pathlen > SOCK_MAXADDRLEN) {
249			free(path, M_NETGRAPH_KSOCKET);
250			return (E2BIG);
251		}
252		if (*buflen < pathoff + pathlen) {
253			free(path, M_NETGRAPH_KSOCKET);
254			return (ERANGE);
255		}
256		*off += toklen;
257		bcopy(path, sun->sun_path, pathlen);
258		sun->sun_len = pathoff + pathlen;
259		free(path, M_NETGRAPH_KSOCKET);
260		break;
261	    }
262
263	case PF_INET:		/* Get an IP address with optional port */
264	    {
265		struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
266		int i;
267
268		/* Parse this: <ipaddress>[:port] */
269		for (i = 0; i < 4; i++) {
270			u_long val;
271			char *eptr;
272
273			val = strtoul(s + *off, &eptr, 10);
274			if (val > 0xff || eptr == s + *off)
275				return (EINVAL);
276			*off += (eptr - (s + *off));
277			((u_char *)&sin->sin_addr)[i] = (u_char)val;
278			if (i < 3) {
279				if (s[*off] != '.')
280					return (EINVAL);
281				(*off)++;
282			} else if (s[*off] == ':') {
283				(*off)++;
284				val = strtoul(s + *off, &eptr, 10);
285				if (val > 0xffff || eptr == s + *off)
286					return (EINVAL);
287				*off += (eptr - (s + *off));
288				sin->sin_port = htons(val);
289			} else
290				sin->sin_port = 0;
291		}
292		bzero(&sin->sin_zero, sizeof(sin->sin_zero));
293		sin->sin_len = sizeof(*sin);
294		break;
295	    }
296
297#if 0
298	case PF_INET6:	/* XXX implement this someday */
299#endif
300
301	default:
302		return (EINVAL);
303	}
304
305	/* Done */
306	*buflen = sa->sa_len;
307	return (0);
308}
309
310/* Convert a struct sockaddr from binary to ASCII */
311static int
312ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
313	const u_char *data, int *off, char *cbuf, int cbuflen)
314{
315	const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
316	int slen = 0;
317
318	/* Output socket address, either in special or generic format */
319	switch (sa->sa_family) {
320	case PF_LOCAL:
321	    {
322		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
323		const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
324		const int pathlen = sun->sun_len - pathoff;
325		char pathbuf[SOCK_MAXADDRLEN + 1];
326		char *pathtoken;
327
328		bcopy(sun->sun_path, pathbuf, pathlen);
329		if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL)
330			return (ENOMEM);
331		slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken);
332		free(pathtoken, M_NETGRAPH_KSOCKET);
333		if (slen >= cbuflen)
334			return (ERANGE);
335		*off += sun->sun_len;
336		return (0);
337	    }
338
339	case PF_INET:
340	    {
341		const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
342
343		slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
344		  ((const u_char *)&sin->sin_addr)[0],
345		  ((const u_char *)&sin->sin_addr)[1],
346		  ((const u_char *)&sin->sin_addr)[2],
347		  ((const u_char *)&sin->sin_addr)[3]);
348		if (sin->sin_port != 0) {
349			slen += snprintf(cbuf + strlen(cbuf),
350			    cbuflen - strlen(cbuf), ":%d",
351			    (u_int)ntohs(sin->sin_port));
352		}
353		if (slen >= cbuflen)
354			return (ERANGE);
355		*off += sizeof(*sin);
356		return(0);
357	    }
358
359#if 0
360	case PF_INET6:	/* XXX implement this someday */
361#endif
362
363	default:
364		return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
365		    (&ng_ksocket_generic_sockaddr_type,
366		    data, off, cbuf, cbuflen);
367	}
368}
369
370/* Parse type for struct sockaddr */
371static const struct ng_parse_type ng_ksocket_sockaddr_type = {
372	NULL,
373	NULL,
374	NULL,
375	&ng_ksocket_sockaddr_parse,
376	&ng_ksocket_sockaddr_unparse,
377	NULL		/* no such thing as a default struct sockaddr */
378};
379
380/************************************************************************
381		STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
382 ************************************************************************/
383
384/* Get length of the struct ng_ksocket_sockopt value field, which is the
385   just the excess of the message argument portion over the length of
386   the struct ng_ksocket_sockopt. */
387static int
388ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
389	const u_char *start, const u_char *buf)
390{
391	static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
392	const struct ng_ksocket_sockopt *sopt;
393	const struct ng_mesg *msg;
394
395	sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
396	msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
397	return msg->header.arglen - sizeof(*sopt);
398}
399
400/* Parse type for the option value part of a struct ng_ksocket_sockopt
401   XXX Eventually, we should handle the different socket options specially.
402   XXX This would avoid byte order problems, eg an integer value of 1 is
403   XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
404static const struct ng_parse_type ng_ksocket_sockoptval_type = {
405	&ng_parse_bytearray_type,
406	&ng_parse_sockoptval_getLength
407};
408
409/* Parse type for struct ng_ksocket_sockopt */
410static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[]
411	= NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
412static const struct ng_parse_type ng_ksocket_sockopt_type = {
413	&ng_parse_struct_type,
414	&ng_ksocket_sockopt_type_fields
415};
416
417/* Parse type for struct ng_ksocket_accept */
418static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[]
419	= NGM_KSOCKET_ACCEPT_INFO;
420static const struct ng_parse_type ng_ksocket_accept_type = {
421	&ng_parse_struct_type,
422	&ng_ksocket_accept_type_fields
423};
424
425/* List of commands and how to convert arguments to/from ASCII */
426static const struct ng_cmdlist ng_ksocket_cmds[] = {
427	{
428	  NGM_KSOCKET_COOKIE,
429	  NGM_KSOCKET_BIND,
430	  "bind",
431	  &ng_ksocket_sockaddr_type,
432	  NULL
433	},
434	{
435	  NGM_KSOCKET_COOKIE,
436	  NGM_KSOCKET_LISTEN,
437	  "listen",
438	  &ng_parse_int32_type,
439	  NULL
440	},
441	{
442	  NGM_KSOCKET_COOKIE,
443	  NGM_KSOCKET_ACCEPT,
444	  "accept",
445	  NULL,
446	  &ng_ksocket_accept_type
447	},
448	{
449	  NGM_KSOCKET_COOKIE,
450	  NGM_KSOCKET_CONNECT,
451	  "connect",
452	  &ng_ksocket_sockaddr_type,
453	  &ng_parse_int32_type
454	},
455	{
456	  NGM_KSOCKET_COOKIE,
457	  NGM_KSOCKET_GETNAME,
458	  "getname",
459	  NULL,
460	  &ng_ksocket_sockaddr_type
461	},
462	{
463	  NGM_KSOCKET_COOKIE,
464	  NGM_KSOCKET_GETPEERNAME,
465	  "getpeername",
466	  NULL,
467	  &ng_ksocket_sockaddr_type
468	},
469	{
470	  NGM_KSOCKET_COOKIE,
471	  NGM_KSOCKET_SETOPT,
472	  "setopt",
473	  &ng_ksocket_sockopt_type,
474	  NULL
475	},
476	{
477	  NGM_KSOCKET_COOKIE,
478	  NGM_KSOCKET_GETOPT,
479	  "getopt",
480	  &ng_ksocket_sockopt_type,
481	  &ng_ksocket_sockopt_type
482	},
483	{ 0 }
484};
485
486/* Node type descriptor */
487static struct ng_type ng_ksocket_typestruct = {
488	.version =	NG_ABI_VERSION,
489	.name =		NG_KSOCKET_NODE_TYPE,
490	.constructor =	ng_ksocket_constructor,
491	.rcvmsg =	ng_ksocket_rcvmsg,
492	.shutdown =	ng_ksocket_shutdown,
493	.newhook =	ng_ksocket_newhook,
494	.connect =	ng_ksocket_connect,
495	.rcvdata =	ng_ksocket_rcvdata,
496	.disconnect =	ng_ksocket_disconnect,
497	.cmdlist =	ng_ksocket_cmds,
498};
499NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
500
501#define ERROUT(x)	do { error = (x); goto done; } while (0)
502
503/************************************************************************
504			NETGRAPH NODE STUFF
505 ************************************************************************/
506
507/*
508 * Node type constructor
509 * The NODE part is assumed to be all set up.
510 * There is already a reference to the node for us.
511 */
512static int
513ng_ksocket_constructor(node_p node)
514{
515	priv_p priv;
516
517	/* Allocate private structure */
518	priv = malloc(sizeof(*priv), M_NETGRAPH_KSOCKET, M_NOWAIT | M_ZERO);
519	if (priv == NULL)
520		return (ENOMEM);
521
522	LIST_INIT(&priv->embryos);
523	/* cross link them */
524	priv->node = node;
525	NG_NODE_SET_PRIVATE(node, priv);
526
527	/* Done */
528	return (0);
529}
530
531/*
532 * Give our OK for a hook to be added. The hook name is of the
533 * form "<family>/<type>/<proto>" where the three components may
534 * be decimal numbers or else aliases from the above lists.
535 *
536 * Connecting a hook amounts to opening the socket.  Disconnecting
537 * the hook closes the socket and destroys the node as well.
538 */
539static int
540ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
541{
542	struct thread *td = curthread;	/* XXX broken */
543	const priv_p priv = NG_NODE_PRIVATE(node);
544	char *s1, *s2, name[NG_HOOKSIZ];
545	int family, type, protocol, error;
546
547	/* Check if we're already connected */
548	if (priv->hook != NULL)
549		return (EISCONN);
550
551	if (priv->flags & KSF_CLONED) {
552		if (priv->flags & KSF_EMBRYONIC) {
553			/* Remove ourselves from our parent's embryo list */
554			LIST_REMOVE(priv, siblings);
555			priv->flags &= ~KSF_EMBRYONIC;
556		}
557	} else {
558		/* Extract family, type, and protocol from hook name */
559		snprintf(name, sizeof(name), "%s", name0);
560		s1 = name;
561		if ((s2 = strchr(s1, '/')) == NULL)
562			return (EINVAL);
563		*s2++ = '\0';
564		family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
565		if (family == -1)
566			return (EINVAL);
567		s1 = s2;
568		if ((s2 = strchr(s1, '/')) == NULL)
569			return (EINVAL);
570		*s2++ = '\0';
571		type = ng_ksocket_parse(ng_ksocket_types, s1, 0);
572		if (type == -1)
573			return (EINVAL);
574		s1 = s2;
575		protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family);
576		if (protocol == -1)
577			return (EINVAL);
578
579		/* Create the socket */
580		error = socreate(family, &priv->so, type, protocol,
581		   td->td_ucred, td);
582		if (error != 0)
583			return (error);
584
585		/* XXX call soreserve() ? */
586
587	}
588
589	/* OK */
590	priv->hook = hook;
591
592	/*
593	 * In case of misconfigured routing a packet may reenter
594	 * ksocket node recursively. Decouple stack to avoid possible
595	 * panics about sleeping with locks held.
596	 */
597	NG_HOOK_FORCE_QUEUE(hook);
598
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	SOCKBUF_LOCK(&priv->so->so_rcv);
611	soupcall_set(priv->so, SO_RCV, ng_ksocket_incoming, node);
612	SOCKBUF_UNLOCK(&priv->so->so_rcv);
613	SOCKBUF_LOCK(&priv->so->so_snd);
614	soupcall_set(priv->so, SO_SND, ng_ksocket_incoming, node);
615	SOCKBUF_UNLOCK(&priv->so->so_snd);
616	SOCK_LOCK(priv->so);
617	priv->so->so_state |= SS_NBIO;
618	SOCK_UNLOCK(priv->so);
619	/*
620	 * --Original comment--
621	 * On a cloned socket we may have already received one or more
622	 * upcalls which we couldn't handle without a hook.  Handle
623	 * those now.
624	 * We cannot call the upcall function directly
625	 * from here, because until this function has returned our
626	 * hook isn't connected.
627	 *
628	 * ---meta comment for -current ---
629	 * XXX This is dubius.
630	 * Upcalls between the time that the hook was
631	 * first created and now (on another processesor) will
632	 * be earlier on the queue than the request to finalise the hook.
633	 * By the time the hook is finalised,
634	 * The queued upcalls will have happened and the code
635	 * will have discarded them because of a lack of a hook.
636	 * (socket not open).
637	 *
638	 * This is a bad byproduct of the complicated way in which hooks
639	 * are now created (3 daisy chained async events).
640	 *
641	 * Since we are a netgraph operation
642	 * We know that we hold a lock on this node. This forces the
643	 * request we make below to be queued rather than implemented
644	 * immediately which will cause the upcall function to be called a bit
645	 * later.
646	 * However, as we will run any waiting queued operations immediately
647	 * after doing this one, if we have not finalised the other end
648	 * of the hook, those queued operations will fail.
649	 */
650	if (priv->flags & KSF_CLONED) {
651		ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, M_NOWAIT);
652	}
653
654	return (0);
655}
656
657/*
658 * Receive a control message
659 */
660static int
661ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook)
662{
663	struct thread *td = curthread;	/* XXX broken */
664	const priv_p priv = NG_NODE_PRIVATE(node);
665	struct socket *const so = priv->so;
666	struct ng_mesg *resp = NULL;
667	int error = 0;
668	struct ng_mesg *msg;
669	ng_ID_t raddr;
670
671	NGI_GET_MSG(item, msg);
672	switch (msg->header.typecookie) {
673	case NGM_KSOCKET_COOKIE:
674		switch (msg->header.cmd) {
675		case NGM_KSOCKET_BIND:
676		    {
677			struct sockaddr *const sa
678			    = (struct sockaddr *)msg->data;
679
680			/* Sanity check */
681			if (msg->header.arglen < SADATA_OFFSET
682			    || msg->header.arglen < sa->sa_len)
683				ERROUT(EINVAL);
684			if (so == NULL)
685				ERROUT(ENXIO);
686
687			/* Bind */
688			error = sobind(so, sa, td);
689			break;
690		    }
691		case NGM_KSOCKET_LISTEN:
692		    {
693			/* Sanity check */
694			if (msg->header.arglen != sizeof(int32_t))
695				ERROUT(EINVAL);
696			if (so == NULL)
697				ERROUT(ENXIO);
698
699			/* Listen */
700			so->so_state |= SS_NBIO;
701			error = solisten(so, *((int32_t *)msg->data), td);
702			break;
703		    }
704
705		case NGM_KSOCKET_ACCEPT:
706		    {
707			/* Sanity check */
708			if (msg->header.arglen != 0)
709				ERROUT(EINVAL);
710			if (so == NULL)
711				ERROUT(ENXIO);
712
713			/* Make sure the socket is capable of accepting */
714			if (!(so->so_options & SO_ACCEPTCONN))
715				ERROUT(EINVAL);
716			if (priv->flags & KSF_ACCEPTING)
717				ERROUT(EALREADY);
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			error = ng_ksocket_accept(priv);
725			if (error != 0 && error != EWOULDBLOCK)
726				ERROUT(error);
727			priv->response_token = msg->header.token;
728			raddr = priv->response_addr = NGI_RETADDR(item);
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 = NGI_RETADDR(item);
755				priv->flags |= KSF_CONNECTING;
756				ERROUT(EINPROGRESS);
757			}
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;	/* 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	int error;
891	struct mbuf *m;
892#ifdef ALIGNED_POINTER
893	struct mbuf *n;
894#endif /* ALIGNED_POINTER */
895	struct sa_tag *stag;
896
897	/* Extract data */
898	NGI_GET_M(item, m);
899	NG_FREE_ITEM(item);
900#ifdef ALIGNED_POINTER
901	if (!ALIGNED_POINTER(mtod(m, caddr_t), uint32_t)) {
902		n = m_defrag(m, M_NOWAIT);
903		if (n == NULL) {
904			m_freem(m);
905			return (ENOBUFS);
906		}
907		m = n;
908	}
909#endif /* ALIGNED_POINTER */
910	/*
911	 * Look if socket address is stored in packet tags.
912	 * If sockaddr is ours, or provided by a third party (zero id),
913	 * then we accept it.
914	 */
915	if (((stag = (struct sa_tag *)m_tag_locate(m, NGM_KSOCKET_COOKIE,
916	    NG_KSOCKET_TAG_SOCKADDR, NULL)) != NULL) &&
917	    (stag->id == NG_NODE_ID(node) || stag->id == 0))
918		sa = &stag->sa;
919
920	/* Reset specific mbuf flags to prevent addressing problems. */
921	m->m_flags &= ~(M_BCAST|M_MCAST);
922
923	/* Send packet */
924	error = sosend(so, sa, 0, m, 0, 0, td);
925
926	return (error);
927}
928
929/*
930 * Destroy node
931 */
932static int
933ng_ksocket_shutdown(node_p node)
934{
935	const priv_p priv = NG_NODE_PRIVATE(node);
936	priv_p embryo;
937
938	/* Close our socket (if any) */
939	if (priv->so != NULL) {
940		SOCKBUF_LOCK(&priv->so->so_rcv);
941		soupcall_clear(priv->so, SO_RCV);
942		SOCKBUF_UNLOCK(&priv->so->so_rcv);
943		SOCKBUF_LOCK(&priv->so->so_snd);
944		soupcall_clear(priv->so, SO_SND);
945		SOCKBUF_UNLOCK(&priv->so->so_snd);
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 not "just call" a netgraph node function from an external
989 * asynchronous event. This is because in doing so you are ignoring the
990 * locking on the netgraph nodes. Instead call your function via ng_send_fn().
991 * This will call the function you chose, but will first do all the
992 * locking rigmarole. Your function MAY only be called at some distant future
993 * time (several millisecs away) so don't give it any arguments
994 * that may be revoked soon (e.g. on your stack).
995 *
996 * To decouple stack, we use queue version of ng_send_fn().
997 */
998
999static int
1000ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
1001{
1002	const node_p node = arg;
1003	const priv_p priv = NG_NODE_PRIVATE(node);
1004	int wait = ((waitflag & M_WAITOK) ? NG_WAITOK : 0) | NG_QUEUE;
1005
1006	/*
1007	 * Even if node is not locked, as soon as we are called, we assume
1008	 * it exist and it's private area is valid. With some care we can
1009	 * access it. Mark node that incoming event for it was sent to
1010	 * avoid unneded queue trashing.
1011	 */
1012	if (atomic_cmpset_int(&priv->fn_sent, 0, 1) &&
1013	    ng_send_fn1(node, NULL, &ng_ksocket_incoming2, so, 0, wait)) {
1014		atomic_store_rel_int(&priv->fn_sent, 0);
1015	}
1016	return (SU_OK);
1017}
1018
1019
1020/*
1021 * When incoming data is appended to the socket, we get notified here.
1022 * This is also called whenever a significant event occurs for the socket.
1023 * Our original caller may have queued this even some time ago and
1024 * we cannot trust that he even still exists. The node however is being
1025 * held with a reference by the queueing code and guarantied to be valid.
1026 */
1027static void
1028ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int arg2)
1029{
1030	struct socket *so = arg1;
1031	const priv_p priv = NG_NODE_PRIVATE(node);
1032	struct ng_mesg *response;
1033	int error;
1034
1035	KASSERT(so == priv->so, ("%s: wrong socket", __func__));
1036
1037	/* Allow next incoming event to be queued. */
1038	atomic_store_rel_int(&priv->fn_sent, 0);
1039
1040	/* Check whether a pending connect operation has completed */
1041	if (priv->flags & KSF_CONNECTING) {
1042		if ((error = so->so_error) != 0) {
1043			so->so_error = 0;
1044			so->so_state &= ~SS_ISCONNECTING;
1045		}
1046		if (!(so->so_state & SS_ISCONNECTING)) {
1047			NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
1048			    NGM_KSOCKET_CONNECT, sizeof(int32_t), M_NOWAIT);
1049			if (response != NULL) {
1050				response->header.flags |= NGF_RESP;
1051				response->header.token = priv->response_token;
1052				*(int32_t *)response->data = error;
1053				/*
1054				 * send an async "response" message
1055				 * to the node that set us up
1056				 * (if it still exists)
1057				 */
1058				NG_SEND_MSG_ID(error, node,
1059				    response, priv->response_addr, 0);
1060			}
1061			priv->flags &= ~KSF_CONNECTING;
1062		}
1063	}
1064
1065	/* Check whether a pending accept operation has completed */
1066	if (priv->flags & KSF_ACCEPTING)
1067		(void )ng_ksocket_accept(priv);
1068
1069	/*
1070	 * If we don't have a hook, we must handle data events later.  When
1071	 * the hook gets created and is connected, this upcall function
1072	 * will be called again.
1073	 */
1074	if (priv->hook == NULL)
1075		return;
1076
1077	/* Read and forward available mbufs. */
1078	while (1) {
1079		struct uio uio;
1080		struct sockaddr *sa;
1081		struct mbuf *m;
1082		int flags;
1083
1084		/* Try to get next packet from socket. */
1085		uio.uio_td = NULL;
1086		uio.uio_resid = IP_MAXPACKET;
1087		flags = MSG_DONTWAIT;
1088		sa = NULL;
1089		if ((error = soreceive(so, (so->so_state & SS_ISCONNECTED) ?
1090		    NULL : &sa, &uio, &m, NULL, &flags)) != 0)
1091			break;
1092
1093		/* See if we got anything. */
1094		if (flags & MSG_TRUNC) {
1095			m_freem(m);
1096			m = NULL;
1097		}
1098		if (m == NULL) {
1099			if (sa != NULL)
1100				free(sa, M_SONAME);
1101			break;
1102		}
1103
1104		KASSERT(m->m_nextpkt == NULL, ("%s: nextpkt", __func__));
1105
1106		/*
1107		 * Stream sockets do not have packet boundaries, so
1108		 * we have to allocate a header mbuf and attach the
1109		 * stream of data to it.
1110		 */
1111		if (so->so_type == SOCK_STREAM) {
1112			struct mbuf *mh;
1113
1114			mh = m_gethdr(M_NOWAIT, MT_DATA);
1115			if (mh == NULL) {
1116				m_freem(m);
1117				if (sa != NULL)
1118					free(sa, M_SONAME);
1119				break;
1120			}
1121
1122			mh->m_next = m;
1123			for (; m; m = m->m_next)
1124				mh->m_pkthdr.len += m->m_len;
1125			m = mh;
1126		}
1127
1128		/* Put peer's socket address (if any) into a tag */
1129		if (sa != NULL) {
1130			struct sa_tag	*stag;
1131
1132			stag = (struct sa_tag *)m_tag_alloc(NGM_KSOCKET_COOKIE,
1133			    NG_KSOCKET_TAG_SOCKADDR, sizeof(ng_ID_t) +
1134			    sa->sa_len, M_NOWAIT);
1135			if (stag == NULL) {
1136				free(sa, M_SONAME);
1137				goto sendit;
1138			}
1139			bcopy(sa, &stag->sa, sa->sa_len);
1140			free(sa, M_SONAME);
1141			stag->id = NG_NODE_ID(node);
1142			m_tag_prepend(m, &stag->tag);
1143		}
1144
1145sendit:		/* Forward data with optional peer sockaddr as packet tag */
1146		NG_SEND_DATA_ONLY(error, priv->hook, m);
1147	}
1148
1149	/*
1150	 * If the peer has closed the connection, forward a 0-length mbuf
1151	 * to indicate end-of-file.
1152	 */
1153	if (so->so_rcv.sb_state & SBS_CANTRCVMORE &&
1154	    !(priv->flags & KSF_EOFSEEN)) {
1155		struct mbuf *m;
1156
1157		m = m_gethdr(M_NOWAIT, MT_DATA);
1158		if (m != NULL)
1159			NG_SEND_DATA_ONLY(error, priv->hook, m);
1160		priv->flags |= KSF_EOFSEEN;
1161	}
1162}
1163
1164static int
1165ng_ksocket_accept(priv_p priv)
1166{
1167	struct socket *const head = priv->so;
1168	struct socket *so;
1169	struct sockaddr *sa = NULL;
1170	struct ng_mesg *resp;
1171	struct ng_ksocket_accept *resp_data;
1172	node_p node;
1173	priv_p priv2;
1174	int len;
1175	int error;
1176
1177	SOLISTEN_LOCK(head);
1178	error = solisten_dequeue(head, &so, SOCK_NONBLOCK);
1179	if (error == EWOULDBLOCK) {
1180		priv->flags |= KSF_ACCEPTING;
1181		return (error);
1182	}
1183	priv->flags &= ~KSF_ACCEPTING;
1184	if (error)
1185		return (error);
1186
1187	if ((error = soaccept(so, &sa)) != 0)
1188		return (error);
1189
1190	len = OFFSETOF(struct ng_ksocket_accept, addr);
1191	if (sa != NULL)
1192		len += sa->sa_len;
1193
1194	NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1195	    M_NOWAIT);
1196	if (resp == NULL) {
1197		soclose(so);
1198		goto out;
1199	}
1200	resp->header.flags |= NGF_RESP;
1201	resp->header.token = priv->response_token;
1202
1203	/* Clone a ksocket node to wrap the new socket */
1204	error = ng_make_node_common(&ng_ksocket_typestruct, &node);
1205	if (error) {
1206		free(resp, M_NETGRAPH);
1207		soclose(so);
1208		goto out;
1209	}
1210
1211	if (ng_ksocket_constructor(node) != 0) {
1212		NG_NODE_UNREF(node);
1213		free(resp, M_NETGRAPH);
1214		soclose(so);
1215		goto out;
1216	}
1217
1218	priv2 = NG_NODE_PRIVATE(node);
1219	priv2->so = so;
1220	priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1221
1222	/*
1223	 * Insert the cloned node into a list of embryonic children
1224	 * on the parent node.  When a hook is created on the cloned
1225	 * node it will be removed from this list.  When the parent
1226	 * is destroyed it will destroy any embryonic children it has.
1227	 */
1228	LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1229
1230	SOCKBUF_LOCK(&so->so_rcv);
1231	soupcall_set(so, SO_RCV, ng_ksocket_incoming, node);
1232	SOCKBUF_UNLOCK(&so->so_rcv);
1233	SOCKBUF_LOCK(&so->so_snd);
1234	soupcall_set(so, SO_SND, ng_ksocket_incoming, node);
1235	SOCKBUF_UNLOCK(&so->so_snd);
1236
1237	/* Fill in the response data and send it or return it to the caller */
1238	resp_data = (struct ng_ksocket_accept *)resp->data;
1239	resp_data->nodeid = NG_NODE_ID(node);
1240	if (sa != NULL)
1241		bcopy(sa, &resp_data->addr, sa->sa_len);
1242	NG_SEND_MSG_ID(error, node, resp, priv->response_addr, 0);
1243
1244out:
1245	if (sa != NULL)
1246		free(sa, M_SONAME);
1247
1248	return (0);
1249}
1250
1251/*
1252 * Parse out either an integer value or an alias.
1253 */
1254static int
1255ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1256	const char *s, int family)
1257{
1258	int k, val;
1259	char *eptr;
1260
1261	/* Try aliases */
1262	for (k = 0; aliases[k].name != NULL; k++) {
1263		if (strcmp(s, aliases[k].name) == 0
1264		    && aliases[k].family == family)
1265			return aliases[k].value;
1266	}
1267
1268	/* Try parsing as a number */
1269	val = (int)strtoul(s, &eptr, 10);
1270	if (val < 0 || *eptr != '\0')
1271		return (-1);
1272	return (val);
1273}
1274
1275