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