streams.c revision 141466
1199481Srdivacky/*-
2199481Srdivacky * Copyright (c) 1998 Mark Newton
3199481Srdivacky * Copyright (c) 1994 Christos Zoulas
4199481Srdivacky * Copyright (c) 1997 Todd Vierling
5199481Srdivacky * All rights reserved.
6199481Srdivacky *
7199481Srdivacky * Redistribution and use in source and binary forms, with or without
8199481Srdivacky * modification, are permitted provided that the following conditions
9199481Srdivacky * are met:
10199481Srdivacky * 1. Redistributions of source code must retain the above copyright
11199481Srdivacky *    notice, this list of conditions and the following disclaimer.
12199481Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
13199481Srdivacky *    notice, this list of conditions and the following disclaimer in the
14199481Srdivacky *    documentation and/or other materials provided with the distribution.
15199481Srdivacky * 3. The names of the authors may not be used to endorse or promote products
16199481Srdivacky *    derived from this software without specific prior written permission
17199481Srdivacky *
18199481Srdivacky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19199481Srdivacky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20199481Srdivacky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21199481Srdivacky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22199481Srdivacky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23199481Srdivacky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24199481Srdivacky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25199481Srdivacky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26199481Srdivacky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27199481Srdivacky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28199481Srdivacky *
29199481Srdivacky * Stolen from NetBSD /sys/compat/svr4/svr4_net.c.  Pseudo-device driver
30199481Srdivacky * skeleton produced from /usr/share/examples/drivers/make_pseudo_driver.sh
31199481Srdivacky * in 3.0-980524-SNAP then hacked a bit (but probably not enough :-).
32199481Srdivacky *
33199481Srdivacky */
34199481Srdivacky
35199481Srdivacky#include <sys/cdefs.h>
36199481Srdivacky__FBSDID("$FreeBSD: head/sys/dev/streams/streams.c 141466 2005-02-07 18:22:20Z jhb $");
37199481Srdivacky
38199481Srdivacky#include <sys/param.h>
39199481Srdivacky#include <sys/systm.h>
40199481Srdivacky#include <sys/kernel.h>		/* SYSINIT stuff */
41199481Srdivacky#include <sys/conf.h>		/* cdevsw stuff */
42199481Srdivacky#include <sys/malloc.h>		/* malloc region definitions */
43199481Srdivacky#include <sys/file.h>
44199481Srdivacky#include <sys/filedesc.h>
45199481Srdivacky#include <sys/unistd.h>
46199481Srdivacky#include <sys/fcntl.h>
47199481Srdivacky#include <sys/socket.h>
48199481Srdivacky#include <sys/protosw.h>
49199481Srdivacky#include <sys/socketvar.h>
50199481Srdivacky#include <sys/syscallsubr.h>
51199481Srdivacky#include <sys/un.h>
52199481Srdivacky#include <sys/domain.h>
53199481Srdivacky#include <net/if.h>
54199481Srdivacky#include <netinet/in.h>
55199481Srdivacky#include <sys/proc.h>
56199481Srdivacky#include <sys/uio.h>
57199481Srdivacky
58199481Srdivacky#include <sys/sysproto.h>
59199481Srdivacky
60199481Srdivacky#include <compat/svr4/svr4_types.h>
61199481Srdivacky#include <compat/svr4/svr4_util.h>
62199481Srdivacky#include <compat/svr4/svr4_signal.h>
63199481Srdivacky#include <compat/svr4/svr4_ioctl.h>
64199481Srdivacky#include <compat/svr4/svr4_stropts.h>
65199481Srdivacky#include <compat/svr4/svr4_socket.h>
66199481Srdivacky
67199481Srdivackystatic int svr4_soo_close(struct file *, struct thread *);
68199481Srdivackystatic int svr4_ptm_alloc(struct thread *);
69199481Srdivackystatic  d_open_t	streamsopen;
70199481Srdivacky
71199481Srdivackystruct svr4_sockcache_head svr4_head;
72199481Srdivacky
73199481Srdivacky/* Initialization flag (set/queried by svr4_mod LKM) */
74199481Srdivackyint svr4_str_initialized = 0;
75199481Srdivacky
76199481Srdivacky/*
77199481Srdivacky * Device minor numbers
78199481Srdivacky */
79199989Srdivackyenum {
80199989Srdivacky	dev_ptm			= 10,
81199989Srdivacky	dev_arp			= 26,
82199989Srdivacky	dev_icmp		= 27,
83199989Srdivacky	dev_ip			= 28,
84199989Srdivacky	dev_tcp			= 35,
85199989Srdivacky	dev_udp			= 36,
86199989Srdivacky	dev_rawip		= 37,
87199989Srdivacky	dev_unix_dgram		= 38,
88199989Srdivacky	dev_unix_stream		= 39,
89199989Srdivacky	dev_unix_ord_stream	= 40
90199989Srdivacky};
91199989Srdivacky
92199989Srdivackystatic struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp, *dt_rawip,
93199989Srdivacky	*dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
94199989Srdivacky
95199989Srdivackystatic struct fileops svr4_netops = {
96199989Srdivacky	.fo_read = soo_read,
97199989Srdivacky	.fo_write = soo_write,
98199989Srdivacky	.fo_ioctl = soo_ioctl,
99199989Srdivacky	.fo_poll = soo_poll,
100199989Srdivacky	.fo_kqfilter = soo_kqfilter,
101199989Srdivacky	.fo_stat = soo_stat,
102199481Srdivacky	.fo_close =  svr4_soo_close
103199481Srdivacky};
104199481Srdivacky
105199481Srdivackystatic struct cdevsw streams_cdevsw = {
106199481Srdivacky	.d_version =	D_VERSION,
107199481Srdivacky	.d_flags =	D_NEEDGIANT,
108199481Srdivacky	.d_open =	streamsopen,
109199481Srdivacky	.d_name =	"streams",
110199481Srdivacky};
111199481Srdivacky
112199481Srdivackystruct streams_softc {
113199481Srdivacky	struct isa_device *dev;
114199481Srdivacky} ;
115199481Srdivacky
116199481Srdivacky#define UNIT(dev) minor(dev)	/* assume one minor number per unit */
117199481Srdivacky
118199481Srdivackytypedef	struct streams_softc *sc_p;
119199481Srdivacky
120199481Srdivackystatic	int
121199481Srdivackystreams_modevent(module_t mod, int type, void *unused)
122199481Srdivacky{
123199481Srdivacky	switch (type) {
124199481Srdivacky	case MOD_LOAD:
125199481Srdivacky		/* XXX should make sure it isn't already loaded first */
126199481Srdivacky		dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
127199481Srdivacky			"ptm");
128199481Srdivacky		dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
129			"arp");
130		dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
131			"icmp");
132		dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
133			"ip");
134		dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
135			"tcp");
136		dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
137			"udp");
138		dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
139			"rawip");
140		dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
141			0, 0, 0666, "ticlts");
142		dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
143			0, 0, 0666, "ticots");
144		dt_unix_ord_stream = make_dev(&streams_cdevsw,
145			dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
146
147		if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
148				dt_udp && dt_rawip && dt_unix_dgram &&
149				dt_unix_stream && dt_unix_ord_stream)) {
150			printf("WARNING: device config for STREAMS failed\n");
151			printf("Suggest unloading streams KLD\n");
152		}
153		return 0;
154	case MOD_UNLOAD:
155	  	/* XXX should check to see if it's busy first */
156		destroy_dev(dt_ptm);
157		destroy_dev(dt_arp);
158		destroy_dev(dt_icmp);
159		destroy_dev(dt_ip);
160		destroy_dev(dt_tcp);
161		destroy_dev(dt_udp);
162		destroy_dev(dt_rawip);
163		destroy_dev(dt_unix_dgram);
164		destroy_dev(dt_unix_stream);
165		destroy_dev(dt_unix_ord_stream);
166
167		return 0;
168	default:
169		return EOPNOTSUPP;
170		break;
171	}
172	return 0;
173}
174
175static moduledata_t streams_mod = {
176	"streams",
177	streams_modevent,
178	0
179};
180DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
181MODULE_VERSION(streams, 1);
182
183/*
184 * We only need open() and close() routines.  open() calls socreate()
185 * to allocate a "real" object behind the stream and mallocs some state
186 * info for use by the svr4 emulator;  close() deallocates the state
187 * information and passes the underlying object to the normal socket close
188 * routine.
189 */
190static  int
191streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
192{
193	int type, protocol;
194	int fd, extraref;
195	struct file *fp;
196	struct socket *so;
197	int error;
198	int family;
199	struct proc *p = td->td_proc;
200
201	PROC_LOCK(p);
202	if (td->td_dupfd >= 0) {
203	  PROC_UNLOCK(p);
204	  return ENODEV;
205	}
206	PROC_UNLOCK(p);
207
208	switch (minor(dev)) {
209	case dev_udp:
210	  family = AF_INET;
211	  type = SOCK_DGRAM;
212	  protocol = IPPROTO_UDP;
213	  break;
214
215	case dev_tcp:
216	  family = AF_INET;
217	  type = SOCK_STREAM;
218	  protocol = IPPROTO_TCP;
219	  break;
220
221	case dev_ip:
222	case dev_rawip:
223	  family = AF_INET;
224	  type = SOCK_RAW;
225	  protocol = IPPROTO_IP;
226	  break;
227
228	case dev_icmp:
229	  family = AF_INET;
230	  type = SOCK_RAW;
231	  protocol = IPPROTO_ICMP;
232	  break;
233
234	case dev_unix_dgram:
235	  family = AF_LOCAL;
236	  type = SOCK_DGRAM;
237	  protocol = 0;
238	  break;
239
240	case dev_unix_stream:
241	case dev_unix_ord_stream:
242	  family = AF_LOCAL;
243	  type = SOCK_STREAM;
244	  protocol = 0;
245	  break;
246
247	case dev_ptm:
248	  return svr4_ptm_alloc(td);
249
250	default:
251	  return EOPNOTSUPP;
252	}
253
254	if ((error = falloc(td, &fp, &fd)) != 0)
255	  return error;
256	/* An extra reference on `fp' has been held for us by falloc(). */
257
258	if ((error = socreate(family, &so, type, protocol,
259	    td->td_ucred, td)) != 0) {
260	  FILEDESC_LOCK_FAST(p->p_fd);
261	  /* Check the fd table entry hasn't changed since we made it. */
262	  extraref = 0;
263	  if (p->p_fd->fd_ofiles[fd] == fp) {
264	    p->p_fd->fd_ofiles[fd] = NULL;
265	    extraref = 1;
266	  }
267	  FILEDESC_UNLOCK_FAST(p->p_fd);
268	  if (extraref)
269	    fdrop(fp, td);
270	  fdrop(fp, td);
271	  return error;
272	}
273
274	FILEDESC_LOCK_FAST(p->p_fd);
275	fp->f_data = so;
276	fp->f_flag = FREAD|FWRITE;
277	fp->f_ops = &svr4_netops;
278	fp->f_type = DTYPE_SOCKET;
279	FILEDESC_UNLOCK_FAST(p->p_fd);
280
281	(void)svr4_stream_get(fp);
282	fdrop(fp, td);
283	PROC_LOCK(p);
284	td->td_dupfd = fd;
285	PROC_UNLOCK(p);
286	return ENXIO;
287}
288
289static int
290svr4_ptm_alloc(td)
291	struct thread *td;
292{
293	/*
294	 * XXX this is very, very ugly.  But I can't find a better
295	 * way that won't duplicate a big amount of code from
296	 * sys_open().  Ho hum...
297	 *
298	 * Fortunately for us, Solaris (at least 2.5.1) makes the
299	 * /dev/ptmx open automatically just open a pty, that (after
300	 * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
301	 * to get the minor device number to map to a tty.
302	 *
303	 * Cycle through the names. If sys_open() returns ENOENT (or
304	 * ENXIO), short circuit the cycle and exit.
305	 */
306	static char ptyname[] = "/dev/ptyXX";
307	static char ttyletters[] = "pqrstuwxyzPQRST";
308	static char ttynumbers[] = "0123456789abcdef";
309	struct proc *p;
310	register_t fd;
311	int error, l, n;
312
313	fd = -1;
314	n = 0;
315	l = 0;
316	p = td->td_proc;
317	while (fd == -1) {
318		ptyname[8] = ttyletters[l];
319		ptyname[9] = ttynumbers[n];
320
321		error = kern_open(td, ptyname, UIO_SYSSPACE, O_RDWR, 0);
322		switch (error) {
323		case ENOENT:
324		case ENXIO:
325			return error;
326		case 0:
327			PROC_LOCK(p);
328			td->td_dupfd = td->td_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 = fp->f_data;
354
355	/*
356	 * mpfixme: lock socketbuffer here
357	 */
358	if (so->so_emuldata) {
359		return so->so_emuldata;
360	}
361
362	/* Allocate a new one. */
363	st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
364	st->s_family = so->so_proto->pr_domain->dom_family;
365	st->s_cmd = ~0;
366	st->s_afd = -1;
367	st->s_eventmask = 0;
368	/*
369	 * avoid a race where we loose due to concurrancy issues
370	 * of two threads trying to allocate the so_emuldata.
371	 */
372	if (so->so_emuldata) {
373		/* lost the race, use the existing emuldata */
374		FREE(st, M_TEMP);
375		st = so->so_emuldata;
376	} else {
377		/* we won, or there was no race, use our copy */
378		so->so_emuldata = st;
379		fp->f_ops = &svr4_netops;
380	}
381
382	return st;
383}
384
385void
386svr4_delete_socket(p, fp)
387	struct proc *p;
388	struct file *fp;
389{
390	struct svr4_sockcache_entry *e;
391	void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
392
393	while (svr4_str_initialized != 2) {
394		if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
395			TAILQ_INIT(&svr4_head);
396			atomic_store_rel_int(&svr4_str_initialized, 2);
397		}
398		return;
399	}
400
401	TAILQ_FOREACH(e, &svr4_head, entries)
402		if (e->p == p && e->cookie == cookie) {
403			TAILQ_REMOVE(&svr4_head, e, entries);
404			DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
405				 e->sock.sun_path, p, (int)e->dev, e->ino));
406			free(e, M_TEMP);
407			return;
408		}
409}
410
411static int
412svr4_soo_close(struct file *fp, struct thread *td)
413{
414        struct socket *so = fp->f_data;
415
416	/*	CHECKUNIT_DIAG(ENXIO);*/
417
418	svr4_delete_socket(td->td_proc, fp);
419	free(so->so_emuldata, M_TEMP);
420	return soo_close(fp, td);
421}
422