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