streams.c revision 139749
1299529Smm/*- 2299529Smm * Copyright (c) 1998 Mark Newton 3299529Smm * Copyright (c) 1994 Christos Zoulas 4299529Smm * Copyright (c) 1997 Todd Vierling 5299529Smm * All rights reserved. 6299529Smm * 7299529Smm * Redistribution and use in source and binary forms, with or without 8299529Smm * modification, are permitted provided that the following conditions 9299529Smm * are met: 10299529Smm * 1. Redistributions of source code must retain the above copyright 11299529Smm * notice, this list of conditions and the following disclaimer. 12299529Smm * 2. Redistributions in binary form must reproduce the above copyright 13299529Smm * notice, this list of conditions and the following disclaimer in the 14299529Smm * documentation and/or other materials provided with the distribution. 15299529Smm * 3. The names of the authors may not be used to endorse or promote products 16299529Smm * derived from this software without specific prior written permission 17299529Smm * 18299529Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19299529Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20299529Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21299529Smm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22299529Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23299529Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24299529Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25299529Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26299529Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27299529Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28299529Smm * 29299529Smm * Stolen from NetBSD /sys/compat/svr4/svr4_net.c. Pseudo-device driver 30299529Smm * skeleton produced from /usr/share/examples/drivers/make_pseudo_driver.sh 31299529Smm * in 3.0-980524-SNAP then hacked a bit (but probably not enough :-). 32299529Smm * 33299529Smm */ 34299529Smm 35299529Smm#include <sys/cdefs.h> 36299529Smm__FBSDID("$FreeBSD: head/sys/dev/streams/streams.c 139749 2005-01-06 01:43:34Z imp $"); 37299529Smm 38299529Smm#include <sys/param.h> 39299529Smm#include <sys/systm.h> 40299529Smm#include <sys/kernel.h> /* SYSINIT stuff */ 41299529Smm#include <sys/conf.h> /* cdevsw stuff */ 42299529Smm#include <sys/malloc.h> /* malloc region definitions */ 43299529Smm#include <sys/file.h> 44299529Smm#include <sys/filedesc.h> 45299529Smm#include <sys/unistd.h> 46299529Smm#include <sys/fcntl.h> 47299529Smm#include <sys/socket.h> 48299529Smm#include <sys/protosw.h> 49299529Smm#include <sys/socketvar.h> 50299529Smm#include <sys/un.h> 51299529Smm#include <sys/domain.h> 52299529Smm#include <net/if.h> 53299529Smm#include <netinet/in.h> 54299529Smm#include <sys/proc.h> 55299529Smm#include <sys/uio.h> 56299529Smm 57299529Smm#include <sys/sysproto.h> 58299529Smm 59299529Smm#include <compat/svr4/svr4_types.h> 60299529Smm#include <compat/svr4/svr4_util.h> 61299529Smm#include <compat/svr4/svr4_signal.h> 62299529Smm#include <compat/svr4/svr4_ioctl.h> 63299529Smm#include <compat/svr4/svr4_stropts.h> 64299529Smm#include <compat/svr4/svr4_socket.h> 65299529Smm 66299529Smmstatic int svr4_soo_close(struct file *, struct thread *); 67299529Smmstatic int svr4_ptm_alloc(struct thread *); 68299529Smmstatic d_open_t streamsopen; 69 70struct svr4_sockcache_head svr4_head; 71 72/* Initialization flag (set/queried by svr4_mod LKM) */ 73int svr4_str_initialized = 0; 74 75/* 76 * Device minor numbers 77 */ 78enum { 79 dev_ptm = 10, 80 dev_arp = 26, 81 dev_icmp = 27, 82 dev_ip = 28, 83 dev_tcp = 35, 84 dev_udp = 36, 85 dev_rawip = 37, 86 dev_unix_dgram = 38, 87 dev_unix_stream = 39, 88 dev_unix_ord_stream = 40 89}; 90 91static struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp, *dt_rawip, 92 *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream; 93 94static struct fileops svr4_netops = { 95 .fo_read = soo_read, 96 .fo_write = soo_write, 97 .fo_ioctl = soo_ioctl, 98 .fo_poll = soo_poll, 99 .fo_kqfilter = soo_kqfilter, 100 .fo_stat = soo_stat, 101 .fo_close = svr4_soo_close 102}; 103 104static struct cdevsw streams_cdevsw = { 105 .d_version = D_VERSION, 106 .d_flags = D_NEEDGIANT, 107 .d_open = streamsopen, 108 .d_name = "streams", 109}; 110 111struct streams_softc { 112 struct isa_device *dev; 113} ; 114 115#define UNIT(dev) minor(dev) /* assume one minor number per unit */ 116 117typedef struct streams_softc *sc_p; 118 119static int 120streams_modevent(module_t mod, int type, void *unused) 121{ 122 switch (type) { 123 case MOD_LOAD: 124 /* XXX should make sure it isn't already loaded first */ 125 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666, 126 "ptm"); 127 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666, 128 "arp"); 129 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666, 130 "icmp"); 131 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666, 132 "ip"); 133 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666, 134 "tcp"); 135 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666, 136 "udp"); 137 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666, 138 "rawip"); 139 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram, 140 0, 0, 0666, "ticlts"); 141 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream, 142 0, 0, 0666, "ticots"); 143 dt_unix_ord_stream = make_dev(&streams_cdevsw, 144 dev_unix_ord_stream, 0, 0, 0666, "ticotsord"); 145 146 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp && 147 dt_udp && dt_rawip && dt_unix_dgram && 148 dt_unix_stream && dt_unix_ord_stream)) { 149 printf("WARNING: device config for STREAMS failed\n"); 150 printf("Suggest unloading streams KLD\n"); 151 } 152 return 0; 153 case MOD_UNLOAD: 154 /* XXX should check to see if it's busy first */ 155 destroy_dev(dt_ptm); 156 destroy_dev(dt_arp); 157 destroy_dev(dt_icmp); 158 destroy_dev(dt_ip); 159 destroy_dev(dt_tcp); 160 destroy_dev(dt_udp); 161 destroy_dev(dt_rawip); 162 destroy_dev(dt_unix_dgram); 163 destroy_dev(dt_unix_stream); 164 destroy_dev(dt_unix_ord_stream); 165 166 return 0; 167 default: 168 return EOPNOTSUPP; 169 break; 170 } 171 return 0; 172} 173 174static moduledata_t streams_mod = { 175 "streams", 176 streams_modevent, 177 0 178}; 179DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 180MODULE_VERSION(streams, 1); 181 182/* 183 * We only need open() and close() routines. open() calls socreate() 184 * to allocate a "real" object behind the stream and mallocs some state 185 * info for use by the svr4 emulator; close() deallocates the state 186 * information and passes the underlying object to the normal socket close 187 * routine. 188 */ 189static int 190streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 191{ 192 int type, protocol; 193 int fd, extraref; 194 struct file *fp; 195 struct socket *so; 196 int error; 197 int family; 198 struct proc *p = td->td_proc; 199 200 PROC_LOCK(p); 201 if (td->td_dupfd >= 0) { 202 PROC_UNLOCK(p); 203 return ENODEV; 204 } 205 PROC_UNLOCK(p); 206 207 switch (minor(dev)) { 208 case dev_udp: 209 family = AF_INET; 210 type = SOCK_DGRAM; 211 protocol = IPPROTO_UDP; 212 break; 213 214 case dev_tcp: 215 family = AF_INET; 216 type = SOCK_STREAM; 217 protocol = IPPROTO_TCP; 218 break; 219 220 case dev_ip: 221 case dev_rawip: 222 family = AF_INET; 223 type = SOCK_RAW; 224 protocol = IPPROTO_IP; 225 break; 226 227 case dev_icmp: 228 family = AF_INET; 229 type = SOCK_RAW; 230 protocol = IPPROTO_ICMP; 231 break; 232 233 case dev_unix_dgram: 234 family = AF_LOCAL; 235 type = SOCK_DGRAM; 236 protocol = 0; 237 break; 238 239 case dev_unix_stream: 240 case dev_unix_ord_stream: 241 family = AF_LOCAL; 242 type = SOCK_STREAM; 243 protocol = 0; 244 break; 245 246 case dev_ptm: 247 return svr4_ptm_alloc(td); 248 249 default: 250 return EOPNOTSUPP; 251 } 252 253 if ((error = falloc(td, &fp, &fd)) != 0) 254 return error; 255 /* An extra reference on `fp' has been held for us by falloc(). */ 256 257 if ((error = socreate(family, &so, type, protocol, 258 td->td_ucred, td)) != 0) { 259 FILEDESC_LOCK_FAST(p->p_fd); 260 /* Check the fd table entry hasn't changed since we made it. */ 261 extraref = 0; 262 if (p->p_fd->fd_ofiles[fd] == fp) { 263 p->p_fd->fd_ofiles[fd] = NULL; 264 extraref = 1; 265 } 266 FILEDESC_UNLOCK_FAST(p->p_fd); 267 if (extraref) 268 fdrop(fp, td); 269 fdrop(fp, td); 270 return error; 271 } 272 273 FILEDESC_LOCK_FAST(p->p_fd); 274 fp->f_data = so; 275 fp->f_flag = FREAD|FWRITE; 276 fp->f_ops = &svr4_netops; 277 fp->f_type = DTYPE_SOCKET; 278 FILEDESC_UNLOCK_FAST(p->p_fd); 279 280 (void)svr4_stream_get(fp); 281 fdrop(fp, td); 282 PROC_LOCK(p); 283 td->td_dupfd = fd; 284 PROC_UNLOCK(p); 285 return ENXIO; 286} 287 288static int 289svr4_ptm_alloc(td) 290 struct thread *td; 291{ 292 struct proc *p = td->td_proc; 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 caddr_t sg = stackgap_init(); 310 char *path = stackgap_alloc(&sg, sizeof(ptyname)); 311 struct open_args oa; 312 int l = 0, n = 0; 313 register_t fd = -1; 314 int error; 315 316 oa.path = path; 317 oa.flags = O_RDWR; 318 oa.mode = 0; 319 320 while (fd == -1) { 321 ptyname[8] = ttyletters[l]; 322 ptyname[9] = ttynumbers[n]; 323 324 if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0) 325 return error; 326 327 switch (error = open(td, &oa)) { 328 case ENOENT: 329 case ENXIO: 330 return error; 331 case 0: 332 PROC_LOCK(p); 333 td->td_dupfd = td->td_retval[0]; 334 PROC_UNLOCK(p); 335 return ENXIO; 336 default: 337 if (ttynumbers[++n] == '\0') { 338 if (ttyletters[++l] == '\0') 339 break; 340 n = 0; 341 } 342 } 343 } 344 return ENOENT; 345} 346 347 348struct svr4_strm * 349svr4_stream_get(fp) 350 struct file *fp; 351{ 352 struct socket *so; 353 struct svr4_strm *st; 354 355 if (fp == NULL || fp->f_type != DTYPE_SOCKET) 356 return NULL; 357 358 so = fp->f_data; 359 360 /* 361 * mpfixme: lock socketbuffer here 362 */ 363 if (so->so_emuldata) { 364 return so->so_emuldata; 365 } 366 367 /* Allocate a new one. */ 368 st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK); 369 st->s_family = so->so_proto->pr_domain->dom_family; 370 st->s_cmd = ~0; 371 st->s_afd = -1; 372 st->s_eventmask = 0; 373 /* 374 * avoid a race where we loose due to concurrancy issues 375 * of two threads trying to allocate the so_emuldata. 376 */ 377 if (so->so_emuldata) { 378 /* lost the race, use the existing emuldata */ 379 FREE(st, M_TEMP); 380 st = so->so_emuldata; 381 } else { 382 /* we won, or there was no race, use our copy */ 383 so->so_emuldata = st; 384 fp->f_ops = &svr4_netops; 385 } 386 387 return st; 388} 389 390void 391svr4_delete_socket(p, fp) 392 struct proc *p; 393 struct file *fp; 394{ 395 struct svr4_sockcache_entry *e; 396 void *cookie = ((struct socket *)fp->f_data)->so_emuldata; 397 398 while (svr4_str_initialized != 2) { 399 if (atomic_cmpset_acq_int(&svr4_str_initialized, 0, 1)) { 400 TAILQ_INIT(&svr4_head); 401 atomic_store_rel_int(&svr4_str_initialized, 2); 402 } 403 return; 404 } 405 406 TAILQ_FOREACH(e, &svr4_head, entries) 407 if (e->p == p && e->cookie == cookie) { 408 TAILQ_REMOVE(&svr4_head, e, entries); 409 DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n", 410 e->sock.sun_path, p, (int)e->dev, e->ino)); 411 free(e, M_TEMP); 412 return; 413 } 414} 415 416static int 417svr4_soo_close(struct file *fp, struct thread *td) 418{ 419 struct socket *so = fp->f_data; 420 421 /* CHECKUNIT_DIAG(ENXIO);*/ 422 423 svr4_delete_socket(td->td_proc, fp); 424 free(so->so_emuldata, M_TEMP); 425 return soo_close(fp, td); 426} 427