version 1.211.2.1, 2012/11/20 03:02:44 |
version 1.222, 2014/05/17 23:27:59 |
|
|
* @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 |
* @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 |
*/ |
*/ |
|
|
|
/* |
|
* Socket operation routines. |
|
* |
|
* These routines are called by the routines in sys_socket.c or from a |
|
* system process, and implement the semantics of socket operations by |
|
* switching out to the protocol specific routines. |
|
*/ |
|
|
#include <sys/cdefs.h> |
#include <sys/cdefs.h> |
__KERNEL_RCSID(0, "$NetBSD$"); |
__KERNEL_RCSID(0, "$NetBSD$"); |
|
|
Line 103 __KERNEL_RCSID(0, "$NetBSD$"); |
|
Line 111 __KERNEL_RCSID(0, "$NetBSD$"); |
|
#include <uvm/uvm_loan.h> |
#include <uvm/uvm_loan.h> |
#include <uvm/uvm_page.h> |
#include <uvm/uvm_page.h> |
|
|
MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); |
|
MALLOC_DEFINE(M_SONAME, "soname", "socket name"); |
MALLOC_DEFINE(M_SONAME, "soname", "socket name"); |
|
|
extern const struct fileops socketops; |
extern const struct fileops socketops; |
Line 416 socket_listener_cb(kauth_cred_t cred, ka |
|
Line 423 socket_listener_cb(kauth_cred_t cred, ka |
|
/* Normal users can only drop their own connections. */ |
/* Normal users can only drop their own connections. */ |
struct socket *so = (struct socket *)arg1; |
struct socket *so = (struct socket *)arg1; |
|
|
if (proc_uidmatch(cred, so->so_cred)) |
if (so->so_cred && proc_uidmatch(cred, so->so_cred) == 0) |
result = KAUTH_RESULT_ALLOW; |
result = KAUTH_RESULT_ALLOW; |
|
|
break; |
break; |
|
|
} |
} |
|
|
/* |
/* |
* Socket operation routines. |
* socreate: create a new socket of the specified type and the protocol. |
* These routines are called by the routines in |
* |
* sys_socket.c or from a system process, and |
* => Caller may specify another socket for lock sharing (must not be held). |
* implement the semantics of socket operations by |
* => Returns the new socket without lock held. |
* switching out to the protocol specific routines. |
*/ |
*/ |
|
/*ARGSUSED*/ |
|
int |
int |
socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l, |
socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l, |
struct socket *lockso) |
struct socket *lockso) |
Line 538 socreate(int dom, struct socket **aso, i |
|
Line 543 socreate(int dom, struct socket **aso, i |
|
lock = lockso->so_lock; |
lock = lockso->so_lock; |
so->so_lock = lock; |
so->so_lock = lock; |
mutex_obj_hold(lock); |
mutex_obj_hold(lock); |
|
/* XXX Why is this not solock, to match sounlock? */ |
mutex_enter(lock); |
mutex_enter(lock); |
} else { |
} else { |
/* Lock assigned and taken during PRU_ATTACH. */ |
/* Lock assigned and taken during PRU_ATTACH. */ |
Line 546 socreate(int dom, struct socket **aso, i |
|
Line 552 socreate(int dom, struct socket **aso, i |
|
(struct mbuf *)(long)proto, NULL, l); |
(struct mbuf *)(long)proto, NULL, l); |
KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
if (error != 0) { |
if (error != 0) { |
|
KASSERT(so->so_pcb == NULL); |
so->so_state |= SS_NOFDREF; |
so->so_state |= SS_NOFDREF; |
sofree(so); |
sofree(so); |
return error; |
return error; |
Line 556 socreate(int dom, struct socket **aso, i |
|
Line 563 socreate(int dom, struct socket **aso, i |
|
return 0; |
return 0; |
} |
} |
|
|
/* On success, write file descriptor to fdout and return zero. On |
/* |
* failure, return non-zero; *fdout will be undefined. |
* fsocreate: create a socket and a file descriptor associated with it. |
|
* |
|
* => On success, write file descriptor to fdout and return zero. |
|
* => On failure, return non-zero; *fdout will be undefined. |
*/ |
*/ |
int |
int |
fsocreate(int domain, struct socket **sop, int type, int protocol, |
fsocreate(int domain, struct socket **sop, int type, int proto, int *fdout) |
struct lwp *l, int *fdout) |
|
{ |
{ |
struct socket *so; |
lwp_t *l = curlwp; |
struct file *fp; |
int error, fd, flags; |
int fd, error; |
struct socket *so; |
int flags = type & SOCK_FLAGS_MASK; |
struct file *fp; |
|
|
type &= ~SOCK_FLAGS_MASK; |
if ((error = fd_allocfile(&fp, &fd)) != 0) { |
if ((error = fd_allocfile(&fp, &fd)) != 0) |
|
return error; |
return error; |
|
} |
|
flags = type & SOCK_FLAGS_MASK; |
fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); |
fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); |
fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| |
fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| |
((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); |
((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); |
fp->f_type = DTYPE_SOCKET; |
fp->f_type = DTYPE_SOCKET; |
fp->f_ops = &socketops; |
fp->f_ops = &socketops; |
error = socreate(domain, &so, type, protocol, l, NULL); |
|
if (error != 0) { |
type &= ~SOCK_FLAGS_MASK; |
|
error = socreate(domain, &so, type, proto, l, NULL); |
|
if (error) { |
fd_abort(curproc, fp, fd); |
fd_abort(curproc, fp, fd); |
} else { |
return error; |
if (sop != NULL) |
} |
*sop = so; |
if (flags & SOCK_NONBLOCK) { |
fp->f_data = so; |
so->so_state |= SS_NBIO; |
fd_affix(curproc, fp, fd); |
|
*fdout = fd; |
|
} |
} |
|
fp->f_data = so; |
|
fd_affix(curproc, fp, fd); |
|
|
|
if (sop != NULL) { |
|
*sop = so; |
|
} |
|
*fdout = fd; |
return error; |
return error; |
} |
} |
|
|
Line 621 solisten(struct socket *so, int backlog, |
|
Line 638 solisten(struct socket *so, int backlog, |
|
solock(so); |
solock(so); |
if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | |
if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | |
SS_ISDISCONNECTING)) != 0) { |
SS_ISDISCONNECTING)) != 0) { |
sounlock(so); |
sounlock(so); |
return (EINVAL); |
return EINVAL; |
} |
} |
error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, |
error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, |
NULL, NULL, l); |
NULL, NULL, l); |
Line 682 sofree(struct socket *so) |
|
Line 699 sofree(struct socket *so) |
|
} |
} |
|
|
/* |
/* |
* Close a socket on last file table reference removal. |
* soclose: close a socket on last file table reference removal. |
* Initiate disconnect if connected. |
* Initiate disconnect if connected. Free socket when disconnect complete. |
* Free socket when disconnect complete. |
|
*/ |
*/ |
int |
int |
soclose(struct socket *so) |
soclose(struct socket *so) |
{ |
{ |
struct socket *so2; |
struct socket *so2; |
int error; |
int error = 0; |
int error2; |
|
|
|
error = 0; |
|
solock(so); |
solock(so); |
if (so->so_options & SO_ACCEPTCONN) { |
if (so->so_options & SO_ACCEPTCONN) { |
for (;;) { |
for (;;) { |
Line 716 soclose(struct socket *so) |
|
Line 730 soclose(struct socket *so) |
|
break; |
break; |
} |
} |
} |
} |
if (so->so_pcb == 0) |
if (so->so_pcb == NULL) |
goto discard; |
goto discard; |
if (so->so_state & SS_ISCONNECTED) { |
if (so->so_state & SS_ISCONNECTED) { |
if ((so->so_state & SS_ISDISCONNECTING) == 0) { |
if ((so->so_state & SS_ISDISCONNECTING) == 0) { |
Line 737 soclose(struct socket *so) |
|
Line 751 soclose(struct socket *so) |
|
} |
} |
drop: |
drop: |
if (so->so_pcb) { |
if (so->so_pcb) { |
error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, |
int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, |
NULL, NULL, NULL, NULL); |
NULL, NULL, NULL, NULL); |
if (error == 0) |
if (error == 0) |
error = error2; |
error = error2; |
} |
} |
discard: |
discard: |
if (so->so_state & SS_NOFDREF) |
KASSERT((so->so_state & SS_NOFDREF) == 0); |
panic("soclose: NOFDREF"); |
|
kauth_cred_free(so->so_cred); |
kauth_cred_free(so->so_cred); |
so->so_state |= SS_NOFDREF; |
so->so_state |= SS_NOFDREF; |
sofree(so); |
sofree(so); |
return (error); |
return error; |
} |
} |
|
|
/* |
/* |
Line 778 soabort(struct socket *so) |
|
Line 791 soabort(struct socket *so) |
|
int |
int |
soaccept(struct socket *so, struct mbuf *nam) |
soaccept(struct socket *so, struct mbuf *nam) |
{ |
{ |
int error; |
int error; |
|
|
KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
|
KASSERT((so->so_state & SS_NOFDREF) != 0); |
|
|
error = 0; |
|
if ((so->so_state & SS_NOFDREF) == 0) |
|
panic("soaccept: !NOFDREF"); |
|
so->so_state &= ~SS_NOFDREF; |
so->so_state &= ~SS_NOFDREF; |
if ((so->so_state & SS_ISDISCONNECTED) == 0 || |
if ((so->so_state & SS_ISDISCONNECTED) == 0 || |
(so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) |
(so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) |
Line 793 soaccept(struct socket *so, struct mbuf |
|
Line 804 soaccept(struct socket *so, struct mbuf |
|
else |
else |
error = ECONNABORTED; |
error = ECONNABORTED; |
|
|
return (error); |
return error; |
} |
} |
|
|
int |
int |
soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) |
soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) |
{ |
{ |
int error; |
int error; |
|
|
KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
|
|
if (so->so_options & SO_ACCEPTCONN) |
if (so->so_options & SO_ACCEPTCONN) |
return (EOPNOTSUPP); |
return EOPNOTSUPP; |
/* |
/* |
* If protocol is connection-based, can only connect once. |
* If protocol is connection-based, can only connect once. |
* Otherwise, if connected, try to disconnect first. |
* Otherwise, if connected, try to disconnect first. |
Line 818 soconnect(struct socket *so, struct mbuf |
|
Line 829 soconnect(struct socket *so, struct mbuf |
|
else |
else |
error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, |
error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, |
NULL, nam, NULL, l); |
NULL, nam, NULL, l); |
return (error); |
|
|
return error; |
} |
} |
|
|
int |
int |
soconnect2(struct socket *so1, struct socket *so2) |
soconnect2(struct socket *so1, struct socket *so2) |
{ |
{ |
int error; |
|
|
|
KASSERT(solocked2(so1, so2)); |
KASSERT(solocked2(so1, so2)); |
|
|
error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, |
return (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, |
NULL, (struct mbuf *)so2, NULL, NULL); |
NULL, (struct mbuf *)so2, NULL, NULL); |
return (error); |
|
} |
} |
|
|
int |
int |
Line 874 sosend(struct socket *so, struct mbuf *a |
|
Line 883 sosend(struct socket *so, struct mbuf *a |
|
struct mbuf *control, int flags, struct lwp *l) |
struct mbuf *control, int flags, struct lwp *l) |
{ |
{ |
struct mbuf **mp, *m; |
struct mbuf **mp, *m; |
struct proc *p; |
|
long space, len, resid, clen, mlen; |
long space, len, resid, clen, mlen; |
int error, s, dontroute, atomic; |
int error, s, dontroute, atomic; |
short wakeup_state = 0; |
short wakeup_state = 0; |
|
|
p = l->l_proc; |
|
clen = 0; |
clen = 0; |
|
|
/* |
/* |
Line 926 sosend(struct socket *so, struct mbuf *a |
|
Line 933 sosend(struct socket *so, struct mbuf *a |
|
} |
} |
if ((so->so_state & SS_ISCONNECTED) == 0) { |
if ((so->so_state & SS_ISCONNECTED) == 0) { |
if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
if (so->so_proto->pr_flags & PR_CONNREQUIRED) { |
if ((so->so_state & SS_ISCONFIRMING) == 0 && |
if (resid || clen == 0) { |
!(resid == 0 && clen != 0)) { |
|
error = ENOTCONN; |
error = ENOTCONN; |
goto release; |
goto release; |
} |
} |
Line 1184 soreceive(struct socket *so, struct mbuf |
|
Line 1190 soreceive(struct socket *so, struct mbuf |
|
*/ |
*/ |
s = splsoftnet(); |
s = splsoftnet(); |
solock(so); |
solock(so); |
if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) |
|
(*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l); |
|
|
|
restart: |
restart: |
if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) { |
if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) { |
sounlock(so); |
sounlock(so); |
Line 1667 sorflush(struct socket *so) |
|
Line 1670 sorflush(struct socket *so) |
|
static int |
static int |
sosetopt1(struct socket *so, const struct sockopt *sopt) |
sosetopt1(struct socket *so, const struct sockopt *sopt) |
{ |
{ |
int error = EINVAL, optval, opt; |
int error = EINVAL, opt; |
|
int optval = 0; /* XXX: gcc */ |
struct linger l; |
struct linger l; |
struct timeval tv; |
struct timeval tv; |
|
|
Line 1909 sogetopt1(struct socket *so, struct sock |
|
Line 1913 sogetopt1(struct socket *so, struct sock |
|
#ifdef SO_OTIMESTAMP |
#ifdef SO_OTIMESTAMP |
case SO_OTIMESTAMP: |
case SO_OTIMESTAMP: |
#endif |
#endif |
|
case SO_ACCEPTCONN: |
error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0); |
error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0); |
break; |
break; |
|
|
Line 2426 sysctl_kern_socket_setup(void) |
|
Line 2431 sysctl_kern_socket_setup(void) |
|
{ |
{ |
|
|
KASSERT(socket_sysctllog == NULL); |
KASSERT(socket_sysctllog == NULL); |
sysctl_createv(&socket_sysctllog, 0, NULL, NULL, |
|
CTLFLAG_PERMANENT, |
|
CTLTYPE_NODE, "kern", NULL, |
|
NULL, 0, NULL, 0, |
|
CTL_KERN, CTL_EOL); |
|
|
|
sysctl_createv(&socket_sysctllog, 0, NULL, NULL, |
sysctl_createv(&socket_sysctllog, 0, NULL, NULL, |
CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |