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 :-).
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h> /* SYSINIT stuff */
39 #include <sys/conf.h> /* cdevsw stuff */
40 #include <sys/malloc.h> /* malloc region definitions */
42 #include <sys/filedesc.h>
43 #include <sys/unistd.h>
44 #include <sys/fcntl.h>
45 #include <sys/socket.h>
46 #include <sys/protosw.h>
47 #include <sys/socketvar.h>
49 #include <sys/domain.h>
51 #include <netinet/in.h>
55 #include <sys/sysproto.h>
57 #include <compat/svr4/svr4_types.h>
58 #include <compat/svr4/svr4_util.h>
59 #include <compat/svr4/svr4_signal.h>
60 #include <compat/svr4/svr4_ioctl.h>
61 #include <compat/svr4/svr4_stropts.h>
62 #include <compat/svr4/svr4_socket.h>
64 static int svr4_soo_close(struct file *, struct thread *);
65 static int svr4_ptm_alloc(struct thread *);
66 static d_open_t streamsopen;
68 struct svr4_sockcache_entry {
69 struct proc *p; /* Process for the socket */
70 void *cookie; /* Internal cookie used for matching */
71 struct sockaddr_un sock;/* Pathname for the socket */
72 dev_t dev; /* Device where the socket lives on */
73 ino_t ino; /* Inode where the socket lives on */
74 TAILQ_ENTRY(svr4_sockcache_entry) entries;
77 TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
79 /* Initialization flag (set/queried by svr4_mod LKM) */
80 int svr4_str_initialized = 0;
83 * Device minor numbers
95 dev_unix_ord_stream = 40
98 static dev_t dt_ptm, dt_arp, dt_icmp, dt_ip, dt_tcp, dt_udp, dt_rawip,
99 dt_unix_dgram, dt_unix_stream, dt_unix_ord_stream;
101 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 /* open */ streamsopen,
115 /* strategy */ nostrategy,
116 /* name */ "streams",
117 /* maj */ CDEV_MAJOR,
123 struct streams_softc {
124 struct isa_device *dev;
127 #define UNIT(dev) minor(dev) /* assume one minor number per unit */
129 typedef struct streams_softc *sc_p;
132 streams_modevent(module_t mod, int type, void *unused)
136 /* XXX should make sure it isn't already loaded first */
137 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
139 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
141 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
143 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
145 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
147 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
149 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
151 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
152 0, 0, 0666, "ticlts");
153 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
154 0, 0, 0666, "ticots");
155 dt_unix_ord_stream = make_dev(&streams_cdevsw,
156 dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
158 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
159 dt_udp && dt_rawip && dt_unix_dgram &&
160 dt_unix_stream && dt_unix_ord_stream)) {
161 printf("WARNING: device config for STREAMS failed\n");
162 printf("Suggest unloading streams KLD\n");
166 /* XXX should check to see if it's busy first */
169 destroy_dev(dt_icmp);
173 destroy_dev(dt_rawip);
174 destroy_dev(dt_unix_dgram);
175 destroy_dev(dt_unix_stream);
176 destroy_dev(dt_unix_ord_stream);
185 static moduledata_t streams_mod = {
190 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
191 MODULE_VERSION(streams, 1);
194 * We only need open() and close() routines. open() calls socreate()
195 * to allocate a "real" object behind the stream and mallocs some state
196 * info for use by the svr4 emulator; close() deallocates the state
197 * information and passes the underlying object to the normal socket close
201 streamsopen(dev_t dev, int oflags, int devtype, struct thread *td)
209 struct proc *p = td->td_proc;
212 if (td->td_dupfd >= 0) {
218 switch (minor(dev)) {
222 protocol = IPPROTO_UDP;
228 protocol = IPPROTO_TCP;
235 protocol = IPPROTO_IP;
241 protocol = IPPROTO_ICMP;
250 case dev_unix_stream:
251 case dev_unix_ord_stream:
258 return svr4_ptm_alloc(td);
264 if ((error = falloc(td, &fp, &fd)) != 0)
267 if ((error = socreate(family, &so, type, protocol,
268 td->td_ucred, td)) != 0) {
269 FILEDESC_LOCK(p->p_fd);
270 p->p_fd->fd_ofiles[fd] = 0;
271 FILEDESC_UNLOCK(p->p_fd);
276 FILEDESC_LOCK(p->p_fd);
277 fp->f_data = (caddr_t)so;
278 fp->f_flag = FREAD|FWRITE;
279 fp->f_ops = &svr4_netops;
280 fp->f_type = DTYPE_SOCKET;
281 FILEDESC_UNLOCK(p->p_fd);
283 (void)svr4_stream_get(fp);
294 struct proc *p = td->td_proc;
296 * XXX this is very, very ugly. But I can't find a better
297 * way that won't duplicate a big amount of code from
298 * sys_open(). Ho hum...
300 * Fortunately for us, Solaris (at least 2.5.1) makes the
301 * /dev/ptmx open automatically just open a pty, that (after
302 * STREAMS I_PUSHes), is just a plain pty. fstat() is used
303 * to get the minor device number to map to a tty.
305 * Cycle through the names. If sys_open() returns ENOENT (or
306 * ENXIO), short circuit the cycle and exit.
308 static char ptyname[] = "/dev/ptyXX";
309 static char ttyletters[] = "pqrstuwxyzPQRST";
310 static char ttynumbers[] = "0123456789abcdef";
311 caddr_t sg = stackgap_init();
312 char *path = stackgap_alloc(&sg, sizeof(ptyname));
318 SCARG(&oa, path) = path;
319 SCARG(&oa, flags) = O_RDWR;
320 SCARG(&oa, mode) = 0;
323 ptyname[8] = ttyletters[l];
324 ptyname[9] = ttynumbers[n];
326 if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
329 switch (error = open(td, &oa)) {
335 td->td_dupfd = td->td_retval[0];
339 if (ttynumbers[++n] == '\0') {
340 if (ttyletters[++l] == '\0')
355 struct svr4_strm *st;
357 if (fp == NULL || fp->f_type != DTYPE_SOCKET)
360 so = (struct socket *) fp->f_data;
363 * mpfixme: lock socketbuffer here
365 if (so->so_emuldata) {
366 return so->so_emuldata;
369 /* Allocate a new one. */
370 st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
371 st->s_family = so->so_proto->pr_domain->dom_family;
376 * avoid a race where we loose due to concurrancy issues
377 * of two threads trying to allocate the so_emuldata.
379 if (so->so_emuldata) {
380 /* lost the race, use the existing emuldata */
382 st = so->so_emuldata;
384 /* we won, or there was no race, use our copy */
385 so->so_emuldata = st;
386 fp->f_ops = &svr4_netops;
393 svr4_delete_socket(p, fp)
397 struct svr4_sockcache_entry *e;
398 void *cookie = ((struct socket *) fp->f_data)->so_emuldata;
400 while (svr4_str_initialized != 2) {
401 if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
402 TAILQ_INIT(&svr4_head);
403 atomic_store_rel_int(&svr4_str_initialized, 2);
408 TAILQ_FOREACH(e, &svr4_head, entries)
409 if (e->p == p && e->cookie == cookie) {
410 TAILQ_REMOVE(&svr4_head, e, entries);
411 DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
412 e->sock.sun_path, p, (int)e->dev, e->ino));
419 svr4_soo_close(struct file *fp, struct thread *td)
421 struct socket *so = (struct socket *)fp->f_data;
423 /* CHECKUNIT_DIAG(ENXIO);*/
425 svr4_delete_socket(td->td_proc, fp);
426 free(so->so_emuldata, M_TEMP);
427 return soo_close(fp, td);