| version 1.160.2.4, 2010/03/11 15:04:20 |
version 1.161, 2008/04/27 14:26:58 |
|
|
| /* $NetBSD$ */ |
/* $NetBSD$ */ |
| |
|
| /*- |
/*- |
| * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. |
* Copyright (c) 2002, 2007, 2008 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, and by Andrew Doran. |
* by Jason R. Thorpe of Wasabi Systems, Inc. |
| * |
* |
| * 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/kmem.h> |
#include <sys/malloc.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 86 __KERNEL_RCSID(0, "$NetBSD$"); |
|
| Line 91 __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 135 EVCNT_ATTACH_STATIC(sosend_kvalimit); |
|
| Line 134 EVCNT_ATTACH_STATIC(sosend_kvalimit); |
|
| |
|
| static struct callback_entry sokva_reclaimerentry; |
static struct callback_entry sokva_reclaimerentry; |
| |
|
| #if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR) |
#ifdef SOSEND_NO_LOAN |
| int sock_loan_thresh = -1; |
int sock_loan_thresh = -1; |
| #else |
#else |
| int sock_loan_thresh = 4096; |
int sock_loan_thresh = 4096; |
| Line 151 int somaxkva = SOMAXKVA; |
|
| Line 150 int somaxkva = SOMAXKVA; |
|
| static int socurkva; |
static int socurkva; |
| static kcondvar_t socurkva_cv; |
static kcondvar_t socurkva_cv; |
| |
|
| static kauth_listener_t socket_listener; |
|
| |
|
| #define SOCK_LOAN_CHUNK 65536 |
#define SOCK_LOAN_CHUNK 65536 |
| |
|
| 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 386 sosend_loan(struct socket *so, struct ui |
|
| Line 380 sosend_loan(struct socket *so, struct ui |
|
| |
|
| for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE) |
for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE) |
| pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]), |
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]), |
| VM_PROT_READ, 0); |
VM_PROT_READ); |
| pmap_update(pmap_kernel()); |
pmap_update(pmap_kernel()); |
| |
|
| lva += (vaddr_t) iov->iov_base & PAGE_MASK; |
lva += (vaddr_t) iov->iov_base & PAGE_MASK; |
| Line 430 getsombuf(struct socket *so, int type) |
|
| Line 424 getsombuf(struct socket *so, int type) |
|
| return m; |
return m; |
| } |
} |
| |
|
| static int |
struct mbuf * |
| socket_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, |
m_intopt(struct socket *so, int val) |
| void *arg0, void *arg1, void *arg2, void *arg3) |
|
| { |
{ |
| int result; |
struct mbuf *m; |
| enum kauth_network_req req; |
|
| |
|
| result = KAUTH_RESULT_DEFER; |
|
| req = (enum kauth_network_req)arg0; |
|
| |
|
| if ((action != KAUTH_NETWORK_SOCKET) && |
|
| (action != KAUTH_NETWORK_BIND)) |
|
| return result; |
|
| |
|
| switch (req) { |
|
| case KAUTH_REQ_NETWORK_BIND_PORT: |
|
| result = KAUTH_RESULT_ALLOW; |
|
| break; |
|
| |
|
| case KAUTH_REQ_NETWORK_SOCKET_DROP: { |
|
| /* Normal users can only drop their own connections. */ |
|
| struct socket *so = (struct socket *)arg1; |
|
| |
|
| if (proc_uidmatch(cred, so->so_cred)) |
|
| result = KAUTH_RESULT_ALLOW; |
|
| |
|
| break; |
|
| } |
|
| |
|
| case KAUTH_REQ_NETWORK_SOCKET_OPEN: |
|
| /* We allow "raw" routing/bluetooth sockets to anyone. */ |
|
| if ((u_long)arg1 == PF_ROUTE || (u_long)arg1 == PF_BLUETOOTH) |
|
| result = KAUTH_RESULT_ALLOW; |
|
| else { |
|
| /* Privileged, let secmodel handle this. */ |
|
| if ((u_long)arg2 == SOCK_RAW) |
|
| break; |
|
| } |
|
| |
|
| result = KAUTH_RESULT_ALLOW; |
|
| |
|
| break; |
|
| |
|
| case KAUTH_REQ_NETWORK_SOCKET_CANSEE: |
|
| result = KAUTH_RESULT_ALLOW; |
|
| |
|
| break; |
|
| |
|
| default: |
|
| break; |
|
| } |
|
| |
|
| return result; |
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)) |
|
|
| |
|
| callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback, |
callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback, |
| &sokva_reclaimerentry, NULL, sokva_reclaim_callback); |
&sokva_reclaimerentry, NULL, sokva_reclaim_callback); |
| |
|
| socket_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK, |
|
| socket_listener_cb, NULL); |
|
| } |
} |
| |
|
| /* |
/* |
| Line 561 socreate(int dom, struct socket **aso, i |
|
| Line 505 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_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 579 socreate(int dom, struct socket **aso, i |
|
| Line 522 socreate(int dom, struct socket **aso, i |
|
| sofree(so); |
sofree(so); |
| return error; |
return error; |
| } |
} |
| so->so_cred = kauth_cred_dup(l->l_cred); |
|
| sounlock(so); |
sounlock(so); |
| *aso = so; |
*aso = so; |
| return 0; |
return 0; |
| Line 615 fsocreate(int domain, struct socket **so |
|
| Line 557 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 645 solisten(struct socket *so, int backlog, |
|
| Line 574 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 698 sofree(struct socket *so) |
|
| Line 625 sofree(struct socket *so) |
|
| 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 */ |
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); |
| if (refs == 0) /* XXX */ |
if (refs == 0) /* XXX */ |
| soput(so); |
soput(so); |
| Line 721 soclose(struct socket *so) |
|
| Line 645 soclose(struct socket *so) |
|
| error = 0; |
error = 0; |
| solock(so); |
solock(so); |
| if (so->so_options & SO_ACCEPTCONN) { |
if (so->so_options & SO_ACCEPTCONN) { |
| for (;;) { |
do { |
| 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 738 soclose(struct socket *so) |
|
| Line 662 soclose(struct socket *so) |
|
| solock(so); |
solock(so); |
| continue; |
continue; |
| } |
} |
| break; |
} while (0); |
| } |
|
| } |
} |
| if (so->so_pcb == 0) |
if (so->so_pcb == 0) |
| goto discard; |
goto discard; |
| Line 753 soclose(struct socket *so) |
|
| Line 676 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, true, so->so_linger * hz); |
error = sowait(so, so->so_linger * hz); |
| if (error) |
if (error) |
| break; |
break; |
| } |
} |
| Line 769 soclose(struct socket *so) |
|
| Line 692 soclose(struct socket *so) |
|
| discard: |
discard: |
| if (so->so_state & SS_NOFDREF) |
if (so->so_state & SS_NOFDREF) |
| panic("soclose: NOFDREF"); |
panic("soclose: NOFDREF"); |
| kauth_cred_free(so->so_cred); |
|
| so->so_state |= SS_NOFDREF; |
so->so_state |= SS_NOFDREF; |
| sofree(so); |
sofree(so); |
| return (error); |
return (error); |
| Line 791 soabort(struct socket *so) |
|
| Line 713 soabort(struct socket *so) |
|
| 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); |
| refs = --so->so_aborting; /* XXX */ |
refs = --so->so_aborting; /* XXX */ |
| if (error || (refs == 0)) { |
if (error) { |
| sofree(so); |
sofree(so); |
| } else { |
} else { |
| sounlock(so); |
sounlock(so); |
| |
if (refs == 0) |
| |
sofree(so); |
| } |
} |
| return error; |
return error; |
| } |
} |
| Line 902 sosend(struct socket *so, struct mbuf *a |
|
| Line 826 sosend(struct socket *so, struct mbuf *a |
|
| struct proc *p; |
struct proc *p; |
| long space, len, resid, clen, mlen; |
long space, len, resid, clen, mlen; |
| int error, s, dontroute, atomic; |
int error, s, dontroute, atomic; |
| short wakeup_state = 0; |
|
| |
|
| p = l->l_proc; |
p = l->l_proc; |
| sodopendfree(); |
sodopendfree(); |
| Line 934 sosend(struct socket *so, struct mbuf *a |
|
| Line 857 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); |
| l->l_ru.ru_msgsnd++; |
if (l) |
| |
l->l_ru.ru_msgsnd++; |
| if (control) |
if (control) |
| clen = control->m_len; |
clen = control->m_len; |
| restart: |
restart: |
| Line 977 sosend(struct socket *so, struct mbuf *a |
|
| Line 901 sosend(struct socket *so, struct mbuf *a |
|
| goto release; |
goto release; |
| } |
} |
| sbunlock(&so->so_snd); |
sbunlock(&so->so_snd); |
| if (wakeup_state & SS_RESTARTSYS) { |
|
| error = ERESTART; |
|
| goto out; |
|
| } |
|
| error = sbwait(&so->so_snd); |
error = sbwait(&so->so_snd); |
| if (error) |
if (error) |
| goto out; |
goto out; |
| wakeup_state = so->so_state; |
|
| goto restart; |
goto restart; |
| } |
} |
| wakeup_state = 0; |
|
| mp = ⊤ |
mp = ⊤ |
| space -= clen; |
space -= clen; |
| do { |
do { |
| Line 1163 soreceive(struct socket *so, struct mbuf |
|
| Line 1081 soreceive(struct socket *so, struct mbuf |
|
| struct mbuf *nextrecord; |
struct mbuf *nextrecord; |
| int mbuf_removed = 0; |
int mbuf_removed = 0; |
| const struct domain *dom; |
const struct domain *dom; |
| short wakeup_state = 0; |
|
| |
|
| pr = so->so_proto; |
pr = so->so_proto; |
| atomic = pr->pr_flags & PR_ATOMIC; |
atomic = pr->pr_flags & PR_ATOMIC; |
| Line 1278 soreceive(struct socket *so, struct mbuf |
|
| Line 1195 soreceive(struct socket *so, struct mbuf |
|
| SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); |
SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); |
| SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); |
SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); |
| sbunlock(&so->so_rcv); |
sbunlock(&so->so_rcv); |
| if (wakeup_state & SS_RESTARTSYS) |
error = sbwait(&so->so_rcv); |
| error = ERESTART; |
|
| else |
|
| error = sbwait(&so->so_rcv); |
|
| if (error != 0) { |
if (error != 0) { |
| sounlock(so); |
sounlock(so); |
| splx(s); |
splx(s); |
| return error; |
return error; |
| } |
} |
| wakeup_state = so->so_state; |
|
| goto restart; |
goto restart; |
| } |
} |
| dontblock: |
dontblock: |
| Line 1426 soreceive(struct socket *so, struct mbuf |
|
| Line 1339 soreceive(struct socket *so, struct mbuf |
|
| panic("receive 3"); |
panic("receive 3"); |
| #endif |
#endif |
| so->so_state &= ~SS_RCVATMARK; |
so->so_state &= ~SS_RCVATMARK; |
| wakeup_state = 0; |
|
| len = uio->uio_resid; |
len = uio->uio_resid; |
| if (so->so_oobmark && len > so->so_oobmark - offset) |
if (so->so_oobmark && len > so->so_oobmark - offset) |
| len = so->so_oobmark - offset; |
len = so->so_oobmark - offset; |
| Line 1559 soreceive(struct socket *so, struct mbuf |
|
| Line 1471 soreceive(struct socket *so, struct mbuf |
|
| NULL, (struct mbuf *)(long)flags, NULL, 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) |
error = sbwait(&so->so_rcv); |
| error = ERESTART; |
|
| else |
|
| error = sbwait(&so->so_rcv); |
|
| if (error != 0) { |
if (error != 0) { |
| sbunlock(&so->so_rcv); |
sbunlock(&so->so_rcv); |
| sounlock(so); |
sounlock(so); |
| Line 1571 soreceive(struct socket *so, struct mbuf |
|
| Line 1480 soreceive(struct socket *so, struct mbuf |
|
| } |
} |
| if ((m = so->so_rcv.sb_mb) != NULL) |
if ((m = so->so_rcv.sb_mb) != NULL) |
| nextrecord = m->m_nextpkt; |
nextrecord = m->m_nextpkt; |
| wakeup_state = so->so_state; |
|
| } |
} |
| } |
} |
| |
|
| Line 1639 soshutdown(struct socket *so, int how) |
|
| Line 1547 soshutdown(struct socket *so, int how) |
|
| } |
} |
| |
|
| void |
void |
| sorestart(struct socket *so) |
|
| { |
|
| /* |
|
| * An application has called close() on an fd on which another |
|
| * of its threads has called a socket system call. |
|
| * Mark this and wake everyone up, and code that would block again |
|
| * instead returns ERESTART. |
|
| * On system call re-entry the fd is validated and EBADF returned. |
|
| * Any other fd will block again on the 2nd syscall. |
|
| */ |
|
| solock(so); |
|
| so->so_state |= SS_RESTARTSYS; |
|
| cv_broadcast(&so->so_cv); |
|
| cv_broadcast(&so->so_snd.sb_cv); |
|
| cv_broadcast(&so->so_rcv.sb_cv); |
|
| sounlock(so); |
|
| } |
|
| |
|
| void |
|
| sorflush(struct socket *so) |
sorflush(struct socket *so) |
| { |
{ |
| struct sockbuf *sb, asb; |
struct sockbuf *sb, asb; |
| Line 1686 sorflush(struct socket *so) |
|
| Line 1575 sorflush(struct socket *so) |
|
| sbrelease(&asb, so); |
sbrelease(&asb, so); |
| } |
} |
| |
|
| /* |
|
| * internal set SOL_SOCKET options |
|
| */ |
|
| static int |
static int |
| sosetopt1(struct socket *so, const struct sockopt *sopt) |
sosetopt1(struct socket *so, int level, int optname, struct mbuf *m) |
| { |
{ |
| int error = EINVAL, optval, opt; |
int optval, val; |
| struct linger l; |
struct linger *l; |
| struct timeval tv; |
struct sockbuf *sb; |
| |
struct timeval *tv; |
| |
|
| switch ((opt = sopt->sopt_name)) { |
switch (optname) { |
| |
|
| case SO_ACCEPTFILTER: |
case SO_LINGER: |
| error = accept_filt_setopt(so, sopt); |
if (m == NULL || m->m_len != sizeof(struct linger)) |
| KASSERT(solocked(so)); |
return EINVAL; |
| |
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 1729 sosetopt1(struct socket *so, const struc |
|
| Line 1608 sosetopt1(struct socket *so, const struc |
|
| case SO_REUSEPORT: |
case SO_REUSEPORT: |
| case SO_OOBINLINE: |
case SO_OOBINLINE: |
| case SO_TIMESTAMP: |
case SO_TIMESTAMP: |
| #ifdef SO_OTIMESTAMP |
if (m == NULL || m->m_len < sizeof(int)) |
| case SO_OTIMESTAMP: |
return EINVAL; |
| #endif |
if (*mtod(m, int *)) |
| error = sockopt_getint(sopt, &optval); |
so->so_options |= optname; |
| solock(so); |
|
| if (error) |
|
| break; |
|
| if (optval) |
|
| so->so_options |= opt; |
|
| else |
else |
| so->so_options &= ~opt; |
so->so_options &= ~optname; |
| 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: |
| error = sockopt_getint(sopt, &optval); |
if (m == NULL || m->m_len < sizeof(int)) |
| solock(so); |
return EINVAL; |
| 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. |
| */ |
*/ |
| if (optval < 1) { |
optval = *mtod(m, int *); |
| error = EINVAL; |
if (optval < 1) |
| break; |
return EINVAL; |
| } |
|
| |
|
| switch (opt) { |
switch (optname) { |
| 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_SNDBUF: |
| case SO_RCVBUF: |
case SO_RCVBUF: |
| if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0) { |
sb = (optname == SO_SNDBUF) ? |
| error = ENOBUFS; |
&so->so_snd : &so->so_rcv; |
| break; |
if (sbreserve(sb, (u_long)optval, so) == 0) |
| } |
return ENOBUFS; |
| so->so_rcv.sb_flags &= ~SB_AUTOSIZE; |
sb->sb_flags &= ~SB_AUTOSIZE; |
| break; |
break; |
| |
|
| /* |
/* |
| Line 1782 sosetopt1(struct socket *so, const struc |
|
| Line 1647 sosetopt1(struct socket *so, const struc |
|
| * the high-water. |
* the high-water. |
| */ |
*/ |
| case SO_SNDLOWAT: |
case SO_SNDLOWAT: |
| if (optval > so->so_snd.sb_hiwat) |
so->so_snd.sb_lowat = |
| 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: |
| if (optval > so->so_rcv.sb_hiwat) |
so->so_rcv.sb_lowat = |
| 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 (error) |
if (m == NULL || m->m_len < sizeof(*tv)) |
| error = sockopt_get(sopt, &tv, sizeof(tv)); |
return EINVAL; |
| solock(so); |
tv = mtod(m, struct timeval *); |
| if (error) |
if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) |
| break; |
return EDOM; |
| |
val = tv->tv_sec * hz + tv->tv_usec / tick; |
| if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz) { |
if (val == 0 && tv->tv_usec != 0) |
| error = EDOM; |
val = 1; |
| break; |
|
| } |
|
| |
|
| optval = tv.tv_sec * hz + tv.tv_usec / tick; |
switch (optname) { |
| if (optval == 0 && tv.tv_usec != 0) |
|
| optval = 1; |
|
| |
|
| switch (opt) { |
|
| case SO_SNDTIMEO: |
case SO_SNDTIMEO: |
| so->so_snd.sb_timeo = optval; |
so->so_snd.sb_timeo = val; |
| break; |
break; |
| case SO_RCVTIMEO: |
case SO_RCVTIMEO: |
| so->so_rcv.sb_timeo = optval; |
so->so_rcv.sb_timeo = val; |
| break; |
break; |
| } |
} |
| break; |
break; |
| |
|
| default: |
default: |
| solock(so); |
return ENOPROTOOPT; |
| error = ENOPROTOOPT; |
|
| break; |
|
| } |
} |
| KASSERT(solocked(so)); |
return 0; |
| return error; |
|
| } |
} |
| |
|
| int |
int |
| sosetopt(struct socket *so, struct sockopt *sopt) |
sosetopt(struct socket *so, int level, int optname, struct mbuf *m) |
| { |
{ |
| int error, prerr; |
int error, prerr; |
| |
|
| if (sopt->sopt_level == SOL_SOCKET) { |
solock(so); |
| error = sosetopt1(so, sopt); |
if (level == SOL_SOCKET) |
| KASSERT(solocked(so)); |
error = sosetopt1(so, level, optname, m); |
| } 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, sopt); |
prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, level, |
| |
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 |
int |
| so_setsockopt(struct lwp *l, struct socket *so, int level, int name, |
sogetopt(struct socket *so, int level, int optname, struct mbuf **mp) |
| 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 |
|
| sogetopt(struct socket *so, struct sockopt *sopt) |
|
| { |
{ |
| |
struct mbuf *m; |
| int error; |
int error; |
| |
|
| solock(so); |
solock(so); |
| if (sopt->sopt_level != SOL_SOCKET) { |
if (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, sopt)); |
(PRCO_GETOPT, so, level, optname, mp)); |
| } else |
} else |
| error = (ENOPROTOOPT); |
error = (ENOPROTOOPT); |
| } else { |
} else { |
| error = sogetopt1(so, sopt); |
m = m_get(M_WAIT, MT_SOOPTS); |
| } |
m->m_len = sizeof(int); |
| sounlock(so); |
|
| return (error); |
|
| } |
|
| |
|
| /* |
|
| * alloc sockopt data buffer buffer |
|
| * - will be released at destroy |
|
| */ |
|
| static int |
|
| sockopt_alloc(struct sockopt *sopt, size_t len, km_flag_t kmflag) |
|
| { |
|
| |
|
| KASSERT(sopt->sopt_size == 0); |
|
| |
|
| if (len > sizeof(sopt->sopt_buf)) { |
|
| sopt->sopt_data = kmem_zalloc(len, kmflag); |
|
| if (sopt->sopt_data == NULL) |
|
| return ENOMEM; |
|
| } else |
|
| sopt->sopt_data = sopt->sopt_buf; |
|
| |
|
| sopt->sopt_size = len; |
|
| return 0; |
|
| } |
|
| |
|
| /* |
|
| * initialise sockopt storage |
|
| * - MAY sleep during allocation |
|
| */ |
|
| void |
|
| sockopt_init(struct sockopt *sopt, int level, int name, size_t size) |
|
| { |
|
| |
|
| memset(sopt, 0, sizeof(*sopt)); |
|
| |
|
| sopt->sopt_level = level; |
|
| sopt->sopt_name = name; |
|
| (void)sockopt_alloc(sopt, size, KM_SLEEP); |
|
| } |
|
| |
|
| /* |
switch (optname) { |
| * destroy sockopt storage |
|
| * - will release any held memory references |
|
| */ |
|
| void |
|
| sockopt_destroy(struct sockopt *sopt) |
|
| { |
|
| |
|
| if (sopt->sopt_data != sopt->sopt_buf) |
|
| kmem_free(sopt->sopt_data, sopt->sopt_size); |
|
| |
|
| memset(sopt, 0, sizeof(*sopt)); |
case SO_LINGER: |
| } |
m->m_len = sizeof(struct linger); |
| |
mtod(m, struct linger *)->l_onoff = |
| /* |
(so->so_options & SO_LINGER) ? 1 : 0; |
| * set sockopt value |
mtod(m, struct linger *)->l_linger = so->so_linger; |
| * - value is copied into sockopt |
break; |
| * - memory is allocated when necessary, will not sleep |
|
| */ |
|
| int |
|
| sockopt_set(struct sockopt *sopt, const void *buf, size_t len) |
|
| { |
|
| int error; |
|
| |
|
| if (sopt->sopt_size == 0) { |
|
| error = sockopt_alloc(sopt, len, KM_NOSLEEP); |
|
| if (error) |
|
| 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)); |
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: |
| |
*mtod(m, int *) = (so->so_options & optname) ? 1 : 0; |
| |
break; |
| |
|
| /* |
case SO_TYPE: |
| * set sockopt value from mbuf |
*mtod(m, int *) = so->so_type; |
| * - ONLY for legacy code |
break; |
| * - 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); |
case SO_ERROR: |
| |
*mtod(m, int *) = so->so_error; |
| |
so->so_error = 0; |
| |
break; |
| |
|
| if (sopt->sopt_size == 0) { |
case SO_SNDBUF: |
| error = sockopt_alloc(sopt, len, KM_NOSLEEP); |
*mtod(m, int *) = so->so_snd.sb_hiwat; |
| if (error) |
break; |
| return error; |
|
| } |
|
| |
|
| KASSERT(sopt->sopt_size == len); |
case SO_RCVBUF: |
| m_copydata(m, 0, len, sopt->sopt_data); |
*mtod(m, int *) = so->so_rcv.sb_hiwat; |
| m_freem(m); |
break; |
| |
|
| return 0; |
case SO_SNDLOWAT: |
| } |
*mtod(m, int *) = so->so_snd.sb_lowat; |
| |
break; |
| |
|
| /* |
case SO_RCVLOWAT: |
| * get sockopt value into mbuf |
*mtod(m, int *) = so->so_rcv.sb_lowat; |
| * - ONLY for legacy code |
break; |
| * - 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) |
case SO_SNDTIMEO: |
| return NULL; |
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; |
| |
} |
| |
|
| m = m_get(M_DONTWAIT, MT_SOOPTS); |
case SO_OVERFLOWED: |
| if (m == NULL) |
*mtod(m, int *) = so->so_rcv.sb_overflowed; |
| return NULL; |
break; |
| |
|
| if (sopt->sopt_size > MLEN) { |
default: |
| MCLGET(m, M_DONTWAIT); |
sounlock(so); |
| if ((m->m_flags & M_EXT) == 0) { |
(void)m_free(m); |
| m_free(m); |
return (ENOPROTOOPT); |
| return NULL; |
|
| } |
} |
| |
*mp = m; |
| |
error = 0; |
| } |
} |
| |
|
| memcpy(mtod(m, void *), sopt->sopt_data, sopt->sopt_size); |
sounlock(so); |
| m->m_len = sopt->sopt_size; |
return (error); |
| |
|
| return m; |
|
| } |
} |
| |
|
| void |
void |
| Line 2188 sohasoutofband(struct socket *so) |
|
| Line 1811 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, NOTE_SUBMIT); |
selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0); |
| } |
} |
| |
|
| static void |
static void |
| Line 2419 sysctl_kern_somaxkva(SYSCTLFN_ARGS) |
|
| Line 2042 sysctl_kern_somaxkva(SYSCTLFN_ARGS) |
|
| return (error); |
return (error); |
| } |
} |
| |
|
| static void |
SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup") |
| sysctl_kern_somaxkva_setup(void) |
|
| { |
{ |
| |
|
| KASSERT(socket_sysctllog == NULL); |
sysctl_createv(clog, 0, NULL, 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(&socket_sysctllog, 0, NULL, NULL, |
sysctl_createv(clog, 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 " |