xref: /freebsd/sys/netgraph/ng_ksocket.c (revision 1a3d1be4)
1cb3c7a5dSArchie Cobbs /*
2cb3c7a5dSArchie Cobbs  * ng_ksocket.c
3c398230bSWarner Losh  */
4c398230bSWarner Losh 
5c398230bSWarner Losh /*-
6cb3c7a5dSArchie Cobbs  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7cb3c7a5dSArchie Cobbs  * All rights reserved.
8cb3c7a5dSArchie Cobbs  *
9cb3c7a5dSArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
10cb3c7a5dSArchie Cobbs  * redistribution of this software, in source or object code forms, with or
11cb3c7a5dSArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
12cb3c7a5dSArchie Cobbs  * provided, however, that:
13cb3c7a5dSArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
14cb3c7a5dSArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
15cb3c7a5dSArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
16cb3c7a5dSArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
17cb3c7a5dSArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18cb3c7a5dSArchie Cobbs  *    such appears in the above copyright notice or in the software.
19cb3c7a5dSArchie Cobbs  *
20cb3c7a5dSArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21cb3c7a5dSArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22cb3c7a5dSArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23cb3c7a5dSArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24cb3c7a5dSArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25cb3c7a5dSArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26cb3c7a5dSArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27cb3c7a5dSArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28cb3c7a5dSArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29cb3c7a5dSArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30cb3c7a5dSArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31cb3c7a5dSArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32cb3c7a5dSArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33cb3c7a5dSArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34cb3c7a5dSArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35cb3c7a5dSArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36cb3c7a5dSArchie Cobbs  * OF SUCH DAMAGE.
37cb3c7a5dSArchie Cobbs  *
38cc3bbd68SJulian Elischer  * Author: Archie Cobbs <archie@freebsd.org>
39cb3c7a5dSArchie Cobbs  * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $
40cb3c7a5dSArchie Cobbs  */
41cb3c7a5dSArchie Cobbs 
42cb3c7a5dSArchie Cobbs /*
43cb3c7a5dSArchie Cobbs  * Kernel socket node type.  This node type is basically a kernel-mode
44cb3c7a5dSArchie Cobbs  * version of a socket... kindof like the reverse of the socket node type.
45cb3c7a5dSArchie Cobbs  */
46cb3c7a5dSArchie Cobbs 
47cb3c7a5dSArchie Cobbs #include <sys/param.h>
48cb3c7a5dSArchie Cobbs #include <sys/systm.h>
49cb3c7a5dSArchie Cobbs #include <sys/kernel.h>
50cb3c7a5dSArchie Cobbs #include <sys/mbuf.h>
51cb3c7a5dSArchie Cobbs #include <sys/proc.h>
52cb3c7a5dSArchie Cobbs #include <sys/malloc.h>
53f8307e12SArchie Cobbs #include <sys/ctype.h>
54cb3c7a5dSArchie Cobbs #include <sys/protosw.h>
55cb3c7a5dSArchie Cobbs #include <sys/errno.h>
56cb3c7a5dSArchie Cobbs #include <sys/socket.h>
57cb3c7a5dSArchie Cobbs #include <sys/socketvar.h>
58cb3c7a5dSArchie Cobbs #include <sys/uio.h>
59f8307e12SArchie Cobbs #include <sys/un.h>
60cb3c7a5dSArchie Cobbs 
61cb3c7a5dSArchie Cobbs #include <netgraph/ng_message.h>
62cb3c7a5dSArchie Cobbs #include <netgraph/netgraph.h>
63f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
64cb3c7a5dSArchie Cobbs #include <netgraph/ng_ksocket.h>
65cb3c7a5dSArchie Cobbs 
66cb3c7a5dSArchie Cobbs #include <netinet/in.h>
679165bf62SGleb Smirnoff #include <netinet/ip.h>
68cb3c7a5dSArchie Cobbs 
699c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
70d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock",
71d745c852SEd Schouten     "netgraph ksock node");
729c8c302fSJulian Elischer #else
739c8c302fSJulian Elischer #define M_NETGRAPH_KSOCKET M_NETGRAPH
749c8c302fSJulian Elischer #endif
759c8c302fSJulian Elischer 
76f8307e12SArchie Cobbs #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
77f8307e12SArchie Cobbs #define SADATA_OFFSET	(OFFSETOF(struct sockaddr, sa_data))
78f8307e12SArchie Cobbs 
79cb3c7a5dSArchie Cobbs /* Node private data */
8019bff684SArchie Cobbs struct ng_ksocket_private {
81f97e0a07SJulian Elischer 	node_p		node;
82cb3c7a5dSArchie Cobbs 	hook_p		hook;
83cb3c7a5dSArchie Cobbs 	struct socket	*so;
846e7ed930SAlexander Motin 	int		fn_sent;	/* FN call on incoming event was sent */
85f97e0a07SJulian Elischer 	LIST_HEAD(, ng_ksocket_private)	embryos;
86f97e0a07SJulian Elischer 	LIST_ENTRY(ng_ksocket_private)	siblings;
87f97e0a07SJulian Elischer 	u_int32_t	flags;
88f97e0a07SJulian Elischer 	u_int32_t	response_token;
89f97e0a07SJulian Elischer 	ng_ID_t		response_addr;
90cb3c7a5dSArchie Cobbs };
9119bff684SArchie Cobbs typedef struct ng_ksocket_private *priv_p;
92cb3c7a5dSArchie Cobbs 
93f97e0a07SJulian Elischer /* Flags for priv_p */
94f97e0a07SJulian Elischer #define	KSF_CONNECTING	0x00000001	/* Waiting for connection complete */
95f97e0a07SJulian Elischer #define	KSF_ACCEPTING	0x00000002	/* Waiting for accept complete */
96f97e0a07SJulian Elischer #define	KSF_EOFSEEN	0x00000004	/* Have sent 0-length EOF mbuf */
97f97e0a07SJulian Elischer #define	KSF_CLONED	0x00000008	/* Cloned from an accepting socket */
98f97e0a07SJulian Elischer #define	KSF_EMBRYONIC	0x00000010	/* Cloned node with no hooks yet */
99f97e0a07SJulian Elischer 
100cb3c7a5dSArchie Cobbs /* Netgraph node methods */
101cb3c7a5dSArchie Cobbs static ng_constructor_t	ng_ksocket_constructor;
102cb3c7a5dSArchie Cobbs static ng_rcvmsg_t	ng_ksocket_rcvmsg;
103069154d5SJulian Elischer static ng_shutdown_t	ng_ksocket_shutdown;
104cb3c7a5dSArchie Cobbs static ng_newhook_t	ng_ksocket_newhook;
105cb3c7a5dSArchie Cobbs static ng_rcvdata_t	ng_ksocket_rcvdata;
106f97e0a07SJulian Elischer static ng_connect_t	ng_ksocket_connect;
107cb3c7a5dSArchie Cobbs static ng_disconnect_t	ng_ksocket_disconnect;
108cb3c7a5dSArchie Cobbs 
109cb3c7a5dSArchie Cobbs /* Alias structure */
110cb3c7a5dSArchie Cobbs struct ng_ksocket_alias {
111cb3c7a5dSArchie Cobbs 	const char	*name;
112cb3c7a5dSArchie Cobbs 	const int	value;
113cb3c7a5dSArchie Cobbs 	const int	family;
114cb3c7a5dSArchie Cobbs };
115cb3c7a5dSArchie Cobbs 
116cb3c7a5dSArchie Cobbs /* Protocol family aliases */
117cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_families[] = {
118cb3c7a5dSArchie Cobbs 	{ "local",	PF_LOCAL	},
119cb3c7a5dSArchie Cobbs 	{ "inet",	PF_INET		},
120cb3c7a5dSArchie Cobbs 	{ "inet6",	PF_INET6	},
121cb3c7a5dSArchie Cobbs 	{ "atm",	PF_ATM		},
1228624f434SGleb Smirnoff 	{ "divert",	PF_DIVERT	},
123cb3c7a5dSArchie Cobbs 	{ NULL,		-1		},
124cb3c7a5dSArchie Cobbs };
125cb3c7a5dSArchie Cobbs 
126cb3c7a5dSArchie Cobbs /* Socket type aliases */
127cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_types[] = {
128cb3c7a5dSArchie Cobbs 	{ "stream",	SOCK_STREAM	},
129cb3c7a5dSArchie Cobbs 	{ "dgram",	SOCK_DGRAM	},
130cb3c7a5dSArchie Cobbs 	{ "raw",	SOCK_RAW	},
131cb3c7a5dSArchie Cobbs 	{ "rdm",	SOCK_RDM	},
132cb3c7a5dSArchie Cobbs 	{ "seqpacket",	SOCK_SEQPACKET	},
133cb3c7a5dSArchie Cobbs 	{ NULL,		-1		},
134cb3c7a5dSArchie Cobbs };
135cb3c7a5dSArchie Cobbs 
136cb3c7a5dSArchie Cobbs /* Protocol aliases */
137cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_protos[] = {
138cb3c7a5dSArchie Cobbs 	{ "ip",		IPPROTO_IP,		PF_INET		},
139129bc895SArchie Cobbs 	{ "raw",	IPPROTO_RAW,		PF_INET		},
140cb3c7a5dSArchie Cobbs 	{ "icmp",	IPPROTO_ICMP,		PF_INET		},
141cb3c7a5dSArchie Cobbs 	{ "igmp",	IPPROTO_IGMP,		PF_INET		},
142cb3c7a5dSArchie Cobbs 	{ "tcp",	IPPROTO_TCP,		PF_INET		},
143cb3c7a5dSArchie Cobbs 	{ "udp",	IPPROTO_UDP,		PF_INET		},
144cb3c7a5dSArchie Cobbs 	{ "gre",	IPPROTO_GRE,		PF_INET		},
145cb3c7a5dSArchie Cobbs 	{ "esp",	IPPROTO_ESP,		PF_INET		},
146cb3c7a5dSArchie Cobbs 	{ "ah",		IPPROTO_AH,		PF_INET		},
147cb3c7a5dSArchie Cobbs 	{ "swipe",	IPPROTO_SWIPE,		PF_INET		},
148cb3c7a5dSArchie Cobbs 	{ "encap",	IPPROTO_ENCAP,		PF_INET		},
14933583c6fSJeffrey Hsu 	{ "pim",	IPPROTO_PIM,		PF_INET		},
150cb3c7a5dSArchie Cobbs 	{ NULL,		-1					},
151cb3c7a5dSArchie Cobbs };
152cb3c7a5dSArchie Cobbs 
153f8307e12SArchie Cobbs /* Helper functions */
154779f106aSGleb Smirnoff static int	ng_ksocket_accept(priv_p);
15543f7e216SGleb Smirnoff static int	ng_ksocket_listen_upcall(struct socket *so, void *arg,
15643f7e216SGleb Smirnoff     int waitflag);
15743f7e216SGleb Smirnoff static void	ng_ksocket_listen_upcall2(node_p node, hook_p hook,
15843f7e216SGleb Smirnoff     void *arg1, int arg2);
15974fb0ba7SJohn Baldwin static int	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
160f8307e12SArchie Cobbs static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
161f8307e12SArchie Cobbs 			const char *s, int family);
162f97e0a07SJulian Elischer static void	ng_ksocket_incoming2(node_p node, hook_p hook,
1636e7ed930SAlexander Motin 			void *arg1, int arg2);
164f8307e12SArchie Cobbs 
165f8307e12SArchie Cobbs /************************************************************************
166f8307e12SArchie Cobbs 			STRUCT SOCKADDR PARSE TYPE
167f8307e12SArchie Cobbs  ************************************************************************/
168f8307e12SArchie Cobbs 
169f8307e12SArchie Cobbs /* Get the length of the data portion of a generic struct sockaddr */
170f8307e12SArchie Cobbs static int
ng_parse_generic_sockdata_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)171f8307e12SArchie Cobbs ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
172f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
173f8307e12SArchie Cobbs {
174f8307e12SArchie Cobbs 	const struct sockaddr *sa;
175f8307e12SArchie Cobbs 
176f8307e12SArchie Cobbs 	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
1771baeddb8SArchie Cobbs 	return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
178f8307e12SArchie Cobbs }
179f8307e12SArchie Cobbs 
180f8307e12SArchie Cobbs /* Type for the variable length data portion of a generic struct sockaddr */
181f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
182f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
183f8307e12SArchie Cobbs 	&ng_parse_generic_sockdata_getLength
184f8307e12SArchie Cobbs };
185f8307e12SArchie Cobbs 
186f8307e12SArchie Cobbs /* Type for a generic struct sockaddr */
187f0184ff8SArchie Cobbs static const struct ng_parse_struct_field
188f0184ff8SArchie Cobbs     ng_parse_generic_sockaddr_type_fields[] = {
18957b57be3SArchie Cobbs 	  { "len",	&ng_parse_uint8_type			},
19057b57be3SArchie Cobbs 	  { "family",	&ng_parse_uint8_type			},
191f8307e12SArchie Cobbs 	  { "data",	&ng_ksocket_generic_sockdata_type	},
192f8307e12SArchie Cobbs 	  { NULL }
193f8307e12SArchie Cobbs };
194f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
195f8307e12SArchie Cobbs 	&ng_parse_struct_type,
196f0184ff8SArchie Cobbs 	&ng_parse_generic_sockaddr_type_fields
197f8307e12SArchie Cobbs };
198f8307e12SArchie Cobbs 
199f8307e12SArchie Cobbs /* Convert a struct sockaddr from ASCII to binary.  If its a protocol
200f8307e12SArchie Cobbs    family that we specially handle, do that, otherwise defer to the
201f8307e12SArchie Cobbs    generic parse type ng_ksocket_generic_sockaddr_type. */
202f8307e12SArchie Cobbs static int
ng_ksocket_sockaddr_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)203f8307e12SArchie Cobbs ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
204f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
205f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
206f8307e12SArchie Cobbs {
207f8307e12SArchie Cobbs 	struct sockaddr *const sa = (struct sockaddr *)buf;
208f8307e12SArchie Cobbs 	enum ng_parse_token tok;
209f8307e12SArchie Cobbs 	char fambuf[32];
210f8307e12SArchie Cobbs 	int family, len;
211f8307e12SArchie Cobbs 	char *t;
212f8307e12SArchie Cobbs 
213f8307e12SArchie Cobbs 	/* If next token is a left curly brace, use generic parse type */
214f8307e12SArchie Cobbs 	if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
215f8307e12SArchie Cobbs 		return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
216f8307e12SArchie Cobbs 		    (&ng_ksocket_generic_sockaddr_type,
217f8307e12SArchie Cobbs 		    s, off, start, buf, buflen);
218f8307e12SArchie Cobbs 	}
219f8307e12SArchie Cobbs 
220f8307e12SArchie Cobbs 	/* Get socket address family followed by a slash */
221f8307e12SArchie Cobbs 	while (isspace(s[*off]))
222f8307e12SArchie Cobbs 		(*off)++;
223dc15eac0SEd Schouten 	if ((t = strchr(s + *off, '/')) == NULL)
224f8307e12SArchie Cobbs 		return (EINVAL);
225f8307e12SArchie Cobbs 	if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
226f8307e12SArchie Cobbs 		return (EINVAL);
227f8307e12SArchie Cobbs 	strncpy(fambuf, s + *off, len);
228f8307e12SArchie Cobbs 	fambuf[len] = '\0';
229f8307e12SArchie Cobbs 	*off += len + 1;
230f8307e12SArchie Cobbs 	if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
231f8307e12SArchie Cobbs 		return (EINVAL);
232f8307e12SArchie Cobbs 
233f8307e12SArchie Cobbs 	/* Set family */
234f8307e12SArchie Cobbs 	if (*buflen < SADATA_OFFSET)
235f8307e12SArchie Cobbs 		return (ERANGE);
236f8307e12SArchie Cobbs 	sa->sa_family = family;
237f8307e12SArchie Cobbs 
238f8307e12SArchie Cobbs 	/* Set family-specific data and length */
239f8307e12SArchie Cobbs 	switch (sa->sa_family) {
240f8307e12SArchie Cobbs 	case PF_LOCAL:		/* Get pathname */
241f8307e12SArchie Cobbs 	    {
242f8307e12SArchie Cobbs 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
243f8307e12SArchie Cobbs 		struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
244f8307e12SArchie Cobbs 		int toklen, pathlen;
245f8307e12SArchie Cobbs 		char *path;
246f8307e12SArchie Cobbs 
24727121ab1SBrian Somers 		if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL)
248f8307e12SArchie Cobbs 			return (EINVAL);
249f8307e12SArchie Cobbs 		pathlen = strlen(path);
250f8307e12SArchie Cobbs 		if (pathlen > SOCK_MAXADDRLEN) {
2511ede983cSDag-Erling Smørgrav 			free(path, M_NETGRAPH_KSOCKET);
252f8307e12SArchie Cobbs 			return (E2BIG);
253f8307e12SArchie Cobbs 		}
254f8307e12SArchie Cobbs 		if (*buflen < pathoff + pathlen) {
2551ede983cSDag-Erling Smørgrav 			free(path, M_NETGRAPH_KSOCKET);
256f8307e12SArchie Cobbs 			return (ERANGE);
257f8307e12SArchie Cobbs 		}
258f8307e12SArchie Cobbs 		*off += toklen;
259f8307e12SArchie Cobbs 		bcopy(path, sun->sun_path, pathlen);
260f8307e12SArchie Cobbs 		sun->sun_len = pathoff + pathlen;
2611ede983cSDag-Erling Smørgrav 		free(path, M_NETGRAPH_KSOCKET);
262f8307e12SArchie Cobbs 		break;
263f8307e12SArchie Cobbs 	    }
264f8307e12SArchie Cobbs 
265f8307e12SArchie Cobbs 	case PF_INET:		/* Get an IP address with optional port */
266f8307e12SArchie Cobbs 	    {
267f8307e12SArchie Cobbs 		struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
268f8307e12SArchie Cobbs 		int i;
269f8307e12SArchie Cobbs 
270f8307e12SArchie Cobbs 		/* Parse this: <ipaddress>[:port] */
271f8307e12SArchie Cobbs 		for (i = 0; i < 4; i++) {
272f8307e12SArchie Cobbs 			u_long val;
273f8307e12SArchie Cobbs 			char *eptr;
274f8307e12SArchie Cobbs 
275f8307e12SArchie Cobbs 			val = strtoul(s + *off, &eptr, 10);
276f8307e12SArchie Cobbs 			if (val > 0xff || eptr == s + *off)
277f8307e12SArchie Cobbs 				return (EINVAL);
278f8307e12SArchie Cobbs 			*off += (eptr - (s + *off));
279f8307e12SArchie Cobbs 			((u_char *)&sin->sin_addr)[i] = (u_char)val;
280f8307e12SArchie Cobbs 			if (i < 3) {
281f8307e12SArchie Cobbs 				if (s[*off] != '.')
282f8307e12SArchie Cobbs 					return (EINVAL);
283f8307e12SArchie Cobbs 				(*off)++;
284f8307e12SArchie Cobbs 			} else if (s[*off] == ':') {
285f8307e12SArchie Cobbs 				(*off)++;
286f8307e12SArchie Cobbs 				val = strtoul(s + *off, &eptr, 10);
287f8307e12SArchie Cobbs 				if (val > 0xffff || eptr == s + *off)
288f8307e12SArchie Cobbs 					return (EINVAL);
289f8307e12SArchie Cobbs 				*off += (eptr - (s + *off));
290f8307e12SArchie Cobbs 				sin->sin_port = htons(val);
291f8307e12SArchie Cobbs 			} else
292f8307e12SArchie Cobbs 				sin->sin_port = 0;
293f8307e12SArchie Cobbs 		}
294f8307e12SArchie Cobbs 		bzero(&sin->sin_zero, sizeof(sin->sin_zero));
295f8307e12SArchie Cobbs 		sin->sin_len = sizeof(*sin);
296f8307e12SArchie Cobbs 		break;
297f8307e12SArchie Cobbs 	    }
298f8307e12SArchie Cobbs 
299f8307e12SArchie Cobbs #if 0
30045c203fcSGleb Smirnoff 	case PF_INET6:	/* XXX implement this someday */
301f8307e12SArchie Cobbs #endif
302f8307e12SArchie Cobbs 
303f8307e12SArchie Cobbs 	default:
304f8307e12SArchie Cobbs 		return (EINVAL);
305f8307e12SArchie Cobbs 	}
306f8307e12SArchie Cobbs 
307f8307e12SArchie Cobbs 	/* Done */
308f8307e12SArchie Cobbs 	*buflen = sa->sa_len;
309f8307e12SArchie Cobbs 	return (0);
310f8307e12SArchie Cobbs }
311f8307e12SArchie Cobbs 
312f8307e12SArchie Cobbs /* Convert a struct sockaddr from binary to ASCII */
313f8307e12SArchie Cobbs static int
ng_ksocket_sockaddr_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)314f8307e12SArchie Cobbs ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
315f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
316f8307e12SArchie Cobbs {
317f8307e12SArchie Cobbs 	const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
318f8307e12SArchie Cobbs 	int slen = 0;
319f8307e12SArchie Cobbs 
320f8307e12SArchie Cobbs 	/* Output socket address, either in special or generic format */
321f8307e12SArchie Cobbs 	switch (sa->sa_family) {
322f8307e12SArchie Cobbs 	case PF_LOCAL:
323f8307e12SArchie Cobbs 	    {
324f8307e12SArchie Cobbs 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
325f8307e12SArchie Cobbs 		const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
326f8307e12SArchie Cobbs 		const int pathlen = sun->sun_len - pathoff;
327f8307e12SArchie Cobbs 		char pathbuf[SOCK_MAXADDRLEN + 1];
328f8307e12SArchie Cobbs 		char *pathtoken;
329f8307e12SArchie Cobbs 
330f8307e12SArchie Cobbs 		bcopy(sun->sun_path, pathbuf, pathlen);
33127121ab1SBrian Somers 		if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL)
332f8307e12SArchie Cobbs 			return (ENOMEM);
333f8307e12SArchie Cobbs 		slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken);
3341ede983cSDag-Erling Smørgrav 		free(pathtoken, M_NETGRAPH_KSOCKET);
335f8307e12SArchie Cobbs 		if (slen >= cbuflen)
336f8307e12SArchie Cobbs 			return (ERANGE);
337f8307e12SArchie Cobbs 		*off += sun->sun_len;
338f8307e12SArchie Cobbs 		return (0);
339f8307e12SArchie Cobbs 	    }
340f8307e12SArchie Cobbs 
341f8307e12SArchie Cobbs 	case PF_INET:
342f8307e12SArchie Cobbs 	    {
343f8307e12SArchie Cobbs 		const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
344f8307e12SArchie Cobbs 
345f8307e12SArchie Cobbs 		slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
346f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[0],
347f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[1],
348f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[2],
349f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[3]);
350f8307e12SArchie Cobbs 		if (sin->sin_port != 0) {
351f8307e12SArchie Cobbs 			slen += snprintf(cbuf + strlen(cbuf),
352f8307e12SArchie Cobbs 			    cbuflen - strlen(cbuf), ":%d",
353f8307e12SArchie Cobbs 			    (u_int)ntohs(sin->sin_port));
354f8307e12SArchie Cobbs 		}
355f8307e12SArchie Cobbs 		if (slen >= cbuflen)
356f8307e12SArchie Cobbs 			return (ERANGE);
357f8307e12SArchie Cobbs 		*off += sizeof(*sin);
358f8307e12SArchie Cobbs 		return(0);
359f8307e12SArchie Cobbs 	    }
360f8307e12SArchie Cobbs 
361f8307e12SArchie Cobbs #if 0
36245c203fcSGleb Smirnoff 	case PF_INET6:	/* XXX implement this someday */
363f8307e12SArchie Cobbs #endif
364f8307e12SArchie Cobbs 
365f8307e12SArchie Cobbs 	default:
366f8307e12SArchie Cobbs 		return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
367f8307e12SArchie Cobbs 		    (&ng_ksocket_generic_sockaddr_type,
368f8307e12SArchie Cobbs 		    data, off, cbuf, cbuflen);
369f8307e12SArchie Cobbs 	}
370f8307e12SArchie Cobbs }
371f8307e12SArchie Cobbs 
372f8307e12SArchie Cobbs /* Parse type for struct sockaddr */
373f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockaddr_type = {
374f8307e12SArchie Cobbs 	NULL,
375f8307e12SArchie Cobbs 	NULL,
376f8307e12SArchie Cobbs 	NULL,
377f8307e12SArchie Cobbs 	&ng_ksocket_sockaddr_parse,
378f8307e12SArchie Cobbs 	&ng_ksocket_sockaddr_unparse,
379f8307e12SArchie Cobbs 	NULL		/* no such thing as a default struct sockaddr */
380f8307e12SArchie Cobbs };
381f8307e12SArchie Cobbs 
382f8307e12SArchie Cobbs /************************************************************************
383f8307e12SArchie Cobbs 		STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
384f8307e12SArchie Cobbs  ************************************************************************/
385f8307e12SArchie Cobbs 
386f8307e12SArchie Cobbs /* Get length of the struct ng_ksocket_sockopt value field, which is the
387f8307e12SArchie Cobbs    just the excess of the message argument portion over the length of
388f8307e12SArchie Cobbs    the struct ng_ksocket_sockopt. */
389f8307e12SArchie Cobbs static int
ng_parse_sockoptval_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)390f8307e12SArchie Cobbs ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
391f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
392f8307e12SArchie Cobbs {
393f8307e12SArchie Cobbs 	static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
394f8307e12SArchie Cobbs 	const struct ng_ksocket_sockopt *sopt;
395f8307e12SArchie Cobbs 	const struct ng_mesg *msg;
396f8307e12SArchie Cobbs 
397f8307e12SArchie Cobbs 	sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
398f8307e12SArchie Cobbs 	msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
399f8307e12SArchie Cobbs 	return msg->header.arglen - sizeof(*sopt);
400f8307e12SArchie Cobbs }
401f8307e12SArchie Cobbs 
402f8307e12SArchie Cobbs /* Parse type for the option value part of a struct ng_ksocket_sockopt
403f8307e12SArchie Cobbs    XXX Eventually, we should handle the different socket options specially.
404f8307e12SArchie Cobbs    XXX This would avoid byte order problems, eg an integer value of 1 is
405f8307e12SArchie Cobbs    XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
406f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockoptval_type = {
407f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
408f8307e12SArchie Cobbs 	&ng_parse_sockoptval_getLength
409f8307e12SArchie Cobbs };
410f8307e12SArchie Cobbs 
411f8307e12SArchie Cobbs /* Parse type for struct ng_ksocket_sockopt */
412f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[]
413f8307e12SArchie Cobbs 	= NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
414f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockopt_type = {
415f8307e12SArchie Cobbs 	&ng_parse_struct_type,
416f0184ff8SArchie Cobbs 	&ng_ksocket_sockopt_type_fields
417f8307e12SArchie Cobbs };
418f8307e12SArchie Cobbs 
419f97e0a07SJulian Elischer /* Parse type for struct ng_ksocket_accept */
420f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[]
421f97e0a07SJulian Elischer 	= NGM_KSOCKET_ACCEPT_INFO;
422f97e0a07SJulian Elischer static const struct ng_parse_type ng_ksocket_accept_type = {
423f97e0a07SJulian Elischer 	&ng_parse_struct_type,
424f0184ff8SArchie Cobbs 	&ng_ksocket_accept_type_fields
425f97e0a07SJulian Elischer };
426f97e0a07SJulian Elischer 
427f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
428f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ksocket_cmds[] = {
429f8307e12SArchie Cobbs 	{
430f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
431f8307e12SArchie Cobbs 	  NGM_KSOCKET_BIND,
432f8307e12SArchie Cobbs 	  "bind",
433f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type,
434f8307e12SArchie Cobbs 	  NULL
435f8307e12SArchie Cobbs 	},
436f8307e12SArchie Cobbs 	{
437f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
438f8307e12SArchie Cobbs 	  NGM_KSOCKET_LISTEN,
439f8307e12SArchie Cobbs 	  "listen",
440f8307e12SArchie Cobbs 	  &ng_parse_int32_type,
441f8307e12SArchie Cobbs 	  NULL
442f8307e12SArchie Cobbs 	},
443f8307e12SArchie Cobbs 	{
444f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
445f8307e12SArchie Cobbs 	  NGM_KSOCKET_ACCEPT,
446f8307e12SArchie Cobbs 	  "accept",
447f8307e12SArchie Cobbs 	  NULL,
448f97e0a07SJulian Elischer 	  &ng_ksocket_accept_type
449f8307e12SArchie Cobbs 	},
450f8307e12SArchie Cobbs 	{
451f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
452f8307e12SArchie Cobbs 	  NGM_KSOCKET_CONNECT,
453f8307e12SArchie Cobbs 	  "connect",
454f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type,
455f97e0a07SJulian Elischer 	  &ng_parse_int32_type
456f8307e12SArchie Cobbs 	},
457f8307e12SArchie Cobbs 	{
458f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
459f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETNAME,
460f8307e12SArchie Cobbs 	  "getname",
461f8307e12SArchie Cobbs 	  NULL,
462f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
463f8307e12SArchie Cobbs 	},
464f8307e12SArchie Cobbs 	{
465f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
466f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETPEERNAME,
467f8307e12SArchie Cobbs 	  "getpeername",
468f8307e12SArchie Cobbs 	  NULL,
469f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
470f8307e12SArchie Cobbs 	},
471f8307e12SArchie Cobbs 	{
472f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
473f8307e12SArchie Cobbs 	  NGM_KSOCKET_SETOPT,
474f8307e12SArchie Cobbs 	  "setopt",
475f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type,
476f8307e12SArchie Cobbs 	  NULL
477f8307e12SArchie Cobbs 	},
478f8307e12SArchie Cobbs 	{
479f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
480f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETOPT,
481f8307e12SArchie Cobbs 	  "getopt",
482f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type,
483f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type
484f8307e12SArchie Cobbs 	},
485f8307e12SArchie Cobbs 	{ 0 }
486f8307e12SArchie Cobbs };
487f8307e12SArchie Cobbs 
488f8307e12SArchie Cobbs /* Node type descriptor */
489f8307e12SArchie Cobbs static struct ng_type ng_ksocket_typestruct = {
490f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
491f8aae777SJulian Elischer 	.name =		NG_KSOCKET_NODE_TYPE,
492f8aae777SJulian Elischer 	.constructor =	ng_ksocket_constructor,
493f8aae777SJulian Elischer 	.rcvmsg =	ng_ksocket_rcvmsg,
494f8aae777SJulian Elischer 	.shutdown =	ng_ksocket_shutdown,
495f8aae777SJulian Elischer 	.newhook =	ng_ksocket_newhook,
496f8aae777SJulian Elischer 	.connect =	ng_ksocket_connect,
497f8aae777SJulian Elischer 	.rcvdata =	ng_ksocket_rcvdata,
498f8aae777SJulian Elischer 	.disconnect =	ng_ksocket_disconnect,
499f8aae777SJulian Elischer 	.cmdlist =	ng_ksocket_cmds,
500f8307e12SArchie Cobbs };
501f8307e12SArchie Cobbs NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
502f8307e12SArchie Cobbs 
5034cc20ab1SSeigo Tanimura #define ERROUT(x)	do { error = (x); goto done; } while (0)
504cb3c7a5dSArchie Cobbs 
505cb3c7a5dSArchie Cobbs /************************************************************************
506cb3c7a5dSArchie Cobbs 			NETGRAPH NODE STUFF
507cb3c7a5dSArchie Cobbs  ************************************************************************/
508cb3c7a5dSArchie Cobbs 
509cb3c7a5dSArchie Cobbs /*
510cb3c7a5dSArchie Cobbs  * Node type constructor
511f97e0a07SJulian Elischer  * The NODE part is assumed to be all set up.
512f97e0a07SJulian Elischer  * There is already a reference to the node for us.
513cb3c7a5dSArchie Cobbs  */
514cb3c7a5dSArchie Cobbs static int
ng_ksocket_constructor(node_p node)515069154d5SJulian Elischer ng_ksocket_constructor(node_p node)
516cb3c7a5dSArchie Cobbs {
517cb3c7a5dSArchie Cobbs 	priv_p priv;
518cb3c7a5dSArchie Cobbs 
519cb3c7a5dSArchie Cobbs 	/* Allocate private structure */
52038f1b2d1SGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_KSOCKET, M_NOWAIT | M_ZERO);
52138f1b2d1SGleb Smirnoff 	if (priv == NULL)
52238f1b2d1SGleb Smirnoff 		return (ENOMEM);
523cb3c7a5dSArchie Cobbs 
524f97e0a07SJulian Elischer 	LIST_INIT(&priv->embryos);
525f97e0a07SJulian Elischer 	/* cross link them */
526f97e0a07SJulian Elischer 	priv->node = node;
52730400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
528cb3c7a5dSArchie Cobbs 
529cb3c7a5dSArchie Cobbs 	/* Done */
530cb3c7a5dSArchie Cobbs 	return (0);
531cb3c7a5dSArchie Cobbs }
532cb3c7a5dSArchie Cobbs 
533cb3c7a5dSArchie Cobbs /*
534cb3c7a5dSArchie Cobbs  * Give our OK for a hook to be added. The hook name is of the
53593caaaa7SArchie Cobbs  * form "<family>/<type>/<proto>" where the three components may
536cb3c7a5dSArchie Cobbs  * be decimal numbers or else aliases from the above lists.
537cb3c7a5dSArchie Cobbs  *
538cb3c7a5dSArchie Cobbs  * Connecting a hook amounts to opening the socket.  Disconnecting
539cb3c7a5dSArchie Cobbs  * the hook closes the socket and destroys the node as well.
540cb3c7a5dSArchie Cobbs  */
541cb3c7a5dSArchie Cobbs static int
ng_ksocket_newhook(node_p node,hook_p hook,const char * name0)542cb3c7a5dSArchie Cobbs ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
543cb3c7a5dSArchie Cobbs {
54442ec1da4SRobert Watson 	struct thread *td = curthread;	/* XXX broken */
54530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
54687e2c66aSHartmut Brandt 	char *s1, *s2, name[NG_HOOKSIZ];
547cb3c7a5dSArchie Cobbs 	int family, type, protocol, error;
548cb3c7a5dSArchie Cobbs 
549cb3c7a5dSArchie Cobbs 	/* Check if we're already connected */
550cb3c7a5dSArchie Cobbs 	if (priv->hook != NULL)
551cb3c7a5dSArchie Cobbs 		return (EISCONN);
552cb3c7a5dSArchie Cobbs 
553f97e0a07SJulian Elischer 	if (priv->flags & KSF_CLONED) {
554f97e0a07SJulian Elischer 		if (priv->flags & KSF_EMBRYONIC) {
555f97e0a07SJulian Elischer 			/* Remove ourselves from our parent's embryo list */
556f97e0a07SJulian Elischer 			LIST_REMOVE(priv, siblings);
557f97e0a07SJulian Elischer 			priv->flags &= ~KSF_EMBRYONIC;
558f97e0a07SJulian Elischer 		}
559f97e0a07SJulian Elischer 	} else {
560cb3c7a5dSArchie Cobbs 		/* Extract family, type, and protocol from hook name */
561cb3c7a5dSArchie Cobbs 		snprintf(name, sizeof(name), "%s", name0);
562cb3c7a5dSArchie Cobbs 		s1 = name;
563dc15eac0SEd Schouten 		if ((s2 = strchr(s1, '/')) == NULL)
564cb3c7a5dSArchie Cobbs 			return (EINVAL);
565cb3c7a5dSArchie Cobbs 		*s2++ = '\0';
566f97e0a07SJulian Elischer 		family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
567f97e0a07SJulian Elischer 		if (family == -1)
568cb3c7a5dSArchie Cobbs 			return (EINVAL);
569cb3c7a5dSArchie Cobbs 		s1 = s2;
570dc15eac0SEd Schouten 		if ((s2 = strchr(s1, '/')) == NULL)
571cb3c7a5dSArchie Cobbs 			return (EINVAL);
572cb3c7a5dSArchie Cobbs 		*s2++ = '\0';
573f97e0a07SJulian Elischer 		type = ng_ksocket_parse(ng_ksocket_types, s1, 0);
574f97e0a07SJulian Elischer 		if (type == -1)
575cb3c7a5dSArchie Cobbs 			return (EINVAL);
576cb3c7a5dSArchie Cobbs 		s1 = s2;
577f97e0a07SJulian Elischer 		protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family);
578f97e0a07SJulian Elischer 		if (protocol == -1)
579cb3c7a5dSArchie Cobbs 			return (EINVAL);
580cb3c7a5dSArchie Cobbs 
581cb3c7a5dSArchie Cobbs 		/* Create the socket */
5829c4d63daSRobert Watson 		error = socreate(family, &priv->so, type, protocol,
583a854ed98SJohn Baldwin 		   td->td_ucred, td);
584f97e0a07SJulian Elischer 		if (error != 0)
585cb3c7a5dSArchie Cobbs 			return (error);
586cb3c7a5dSArchie Cobbs 
587cb3c7a5dSArchie Cobbs 		/* XXX call soreserve() ? */
588f97e0a07SJulian Elischer 	}
589cb3c7a5dSArchie Cobbs 
590cb3c7a5dSArchie Cobbs 	/* OK */
591cb3c7a5dSArchie Cobbs 	priv->hook = hook;
592bc90ff47SGleb Smirnoff 
593bc90ff47SGleb Smirnoff 	/*
594bc90ff47SGleb Smirnoff 	 * In case of misconfigured routing a packet may reenter
595bc90ff47SGleb Smirnoff 	 * ksocket node recursively. Decouple stack to avoid possible
596bc90ff47SGleb Smirnoff 	 * panics about sleeping with locks held.
597bc90ff47SGleb Smirnoff 	 */
598bc90ff47SGleb Smirnoff 	NG_HOOK_FORCE_QUEUE(hook);
599bc90ff47SGleb Smirnoff 
600cb3c7a5dSArchie Cobbs 	return(0);
601cb3c7a5dSArchie Cobbs }
602cb3c7a5dSArchie Cobbs 
603f97e0a07SJulian Elischer static int
ng_ksocket_connect(hook_p hook)604f97e0a07SJulian Elischer ng_ksocket_connect(hook_p hook)
605f97e0a07SJulian Elischer {
606f97e0a07SJulian Elischer 	node_p node = NG_HOOK_NODE(hook);
607f97e0a07SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
608f97e0a07SJulian Elischer 	struct socket *const so = priv->so;
609f97e0a07SJulian Elischer 
610f97e0a07SJulian Elischer 	/* Add our hook for incoming data and other events */
6111a3d1be4SGleb Smirnoff 	SOCK_RECVBUF_LOCK(so);
61274fb0ba7SJohn Baldwin 	soupcall_set(priv->so, SO_RCV, ng_ksocket_incoming, node);
6131a3d1be4SGleb Smirnoff 	SOCK_RECVBUF_UNLOCK(so);
6141a3d1be4SGleb Smirnoff 	SOCK_SENDBUF_LOCK(so);
61574fb0ba7SJohn Baldwin 	soupcall_set(priv->so, SO_SND, ng_ksocket_incoming, node);
6161a3d1be4SGleb Smirnoff 	SOCK_SENDBUF_UNLOCK(so);
6179535efc0SRobert Watson 	SOCK_LOCK(priv->so);
618f97e0a07SJulian Elischer 	priv->so->so_state |= SS_NBIO;
6199535efc0SRobert Watson 	SOCK_UNLOCK(priv->so);
620f97e0a07SJulian Elischer 	/*
621f97e0a07SJulian Elischer 	 * --Original comment--
622f97e0a07SJulian Elischer 	 * On a cloned socket we may have already received one or more
623f97e0a07SJulian Elischer 	 * upcalls which we couldn't handle without a hook.  Handle
624f97e0a07SJulian Elischer 	 * those now.
625f97e0a07SJulian Elischer 	 * We cannot call the upcall function directly
626f97e0a07SJulian Elischer 	 * from here, because until this function has returned our
627f97e0a07SJulian Elischer 	 * hook isn't connected.
628f97e0a07SJulian Elischer 	 *
629f97e0a07SJulian Elischer 	 * ---meta comment for -current ---
630f97e0a07SJulian Elischer 	 * XXX This is dubius.
631f97e0a07SJulian Elischer 	 * Upcalls between the time that the hook was
632f97e0a07SJulian Elischer 	 * first created and now (on another processesor) will
633f97e0a07SJulian Elischer 	 * be earlier on the queue than the request to finalise the hook.
634f97e0a07SJulian Elischer 	 * By the time the hook is finalised,
635053359b7SPedro F. Giffuni 	 * The queued upcalls will have happened and the code
636f97e0a07SJulian Elischer 	 * will have discarded them because of a lack of a hook.
637f97e0a07SJulian Elischer 	 * (socket not open).
638f97e0a07SJulian Elischer 	 *
639f97e0a07SJulian Elischer 	 * This is a bad byproduct of the complicated way in which hooks
640f97e0a07SJulian Elischer 	 * are now created (3 daisy chained async events).
641f97e0a07SJulian Elischer 	 *
642f97e0a07SJulian Elischer 	 * Since we are a netgraph operation
643f97e0a07SJulian Elischer 	 * We know that we hold a lock on this node. This forces the
644f97e0a07SJulian Elischer 	 * request we make below to be queued rather than implemented
645053359b7SPedro F. Giffuni 	 * immediately which will cause the upcall function to be called a bit
646f97e0a07SJulian Elischer 	 * later.
647053359b7SPedro F. Giffuni 	 * However, as we will run any waiting queued operations immediately
648f97e0a07SJulian Elischer 	 * after doing this one, if we have not finalised the other end
649f97e0a07SJulian Elischer 	 * of the hook, those queued operations will fail.
650f97e0a07SJulian Elischer 	 */
651f97e0a07SJulian Elischer 	if (priv->flags & KSF_CLONED) {
652f97e0a07SJulian Elischer 		ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, M_NOWAIT);
653f97e0a07SJulian Elischer 	}
654f97e0a07SJulian Elischer 
655f97e0a07SJulian Elischer 	return (0);
656f97e0a07SJulian Elischer }
657f97e0a07SJulian Elischer 
658cb3c7a5dSArchie Cobbs /*
659cb3c7a5dSArchie Cobbs  * Receive a control message
660cb3c7a5dSArchie Cobbs  */
661cb3c7a5dSArchie Cobbs static int
ng_ksocket_rcvmsg(node_p node,item_p item,hook_p lasthook)662069154d5SJulian Elischer ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook)
663cb3c7a5dSArchie Cobbs {
66442ec1da4SRobert Watson 	struct thread *td = curthread;	/* XXX broken */
66530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
666f8307e12SArchie Cobbs 	struct socket *const so = priv->so;
667cb3c7a5dSArchie Cobbs 	struct ng_mesg *resp = NULL;
668cb3c7a5dSArchie Cobbs 	int error = 0;
669069154d5SJulian Elischer 	struct ng_mesg *msg;
670cb3c7a5dSArchie Cobbs 
671069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
672cb3c7a5dSArchie Cobbs 	switch (msg->header.typecookie) {
673cb3c7a5dSArchie Cobbs 	case NGM_KSOCKET_COOKIE:
674cb3c7a5dSArchie Cobbs 		switch (msg->header.cmd) {
675cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_BIND:
676cb3c7a5dSArchie Cobbs 		    {
677f8307e12SArchie Cobbs 			struct sockaddr *const sa
678f8307e12SArchie Cobbs 			    = (struct sockaddr *)msg->data;
679cb3c7a5dSArchie Cobbs 
680f8307e12SArchie Cobbs 			/* Sanity check */
681f8307e12SArchie Cobbs 			if (msg->header.arglen < SADATA_OFFSET
682f8307e12SArchie Cobbs 			    || msg->header.arglen < sa->sa_len)
683f8307e12SArchie Cobbs 				ERROUT(EINVAL);
684f8307e12SArchie Cobbs 			if (so == NULL)
685f8307e12SArchie Cobbs 				ERROUT(ENXIO);
686cb3c7a5dSArchie Cobbs 
687f8307e12SArchie Cobbs 			/* Bind */
688b40ce416SJulian Elischer 			error = sobind(so, sa, td);
689cb3c7a5dSArchie Cobbs 			break;
690cb3c7a5dSArchie Cobbs 		    }
691cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_LISTEN:
692cb3c7a5dSArchie Cobbs 		    {
693f8307e12SArchie Cobbs 			/* Sanity check */
694f97e0a07SJulian Elischer 			if (msg->header.arglen != sizeof(int32_t))
695cb3c7a5dSArchie Cobbs 				ERROUT(EINVAL);
696f8307e12SArchie Cobbs 			if (so == NULL)
697f8307e12SArchie Cobbs 				ERROUT(ENXIO);
698cb3c7a5dSArchie Cobbs 
699f8307e12SArchie Cobbs 			/* Listen */
700779f106aSGleb Smirnoff 			so->so_state |= SS_NBIO;
701b40ce416SJulian Elischer 			error = solisten(so, *((int32_t *)msg->data), td);
70243f7e216SGleb Smirnoff 			if (error == 0) {
70343f7e216SGleb Smirnoff 				SOLISTEN_LOCK(so);
70443f7e216SGleb Smirnoff 				solisten_upcall_set(so,
70543f7e216SGleb Smirnoff 				    ng_ksocket_listen_upcall, priv);
70643f7e216SGleb Smirnoff 				SOLISTEN_UNLOCK(so);
70743f7e216SGleb Smirnoff 			}
708cb3c7a5dSArchie Cobbs 			break;
709cb3c7a5dSArchie Cobbs 		    }
710cb3c7a5dSArchie Cobbs 
711cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_ACCEPT:
712cb3c7a5dSArchie Cobbs 		    {
713f8307e12SArchie Cobbs 			/* Sanity check */
714f8307e12SArchie Cobbs 			if (msg->header.arglen != 0)
715f8307e12SArchie Cobbs 				ERROUT(EINVAL);
716f8307e12SArchie Cobbs 			if (so == NULL)
717f8307e12SArchie Cobbs 				ERROUT(ENXIO);
718f8307e12SArchie Cobbs 
719f97e0a07SJulian Elischer 			/* Make sure the socket is capable of accepting */
720f97e0a07SJulian Elischer 			if (!(so->so_options & SO_ACCEPTCONN))
7214cc20ab1SSeigo Tanimura 				ERROUT(EINVAL);
722f97e0a07SJulian Elischer 			if (priv->flags & KSF_ACCEPTING)
723f97e0a07SJulian Elischer 				ERROUT(EALREADY);
724f8307e12SArchie Cobbs 
725f97e0a07SJulian Elischer 			/*
726f97e0a07SJulian Elischer 			 * If a connection is already complete, take it.
727f97e0a07SJulian Elischer 			 * Otherwise let the upcall function deal with
72843f7e216SGleb Smirnoff 			 * the connection when it comes in.  Don't return
72943f7e216SGleb Smirnoff 			 * EWOULDBLOCK, per ng_ksocket(4) documentation.
730f97e0a07SJulian Elischer 			 */
731779f106aSGleb Smirnoff 			error = ng_ksocket_accept(priv);
73243f7e216SGleb Smirnoff 			if (error == EWOULDBLOCK)
73343f7e216SGleb Smirnoff 				error = 0;
73443f7e216SGleb Smirnoff 			if (error != 0)
735779f106aSGleb Smirnoff 				ERROUT(error);
73643f7e216SGleb Smirnoff 
737f97e0a07SJulian Elischer 			priv->response_token = msg->header.token;
738497c2cf4SJohn Baldwin 			priv->response_addr = NGI_RETADDR(item);
739cb3c7a5dSArchie Cobbs 			break;
740cb3c7a5dSArchie Cobbs 		    }
741cb3c7a5dSArchie Cobbs 
742cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_CONNECT:
743cb3c7a5dSArchie Cobbs 		    {
744f8307e12SArchie Cobbs 			struct sockaddr *const sa
745f8307e12SArchie Cobbs 			    = (struct sockaddr *)msg->data;
746cb3c7a5dSArchie Cobbs 
747f8307e12SArchie Cobbs 			/* Sanity check */
748f8307e12SArchie Cobbs 			if (msg->header.arglen < SADATA_OFFSET
749f8307e12SArchie Cobbs 			    || msg->header.arglen < sa->sa_len)
750f8307e12SArchie Cobbs 				ERROUT(EINVAL);
751f8307e12SArchie Cobbs 			if (so == NULL)
752f8307e12SArchie Cobbs 				ERROUT(ENXIO);
753cb3c7a5dSArchie Cobbs 
754cb3c7a5dSArchie Cobbs 			/* Do connect */
755cb3c7a5dSArchie Cobbs 			if ((so->so_state & SS_ISCONNECTING) != 0)
7564cc20ab1SSeigo Tanimura 				ERROUT(EALREADY);
757b40ce416SJulian Elischer 			if ((error = soconnect(so, sa, td)) != 0) {
758cb3c7a5dSArchie Cobbs 				so->so_state &= ~SS_ISCONNECTING;
7594cc20ab1SSeigo Tanimura 				ERROUT(error);
760cb3c7a5dSArchie Cobbs 			}
7617d780740SArchie Cobbs 			if ((so->so_state & SS_ISCONNECTING) != 0) {
762f97e0a07SJulian Elischer 				/* We will notify the sender when we connect */
763f97e0a07SJulian Elischer 				priv->response_token = msg->header.token;
764497c2cf4SJohn Baldwin 				priv->response_addr = NGI_RETADDR(item);
765f97e0a07SJulian Elischer 				priv->flags |= KSF_CONNECTING;
7664cc20ab1SSeigo Tanimura 				ERROUT(EINPROGRESS);
7677d780740SArchie Cobbs 			}
768cb3c7a5dSArchie Cobbs 			break;
769cb3c7a5dSArchie Cobbs 		    }
770cb3c7a5dSArchie Cobbs 
771cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETNAME:
772cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETPEERNAME:
773cb3c7a5dSArchie Cobbs 		    {
7740fac350cSGleb Smirnoff 			int (*func)(struct socket *so, struct sockaddr *sa);
7750fac350cSGleb Smirnoff 			struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
776f8307e12SArchie Cobbs 
777f8307e12SArchie Cobbs 			/* Sanity check */
778f8307e12SArchie Cobbs 			if (msg->header.arglen != 0)
779f8307e12SArchie Cobbs 				ERROUT(EINVAL);
780f8307e12SArchie Cobbs 			if (so == NULL)
781f8307e12SArchie Cobbs 				ERROUT(ENXIO);
782f8307e12SArchie Cobbs 
783f8307e12SArchie Cobbs 			/* Get function */
784f8307e12SArchie Cobbs 			if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
785f79a8585SGleb Smirnoff 				if ((so->so_state & SS_ISCONNECTED) == 0)
7864cc20ab1SSeigo Tanimura 					ERROUT(ENOTCONN);
7870fac350cSGleb Smirnoff 				func = sopeeraddr;
788f8307e12SArchie Cobbs 			} else
7890fac350cSGleb Smirnoff 				func = sosockaddr;
790f8307e12SArchie Cobbs 
791f8307e12SArchie Cobbs 			/* Get local or peer address */
7920fac350cSGleb Smirnoff 			error = (*func)(so, (struct sockaddr *)&ss);
7930fac350cSGleb Smirnoff 			if (error)
7940fac350cSGleb Smirnoff 				break;
795f8307e12SArchie Cobbs 
796f8307e12SArchie Cobbs 			/* Send it back in a response */
7970fac350cSGleb Smirnoff 			NG_MKRESPONSE(resp, msg, ss.ss_len, M_NOWAIT);
7980fac350cSGleb Smirnoff 			if (resp != NULL)
7990fac350cSGleb Smirnoff 				bcopy(&ss, resp->data, ss.ss_len);
8000fac350cSGleb Smirnoff 			else
801f8307e12SArchie Cobbs 				error = ENOMEM;
802f8307e12SArchie Cobbs 
803cb3c7a5dSArchie Cobbs 			break;
804cb3c7a5dSArchie Cobbs 		    }
805cb3c7a5dSArchie Cobbs 
806cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETOPT:
807cb3c7a5dSArchie Cobbs 		    {
808f8307e12SArchie Cobbs 			struct ng_ksocket_sockopt *ksopt =
809f8307e12SArchie Cobbs 			    (struct ng_ksocket_sockopt *)msg->data;
810f8307e12SArchie Cobbs 			struct sockopt sopt;
811f8307e12SArchie Cobbs 
812f8307e12SArchie Cobbs 			/* Sanity check */
813f8307e12SArchie Cobbs 			if (msg->header.arglen != sizeof(*ksopt))
814f8307e12SArchie Cobbs 				ERROUT(EINVAL);
815f8307e12SArchie Cobbs 			if (so == NULL)
816f8307e12SArchie Cobbs 				ERROUT(ENXIO);
817f8307e12SArchie Cobbs 
818f8307e12SArchie Cobbs 			/* Get response with room for option value */
819f8307e12SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
820f8307e12SArchie Cobbs 			    + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
821f8307e12SArchie Cobbs 			if (resp == NULL)
822f8307e12SArchie Cobbs 				ERROUT(ENOMEM);
823f8307e12SArchie Cobbs 
824f8307e12SArchie Cobbs 			/* Get socket option, and put value in the response */
825f8307e12SArchie Cobbs 			sopt.sopt_dir = SOPT_GET;
826f8307e12SArchie Cobbs 			sopt.sopt_level = ksopt->level;
827f8307e12SArchie Cobbs 			sopt.sopt_name = ksopt->name;
828b40ce416SJulian Elischer 			sopt.sopt_td = NULL;
829f8307e12SArchie Cobbs 			sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
830f8307e12SArchie Cobbs 			ksopt = (struct ng_ksocket_sockopt *)resp->data;
831f8307e12SArchie Cobbs 			sopt.sopt_val = ksopt->value;
832f8307e12SArchie Cobbs 			if ((error = sogetopt(so, &sopt)) != 0) {
833069154d5SJulian Elischer 				NG_FREE_MSG(resp);
834f8307e12SArchie Cobbs 				break;
835f8307e12SArchie Cobbs 			}
836f8307e12SArchie Cobbs 
837f8307e12SArchie Cobbs 			/* Set actual value length */
838f8307e12SArchie Cobbs 			resp->header.arglen = sizeof(*ksopt)
839f8307e12SArchie Cobbs 			    + sopt.sopt_valsize;
840cb3c7a5dSArchie Cobbs 			break;
841cb3c7a5dSArchie Cobbs 		    }
842cb3c7a5dSArchie Cobbs 
843cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_SETOPT:
844cb3c7a5dSArchie Cobbs 		    {
845f8307e12SArchie Cobbs 			struct ng_ksocket_sockopt *const ksopt =
846f8307e12SArchie Cobbs 			    (struct ng_ksocket_sockopt *)msg->data;
847f8307e12SArchie Cobbs 			const int valsize = msg->header.arglen - sizeof(*ksopt);
848f8307e12SArchie Cobbs 			struct sockopt sopt;
849f8307e12SArchie Cobbs 
850f8307e12SArchie Cobbs 			/* Sanity check */
851f8307e12SArchie Cobbs 			if (valsize < 0)
852f8307e12SArchie Cobbs 				ERROUT(EINVAL);
853f8307e12SArchie Cobbs 			if (so == NULL)
854f8307e12SArchie Cobbs 				ERROUT(ENXIO);
855f8307e12SArchie Cobbs 
856f8307e12SArchie Cobbs 			/* Set socket option */
857f8307e12SArchie Cobbs 			sopt.sopt_dir = SOPT_SET;
858f8307e12SArchie Cobbs 			sopt.sopt_level = ksopt->level;
859f8307e12SArchie Cobbs 			sopt.sopt_name = ksopt->name;
860f8307e12SArchie Cobbs 			sopt.sopt_val = ksopt->value;
861f8307e12SArchie Cobbs 			sopt.sopt_valsize = valsize;
862b40ce416SJulian Elischer 			sopt.sopt_td = NULL;
863f8307e12SArchie Cobbs 			error = sosetopt(so, &sopt);
864cb3c7a5dSArchie Cobbs 			break;
865cb3c7a5dSArchie Cobbs 		    }
866cb3c7a5dSArchie Cobbs 
867cb3c7a5dSArchie Cobbs 		default:
868cb3c7a5dSArchie Cobbs 			error = EINVAL;
869cb3c7a5dSArchie Cobbs 			break;
870cb3c7a5dSArchie Cobbs 		}
871cb3c7a5dSArchie Cobbs 		break;
872cb3c7a5dSArchie Cobbs 	default:
873cb3c7a5dSArchie Cobbs 		error = EINVAL;
874cb3c7a5dSArchie Cobbs 		break;
875cb3c7a5dSArchie Cobbs 	}
876cb3c7a5dSArchie Cobbs done:
877069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
878069154d5SJulian Elischer 	NG_FREE_MSG(msg);
879cb3c7a5dSArchie Cobbs 	return (error);
880cb3c7a5dSArchie Cobbs }
881cb3c7a5dSArchie Cobbs 
882cb3c7a5dSArchie Cobbs /*
883cb3c7a5dSArchie Cobbs  * Receive incoming data on our hook.  Send it out the socket.
884cb3c7a5dSArchie Cobbs  */
885cb3c7a5dSArchie Cobbs static int
ng_ksocket_rcvdata(hook_p hook,item_p item)886069154d5SJulian Elischer ng_ksocket_rcvdata(hook_p hook, item_p item)
887cb3c7a5dSArchie Cobbs {
88842ec1da4SRobert Watson 	struct thread *td = curthread;	/* XXX broken */
88930400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
89030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
891cb3c7a5dSArchie Cobbs 	struct socket *const so = priv->so;
89219ff9e5fSArchie Cobbs 	struct sockaddr *sa = NULL;
893cb3c7a5dSArchie Cobbs 	int error;
894069154d5SJulian Elischer 	struct mbuf *m;
8955c100aeaSAlexander Motin #ifdef ALIGNED_POINTER
8965c100aeaSAlexander Motin 	struct mbuf *n;
8975c100aeaSAlexander Motin #endif /* ALIGNED_POINTER */
898327b288eSJulian Elischer 	struct sa_tag *stag;
899cb3c7a5dSArchie Cobbs 
900327b288eSJulian Elischer 	/* Extract data */
901069154d5SJulian Elischer 	NGI_GET_M(item, m);
902069154d5SJulian Elischer 	NG_FREE_ITEM(item);
9035c100aeaSAlexander Motin #ifdef ALIGNED_POINTER
9045c100aeaSAlexander Motin 	if (!ALIGNED_POINTER(mtod(m, caddr_t), uint32_t)) {
9055c100aeaSAlexander Motin 		n = m_defrag(m, M_NOWAIT);
9065c100aeaSAlexander Motin 		if (n == NULL) {
9075c100aeaSAlexander Motin 			m_freem(m);
9085c100aeaSAlexander Motin 			return (ENOBUFS);
9095c100aeaSAlexander Motin 		}
9105c100aeaSAlexander Motin 		m = n;
9115c100aeaSAlexander Motin 	}
9125c100aeaSAlexander Motin #endif /* ALIGNED_POINTER */
913b07785efSGleb Smirnoff 	/*
914b07785efSGleb Smirnoff 	 * Look if socket address is stored in packet tags.
915b07785efSGleb Smirnoff 	 * If sockaddr is ours, or provided by a third party (zero id),
916b07785efSGleb Smirnoff 	 * then we accept it.
917b07785efSGleb Smirnoff 	 */
918b07785efSGleb Smirnoff 	if (((stag = (struct sa_tag *)m_tag_locate(m, NGM_KSOCKET_COOKIE,
919b07785efSGleb Smirnoff 	    NG_KSOCKET_TAG_SOCKADDR, NULL)) != NULL) &&
920b07785efSGleb Smirnoff 	    (stag->id == NG_NODE_ID(node) || stag->id == 0))
921327b288eSJulian Elischer 		sa = &stag->sa;
92219ff9e5fSArchie Cobbs 
9234ae54e2fSBruce M Simpson 	/* Reset specific mbuf flags to prevent addressing problems. */
9244ae54e2fSBruce M Simpson 	m->m_flags &= ~(M_BCAST|M_MCAST);
9254ae54e2fSBruce M Simpson 
92619ff9e5fSArchie Cobbs 	/* Send packet */
927b0668f71SRobert Watson 	error = sosend(so, sa, 0, m, 0, 0, td);
92819ff9e5fSArchie Cobbs 
929cb3c7a5dSArchie Cobbs 	return (error);
930cb3c7a5dSArchie Cobbs }
931cb3c7a5dSArchie Cobbs 
932cb3c7a5dSArchie Cobbs /*
933cb3c7a5dSArchie Cobbs  * Destroy node
934cb3c7a5dSArchie Cobbs  */
935cb3c7a5dSArchie Cobbs static int
ng_ksocket_shutdown(node_p node)936069154d5SJulian Elischer ng_ksocket_shutdown(node_p node)
937cb3c7a5dSArchie Cobbs {
93830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
939efad7cbfSGleb Smirnoff 	struct socket *so = priv->so;
940f97e0a07SJulian Elischer 	priv_p embryo;
941cb3c7a5dSArchie Cobbs 
94219bff684SArchie Cobbs 	/* Close our socket (if any) */
94319bff684SArchie Cobbs 	if (priv->so != NULL) {
944efad7cbfSGleb Smirnoff 		if (SOLISTENING(so)) {
945efad7cbfSGleb Smirnoff 			SOLISTEN_LOCK(so);
946efad7cbfSGleb Smirnoff 			solisten_upcall_set(so, NULL, NULL);
947efad7cbfSGleb Smirnoff 			SOLISTEN_UNLOCK(so);
948efad7cbfSGleb Smirnoff 		} else {
949efad7cbfSGleb Smirnoff 			SOCK_RECVBUF_LOCK(so);
950efad7cbfSGleb Smirnoff 			soupcall_clear(so, SO_RCV);
951efad7cbfSGleb Smirnoff 			SOCK_RECVBUF_UNLOCK(so);
952efad7cbfSGleb Smirnoff 			SOCK_SENDBUF_LOCK(so);
953efad7cbfSGleb Smirnoff 			soupcall_clear(so, SO_SND);
954efad7cbfSGleb Smirnoff 			SOCK_SENDBUF_UNLOCK(so);
955efad7cbfSGleb Smirnoff 		}
956efad7cbfSGleb Smirnoff 		soclose(so);
95719bff684SArchie Cobbs 		priv->so = NULL;
95819bff684SArchie Cobbs 	}
95919bff684SArchie Cobbs 
960f97e0a07SJulian Elischer 	/* If we are an embryo, take ourselves out of the parent's list */
961f97e0a07SJulian Elischer 	if (priv->flags & KSF_EMBRYONIC) {
962f97e0a07SJulian Elischer 		LIST_REMOVE(priv, siblings);
963f97e0a07SJulian Elischer 		priv->flags &= ~KSF_EMBRYONIC;
964f97e0a07SJulian Elischer 	}
965f97e0a07SJulian Elischer 
966f97e0a07SJulian Elischer 	/* Remove any embryonic children we have */
967f97e0a07SJulian Elischer 	while (!LIST_EMPTY(&priv->embryos)) {
968f97e0a07SJulian Elischer 		embryo = LIST_FIRST(&priv->embryos);
969f97e0a07SJulian Elischer 		ng_rmnode_self(embryo->node);
970f97e0a07SJulian Elischer 	}
971f97e0a07SJulian Elischer 
972cb3c7a5dSArchie Cobbs 	/* Take down netgraph node */
973cb3c7a5dSArchie Cobbs 	bzero(priv, sizeof(*priv));
9741ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_KSOCKET);
97530400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
97630400f03SJulian Elischer 	NG_NODE_UNREF(node);		/* let the node escape */
977cb3c7a5dSArchie Cobbs 	return (0);
978cb3c7a5dSArchie Cobbs }
979cb3c7a5dSArchie Cobbs 
980cb3c7a5dSArchie Cobbs /*
981cb3c7a5dSArchie Cobbs  * Hook disconnection
982cb3c7a5dSArchie Cobbs  */
983cb3c7a5dSArchie Cobbs static int
ng_ksocket_disconnect(hook_p hook)984cb3c7a5dSArchie Cobbs ng_ksocket_disconnect(hook_p hook)
985cb3c7a5dSArchie Cobbs {
98630400f03SJulian Elischer 	KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0,
9876e551fb6SDavid E. O'Brien 	    ("%s: numhooks=%d?", __func__,
98893caaaa7SArchie Cobbs 	    NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook))));
98930400f03SJulian Elischer 	if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
99030400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
991cb3c7a5dSArchie Cobbs 	return (0);
992cb3c7a5dSArchie Cobbs }
993cb3c7a5dSArchie Cobbs 
994cb3c7a5dSArchie Cobbs /************************************************************************
995cb3c7a5dSArchie Cobbs 			HELPER STUFF
996cb3c7a5dSArchie Cobbs  ************************************************************************/
997cb3c7a5dSArchie Cobbs /*
9986e7ed930SAlexander Motin  * You should not "just call" a netgraph node function from an external
9996e7ed930SAlexander Motin  * asynchronous event. This is because in doing so you are ignoring the
10006e7ed930SAlexander Motin  * locking on the netgraph nodes. Instead call your function via ng_send_fn().
10016e7ed930SAlexander Motin  * This will call the function you chose, but will first do all the
1002f97e0a07SJulian Elischer  * locking rigmarole. Your function MAY only be called at some distant future
1003f97e0a07SJulian Elischer  * time (several millisecs away) so don't give it any arguments
1004f97e0a07SJulian Elischer  * that may be revoked soon (e.g. on your stack).
1005e71fefbeSGleb Smirnoff  *
1006e71fefbeSGleb Smirnoff  * To decouple stack, we use queue version of ng_send_fn().
1007cb3c7a5dSArchie Cobbs  */
1008f97e0a07SJulian Elischer 
100974fb0ba7SJohn Baldwin static int
ng_ksocket_incoming(struct socket * so,void * arg,int waitflag)1010cb3c7a5dSArchie Cobbs ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
1011cb3c7a5dSArchie Cobbs {
1012cb3c7a5dSArchie Cobbs 	const node_p node = arg;
10136e7ed930SAlexander Motin 	const priv_p priv = NG_NODE_PRIVATE(node);
10146e7ed930SAlexander Motin 	int wait = ((waitflag & M_WAITOK) ? NG_WAITOK : 0) | NG_QUEUE;
1015f97e0a07SJulian Elischer 
10166e7ed930SAlexander Motin 	/*
10176e7ed930SAlexander Motin 	 * Even if node is not locked, as soon as we are called, we assume
10186e7ed930SAlexander Motin 	 * it exist and it's private area is valid. With some care we can
10196e7ed930SAlexander Motin 	 * access it. Mark node that incoming event for it was sent to
10206e7ed930SAlexander Motin 	 * avoid unneded queue trashing.
10216e7ed930SAlexander Motin 	 */
10226e7ed930SAlexander Motin 	if (atomic_cmpset_int(&priv->fn_sent, 0, 1) &&
10236e7ed930SAlexander Motin 	    ng_send_fn1(node, NULL, &ng_ksocket_incoming2, so, 0, wait)) {
10246e7ed930SAlexander Motin 		atomic_store_rel_int(&priv->fn_sent, 0);
10256e7ed930SAlexander Motin 	}
102674fb0ba7SJohn Baldwin 	return (SU_OK);
1027f97e0a07SJulian Elischer }
1028f97e0a07SJulian Elischer 
1029f97e0a07SJulian Elischer /*
1030f97e0a07SJulian Elischer  * When incoming data is appended to the socket, we get notified here.
1031f97e0a07SJulian Elischer  * This is also called whenever a significant event occurs for the socket.
1032f97e0a07SJulian Elischer  * Our original caller may have queued this even some time ago and
1033f97e0a07SJulian Elischer  * we cannot trust that he even still exists. The node however is being
10346e7ed930SAlexander Motin  * held with a reference by the queueing code and guarantied to be valid.
1035f97e0a07SJulian Elischer  */
1036f97e0a07SJulian Elischer static void
ng_ksocket_incoming2(node_p node,hook_p hook,void * arg1,int arg2)10376e7ed930SAlexander Motin ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int arg2)
1038f97e0a07SJulian Elischer {
1039f97e0a07SJulian Elischer 	struct socket *so = arg1;
104030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1041f97e0a07SJulian Elischer 	struct ng_mesg *response;
10429165bf62SGleb Smirnoff 	int error;
1043cb3c7a5dSArchie Cobbs 
10446e551fb6SDavid E. O'Brien 	KASSERT(so == priv->so, ("%s: wrong socket", __func__));
1045f97e0a07SJulian Elischer 
10466e7ed930SAlexander Motin 	/* Allow next incoming event to be queued. */
10476e7ed930SAlexander Motin 	atomic_store_rel_int(&priv->fn_sent, 0);
10486e7ed930SAlexander Motin 
1049f97e0a07SJulian Elischer 	/* Check whether a pending connect operation has completed */
1050f97e0a07SJulian Elischer 	if (priv->flags & KSF_CONNECTING) {
1051f97e0a07SJulian Elischer 		if ((error = so->so_error) != 0) {
1052f97e0a07SJulian Elischer 			so->so_error = 0;
1053f97e0a07SJulian Elischer 			so->so_state &= ~SS_ISCONNECTING;
1054f97e0a07SJulian Elischer 		}
1055f97e0a07SJulian Elischer 		if (!(so->so_state & SS_ISCONNECTING)) {
1056f97e0a07SJulian Elischer 			NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
10576e7ed930SAlexander Motin 			    NGM_KSOCKET_CONNECT, sizeof(int32_t), M_NOWAIT);
1058f97e0a07SJulian Elischer 			if (response != NULL) {
1059f97e0a07SJulian Elischer 				response->header.flags |= NGF_RESP;
1060f97e0a07SJulian Elischer 				response->header.token = priv->response_token;
1061f97e0a07SJulian Elischer 				*(int32_t *)response->data = error;
1062f97e0a07SJulian Elischer 				/*
1063f97e0a07SJulian Elischer 				 * send an async "response" message
1064f97e0a07SJulian Elischer 				 * to the node that set us up
1065f97e0a07SJulian Elischer 				 * (if it still exists)
1066f97e0a07SJulian Elischer 				 */
1067facfd889SArchie Cobbs 				NG_SEND_MSG_ID(error, node,
1068facfd889SArchie Cobbs 				    response, priv->response_addr, 0);
1069f97e0a07SJulian Elischer 			}
1070f97e0a07SJulian Elischer 			priv->flags &= ~KSF_CONNECTING;
10714cc20ab1SSeigo Tanimura 		}
1072f97e0a07SJulian Elischer 	}
1073f97e0a07SJulian Elischer 
1074f97e0a07SJulian Elischer 	/*
1075f97e0a07SJulian Elischer 	 * If we don't have a hook, we must handle data events later.  When
1076f97e0a07SJulian Elischer 	 * the hook gets created and is connected, this upcall function
1077f97e0a07SJulian Elischer 	 * will be called again.
1078f97e0a07SJulian Elischer 	 */
1079c9b652e3SAndre Oppermann 	if (priv->hook == NULL)
1080f97e0a07SJulian Elischer 		return;
1081cb3c7a5dSArchie Cobbs 
10829165bf62SGleb Smirnoff 	/* Read and forward available mbufs. */
108319ff9e5fSArchie Cobbs 	while (1) {
10849165bf62SGleb Smirnoff 		struct uio uio;
10859165bf62SGleb Smirnoff 		struct sockaddr *sa;
10869a4d9e19SGleb Smirnoff 		struct mbuf *m;
10879165bf62SGleb Smirnoff 		int flags;
1088f8307e12SArchie Cobbs 
10899165bf62SGleb Smirnoff 		/* Try to get next packet from socket. */
10909165bf62SGleb Smirnoff 		uio.uio_td = NULL;
10919165bf62SGleb Smirnoff 		uio.uio_resid = IP_MAXPACKET;
10929165bf62SGleb Smirnoff 		flags = MSG_DONTWAIT;
10939165bf62SGleb Smirnoff 		sa = NULL;
1094b0668f71SRobert Watson 		if ((error = soreceive(so, (so->so_state & SS_ISCONNECTED) ?
10959165bf62SGleb Smirnoff 		    NULL : &sa, &uio, &m, NULL, &flags)) != 0)
109619ff9e5fSArchie Cobbs 			break;
109719ff9e5fSArchie Cobbs 
10989165bf62SGleb Smirnoff 		/* See if we got anything. */
10999165bf62SGleb Smirnoff 		if (flags & MSG_TRUNC) {
11009165bf62SGleb Smirnoff 			m_freem(m);
11019165bf62SGleb Smirnoff 			m = NULL;
11029165bf62SGleb Smirnoff 		}
110319ff9e5fSArchie Cobbs 		if (m == NULL) {
110419ff9e5fSArchie Cobbs 			if (sa != NULL)
11051ede983cSDag-Erling Smørgrav 				free(sa, M_SONAME);
110619ff9e5fSArchie Cobbs 			break;
110719ff9e5fSArchie Cobbs 		}
110819ff9e5fSArchie Cobbs 
11099a4d9e19SGleb Smirnoff 		KASSERT(m->m_nextpkt == NULL, ("%s: nextpkt", __func__));
11109a4d9e19SGleb Smirnoff 
1111e71fefbeSGleb Smirnoff 		/*
11129a4d9e19SGleb Smirnoff 		 * Stream sockets do not have packet boundaries, so
11139a4d9e19SGleb Smirnoff 		 * we have to allocate a header mbuf and attach the
11149a4d9e19SGleb Smirnoff 		 * stream of data to it.
1115e71fefbeSGleb Smirnoff 		 */
11169a4d9e19SGleb Smirnoff 		if (so->so_type == SOCK_STREAM) {
11179a4d9e19SGleb Smirnoff 			struct mbuf *mh;
11189a4d9e19SGleb Smirnoff 
11199a4d9e19SGleb Smirnoff 			mh = m_gethdr(M_NOWAIT, MT_DATA);
11209a4d9e19SGleb Smirnoff 			if (mh == NULL) {
11219a4d9e19SGleb Smirnoff 				m_freem(m);
11229a4d9e19SGleb Smirnoff 				if (sa != NULL)
11239a4d9e19SGleb Smirnoff 					free(sa, M_SONAME);
11249a4d9e19SGleb Smirnoff 				break;
11259a4d9e19SGleb Smirnoff 			}
11269a4d9e19SGleb Smirnoff 
11279a4d9e19SGleb Smirnoff 			mh->m_next = m;
11289a4d9e19SGleb Smirnoff 			for (; m; m = m->m_next)
11299a4d9e19SGleb Smirnoff 				mh->m_pkthdr.len += m->m_len;
11309a4d9e19SGleb Smirnoff 			m = mh;
1131e71fefbeSGleb Smirnoff 		}
113219ff9e5fSArchie Cobbs 
1133327b288eSJulian Elischer 		/* Put peer's socket address (if any) into a tag */
113419ff9e5fSArchie Cobbs 		if (sa != NULL) {
1135327b288eSJulian Elischer 			struct sa_tag	*stag;
113619ff9e5fSArchie Cobbs 
1137327b288eSJulian Elischer 			stag = (struct sa_tag *)m_tag_alloc(NGM_KSOCKET_COOKIE,
1138d96bd8d1SGleb Smirnoff 			    NG_KSOCKET_TAG_SOCKADDR, sizeof(ng_ID_t) +
1139d96bd8d1SGleb Smirnoff 			    sa->sa_len, M_NOWAIT);
1140327b288eSJulian Elischer 			if (stag == NULL) {
11411ede983cSDag-Erling Smørgrav 				free(sa, M_SONAME);
114219ff9e5fSArchie Cobbs 				goto sendit;
1143f8307e12SArchie Cobbs 			}
1144327b288eSJulian Elischer 			bcopy(sa, &stag->sa, sa->sa_len);
11451ede983cSDag-Erling Smørgrav 			free(sa, M_SONAME);
1146b07785efSGleb Smirnoff 			stag->id = NG_NODE_ID(node);
1147327b288eSJulian Elischer 			m_tag_prepend(m, &stag->tag);
114819ff9e5fSArchie Cobbs 		}
114919ff9e5fSArchie Cobbs 
1150327b288eSJulian Elischer sendit:		/* Forward data with optional peer sockaddr as packet tag */
1151327b288eSJulian Elischer 		NG_SEND_DATA_ONLY(error, priv->hook, m);
115219ff9e5fSArchie Cobbs 	}
1153f97e0a07SJulian Elischer 
1154f97e0a07SJulian Elischer 	/*
1155f97e0a07SJulian Elischer 	 * If the peer has closed the connection, forward a 0-length mbuf
1156f97e0a07SJulian Elischer 	 * to indicate end-of-file.
1157f97e0a07SJulian Elischer 	 */
1158d09c774bSGleb Smirnoff 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE &&
1159d09c774bSGleb Smirnoff 	    !(priv->flags & KSF_EOFSEEN)) {
1160d09c774bSGleb Smirnoff 		struct mbuf *m;
1161d09c774bSGleb Smirnoff 
1162d09c774bSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
1163d09c774bSGleb Smirnoff 		if (m != NULL)
1164f97e0a07SJulian Elischer 			NG_SEND_DATA_ONLY(error, priv->hook, m);
1165f97e0a07SJulian Elischer 		priv->flags |= KSF_EOFSEEN;
11664cc20ab1SSeigo Tanimura 	}
1167cb3c7a5dSArchie Cobbs }
1168cb3c7a5dSArchie Cobbs 
1169f97e0a07SJulian Elischer static int
ng_ksocket_accept(priv_p priv)1170779f106aSGleb Smirnoff ng_ksocket_accept(priv_p priv)
1171f97e0a07SJulian Elischer {
1172f97e0a07SJulian Elischer 	struct socket *const head = priv->so;
1173f97e0a07SJulian Elischer 	struct socket *so;
1174cfb1e929SGleb Smirnoff 	struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
1175f97e0a07SJulian Elischer 	struct ng_mesg *resp;
1176f97e0a07SJulian Elischer 	struct ng_ksocket_accept *resp_data;
1177f97e0a07SJulian Elischer 	node_p node;
1178f97e0a07SJulian Elischer 	priv_p priv2;
1179f97e0a07SJulian Elischer 	int len;
1180f97e0a07SJulian Elischer 	int error;
1181f97e0a07SJulian Elischer 
1182779f106aSGleb Smirnoff 	SOLISTEN_LOCK(head);
1183779f106aSGleb Smirnoff 	error = solisten_dequeue(head, &so, SOCK_NONBLOCK);
1184779f106aSGleb Smirnoff 	if (error == EWOULDBLOCK) {
1185779f106aSGleb Smirnoff 		priv->flags |= KSF_ACCEPTING;
1186779f106aSGleb Smirnoff 		return (error);
11872658b3bbSRobert Watson 	}
1188779f106aSGleb Smirnoff 	priv->flags &= ~KSF_ACCEPTING;
1189779f106aSGleb Smirnoff 	if (error)
1190779f106aSGleb Smirnoff 		return (error);
1191f97e0a07SJulian Elischer 
1192cfb1e929SGleb Smirnoff 	if ((error = soaccept(so, (struct sockaddr *)&ss)) != 0)
11937737de95SGleb Smirnoff 		return (error);
1194f97e0a07SJulian Elischer 
1195f97e0a07SJulian Elischer 	len = OFFSETOF(struct ng_ksocket_accept, addr);
1196cfb1e929SGleb Smirnoff 	len += ss.ss_len;
1197f97e0a07SJulian Elischer 
1198f97e0a07SJulian Elischer 	NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1199f97e0a07SJulian Elischer 	    M_NOWAIT);
1200f97e0a07SJulian Elischer 	if (resp == NULL) {
1201f97e0a07SJulian Elischer 		soclose(so);
1202f97e0a07SJulian Elischer 		goto out;
1203f97e0a07SJulian Elischer 	}
1204f97e0a07SJulian Elischer 	resp->header.flags |= NGF_RESP;
1205f97e0a07SJulian Elischer 	resp->header.token = priv->response_token;
1206f97e0a07SJulian Elischer 
1207f97e0a07SJulian Elischer 	/* Clone a ksocket node to wrap the new socket */
1208f97e0a07SJulian Elischer 	error = ng_make_node_common(&ng_ksocket_typestruct, &node);
1209f97e0a07SJulian Elischer 	if (error) {
12101ede983cSDag-Erling Smørgrav 		free(resp, M_NETGRAPH);
1211f97e0a07SJulian Elischer 		soclose(so);
1212f97e0a07SJulian Elischer 		goto out;
1213f97e0a07SJulian Elischer 	}
1214f97e0a07SJulian Elischer 
1215f97e0a07SJulian Elischer 	if (ng_ksocket_constructor(node) != 0) {
1216f97e0a07SJulian Elischer 		NG_NODE_UNREF(node);
12171ede983cSDag-Erling Smørgrav 		free(resp, M_NETGRAPH);
1218f97e0a07SJulian Elischer 		soclose(so);
1219f97e0a07SJulian Elischer 		goto out;
1220f97e0a07SJulian Elischer 	}
1221f97e0a07SJulian Elischer 
1222f97e0a07SJulian Elischer 	priv2 = NG_NODE_PRIVATE(node);
1223f97e0a07SJulian Elischer 	priv2->so = so;
1224f97e0a07SJulian Elischer 	priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1225f97e0a07SJulian Elischer 
1226f97e0a07SJulian Elischer 	/*
1227f97e0a07SJulian Elischer 	 * Insert the cloned node into a list of embryonic children
1228f97e0a07SJulian Elischer 	 * on the parent node.  When a hook is created on the cloned
1229f97e0a07SJulian Elischer 	 * node it will be removed from this list.  When the parent
1230f97e0a07SJulian Elischer 	 * is destroyed it will destroy any embryonic children it has.
1231f97e0a07SJulian Elischer 	 */
1232f97e0a07SJulian Elischer 	LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1233f97e0a07SJulian Elischer 
12341a3d1be4SGleb Smirnoff 	SOCK_RECVBUF_LOCK(so);
123574fb0ba7SJohn Baldwin 	soupcall_set(so, SO_RCV, ng_ksocket_incoming, node);
12361a3d1be4SGleb Smirnoff 	SOCK_RECVBUF_UNLOCK(so);
12371a3d1be4SGleb Smirnoff 	SOCK_SENDBUF_LOCK(so);
1238f9e4dd71SFabien Thomas 	soupcall_set(so, SO_SND, ng_ksocket_incoming, node);
12391a3d1be4SGleb Smirnoff 	SOCK_SENDBUF_UNLOCK(so);
1240f97e0a07SJulian Elischer 
1241f97e0a07SJulian Elischer 	/* Fill in the response data and send it or return it to the caller */
1242f97e0a07SJulian Elischer 	resp_data = (struct ng_ksocket_accept *)resp->data;
1243f97e0a07SJulian Elischer 	resp_data->nodeid = NG_NODE_ID(node);
1244cfb1e929SGleb Smirnoff 	bcopy(&ss, &resp_data->addr, ss.ss_len);
1245facfd889SArchie Cobbs 	NG_SEND_MSG_ID(error, node, resp, priv->response_addr, 0);
1246f97e0a07SJulian Elischer 
1247f97e0a07SJulian Elischer out:
1248779f106aSGleb Smirnoff 
1249779f106aSGleb Smirnoff 	return (0);
1250f97e0a07SJulian Elischer }
1251f97e0a07SJulian Elischer 
125243f7e216SGleb Smirnoff static int
ng_ksocket_listen_upcall(struct socket * so,void * arg,int waitflag)125343f7e216SGleb Smirnoff ng_ksocket_listen_upcall(struct socket *so, void *arg, int waitflag)
125443f7e216SGleb Smirnoff {
125543f7e216SGleb Smirnoff 	priv_p priv = arg;
125643f7e216SGleb Smirnoff 	int wait = ((waitflag & M_WAITOK) ? NG_WAITOK : 0) | NG_QUEUE;
125743f7e216SGleb Smirnoff 
125843f7e216SGleb Smirnoff 	ng_send_fn1(priv->node, NULL, &ng_ksocket_listen_upcall2, priv, 0,
125943f7e216SGleb Smirnoff 	    wait);
126043f7e216SGleb Smirnoff 	return (SU_OK);
126143f7e216SGleb Smirnoff }
126243f7e216SGleb Smirnoff 
126343f7e216SGleb Smirnoff static void
ng_ksocket_listen_upcall2(node_p node,hook_p hook,void * arg1,int arg2)126443f7e216SGleb Smirnoff ng_ksocket_listen_upcall2(node_p node, hook_p hook, void *arg1, int arg2)
126543f7e216SGleb Smirnoff {
126643f7e216SGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
126743f7e216SGleb Smirnoff 
126843f7e216SGleb Smirnoff 	(void )ng_ksocket_accept(priv);
126943f7e216SGleb Smirnoff }
127043f7e216SGleb Smirnoff 
1271f97e0a07SJulian Elischer /*
1272cb3c7a5dSArchie Cobbs  * Parse out either an integer value or an alias.
1273cb3c7a5dSArchie Cobbs  */
1274cb3c7a5dSArchie Cobbs static int
ng_ksocket_parse(const struct ng_ksocket_alias * aliases,const char * s,int family)1275cb3c7a5dSArchie Cobbs ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1276cb3c7a5dSArchie Cobbs 	const char *s, int family)
1277cb3c7a5dSArchie Cobbs {
1278cb3c7a5dSArchie Cobbs 	int k, val;
127925792ef3SArchie Cobbs 	char *eptr;
1280cb3c7a5dSArchie Cobbs 
1281cb3c7a5dSArchie Cobbs 	/* Try aliases */
1282cb3c7a5dSArchie Cobbs 	for (k = 0; aliases[k].name != NULL; k++) {
1283cb3c7a5dSArchie Cobbs 		if (strcmp(s, aliases[k].name) == 0
1284cb3c7a5dSArchie Cobbs 		    && aliases[k].family == family)
1285cb3c7a5dSArchie Cobbs 			return aliases[k].value;
1286cb3c7a5dSArchie Cobbs 	}
1287cb3c7a5dSArchie Cobbs 
1288cb3c7a5dSArchie Cobbs 	/* Try parsing as a number */
1289cb3c7a5dSArchie Cobbs 	val = (int)strtoul(s, &eptr, 10);
1290f8307e12SArchie Cobbs 	if (val < 0 || *eptr != '\0')
1291cb3c7a5dSArchie Cobbs 		return (-1);
1292cb3c7a5dSArchie Cobbs 	return (val);
1293cb3c7a5dSArchie Cobbs }
1294