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$"); 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 9043410Snewtonstatic struct fileops svr4_netops = { 91116546Sphk .fo_read = soo_read, 92116546Sphk .fo_write = soo_write, 93175140Sjhb .fo_truncate = soo_truncate, 94116546Sphk .fo_ioctl = soo_ioctl, 95116546Sphk .fo_poll = soo_poll, 96116546Sphk .fo_kqfilter = soo_kqfilter, 97116546Sphk .fo_stat = soo_stat, 98224914Skib .fo_close = svr4_soo_close, 99224914Skib .fo_chmod = invfo_chmod, 100224914Skib .fo_chown = invfo_chown, 101254415Skib .fo_sendfile = invfo_sendfile, 10243410Snewton}; 10343410Snewton 10443410Snewtonstatic struct cdevsw streams_cdevsw = { 105126080Sphk .d_version = D_VERSION, 106111815Sphk .d_open = streamsopen, 107111815Sphk .d_name = "streams", 10847625Sphk}; 10943410Snewton 11043410Snewtonstruct streams_softc { 11143410Snewton struct isa_device *dev; 11243410Snewton} ; 11343410Snewton 114183397Sed#define UNIT(dev) dev2unit(dev) /* assume one minor number per unit */ 11543410Snewton 11643410Snewtontypedef struct streams_softc *sc_p; 11743410Snewton 11844208Snewtonstatic int 11944208Snewtonstreams_modevent(module_t mod, int type, void *unused) 12044208Snewton{ 12144208Snewton switch (type) { 12244208Snewton case MOD_LOAD: 12352114Snewton dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666, 12452114Snewton "ptm"); 12552114Snewton dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666, 12652114Snewton "arp"); 12752114Snewton dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666, 12852114Snewton "icmp"); 12952114Snewton dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666, 13052114Snewton "ip"); 13152114Snewton dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666, 13252114Snewton "tcp"); 13352114Snewton dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666, 13452114Snewton "udp"); 13552114Snewton dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666, 13652114Snewton "rawip"); 13752114Snewton dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram, 13852114Snewton 0, 0, 0666, "ticlts"); 13952114Snewton dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream, 14052114Snewton 0, 0, 0666, "ticots"); 14152114Snewton dt_unix_ord_stream = make_dev(&streams_cdevsw, 14252114Snewton dev_unix_ord_stream, 0, 0, 0666, "ticotsord"); 14352114Snewton 14452114Snewton if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp && 14552114Snewton dt_udp && dt_rawip && dt_unix_dgram && 14652114Snewton dt_unix_stream && dt_unix_ord_stream)) { 14752114Snewton printf("WARNING: device config for STREAMS failed\n"); 14852114Snewton printf("Suggest unloading streams KLD\n"); 14952114Snewton } 15044208Snewton return 0; 15144208Snewton case MOD_UNLOAD: 15249344Snewton /* XXX should check to see if it's busy first */ 15353000Sphk destroy_dev(dt_ptm); 15453000Sphk destroy_dev(dt_arp); 15553000Sphk destroy_dev(dt_icmp); 15653000Sphk destroy_dev(dt_ip); 15753000Sphk destroy_dev(dt_tcp); 15853000Sphk destroy_dev(dt_udp); 15953000Sphk destroy_dev(dt_rawip); 16053000Sphk destroy_dev(dt_unix_dgram); 16153000Sphk destroy_dev(dt_unix_stream); 16253000Sphk destroy_dev(dt_unix_ord_stream); 16352114Snewton 16444208Snewton return 0; 16544208Snewton default: 166132199Sphk return EOPNOTSUPP; 16744208Snewton break; 16844208Snewton } 16944208Snewton return 0; 17044208Snewton} 17144208Snewton 17244208Snewtonstatic moduledata_t streams_mod = { 17344208Snewton "streams", 17444208Snewton streams_modevent, 175241394Skevlo 0 17644208Snewton}; 17744208SnewtonDECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 17860060SgreenMODULE_VERSION(streams, 1); 17944208Snewton 18043410Snewton/* 18143410Snewton * We only need open() and close() routines. open() calls socreate() 18243410Snewton * to allocate a "real" object behind the stream and mallocs some state 18343410Snewton * info for use by the svr4 emulator; close() deallocates the state 18443410Snewton * information and passes the underlying object to the normal socket close 18543410Snewton * routine. 18643410Snewton */ 18743410Snewtonstatic int 188130585Sphkstreamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 18943410Snewton{ 190160558Sjhb struct filedesc *fdp; 191160558Sjhb struct svr4_strm *st; 192160558Sjhb struct socket *so; 19343410Snewton struct file *fp; 194160558Sjhb int family, type, protocol; 195160558Sjhb int error, fd; 19643410Snewton 197160490Sjhb if (td->td_dupfd >= 0) 19843410Snewton return ENODEV; 19943410Snewton 200183397Sed switch (dev2unit(dev)) { 20143410Snewton case dev_udp: 20243410Snewton family = AF_INET; 20343410Snewton type = SOCK_DGRAM; 20443410Snewton protocol = IPPROTO_UDP; 20543410Snewton break; 20643410Snewton 20743410Snewton case dev_tcp: 20843410Snewton family = AF_INET; 20943410Snewton type = SOCK_STREAM; 21043410Snewton protocol = IPPROTO_TCP; 21143410Snewton break; 21243410Snewton 21343410Snewton case dev_ip: 21443410Snewton case dev_rawip: 21543410Snewton family = AF_INET; 21643410Snewton type = SOCK_RAW; 21743410Snewton protocol = IPPROTO_IP; 21843410Snewton break; 21943410Snewton 22043410Snewton case dev_icmp: 22143410Snewton family = AF_INET; 22243410Snewton type = SOCK_RAW; 22343410Snewton protocol = IPPROTO_ICMP; 22443410Snewton break; 22543410Snewton 22643410Snewton case dev_unix_dgram: 22743410Snewton family = AF_LOCAL; 22843410Snewton type = SOCK_DGRAM; 22943410Snewton protocol = 0; 23043410Snewton break; 23143410Snewton 23243410Snewton case dev_unix_stream: 23343410Snewton case dev_unix_ord_stream: 23443410Snewton family = AF_LOCAL; 23543410Snewton type = SOCK_STREAM; 23643410Snewton protocol = 0; 23743410Snewton break; 23843410Snewton 23943410Snewton case dev_ptm: 24083366Sjulian return svr4_ptm_alloc(td); 24143410Snewton 24243410Snewton default: 24343410Snewton return EOPNOTSUPP; 24443410Snewton } 24543410Snewton 246160558Sjhb fdp = td->td_proc->p_fd; 247220245Skib if ((error = falloc(td, &fp, &fd, 0)) != 0) 24843410Snewton return error; 249121256Sdwmalone /* An extra reference on `fp' has been held for us by falloc(). */ 25043410Snewton 251160558Sjhb error = socreate(family, &so, type, protocol, td->td_ucred, td); 252160558Sjhb if (error) { 253160558Sjhb fdclose(fdp, fp, fd, td); 254160558Sjhb fdrop(fp, td); 255160558Sjhb return error; 25643410Snewton } 25743410Snewton 258174988Sjeff finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops); 25943410Snewton 260160558Sjhb /* 261160558Sjhb * Allocate a stream structure and attach it to this socket. 262160558Sjhb * We don't bother locking so_emuldata for SVR4 stream sockets as 263160558Sjhb * its value is constant for the lifetime of the stream once it 264160558Sjhb * is initialized here. 265160558Sjhb */ 266160558Sjhb st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK); 267160558Sjhb st->s_family = so->so_proto->pr_domain->dom_family; 268160558Sjhb st->s_cmd = ~0; 269160558Sjhb st->s_afd = -1; 270160558Sjhb st->s_eventmask = 0; 271160558Sjhb so->so_emuldata = st; 272160558Sjhb 273121256Sdwmalone fdrop(fp, td); 27483366Sjulian td->td_dupfd = fd; 27543410Snewton return ENXIO; 27643410Snewton} 27743410Snewton 27843410Snewtonstatic int 27983366Sjuliansvr4_ptm_alloc(td) 28083366Sjulian struct thread *td; 28143410Snewton{ 28243410Snewton /* 28343410Snewton * XXX this is very, very ugly. But I can't find a better 28443410Snewton * way that won't duplicate a big amount of code from 28543410Snewton * sys_open(). Ho hum... 28643410Snewton * 28743410Snewton * Fortunately for us, Solaris (at least 2.5.1) makes the 28843410Snewton * /dev/ptmx open automatically just open a pty, that (after 28943410Snewton * STREAMS I_PUSHes), is just a plain pty. fstat() is used 29043410Snewton * to get the minor device number to map to a tty. 29143410Snewton * 29243410Snewton * Cycle through the names. If sys_open() returns ENOENT (or 29343410Snewton * ENXIO), short circuit the cycle and exit. 294188697Sed * 295188697Sed * XXX: Maybe this can now be implemented by posix_openpt()? 29643410Snewton */ 29743410Snewton static char ptyname[] = "/dev/ptyXX"; 29843410Snewton static char ttyletters[] = "pqrstuwxyzPQRST"; 29943410Snewton static char ttynumbers[] = "0123456789abcdef"; 300141466Sjhb struct proc *p; 301141466Sjhb register_t fd; 302141466Sjhb int error, l, n; 30343410Snewton 304141466Sjhb fd = -1; 305141466Sjhb n = 0; 306141466Sjhb l = 0; 307141466Sjhb p = td->td_proc; 30843410Snewton while (fd == -1) { 30943410Snewton ptyname[8] = ttyletters[l]; 31043410Snewton ptyname[9] = ttynumbers[n]; 31143410Snewton 312141466Sjhb error = kern_open(td, ptyname, UIO_SYSSPACE, O_RDWR, 0); 313141466Sjhb switch (error) { 31443410Snewton case ENOENT: 31543410Snewton case ENXIO: 31643410Snewton return error; 31743410Snewton case 0: 31883366Sjulian td->td_dupfd = td->td_retval[0]; 31943410Snewton return ENXIO; 32043410Snewton default: 32143410Snewton if (ttynumbers[++n] == '\0') { 32243410Snewton if (ttyletters[++l] == '\0') 32343410Snewton break; 32443410Snewton n = 0; 32543410Snewton } 32643410Snewton } 32743410Snewton } 32843410Snewton return ENOENT; 32943410Snewton} 33043410Snewton 33143410Snewton 33243410Snewtonstruct svr4_strm * 33343410Snewtonsvr4_stream_get(fp) 33443410Snewton struct file *fp; 33543410Snewton{ 33643410Snewton struct socket *so; 33743410Snewton 33843410Snewton if (fp == NULL || fp->f_type != DTYPE_SOCKET) 33943410Snewton return NULL; 34043410Snewton 341109153Sdillon so = fp->f_data; 342160558Sjhb return so->so_emuldata; 34343410Snewton} 34443410Snewton 34543410Snewtonstatic int 34683366Sjuliansvr4_soo_close(struct file *fp, struct thread *td) 34743410Snewton{ 348109153Sdillon struct socket *so = fp->f_data; 34943410Snewton 35043410Snewton /* CHECKUNIT_DIAG(ENXIO);*/ 35143410Snewton 35283366Sjulian svr4_delete_socket(td->td_proc, fp); 35343410Snewton free(so->so_emuldata, M_TEMP); 35483366Sjulian return soo_close(fp, td); 35543410Snewton} 356