streams.c revision 116546
1/*
2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
4 * Copyright (c) 1997 Todd Vierling
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Stolen from NetBSD /sys/compat/svr4/svr4_net.c.  Pseudo-device driver
30 * skeleton produced from /usr/share/examples/drivers/make_pseudo_driver.sh
31 * in 3.0-980524-SNAP then hacked a bit (but probably not enough :-).
32 *
33 * $FreeBSD: head/sys/dev/streams/streams.c 116546 2003-06-18 18:16:40Z phk $
34 */
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>		/* SYSINIT stuff */
39#include <sys/conf.h>		/* cdevsw stuff */
40#include <sys/malloc.h>		/* malloc region definitions */
41#include <sys/file.h>
42#include <sys/filedesc.h>
43#include <sys/unistd.h>
44#include <sys/fcntl.h>
45#include <sys/socket.h>
46#include <sys/protosw.h>
47#include <sys/socketvar.h>
48#include <sys/un.h>
49#include <sys/domain.h>
50#include <net/if.h>
51#include <netinet/in.h>
52#include <sys/proc.h>
53#include <sys/uio.h>
54
55#include <sys/sysproto.h>
56
57#include <compat/svr4/svr4_types.h>
58#include <compat/svr4/svr4_util.h>
59#include <compat/svr4/svr4_signal.h>
60#include <compat/svr4/svr4_ioctl.h>
61#include <compat/svr4/svr4_stropts.h>
62#include <compat/svr4/svr4_socket.h>
63
64static int svr4_soo_close(struct file *, struct thread *);
65static int svr4_ptm_alloc(struct thread *);
66static  d_open_t	streamsopen;
67
68struct svr4_sockcache_head svr4_head;
69
70/* Initialization flag (set/queried by svr4_mod LKM) */
71int svr4_str_initialized = 0;
72
73/*
74 * Device minor numbers
75 */
76enum {
77	dev_ptm			= 10,
78	dev_arp			= 26,
79	dev_icmp		= 27,
80	dev_ip			= 28,
81	dev_tcp			= 35,
82	dev_udp			= 36,
83	dev_rawip		= 37,
84	dev_unix_dgram		= 38,
85	dev_unix_stream		= 39,
86	dev_unix_ord_stream	= 40
87};
88
89static dev_t dt_ptm, dt_arp, dt_icmp, dt_ip, dt_tcp, dt_udp, dt_rawip,
90	dt_unix_dgram, dt_unix_stream, dt_unix_ord_stream;
91
92static struct fileops svr4_netops = {
93	.fo_read = soo_read,
94	.fo_write = soo_write,
95	.fo_ioctl = soo_ioctl,
96	.fo_poll = soo_poll,
97	.fo_kqfilter = soo_kqfilter,
98	.fo_stat = soo_stat,
99	.fo_close =  svr4_soo_close
100};
101
102#define CDEV_MAJOR 103
103static struct cdevsw streams_cdevsw = {
104	.d_open =	streamsopen,
105	.d_name =	"streams",
106	.d_maj =	CDEV_MAJOR,
107};
108
109struct streams_softc {
110	struct isa_device *dev;
111} ;
112
113#define UNIT(dev) minor(dev)	/* assume one minor number per unit */
114
115typedef	struct streams_softc *sc_p;
116
117static	int
118streams_modevent(module_t mod, int type, void *unused)
119{
120	switch (type) {
121	case MOD_LOAD:
122		/* XXX should make sure it isn't already loaded first */
123		dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
124			"ptm");
125		dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
126			"arp");
127		dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
128			"icmp");
129		dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
130			"ip");
131		dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
132			"tcp");
133		dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
134			"udp");
135		dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
136			"rawip");
137		dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
138			0, 0, 0666, "ticlts");
139		dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
140			0, 0, 0666, "ticots");
141		dt_unix_ord_stream = make_dev(&streams_cdevsw,
142			dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
143
144		if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
145				dt_udp && dt_rawip && dt_unix_dgram &&
146				dt_unix_stream && dt_unix_ord_stream)) {
147			printf("WARNING: device config for STREAMS failed\n");
148			printf("Suggest unloading streams KLD\n");
149		}
150		return 0;
151	case MOD_UNLOAD:
152	  	/* XXX should check to see if it's busy first */
153		destroy_dev(dt_ptm);
154		destroy_dev(dt_arp);
155		destroy_dev(dt_icmp);
156		destroy_dev(dt_ip);
157		destroy_dev(dt_tcp);
158		destroy_dev(dt_udp);
159		destroy_dev(dt_rawip);
160		destroy_dev(dt_unix_dgram);
161		destroy_dev(dt_unix_stream);
162		destroy_dev(dt_unix_ord_stream);
163
164		return 0;
165	default:
166		break;
167	}
168	return 0;
169}
170
171static moduledata_t streams_mod = {
172	"streams",
173	streams_modevent,
174	0
175};
176DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
177MODULE_VERSION(streams, 1);
178
179/*
180 * We only need open() and close() routines.  open() calls socreate()
181 * to allocate a "real" object behind the stream and mallocs some state
182 * info for use by the svr4 emulator;  close() deallocates the state
183 * information and passes the underlying object to the normal socket close
184 * routine.
185 */
186static  int
187streamsopen(dev_t dev, int oflags, int devtype, struct thread *td)
188{
189	int type, protocol;
190	int fd;
191	struct file *fp;
192	struct socket *so;
193	int error;
194	int family;
195	struct proc *p = td->td_proc;
196
197	PROC_LOCK(p);
198	if (td->td_dupfd >= 0) {
199	  PROC_UNLOCK(p);
200	  return ENODEV;
201	}
202	PROC_UNLOCK(p);
203
204	switch (minor(dev)) {
205	case dev_udp:
206	  family = AF_INET;
207	  type = SOCK_DGRAM;
208	  protocol = IPPROTO_UDP;
209	  break;
210
211	case dev_tcp:
212	  family = AF_INET;
213	  type = SOCK_STREAM;
214	  protocol = IPPROTO_TCP;
215	  break;
216
217	case dev_ip:
218	case dev_rawip:
219	  family = AF_INET;
220	  type = SOCK_RAW;
221	  protocol = IPPROTO_IP;
222	  break;
223
224	case dev_icmp:
225	  family = AF_INET;
226	  type = SOCK_RAW;
227	  protocol = IPPROTO_ICMP;
228	  break;
229
230	case dev_unix_dgram:
231	  family = AF_LOCAL;
232	  type = SOCK_DGRAM;
233	  protocol = 0;
234	  break;
235
236	case dev_unix_stream:
237	case dev_unix_ord_stream:
238	  family = AF_LOCAL;
239	  type = SOCK_STREAM;
240	  protocol = 0;
241	  break;
242
243	case dev_ptm:
244	  return svr4_ptm_alloc(td);
245
246	default:
247	  return EOPNOTSUPP;
248	}
249
250	if ((error = falloc(td, &fp, &fd)) != 0)
251	  return error;
252
253	if ((error = socreate(family, &so, type, protocol,
254	    td->td_ucred, td)) != 0) {
255	  FILEDESC_LOCK(p->p_fd);
256	  p->p_fd->fd_ofiles[fd] = 0;
257	  FILEDESC_UNLOCK(p->p_fd);
258	  ffree(fp);
259	  return error;
260	}
261
262	FILEDESC_LOCK(p->p_fd);
263	fp->f_data = so;
264	fp->f_flag = FREAD|FWRITE;
265	fp->f_ops = &svr4_netops;
266	fp->f_type = DTYPE_SOCKET;
267	FILEDESC_UNLOCK(p->p_fd);
268
269	(void)svr4_stream_get(fp);
270	PROC_LOCK(p);
271	td->td_dupfd = fd;
272	PROC_UNLOCK(p);
273	return ENXIO;
274}
275
276static int
277svr4_ptm_alloc(td)
278	struct thread *td;
279{
280	struct proc *p = td->td_proc;
281	/*
282	 * XXX this is very, very ugly.  But I can't find a better
283	 * way that won't duplicate a big amount of code from
284	 * sys_open().  Ho hum...
285	 *
286	 * Fortunately for us, Solaris (at least 2.5.1) makes the
287	 * /dev/ptmx open automatically just open a pty, that (after
288	 * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
289	 * to get the minor device number to map to a tty.
290	 *
291	 * Cycle through the names. If sys_open() returns ENOENT (or
292	 * ENXIO), short circuit the cycle and exit.
293	 */
294	static char ptyname[] = "/dev/ptyXX";
295	static char ttyletters[] = "pqrstuwxyzPQRST";
296	static char ttynumbers[] = "0123456789abcdef";
297	caddr_t sg = stackgap_init();
298	char *path = stackgap_alloc(&sg, sizeof(ptyname));
299	struct open_args oa;
300	int l = 0, n = 0;
301	register_t fd = -1;
302	int error;
303
304	oa.path = path;
305	oa.flags = O_RDWR;
306	oa.mode = 0;
307
308	while (fd == -1) {
309		ptyname[8] = ttyletters[l];
310		ptyname[9] = ttynumbers[n];
311
312		if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
313			return error;
314
315		switch (error = open(td, &oa)) {
316		case ENOENT:
317		case ENXIO:
318			return error;
319		case 0:
320			PROC_LOCK(p);
321			td->td_dupfd = td->td_retval[0];
322			PROC_UNLOCK(p);
323			return ENXIO;
324		default:
325			if (ttynumbers[++n] == '\0') {
326				if (ttyletters[++l] == '\0')
327					break;
328				n = 0;
329			}
330		}
331	}
332	return ENOENT;
333}
334
335
336struct svr4_strm *
337svr4_stream_get(fp)
338	struct file *fp;
339{
340	struct socket *so;
341	struct svr4_strm *st;
342
343	if (fp == NULL || fp->f_type != DTYPE_SOCKET)
344		return NULL;
345
346	so = fp->f_data;
347
348	/*
349	 * mpfixme: lock socketbuffer here
350	 */
351	if (so->so_emuldata) {
352		return so->so_emuldata;
353	}
354
355	/* Allocate a new one. */
356	st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
357	st->s_family = so->so_proto->pr_domain->dom_family;
358	st->s_cmd = ~0;
359	st->s_afd = -1;
360	st->s_eventmask = 0;
361	/*
362	 * avoid a race where we loose due to concurrancy issues
363	 * of two threads trying to allocate the so_emuldata.
364	 */
365	if (so->so_emuldata) {
366		/* lost the race, use the existing emuldata */
367		FREE(st, M_TEMP);
368		st = so->so_emuldata;
369	} else {
370		/* we won, or there was no race, use our copy */
371		so->so_emuldata = st;
372		fp->f_ops = &svr4_netops;
373	}
374
375	return st;
376}
377
378void
379svr4_delete_socket(p, fp)
380	struct proc *p;
381	struct file *fp;
382{
383	struct svr4_sockcache_entry *e;
384	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
385
386	while (svr4_str_initialized != 2) {
387		if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
388			TAILQ_INIT(&svr4_head);
389			atomic_store_rel_int(&svr4_str_initialized, 2);
390		}
391		return;
392	}
393
394	TAILQ_FOREACH(e, &svr4_head, entries)
395		if (e->p == p && e->cookie == cookie) {
396			TAILQ_REMOVE(&svr4_head, e, entries);
397			DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
398				 e->sock.sun_path, p, (int)e->dev, e->ino));
399			free(e, M_TEMP);
400			return;
401		}
402}
403
404static int
405svr4_soo_close(struct file *fp, struct thread *td)
406{
407        struct socket *so = fp->f_data;
408
409	/*	CHECKUNIT_DIAG(ENXIO);*/
410
411	svr4_delete_socket(td->td_proc, fp);
412	free(so->so_emuldata, M_TEMP);
413	return soo_close(fp, td);
414}
415