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 rcsdiff: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.179 retrieving revision 1.215.4.3 diff -u -p -r1.179 -r1.215.4.3 --- src/sys/kern/uipc_socket.c 2009/01/14 18:09:00 1.179 +++ src/sys/kern/uipc_socket.c 2013/10/18 02:32:12 1.215.4.3 @@ -1,11 +1,11 @@ -/* $NetBSD: uipc_socket.c,v 1.179 2009/01/14 18:09:00 christos Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.215.4.3 2013/10/18 02:32:12 rmind Exp $ */ /*- - * Copyright (c) 2002, 2007, 2008 The NetBSD Foundation, Inc. + * Copyright (c) 2002, 2007, 2008, 2009 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. + * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -62,8 +62,16 @@ * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 */ +/* + * Socket operation routines. + * + * These routines are called by the routines in sys_socket.c or from a + * system process, and implement the semantics of socket operations by + * switching out to the protocol specific routines. + */ + #include -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.179 2009/01/14 18:09:00 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.215.4.3 2013/10/18 02:32:12 rmind Exp $"); #include "opt_compat_netbsd.h" #include "opt_sock_counters.h" @@ -92,16 +100,17 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_socket. #include #include #include +#include #ifdef COMPAT_50 #include -#define SO_OSNDTIMEO 0x1005 -#define SO_ORCVTIMEO 0x1006 +#include #endif -#include +#include +#include +#include -MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); MALLOC_DEFINE(M_SONAME, "soname", "socket name"); extern const struct fileops socketops; @@ -134,8 +143,6 @@ EVCNT_ATTACH_STATIC(sosend_kvalimit); #endif /* SOSEND_COUNTERS */ -static struct callback_entry sokva_reclaimerentry; - #if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR) int sock_loan_thresh = -1; #else @@ -143,7 +150,7 @@ int sock_loan_thresh = 4096; #endif static kmutex_t so_pendfree_lock; -static struct mbuf *so_pendfree; +static struct mbuf *so_pendfree = NULL; #ifndef SOMAXKVA #define SOMAXKVA (16 * 1024 * 1024) @@ -152,12 +159,15 @@ 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 sopendfree_thread(void *); +static kcondvar_t pendfree_thread_cv; +static lwp_t *sopendfree_lwp; -static void sysctl_kern_somaxkva_setup(void); +static void sysctl_kern_socket_setup(void); static struct sysctllog *socket_sysctllog; static vsize_t @@ -167,21 +177,6 @@ sokvareserve(struct socket *so, vsize_t mutex_enter(&so_pendfree_lock); while (socurkva + len > somaxkva) { - size_t freed; - - /* - * try to do pendfree. - */ - - freed = sodopendfreel(); - - /* - * if some kva was freed, try again. - */ - - if (freed) - continue; - SOSEND_COUNTER_INCR(&sosend_kvalimit); error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock); if (error) { @@ -209,7 +204,7 @@ sokvaunreserve(vsize_t len) */ vaddr_t -sokvaalloc(vsize_t len, struct socket *so) +sokvaalloc(vaddr_t sva, vsize_t len, struct socket *so) { vaddr_t lva; @@ -224,7 +219,8 @@ sokvaalloc(vsize_t len, struct socket *s * allocate kva. */ - lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA); + lva = uvm_km_alloc(kernel_map, len, atop(sva) & uvmexp.colormask, + UVM_KMF_COLORMATCH | UVM_KMF_VAONLY | UVM_KMF_WAITVA); if (lva == 0) { sokvaunreserve(len); return (0); @@ -274,56 +270,45 @@ sodoloanfree(struct vm_page **pgs, void sokvafree(sva, len); } -static size_t -sodopendfree(void) -{ - size_t rv; - - if (__predict_true(so_pendfree == NULL)) - return 0; - - mutex_enter(&so_pendfree_lock); - rv = sodopendfreel(); - mutex_exit(&so_pendfree_lock); - - return rv; -} - /* - * sodopendfreel: free mbufs on "pendfree" list. + * sopendfree_thread: free mbufs on "pendfree" list. * unlock and relock so_pendfree_lock when freeing mbufs. - * - * => called with so_pendfree_lock held. */ -static size_t -sodopendfreel(void) +static void +sopendfree_thread(void *v) { struct mbuf *m, *next; - size_t rv = 0; - - KASSERT(mutex_owned(&so_pendfree_lock)); + size_t rv; - while (so_pendfree != NULL) { - m = so_pendfree; - so_pendfree = NULL; - mutex_exit(&so_pendfree_lock); + mutex_enter(&so_pendfree_lock); - for (; m != NULL; m = next) { - next = m->m_next; - KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0); - KASSERT(m->m_ext.ext_refcnt == 0); + for (;;) { + rv = 0; + while (so_pendfree != NULL) { + m = so_pendfree; + so_pendfree = NULL; + mutex_exit(&so_pendfree_lock); + + for (; m != NULL; m = next) { + next = m->m_next; + KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0); + KASSERT(m->m_ext.ext_refcnt == 0); + + rv += m->m_ext.ext_size; + sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf, + m->m_ext.ext_size); + pool_cache_put(mb_cache, m); + } - rv += m->m_ext.ext_size; - sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf, - m->m_ext.ext_size); - pool_cache_put(mb_cache, m); + mutex_enter(&so_pendfree_lock); } - - mutex_enter(&so_pendfree_lock); + if (rv) + cv_broadcast(&socurkva_cv); + cv_wait(&pendfree_thread_cv, &so_pendfree_lock); } - - return (rv); + panic("sopendfree_thread"); + /* NOTREACHED */ } void @@ -342,7 +327,7 @@ soloanfree(struct mbuf *m, void *buf, si mutex_enter(&so_pendfree_lock); m->m_next = so_pendfree; so_pendfree = m; - cv_broadcast(&socurkva_cv); + cv_signal(&pendfree_thread_cv); mutex_exit(&so_pendfree_lock); } @@ -372,7 +357,7 @@ sosend_loan(struct socket *so, struct ui KASSERT(npgs <= M_EXT_MAXPAGES); - lva = sokvaalloc(len, so); + lva = sokvaalloc(sva, len, so); if (lva == 0) return 0; @@ -385,7 +370,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); + VM_PROT_READ, 0); pmap_update(pmap_kernel()); lva += (vaddr_t) iov->iov_base & PAGE_MASK; @@ -405,20 +390,6 @@ sosend_loan(struct socket *so, struct ui return (space); } -static int -sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg) -{ - - KASSERT(ce == &sokva_reclaimerentry); - KASSERT(obj == NULL); - - sodopendfree(); - if (!vm_map_starved_p(kernel_map)) { - return CALLBACK_CHAIN_ABORT; - } - return CALLBACK_CHAIN_CONTINUE; -} - struct mbuf * getsombuf(struct socket *so, int type) { @@ -429,33 +400,97 @@ 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) +{ + 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) == 0) + 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_OROUTE + || (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; +} + void soinit(void) { - sysctl_kern_somaxkva_setup(); + sysctl_kern_socket_setup(); mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); cv_init(&socurkva_cv, "sokva"); + cv_init(&pendfree_thread_cv, "sopendfr"); soinit2(); /* Set the initial adjusted socket buffer size. */ if (sb_max_set(sb_max)) panic("bad initial sb_max value: %lu", sb_max); - 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); +} + +void +soinit1(void) +{ + int error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, + sopendfree_thread, NULL, &sopendfree_lwp, "sopendfree"); + if (error) + panic("soinit1 %d", error); } /* - * Socket operation routines. - * These routines are called by the routines in - * sys_socket.c or from a system process, and - * implement the semantics of socket operations by - * switching out to the protocol specific routines. + * socreate: create a new socket of the specified type and the protocol. + * + * => Caller may specify another socket for lock sharing (must not be held). + * => Returns the new socket without lock held. */ -/*ARGSUSED*/ int socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l, struct socket *lockso) @@ -485,7 +520,7 @@ socreate(int dom, struct socket **aso, i return EPROTOTYPE; return EPROTONOSUPPORT; } - if (prp->pr_usrreq == NULL) + if (prp->pr_usrreqs == NULL) return EPROTONOSUPPORT; if (prp->pr_type != type) return EPROTOTYPE; @@ -493,6 +528,8 @@ socreate(int dom, struct socket **aso, i so = soget(true); so->so_type = type; so->so_proto = prp; + so->so_refcnt = 1; + so->so_send = sosend; so->so_receive = soreceive; #ifdef MBUFTRACE @@ -502,66 +539,112 @@ socreate(int dom, struct socket **aso, i #endif uid = kauth_cred_geteuid(l->l_cred); 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) { - /* Caller wants us to share a lock. */ + + /* + * Lock assigned and taken during pr_attach, unless we share + * the lock with another socket, e.g. socketpair(2) case. + */ + if (lockso) { lock = lockso->so_lock; so->so_lock = lock; mutex_obj_hold(lock); - mutex_enter(lock); - } else { - /* Lock assigned and taken during PRU_ATTACH. */ } - error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, - (struct mbuf *)(long)proto, NULL, l); - KASSERT(solocked(so)); - if (error != 0) { + + error = (*prp->pr_usrreqs->pr_attach)(so, proto); + if (error) { + solock(so); + KASSERT(so->so_pcb == NULL); so->so_state |= SS_NOFDREF; sofree(so); return error; } - sounlock(so); + so->so_cred = kauth_cred_dup(l->l_cred); + *aso = so; return 0; } -/* On success, write file descriptor to fdout and return zero. On - * failure, return non-zero; *fdout will be undefined. +/* + * fsocreate: create a socket and a file descriptor associated with it. + * + * => 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) +fsocreate(int domain, struct socket **sop, int type, int protocol, int *fdout) { - struct socket *so; - struct file *fp; - int fd, error; + lwp_t *l = curlwp; + int error, fd, flags; + struct socket *so; + struct file *fp; - if ((error = fd_allocfile(&fp, &fd)) != 0) - return (error); - fp->f_flag = FREAD|FWRITE; + if ((error = fd_allocfile(&fp, &fd)) != 0) { + return error; + } + + flags = type & SOCK_FLAGS_MASK; + fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); + fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| + ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); fp->f_type = DTYPE_SOCKET; fp->f_ops = &socketops; + + type &= ~SOCK_FLAGS_MASK; error = socreate(domain, &so, type, protocol, l, NULL); if (error != 0) { fd_abort(curproc, fp, fd); - } else { - if (sop != NULL) - *sop = so; - fp->f_data = so; - fd_affix(curproc, fp, fd); - *fdout = fd; + return error; + } + if (flags & SOCK_NONBLOCK) { + so->so_state |= SS_NBIO; } + + fp->f_data = so; + fd_affix(curproc, fp, fd); + if (sop != NULL) { + *sop = so; + } + *fdout = fd; return error; } +void +soref(struct socket *so) +{ + atomic_inc_uint(&so->so_refcnt); +} + +void +sounref(struct socket *so) +{ + if (atomic_dec_uint_nv(&so->so_refcnt) > 0) { + return; + } + soput(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; + int error; solock(so); - error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l); + error = (*so->so_proto->pr_usrreqs->pr_generic)(so, + PRU_BIND, NULL, nam, NULL, l); sounlock(so); return error; } @@ -569,15 +652,15 @@ sobind(struct socket *so, struct mbuf *n int solisten(struct socket *so, int backlog, struct lwp *l) { - int error; + int error; solock(so); if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) != 0) { - sounlock(so); - return (EOPNOTSUPP); + sounlock(so); + return (EINVAL); } - error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, + error = (*so->so_proto->pr_usrreqs->pr_generic)(so, PRU_LISTEN, NULL, NULL, NULL, l); if (error != 0) { sounlock(so); @@ -595,7 +678,6 @@ solisten(struct socket *so, int backlog, void sofree(struct socket *so) { - u_int refs; KASSERT(solocked(so)); @@ -625,26 +707,24 @@ sofree(struct socket *so) KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv)); 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); + + /* Will soput() if the last reference. */ + sounref(so); } /* - * Close a socket on last file table reference removal. - * Initiate disconnect if connected. - * Free socket when disconnect complete. + * soclose: close a socket on last file table reference removal. + * Initiate disconnect if connected. Free socket when disconnect complete. */ int soclose(struct socket *so) { - struct socket *so2; - int error; - int error2; + struct socket *so2; + int error; error = 0; solock(so); @@ -669,7 +749,7 @@ soclose(struct socket *so) break; } } - if (so->so_pcb == 0) + if (so->so_pcb == NULL) goto discard; if (so->so_state & SS_ISCONNECTED) { if ((so->so_state & SS_ISDISCONNECTING) == 0) { @@ -678,10 +758,11 @@ soclose(struct socket *so) goto drop; } if (so->so_options & SO_LINGER) { - if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio) + if ((so->so_state & (SS_ISDISCONNECTING|SS_NBIO)) == + (SS_ISDISCONNECTING|SS_NBIO)) goto drop; while (so->so_state & SS_ISCONNECTED) { - error = sowait(so, so->so_linger * hz); + error = sowait(so, true, so->so_linger * hz); if (error) break; } @@ -689,17 +770,14 @@ soclose(struct socket *so) } drop: if (so->so_pcb) { - error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, - NULL, NULL, NULL, NULL); - if (error == 0) - error = error2; + (*so->so_proto->pr_usrreqs->pr_detach)(so); } discard: - if (so->so_state & SS_NOFDREF) - panic("soclose: NOFDREF"); + KASSERT((so->so_state & SS_NOFDREF) == 0); + kauth_cred_free(so->so_cred); so->so_state |= SS_NOFDREF; sofree(so); - return (error); + return error; } /* @@ -710,19 +788,23 @@ soabort(struct socket *so) { u_int refs; int error; - + KASSERT(solocked(so)); KASSERT(so->so_head == NULL); - so->so_aborting++; /* XXX */ - error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, - NULL, NULL, NULL); - refs = --so->so_aborting; /* XXX */ - if (error || (refs == 0)) { + soref(so); + error = (*so->so_proto->pr_usrreqs->pr_generic)(so, + PRU_ABORT, NULL, NULL, NULL, NULL); + refs = so->so_refcnt; + sounref(so); + + /* XXX: Fix PRU_ABORT to behave consistently. */ + if (error || refs == 1) { sofree(so); } else { sounlock(so); } + sounref(so); return error; } @@ -734,13 +816,13 @@ soaccept(struct socket *so, struct mbuf KASSERT(solocked(so)); error = 0; - if ((so->so_state & SS_NOFDREF) == 0) - panic("soaccept: !NOFDREF"); + KASSERT((so->so_state & SS_NOFDREF) != 0); so->so_state &= ~SS_NOFDREF; + if ((so->so_state & SS_ISDISCONNECTED) == 0 || (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) - error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, - NULL, nam, NULL, NULL); + error = (*so->so_proto->pr_usrreqs->pr_generic)(so, + PRU_ACCEPT, NULL, nam, NULL, NULL); else error = ECONNABORTED; @@ -750,7 +832,7 @@ soaccept(struct socket *so, struct mbuf int soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) { - int error; + int error; KASSERT(solocked(so)); @@ -767,9 +849,10 @@ soconnect(struct socket *so, struct mbuf (error = sodisconnect(so)))) error = EISCONN; else - error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, - NULL, nam, NULL, l); - return (error); + error = (*so->so_proto->pr_usrreqs->pr_generic)(so, + PRU_CONNECT, NULL, nam, NULL, l); + + return error; } int @@ -779,7 +862,7 @@ soconnect2(struct socket *so1, struct so KASSERT(solocked2(so1, so2)); - error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, + error = (*so1->so_proto->pr_usrreqs->pr_generic)(so1, PRU_CONNECT2, NULL, (struct mbuf *)so2, NULL, NULL); return (error); } @@ -796,10 +879,9 @@ sodisconnect(struct socket *so) } else if (so->so_state & SS_ISDISCONNECTING) { error = EALREADY; } else { - error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, - NULL, NULL, NULL, NULL); + error = (*so->so_proto->pr_usrreqs->pr_generic)(so, + PRU_DISCONNECT, NULL, NULL, NULL, NULL); } - sodopendfree(); return (error); } @@ -826,12 +908,10 @@ sosend(struct socket *so, struct mbuf *a struct mbuf *control, int flags, struct lwp *l) { struct mbuf **mp, *m; - struct proc *p; long space, len, resid, clen, mlen; int error, s, dontroute, atomic; + short wakeup_state = 0; - p = l->l_proc; - sodopendfree(); clen = 0; /* @@ -898,16 +978,22 @@ sosend(struct socket *so, struct mbuf *a } if (space < resid + clen && (atomic || space < so->so_snd.sb_lowat || space < clen)) { - if (so->so_nbio) { + if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) { error = EWOULDBLOCK; 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 { @@ -942,7 +1028,7 @@ sosend(struct socket *so, struct mbuf *a } if (resid >= MINCLSIZE && space >= MCLBYTES) { SOSEND_COUNTER_INCR(&sosend_copy_big); - m_clget(m, M_WAIT); + m_clget(m, M_DONTWAIT); if ((m->m_flags & M_EXT) == 0) goto nopages; mlen = MCLBYTES; @@ -991,7 +1077,7 @@ sosend(struct socket *so, struct mbuf *a so->so_options |= SO_DONTROUTE; if (resid > 0) so->so_state |= SS_MORETOCOME; - error = (*so->so_proto->pr_usrreq)(so, + error = (*so->so_proto->pr_usrreqs->pr_generic)(so, (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, top, addr, control, curlwp); if (dontroute) @@ -1078,11 +1164,13 @@ soreceive(struct socket *so, struct mbuf { struct lwp *l = curlwp; struct mbuf *m, **mp, *mt; - int atomic, flags, len, error, s, offset, moff, type, orig_resid; + size_t len, offset, moff, orig_resid; + int atomic, flags, error, s, type; const struct protosw *pr; 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; @@ -1100,20 +1188,17 @@ soreceive(struct socket *so, struct mbuf else flags = 0; - if ((flags & MSG_DONTWAIT) == 0) - sodopendfree(); - if (flags & MSG_OOB) { m = m_get(M_WAIT, MT_DATA); solock(so); - error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, + error = (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVOOB, m, (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l); sounlock(so); if (error) goto bad; do { error = uiomove(mtod(m, void *), - (int) min(uio->uio_resid, m->m_len), uio); + MIN(uio->uio_resid, m->m_len), uio); m = m_free(m); } while (uio->uio_resid > 0 && error == 0 && m); bad: @@ -1132,7 +1217,7 @@ soreceive(struct socket *so, struct mbuf s = splsoftnet(); solock(so); if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) - (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l); + (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, NULL, NULL, NULL, l); restart: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) { @@ -1190,19 +1275,24 @@ soreceive(struct socket *so, struct mbuf } if (uio->uio_resid == 0) goto release; - if (so->so_nbio || (flags & MSG_DONTWAIT)) { + if ((so->so_state & SS_NBIO) || + (flags & (MSG_DONTWAIT|MSG_NBIO))) { error = EWOULDBLOCK; goto release; } SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); sbunlock(&so->so_rcv); - error = sbwait(&so->so_rcv); + if (wakeup_state & SS_RESTARTSYS) + error = ERESTART; + else + error = sbwait(&so->so_rcv); if (error != 0) { sounlock(so); splx(s); return error; } + wakeup_state = so->so_state; goto restart; } dontblock: @@ -1291,7 +1381,9 @@ soreceive(struct socket *so, struct mbuf type == SCM_RIGHTS) { sounlock(so); splx(s); - error = (*dom->dom_externalize)(cm, l); + error = (*dom->dom_externalize)(cm, l, + (flags & MSG_CMSG_CLOEXEC) ? + O_CLOEXEC : 0); s = splsoftnet(); solock(so); } @@ -1341,6 +1433,7 @@ 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; @@ -1359,7 +1452,7 @@ soreceive(struct socket *so, struct mbuf SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); sounlock(so); splx(s); - error = uiomove(mtod(m, char *) + moff, (int)len, uio); + error = uiomove(mtod(m, char *) + moff, len, uio); s = splsoftnet(); solock(so); if (error != 0) { @@ -1469,11 +1562,14 @@ soreceive(struct socket *so, struct mbuf * get it filled again. */ if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) - (*pr->pr_usrreq)(so, PRU_RCVD, + (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, NULL, (struct mbuf *)(long)flags, NULL, l); SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); - error = sbwait(&so->so_rcv); + if (wakeup_state & SS_RESTARTSYS) + error = ERESTART; + else + error = sbwait(&so->so_rcv); if (error != 0) { sbunlock(&so->so_rcv); sounlock(so); @@ -1482,6 +1578,7 @@ soreceive(struct socket *so, struct mbuf } if ((m = so->so_rcv.sb_mb) != NULL) nextrecord = m->m_nextpkt; + wakeup_state = so->so_state; } } @@ -1507,7 +1604,7 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) - (*pr->pr_usrreq)(so, PRU_RCVD, NULL, + (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, NULL, (struct mbuf *)(long)flags, NULL, l); } if (orig_resid == uio->uio_resid && orig_resid && @@ -1542,13 +1639,32 @@ soshutdown(struct socket *so, int how) error = 0; } if (how == SHUT_WR || how == SHUT_RDWR) - error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, + error = (*pr->pr_usrreqs->pr_generic)(so, PRU_SHUTDOWN, NULL, NULL, NULL, NULL); return error; } 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; @@ -1583,7 +1699,7 @@ sorflush(struct socket *so) static int sosetopt1(struct socket *so, const struct sockopt *sopt) { - int error = 0, optval, opt; + int error = EINVAL, optval, opt; struct linger l; struct timeval tv; @@ -1620,6 +1736,10 @@ sosetopt1(struct socket *so, const struc case SO_REUSEPORT: case SO_OOBINLINE: case SO_TIMESTAMP: + case SO_NOSIGPIPE: +#ifdef SO_OTIMESTAMP + case SO_OTIMESTAMP: +#endif error = sockopt_getint(sopt, &optval); solock(so); if (error) @@ -1690,9 +1810,13 @@ sosetopt1(struct socket *so, const struc 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 = 1; + error = 0; /*FALLTHROUGH*/ } #endif /* COMPAT_50 */ @@ -1813,6 +1937,10 @@ sogetopt1(struct socket *so, struct sock case SO_BROADCAST: case SO_OOBINLINE: case SO_TIMESTAMP: + case SO_NOSIGPIPE: +#ifdef SO_OTIMESTAMP + case SO_OTIMESTAMP: +#endif error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0); break; @@ -2069,7 +2197,7 @@ sohasoutofband(struct socket *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 @@ -2270,6 +2398,7 @@ sopoll(struct socket *so, int events) #include static int sysctl_kern_somaxkva(SYSCTLFN_PROTO); +static int sysctl_kern_sbmax(SYSCTLFN_PROTO); /* * sysctl helper routine for kern.somaxkva. ensures that the given @@ -2300,8 +2429,32 @@ sysctl_kern_somaxkva(SYSCTLFN_ARGS) return (error); } +/* + * sysctl helper routine for kern.sbmax. Basically just ensures that + * any new value is not too small. + */ +static int +sysctl_kern_sbmax(SYSCTLFN_ARGS) +{ + int error, new_sbmax; + struct sysctlnode node; + + new_sbmax = sb_max; + node = *rnode; + node.sysctl_data = &new_sbmax; + error = sysctl_lookup(SYSCTLFN_CALL(&node)); + if (error || newp == NULL) + return (error); + + KERNEL_LOCK(1, NULL); + error = sb_max_set(new_sbmax); + KERNEL_UNLOCK_ONE(NULL); + + return (error); +} + static void -sysctl_kern_somaxkva_setup() +sysctl_kern_socket_setup(void) { KASSERT(socket_sysctllog == NULL); @@ -2318,4 +2471,11 @@ sysctl_kern_somaxkva_setup() "used for socket buffers"), sysctl_kern_somaxkva, 0, NULL, 0, CTL_KERN, KERN_SOMAXKVA, CTL_EOL); + + sysctl_createv(&socket_sysctllog, 0, NULL, NULL, + CTLFLAG_PERMANENT|CTLFLAG_READWRITE, + CTLTYPE_INT, "sbmax", + SYSCTL_DESCR("Maximum socket buffer size"), + sysctl_kern_sbmax, 0, NULL, 0, + CTL_KERN, KERN_SBMAX, CTL_EOL); }