Merge from vendor branch AWK:
[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.10 2003/08/27 10:35:18 rob 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 dev_t dt_ptm, dt_arp, dt_icmp, dt_ip, dt_tcp, dt_udp, dt_rawip,
100         dt_unix_dgram, dt_unix_stream, dt_unix_ord_stream;
101
102 static struct fileops svr4_netops = {
103         NULL,   /* port */
104         0,      /* autoq */
105         soo_read, soo_write, soo_ioctl, soo_poll, sokqfilter,
106         soo_stat, svr4_soo_close
107 };
108  
109 #define CDEV_MAJOR 103
110 static struct cdevsw streams_cdevsw = {
111         /* name */      "streams",
112         /* maj */       CDEV_MAJOR,
113         /* flags */     0,
114         /* port */      NULL,
115         /* autoq */     0,
116
117         /* open */      streamsopen,
118         /* close */     noclose,
119         /* read */      noread,
120         /* write */     nowrite,
121         /* ioctl */     noioctl,
122         /* poll */      nopoll,
123         /* mmap */      nommap,
124         /* strategy */  nostrategy,
125         /* dump */      nodump,
126         /* psize */     nopsize
127 };
128  
129 struct streams_softc {
130         struct isa_device *dev;
131 } ;
132
133 #define UNIT(dev) minor(dev)    /* assume one minor number per unit */
134
135 typedef struct streams_softc *sc_p;
136
137 static  int
138 streams_modevent(module_t mod, int type, void *unused)
139 {
140         switch (type) {
141         case MOD_LOAD:
142                 /* XXX should make sure it isn't already loaded first */
143                 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
144                         "ptm");
145                 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
146                         "arp");
147                 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
148                         "icmp");
149                 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
150                         "ip");
151                 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
152                         "tcp");
153                 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
154                         "udp");
155                 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
156                         "rawip");
157                 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
158                         0, 0, 0666, "ticlts");
159                 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
160                         0, 0, 0666, "ticots");
161                 dt_unix_ord_stream = make_dev(&streams_cdevsw,
162                         dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
163
164                 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
165                                 dt_udp && dt_rawip && dt_unix_dgram &&
166                                 dt_unix_stream && dt_unix_ord_stream)) {
167                         printf("WARNING: device config for STREAMS failed\n");
168                         printf("Suggest unloading streams KLD\n");
169                 }
170                 return 0;
171         case MOD_UNLOAD:
172                 /* XXX should check to see if it's busy first */
173                 destroy_dev(dt_ptm);
174                 destroy_dev(dt_arp);
175                 destroy_dev(dt_icmp);
176                 destroy_dev(dt_ip);
177                 destroy_dev(dt_tcp);
178                 destroy_dev(dt_udp);
179                 destroy_dev(dt_rawip);
180                 destroy_dev(dt_unix_dgram);
181                 destroy_dev(dt_unix_stream);
182                 destroy_dev(dt_unix_ord_stream);
183
184                 return 0;
185         default:
186                 break;
187         }
188         return 0;
189 }
190
191 static moduledata_t streams_mod = {
192         "streams",
193         streams_modevent,
194         0
195 };
196 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
197
198 /*
199  * We only need open() and close() routines.  open() calls socreate()
200  * to allocate a "real" object behind the stream and mallocs some state
201  * info for use by the svr4 emulator;  close() deallocates the state
202  * information and passes the underlying object to the normal socket close
203  * routine.
204  */
205 static  int
206 streamsopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
207 {
208         struct proc *p = td->td_proc;
209         int type, protocol;
210         int fd;
211         struct file *fp;
212         struct socket *so;
213         int error;
214         int family;
215
216         KKASSERT(p != NULL);
217         
218         if (p->p_dupfd >= 0)
219           return ENODEV;
220
221         switch (minor(dev)) {
222         case dev_udp:
223           family = AF_INET;
224           type = SOCK_DGRAM;
225           protocol = IPPROTO_UDP;
226           break;
227
228         case dev_tcp:
229           family = AF_INET;
230           type = SOCK_STREAM;
231           protocol = IPPROTO_TCP;
232           break;
233
234         case dev_ip:
235         case dev_rawip:
236           family = AF_INET;
237           type = SOCK_RAW;
238           protocol = IPPROTO_IP;
239           break;
240
241         case dev_icmp:
242           family = AF_INET;
243           type = SOCK_RAW;
244           protocol = IPPROTO_ICMP;
245           break;
246
247         case dev_unix_dgram:
248           family = AF_LOCAL;
249           type = SOCK_DGRAM;
250           protocol = 0;
251           break;
252
253         case dev_unix_stream:
254         case dev_unix_ord_stream:
255           family = AF_LOCAL;
256           type = SOCK_STREAM;
257           protocol = 0;
258           break;
259
260         case dev_ptm:
261           return svr4_ptm_alloc(td);
262
263         default:
264           return EOPNOTSUPP;
265         }
266
267         if ((error = falloc(p, &fp, &fd)) != 0)
268           return error;
269
270         if ((error = socreate(family, &so, type, protocol, td)) != 0) {
271           p->p_fd->fd_ofiles[fd] = 0;
272           ffree(fp);
273           return error;
274         }
275
276         fp->f_data = (caddr_t)so;
277         fp->f_flag = FREAD|FWRITE;
278         fp->f_ops = &svr4_netops;
279         fp->f_type = DTYPE_SOCKET;
280
281         (void)svr4_stream_get(fp);
282         p->p_dupfd = fd;
283         return ENXIO;
284 }
285
286 static int
287 svr4_ptm_alloc(struct thread *td)
288 {
289         /*
290          * XXX this is very, very ugly.  But I can't find a better
291          * way that won't duplicate a big amount of code from
292          * sys_open().  Ho hum...
293          *
294          * Fortunately for us, Solaris (at least 2.5.1) makes the
295          * /dev/ptmx open automatically just open a pty, that (after
296          * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
297          * to get the minor device number to map to a tty.
298          * 
299          * Cycle through the names. If sys_open() returns ENOENT (or
300          * ENXIO), short circuit the cycle and exit.
301          */
302         static char ptyname[] = "/dev/ptyXX";
303         static char ttyletters[] = "pqrstuwxyzPQRST";
304         static char ttynumbers[] = "0123456789abcdef";
305         caddr_t sg = stackgap_init();
306         char *path = stackgap_alloc(&sg, sizeof(ptyname));
307         struct open_args oa;
308         int l = 0, n = 0;
309         register_t fd = -1;
310         int error;
311         struct proc *p = td->td_proc;
312
313         KKASSERT(p);
314
315         SCARG(&oa, path) = path;
316         SCARG(&oa, flags) = O_RDWR;
317         SCARG(&oa, mode) = 0;
318
319         while (fd == -1) {
320                 ptyname[8] = ttyletters[l];
321                 ptyname[9] = ttynumbers[n];
322
323                 if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
324                         return error;
325
326                 switch (error = open(&oa)) {
327                 case ENOENT:
328                 case ENXIO:
329                         return error;
330                 case 0:
331                         p->p_dupfd = oa.sysmsg_result;
332                         return ENXIO;
333                 default:
334                         if (ttynumbers[++n] == '\0') {
335                                 if (ttyletters[++l] == '\0')
336                                         break;
337                                 n = 0;
338                         }
339                 }
340         }
341         return ENOENT;
342 }
343
344
345 struct svr4_strm *
346 svr4_stream_get(fp)
347         struct file *fp;
348 {
349         struct socket *so;
350         struct svr4_strm *st;
351
352         if (fp == NULL || fp->f_type != DTYPE_SOCKET)
353                 return NULL;
354
355         so = (struct socket *) fp->f_data;
356
357         if (so->so_emuldata)
358                 return so->so_emuldata;
359
360         /* Allocate a new one. */
361         st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
362         st->s_family = so->so_proto->pr_domain->dom_family;
363         st->s_cmd = ~0;
364         st->s_afd = -1;
365         st->s_eventmask = 0;
366         so->so_emuldata = st;
367         fp->f_ops = &svr4_netops;
368
369         return st;
370 }
371
372 void
373 svr4_delete_socket(struct thread *td, struct file *fp)
374 {
375         struct svr4_sockcache_entry *e;
376         void *cookie = ((struct socket *) fp->f_data)->so_emuldata;
377
378         if (!svr4_str_initialized) {
379                 TAILQ_INIT(&svr4_head);
380                 svr4_str_initialized = 1;
381                 return;
382         }
383
384         for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next)
385                 if (e->td == td && e->cookie == cookie) {
386                         TAILQ_REMOVE(&svr4_head, e, entries);
387                         DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
388                                  e->sock.sun_path, td, (int)e->dev,
389                                  (int)e->ino));
390                         free(e, M_TEMP);
391                         return;
392                 }
393 }
394
395 static int
396 svr4_soo_close(struct file *fp, struct thread *td)
397 {
398         struct socket *so = (struct socket *)fp->f_data;
399         
400         /*      CHECKUNIT_DIAG(ENXIO);*/
401
402         svr4_delete_socket(td, fp);
403         free(so->so_emuldata, M_TEMP);
404         return soo_close(fp, td);
405         return (0);
406 }