streams.c revision 241394
175406Sache/*-
226497Sache * Copyright (c) 1998 Mark Newton
335486Sache * Copyright (c) 1994 Christos Zoulas
435486Sache * Copyright (c) 1997 Todd Vierling
535486Sache * All rights reserved.
626497Sache *
726497Sache * Redistribution and use in source and binary forms, with or without
826497Sache * modification, are permitted provided that the following conditions
926497Sache * are met:
1026497Sache * 1. Redistributions of source code must retain the above copyright
1126497Sache *    notice, this list of conditions and the following disclaimer.
1226497Sache * 2. Redistributions in binary form must reproduce the above copyright
1326497Sache *    notice, this list of conditions and the following disclaimer in the
1426497Sache *    documentation and/or other materials provided with the distribution.
1526497Sache * 3. The names of the authors may not be used to endorse or promote products
1626497Sache *    derived from this software without specific prior written permission
1758310Sache *
1858310Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1958310Sache * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2026497Sache * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2126497Sache * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2226497Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2326497Sache * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2426497Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2526497Sache * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2626497Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2726497Sache * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2826497Sache *
2926497Sache * Stolen from NetBSD /sys/compat/svr4/svr4_net.c.  Pseudo-device driver
3026497Sache * skeleton produced from /usr/share/examples/drivers/make_pseudo_driver.sh
3126497Sache * in 3.0-980524-SNAP then hacked a bit (but probably not enough :-).
3226497Sache *
3326497Sache */
3426497Sache
3526497Sache#include <sys/cdefs.h>
3626497Sache__FBSDID("$FreeBSD: head/sys/dev/streams/streams.c 241394 2012-10-10 08:36:38Z kevlo $");
3726497Sache
3826497Sache#include <sys/param.h>
3926497Sache#include <sys/systm.h>
4026497Sache#include <sys/kernel.h>		/* SYSINIT stuff */
4126497Sache#include <sys/conf.h>		/* cdevsw stuff */
4226497Sache#include <sys/malloc.h>		/* malloc region definitions */
4326497Sache#include <sys/file.h>
4426497Sache#include <sys/filedesc.h>
4526497Sache#include <sys/unistd.h>
4626497Sache#include <sys/fcntl.h>
4726497Sache#include <sys/socket.h>
4826497Sache#include <sys/protosw.h>
4926497Sache#include <sys/socketvar.h>
5026497Sache#include <sys/syscallsubr.h>
5126497Sache#include <sys/un.h>
5226497Sache#include <sys/domain.h>
5326497Sache#include <net/if.h>
5426497Sache#include <netinet/in.h>
5526497Sache#include <sys/proc.h>
5626497Sache#include <sys/uio.h>
5726497Sache
5826497Sache#include <sys/sysproto.h>
5926497Sache
6026497Sache#include <compat/svr4/svr4_types.h>
6126497Sache#include <compat/svr4/svr4_util.h>
6226497Sache#include <compat/svr4/svr4_signal.h>
6326497Sache#include <compat/svr4/svr4_ioctl.h>
6426497Sache#include <compat/svr4/svr4_stropts.h>
6526497Sache#include <compat/svr4/svr4_socket.h>
6626497Sache
6726497Sachestatic int svr4_soo_close(struct file *, struct thread *);
6826497Sachestatic int svr4_ptm_alloc(struct thread *);
6926497Sachestatic  d_open_t	streamsopen;
7026497Sache
7126497Sache/*
7226497Sache * Device minor numbers
7326497Sache */
7426497Sacheenum {
7526497Sache	dev_ptm			= 10,
7626497Sache	dev_arp			= 26,
7726497Sache	dev_icmp		= 27,
7826497Sache	dev_ip			= 28,
7926497Sache	dev_tcp			= 35,
8026497Sache	dev_udp			= 36,
8126497Sache	dev_rawip		= 37,
8226497Sache	dev_unix_dgram		= 38,
8326497Sache	dev_unix_stream		= 39,
8426497Sache	dev_unix_ord_stream	= 40
8526497Sache};
8626497Sache
8726497Sachestatic struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp,
8826497Sache	*dt_rawip, *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
8926497Sache
9026497Sachestatic struct fileops svr4_netops = {
9126497Sache	.fo_read = soo_read,
9226497Sache	.fo_write = soo_write,
9326497Sache	.fo_truncate = soo_truncate,
9426497Sache	.fo_ioctl = soo_ioctl,
9526497Sache	.fo_poll = soo_poll,
9626497Sache	.fo_kqfilter = soo_kqfilter,
9726497Sache	.fo_stat = soo_stat,
9826497Sache	.fo_close =  svr4_soo_close,
9926497Sache	.fo_chmod = invfo_chmod,
10026497Sache	.fo_chown = invfo_chown,
10135486Sache};
10235486Sache
10335486Sachestatic struct cdevsw streams_cdevsw = {
10435486Sache	.d_version =	D_VERSION,
10526497Sache	.d_open =	streamsopen,
10626497Sache	.d_name =	"streams",
10726497Sache};
10826497Sache
10926497Sachestruct streams_softc {
11026497Sache	struct isa_device *dev;
11126497Sache} ;
11226497Sache
11326497Sache#define UNIT(dev) dev2unit(dev)	/* assume one minor number per unit */
11426497Sache
11526497Sachetypedef	struct streams_softc *sc_p;
11626497Sache
11726497Sachestatic	int
11826497Sachestreams_modevent(module_t mod, int type, void *unused)
11926497Sache{
12026497Sache	switch (type) {
12126497Sache	case MOD_LOAD:
12226497Sache		dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
12326497Sache			"ptm");
12426497Sache		dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
12526497Sache			"arp");
12626497Sache		dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
12726497Sache			"icmp");
12826497Sache		dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
12926497Sache			"ip");
13026497Sache		dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
13126497Sache			"tcp");
13226497Sache		dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
13326497Sache			"udp");
13426497Sache		dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
13526497Sache			"rawip");
13626497Sache		dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
13726497Sache			0, 0, 0666, "ticlts");
13826497Sache		dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
13926497Sache			0, 0, 0666, "ticots");
14026497Sache		dt_unix_ord_stream = make_dev(&streams_cdevsw,
14126497Sache			dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
14226497Sache
14326497Sache		if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
14426497Sache				dt_udp && dt_rawip && dt_unix_dgram &&
14526497Sache				dt_unix_stream && dt_unix_ord_stream)) {
14626497Sache			printf("WARNING: device config for STREAMS failed\n");
14726497Sache			printf("Suggest unloading streams KLD\n");
148		}
149		return 0;
150	case MOD_UNLOAD:
151	  	/* XXX should check to see if it's busy first */
152		destroy_dev(dt_ptm);
153		destroy_dev(dt_arp);
154		destroy_dev(dt_icmp);
155		destroy_dev(dt_ip);
156		destroy_dev(dt_tcp);
157		destroy_dev(dt_udp);
158		destroy_dev(dt_rawip);
159		destroy_dev(dt_unix_dgram);
160		destroy_dev(dt_unix_stream);
161		destroy_dev(dt_unix_ord_stream);
162
163		return 0;
164	default:
165		return EOPNOTSUPP;
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(struct cdev *dev, int oflags, int devtype, struct thread *td)
188{
189	struct filedesc *fdp;
190	struct svr4_strm *st;
191	struct socket *so;
192	struct file *fp;
193	int family, type, protocol;
194	int error, fd;
195
196	if (td->td_dupfd >= 0)
197	  return ENODEV;
198
199	switch (dev2unit(dev)) {
200	case dev_udp:
201	  family = AF_INET;
202	  type = SOCK_DGRAM;
203	  protocol = IPPROTO_UDP;
204	  break;
205
206	case dev_tcp:
207	  family = AF_INET;
208	  type = SOCK_STREAM;
209	  protocol = IPPROTO_TCP;
210	  break;
211
212	case dev_ip:
213	case dev_rawip:
214	  family = AF_INET;
215	  type = SOCK_RAW;
216	  protocol = IPPROTO_IP;
217	  break;
218
219	case dev_icmp:
220	  family = AF_INET;
221	  type = SOCK_RAW;
222	  protocol = IPPROTO_ICMP;
223	  break;
224
225	case dev_unix_dgram:
226	  family = AF_LOCAL;
227	  type = SOCK_DGRAM;
228	  protocol = 0;
229	  break;
230
231	case dev_unix_stream:
232	case dev_unix_ord_stream:
233	  family = AF_LOCAL;
234	  type = SOCK_STREAM;
235	  protocol = 0;
236	  break;
237
238	case dev_ptm:
239	  return svr4_ptm_alloc(td);
240
241	default:
242	  return EOPNOTSUPP;
243	}
244
245	fdp = td->td_proc->p_fd;
246	if ((error = falloc(td, &fp, &fd, 0)) != 0)
247	  return error;
248	/* An extra reference on `fp' has been held for us by falloc(). */
249
250	error = socreate(family, &so, type, protocol, td->td_ucred, td);
251	if (error) {
252	   fdclose(fdp, fp, fd, td);
253	   fdrop(fp, td);
254	   return error;
255	}
256
257	finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops);
258
259	/*
260	 * Allocate a stream structure and attach it to this socket.
261	 * We don't bother locking so_emuldata for SVR4 stream sockets as
262	 * its value is constant for the lifetime of the stream once it
263	 * is initialized here.
264	 */
265	st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
266	st->s_family = so->so_proto->pr_domain->dom_family;
267	st->s_cmd = ~0;
268	st->s_afd = -1;
269	st->s_eventmask = 0;
270	so->so_emuldata = st;
271
272	fdrop(fp, td);
273	td->td_dupfd = fd;
274	return ENXIO;
275}
276
277static int
278svr4_ptm_alloc(td)
279	struct thread *td;
280{
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	 * XXX: Maybe this can now be implemented by posix_openpt()?
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