version 1.221, 2014/02/25 18:30:11 |
version 1.235, 2014/09/05 09:20: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; |
|
|
} |
} |
|
|
/* |
/* |
* 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 515 socreate(int dom, struct socket **aso, i |
|
Line 520 socreate(int dom, struct socket **aso, i |
|
return EPROTOTYPE; |
return EPROTOTYPE; |
return EPROTONOSUPPORT; |
return EPROTONOSUPPORT; |
} |
} |
if (prp->pr_usrreq == NULL) |
if (prp->pr_usrreqs == NULL) |
return EPROTONOSUPPORT; |
return EPROTONOSUPPORT; |
if (prp->pr_type != type) |
if (prp->pr_type != type) |
return EPROTOTYPE; |
return EPROTOTYPE; |
Line 533 socreate(int dom, struct socket **aso, i |
|
Line 538 socreate(int dom, struct socket **aso, i |
|
uid = kauth_cred_geteuid(l->l_cred); |
uid = kauth_cred_geteuid(l->l_cred); |
so->so_uidinfo = uid_find(uid); |
so->so_uidinfo = uid_find(uid); |
so->so_cpid = l->l_proc->p_pid; |
so->so_cpid = l->l_proc->p_pid; |
if (lockso != NULL) { |
|
/* Caller wants us to share a lock. */ |
/* |
|
* Lock assigned and taken during PCB attach, unless we share |
|
* the lock with another socket, e.g. socketpair(2) case. |
|
*/ |
|
if (lockso) { |
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 { |
|
/* Lock assigned and taken during PRU_ATTACH. */ |
|
} |
} |
error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, |
|
(struct mbuf *)(long)proto, NULL, l); |
/* Attach the PCB (returns with the socket lock held). */ |
|
error = (*prp->pr_usrreqs->pr_attach)(so, proto); |
KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
if (error != 0) { |
|
|
if (error) { |
|
KASSERT(so->so_pcb == NULL); |
so->so_state |= SS_NOFDREF; |
so->so_state |= SS_NOFDREF; |
sofree(so); |
sofree(so); |
return error; |
return error; |
} |
} |
so->so_cred = kauth_cred_dup(l->l_cred); |
so->so_cred = kauth_cred_dup(l->l_cred); |
sounlock(so); |
sounlock(so); |
|
|
*aso = so; |
*aso = so; |
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; |
|
if (flags & SOCK_NONBLOCK) |
|
so->so_state |= SS_NBIO; |
|
} |
} |
|
fp->f_socket = so; |
|
fd_affix(curproc, fp, fd); |
|
|
|
if (sop != NULL) { |
|
*sop = so; |
|
} |
|
*fdout = fd; |
return error; |
return error; |
} |
} |
|
|
Line 611 sobind(struct socket *so, struct mbuf *n |
|
Line 629 sobind(struct socket *so, struct mbuf *n |
|
int error; |
int error; |
|
|
solock(so); |
solock(so); |
error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l); |
error = (*so->so_proto->pr_usrreqs->pr_bind)(so, nam, l); |
sounlock(so); |
sounlock(so); |
return error; |
return error; |
} |
} |
Line 624 solisten(struct socket *so, int backlog, |
|
Line 642 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_usrreqs->pr_listen)(so, l); |
NULL, NULL, l); |
|
if (error != 0) { |
if (error != 0) { |
sounlock(so); |
sounlock(so); |
return error; |
return error; |
Line 685 sofree(struct socket *so) |
|
Line 702 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 719 soclose(struct socket *so) |
|
Line 733 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 740 soclose(struct socket *so) |
|
Line 754 soclose(struct socket *so) |
|
} |
} |
drop: |
drop: |
if (so->so_pcb) { |
if (so->so_pcb) { |
error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, |
KASSERT(solocked(so)); |
NULL, NULL, NULL, NULL); |
(*so->so_proto->pr_usrreqs->pr_detach)(so); |
if (error == 0) |
|
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 767 soabort(struct socket *so) |
|
Line 778 soabort(struct socket *so) |
|
KASSERT(so->so_head == NULL); |
KASSERT(so->so_head == NULL); |
|
|
so->so_aborting++; /* XXX */ |
so->so_aborting++; /* XXX */ |
error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, |
error = (*so->so_proto->pr_usrreqs->pr_abort)(so); |
NULL, NULL, NULL); |
|
refs = --so->so_aborting; /* XXX */ |
refs = --so->so_aborting; /* XXX */ |
if (error || (refs == 0)) { |
if (error || (refs == 0)) { |
sofree(so); |
sofree(so); |
Line 781 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) |
error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, |
error = (*so->so_proto->pr_usrreqs->pr_accept)(so, nam); |
NULL, nam, NULL, NULL); |
|
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 819 soconnect(struct socket *so, struct mbuf |
|
Line 826 soconnect(struct socket *so, struct mbuf |
|
(error = sodisconnect(so)))) |
(error = sodisconnect(so)))) |
error = EISCONN; |
error = EISCONN; |
else |
else |
error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, |
error = (*so->so_proto->pr_usrreqs->pr_connect)(so, nam, 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_usrreqs->pr_connect2)(so1, so2); |
NULL, (struct mbuf *)so2, NULL, NULL); |
|
return (error); |
|
} |
} |
|
|
int |
int |
Line 848 sodisconnect(struct socket *so) |
|
Line 851 sodisconnect(struct socket *so) |
|
} else if (so->so_state & SS_ISDISCONNECTING) { |
} else if (so->so_state & SS_ISDISCONNECTING) { |
error = EALREADY; |
error = EALREADY; |
} else { |
} else { |
error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, |
error = (*so->so_proto->pr_usrreqs->pr_disconnect)(so); |
NULL, NULL, NULL, NULL); |
|
} |
} |
return (error); |
return (error); |
} |
} |
Line 1045 sosend(struct socket *so, struct mbuf *a |
|
Line 1047 sosend(struct socket *so, struct mbuf *a |
|
so->so_options |= SO_DONTROUTE; |
so->so_options |= SO_DONTROUTE; |
if (resid > 0) |
if (resid > 0) |
so->so_state |= SS_MORETOCOME; |
so->so_state |= SS_MORETOCOME; |
error = (*so->so_proto->pr_usrreq)(so, |
if (flags & MSG_OOB) |
(flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, |
error = (*so->so_proto->pr_usrreqs->pr_sendoob)(so, |
top, addr, control, curlwp); |
top, control); |
|
else |
|
error = (*so->so_proto->pr_usrreqs->pr_send)(so, |
|
top, addr, control, l); |
if (dontroute) |
if (dontroute) |
so->so_options &= ~SO_DONTROUTE; |
so->so_options &= ~SO_DONTROUTE; |
if (resid > 0) |
if (resid > 0) |
Line 1159 soreceive(struct socket *so, struct mbuf |
|
Line 1164 soreceive(struct socket *so, struct mbuf |
|
if (flags & MSG_OOB) { |
if (flags & MSG_OOB) { |
m = m_get(M_WAIT, MT_DATA); |
m = m_get(M_WAIT, MT_DATA); |
solock(so); |
solock(so); |
error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, |
error = (*pr->pr_usrreqs->pr_recvoob)(so, m, flags & MSG_PEEK); |
(struct mbuf *)(long)(flags & MSG_PEEK), NULL, l); |
|
sounlock(so); |
sounlock(so); |
if (error) |
if (error) |
goto bad; |
goto bad; |
Line 1527 soreceive(struct socket *so, struct mbuf |
|
Line 1531 soreceive(struct socket *so, struct mbuf |
|
* get it filled again. |
* get it filled again. |
*/ |
*/ |
if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) |
if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) |
(*pr->pr_usrreq)(so, PRU_RCVD, |
(*pr->pr_usrreqs->pr_rcvd)(so, flags, l); |
NULL, (struct mbuf *)(long)flags, NULL, l); |
|
SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); |
if (wakeup_state & SS_RESTARTSYS) |
if (wakeup_state & SS_RESTARTSYS) |
Line 1569 soreceive(struct socket *so, struct mbuf |
|
Line 1572 soreceive(struct socket *so, struct mbuf |
|
SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); |
if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) |
if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) |
(*pr->pr_usrreq)(so, PRU_RCVD, NULL, |
(*pr->pr_usrreqs->pr_rcvd)(so, flags, l); |
(struct mbuf *)(long)flags, NULL, l); |
|
} |
} |
if (orig_resid == uio->uio_resid && orig_resid && |
if (orig_resid == uio->uio_resid && orig_resid && |
(flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { |
(flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { |
Line 1604 soshutdown(struct socket *so, int how) |
|
Line 1606 soshutdown(struct socket *so, int how) |
|
error = 0; |
error = 0; |
} |
} |
if (how == SHUT_WR || how == SHUT_RDWR) |
if (how == SHUT_WR || how == SHUT_RDWR) |
error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, |
error = (*pr->pr_usrreqs->pr_shutdown)(so); |
NULL, NULL, NULL); |
|
|
|
return error; |
return error; |
} |
} |
Line 2172 filt_sordetach(struct knote *kn) |
|
Line 2173 filt_sordetach(struct knote *kn) |
|
{ |
{ |
struct socket *so; |
struct socket *so; |
|
|
so = ((file_t *)kn->kn_obj)->f_data; |
so = ((file_t *)kn->kn_obj)->f_socket; |
solock(so); |
solock(so); |
SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext); |
SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext); |
if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist)) |
if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist)) |
Line 2187 filt_soread(struct knote *kn, long hint) |
|
Line 2188 filt_soread(struct knote *kn, long hint) |
|
struct socket *so; |
struct socket *so; |
int rv; |
int rv; |
|
|
so = ((file_t *)kn->kn_obj)->f_data; |
so = ((file_t *)kn->kn_obj)->f_socket; |
if (hint != NOTE_SUBMIT) |
if (hint != NOTE_SUBMIT) |
solock(so); |
solock(so); |
kn->kn_data = so->so_rcv.sb_cc; |
kn->kn_data = so->so_rcv.sb_cc; |
Line 2211 filt_sowdetach(struct knote *kn) |
|
Line 2212 filt_sowdetach(struct knote *kn) |
|
{ |
{ |
struct socket *so; |
struct socket *so; |
|
|
so = ((file_t *)kn->kn_obj)->f_data; |
so = ((file_t *)kn->kn_obj)->f_socket; |
solock(so); |
solock(so); |
SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext); |
SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext); |
if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist)) |
if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist)) |
Line 2226 filt_sowrite(struct knote *kn, long hint |
|
Line 2227 filt_sowrite(struct knote *kn, long hint |
|
struct socket *so; |
struct socket *so; |
int rv; |
int rv; |
|
|
so = ((file_t *)kn->kn_obj)->f_data; |
so = ((file_t *)kn->kn_obj)->f_socket; |
if (hint != NOTE_SUBMIT) |
if (hint != NOTE_SUBMIT) |
solock(so); |
solock(so); |
kn->kn_data = sbspace(&so->so_snd); |
kn->kn_data = sbspace(&so->so_snd); |
Line 2255 filt_solisten(struct knote *kn, long hin |
|
Line 2256 filt_solisten(struct knote *kn, long hin |
|
struct socket *so; |
struct socket *so; |
int rv; |
int rv; |
|
|
so = ((file_t *)kn->kn_obj)->f_data; |
so = ((file_t *)kn->kn_obj)->f_socket; |
|
|
/* |
/* |
* Set kn_data to number of incoming connections, not |
* Set kn_data to number of incoming connections, not |
Line 2283 soo_kqfilter(struct file *fp, struct kno |
|
Line 2284 soo_kqfilter(struct file *fp, struct kno |
|
struct socket *so; |
struct socket *so; |
struct sockbuf *sb; |
struct sockbuf *sb; |
|
|
so = ((file_t *)kn->kn_obj)->f_data; |
so = ((file_t *)kn->kn_obj)->f_socket; |
solock(so); |
solock(so); |
switch (kn->kn_filter) { |
switch (kn->kn_filter) { |
case EVFILT_READ: |
case EVFILT_READ: |