xref: /freebsd/sys/netgraph/ng_ksocket.c (revision 069154d55f4e6cb7243ee37c5aa0391aa639e721)
1cb3c7a5dSArchie Cobbs 
2cb3c7a5dSArchie Cobbs /*
3cb3c7a5dSArchie Cobbs  * ng_ksocket.c
4cb3c7a5dSArchie Cobbs  *
5cb3c7a5dSArchie Cobbs  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6cb3c7a5dSArchie Cobbs  * All rights reserved.
7cb3c7a5dSArchie Cobbs  *
8cb3c7a5dSArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
9cb3c7a5dSArchie Cobbs  * redistribution of this software, in source or object code forms, with or
10cb3c7a5dSArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
11cb3c7a5dSArchie Cobbs  * provided, however, that:
12cb3c7a5dSArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
13cb3c7a5dSArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
14cb3c7a5dSArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
15cb3c7a5dSArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
16cb3c7a5dSArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17cb3c7a5dSArchie Cobbs  *    such appears in the above copyright notice or in the software.
18cb3c7a5dSArchie Cobbs  *
19cb3c7a5dSArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20cb3c7a5dSArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21cb3c7a5dSArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22cb3c7a5dSArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23cb3c7a5dSArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24cb3c7a5dSArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25cb3c7a5dSArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26cb3c7a5dSArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27cb3c7a5dSArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28cb3c7a5dSArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29cb3c7a5dSArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30cb3c7a5dSArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31cb3c7a5dSArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32cb3c7a5dSArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33cb3c7a5dSArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34cb3c7a5dSArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35cb3c7a5dSArchie Cobbs  * OF SUCH DAMAGE.
36cb3c7a5dSArchie Cobbs  *
37cc3bbd68SJulian Elischer  * Author: Archie Cobbs <archie@freebsd.org>
38cb3c7a5dSArchie Cobbs  *
39cb3c7a5dSArchie Cobbs  * $FreeBSD$
40cb3c7a5dSArchie Cobbs  * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $
41cb3c7a5dSArchie Cobbs  */
42cb3c7a5dSArchie Cobbs 
43cb3c7a5dSArchie Cobbs /*
44cb3c7a5dSArchie Cobbs  * Kernel socket node type.  This node type is basically a kernel-mode
45cb3c7a5dSArchie Cobbs  * version of a socket... kindof like the reverse of the socket node type.
46cb3c7a5dSArchie Cobbs  */
47cb3c7a5dSArchie Cobbs 
48cb3c7a5dSArchie Cobbs #include <sys/param.h>
49cb3c7a5dSArchie Cobbs #include <sys/systm.h>
50cb3c7a5dSArchie Cobbs #include <sys/kernel.h>
51cb3c7a5dSArchie Cobbs #include <sys/mbuf.h>
52cb3c7a5dSArchie Cobbs #include <sys/proc.h>
53cb3c7a5dSArchie Cobbs #include <sys/malloc.h>
54f8307e12SArchie Cobbs #include <sys/ctype.h>
55cb3c7a5dSArchie Cobbs #include <sys/protosw.h>
56cb3c7a5dSArchie Cobbs #include <sys/errno.h>
57cb3c7a5dSArchie Cobbs #include <sys/socket.h>
58cb3c7a5dSArchie Cobbs #include <sys/socketvar.h>
59cb3c7a5dSArchie Cobbs #include <sys/uio.h>
60f8307e12SArchie Cobbs #include <sys/un.h>
61cb3c7a5dSArchie Cobbs 
62cb3c7a5dSArchie Cobbs #include <netgraph/ng_message.h>
63cb3c7a5dSArchie Cobbs #include <netgraph/netgraph.h>
64f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
65cb3c7a5dSArchie Cobbs #include <netgraph/ng_ksocket.h>
66cb3c7a5dSArchie Cobbs 
67cb3c7a5dSArchie Cobbs #include <netinet/in.h>
68cb3c7a5dSArchie Cobbs #include <netatalk/at.h>
69cb3c7a5dSArchie Cobbs 
70f8307e12SArchie Cobbs #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
71f8307e12SArchie Cobbs #define SADATA_OFFSET	(OFFSETOF(struct sockaddr, sa_data))
72f8307e12SArchie Cobbs 
73cb3c7a5dSArchie Cobbs /* Node private data */
7419bff684SArchie Cobbs struct ng_ksocket_private {
75cb3c7a5dSArchie Cobbs 	hook_p		hook;
76cb3c7a5dSArchie Cobbs 	struct socket	*so;
77cb3c7a5dSArchie Cobbs };
7819bff684SArchie Cobbs typedef struct ng_ksocket_private *priv_p;
79cb3c7a5dSArchie Cobbs 
80cb3c7a5dSArchie Cobbs /* Netgraph node methods */
81cb3c7a5dSArchie Cobbs static ng_constructor_t	ng_ksocket_constructor;
82cb3c7a5dSArchie Cobbs static ng_rcvmsg_t	ng_ksocket_rcvmsg;
83069154d5SJulian Elischer static ng_shutdown_t	ng_ksocket_shutdown;
84cb3c7a5dSArchie Cobbs static ng_newhook_t	ng_ksocket_newhook;
85cb3c7a5dSArchie Cobbs static ng_rcvdata_t	ng_ksocket_rcvdata;
86cb3c7a5dSArchie Cobbs static ng_disconnect_t	ng_ksocket_disconnect;
87cb3c7a5dSArchie Cobbs 
88cb3c7a5dSArchie Cobbs /* Alias structure */
89cb3c7a5dSArchie Cobbs struct ng_ksocket_alias {
90cb3c7a5dSArchie Cobbs 	const char	*name;
91cb3c7a5dSArchie Cobbs 	const int	value;
92cb3c7a5dSArchie Cobbs 	const int	family;
93cb3c7a5dSArchie Cobbs };
94cb3c7a5dSArchie Cobbs 
95cb3c7a5dSArchie Cobbs /* Protocol family aliases */
96cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_families[] = {
97cb3c7a5dSArchie Cobbs 	{ "local",	PF_LOCAL	},
98cb3c7a5dSArchie Cobbs 	{ "inet",	PF_INET		},
99cb3c7a5dSArchie Cobbs 	{ "inet6",	PF_INET6	},
100cb3c7a5dSArchie Cobbs 	{ "atalk",	PF_APPLETALK	},
101cb3c7a5dSArchie Cobbs 	{ "ipx",	PF_IPX		},
102cb3c7a5dSArchie Cobbs 	{ "atm",	PF_ATM		},
103cb3c7a5dSArchie Cobbs 	{ NULL,		-1		},
104cb3c7a5dSArchie Cobbs };
105cb3c7a5dSArchie Cobbs 
106cb3c7a5dSArchie Cobbs /* Socket type aliases */
107cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_types[] = {
108cb3c7a5dSArchie Cobbs 	{ "stream",	SOCK_STREAM	},
109cb3c7a5dSArchie Cobbs 	{ "dgram",	SOCK_DGRAM	},
110cb3c7a5dSArchie Cobbs 	{ "raw",	SOCK_RAW	},
111cb3c7a5dSArchie Cobbs 	{ "rdm",	SOCK_RDM	},
112cb3c7a5dSArchie Cobbs 	{ "seqpacket",	SOCK_SEQPACKET	},
113cb3c7a5dSArchie Cobbs 	{ NULL,		-1		},
114cb3c7a5dSArchie Cobbs };
115cb3c7a5dSArchie Cobbs 
116cb3c7a5dSArchie Cobbs /* Protocol aliases */
117cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_protos[] = {
118cb3c7a5dSArchie Cobbs 	{ "ip",		IPPROTO_IP,		PF_INET		},
119cb3c7a5dSArchie Cobbs 	{ "raw",	IPPROTO_IP,		PF_INET		},
120cb3c7a5dSArchie Cobbs 	{ "icmp",	IPPROTO_ICMP,		PF_INET		},
121cb3c7a5dSArchie Cobbs 	{ "igmp",	IPPROTO_IGMP,		PF_INET		},
122cb3c7a5dSArchie Cobbs 	{ "tcp",	IPPROTO_TCP,		PF_INET		},
123cb3c7a5dSArchie Cobbs 	{ "udp",	IPPROTO_UDP,		PF_INET		},
124cb3c7a5dSArchie Cobbs 	{ "gre",	IPPROTO_GRE,		PF_INET		},
125cb3c7a5dSArchie Cobbs 	{ "esp",	IPPROTO_ESP,		PF_INET		},
126cb3c7a5dSArchie Cobbs 	{ "ah",		IPPROTO_AH,		PF_INET		},
127cb3c7a5dSArchie Cobbs 	{ "swipe",	IPPROTO_SWIPE,		PF_INET		},
128cb3c7a5dSArchie Cobbs 	{ "encap",	IPPROTO_ENCAP,		PF_INET		},
129cb3c7a5dSArchie Cobbs 	{ "divert",	IPPROTO_DIVERT,		PF_INET		},
130cb3c7a5dSArchie Cobbs 	{ "ddp",	ATPROTO_DDP,		PF_APPLETALK	},
131cb3c7a5dSArchie Cobbs 	{ "aarp",	ATPROTO_AARP,		PF_APPLETALK	},
132cb3c7a5dSArchie Cobbs 	{ NULL,		-1					},
133cb3c7a5dSArchie Cobbs };
134cb3c7a5dSArchie Cobbs 
135f8307e12SArchie Cobbs /* Helper functions */
136f8307e12SArchie Cobbs static void	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
137f8307e12SArchie Cobbs static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
138f8307e12SArchie Cobbs 			const char *s, int family);
139f8307e12SArchie Cobbs 
140f8307e12SArchie Cobbs /************************************************************************
141f8307e12SArchie Cobbs 			STRUCT SOCKADDR PARSE TYPE
142f8307e12SArchie Cobbs  ************************************************************************/
143f8307e12SArchie Cobbs 
144f8307e12SArchie Cobbs /* Get the length of the data portion of a generic struct sockaddr */
145f8307e12SArchie Cobbs static int
146f8307e12SArchie Cobbs ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
147f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
148f8307e12SArchie Cobbs {
149f8307e12SArchie Cobbs 	const struct sockaddr *sa;
150f8307e12SArchie Cobbs 
151f8307e12SArchie Cobbs 	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
1521baeddb8SArchie Cobbs 	return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
153f8307e12SArchie Cobbs }
154f8307e12SArchie Cobbs 
155f8307e12SArchie Cobbs /* Type for the variable length data portion of a generic struct sockaddr */
156f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
157f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
158f8307e12SArchie Cobbs 	&ng_parse_generic_sockdata_getLength
159f8307e12SArchie Cobbs };
160f8307e12SArchie Cobbs 
161f8307e12SArchie Cobbs /* Type for a generic struct sockaddr */
162f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_parse_generic_sockaddr_type_info = {
163f8307e12SArchie Cobbs 	{
16457b57be3SArchie Cobbs 	  { "len",	&ng_parse_uint8_type			},
16557b57be3SArchie Cobbs 	  { "family",	&ng_parse_uint8_type			},
166f8307e12SArchie Cobbs 	  { "data",	&ng_ksocket_generic_sockdata_type	},
167f8307e12SArchie Cobbs 	  { NULL }
168f8307e12SArchie Cobbs 	}
169f8307e12SArchie Cobbs };
170f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
171f8307e12SArchie Cobbs 	&ng_parse_struct_type,
172f8307e12SArchie Cobbs 	&ng_parse_generic_sockaddr_type_info
173f8307e12SArchie Cobbs };
174f8307e12SArchie Cobbs 
175f8307e12SArchie Cobbs /* Convert a struct sockaddr from ASCII to binary.  If its a protocol
176f8307e12SArchie Cobbs    family that we specially handle, do that, otherwise defer to the
177f8307e12SArchie Cobbs    generic parse type ng_ksocket_generic_sockaddr_type. */
178f8307e12SArchie Cobbs static int
179f8307e12SArchie Cobbs ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
180f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
181f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
182f8307e12SArchie Cobbs {
183f8307e12SArchie Cobbs 	struct sockaddr *const sa = (struct sockaddr *)buf;
184f8307e12SArchie Cobbs 	enum ng_parse_token tok;
185f8307e12SArchie Cobbs 	char fambuf[32];
186f8307e12SArchie Cobbs 	int family, len;
187f8307e12SArchie Cobbs 	char *t;
188f8307e12SArchie Cobbs 
189f8307e12SArchie Cobbs 	/* If next token is a left curly brace, use generic parse type */
190f8307e12SArchie Cobbs 	if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
191f8307e12SArchie Cobbs 		return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
192f8307e12SArchie Cobbs 		    (&ng_ksocket_generic_sockaddr_type,
193f8307e12SArchie Cobbs 		    s, off, start, buf, buflen);
194f8307e12SArchie Cobbs 	}
195f8307e12SArchie Cobbs 
196f8307e12SArchie Cobbs 	/* Get socket address family followed by a slash */
197f8307e12SArchie Cobbs 	while (isspace(s[*off]))
198f8307e12SArchie Cobbs 		(*off)++;
199f8307e12SArchie Cobbs 	if ((t = index(s + *off, '/')) == NULL)
200f8307e12SArchie Cobbs 		return (EINVAL);
201f8307e12SArchie Cobbs 	if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
202f8307e12SArchie Cobbs 		return (EINVAL);
203f8307e12SArchie Cobbs 	strncpy(fambuf, s + *off, len);
204f8307e12SArchie Cobbs 	fambuf[len] = '\0';
205f8307e12SArchie Cobbs 	*off += len + 1;
206f8307e12SArchie Cobbs 	if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
207f8307e12SArchie Cobbs 		return (EINVAL);
208f8307e12SArchie Cobbs 
209f8307e12SArchie Cobbs 	/* Set family */
210f8307e12SArchie Cobbs 	if (*buflen < SADATA_OFFSET)
211f8307e12SArchie Cobbs 		return (ERANGE);
212f8307e12SArchie Cobbs 	sa->sa_family = family;
213f8307e12SArchie Cobbs 
214f8307e12SArchie Cobbs 	/* Set family-specific data and length */
215f8307e12SArchie Cobbs 	switch (sa->sa_family) {
216f8307e12SArchie Cobbs 	case PF_LOCAL:		/* Get pathname */
217f8307e12SArchie Cobbs 	    {
218f8307e12SArchie Cobbs 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
219f8307e12SArchie Cobbs 		struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
220f8307e12SArchie Cobbs 		int toklen, pathlen;
221f8307e12SArchie Cobbs 		char *path;
222f8307e12SArchie Cobbs 
22327121ab1SBrian Somers 		if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL)
224f8307e12SArchie Cobbs 			return (EINVAL);
225f8307e12SArchie Cobbs 		pathlen = strlen(path);
226f8307e12SArchie Cobbs 		if (pathlen > SOCK_MAXADDRLEN) {
227f8307e12SArchie Cobbs 			FREE(path, M_NETGRAPH);
228f8307e12SArchie Cobbs 			return (E2BIG);
229f8307e12SArchie Cobbs 		}
230f8307e12SArchie Cobbs 		if (*buflen < pathoff + pathlen) {
231f8307e12SArchie Cobbs 			FREE(path, M_NETGRAPH);
232f8307e12SArchie Cobbs 			return (ERANGE);
233f8307e12SArchie Cobbs 		}
234f8307e12SArchie Cobbs 		*off += toklen;
235f8307e12SArchie Cobbs 		bcopy(path, sun->sun_path, pathlen);
236f8307e12SArchie Cobbs 		sun->sun_len = pathoff + pathlen;
237f8307e12SArchie Cobbs 		FREE(path, M_NETGRAPH);
238f8307e12SArchie Cobbs 		break;
239f8307e12SArchie Cobbs 	    }
240f8307e12SArchie Cobbs 
241f8307e12SArchie Cobbs 	case PF_INET:		/* Get an IP address with optional port */
242f8307e12SArchie Cobbs 	    {
243f8307e12SArchie Cobbs 		struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
244f8307e12SArchie Cobbs 		int i;
245f8307e12SArchie Cobbs 
246f8307e12SArchie Cobbs 		/* Parse this: <ipaddress>[:port] */
247f8307e12SArchie Cobbs 		for (i = 0; i < 4; i++) {
248f8307e12SArchie Cobbs 			u_long val;
249f8307e12SArchie Cobbs 			char *eptr;
250f8307e12SArchie Cobbs 
251f8307e12SArchie Cobbs 			val = strtoul(s + *off, &eptr, 10);
252f8307e12SArchie Cobbs 			if (val > 0xff || eptr == s + *off)
253f8307e12SArchie Cobbs 				return (EINVAL);
254f8307e12SArchie Cobbs 			*off += (eptr - (s + *off));
255f8307e12SArchie Cobbs 			((u_char *)&sin->sin_addr)[i] = (u_char)val;
256f8307e12SArchie Cobbs 			if (i < 3) {
257f8307e12SArchie Cobbs 				if (s[*off] != '.')
258f8307e12SArchie Cobbs 					return (EINVAL);
259f8307e12SArchie Cobbs 				(*off)++;
260f8307e12SArchie Cobbs 			} else if (s[*off] == ':') {
261f8307e12SArchie Cobbs 				(*off)++;
262f8307e12SArchie Cobbs 				val = strtoul(s + *off, &eptr, 10);
263f8307e12SArchie Cobbs 				if (val > 0xffff || eptr == s + *off)
264f8307e12SArchie Cobbs 					return (EINVAL);
265f8307e12SArchie Cobbs 				*off += (eptr - (s + *off));
266f8307e12SArchie Cobbs 				sin->sin_port = htons(val);
267f8307e12SArchie Cobbs 			} else
268f8307e12SArchie Cobbs 				sin->sin_port = 0;
269f8307e12SArchie Cobbs 		}
270f8307e12SArchie Cobbs 		bzero(&sin->sin_zero, sizeof(sin->sin_zero));
271f8307e12SArchie Cobbs 		sin->sin_len = sizeof(*sin);
272f8307e12SArchie Cobbs 		break;
273f8307e12SArchie Cobbs 	    }
274f8307e12SArchie Cobbs 
275f8307e12SArchie Cobbs #if 0
276f8307e12SArchie Cobbs 	case PF_APPLETALK:	/* XXX implement these someday */
277f8307e12SArchie Cobbs 	case PF_INET6:
278f8307e12SArchie Cobbs 	case PF_IPX:
279f8307e12SArchie Cobbs #endif
280f8307e12SArchie Cobbs 
281f8307e12SArchie Cobbs 	default:
282f8307e12SArchie Cobbs 		return (EINVAL);
283f8307e12SArchie Cobbs 	}
284f8307e12SArchie Cobbs 
285f8307e12SArchie Cobbs 	/* Done */
286f8307e12SArchie Cobbs 	*buflen = sa->sa_len;
287f8307e12SArchie Cobbs 	return (0);
288f8307e12SArchie Cobbs }
289f8307e12SArchie Cobbs 
290f8307e12SArchie Cobbs /* Convert a struct sockaddr from binary to ASCII */
291f8307e12SArchie Cobbs static int
292f8307e12SArchie Cobbs ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
293f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
294f8307e12SArchie Cobbs {
295f8307e12SArchie Cobbs 	const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
296f8307e12SArchie Cobbs 	int slen = 0;
297f8307e12SArchie Cobbs 
298f8307e12SArchie Cobbs 	/* Output socket address, either in special or generic format */
299f8307e12SArchie Cobbs 	switch (sa->sa_family) {
300f8307e12SArchie Cobbs 	case PF_LOCAL:
301f8307e12SArchie Cobbs 	    {
302f8307e12SArchie Cobbs 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
303f8307e12SArchie Cobbs 		const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
304f8307e12SArchie Cobbs 		const int pathlen = sun->sun_len - pathoff;
305f8307e12SArchie Cobbs 		char pathbuf[SOCK_MAXADDRLEN + 1];
306f8307e12SArchie Cobbs 		char *pathtoken;
307f8307e12SArchie Cobbs 
308f8307e12SArchie Cobbs 		bcopy(sun->sun_path, pathbuf, pathlen);
30927121ab1SBrian Somers 		if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL)
310f8307e12SArchie Cobbs 			return (ENOMEM);
311f8307e12SArchie Cobbs 		slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken);
312f8307e12SArchie Cobbs 		FREE(pathtoken, M_NETGRAPH);
313f8307e12SArchie Cobbs 		if (slen >= cbuflen)
314f8307e12SArchie Cobbs 			return (ERANGE);
315f8307e12SArchie Cobbs 		*off += sun->sun_len;
316f8307e12SArchie Cobbs 		return (0);
317f8307e12SArchie Cobbs 	    }
318f8307e12SArchie Cobbs 
319f8307e12SArchie Cobbs 	case PF_INET:
320f8307e12SArchie Cobbs 	    {
321f8307e12SArchie Cobbs 		const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
322f8307e12SArchie Cobbs 
323f8307e12SArchie Cobbs 		slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
324f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[0],
325f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[1],
326f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[2],
327f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[3]);
328f8307e12SArchie Cobbs 		if (sin->sin_port != 0) {
329f8307e12SArchie Cobbs 			slen += snprintf(cbuf + strlen(cbuf),
330f8307e12SArchie Cobbs 			    cbuflen - strlen(cbuf), ":%d",
331f8307e12SArchie Cobbs 			    (u_int)ntohs(sin->sin_port));
332f8307e12SArchie Cobbs 		}
333f8307e12SArchie Cobbs 		if (slen >= cbuflen)
334f8307e12SArchie Cobbs 			return (ERANGE);
335f8307e12SArchie Cobbs 		*off += sizeof(*sin);
336f8307e12SArchie Cobbs 		return(0);
337f8307e12SArchie Cobbs 	    }
338f8307e12SArchie Cobbs 
339f8307e12SArchie Cobbs #if 0
340f8307e12SArchie Cobbs 	case PF_APPLETALK:	/* XXX implement these someday */
341f8307e12SArchie Cobbs 	case PF_INET6:
342f8307e12SArchie Cobbs 	case PF_IPX:
343f8307e12SArchie Cobbs #endif
344f8307e12SArchie Cobbs 
345f8307e12SArchie Cobbs 	default:
346f8307e12SArchie Cobbs 		return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
347f8307e12SArchie Cobbs 		    (&ng_ksocket_generic_sockaddr_type,
348f8307e12SArchie Cobbs 		    data, off, cbuf, cbuflen);
349f8307e12SArchie Cobbs 	}
350f8307e12SArchie Cobbs }
351f8307e12SArchie Cobbs 
352f8307e12SArchie Cobbs /* Parse type for struct sockaddr */
353f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockaddr_type = {
354f8307e12SArchie Cobbs 	NULL,
355f8307e12SArchie Cobbs 	NULL,
356f8307e12SArchie Cobbs 	NULL,
357f8307e12SArchie Cobbs 	&ng_ksocket_sockaddr_parse,
358f8307e12SArchie Cobbs 	&ng_ksocket_sockaddr_unparse,
359f8307e12SArchie Cobbs 	NULL		/* no such thing as a default struct sockaddr */
360f8307e12SArchie Cobbs };
361f8307e12SArchie Cobbs 
362f8307e12SArchie Cobbs /************************************************************************
363f8307e12SArchie Cobbs 		STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
364f8307e12SArchie Cobbs  ************************************************************************/
365f8307e12SArchie Cobbs 
366f8307e12SArchie Cobbs /* Get length of the struct ng_ksocket_sockopt value field, which is the
367f8307e12SArchie Cobbs    just the excess of the message argument portion over the length of
368f8307e12SArchie Cobbs    the struct ng_ksocket_sockopt. */
369f8307e12SArchie Cobbs static int
370f8307e12SArchie Cobbs ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
371f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
372f8307e12SArchie Cobbs {
373f8307e12SArchie Cobbs 	static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
374f8307e12SArchie Cobbs 	const struct ng_ksocket_sockopt *sopt;
375f8307e12SArchie Cobbs 	const struct ng_mesg *msg;
376f8307e12SArchie Cobbs 
377f8307e12SArchie Cobbs 	sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
378f8307e12SArchie Cobbs 	msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
379f8307e12SArchie Cobbs 	return msg->header.arglen - sizeof(*sopt);
380f8307e12SArchie Cobbs }
381f8307e12SArchie Cobbs 
382f8307e12SArchie Cobbs /* Parse type for the option value part of a struct ng_ksocket_sockopt
383f8307e12SArchie Cobbs    XXX Eventually, we should handle the different socket options specially.
384f8307e12SArchie Cobbs    XXX This would avoid byte order problems, eg an integer value of 1 is
385f8307e12SArchie Cobbs    XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
386f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockoptval_type = {
387f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
388f8307e12SArchie Cobbs 	&ng_parse_sockoptval_getLength
389f8307e12SArchie Cobbs };
390f8307e12SArchie Cobbs 
391f8307e12SArchie Cobbs /* Parse type for struct ng_ksocket_sockopt */
392f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_ksocket_sockopt_type_info
393f8307e12SArchie Cobbs 	= NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
394f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockopt_type = {
395f8307e12SArchie Cobbs 	&ng_parse_struct_type,
396f8307e12SArchie Cobbs 	&ng_ksocket_sockopt_type_info,
397f8307e12SArchie Cobbs };
398f8307e12SArchie Cobbs 
399f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
400f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ksocket_cmds[] = {
401f8307e12SArchie Cobbs 	{
402f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
403f8307e12SArchie Cobbs 	  NGM_KSOCKET_BIND,
404f8307e12SArchie Cobbs 	  "bind",
405f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type,
406f8307e12SArchie Cobbs 	  NULL
407f8307e12SArchie Cobbs 	},
408f8307e12SArchie Cobbs 	{
409f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
410f8307e12SArchie Cobbs 	  NGM_KSOCKET_LISTEN,
411f8307e12SArchie Cobbs 	  "listen",
412f8307e12SArchie Cobbs 	  &ng_parse_int32_type,
413f8307e12SArchie Cobbs 	  NULL
414f8307e12SArchie Cobbs 	},
415f8307e12SArchie Cobbs 	{
416f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
417f8307e12SArchie Cobbs 	  NGM_KSOCKET_ACCEPT,
418f8307e12SArchie Cobbs 	  "accept",
419f8307e12SArchie Cobbs 	  NULL,
420f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
421f8307e12SArchie Cobbs 	},
422f8307e12SArchie Cobbs 	{
423f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
424f8307e12SArchie Cobbs 	  NGM_KSOCKET_CONNECT,
425f8307e12SArchie Cobbs 	  "connect",
426f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type,
427f8307e12SArchie Cobbs 	  NULL
428f8307e12SArchie Cobbs 	},
429f8307e12SArchie Cobbs 	{
430f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
431f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETNAME,
432f8307e12SArchie Cobbs 	  "getname",
433f8307e12SArchie Cobbs 	  NULL,
434f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
435f8307e12SArchie Cobbs 	},
436f8307e12SArchie Cobbs 	{
437f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
438f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETPEERNAME,
439f8307e12SArchie Cobbs 	  "getpeername",
440f8307e12SArchie Cobbs 	  NULL,
441f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
442f8307e12SArchie Cobbs 	},
443f8307e12SArchie Cobbs 	{
444f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
445f8307e12SArchie Cobbs 	  NGM_KSOCKET_SETOPT,
446f8307e12SArchie Cobbs 	  "setopt",
447f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type,
448f8307e12SArchie Cobbs 	  NULL
449f8307e12SArchie Cobbs 	},
450f8307e12SArchie Cobbs 	{
451f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
452f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETOPT,
453f8307e12SArchie Cobbs 	  "getopt",
454f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type,
455f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type
456f8307e12SArchie Cobbs 	},
457f8307e12SArchie Cobbs 	{ 0 }
458f8307e12SArchie Cobbs };
459f8307e12SArchie Cobbs 
460f8307e12SArchie Cobbs /* Node type descriptor */
461f8307e12SArchie Cobbs static struct ng_type ng_ksocket_typestruct = {
462589f6ed8SJulian Elischer 	NG_ABI_VERSION,
463f8307e12SArchie Cobbs 	NG_KSOCKET_NODE_TYPE,
464f8307e12SArchie Cobbs 	NULL,
465f8307e12SArchie Cobbs 	ng_ksocket_constructor,
466f8307e12SArchie Cobbs 	ng_ksocket_rcvmsg,
467069154d5SJulian Elischer 	ng_ksocket_shutdown,
468f8307e12SArchie Cobbs 	ng_ksocket_newhook,
469f8307e12SArchie Cobbs 	NULL,
470f8307e12SArchie Cobbs 	NULL,
471f8307e12SArchie Cobbs 	ng_ksocket_rcvdata,
472f8307e12SArchie Cobbs 	ng_ksocket_disconnect,
473f8307e12SArchie Cobbs 	ng_ksocket_cmds
474f8307e12SArchie Cobbs };
475f8307e12SArchie Cobbs NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
476f8307e12SArchie Cobbs 
477cb3c7a5dSArchie Cobbs #define ERROUT(x)	do { error = (x); goto done; } while (0)
478cb3c7a5dSArchie Cobbs 
479cb3c7a5dSArchie Cobbs /************************************************************************
480cb3c7a5dSArchie Cobbs 			NETGRAPH NODE STUFF
481cb3c7a5dSArchie Cobbs  ************************************************************************/
482cb3c7a5dSArchie Cobbs 
483cb3c7a5dSArchie Cobbs /*
484cb3c7a5dSArchie Cobbs  * Node type constructor
485cb3c7a5dSArchie Cobbs  */
486cb3c7a5dSArchie Cobbs static int
487069154d5SJulian Elischer ng_ksocket_constructor(node_p node)
488cb3c7a5dSArchie Cobbs {
489cb3c7a5dSArchie Cobbs 	priv_p priv;
490cb3c7a5dSArchie Cobbs 
491cb3c7a5dSArchie Cobbs 	/* Allocate private structure */
49299cdf4ccSDavid Malone 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
493cb3c7a5dSArchie Cobbs 	if (priv == NULL)
494cb3c7a5dSArchie Cobbs 		return (ENOMEM);
495cb3c7a5dSArchie Cobbs 
496069154d5SJulian Elischer 	node->private = priv;
497cb3c7a5dSArchie Cobbs 
498cb3c7a5dSArchie Cobbs 	/* Done */
499cb3c7a5dSArchie Cobbs 	return (0);
500cb3c7a5dSArchie Cobbs }
501cb3c7a5dSArchie Cobbs 
502cb3c7a5dSArchie Cobbs /*
503cb3c7a5dSArchie Cobbs  * Give our OK for a hook to be added. The hook name is of the
504cb3c7a5dSArchie Cobbs  * form "<family>:<type>:<proto>" where the three components may
505cb3c7a5dSArchie Cobbs  * be decimal numbers or else aliases from the above lists.
506cb3c7a5dSArchie Cobbs  *
507cb3c7a5dSArchie Cobbs  * Connecting a hook amounts to opening the socket.  Disconnecting
508cb3c7a5dSArchie Cobbs  * the hook closes the socket and destroys the node as well.
509cb3c7a5dSArchie Cobbs  */
510cb3c7a5dSArchie Cobbs static int
511cb3c7a5dSArchie Cobbs ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
512cb3c7a5dSArchie Cobbs {
513f8307e12SArchie Cobbs 	struct proc *p = curproc ? curproc : &proc0;	/* XXX broken */
514cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
515cb3c7a5dSArchie Cobbs 	char *s1, *s2, name[NG_HOOKLEN+1];
516cb3c7a5dSArchie Cobbs 	int family, type, protocol, error;
517cb3c7a5dSArchie Cobbs 
518cb3c7a5dSArchie Cobbs 	/* Check if we're already connected */
519cb3c7a5dSArchie Cobbs 	if (priv->hook != NULL)
520cb3c7a5dSArchie Cobbs 		return (EISCONN);
521cb3c7a5dSArchie Cobbs 
522cb3c7a5dSArchie Cobbs 	/* Extract family, type, and protocol from hook name */
523cb3c7a5dSArchie Cobbs 	snprintf(name, sizeof(name), "%s", name0);
524cb3c7a5dSArchie Cobbs 	s1 = name;
525cb3c7a5dSArchie Cobbs 	if ((s2 = index(s1, '/')) == NULL)
526cb3c7a5dSArchie Cobbs 		return (EINVAL);
527cb3c7a5dSArchie Cobbs 	*s2++ = '\0';
528cb3c7a5dSArchie Cobbs 	if ((family = ng_ksocket_parse(ng_ksocket_families, s1, 0)) == -1)
529cb3c7a5dSArchie Cobbs 		return (EINVAL);
530cb3c7a5dSArchie Cobbs 	s1 = s2;
531cb3c7a5dSArchie Cobbs 	if ((s2 = index(s1, '/')) == NULL)
532cb3c7a5dSArchie Cobbs 		return (EINVAL);
533cb3c7a5dSArchie Cobbs 	*s2++ = '\0';
534cb3c7a5dSArchie Cobbs 	if ((type = ng_ksocket_parse(ng_ksocket_types, s1, 0)) == -1)
535cb3c7a5dSArchie Cobbs 		return (EINVAL);
536cb3c7a5dSArchie Cobbs 	s1 = s2;
537cb3c7a5dSArchie Cobbs 	if ((protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family)) == -1)
538cb3c7a5dSArchie Cobbs 		return (EINVAL);
539cb3c7a5dSArchie Cobbs 
540cb3c7a5dSArchie Cobbs 	/* Create the socket */
541cb3c7a5dSArchie Cobbs 	if ((error = socreate(family, &priv->so, type, protocol, p)) != 0)
542cb3c7a5dSArchie Cobbs 		return (error);
543cb3c7a5dSArchie Cobbs 
544cb3c7a5dSArchie Cobbs 	/* XXX call soreserve() ? */
545cb3c7a5dSArchie Cobbs 
546cb3c7a5dSArchie Cobbs 	/* Add our hook for incoming data */
547cb3c7a5dSArchie Cobbs 	priv->so->so_upcallarg = (caddr_t)node;
548cb3c7a5dSArchie Cobbs 	priv->so->so_upcall = ng_ksocket_incoming;
549cb3c7a5dSArchie Cobbs 	priv->so->so_rcv.sb_flags |= SB_UPCALL;
550cb3c7a5dSArchie Cobbs 
551cb3c7a5dSArchie Cobbs 	/* OK */
552cb3c7a5dSArchie Cobbs 	priv->hook = hook;
553cb3c7a5dSArchie Cobbs 	return (0);
554cb3c7a5dSArchie Cobbs }
555cb3c7a5dSArchie Cobbs 
556cb3c7a5dSArchie Cobbs /*
557cb3c7a5dSArchie Cobbs  * Receive a control message
558cb3c7a5dSArchie Cobbs  */
559cb3c7a5dSArchie Cobbs static int
560069154d5SJulian Elischer ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook)
561cb3c7a5dSArchie Cobbs {
562f8307e12SArchie Cobbs 	struct proc *p = curproc ? curproc : &proc0;	/* XXX broken */
563cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
564f8307e12SArchie Cobbs 	struct socket *const so = priv->so;
565cb3c7a5dSArchie Cobbs 	struct ng_mesg *resp = NULL;
566cb3c7a5dSArchie Cobbs 	int error = 0;
567069154d5SJulian Elischer 	struct ng_mesg *msg;
568cb3c7a5dSArchie Cobbs 
569069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
570cb3c7a5dSArchie Cobbs 	switch (msg->header.typecookie) {
571cb3c7a5dSArchie Cobbs 	case NGM_KSOCKET_COOKIE:
572cb3c7a5dSArchie Cobbs 		switch (msg->header.cmd) {
573cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_BIND:
574cb3c7a5dSArchie Cobbs 		    {
575f8307e12SArchie Cobbs 			struct sockaddr *const sa
576f8307e12SArchie Cobbs 			    = (struct sockaddr *)msg->data;
577cb3c7a5dSArchie Cobbs 
578f8307e12SArchie Cobbs 			/* Sanity check */
579f8307e12SArchie Cobbs 			if (msg->header.arglen < SADATA_OFFSET
580f8307e12SArchie Cobbs 			    || msg->header.arglen < sa->sa_len)
581f8307e12SArchie Cobbs 				ERROUT(EINVAL);
582f8307e12SArchie Cobbs 			if (so == NULL)
583f8307e12SArchie Cobbs 				ERROUT(ENXIO);
584cb3c7a5dSArchie Cobbs 
585f8307e12SArchie Cobbs 			/* Bind */
586cb3c7a5dSArchie Cobbs 			error = sobind(so, sa, p);
587cb3c7a5dSArchie Cobbs 			break;
588cb3c7a5dSArchie Cobbs 		    }
589cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_LISTEN:
590cb3c7a5dSArchie Cobbs 		    {
591f8307e12SArchie Cobbs 			/* Sanity check */
592cb3c7a5dSArchie Cobbs 			if (msg->header.arglen != sizeof(int))
593cb3c7a5dSArchie Cobbs 				ERROUT(EINVAL);
594f8307e12SArchie Cobbs 			if (so == NULL)
595f8307e12SArchie Cobbs 				ERROUT(ENXIO);
596cb3c7a5dSArchie Cobbs 
597f8307e12SArchie Cobbs 			/* Listen */
598f8307e12SArchie Cobbs 			if ((error = solisten(so, *((int *)msg->data), p)) != 0)
599cb3c7a5dSArchie Cobbs 				break;
600cb3c7a5dSArchie Cobbs 
601cb3c7a5dSArchie Cobbs 			/* Notify sender when we get a connection attempt */
602cb3c7a5dSArchie Cobbs 				/* XXX implement me */
603f8307e12SArchie Cobbs 			error = ENODEV;
604cb3c7a5dSArchie Cobbs 			break;
605cb3c7a5dSArchie Cobbs 		    }
606cb3c7a5dSArchie Cobbs 
607cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_ACCEPT:
608cb3c7a5dSArchie Cobbs 		    {
609f8307e12SArchie Cobbs 			/* Sanity check */
610f8307e12SArchie Cobbs 			if (msg->header.arglen != 0)
611f8307e12SArchie Cobbs 				ERROUT(EINVAL);
612f8307e12SArchie Cobbs 			if (so == NULL)
613f8307e12SArchie Cobbs 				ERROUT(ENXIO);
614f8307e12SArchie Cobbs 
615f8307e12SArchie Cobbs 			/* Accept on the socket in a non-blocking way */
616f8307e12SArchie Cobbs 			/* Create a new ksocket node for the new connection */
617f8307e12SArchie Cobbs 			/* Return a response with the peer's sockaddr and
618f8307e12SArchie Cobbs 			   the absolute name of the newly created node */
619f8307e12SArchie Cobbs 
620f8307e12SArchie Cobbs 				/* XXX implement me */
621f8307e12SArchie Cobbs 
622f8307e12SArchie Cobbs 			error = ENODEV;
623cb3c7a5dSArchie Cobbs 			break;
624cb3c7a5dSArchie Cobbs 		    }
625cb3c7a5dSArchie Cobbs 
626cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_CONNECT:
627cb3c7a5dSArchie Cobbs 		    {
628f8307e12SArchie Cobbs 			struct sockaddr *const sa
629f8307e12SArchie Cobbs 			    = (struct sockaddr *)msg->data;
630cb3c7a5dSArchie Cobbs 
631f8307e12SArchie Cobbs 			/* Sanity check */
632f8307e12SArchie Cobbs 			if (msg->header.arglen < SADATA_OFFSET
633f8307e12SArchie Cobbs 			    || msg->header.arglen < sa->sa_len)
634f8307e12SArchie Cobbs 				ERROUT(EINVAL);
635f8307e12SArchie Cobbs 			if (so == NULL)
636f8307e12SArchie Cobbs 				ERROUT(ENXIO);
637cb3c7a5dSArchie Cobbs 
638cb3c7a5dSArchie Cobbs 			/* Do connect */
639cb3c7a5dSArchie Cobbs 			if ((so->so_state & SS_ISCONNECTING) != 0)
640cb3c7a5dSArchie Cobbs 				ERROUT(EALREADY);
641cb3c7a5dSArchie Cobbs 			if ((error = soconnect(so, sa, p)) != 0) {
642cb3c7a5dSArchie Cobbs 				so->so_state &= ~SS_ISCONNECTING;
643cb3c7a5dSArchie Cobbs 				ERROUT(error);
644cb3c7a5dSArchie Cobbs 			}
645cb3c7a5dSArchie Cobbs 			if ((so->so_state & SS_ISCONNECTING) != 0)
646cb3c7a5dSArchie Cobbs 				/* Notify sender when we connect */
647cb3c7a5dSArchie Cobbs 				/* XXX implement me */
648cb3c7a5dSArchie Cobbs 				ERROUT(EINPROGRESS);
649cb3c7a5dSArchie Cobbs 			break;
650cb3c7a5dSArchie Cobbs 		    }
651cb3c7a5dSArchie Cobbs 
652cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETNAME:
653cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETPEERNAME:
654cb3c7a5dSArchie Cobbs 		    {
655f8307e12SArchie Cobbs 			int (*func)(struct socket *so, struct sockaddr **nam);
656f8307e12SArchie Cobbs 			struct sockaddr *sa = NULL;
657f8307e12SArchie Cobbs 			int len;
658f8307e12SArchie Cobbs 
659f8307e12SArchie Cobbs 			/* Sanity check */
660f8307e12SArchie Cobbs 			if (msg->header.arglen != 0)
661f8307e12SArchie Cobbs 				ERROUT(EINVAL);
662f8307e12SArchie Cobbs 			if (so == NULL)
663f8307e12SArchie Cobbs 				ERROUT(ENXIO);
664f8307e12SArchie Cobbs 
665f8307e12SArchie Cobbs 			/* Get function */
666f8307e12SArchie Cobbs 			if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
667f8307e12SArchie Cobbs 				if ((so->so_state
668f8307e12SArchie Cobbs 				    & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
669f8307e12SArchie Cobbs 					ERROUT(ENOTCONN);
670f8307e12SArchie Cobbs 				func = so->so_proto->pr_usrreqs->pru_peeraddr;
671f8307e12SArchie Cobbs 			} else
672f8307e12SArchie Cobbs 				func = so->so_proto->pr_usrreqs->pru_sockaddr;
673f8307e12SArchie Cobbs 
674f8307e12SArchie Cobbs 			/* Get local or peer address */
675f8307e12SArchie Cobbs 			if ((error = (*func)(so, &sa)) != 0)
676f8307e12SArchie Cobbs 				goto bail;
677f8307e12SArchie Cobbs 			len = (sa == NULL) ? 0 : sa->sa_len;
678f8307e12SArchie Cobbs 
679f8307e12SArchie Cobbs 			/* Send it back in a response */
680f8307e12SArchie Cobbs 			NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
681f8307e12SArchie Cobbs 			if (resp == NULL) {
682f8307e12SArchie Cobbs 				error = ENOMEM;
683f8307e12SArchie Cobbs 				goto bail;
684f8307e12SArchie Cobbs 			}
685f8307e12SArchie Cobbs 			bcopy(sa, resp->data, len);
686f8307e12SArchie Cobbs 
687f8307e12SArchie Cobbs 		bail:
688f8307e12SArchie Cobbs 			/* Cleanup */
689f8307e12SArchie Cobbs 			if (sa != NULL)
690f8307e12SArchie Cobbs 				FREE(sa, M_SONAME);
691cb3c7a5dSArchie Cobbs 			break;
692cb3c7a5dSArchie Cobbs 		    }
693cb3c7a5dSArchie Cobbs 
694cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETOPT:
695cb3c7a5dSArchie Cobbs 		    {
696f8307e12SArchie Cobbs 			struct ng_ksocket_sockopt *ksopt =
697f8307e12SArchie Cobbs 			    (struct ng_ksocket_sockopt *)msg->data;
698f8307e12SArchie Cobbs 			struct sockopt sopt;
699f8307e12SArchie Cobbs 
700f8307e12SArchie Cobbs 			/* Sanity check */
701f8307e12SArchie Cobbs 			if (msg->header.arglen != sizeof(*ksopt))
702f8307e12SArchie Cobbs 				ERROUT(EINVAL);
703f8307e12SArchie Cobbs 			if (so == NULL)
704f8307e12SArchie Cobbs 				ERROUT(ENXIO);
705f8307e12SArchie Cobbs 
706f8307e12SArchie Cobbs 			/* Get response with room for option value */
707f8307e12SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
708f8307e12SArchie Cobbs 			    + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
709f8307e12SArchie Cobbs 			if (resp == NULL)
710f8307e12SArchie Cobbs 				ERROUT(ENOMEM);
711f8307e12SArchie Cobbs 
712f8307e12SArchie Cobbs 			/* Get socket option, and put value in the response */
713f8307e12SArchie Cobbs 			sopt.sopt_dir = SOPT_GET;
714f8307e12SArchie Cobbs 			sopt.sopt_level = ksopt->level;
715f8307e12SArchie Cobbs 			sopt.sopt_name = ksopt->name;
716f8307e12SArchie Cobbs 			sopt.sopt_p = p;
717f8307e12SArchie Cobbs 			sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
718f8307e12SArchie Cobbs 			ksopt = (struct ng_ksocket_sockopt *)resp->data;
719f8307e12SArchie Cobbs 			sopt.sopt_val = ksopt->value;
720f8307e12SArchie Cobbs 			if ((error = sogetopt(so, &sopt)) != 0) {
721069154d5SJulian Elischer 				NG_FREE_MSG(resp);
722f8307e12SArchie Cobbs 				break;
723f8307e12SArchie Cobbs 			}
724f8307e12SArchie Cobbs 
725f8307e12SArchie Cobbs 			/* Set actual value length */
726f8307e12SArchie Cobbs 			resp->header.arglen = sizeof(*ksopt)
727f8307e12SArchie Cobbs 			    + sopt.sopt_valsize;
728cb3c7a5dSArchie Cobbs 			break;
729cb3c7a5dSArchie Cobbs 		    }
730cb3c7a5dSArchie Cobbs 
731cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_SETOPT:
732cb3c7a5dSArchie Cobbs 		    {
733f8307e12SArchie Cobbs 			struct ng_ksocket_sockopt *const ksopt =
734f8307e12SArchie Cobbs 			    (struct ng_ksocket_sockopt *)msg->data;
735f8307e12SArchie Cobbs 			const int valsize = msg->header.arglen - sizeof(*ksopt);
736f8307e12SArchie Cobbs 			struct sockopt sopt;
737f8307e12SArchie Cobbs 
738f8307e12SArchie Cobbs 			/* Sanity check */
739f8307e12SArchie Cobbs 			if (valsize < 0)
740f8307e12SArchie Cobbs 				ERROUT(EINVAL);
741f8307e12SArchie Cobbs 			if (so == NULL)
742f8307e12SArchie Cobbs 				ERROUT(ENXIO);
743f8307e12SArchie Cobbs 
744f8307e12SArchie Cobbs 			/* Set socket option */
745f8307e12SArchie Cobbs 			sopt.sopt_dir = SOPT_SET;
746f8307e12SArchie Cobbs 			sopt.sopt_level = ksopt->level;
747f8307e12SArchie Cobbs 			sopt.sopt_name = ksopt->name;
748f8307e12SArchie Cobbs 			sopt.sopt_val = ksopt->value;
749f8307e12SArchie Cobbs 			sopt.sopt_valsize = valsize;
750f8307e12SArchie Cobbs 			sopt.sopt_p = p;
751f8307e12SArchie Cobbs 			error = sosetopt(so, &sopt);
752cb3c7a5dSArchie Cobbs 			break;
753cb3c7a5dSArchie Cobbs 		    }
754cb3c7a5dSArchie Cobbs 
755cb3c7a5dSArchie Cobbs 		default:
756cb3c7a5dSArchie Cobbs 			error = EINVAL;
757cb3c7a5dSArchie Cobbs 			break;
758cb3c7a5dSArchie Cobbs 		}
759cb3c7a5dSArchie Cobbs 		break;
760cb3c7a5dSArchie Cobbs 	default:
761cb3c7a5dSArchie Cobbs 		error = EINVAL;
762cb3c7a5dSArchie Cobbs 		break;
763cb3c7a5dSArchie Cobbs 	}
764cb3c7a5dSArchie Cobbs done:
765069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
766069154d5SJulian Elischer 	NG_FREE_MSG(msg);
767cb3c7a5dSArchie Cobbs 	return (error);
768cb3c7a5dSArchie Cobbs }
769cb3c7a5dSArchie Cobbs 
770cb3c7a5dSArchie Cobbs /*
771cb3c7a5dSArchie Cobbs  * Receive incoming data on our hook.  Send it out the socket.
772cb3c7a5dSArchie Cobbs  */
773cb3c7a5dSArchie Cobbs static int
774069154d5SJulian Elischer ng_ksocket_rcvdata(hook_p hook, item_p item)
775cb3c7a5dSArchie Cobbs {
776f8307e12SArchie Cobbs 	struct proc *p = curproc ? curproc : &proc0;	/* XXX broken */
777cb3c7a5dSArchie Cobbs 	const node_p node = hook->node;
778cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
779cb3c7a5dSArchie Cobbs 	struct socket *const so = priv->so;
780cb3c7a5dSArchie Cobbs 	int error;
781069154d5SJulian Elischer 	struct mbuf *m;
782cb3c7a5dSArchie Cobbs 
783069154d5SJulian Elischer 	NGI_GET_M(item, m);
784069154d5SJulian Elischer 	NG_FREE_ITEM(item);
785cb3c7a5dSArchie Cobbs 	error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, 0, 0, m, 0, 0, p);
786cb3c7a5dSArchie Cobbs 	return (error);
787cb3c7a5dSArchie Cobbs }
788cb3c7a5dSArchie Cobbs 
789cb3c7a5dSArchie Cobbs /*
790cb3c7a5dSArchie Cobbs  * Destroy node
791cb3c7a5dSArchie Cobbs  */
792cb3c7a5dSArchie Cobbs static int
793069154d5SJulian Elischer ng_ksocket_shutdown(node_p node)
794cb3c7a5dSArchie Cobbs {
795cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
796cb3c7a5dSArchie Cobbs 
79719bff684SArchie Cobbs 	/* Close our socket (if any) */
79819bff684SArchie Cobbs 	if (priv->so != NULL) {
799f8307e12SArchie Cobbs 
800f8307e12SArchie Cobbs 		priv->so->so_upcall = NULL;
801f8307e12SArchie Cobbs 		priv->so->so_rcv.sb_flags &= ~SB_UPCALL;
80219bff684SArchie Cobbs 		soclose(priv->so);
80319bff684SArchie Cobbs 		priv->so = NULL;
80419bff684SArchie Cobbs 	}
80519bff684SArchie Cobbs 
806cb3c7a5dSArchie Cobbs 	/* Take down netgraph node */
807cb3c7a5dSArchie Cobbs 	node->flags |= NG_INVALID;
808cb3c7a5dSArchie Cobbs 	bzero(priv, sizeof(*priv));
809cb3c7a5dSArchie Cobbs 	FREE(priv, M_NETGRAPH);
810cb3c7a5dSArchie Cobbs 	node->private = NULL;
811cb3c7a5dSArchie Cobbs 	ng_unref(node);		/* let the node escape */
812cb3c7a5dSArchie Cobbs 	return (0);
813cb3c7a5dSArchie Cobbs }
814cb3c7a5dSArchie Cobbs 
815cb3c7a5dSArchie Cobbs /*
816cb3c7a5dSArchie Cobbs  * Hook disconnection
817cb3c7a5dSArchie Cobbs  */
818cb3c7a5dSArchie Cobbs static int
819cb3c7a5dSArchie Cobbs ng_ksocket_disconnect(hook_p hook)
820cb3c7a5dSArchie Cobbs {
82119bff684SArchie Cobbs 	KASSERT(hook->node->numhooks == 0,
82219bff684SArchie Cobbs 	    ("%s: numhooks=%d?", __FUNCTION__, hook->node->numhooks));
823069154d5SJulian Elischer 	if ((hook->node->flags & NG_INVALID) == 0)
824069154d5SJulian Elischer 		ng_rmnode_self(hook->node);
825cb3c7a5dSArchie Cobbs 	return (0);
826cb3c7a5dSArchie Cobbs }
827cb3c7a5dSArchie Cobbs 
828cb3c7a5dSArchie Cobbs /************************************************************************
829cb3c7a5dSArchie Cobbs 			HELPER STUFF
830cb3c7a5dSArchie Cobbs  ************************************************************************/
831cb3c7a5dSArchie Cobbs 
832cb3c7a5dSArchie Cobbs /*
833cb3c7a5dSArchie Cobbs  * When incoming data is appended to the socket, we get notified here.
834cb3c7a5dSArchie Cobbs  */
835cb3c7a5dSArchie Cobbs static void
836cb3c7a5dSArchie Cobbs ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
837cb3c7a5dSArchie Cobbs {
838cb3c7a5dSArchie Cobbs 	const node_p node = arg;
839cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
840cb3c7a5dSArchie Cobbs 	struct mbuf *m;
841cb3c7a5dSArchie Cobbs 	struct uio auio;
84219bff684SArchie Cobbs 	int s, flags, error;
84319bff684SArchie Cobbs 
84419bff684SArchie Cobbs 	s = splnet();
845cb3c7a5dSArchie Cobbs 
846cb3c7a5dSArchie Cobbs 	/* Sanity check */
84719bff684SArchie Cobbs 	if ((node->flags & NG_INVALID) != 0) {
84819bff684SArchie Cobbs 		splx(s);
849cb3c7a5dSArchie Cobbs 		return;
85019bff684SArchie Cobbs 	}
851cb3c7a5dSArchie Cobbs 	KASSERT(so == priv->so, ("%s: wrong socket", __FUNCTION__));
852cb3c7a5dSArchie Cobbs 	KASSERT(priv->hook != NULL, ("%s: no hook", __FUNCTION__));
853cb3c7a5dSArchie Cobbs 
854cb3c7a5dSArchie Cobbs 	/* Read and forward available mbuf's */
855cb3c7a5dSArchie Cobbs 	auio.uio_procp = NULL;
856cb3c7a5dSArchie Cobbs 	auio.uio_resid = 1000000000;
857cb3c7a5dSArchie Cobbs 	flags = MSG_DONTWAIT;
858cb3c7a5dSArchie Cobbs 	do {
859cb3c7a5dSArchie Cobbs 		if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive)
860be731c30SArchie Cobbs 		      (so, (struct sockaddr **)0, &auio, &m,
861be731c30SArchie Cobbs 		      (struct mbuf **)0, &flags)) == 0
862f8307e12SArchie Cobbs 		    && m != NULL) {
863f8307e12SArchie Cobbs 			struct mbuf *n;
864f8307e12SArchie Cobbs 
865f8307e12SArchie Cobbs 			/* Don't trust the various socket layers to get the
866f8307e12SArchie Cobbs 			   packet header and length correct (eg. kern/15175) */
867f8307e12SArchie Cobbs 			for (n = m, m->m_pkthdr.len = 0; n; n = n->m_next)
868f8307e12SArchie Cobbs 				m->m_pkthdr.len += n->m_len;
869069154d5SJulian Elischer 			NG_SEND_DATA_ONLY(error, priv->hook, m);
870f8307e12SArchie Cobbs 		}
871cb3c7a5dSArchie Cobbs 	} while (error == 0 && m != NULL);
87219bff684SArchie Cobbs 	splx(s);
873cb3c7a5dSArchie Cobbs }
874cb3c7a5dSArchie Cobbs 
875cb3c7a5dSArchie Cobbs /*
876cb3c7a5dSArchie Cobbs  * Parse out either an integer value or an alias.
877cb3c7a5dSArchie Cobbs  */
878cb3c7a5dSArchie Cobbs static int
879cb3c7a5dSArchie Cobbs ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
880cb3c7a5dSArchie Cobbs 	const char *s, int family)
881cb3c7a5dSArchie Cobbs {
882cb3c7a5dSArchie Cobbs 	int k, val;
88325792ef3SArchie Cobbs 	char *eptr;
884cb3c7a5dSArchie Cobbs 
885cb3c7a5dSArchie Cobbs 	/* Try aliases */
886cb3c7a5dSArchie Cobbs 	for (k = 0; aliases[k].name != NULL; k++) {
887cb3c7a5dSArchie Cobbs 		if (strcmp(s, aliases[k].name) == 0
888cb3c7a5dSArchie Cobbs 		    && aliases[k].family == family)
889cb3c7a5dSArchie Cobbs 			return aliases[k].value;
890cb3c7a5dSArchie Cobbs 	}
891cb3c7a5dSArchie Cobbs 
892cb3c7a5dSArchie Cobbs 	/* Try parsing as a number */
893cb3c7a5dSArchie Cobbs 	val = (int)strtoul(s, &eptr, 10);
894f8307e12SArchie Cobbs 	if (val < 0 || *eptr != '\0')
895cb3c7a5dSArchie Cobbs 		return (-1);
896cb3c7a5dSArchie Cobbs 	return (val);
897cb3c7a5dSArchie Cobbs }
898cb3c7a5dSArchie Cobbs 
899