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 :-).
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h> /* SYSINIT stuff */
41 #include <sys/conf.h> /* cdevsw stuff */
42 #include <sys/malloc.h> /* malloc region definitions */
44 #include <sys/filedesc.h>
45 #include <sys/unistd.h>
46 #include <sys/fcntl.h>
47 #include <sys/socket.h>
48 #include <sys/protosw.h>
49 #include <sys/socketvar.h>
50 #include <sys/syscallsubr.h>
52 #include <sys/domain.h>
54 #include <netinet/in.h>
58 #include <sys/sysproto.h>
60 #include <compat/svr4/svr4_types.h>
61 #include <compat/svr4/svr4_util.h>
62 #include <compat/svr4/svr4_signal.h>
63 #include <compat/svr4/svr4_ioctl.h>
64 #include <compat/svr4/svr4_stropts.h>
65 #include <compat/svr4/svr4_socket.h>
67 static int svr4_soo_close(struct file *, struct thread *);
68 static int svr4_ptm_alloc(struct thread *);
69 static d_open_t streamsopen;
72 * Device minor numbers
84 dev_unix_ord_stream = 40
87 static struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp,
88 *dt_rawip, *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
90 static struct fileops svr4_netops = {
92 .fo_write = soo_write,
93 .fo_truncate = soo_truncate,
94 .fo_ioctl = soo_ioctl,
96 .fo_kqfilter = soo_kqfilter,
98 .fo_close = svr4_soo_close
101 static struct cdevsw streams_cdevsw = {
102 .d_version = D_VERSION,
103 .d_open = streamsopen,
107 struct streams_softc {
108 struct isa_device *dev;
111 #define UNIT(dev) dev2unit(dev) /* assume one minor number per unit */
113 typedef struct streams_softc *sc_p;
116 streams_modevent(module_t mod, int type, void *unused)
120 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
122 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
124 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
126 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
128 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
130 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
132 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
134 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
135 0, 0, 0666, "ticlts");
136 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
137 0, 0, 0666, "ticots");
138 dt_unix_ord_stream = make_dev(&streams_cdevsw,
139 dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
141 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
142 dt_udp && dt_rawip && dt_unix_dgram &&
143 dt_unix_stream && dt_unix_ord_stream)) {
144 printf("WARNING: device config for STREAMS failed\n");
145 printf("Suggest unloading streams KLD\n");
149 /* XXX should check to see if it's busy first */
152 destroy_dev(dt_icmp);
156 destroy_dev(dt_rawip);
157 destroy_dev(dt_unix_dgram);
158 destroy_dev(dt_unix_stream);
159 destroy_dev(dt_unix_ord_stream);
169 static moduledata_t streams_mod = {
174 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
175 MODULE_VERSION(streams, 1);
178 * We only need open() and close() routines. open() calls socreate()
179 * to allocate a "real" object behind the stream and mallocs some state
180 * info for use by the svr4 emulator; close() deallocates the state
181 * information and passes the underlying object to the normal socket close
185 streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
187 struct filedesc *fdp;
188 struct svr4_strm *st;
191 int family, type, protocol;
194 if (td->td_dupfd >= 0)
197 switch (dev2unit(dev)) {
201 protocol = IPPROTO_UDP;
207 protocol = IPPROTO_TCP;
214 protocol = IPPROTO_IP;
220 protocol = IPPROTO_ICMP;
229 case dev_unix_stream:
230 case dev_unix_ord_stream:
237 return svr4_ptm_alloc(td);
243 fdp = td->td_proc->p_fd;
244 if ((error = falloc(td, &fp, &fd)) != 0)
246 /* An extra reference on `fp' has been held for us by falloc(). */
248 error = socreate(family, &so, type, protocol, td->td_ucred, td);
250 fdclose(fdp, fp, fd, td);
255 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops);
258 * Allocate a stream structure and attach it to this socket.
259 * We don't bother locking so_emuldata for SVR4 stream sockets as
260 * its value is constant for the lifetime of the stream once it
261 * is initialized here.
263 st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
264 st->s_family = so->so_proto->pr_domain->dom_family;
268 so->so_emuldata = st;
280 * XXX this is very, very ugly. But I can't find a better
281 * way that won't duplicate a big amount of code from
282 * sys_open(). Ho hum...
284 * Fortunately for us, Solaris (at least 2.5.1) makes the
285 * /dev/ptmx open automatically just open a pty, that (after
286 * STREAMS I_PUSHes), is just a plain pty. fstat() is used
287 * to get the minor device number to map to a tty.
289 * Cycle through the names. If sys_open() returns ENOENT (or
290 * ENXIO), short circuit the cycle and exit.
292 static char ptyname[] = "/dev/ptyXX";
293 static char ttyletters[] = "pqrstuwxyzPQRST";
294 static char ttynumbers[] = "0123456789abcdef";
304 ptyname[8] = ttyletters[l];
305 ptyname[9] = ttynumbers[n];
307 error = kern_open(td, ptyname, UIO_SYSSPACE, O_RDWR, 0);
313 td->td_dupfd = td->td_retval[0];
316 if (ttynumbers[++n] == '\0') {
317 if (ttyletters[++l] == '\0')
333 if (fp == NULL || fp->f_type != DTYPE_SOCKET)
337 return so->so_emuldata;
341 svr4_soo_close(struct file *fp, struct thread *td)
343 struct socket *so = fp->f_data;
345 /* CHECKUNIT_DIAG(ENXIO);*/
347 svr4_delete_socket(td->td_proc, fp);
348 free(so->so_emuldata, M_TEMP);
349 return soo_close(fp, td);