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