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