Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / dev / misc / streams / streams.c
1 /*
2  * Copyright (c) 1998 Mark Newton
3  * Copyright (c) 1994 Christos Zoulas
4  * Copyright (c) 1997 Todd Vierling
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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
17  *
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.
28  *
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 :-).
32  *
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.13 2004/08/02 13:22:32 joerg Exp $
35  */
36
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 */
42 #include <sys/file.h>
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>
49 #include <sys/un.h>
50 #include <sys/domain.h>
51 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <sys/proc.h>
54 #include <sys/uio.h>
55
56 #include <sys/sysproto.h>
57
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>
64
65 static int svr4_soo_close (struct file *, struct thread *);
66 static int svr4_ptm_alloc (struct thread *);
67 static  d_open_t        streamsopen;
68
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;
76 };
77
78 TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
79
80 /* Initialization flag (set/queried by svr4_mod LKM) */
81 int svr4_str_initialized = 0;
82
83 /*
84  * Device minor numbers
85  */
86 enum {
87         dev_ptm                 = 10,
88         dev_arp                 = 26,
89         dev_icmp                = 27,
90         dev_ip                  = 28,
91         dev_tcp                 = 35,
92         dev_udp                 = 36,
93         dev_rawip               = 37,
94         dev_unix_dgram          = 38,
95         dev_unix_stream         = 39,
96         dev_unix_ord_stream     = 40
97 };
98
99 static struct fileops svr4_netops = {
100         NULL,   /* port */
101         NULL,   /* clone */
102         soo_read, soo_write, soo_ioctl, soo_poll, sokqfilter,
103         soo_stat, svr4_soo_close
104 };
105  
106 #define CDEV_MAJOR 103
107 static struct cdevsw streams_cdevsw = {
108         /* name */      "streams",
109         /* maj */       CDEV_MAJOR,
110         /* flags */     0,
111         /* port */      NULL,
112         /* clone */     NULL,
113
114         /* open */      streamsopen,
115         /* close */     noclose,
116         /* read */      noread,
117         /* write */     nowrite,
118         /* ioctl */     noioctl,
119         /* poll */      nopoll,
120         /* mmap */      nommap,
121         /* strategy */  nostrategy,
122         /* dump */      nodump,
123         /* psize */     nopsize
124 };
125  
126 struct streams_softc {
127         struct isa_device *dev;
128 } ;
129
130 #define UNIT(dev) minor(dev)    /* assume one minor number per unit */
131
132 typedef struct streams_softc *sc_p;
133
134 static  int
135 streams_modevent(module_t mod, int type, void *unused)
136 {
137         switch (type) {
138         case MOD_LOAD:
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");
153                 return 0;
154         case MOD_UNLOAD:
155                 /* XXX should check to see if it's busy first */
156                 cdevsw_remove(&streams_cdevsw, 0, 0);
157
158                 return 0;
159         default:
160                 break;
161         }
162         return 0;
163 }
164
165 static moduledata_t streams_mod = {
166         "streams",
167         streams_modevent,
168         0
169 };
170 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
171
172 /*
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
177  * routine.
178  */
179 static  int
180 streamsopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
181 {
182         struct proc *p = td->td_proc;
183         int type, protocol;
184         int fd;
185         struct file *fp;
186         struct socket *so;
187         int error;
188         int family;
189
190         KKASSERT(p != NULL);
191         
192         if (p->p_dupfd >= 0)
193           return ENODEV;
194
195         switch (minor(dev)) {
196         case dev_udp:
197           family = AF_INET;
198           type = SOCK_DGRAM;
199           protocol = IPPROTO_UDP;
200           break;
201
202         case dev_tcp:
203           family = AF_INET;
204           type = SOCK_STREAM;
205           protocol = IPPROTO_TCP;
206           break;
207
208         case dev_ip:
209         case dev_rawip:
210           family = AF_INET;
211           type = SOCK_RAW;
212           protocol = IPPROTO_IP;
213           break;
214
215         case dev_icmp:
216           family = AF_INET;
217           type = SOCK_RAW;
218           protocol = IPPROTO_ICMP;
219           break;
220
221         case dev_unix_dgram:
222           family = AF_LOCAL;
223           type = SOCK_DGRAM;
224           protocol = 0;
225           break;
226
227         case dev_unix_stream:
228         case dev_unix_ord_stream:
229           family = AF_LOCAL;
230           type = SOCK_STREAM;
231           protocol = 0;
232           break;
233
234         case dev_ptm:
235           return svr4_ptm_alloc(td);
236
237         default:
238           return EOPNOTSUPP;
239         }
240
241         if ((error = falloc(p, &fp, &fd)) != 0)
242           return error;
243
244         if ((error = socreate(family, &so, type, protocol, td)) != 0) {
245           p->p_fd->fd_ofiles[fd] = 0;
246           ffree(fp);
247           return error;
248         }
249
250         fp->f_data = (caddr_t)so;
251         fp->f_flag = FREAD|FWRITE;
252         fp->f_ops = &svr4_netops;
253         fp->f_type = DTYPE_SOCKET;
254
255         (void)svr4_stream_get(fp);
256         p->p_dupfd = fd;
257         return ENXIO;
258 }
259
260 static int
261 svr4_ptm_alloc(struct thread *td)
262 {
263         /*
264          * XXX this is very, very ugly.  But I can't find a better
265          * way that won't duplicate a big amount of code from
266          * sys_open().  Ho hum...
267          *
268          * Fortunately for us, Solaris (at least 2.5.1) makes the
269          * /dev/ptmx open automatically just open a pty, that (after
270          * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
271          * to get the minor device number to map to a tty.
272          * 
273          * Cycle through the names. If sys_open() returns ENOENT (or
274          * ENXIO), short circuit the cycle and exit.
275          */
276         static char ptyname[] = "/dev/ptyXX";
277         static char ttyletters[] = "pqrstuwxyzPQRST";
278         static char ttynumbers[] = "0123456789abcdef";
279         caddr_t sg = stackgap_init();
280         char *path = stackgap_alloc(&sg, sizeof(ptyname));
281         struct open_args oa;
282         int l = 0, n = 0;
283         register_t fd = -1;
284         int error;
285         struct proc *p = td->td_proc;
286
287         KKASSERT(p);
288
289         SCARG(&oa, path) = path;
290         SCARG(&oa, flags) = O_RDWR;
291         SCARG(&oa, mode) = 0;
292
293         while (fd == -1) {
294                 ptyname[8] = ttyletters[l];
295                 ptyname[9] = ttynumbers[n];
296
297                 if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
298                         return error;
299
300                 switch (error = open(&oa)) {
301                 case ENOENT:
302                 case ENXIO:
303                         return error;
304                 case 0:
305                         p->p_dupfd = oa.sysmsg_result;
306                         return ENXIO;
307                 default:
308                         if (ttynumbers[++n] == '\0') {
309                                 if (ttyletters[++l] == '\0')
310                                         break;
311                                 n = 0;
312                         }
313                 }
314         }
315         return ENOENT;
316 }
317
318
319 struct svr4_strm *
320 svr4_stream_get(fp)
321         struct file *fp;
322 {
323         struct socket *so;
324         struct svr4_strm *st;
325
326         if (fp == NULL || fp->f_type != DTYPE_SOCKET)
327                 return NULL;
328
329         so = (struct socket *) fp->f_data;
330
331         if (so->so_emuldata)
332                 return so->so_emuldata;
333
334         /* Allocate a new one. */
335         st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
336         st->s_family = so->so_proto->pr_domain->dom_family;
337         st->s_cmd = ~0;
338         st->s_afd = -1;
339         st->s_eventmask = 0;
340         so->so_emuldata = st;
341         fp->f_ops = &svr4_netops;
342
343         return st;
344 }
345
346 void
347 svr4_delete_socket(struct thread *td, struct file *fp)
348 {
349         struct svr4_sockcache_entry *e;
350         void *cookie = ((struct socket *) fp->f_data)->so_emuldata;
351
352         if (!svr4_str_initialized) {
353                 TAILQ_INIT(&svr4_head);
354                 svr4_str_initialized = 1;
355                 return;
356         }
357
358         TAILQ_FOREACH(e, &svr4_head, entries) {
359                 if (e->td == td && e->cookie == cookie) {
360                         TAILQ_REMOVE(&svr4_head, e, entries);
361                         DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
362                                  e->sock.sun_path, td, (int)e->dev,
363                                  (int)e->ino));
364                         free(e, M_TEMP);
365                         return;
366                 }
367         }
368 }
369
370 static int
371 svr4_soo_close(struct file *fp, struct thread *td)
372 {
373         struct socket *so = (struct socket *)fp->f_data;
374         
375         /*      CHECKUNIT_DIAG(ENXIO);*/
376
377         svr4_delete_socket(td, fp);
378         free(so->so_emuldata, M_TEMP);
379         return soo_close(fp, td);
380         return (0);
381 }