version 1.215.4.3, 2013/10/18 02:32:12 |
version 1.233, 2014/08/08 03:05:45 |
Line 423 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) == 0) |
if (so->so_cred && proc_uidmatch(cred, so->so_cred) == 0) |
result = KAUTH_RESULT_ALLOW; |
result = KAUTH_RESULT_ALLOW; |
|
|
break; |
break; |
Line 528 socreate(int dom, struct socket **aso, i |
|
Line 528 socreate(int dom, struct socket **aso, i |
|
so = soget(true); |
so = soget(true); |
so->so_type = type; |
so->so_type = type; |
so->so_proto = prp; |
so->so_proto = prp; |
so->so_refcnt = 1; |
|
|
|
so->so_send = sosend; |
so->so_send = sosend; |
so->so_receive = soreceive; |
so->so_receive = soreceive; |
#ifdef MBUFTRACE |
#ifdef MBUFTRACE |
Line 542 socreate(int dom, struct socket **aso, i |
|
Line 540 socreate(int dom, struct socket **aso, i |
|
so->so_cpid = l->l_proc->p_pid; |
so->so_cpid = l->l_proc->p_pid; |
|
|
/* |
/* |
* Lock assigned and taken during pr_attach, unless we share |
* Lock assigned and taken during PCB attach, unless we share |
* the lock with another socket, e.g. socketpair(2) case. |
* the lock with another socket, e.g. socketpair(2) case. |
*/ |
*/ |
if (lockso) { |
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); |
|
mutex_enter(lock); |
} |
} |
|
|
|
/* Attach the PCB (returns with the socket lock held). */ |
error = (*prp->pr_usrreqs->pr_attach)(so, proto); |
error = (*prp->pr_usrreqs->pr_attach)(so, proto); |
|
KASSERT(solocked(so)); |
|
|
if (error) { |
if (error) { |
solock(so); |
|
KASSERT(so->so_pcb == NULL); |
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); |
|
|
*aso = so; |
*aso = so; |
return 0; |
return 0; |
Line 572 socreate(int dom, struct socket **aso, i |
|
Line 574 socreate(int dom, struct socket **aso, i |
|
* => On failure, return non-zero; *fdout will be undefined. |
* => On failure, return non-zero; *fdout will be undefined. |
*/ |
*/ |
int |
int |
fsocreate(int domain, struct socket **sop, int type, int protocol, int *fdout) |
fsocreate(int domain, struct socket **sop, int type, int proto, int *fdout) |
{ |
{ |
lwp_t *l = curlwp; |
lwp_t *l = curlwp; |
int error, fd, flags; |
int error, fd, flags; |
Line 582 fsocreate(int domain, struct socket **so |
|
Line 584 fsocreate(int domain, struct socket **so |
|
if ((error = fd_allocfile(&fp, &fd)) != 0) { |
if ((error = fd_allocfile(&fp, &fd)) != 0) { |
return error; |
return error; |
} |
} |
|
|
flags = type & SOCK_FLAGS_MASK; |
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)| |
Line 591 fsocreate(int domain, struct socket **so |
|
Line 592 fsocreate(int domain, struct socket **so |
|
fp->f_ops = &socketops; |
fp->f_ops = &socketops; |
|
|
type &= ~SOCK_FLAGS_MASK; |
type &= ~SOCK_FLAGS_MASK; |
error = socreate(domain, &so, type, protocol, l, NULL); |
error = socreate(domain, &so, type, proto, l, NULL); |
if (error != 0) { |
if (error) { |
fd_abort(curproc, fp, fd); |
fd_abort(curproc, fp, fd); |
return error; |
return error; |
} |
} |
if (flags & SOCK_NONBLOCK) { |
if (flags & SOCK_NONBLOCK) { |
so->so_state |= SS_NBIO; |
so->so_state |= SS_NBIO; |
} |
} |
|
|
fp->f_data = so; |
fp->f_data = so; |
fd_affix(curproc, fp, fd); |
fd_affix(curproc, fp, fd); |
|
|
if (sop != NULL) { |
if (sop != NULL) { |
*sop = so; |
*sop = so; |
} |
} |
Line 609 fsocreate(int domain, struct socket **so |
|
Line 610 fsocreate(int domain, struct socket **so |
|
return error; |
return error; |
} |
} |
|
|
void |
|
soref(struct socket *so) |
|
{ |
|
atomic_inc_uint(&so->so_refcnt); |
|
} |
|
|
|
void |
|
sounref(struct socket *so) |
|
{ |
|
if (atomic_dec_uint_nv(&so->so_refcnt) > 0) { |
|
return; |
|
} |
|
soput(so); |
|
} |
|
|
|
int |
int |
sofamily(const struct socket *so) |
sofamily(const struct socket *so) |
{ |
{ |
Line 640 sofamily(const struct socket *so) |
|
Line 626 sofamily(const struct socket *so) |
|
int |
int |
sobind(struct socket *so, struct mbuf *nam, struct lwp *l) |
sobind(struct socket *so, struct mbuf *nam, struct lwp *l) |
{ |
{ |
int error; |
int error; |
|
|
solock(so); |
solock(so); |
error = (*so->so_proto->pr_usrreqs->pr_generic)(so, |
error = (*so->so_proto->pr_usrreqs->pr_bind)(so, nam, l); |
PRU_BIND, NULL, nam, NULL, l); |
|
sounlock(so); |
sounlock(so); |
return error; |
return error; |
} |
} |
Line 652 sobind(struct socket *so, struct mbuf *n |
|
Line 637 sobind(struct socket *so, struct mbuf *n |
|
int |
int |
solisten(struct socket *so, int backlog, struct lwp *l) |
solisten(struct socket *so, int backlog, struct lwp *l) |
{ |
{ |
int error; |
int error; |
|
|
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_usrreqs->pr_generic)(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 678 solisten(struct socket *so, int backlog, |
|
Line 662 solisten(struct socket *so, int backlog, |
|
void |
void |
sofree(struct socket *so) |
sofree(struct socket *so) |
{ |
{ |
|
u_int refs; |
|
|
KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
|
|
Line 707 sofree(struct socket *so) |
|
Line 692 sofree(struct socket *so) |
|
KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv)); |
KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv)); |
KASSERT(!cv_has_waiters(&so->so_snd.sb_cv)); |
KASSERT(!cv_has_waiters(&so->so_snd.sb_cv)); |
sorflush(so); |
sorflush(so); |
|
refs = so->so_aborting; /* XXX */ |
/* Remove acccept filter if one is present. */ |
/* Remove acccept filter if one is present. */ |
if (so->so_accf != NULL) |
if (so->so_accf != NULL) |
(void)accept_filt_clear(so); |
(void)accept_filt_clear(so); |
sounlock(so); |
sounlock(so); |
|
if (refs == 0) /* XXX */ |
/* Will soput() if the last reference. */ |
soput(so); |
sounref(so); |
|
} |
} |
|
|
/* |
/* |
|
|
soclose(struct socket *so) |
soclose(struct socket *so) |
{ |
{ |
struct socket *so2; |
struct socket *so2; |
int error; |
int error = 0; |
|
|
error = 0; |
|
solock(so); |
solock(so); |
if (so->so_options & SO_ACCEPTCONN) { |
if (so->so_options & SO_ACCEPTCONN) { |
for (;;) { |
for (;;) { |
Line 770 soclose(struct socket *so) |
|
Line 754 soclose(struct socket *so) |
|
} |
} |
drop: |
drop: |
if (so->so_pcb) { |
if (so->so_pcb) { |
|
KASSERT(solocked(so)); |
(*so->so_proto->pr_usrreqs->pr_detach)(so); |
(*so->so_proto->pr_usrreqs->pr_detach)(so); |
} |
} |
discard: |
discard: |
Line 788 soabort(struct socket *so) |
|
Line 773 soabort(struct socket *so) |
|
{ |
{ |
u_int refs; |
u_int refs; |
int error; |
int error; |
|
|
KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
KASSERT(so->so_head == NULL); |
KASSERT(so->so_head == NULL); |
|
|
soref(so); |
so->so_aborting++; /* XXX */ |
error = (*so->so_proto->pr_usrreqs->pr_generic)(so, |
error = (*so->so_proto->pr_usrreqs->pr_abort)(so); |
PRU_ABORT, NULL, NULL, NULL, NULL); |
refs = --so->so_aborting; /* XXX */ |
refs = so->so_refcnt; |
if (error || (refs == 0)) { |
sounref(so); |
|
|
|
/* XXX: Fix PRU_ABORT to behave consistently. */ |
|
if (error || refs == 1) { |
|
sofree(so); |
sofree(so); |
} else { |
} else { |
sounlock(so); |
sounlock(so); |
} |
} |
sounref(so); |
|
return error; |
return error; |
} |
} |
|
|
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)); |
|
|
error = 0; |
|
KASSERT((so->so_state & SS_NOFDREF) != 0); |
KASSERT((so->so_state & SS_NOFDREF) != 0); |
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_usrreqs->pr_generic)(so, |
error = (*so->so_proto->pr_usrreqs->pr_accept)(so, nam); |
PRU_ACCEPT, NULL, nam, NULL, NULL); |
|
else |
else |
error = ECONNABORTED; |
error = ECONNABORTED; |
|
|
return (error); |
return error; |
} |
} |
|
|
int |
int |
Line 837 soconnect(struct socket *so, struct mbuf |
|
Line 814 soconnect(struct socket *so, struct mbuf |
|
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 849 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_usrreqs->pr_generic)(so, |
error = (*so->so_proto->pr_usrreqs->pr_connect)(so, nam, l); |
PRU_CONNECT, NULL, nam, NULL, l); |
|
|
|
return error; |
return error; |
} |
} |
Line 858 soconnect(struct socket *so, struct mbuf |
|
Line 834 soconnect(struct socket *so, struct mbuf |
|
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_usrreqs->pr_generic)(so1, PRU_CONNECT2, |
return (*so1->so_proto->pr_usrreqs->pr_generic)(so1, |
NULL, (struct mbuf *)so2, NULL, NULL); |
PRU_CONNECT2, NULL, (struct mbuf *)so2, NULL, NULL); |
return (error); |
|
} |
} |
|
|
int |
int |
Line 879 sodisconnect(struct socket *so) |
|
Line 852 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_usrreqs->pr_generic)(so, |
error = (*so->so_proto->pr_usrreqs->pr_disconnect)(so); |
PRU_DISCONNECT, NULL, NULL, NULL, NULL); |
|
} |
} |
return (error); |
return (error); |
} |
} |
Line 958 sosend(struct socket *so, struct mbuf *a |
|
Line 930 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 1077 sosend(struct socket *so, struct mbuf *a |
|
Line 1048 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_usrreqs->pr_generic)(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 1191 soreceive(struct socket *so, struct mbuf |
|
Line 1165 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_usrreqs->pr_generic)(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 1216 soreceive(struct socket *so, struct mbuf |
|
Line 1189 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_usrreqs->pr_generic)(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 1562 soreceive(struct socket *so, struct mbuf |
|
Line 1532 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_usrreqs->pr_generic)(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 1604 soreceive(struct socket *so, struct mbuf |
|
Line 1573 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_usrreqs->pr_generic)(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 1639 soshutdown(struct socket *so, int how) |
|
Line 1607 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_usrreqs->pr_generic)(so, PRU_SHUTDOWN, NULL, |
error = (*pr->pr_usrreqs->pr_shutdown)(so); |
NULL, NULL, NULL); |
|
|
|
return error; |
return error; |
} |
} |
Line 1699 sorflush(struct socket *so) |
|
Line 1666 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 1941 sogetopt1(struct socket *so, struct sock |
|
Line 1909 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 2458 sysctl_kern_socket_setup(void) |
|
Line 2427 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, |