streams.c revision 71448
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 71448 2001-01-23 21:06:30Z jhb $
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 __P((struct file *, struct proc *));
65static int svr4_ptm_alloc __P((struct proc *));
66static  d_open_t	streamsopen;
67
68struct svr4_sockcache_entry {
69	struct proc *p;		/* Process for the socket		*/
70	void *cookie;		/* Internal cookie used for matching	*/
71	struct sockaddr_un sock;/* Pathname for the socket		*/
72	dev_t dev;		/* Device where the socket lives on	*/
73	ino_t ino;		/* Inode where the socket lives on	*/
74	TAILQ_ENTRY(svr4_sockcache_entry) entries;
75};
76
77TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
78
79/* Initialization flag (set/queried by svr4_mod LKM) */
80int svr4_str_initialized = 0;
81
82/*
83 * Device minor numbers
84 */
85enum {
86	dev_ptm			= 10,
87	dev_arp			= 26,
88	dev_icmp		= 27,
89	dev_ip			= 28,
90	dev_tcp			= 35,
91	dev_udp			= 36,
92	dev_rawip		= 37,
93	dev_unix_dgram		= 38,
94	dev_unix_stream		= 39,
95	dev_unix_ord_stream	= 40
96};
97
98dev_t dt_ptm, dt_arp, dt_icmp, dt_ip, dt_tcp, dt_udp, dt_rawip,
99	dt_unix_dgram, dt_unix_stream, dt_unix_ord_stream;
100
101static struct fileops svr4_netops = {
102	soo_read, soo_write, soo_ioctl, soo_poll, soo_stat, svr4_soo_close
103};
104
105#define CDEV_MAJOR 103
106static struct cdevsw streams_cdevsw = {
107	/* open */	streamsopen,
108	/* close */	noclose,
109	/* read */	noread,
110	/* write */	nowrite,
111	/* ioctl */	noioctl,
112	/* poll */	nopoll,
113	/* mmap */	nommap,
114	/* strategy */	nostrategy,
115	/* name */	"streams",
116	/* maj */	CDEV_MAJOR,
117	/* dump */	nodump,
118	/* psize */	nopsize,
119	/* flags */	0,
120	/* bmaj */	-1
121};
122
123struct streams_softc {
124	struct isa_device *dev;
125} ;
126
127#define UNIT(dev) minor(dev)	/* assume one minor number per unit */
128
129typedef	struct streams_softc *sc_p;
130
131static	int
132streams_modevent(module_t mod, int type, void *unused)
133{
134	switch (type) {
135	case MOD_LOAD:
136		/* XXX should make sure it isn't already loaded first */
137		dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
138			"ptm");
139		dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
140			"arp");
141		dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
142			"icmp");
143		dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
144			"ip");
145		dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
146			"tcp");
147		dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
148			"udp");
149		dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
150			"rawip");
151		dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
152			0, 0, 0666, "ticlts");
153		dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
154			0, 0, 0666, "ticots");
155		dt_unix_ord_stream = make_dev(&streams_cdevsw,
156			dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
157
158		if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
159				dt_udp && dt_rawip && dt_unix_dgram &&
160				dt_unix_stream && dt_unix_ord_stream)) {
161			printf("WARNING: device config for STREAMS failed\n");
162			printf("Suggest unloading streams KLD\n");
163		}
164		return 0;
165	case MOD_UNLOAD:
166	  	/* XXX should check to see if it's busy first */
167		destroy_dev(dt_ptm);
168		destroy_dev(dt_arp);
169		destroy_dev(dt_icmp);
170		destroy_dev(dt_ip);
171		destroy_dev(dt_tcp);
172		destroy_dev(dt_udp);
173		destroy_dev(dt_rawip);
174		destroy_dev(dt_unix_dgram);
175		destroy_dev(dt_unix_stream);
176		destroy_dev(dt_unix_ord_stream);
177
178		return 0;
179	default:
180		break;
181	}
182	return 0;
183}
184
185static moduledata_t streams_mod = {
186	"streams",
187	streams_modevent,
188	0
189};
190DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
191MODULE_VERSION(streams, 1);
192
193/*
194 * We only need open() and close() routines.  open() calls socreate()
195 * to allocate a "real" object behind the stream and mallocs some state
196 * info for use by the svr4 emulator;  close() deallocates the state
197 * information and passes the underlying object to the normal socket close
198 * routine.
199 */
200static  int
201streamsopen(dev_t dev, int oflags, int devtype, struct proc *p)
202{
203	int type, protocol;
204	int fd;
205	struct file *fp;
206	struct socket *so;
207	int error;
208	int family;
209
210	PROC_LOCK(p);
211	if (p->p_dupfd >= 0) {
212	  PROC_UNLOCK(p);
213	  return ENODEV;
214	}
215	PROC_UNLOCK(p);
216
217	switch (minor(dev)) {
218	case dev_udp:
219	  family = AF_INET;
220	  type = SOCK_DGRAM;
221	  protocol = IPPROTO_UDP;
222	  break;
223
224	case dev_tcp:
225	  family = AF_INET;
226	  type = SOCK_STREAM;
227	  protocol = IPPROTO_TCP;
228	  break;
229
230	case dev_ip:
231	case dev_rawip:
232	  family = AF_INET;
233	  type = SOCK_RAW;
234	  protocol = IPPROTO_IP;
235	  break;
236
237	case dev_icmp:
238	  family = AF_INET;
239	  type = SOCK_RAW;
240	  protocol = IPPROTO_ICMP;
241	  break;
242
243	case dev_unix_dgram:
244	  family = AF_LOCAL;
245	  type = SOCK_DGRAM;
246	  protocol = 0;
247	  break;
248
249	case dev_unix_stream:
250	case dev_unix_ord_stream:
251	  family = AF_LOCAL;
252	  type = SOCK_STREAM;
253	  protocol = 0;
254	  break;
255
256	case dev_ptm:
257	  return svr4_ptm_alloc(p);
258
259	default:
260	  return EOPNOTSUPP;
261	}
262
263	if ((error = falloc(p, &fp, &fd)) != 0)
264	  return error;
265
266	if ((error = socreate(family, &so, type, protocol, p)) != 0) {
267	  p->p_fd->fd_ofiles[fd] = 0;
268	  ffree(fp);
269	  return error;
270	}
271
272	fp->f_data = (caddr_t)so;
273	fp->f_flag = FREAD|FWRITE;
274	fp->f_ops = &svr4_netops;
275	fp->f_type = DTYPE_SOCKET;
276
277	(void)svr4_stream_get(fp);
278	PROC_LOCK(p);
279	p->p_dupfd = fd;
280	PROC_UNLOCK(p);
281	return ENXIO;
282}
283
284static int
285svr4_ptm_alloc(p)
286	struct proc *p;
287{
288	/*
289	 * XXX this is very, very ugly.  But I can't find a better
290	 * way that won't duplicate a big amount of code from
291	 * sys_open().  Ho hum...
292	 *
293	 * Fortunately for us, Solaris (at least 2.5.1) makes the
294	 * /dev/ptmx open automatically just open a pty, that (after
295	 * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
296	 * to get the minor device number to map to a tty.
297	 *
298	 * Cycle through the names. If sys_open() returns ENOENT (or
299	 * ENXIO), short circuit the cycle and exit.
300	 */
301	static char ptyname[] = "/dev/ptyXX";
302	static char ttyletters[] = "pqrstuwxyzPQRST";
303	static char ttynumbers[] = "0123456789abcdef";
304	caddr_t sg = stackgap_init();
305	char *path = stackgap_alloc(&sg, sizeof(ptyname));
306	struct open_args oa;
307	int l = 0, n = 0;
308	register_t fd = -1;
309	int error;
310
311	SCARG(&oa, path) = path;
312	SCARG(&oa, flags) = O_RDWR;
313	SCARG(&oa, mode) = 0;
314
315	while (fd == -1) {
316		ptyname[8] = ttyletters[l];
317		ptyname[9] = ttynumbers[n];
318
319		if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
320			return error;
321
322		switch (error = open(p, &oa)) {
323		case ENOENT:
324		case ENXIO:
325			return error;
326		case 0:
327			PROC_LOCK(p);
328			p->p_dupfd = p->p_retval[0];
329			PROC_UNLOCK(p);
330			return ENXIO;
331		default:
332			if (ttynumbers[++n] == '\0') {
333				if (ttyletters[++l] == '\0')
334					break;
335				n = 0;
336			}
337		}
338	}
339	return ENOENT;
340}
341
342
343struct svr4_strm *
344svr4_stream_get(fp)
345	struct file *fp;
346{
347	struct socket *so;
348	struct svr4_strm *st;
349
350	if (fp == NULL || fp->f_type != DTYPE_SOCKET)
351		return NULL;
352
353	so = (struct socket *) fp->f_data;
354
355       	if (so->so_emuldata)
356		return so->so_emuldata;
357
358	/* Allocate a new one. */
359	st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
360	st->s_family = so->so_proto->pr_domain->dom_family;
361	st->s_cmd = ~0;
362	st->s_afd = -1;
363	st->s_eventmask = 0;
364	so->so_emuldata = st;
365	fp->f_ops = &svr4_netops;
366
367	return st;
368}
369
370void
371svr4_delete_socket(p, fp)
372	struct proc *p;
373	struct file *fp;
374{
375	struct svr4_sockcache_entry *e;
376	void *cookie = ((struct socket *) fp->f_data)->so_emuldata;
377
378	while (svr4_str_initialized != 2) {
379		if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
380			TAILQ_INIT(&svr4_head);
381			atomic_store_rel_int(&svr4_str_initialized, 2);
382		}
383		return;
384	}
385
386	TAILQ_FOREACH(e, &svr4_head, entries)
387		if (e->p == p && e->cookie == cookie) {
388			TAILQ_REMOVE(&svr4_head, e, entries);
389			DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
390				 e->sock.sun_path, p, e->dev, e->ino));
391			free(e, M_TEMP);
392			return;
393		}
394}
395
396static int
397svr4_soo_close(struct file *fp, struct proc *p)
398{
399        struct socket *so = (struct socket *)fp->f_data;
400
401	/*	CHECKUNIT_DIAG(ENXIO);*/
402
403	svr4_delete_socket(p, fp);
404	free(so->so_emuldata, M_TEMP);
405	return soo_close(fp, p);
406	return (0);
407}
408