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 .fo_read = soo_read,
92 .fo_write = soo_write,
93 .fo_truncate = soo_truncate,
94 .fo_ioctl = soo_ioctl,
95 .fo_poll = soo_poll,
96 .fo_kqfilter = soo_kqfilter,
97 .fo_stat = soo_stat,
98 .fo_close = svr4_soo_close,
99 .fo_chmod = invfo_chmod,
100 .fo_chown = invfo_chown,
101 };
102
103 static struct cdevsw streams_cdevsw = {
104 .d_version = D_VERSION,
105 .d_open = streamsopen,
106 .d_name = "streams",
107 };
108
109 struct streams_softc {
110 struct isa_device *dev;
111 } ;
112
113 #define UNIT(dev) dev2unit(dev) /* assume one minor number per unit */
114
115 typedef struct streams_softc *sc_p;
116
117 static int
118 streams_modevent(module_t mod, int type, void *unused)
119 {
120 switch (type) {
121 case MOD_LOAD:
122 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
123 "ptm");
124 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
125 "arp");
126 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
127 "icmp");
128 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
129 "ip");
130 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
131 "tcp");
132 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
133 "udp");
134 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
135 "rawip");
136 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
137 0, 0, 0666, "ticlts");
138 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
139 0, 0, 0666, "ticots");
140 dt_unix_ord_stream = make_dev(&streams_cdevsw,
141 dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
142
143 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
144 dt_udp && dt_rawip && dt_unix_dgram &&
145 dt_unix_stream && dt_unix_ord_stream)) {
146 printf("WARNING: device config for STREAMS failed\n");
147 printf("Suggest unloading streams KLD\n");
148 }
149 return 0;
150 case MOD_UNLOAD:
151 /* XXX should check to see if it's busy first */
152 destroy_dev(dt_ptm);
153 destroy_dev(dt_arp);
154 destroy_dev(dt_icmp);
155 destroy_dev(dt_ip);
156 destroy_dev(dt_tcp);
157 destroy_dev(dt_udp);
158 destroy_dev(dt_rawip);
159 destroy_dev(dt_unix_dgram);
160 destroy_dev(dt_unix_stream);
161 destroy_dev(dt_unix_ord_stream);
162
163 return 0;
164 default:
165 return EOPNOTSUPP;
166 break;
167 }
168 return 0;
169 }
170
171 static moduledata_t streams_mod = {
172 "streams",
173 streams_modevent,
174 0
175 };
176 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
177 MODULE_VERSION(streams, 1);
178 MODULE_DEPEND(streams, svr4elf, 1, 1, 1);
179
180 /*
181 * We only need open() and close() routines. open() calls socreate()
182 * to allocate a "real" object behind the stream and mallocs some state
183 * info for use by the svr4 emulator; close() deallocates the state
184 * information and passes the underlying object to the normal socket close
185 * routine.
186 */
187 static int
188 streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
189 {
190 struct filedesc *fdp;
191 struct svr4_strm *st;
192 struct socket *so;
193 struct file *fp;
194 int family, type, protocol;
195 int error, fd;
196
197 if (td->td_dupfd >= 0)
198 return ENODEV;
199
200 switch (dev2unit(dev)) {
201 case dev_udp:
202 family = AF_INET;
203 type = SOCK_DGRAM;
204 protocol = IPPROTO_UDP;
205 break;
206
207 case dev_tcp:
208 family = AF_INET;
209 type = SOCK_STREAM;
210 protocol = IPPROTO_TCP;
211 break;
212
213 case dev_ip:
214 case dev_rawip:
215 family = AF_INET;
216 type = SOCK_RAW;
217 protocol = IPPROTO_IP;
218 break;
219
220 case dev_icmp:
221 family = AF_INET;
222 type = SOCK_RAW;
223 protocol = IPPROTO_ICMP;
224 break;
225
226 case dev_unix_dgram:
227 family = AF_LOCAL;
228 type = SOCK_DGRAM;
229 protocol = 0;
230 break;
231
232 case dev_unix_stream:
233 case dev_unix_ord_stream:
234 family = AF_LOCAL;
235 type = SOCK_STREAM;
236 protocol = 0;
237 break;
238
239 case dev_ptm:
240 return svr4_ptm_alloc(td);
241
242 default:
243 return EOPNOTSUPP;
244 }
245
246 fdp = td->td_proc->p_fd;
247 if ((error = falloc(td, &fp, &fd, 0)) != 0)
248 return error;
249 /* An extra reference on `fp' has been held for us by falloc(). */
250
251 error = socreate(family, &so, type, protocol, td->td_ucred, td);
252 if (error) {
253 fdclose(fdp, fp, fd, td);
254 fdrop(fp, td);
255 return error;
256 }
257
258 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops);
259
260 /*
261 * Allocate a stream structure and attach it to this socket.
262 * We don't bother locking so_emuldata for SVR4 stream sockets as
263 * its value is constant for the lifetime of the stream once it
264 * is initialized here.
265 */
266 st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
267 st->s_family = so->so_proto->pr_domain->dom_family;
268 st->s_cmd = ~0;
269 st->s_afd = -1;
270 st->s_eventmask = 0;
271 so->so_emuldata = st;
272
273 fdrop(fp, td);
274 td->td_dupfd = fd;
275 return ENXIO;
276 }
277
278 static int
279 svr4_ptm_alloc(td)
280 struct thread *td;
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 * XXX: Maybe this can now be implemented by posix_openpt()?
296 */
297 static char ptyname[] = "/dev/ptyXX";
298 static char ttyletters[] = "pqrstuwxyzPQRST";
299 static char ttynumbers[] = "0123456789abcdef";
300 struct proc *p;
301 register_t fd;
302 int error, l, n;
303
304 fd = -1;
305 n = 0;
306 l = 0;
307 p = td->td_proc;
308 while (fd == -1) {
309 ptyname[8] = ttyletters[l];
310 ptyname[9] = ttynumbers[n];
311
312 error = kern_open(td, ptyname, UIO_SYSSPACE, O_RDWR, 0);
313 switch (error) {
314 case ENOENT:
315 case ENXIO:
316 return error;
317 case 0:
318 td->td_dupfd = td->td_retval[0];
319 return ENXIO;
320 default:
321 if (ttynumbers[++n] == '\0') {
322 if (ttyletters[++l] == '\0')
323 break;
324 n = 0;
325 }
326 }
327 }
328 return ENOENT;
329 }
330
331
332 static int
333 svr4_soo_close(struct file *fp, struct thread *td)
334 {
335 struct socket *so = fp->f_data;
336
337 /* CHECKUNIT_DIAG(ENXIO);*/
338
339 svr4_delete_socket(td->td_proc, fp);
340 free(so->so_emuldata, M_TEMP);
341 return soo_close(fp, td);
342 }
Cache object: 0bee39ddfc76a24a562384f15c69019e
|