streams.c revision 188697
1139749Simp/*-
243410Snewton * Copyright (c) 1998 Mark Newton
343410Snewton * Copyright (c) 1994 Christos Zoulas
443410Snewton * Copyright (c) 1997 Todd Vierling
543410Snewton * All rights reserved.
643410Snewton *
743410Snewton * Redistribution and use in source and binary forms, with or without
843410Snewton * modification, are permitted provided that the following conditions
943410Snewton * are met:
1043410Snewton * 1. Redistributions of source code must retain the above copyright
1143410Snewton *    notice, this list of conditions and the following disclaimer.
1243410Snewton * 2. Redistributions in binary form must reproduce the above copyright
1343410Snewton *    notice, this list of conditions and the following disclaimer in the
1443410Snewton *    documentation and/or other materials provided with the distribution.
1543410Snewton * 3. The names of the authors may not be used to endorse or promote products
1643410Snewton *    derived from this software without specific prior written permission
1743410Snewton *
1843410Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1943410Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2043410Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2143410Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2243410Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2343410Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2443410Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2543410Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2643410Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2743410Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2843410Snewton *
2943410Snewton * Stolen from NetBSD /sys/compat/svr4/svr4_net.c.  Pseudo-device driver
3043410Snewton * skeleton produced from /usr/share/examples/drivers/make_pseudo_driver.sh
3143410Snewton * in 3.0-980524-SNAP then hacked a bit (but probably not enough :-).
3249263Snewton *
3343410Snewton */
3443410Snewton
35119420Sobrien#include <sys/cdefs.h>
36119420Sobrien__FBSDID("$FreeBSD: head/sys/dev/streams/streams.c 188697 2009-02-16 20:12:28Z ed $");
37119420Sobrien
3843410Snewton#include <sys/param.h>
3943410Snewton#include <sys/systm.h>
4043410Snewton#include <sys/kernel.h>		/* SYSINIT stuff */
4143410Snewton#include <sys/conf.h>		/* cdevsw stuff */
4243410Snewton#include <sys/malloc.h>		/* malloc region definitions */
4343410Snewton#include <sys/file.h>
4443410Snewton#include <sys/filedesc.h>
4543410Snewton#include <sys/unistd.h>
4643410Snewton#include <sys/fcntl.h>
4743410Snewton#include <sys/socket.h>
4843410Snewton#include <sys/protosw.h>
4943410Snewton#include <sys/socketvar.h>
50141466Sjhb#include <sys/syscallsubr.h>
5143410Snewton#include <sys/un.h>
5243410Snewton#include <sys/domain.h>
5343410Snewton#include <net/if.h>
5443410Snewton#include <netinet/in.h>
5543410Snewton#include <sys/proc.h>
5643410Snewton#include <sys/uio.h>
5743410Snewton
5843410Snewton#include <sys/sysproto.h>
5943410Snewton
6065314Sobrien#include <compat/svr4/svr4_types.h>
6165314Sobrien#include <compat/svr4/svr4_util.h>
6265314Sobrien#include <compat/svr4/svr4_signal.h>
6365314Sobrien#include <compat/svr4/svr4_ioctl.h>
6465314Sobrien#include <compat/svr4/svr4_stropts.h>
6565314Sobrien#include <compat/svr4/svr4_socket.h>
6643410Snewton
6792739Salfredstatic int svr4_soo_close(struct file *, struct thread *);
6892739Salfredstatic int svr4_ptm_alloc(struct thread *);
6943410Snewtonstatic  d_open_t	streamsopen;
7043410Snewton
7143410Snewton/*
7243410Snewton * Device minor numbers
7343410Snewton */
7443410Snewtonenum {
7543410Snewton	dev_ptm			= 10,
7643410Snewton	dev_arp			= 26,
7743410Snewton	dev_icmp		= 27,
7843410Snewton	dev_ip			= 28,
7943410Snewton	dev_tcp			= 35,
8043410Snewton	dev_udp			= 36,
8143410Snewton	dev_rawip		= 37,
8243410Snewton	dev_unix_dgram		= 38,
8343410Snewton	dev_unix_stream		= 39,
8443410Snewton	dev_unix_ord_stream	= 40
8543410Snewton};
8643410Snewton
87160508Sjhbstatic struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp,
88160508Sjhb	*dt_rawip, *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
8952114Snewton
9043410Snewtonstatic struct fileops svr4_netops = {
91116546Sphk	.fo_read = soo_read,
92116546Sphk	.fo_write = soo_write,
93175140Sjhb	.fo_truncate = soo_truncate,
94116546Sphk	.fo_ioctl = soo_ioctl,
95116546Sphk	.fo_poll = soo_poll,
96116546Sphk	.fo_kqfilter = soo_kqfilter,
97116546Sphk	.fo_stat = soo_stat,
98116546Sphk	.fo_close =  svr4_soo_close
9943410Snewton};
10043410Snewton
10143410Snewtonstatic struct cdevsw streams_cdevsw = {
102126080Sphk	.d_version =	D_VERSION,
103111815Sphk	.d_open =	streamsopen,
104111815Sphk	.d_name =	"streams",
10547625Sphk};
10643410Snewton
10743410Snewtonstruct streams_softc {
10843410Snewton	struct isa_device *dev;
10943410Snewton} ;
11043410Snewton
111183397Sed#define UNIT(dev) dev2unit(dev)	/* assume one minor number per unit */
11243410Snewton
11343410Snewtontypedef	struct streams_softc *sc_p;
11443410Snewton
11544208Snewtonstatic	int
11644208Snewtonstreams_modevent(module_t mod, int type, void *unused)
11744208Snewton{
11844208Snewton	switch (type) {
11944208Snewton	case MOD_LOAD:
12052114Snewton		dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
12152114Snewton			"ptm");
12252114Snewton		dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
12352114Snewton			"arp");
12452114Snewton		dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
12552114Snewton			"icmp");
12652114Snewton		dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
12752114Snewton			"ip");
12852114Snewton		dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
12952114Snewton			"tcp");
13052114Snewton		dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
13152114Snewton			"udp");
13252114Snewton		dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
13352114Snewton			"rawip");
13452114Snewton		dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
13552114Snewton			0, 0, 0666, "ticlts");
13652114Snewton		dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
13752114Snewton			0, 0, 0666, "ticots");
13852114Snewton		dt_unix_ord_stream = make_dev(&streams_cdevsw,
13952114Snewton			dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
14052114Snewton
14152114Snewton		if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
14252114Snewton				dt_udp && dt_rawip && dt_unix_dgram &&
14352114Snewton				dt_unix_stream && dt_unix_ord_stream)) {
14452114Snewton			printf("WARNING: device config for STREAMS failed\n");
14552114Snewton			printf("Suggest unloading streams KLD\n");
14652114Snewton		}
14744208Snewton		return 0;
14844208Snewton	case MOD_UNLOAD:
14949344Snewton	  	/* XXX should check to see if it's busy first */
15053000Sphk		destroy_dev(dt_ptm);
15153000Sphk		destroy_dev(dt_arp);
15253000Sphk		destroy_dev(dt_icmp);
15353000Sphk		destroy_dev(dt_ip);
15453000Sphk		destroy_dev(dt_tcp);
15553000Sphk		destroy_dev(dt_udp);
15653000Sphk		destroy_dev(dt_rawip);
15753000Sphk		destroy_dev(dt_unix_dgram);
15853000Sphk		destroy_dev(dt_unix_stream);
15953000Sphk		destroy_dev(dt_unix_ord_stream);
16052114Snewton
16144208Snewton		return 0;
16244208Snewton	default:
163132199Sphk		return EOPNOTSUPP;
16444208Snewton		break;
16544208Snewton	}
16644208Snewton	return 0;
16744208Snewton}
16844208Snewton
16944208Snewtonstatic moduledata_t streams_mod = {
17044208Snewton	"streams",
17144208Snewton	streams_modevent,
17244208Snewton	0
17344208Snewton};
17444208SnewtonDECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
17560060SgreenMODULE_VERSION(streams, 1);
17644208Snewton
17743410Snewton/*
17843410Snewton * We only need open() and close() routines.  open() calls socreate()
17943410Snewton * to allocate a "real" object behind the stream and mallocs some state
18043410Snewton * info for use by the svr4 emulator;  close() deallocates the state
18143410Snewton * information and passes the underlying object to the normal socket close
18243410Snewton * routine.
18343410Snewton */
18443410Snewtonstatic  int
185130585Sphkstreamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
18643410Snewton{
187160558Sjhb	struct filedesc *fdp;
188160558Sjhb	struct svr4_strm *st;
189160558Sjhb	struct socket *so;
19043410Snewton	struct file *fp;
191160558Sjhb	int family, type, protocol;
192160558Sjhb	int error, fd;
19343410Snewton
194160490Sjhb	if (td->td_dupfd >= 0)
19543410Snewton	  return ENODEV;
19643410Snewton
197183397Sed	switch (dev2unit(dev)) {
19843410Snewton	case dev_udp:
19943410Snewton	  family = AF_INET;
20043410Snewton	  type = SOCK_DGRAM;
20143410Snewton	  protocol = IPPROTO_UDP;
20243410Snewton	  break;
20343410Snewton
20443410Snewton	case dev_tcp:
20543410Snewton	  family = AF_INET;
20643410Snewton	  type = SOCK_STREAM;
20743410Snewton	  protocol = IPPROTO_TCP;
20843410Snewton	  break;
20943410Snewton
21043410Snewton	case dev_ip:
21143410Snewton	case dev_rawip:
21243410Snewton	  family = AF_INET;
21343410Snewton	  type = SOCK_RAW;
21443410Snewton	  protocol = IPPROTO_IP;
21543410Snewton	  break;
21643410Snewton
21743410Snewton	case dev_icmp:
21843410Snewton	  family = AF_INET;
21943410Snewton	  type = SOCK_RAW;
22043410Snewton	  protocol = IPPROTO_ICMP;
22143410Snewton	  break;
22243410Snewton
22343410Snewton	case dev_unix_dgram:
22443410Snewton	  family = AF_LOCAL;
22543410Snewton	  type = SOCK_DGRAM;
22643410Snewton	  protocol = 0;
22743410Snewton	  break;
22843410Snewton
22943410Snewton	case dev_unix_stream:
23043410Snewton	case dev_unix_ord_stream:
23143410Snewton	  family = AF_LOCAL;
23243410Snewton	  type = SOCK_STREAM;
23343410Snewton	  protocol = 0;
23443410Snewton	  break;
23543410Snewton
23643410Snewton	case dev_ptm:
23783366Sjulian	  return svr4_ptm_alloc(td);
23843410Snewton
23943410Snewton	default:
24043410Snewton	  return EOPNOTSUPP;
24143410Snewton	}
24243410Snewton
243160558Sjhb	fdp = td->td_proc->p_fd;
24483366Sjulian	if ((error = falloc(td, &fp, &fd)) != 0)
24543410Snewton	  return error;
246121256Sdwmalone	/* An extra reference on `fp' has been held for us by falloc(). */
24743410Snewton
248160558Sjhb	error = socreate(family, &so, type, protocol, td->td_ucred, td);
249160558Sjhb	if (error) {
250160558Sjhb	   fdclose(fdp, fp, fd, td);
251160558Sjhb	   fdrop(fp, td);
252160558Sjhb	   return error;
25343410Snewton	}
25443410Snewton
255174988Sjeff	finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops);
25643410Snewton
257160558Sjhb	/*
258160558Sjhb	 * Allocate a stream structure and attach it to this socket.
259160558Sjhb	 * We don't bother locking so_emuldata for SVR4 stream sockets as
260160558Sjhb	 * its value is constant for the lifetime of the stream once it
261160558Sjhb	 * is initialized here.
262160558Sjhb	 */
263160558Sjhb	st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
264160558Sjhb	st->s_family = so->so_proto->pr_domain->dom_family;
265160558Sjhb	st->s_cmd = ~0;
266160558Sjhb	st->s_afd = -1;
267160558Sjhb	st->s_eventmask = 0;
268160558Sjhb	so->so_emuldata = st;
269160558Sjhb
270121256Sdwmalone	fdrop(fp, td);
27183366Sjulian	td->td_dupfd = fd;
27243410Snewton	return ENXIO;
27343410Snewton}
27443410Snewton
27543410Snewtonstatic int
27683366Sjuliansvr4_ptm_alloc(td)
27783366Sjulian	struct thread *td;
27843410Snewton{
27943410Snewton	/*
28043410Snewton	 * XXX this is very, very ugly.  But I can't find a better
28143410Snewton	 * way that won't duplicate a big amount of code from
28243410Snewton	 * sys_open().  Ho hum...
28343410Snewton	 *
28443410Snewton	 * Fortunately for us, Solaris (at least 2.5.1) makes the
28543410Snewton	 * /dev/ptmx open automatically just open a pty, that (after
28643410Snewton	 * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
28743410Snewton	 * to get the minor device number to map to a tty.
28843410Snewton	 *
28943410Snewton	 * Cycle through the names. If sys_open() returns ENOENT (or
29043410Snewton	 * ENXIO), short circuit the cycle and exit.
291188697Sed	 *
292188697Sed	 * XXX: Maybe this can now be implemented by posix_openpt()?
29343410Snewton	 */
29443410Snewton	static char ptyname[] = "/dev/ptyXX";
29543410Snewton	static char ttyletters[] = "pqrstuwxyzPQRST";
29643410Snewton	static char ttynumbers[] = "0123456789abcdef";
297141466Sjhb	struct proc *p;
298141466Sjhb	register_t fd;
299141466Sjhb	int error, l, n;
30043410Snewton
301141466Sjhb	fd = -1;
302141466Sjhb	n = 0;
303141466Sjhb	l = 0;
304141466Sjhb	p = td->td_proc;
30543410Snewton	while (fd == -1) {
30643410Snewton		ptyname[8] = ttyletters[l];
30743410Snewton		ptyname[9] = ttynumbers[n];
30843410Snewton
309141466Sjhb		error = kern_open(td, ptyname, UIO_SYSSPACE, O_RDWR, 0);
310141466Sjhb		switch (error) {
31143410Snewton		case ENOENT:
31243410Snewton		case ENXIO:
31343410Snewton			return error;
31443410Snewton		case 0:
31583366Sjulian			td->td_dupfd = td->td_retval[0];
31643410Snewton			return ENXIO;
31743410Snewton		default:
31843410Snewton			if (ttynumbers[++n] == '\0') {
31943410Snewton				if (ttyletters[++l] == '\0')
32043410Snewton					break;
32143410Snewton				n = 0;
32243410Snewton			}
32343410Snewton		}
32443410Snewton	}
32543410Snewton	return ENOENT;
32643410Snewton}
32743410Snewton
32843410Snewton
32943410Snewtonstruct svr4_strm *
33043410Snewtonsvr4_stream_get(fp)
33143410Snewton	struct file *fp;
33243410Snewton{
33343410Snewton	struct socket *so;
33443410Snewton
33543410Snewton	if (fp == NULL || fp->f_type != DTYPE_SOCKET)
33643410Snewton		return NULL;
33743410Snewton
338109153Sdillon	so = fp->f_data;
339160558Sjhb	return so->so_emuldata;
34043410Snewton}
34143410Snewton
34243410Snewtonstatic int
34383366Sjuliansvr4_soo_close(struct file *fp, struct thread *td)
34443410Snewton{
345109153Sdillon        struct socket *so = fp->f_data;
34643410Snewton
34743410Snewton	/*	CHECKUNIT_DIAG(ENXIO);*/
34843410Snewton
34983366Sjulian	svr4_delete_socket(td->td_proc, fp);
35043410Snewton	free(so->so_emuldata, M_TEMP);
35183366Sjulian	return soo_close(fp, td);
35243410Snewton}
353