| version 1.134.2.5, 2007/09/01 12:56:49 |
version 1.134.2.6, 2007/10/09 13:44:32 |
| Line 79 __KERNEL_RCSID(0, "$NetBSD$"); |
|
| Line 79 __KERNEL_RCSID(0, "$NetBSD$"); |
|
| #include <sys/systm.h> |
#include <sys/systm.h> |
| #include <sys/proc.h> |
#include <sys/proc.h> |
| #include <sys/file.h> |
#include <sys/file.h> |
| |
#include <sys/filedesc.h> |
| #include <sys/malloc.h> |
#include <sys/malloc.h> |
| #include <sys/mbuf.h> |
#include <sys/mbuf.h> |
| #include <sys/domain.h> |
#include <sys/domain.h> |
| Line 103 POOL_INIT(socket_pool, sizeof(struct soc |
|
| Line 104 POOL_INIT(socket_pool, sizeof(struct soc |
|
| MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); |
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 int somaxconn; /* patchable (XXX sysctl) */ |
extern int somaxconn; /* patchable (XXX sysctl) */ |
| int somaxconn = SOMAXCONN; |
int somaxconn = SOMAXCONN; |
| |
|
| Line 424 sokva_reclaim_callback(struct callback_e |
|
| Line 427 sokva_reclaim_callback(struct callback_e |
|
| return CALLBACK_CHAIN_CONTINUE; |
return CALLBACK_CHAIN_CONTINUE; |
| } |
} |
| |
|
| |
struct mbuf * |
| |
getsombuf(struct socket *so) |
| |
{ |
| |
struct mbuf *m; |
| |
|
| |
m = m_get(M_WAIT, MT_SONAME); |
| |
MCLAIM(m, so->so_mowner); |
| |
return m; |
| |
} |
| |
|
| |
struct mbuf * |
| |
m_intopt(struct socket *so, int val) |
| |
{ |
| |
struct mbuf *m; |
| |
|
| |
m = getsombuf(so); |
| |
m->m_len = sizeof(int); |
| |
*mtod(m, int *) = val; |
| |
return m; |
| |
} |
| |
|
| void |
void |
| soinit(void) |
soinit(void) |
| { |
{ |
| Line 492 socreate(int dom, struct socket **aso, i |
|
| Line 516 socreate(int dom, struct socket **aso, i |
|
| so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner; |
so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner; |
| so->so_mowner = &prp->pr_domain->dom_mowner; |
so->so_mowner = &prp->pr_domain->dom_mowner; |
| #endif |
#endif |
| |
selinit(&so->so_rcv.sb_sel); |
| |
selinit(&so->so_snd.sb_sel); |
| 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); |
| error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, |
error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, |
| Line 507 socreate(int dom, struct socket **aso, i |
|
| Line 533 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. |
| |
*/ |
| |
int |
| |
fsocreate(int domain, struct socket **sop, int type, int protocol, |
| |
struct lwp *l, int *fdout) |
| |
{ |
| |
struct filedesc *fdp; |
| |
struct socket *so; |
| |
struct file *fp; |
| |
int fd, error; |
| |
|
| |
fdp = l->l_proc->p_fd; |
| |
/* falloc() will use the desciptor for us */ |
| |
if ((error = falloc(l, &fp, &fd)) != 0) |
| |
return (error); |
| |
fp->f_flag = FREAD|FWRITE; |
| |
fp->f_type = DTYPE_SOCKET; |
| |
fp->f_ops = &socketops; |
| |
error = socreate(domain, &so, type, protocol, l); |
| |
if (error != 0) { |
| |
FILE_UNUSE(fp, l); |
| |
fdremove(fdp, fd); |
| |
ffree(fp); |
| |
} else { |
| |
if (sop != NULL) |
| |
*sop = so; |
| |
fp->f_data = so; |
| |
FILE_SET_MATURE(fp); |
| |
FILE_UNUSE(fp, l); |
| |
*fdout = fd; |
| |
} |
| |
return error; |
| |
} |
| |
|
| int |
int |
| sobind(struct socket *so, struct mbuf *nam, struct lwp *l) |
sobind(struct socket *so, struct mbuf *nam, struct lwp *l) |
| { |
{ |
| Line 562 sofree(struct socket *so) |
|
| Line 623 sofree(struct socket *so) |
|
| RLIM_INFINITY); |
RLIM_INFINITY); |
| sbrelease(&so->so_snd, so); |
sbrelease(&so->so_snd, so); |
| sorflush(so); |
sorflush(so); |
| |
seldestroy(&so->so_rcv.sb_sel); |
| |
seldestroy(&so->so_snd.sb_sel); |
| pool_put(&socket_pool, so); |
pool_put(&socket_pool, so); |
| } |
} |
| |
|
| Line 828 sosend(struct socket *so, struct mbuf *a |
|
| Line 891 sosend(struct socket *so, struct mbuf *a |
|
| if (flags & MSG_EOR) |
if (flags & MSG_EOR) |
| top->m_flags |= M_EOR; |
top->m_flags |= M_EOR; |
| } else do { |
} else do { |
| if (top == 0) { |
if (top == NULL) { |
| 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; |
| Line 872 sosend(struct socket *so, struct mbuf *a |
|
| Line 935 sosend(struct socket *so, struct mbuf *a |
|
| if (atomic && top == 0 && len < mlen) |
if (atomic && top == 0 && len < mlen) |
| MH_ALIGN(m, len); |
MH_ALIGN(m, len); |
| } |
} |
| error = uiomove(mtod(m, void *), (int)len, |
error = uiomove(mtod(m, void *), (int)len, uio); |
| uio); |
|
| have_data: |
have_data: |
| resid = uio->uio_resid; |
resid = uio->uio_resid; |
| m->m_len = len; |
m->m_len = len; |
| *mp = m; |
*mp = m; |
| top->m_pkthdr.len += len; |
top->m_pkthdr.len += len; |
| if (error) |
if (error != 0) |
| goto release; |
goto release; |
| mp = &m->m_next; |
mp = &m->m_next; |
| if (resid <= 0) { |
if (resid <= 0) { |
| Line 908 sosend(struct socket *so, struct mbuf *a |
|
| Line 970 sosend(struct socket *so, struct mbuf *a |
|
| splx(s); |
splx(s); |
| |
|
| clen = 0; |
clen = 0; |
| control = 0; |
control = NULL; |
| top = 0; |
top = NULL; |
| mp = ⊤ |
mp = ⊤ |
| if (error) |
if (error != 0) |
| goto release; |
goto release; |
| } while (resid && space > 0); |
} while (resid && space > 0); |
| } while (resid); |
} while (resid); |
| Line 958 soreceive(struct socket *so, struct mbuf |
|
| Line 1020 soreceive(struct socket *so, struct mbuf |
|
| type = 0; |
type = 0; |
| orig_resid = uio->uio_resid; |
orig_resid = uio->uio_resid; |
| |
|
| if (paddr) |
if (paddr != NULL) |
| *paddr = 0; |
*paddr = NULL; |
| if (controlp) |
if (controlp != NULL) |
| *controlp = 0; |
*controlp = NULL; |
| if (flagsp) |
if (flagsp != NULL) |
| flags = *flagsp &~ MSG_EOR; |
flags = *flagsp &~ MSG_EOR; |
| else |
else |
| flags = 0; |
flags = 0; |
| Line 980 soreceive(struct socket *so, struct mbuf |
|
| Line 1042 soreceive(struct socket *so, struct mbuf |
|
| error = uiomove(mtod(m, void *), |
error = uiomove(mtod(m, void *), |
| (int) min(uio->uio_resid, m->m_len), uio); |
(int) min(uio->uio_resid, m->m_len), uio); |
| m = m_free(m); |
m = m_free(m); |
| } while (uio->uio_resid && error == 0 && m); |
} while (uio->uio_resid > 0 && error == 0 && m); |
| bad: |
bad: |
| if (m) |
if (m != NULL) |
| m_freem(m); |
m_freem(m); |
| return (error); |
return error; |
| } |
} |
| if (mp) |
if (mp != NULL) |
| *mp = NULL; |
*mp = NULL; |
| if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) |
if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) |
| (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l); |
(*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) |
| return (error); |
return error; |
| s = splsoftnet(); |
s = splsoftnet(); |
| |
|
| m = so->so_rcv.sb_mb; |
m = so->so_rcv.sb_mb; |
| Line 1008 soreceive(struct socket *so, struct mbuf |
|
| Line 1070 soreceive(struct socket *so, struct mbuf |
|
| * we have to do the receive in sections, and thus risk returning |
* we have to do the receive in sections, and thus risk returning |
| * a short count if a timeout or signal occurs after we start. |
* a short count if a timeout or signal occurs after we start. |
| */ |
*/ |
| if (m == 0 || (((flags & MSG_DONTWAIT) == 0 && |
if (m == NULL || |
| so->so_rcv.sb_cc < uio->uio_resid) && |
((flags & MSG_DONTWAIT) == 0 && |
| (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || |
so->so_rcv.sb_cc < uio->uio_resid && |
| ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && |
(so->so_rcv.sb_cc < so->so_rcv.sb_lowat || |
| m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { |
((flags & MSG_WAITALL) && |
| |
uio->uio_resid <= so->so_rcv.sb_hiwat)) && |
| |
m->m_nextpkt == NULL && |
| |
(pr->pr_flags & PR_ATOMIC) == 0)) { |
| #ifdef DIAGNOSTIC |
#ifdef DIAGNOSTIC |
| if (m == 0 && so->so_rcv.sb_cc) |
if (m == NULL && so->so_rcv.sb_cc) |
| panic("receive 1"); |
panic("receive 1"); |
| #endif |
#endif |
| if (so->so_error) { |
if (so->so_error) { |
| if (m) |
if (m != NULL) |
| goto dontblock; |
goto dontblock; |
| error = so->so_error; |
error = so->so_error; |
| if ((flags & MSG_PEEK) == 0) |
if ((flags & MSG_PEEK) == 0) |
| Line 1026 soreceive(struct socket *so, struct mbuf |
|
| Line 1091 soreceive(struct socket *so, struct mbuf |
|
| goto release; |
goto release; |
| } |
} |
| if (so->so_state & SS_CANTRCVMORE) { |
if (so->so_state & SS_CANTRCVMORE) { |
| if (m) |
if (m != NULL) |
| goto dontblock; |
goto dontblock; |
| else |
else |
| goto release; |
goto release; |
| } |
} |
| for (; m; m = m->m_next) |
for (; m != NULL; m = m->m_next) |
| if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { |
if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { |
| m = so->so_rcv.sb_mb; |
m = so->so_rcv.sb_mb; |
| goto dontblock; |
goto dontblock; |
| Line 1052 soreceive(struct socket *so, struct mbuf |
|
| Line 1117 soreceive(struct socket *so, struct mbuf |
|
| sbunlock(&so->so_rcv); |
sbunlock(&so->so_rcv); |
| error = sbwait(&so->so_rcv); |
error = sbwait(&so->so_rcv); |
| splx(s); |
splx(s); |
| if (error) |
if (error != 0) |
| return (error); |
return error; |
| goto restart; |
goto restart; |
| } |
} |
| dontblock: |
dontblock: |
| Line 1062 soreceive(struct socket *so, struct mbuf |
|
| Line 1127 soreceive(struct socket *so, struct mbuf |
|
| * While we process the initial mbufs containing address and control |
* While we process the initial mbufs containing address and control |
| * info, we save a copy of m->m_nextpkt into nextrecord. |
* info, we save a copy of m->m_nextpkt into nextrecord. |
| */ |
*/ |
| if (l) |
if (l != NULL) |
| l->l_proc->p_stats->p_ru.ru_msgrcv++; |
l->l_proc->p_stats->p_ru.ru_msgrcv++; |
| KASSERT(m == so->so_rcv.sb_mb); |
KASSERT(m == so->so_rcv.sb_mb); |
| SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); |
| Line 1081 soreceive(struct socket *so, struct mbuf |
|
| Line 1146 soreceive(struct socket *so, struct mbuf |
|
| } else { |
} else { |
| sbfree(&so->so_rcv, m); |
sbfree(&so->so_rcv, m); |
| mbuf_removed = 1; |
mbuf_removed = 1; |
| if (paddr) { |
if (paddr != NULL) { |
| *paddr = m; |
*paddr = m; |
| so->so_rcv.sb_mb = m->m_next; |
so->so_rcv.sb_mb = m->m_next; |
| m->m_next = 0; |
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); |
MFREE(m, so->so_rcv.sb_mb); |
| Line 1092 soreceive(struct socket *so, struct mbuf |
|
| Line 1157 soreceive(struct socket *so, struct mbuf |
|
| } |
} |
| } |
} |
| } |
} |
| while (m && m->m_type == MT_CONTROL && error == 0) { |
while (m != NULL && m->m_type == MT_CONTROL && error == 0) { |
| if (flags & MSG_PEEK) { |
if (flags & MSG_PEEK) { |
| if (controlp) |
if (controlp != NULL) |
| *controlp = m_copy(m, 0, m->m_len); |
*controlp = m_copy(m, 0, m->m_len); |
| m = m->m_next; |
m = m->m_next; |
| } else { |
} else { |
| sbfree(&so->so_rcv, m); |
sbfree(&so->so_rcv, m); |
| mbuf_removed = 1; |
mbuf_removed = 1; |
| if (controlp) { |
if (controlp != NULL) { |
| struct domain *dom = pr->pr_domain; |
struct domain *dom = pr->pr_domain; |
| if (dom->dom_externalize && l && |
if (dom->dom_externalize && l && |
| mtod(m, struct cmsghdr *)->cmsg_type == |
mtod(m, struct cmsghdr *)->cmsg_type == |
| Line 1108 soreceive(struct socket *so, struct mbuf |
|
| Line 1173 soreceive(struct socket *so, struct mbuf |
|
| error = (*dom->dom_externalize)(m, l); |
error = (*dom->dom_externalize)(m, l); |
| *controlp = m; |
*controlp = m; |
| so->so_rcv.sb_mb = m->m_next; |
so->so_rcv.sb_mb = m->m_next; |
| m->m_next = 0; |
m->m_next = NULL; |
| m = so->so_rcv.sb_mb; |
m = so->so_rcv.sb_mb; |
| } else { |
} else { |
| /* |
/* |
| Line 1122 soreceive(struct socket *so, struct mbuf |
|
| Line 1187 soreceive(struct socket *so, struct mbuf |
|
| m = so->so_rcv.sb_mb; |
m = so->so_rcv.sb_mb; |
| } |
} |
| } |
} |
| if (controlp) { |
if (controlp != NULL) { |
| orig_resid = 0; |
orig_resid = 0; |
| controlp = &(*controlp)->m_next; |
controlp = &(*controlp)->m_next; |
| } |
} |
| Line 1134 soreceive(struct socket *so, struct mbuf |
|
| Line 1199 soreceive(struct socket *so, struct mbuf |
|
| * the last packet on the chain (nextrecord == NULL) and we |
* the last packet on the chain (nextrecord == NULL) and we |
| * change m->m_nextpkt. |
* change m->m_nextpkt. |
| */ |
*/ |
| if (m) { |
if (m != NULL) { |
| if ((flags & MSG_PEEK) == 0) { |
if ((flags & MSG_PEEK) == 0) { |
| m->m_nextpkt = nextrecord; |
m->m_nextpkt = nextrecord; |
| /* |
/* |
| Line 1162 soreceive(struct socket *so, struct mbuf |
|
| Line 1227 soreceive(struct socket *so, struct mbuf |
|
| |
|
| moff = 0; |
moff = 0; |
| offset = 0; |
offset = 0; |
| while (m && uio->uio_resid > 0 && error == 0) { |
while (m != NULL && uio->uio_resid > 0 && error == 0) { |
| if (m->m_type == MT_OOBDATA) { |
if (m->m_type == MT_OOBDATA) { |
| if (type != MT_OOBDATA) |
if (type != MT_OOBDATA) |
| break; |
break; |
| Line 1186 soreceive(struct socket *so, struct mbuf |
|
| Line 1251 soreceive(struct socket *so, struct mbuf |
|
| * we must note any additions to the sockbuf when we |
* we must note any additions to the sockbuf when we |
| * block interrupts again. |
* block interrupts again. |
| */ |
*/ |
| if (mp == 0) { |
if (mp == NULL) { |
| SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); |
| SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); |
| splx(s); |
splx(s); |
| error = uiomove(mtod(m, char *) + moff, (int)len, uio); |
error = uiomove(mtod(m, char *) + moff, (int)len, uio); |
| s = splsoftnet(); |
s = splsoftnet(); |
| if (error) { |
if (error != 0) { |
| /* |
/* |
| * If any part of the record has been removed |
* If any part of the record has been removed |
| * (such as the MT_SONAME mbuf, which will |
* (such as the MT_SONAME mbuf, which will |
| Line 1246 soreceive(struct socket *so, struct mbuf |
|
| Line 1311 soreceive(struct socket *so, struct mbuf |
|
| SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); |
| SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); |
| } |
} |
| } else { |
} else if (flags & MSG_PEEK) |
| if (flags & MSG_PEEK) |
moff += len; |
| moff += len; |
else { |
| else { |
if (mp != NULL) |
| if (mp) |
*mp = m_copym(m, 0, len, M_WAIT); |
| *mp = m_copym(m, 0, len, M_WAIT); |
m->m_data += len; |
| m->m_data += len; |
m->m_len -= len; |
| m->m_len -= len; |
so->so_rcv.sb_cc -= len; |
| so->so_rcv.sb_cc -= len; |
|
| } |
|
| } |
} |
| if (so->so_oobmark) { |
if (so->so_oobmark) { |
| if ((flags & MSG_PEEK) == 0) { |
if ((flags & MSG_PEEK) == 0) { |
| Line 1279 soreceive(struct socket *so, struct mbuf |
|
| Line 1342 soreceive(struct socket *so, struct mbuf |
|
| * with a short count but without error. |
* with a short count but without error. |
| * Keep sockbuf locked against other readers. |
* Keep sockbuf locked against other readers. |
| */ |
*/ |
| while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && |
while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && |
| !sosendallatonce(so) && !nextrecord) { |
!sosendallatonce(so) && !nextrecord) { |
| if (so->so_error || so->so_state & SS_CANTRCVMORE) |
if (so->so_error || so->so_state & SS_CANTRCVMORE) |
| break; |
break; |
| Line 1300 soreceive(struct socket *so, struct mbuf |
|
| Line 1363 soreceive(struct socket *so, struct mbuf |
|
| 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"); |
| error = sbwait(&so->so_rcv); |
error = sbwait(&so->so_rcv); |
| if (error) { |
if (error != 0) { |
| sbunlock(&so->so_rcv); |
sbunlock(&so->so_rcv); |
| splx(s); |
splx(s); |
| return (0); |
return 0; |
| } |
} |
| if ((m = so->so_rcv.sb_mb) != NULL) |
if ((m = so->so_rcv.sb_mb) != NULL) |
| nextrecord = m->m_nextpkt; |
nextrecord = m->m_nextpkt; |
| Line 1316 soreceive(struct socket *so, struct mbuf |
|
| Line 1379 soreceive(struct socket *so, struct mbuf |
|
| (void) sbdroprecord(&so->so_rcv); |
(void) sbdroprecord(&so->so_rcv); |
| } |
} |
| if ((flags & MSG_PEEK) == 0) { |
if ((flags & MSG_PEEK) == 0) { |
| if (m == 0) { |
if (m == NULL) { |
| /* |
/* |
| * First part is an inline SB_EMPTY_FIXUP(). Second |
* First part is an inline SB_EMPTY_FIXUP(). Second |
| * part makes sure sb_lastrecord is up-to-date if |
* part makes sure sb_lastrecord is up-to-date if |
| Line 1342 soreceive(struct socket *so, struct mbuf |
|
| Line 1405 soreceive(struct socket *so, struct mbuf |
|
| goto restart; |
goto restart; |
| } |
} |
| |
|
| if (flagsp) |
if (flagsp != NULL) |
| *flagsp |= flags; |
*flagsp |= flags; |
| release: |
release: |
| sbunlock(&so->so_rcv); |
sbunlock(&so->so_rcv); |
| splx(s); |
splx(s); |
| return (error); |
return error; |
| } |
} |
| |
|
| int |
int |
| Line 1364 soshutdown(struct socket *so, int how) |
|
| Line 1427 soshutdown(struct socket *so, int how) |
|
| if (how == SHUT_WR || how == SHUT_RDWR) |
if (how == SHUT_WR || how == SHUT_RDWR) |
| return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, |
return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, |
| NULL, NULL, NULL); |
NULL, NULL, NULL); |
| return (0); |
return 0; |
| } |
} |
| |
|
| void |
void |
| Line 1394 sorflush(struct socket *so) |
|
| Line 1457 sorflush(struct socket *so) |
|
| sbrelease(&asb, so); |
sbrelease(&asb, so); |
| } |
} |
| |
|
| int |
static int |
| sosetopt(struct socket *so, int level, int optname, struct mbuf *m0) |
sosetopt1(struct socket *so, int level, int optname, struct mbuf *m) |
| { |
{ |
| int error; |
int optval, val; |
| struct mbuf *m; |
|
| struct linger *l; |
struct linger *l; |
| struct sockbuf *sb; |
struct sockbuf *sb; |
| |
struct timeval *tv; |
| |
|
| error = 0; |
switch (optname) { |
| m = m0; |
|
| if (level != SOL_SOCKET) { |
|
| if (so->so_proto && so->so_proto->pr_ctloutput) |
|
| return ((*so->so_proto->pr_ctloutput) |
|
| (PRCO_SETOPT, so, level, optname, &m0)); |
|
| error = ENOPROTOOPT; |
|
| } else { |
|
| switch (optname) { |
|
| |
|
| case SO_LINGER: |
case SO_LINGER: |
| if (m == NULL || m->m_len != sizeof(struct linger)) { |
if (m == NULL || m->m_len != sizeof(struct linger)) |
| error = EINVAL; |
return EINVAL; |
| goto bad; |
l = mtod(m, struct linger *); |
| } |
if (l->l_linger < 0 || l->l_linger > USHRT_MAX || |
| l = mtod(m, struct linger *); |
l->l_linger > (INT_MAX / hz)) |
| if (l->l_linger < 0 || l->l_linger > USHRT_MAX || |
return EDOM; |
| l->l_linger > (INT_MAX / hz)) { |
so->so_linger = l->l_linger; |
| error = EDOM; |
if (l->l_onoff) |
| goto bad; |
so->so_options |= SO_LINGER; |
| } |
else |
| so->so_linger = l->l_linger; |
so->so_options &= ~SO_LINGER; |
| if (l->l_onoff) |
break; |
| so->so_options |= SO_LINGER; |
|
| else |
|
| so->so_options &= ~SO_LINGER; |
|
| break; |
|
| |
|
| case SO_DEBUG: |
case SO_DEBUG: |
| case SO_KEEPALIVE: |
case SO_KEEPALIVE: |
| case SO_DONTROUTE: |
case SO_DONTROUTE: |
| case SO_USELOOPBACK: |
case SO_USELOOPBACK: |
| case SO_BROADCAST: |
case SO_BROADCAST: |
| case SO_REUSEADDR: |
case SO_REUSEADDR: |
| case SO_REUSEPORT: |
case SO_REUSEPORT: |
| case SO_OOBINLINE: |
case SO_OOBINLINE: |
| case SO_TIMESTAMP: |
case SO_TIMESTAMP: |
| if (m == NULL || m->m_len < sizeof(int)) { |
if (m == NULL || m->m_len < sizeof(int)) |
| error = EINVAL; |
return EINVAL; |
| goto bad; |
if (*mtod(m, int *)) |
| } |
so->so_options |= optname; |
| if (*mtod(m, int *)) |
else |
| so->so_options |= optname; |
so->so_options &= ~optname; |
| else |
break; |
| so->so_options &= ~optname; |
|
| break; |
case SO_SNDBUF: |
| |
case SO_RCVBUF: |
| |
case SO_SNDLOWAT: |
| |
case SO_RCVLOWAT: |
| |
if (m == NULL || m->m_len < sizeof(int)) |
| |
return EINVAL; |
| |
|
| |
/* |
| |
* Values < 1 make no sense for any of these |
| |
* options, so disallow them. |
| |
*/ |
| |
optval = *mtod(m, int *); |
| |
if (optval < 1) |
| |
return EINVAL; |
| |
|
| |
switch (optname) { |
| |
|
| case SO_SNDBUF: |
case SO_SNDBUF: |
| case SO_RCVBUF: |
case SO_RCVBUF: |
| |
sb = (optname == SO_SNDBUF) ? |
| |
&so->so_snd : &so->so_rcv; |
| |
if (sbreserve(sb, (u_long)optval, so) == 0) |
| |
return ENOBUFS; |
| |
sb->sb_flags &= ~SB_AUTOSIZE; |
| |
break; |
| |
|
| |
/* |
| |
* Make sure the low-water is never greater than |
| |
* the high-water. |
| |
*/ |
| case SO_SNDLOWAT: |
case SO_SNDLOWAT: |
| |
so->so_snd.sb_lowat = |
| |
(optval > so->so_snd.sb_hiwat) ? |
| |
so->so_snd.sb_hiwat : optval; |
| |
break; |
| case SO_RCVLOWAT: |
case SO_RCVLOWAT: |
| { |
so->so_rcv.sb_lowat = |
| int optval; |
(optval > so->so_rcv.sb_hiwat) ? |
| |
so->so_rcv.sb_hiwat : optval; |
| if (m == NULL || m->m_len < sizeof(int)) { |
break; |
| error = EINVAL; |
} |
| goto bad; |
break; |
| } |
|
| |
|
| /* |
|
| * Values < 1 make no sense for any of these |
|
| * options, so disallow them. |
|
| */ |
|
| optval = *mtod(m, int *); |
|
| if (optval < 1) { |
|
| error = EINVAL; |
|
| goto bad; |
|
| } |
|
| |
|
| switch (optname) { |
case SO_SNDTIMEO: |
| |
case SO_RCVTIMEO: |
| |
if (m == NULL || m->m_len < sizeof(*tv)) |
| |
return EINVAL; |
| |
tv = mtod(m, struct timeval *); |
| |
if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) |
| |
return EDOM; |
| |
val = tv->tv_sec * hz + tv->tv_usec / tick; |
| |
if (val == 0 && tv->tv_usec != 0) |
| |
val = 1; |
| |
|
| case SO_SNDBUF: |
switch (optname) { |
| case SO_RCVBUF: |
|
| sb = (optname == SO_SNDBUF) ? |
|
| &so->so_snd : &so->so_rcv; |
|
| if (sbreserve(sb, (u_long)optval, so) == 0) { |
|
| error = ENOBUFS; |
|
| goto bad; |
|
| } |
|
| sb->sb_flags &= ~SB_AUTOSIZE; |
|
| break; |
|
| |
|
| /* |
|
| * Make sure the low-water is never greater than |
|
| * the high-water. |
|
| */ |
|
| case SO_SNDLOWAT: |
|
| so->so_snd.sb_lowat = |
|
| (optval > so->so_snd.sb_hiwat) ? |
|
| so->so_snd.sb_hiwat : optval; |
|
| break; |
|
| case SO_RCVLOWAT: |
|
| so->so_rcv.sb_lowat = |
|
| (optval > so->so_rcv.sb_hiwat) ? |
|
| so->so_rcv.sb_hiwat : optval; |
|
| break; |
|
| } |
|
| break; |
|
| } |
|
| |
|
| case SO_SNDTIMEO: |
case SO_SNDTIMEO: |
| |
so->so_snd.sb_timeo = val; |
| |
break; |
| case SO_RCVTIMEO: |
case SO_RCVTIMEO: |
| { |
so->so_rcv.sb_timeo = val; |
| struct timeval *tv; |
break; |
| int val; |
} |
| |
break; |
| |
|
| if (m == NULL || m->m_len < sizeof(*tv)) { |
default: |
| error = EINVAL; |
return ENOPROTOOPT; |
| goto bad; |
} |
| } |
return 0; |
| tv = mtod(m, struct timeval *); |
} |
| if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) { |
|
| error = EDOM; |
|
| goto bad; |
|
| } |
|
| val = tv->tv_sec * hz + tv->tv_usec / tick; |
|
| if (val == 0 && tv->tv_usec != 0) |
|
| val = 1; |
|
| |
|
| switch (optname) { |
int |
| |
sosetopt(struct socket *so, int level, int optname, struct mbuf *m) |
| |
{ |
| |
int error, prerr; |
| |
|
| case SO_SNDTIMEO: |
if (level == SOL_SOCKET) |
| so->so_snd.sb_timeo = val; |
error = sosetopt1(so, level, optname, m); |
| break; |
else |
| case SO_RCVTIMEO: |
error = ENOPROTOOPT; |
| so->so_rcv.sb_timeo = val; |
|
| break; |
|
| } |
|
| break; |
|
| } |
|
| |
|
| default: |
if ((error == 0 || error == ENOPROTOOPT) && |
| error = ENOPROTOOPT; |
so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { |
| break; |
/* give the protocol stack a shot */ |
| } |
prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, level, |
| if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { |
optname, &m); |
| (void) ((*so->so_proto->pr_ctloutput) |
if (prerr == 0) |
| (PRCO_SETOPT, so, level, optname, &m0)); |
error = 0; |
| m = NULL; /* freed by protocol */ |
else if (prerr != ENOPROTOOPT) |
| } |
error = prerr; |
| } |
} else if (m != NULL) |
| bad: |
(void)m_free(m); |
| if (m) |
return error; |
| (void) m_free(m); |
|
| return (error); |
|
| } |
} |
| |
|
| int |
int |