]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/streams/streams.c
Merge ACPICA 20141107 and 20150204.
[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 /*
72  * Device minor numbers
73  */
74 enum {
75         dev_ptm                 = 10,
76         dev_arp                 = 26,
77         dev_icmp                = 27,
78         dev_ip                  = 28,
79         dev_tcp                 = 35,
80         dev_udp                 = 36,
81         dev_rawip               = 37,
82         dev_unix_dgram          = 38,
83         dev_unix_stream         = 39,
84         dev_unix_ord_stream     = 40
85 };
86
87 static struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp,
88         *dt_rawip, *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream;
89
90 static struct fileops svr4_netops;
91
92 static struct cdevsw streams_cdevsw = {
93         .d_version =    D_VERSION,
94         .d_open =       streamsopen,
95         .d_name =       "streams",
96 };
97  
98 struct streams_softc {
99         struct isa_device *dev;
100 } ;
101
102 #define UNIT(dev) dev2unit(dev) /* assume one minor number per unit */
103
104 typedef struct streams_softc *sc_p;
105
106 static  int
107 streams_modevent(module_t mod, int type, void *unused)
108 {
109         switch (type) {
110         case MOD_LOAD:
111                 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
112                         "ptm");
113                 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
114                         "arp");
115                 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
116                         "icmp");
117                 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
118                         "ip");
119                 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
120                         "tcp");
121                 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
122                         "udp");
123                 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
124                         "rawip");
125                 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
126                         0, 0, 0666, "ticlts");
127                 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
128                         0, 0, 0666, "ticots");
129                 dt_unix_ord_stream = make_dev(&streams_cdevsw,
130                         dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
131
132                 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
133                                 dt_udp && dt_rawip && dt_unix_dgram &&
134                                 dt_unix_stream && dt_unix_ord_stream)) {
135                         printf("WARNING: device config for STREAMS failed\n");
136                         printf("Suggest unloading streams KLD\n");
137                 }
138
139                 /* Inherit generic socket file operations, except close(2). */
140                 bcopy(&socketops, &svr4_netops, sizeof(struct fileops));
141                 svr4_netops.fo_close = svr4_soo_close;
142
143                 return 0;
144         case MOD_UNLOAD:
145                 /* XXX should check to see if it's busy first */
146                 destroy_dev(dt_ptm);
147                 destroy_dev(dt_arp);
148                 destroy_dev(dt_icmp);
149                 destroy_dev(dt_ip);
150                 destroy_dev(dt_tcp);
151                 destroy_dev(dt_udp);
152                 destroy_dev(dt_rawip);
153                 destroy_dev(dt_unix_dgram);
154                 destroy_dev(dt_unix_stream);
155                 destroy_dev(dt_unix_ord_stream);
156
157                 return 0;
158         default:
159                 return EOPNOTSUPP;
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 MODULE_VERSION(streams, 1);
172
173 /*
174  * We only need open() and close() routines.  open() calls socreate()
175  * to allocate a "real" object behind the stream and mallocs some state
176  * info for use by the svr4 emulator;  close() deallocates the state
177  * information and passes the underlying object to the normal socket close
178  * routine.
179  */
180 static  int
181 streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
182 {
183         struct filedesc *fdp;
184         struct svr4_strm *st;
185         struct socket *so;
186         struct file *fp;
187         int family, type, protocol;
188         int error, fd;
189         
190         if (td->td_dupfd >= 0)
191           return ENODEV;
192
193         switch (dev2unit(dev)) {
194         case dev_udp:
195           family = AF_INET;
196           type = SOCK_DGRAM;
197           protocol = IPPROTO_UDP;
198           break;
199
200         case dev_tcp:
201           family = AF_INET;
202           type = SOCK_STREAM;
203           protocol = IPPROTO_TCP;
204           break;
205
206         case dev_ip:
207         case dev_rawip:
208           family = AF_INET;
209           type = SOCK_RAW;
210           protocol = IPPROTO_IP;
211           break;
212
213         case dev_icmp:
214           family = AF_INET;
215           type = SOCK_RAW;
216           protocol = IPPROTO_ICMP;
217           break;
218
219         case dev_unix_dgram:
220           family = AF_LOCAL;
221           type = SOCK_DGRAM;
222           protocol = 0;
223           break;
224
225         case dev_unix_stream:
226         case dev_unix_ord_stream:
227           family = AF_LOCAL;
228           type = SOCK_STREAM;
229           protocol = 0;
230           break;
231
232         case dev_ptm:
233           return svr4_ptm_alloc(td);
234
235         default:
236           return EOPNOTSUPP;
237         }
238
239         fdp = td->td_proc->p_fd;
240         if ((error = falloc(td, &fp, &fd, 0)) != 0)
241           return error;
242         /* An extra reference on `fp' has been held for us by falloc(). */
243
244         error = socreate(family, &so, type, protocol, td->td_ucred, td);
245         if (error) {
246            fdclose(fdp, fp, fd, td);
247            fdrop(fp, td);
248            return error;
249         }
250
251         finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops);
252
253         /*
254          * Allocate a stream structure and attach it to this socket.
255          * We don't bother locking so_emuldata for SVR4 stream sockets as
256          * its value is constant for the lifetime of the stream once it
257          * is initialized here.
258          */
259         st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
260         st->s_family = so->so_proto->pr_domain->dom_family;
261         st->s_cmd = ~0;
262         st->s_afd = -1;
263         st->s_eventmask = 0;
264         so->so_emuldata = st;
265
266         fdrop(fp, td);
267         td->td_dupfd = fd;
268         return ENXIO;
269 }
270
271 static int
272 svr4_ptm_alloc(td)
273         struct thread *td;
274 {
275         /*
276          * XXX this is very, very ugly.  But I can't find a better
277          * way that won't duplicate a big amount of code from
278          * sys_open().  Ho hum...
279          *
280          * Fortunately for us, Solaris (at least 2.5.1) makes the
281          * /dev/ptmx open automatically just open a pty, that (after
282          * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
283          * to get the minor device number to map to a tty.
284          * 
285          * Cycle through the names. If sys_open() returns ENOENT (or
286          * ENXIO), short circuit the cycle and exit.
287          *
288          * XXX: Maybe this can now be implemented by posix_openpt()?
289          */
290         static char ptyname[] = "/dev/ptyXX";
291         static char ttyletters[] = "pqrstuwxyzPQRST";
292         static char ttynumbers[] = "0123456789abcdef";
293         struct proc *p;
294         register_t fd;
295         int error, l, n;
296
297         fd = -1;
298         n = 0;
299         l = 0;
300         p = td->td_proc;
301         while (fd == -1) {
302                 ptyname[8] = ttyletters[l];
303                 ptyname[9] = ttynumbers[n];
304
305                 error = kern_openat(td, AT_FDCWD, ptyname, UIO_SYSSPACE,
306                     O_RDWR, 0);
307                 switch (error) {
308                 case ENOENT:
309                 case ENXIO:
310                         return error;
311                 case 0:
312                         td->td_dupfd = td->td_retval[0];
313                         return ENXIO;
314                 default:
315                         if (ttynumbers[++n] == '\0') {
316                                 if (ttyletters[++l] == '\0')
317                                         break;
318                                 n = 0;
319                         }
320                 }
321         }
322         return ENOENT;
323 }
324
325
326 struct svr4_strm *
327 svr4_stream_get(fp)
328         struct file *fp;
329 {
330         struct socket *so;
331
332         if (fp == NULL || fp->f_type != DTYPE_SOCKET)
333                 return NULL;
334
335         so = fp->f_data;
336         return so->so_emuldata;
337 }
338
339 static int
340 svr4_soo_close(struct file *fp, struct thread *td)
341 {
342         struct socket *so = fp->f_data;
343         
344         /*      CHECKUNIT_DIAG(ENXIO);*/
345
346         svr4_delete_socket(td->td_proc, fp);
347         free(so->so_emuldata, M_TEMP);
348
349         fp->f_ops = &badfileops;
350         fp->f_data = NULL;
351
352         return soclose(so);
353 }