streams.c revision 50477
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 * $FreeBSD: head/sys/dev/streams/streams.c 50477 1999-08-28 01:08:13Z peter $ 34 */ 35 36#include "streams.h" /* generated file.. defines NSTREAMS */ 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> /* SYSINIT stuff */ 40#include <sys/conf.h> /* cdevsw stuff */ 41#include <sys/malloc.h> /* malloc region definitions */ 42#include <sys/file.h> 43#include <sys/filedesc.h> 44#include <sys/unistd.h> 45#include <sys/fcntl.h> 46#include <sys/socket.h> 47#include <sys/protosw.h> 48#include <sys/socketvar.h> 49#include <sys/un.h> 50#include <sys/domain.h> 51#include <net/if.h> 52#include <netinet/in.h> 53#include <sys/proc.h> 54#include <sys/uio.h> 55 56#include <sys/sysproto.h> 57 58#include <svr4/svr4_types.h> 59#include <svr4/svr4_util.h> 60#include <svr4/svr4_signal.h> 61#include <svr4/svr4_ioctl.h> 62#include <svr4/svr4_stropts.h> 63#include <svr4/svr4_socket.h> 64 65static int svr4_soo_close __P((struct file *, struct proc *)); 66static int svr4_ptm_alloc __P((struct proc *)); 67static d_open_t streamsopen; 68 69struct svr4_sockcache_entry { 70 struct proc *p; /* Process for the socket */ 71 void *cookie; /* Internal cookie used for matching */ 72 struct sockaddr_un sock;/* Pathname for the socket */ 73 dev_t dev; /* Device where the socket lives on */ 74 ino_t ino; /* Inode where the socket lives on */ 75 TAILQ_ENTRY(svr4_sockcache_entry) entries; 76}; 77 78TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head; 79 80/* Initialization flag (set/queried by svr4_mod LKM) */ 81int svr4_str_initialized = 0; 82 83/* 84 * Device minor numbers 85 */ 86enum { 87 dev_ptm = 10, 88 dev_arp = 26, 89 dev_icmp = 27, 90 dev_ip = 28, 91 dev_tcp = 35, 92 dev_udp = 36, 93 dev_rawip = 37, 94 dev_unix_dgram = 38, 95 dev_unix_stream = 39, 96 dev_unix_ord_stream = 40 97}; 98 99static struct fileops svr4_netops = { 100 soo_read, soo_write, soo_ioctl, soo_poll, svr4_soo_close 101}; 102 103#define CDEV_MAJOR 103 104static struct cdevsw streams_cdevsw = { 105 /* open */ streamsopen, 106 /* close */ noclose, 107 /* read */ noread, 108 /* write */ nowrite, 109 /* ioctl */ noioctl, 110 /* stop */ nostop, 111 /* reset */ noreset, 112 /* devtotty */ nodevtotty, 113 /* poll */ nopoll, 114 /* mmap */ nommap, 115 /* strategy */ nostrategy, 116 /* name */ "streams", 117 /* parms */ noparms, 118 /* maj */ CDEV_MAJOR, 119 /* dump */ nodump, 120 /* psize */ nopsize, 121 /* flags */ 0, 122 /* maxio */ 0, 123 /* bmaj */ -1 124}; 125 126struct streams_softc { 127 struct isa_device *dev; 128} ; 129 130#define UNIT(dev) minor(dev) /* assume one minor number per unit */ 131 132typedef struct streams_softc *sc_p; 133 134static int 135streams_modevent(module_t mod, int type, void *unused) 136{ 137 switch (type) { 138 case MOD_LOAD: 139 /* XXX should make sure it isn't already loaded first */ 140 cdevsw_add(&streams_cdevsw); 141 return 0; 142 case MOD_UNLOAD: 143 /* XXX should check to see if it's busy first */ 144 cdevsw_remove(&streams_cdevsw); 145 return 0; 146 default: 147 break; 148 } 149 return 0; 150} 151 152static moduledata_t streams_mod = { 153 "streams", 154 streams_modevent, 155 0 156}; 157DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 158 159/* 160 * We only need open() and close() routines. open() calls socreate() 161 * to allocate a "real" object behind the stream and mallocs some state 162 * info for use by the svr4 emulator; close() deallocates the state 163 * information and passes the underlying object to the normal socket close 164 * routine. 165 */ 166static int 167streamsopen(dev_t dev, int oflags, int devtype, struct proc *p) 168{ 169 int type, protocol; 170 int fd; 171 struct file *fp; 172 struct socket *so; 173 int error; 174 int family; 175 176 if (p->p_dupfd >= 0) 177 return ENODEV; 178 179 switch (minor(dev)) { 180 case dev_udp: 181 family = AF_INET; 182 type = SOCK_DGRAM; 183 protocol = IPPROTO_UDP; 184 break; 185 186 case dev_tcp: 187 family = AF_INET; 188 type = SOCK_STREAM; 189 protocol = IPPROTO_TCP; 190 break; 191 192 case dev_ip: 193 case dev_rawip: 194 family = AF_INET; 195 type = SOCK_RAW; 196 protocol = IPPROTO_IP; 197 break; 198 199 case dev_icmp: 200 family = AF_INET; 201 type = SOCK_RAW; 202 protocol = IPPROTO_ICMP; 203 break; 204 205 case dev_unix_dgram: 206 family = AF_LOCAL; 207 type = SOCK_DGRAM; 208 protocol = 0; 209 break; 210 211 case dev_unix_stream: 212 case dev_unix_ord_stream: 213 family = AF_LOCAL; 214 type = SOCK_STREAM; 215 protocol = 0; 216 break; 217 218 case dev_ptm: 219 return svr4_ptm_alloc(p); 220 221 default: 222 return EOPNOTSUPP; 223 } 224 225 if ((error = falloc(p, &fp, &fd)) != 0) 226 return error; 227 228 if ((error = socreate(family, &so, type, protocol, p)) != 0) { 229 p->p_fd->fd_ofiles[fd] = 0; 230 ffree(fp); 231 return error; 232 } 233 234 fp->f_data = (caddr_t)so; 235 fp->f_flag = FREAD|FWRITE; 236 fp->f_ops = &svr4_netops; 237 fp->f_type = DTYPE_SOCKET; 238 239 (void)svr4_stream_get(fp); 240 p->p_dupfd = fd; 241 return ENXIO; 242} 243 244static int 245svr4_ptm_alloc(p) 246 struct proc *p; 247{ 248 /* 249 * XXX this is very, very ugly. But I can't find a better 250 * way that won't duplicate a big amount of code from 251 * sys_open(). Ho hum... 252 * 253 * Fortunately for us, Solaris (at least 2.5.1) makes the 254 * /dev/ptmx open automatically just open a pty, that (after 255 * STREAMS I_PUSHes), is just a plain pty. fstat() is used 256 * to get the minor device number to map to a tty. 257 * 258 * Cycle through the names. If sys_open() returns ENOENT (or 259 * ENXIO), short circuit the cycle and exit. 260 */ 261 static char ptyname[] = "/dev/ptyXX"; 262 static char ttyletters[] = "pqrstuwxyzPQRST"; 263 static char ttynumbers[] = "0123456789abcdef"; 264 caddr_t sg = stackgap_init(); 265 char *path = stackgap_alloc(&sg, sizeof(ptyname)); 266 struct open_args oa; 267 int l = 0, n = 0; 268 register_t fd = -1; 269 int error; 270 271 SCARG(&oa, path) = path; 272 SCARG(&oa, flags) = O_RDWR; 273 SCARG(&oa, mode) = 0; 274 275 while (fd == -1) { 276 ptyname[8] = ttyletters[l]; 277 ptyname[9] = ttynumbers[n]; 278 279 if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0) 280 return error; 281 282 switch (error = open(p, &oa)) { 283 case ENOENT: 284 case ENXIO: 285 return error; 286 case 0: 287 p->p_dupfd = p->p_retval[0]; 288 return ENXIO; 289 default: 290 if (ttynumbers[++n] == '\0') { 291 if (ttyletters[++l] == '\0') 292 break; 293 n = 0; 294 } 295 } 296 } 297 return ENOENT; 298} 299 300 301struct svr4_strm * 302svr4_stream_get(fp) 303 struct file *fp; 304{ 305 struct socket *so; 306 struct svr4_strm *st; 307 308 if (fp == NULL || fp->f_type != DTYPE_SOCKET) 309 return NULL; 310 311 so = (struct socket *) fp->f_data; 312 313 if (so->so_emuldata) 314 return so->so_emuldata; 315 316 /* Allocate a new one. */ 317 st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK); 318 st->s_family = so->so_proto->pr_domain->dom_family; 319 st->s_cmd = ~0; 320 st->s_afd = -1; 321 st->s_eventmask = 0; 322 so->so_emuldata = st; 323 fp->f_ops = &svr4_netops; 324 325 return st; 326} 327 328void 329svr4_delete_socket(p, fp) 330 struct proc *p; 331 struct file *fp; 332{ 333 struct svr4_sockcache_entry *e; 334 void *cookie = ((struct socket *) fp->f_data)->so_emuldata; 335 336 if (!svr4_str_initialized) { 337 TAILQ_INIT(&svr4_head); 338 svr4_str_initialized = 1; 339 return; 340 } 341 342 for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next) 343 if (e->p == p && e->cookie == cookie) { 344 TAILQ_REMOVE(&svr4_head, e, entries); 345 DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n", 346 e->sock.sun_path, p, e->dev, e->ino)); 347 free(e, M_TEMP); 348 return; 349 } 350} 351 352static int 353svr4_soo_close(struct file *fp, struct proc *p) 354{ 355 struct socket *so = (struct socket *)fp->f_data; 356 357 /* CHECKUNIT_DIAG(ENXIO);*/ 358 359 svr4_delete_socket(p, fp); 360 free(so->so_emuldata, M_TEMP); 361 return soo_close(fp, p); 362 return (0); 363} 364