Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v retrieving revision 1.160.2.4 retrieving revision 1.161 diff -u -p -r1.160.2.4 -r1.161 --- src/sys/kern/uipc_socket.c 2010/03/11 15:04:20 1.160.2.4 +++ src/sys/kern/uipc_socket.c 2008/04/27 14:26:58 1.161 @@ -1,11 +1,11 @@ -/* $NetBSD: uipc_socket.c,v 1.160.2.4 2010/03/11 15:04:20 yamt Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.161 2008/04/27 14:26:58 ad Exp $ */ /*- - * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. + * Copyright (c) 2002, 2007, 2008 The NetBSD Foundation, Inc. * All rights reserved. * * 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 * modification, are permitted provided that the following conditions @@ -15,6 +15,13 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * 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 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED @@ -63,21 +70,19 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.160.2.4 2010/03/11 15:04:20 yamt Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.161 2008/04/27 14:26:58 ad Exp $"); -#include "opt_compat_netbsd.h" #include "opt_sock_counters.h" #include "opt_sosend_loan.h" #include "opt_mbuftrace.h" #include "opt_somaxkva.h" -#include "opt_multiprocessor.h" /* XXX */ #include #include #include #include #include -#include +#include #include #include #include @@ -86,18 +91,12 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_socket. #include #include #include -#include #include #include #include #include #include -#ifdef COMPAT_50 -#include -#include -#endif - #include MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); @@ -135,7 +134,7 @@ EVCNT_ATTACH_STATIC(sosend_kvalimit); static struct callback_entry sokva_reclaimerentry; -#if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR) +#ifdef SOSEND_NO_LOAN int sock_loan_thresh = -1; #else int sock_loan_thresh = 4096; @@ -151,16 +150,11 @@ int somaxkva = SOMAXKVA; static int socurkva; static kcondvar_t socurkva_cv; -static kauth_listener_t socket_listener; - #define SOCK_LOAN_CHUNK 65536 static size_t sodopendfree(void); static size_t sodopendfreel(void); -static void sysctl_kern_somaxkva_setup(void); -static struct sysctllog *socket_sysctllog; - static vsize_t sokvareserve(struct socket *so, vsize_t len) { @@ -386,7 +380,7 @@ sosend_loan(struct socket *so, struct ui 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]), - VM_PROT_READ, 0); + VM_PROT_READ); pmap_update(pmap_kernel()); lva += (vaddr_t) iov->iov_base & PAGE_MASK; @@ -430,71 +424,24 @@ getsombuf(struct socket *so, int type) return m; } -static int -socket_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, - void *arg0, void *arg1, void *arg2, void *arg3) +struct mbuf * +m_intopt(struct socket *so, int val) { - int result; - 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; - } + struct mbuf *m; - return result; + m = getsombuf(so, MT_SOOPTS); + m->m_len = sizeof(int); + *mtod(m, int *) = val; + return m; } void soinit(void) { - sysctl_kern_somaxkva_setup(); - mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); cv_init(&socurkva_cv, "sokva"); - soinit2(); /* Set the initial adjusted socket buffer size. */ if (sb_max_set(sb_max)) @@ -502,9 +449,6 @@ soinit(void) callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback, &sokva_reclaimerentry, NULL, sokva_reclaim_callback); - - socket_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK, - socket_listener_cb, NULL); } /* @@ -561,7 +505,6 @@ socreate(int dom, struct socket **aso, i #endif uid = kauth_cred_geteuid(l->l_cred); so->so_uidinfo = uid_find(uid); - so->so_cpid = l->l_proc->p_pid; if (lockso != NULL) { /* Caller wants us to share a lock. */ lock = lockso->so_lock; @@ -579,7 +522,6 @@ socreate(int dom, struct socket **aso, i sofree(so); return error; } - so->so_cred = kauth_cred_dup(l->l_cred); sounlock(so); *aso = so; return 0; @@ -615,19 +557,6 @@ fsocreate(int domain, struct socket **so } 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) { int error; @@ -645,10 +574,8 @@ solisten(struct socket *so, int backlog, solock(so); if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | - SS_ISDISCONNECTING)) != 0) { - sounlock(so); + SS_ISDISCONNECTING)) != 0) return (EOPNOTSUPP); - } error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL, l); if (error != 0) { @@ -698,9 +625,6 @@ sofree(struct socket *so) KASSERT(!cv_has_waiters(&so->so_snd.sb_cv)); 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); if (refs == 0) /* XXX */ soput(so); @@ -721,7 +645,7 @@ soclose(struct socket *so) error = 0; solock(so); if (so->so_options & SO_ACCEPTCONN) { - for (;;) { + do { if ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) { KASSERT(solocked2(so, so2)); (void) soqremque(so2, 0); @@ -738,8 +662,7 @@ soclose(struct socket *so) solock(so); continue; } - break; - } + } while (0); } if (so->so_pcb == 0) goto discard; @@ -753,7 +676,7 @@ soclose(struct socket *so) if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio) goto drop; while (so->so_state & SS_ISCONNECTED) { - error = sowait(so, true, so->so_linger * hz); + error = sowait(so, so->so_linger * hz); if (error) break; } @@ -769,7 +692,6 @@ soclose(struct socket *so) discard: if (so->so_state & SS_NOFDREF) panic("soclose: NOFDREF"); - kauth_cred_free(so->so_cred); so->so_state |= SS_NOFDREF; sofree(so); return (error); @@ -791,10 +713,12 @@ soabort(struct socket *so) error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, NULL, NULL, NULL); refs = --so->so_aborting; /* XXX */ - if (error || (refs == 0)) { + if (error) { sofree(so); } else { sounlock(so); + if (refs == 0) + sofree(so); } return error; } @@ -902,7 +826,6 @@ sosend(struct socket *so, struct mbuf *a struct proc *p; long space, len, resid, clen, mlen; int error, s, dontroute, atomic; - short wakeup_state = 0; p = l->l_proc; sodopendfree(); @@ -934,7 +857,8 @@ sosend(struct socket *so, struct mbuf *a dontroute = (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && (so->so_proto->pr_flags & PR_ATOMIC); - l->l_ru.ru_msgsnd++; + if (l) + l->l_ru.ru_msgsnd++; if (control) clen = control->m_len; restart: @@ -977,17 +901,11 @@ sosend(struct socket *so, struct mbuf *a goto release; } sbunlock(&so->so_snd); - if (wakeup_state & SS_RESTARTSYS) { - error = ERESTART; - goto out; - } error = sbwait(&so->so_snd); if (error) goto out; - wakeup_state = so->so_state; goto restart; } - wakeup_state = 0; mp = ⊤ space -= clen; do { @@ -1163,7 +1081,6 @@ soreceive(struct socket *so, struct mbuf struct mbuf *nextrecord; int mbuf_removed = 0; const struct domain *dom; - short wakeup_state = 0; pr = so->so_proto; atomic = pr->pr_flags & PR_ATOMIC; @@ -1278,16 +1195,12 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); sbunlock(&so->so_rcv); - if (wakeup_state & SS_RESTARTSYS) - error = ERESTART; - else - error = sbwait(&so->so_rcv); + error = sbwait(&so->so_rcv); if (error != 0) { sounlock(so); splx(s); return error; } - wakeup_state = so->so_state; goto restart; } dontblock: @@ -1426,7 +1339,6 @@ soreceive(struct socket *so, struct mbuf panic("receive 3"); #endif so->so_state &= ~SS_RCVATMARK; - wakeup_state = 0; len = uio->uio_resid; if (so->so_oobmark && len > so->so_oobmark - offset) len = so->so_oobmark - offset; @@ -1559,10 +1471,7 @@ soreceive(struct socket *so, struct mbuf NULL, (struct mbuf *)(long)flags, NULL, l); SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); - if (wakeup_state & SS_RESTARTSYS) - error = ERESTART; - else - error = sbwait(&so->so_rcv); + error = sbwait(&so->so_rcv); if (error != 0) { sbunlock(&so->so_rcv); sounlock(so); @@ -1571,7 +1480,6 @@ soreceive(struct socket *so, struct mbuf } if ((m = so->so_rcv.sb_mb) != NULL) nextrecord = m->m_nextpkt; - wakeup_state = so->so_state; } } @@ -1639,25 +1547,6 @@ soshutdown(struct socket *so, int how) } 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) { struct sockbuf *sb, asb; @@ -1686,40 +1575,30 @@ sorflush(struct socket *so) sbrelease(&asb, so); } -/* - * internal set SOL_SOCKET options - */ 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; - struct linger l; - struct timeval tv; + int optval, val; + struct linger *l; + struct sockbuf *sb; + struct timeval *tv; - switch ((opt = sopt->sopt_name)) { + switch (optname) { - case SO_ACCEPTFILTER: - error = accept_filt_setopt(so, sopt); - KASSERT(solocked(so)); + case SO_LINGER: + if (m == NULL || m->m_len != sizeof(struct linger)) + 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; - 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_KEEPALIVE: case SO_DONTROUTE: @@ -1729,52 +1608,38 @@ sosetopt1(struct socket *so, const struc case SO_REUSEPORT: case SO_OOBINLINE: case SO_TIMESTAMP: -#ifdef SO_OTIMESTAMP - case SO_OTIMESTAMP: -#endif - error = sockopt_getint(sopt, &optval); - solock(so); - if (error) - break; - if (optval) - so->so_options |= opt; + if (m == NULL || m->m_len < sizeof(int)) + return EINVAL; + if (*mtod(m, int *)) + so->so_options |= optname; else - so->so_options &= ~opt; + so->so_options &= ~optname; break; case SO_SNDBUF: case SO_RCVBUF: case SO_SNDLOWAT: case SO_RCVLOWAT: - error = sockopt_getint(sopt, &optval); - solock(so); - if (error) - break; + if (m == NULL || m->m_len < sizeof(int)) + return EINVAL; /* * Values < 1 make no sense for any of these * options, so disallow them. */ - if (optval < 1) { - error = EINVAL; - break; - } + optval = *mtod(m, int *); + if (optval < 1) + return EINVAL; - switch (opt) { - case SO_SNDBUF: - if (sbreserve(&so->so_snd, (u_long)optval, so) == 0) { - error = ENOBUFS; - break; - } - so->so_snd.sb_flags &= ~SB_AUTOSIZE; - break; + switch (optname) { + case SO_SNDBUF: case SO_RCVBUF: - if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0) { - error = ENOBUFS; - break; - } - so->so_rcv.sb_flags &= ~SB_AUTOSIZE; + 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; /* @@ -1782,405 +1647,163 @@ sosetopt1(struct socket *so, const struc * the high-water. */ case SO_SNDLOWAT: - if (optval > so->so_snd.sb_hiwat) - optval = so->so_snd.sb_hiwat; - - so->so_snd.sb_lowat = optval; + so->so_snd.sb_lowat = + (optval > so->so_snd.sb_hiwat) ? + so->so_snd.sb_hiwat : optval; break; - case SO_RCVLOWAT: - if (optval > so->so_rcv.sb_hiwat) - optval = so->so_rcv.sb_hiwat; - - so->so_rcv.sb_lowat = optval; + so->so_rcv.sb_lowat = + (optval > so->so_rcv.sb_hiwat) ? + so->so_rcv.sb_hiwat : optval; 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_RCVTIMEO: - if (error) - error = sockopt_get(sopt, &tv, sizeof(tv)); - solock(so); - if (error) - break; - - if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz) { - error = EDOM; - break; - } + 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; - optval = tv.tv_sec * hz + tv.tv_usec / tick; - if (optval == 0 && tv.tv_usec != 0) - optval = 1; + switch (optname) { - switch (opt) { case SO_SNDTIMEO: - so->so_snd.sb_timeo = optval; + so->so_snd.sb_timeo = val; break; case SO_RCVTIMEO: - so->so_rcv.sb_timeo = optval; + so->so_rcv.sb_timeo = val; break; } break; default: - solock(so); - error = ENOPROTOOPT; - break; + return ENOPROTOOPT; } - KASSERT(solocked(so)); - return error; + return 0; } int -sosetopt(struct socket *so, struct sockopt *sopt) +sosetopt(struct socket *so, int level, int optname, struct mbuf *m) { int error, prerr; - if (sopt->sopt_level == SOL_SOCKET) { - error = sosetopt1(so, sopt); - KASSERT(solocked(so)); - } else { + solock(so); + if (level == SOL_SOCKET) + error = sosetopt1(so, level, optname, m); + else error = ENOPROTOOPT; - solock(so); - } if ((error == 0 || error == ENOPROTOOPT) && so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { /* 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) error = 0; else if (prerr != ENOPROTOOPT) error = prerr; - } + } else if (m != NULL) + (void)m_free(m); sounlock(so); 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 -sogetopt(struct socket *so, struct sockopt *sopt) +sogetopt(struct socket *so, int level, int optname, struct mbuf **mp) { + struct mbuf *m; int error; solock(so); - if (sopt->sopt_level != SOL_SOCKET) { + if (level != SOL_SOCKET) { if (so->so_proto && so->so_proto->pr_ctloutput) { error = ((*so->so_proto->pr_ctloutput) - (PRCO_GETOPT, so, sopt)); + (PRCO_GETOPT, so, level, optname, mp)); } else error = (ENOPROTOOPT); } else { - error = sogetopt1(so, sopt); - } - 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); -} + m = m_get(M_WAIT, MT_SOOPTS); + m->m_len = sizeof(int); -/* - * 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); + switch (optname) { - memset(sopt, 0, sizeof(*sopt)); -} - -/* - * set sockopt value - * - 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; - - 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) -{ + case SO_LINGER: + 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; - 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; -/* - * 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; + case SO_TYPE: + *mtod(m, int *) = so->so_type; + break; - len = m_length(m); + case SO_ERROR: + *mtod(m, int *) = so->so_error; + so->so_error = 0; + break; - if (sopt->sopt_size == 0) { - error = sockopt_alloc(sopt, len, KM_NOSLEEP); - if (error) - return error; - } + case SO_SNDBUF: + *mtod(m, int *) = so->so_snd.sb_hiwat; + break; - KASSERT(sopt->sopt_size == len); - m_copydata(m, 0, len, sopt->sopt_data); - m_freem(m); + case SO_RCVBUF: + *mtod(m, int *) = so->so_rcv.sb_hiwat; + break; - return 0; -} + case SO_SNDLOWAT: + *mtod(m, int *) = so->so_snd.sb_lowat; + break; -/* - * 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; + case SO_RCVLOWAT: + *mtod(m, int *) = so->so_rcv.sb_lowat; + break; - if (sopt->sopt_size > MCLBYTES) - return NULL; + case SO_SNDTIMEO: + 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); - if (m == NULL) - return NULL; + case SO_OVERFLOWED: + *mtod(m, int *) = so->so_rcv.sb_overflowed; + break; - if (sopt->sopt_size > MLEN) { - MCLGET(m, M_DONTWAIT); - if ((m->m_flags & M_EXT) == 0) { - m_free(m); - return NULL; + default: + sounlock(so); + (void)m_free(m); + return (ENOPROTOOPT); } + *mp = m; + error = 0; } - memcpy(mtod(m, void *), sopt->sopt_data, sopt->sopt_size); - m->m_len = sopt->sopt_size; - - return m; + sounlock(so); + return (error); } void @@ -2188,7 +1811,7 @@ sohasoutofband(struct socket *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 @@ -2419,18 +2042,16 @@ sysctl_kern_somaxkva(SYSCTLFN_ARGS) return (error); } -static void -sysctl_kern_somaxkva_setup(void) +SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup") { - KASSERT(socket_sysctllog == NULL); - sysctl_createv(&socket_sysctllog, 0, NULL, NULL, + sysctl_createv(clog, 0, NULL, NULL, CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL, NULL, 0, NULL, 0, CTL_KERN, CTL_EOL); - sysctl_createv(&socket_sysctllog, 0, NULL, NULL, + sysctl_createv(clog, 0, NULL, NULL, CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "somaxkva", SYSCTL_DESCR("Maximum amount of kernel memory to be "