From owner-svn-src-head@freebsd.org Sat Sep 28 13:13:23 2019 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C4475126F99; Sat, 28 Sep 2019 13:13:23 +0000 (UTC) (envelope-from tuexen@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46gTcg4qfrz4Cqc; Sat, 28 Sep 2019 13:13:23 +0000 (UTC) (envelope-from tuexen@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8A7121E1C; Sat, 28 Sep 2019 13:13:23 +0000 (UTC) (envelope-from tuexen@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x8SDDNUp091652; Sat, 28 Sep 2019 13:13:23 GMT (envelope-from tuexen@FreeBSD.org) Received: (from tuexen@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x8SDDNix091651; Sat, 28 Sep 2019 13:13:23 GMT (envelope-from tuexen@FreeBSD.org) Message-Id: <201909281313.x8SDDNix091651@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: tuexen set sender to tuexen@FreeBSD.org using -f From: Michael Tuexen Date: Sat, 28 Sep 2019 13:13:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r352843 - head/sys/netinet X-SVN-Group: head X-SVN-Commit-Author: tuexen X-SVN-Commit-Paths: head/sys/netinet X-SVN-Commit-Revision: 352843 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 Sep 2019 13:13:23 -0000 Author: tuexen Date: Sat Sep 28 13:13:23 2019 New Revision: 352843 URL: https://svnweb.freebsd.org/changeset/base/352843 Log: Replacing MD5 by SipHash improves the performance of the TCP time stamp initialisation, which is important when the host is dealing with a SYN flood. This affects the computation of the initial TCP sequence number for the client side. This has been discussed with secteam@. Reviewed by: gallatin@ Sponsored by: Netflix, Inc. Differential Revision: https://reviews.freebsd.org/D21616 Modified: head/sys/netinet/tcp_subr.c Modified: head/sys/netinet/tcp_subr.c ============================================================================== --- head/sys/netinet/tcp_subr.c Sat Sep 28 13:05:37 2019 (r352842) +++ head/sys/netinet/tcp_subr.c Sat Sep 28 13:13:23 2019 (r352843) @@ -126,7 +126,7 @@ __FBSDID("$FreeBSD$"); #include #include -#include +#include #include @@ -242,7 +242,7 @@ VNET_DEFINE(uma_zone_t, sack_hole_zone); VNET_DEFINE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST+1]); #endif -#define TS_OFFSET_SECRET_LENGTH 32 +#define TS_OFFSET_SECRET_LENGTH SIPHASH_KEY_LENGTH VNET_DEFINE_STATIC(u_char, ts_offset_secret[TS_OFFSET_SECRET_LENGTH]); #define V_ts_offset_secret VNET(ts_offset_secret) @@ -2621,30 +2621,32 @@ out: static uint32_t tcp_keyed_hash(struct in_conninfo *inc, u_char *key, u_int len) { - MD5_CTX ctx; - uint32_t hash[4]; + SIPHASH_CTX ctx; + uint32_t hash[2]; - MD5Init(&ctx); - MD5Update(&ctx, &inc->inc_fport, sizeof(uint16_t)); - MD5Update(&ctx, &inc->inc_lport, sizeof(uint16_t)); + KASSERT(len >= SIPHASH_KEY_LENGTH, + ("%s: keylen %u too short ", __func__, len)); + SipHash24_Init(&ctx); + SipHash_SetKey(&ctx, (uint8_t *)key); + SipHash_Update(&ctx, &inc->inc_fport, sizeof(uint16_t)); + SipHash_Update(&ctx, &inc->inc_lport, sizeof(uint16_t)); switch (inc->inc_flags & INC_ISIPV6) { #ifdef INET case 0: - MD5Update(&ctx, &inc->inc_faddr, sizeof(struct in_addr)); - MD5Update(&ctx, &inc->inc_laddr, sizeof(struct in_addr)); + SipHash_Update(&ctx, &inc->inc_faddr, sizeof(struct in_addr)); + SipHash_Update(&ctx, &inc->inc_laddr, sizeof(struct in_addr)); break; #endif #ifdef INET6 case INC_ISIPV6: - MD5Update(&ctx, &inc->inc6_faddr, sizeof(struct in6_addr)); - MD5Update(&ctx, &inc->inc6_laddr, sizeof(struct in6_addr)); + SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(struct in6_addr)); + SipHash_Update(&ctx, &inc->inc6_laddr, sizeof(struct in6_addr)); break; #endif } - MD5Update(&ctx, key, len); - MD5Final((unsigned char *)hash, &ctx); + SipHash_Final((uint8_t *)hash, &ctx); - return (hash[0]); + return (hash[0] ^ hash[1]); } uint32_t @@ -2711,7 +2713,7 @@ tcp_new_ts_offset(struct in_conninfo *inc) #define ISN_BYTES_PER_SECOND 1048576 #define ISN_STATIC_INCREMENT 4096 #define ISN_RANDOM_INCREMENT (4096 - 1) -#define ISN_SECRET_LENGTH 32 +#define ISN_SECRET_LENGTH SIPHASH_KEY_LENGTH VNET_DEFINE_STATIC(u_char, isn_secret[ISN_SECRET_LENGTH]); VNET_DEFINE_STATIC(int, isn_last); @@ -2740,7 +2742,7 @@ tcp_new_isn(struct in_conninfo *inc) V_isn_last_reseed = ticks; } - /* Compute the md5 hash and return the ISN. */ + /* Compute the hash and return the ISN. */ new_isn = (tcp_seq)tcp_keyed_hash(inc, V_isn_secret, sizeof(V_isn_secret)); V_isn_offset += ISN_STATIC_INCREMENT +