2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
4 * Copyright (c) 1997 Todd Vierling
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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 :-).
33 * $FreeBSD: src/sys/dev/streams/streams.c,v 1.16.2.1 2001/02/26 04:23:07 jlemon Exp $
34 * $DragonFly: src/sys/dev/misc/streams/Attic/streams.c,v 1.16 2005/06/22 01:33:24 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h> /* SYSINIT stuff */
40 #include <sys/conf.h> /* cdevsw stuff */
41 #include <sys/malloc.h> /* malloc region definitions */
43 #include <sys/filedesc.h>
44 #include <sys/unistd.h>
45 #include <sys/fcntl.h>
46 #include <sys/socket.h>
47 #include <sys/protosw.h>
48 #include <sys/socketvar.h>
50 #include <sys/domain.h>
52 #include <netinet/in.h>
56 #include <sys/sysproto.h>
58 #include <emulation/svr4/svr4_types.h>
59 #include <emulation/svr4/svr4_util.h>
60 #include <emulation/svr4/svr4_signal.h>
61 #include <emulation/svr4/svr4_ioctl.h>
62 #include <emulation/svr4/svr4_stropts.h>
63 #include <emulation/svr4/svr4_socket.h>
65 static int svr4_soo_close (struct file *, struct thread *);
66 static int svr4_ptm_alloc (struct thread *);
67 static d_open_t streamsopen;
69 struct svr4_sockcache_entry {
70 struct thread *td; /* Thread for the socket */
71 void *cookie; /* Internal cookie used for matching */
72 struct sockaddr_un sock;/* Pathname for the socket */
73 dev_t dev; /* Device where the socket lives on */
74 ino_t ino; /* Inode where the socket lives on */
75 TAILQ_ENTRY(svr4_sockcache_entry) entries;
78 TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
80 /* Initialization flag (set/queried by svr4_mod LKM) */
81 int svr4_str_initialized = 0;
84 * Device minor numbers
96 dev_unix_ord_stream = 40
99 static struct fileops svr4_netops = {
102 soo_read, soo_write, soo_ioctl, soo_poll, sokqfilter,
103 soo_stat, svr4_soo_close
106 #define CDEV_MAJOR 103
107 static struct cdevsw streams_cdevsw = {
108 /* name */ "streams",
109 /* maj */ CDEV_MAJOR,
114 /* open */ streamsopen,
121 /* strategy */ nostrategy,
126 struct streams_softc {
127 struct isa_device *dev;
130 #define UNIT(dev) minor(dev) /* assume one minor number per unit */
132 typedef struct streams_softc *sc_p;
135 streams_modevent(module_t mod, int type, void *unused)
139 /* XXX should make sure it isn't already loaded first */
140 cdevsw_add(&streams_cdevsw, 0, 0);
141 make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666, "ptm");
142 make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666, "arp");
143 make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666, "icmp");
144 make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666, "ip");
145 make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666, "tcp");
146 make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666, "udp");
147 make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666, "rawip");
148 make_dev(&streams_cdevsw, dev_unix_dgram, 0, 0, 0666, "ticlts");
149 make_dev(&streams_cdevsw, dev_unix_stream,
150 0, 0, 0666, "ticots");
151 make_dev(&streams_cdevsw, dev_unix_ord_stream,
152 0, 0, 0666, "ticotsord");
155 /* XXX should check to see if it's busy first */
156 cdevsw_remove(&streams_cdevsw, 0, 0);
165 static moduledata_t streams_mod = {
170 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
173 * We only need open() and close() routines. open() calls socreate()
174 * to allocate a "real" object behind the stream and mallocs some state
175 * info for use by the svr4 emulator; close() deallocates the state
176 * information and passes the underlying object to the normal socket close
180 streamsopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
182 struct proc *p = td->td_proc;
195 switch (minor(dev)) {
199 protocol = IPPROTO_UDP;
205 protocol = IPPROTO_TCP;
212 protocol = IPPROTO_IP;
218 protocol = IPPROTO_ICMP;
227 case dev_unix_stream:
228 case dev_unix_ord_stream:
235 return svr4_ptm_alloc(td);
241 if ((error = falloc(p, &fp, &fd)) != 0)
244 if ((error = socreate(family, &so, type, protocol, td)) != 0) {
245 if (p->p_fd->fd_files[fd].fp == fp) {
246 funsetfd(p->p_fd, fd);
253 fp->f_data = (caddr_t)so;
254 fp->f_flag = FREAD|FWRITE;
255 fp->f_ops = &svr4_netops;
256 fp->f_type = DTYPE_SOCKET;
257 (void)svr4_stream_get(fp);
264 svr4_ptm_alloc(struct thread *td)
267 * XXX this is very, very ugly. But I can't find a better
268 * way that won't duplicate a big amount of code from
269 * sys_open(). Ho hum...
271 * Fortunately for us, Solaris (at least 2.5.1) makes the
272 * /dev/ptmx open automatically just open a pty, that (after
273 * STREAMS I_PUSHes), is just a plain pty. fstat() is used
274 * to get the minor device number to map to a tty.
276 * Cycle through the names. If sys_open() returns ENOENT (or
277 * ENXIO), short circuit the cycle and exit.
279 static char ptyname[] = "/dev/ptyXX";
280 static char ttyletters[] = "pqrstuwxyzPQRST";
281 static char ttynumbers[] = "0123456789abcdef";
282 caddr_t sg = stackgap_init();
283 char *path = stackgap_alloc(&sg, sizeof(ptyname));
288 struct proc *p = td->td_proc;
292 SCARG(&oa, path) = path;
293 SCARG(&oa, flags) = O_RDWR;
294 SCARG(&oa, mode) = 0;
297 ptyname[8] = ttyletters[l];
298 ptyname[9] = ttynumbers[n];
300 if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
303 switch (error = open(&oa)) {
308 p->p_dupfd = oa.sysmsg_result;
311 if (ttynumbers[++n] == '\0') {
312 if (ttyletters[++l] == '\0')
327 struct svr4_strm *st;
329 if (fp == NULL || fp->f_type != DTYPE_SOCKET)
332 so = (struct socket *) fp->f_data;
335 return so->so_emuldata;
337 /* Allocate a new one. */
338 st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
339 st->s_family = so->so_proto->pr_domain->dom_family;
343 so->so_emuldata = st;
344 fp->f_ops = &svr4_netops;
350 svr4_delete_socket(struct thread *td, struct file *fp)
352 struct svr4_sockcache_entry *e;
353 void *cookie = ((struct socket *) fp->f_data)->so_emuldata;
355 if (!svr4_str_initialized) {
356 TAILQ_INIT(&svr4_head);
357 svr4_str_initialized = 1;
361 TAILQ_FOREACH(e, &svr4_head, entries) {
362 if (e->td == td && e->cookie == cookie) {
363 TAILQ_REMOVE(&svr4_head, e, entries);
364 DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
365 e->sock.sun_path, td, (int)e->dev,
374 svr4_soo_close(struct file *fp, struct thread *td)
376 struct socket *so = (struct socket *)fp->f_data;
378 /* CHECKUNIT_DIAG(ENXIO);*/
380 svr4_delete_socket(td, fp);
381 free(so->so_emuldata, M_TEMP);
382 return soo_close(fp, td);