]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/streams/streams.c
This commit was generated by cvs2svn to compensate for changes in r151208,
[FreeBSD/FreeBSD.git] / sys / dev / 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  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>         /* SYSINIT stuff */
41 #include <sys/conf.h>           /* cdevsw stuff */
42 #include <sys/malloc.h>         /* malloc region definitions */
43 #include <sys/file.h>
44 #include <sys/filedesc.h>
45 #include <sys/unistd.h>
46 #include <sys/fcntl.h>
47 #include <sys/socket.h>
48 #include <sys/protosw.h>
49 #include <sys/socketvar.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/un.h>
52 #include <sys/domain.h>
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <sys/proc.h>
56 #include <sys/uio.h>
57
58 #include <sys/sysproto.h>
59
60 #include <compat/svr4/svr4_types.h>
61 #include <compat/svr4/svr4_util.h>
62 #include <compat/svr4/svr4_signal.h>
63 #include <compat/svr4/svr4_ioctl.h>
64 #include <compat/svr4/svr4_stropts.h>
65 #include <compat/svr4/svr4_socket.h>
66
67 static int svr4_soo_close(struct file *, struct thread *);
68 static int svr4_ptm_alloc(struct thread *);
69 static  d_open_t        streamsopen;
70
71 struct svr4_sockcache_head svr4_head;
72
73 /* Initialization flag (set/queried by svr4_mod LKM) */
74 int svr4_str_initialized = 0;
75
76 /*
77  * Device minor numbers
78  */
79 enum {
80         dev_ptm                 = 10,
81         dev_arp                 = 26,
82         dev_icmp                = 27,
83         dev_ip                  = 28,
84         dev_tcp                 = 35,
85         dev_udp                 = 36,
86         dev_rawip               = 37,
87         dev_unix_dgram          = 38,
88         dev_unix_stream         = 39,
89         dev_unix_ord_stream     = 40
90 };
91
92 static struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp, *dt_rawip,
93         *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
94
95 static struct fileops svr4_netops = {
96         .fo_read = soo_read,
97         .fo_write = soo_write,
98         .fo_ioctl = soo_ioctl,
99         .fo_poll = soo_poll,
100         .fo_kqfilter = soo_kqfilter,
101         .fo_stat = soo_stat,
102         .fo_close =  svr4_soo_close
103 };
104  
105 static struct cdevsw streams_cdevsw = {
106         .d_version =    D_VERSION,
107         .d_flags =      D_NEEDGIANT,
108         .d_open =       streamsopen,
109         .d_name =       "streams",
110 };
111  
112 struct streams_softc {
113         struct isa_device *dev;
114 } ;
115
116 #define UNIT(dev) minor(dev)    /* assume one minor number per unit */
117
118 typedef struct streams_softc *sc_p;
119
120 static  int
121 streams_modevent(module_t mod, int type, void *unused)
122 {
123         switch (type) {
124         case MOD_LOAD:
125                 /* XXX should make sure it isn't already loaded first */
126                 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
127                         "ptm");
128                 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
129                         "arp");
130                 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
131                         "icmp");
132                 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
133                         "ip");
134                 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
135                         "tcp");
136                 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
137                         "udp");
138                 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
139                         "rawip");
140                 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
141                         0, 0, 0666, "ticlts");
142                 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
143                         0, 0, 0666, "ticots");
144                 dt_unix_ord_stream = make_dev(&streams_cdevsw,
145                         dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
146
147                 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
148                                 dt_udp && dt_rawip && dt_unix_dgram &&
149                                 dt_unix_stream && dt_unix_ord_stream)) {
150                         printf("WARNING: device config for STREAMS failed\n");
151                         printf("Suggest unloading streams KLD\n");
152                 }
153                 return 0;
154         case MOD_UNLOAD:
155                 /* XXX should check to see if it's busy first */
156                 destroy_dev(dt_ptm);
157                 destroy_dev(dt_arp);
158                 destroy_dev(dt_icmp);
159                 destroy_dev(dt_ip);
160                 destroy_dev(dt_tcp);
161                 destroy_dev(dt_udp);
162                 destroy_dev(dt_rawip);
163                 destroy_dev(dt_unix_dgram);
164                 destroy_dev(dt_unix_stream);
165                 destroy_dev(dt_unix_ord_stream);
166
167                 return 0;
168         default:
169                 return EOPNOTSUPP;
170                 break;
171         }
172         return 0;
173 }
174
175 static moduledata_t streams_mod = {
176         "streams",
177         streams_modevent,
178         0
179 };
180 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
181 MODULE_VERSION(streams, 1);
182
183 /*
184  * We only need open() and close() routines.  open() calls socreate()
185  * to allocate a "real" object behind the stream and mallocs some state
186  * info for use by the svr4 emulator;  close() deallocates the state
187  * information and passes the underlying object to the normal socket close
188  * routine.
189  */
190 static  int
191 streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
192 {
193         int type, protocol;
194         int fd, extraref;
195         struct file *fp;
196         struct socket *so;
197         int error;
198         int family;
199         struct proc *p = td->td_proc;
200         
201         PROC_LOCK(p);
202         if (td->td_dupfd >= 0) {
203           PROC_UNLOCK(p);
204           return ENODEV;
205         }
206         PROC_UNLOCK(p);
207
208         switch (minor(dev)) {
209         case dev_udp:
210           family = AF_INET;
211           type = SOCK_DGRAM;
212           protocol = IPPROTO_UDP;
213           break;
214
215         case dev_tcp:
216           family = AF_INET;
217           type = SOCK_STREAM;
218           protocol = IPPROTO_TCP;
219           break;
220
221         case dev_ip:
222         case dev_rawip:
223           family = AF_INET;
224           type = SOCK_RAW;
225           protocol = IPPROTO_IP;
226           break;
227
228         case dev_icmp:
229           family = AF_INET;
230           type = SOCK_RAW;
231           protocol = IPPROTO_ICMP;
232           break;
233
234         case dev_unix_dgram:
235           family = AF_LOCAL;
236           type = SOCK_DGRAM;
237           protocol = 0;
238           break;
239
240         case dev_unix_stream:
241         case dev_unix_ord_stream:
242           family = AF_LOCAL;
243           type = SOCK_STREAM;
244           protocol = 0;
245           break;
246
247         case dev_ptm:
248           return svr4_ptm_alloc(td);
249
250         default:
251           return EOPNOTSUPP;
252         }
253
254         if ((error = falloc(td, &fp, &fd)) != 0)
255           return error;
256         /* An extra reference on `fp' has been held for us by falloc(). */
257
258         if ((error = socreate(family, &so, type, protocol,
259             td->td_ucred, td)) != 0) {
260           FILEDESC_LOCK_FAST(p->p_fd);
261           /* Check the fd table entry hasn't changed since we made it. */
262           extraref = 0;
263           if (p->p_fd->fd_ofiles[fd] == fp) {
264             p->p_fd->fd_ofiles[fd] = NULL;
265             extraref = 1;
266           }
267           FILEDESC_UNLOCK_FAST(p->p_fd);
268           if (extraref)
269             fdrop(fp, td);
270           fdrop(fp, td);
271           return error;
272         }
273
274         FILEDESC_LOCK_FAST(p->p_fd);
275         fp->f_data = so;
276         fp->f_flag = FREAD|FWRITE;
277         fp->f_ops = &svr4_netops;
278         fp->f_type = DTYPE_SOCKET;
279         FILEDESC_UNLOCK_FAST(p->p_fd);
280
281         (void)svr4_stream_get(fp);
282         fdrop(fp, td);
283         PROC_LOCK(p);
284         td->td_dupfd = fd;
285         PROC_UNLOCK(p);
286         return ENXIO;
287 }
288
289 static int
290 svr4_ptm_alloc(td)
291         struct thread *td;
292 {
293         /*
294          * XXX this is very, very ugly.  But I can't find a better
295          * way that won't duplicate a big amount of code from
296          * sys_open().  Ho hum...
297          *
298          * Fortunately for us, Solaris (at least 2.5.1) makes the
299          * /dev/ptmx open automatically just open a pty, that (after
300          * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
301          * to get the minor device number to map to a tty.
302          * 
303          * Cycle through the names. If sys_open() returns ENOENT (or
304          * ENXIO), short circuit the cycle and exit.
305          */
306         static char ptyname[] = "/dev/ptyXX";
307         static char ttyletters[] = "pqrstuwxyzPQRST";
308         static char ttynumbers[] = "0123456789abcdef";
309         struct proc *p;
310         register_t fd;
311         int error, l, n;
312
313         fd = -1;
314         n = 0;
315         l = 0;
316         p = td->td_proc;
317         while (fd == -1) {
318                 ptyname[8] = ttyletters[l];
319                 ptyname[9] = ttynumbers[n];
320
321                 error = kern_open(td, ptyname, UIO_SYSSPACE, O_RDWR, 0);
322                 switch (error) {
323                 case ENOENT:
324                 case ENXIO:
325                         return error;
326                 case 0:
327                         PROC_LOCK(p);
328                         td->td_dupfd = td->td_retval[0];
329                         PROC_UNLOCK(p);
330                         return ENXIO;
331                 default:
332                         if (ttynumbers[++n] == '\0') {
333                                 if (ttyletters[++l] == '\0')
334                                         break;
335                                 n = 0;
336                         }
337                 }
338         }
339         return ENOENT;
340 }
341
342
343 struct svr4_strm *
344 svr4_stream_get(fp)
345         struct file *fp;
346 {
347         struct socket *so;
348         struct svr4_strm *st;
349
350         if (fp == NULL || fp->f_type != DTYPE_SOCKET)
351                 return NULL;
352
353         so = fp->f_data;
354
355         /*
356          * mpfixme: lock socketbuffer here
357          */
358         if (so->so_emuldata) {
359                 return so->so_emuldata;
360         }
361
362         /* Allocate a new one. */
363         st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
364         st->s_family = so->so_proto->pr_domain->dom_family;
365         st->s_cmd = ~0;
366         st->s_afd = -1;
367         st->s_eventmask = 0;
368         /*
369          * avoid a race where we loose due to concurrancy issues
370          * of two threads trying to allocate the so_emuldata.
371          */
372         if (so->so_emuldata) {
373                 /* lost the race, use the existing emuldata */
374                 FREE(st, M_TEMP);
375                 st = so->so_emuldata;
376         } else {
377                 /* we won, or there was no race, use our copy */
378                 so->so_emuldata = st;
379                 fp->f_ops = &svr4_netops;
380         }
381
382         return st;
383 }
384
385 void
386 svr4_delete_socket(p, fp)
387         struct proc *p;
388         struct file *fp;
389 {
390         struct svr4_sockcache_entry *e;
391         void *cookie = ((struct socket *)fp->f_data)->so_emuldata;
392
393         while (svr4_str_initialized != 2) {
394                 if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) {
395                         TAILQ_INIT(&svr4_head);
396                         atomic_store_rel_int(&svr4_str_initialized, 2);
397                 }
398                 return;
399         }
400
401         TAILQ_FOREACH(e, &svr4_head, entries)
402                 if (e->p == p && e->cookie == cookie) {
403                         TAILQ_REMOVE(&svr4_head, e, entries);
404                         DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
405                                  e->sock.sun_path, p, (int)e->dev, e->ino));
406                         free(e, M_TEMP);
407                         return;
408                 }
409 }
410
411 static int
412 svr4_soo_close(struct file *fp, struct thread *td)
413 {
414         struct socket *so = fp->f_data;
415         
416         /*      CHECKUNIT_DIAG(ENXIO);*/
417
418         svr4_delete_socket(td->td_proc, fp);
419         free(so->so_emuldata, M_TEMP);
420         return soo_close(fp, td);
421 }