streams.c revision 171744
1311128Sdim/*-
2311128Sdim * Copyright (c) 1998 Mark Newton
3311128Sdim * Copyright (c) 1994 Christos Zoulas
4311128Sdim * Copyright (c) 1997 Todd Vierling
5311128Sdim * All rights reserved.
6311128Sdim *
7311128Sdim * Redistribution and use in source and binary forms, with or without
8311128Sdim * modification, are permitted provided that the following conditions
9311128Sdim * are met:
10311128Sdim * 1. Redistributions of source code must retain the above copyright
11311128Sdim *    notice, this list of conditions and the following disclaimer.
12311128Sdim * 2. Redistributions in binary form must reproduce the above copyright
13311128Sdim *    notice, this list of conditions and the following disclaimer in the
14311128Sdim *    documentation and/or other materials provided with the distribution.
15311128Sdim * 3. The names of the authors may not be used to endorse or promote products
16311128Sdim *    derived from this software without specific prior written permission
17311128Sdim *
18311128Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19311128Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20311128Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21311128Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22311128Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23311128Sdim * 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 171744 2007-08-06 14:26:03Z rwatson $");
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/syscallsubr.h>
51#include <sys/un.h>
52#include <sys/domain.h>
53#include <net/if.h>
54#include <netinet/in.h>
55#include <sys/proc.h>
56#include <sys/uio.h>
57
58#include <sys/sysproto.h>
59
60#include <compat/svr4/svr4_types.h>
61#include <compat/svr4/svr4_util.h>
62#include <compat/svr4/svr4_signal.h>
63#include <compat/svr4/svr4_ioctl.h>
64#include <compat/svr4/svr4_stropts.h>
65#include <compat/svr4/svr4_socket.h>
66
67static int svr4_soo_close(struct file *, struct thread *);
68static int svr4_ptm_alloc(struct thread *);
69static  d_open_t	streamsopen;
70
71/*
72 * Device minor numbers
73 */
74enum {
75	dev_ptm			= 10,
76	dev_arp			= 26,
77	dev_icmp		= 27,
78	dev_ip			= 28,
79	dev_tcp			= 35,
80	dev_udp			= 36,
81	dev_rawip		= 37,
82	dev_unix_dgram		= 38,
83	dev_unix_stream		= 39,
84	dev_unix_ord_stream	= 40
85};
86
87static struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp,
88	*dt_rawip, *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
89
90static struct fileops svr4_netops = {
91	.fo_read = soo_read,
92	.fo_write = soo_write,
93	.fo_ioctl = soo_ioctl,
94	.fo_poll = soo_poll,
95	.fo_kqfilter = soo_kqfilter,
96	.fo_stat = soo_stat,
97	.fo_close =  svr4_soo_close
98};
99
100static struct cdevsw streams_cdevsw = {
101	.d_version =	D_VERSION,
102	.d_open =	streamsopen,
103	.d_name =	"streams",
104};
105
106struct streams_softc {
107	struct isa_device *dev;
108} ;
109
110#define UNIT(dev) minor(dev)	/* assume one minor number per unit */
111
112typedef	struct streams_softc *sc_p;
113
114static	int
115streams_modevent(module_t mod, int type, void *unused)
116{
117	switch (type) {
118	case MOD_LOAD:
119		dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
120			"ptm");
121		dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
122			"arp");
123		dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
124			"icmp");
125		dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
126			"ip");
127		dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
128			"tcp");
129		dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
130			"udp");
131		dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
132			"rawip");
133		dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
134			0, 0, 0666, "ticlts");
135		dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
136			0, 0, 0666, "ticots");
137		dt_unix_ord_stream = make_dev(&streams_cdevsw,
138			dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
139
140		if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
141				dt_udp && dt_rawip && dt_unix_dgram &&
142				dt_unix_stream && dt_unix_ord_stream)) {
143			printf("WARNING: device config for STREAMS failed\n");
144			printf("Suggest unloading streams KLD\n");
145		}
146		return 0;
147	case MOD_UNLOAD:
148	  	/* XXX should check to see if it's busy first */
149		destroy_dev(dt_ptm);
150		destroy_dev(dt_arp);
151		destroy_dev(dt_icmp);
152		destroy_dev(dt_ip);
153		destroy_dev(dt_tcp);
154		destroy_dev(dt_udp);
155		destroy_dev(dt_rawip);
156		destroy_dev(dt_unix_dgram);
157		destroy_dev(dt_unix_stream);
158		destroy_dev(dt_unix_ord_stream);
159
160		return 0;
161	default:
162		return EOPNOTSUPP;
163		break;
164	}
165	return 0;
166}
167
168static moduledata_t streams_mod = {
169	"streams",
170	streams_modevent,
171	0
172};
173DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
174MODULE_VERSION(streams, 1);
175
176/*
177 * We only need open() and close() routines.  open() calls socreate()
178 * to allocate a "real" object behind the stream and mallocs some state
179 * info for use by the svr4 emulator;  close() deallocates the state
180 * information and passes the underlying object to the normal socket close
181 * routine.
182 */
183static  int
184streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
185{
186	struct filedesc *fdp;
187	struct svr4_strm *st;
188	struct socket *so;
189	struct file *fp;
190	int family, type, protocol;
191	int error, fd;
192
193	if (td->td_dupfd >= 0)
194	  return ENODEV;
195
196	switch (minor(dev)) {
197	case dev_udp:
198	  family = AF_INET;
199	  type = SOCK_DGRAM;
200	  protocol = IPPROTO_UDP;
201	  break;
202
203	case dev_tcp:
204	  family = AF_INET;
205	  type = SOCK_STREAM;
206	  protocol = IPPROTO_TCP;
207	  break;
208
209	case dev_ip:
210	case dev_rawip:
211	  family = AF_INET;
212	  type = SOCK_RAW;
213	  protocol = IPPROTO_IP;
214	  break;
215
216	case dev_icmp:
217	  family = AF_INET;
218	  type = SOCK_RAW;
219	  protocol = IPPROTO_ICMP;
220	  break;
221
222	case dev_unix_dgram:
223	  family = AF_LOCAL;
224	  type = SOCK_DGRAM;
225	  protocol = 0;
226	  break;
227
228	case dev_unix_stream:
229	case dev_unix_ord_stream:
230	  family = AF_LOCAL;
231	  type = SOCK_STREAM;
232	  protocol = 0;
233	  break;
234
235	case dev_ptm:
236	  return svr4_ptm_alloc(td);
237
238	default:
239	  return EOPNOTSUPP;
240	}
241
242	fdp = td->td_proc->p_fd;
243	if ((error = falloc(td, &fp, &fd)) != 0)
244	  return error;
245	/* An extra reference on `fp' has been held for us by falloc(). */
246
247	error = socreate(family, &so, type, protocol, td->td_ucred, td);
248	if (error) {
249	   fdclose(fdp, fp, fd, td);
250	   fdrop(fp, td);
251	   return error;
252	}
253
254	FILE_LOCK(fp);
255	fp->f_data = so;
256	fp->f_flag = FREAD|FWRITE;
257	fp->f_ops = &svr4_netops;
258	fp->f_type = DTYPE_SOCKET;
259	FILE_UNLOCK(fp);
260
261	/*
262	 * Allocate a stream structure and attach it to this socket.
263	 * We don't bother locking so_emuldata for SVR4 stream sockets as
264	 * its value is constant for the lifetime of the stream once it
265	 * is initialized here.
266	 */
267	st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
268	st->s_family = so->so_proto->pr_domain->dom_family;
269	st->s_cmd = ~0;
270	st->s_afd = -1;
271	st->s_eventmask = 0;
272	so->so_emuldata = st;
273
274	fdrop(fp, td);
275	td->td_dupfd = fd;
276	return ENXIO;
277}
278
279static int
280svr4_ptm_alloc(td)
281	struct thread *td;
282{
283	/*
284	 * XXX this is very, very ugly.  But I can't find a better
285	 * way that won't duplicate a big amount of code from
286	 * sys_open().  Ho hum...
287	 *
288	 * Fortunately for us, Solaris (at least 2.5.1) makes the
289	 * /dev/ptmx open automatically just open a pty, that (after
290	 * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
291	 * to get the minor device number to map to a tty.
292	 *
293	 * Cycle through the names. If sys_open() returns ENOENT (or
294	 * ENXIO), short circuit the cycle and exit.
295	 */
296	static char ptyname[] = "/dev/ptyXX";
297	static char ttyletters[] = "pqrstuwxyzPQRST";
298	static char ttynumbers[] = "0123456789abcdef";
299	struct proc *p;
300	register_t fd;
301	int error, l, n;
302
303	fd = -1;
304	n = 0;
305	l = 0;
306	p = td->td_proc;
307	while (fd == -1) {
308		ptyname[8] = ttyletters[l];
309		ptyname[9] = ttynumbers[n];
310
311		error = kern_open(td, ptyname, UIO_SYSSPACE, O_RDWR, 0);
312		switch (error) {
313		case ENOENT:
314		case ENXIO:
315			return error;
316		case 0:
317			td->td_dupfd = td->td_retval[0];
318			return ENXIO;
319		default:
320			if (ttynumbers[++n] == '\0') {
321				if (ttyletters[++l] == '\0')
322					break;
323				n = 0;
324			}
325		}
326	}
327	return ENOENT;
328}
329
330
331struct svr4_strm *
332svr4_stream_get(fp)
333	struct file *fp;
334{
335	struct socket *so;
336
337	if (fp == NULL || fp->f_type != DTYPE_SOCKET)
338		return NULL;
339
340	so = fp->f_data;
341	return so->so_emuldata;
342}
343
344static int
345svr4_soo_close(struct file *fp, struct thread *td)
346{
347        struct socket *so = fp->f_data;
348
349	/*	CHECKUNIT_DIAG(ENXIO);*/
350
351	svr4_delete_socket(td->td_proc, fp);
352	free(so->so_emuldata, M_TEMP);
353	return soo_close(fp, td);
354}
355