| version 1.160, 2008/04/24 11:38:36 |
version 1.160.2.3, 2009/09/16 13:38:01 |
|
|
| /* $NetBSD$ */ |
/* $NetBSD$ */ |
| |
|
| /*- |
/*- |
| * Copyright (c) 2002, 2007, 2008 The NetBSD Foundation, Inc. |
* Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. |
| * All rights reserved. |
* All rights reserved. |
| * |
* |
| * This code is derived from software contributed to The NetBSD Foundation |
* This code is derived from software contributed to The NetBSD Foundation |
| * by Jason R. Thorpe of Wasabi Systems, Inc. |
* by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran. |
| * |
* |
| * Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
* modification, are permitted provided that the following conditions |
|
|
| * 2. Redistributions in binary form must reproduce the above copyright |
* 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
| * documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
| * 3. All advertising materials mentioning features or use of this software |
|
| * must display the following acknowledgement: |
|
| * This product includes software developed by the NetBSD |
|
| * Foundation, Inc. and its contributors. |
|
| * 4. Neither the name of The NetBSD Foundation nor the names of its |
|
| * contributors may be used to endorse or promote products derived |
|
| * from this software without specific prior written permission. |
|
| * |
* |
| * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
|
| #include <sys/cdefs.h> |
#include <sys/cdefs.h> |
| __KERNEL_RCSID(0, "$NetBSD$"); |
__KERNEL_RCSID(0, "$NetBSD$"); |
| |
|
| |
#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 <sys/param.h> |
#include <sys/param.h> |
| #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/filedesc.h> |
| #include <sys/malloc.h> |
#include <sys/kmem.h> |
| #include <sys/mbuf.h> |
#include <sys/mbuf.h> |
| #include <sys/domain.h> |
#include <sys/domain.h> |
| #include <sys/kernel.h> |
#include <sys/kernel.h> |
| Line 91 __KERNEL_RCSID(0, "$NetBSD$"); |
|
| Line 86 __KERNEL_RCSID(0, "$NetBSD$"); |
|
| #include <sys/socketvar.h> |
#include <sys/socketvar.h> |
| #include <sys/signalvar.h> |
#include <sys/signalvar.h> |
| #include <sys/resourcevar.h> |
#include <sys/resourcevar.h> |
| |
#include <sys/uidinfo.h> |
| #include <sys/event.h> |
#include <sys/event.h> |
| #include <sys/poll.h> |
#include <sys/poll.h> |
| #include <sys/kauth.h> |
#include <sys/kauth.h> |
| #include <sys/mutex.h> |
#include <sys/mutex.h> |
| #include <sys/condvar.h> |
#include <sys/condvar.h> |
| |
|
| |
#ifdef COMPAT_50 |
| |
#include <compat/sys/time.h> |
| |
#include <compat/sys/socket.h> |
| |
#endif |
| |
|
| #include <uvm/uvm.h> |
#include <uvm/uvm.h> |
| |
|
| MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); |
MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); |
| Line 134 EVCNT_ATTACH_STATIC(sosend_kvalimit); |
|
| Line 135 EVCNT_ATTACH_STATIC(sosend_kvalimit); |
|
| |
|
| static struct callback_entry sokva_reclaimerentry; |
static struct callback_entry sokva_reclaimerentry; |
| |
|
| #ifdef SOSEND_NO_LOAN |
#if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR) |
| int sock_loan_thresh = -1; |
int sock_loan_thresh = -1; |
| #else |
#else |
| int sock_loan_thresh = 4096; |
int sock_loan_thresh = 4096; |
| Line 155 static kcondvar_t socurkva_cv; |
|
| Line 156 static kcondvar_t socurkva_cv; |
|
| static size_t sodopendfree(void); |
static size_t sodopendfree(void); |
| static size_t sodopendfreel(void); |
static size_t sodopendfreel(void); |
| |
|
| |
static void sysctl_kern_somaxkva_setup(void); |
| |
static struct sysctllog *socket_sysctllog; |
| |
|
| static vsize_t |
static vsize_t |
| sokvareserve(struct socket *so, vsize_t len) |
sokvareserve(struct socket *so, vsize_t len) |
| { |
{ |
| Line 424 getsombuf(struct socket *so, int type) |
|
| Line 428 getsombuf(struct socket *so, int type) |
|
| return m; |
return m; |
| } |
} |
| |
|
| struct mbuf * |
|
| m_intopt(struct socket *so, int val) |
|
| { |
|
| struct mbuf *m; |
|
| |
|
| m = getsombuf(so, MT_SOOPTS); |
|
| m->m_len = sizeof(int); |
|
| *mtod(m, int *) = val; |
|
| return m; |
|
| } |
|
| |
|
| void |
void |
| soinit(void) |
soinit(void) |
| { |
{ |
| |
|
| |
sysctl_kern_somaxkva_setup(); |
| |
|
| mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); |
mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); |
| softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); |
softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); |
| cv_init(&socurkva_cv, "sokva"); |
cv_init(&socurkva_cv, "sokva"); |
| |
soinit2(); |
| |
|
| /* Set the initial adjusted socket buffer size. */ |
/* Set the initial adjusted socket buffer size. */ |
| if (sb_max_set(sb_max)) |
if (sb_max_set(sb_max)) |
| Line 505 socreate(int dom, struct socket **aso, i |
|
| Line 501 socreate(int dom, struct socket **aso, i |
|
| #endif |
#endif |
| 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_egid = kauth_cred_getegid(l->l_cred); |
| |
so->so_cpid = l->l_proc->p_pid; |
| if (lockso != NULL) { |
if (lockso != NULL) { |
| /* Caller wants us to share a lock. */ |
/* Caller wants us to share a lock. */ |
| lock = lockso->so_lock; |
lock = lockso->so_lock; |
| Line 557 fsocreate(int domain, struct socket **so |
|
| Line 555 fsocreate(int domain, struct socket **so |
|
| } |
} |
| |
|
| int |
int |
| |
sofamily(const struct socket *so) |
| |
{ |
| |
const struct protosw *pr; |
| |
const struct domain *dom; |
| |
|
| |
if ((pr = so->so_proto) == NULL) |
| |
return AF_UNSPEC; |
| |
if ((dom = pr->pr_domain) == NULL) |
| |
return AF_UNSPEC; |
| |
return dom->dom_family; |
| |
} |
| |
|
| |
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; |
| Line 574 solisten(struct socket *so, int backlog, |
|
| Line 585 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); |
| return (EOPNOTSUPP); |
return (EOPNOTSUPP); |
| |
} |
| error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, |
error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, |
| NULL, NULL, l); |
NULL, NULL, l); |
| if (error != 0) { |
if (error != 0) { |
| Line 594 solisten(struct socket *so, int backlog, |
|
| Line 607 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 623 sofree(struct socket *so) |
|
| Line 637 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. */ |
| |
if (so->so_accf != NULL) |
| |
(void)accept_filt_clear(so); |
| sounlock(so); |
sounlock(so); |
| soput(so); |
if (refs == 0) /* XXX */ |
| |
soput(so); |
| } |
} |
| |
|
| /* |
/* |
| Line 642 soclose(struct socket *so) |
|
| Line 661 soclose(struct socket *so) |
|
| error = 0; |
error = 0; |
| solock(so); |
solock(so); |
| if (so->so_options & SO_ACCEPTCONN) { |
if (so->so_options & SO_ACCEPTCONN) { |
| do { |
for (;;) { |
| if ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) { |
if ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) { |
| KASSERT(solocked2(so, so2)); |
KASSERT(solocked2(so, so2)); |
| (void) soqremque(so2, 0); |
(void) soqremque(so2, 0); |
| Line 659 soclose(struct socket *so) |
|
| Line 678 soclose(struct socket *so) |
|
| solock(so); |
solock(so); |
| continue; |
continue; |
| } |
} |
| } while (0); |
break; |
| |
} |
| } |
} |
| if (so->so_pcb == 0) |
if (so->so_pcb == 0) |
| goto discard; |
goto discard; |
| Line 673 soclose(struct socket *so) |
|
| Line 693 soclose(struct socket *so) |
|
| if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio) |
if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio) |
| goto drop; |
goto drop; |
| while (so->so_state & SS_ISCONNECTED) { |
while (so->so_state & SS_ISCONNECTED) { |
| error = sowait(so, so->so_linger * hz); |
error = sowait(so, true, so->so_linger * hz); |
| if (error) |
if (error) |
| break; |
break; |
| } |
} |
| Line 700 soclose(struct socket *so) |
|
| Line 720 soclose(struct socket *so) |
|
| int |
int |
| soabort(struct socket *so) |
soabort(struct socket *so) |
| { |
{ |
| |
u_int refs; |
| int error; |
int error; |
| |
|
| KASSERT(solocked(so)); |
KASSERT(solocked(so)); |
| KASSERT(so->so_head == NULL); |
KASSERT(so->so_head == NULL); |
| |
|
| |
so->so_aborting++; /* XXX */ |
| error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, |
error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, |
| NULL, NULL, NULL); |
NULL, NULL, NULL); |
| if (error) { |
refs = --so->so_aborting; /* XXX */ |
| |
if (error || (refs == 0)) { |
| sofree(so); |
sofree(so); |
| } else { |
} else { |
| sounlock(so); |
sounlock(so); |
| Line 849 sosend(struct socket *so, struct mbuf *a |
|
| Line 872 sosend(struct socket *so, struct mbuf *a |
|
| dontroute = |
dontroute = |
| (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && |
(flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && |
| (so->so_proto->pr_flags & PR_ATOMIC); |
(so->so_proto->pr_flags & PR_ATOMIC); |
| if (l) |
l->l_ru.ru_msgsnd++; |
| l->l_ru.ru_msgsnd++; |
|
| if (control) |
if (control) |
| clen = control->m_len; |
clen = control->m_len; |
| restart: |
restart: |
| Line 1538 soshutdown(struct socket *so, int how) |
|
| Line 1560 soshutdown(struct socket *so, int how) |
|
| return error; |
return error; |
| } |
} |
| |
|
| |
int |
| |
sodrain(struct socket *so) |
| |
{ |
| |
int error; |
| |
|
| |
solock(so); |
| |
so->so_state |= SS_ISDRAINING; |
| |
cv_broadcast(&so->so_cv); |
| |
error = soshutdown(so, SHUT_RDWR); |
| |
sounlock(so); |
| |
|
| |
return error; |
| |
} |
| |
|
| void |
void |
| sorflush(struct socket *so) |
sorflush(struct socket *so) |
| { |
{ |
| Line 1567 sorflush(struct socket *so) |
|
| Line 1603 sorflush(struct socket *so) |
|
| sbrelease(&asb, so); |
sbrelease(&asb, so); |
| } |
} |
| |
|
| |
/* |
| |
* internal set SOL_SOCKET options |
| |
*/ |
| static int |
static int |
| sosetopt1(struct socket *so, int level, int optname, struct mbuf *m) |
sosetopt1(struct socket *so, const struct sockopt *sopt) |
| { |
{ |
| int optval, val; |
int error = EINVAL, optval, opt; |
| struct linger *l; |
struct linger l; |
| struct sockbuf *sb; |
struct timeval tv; |
| struct timeval *tv; |
|
| |
|
| switch (optname) { |
switch ((opt = sopt->sopt_name)) { |
| |
|
| case SO_LINGER: |
case SO_ACCEPTFILTER: |
| if (m == NULL || m->m_len != sizeof(struct linger)) |
error = accept_filt_setopt(so, sopt); |
| return EINVAL; |
KASSERT(solocked(so)); |
| l = mtod(m, struct linger *); |
|
| if (l->l_linger < 0 || l->l_linger > USHRT_MAX || |
|
| l->l_linger > (INT_MAX / hz)) |
|
| return EDOM; |
|
| so->so_linger = l->l_linger; |
|
| if (l->l_onoff) |
|
| so->so_options |= SO_LINGER; |
|
| else |
|
| so->so_options &= ~SO_LINGER; |
|
| break; |
break; |
| |
|
| |
case SO_LINGER: |
| |
error = sockopt_get(sopt, &l, sizeof(l)); |
| |
solock(so); |
| |
if (error) |
| |
break; |
| |
if (l.l_linger < 0 || l.l_linger > USHRT_MAX || |
| |
l.l_linger > (INT_MAX / hz)) { |
| |
error = EDOM; |
| |
break; |
| |
} |
| |
so->so_linger = l.l_linger; |
| |
if (l.l_onoff) |
| |
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: |
| Line 1600 sosetopt1(struct socket *so, int level, |
|
| Line 1646 sosetopt1(struct socket *so, int level, |
|
| 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)) |
#ifdef SO_OTIMESTAMP |
| return EINVAL; |
case SO_OTIMESTAMP: |
| if (*mtod(m, int *)) |
#endif |
| so->so_options |= optname; |
error = sockopt_getint(sopt, &optval); |
| |
solock(so); |
| |
if (error) |
| |
break; |
| |
if (optval) |
| |
so->so_options |= opt; |
| else |
else |
| so->so_options &= ~optname; |
so->so_options &= ~opt; |
| break; |
break; |
| |
|
| case SO_SNDBUF: |
case SO_SNDBUF: |
| case SO_RCVBUF: |
case SO_RCVBUF: |
| case SO_SNDLOWAT: |
case SO_SNDLOWAT: |
| case SO_RCVLOWAT: |
case SO_RCVLOWAT: |
| if (m == NULL || m->m_len < sizeof(int)) |
error = sockopt_getint(sopt, &optval); |
| return EINVAL; |
solock(so); |
| |
if (error) |
| |
break; |
| |
|
| /* |
/* |
| * Values < 1 make no sense for any of these |
* Values < 1 make no sense for any of these |
| * options, so disallow them. |
* options, so disallow them. |
| */ |
*/ |
| optval = *mtod(m, int *); |
if (optval < 1) { |
| if (optval < 1) |
error = EINVAL; |
| return EINVAL; |
break; |
| |
} |
| switch (optname) { |
|
| |
|
| |
switch (opt) { |
| case SO_SNDBUF: |
case SO_SNDBUF: |
| |
if (sbreserve(&so->so_snd, (u_long)optval, so) == 0) { |
| |
error = ENOBUFS; |
| |
break; |
| |
} |
| |
so->so_snd.sb_flags &= ~SB_AUTOSIZE; |
| |
break; |
| |
|
| case SO_RCVBUF: |
case SO_RCVBUF: |
| sb = (optname == SO_SNDBUF) ? |
if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0) { |
| &so->so_snd : &so->so_rcv; |
error = ENOBUFS; |
| if (sbreserve(sb, (u_long)optval, so) == 0) |
break; |
| return ENOBUFS; |
} |
| sb->sb_flags &= ~SB_AUTOSIZE; |
so->so_rcv.sb_flags &= ~SB_AUTOSIZE; |
| break; |
break; |
| |
|
| /* |
/* |
| Line 1639 sosetopt1(struct socket *so, int level, |
|
| Line 1699 sosetopt1(struct socket *so, int level, |
|
| * the high-water. |
* the high-water. |
| */ |
*/ |
| case SO_SNDLOWAT: |
case SO_SNDLOWAT: |
| so->so_snd.sb_lowat = |
if (optval > so->so_snd.sb_hiwat) |
| (optval > so->so_snd.sb_hiwat) ? |
optval = so->so_snd.sb_hiwat; |
| so->so_snd.sb_hiwat : optval; |
|
| |
so->so_snd.sb_lowat = optval; |
| break; |
break; |
| |
|
| case SO_RCVLOWAT: |
case SO_RCVLOWAT: |
| so->so_rcv.sb_lowat = |
if (optval > so->so_rcv.sb_hiwat) |
| (optval > so->so_rcv.sb_hiwat) ? |
optval = so->so_rcv.sb_hiwat; |
| so->so_rcv.sb_hiwat : optval; |
|
| |
so->so_rcv.sb_lowat = optval; |
| break; |
break; |
| } |
} |
| break; |
break; |
| |
|
| |
#ifdef COMPAT_50 |
| |
case SO_OSNDTIMEO: |
| |
case SO_ORCVTIMEO: { |
| |
struct timeval50 otv; |
| |
error = sockopt_get(sopt, &otv, sizeof(otv)); |
| |
if (error) { |
| |
solock(so); |
| |
break; |
| |
} |
| |
timeval50_to_timeval(&otv, &tv); |
| |
opt = opt == SO_OSNDTIMEO ? SO_SNDTIMEO : SO_RCVTIMEO; |
| |
error = 0; |
| |
/*FALLTHROUGH*/ |
| |
} |
| |
#endif /* COMPAT_50 */ |
| |
|
| case SO_SNDTIMEO: |
case SO_SNDTIMEO: |
| case SO_RCVTIMEO: |
case SO_RCVTIMEO: |
| if (m == NULL || m->m_len < sizeof(*tv)) |
if (error) |
| return EINVAL; |
error = sockopt_get(sopt, &tv, sizeof(tv)); |
| tv = mtod(m, struct timeval *); |
solock(so); |
| if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) |
if (error) |
| return EDOM; |
break; |
| val = tv->tv_sec * hz + tv->tv_usec / tick; |
|
| if (val == 0 && tv->tv_usec != 0) |
|
| val = 1; |
|
| |
|
| switch (optname) { |
if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz) { |
| |
error = EDOM; |
| |
break; |
| |
} |
| |
|
| |
optval = tv.tv_sec * hz + tv.tv_usec / tick; |
| |
if (optval == 0 && tv.tv_usec != 0) |
| |
optval = 1; |
| |
|
| |
switch (opt) { |
| case SO_SNDTIMEO: |
case SO_SNDTIMEO: |
| so->so_snd.sb_timeo = val; |
so->so_snd.sb_timeo = optval; |
| break; |
break; |
| case SO_RCVTIMEO: |
case SO_RCVTIMEO: |
| so->so_rcv.sb_timeo = val; |
so->so_rcv.sb_timeo = optval; |
| break; |
break; |
| } |
} |
| break; |
break; |
| |
|
| default: |
default: |
| return ENOPROTOOPT; |
solock(so); |
| |
error = ENOPROTOOPT; |
| |
break; |
| } |
} |
| return 0; |
KASSERT(solocked(so)); |
| |
return error; |
| } |
} |
| |
|
| int |
int |
| sosetopt(struct socket *so, int level, int optname, struct mbuf *m) |
sosetopt(struct socket *so, struct sockopt *sopt) |
| { |
{ |
| int error, prerr; |
int error, prerr; |
| |
|
| solock(so); |
if (sopt->sopt_level == SOL_SOCKET) { |
| if (level == SOL_SOCKET) |
error = sosetopt1(so, sopt); |
| error = sosetopt1(so, level, optname, m); |
KASSERT(solocked(so)); |
| else |
} else { |
| error = ENOPROTOOPT; |
error = ENOPROTOOPT; |
| |
solock(so); |
| |
} |
| |
|
| if ((error == 0 || error == ENOPROTOOPT) && |
if ((error == 0 || error == ENOPROTOOPT) && |
| so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { |
so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { |
| /* give the protocol stack a shot */ |
/* give the protocol stack a shot */ |
| prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, level, |
prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, sopt); |
| optname, &m); |
|
| if (prerr == 0) |
if (prerr == 0) |
| error = 0; |
error = 0; |
| else if (prerr != ENOPROTOOPT) |
else if (prerr != ENOPROTOOPT) |
| error = prerr; |
error = prerr; |
| } else if (m != NULL) |
} |
| (void)m_free(m); |
|
| sounlock(so); |
sounlock(so); |
| return error; |
return error; |
| } |
} |
| |
|
| |
/* |
| |
* so_setsockopt() is a wrapper providing a sockopt structure for sosetopt() |
| |
*/ |
| |
int |
| |
so_setsockopt(struct lwp *l, struct socket *so, int level, int name, |
| |
const void *val, size_t valsize) |
| |
{ |
| |
struct sockopt sopt; |
| |
int error; |
| |
|
| |
KASSERT(valsize == 0 || val != NULL); |
| |
|
| |
sockopt_init(&sopt, level, name, valsize); |
| |
sockopt_set(&sopt, val, valsize); |
| |
|
| |
error = sosetopt(so, &sopt); |
| |
|
| |
sockopt_destroy(&sopt); |
| |
|
| |
return error; |
| |
} |
| |
|
| |
/* |
| |
* internal get SOL_SOCKET options |
| |
*/ |
| |
static int |
| |
sogetopt1(struct socket *so, struct sockopt *sopt) |
| |
{ |
| |
int error, optval, opt; |
| |
struct linger l; |
| |
struct timeval tv; |
| |
|
| |
switch ((opt = sopt->sopt_name)) { |
| |
|
| |
case SO_ACCEPTFILTER: |
| |
error = accept_filt_getopt(so, sopt); |
| |
break; |
| |
|
| |
case SO_LINGER: |
| |
l.l_onoff = (so->so_options & SO_LINGER) ? 1 : 0; |
| |
l.l_linger = so->so_linger; |
| |
|
| |
error = sockopt_set(sopt, &l, sizeof(l)); |
| |
break; |
| |
|
| |
case SO_USELOOPBACK: |
| |
case SO_DONTROUTE: |
| |
case SO_DEBUG: |
| |
case SO_KEEPALIVE: |
| |
case SO_REUSEADDR: |
| |
case SO_REUSEPORT: |
| |
case SO_BROADCAST: |
| |
case SO_OOBINLINE: |
| |
case SO_TIMESTAMP: |
| |
#ifdef SO_OTIMESTAMP |
| |
case SO_OTIMESTAMP: |
| |
#endif |
| |
error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0); |
| |
break; |
| |
|
| |
case SO_TYPE: |
| |
error = sockopt_setint(sopt, so->so_type); |
| |
break; |
| |
|
| |
case SO_ERROR: |
| |
error = sockopt_setint(sopt, so->so_error); |
| |
so->so_error = 0; |
| |
break; |
| |
|
| |
case SO_SNDBUF: |
| |
error = sockopt_setint(sopt, so->so_snd.sb_hiwat); |
| |
break; |
| |
|
| |
case SO_RCVBUF: |
| |
error = sockopt_setint(sopt, so->so_rcv.sb_hiwat); |
| |
break; |
| |
|
| |
case SO_SNDLOWAT: |
| |
error = sockopt_setint(sopt, so->so_snd.sb_lowat); |
| |
break; |
| |
|
| |
case SO_RCVLOWAT: |
| |
error = sockopt_setint(sopt, so->so_rcv.sb_lowat); |
| |
break; |
| |
|
| |
#ifdef COMPAT_50 |
| |
case SO_OSNDTIMEO: |
| |
case SO_ORCVTIMEO: { |
| |
struct timeval50 otv; |
| |
|
| |
optval = (opt == SO_OSNDTIMEO ? |
| |
so->so_snd.sb_timeo : so->so_rcv.sb_timeo); |
| |
|
| |
otv.tv_sec = optval / hz; |
| |
otv.tv_usec = (optval % hz) * tick; |
| |
|
| |
error = sockopt_set(sopt, &otv, sizeof(otv)); |
| |
break; |
| |
} |
| |
#endif /* COMPAT_50 */ |
| |
|
| |
case SO_SNDTIMEO: |
| |
case SO_RCVTIMEO: |
| |
optval = (opt == SO_SNDTIMEO ? |
| |
so->so_snd.sb_timeo : so->so_rcv.sb_timeo); |
| |
|
| |
tv.tv_sec = optval / hz; |
| |
tv.tv_usec = (optval % hz) * tick; |
| |
|
| |
error = sockopt_set(sopt, &tv, sizeof(tv)); |
| |
break; |
| |
|
| |
case SO_OVERFLOWED: |
| |
error = sockopt_setint(sopt, so->so_rcv.sb_overflowed); |
| |
break; |
| |
|
| |
default: |
| |
error = ENOPROTOOPT; |
| |
break; |
| |
} |
| |
|
| |
return (error); |
| |
} |
| |
|
| int |
int |
| sogetopt(struct socket *so, int level, int optname, struct mbuf **mp) |
sogetopt(struct socket *so, struct sockopt *sopt) |
| { |
{ |
| struct mbuf *m; |
|
| int error; |
int error; |
| |
|
| solock(so); |
solock(so); |
| if (level != SOL_SOCKET) { |
if (sopt->sopt_level != SOL_SOCKET) { |
| if (so->so_proto && so->so_proto->pr_ctloutput) { |
if (so->so_proto && so->so_proto->pr_ctloutput) { |
| error = ((*so->so_proto->pr_ctloutput) |
error = ((*so->so_proto->pr_ctloutput) |
| (PRCO_GETOPT, so, level, optname, mp)); |
(PRCO_GETOPT, so, sopt)); |
| } else |
} else |
| error = (ENOPROTOOPT); |
error = (ENOPROTOOPT); |
| } else { |
} else { |
| m = m_get(M_WAIT, MT_SOOPTS); |
error = sogetopt1(so, sopt); |
| m->m_len = sizeof(int); |
} |
| |
sounlock(so); |
| |
return (error); |
| |
} |
| |
|
| switch (optname) { |
/* |
| |
* alloc sockopt data buffer buffer |
| |
* - will be released at destroy |
| |
*/ |
| |
static int |
| |
sockopt_alloc(struct sockopt *sopt, size_t len, km_flag_t kmflag) |
| |
{ |
| |
|
| case SO_LINGER: |
KASSERT(sopt->sopt_size == 0); |
| m->m_len = sizeof(struct linger); |
|
| mtod(m, struct linger *)->l_onoff = |
|
| (so->so_options & SO_LINGER) ? 1 : 0; |
|
| mtod(m, struct linger *)->l_linger = so->so_linger; |
|
| break; |
|
| |
|
| case SO_USELOOPBACK: |
if (len > sizeof(sopt->sopt_buf)) { |
| case SO_DONTROUTE: |
sopt->sopt_data = kmem_zalloc(len, kmflag); |
| case SO_DEBUG: |
if (sopt->sopt_data == NULL) |
| case SO_KEEPALIVE: |
return ENOMEM; |
| case SO_REUSEADDR: |
} else |
| case SO_REUSEPORT: |
sopt->sopt_data = sopt->sopt_buf; |
| case SO_BROADCAST: |
|
| case SO_OOBINLINE: |
|
| case SO_TIMESTAMP: |
|
| *mtod(m, int *) = (so->so_options & optname) ? 1 : 0; |
|
| break; |
|
| |
|
| case SO_TYPE: |
sopt->sopt_size = len; |
| *mtod(m, int *) = so->so_type; |
return 0; |
| break; |
} |
| |
|
| case SO_ERROR: |
/* |
| *mtod(m, int *) = so->so_error; |
* initialise sockopt storage |
| so->so_error = 0; |
* - MAY sleep during allocation |
| break; |
*/ |
| |
void |
| |
sockopt_init(struct sockopt *sopt, int level, int name, size_t size) |
| |
{ |
| |
|
| case SO_SNDBUF: |
memset(sopt, 0, sizeof(*sopt)); |
| *mtod(m, int *) = so->so_snd.sb_hiwat; |
|
| break; |
|
| |
|
| case SO_RCVBUF: |
sopt->sopt_level = level; |
| *mtod(m, int *) = so->so_rcv.sb_hiwat; |
sopt->sopt_name = name; |
| break; |
(void)sockopt_alloc(sopt, size, KM_SLEEP); |
| |
} |
| |
|
| case SO_SNDLOWAT: |
/* |
| *mtod(m, int *) = so->so_snd.sb_lowat; |
* destroy sockopt storage |
| break; |
* - will release any held memory references |
| |
*/ |
| |
void |
| |
sockopt_destroy(struct sockopt *sopt) |
| |
{ |
| |
|
| case SO_RCVLOWAT: |
if (sopt->sopt_data != sopt->sopt_buf) |
| *mtod(m, int *) = so->so_rcv.sb_lowat; |
kmem_free(sopt->sopt_data, sopt->sopt_size); |
| break; |
|
| |
|
| case SO_SNDTIMEO: |
memset(sopt, 0, sizeof(*sopt)); |
| case SO_RCVTIMEO: |
} |
| { |
|
| int val = (optname == SO_SNDTIMEO ? |
|
| so->so_snd.sb_timeo : so->so_rcv.sb_timeo); |
|
| |
|
| m->m_len = sizeof(struct timeval); |
|
| mtod(m, struct timeval *)->tv_sec = val / hz; |
|
| mtod(m, struct timeval *)->tv_usec = |
|
| (val % hz) * tick; |
|
| break; |
|
| } |
|
| |
|
| case SO_OVERFLOWED: |
/* |
| *mtod(m, int *) = so->so_rcv.sb_overflowed; |
* set sockopt value |
| break; |
* - value is copied into sockopt |
| |
* - memory is allocated when necessary, will not sleep |
| |
*/ |
| |
int |
| |
sockopt_set(struct sockopt *sopt, const void *buf, size_t len) |
| |
{ |
| |
int error; |
| |
|
| default: |
if (sopt->sopt_size == 0) { |
| sounlock(so); |
error = sockopt_alloc(sopt, len, KM_NOSLEEP); |
| (void)m_free(m); |
if (error) |
| return (ENOPROTOOPT); |
return error; |
| |
} |
| |
|
| |
KASSERT(sopt->sopt_size == len); |
| |
memcpy(sopt->sopt_data, buf, len); |
| |
return 0; |
| |
} |
| |
|
| |
/* |
| |
* common case of set sockopt integer value |
| |
*/ |
| |
int |
| |
sockopt_setint(struct sockopt *sopt, int val) |
| |
{ |
| |
|
| |
return sockopt_set(sopt, &val, sizeof(int)); |
| |
} |
| |
|
| |
/* |
| |
* get sockopt value |
| |
* - correct size must be given |
| |
*/ |
| |
int |
| |
sockopt_get(const struct sockopt *sopt, void *buf, size_t len) |
| |
{ |
| |
|
| |
if (sopt->sopt_size != len) |
| |
return EINVAL; |
| |
|
| |
memcpy(buf, sopt->sopt_data, len); |
| |
return 0; |
| |
} |
| |
|
| |
/* |
| |
* common case of get sockopt integer value |
| |
*/ |
| |
int |
| |
sockopt_getint(const struct sockopt *sopt, int *valp) |
| |
{ |
| |
|
| |
return sockopt_get(sopt, valp, sizeof(int)); |
| |
} |
| |
|
| |
/* |
| |
* set sockopt value from mbuf |
| |
* - ONLY for legacy code |
| |
* - mbuf is released by sockopt |
| |
* - will not sleep |
| |
*/ |
| |
int |
| |
sockopt_setmbuf(struct sockopt *sopt, struct mbuf *m) |
| |
{ |
| |
size_t len; |
| |
int error; |
| |
|
| |
len = m_length(m); |
| |
|
| |
if (sopt->sopt_size == 0) { |
| |
error = sockopt_alloc(sopt, len, KM_NOSLEEP); |
| |
if (error) |
| |
return error; |
| |
} |
| |
|
| |
KASSERT(sopt->sopt_size == len); |
| |
m_copydata(m, 0, len, sopt->sopt_data); |
| |
m_freem(m); |
| |
|
| |
return 0; |
| |
} |
| |
|
| |
/* |
| |
* get sockopt value into mbuf |
| |
* - ONLY for legacy code |
| |
* - mbuf to be released by the caller |
| |
* - will not sleep |
| |
*/ |
| |
struct mbuf * |
| |
sockopt_getmbuf(const struct sockopt *sopt) |
| |
{ |
| |
struct mbuf *m; |
| |
|
| |
if (sopt->sopt_size > MCLBYTES) |
| |
return NULL; |
| |
|
| |
m = m_get(M_DONTWAIT, MT_SOOPTS); |
| |
if (m == NULL) |
| |
return NULL; |
| |
|
| |
if (sopt->sopt_size > MLEN) { |
| |
MCLGET(m, M_DONTWAIT); |
| |
if ((m->m_flags & M_EXT) == 0) { |
| |
m_free(m); |
| |
return NULL; |
| } |
} |
| *mp = m; |
|
| error = 0; |
|
| } |
} |
| |
|
| sounlock(so); |
memcpy(mtod(m, void *), sopt->sopt_data, sopt->sopt_size); |
| return (error); |
m->m_len = sopt->sopt_size; |
| |
|
| |
return m; |
| } |
} |
| |
|
| void |
void |
| Line 1803 sohasoutofband(struct socket *so) |
|
| Line 2105 sohasoutofband(struct socket *so) |
|
| { |
{ |
| |
|
| fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); |
fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); |
| selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0); |
selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, NOTE_SUBMIT); |
| } |
} |
| |
|
| static void |
static void |
| Line 2034 sysctl_kern_somaxkva(SYSCTLFN_ARGS) |
|
| Line 2336 sysctl_kern_somaxkva(SYSCTLFN_ARGS) |
|
| return (error); |
return (error); |
| } |
} |
| |
|
| SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup") |
static void |
| |
sysctl_kern_somaxkva_setup(void) |
| { |
{ |
| |
|
| sysctl_createv(clog, 0, NULL, NULL, |
KASSERT(socket_sysctllog == NULL); |
| |
sysctl_createv(&socket_sysctllog, 0, NULL, NULL, |
| CTLFLAG_PERMANENT, |
CTLFLAG_PERMANENT, |
| CTLTYPE_NODE, "kern", NULL, |
CTLTYPE_NODE, "kern", NULL, |
| NULL, 0, NULL, 0, |
NULL, 0, NULL, 0, |
| CTL_KERN, CTL_EOL); |
CTL_KERN, CTL_EOL); |
| |
|
| sysctl_createv(clog, 0, NULL, NULL, |
sysctl_createv(&socket_sysctllog, 0, NULL, NULL, |
| CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
CTLFLAG_PERMANENT|CTLFLAG_READWRITE, |
| CTLTYPE_INT, "somaxkva", |
CTLTYPE_INT, "somaxkva", |
| SYSCTL_DESCR("Maximum amount of kernel memory to be " |
SYSCTL_DESCR("Maximum amount of kernel memory to be " |