2007-03-04 08:59:00 +03:00
|
|
|
/* $NetBSD: sys_socket.c,v 1.50 2007/03/04 06:03:09 christos Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-05-11 14:26:49 +04:00
|
|
|
* Copyright (c) 1982, 1986, 1990, 1993
|
1994-05-11 14:27:22 +04:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*
|
1998-03-01 05:20:01 +03:00
|
|
|
* @(#)sys_socket.c 8.3 (Berkeley) 2/14/95
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2007-03-04 08:59:00 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.50 2007/03/04 06:03:09 christos Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1993-12-18 07:21:37 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
1996-09-07 16:40:22 +04:00
|
|
|
#include <sys/poll.h>
|
2001-05-06 23:22:32 +04:00
|
|
|
#include <sys/proc.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-12-18 07:21:37 +03:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
struct fileops socketops = {
|
|
|
|
soo_read, soo_write, soo_ioctl, soo_fcntl, soo_poll,
|
|
|
|
soo_stat, soo_close, soo_kqfilter
|
|
|
|
};
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* ARGSUSED */
|
1993-06-27 10:01:27 +04:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
soo_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
|
|
|
|
int flags)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-04-25 21:35:18 +04:00
|
|
|
struct socket *so = (struct socket *) fp->f_data;
|
|
|
|
return ((*so->so_receive)(so, (struct mbuf **)0,
|
1993-03-21 12:45:37 +03:00
|
|
|
uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
1993-06-27 10:01:27 +04:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
soo_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
|
|
|
|
int flags)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-04-25 21:35:18 +04:00
|
|
|
struct socket *so = (struct socket *) fp->f_data;
|
2006-03-01 15:38:10 +03:00
|
|
|
return (*so->so_send)(so, (struct mbuf *)0,
|
|
|
|
uio, (struct mbuf *)0, (struct mbuf *)0, 0, curlwp);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1993-06-27 10:01:27 +04:00
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
soo_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct socket *so = (struct socket *)fp->f_data;
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
|
|
|
case FIONBIO:
|
|
|
|
if (*(int *)data)
|
|
|
|
so->so_state |= SS_NBIO;
|
|
|
|
else
|
|
|
|
so->so_state &= ~SS_NBIO;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
case FIOASYNC:
|
2001-06-17 01:29:32 +04:00
|
|
|
if (*(int *)data) {
|
2001-05-07 06:28:55 +04:00
|
|
|
so->so_state |= SS_ASYNC;
|
|
|
|
so->so_rcv.sb_flags |= SB_ASYNC;
|
|
|
|
so->so_snd.sb_flags |= SB_ASYNC;
|
|
|
|
} else {
|
|
|
|
so->so_state &= ~SS_ASYNC;
|
|
|
|
so->so_rcv.sb_flags &= ~SB_ASYNC;
|
|
|
|
so->so_snd.sb_flags &= ~SB_ASYNC;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
case FIONREAD:
|
|
|
|
*(int *)data = so->so_rcv.sb_cc;
|
|
|
|
return (0);
|
|
|
|
|
2004-11-06 05:03:20 +03:00
|
|
|
case FIONWRITE:
|
|
|
|
*(int *)data = so->so_snd.sb_cc;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
case FIONSPACE:
|
|
|
|
/*
|
|
|
|
* See the comment around sbspace()'s definition
|
|
|
|
* in sys/socketvar.h in face of counts about maximum
|
|
|
|
* to understand the following test. We detect overflow
|
|
|
|
* and return zero.
|
|
|
|
*/
|
2004-11-06 10:31:55 +03:00
|
|
|
if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
|
|
|
|
|| (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
|
2004-11-06 05:03:20 +03:00
|
|
|
*(int *)data = 0;
|
|
|
|
else
|
2004-11-06 10:31:55 +03:00
|
|
|
*(int *)data = sbspace(&so->so_snd);
|
2004-11-06 05:03:20 +03:00
|
|
|
return (0);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case SIOCSPGRP:
|
2003-09-21 23:16:48 +04:00
|
|
|
case FIOSETOWN:
|
|
|
|
case TIOCSPGRP:
|
|
|
|
return fsetown(p, &so->so_pgid, cmd, data);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
case SIOCGPGRP:
|
2003-09-21 23:16:48 +04:00
|
|
|
case FIOGETOWN:
|
|
|
|
case TIOCGPGRP:
|
|
|
|
return fgetown(p, so->so_pgid, cmd, data);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
case SIOCATMARK:
|
|
|
|
*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Interface/routing/protocol specific ioctls:
|
|
|
|
* interface and routing ioctls should have a
|
|
|
|
* different entry since a socket's unnecessary
|
|
|
|
*/
|
|
|
|
if (IOCGROUP(cmd) == 'i')
|
2005-12-11 15:16:03 +03:00
|
|
|
return (ifioctl(so, cmd, data, l));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (IOCGROUP(cmd) == 'r')
|
2005-12-11 15:16:03 +03:00
|
|
|
return (rtioctl(cmd, data, l));
|
2005-02-27 00:34:55 +03:00
|
|
|
return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
|
2005-12-11 15:16:03 +03:00
|
|
|
(struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0, l));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1999-08-04 00:19:16 +04:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
soo_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
|
1999-08-04 00:19:16 +04:00
|
|
|
{
|
|
|
|
if (cmd == F_SETFL)
|
|
|
|
return (0);
|
|
|
|
else
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
|
1993-06-27 10:01:27 +04:00
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
soo_poll(struct file *fp, int events, struct lwp *l)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct socket *so = (struct socket *)fp->f_data;
|
1996-09-07 16:40:22 +04:00
|
|
|
int revents = 0;
|
2000-03-30 13:27:11 +04:00
|
|
|
int s = splsoftnet();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-09-07 16:40:22 +04:00
|
|
|
if (events & (POLLIN | POLLRDNORM))
|
|
|
|
if (soreadable(so))
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-09-07 16:40:22 +04:00
|
|
|
if (events & (POLLOUT | POLLWRNORM))
|
2003-01-06 23:30:28 +03:00
|
|
|
if (sowritable(so))
|
1996-09-07 16:40:22 +04:00
|
|
|
revents |= events & (POLLOUT | POLLWRNORM);
|
|
|
|
|
|
|
|
if (events & (POLLPRI | POLLRDBAND))
|
|
|
|
if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
|
|
|
|
revents |= events & (POLLPRI | POLLRDBAND);
|
|
|
|
|
|
|
|
if (revents == 0) {
|
|
|
|
if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
|
2005-12-11 15:16:03 +03:00
|
|
|
selrecord(l, &so->so_rcv.sb_sel);
|
1996-09-07 16:40:22 +04:00
|
|
|
so->so_rcv.sb_flags |= SB_SEL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-09-07 16:40:22 +04:00
|
|
|
|
|
|
|
if (events & (POLLOUT | POLLWRNORM)) {
|
2005-12-11 15:16:03 +03:00
|
|
|
selrecord(l, &so->so_snd.sb_sel);
|
1996-09-07 16:40:22 +04:00
|
|
|
so->so_snd.sb_flags |= SB_SEL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1996-09-07 16:40:22 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
splx(s);
|
1996-09-07 16:40:22 +04:00
|
|
|
return (revents);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1993-06-27 10:01:27 +04:00
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
soo_stat(struct file *fp, struct stat *ub, struct lwp *l)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-04-09 14:22:00 +04:00
|
|
|
struct socket *so = (struct socket *)fp->f_data;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
memset((void *)ub, 0, sizeof(*ub));
|
1994-04-25 12:09:59 +04:00
|
|
|
ub->st_mode = S_IFSOCK;
|
1993-03-21 12:45:37 +03:00
|
|
|
return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
|
2005-12-11 15:16:03 +03:00
|
|
|
(struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0, l));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
1993-06-27 10:01:27 +04:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
soo_close(struct file *fp, struct lwp *l)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (fp->f_data)
|
|
|
|
error = soclose((struct socket *)fp->f_data);
|
|
|
|
fp->f_data = 0;
|
|
|
|
return (error);
|
|
|
|
}
|