Module Name:    src
Committed By:   tls
Date:           Mon May 23 13:54:34 UTC 2016

Modified Files:
        src/sys/kern: uipc_socket2.c

Log Message:
Fix a longstanding problem with accept filters noticed by Timo Buhrmester:
sockets sitting in the accept filter can consume the entire listen queue,
such that the application is never able to handle any connections.  Handle
this by simply passing through the oldest queued cxn when the queue is full.

This is fair because the longer a cxn lingers in the queue (stays connected
but does not meet the requirements of the filter for passage) the more likely
it is to be passed through, at which point the application can dispose of it.

Works because none of our accept filters actually allocate private state
per-cxn.  If they did, we'd have to fix the API bug that there is presently
no way to tell an accf to finish/deallocate for a single cxn (accf_destroy
kills off the entire filter instance for a given listen socket).


To generate a diff of this commit:
cvs rdiff -u -r1.122 -r1.123 src/sys/kern/uipc_socket2.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/uipc_socket2.c
diff -u src/sys/kern/uipc_socket2.c:1.122 src/sys/kern/uipc_socket2.c:1.123
--- src/sys/kern/uipc_socket2.c:1.122	Mon Aug 24 22:21:26 2015
+++ src/sys/kern/uipc_socket2.c	Mon May 23 13:54:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_socket2.c,v 1.122 2015/08/24 22:21:26 pooka Exp $	*/
+/*	$NetBSD: uipc_socket2.c,v 1.123 2016/05/23 13:54:34 tls Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.122 2015/08/24 22:21:26 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.123 2016/05/23 13:54:34 tls Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_mbuftrace.h"
@@ -262,8 +262,37 @@ sonewconn(struct socket *head, bool sore
 	KASSERT(solocked(head));
 
 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) {
-		/* Listen queue overflow. */
-		return NULL;
+		/*
+		 * Listen queue overflow.  If there is an accept filter
+		 * active, pass through the oldest cxn it's handling.
+		 */
+		if (head->so_accf == NULL) {
+			return NULL;
+		} else {
+			struct socket *so2, *next;
+
+			/* Pass the oldest connection waiting in the
+			   accept filter */
+			for (so2 = TAILQ_FIRST(&head->so_q0);
+			     so2 != NULL; so2 = next) {
+				next = TAILQ_NEXT(so2, so_qe);
+				if (so2->so_upcall == NULL) {
+					continue;
+				}
+				so2->so_upcall = NULL;
+				so2->so_upcallarg = NULL;
+				so2->so_options &= ~SO_ACCEPTFILTER;
+				so2->so_rcv.sb_flags &= ~SB_UPCALL;
+				soisconnected(so2);
+				break;
+			}
+
+			/* If nothing was nudged out of the acept filter, bail
+			 * out; otherwise proceed allocating the socket. */
+			if (so2 == NULL) {
+				return NULL;
+			}
+		}
 	}
 	if ((head->so_options & SO_ACCEPTFILTER) != 0) {
 		soready = false;

Reply via email to