version 1.235.2.1, 2015/04/06 15:18:20 |
version 1.258, 2018/01/01 00:45:12 |
|
|
#include <sys/cdefs.h> |
#include <sys/cdefs.h> |
__KERNEL_RCSID(0, "$NetBSD$"); |
__KERNEL_RCSID(0, "$NetBSD$"); |
|
|
|
#ifdef _KERNEL_OPT |
#include "opt_compat_netbsd.h" |
#include "opt_compat_netbsd.h" |
#include "opt_sock_counters.h" |
#include "opt_sock_counters.h" |
#include "opt_sosend_loan.h" |
#include "opt_sosend_loan.h" |
#include "opt_mbuftrace.h" |
#include "opt_mbuftrace.h" |
#include "opt_somaxkva.h" |
#include "opt_somaxkva.h" |
#include "opt_multiprocessor.h" /* XXX */ |
#include "opt_multiprocessor.h" /* XXX */ |
|
#include "opt_sctp.h" |
|
#endif |
|
|
#include <sys/param.h> |
#include <sys/param.h> |
#include <sys/systm.h> |
#include <sys/systm.h> |
Line 292 sopendfree_thread(void *v) |
|
Line 295 sopendfree_thread(void *v) |
|
|
|
for (; m != NULL; m = next) { |
for (; m != NULL; m = next) { |
next = m->m_next; |
next = m->m_next; |
KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0); |
KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == |
|
0); |
KASSERT(m->m_ext.ext_refcnt == 0); |
KASSERT(m->m_ext.ext_refcnt == 0); |
|
|
rv += m->m_ext.ext_size; |
rv += m->m_ext.ext_size; |
Line 431 socket_listener_cb(kauth_cred_t cred, ka |
|
Line 435 socket_listener_cb(kauth_cred_t cred, ka |
|
|
|
case KAUTH_REQ_NETWORK_SOCKET_OPEN: |
case KAUTH_REQ_NETWORK_SOCKET_OPEN: |
/* We allow "raw" routing/bluetooth sockets to anyone. */ |
/* We allow "raw" routing/bluetooth sockets to anyone. */ |
if ((u_long)arg1 == PF_ROUTE || (u_long)arg1 == PF_OROUTE |
switch ((u_long)arg1) { |
|| (u_long)arg1 == PF_BLUETOOTH) { |
case PF_ROUTE: |
|
case PF_OROUTE: |
|
case PF_BLUETOOTH: |
|
case PF_CAN: |
result = KAUTH_RESULT_ALLOW; |
result = KAUTH_RESULT_ALLOW; |
} else { |
break; |
|
default: |
/* Privileged, let secmodel handle this. */ |
/* Privileged, let secmodel handle this. */ |
if ((u_long)arg2 == SOCK_RAW) |
if ((u_long)arg2 == SOCK_RAW) |
break; |
break; |
|
result = KAUTH_RESULT_ALLOW; |
|
break; |
} |
} |
|
|
result = KAUTH_RESULT_ALLOW; |
|
|
|
break; |
break; |
|
|
case KAUTH_REQ_NETWORK_SOCKET_CANSEE: |
case KAUTH_REQ_NETWORK_SOCKET_CANSEE: |
|
|
solisten(struct socket *so, int backlog, struct lwp *l) |
solisten(struct socket *so, int backlog, struct lwp *l) |
{ |
{ |
int error; |
int error; |
|
short oldopt, oldqlimit; |
|
|
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_listen)(so, l); |
oldopt = so->so_options; |
if (error != 0) { |
oldqlimit = so->so_qlimit; |
sounlock(so); |
|
return error; |
|
} |
|
if (TAILQ_EMPTY(&so->so_q)) |
if (TAILQ_EMPTY(&so->so_q)) |
so->so_options |= SO_ACCEPTCONN; |
so->so_options |= SO_ACCEPTCONN; |
if (backlog < 0) |
if (backlog < 0) |
backlog = 0; |
backlog = 0; |
so->so_qlimit = min(backlog, somaxconn); |
so->so_qlimit = min(backlog, somaxconn); |
|
|
|
error = (*so->so_proto->pr_usrreqs->pr_listen)(so, l); |
|
if (error != 0) { |
|
so->so_options = oldopt; |
|
so->so_qlimit = oldqlimit; |
|
sounlock(so); |
|
return error; |
|
} |
sounlock(so); |
sounlock(so); |
return 0; |
return 0; |
} |
} |
Line 777 soabort(struct socket *so) |
|
Line 790 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); |
|
|
Line 793 soabort(struct socket *so) |
|
Line 806 soabort(struct socket *so) |
|
} |
} |
|
|
int |
int |
soaccept(struct socket *so, struct mbuf *nam) |
soaccept(struct socket *so, struct sockaddr *nam) |
{ |
{ |
int error; |
int error; |
|
|
Line 811 soaccept(struct socket *so, struct mbuf |
|
Line 824 soaccept(struct socket *so, struct mbuf |
|
} |
} |
|
|
int |
int |
soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) |
soconnect(struct socket *so, struct sockaddr *nam, struct lwp *l) |
{ |
{ |
int error; |
int error; |
|
|
Line 827 soconnect(struct socket *so, struct mbuf |
|
Line 840 soconnect(struct socket *so, struct mbuf |
|
*/ |
*/ |
if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && |
if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && |
((so->so_proto->pr_flags & PR_CONNREQUIRED) || |
((so->so_proto->pr_flags & PR_CONNREQUIRED) || |
(error = sodisconnect(so)))) |
(error = sodisconnect(so)))) { |
error = EISCONN; |
error = EISCONN; |
else |
} else { |
|
if (nam->sa_family != so->so_proto->pr_domain->dom_family) { |
|
return EAFNOSUPPORT; |
|
} |
error = (*so->so_proto->pr_usrreqs->pr_connect)(so, nam, l); |
error = (*so->so_proto->pr_usrreqs->pr_connect)(so, nam, l); |
|
} |
|
|
return error; |
return error; |
} |
} |
Line 879 sodisconnect(struct socket *so) |
|
Line 896 sodisconnect(struct socket *so) |
|
* Data and control buffers are freed on return. |
* Data and control buffers are freed on return. |
*/ |
*/ |
int |
int |
sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, |
sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, |
struct mbuf *control, int flags, struct lwp *l) |
struct mbuf *top, struct mbuf *control, int flags, struct lwp *l) |
{ |
{ |
struct mbuf **mp, *m; |
struct mbuf **mp, *m; |
long space, len, resid, clen, mlen; |
long space, len, resid, clen, mlen; |
Line 937 sosend(struct socket *so, struct mbuf *a |
|
Line 954 sosend(struct socket *so, struct mbuf *a |
|
error = ENOTCONN; |
error = ENOTCONN; |
goto release; |
goto release; |
} |
} |
} else if (addr == 0) { |
} else if (addr == NULL) { |
error = EDESTADDRREQ; |
error = EDESTADDRREQ; |
goto release; |
goto release; |
} |
} |
Line 985 sosend(struct socket *so, struct mbuf *a |
|
Line 1002 sosend(struct socket *so, struct mbuf *a |
|
m = m_gethdr(M_WAIT, MT_DATA); |
m = m_gethdr(M_WAIT, MT_DATA); |
mlen = MHLEN; |
mlen = MHLEN; |
m->m_pkthdr.len = 0; |
m->m_pkthdr.len = 0; |
m->m_pkthdr.rcvif = NULL; |
m_reset_rcvif(m); |
} else { |
} else { |
m = m_get(M_WAIT, MT_DATA); |
m = m_get(M_WAIT, MT_DATA); |
mlen = MLEN; |
mlen = MLEN; |
Line 1051 sosend(struct socket *so, struct mbuf *a |
|
Line 1068 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; |
if (flags & MSG_OOB) |
if (flags & MSG_OOB) { |
error = (*so->so_proto->pr_usrreqs->pr_sendoob)(so, |
error = (*so->so_proto->pr_usrreqs->pr_sendoob)( |
top, control); |
so, top, control); |
else |
} else { |
error = (*so->so_proto->pr_usrreqs->pr_send)(so, |
error = (*so->so_proto->pr_usrreqs->pr_send)(so, |
top, addr, control, l); |
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 1310 soreceive(struct socket *so, struct mbuf |
|
Line 1328 soreceive(struct socket *so, struct mbuf |
|
m->m_next = NULL; |
m->m_next = NULL; |
m = so->so_rcv.sb_mb; |
m = so->so_rcv.sb_mb; |
} else { |
} else { |
MFREE(m, so->so_rcv.sb_mb); |
m = so->so_rcv.sb_mb = m_free(m); |
m = so->so_rcv.sb_mb; |
|
} |
} |
sbsync(&so->so_rcv, nextrecord); |
sbsync(&so->so_rcv, nextrecord); |
} |
} |
} |
} |
|
if (pr->pr_flags & PR_ADDR_OPT) { |
|
/* |
|
* For SCTP we may be getting a |
|
* whole message OR a partial delivery. |
|
*/ |
|
if (m->m_type == MT_SONAME) { |
|
orig_resid = 0; |
|
if (flags & MSG_PEEK) { |
|
if (paddr) |
|
*paddr = m_copy(m, 0, m->m_len); |
|
m = m->m_next; |
|
} else { |
|
sbfree(&so->so_rcv, m); |
|
if (paddr) { |
|
*paddr = m; |
|
so->so_rcv.sb_mb = m->m_next; |
|
m->m_next = 0; |
|
m = so->so_rcv.sb_mb; |
|
} else { |
|
m = so->so_rcv.sb_mb = m_free(m); |
|
} |
|
} |
|
} |
|
} |
|
|
/* |
/* |
* Process one or more MT_CONTROL mbufs present before any data mbufs |
* Process one or more MT_CONTROL mbufs present before any data mbufs |
Line 1370 soreceive(struct socket *so, struct mbuf |
|
Line 1411 soreceive(struct socket *so, struct mbuf |
|
*/ |
*/ |
if (dom->dom_dispose != NULL && |
if (dom->dom_dispose != NULL && |
type == SCM_RIGHTS) { |
type == SCM_RIGHTS) { |
sounlock(so); |
sounlock(so); |
(*dom->dom_dispose)(cm); |
(*dom->dom_dispose)(cm); |
solock(so); |
solock(so); |
} |
} |
Line 1450 soreceive(struct socket *so, struct mbuf |
|
Line 1491 soreceive(struct socket *so, struct mbuf |
|
if (len == m->m_len - moff) { |
if (len == m->m_len - moff) { |
if (m->m_flags & M_EOR) |
if (m->m_flags & M_EOR) |
flags |= MSG_EOR; |
flags |= MSG_EOR; |
|
#ifdef SCTP |
|
if (m->m_flags & M_NOTIFICATION) |
|
flags |= MSG_NOTIFICATION; |
|
#endif /* SCTP */ |
if (flags & MSG_PEEK) { |
if (flags & MSG_PEEK) { |
m = m->m_next; |
m = m->m_next; |
moff = 0; |
moff = 0; |
Line 1462 soreceive(struct socket *so, struct mbuf |
|
Line 1507 soreceive(struct socket *so, struct mbuf |
|
so->so_rcv.sb_mb = m = m->m_next; |
so->so_rcv.sb_mb = m = m->m_next; |
*mp = NULL; |
*mp = NULL; |
} else { |
} else { |
MFREE(m, so->so_rcv.sb_mb); |
m = so->so_rcv.sb_mb = m_free(m); |
m = so->so_rcv.sb_mb; |
|
} |
} |
/* |
/* |
* If m != NULL, we also know that |
* If m != NULL, we also know that |
Line 1681 sosetopt1(struct socket *so, const struc |
|
Line 1725 sosetopt1(struct socket *so, const struc |
|
KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
break; |
break; |
|
|
case SO_LINGER: |
case SO_LINGER: |
error = sockopt_get(sopt, &l, sizeof(l)); |
error = sockopt_get(sopt, &l, sizeof(l)); |
solock(so); |
solock(so); |
if (error) |
if (error) |
break; |
break; |
if (l.l_linger < 0 || l.l_linger > USHRT_MAX || |
if (l.l_linger < 0 || l.l_linger > USHRT_MAX || |
l.l_linger > (INT_MAX / hz)) { |
l.l_linger > (INT_MAX / hz)) { |
error = EDOM; |
error = EDOM; |
break; |
break; |
} |
} |
so->so_linger = l.l_linger; |
so->so_linger = l.l_linger; |
if (l.l_onoff) |
if (l.l_onoff) |
so->so_options |= SO_LINGER; |
so->so_options |= SO_LINGER; |
else |
else |
so->so_options &= ~SO_LINGER; |
so->so_options &= ~SO_LINGER; |
break; |
break; |
|
|
case SO_DEBUG: |
case SO_DEBUG: |
case SO_KEEPALIVE: |
case SO_KEEPALIVE: |
Line 1875 so_setsockopt(struct lwp *l, struct sock |
|
Line 1919 so_setsockopt(struct lwp *l, struct sock |
|
|
|
return error; |
return error; |
} |
} |
|
|
/* |
/* |
* internal get SOL_SOCKET options |
* internal get SOL_SOCKET options |
*/ |
*/ |
Line 2052 sockopt_destroy(struct sockopt *sopt) |
|
Line 2096 sockopt_destroy(struct sockopt *sopt) |
|
/* |
/* |
* set sockopt value |
* set sockopt value |
* - value is copied into sockopt |
* - value is copied into sockopt |
* - memory is allocated when necessary, will not sleep |
* - memory is allocated when necessary, will not sleep |
*/ |
*/ |
int |
int |
sockopt_set(struct sockopt *sopt, const void *buf, size_t len) |
sockopt_set(struct sockopt *sopt, const void *buf, size_t len) |
Line 2065 sockopt_set(struct sockopt *sopt, const |
|
Line 2109 sockopt_set(struct sockopt *sopt, const |
|
return error; |
return error; |
} |
} |
|
|
KASSERT(sopt->sopt_size == len); |
if (sopt->sopt_size < len) |
|
return EINVAL; |
|
|
memcpy(sopt->sopt_data, buf, len); |
memcpy(sopt->sopt_data, buf, len); |
return 0; |
return 0; |
} |
} |
Line 2125 sockopt_setmbuf(struct sockopt *sopt, st |
|
Line 2171 sockopt_setmbuf(struct sockopt *sopt, st |
|
return error; |
return error; |
} |
} |
|
|
KASSERT(sopt->sopt_size == len); |
if (sopt->sopt_size < len) |
|
return EINVAL; |
|
|
m_copydata(m, 0, len, sopt->sopt_data); |
m_copydata(m, 0, len, sopt->sopt_data); |
m_freem(m); |
m_freem(m); |
|
|
Line 2204 filt_soread(struct knote *kn, long hint) |
|
Line 2252 filt_soread(struct knote *kn, long hint) |
|
rv = 1; |
rv = 1; |
else if (kn->kn_sfflags & NOTE_LOWAT) |
else if (kn->kn_sfflags & NOTE_LOWAT) |
rv = (kn->kn_data >= kn->kn_sdata); |
rv = (kn->kn_data >= kn->kn_sdata); |
else |
else |
rv = (kn->kn_data >= so->so_rcv.sb_lowat); |
rv = (kn->kn_data >= so->so_rcv.sb_lowat); |
if (hint != NOTE_SUBMIT) |
if (hint != NOTE_SUBMIT) |
sounlock(so); |
sounlock(so); |
Line 2275 filt_solisten(struct knote *kn, long hin |
|
Line 2323 filt_solisten(struct knote *kn, long hin |
|
return rv; |
return rv; |
} |
} |
|
|
static const struct filterops solisten_filtops = |
static const struct filterops solisten_filtops = { |
{ 1, NULL, filt_sordetach, filt_solisten }; |
.f_isfd = 1, |
static const struct filterops soread_filtops = |
.f_attach = NULL, |
{ 1, NULL, filt_sordetach, filt_soread }; |
.f_detach = filt_sordetach, |
static const struct filterops sowrite_filtops = |
.f_event = filt_solisten, |
{ 1, NULL, filt_sowdetach, filt_sowrite }; |
}; |
|
|
|
static const struct filterops soread_filtops = { |
|
.f_isfd = 1, |
|
.f_attach = NULL, |
|
.f_detach = filt_sordetach, |
|
.f_event = filt_soread, |
|
}; |
|
|
|
static const struct filterops sowrite_filtops = { |
|
.f_isfd = 1, |
|
.f_attach = NULL, |
|
.f_detach = filt_sowdetach, |
|
.f_event = filt_sowrite, |
|
}; |
|
|
int |
int |
soo_kqfilter(struct file *fp, struct knote *kn) |
soo_kqfilter(struct file *fp, struct knote *kn) |
Line 2366 sopoll(struct socket *so, int events) |
|
Line 2428 sopoll(struct socket *so, int events) |
|
return revents; |
return revents; |
} |
} |
|
|
|
struct mbuf ** |
|
sbsavetimestamp(int opt, struct mbuf *m, struct mbuf **mp) |
|
{ |
|
struct timeval tv; |
|
microtime(&tv); |
|
|
|
#ifdef SO_OTIMESTAMP |
|
if (opt & SO_OTIMESTAMP) { |
|
struct timeval50 tv50; |
|
|
|
timeval_to_timeval50(&tv, &tv50); |
|
*mp = sbcreatecontrol(&tv50, sizeof(tv50), |
|
SCM_OTIMESTAMP, SOL_SOCKET); |
|
if (*mp) |
|
mp = &(*mp)->m_next; |
|
} else |
|
#endif |
|
|
|
if (opt & SO_TIMESTAMP) { |
|
*mp = sbcreatecontrol(&tv, sizeof(tv), |
|
SCM_TIMESTAMP, SOL_SOCKET); |
|
if (*mp) |
|
mp = &(*mp)->m_next; |
|
} |
|
return mp; |
|
} |
|
|
|
|
#include <sys/sysctl.h> |
#include <sys/sysctl.h> |
|
|