xref: /freebsd/sys/netgraph/ng_ksocket.c (revision f8307e1233657707bc582110f07373c96d91943b)
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  *
37cb3c7a5dSArchie Cobbs  * Author: Archie Cobbs <archie@whistle.com>
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/conf.h>
52cb3c7a5dSArchie Cobbs #include <sys/mbuf.h>
53cb3c7a5dSArchie Cobbs #include <sys/proc.h>
54cb3c7a5dSArchie Cobbs #include <sys/malloc.h>
55f8307e12SArchie Cobbs #include <sys/ctype.h>
56cb3c7a5dSArchie Cobbs #include <sys/protosw.h>
57cb3c7a5dSArchie Cobbs #include <sys/errno.h>
58cb3c7a5dSArchie Cobbs #include <sys/socket.h>
59cb3c7a5dSArchie Cobbs #include <sys/socketvar.h>
60cb3c7a5dSArchie Cobbs #include <sys/syslog.h>
61cb3c7a5dSArchie Cobbs #include <sys/uio.h>
62f8307e12SArchie Cobbs #include <sys/un.h>
63cb3c7a5dSArchie Cobbs 
64cb3c7a5dSArchie Cobbs #include <netgraph/ng_message.h>
65cb3c7a5dSArchie Cobbs #include <netgraph/netgraph.h>
66f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
67cb3c7a5dSArchie Cobbs #include <netgraph/ng_ksocket.h>
68cb3c7a5dSArchie Cobbs 
69cb3c7a5dSArchie Cobbs #include <netinet/in.h>
70cb3c7a5dSArchie Cobbs #include <netatalk/at.h>
71cb3c7a5dSArchie Cobbs 
72f8307e12SArchie Cobbs #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
73f8307e12SArchie Cobbs #define SADATA_OFFSET	(OFFSETOF(struct sockaddr, sa_data))
74f8307e12SArchie Cobbs 
75cb3c7a5dSArchie Cobbs /* Node private data */
7619bff684SArchie Cobbs struct ng_ksocket_private {
77cb3c7a5dSArchie Cobbs 	hook_p		hook;
78cb3c7a5dSArchie Cobbs 	struct socket	*so;
79cb3c7a5dSArchie Cobbs };
8019bff684SArchie Cobbs typedef struct ng_ksocket_private *priv_p;
81cb3c7a5dSArchie Cobbs 
82cb3c7a5dSArchie Cobbs /* Netgraph node methods */
83cb3c7a5dSArchie Cobbs static ng_constructor_t	ng_ksocket_constructor;
84cb3c7a5dSArchie Cobbs static ng_rcvmsg_t	ng_ksocket_rcvmsg;
85cb3c7a5dSArchie Cobbs static ng_shutdown_t	ng_ksocket_rmnode;
86cb3c7a5dSArchie Cobbs static ng_newhook_t	ng_ksocket_newhook;
87cb3c7a5dSArchie Cobbs static ng_rcvdata_t	ng_ksocket_rcvdata;
88cb3c7a5dSArchie Cobbs static ng_disconnect_t	ng_ksocket_disconnect;
89cb3c7a5dSArchie Cobbs 
90cb3c7a5dSArchie Cobbs /* Alias structure */
91cb3c7a5dSArchie Cobbs struct ng_ksocket_alias {
92cb3c7a5dSArchie Cobbs 	const char	*name;
93cb3c7a5dSArchie Cobbs 	const int	value;
94cb3c7a5dSArchie Cobbs 	const int	family;
95cb3c7a5dSArchie Cobbs };
96cb3c7a5dSArchie Cobbs 
97cb3c7a5dSArchie Cobbs /* Protocol family aliases */
98cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_families[] = {
99cb3c7a5dSArchie Cobbs 	{ "local",	PF_LOCAL	},
100cb3c7a5dSArchie Cobbs 	{ "inet",	PF_INET		},
101cb3c7a5dSArchie Cobbs 	{ "inet6",	PF_INET6	},
102cb3c7a5dSArchie Cobbs 	{ "atalk",	PF_APPLETALK	},
103cb3c7a5dSArchie Cobbs 	{ "ipx",	PF_IPX		},
104cb3c7a5dSArchie Cobbs 	{ "atm",	PF_ATM		},
105cb3c7a5dSArchie Cobbs 	{ NULL,		-1		},
106cb3c7a5dSArchie Cobbs };
107cb3c7a5dSArchie Cobbs 
108cb3c7a5dSArchie Cobbs /* Socket type aliases */
109cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_types[] = {
110cb3c7a5dSArchie Cobbs 	{ "stream",	SOCK_STREAM	},
111cb3c7a5dSArchie Cobbs 	{ "dgram",	SOCK_DGRAM	},
112cb3c7a5dSArchie Cobbs 	{ "raw",	SOCK_RAW	},
113cb3c7a5dSArchie Cobbs 	{ "rdm",	SOCK_RDM	},
114cb3c7a5dSArchie Cobbs 	{ "seqpacket",	SOCK_SEQPACKET	},
115cb3c7a5dSArchie Cobbs 	{ NULL,		-1		},
116cb3c7a5dSArchie Cobbs };
117cb3c7a5dSArchie Cobbs 
118cb3c7a5dSArchie Cobbs /* Protocol aliases */
119cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_protos[] = {
120cb3c7a5dSArchie Cobbs 	{ "ip",		IPPROTO_IP,		PF_INET		},
121cb3c7a5dSArchie Cobbs 	{ "raw",	IPPROTO_IP,		PF_INET		},
122cb3c7a5dSArchie Cobbs 	{ "icmp",	IPPROTO_ICMP,		PF_INET		},
123cb3c7a5dSArchie Cobbs 	{ "igmp",	IPPROTO_IGMP,		PF_INET		},
124cb3c7a5dSArchie Cobbs 	{ "tcp",	IPPROTO_TCP,		PF_INET		},
125cb3c7a5dSArchie Cobbs 	{ "udp",	IPPROTO_UDP,		PF_INET		},
126cb3c7a5dSArchie Cobbs 	{ "gre",	IPPROTO_GRE,		PF_INET		},
127cb3c7a5dSArchie Cobbs 	{ "esp",	IPPROTO_ESP,		PF_INET		},
128cb3c7a5dSArchie Cobbs 	{ "ah",		IPPROTO_AH,		PF_INET		},
129cb3c7a5dSArchie Cobbs 	{ "swipe",	IPPROTO_SWIPE,		PF_INET		},
130cb3c7a5dSArchie Cobbs 	{ "encap",	IPPROTO_ENCAP,		PF_INET		},
131cb3c7a5dSArchie Cobbs 	{ "divert",	IPPROTO_DIVERT,		PF_INET		},
132cb3c7a5dSArchie Cobbs 	{ "ddp",	ATPROTO_DDP,		PF_APPLETALK	},
133cb3c7a5dSArchie Cobbs 	{ "aarp",	ATPROTO_AARP,		PF_APPLETALK	},
134cb3c7a5dSArchie Cobbs 	{ NULL,		-1					},
135cb3c7a5dSArchie Cobbs };
136cb3c7a5dSArchie Cobbs 
137f8307e12SArchie Cobbs /* Helper functions */
138f8307e12SArchie Cobbs static void	ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
139f8307e12SArchie Cobbs static int	ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
140f8307e12SArchie Cobbs 			const char *s, int family);
141f8307e12SArchie Cobbs 
142f8307e12SArchie Cobbs /************************************************************************
143f8307e12SArchie Cobbs 			STRUCT SOCKADDR PARSE TYPE
144f8307e12SArchie Cobbs  ************************************************************************/
145f8307e12SArchie Cobbs 
146f8307e12SArchie Cobbs /* Get the length of the data portion of a generic struct sockaddr */
147f8307e12SArchie Cobbs static int
148f8307e12SArchie Cobbs ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
149f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
150f8307e12SArchie Cobbs {
151f8307e12SArchie Cobbs 	const struct sockaddr *sa;
152f8307e12SArchie Cobbs 
153f8307e12SArchie Cobbs 	sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
154f8307e12SArchie Cobbs 	return sa->sa_len - SADATA_OFFSET;
155f8307e12SArchie Cobbs }
156f8307e12SArchie Cobbs 
157f8307e12SArchie Cobbs /* Type for the variable length data portion of a generic struct sockaddr */
158f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
159f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
160f8307e12SArchie Cobbs 	&ng_parse_generic_sockdata_getLength
161f8307e12SArchie Cobbs };
162f8307e12SArchie Cobbs 
163f8307e12SArchie Cobbs /* Type for a generic struct sockaddr */
164f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_parse_generic_sockaddr_type_info = {
165f8307e12SArchie Cobbs 	{
166f8307e12SArchie Cobbs 	  { "len",	&ng_parse_int8_type			},
167f8307e12SArchie Cobbs 	  { "family",	&ng_parse_int8_type			},
168f8307e12SArchie Cobbs 	  { "data",	&ng_ksocket_generic_sockdata_type	},
169f8307e12SArchie Cobbs 	  { NULL }
170f8307e12SArchie Cobbs 	}
171f8307e12SArchie Cobbs };
172f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
173f8307e12SArchie Cobbs 	&ng_parse_struct_type,
174f8307e12SArchie Cobbs 	&ng_parse_generic_sockaddr_type_info
175f8307e12SArchie Cobbs };
176f8307e12SArchie Cobbs 
177f8307e12SArchie Cobbs /* Convert a struct sockaddr from ASCII to binary.  If its a protocol
178f8307e12SArchie Cobbs    family that we specially handle, do that, otherwise defer to the
179f8307e12SArchie Cobbs    generic parse type ng_ksocket_generic_sockaddr_type. */
180f8307e12SArchie Cobbs static int
181f8307e12SArchie Cobbs ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
182f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
183f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
184f8307e12SArchie Cobbs {
185f8307e12SArchie Cobbs 	struct sockaddr *const sa = (struct sockaddr *)buf;
186f8307e12SArchie Cobbs 	enum ng_parse_token tok;
187f8307e12SArchie Cobbs 	char fambuf[32];
188f8307e12SArchie Cobbs 	int family, len;
189f8307e12SArchie Cobbs 	char *t;
190f8307e12SArchie Cobbs 
191f8307e12SArchie Cobbs 	/* If next token is a left curly brace, use generic parse type */
192f8307e12SArchie Cobbs 	if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
193f8307e12SArchie Cobbs 		return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
194f8307e12SArchie Cobbs 		    (&ng_ksocket_generic_sockaddr_type,
195f8307e12SArchie Cobbs 		    s, off, start, buf, buflen);
196f8307e12SArchie Cobbs 	}
197f8307e12SArchie Cobbs 
198f8307e12SArchie Cobbs 	/* Get socket address family followed by a slash */
199f8307e12SArchie Cobbs 	while (isspace(s[*off]))
200f8307e12SArchie Cobbs 		(*off)++;
201f8307e12SArchie Cobbs 	if ((t = index(s + *off, '/')) == NULL)
202f8307e12SArchie Cobbs 		return (EINVAL);
203f8307e12SArchie Cobbs 	if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
204f8307e12SArchie Cobbs 		return (EINVAL);
205f8307e12SArchie Cobbs 	strncpy(fambuf, s + *off, len);
206f8307e12SArchie Cobbs 	fambuf[len] = '\0';
207f8307e12SArchie Cobbs 	*off += len + 1;
208f8307e12SArchie Cobbs 	if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
209f8307e12SArchie Cobbs 		return (EINVAL);
210f8307e12SArchie Cobbs 
211f8307e12SArchie Cobbs 	/* Set family */
212f8307e12SArchie Cobbs 	if (*buflen < SADATA_OFFSET)
213f8307e12SArchie Cobbs 		return (ERANGE);
214f8307e12SArchie Cobbs 	sa->sa_family = family;
215f8307e12SArchie Cobbs 
216f8307e12SArchie Cobbs 	/* Set family-specific data and length */
217f8307e12SArchie Cobbs 	switch (sa->sa_family) {
218f8307e12SArchie Cobbs 	case PF_LOCAL:		/* Get pathname */
219f8307e12SArchie Cobbs 	    {
220f8307e12SArchie Cobbs 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
221f8307e12SArchie Cobbs 		struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
222f8307e12SArchie Cobbs 		int toklen, pathlen;
223f8307e12SArchie Cobbs 		char *path;
224f8307e12SArchie Cobbs 
225f8307e12SArchie Cobbs 		if ((path = ng_get_string_token(s, off, &toklen)) == NULL)
226f8307e12SArchie Cobbs 			return (EINVAL);
227f8307e12SArchie Cobbs 		pathlen = strlen(path);
228f8307e12SArchie Cobbs 		if (pathlen > SOCK_MAXADDRLEN) {
229f8307e12SArchie Cobbs 			FREE(path, M_NETGRAPH);
230f8307e12SArchie Cobbs 			return (E2BIG);
231f8307e12SArchie Cobbs 		}
232f8307e12SArchie Cobbs 		if (*buflen < pathoff + pathlen) {
233f8307e12SArchie Cobbs 			FREE(path, M_NETGRAPH);
234f8307e12SArchie Cobbs 			return (ERANGE);
235f8307e12SArchie Cobbs 		}
236f8307e12SArchie Cobbs 		*off += toklen;
237f8307e12SArchie Cobbs 		bcopy(path, sun->sun_path, pathlen);
238f8307e12SArchie Cobbs 		sun->sun_len = pathoff + pathlen;
239f8307e12SArchie Cobbs 		FREE(path, M_NETGRAPH);
240f8307e12SArchie Cobbs 		break;
241f8307e12SArchie Cobbs 	    }
242f8307e12SArchie Cobbs 
243f8307e12SArchie Cobbs 	case PF_INET:		/* Get an IP address with optional port */
244f8307e12SArchie Cobbs 	    {
245f8307e12SArchie Cobbs 		struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
246f8307e12SArchie Cobbs 		int i;
247f8307e12SArchie Cobbs 
248f8307e12SArchie Cobbs 		/* Parse this: <ipaddress>[:port] */
249f8307e12SArchie Cobbs 		for (i = 0; i < 4; i++) {
250f8307e12SArchie Cobbs 			u_long val;
251f8307e12SArchie Cobbs 			char *eptr;
252f8307e12SArchie Cobbs 
253f8307e12SArchie Cobbs 			val = strtoul(s + *off, &eptr, 10);
254f8307e12SArchie Cobbs 			if (val > 0xff || eptr == s + *off)
255f8307e12SArchie Cobbs 				return (EINVAL);
256f8307e12SArchie Cobbs 			*off += (eptr - (s + *off));
257f8307e12SArchie Cobbs 			((u_char *)&sin->sin_addr)[i] = (u_char)val;
258f8307e12SArchie Cobbs 			if (i < 3) {
259f8307e12SArchie Cobbs 				if (s[*off] != '.')
260f8307e12SArchie Cobbs 					return (EINVAL);
261f8307e12SArchie Cobbs 				(*off)++;
262f8307e12SArchie Cobbs 			} else if (s[*off] == ':') {
263f8307e12SArchie Cobbs 				(*off)++;
264f8307e12SArchie Cobbs 				val = strtoul(s + *off, &eptr, 10);
265f8307e12SArchie Cobbs 				if (val > 0xffff || eptr == s + *off)
266f8307e12SArchie Cobbs 					return (EINVAL);
267f8307e12SArchie Cobbs 				*off += (eptr - (s + *off));
268f8307e12SArchie Cobbs 				sin->sin_port = htons(val);
269f8307e12SArchie Cobbs 			} else
270f8307e12SArchie Cobbs 				sin->sin_port = 0;
271f8307e12SArchie Cobbs 		}
272f8307e12SArchie Cobbs 		bzero(&sin->sin_zero, sizeof(sin->sin_zero));
273f8307e12SArchie Cobbs 		sin->sin_len = sizeof(*sin);
274f8307e12SArchie Cobbs 		break;
275f8307e12SArchie Cobbs 	    }
276f8307e12SArchie Cobbs 
277f8307e12SArchie Cobbs #if 0
278f8307e12SArchie Cobbs 	case PF_APPLETALK:	/* XXX implement these someday */
279f8307e12SArchie Cobbs 	case PF_INET6:
280f8307e12SArchie Cobbs 	case PF_IPX:
281f8307e12SArchie Cobbs #endif
282f8307e12SArchie Cobbs 
283f8307e12SArchie Cobbs 	default:
284f8307e12SArchie Cobbs 		return (EINVAL);
285f8307e12SArchie Cobbs 	}
286f8307e12SArchie Cobbs 
287f8307e12SArchie Cobbs 	/* Done */
288f8307e12SArchie Cobbs 	*buflen = sa->sa_len;
289f8307e12SArchie Cobbs 	return (0);
290f8307e12SArchie Cobbs }
291f8307e12SArchie Cobbs 
292f8307e12SArchie Cobbs /* Convert a struct sockaddr from binary to ASCII */
293f8307e12SArchie Cobbs static int
294f8307e12SArchie Cobbs ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
295f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
296f8307e12SArchie Cobbs {
297f8307e12SArchie Cobbs 	const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
298f8307e12SArchie Cobbs 	int slen = 0;
299f8307e12SArchie Cobbs 
300f8307e12SArchie Cobbs 	/* Output socket address, either in special or generic format */
301f8307e12SArchie Cobbs 	switch (sa->sa_family) {
302f8307e12SArchie Cobbs 	case PF_LOCAL:
303f8307e12SArchie Cobbs 	    {
304f8307e12SArchie Cobbs 		const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
305f8307e12SArchie Cobbs 		const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
306f8307e12SArchie Cobbs 		const int pathlen = sun->sun_len - pathoff;
307f8307e12SArchie Cobbs 		char pathbuf[SOCK_MAXADDRLEN + 1];
308f8307e12SArchie Cobbs 		char *pathtoken;
309f8307e12SArchie Cobbs 
310f8307e12SArchie Cobbs 		bcopy(sun->sun_path, pathbuf, pathlen);
311f8307e12SArchie Cobbs 		pathbuf[pathlen] = '\0';
312f8307e12SArchie Cobbs 		if ((pathtoken = ng_encode_string(pathbuf)) == NULL)
313f8307e12SArchie Cobbs 			return (ENOMEM);
314f8307e12SArchie Cobbs 		slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken);
315f8307e12SArchie Cobbs 		FREE(pathtoken, M_NETGRAPH);
316f8307e12SArchie Cobbs 		if (slen >= cbuflen)
317f8307e12SArchie Cobbs 			return (ERANGE);
318f8307e12SArchie Cobbs 		*off += sun->sun_len;
319f8307e12SArchie Cobbs 		return (0);
320f8307e12SArchie Cobbs 	    }
321f8307e12SArchie Cobbs 
322f8307e12SArchie Cobbs 	case PF_INET:
323f8307e12SArchie Cobbs 	    {
324f8307e12SArchie Cobbs 		const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
325f8307e12SArchie Cobbs 
326f8307e12SArchie Cobbs 		slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
327f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[0],
328f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[1],
329f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[2],
330f8307e12SArchie Cobbs 		  ((const u_char *)&sin->sin_addr)[3]);
331f8307e12SArchie Cobbs 		if (sin->sin_port != 0) {
332f8307e12SArchie Cobbs 			slen += snprintf(cbuf + strlen(cbuf),
333f8307e12SArchie Cobbs 			    cbuflen - strlen(cbuf), ":%d",
334f8307e12SArchie Cobbs 			    (u_int)ntohs(sin->sin_port));
335f8307e12SArchie Cobbs 		}
336f8307e12SArchie Cobbs 		if (slen >= cbuflen)
337f8307e12SArchie Cobbs 			return (ERANGE);
338f8307e12SArchie Cobbs 		*off += sizeof(*sin);
339f8307e12SArchie Cobbs 		return(0);
340f8307e12SArchie Cobbs 	    }
341f8307e12SArchie Cobbs 
342f8307e12SArchie Cobbs #if 0
343f8307e12SArchie Cobbs 	case PF_APPLETALK:	/* XXX implement these someday */
344f8307e12SArchie Cobbs 	case PF_INET6:
345f8307e12SArchie Cobbs 	case PF_IPX:
346f8307e12SArchie Cobbs #endif
347f8307e12SArchie Cobbs 
348f8307e12SArchie Cobbs 	default:
349f8307e12SArchie Cobbs 		return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
350f8307e12SArchie Cobbs 		    (&ng_ksocket_generic_sockaddr_type,
351f8307e12SArchie Cobbs 		    data, off, cbuf, cbuflen);
352f8307e12SArchie Cobbs 	}
353f8307e12SArchie Cobbs }
354f8307e12SArchie Cobbs 
355f8307e12SArchie Cobbs /* Parse type for struct sockaddr */
356f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockaddr_type = {
357f8307e12SArchie Cobbs 	NULL,
358f8307e12SArchie Cobbs 	NULL,
359f8307e12SArchie Cobbs 	NULL,
360f8307e12SArchie Cobbs 	&ng_ksocket_sockaddr_parse,
361f8307e12SArchie Cobbs 	&ng_ksocket_sockaddr_unparse,
362f8307e12SArchie Cobbs 	NULL		/* no such thing as a default struct sockaddr */
363f8307e12SArchie Cobbs };
364f8307e12SArchie Cobbs 
365f8307e12SArchie Cobbs /************************************************************************
366f8307e12SArchie Cobbs 		STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
367f8307e12SArchie Cobbs  ************************************************************************/
368f8307e12SArchie Cobbs 
369f8307e12SArchie Cobbs /* Get length of the struct ng_ksocket_sockopt value field, which is the
370f8307e12SArchie Cobbs    just the excess of the message argument portion over the length of
371f8307e12SArchie Cobbs    the struct ng_ksocket_sockopt. */
372f8307e12SArchie Cobbs static int
373f8307e12SArchie Cobbs ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
374f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
375f8307e12SArchie Cobbs {
376f8307e12SArchie Cobbs 	static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
377f8307e12SArchie Cobbs 	const struct ng_ksocket_sockopt *sopt;
378f8307e12SArchie Cobbs 	const struct ng_mesg *msg;
379f8307e12SArchie Cobbs 
380f8307e12SArchie Cobbs 	sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
381f8307e12SArchie Cobbs 	msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
382f8307e12SArchie Cobbs 	return msg->header.arglen - sizeof(*sopt);
383f8307e12SArchie Cobbs }
384f8307e12SArchie Cobbs 
385f8307e12SArchie Cobbs /* Parse type for the option value part of a struct ng_ksocket_sockopt
386f8307e12SArchie Cobbs    XXX Eventually, we should handle the different socket options specially.
387f8307e12SArchie Cobbs    XXX This would avoid byte order problems, eg an integer value of 1 is
388f8307e12SArchie Cobbs    XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
389f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockoptval_type = {
390f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
391f8307e12SArchie Cobbs 	&ng_parse_sockoptval_getLength
392f8307e12SArchie Cobbs };
393f8307e12SArchie Cobbs 
394f8307e12SArchie Cobbs /* Parse type for struct ng_ksocket_sockopt */
395f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_ksocket_sockopt_type_info
396f8307e12SArchie Cobbs 	= NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
397f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockopt_type = {
398f8307e12SArchie Cobbs 	&ng_parse_struct_type,
399f8307e12SArchie Cobbs 	&ng_ksocket_sockopt_type_info,
400f8307e12SArchie Cobbs };
401f8307e12SArchie Cobbs 
402f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
403f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ksocket_cmds[] = {
404f8307e12SArchie Cobbs 	{
405f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
406f8307e12SArchie Cobbs 	  NGM_KSOCKET_BIND,
407f8307e12SArchie Cobbs 	  "bind",
408f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type,
409f8307e12SArchie Cobbs 	  NULL
410f8307e12SArchie Cobbs 	},
411f8307e12SArchie Cobbs 	{
412f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
413f8307e12SArchie Cobbs 	  NGM_KSOCKET_LISTEN,
414f8307e12SArchie Cobbs 	  "listen",
415f8307e12SArchie Cobbs 	  &ng_parse_int32_type,
416f8307e12SArchie Cobbs 	  NULL
417f8307e12SArchie Cobbs 	},
418f8307e12SArchie Cobbs 	{
419f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
420f8307e12SArchie Cobbs 	  NGM_KSOCKET_ACCEPT,
421f8307e12SArchie Cobbs 	  "accept",
422f8307e12SArchie Cobbs 	  NULL,
423f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
424f8307e12SArchie Cobbs 	},
425f8307e12SArchie Cobbs 	{
426f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
427f8307e12SArchie Cobbs 	  NGM_KSOCKET_CONNECT,
428f8307e12SArchie Cobbs 	  "connect",
429f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type,
430f8307e12SArchie Cobbs 	  NULL
431f8307e12SArchie Cobbs 	},
432f8307e12SArchie Cobbs 	{
433f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
434f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETNAME,
435f8307e12SArchie Cobbs 	  "getname",
436f8307e12SArchie Cobbs 	  NULL,
437f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
438f8307e12SArchie Cobbs 	},
439f8307e12SArchie Cobbs 	{
440f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
441f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETPEERNAME,
442f8307e12SArchie Cobbs 	  "getpeername",
443f8307e12SArchie Cobbs 	  NULL,
444f8307e12SArchie Cobbs 	  &ng_ksocket_sockaddr_type
445f8307e12SArchie Cobbs 	},
446f8307e12SArchie Cobbs 	{
447f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
448f8307e12SArchie Cobbs 	  NGM_KSOCKET_SETOPT,
449f8307e12SArchie Cobbs 	  "setopt",
450f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type,
451f8307e12SArchie Cobbs 	  NULL
452f8307e12SArchie Cobbs 	},
453f8307e12SArchie Cobbs 	{
454f8307e12SArchie Cobbs 	  NGM_KSOCKET_COOKIE,
455f8307e12SArchie Cobbs 	  NGM_KSOCKET_GETOPT,
456f8307e12SArchie Cobbs 	  "getopt",
457f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type,
458f8307e12SArchie Cobbs 	  &ng_ksocket_sockopt_type
459f8307e12SArchie Cobbs 	},
460f8307e12SArchie Cobbs 	{ 0 }
461f8307e12SArchie Cobbs };
462f8307e12SArchie Cobbs 
463f8307e12SArchie Cobbs /* Node type descriptor */
464f8307e12SArchie Cobbs static struct ng_type ng_ksocket_typestruct = {
465f8307e12SArchie Cobbs 	NG_VERSION,
466f8307e12SArchie Cobbs 	NG_KSOCKET_NODE_TYPE,
467f8307e12SArchie Cobbs 	NULL,
468f8307e12SArchie Cobbs 	ng_ksocket_constructor,
469f8307e12SArchie Cobbs 	ng_ksocket_rcvmsg,
470f8307e12SArchie Cobbs 	ng_ksocket_rmnode,
471f8307e12SArchie Cobbs 	ng_ksocket_newhook,
472f8307e12SArchie Cobbs 	NULL,
473f8307e12SArchie Cobbs 	NULL,
474f8307e12SArchie Cobbs 	ng_ksocket_rcvdata,
475f8307e12SArchie Cobbs 	ng_ksocket_rcvdata,
476f8307e12SArchie Cobbs 	ng_ksocket_disconnect,
477f8307e12SArchie Cobbs 	ng_ksocket_cmds
478f8307e12SArchie Cobbs };
479f8307e12SArchie Cobbs NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
480f8307e12SArchie Cobbs 
481cb3c7a5dSArchie Cobbs #define ERROUT(x)	do { error = (x); goto done; } while (0)
482cb3c7a5dSArchie Cobbs 
483cb3c7a5dSArchie Cobbs /************************************************************************
484cb3c7a5dSArchie Cobbs 			NETGRAPH NODE STUFF
485cb3c7a5dSArchie Cobbs  ************************************************************************/
486cb3c7a5dSArchie Cobbs 
487cb3c7a5dSArchie Cobbs /*
488cb3c7a5dSArchie Cobbs  * Node type constructor
489cb3c7a5dSArchie Cobbs  */
490cb3c7a5dSArchie Cobbs static int
491cb3c7a5dSArchie Cobbs ng_ksocket_constructor(node_p *nodep)
492cb3c7a5dSArchie Cobbs {
493cb3c7a5dSArchie Cobbs 	priv_p priv;
494cb3c7a5dSArchie Cobbs 	int error;
495cb3c7a5dSArchie Cobbs 
496cb3c7a5dSArchie Cobbs 	/* Allocate private structure */
497cb3c7a5dSArchie Cobbs 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK);
498cb3c7a5dSArchie Cobbs 	if (priv == NULL)
499cb3c7a5dSArchie Cobbs 		return (ENOMEM);
500cb3c7a5dSArchie Cobbs 	bzero(priv, sizeof(*priv));
501cb3c7a5dSArchie Cobbs 
502cb3c7a5dSArchie Cobbs 	/* Call generic node constructor */
503cb3c7a5dSArchie Cobbs 	if ((error = ng_make_node_common(&ng_ksocket_typestruct, nodep))) {
504cb3c7a5dSArchie Cobbs 		FREE(priv, M_NETGRAPH);
505cb3c7a5dSArchie Cobbs 		return (error);
506cb3c7a5dSArchie Cobbs 	}
507cb3c7a5dSArchie Cobbs 	(*nodep)->private = priv;
508cb3c7a5dSArchie Cobbs 
509cb3c7a5dSArchie Cobbs 	/* Done */
510cb3c7a5dSArchie Cobbs 	return (0);
511cb3c7a5dSArchie Cobbs }
512cb3c7a5dSArchie Cobbs 
513cb3c7a5dSArchie Cobbs /*
514cb3c7a5dSArchie Cobbs  * Give our OK for a hook to be added. The hook name is of the
515cb3c7a5dSArchie Cobbs  * form "<family>:<type>:<proto>" where the three components may
516cb3c7a5dSArchie Cobbs  * be decimal numbers or else aliases from the above lists.
517cb3c7a5dSArchie Cobbs  *
518cb3c7a5dSArchie Cobbs  * Connecting a hook amounts to opening the socket.  Disconnecting
519cb3c7a5dSArchie Cobbs  * the hook closes the socket and destroys the node as well.
520cb3c7a5dSArchie Cobbs  */
521cb3c7a5dSArchie Cobbs static int
522cb3c7a5dSArchie Cobbs ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
523cb3c7a5dSArchie Cobbs {
524f8307e12SArchie Cobbs 	struct proc *p = curproc ? curproc : &proc0;	/* XXX broken */
525cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
526cb3c7a5dSArchie Cobbs 	char *s1, *s2, name[NG_HOOKLEN+1];
527cb3c7a5dSArchie Cobbs 	int family, type, protocol, error;
528cb3c7a5dSArchie Cobbs 
529cb3c7a5dSArchie Cobbs 	/* Check if we're already connected */
530cb3c7a5dSArchie Cobbs 	if (priv->hook != NULL)
531cb3c7a5dSArchie Cobbs 		return (EISCONN);
532cb3c7a5dSArchie Cobbs 
533cb3c7a5dSArchie Cobbs 	/* Extract family, type, and protocol from hook name */
534cb3c7a5dSArchie Cobbs 	snprintf(name, sizeof(name), "%s", name0);
535cb3c7a5dSArchie Cobbs 	s1 = name;
536cb3c7a5dSArchie Cobbs 	if ((s2 = index(s1, '/')) == NULL)
537cb3c7a5dSArchie Cobbs 		return (EINVAL);
538cb3c7a5dSArchie Cobbs 	*s2++ = '\0';
539cb3c7a5dSArchie Cobbs 	if ((family = ng_ksocket_parse(ng_ksocket_families, s1, 0)) == -1)
540cb3c7a5dSArchie Cobbs 		return (EINVAL);
541cb3c7a5dSArchie Cobbs 	s1 = s2;
542cb3c7a5dSArchie Cobbs 	if ((s2 = index(s1, '/')) == NULL)
543cb3c7a5dSArchie Cobbs 		return (EINVAL);
544cb3c7a5dSArchie Cobbs 	*s2++ = '\0';
545cb3c7a5dSArchie Cobbs 	if ((type = ng_ksocket_parse(ng_ksocket_types, s1, 0)) == -1)
546cb3c7a5dSArchie Cobbs 		return (EINVAL);
547cb3c7a5dSArchie Cobbs 	s1 = s2;
548cb3c7a5dSArchie Cobbs 	if ((protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family)) == -1)
549cb3c7a5dSArchie Cobbs 		return (EINVAL);
550cb3c7a5dSArchie Cobbs 
551cb3c7a5dSArchie Cobbs 	/* Create the socket */
552cb3c7a5dSArchie Cobbs 	if ((error = socreate(family, &priv->so, type, protocol, p)) != 0)
553cb3c7a5dSArchie Cobbs 		return (error);
554cb3c7a5dSArchie Cobbs 
555cb3c7a5dSArchie Cobbs 	/* XXX call soreserve() ? */
556cb3c7a5dSArchie Cobbs 
557cb3c7a5dSArchie Cobbs 	/* Add our hook for incoming data */
558cb3c7a5dSArchie Cobbs 	priv->so->so_upcallarg = (caddr_t)node;
559cb3c7a5dSArchie Cobbs 	priv->so->so_upcall = ng_ksocket_incoming;
560cb3c7a5dSArchie Cobbs 	priv->so->so_rcv.sb_flags |= SB_UPCALL;
561cb3c7a5dSArchie Cobbs 
562cb3c7a5dSArchie Cobbs 	/* OK */
563cb3c7a5dSArchie Cobbs 	priv->hook = hook;
564cb3c7a5dSArchie Cobbs 	return (0);
565cb3c7a5dSArchie Cobbs }
566cb3c7a5dSArchie Cobbs 
567cb3c7a5dSArchie Cobbs /*
568cb3c7a5dSArchie Cobbs  * Receive a control message
569cb3c7a5dSArchie Cobbs  */
570cb3c7a5dSArchie Cobbs static int
571cb3c7a5dSArchie Cobbs ng_ksocket_rcvmsg(node_p node, struct ng_mesg *msg,
572cb3c7a5dSArchie Cobbs 	      const char *raddr, struct ng_mesg **rptr)
573cb3c7a5dSArchie Cobbs {
574f8307e12SArchie Cobbs 	struct proc *p = curproc ? curproc : &proc0;	/* XXX broken */
575cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
576f8307e12SArchie Cobbs 	struct socket *const so = priv->so;
577cb3c7a5dSArchie Cobbs 	struct ng_mesg *resp = NULL;
578cb3c7a5dSArchie Cobbs 	int error = 0;
579cb3c7a5dSArchie Cobbs 
580cb3c7a5dSArchie Cobbs 	switch (msg->header.typecookie) {
581cb3c7a5dSArchie Cobbs 	case NGM_KSOCKET_COOKIE:
582cb3c7a5dSArchie Cobbs 		switch (msg->header.cmd) {
583cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_BIND:
584cb3c7a5dSArchie Cobbs 		    {
585f8307e12SArchie Cobbs 			struct sockaddr *const sa
586f8307e12SArchie Cobbs 			    = (struct sockaddr *)msg->data;
587cb3c7a5dSArchie Cobbs 
588f8307e12SArchie Cobbs 			/* Sanity check */
589f8307e12SArchie Cobbs 			if (msg->header.arglen < SADATA_OFFSET
590f8307e12SArchie Cobbs 			    || msg->header.arglen < sa->sa_len)
591f8307e12SArchie Cobbs 				ERROUT(EINVAL);
592f8307e12SArchie Cobbs 			if (so == NULL)
593f8307e12SArchie Cobbs 				ERROUT(ENXIO);
594cb3c7a5dSArchie Cobbs 
595f8307e12SArchie Cobbs 			/* Bind */
596cb3c7a5dSArchie Cobbs 			error = sobind(so, sa, p);
597cb3c7a5dSArchie Cobbs 			break;
598cb3c7a5dSArchie Cobbs 		    }
599cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_LISTEN:
600cb3c7a5dSArchie Cobbs 		    {
601f8307e12SArchie Cobbs 			/* Sanity check */
602cb3c7a5dSArchie Cobbs 			if (msg->header.arglen != sizeof(int))
603cb3c7a5dSArchie Cobbs 				ERROUT(EINVAL);
604f8307e12SArchie Cobbs 			if (so == NULL)
605f8307e12SArchie Cobbs 				ERROUT(ENXIO);
606cb3c7a5dSArchie Cobbs 
607f8307e12SArchie Cobbs 			/* Listen */
608f8307e12SArchie Cobbs 			if ((error = solisten(so, *((int *)msg->data), p)) != 0)
609cb3c7a5dSArchie Cobbs 				break;
610cb3c7a5dSArchie Cobbs 
611cb3c7a5dSArchie Cobbs 			/* Notify sender when we get a connection attempt */
612cb3c7a5dSArchie Cobbs 				/* XXX implement me */
613f8307e12SArchie Cobbs 			error = ENODEV;
614cb3c7a5dSArchie Cobbs 			break;
615cb3c7a5dSArchie Cobbs 		    }
616cb3c7a5dSArchie Cobbs 
617cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_ACCEPT:
618cb3c7a5dSArchie Cobbs 		    {
619f8307e12SArchie Cobbs 			/* Sanity check */
620f8307e12SArchie Cobbs 			if (msg->header.arglen != 0)
621f8307e12SArchie Cobbs 				ERROUT(EINVAL);
622f8307e12SArchie Cobbs 			if (so == NULL)
623f8307e12SArchie Cobbs 				ERROUT(ENXIO);
624f8307e12SArchie Cobbs 
625f8307e12SArchie Cobbs 			/* Accept on the socket in a non-blocking way */
626f8307e12SArchie Cobbs 			/* Create a new ksocket node for the new connection */
627f8307e12SArchie Cobbs 			/* Return a response with the peer's sockaddr and
628f8307e12SArchie Cobbs 			   the absolute name of the newly created node */
629f8307e12SArchie Cobbs 
630f8307e12SArchie Cobbs 				/* XXX implement me */
631f8307e12SArchie Cobbs 
632f8307e12SArchie Cobbs 			error = ENODEV;
633cb3c7a5dSArchie Cobbs 			break;
634cb3c7a5dSArchie Cobbs 		    }
635cb3c7a5dSArchie Cobbs 
636cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_CONNECT:
637cb3c7a5dSArchie Cobbs 		    {
638f8307e12SArchie Cobbs 			struct sockaddr *const sa
639f8307e12SArchie Cobbs 			    = (struct sockaddr *)msg->data;
640cb3c7a5dSArchie Cobbs 
641f8307e12SArchie Cobbs 			/* Sanity check */
642f8307e12SArchie Cobbs 			if (msg->header.arglen < SADATA_OFFSET
643f8307e12SArchie Cobbs 			    || msg->header.arglen < sa->sa_len)
644f8307e12SArchie Cobbs 				ERROUT(EINVAL);
645f8307e12SArchie Cobbs 			if (so == NULL)
646f8307e12SArchie Cobbs 				ERROUT(ENXIO);
647cb3c7a5dSArchie Cobbs 
648cb3c7a5dSArchie Cobbs 			/* Do connect */
649cb3c7a5dSArchie Cobbs 			if ((so->so_state & SS_ISCONNECTING) != 0)
650cb3c7a5dSArchie Cobbs 				ERROUT(EALREADY);
651cb3c7a5dSArchie Cobbs 			if ((error = soconnect(so, sa, p)) != 0) {
652cb3c7a5dSArchie Cobbs 				so->so_state &= ~SS_ISCONNECTING;
653cb3c7a5dSArchie Cobbs 				ERROUT(error);
654cb3c7a5dSArchie Cobbs 			}
655cb3c7a5dSArchie Cobbs 			if ((so->so_state & SS_ISCONNECTING) != 0)
656cb3c7a5dSArchie Cobbs 				/* Notify sender when we connect */
657cb3c7a5dSArchie Cobbs 				/* XXX implement me */
658cb3c7a5dSArchie Cobbs 				ERROUT(EINPROGRESS);
659cb3c7a5dSArchie Cobbs 			break;
660cb3c7a5dSArchie Cobbs 		    }
661cb3c7a5dSArchie Cobbs 
662cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETNAME:
663cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETPEERNAME:
664cb3c7a5dSArchie Cobbs 		    {
665f8307e12SArchie Cobbs 			int (*func)(struct socket *so, struct sockaddr **nam);
666f8307e12SArchie Cobbs 			struct sockaddr *sa = NULL;
667f8307e12SArchie Cobbs 			int len;
668f8307e12SArchie Cobbs 
669f8307e12SArchie Cobbs 			/* Sanity check */
670f8307e12SArchie Cobbs 			if (msg->header.arglen != 0)
671f8307e12SArchie Cobbs 				ERROUT(EINVAL);
672f8307e12SArchie Cobbs 			if (so == NULL)
673f8307e12SArchie Cobbs 				ERROUT(ENXIO);
674f8307e12SArchie Cobbs 
675f8307e12SArchie Cobbs 			/* Get function */
676f8307e12SArchie Cobbs 			if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
677f8307e12SArchie Cobbs 				if ((so->so_state
678f8307e12SArchie Cobbs 				    & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
679f8307e12SArchie Cobbs 					ERROUT(ENOTCONN);
680f8307e12SArchie Cobbs 				func = so->so_proto->pr_usrreqs->pru_peeraddr;
681f8307e12SArchie Cobbs 			} else
682f8307e12SArchie Cobbs 				func = so->so_proto->pr_usrreqs->pru_sockaddr;
683f8307e12SArchie Cobbs 
684f8307e12SArchie Cobbs 			/* Get local or peer address */
685f8307e12SArchie Cobbs 			if ((error = (*func)(so, &sa)) != 0)
686f8307e12SArchie Cobbs 				goto bail;
687f8307e12SArchie Cobbs 			len = (sa == NULL) ? 0 : sa->sa_len;
688f8307e12SArchie Cobbs 
689f8307e12SArchie Cobbs 			/* Send it back in a response */
690f8307e12SArchie Cobbs 			NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
691f8307e12SArchie Cobbs 			if (resp == NULL) {
692f8307e12SArchie Cobbs 				error = ENOMEM;
693f8307e12SArchie Cobbs 				goto bail;
694f8307e12SArchie Cobbs 			}
695f8307e12SArchie Cobbs 			bcopy(sa, resp->data, len);
696f8307e12SArchie Cobbs 
697f8307e12SArchie Cobbs 		bail:
698f8307e12SArchie Cobbs 			/* Cleanup */
699f8307e12SArchie Cobbs 			if (sa != NULL)
700f8307e12SArchie Cobbs 				FREE(sa, M_SONAME);
701cb3c7a5dSArchie Cobbs 			break;
702cb3c7a5dSArchie Cobbs 		    }
703cb3c7a5dSArchie Cobbs 
704cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_GETOPT:
705cb3c7a5dSArchie Cobbs 		    {
706f8307e12SArchie Cobbs 			struct ng_ksocket_sockopt *ksopt =
707f8307e12SArchie Cobbs 			    (struct ng_ksocket_sockopt *)msg->data;
708f8307e12SArchie Cobbs 			struct sockopt sopt;
709f8307e12SArchie Cobbs 
710f8307e12SArchie Cobbs 			/* Sanity check */
711f8307e12SArchie Cobbs 			if (msg->header.arglen != sizeof(*ksopt))
712f8307e12SArchie Cobbs 				ERROUT(EINVAL);
713f8307e12SArchie Cobbs 			if (so == NULL)
714f8307e12SArchie Cobbs 				ERROUT(ENXIO);
715f8307e12SArchie Cobbs 
716f8307e12SArchie Cobbs 			/* Get response with room for option value */
717f8307e12SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
718f8307e12SArchie Cobbs 			    + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
719f8307e12SArchie Cobbs 			if (resp == NULL)
720f8307e12SArchie Cobbs 				ERROUT(ENOMEM);
721f8307e12SArchie Cobbs 
722f8307e12SArchie Cobbs 			/* Get socket option, and put value in the response */
723f8307e12SArchie Cobbs 			sopt.sopt_dir = SOPT_GET;
724f8307e12SArchie Cobbs 			sopt.sopt_level = ksopt->level;
725f8307e12SArchie Cobbs 			sopt.sopt_name = ksopt->name;
726f8307e12SArchie Cobbs 			sopt.sopt_p = p;
727f8307e12SArchie Cobbs 			sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
728f8307e12SArchie Cobbs 			ksopt = (struct ng_ksocket_sockopt *)resp->data;
729f8307e12SArchie Cobbs 			sopt.sopt_val = ksopt->value;
730f8307e12SArchie Cobbs 			if ((error = sogetopt(so, &sopt)) != 0) {
731f8307e12SArchie Cobbs 				FREE(resp, M_NETGRAPH);
732f8307e12SArchie Cobbs 				break;
733f8307e12SArchie Cobbs 			}
734f8307e12SArchie Cobbs 
735f8307e12SArchie Cobbs 			/* Set actual value length */
736f8307e12SArchie Cobbs 			resp->header.arglen = sizeof(*ksopt)
737f8307e12SArchie Cobbs 			    + sopt.sopt_valsize;
738cb3c7a5dSArchie Cobbs 			break;
739cb3c7a5dSArchie Cobbs 		    }
740cb3c7a5dSArchie Cobbs 
741cb3c7a5dSArchie Cobbs 		case NGM_KSOCKET_SETOPT:
742cb3c7a5dSArchie Cobbs 		    {
743f8307e12SArchie Cobbs 			struct ng_ksocket_sockopt *const ksopt =
744f8307e12SArchie Cobbs 			    (struct ng_ksocket_sockopt *)msg->data;
745f8307e12SArchie Cobbs 			const int valsize = msg->header.arglen - sizeof(*ksopt);
746f8307e12SArchie Cobbs 			struct sockopt sopt;
747f8307e12SArchie Cobbs 
748f8307e12SArchie Cobbs 			/* Sanity check */
749f8307e12SArchie Cobbs 			if (valsize < 0)
750f8307e12SArchie Cobbs 				ERROUT(EINVAL);
751f8307e12SArchie Cobbs 			if (so == NULL)
752f8307e12SArchie Cobbs 				ERROUT(ENXIO);
753f8307e12SArchie Cobbs 
754f8307e12SArchie Cobbs 			/* Set socket option */
755f8307e12SArchie Cobbs 			sopt.sopt_dir = SOPT_SET;
756f8307e12SArchie Cobbs 			sopt.sopt_level = ksopt->level;
757f8307e12SArchie Cobbs 			sopt.sopt_name = ksopt->name;
758f8307e12SArchie Cobbs 			sopt.sopt_val = ksopt->value;
759f8307e12SArchie Cobbs 			sopt.sopt_valsize = valsize;
760f8307e12SArchie Cobbs 			sopt.sopt_p = p;
761f8307e12SArchie Cobbs 			error = sosetopt(so, &sopt);
762cb3c7a5dSArchie Cobbs 			break;
763cb3c7a5dSArchie Cobbs 		    }
764cb3c7a5dSArchie Cobbs 
765cb3c7a5dSArchie Cobbs 		default:
766cb3c7a5dSArchie Cobbs 			error = EINVAL;
767cb3c7a5dSArchie Cobbs 			break;
768cb3c7a5dSArchie Cobbs 		}
769cb3c7a5dSArchie Cobbs 		break;
770cb3c7a5dSArchie Cobbs 	default:
771cb3c7a5dSArchie Cobbs 		error = EINVAL;
772cb3c7a5dSArchie Cobbs 		break;
773cb3c7a5dSArchie Cobbs 	}
774cb3c7a5dSArchie Cobbs 	if (rptr)
775cb3c7a5dSArchie Cobbs 		*rptr = resp;
776cb3c7a5dSArchie Cobbs 	else if (resp)
777cb3c7a5dSArchie Cobbs 		FREE(resp, M_NETGRAPH);
778cb3c7a5dSArchie Cobbs 
779cb3c7a5dSArchie Cobbs done:
780cb3c7a5dSArchie Cobbs 	FREE(msg, M_NETGRAPH);
781cb3c7a5dSArchie Cobbs 	return (error);
782cb3c7a5dSArchie Cobbs }
783cb3c7a5dSArchie Cobbs 
784cb3c7a5dSArchie Cobbs /*
785cb3c7a5dSArchie Cobbs  * Receive incoming data on our hook.  Send it out the socket.
786cb3c7a5dSArchie Cobbs  */
787cb3c7a5dSArchie Cobbs static int
788cb3c7a5dSArchie Cobbs ng_ksocket_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
789cb3c7a5dSArchie Cobbs {
790f8307e12SArchie Cobbs 	struct proc *p = curproc ? curproc : &proc0;	/* XXX broken */
791cb3c7a5dSArchie Cobbs 	const node_p node = hook->node;
792cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
793cb3c7a5dSArchie Cobbs 	struct socket *const so = priv->so;
794cb3c7a5dSArchie Cobbs 	int error;
795cb3c7a5dSArchie Cobbs 
796cb3c7a5dSArchie Cobbs 	NG_FREE_META(meta);
797cb3c7a5dSArchie Cobbs 	error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, 0, 0, m, 0, 0, p);
798cb3c7a5dSArchie Cobbs 	return (error);
799cb3c7a5dSArchie Cobbs }
800cb3c7a5dSArchie Cobbs 
801cb3c7a5dSArchie Cobbs /*
802cb3c7a5dSArchie Cobbs  * Destroy node
803cb3c7a5dSArchie Cobbs  */
804cb3c7a5dSArchie Cobbs static int
805cb3c7a5dSArchie Cobbs ng_ksocket_rmnode(node_p node)
806cb3c7a5dSArchie Cobbs {
807cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
808cb3c7a5dSArchie Cobbs 
80919bff684SArchie Cobbs 	/* Close our socket (if any) */
81019bff684SArchie Cobbs 	if (priv->so != NULL) {
811f8307e12SArchie Cobbs 
812f8307e12SArchie Cobbs 		priv->so->so_upcall = NULL;
813f8307e12SArchie Cobbs 		priv->so->so_rcv.sb_flags &= ~SB_UPCALL;
81419bff684SArchie Cobbs 		soclose(priv->so);
81519bff684SArchie Cobbs 		priv->so = NULL;
81619bff684SArchie Cobbs 	}
81719bff684SArchie Cobbs 
818cb3c7a5dSArchie Cobbs 	/* Take down netgraph node */
819cb3c7a5dSArchie Cobbs 	node->flags |= NG_INVALID;
820cb3c7a5dSArchie Cobbs 	ng_cutlinks(node);
821cb3c7a5dSArchie Cobbs 	ng_unname(node);
822cb3c7a5dSArchie Cobbs 	bzero(priv, sizeof(*priv));
823cb3c7a5dSArchie Cobbs 	FREE(priv, M_NETGRAPH);
824cb3c7a5dSArchie Cobbs 	node->private = NULL;
825cb3c7a5dSArchie Cobbs 	ng_unref(node);		/* let the node escape */
826cb3c7a5dSArchie Cobbs 	return (0);
827cb3c7a5dSArchie Cobbs }
828cb3c7a5dSArchie Cobbs 
829cb3c7a5dSArchie Cobbs /*
830cb3c7a5dSArchie Cobbs  * Hook disconnection
831cb3c7a5dSArchie Cobbs  */
832cb3c7a5dSArchie Cobbs static int
833cb3c7a5dSArchie Cobbs ng_ksocket_disconnect(hook_p hook)
834cb3c7a5dSArchie Cobbs {
83519bff684SArchie Cobbs 	KASSERT(hook->node->numhooks == 0,
83619bff684SArchie Cobbs 	    ("%s: numhooks=%d?", __FUNCTION__, hook->node->numhooks));
837cb3c7a5dSArchie Cobbs 	ng_rmnode(hook->node);
838cb3c7a5dSArchie Cobbs 	return (0);
839cb3c7a5dSArchie Cobbs }
840cb3c7a5dSArchie Cobbs 
841cb3c7a5dSArchie Cobbs /************************************************************************
842cb3c7a5dSArchie Cobbs 			HELPER STUFF
843cb3c7a5dSArchie Cobbs  ************************************************************************/
844cb3c7a5dSArchie Cobbs 
845cb3c7a5dSArchie Cobbs /*
846cb3c7a5dSArchie Cobbs  * When incoming data is appended to the socket, we get notified here.
847cb3c7a5dSArchie Cobbs  */
848cb3c7a5dSArchie Cobbs static void
849cb3c7a5dSArchie Cobbs ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
850cb3c7a5dSArchie Cobbs {
851cb3c7a5dSArchie Cobbs 	const node_p node = arg;
852cb3c7a5dSArchie Cobbs 	const priv_p priv = node->private;
853cb3c7a5dSArchie Cobbs 	meta_p meta = NULL;
854cb3c7a5dSArchie Cobbs 	struct sockaddr *nam;
855cb3c7a5dSArchie Cobbs 	struct mbuf *m;
856cb3c7a5dSArchie Cobbs 	struct uio auio;
85719bff684SArchie Cobbs 	int s, flags, error;
85819bff684SArchie Cobbs 
85919bff684SArchie Cobbs 	s = splnet();
860cb3c7a5dSArchie Cobbs 
861cb3c7a5dSArchie Cobbs 	/* Sanity check */
86219bff684SArchie Cobbs 	if ((node->flags & NG_INVALID) != 0) {
86319bff684SArchie Cobbs 		splx(s);
864cb3c7a5dSArchie Cobbs 		return;
86519bff684SArchie Cobbs 	}
866cb3c7a5dSArchie Cobbs 	KASSERT(so == priv->so, ("%s: wrong socket", __FUNCTION__));
867cb3c7a5dSArchie Cobbs 	KASSERT(priv->hook != NULL, ("%s: no hook", __FUNCTION__));
868cb3c7a5dSArchie Cobbs 
869cb3c7a5dSArchie Cobbs 	/* Read and forward available mbuf's */
870cb3c7a5dSArchie Cobbs 	auio.uio_procp = NULL;
871cb3c7a5dSArchie Cobbs 	auio.uio_resid = 1000000000;
872cb3c7a5dSArchie Cobbs 	flags = MSG_DONTWAIT;
873cb3c7a5dSArchie Cobbs 	do {
874cb3c7a5dSArchie Cobbs 		if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive)
875f8307e12SArchie Cobbs 		      (so, &nam, &auio, &m, (struct mbuf **)0, &flags)) == 0
876f8307e12SArchie Cobbs 		    && m != NULL) {
877f8307e12SArchie Cobbs 			struct mbuf *n;
878f8307e12SArchie Cobbs 
879f8307e12SArchie Cobbs 			/* Don't trust the various socket layers to get the
880f8307e12SArchie Cobbs 			   packet header and length correct (eg. kern/15175) */
881f8307e12SArchie Cobbs 			for (n = m, m->m_pkthdr.len = 0; n; n = n->m_next)
882f8307e12SArchie Cobbs 				m->m_pkthdr.len += n->m_len;
883cb3c7a5dSArchie Cobbs 			NG_SEND_DATA(error, priv->hook, m, meta);
884f8307e12SArchie Cobbs 		}
885cb3c7a5dSArchie Cobbs 	} while (error == 0 && m != NULL);
88619bff684SArchie Cobbs 	splx(s);
887cb3c7a5dSArchie Cobbs }
888cb3c7a5dSArchie Cobbs 
889cb3c7a5dSArchie Cobbs /*
890cb3c7a5dSArchie Cobbs  * Parse out either an integer value or an alias.
891cb3c7a5dSArchie Cobbs  */
892cb3c7a5dSArchie Cobbs static int
893cb3c7a5dSArchie Cobbs ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
894cb3c7a5dSArchie Cobbs 	const char *s, int family)
895cb3c7a5dSArchie Cobbs {
896cb3c7a5dSArchie Cobbs 	int k, val;
89725792ef3SArchie Cobbs 	char *eptr;
898cb3c7a5dSArchie Cobbs 
899cb3c7a5dSArchie Cobbs 	/* Try aliases */
900cb3c7a5dSArchie Cobbs 	for (k = 0; aliases[k].name != NULL; k++) {
901cb3c7a5dSArchie Cobbs 		if (strcmp(s, aliases[k].name) == 0
902cb3c7a5dSArchie Cobbs 		    && aliases[k].family == family)
903cb3c7a5dSArchie Cobbs 			return aliases[k].value;
904cb3c7a5dSArchie Cobbs 	}
905cb3c7a5dSArchie Cobbs 
906cb3c7a5dSArchie Cobbs 	/* Try parsing as a number */
907cb3c7a5dSArchie Cobbs 	val = (int)strtoul(s, &eptr, 10);
908f8307e12SArchie Cobbs 	if (val < 0 || *eptr != '\0')
909cb3c7a5dSArchie Cobbs 		return (-1);
910cb3c7a5dSArchie Cobbs 	return (val);
911cb3c7a5dSArchie Cobbs }
912cb3c7a5dSArchie Cobbs 
913