| version 1.140.6.1, 2007/08/09 02:37:21 |
version 1.140.6.2, 2007/10/02 18:29:06 |
| 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 427 sokva_reclaim_callback(struct callback_e |
|
| Line 430 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 495 socreate(int dom, struct socket **aso, i |
|
| Line 519 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 510 socreate(int dom, struct socket **aso, i |
|
| Line 536 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 565 sofree(struct socket *so) |
|
| Line 626 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 1397 sorflush(struct socket *so) |
|
| Line 1460 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_SNDBUF: |
case SO_SNDTIMEO: |
| case SO_RCVBUF: |
case SO_RCVTIMEO: |
| sb = (optname == SO_SNDBUF) ? |
if (m == NULL || m->m_len < sizeof(*tv)) |
| &so->so_snd : &so->so_rcv; |
return EINVAL; |
| if (sbreserve(sb, (u_long)optval, so) == 0) { |
tv = mtod(m, struct timeval *); |
| error = ENOBUFS; |
if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) |
| goto bad; |
return EDOM; |
| } |
val = tv->tv_sec * hz + tv->tv_usec / tick; |
| sb->sb_flags &= ~SB_AUTOSIZE; |
if (val == 0 && tv->tv_usec != 0) |
| break; |
val = 1; |
| |
|
| /* |
switch (optname) { |
| * 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 |