This makes several tweaks to improve the logic which decides whether we're able to use the splice method for a new connection. * Rather than only calling tcp_splice_conn_from_sock() in pasta mode, we check for pasta mode within it, better localising the checks. * Previously if we got a connection from a non-loopback address we'd always fall back to the "tap" path, even if the connection was on a socket in the namespace. If we did get a non-loopback address on a namespace socket, something has gone wrong and the "tap" path certainly won't be able to handle it. Report the error and close, rather than passing it along to tap. Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au> --- inany.c | 1 - tcp.c | 3 +-- tcp_splice.c | 49 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/inany.c b/inany.c index edf0b055..eaf2755d 100644 --- a/inany.c +++ b/inany.c @@ -23,7 +23,6 @@ * * Return: On success, a non-null pointer to @dst, NULL on failure */ -/* cppcheck-suppress unusedFunction */ const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size) { const struct in_addr *v4 = inany_v4(src); diff --git a/tcp.c b/tcp.c index 8daefe99..052bf7cb 100644 --- a/tcp.c +++ b/tcp.c @@ -2733,8 +2733,7 @@ void tcp_listen_handler(struct ctx *c, union epoll_ref ref, if (s < 0) goto cancel; - if (c->mode == MODE_PASTA && - tcp_splice_conn_from_sock(c, ref.tcp_listen, &flow->tcp_splice, + if (tcp_splice_conn_from_sock(c, ref.tcp_listen, &flow->tcp_splice, s, (struct sockaddr *)&sa)) return; diff --git a/tcp_splice.c b/tcp_splice.c index abd698d4..3b438313 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -389,36 +389,51 @@ bool tcp_splice_conn_from_sock(const struct ctx *c, sa_family_t af; int s1, rc; - ASSERT(c->mode == MODE_PASTA); + if (c->mode != MODE_PASTA) + return false; inany_from_sockaddr(&src, &srcport, sa); - if (!inany_is_loopback(&src)) - return false; + af = inany_v4(&src) ? AF_INET : AF_INET6; - conn->flags = inany_v4(&src) ? 0 : SPLICE_V6; - af = CONN_V6(conn) ? AF_INET6 : AF_INET; + switch (ref.pif) { + case PIF_SPLICE: + if (!inany_is_loopback(&src)) { + char str[INANY_ADDRSTRLEN]; - if (setsockopt(s0, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) - flow_trace(conn, "failed to set TCP_QUICKACK on %i", s0); + flow_err(conn, "Bad source address %s for splice, closing", + inany_ntop(&src, str, sizeof(str))); - conn->f.type = FLOW_TCP_SPLICE; - conn->s[0] = s0; - conn->s[1] = -1; - conn->pipe[0][0] = conn->pipe[0][1] = -1; - conn->pipe[1][0] = conn->pipe[1][1] = -1; + /* We *don't* want to fall back to tap */ + flow_alloc_cancel((union flow *)conn); + return true; + } - if (ref.pif == PIF_SPLICE) { dstport += c->tcp.fwd_out.delta[dstport]; - s1 = tcp_conn_sock(c, af); - } else { - ASSERT(ref.pif == PIF_HOST); + break; - dstport += c->tcp.fwd_in.delta[dstport]; + case PIF_HOST: + if (!inany_is_loopback(&src)) + return false; + dstport += c->tcp.fwd_in.delta[dstport]; s1 = tcp_conn_sock_ns(c, af); + break; + + default: + return false; } + conn->f.type = FLOW_TCP_SPLICE; + conn->s[0] = s0; + conn->s[1] = -1; + conn->pipe[0][0] = conn->pipe[0][1] = -1; + conn->pipe[1][0] = conn->pipe[1][1] = -1; + conn->flags = af == AF_INET ? 0 : SPLICE_V6; + + if (setsockopt(s0, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) + flow_trace(conn, "failed to set TCP_QUICKACK on %i", s0); + if (s1 < 0) { flow_err(conn, "Couldn't open connectable socket for splice: %s", -- 2.43.0