streams.c revision 130585
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 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/dev/streams/streams.c 130585 2004-06-16 09:47:26Z phk $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>		/* SYSINIT stuff */
41#include <sys/conf.h>		/* cdevsw stuff */
42#include <sys/malloc.h>		/* malloc region definitions */
43#include <sys/file.h>
44#include <sys/filedesc.h>
45#include <sys/unistd.h>
46#include <sys/fcntl.h>
47#include <sys/socket.h>
48#include <sys/protosw.h>
49#include <sys/socketvar.h>
50#include <sys/un.h>
51#include <sys/domain.h>
52#include <net/if.h>
53#include <netinet/in.h>
54#include <sys/proc.h>
55#include <sys/uio.h>
56
57#include <sys/sysproto.h>
58
59#include <compat/svr4/svr4_types.h>
60#include <compat/svr4/svr4_util.h>
61#include <compat/svr4/svr4_signal.h>
62#include <compat/svr4/svr4_ioctl.h>
63#include <compat/svr4/svr4_stropts.h>
64#include <compat/svr4/svr4_socket.h>
65
66static int svr4_soo_close(struct file *, struct thread *);
67static int svr4_ptm_alloc(struct thread *);
68static  d_open_t	streamsopen;
69
70struct svr4_sockcache_head svr4_head;
71
72/* Initialization flag (set/queried by svr4_mod LKM) */
73int svr4_str_initialized = 0;
74
75/*
76 * Device minor numbers
77 */
78enum {
79	dev_ptm			= 10,
80	dev_arp			= 26,
81	dev_icmp		= 27,
82	dev_ip			= 28,
83	dev_tcp			= 35,
84	dev_udp			= 36,
85	dev_rawip		= 37,
86	dev_unix_dgram		= 38,
87	dev_unix_stream		= 39,
88	dev_unix_ord_stream	= 40
89};
90
91static struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp, *dt_rawip,
92	*dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
93
94static struct fileops svr4_netops = {
95	.fo_read = soo_read,
96	.fo_write = soo_write,
97	.fo_ioctl = soo_ioctl,
98	.fo_poll = soo_poll,
99	.fo_kqfilter = soo_kqfilter,
100	.fo_stat = soo_stat,
101	.fo_close =  svr4_soo_close
102};
103
104static struct cdevsw streams_cdevsw = {
105	.d_version =	D_VERSION,
106	.d_flags =	D_NEEDGIANT,
107	.d_open =	streamsopen,
108	.d_name =	"streams",
109};
110
111struct streams_softc {
112	struct isa_device *dev;
113} ;
114
115#define UNIT(dev) minor(dev)	/* assume one minor number per unit */
116
117typedef	struct streams_softc *sc_p;
118
119static	int
120streams_modevent(module_t mod, int type, void *unused)
121{
122	switch (type) {
123	case MOD_LOAD:
124		/* XXX should make sure it isn't already loaded first */
125		dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
126			"ptm");
127		dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
128			"arp");
129		dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
130			"icmp");
131		dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
132			"ip");
133		dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
134			"tcp");
135		dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
136			"udp");
137		dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
138			"rawip");
139		dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
140			0, 0, 0666, "ticlts");
141		dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
142			0, 0, 0666, "ticots");
143		dt_unix_ord_stream = make_dev(&streams_cdevsw,
144			dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
145
146		if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
147				dt_udp && dt_rawip && dt_unix_dgram &&
148				dt_unix_stream && dt_unix_ord_stream)) {
149			printf("WARNING: device config for STREAMS failed\n");
150			printf("Suggest unloading streams KLD\n");
151		}
152		return 0;
153	case MOD_UNLOAD:
154	  	/* XXX should check to see if it's busy first */
155		destroy_dev(dt_ptm);
156		destroy_dev(dt_arp);
157		destroy_dev(dt_icmp);
158		destroy_dev(dt_ip);
159		destroy_dev(dt_tcp);
160		destroy_dev(dt_udp);
161		destroy_dev(dt_rawip);
162		destroy_dev(dt_unix_dgram);
163		destroy_dev(dt_unix_stream);
164		destroy_dev(dt_unix_ord_stream);
165
166		return 0;
167	default:
168		break;
169	}
170	return 0;
171}
172
173static moduledata_t streams_mod = {
174	"streams",
175	streams_modevent,
176	0
177};
178DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
179MODULE_VERSION(streams, 1);
180
181/*
182 * We only need open() and close() routines.  open() calls socreate()
183 * to allocate a "real" object behind the stream and mallocs some state
184 * info for use by the svr4 emulator;  close() deallocates the state
185 * information and passes the underlying object to the normal socket close
186 * routine.
187 */
188static  int
189streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
190{
191	int type, protocol;
192	int fd, extraref;
193	struct file *fp;
194	struct socket *so;
195	int error;
196	int family;
197	struct proc *p = td->td_proc;
198
199	PROC_LOCK(p);
200	if (td->td_dupfd >= 0) {
201	  PROC_UNLOCK(p);
202	  return ENODEV;
203	}
204	PROC_UNLOCK(p);
205
206	switch (minor(dev)) {
207	case dev_udp:
208	  family = AF_INET;
209	  type = SOCK_DGRAM;
210	  protocol = IPPROTO_UDP;
211	  break;
212
213	case dev_tcp:
214	  family = AF_INET;
215	  type = SOCK_STREAM;
216	  protocol = IPPROTO_TCP;
217	  break;
218
219	case dev_ip:
220	case dev_rawip:
221	  family = AF_INET;
222	  type = SOCK_RAW;
223	  protocol = IPPROTO_IP;
224	  break;
225
226	case dev_icmp:
227	  family = AF_INET;
228	  type = SOCK_RAW;
229	  protocol = IPPROTO_ICMP;
230	  break;
231
232	case dev_unix_dgram:
233	  family = AF_LOCAL;
234	  type = SOCK_DGRAM;
235	  protocol = 0;
236	  break;
237
238	case dev_unix_stream:
239	case dev_unix_ord_stream:
240	  family = AF_LOCAL;
241	  type = SOCK_STREAM;
242	  protocol = 0;
243	  break;
244
245	case dev_ptm:
246	  return svr4_ptm_alloc(td);
247
248	default:
249	  return EOPNOTSUPP;
250	}
251
252	if ((error = falloc(td, &fp, &fd)) != 0)
253	  return error;
254	/* An extra reference on `fp' has been held for us by falloc(). */
255
256	if ((error = socreate(family, &so, type, protocol,
257	    td->td_ucred, td)) != 0) {
258	  FILEDESC_LOCK(p->p_fd);
259	  /* Check the fd table entry hasn't changed since we made it. */
260	  extraref = 0;
261	  if (p->p_fd->fd_ofiles[fd] == fp) {
262	    p->p_fd->fd_ofiles[fd] = NULL;
263	    extraref = 1;
264	  }
265	  FILEDESC_UNLOCK(p->p_fd);
266	  if (extraref)
267	    fdrop(fp, td);
268	  fdrop(fp, td);
269	  return error;
270	}
271
272	FILEDESC_LOCK(p->p_fd);
273	fp->f_data = so;
274	fp->f_flag = FREAD|FWRITE;
275	fp->f_ops = &svr4_netops;
276	fp->f_type = DTYPE_SOCKET;
277	FILEDESC_UNLOCK(p->p_fd);
278
279	(void)svr4_stream_get(fp);
280	fdrop(fp, td);
281	PROC_LOCK(p);
282	td->td_dupfd = fd;
283	PROC_UNLOCK(p);
284	return ENXIO;
285}
286
287static int
288svr4_ptm_alloc(td)
289	struct thread *td;
290{
291	struct proc *p = td->td_proc;
292	/*
293	 * XXX this is very, very ugly.  But I can't find a better
294	 * way that won't duplicate a big amount of code from
295	 * sys_open().  Ho hum...
296	 *
297	 * Fortunately for us, Solaris (at least 2.5.1) makes the
298	 * /dev/ptmx open automatically just open a pty, that (after
299	 * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
300	 * to get the minor device number to map to a tty.
301	 *
302	 * Cycle through the names. If sys_open() returns ENOENT (or
303	 * ENXIO), short circuit the cycle and exit.
304	 */
305	static char ptyname[] = "/dev/ptyXX";
306	static char ttyletters[] = "pqrstuwxyzPQRST";
307	static char ttynumbers[] = "0123456789abcdef";
308	caddr_t sg = stackgap_init();
309	char *path = stackgap_alloc(&sg, sizeof(ptyname));
310	struct open_args oa;
311	int l = 0, n = 0;
312	register_t fd = -1;
313	int error;
314
315	oa.path = path;
316	oa.flags = O_RDWR;
317	oa.mode = 0;
318
319	while (fd == -1) {
320		ptyname[8] = ttyletters[l];
321		ptyname[9] = ttynumbers[n];
322
323		if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
324			return error;
325
326		switch (error = open(td, &oa)) {
327		case ENOENT:
328		case ENXIO:
329			return error;
330		case 0:
331			PROC_LOCK(p);
332			td->td_dupfd = td->td_retval[0];
333			PROC_UNLOCK(p);
334			return ENXIO;
335		default:
336			if (ttynumbers[++n] == '\0') {
337				if (ttyletters[++l] == '\0')
338					break;
339				n = 0;
340			}
341		}
342	}
343	return ENOENT;
344}
345
346
347struct svr4_strm *
348svr4_stream_get(fp)
349	struct file *fp;
350{
351	struct socket *so;
352	struct svr4_strm *st;
353
354	if (fp == NULL || fp->f_type != DTYPE_SOCKET)
355		return NULL;
356
357	so = fp->f_data;
358
359	/*
360	 * mpfixme: lock socketbuffer here
361	 */
362	if (so->so_emuldata) {
363		return so->so_emuldata;
364	}
365
366	/* Allocate a new one. */
367	st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
368	st->s_family = so->so_proto->pr_domain->dom_family;
369	st->s_cmd = ~0;
370	st->s_afd = -1;
371	st->s_eventmask = 0;
372	/*
373	 * avoid a race where we loose due to concurrancy issues
374	 * of two threads trying to allocate the so_emuldata.
375	 */
376	if (so->so_emuldata) {
377		/* lost the race, use the existing emuldata */
378		FREE(st, M_TEMP);
379		st = so->so_emuldata;
380	} else {
381		/* we won, or there was no race, use our copy */
382		so->so_emuldata = st;
383		fp->f_ops = &svr4_netops;
384	}
385
386	return st;
387}
388
389void
390svr4_delete_socket(p, fp)
391	struct proc *p;
392	struct file *fp;
393{
394	struct svr4_sockcache_entry *e;
395	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
396
397	while (svr4_str_initialized != 2) {
398		if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
399			TAILQ_INIT(&svr4_head);
400			atomic_store_rel_int(&svr4_str_initialized, 2);
401		}
402		return;
403	}
404
405	TAILQ_FOREACH(e, &svr4_head, entries)
406		if (e->p == p && e->cookie == cookie) {
407			TAILQ_REMOVE(&svr4_head, e, entries);
408			DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
409				 e->sock.sun_path, p, (int)e->dev, e->ino));
410			free(e, M_TEMP);
411			return;
412		}
413}
414
415static int
416svr4_soo_close(struct file *fp, struct thread *td)
417{
418        struct socket *so = fp->f_data;
419
420	/*	CHECKUNIT_DIAG(ENXIO);*/
421
422	svr4_delete_socket(td->td_proc, fp);
423	free(so->so_emuldata, M_TEMP);
424	return soo_close(fp, td);
425}
426