1139749Simp/*- 243410Snewton * Copyright (c) 1998 Mark Newton 343410Snewton * Copyright (c) 1994 Christos Zoulas 443410Snewton * Copyright (c) 1997 Todd Vierling 543410Snewton * All rights reserved. 643410Snewton * 743410Snewton * Redistribution and use in source and binary forms, with or without 843410Snewton * modification, are permitted provided that the following conditions 943410Snewton * are met: 1043410Snewton * 1. Redistributions of source code must retain the above copyright 1143410Snewton * notice, this list of conditions and the following disclaimer. 1243410Snewton * 2. Redistributions in binary form must reproduce the above copyright 1343410Snewton * notice, this list of conditions and the following disclaimer in the 1443410Snewton * documentation and/or other materials provided with the distribution. 1543410Snewton * 3. The names of the authors may not be used to endorse or promote products 1643410Snewton * derived from this software without specific prior written permission 1743410Snewton * 1843410Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1943410Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2043410Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2143410Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2243410Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2343410Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2443410Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2543410Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2643410Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2743410Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2843410Snewton * 2943410Snewton * Stolen from NetBSD /sys/compat/svr4/svr4_net.c. Pseudo-device driver 3043410Snewton * skeleton produced from /usr/share/examples/drivers/make_pseudo_driver.sh 3143410Snewton * in 3.0-980524-SNAP then hacked a bit (but probably not enough :-). 3249263Snewton * 3343410Snewton */ 3443410Snewton 35119420Sobrien#include <sys/cdefs.h> 36119420Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/dev/streams/streams.c 298519 2016-04-23 20:29:55Z dchagin $"); 37119420Sobrien 3843410Snewton#include <sys/param.h> 3943410Snewton#include <sys/systm.h> 4043410Snewton#include <sys/kernel.h> /* SYSINIT stuff */ 4143410Snewton#include <sys/conf.h> /* cdevsw stuff */ 4243410Snewton#include <sys/malloc.h> /* malloc region definitions */ 4343410Snewton#include <sys/file.h> 4443410Snewton#include <sys/filedesc.h> 4543410Snewton#include <sys/unistd.h> 4643410Snewton#include <sys/fcntl.h> 4743410Snewton#include <sys/socket.h> 4843410Snewton#include <sys/protosw.h> 4943410Snewton#include <sys/socketvar.h> 50141466Sjhb#include <sys/syscallsubr.h> 5143410Snewton#include <sys/un.h> 5243410Snewton#include <sys/domain.h> 5343410Snewton#include <net/if.h> 5443410Snewton#include <netinet/in.h> 5543410Snewton#include <sys/proc.h> 5643410Snewton#include <sys/uio.h> 5743410Snewton 5843410Snewton#include <sys/sysproto.h> 5943410Snewton 6065314Sobrien#include <compat/svr4/svr4_types.h> 6165314Sobrien#include <compat/svr4/svr4_util.h> 6265314Sobrien#include <compat/svr4/svr4_signal.h> 6365314Sobrien#include <compat/svr4/svr4_ioctl.h> 6465314Sobrien#include <compat/svr4/svr4_stropts.h> 6565314Sobrien#include <compat/svr4/svr4_socket.h> 6643410Snewton 6792739Salfredstatic int svr4_soo_close(struct file *, struct thread *); 6892739Salfredstatic int svr4_ptm_alloc(struct thread *); 6943410Snewtonstatic d_open_t streamsopen; 7043410Snewton 7143410Snewton/* 7243410Snewton * Device minor numbers 7343410Snewton */ 7443410Snewtonenum { 7543410Snewton dev_ptm = 10, 7643410Snewton dev_arp = 26, 7743410Snewton dev_icmp = 27, 7843410Snewton dev_ip = 28, 7943410Snewton dev_tcp = 35, 8043410Snewton dev_udp = 36, 8143410Snewton dev_rawip = 37, 8243410Snewton dev_unix_dgram = 38, 8343410Snewton dev_unix_stream = 39, 8443410Snewton dev_unix_ord_stream = 40 8543410Snewton}; 8643410Snewton 87160508Sjhbstatic struct cdev *dt_ptm, *dt_arp, *dt_icmp, *dt_ip, *dt_tcp, *dt_udp, 88160508Sjhb *dt_rawip, *dt_unix_dgram, *dt_unix_stream, *dt_unix_ord_stream; 8952114Snewton 90270664Sglebiusstatic struct fileops svr4_netops; 91270664Sglebius 9243410Snewtonstatic struct cdevsw streams_cdevsw = { 93126080Sphk .d_version = D_VERSION, 94111815Sphk .d_open = streamsopen, 95111815Sphk .d_name = "streams", 9647625Sphk}; 9743410Snewton 9843410Snewtonstruct streams_softc { 9943410Snewton struct isa_device *dev; 10043410Snewton} ; 10143410Snewton 102183397Sed#define UNIT(dev) dev2unit(dev) /* assume one minor number per unit */ 10343410Snewton 10443410Snewtontypedef struct streams_softc *sc_p; 10543410Snewton 10644208Snewtonstatic int 10744208Snewtonstreams_modevent(module_t mod, int type, void *unused) 10844208Snewton{ 10944208Snewton switch (type) { 11044208Snewton case MOD_LOAD: 11152114Snewton dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666, 11252114Snewton "ptm"); 11352114Snewton dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666, 11452114Snewton "arp"); 11552114Snewton dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666, 11652114Snewton "icmp"); 11752114Snewton dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666, 11852114Snewton "ip"); 11952114Snewton dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666, 12052114Snewton "tcp"); 12152114Snewton dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666, 12252114Snewton "udp"); 12352114Snewton dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666, 12452114Snewton "rawip"); 12552114Snewton dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram, 12652114Snewton 0, 0, 0666, "ticlts"); 12752114Snewton dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream, 12852114Snewton 0, 0, 0666, "ticots"); 12952114Snewton dt_unix_ord_stream = make_dev(&streams_cdevsw, 13052114Snewton dev_unix_ord_stream, 0, 0, 0666, "ticotsord"); 13152114Snewton 13252114Snewton if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp && 13352114Snewton dt_udp && dt_rawip && dt_unix_dgram && 13452114Snewton dt_unix_stream && dt_unix_ord_stream)) { 13552114Snewton printf("WARNING: device config for STREAMS failed\n"); 13652114Snewton printf("Suggest unloading streams KLD\n"); 13752114Snewton } 138270664Sglebius 139270664Sglebius /* Inherit generic socket file operations, except close(2). */ 140270664Sglebius bcopy(&socketops, &svr4_netops, sizeof(struct fileops)); 141270664Sglebius svr4_netops.fo_close = svr4_soo_close; 142270664Sglebius 14344208Snewton return 0; 14444208Snewton case MOD_UNLOAD: 14549344Snewton /* XXX should check to see if it's busy first */ 14653000Sphk destroy_dev(dt_ptm); 14753000Sphk destroy_dev(dt_arp); 14853000Sphk destroy_dev(dt_icmp); 14953000Sphk destroy_dev(dt_ip); 15053000Sphk destroy_dev(dt_tcp); 15153000Sphk destroy_dev(dt_udp); 15253000Sphk destroy_dev(dt_rawip); 15353000Sphk destroy_dev(dt_unix_dgram); 15453000Sphk destroy_dev(dt_unix_stream); 15553000Sphk destroy_dev(dt_unix_ord_stream); 15652114Snewton 15744208Snewton return 0; 15844208Snewton default: 159132199Sphk return EOPNOTSUPP; 16044208Snewton break; 16144208Snewton } 16244208Snewton return 0; 16344208Snewton} 16444208Snewton 16544208Snewtonstatic moduledata_t streams_mod = { 16644208Snewton "streams", 16744208Snewton streams_modevent, 168241394Skevlo 0 16944208Snewton}; 17044208SnewtonDECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 17160060SgreenMODULE_VERSION(streams, 1); 172298519SdchaginMODULE_DEPEND(streams, svr4elf, 1, 1, 1); 17344208Snewton 17443410Snewton/* 17543410Snewton * We only need open() and close() routines. open() calls socreate() 17643410Snewton * to allocate a "real" object behind the stream and mallocs some state 17743410Snewton * info for use by the svr4 emulator; close() deallocates the state 17843410Snewton * information and passes the underlying object to the normal socket close 17943410Snewton * routine. 18043410Snewton */ 18143410Snewtonstatic int 182130585Sphkstreamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 18343410Snewton{ 184160558Sjhb struct svr4_strm *st; 185160558Sjhb struct socket *so; 18643410Snewton struct file *fp; 187160558Sjhb int family, type, protocol; 188160558Sjhb int error, fd; 18943410Snewton 190160490Sjhb if (td->td_dupfd >= 0) 19143410Snewton return ENODEV; 19243410Snewton 193183397Sed switch (dev2unit(dev)) { 19443410Snewton case dev_udp: 19543410Snewton family = AF_INET; 19643410Snewton type = SOCK_DGRAM; 19743410Snewton protocol = IPPROTO_UDP; 19843410Snewton break; 19943410Snewton 20043410Snewton case dev_tcp: 20143410Snewton family = AF_INET; 20243410Snewton type = SOCK_STREAM; 20343410Snewton protocol = IPPROTO_TCP; 20443410Snewton break; 20543410Snewton 20643410Snewton case dev_ip: 20743410Snewton case dev_rawip: 20843410Snewton family = AF_INET; 20943410Snewton type = SOCK_RAW; 21043410Snewton protocol = IPPROTO_IP; 21143410Snewton break; 21243410Snewton 21343410Snewton case dev_icmp: 21443410Snewton family = AF_INET; 21543410Snewton type = SOCK_RAW; 21643410Snewton protocol = IPPROTO_ICMP; 21743410Snewton break; 21843410Snewton 21943410Snewton case dev_unix_dgram: 22043410Snewton family = AF_LOCAL; 22143410Snewton type = SOCK_DGRAM; 22243410Snewton protocol = 0; 22343410Snewton break; 22443410Snewton 22543410Snewton case dev_unix_stream: 22643410Snewton case dev_unix_ord_stream: 22743410Snewton family = AF_LOCAL; 22843410Snewton type = SOCK_STREAM; 22943410Snewton protocol = 0; 23043410Snewton break; 23143410Snewton 23243410Snewton case dev_ptm: 23383366Sjulian return svr4_ptm_alloc(td); 23443410Snewton 23543410Snewton default: 23643410Snewton return EOPNOTSUPP; 23743410Snewton } 23843410Snewton 239220245Skib if ((error = falloc(td, &fp, &fd, 0)) != 0) 24043410Snewton return error; 241121256Sdwmalone /* An extra reference on `fp' has been held for us by falloc(). */ 24243410Snewton 243160558Sjhb error = socreate(family, &so, type, protocol, td->td_ucred, td); 244160558Sjhb if (error) { 245281436Smjg fdclose(td, fp, fd); 246160558Sjhb fdrop(fp, td); 247160558Sjhb return error; 24843410Snewton } 24943410Snewton 250174988Sjeff finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops); 25143410Snewton 252160558Sjhb /* 253160558Sjhb * Allocate a stream structure and attach it to this socket. 254160558Sjhb * We don't bother locking so_emuldata for SVR4 stream sockets as 255160558Sjhb * its value is constant for the lifetime of the stream once it 256160558Sjhb * is initialized here. 257160558Sjhb */ 258160558Sjhb st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK); 259160558Sjhb st->s_family = so->so_proto->pr_domain->dom_family; 260160558Sjhb st->s_cmd = ~0; 261160558Sjhb st->s_afd = -1; 262160558Sjhb st->s_eventmask = 0; 263160558Sjhb so->so_emuldata = st; 264160558Sjhb 265121256Sdwmalone fdrop(fp, td); 26683366Sjulian td->td_dupfd = fd; 26743410Snewton return ENXIO; 26843410Snewton} 26943410Snewton 27043410Snewtonstatic int 27183366Sjuliansvr4_ptm_alloc(td) 27283366Sjulian struct thread *td; 27343410Snewton{ 27443410Snewton /* 27543410Snewton * XXX this is very, very ugly. But I can't find a better 27643410Snewton * way that won't duplicate a big amount of code from 27743410Snewton * sys_open(). Ho hum... 27843410Snewton * 27943410Snewton * Fortunately for us, Solaris (at least 2.5.1) makes the 28043410Snewton * /dev/ptmx open automatically just open a pty, that (after 28143410Snewton * STREAMS I_PUSHes), is just a plain pty. fstat() is used 28243410Snewton * to get the minor device number to map to a tty. 28343410Snewton * 28443410Snewton * Cycle through the names. If sys_open() returns ENOENT (or 28543410Snewton * ENXIO), short circuit the cycle and exit. 286188697Sed * 287188697Sed * XXX: Maybe this can now be implemented by posix_openpt()? 28843410Snewton */ 28943410Snewton static char ptyname[] = "/dev/ptyXX"; 29043410Snewton static char ttyletters[] = "pqrstuwxyzPQRST"; 29143410Snewton static char ttynumbers[] = "0123456789abcdef"; 292141466Sjhb struct proc *p; 293141466Sjhb register_t fd; 294141466Sjhb int error, l, n; 29543410Snewton 296141466Sjhb fd = -1; 297141466Sjhb n = 0; 298141466Sjhb l = 0; 299141466Sjhb p = td->td_proc; 30043410Snewton while (fd == -1) { 30143410Snewton ptyname[8] = ttyletters[l]; 30243410Snewton ptyname[9] = ttynumbers[n]; 30343410Snewton 304274476Skib error = kern_openat(td, AT_FDCWD, ptyname, UIO_SYSSPACE, 305274476Skib O_RDWR, 0); 306141466Sjhb switch (error) { 30743410Snewton case ENOENT: 30843410Snewton case ENXIO: 30943410Snewton return error; 31043410Snewton case 0: 31183366Sjulian td->td_dupfd = td->td_retval[0]; 31243410Snewton return ENXIO; 31343410Snewton default: 31443410Snewton if (ttynumbers[++n] == '\0') { 31543410Snewton if (ttyletters[++l] == '\0') 31643410Snewton break; 31743410Snewton n = 0; 31843410Snewton } 31943410Snewton } 32043410Snewton } 32143410Snewton return ENOENT; 32243410Snewton} 32343410Snewton 32443410Snewton 32543410Snewtonstatic int 32683366Sjuliansvr4_soo_close(struct file *fp, struct thread *td) 32743410Snewton{ 328270664Sglebius struct socket *so = fp->f_data; 32943410Snewton 33043410Snewton /* CHECKUNIT_DIAG(ENXIO);*/ 33143410Snewton 33283366Sjulian svr4_delete_socket(td->td_proc, fp); 33343410Snewton free(so->so_emuldata, M_TEMP); 334270664Sglebius 335270664Sglebius fp->f_ops = &badfileops; 336270664Sglebius fp->f_data = NULL; 337270664Sglebius 338270664Sglebius return soclose(so); 33943410Snewton} 340