2001-04-07 21:37:09 +04:00
|
|
|
/* $NetBSD: svr4_net.c,v 1.25 2001/04/07 17:37:10 jdolecek Exp $ */
|
1998-09-05 18:50:25 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1994 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1994-11-14 09:13:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Emulate /dev/{udp,tcp,...}
|
|
|
|
*/
|
|
|
|
|
1998-12-18 19:55:39 +03:00
|
|
|
#define COMPAT_SVR4 1
|
|
|
|
|
1994-11-14 09:13:11 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
1997-11-17 01:50:55 +03:00
|
|
|
#include <sys/fcntl.h>
|
1994-11-14 09:13:11 +03:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
1996-06-05 23:07:37 +04:00
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/domain.h>
|
1994-11-14 09:13:11 +03:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/device.h>
|
1996-03-31 01:40:55 +03:00
|
|
|
#include <sys/conf.h>
|
1997-11-17 01:50:55 +03:00
|
|
|
#include <sys/mount.h>
|
1994-11-14 09:13:11 +03:00
|
|
|
|
1997-11-17 01:50:55 +03:00
|
|
|
#include <sys/syscallargs.h>
|
1994-11-14 09:13:11 +03:00
|
|
|
|
|
|
|
#include <compat/svr4/svr4_types.h>
|
|
|
|
#include <compat/svr4/svr4_util.h>
|
1994-11-18 05:53:37 +03:00
|
|
|
#include <compat/svr4/svr4_signal.h>
|
1999-09-07 22:20:18 +04:00
|
|
|
#include <compat/svr4/svr4_lwp.h>
|
|
|
|
#include <compat/svr4/svr4_ucontext.h>
|
1994-11-14 09:13:11 +03:00
|
|
|
#include <compat/svr4/svr4_syscallargs.h>
|
|
|
|
#include <compat/svr4/svr4_ioctl.h>
|
1994-11-18 05:53:37 +03:00
|
|
|
#include <compat/svr4/svr4_stropts.h>
|
1996-04-11 16:52:41 +04:00
|
|
|
#include <compat/svr4/svr4_socket.h>
|
1994-11-14 09:13:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Device minor numbers
|
|
|
|
*/
|
|
|
|
enum {
|
1997-11-17 01:50:55 +03:00
|
|
|
dev_ptm = 10,
|
1996-04-11 16:52:41 +04:00
|
|
|
dev_arp = 26,
|
|
|
|
dev_icmp = 27,
|
|
|
|
dev_ip = 28,
|
|
|
|
dev_tcp = 35,
|
|
|
|
dev_udp = 36,
|
|
|
|
dev_rawip = 37,
|
|
|
|
dev_unix_dgram = 38,
|
|
|
|
dev_unix_stream = 39,
|
|
|
|
dev_unix_ord_stream = 40
|
1994-11-14 09:13:11 +03:00
|
|
|
};
|
|
|
|
|
1995-10-14 23:24:14 +03:00
|
|
|
int svr4_netattach __P((int));
|
|
|
|
|
2001-02-11 04:16:42 +03:00
|
|
|
int svr4_soo_close __P((struct file *, struct proc *));
|
|
|
|
int svr4_ptm_alloc __P((struct proc *));
|
1994-11-18 05:53:37 +03:00
|
|
|
|
|
|
|
static struct fileops svr4_netops = {
|
2001-04-07 21:37:09 +04:00
|
|
|
soo_read, soo_write, soo_ioctl, soo_fcntl, soo_poll,
|
|
|
|
soo_stat, svr4_soo_close
|
1994-11-18 05:53:37 +03:00
|
|
|
};
|
1994-11-14 09:13:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used by new config, but we don't need it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
svr4_netattach(n)
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1994-12-14 23:08:30 +03:00
|
|
|
svr4_netopen(dev, flag, mode, p)
|
1994-11-14 09:13:11 +03:00
|
|
|
dev_t dev;
|
|
|
|
int flag;
|
|
|
|
int mode;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
1994-12-14 23:08:30 +03:00
|
|
|
int type, protocol;
|
|
|
|
int fd;
|
|
|
|
struct file *fp;
|
1994-11-14 09:13:11 +03:00
|
|
|
struct socket *so;
|
|
|
|
int error;
|
1996-04-11 16:52:41 +04:00
|
|
|
int family;
|
1994-11-14 09:13:11 +03:00
|
|
|
|
|
|
|
DPRINTF(("netopen("));
|
|
|
|
|
1994-12-14 23:08:30 +03:00
|
|
|
if (p->p_dupfd >= 0)
|
1994-11-14 09:13:11 +03:00
|
|
|
return ENODEV;
|
|
|
|
|
|
|
|
switch (minor(dev)) {
|
|
|
|
case dev_udp:
|
1996-04-11 16:52:41 +04:00
|
|
|
family = AF_INET;
|
1994-11-14 09:13:11 +03:00
|
|
|
type = SOCK_DGRAM;
|
|
|
|
protocol = IPPROTO_UDP;
|
|
|
|
DPRINTF(("udp, "));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dev_tcp:
|
1996-04-11 16:52:41 +04:00
|
|
|
family = AF_INET;
|
1994-11-14 09:13:11 +03:00
|
|
|
type = SOCK_STREAM;
|
|
|
|
protocol = IPPROTO_TCP;
|
|
|
|
DPRINTF(("tcp, "));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dev_ip:
|
1996-04-11 16:52:41 +04:00
|
|
|
case dev_rawip:
|
|
|
|
family = AF_INET;
|
1994-11-14 09:13:11 +03:00
|
|
|
type = SOCK_RAW;
|
|
|
|
protocol = IPPROTO_IP;
|
|
|
|
DPRINTF(("ip, "));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dev_icmp:
|
1996-04-11 16:52:41 +04:00
|
|
|
family = AF_INET;
|
1994-11-14 09:13:11 +03:00
|
|
|
type = SOCK_RAW;
|
|
|
|
protocol = IPPROTO_ICMP;
|
|
|
|
DPRINTF(("icmp, "));
|
|
|
|
break;
|
|
|
|
|
1996-04-11 16:52:41 +04:00
|
|
|
case dev_unix_dgram:
|
1998-07-18 09:04:35 +04:00
|
|
|
family = AF_LOCAL;
|
1996-04-11 16:52:41 +04:00
|
|
|
type = SOCK_DGRAM;
|
|
|
|
protocol = 0;
|
|
|
|
DPRINTF(("unix-dgram, "));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dev_unix_stream:
|
|
|
|
case dev_unix_ord_stream:
|
1998-07-18 09:04:35 +04:00
|
|
|
family = AF_LOCAL;
|
1996-04-11 16:52:41 +04:00
|
|
|
type = SOCK_STREAM;
|
|
|
|
protocol = 0;
|
|
|
|
DPRINTF(("unix-stream, "));
|
|
|
|
break;
|
|
|
|
|
1997-11-17 01:50:55 +03:00
|
|
|
case dev_ptm:
|
|
|
|
DPRINTF(("ptm);\n"));
|
|
|
|
return svr4_ptm_alloc(p);
|
|
|
|
|
1994-11-14 09:13:11 +03:00
|
|
|
default:
|
|
|
|
DPRINTF(("%d);\n", minor(dev)));
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
1999-05-06 00:01:01 +04:00
|
|
|
/* falloc() will use the descriptor for us */
|
1994-12-14 23:08:30 +03:00
|
|
|
if ((error = falloc(p, &fp, &fd)) != 0)
|
1997-11-17 01:50:55 +03:00
|
|
|
return error;
|
1994-11-14 09:13:11 +03:00
|
|
|
|
1996-04-11 16:52:41 +04:00
|
|
|
if ((error = socreate(family, &so, type, protocol)) != 0) {
|
1994-11-14 09:13:11 +03:00
|
|
|
DPRINTF(("socreate error %d\n", error));
|
2000-03-23 08:16:12 +03:00
|
|
|
fdremove(p->p_fd, fd);
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_UNUSE(fp, NULL);
|
1994-12-14 23:08:30 +03:00
|
|
|
ffree(fp);
|
1994-11-14 09:13:11 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp->f_flag = FREAD|FWRITE;
|
|
|
|
fp->f_type = DTYPE_SOCKET;
|
|
|
|
fp->f_ops = &svr4_netops;
|
1994-11-18 05:53:37 +03:00
|
|
|
|
1994-11-14 09:13:11 +03:00
|
|
|
fp->f_data = (caddr_t)so;
|
1996-06-05 23:07:37 +04:00
|
|
|
(void) svr4_stream_get(fp);
|
|
|
|
|
1994-11-14 09:13:11 +03:00
|
|
|
DPRINTF(("ok);\n"));
|
|
|
|
|
1994-12-14 23:08:30 +03:00
|
|
|
p->p_dupfd = fd;
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_UNUSE(fp, p);
|
1994-12-14 23:08:30 +03:00
|
|
|
return ENXIO;
|
1994-11-14 09:13:11 +03:00
|
|
|
}
|
1994-11-18 05:53:37 +03:00
|
|
|
|
1997-11-17 01:50:55 +03:00
|
|
|
|
2001-02-11 04:16:42 +03:00
|
|
|
int
|
1996-03-14 22:29:11 +03:00
|
|
|
svr4_soo_close(fp, p)
|
1994-11-18 05:53:37 +03:00
|
|
|
struct file *fp;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
struct socket *so = (struct socket *) fp->f_data;
|
1997-11-17 01:50:55 +03:00
|
|
|
|
1996-04-11 16:52:41 +04:00
|
|
|
svr4_delete_socket(p, fp);
|
1996-02-04 05:01:07 +03:00
|
|
|
free(so->so_internal, M_NETADDR);
|
1994-11-18 05:53:37 +03:00
|
|
|
return soo_close(fp, p);
|
|
|
|
}
|
1996-06-05 23:07:37 +04:00
|
|
|
|
|
|
|
|
2001-02-11 04:16:42 +03:00
|
|
|
int
|
1997-11-17 01:50:55 +03:00
|
|
|
svr4_ptm_alloc(p)
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* XXX this is very, very ugly. But I can't find a better
|
|
|
|
* way that won't duplicate a big amount of code from
|
|
|
|
* sys_open(). Ho hum...
|
|
|
|
*
|
|
|
|
* Fortunately for us, Solaris (at least 2.5.1) makes the
|
|
|
|
* /dev/ptmx open automatically just open a pty, that (after
|
|
|
|
* STREAMS I_PUSHes), is just a plain pty. fstat() is used
|
|
|
|
* to get the minor device number to map to a tty.
|
|
|
|
*
|
|
|
|
* Cycle through the names. If sys_open() returns ENOENT (or
|
|
|
|
* ENXIO), short circuit the cycle and exit.
|
|
|
|
*/
|
|
|
|
static char ptyname[] = "/dev/ptyXX";
|
2000-09-11 19:12:40 +04:00
|
|
|
static const char ttyletters[] = "pqrstuvwxyzPQRST";
|
|
|
|
static const char ttynumbers[] = "0123456789abcdef";
|
1997-11-17 01:50:55 +03:00
|
|
|
caddr_t sg = stackgap_init(p->p_emul);
|
|
|
|
char *path = stackgap_alloc(&sg, sizeof(ptyname));
|
|
|
|
struct sys_open_args oa;
|
|
|
|
int l = 0, n = 0;
|
|
|
|
register_t fd = -1;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
SCARG(&oa, path) = path;
|
|
|
|
SCARG(&oa, flags) = O_RDWR;
|
|
|
|
SCARG(&oa, mode) = 0;
|
|
|
|
|
|
|
|
while (fd == -1) {
|
|
|
|
ptyname[8] = ttyletters[l];
|
|
|
|
ptyname[9] = ttynumbers[n];
|
|
|
|
|
|
|
|
if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
switch (error = sys_open(p, &oa, &fd)) {
|
|
|
|
case ENOENT:
|
|
|
|
case ENXIO:
|
|
|
|
return error;
|
|
|
|
case 0:
|
|
|
|
p->p_dupfd = fd;
|
|
|
|
return ENXIO;
|
|
|
|
default:
|
|
|
|
if (ttynumbers[++n] == '\0') {
|
|
|
|
if (ttyletters[++l] == '\0')
|
|
|
|
break;
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-06-05 23:07:37 +04:00
|
|
|
struct svr4_strm *
|
|
|
|
svr4_stream_get(fp)
|
|
|
|
struct file *fp;
|
|
|
|
{
|
|
|
|
struct socket *so;
|
|
|
|
struct svr4_strm *st;
|
|
|
|
|
|
|
|
if (fp == NULL || fp->f_type != DTYPE_SOCKET)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
so = (struct socket *) fp->f_data;
|
|
|
|
|
|
|
|
if (so->so_internal)
|
|
|
|
return so->so_internal;
|
|
|
|
|
|
|
|
/* Allocate a new one. */
|
|
|
|
fp->f_ops = &svr4_netops;
|
|
|
|
st = malloc(sizeof(struct svr4_strm), M_NETADDR, M_WAITOK);
|
|
|
|
st->s_family = so->so_proto->pr_domain->dom_family;
|
|
|
|
st->s_cmd = ~0;
|
1996-08-31 03:06:24 +04:00
|
|
|
st->s_afd = -1;
|
1998-08-27 11:04:31 +04:00
|
|
|
st->s_eventmask = 0;
|
1996-06-05 23:07:37 +04:00
|
|
|
so->so_internal = st;
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|