From patchwork Wed Aug 14 10:31:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972336 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=th/Vd87P; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPhK5p09z1yYl for ; Wed, 14 Aug 2024 20:32:17 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B08B3889BA; Wed, 14 Aug 2024 12:32:09 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="th/Vd87P"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AA37B889B4; Wed, 14 Aug 2024 12:32:06 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on20717.outbound.protection.outlook.com [IPv6:2a01:111:f403:2608::717]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 94616889BD for ; Wed, 14 Aug 2024 12:32:03 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=kEw524krzbsZ+4NAgXeBGfFw0zhCGt3mCMTCeSn0cHeyAloyqOFJHtO+42xkQT2FubEhnTUed9qzsI5qDeF0ahAPzNhA+xw1FvbQ+KbOA4WiwuD8ZSo00lkRrV7xa3gospz5TcsCmXX6ActxIXrgKLn9bfmYJgXCy52mH8r1XzaSHAkGqOwSi8PRZ/0Jw6DoezrVCjFAVCxkwloxRG22wFz6WYGG6c+r0C+sPXs5tLF+oD+z2yVZn8JoLijn0PBUe4uSN/yZd45ti1cUVD0ph6sVeMUHz7Q409mgivvlc/1FFu+F6brxiwKaGZXsYj6yEUCmNR1uWRqUlmCOJ8TBvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=oDh9nmeaVX1B03YZeUmx1GHxUxxjZR20kqyfubesKzA=; b=Ef82S9gxIIVvmanjqrMo5PTfK5OAyrpk2L9IMowXxjUPkDSMimXZdXuM2E+s0pksfrB6pUqhM9Fu/g+DRB/Owx00mQg9JPZjTR9+mHsxjKfmYxtAg1N40SADtfKZ+/0y9/2gqckAZl8xNM5a084lD23/ds2oaT9ONdnnwUDdAgfX8llYVtTS2Q25ZwOzASOgGjHugb80mG2V3LizkryhgLroKco5OUgzOWeF9rqOPnAtoIp+Lk1ADyc/wtDPOIJZHJHr0fu09rx71tBf2QOpAJqK3KaDNZGkxLbureEMA0OKo3/CWvI7O7JzVATY9L9x+GusjEekjEnfgrPVj2Fshw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=oDh9nmeaVX1B03YZeUmx1GHxUxxjZR20kqyfubesKzA=; b=th/Vd87PJO3FYN6jwb2h4nIdjPwbYjSdger4ENPwhCSIdEyjPuKlzwN6yMRiGh62L7sBIS5YUT9XBoFCqg5CCinkWn4nHFSMYl7CBX5mMe8cUCSEyx7/++JPQ8YEWinq151kIy8KyduCFtWWGGIuhpdwwLXjpbTsL3RUFwrZ1eBYKEDcUIJK0YHAlOC72JGNQouC0SIRjRAzi9uzXJisCjhNn2h3rJ/tFN68RlBvpdbpVYC7hUz1/WedV9GcSpk1liBPlVuuTNx9N93qI8s/W7gBg8gGT0kupRVgSj/BNbAwc4BPU9kVDhfz5ef8UkLW1QKniYe+4ksadRMb6L0c9w== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by PAWPR08MB8887.eurprd08.prod.outlook.com (2603:10a6:102:33b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.13; Wed, 14 Aug 2024 10:32:01 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:01 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 1/9] net/tcp: fix TCP options processing Date: Wed, 14 Aug 2024 14:31:37 +0400 Message-Id: <20240814103145.1347645-2-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|PAWPR08MB8887:EE_ X-MS-Office365-Filtering-Correlation-Id: 45212e11-63e2-4d02-82e1-08dcbc4c54f4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|7416014|52116014|376014|38350700014|921020; X-Microsoft-Antispam-Message-Info: Nk5EpYAKWz4MIuPW6bT/Ki1BAFKeKemBeZjt3upTaHYANA7nz7t9OBKfh+UV7z1zr+toaIVXJFqLFOQtx3+6rhnMuGHX000rgX/wUHOMXLOlLasLH1vZ2zYqu8lBKIGwSwyQEP/JorPXvrvY6I0gnQHqpLnXlfUK1PCXMn18Kjmh6IWgu2HtQl3jH3tHS+2QC6UYR+zpYmZsDoanAJSLFB94IccTOu/d1ddkufGF9LxgivX3utlgjwq3lccH1PmDrWa68QHgYBjYd0DX03Ba9z9+To2EsmPzIaE1AlDY7hC0MXIe1HwpOodvdMOrPBhSMMswFFMg1S8AJHwxFVQIIL5HIaOZul18KIEYA+fwZ/fk9lnhqAcpPpsLFPDs2sZFBSzsaKH/8Ph79lvYsuDPXUF4HcT/JQAEO410NNRT8KVb/W3GaVvJA9orvfxDzE1bqg0w8vOB8OreskyXLF1mwbf4+yrwWfHcnqy4Iqk3QWvF3yvDUIMChN4R52dZtZBsLg76UCzuvCtTTRqo1mgPft1Qkg0GDOQWOzwIDMs9MDfAUXJ8A8RS7K8iGk1z+AfSZZULMy6NBp+8WAL8LSt4X5Wr4BeA6AQWBbiE/dxGjqD5aUrjs2lIVJZyylSWYEIikzXg/MKk7jMCICaqoALFXm4ZnR0BacmofdK8lBYIe+qHzTGNOAuYucu6uIEiSj03kNw00MOgGJ3cx4qeEeC+7aJHRKF224HfM3pKwY4G5A8= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(7416014)(52116014)(376014)(38350700014)(921020); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SzM9Ao1qA+iBD8WG52yaQRlbwiqC1kdymPUgzIYoWfmIjMKtBJ5b1S82GVApJrT++QirwL1gEYbIlpAvI1HMamFUOKykwWhLYJkfqR5ldQX3mZJJi+klXBgQUZSevuILuI7x2IS4dPluf/3U1YZiAc5tr4nTYhzo/IcKzfVvpi8ah/mi8STuGPYDOoAsVH+ttFIxPLamxvICLvwdoqS3L9d8HCKwPTn4LZNRHosjIVM8ACRWxlVQ0qveQEOBcKmRaq0RoUKtCxt/JUSOnJj3D8k6hdGrJsHkLLHy+8xcfkWkT2EkxahWCY/wPtcQLV7GsXH/y8Tcp9Cf9XUhoTQyHCHxek9f4pMcey/azESxKaD3wRz3HTkAUpOBwYYcVvxE00b6SkGxQS/hOLLUlzpezBRt2YONq3TMeiafU5yMh/zIjNnf6ZFzQworMgl50x5Zv1MTySI+TuFroqj7u6mPffoY5n8bPy1Th4MPTKYL8TtY3jVrM+OpDCjEP0FFrSPQy4lhoCVqOpdnTDtkYt4AvMBc11y7DKGhdvZ0cim6kxEERPLa7dvDOYXRbnvC04UCzXis1U2uxi5xDdlMO+4tvhvA4crsFBcDX8iTReCPSu6GIa/aycy7GaEn2Rt/Ay96jfddstavCr1n7QGvydWNBMVsd2ibDZhuApaz6JjvZkA3E7Nbsi2h2PcSKQ3GGfKsbYJGuauiQIBO3tejDXwLy+5bZ3ja9buKqpF4gEZR3P6pOlQi4soQzXZK7kZhGNO4xI/sn16dMMC+v11sPzoQwHb0uHO0/A0T89wvA6M5ZaVrrwqCHpVCyfkE+THhuwQ5M2zacDlRE3yqggLfqGdwuTuybrOtI1W7ZLiPeXA1jnrSTP80ga3dYcBf48zrWSUdHxCkcmsF7TkljS1YQVB8YTZekSzgSjKZScOTx8q5PG2dAfL9U8HC4xC+plXOR+9VbYj0QWeamxbW1nvTW0LlPRaS3XQXbqmjHH6o0ueQqRz8SGXFYFIprMy0Ah7fsls1GozL+JwBdZefBTK3BkD1yUOC2tteowAgSv4cDVI2icIBIJMM5P3O6szAE2jdih2dJi2Nv+QKxocHru+FunciGkZFP89vGJdQnJF1VaFmC3c5tjTrrpVU7ZJzZ8KW6E4dgcCTM+5Wuqfum40c/WpeInXzXYkTRNnbEnfo/Ybh0ubh8YTMnZV/sxQz5qo3vvkopk+QyZ90PeLCuVij+7N+wi0St+Fn+9Lc0+McQ/3FmbdpTOk8gOwACqZWh+2fCVO+ZXZvh7PbHFxRTtlKSmGi1u2go3bcCciJXJfC7lYzG+0OPwJAbBfJXnDtcwwEsXlYA8t5hdoIs8OoZTdvqSMyI9pvFDtywI6bo4OuNN5r/XZl7MSpumrWmt6JJCLDRKtnoyRRXT6cu/Y7zsAktXcCnT3oi/tRQTIZEJ/RuI3zBx3lLVucgdscDIN+ONBdSFjCgG1i1nuTpSRVwTKnTmMSKCFGJvTJgMlQHneo6I1QWBPnVIa/IFD0wuIJ6MI0NXHo2WWTJoIgZE7s1datZ+igiBlPBmZ24gNmSB+MAIlKECpVN1OQgMnKjLIUP12vGJaPbZtXOLMwQHEMf0+YUcmO9g== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: 45212e11-63e2-4d02-82e1-08dcbc4c54f4 X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:01.0548 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kWzJt1lp3NlP2wVjkf/wZDIYoR1ygm4XTxZFY4MavtnXIwEpTg8ZcHTpmedA4AQ4uMnuuVOAD7Lf4scHJVHMndmoB1ps6uyQksz1v3p+Jio= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB8887 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Current TCP code may miss an option if TCP_O_NOP option was used before it for proper aligning. Signed-off-by: Mikhail Kshevetskiy Reviewed-by: Simon Glass --- net/tcp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/net/tcp.c b/net/tcp.c index b0cc8a1fe3e..3e3118de450 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -475,7 +475,7 @@ void tcp_parse_options(uchar *o, int o_len) * NOPs are options with a zero length, and thus are special. * All other options have length fields. */ - for (p = o; p < (o + o_len); p = p + p[1]) { + for (p = o; p < (o + o_len); ) { if (!p[1]) return; /* Finished processing options */ @@ -490,12 +490,14 @@ void tcp_parse_options(uchar *o, int o_len) case TCP_O_TS: tsopt = (struct tcp_t_opt *)p; rmt_timestamp = tsopt->t_snd; - return; + break; } /* Process optional NOPs */ if (p[0] == TCP_O_NOP) p++; + else + p += p[1]; } } From patchwork Wed Aug 14 10:31:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972337 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=0Xjcdb0d; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPhV4FlMz1yYl for ; Wed, 14 Aug 2024 20:32:26 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 19B94889E1; Wed, 14 Aug 2024 12:32:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="0Xjcdb0d"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EEA0C889B4; Wed, 14 Aug 2024 12:32:07 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on20703.outbound.protection.outlook.com [IPv6:2a01:111:f403:2606::703]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DA355889A1 for ; Wed, 14 Aug 2024 12:32:05 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jtnQiKJFgj8lvcwLjfXDQ/Yvyl3D7PdcloRg8/6ieX9M3PqITEQRPK7/Z8zkbQeAjDnwtYJS7LB3AC9SJslRtnpaeabv/yi46JzXzkvHhDm8GW8pQbOeR1WHcMdCo6pe+0Me62O0L4khmEEtQAMRTmKytxS7VrPSWgEFuCDTBTlYMwANGWBGOcfN3huxaW7chpg9qXNl7RqrAEL8YhTBzYHTeZGtnL0qvAEyOha5O3n/etDWEeAVz+1FTQSHQ6ofUKi2J/4jU6NxEFXy4GH7CKh4DW7FLM8W9mqAExt2VYe3OMWBwpWSp1LS1OEW8jxg7PF++BUQkhZ0q/u7XCrx6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=OaHOLg157m/z9PpyW7nMAzrqOgDQGNnaZE3WYxpeOjk=; b=FeIHmYkcmuRQhYt1CidoWRrsx7B+8tgwL5PwlcmHEwpxrsWXIdWKx27EciNkFRfopHSgYBDVbYa1XtqvA4Zyz5Kun+xZvN3rJRAFzx1cT+V1elVUElAxwszfVEdVWSs9q3p9cJiXkvHRmKfsicM3wf+Zzvh8uSH7HJ8CMU/bZHGu8Dujp+1b90nu25DaqJtS4du7JaR421ANuG/2RtX5MfnT/QQu9hFBz1AYR2dDUtBhsJnyo75rf70PHlurXS9jQcNc+PtHZ6oY0kZ1BnWnVSrxyAXjvUWVt894r3LJ/bbKQWUEd52e0FDWfRY9SL2Cn1fWAIqbkrBy4XarOhfm5g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OaHOLg157m/z9PpyW7nMAzrqOgDQGNnaZE3WYxpeOjk=; b=0Xjcdb0d5uDb+FqjA2ILQ3Eelhq9nFsk5e77TDmAktoh7+iMJBNYr6vIbxZvB8WBp1swJHVsH5htaS1FW8hAfdlpaS8SiIhn0jhxZgcore3euymk/IFlRXWjZ9kPvusK3/76VsLa6Jbe2xTv5NC+X6bq443E9yJStIzfmMudlWQvBGSxTdwDHcjwKe+DqryoN/AxXAEQ2MfBHCRlHbKUK5eMaUJ92r/sDzOlLEkyj8nhltZ6VfJSjvbWmlJzfs+wkcv1v0Zh6+wZ8aP+xK/STryIhS5jnT0FzPgRW+adqKar5xbq7hbBas4QewqzM4DTrAZsda10zfixzWEAhQHlPQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by PAWPR08MB8887.eurprd08.prod.outlook.com (2603:10a6:102:33b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.13; Wed, 14 Aug 2024 10:32:04 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:04 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 2/9] net/tcp: fix selective acknowledge Date: Wed, 14 Aug 2024 14:31:38 +0400 Message-Id: <20240814103145.1347645-3-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|PAWPR08MB8887:EE_ X-MS-Office365-Filtering-Correlation-Id: 1c453865-5888-4424-ec1b-08dcbc4c56d9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|7416014|52116014|376014|38350700014|921020; X-Microsoft-Antispam-Message-Info: LD32Q9CTP0MRWo8rqIMnKGoQYdXKqWkatV91NEWyYvcjOj0+9WdNGw+3J7yek4PAqq41z164a8a08LLMcUxyh2aAOBx5+/qbrILJ7LxfG25hJ3UR2R9lT/0h9hEsJtqOPM3OymsWw2rqhTcVoa5yAmSVg919gPYBp9Ws6by05OJdJV3gvG5X/95DQekS0YfqZDENRPokoPLAOr9ZIrIAoxyC0Mt4PPYx+f9x6fyIe0WAiUOSRNBQUTOxNnMNwtu7R+sWH3YGRxg8Mv5gM5JxhUB42dJ7o5fDKwth1AJkaEIIWKzRJccc5zF3W1DDxr8XaIxSeaxX3gl0ZE1uKf7SnRUpe1obsdx5//YBnEKyWLe8jHoPqc9Xr8zM9fN5ZJkE2C6bYyYHJjJfuARLnxR++OAW3JXILTuPpsHN9HFeE0aovMNW3+nPm9xTaC6gZIj3cFM0qTa6U6PJWGAob3iRBIYRPPpU5qsRWOywTOzLXU5BvFLI86ORrwDt4FWYriwiVh+HFt6+2uGh6XXx6CfBYNzMJm7F1aCungfHmTbRf6HkZaMvXY6BqMz0NQZlCKD3N9wPrL2ZCgmLoBFMi+8ASZAvf7UtwHLot/uryDPqUIFk12AB4PIz6aeH37NUwIi04uagKJl/YuLt8fdLjDvASxUYeGaNoiz5K8ui/i0pPJyHudw8V/6MHmw5YXIhR1dM0/nUadZMcTh4PFa/snNtHemncjQuflFOlxkFNi4M/kE= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(7416014)(52116014)(376014)(38350700014)(921020); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: utsH3AzGcn9Dw2IZtUkzOs9FhNf5RskRYZI/MZ1trn+MkGTixEAU26m3VulAjegl36gi88DzKEj73VekcLWrhhdUcwaTV4PXZZUfhahFmi4iJ9Y3C0VKasJpyV3AgeoZ6uSjzebWGj3KCSHV37afRuMbOz5SMvVUWb9QmAiGujL1UjXZaycme24ISHUHK2dzhE0c4RfWOfeN2iLLp+1vwNAlKHY02B488nmLHvvYKN3ZcmFqBSWU6cfWmnsVhtIELbR2JJlmrx4CjhKbNbuAmMtgB2Z/IroIcBI5bm/u13h6Q7spikGLX58cy3vAi9SKqiIVHQk2PIj9N+5sMVzezHyEA/e5t55EoG1yP4wCEldreVmPIFwXYMLOPr0VIFEHs+XmnTStjmuMxj5FeDbgp9TK2bwers5FB0PQST8cuN1tzbxTRyV/1+3v9NnON9TOUN5JOqw9NnkCL86LJ+NBs91OViX08GmUDfHZc1vyJzjlNb8kFp5Il8DTZilTkdrWp9nLeNfHXUflGtULqk0xSqGKJpF1GpGPwmAmOol6zjuDXgvzF4iP/06qHxx7hHMOSFmPf7JCtO/pwu2O34aB1Ublbzil8YNvLGYhX4AwuWiG3KgfNIuxVfjjp1f2Hp/ygkV/uioQI8lQavWS2EJiu69xlSy5itK8Sc0Ztz/N7IunsidyxyCpmRJsD84jbR7vL5C+WcKIinH0mC5HVzJwNWrTnjnuAAjmERxI+dYnTWsX6MPnEM/8V7kpzQ1qNPTPVnSG1mS/zNDLx72DXzTwsOoz8DxNaET6jwYWUEGO1nyWys2zUEssw9VWKi9xENEpao3mo3n3As4ogIVyKpeMONrg6j+xWHHW9OQvoCDOZUkeljavqbrHMHrHWw3kf0RmpevPU2812OXXN4qDHv6Dzp77nJK0IBL4lagRqPIq8OxtVZ25zv9cRsGEouqhf9Kg1zrA3WMRjJWv87eFzrtdDB8Bm5tiSWeWDJe2nzotsetJPoIBNXt2Ancx1EYwteIgPBUXGEk7kBbmxHWLkGA3ksAhAdJZkPRgTDrRvIaKiG2J0yNXmOKTO2ayKAFwtBhQ+q/n9dv5kW1CC7Uvs5vr5FmeSRyh464ZiNqncL/VHaqVLoFh8qm1HkuoSjCNOOYjw9bHMjg57pderw31OWg5PpE2JGJLxKL7yQzY3A76yXguGZB2J/VzVAOSJLTJijXnVVCo6V8x+gDvr1/JohNHmCi9VMzP/TEYFGIdePy/Pfs0RHthkoYiVSbL4gd164rcbxw1d0kkoOmid1+noJiCFLlLyKUP8S6+UhxKSWkjXETSww1PMq4CCXfjH2i+AM40HdBDmQxSeikqnFjx8nBliWN0/wYIs8FczfO6qFwSA3wUUrW3kBftpnS/0+qwGdHdIBut7vHJ8qbAAkgmpFBUPWaYSrhHFL0MsY2wt/RXLTkurM4yX4Z7yzWb0j9BwKxpoAnu5nAoBNoLLv5gVfXgdPXEIoucHFzdzq0uTfY+BaYIa5W2F4EIoNBAzuVYqVfmK0x00uvufkxHAGm2QuSPKCgQK3Nj7wpjFTryo2LjTolHlP7YduwrHp6xY01rGU0HHvNnzm6fFSpq9JnjNDrUkA== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: 1c453865-5888-4424-ec1b-08dcbc4c56d9 X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:04.3726 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: v/hIHvmbTSkDM83+Msqt7Z6W67ML7r9Fl+IrLilJ6/WkUpHURTnpgTGWR9lcPxletqje35CL6c1BCQbrWm8HAiWtcKyfF3wufPUsR244nbk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB8887 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Current code assume that all (except last) packets are of the same size. This is definitely wrong. Replace SACK code with a new one, that does not rely on this assumption. Also this code uses less memory. Signed-off-by: Mikhail Kshevetskiy Reviewed-by: Simon Glass --- net/tcp.c | 200 +++++++++++++++++++++++------------------------------- 1 file changed, 86 insertions(+), 114 deletions(-) diff --git a/net/tcp.c b/net/tcp.c index 3e3118de450..724536cb352 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -38,21 +38,6 @@ static u32 tcp_ack_edge; static int tcp_activity_count; -/* - * Search for TCP_SACK and review the comments before the code section - * TCP_SACK is the number of packets at the front of the stream - */ - -enum pkt_state {PKT, NOPKT}; -struct sack_r { - struct sack_edges se; - enum pkt_state st; -}; - -static struct sack_r edge_a[TCP_SACK]; -static unsigned int sack_idx; -static unsigned int prev_len; - /* * TCP lengths are stored as a rounded up number of 32 bit words. * Add 3 to length round up, rounded, then divided into the @@ -69,6 +54,11 @@ static enum tcp_state current_tcp_state; /* Current TCP RX packet handler */ static rxhand_tcp *tcp_packet_handler; +static inline s32 tcp_seq_cmp(u32 a, u32 b) +{ + return (s32)(a - b); +} + /** * tcp_get_tcp_state() - get current TCP state * @@ -267,6 +257,7 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, action = TCP_FIN; current_tcp_state = TCP_FIN_WAIT_1; } else { + tcp_lost.len = TCP_OPT_LEN_2; current_tcp_state = TCP_SYN_SENT; } break; @@ -353,6 +344,20 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, return pkt_hdr_len; } +static void tcp_update_ack_edge(void) +{ + if (tcp_seq_cmp(tcp_ack_edge, tcp_lost.hill[0].l) >= 0) { + tcp_ack_edge = tcp_lost.hill[0].r; + + memmove(&tcp_lost.hill[0], &tcp_lost.hill[1], + (TCP_SACK_HILLS - 1) * sizeof(struct sack_edges)); + + tcp_lost.len -= TCP_OPT_LEN_8; + tcp_lost.hill[TCP_SACK_HILLS - 1].l = TCP_O_NOP; + tcp_lost.hill[TCP_SACK_HILLS - 1].r = TCP_O_NOP; + } +} + /** * tcp_hole() - Selective Acknowledgment (Essential for fast stream transfer) * @tcp_seq_num: TCP sequence start number @@ -360,106 +365,79 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, */ void tcp_hole(u32 tcp_seq_num, u32 len) { - u32 idx_sack, sack_in; - u32 sack_end = TCP_SACK - 1; - u32 hill = 0; - enum pkt_state expect = PKT; - u32 seq = tcp_seq_num - tcp_seq_init; - u32 hol_l = tcp_ack_edge - tcp_seq_init; - u32 hol_r = 0; - - /* Place new seq number in correct place in receive array */ - if (prev_len == 0) - prev_len = len; - - idx_sack = sack_idx + ((tcp_seq_num - tcp_ack_edge) / prev_len); - if (idx_sack < TCP_SACK) { - edge_a[idx_sack].se.l = tcp_seq_num; - edge_a[idx_sack].se.r = tcp_seq_num + len; - edge_a[idx_sack].st = PKT; + int i, j, cnt, cnt_move; - /* - * The fin (last) packet is not the same length as data - * packets, and if it's length is recorded and used for - * array index calculation, calculation breaks. - */ - if (prev_len < len) - prev_len = len; - } + cnt = (tcp_lost.len - TCP_OPT_LEN_2) / TCP_OPT_LEN_8; + for (i = 0; i < cnt; i++) { + if (tcp_seq_cmp(tcp_lost.hill[i].r, tcp_seq_num) < 0) + continue; + if (tcp_seq_cmp(tcp_lost.hill[i].l, tcp_seq_num + len) > 0) + break; - debug_cond(DEBUG_DEV_PKT, - "TCP 1 seq %d, edg %d, len %d, sack_idx %d, sack_end %d\n", - seq, hol_l, len, sack_idx, sack_end); + if (tcp_seq_cmp(tcp_lost.hill[i].l, tcp_seq_num) > 0) + tcp_lost.hill[i].l = tcp_seq_num; + if (tcp_seq_cmp(tcp_lost.hill[i].l, tcp_seq_num) < 0) { + len += tcp_seq_num - tcp_lost.hill[i].l; + tcp_seq_num = tcp_lost.hill[i].l; + } + if (tcp_seq_cmp(tcp_lost.hill[i].r, tcp_seq_num + len) >= 0) { + tcp_update_ack_edge(); + return; + } - /* Right edge of contiguous stream, is the left edge of first hill */ - hol_l = tcp_seq_num - tcp_seq_init; - hol_r = hol_l + len; + /* check overlapping with next hills */ + cnt_move = 0; + tcp_lost.hill[i].r = tcp_seq_num + len; + for (j = i + 1; j < cnt; j++) { + if (tcp_seq_cmp(tcp_lost.hill[j].l, tcp_lost.hill[i].r) > 0) + break; - if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) - tcp_lost.len = TCP_OPT_LEN_2; + tcp_lost.hill[i].r = tcp_lost.hill[j].r; + cnt_move++; + } - debug_cond(DEBUG_DEV_PKT, - "TCP 1 in %d, seq %d, pkt_l %d, pkt_r %d, sack_idx %d, sack_end %d\n", - idx_sack, seq, hol_l, hol_r, sack_idx, sack_end); - - for (sack_in = sack_idx; sack_in < sack_end && hill < TCP_SACK_HILLS; - sack_in++) { - switch (expect) { - case NOPKT: - switch (edge_a[sack_in].st) { - case NOPKT: - debug_cond(DEBUG_INT_STATE, "N"); - break; - case PKT: - debug_cond(DEBUG_INT_STATE, "n"); - if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) { - tcp_lost.hill[hill].l = - edge_a[sack_in].se.l; - tcp_lost.hill[hill].r = - edge_a[sack_in].se.r; - } - expect = PKT; - break; - } - break; - case PKT: - switch (edge_a[sack_in].st) { - case NOPKT: - debug_cond(DEBUG_INT_STATE, "p"); - if (sack_in > sack_idx && - hill < TCP_SACK_HILLS) { - hill++; - if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) - tcp_lost.len += TCP_OPT_LEN_8; - } - expect = NOPKT; - break; - case PKT: - debug_cond(DEBUG_INT_STATE, "P"); - - if (tcp_ack_edge == edge_a[sack_in].se.l) { - tcp_ack_edge = edge_a[sack_in].se.r; - edge_a[sack_in].st = NOPKT; - sack_idx++; - } else { - if (IS_ENABLED(CONFIG_PROT_TCP_SACK) && - hill < TCP_SACK_HILLS) - tcp_lost.hill[hill].r = - edge_a[sack_in].se.r; - if (IS_ENABLED(CONFIG_PROT_TCP_SACK) && - sack_in == sack_end - 1) - tcp_lost.hill[hill].r = - edge_a[sack_in].se.r; - } - break; + if (cnt_move > 0) { + if (cnt > i + cnt_move + 1) + memmove(&tcp_lost.hill[i + 1], + &tcp_lost.hill[i + cnt_move + 1], + cnt_move * sizeof(struct sack_edges)); + + cnt -= cnt_move; + tcp_lost.len = TCP_OPT_LEN_2 + cnt * TCP_OPT_LEN_8; + for (j = cnt; j < TCP_SACK_HILLS; j++) { + tcp_lost.hill[j].l = TCP_O_NOP; + tcp_lost.hill[j].r = TCP_O_NOP; } - break; } + + tcp_update_ack_edge(); + return; } - debug_cond(DEBUG_INT_STATE, "\n"); - if (!IS_ENABLED(CONFIG_PROT_TCP_SACK) || tcp_lost.len <= TCP_OPT_LEN_2) - sack_idx = 0; -} + + if (i == TCP_SACK_HILLS) { + tcp_update_ack_edge(); + return; + } + + if (cnt < TCP_SACK_HILLS) { + cnt_move = cnt - i; + cnt++; + } else { + cnt = TCP_SACK_HILLS; + cnt_move = TCP_SACK_HILLS - i; + } + + if (cnt_move > 0) + memmove(&tcp_lost.hill[i + 1], + &tcp_lost.hill[i], + cnt_move * sizeof(struct sack_edges)); + + tcp_lost.hill[i].l = tcp_seq_num; + tcp_lost.hill[i].r = tcp_seq_num + len; + tcp_lost.len = TCP_OPT_LEN_2 + cnt * TCP_OPT_LEN_8; + + tcp_update_ack_edge(); +}; /** * tcp_parse_options() - parsing TCP options @@ -509,7 +487,6 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) u8 tcp_push = tcp_flags & TCP_PUSH; u8 tcp_ack = tcp_flags & TCP_ACK; u8 action = TCP_DATA; - int i; /* * tcp_flags are examined to determine TX action in a given state @@ -536,6 +513,7 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) action = TCP_SYN | TCP_ACK; tcp_seq_init = tcp_seq_num; tcp_ack_edge = tcp_seq_num + 1; + tcp_lost.len = TCP_OPT_LEN_2; current_tcp_state = TCP_SYN_RECEIVED; } else if (tcp_ack || tcp_fin) { action = TCP_DATA; @@ -552,13 +530,7 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) action |= TCP_ACK; tcp_seq_init = tcp_seq_num; tcp_ack_edge = tcp_seq_num + 1; - sack_idx = 0; - edge_a[sack_idx].se.l = tcp_ack_edge; - edge_a[sack_idx].se.r = tcp_ack_edge; - prev_len = 0; current_tcp_state = TCP_ESTABLISHED; - for (i = 0; i < TCP_SACK; i++) - edge_a[i].st = NOPKT; if (tcp_syn && tcp_ack) action |= TCP_PUSH; From patchwork Wed Aug 14 10:31:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972338 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=g4kbv3W3; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPhj6jXdz1yYl for ; Wed, 14 Aug 2024 20:32:37 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7C5EE889A1; Wed, 14 Aug 2024 12:32:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="g4kbv3W3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8BA07889CD; Wed, 14 Aug 2024 12:32:11 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2070f.outbound.protection.outlook.com [IPv6:2a01:111:f403:2608::70f]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 397D9889A2 for ; Wed, 14 Aug 2024 12:32:09 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IKckWAc8SVYsACgfJyew8rkrTkoVXmwJbNaeqoARMVd2VqKlLDocght/qWhEPU76s5lMHE0X4iRRZN0Hh1w2bPgdSKGlrKFBYNeDU5AaIexwJl5ookVl6Fy8BNaRQERoZtOR46oJs8pnzpBj1z18rgSlm0gQDPcriCQWwoHSUrLqWfXVFwrA12YuQR9s+XO1f2n0XnLM3fRjQriFQDuDTTJEHs2zkaW1ViYChNujkKIprcjmu9S3kQw5+eOgL6Y1te3M26IHM4nSiIfWcwciYWAHmi6xT3NjsvOcBNes3VOzNPaEbq1fdvEeMvso7wgsxBpH8Q9LPEty7TJf1mmMfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=qdk5HzlBwO/qGkFvpGywFHaQqYZ31i4xmXZQv1+v89Y=; b=rogioB8HwXuEsHR7c5s+MmvyYwvE3NJlezRAgRtBQVI2YisruJplniV/wUN78DtdGZXXufwQgMIW8a3zGipJT2v+ZgM3cCCxi2DaUDlwXVv2N/+623LdJgM5cBtdGAVy/wQKw4oHci9tI/8Ij1+/D8ov2kmo2TwI0Axb3xDyQCqFFn1pbOcabeamXBb5r5kIAO9Mz1P0KcX9RbBFgO34u/otK6AR5YyYC/TQbQo3N46IWGZNjausvjr2U6qztFvT9IOeE3/0RThse6wmQjeiK4ERcCtsztM72QCzxk+F+kHQA8yV1FPmYQEn3V0BbFxEVPRw9LsMdYagTFHNYKClyg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qdk5HzlBwO/qGkFvpGywFHaQqYZ31i4xmXZQv1+v89Y=; b=g4kbv3W3MrPwNsNSUxJlYEJSEqyPSZv0e6BwsF4oQmAKtRI+Hn4TpG0Y1o4Er+fqDI0+VVlN9hR+uoHQ4xL/JhovrtZArsutq0NeHXKWu5ONWukITLtUUBGUzpXJ4GX7TItlhoBhSmFfuBiPY8d+KGOgp8Mytuehw3ML0q9UviROvwv8s/3pA+ps4ReZr4RkOU5TVXClGYDEvI38mRYSdRl6bvfes63hmsZgvRMrtVcOjCdMukHVJyEZYGB2Rp3Y2CEf7agFOQ+FpqaolD7nH2ukqQW+wqi1vVAp5HZiBBotXiPH9IEz50RETyFRuMw4QyTOyd3j+6IrkEd9CWjwwA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by PAWPR08MB8887.eurprd08.prod.outlook.com (2603:10a6:102:33b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.13; Wed, 14 Aug 2024 10:32:07 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:07 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 3/9] net/tcp: put connection specific data into a tcp_stream structure Date: Wed, 14 Aug 2024 14:31:39 +0400 Message-Id: <20240814103145.1347645-4-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|PAWPR08MB8887:EE_ X-MS-Office365-Filtering-Correlation-Id: 13dbc855-e16f-43b8-5309-08dcbc4c58b8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|7416014|52116014|376014|38350700014|921020; X-Microsoft-Antispam-Message-Info: 3I1A1DtHbscmd7WuM2TZxJvqLUG06naLEd7clRAFdDSAAhDKK9bds/+HxJ9dhsv+hlPMBosdyZuxL6cMZakwGDGxYDFZbddoghURKR74Dd1h80o5b48i2OTcAAaxyY05oM5GRm6TbquFEQBPx6Fg17L4GDi1MTP/t35w9S7XWhh713tk0OVS14xM1pGAerN7S/e1N4q2DRqQuPd8m4WYrfAWPanI/8MTWvinj12YKsY8/GOvFuZY6GF9ABpRBrrdj6DvVpDGf3R/nLrsgZpN97QoEc7XELnj9w0AyClxzJex+H67g/EXb5/u3JjIS/E0pHgKmEw8d4B6FOReMmqh3FABU8FTubGO08e/RrTD/BinV2oDPUaTGJtgbUz5/ViiUuoe+Bone/ooG89yqg3WkU5NAjq2dipFSNlm7RZ2jLznA4ds/1swMRLr19LgNIqaAM/Le8dlMpdPQEI3pgIbWG9iG2De3k3sK9qV6EkAJqzhbPzFloqnjzUNlH5pcStNaUHV8kumY3ZRrRlotQ0EKaJ5mgbHNdzFsark7nephn8pcjmntCiLSy4J2EzUKKVZYcBo8Ncg//FuKcNoSd/huFfs2fCONkxfHCOvwwYG2eomP1O0otrwDIrWAhdApg5Bqc839OpLxHqBGto+3zPjFZqSgzHrPA56GQTJ7+AYFszRqKnAhqF5rxaRmjHlTEOZMVk3Y3Yg2aNsjjVtdNk9V4nqoSMS7lymARZ5goE9onc= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(7416014)(52116014)(376014)(38350700014)(921020); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UeFv2u5k1+Coi6To9OM0yPdS1VjTQ/KYsyzEyIZEkgmh3t8EszBdF+2lnU8KQ5Kb9BtyG6VsKSbV+hiOmWWPlMEeUb8Gb1BSbuE6chAc2MQId4dz8yoYgsSZAdc4U/BZf2Kto9CKPEkfFpzar5KvCPh3ErAU4dgBMmyIVpzANnsE3vXwSV8oR59QdMZydxfsM+C0z7kcm3icMusChYbiWpKX+vGwPa3nDeIvIFlG4BJmu7/esxdd9RO0+FuFlZiyYMOhJvSD1tQX/q5qaf79tu7RqQi9tFNCA4+Dzpok+n7cnQqdYhRy0vi1SdSziQHl1xmjphYqnNSnaOnrqYST53qJruCzXOP/28D2DvrJKJdeC0XGoojemWan47L6MrwtS8HB5p+vqhLFm9IJ5qGemEApz1/f8zpJ9I08tjEueRkCOIsMto3pMMPsXwImpa6+R9t4Ob94t8W9OVjwXvNKurleV/pLEuBwG6nj3fHUX7K6McCO4k2rqnuZJbc3prDeh2kihfvSyyDCwwPzfwJrksg7/QQ6bZmXTWY4BGOq4qCH8aaijx5M33N1ScBsdHMVFVebmQVErBdjUxJeCxqlq140pL7G2bQbFevYBRPHi+ZkI+DZ259zZ8TPwMI6IA1mUCf4Gc3b0ALM6YQdYirU3vnnihNKwY3icwZWF4riTlQ1VP2a9sCSRhSnzgWQHNw0ZoL9OU9arrkxmkRLE/f8o4TfreDvJVSA2g7Z9L7S1CEjIImz7AFgqJlop9q1PXKHFTU4KV8mGi83QdZQauSx3QfMg3CTGmdH43kLD5hnQAKFZFoLwjrH+jRiZG9iPIcxnppfwNwk93RvjFm5Uicn1ye5QR5bd+D+PMFNZ7NzfJBPHnrMBXkB77jjEPAIGbrGpYm6N8RFeF6W1Kpw3PPDGFNHqEZ/er8r3QxKZ9Wo3lqS9iPW5QNearmX9q767jcmzhNnjz460YUyhSgmzn16WnwFhKgqvwu6/XyQyI2RXMSc9ayDQOgXS96XhaGCkFnjBk7z4j6qtjk36v9dajpHD+7WxOcQjoi/d35NROrA3zEjoP/u4W5Wugkozc1kAKzvq8hiL6e41nicJon2bISYKy011slsTbx/ZNl0NeW2tAw5QaZ6gjpH6oJ/Ylj9MCUgz1i6JTDtPm9u7igASD/LR9f8A2FgM0JpKq+SQKNkGEYvFq7t4VzmB+U7983tajX8+uAm+6yHx5qMpT2tOPZnCvsYtZ0Ya4DIWoRsiVXiJQkjZobDu2d/eSRlpOIfaifwbX91dIlZiL++1iWlFO5QguhEmOOHoiiUtZPbcyb2ENdz+rWg3ceSJHNjOCQsOyNK3U1njlvTB5aNqephLL3eP+Vjt6EGSvBu6IYEdy4Bsns2SH/BVj1TJ0Xcgf7ZOlovJTPgiH2gMJbQBCoGGb6vcldO4YThwxZjQn5LH7od1gjFOVW2cub5oWEm/0jT6UwqibTDpLJ/sOyln9c5sFY9PYQ9IyIL400JXLparfABujJfQdvSKurmyTaOWFYUPrw8h5vkKbEYEj7fXoj1z+BU0YSm+JrZfee8UveVakvsUmucs/1yT4uaBTl6ilu3oZU3QnDn2WeUGZmbvI1v7FY5BQ== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: 13dbc855-e16f-43b8-5309-08dcbc4c58b8 X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:07.4853 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /DHk6q0FshexnFgee/43pIBsat/9Eo9HHPpPuDA6q7yuTVyH1gIOo56bvL1DoUgjc5oUyHFt2K5L1p7yCVfTMb+qhNccG9P9FbGPb9Zk2Ts= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB8887 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean no functional changes Signed-off-by: Mikhail Kshevetskiy Reviewed-by: Simon Glass --- include/net/tcp.h | 37 +++++++- net/net.c | 11 ++- net/tcp.c | 231 +++++++++++++++++++++++----------------------- net/wget.c | 3 +- 4 files changed, 163 insertions(+), 119 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index c29d4ce24a7..14aee64cb1c 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -277,9 +277,40 @@ enum tcp_state { TCP_FIN_WAIT_2 }; -enum tcp_state tcp_get_tcp_state(void); -void tcp_set_tcp_state(enum tcp_state new_state); -int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, +/** + * struct tcp_stream - TCP data stream structure + * + * @state: TCP connection state + * + * @seq_init: Initial receive sequence number + * @ack_edge: Receive next + * + * @loc_timestamp: Local timestamp + * @rmt_timestamp: Remote timestamp + * + * @lost: Used for SACK + */ +struct tcp_stream { + /* TCP connection state */ + enum tcp_state state; + + u32 seq_init; + u32 ack_edge; + + /* TCP option timestamp */ + u32 loc_timestamp; + u32 rmt_timestamp; + + /* TCP sliding window control used to request re-TX */ + struct tcp_sack_v lost; +}; + +struct tcp_stream *tcp_stream_get(void); + +enum tcp_state tcp_get_tcp_state(struct tcp_stream *tcp); +void tcp_set_tcp_state(struct tcp_stream *tcp, enum tcp_state new_state); +int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, + int sport, int payload_len, u8 action, u32 tcp_seq_num, u32 tcp_ack_num); /** diff --git a/net/net.c b/net/net.c index d9bc9df643f..6c5ee7e0925 100644 --- a/net/net.c +++ b/net/net.c @@ -414,7 +414,7 @@ int net_init(void) /* Only need to setup buffer pointers once. */ first_call = 0; if (IS_ENABLED(CONFIG_PROT_TCP)) - tcp_set_tcp_state(TCP_CLOSED); + tcp_set_tcp_state(tcp_stream_get(), TCP_CLOSED); } return net_init_loop(); @@ -915,6 +915,9 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport, uchar *pkt; int eth_hdr_size; int pkt_hdr_size; +#if defined(CONFIG_PROT_TCP) + struct tcp_stream *tcp; +#endif /* make sure the net_tx_packet is initialized (net_init() was called) */ assert(net_tx_packet != NULL); @@ -941,8 +944,12 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport, break; #if defined(CONFIG_PROT_TCP) case IPPROTO_TCP: + tcp = tcp_stream_get(); + if (tcp == NULL) + return -EINVAL; + pkt_hdr_size = eth_hdr_size - + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport, + + tcp_set_tcp_header(tcp, pkt + eth_hdr_size, dport, sport, payload_len, action, tcp_seq_num, tcp_ack_num); break; diff --git a/net/tcp.c b/net/tcp.c index 724536cb352..6646f171b83 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -24,19 +24,8 @@ #include #include -/* - * TCP sliding window control used by us to request re-TX - */ -static struct tcp_sack_v tcp_lost; - -/* TCP option timestamp */ -static u32 loc_timestamp; -static u32 rmt_timestamp; - -static u32 tcp_seq_init; -static u32 tcp_ack_edge; - static int tcp_activity_count; +static struct tcp_stream tcp_stream; /* * TCP lengths are stored as a rounded up number of 32 bit words. @@ -48,9 +37,6 @@ static int tcp_activity_count; #define SHIFT_TO_TCPHDRLEN_FIELD(x) ((x) << 4) #define GET_TCP_HDR_LEN_IN_BYTES(x) ((x) >> 2) -/* TCP connection state */ -static enum tcp_state current_tcp_state; - /* Current TCP RX packet handler */ static rxhand_tcp *tcp_packet_handler; @@ -60,22 +46,30 @@ static inline s32 tcp_seq_cmp(u32 a, u32 b) } /** - * tcp_get_tcp_state() - get current TCP state + * tcp_get_tcp_state() - get TCP stream state + * @tcp: tcp stream * - * Return: Current TCP state + * Return: TCP stream state */ -enum tcp_state tcp_get_tcp_state(void) +enum tcp_state tcp_get_tcp_state(struct tcp_stream *tcp) { - return current_tcp_state; + return tcp->state; } /** - * tcp_set_tcp_state() - set current TCP state + * tcp_set_tcp_state() - set TCP stream state + * @tcp: tcp stream * @new_state: new TCP state */ -void tcp_set_tcp_state(enum tcp_state new_state) +void tcp_set_tcp_state(struct tcp_stream *tcp, + enum tcp_state new_state) { - current_tcp_state = new_state; + tcp->state = new_state; +} + +struct tcp_stream *tcp_stream_get(void) +{ + return &tcp_stream; } static void dummy_handler(uchar *pkt, u16 dport, @@ -138,29 +132,30 @@ u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr src, struct in_addr dest, /** * net_set_ack_options() - set TCP options in acknowledge packets + * @tcp: tcp stream * @b: the packet * * Return: TCP header length */ -int net_set_ack_options(union tcp_build_pkt *b) +int net_set_ack_options(struct tcp_stream *tcp, union tcp_build_pkt *b) { b->sack.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE)); b->sack.t_opt.kind = TCP_O_TS; b->sack.t_opt.len = TCP_OPT_LEN_A; - b->sack.t_opt.t_snd = htons(loc_timestamp); - b->sack.t_opt.t_rcv = rmt_timestamp; + b->sack.t_opt.t_snd = htons(tcp->loc_timestamp); + b->sack.t_opt.t_rcv = tcp->rmt_timestamp; b->sack.sack_v.kind = TCP_1_NOP; b->sack.sack_v.len = 0; if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) { - if (tcp_lost.len > TCP_OPT_LEN_2) { + if (tcp->lost.len > TCP_OPT_LEN_2) { debug_cond(DEBUG_DEV_PKT, "TCP ack opt lost.len %x\n", - tcp_lost.len); - b->sack.sack_v.len = tcp_lost.len; + tcp->lost.len); + b->sack.sack_v.len = tcp->lost.len; b->sack.sack_v.kind = TCP_V_SACK; - b->sack.sack_v.hill[0].l = htonl(tcp_lost.hill[0].l); - b->sack.sack_v.hill[0].r = htonl(tcp_lost.hill[0].r); + b->sack.sack_v.hill[0].l = htonl(tcp->lost.hill[0].l); + b->sack.sack_v.hill[0].r = htonl(tcp->lost.hill[0].r); /* * These SACK structures are initialized with NOPs to @@ -168,17 +163,17 @@ int net_set_ack_options(union tcp_build_pkt *b) * SACK structures used for both header padding and * internally. */ - b->sack.sack_v.hill[1].l = htonl(tcp_lost.hill[1].l); - b->sack.sack_v.hill[1].r = htonl(tcp_lost.hill[1].r); - b->sack.sack_v.hill[2].l = htonl(tcp_lost.hill[2].l); - b->sack.sack_v.hill[2].r = htonl(tcp_lost.hill[2].r); + b->sack.sack_v.hill[1].l = htonl(tcp->lost.hill[1].l); + b->sack.sack_v.hill[1].r = htonl(tcp->lost.hill[1].r); + b->sack.sack_v.hill[2].l = htonl(tcp->lost.hill[2].l); + b->sack.sack_v.hill[2].r = htonl(tcp->lost.hill[2].r); b->sack.sack_v.hill[3].l = TCP_O_NOP; b->sack.sack_v.hill[3].r = TCP_O_NOP; } b->sack.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE + TCP_TSOPT_SIZE + - tcp_lost.len)); + tcp->lost.len)); } else { b->sack.sack_v.kind = 0; b->sack.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE + @@ -194,13 +189,14 @@ int net_set_ack_options(union tcp_build_pkt *b) } /** - * net_set_ack_options() - set TCP options in SYN packets + * net_set_syn_options() - set TCP options in SYN packets + * @tcp: tcp stream * @b: the packet */ -void net_set_syn_options(union tcp_build_pkt *b) +void net_set_syn_options(struct tcp_stream *tcp, union tcp_build_pkt *b) { if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) - tcp_lost.len = 0; + tcp->lost.len = 0; b->ip.hdr.tcp_hlen = 0xa0; @@ -219,14 +215,15 @@ void net_set_syn_options(union tcp_build_pkt *b) } b->ip.t_opt.kind = TCP_O_TS; b->ip.t_opt.len = TCP_OPT_LEN_A; - loc_timestamp = get_ticks(); - rmt_timestamp = 0; + tcp->loc_timestamp = get_ticks(); + tcp->rmt_timestamp = 0; b->ip.t_opt.t_snd = 0; b->ip.t_opt.t_rcv = 0; b->ip.end = TCP_O_END; } -int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, +int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, + int sport, int payload_len, u8 action, u32 tcp_seq_num, u32 tcp_ack_num) { union tcp_build_pkt *b = (union tcp_build_pkt *)pkt; @@ -249,21 +246,21 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num); tcp_activity_count = 0; - net_set_syn_options(b); + net_set_syn_options(tcp, b); tcp_seq_num = 0; tcp_ack_num = 0; pkt_hdr_len = IP_TCP_O_SIZE; - if (current_tcp_state == TCP_SYN_SENT) { /* Too many SYNs */ + if (tcp->state == TCP_SYN_SENT) { /* Too many SYNs */ action = TCP_FIN; - current_tcp_state = TCP_FIN_WAIT_1; + tcp->state = TCP_FIN_WAIT_1; } else { - tcp_lost.len = TCP_OPT_LEN_2; - current_tcp_state = TCP_SYN_SENT; + tcp->lost.len = TCP_OPT_LEN_2; + tcp->state = TCP_SYN_SENT; } break; case TCP_SYN | TCP_ACK: case TCP_ACK: - pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(b); + pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(tcp, b); b->ip.hdr.tcp_flags = action; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:ACK (%pI4, %pI4, s=%u, a=%u, A=%x)\n", @@ -276,20 +273,20 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num); payload_len = 0; pkt_hdr_len = IP_TCP_HDR_SIZE; - current_tcp_state = TCP_FIN_WAIT_1; + tcp->state = TCP_FIN_WAIT_1; break; case TCP_RST | TCP_ACK: case TCP_RST: debug_cond(DEBUG_DEV_PKT, "TCP Hdr:RST (%pI4, %pI4, s=%u, a=%u)\n", &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num); - current_tcp_state = TCP_CLOSED; + tcp->state = TCP_CLOSED; break; /* Notify connection closing */ case (TCP_FIN | TCP_ACK): case (TCP_FIN | TCP_ACK | TCP_PUSH): - if (current_tcp_state == TCP_CLOSE_WAIT) - current_tcp_state = TCP_CLOSING; + if (tcp->state == TCP_CLOSE_WAIT) + tcp->state = TCP_CLOSING; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:FIN ACK PSH(%pI4, %pI4, s=%u, a=%u, A=%x)\n", @@ -297,7 +294,7 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, tcp_seq_num, tcp_ack_num, action); fallthrough; default: - pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(b); + pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(tcp, b); b->ip.hdr.tcp_flags = action | TCP_PUSH | TCP_ACK; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:dft (%pI4, %pI4, s=%u, a=%u, A=%x)\n", @@ -308,9 +305,9 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, pkt_len = pkt_hdr_len + payload_len; tcp_len = pkt_len - IP_HDR_SIZE; - tcp_ack_edge = tcp_ack_num; + tcp->ack_edge = tcp_ack_num; /* TCP Header */ - b->ip.hdr.tcp_ack = htonl(tcp_ack_edge); + b->ip.hdr.tcp_ack = htonl(tcp->ack_edge); b->ip.hdr.tcp_src = htons(sport); b->ip.hdr.tcp_dst = htons(dport); b->ip.hdr.tcp_seq = htonl(tcp_seq_num); @@ -344,78 +341,79 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, return pkt_hdr_len; } -static void tcp_update_ack_edge(void) +static void tcp_update_ack_edge(struct tcp_stream *tcp) { - if (tcp_seq_cmp(tcp_ack_edge, tcp_lost.hill[0].l) >= 0) { - tcp_ack_edge = tcp_lost.hill[0].r; + if (tcp_seq_cmp(tcp->ack_edge, tcp->lost.hill[0].l) >= 0) { + tcp->ack_edge = tcp->lost.hill[0].r; - memmove(&tcp_lost.hill[0], &tcp_lost.hill[1], + memmove(&tcp->lost.hill[0], &tcp->lost.hill[1], (TCP_SACK_HILLS - 1) * sizeof(struct sack_edges)); - tcp_lost.len -= TCP_OPT_LEN_8; - tcp_lost.hill[TCP_SACK_HILLS - 1].l = TCP_O_NOP; - tcp_lost.hill[TCP_SACK_HILLS - 1].r = TCP_O_NOP; + tcp->lost.len -= TCP_OPT_LEN_8; + tcp->lost.hill[TCP_SACK_HILLS - 1].l = TCP_O_NOP; + tcp->lost.hill[TCP_SACK_HILLS - 1].r = TCP_O_NOP; } } /** * tcp_hole() - Selective Acknowledgment (Essential for fast stream transfer) + * @tcp: tcp stream * @tcp_seq_num: TCP sequence start number * @len: the length of sequence numbers */ -void tcp_hole(u32 tcp_seq_num, u32 len) +void tcp_hole(struct tcp_stream *tcp, u32 tcp_seq_num, u32 len) { int i, j, cnt, cnt_move; - cnt = (tcp_lost.len - TCP_OPT_LEN_2) / TCP_OPT_LEN_8; + cnt = (tcp->lost.len - TCP_OPT_LEN_2) / TCP_OPT_LEN_8; for (i = 0; i < cnt; i++) { - if (tcp_seq_cmp(tcp_lost.hill[i].r, tcp_seq_num) < 0) + if (tcp_seq_cmp(tcp->lost.hill[i].r, tcp_seq_num) < 0) continue; - if (tcp_seq_cmp(tcp_lost.hill[i].l, tcp_seq_num + len) > 0) + if (tcp_seq_cmp(tcp->lost.hill[i].l, tcp_seq_num + len) > 0) break; - if (tcp_seq_cmp(tcp_lost.hill[i].l, tcp_seq_num) > 0) - tcp_lost.hill[i].l = tcp_seq_num; - if (tcp_seq_cmp(tcp_lost.hill[i].l, tcp_seq_num) < 0) { - len += tcp_seq_num - tcp_lost.hill[i].l; - tcp_seq_num = tcp_lost.hill[i].l; + if (tcp_seq_cmp(tcp->lost.hill[i].l, tcp_seq_num) > 0) + tcp->lost.hill[i].l = tcp_seq_num; + if (tcp_seq_cmp(tcp->lost.hill[i].l, tcp_seq_num) < 0) { + len += tcp_seq_num - tcp->lost.hill[i].l; + tcp_seq_num = tcp->lost.hill[i].l; } - if (tcp_seq_cmp(tcp_lost.hill[i].r, tcp_seq_num + len) >= 0) { - tcp_update_ack_edge(); + if (tcp_seq_cmp(tcp->lost.hill[i].r, tcp_seq_num + len) >= 0) { + tcp_update_ack_edge(tcp); return; } /* check overlapping with next hills */ cnt_move = 0; - tcp_lost.hill[i].r = tcp_seq_num + len; + tcp->lost.hill[i].r = tcp_seq_num + len; for (j = i + 1; j < cnt; j++) { - if (tcp_seq_cmp(tcp_lost.hill[j].l, tcp_lost.hill[i].r) > 0) + if (tcp_seq_cmp(tcp->lost.hill[j].l, tcp->lost.hill[i].r) > 0) break; - tcp_lost.hill[i].r = tcp_lost.hill[j].r; + tcp->lost.hill[i].r = tcp->lost.hill[j].r; cnt_move++; } if (cnt_move > 0) { if (cnt > i + cnt_move + 1) - memmove(&tcp_lost.hill[i + 1], - &tcp_lost.hill[i + cnt_move + 1], + memmove(&tcp->lost.hill[i + 1], + &tcp->lost.hill[i + cnt_move + 1], cnt_move * sizeof(struct sack_edges)); cnt -= cnt_move; - tcp_lost.len = TCP_OPT_LEN_2 + cnt * TCP_OPT_LEN_8; + tcp->lost.len = TCP_OPT_LEN_2 + cnt * TCP_OPT_LEN_8; for (j = cnt; j < TCP_SACK_HILLS; j++) { - tcp_lost.hill[j].l = TCP_O_NOP; - tcp_lost.hill[j].r = TCP_O_NOP; + tcp->lost.hill[j].l = TCP_O_NOP; + tcp->lost.hill[j].r = TCP_O_NOP; } } - tcp_update_ack_edge(); + tcp_update_ack_edge(tcp); return; } if (i == TCP_SACK_HILLS) { - tcp_update_ack_edge(); + tcp_update_ack_edge(tcp); return; } @@ -428,23 +426,24 @@ void tcp_hole(u32 tcp_seq_num, u32 len) } if (cnt_move > 0) - memmove(&tcp_lost.hill[i + 1], - &tcp_lost.hill[i], + memmove(&tcp->lost.hill[i + 1], + &tcp->lost.hill[i], cnt_move * sizeof(struct sack_edges)); - tcp_lost.hill[i].l = tcp_seq_num; - tcp_lost.hill[i].r = tcp_seq_num + len; - tcp_lost.len = TCP_OPT_LEN_2 + cnt * TCP_OPT_LEN_8; + tcp->lost.hill[i].l = tcp_seq_num; + tcp->lost.hill[i].r = tcp_seq_num + len; + tcp->lost.len = TCP_OPT_LEN_2 + cnt * TCP_OPT_LEN_8; - tcp_update_ack_edge(); + tcp_update_ack_edge(tcp); }; /** * tcp_parse_options() - parsing TCP options + * @tcp: tcp stream * @o: pointer to the option field. * @o_len: length of the option field. */ -void tcp_parse_options(uchar *o, int o_len) +void tcp_parse_options(struct tcp_stream *tcp, uchar *o, int o_len) { struct tcp_t_opt *tsopt; uchar *p = o; @@ -467,7 +466,7 @@ void tcp_parse_options(uchar *o, int o_len) break; case TCP_O_TS: tsopt = (struct tcp_t_opt *)p; - rmt_timestamp = tsopt->t_snd; + tcp->rmt_timestamp = tsopt->t_snd; break; } @@ -479,7 +478,8 @@ void tcp_parse_options(uchar *o, int o_len) } } -static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) +static u8 tcp_state_machine(struct tcp_stream *tcp, u8 tcp_flags, + u32 tcp_seq_num, int payload_len) { u8 tcp_fin = tcp_flags & TCP_FIN; u8 tcp_syn = tcp_flags & TCP_SYN; @@ -500,21 +500,21 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) debug_cond(DEBUG_INT_STATE, "TCP STATE ENTRY %x\n", action); if (tcp_rst) { action = TCP_DATA; - current_tcp_state = TCP_CLOSED; + tcp->state = TCP_CLOSED; net_set_state(NETLOOP_FAIL); debug_cond(DEBUG_INT_STATE, "TCP Reset %x\n", tcp_flags); return TCP_RST; } - switch (current_tcp_state) { + switch (tcp->state) { case TCP_CLOSED: debug_cond(DEBUG_INT_STATE, "TCP CLOSED %x\n", tcp_flags); if (tcp_syn) { action = TCP_SYN | TCP_ACK; - tcp_seq_init = tcp_seq_num; - tcp_ack_edge = tcp_seq_num + 1; - tcp_lost.len = TCP_OPT_LEN_2; - current_tcp_state = TCP_SYN_RECEIVED; + tcp->seq_init = tcp_seq_num; + tcp->ack_edge = tcp_seq_num + 1; + tcp->lost.len = TCP_OPT_LEN_2; + tcp->state = TCP_SYN_RECEIVED; } else if (tcp_ack || tcp_fin) { action = TCP_DATA; } @@ -525,12 +525,12 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) tcp_flags, tcp_seq_num); if (tcp_fin) { action = action | TCP_PUSH; - current_tcp_state = TCP_CLOSE_WAIT; + tcp->state = TCP_CLOSE_WAIT; } else if (tcp_ack || (tcp_syn && tcp_ack)) { action |= TCP_ACK; - tcp_seq_init = tcp_seq_num; - tcp_ack_edge = tcp_seq_num + 1; - current_tcp_state = TCP_ESTABLISHED; + tcp->seq_init = tcp_seq_num; + tcp->ack_edge = tcp_seq_num + 1; + tcp->state = TCP_ESTABLISHED; if (tcp_syn && tcp_ack) action |= TCP_PUSH; @@ -541,15 +541,15 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) case TCP_ESTABLISHED: debug_cond(DEBUG_INT_STATE, "TCP_ESTABLISHED %x\n", tcp_flags); if (payload_len > 0) { - tcp_hole(tcp_seq_num, payload_len); + tcp_hole(tcp, tcp_seq_num, payload_len); tcp_fin = TCP_DATA; /* cause standalone FIN */ } if ((tcp_fin) && (!IS_ENABLED(CONFIG_PROT_TCP_SACK) || - tcp_lost.len <= TCP_OPT_LEN_2)) { + tcp->lost.len <= TCP_OPT_LEN_2)) { action = action | TCP_FIN | TCP_PUSH | TCP_ACK; - current_tcp_state = TCP_CLOSE_WAIT; + tcp->state = TCP_CLOSE_WAIT; } else if (tcp_ack) { action = TCP_DATA; } @@ -567,7 +567,7 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) debug_cond(DEBUG_INT_STATE, "TCP_FIN_WAIT_2 (%x)\n", tcp_flags); if (tcp_ack) { action = TCP_PUSH | TCP_ACK; - current_tcp_state = TCP_CLOSED; + tcp->state = TCP_CLOSED; puts("\n"); } else if (tcp_syn) { action = TCP_DATA; @@ -578,20 +578,20 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len) case TCP_FIN_WAIT_1: debug_cond(DEBUG_INT_STATE, "TCP_FIN_WAIT_1 (%x)\n", tcp_flags); if (tcp_fin) { - tcp_ack_edge++; + tcp->ack_edge++; action = TCP_ACK | TCP_FIN; - current_tcp_state = TCP_FIN_WAIT_2; + tcp->state = TCP_FIN_WAIT_2; } if (tcp_syn) action = TCP_RST; if (tcp_ack) - current_tcp_state = TCP_CLOSED; + tcp->state = TCP_CLOSED; break; case TCP_CLOSING: debug_cond(DEBUG_INT_STATE, "TCP_CLOSING (%x)\n", tcp_flags); if (tcp_ack) { action = TCP_PUSH; - current_tcp_state = TCP_CLOSED; + tcp->state = TCP_CLOSED; puts("\n"); } else if (tcp_syn) { action = TCP_RST; @@ -615,6 +615,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) u8 tcp_action = TCP_DATA; u32 tcp_seq_num, tcp_ack_num; int tcp_hdr_len, payload_len; + struct tcp_stream *tcp; /* Verify IP header */ debug_cond(DEBUG_DEV_PKT, @@ -643,11 +644,15 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) return; } + tcp = tcp_stream_get(); + if (tcp == NULL) + return; + tcp_hdr_len = GET_TCP_HDR_LEN_IN_BYTES(b->ip.hdr.tcp_hlen); payload_len = tcp_len - tcp_hdr_len; if (tcp_hdr_len > TCP_HDR_SIZE) - tcp_parse_options((uchar *)b + IP_TCP_HDR_SIZE, + tcp_parse_options(tcp, (uchar *)b + IP_TCP_HDR_SIZE, tcp_hdr_len - TCP_HDR_SIZE); /* * Incoming sequence and ack numbers are server's view of the numbers. @@ -657,7 +662,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) tcp_ack_num = ntohl(b->ip.hdr.tcp_ack); /* Packets are not ordered. Send to app as received. */ - tcp_action = tcp_state_machine(b->ip.hdr.tcp_flags, + tcp_action = tcp_state_machine(tcp, b->ip.hdr.tcp_flags, tcp_seq_num, payload_len); tcp_activity_count++; @@ -678,7 +683,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) } else if (tcp_action != TCP_DATA) { debug_cond(DEBUG_DEV_PKT, "TCP Action (action=%x,Seq=%u,Ack=%u,Pay=%d)\n", - tcp_action, tcp_ack_num, tcp_ack_edge, payload_len); + tcp_action, tcp_ack_num, tcp->ack_edge, payload_len); /* * Warning: Incoming Ack & Seq sequence numbers are transposed @@ -687,6 +692,6 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) net_send_tcp_packet(0, ntohs(b->ip.hdr.tcp_src), ntohs(b->ip.hdr.tcp_dst), (tcp_action & (~TCP_PUSH)), - tcp_ack_num, tcp_ack_edge); + tcp_ack_num, tcp->ack_edge); } } diff --git a/net/wget.c b/net/wget.c index f1dd7abeff6..c0a80597bfe 100644 --- a/net/wget.c +++ b/net/wget.c @@ -356,7 +356,8 @@ static void wget_handler(uchar *pkt, u16 dport, u32 tcp_seq_num, u32 tcp_ack_num, u8 action, unsigned int len) { - enum tcp_state wget_tcp_state = tcp_get_tcp_state(); + struct tcp_stream *tcp = tcp_stream_get(); + enum tcp_state wget_tcp_state = tcp_get_tcp_state(tcp); net_set_timeout_handler(wget_timeout, wget_timeout_handler); packets++; From patchwork Wed Aug 14 10:31:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972339 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=wDd1doZf; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPhw1Qybz1yYl for ; Wed, 14 Aug 2024 20:32:48 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EC477889EB; Wed, 14 Aug 2024 12:32:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="wDd1doZf"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C3659889A2; Wed, 14 Aug 2024 12:32:15 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_FILL_THIS_FORM_SHORT,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2071d.outbound.protection.outlook.com [IPv6:2a01:111:f403:2606::71d]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7AB27885FA for ; Wed, 14 Aug 2024 12:32:12 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=n5Af1kDCRyXbCm+iWfX8nIts6yBfr6HLUv475M+yt3tc2nE4zZ6JsVn78mSndqxy3/FdWgazp1ytIpTuvOBQZRNowOiyQMZ72NLua5ppMbszIlkIG6koiZTV7o/fbmqXpeFgR5eAYUZ/TUDcg/5wY+QFMcS2EFS7TaYx5aDK9wQsxQZ6dq3xtpO/Kl9m3pdoLjI4EDCXmQtonT9jZMTkCu/XbQ0fe0ctz4avjQ7JR7H8OsW1scNU/z+sVUiBhiaqNdnpXf5E0v6TBlXunU21BVmwyPldxl2dXGuL0XVcTaF6diPOVHn4Tcef3XQaFlx0FgjyCSELmJqTd4IDUYwNyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=G6WIKLBF06oFNj/Z7a3U07Ge+2zf4MQ3uSMagvNjC8g=; b=WtuWO+O3bmfjNRZXZiHIvjPXrFQ0Oy1YKLXpK85/ZO5qlyk6+6XDhuF9+WQ5qSfyoLJKcF+hjaGflHhHPB3AtY0HWkxe2VZ4sldlRDpefQ/0KRSDDoowWwcAO0YEikRmd6eajUM6IDLD37oXzIlQ0oP0P46Ftcfxl9ALP+2uO01EK6veYajXkvJw/QnYDcQegnp3taOV7vI9cp7w/IrxXy9K8ZzHLuIAAsx1RYqbdiKp3JXqYVROVTi5HTyK1m6JBHEZfZYCDs5l5a5Y4ORlue1/7WqEaUQGPtGxzgiMF9JyYAMuUKXBJzX5cr+Rk9STU/hgVDS3uJtNKDDKfmNO7w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=G6WIKLBF06oFNj/Z7a3U07Ge+2zf4MQ3uSMagvNjC8g=; b=wDd1doZfa2G4ZM5v4aVk91vs2ycwPtexaXo2TL7tHQcTgjABK/55mdcc7wS4W94knd35Dj9VBz1KKuDgnTSsjT9/YYS4p3oYeANYu6d75M8UL0VaTSaQbJ+TTU3csWLcxvBpDbOFlSXBgTGafm5qBAqnjM4K9iJSc+SWZCOkT49Qup23stLj43usOKXHmc4drY3sAYejw9LahO3WXaiPe9IWE//s5znMQnymDXzNJv/SzrWvTuRm0qkiF2nJdzIesRLIHwkG4ISibcHfwg3HSKa7YUuWWDYe8lCyi50wuKnVn+HA/v9Zk/1T6wGNtGDvMk9gZFqjzfpfJY0JZK8ChQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by PAWPR08MB8887.eurprd08.prod.outlook.com (2603:10a6:102:33b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.13; Wed, 14 Aug 2024 10:32:10 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:10 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 4/9] net/tcp: add connection info to tcp_stream structure Date: Wed, 14 Aug 2024 14:31:40 +0400 Message-Id: <20240814103145.1347645-5-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|PAWPR08MB8887:EE_ X-MS-Office365-Filtering-Correlation-Id: fb639759-86e4-4caa-7fc4-08dcbc4c5ab3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|7416014|52116014|376014|38350700014|921020; X-Microsoft-Antispam-Message-Info: K0Vq5IMAjvtkuvbMaJsxOxVHQLdknSC1Ht9r4AKsdlhEIs53BSXCLCo98V02wxg0bFoheF+ErDBDksso7wS8GU6bvCQ+NRWoARPFL70El9ycAqeBu0VdN/N30OQgyZzZQsayk7m2asyW/A9A8OICXzTcUcUw20FE/hQErkHCun27YH6eua9cS6k80FWDtXmeGHAdtTrUl/3kQlHyWlXqxe2hadRZ+UPSu0gOM8qHSA2VZrWbMb0EiFl9/pQoB7DChF/kdUr9HiaAlLKP9xOTb4yaHk1izx7tqfzOkgX+6HIQ58qHXTqcArpQtyf7Msgad+Ne1WjECRqLp1/nW8fc8Ejl9eUlSSGWvd9kkMPrEQx6svnDBuh5tUoij+lxsxd9Gf4GFKaRdY5zgMGYeG5bj24EgZdanq8KR7wUvtiqjcFyZBwniYLBhTZT2kFK0IZ4qgRUeqv/mGEEu6SPUQdDZr7x6JvFjKtl73xcnaDGyG23oAItTlS7m7RSpz6RAGjyEwRS1y5co/WbmtUbP8XjmwPluCRGXv2L3jeQ0PQRydCd3t7mN3FhHS6rlYNNaGM+KMH7rjDg0nNPcHzUOBUOMYZXmxZrGzOXN/taMADftSHyaIAqR7K2VomoeXftLf/gJexWlvxhkX2y9e86LlblNrv77MKYsnbsxebNWyVCxG9NnmburKwA9vnVtFAWnYPklR+K/RBUz+rmI2xRb0BCheWE/6Rn54v5DHsf3jg0bVw= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(7416014)(52116014)(376014)(38350700014)(921020); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8j9OGa1Jd8OdelF3GVJ4eeRTB3LX+tsFB4B1Q5B4IiwVz8AdsQyMzu6mNvDdweBld/e1JawJ1yMEOqx0xh1S3rCyKlABwhbeWzCPk1gLzrQzXp5GtPvAOaLw5odgUg8/xyZmleL4z72wtIV32BcCpYq+8j3NZ9KrjNc+EBr2Mker4mVfZraH1MvYkU29uIzvsri3BaO2fqwpUGfuDVWdW8/DZaESFWCZyDsUH10fty0u4o+M4NPO2u17jnNThLlhgDarao1b4oCLbjRqbCd7f7L9J9FsrW4ZZUH7t5YAap1bCGIH0egAgWTN0UX+Z66rTFC2lBonufMVYulIn6hbCN0jNgkEyZ0vOM+6JTU+VjOxfznecLHotVcz2KIoeIK5xdUP7ypHRfSoegugx7xOKEo7UGvu30BR7GBltjKX2lUDhFCmzeyBYKzWKB1P86XOn1o/bjA7WJcsoUQJDPK89zb9DXEePCGJNa0FKA2VirCSDuki8KjTIaFihDSKY2Ugf2Mhj0lfK32OdvrH3SeuF6eEIkc+5CiXisOBkFjIBNJ6XxMNkOn+9Wx94DcDuM3FxmVOSO/NPEpirDJLAFcg87XmBXF6qpIRaVfuBH27ef4MH4SYf+fNKeh+Otr9Z6tANrMEJy9NFJ4CS8rMeD+rUioyDqaRdwC+TYcAHcntlCqv6GsXwsw9J453kSrP/xWjhX6MqP2TP4M4vc8kSv/iZRlRU8Wje+VcNGD96CqdEf5j57VXQ5VA04y4pIYUUcAoWHeEeafhRyw6FXK1hzTXunrDeAmNKVoRT3WSdTjCJc+NBFgvAPJa9T5EEUoNQbUVUcjWCG2q4+J/yj90YbN4iteKNQ0v9RQ9z1viNfbqJlSO5arseO+pTXpnVjo1gj6MxHj99zZ/gaZNODfRpLpVnNCed27EASYA7Pv3DbMczU78jDyn2Baimsl56udgCdKjAXNGV1oZa5nNodaO4/HMQDKruwrnTf6yyJyppoMdEGLJW33y3AkedRONtbAyPzMLbNZEIBwZ+Ntykh1MMUWaJgTCreQ5+LgxrouSRSvuIMAIWI9WC8wYAkt42d08e6kdkVHhvzqGRrQDRA0or6Vtfb/A41+IDuH4+FHIhHrvJZZkEMbdr/t2VeX1Mie+VNbKLbloPj/DEuQMjs7ABPTBtR7Qr5fn/a0RF8Jzop1kq7Z9YQRvLkz6N5VZXkLouomFie1D9QZQ9gpIx4SylWLyvCebztYQCYOtnJJ0x4MMZl5wSVEnqLqlLJNI1k1p9xStB5qZouFG3SGfdegHHvsnhnqVjLGk1uOxwJk39hXvR6qjIJGWjbDztKff2ToDMHYIztiF3D5QEbbiIgDW8gSQ9IdUNpcJTr8LoRFF6kQh6SUDt1RytBTekzpXN4DuJXS7LDPVjdwAQap646zbVIB/XpWCfe5sU0RptdGUEt8ZS9R5r/Dz9+JmcgoMxYNvOA6Ce76/6pSL5Ui9/XLD1YPMmc9tumDNNSZukJXwlIhu3FY9yfsaCWFTqtXJCejr9Rr9JytiAf+U6cWq90Nr2tiMsnyT9bqV5OJumVZ/ZTsUhH8PaFwdCufxKIdtMvjePBn/V/KwtgbwSxDnlDAK74+2Rw== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: fb639759-86e4-4caa-7fc4-08dcbc4c5ab3 X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:10.6820 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 7dIRfsv4aO6oeciQNx4B0cvCtjkdduXeGLIw/z8biTa2G+5RZoC/sA30oTaxj9X2T6IWUtWofpPTfGPSUGubQVqREiqG8fdbx8pnHMUaRgY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB8887 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Changes: * Avoid use net_server_ip in tcp code, use tcp_stream data instead * Ignore packets from other connections if connection already created. This prevents us from connection break caused by other tcp stream. Signed-off-by: Mikhail Kshevetskiy Reviewed-by: Simon Glass --- include/net.h | 5 +- include/net/tcp.h | 57 +++++++++++++++++--- net/fastboot_tcp.c | 46 ++++++++-------- net/net.c | 12 ++--- net/tcp.c | 129 ++++++++++++++++++++++++++++++++++----------- net/wget.c | 52 +++++++----------- 6 files changed, 201 insertions(+), 100 deletions(-) diff --git a/include/net.h b/include/net.h index bb2ae20f52a..b0ce13e0a9d 100644 --- a/include/net.h +++ b/include/net.h @@ -667,6 +667,7 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport, /** * net_send_tcp_packet() - Transmit TCP packet. * @payload_len: length of payload + * @dhost: Destination host * @dport: Destination TCP port * @sport: Source TCP port * @action: TCP action to be performed @@ -675,8 +676,8 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport, * * Return: 0 on success, other value on failure */ -int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action, - u32 tcp_seq_num, u32 tcp_ack_num); +int net_send_tcp_packet(int payload_len, struct in_addr dhost, int dport, + int sport, u8 action, u32 tcp_seq_num, u32 tcp_ack_num); int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport, int payload_len); diff --git a/include/net/tcp.h b/include/net/tcp.h index 14aee64cb1c..f224d0cae2f 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -279,6 +279,9 @@ enum tcp_state { /** * struct tcp_stream - TCP data stream structure + * @rhost: Remote host, network byte order + * @rport: Remote port, host byte order + * @lport: Local port, host byte order * * @state: TCP connection state * @@ -291,6 +294,10 @@ enum tcp_state { * @lost: Used for SACK */ struct tcp_stream { + struct in_addr rhost; + u16 rport; + u16 lport; + /* TCP connection state */ enum tcp_state state; @@ -305,16 +312,53 @@ struct tcp_stream { struct tcp_sack_v lost; }; -struct tcp_stream *tcp_stream_get(void); +void tcp_init(void); + +typedef int tcp_incoming_filter(struct in_addr rhost, + u16 rport, u16 sport); + +/* + * This function sets user callback used to accept/drop incoming + * connections. Callback should: + * + Check TCP stream endpoint and make connection verdict + * - return non-zero value to accept connection + * - return zero to drop connection + * + * WARNING: If callback is NOT defined, all incoming connections + * will be dropped. + */ +void tcp_set_incoming_filter(tcp_incoming_filter *filter); + +/* + * tcp_stream_get -- Get or create TCP stream + * @is_new: if non-zero and no stream found, then create a new one + * @rhost: Remote host, network byte order + * @rport: Remote port, host byte order + * @lport: Local port, host byte order + * + * Returns: TCP stream structure or NULL (if not found/created) + */ +struct tcp_stream *tcp_stream_get(int is_new, struct in_addr rhost, + u16 rport, u16 lport); + +/* + * tcp_stream_connect -- Create new TCP stream for remote connection. + * @rhost: Remote host, network byte order + * @rport: Remote port, host byte order + * + * Returns: TCP new stream structure or NULL (if not created). + * Random local port will be used. + */ +struct tcp_stream *tcp_stream_connect(struct in_addr rhost, u16 rport); + +enum tcp_state tcp_stream_get_state(struct tcp_stream *tcp); -enum tcp_state tcp_get_tcp_state(struct tcp_stream *tcp); -void tcp_set_tcp_state(struct tcp_stream *tcp, enum tcp_state new_state); -int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, - int sport, int payload_len, +int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, u8 action, u32 tcp_seq_num, u32 tcp_ack_num); /** * rxhand_tcp() - An incoming packet handler. + * @tcp: TCP stream * @pkt: pointer to the application packet * @dport: destination TCP port * @sip: source IP address @@ -324,8 +368,7 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, * @action: TCP action (SYN, ACK, FIN, etc) * @len: packet length */ -typedef void rxhand_tcp(uchar *pkt, u16 dport, - struct in_addr sip, u16 sport, +typedef void rxhand_tcp(struct tcp_stream *tcp, uchar *pkt, u32 tcp_seq_num, u32 tcp_ack_num, u8 action, unsigned int len); void tcp_set_tcp_handler(rxhand_tcp *f); diff --git a/net/fastboot_tcp.c b/net/fastboot_tcp.c index d1fccbc7238..12a4d6690be 100644 --- a/net/fastboot_tcp.c +++ b/net/fastboot_tcp.c @@ -8,14 +8,14 @@ #include #include +#define FASTBOOT_TCP_PORT 5554 + static char command[FASTBOOT_COMMAND_LEN] = {0}; static char response[FASTBOOT_RESPONSE_LEN] = {0}; static const unsigned short handshake_length = 4; static const uchar *handshake = "FB01"; -static u16 curr_sport; -static u16 curr_dport; static u32 curr_tcp_seq_num; static u32 curr_tcp_ack_num; static unsigned int curr_request_len; @@ -25,34 +25,37 @@ static enum fastboot_tcp_state { FASTBOOT_DISCONNECTING } state = FASTBOOT_CLOSED; -static void fastboot_tcp_answer(u8 action, unsigned int len) +static void fastboot_tcp_answer(struct tcp_stream *tcp, u8 action, + unsigned int len) { const u32 response_seq_num = curr_tcp_ack_num; const u32 response_ack_num = curr_tcp_seq_num + (curr_request_len > 0 ? curr_request_len : 1); - net_send_tcp_packet(len, htons(curr_sport), htons(curr_dport), + net_send_tcp_packet(len, tcp->rhost, tcp->rport, tcp->lport, action, response_seq_num, response_ack_num); } -static void fastboot_tcp_reset(void) +static void fastboot_tcp_reset(struct tcp_stream *tcp) { - fastboot_tcp_answer(TCP_RST, 0); + fastboot_tcp_answer(tcp, TCP_RST, 0); state = FASTBOOT_CLOSED; } -static void fastboot_tcp_send_packet(u8 action, const uchar *data, unsigned int len) +static void fastboot_tcp_send_packet(struct tcp_stream *tcp, u8 action, + const uchar *data, unsigned int len) { uchar *pkt = net_get_async_tx_pkt_buf(); memset(pkt, '\0', PKTSIZE); pkt += net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; memcpy(pkt, data, len); - fastboot_tcp_answer(action, len); + fastboot_tcp_answer(tcp, action, len); memset(pkt, '\0', PKTSIZE); } -static void fastboot_tcp_send_message(const char *message, unsigned int len) +static void fastboot_tcp_send_message(struct tcp_stream *tcp, + const char *message, unsigned int len) { __be64 len_be = __cpu_to_be64(len); uchar *pkt = net_get_async_tx_pkt_buf(); @@ -63,12 +66,11 @@ static void fastboot_tcp_send_message(const char *message, unsigned int len) memcpy(pkt, &len_be, 8); pkt += 8; memcpy(pkt, message, len); - fastboot_tcp_answer(TCP_ACK | TCP_PUSH, len + 8); + fastboot_tcp_answer(tcp, TCP_ACK | TCP_PUSH, len + 8); memset(pkt, '\0', PKTSIZE); } -static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, - struct in_addr sip, u16 sport, +static void fastboot_tcp_handler_ipv4(struct tcp_stream *tcp, uchar *pkt, u32 tcp_seq_num, u32 tcp_ack_num, u8 action, unsigned int len) { @@ -77,8 +79,6 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, u8 tcp_fin = action & TCP_FIN; u8 tcp_push = action & TCP_PUSH; - curr_sport = sport; - curr_dport = dport; curr_tcp_seq_num = tcp_seq_num; curr_tcp_ack_num = tcp_ack_num; curr_request_len = len; @@ -89,17 +89,17 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, if (len != handshake_length || strlen(pkt) != handshake_length || memcmp(pkt, handshake, handshake_length) != 0) { - fastboot_tcp_reset(); + fastboot_tcp_reset(tcp); break; } - fastboot_tcp_send_packet(TCP_ACK | TCP_PUSH, + fastboot_tcp_send_packet(tcp, TCP_ACK | TCP_PUSH, handshake, handshake_length); state = FASTBOOT_CONNECTED; } break; case FASTBOOT_CONNECTED: if (tcp_fin) { - fastboot_tcp_answer(TCP_FIN | TCP_ACK, 0); + fastboot_tcp_answer(tcp, TCP_FIN | TCP_ACK, 0); state = FASTBOOT_DISCONNECTING; break; } @@ -111,12 +111,12 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, // Only single packet messages are supported ATM if (strlen(pkt) != command_size) { - fastboot_tcp_reset(); + fastboot_tcp_reset(tcp); break; } strlcpy(command, pkt, len + 1); fastboot_command_id = fastboot_handle_command(command, response); - fastboot_tcp_send_message(response, strlen(response)); + fastboot_tcp_send_message(tcp, response, strlen(response)); fastboot_handle_boot(fastboot_command_id, strncmp("OKAY", response, 4) == 0); } @@ -129,17 +129,21 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, memset(command, 0, FASTBOOT_COMMAND_LEN); memset(response, 0, FASTBOOT_RESPONSE_LEN); - curr_sport = 0; - curr_dport = 0; curr_tcp_seq_num = 0; curr_tcp_ack_num = 0; curr_request_len = 0; } +static int incoming_filter(struct in_addr rhost, u16 rport, u16 lport) +{ + return (lport == FASTBOOT_TCP_PORT); +} + void fastboot_tcp_start_server(void) { printf("Using %s device\n", eth_get_name()); printf("Listening for fastboot command on tcp %pI4\n", &net_ip); + tcp_set_incoming_filter(incoming_filter); tcp_set_tcp_handler(fastboot_tcp_handler_ipv4); } diff --git a/net/net.c b/net/net.c index 6c5ee7e0925..b33ea59a9fa 100644 --- a/net/net.c +++ b/net/net.c @@ -414,7 +414,7 @@ int net_init(void) /* Only need to setup buffer pointers once. */ first_call = 0; if (IS_ENABLED(CONFIG_PROT_TCP)) - tcp_set_tcp_state(tcp_stream_get(), TCP_CLOSED); + tcp_init(); } return net_init_loop(); @@ -899,10 +899,10 @@ int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport, } #if defined(CONFIG_PROT_TCP) -int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action, - u32 tcp_seq_num, u32 tcp_ack_num) +int net_send_tcp_packet(int payload_len, struct in_addr dhost, int dport, + int sport, u8 action, u32 tcp_seq_num, u32 tcp_ack_num) { - return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport, + return net_send_ip_packet(net_server_ethaddr, dhost, dport, sport, payload_len, IPPROTO_TCP, action, tcp_seq_num, tcp_ack_num); } @@ -944,12 +944,12 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport, break; #if defined(CONFIG_PROT_TCP) case IPPROTO_TCP: - tcp = tcp_stream_get(); + tcp = tcp_stream_get(0, dest, dport, sport); if (tcp == NULL) return -EINVAL; pkt_hdr_size = eth_hdr_size - + tcp_set_tcp_header(tcp, pkt + eth_hdr_size, dport, sport, + + tcp_set_tcp_header(tcp, pkt + eth_hdr_size, payload_len, action, tcp_seq_num, tcp_ack_num); break; diff --git a/net/tcp.c b/net/tcp.c index 6646f171b83..9acf9f3ccb2 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -26,6 +26,7 @@ static int tcp_activity_count; static struct tcp_stream tcp_stream; +static tcp_incoming_filter *incoming_filter; /* * TCP lengths are stored as a rounded up number of 32 bit words. @@ -40,40 +41,95 @@ static struct tcp_stream tcp_stream; /* Current TCP RX packet handler */ static rxhand_tcp *tcp_packet_handler; +#define RANDOM_PORT_START 1024 +#define RANDOM_PORT_RANGE 0x4000 + +/** + * random_port() - make port a little random (1024-17407) + * + * Return: random port number from 1024 to 17407 + * + * This keeps the math somewhat trivial to compute, and seems to work with + * all supported protocols/clients/servers + */ +static unsigned int random_port(void) +{ + return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE); +} + static inline s32 tcp_seq_cmp(u32 a, u32 b) { return (s32)(a - b); } /** - * tcp_get_tcp_state() - get TCP stream state + * tcp_stream_get_state() - get TCP stream state * @tcp: tcp stream * * Return: TCP stream state */ -enum tcp_state tcp_get_tcp_state(struct tcp_stream *tcp) +enum tcp_state tcp_stream_get_state(struct tcp_stream *tcp) { return tcp->state; } /** - * tcp_set_tcp_state() - set TCP stream state + * tcp_stream_set_state() - set TCP stream state * @tcp: tcp stream * @new_state: new TCP state */ -void tcp_set_tcp_state(struct tcp_stream *tcp, - enum tcp_state new_state) +static void tcp_stream_set_state(struct tcp_stream *tcp, + enum tcp_state new_state) { tcp->state = new_state; } -struct tcp_stream *tcp_stream_get(void) +void tcp_init(void) +{ + incoming_filter = NULL; + tcp_stream.state = TCP_CLOSED; +} + +void tcp_set_incoming_filter(tcp_incoming_filter *filter) +{ + incoming_filter = filter; +} + +static struct tcp_stream *tcp_stream_add(struct in_addr rhost, + u16 rport, u16 lport) +{ + struct tcp_stream *tcp = &tcp_stream; + + if (tcp->state != TCP_CLOSED) + return NULL; + + memset(tcp, 0, sizeof(struct tcp_stream)); + tcp->rhost.s_addr = rhost.s_addr; + tcp->rport = rport; + tcp->lport = lport; + tcp->state = TCP_CLOSED; + tcp->lost.len = TCP_OPT_LEN_2; + return tcp; +} + +struct tcp_stream *tcp_stream_get(int is_new, struct in_addr rhost, + u16 rport, u16 lport) { - return &tcp_stream; + struct tcp_stream *tcp = &tcp_stream; + + if ((tcp->rhost.s_addr == rhost.s_addr) && + (tcp->rport == rport) && + (tcp->lport == lport)) + return tcp; + + if (!is_new || (incoming_filter == NULL) || + !incoming_filter(rhost, rport, lport)) + return NULL; + + return tcp_stream_add(rhost, rport, lport); } -static void dummy_handler(uchar *pkt, u16 dport, - struct in_addr sip, u16 sport, +static void dummy_handler(struct tcp_stream *tcp, uchar *pkt, u32 tcp_seq_num, u32 tcp_ack_num, u8 action, unsigned int len) { @@ -222,8 +278,7 @@ void net_set_syn_options(struct tcp_stream *tcp, union tcp_build_pkt *b) b->ip.end = TCP_O_END; } -int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, - int sport, int payload_len, +int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, u8 action, u32 tcp_seq_num, u32 tcp_ack_num) { union tcp_build_pkt *b = (union tcp_build_pkt *)pkt; @@ -243,7 +298,7 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, case TCP_SYN: debug_cond(DEBUG_DEV_PKT, "TCP Hdr:SYN (%pI4, %pI4, sq=%u, ak=%u)\n", - &net_server_ip, &net_ip, + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); tcp_activity_count = 0; net_set_syn_options(tcp, b); @@ -264,13 +319,13 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, b->ip.hdr.tcp_flags = action; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:ACK (%pI4, %pI4, s=%u, a=%u, A=%x)\n", - &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num, + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num, action); break; case TCP_FIN: debug_cond(DEBUG_DEV_PKT, "TCP Hdr:FIN (%pI4, %pI4, s=%u, a=%u)\n", - &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num); + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); payload_len = 0; pkt_hdr_len = IP_TCP_HDR_SIZE; tcp->state = TCP_FIN_WAIT_1; @@ -279,7 +334,7 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, case TCP_RST: debug_cond(DEBUG_DEV_PKT, "TCP Hdr:RST (%pI4, %pI4, s=%u, a=%u)\n", - &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num); + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); tcp->state = TCP_CLOSED; break; /* Notify connection closing */ @@ -290,7 +345,7 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, debug_cond(DEBUG_DEV_PKT, "TCP Hdr:FIN ACK PSH(%pI4, %pI4, s=%u, a=%u, A=%x)\n", - &net_server_ip, &net_ip, + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num, action); fallthrough; default: @@ -298,7 +353,7 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, b->ip.hdr.tcp_flags = action | TCP_PUSH | TCP_ACK; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:dft (%pI4, %pI4, s=%u, a=%u, A=%x)\n", - &net_server_ip, &net_ip, + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num, action); } @@ -308,8 +363,8 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, tcp->ack_edge = tcp_ack_num; /* TCP Header */ b->ip.hdr.tcp_ack = htonl(tcp->ack_edge); - b->ip.hdr.tcp_src = htons(sport); - b->ip.hdr.tcp_dst = htons(dport); + b->ip.hdr.tcp_src = htons(tcp->lport); + b->ip.hdr.tcp_dst = htons(tcp->rport); b->ip.hdr.tcp_seq = htonl(tcp_seq_num); /* @@ -332,10 +387,10 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int dport, b->ip.hdr.tcp_xsum = 0; b->ip.hdr.tcp_ugr = 0; - b->ip.hdr.tcp_xsum = tcp_set_pseudo_header(pkt, net_ip, net_server_ip, + b->ip.hdr.tcp_xsum = tcp_set_pseudo_header(pkt, net_ip, tcp->rhost, tcp_len, pkt_len); - net_set_ip_header((uchar *)&b->ip, net_server_ip, net_ip, + net_set_ip_header((uchar *)&b->ip, tcp->rhost, net_ip, pkt_len, IPPROTO_TCP); return pkt_hdr_len; @@ -616,19 +671,26 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) u32 tcp_seq_num, tcp_ack_num; int tcp_hdr_len, payload_len; struct tcp_stream *tcp; + struct in_addr src; /* Verify IP header */ debug_cond(DEBUG_DEV_PKT, "TCP RX in RX Sum (to=%pI4, from=%pI4, len=%d)\n", &b->ip.hdr.ip_src, &b->ip.hdr.ip_dst, pkt_len); - b->ip.hdr.ip_src = net_server_ip; + /* + * src IP address will be destroyed by TCP checksum verification + * algorithm (see tcp_set_pseudo_header()), so remember it before + * it was garbaged. + */ + src.s_addr = b->ip.hdr.ip_src.s_addr; + b->ip.hdr.ip_dst = net_ip; b->ip.hdr.ip_sum = 0; if (tcp_rx_xsum != compute_ip_checksum(b, IP_HDR_SIZE)) { debug_cond(DEBUG_DEV_PKT, "TCP RX IP xSum Error (%pI4, =%pI4, len=%d)\n", - &net_ip, &net_server_ip, pkt_len); + &net_ip, &src, pkt_len); return; } @@ -640,11 +702,14 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) pkt_len)) { debug_cond(DEBUG_DEV_PKT, "TCP RX TCP xSum Error (%pI4, %pI4, len=%d)\n", - &net_ip, &net_server_ip, tcp_len); + &net_ip, &src, tcp_len); return; } - tcp = tcp_stream_get(); + tcp = tcp_stream_get(b->ip.hdr.tcp_flags & TCP_SYN, + src, + ntohs(b->ip.hdr.tcp_src), + ntohs(b->ip.hdr.tcp_dst)); if (tcp == NULL) return; @@ -676,9 +741,9 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) "TCP Notify (action=%x, Seq=%u,Ack=%u,Pay%d)\n", tcp_action, tcp_seq_num, tcp_ack_num, payload_len); - (*tcp_packet_handler) ((uchar *)b + pkt_len - payload_len, b->ip.hdr.tcp_dst, - b->ip.hdr.ip_src, b->ip.hdr.tcp_src, tcp_seq_num, - tcp_ack_num, tcp_action, payload_len); + (*tcp_packet_handler) (tcp, (uchar *)b + pkt_len - payload_len, + tcp_seq_num, tcp_ack_num, tcp_action, + payload_len); } else if (tcp_action != TCP_DATA) { debug_cond(DEBUG_DEV_PKT, @@ -689,9 +754,13 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) * Warning: Incoming Ack & Seq sequence numbers are transposed * here to outgoing Seq & Ack sequence numbers */ - net_send_tcp_packet(0, ntohs(b->ip.hdr.tcp_src), - ntohs(b->ip.hdr.tcp_dst), + net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, (tcp_action & (~TCP_PUSH)), tcp_ack_num, tcp->ack_edge); } } + +struct tcp_stream *tcp_stream_connect(struct in_addr rhost, u16 rport) +{ + return tcp_stream_add(rhost, rport, random_port()); +} diff --git a/net/wget.c b/net/wget.c index c0a80597bfe..ad5db21e97e 100644 --- a/net/wget.c +++ b/net/wget.c @@ -27,9 +27,8 @@ static const char http_eom[] = "\r\n\r\n"; static const char http_ok[] = "200"; static const char content_len[] = "Content-Length"; static const char linefeed[] = "\r\n"; -static struct in_addr web_server_ip; -static int our_port; static int wget_timeout_count; +struct tcp_stream *tcp; struct pkt_qd { uchar *pkt; @@ -137,22 +136,19 @@ static void wget_send_stored(void) int len = retry_len; unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len); unsigned int tcp_seq_num = retry_tcp_ack_num; - unsigned int server_port; uchar *ptr, *offset; - server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff; - switch (current_wget_state) { case WGET_CLOSED: debug_cond(DEBUG_WGET, "wget: send SYN\n"); current_wget_state = WGET_CONNECTING; - net_send_tcp_packet(0, server_port, our_port, action, + net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, action, tcp_seq_num, tcp_ack_num); packets = 0; break; case WGET_CONNECTING: pkt_q_idx = 0; - net_send_tcp_packet(0, server_port, our_port, action, + net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, action, tcp_seq_num, tcp_ack_num); ptr = net_tx_packet + net_eth_hdr_size() + @@ -167,14 +163,14 @@ static void wget_send_stored(void) memcpy(offset, &bootfile3, strlen(bootfile3)); offset += strlen(bootfile3); - net_send_tcp_packet((offset - ptr), server_port, our_port, + net_send_tcp_packet((offset - ptr), tcp->rhost, tcp->rport, tcp->lport, TCP_PUSH, tcp_seq_num, tcp_ack_num); current_wget_state = WGET_CONNECTED; break; case WGET_CONNECTED: case WGET_TRANSFERRING: case WGET_TRANSFERRED: - net_send_tcp_packet(0, server_port, our_port, action, + net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, action, tcp_seq_num, tcp_ack_num); break; } @@ -339,10 +335,8 @@ static void wget_connected(uchar *pkt, unsigned int tcp_seq_num, /** * wget_handler() - TCP handler of wget + * @tcp: TCP stream * @pkt: pointer to the application packet - * @dport: destination TCP port - * @sip: source IP address - * @sport: source TCP port * @tcp_seq_num: TCP sequential number * @tcp_ack_num: TCP acknowledgment number * @action: TCP action (SYN, ACK, FIN, etc) @@ -351,13 +345,11 @@ static void wget_connected(uchar *pkt, unsigned int tcp_seq_num, * In the "application push" invocation, the TCP header with all * its information is pointed to by the packet pointer. */ -static void wget_handler(uchar *pkt, u16 dport, - struct in_addr sip, u16 sport, +static void wget_handler(struct tcp_stream *tcp, uchar *pkt, u32 tcp_seq_num, u32 tcp_ack_num, u8 action, unsigned int len) { - struct tcp_stream *tcp = tcp_stream_get(); - enum tcp_state wget_tcp_state = tcp_get_tcp_state(tcp); + enum tcp_state wget_tcp_state = tcp_stream_get_state(tcp); net_set_timeout_handler(wget_timeout, wget_timeout_handler); packets++; @@ -441,26 +433,13 @@ static void wget_handler(uchar *pkt, u16 dport, } } -#define RANDOM_PORT_START 1024 -#define RANDOM_PORT_RANGE 0x4000 - -/** - * random_port() - make port a little random (1024-17407) - * - * Return: random port number from 1024 to 17407 - * - * This keeps the math somewhat trivial to compute, and seems to work with - * all supported protocols/clients/servers - */ -static unsigned int random_port(void) -{ - return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE); -} - #define BLOCKSIZE 512 void wget_start(void) { + struct in_addr web_server_ip; + unsigned int server_port; + image_url = strchr(net_boot_file_name, ':'); if (image_url > 0) { web_server_ip = string_to_ip(net_boot_file_name); @@ -513,8 +492,6 @@ void wget_start(void) wget_timeout_count = 0; current_wget_state = WGET_CLOSED; - our_port = random_port(); - /* * Zero out server ether to force arp resolution in case * the server ip for the previous u-boot command, for example dns @@ -523,6 +500,13 @@ void wget_start(void) memset(net_server_ethaddr, 0, 6); + server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff; + tcp = tcp_stream_connect(web_server_ip, server_port); + if (tcp == NULL) { + net_set_state(NETLOOP_FAIL); + return; + } + wget_send(TCP_SYN, 0, 0, 0); } From patchwork Wed Aug 14 10:31:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972340 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=I5hQgeR3; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPj65yZ9z1yYl for ; Wed, 14 Aug 2024 20:32:58 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5712088A11; Wed, 14 Aug 2024 12:32:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="I5hQgeR3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1BD8E885FA; Wed, 14 Aug 2024 12:32:18 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2070e.outbound.protection.outlook.com [IPv6:2a01:111:f403:2612::70e]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 24D27889C3 for ; Wed, 14 Aug 2024 12:32:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EG+M8XdcKuNAL6Lj6SvTGanbjjob2jbl1gjW56tPS5LZhnjxANLXXAADLax4FDJRSG8d/4VQ3/lOJIyNjIB82G9lnRCL3jvll5/FdB/mpBclJmFcuEVvx+zGRzjm5mKBCka0ewaJP+9FHq0KnQtUT+dqiS05JXzU+fGdVAUz4zIQmIcUFiuujuK9Dvy1OhdtbkEvmU84sP9VH+DkppvNLPNA0k1ZKJfS3Sz4AenbLj0zjs35hZXPJDPSBZzyirslGCtnySwab81FPmhNVF7xTRTCZ5J8vbg1dUC/ctJabgcYgZoaeJqUgUGRcMkGbiEPVqwbMvWrnbobgUcsLWPrdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4BXXh/0iWR2mIkwLJSLn1ApC3gFSPJ77Lkgt+exOdvQ=; b=Rh9UovhFUjn1+JoSSJv9vx7WWrYt0sznqLdAPHV+t2N3frgw4iFtr0LSX0CIQSt8kf/56AJPUEKP+qL+2jEsdjIaDFd1g95q/oOTfn/kAo7r7diHEW9zEmOhGiK957ANeHZH8blE/XMZ9dj+JT0Ltp4eObyaZJ8dD9OVoVsUlsWJi7w+Oe5GqMbG9dPvOWPpiSFiDmeSE+ofRX5+S9jGLU2NWPW67E/Y0FTybMLgVOSq4U3Z/pytynE9l69qcVd1NKA8weSqCJqa9EE4IyJc69aTY1axSdMn1Dt5wtqGT/9p6Pc1vJOBxTFqOC/BaTU+fRN9PZqpg27h+zfelf21dw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4BXXh/0iWR2mIkwLJSLn1ApC3gFSPJ77Lkgt+exOdvQ=; b=I5hQgeR36z93ZNANmixFxYrL93+dZLt00D8na4PNJVm5nH7zk4Gwo80PQH90F+sERGVkz3HpMeEvwJ+u3k+eET1yck/ZIa65gjYyJD+pmLRdmBTON3WxVIHUoCanUI1YPh11PZlS5txxUfZuyvKFCy4PjHLLmXg7vPG5ARH42WqQ1TleKFS9ugFsWvxvS+U3Sah3K5OGJbJeS1BoQVgXE0jffa47+mPvxoH9eUEHGSyQXWcLupPBmLr+lBYkzg46uDhlQhV3SfoX+ur5lciJIVQWUOe8rhwIXm4jt7l9UR5kiZT1f2bqnyENE9VgJk6H6POayTt2X8KYWACkYcjDJA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by DBBPR08MB6284.eurprd08.prod.outlook.com (2603:10a6:10:20e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.16; Wed, 14 Aug 2024 10:32:14 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:13 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 5/9] net/tcp: rename ack_edge and seq_init to more common rcv_nxt and irs Date: Wed, 14 Aug 2024 14:31:41 +0400 Message-Id: <20240814103145.1347645-6-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|DBBPR08MB6284:EE_ X-MS-Office365-Filtering-Correlation-Id: 6c69248a-0510-49f3-f05c-08dcbc4c5c96 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|7416014|1800799024|376014|52116014|921020|38350700014; X-Microsoft-Antispam-Message-Info: nxBeYt2IqRQC7prrP3g8akAfEOE5hB2IeSsC16XKlLDYk/0/GsXsicbD3QZJ+mk3V1G6uCrqIAQD2oQHp+Q8kx+dWtRSAPFXxxi5rBDhoAfE/Ww4BYotBH2lrgQKQHYjVpOl0+U2RcDC+8DuaElv5YYzz9OQIU7H+3Y+d0Le511NofAE5cuXAgJOR+Dc2NrcEz2TXKvXtSFGJuP/7Ny4Cww53CHRd56Tb6/cBtbqNDVes8sDzR1NghRh81voEUffbTxQpj+mAbJUXzZHfICt8vy+b+hrSOM4HgBu9gx+Xe6zegAPKSJEF3D7+Em5wJK53vuoVNnTunpDriguRInt3KNpi9bHiLWcS15+Qx1qmw4N4q9neNYDUSDb2K0ptCAGMVTMKaZ7KOZUiw+YxNqvSPfEV5zEJLBnKXlCJtAv7/QJtlguRMiubId73OMr9dvJgIctoATrxPTQHv9cQuGEICaNmnxPutRnjbTAmCCb/ohauMyD9gcXss5hH/uurX+GcLcduQc9Ubgmu+slzVTnQqnTeSaewAs92v8RaSOG+zw0uVpGWwNwvv/B1OuvHbzxWVvikCSE6XUTyUIOTJDoQh1/ohaTzZStgZL54O96DdRfoLgVHlVRqyyb5j30pJCgA+sSR+yvooqL+S47XI5/WS9JwcTyaMXjT7HWbVz1QlDCRftPQ1lbM7xq62UFPO1o8DN7qhp3InJe/v2TLha5SwE+Q+9P0ZEmKbG8wUzRg3Jb6RNlbQoiNa3lKcjDShgGa64YWy7fmPnrbjALUctctsV7w2KrHtKEPiC9eYnwC3M5qXMLQnyHcz8NaXytF8NVSssdxtlh+3EymLvJ8QwgZ1y5Bwk4B25u1s4IyiOnqtvppFHSNqR7MUxoaDHPeS9jI8PI5H7OLIESMeEOZLXC8UPE0IxvrjQpGRMgAiOxSqmYW5Kju/srI059RFAL/2BfNKT1Txz8exWA7pgvkUxguPvUbe3IqiiJbzzoY8JXqQ6JbUhtMwC7LnicKTvfEKhjymHrSvwKBTyBPa/u0zt6nNQ/ay4QaEnU0mL7hdn/NOFR/ZbUKFr+ErGhvxOyTWYixMNDS03let4NdwCkSINBwdz+gsutG3waG0Gqlg5FtyW0Q87XXsLB1vEGM47ziW3tIFYV4OYgHMrDhwKRmRrejvxoIU0QccLdysMVL1wClHlTdtAdokRg67DNFJFT6OLzDvreH8uKKBk1eyu2AMhiirT3dYA3yuqPdDJ2dA/8LWf9gTGu9ou17oaWYVxj98lr6EPMVZ+dTHuSmjdfi6maDFIukmlLC+6JXyaedXB9SfvltNhu8a98qrkV43FSydGIIAgwyF4qZM81wPy9Jzlf91N1MVfgruYMJf8SfVRxFQsaVOlWjAzs7Xw+b6Katds9iT+Z4BK8yvtpaaCU+olwJV1Dwx8UjkAvEjWn83i9Dvc= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(7416014)(1800799024)(376014)(52116014)(921020)(38350700014); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hnnBapYM1jA6+wO8PHa5nnBXigMPdCCO3WFtFHffoguunYmxV1hqKoWbprwjALaHNoc4f9VKVbUdFzjoRjRHAJVy+ZGziPPt9PZNhhg629/79dPdCEbJgFpI1jtpGDwD3YTp59qwl8VSWcONs7fXDNoF+VS7c6HxOe6hVndDV5RwDUdhhtRdqH1MH20Uo7TKCvYjc35m3eEhRYljQekXnyMkEAt39nTNRbykI8Pg/f6RT8Kznj3yJNUQLGq684rkNqGJv20o9lV5DpeRr2jylAQyJloOUy5SUFsx550W7MHCdtsj8xteSE/98EALyWojdD5yfEClzcrZvbmNCjZImWx6vSKxAjVsjVjQRPEdn+Hldyg5pdxL7w+nfGMbHTcArB9DtHikROKwtXEmwHH7UnjeW3sf6eK+aA4QoMXTvVMaCI79qlnNWJZgbWTevw1vbKmQ7FPtBsgM9MprHDcMj83pa0hFz9L87mNJvRS3OjKO+KykDNu+W0wThShtpugQo4KcqleJdBH8JTQFRrsSPJtwp/csyiHwPcnzI3V3nevNAF1tsPT4ZHXj95C0qokbuiQykZe2sqrzGcUlcdJH8UXav5cTzPCZgHO2uFxKIRic02VvWn9ZZwzlcIcaZVVEcePGBeif7oy+/fiTCuFJ0H/WYshaIEFaik218ge83NtrsT4B6YyL2vYxSlkbo+mZWRKYUgfzWXzEChQNeYXgDd0vvW2MmVu75n6lqx9HhH4lfULFs9aaCZrQPurI3sD9DNFGikfSKBa7nJ7cEjHU4yYXLEUWe8sIETY4TRy3MVWDH9bBuDGhpP302gx8N+Umk2xAqwD4WG40sdbqeJfTFCCtAjDXoCZ8aCsg0hTKt0oNMeBc63XRoeH9JIfxDEb2zctmr8YzlhkHHNzC650pig+WPfee9TFZOefGdcDIBawJgP4U6+ov7jHUYiyQpHUgO7t+Zw4j6FLxasGAwqFG0yuyUyYp3rIiyQhIFRiUWG678Yj7bzkecVi0lrgX3b2o+qUwus1ZgJ867oJ9h0T71UzksyoJID1lt0zhfnE1WnPzy5/s4pHp+0eUwKtoECFhmI90c6A74swlWqez4c4V7RlBy3VPgpFjjWDKGCd5F2o8TOsnQy4oVUSquVdbWSwLLVsWrMlIBuRN8bSuHyw4E5I83vG8uQdZP+1cprznq6ns9l72GUiReyGS3xo87lJsDlZ0zoRt/w6O9xi5V2uutdnqLzJIouFMdvyQQZ7zUvj+OWe7lGVYe9Wh8BCH22McCK6xN2i7c+HGr0gwrZK2KYOyMj6zkXuHWfiUwlvaHrfp4t6j5nWnhBVZRJe5LCiIzTZziWcEy+hcJJC5qpb1mIi2Svqezj0uOUYXowkgh3LA0X6d2glofPiJUv1yn1SCczwK51BEGr8/fTtyUmM6UNxIrrSSLDkd6XuJhKmuhDEDE+x12oeRGWRr7NyRCZf8oSpuIezKcJ6lpjCYqeDIIBWxYU+Np35wXoXu0ONprxo1UfRuOKCdDWIKiBbrygsp50wKumXZ2YrjlVpxVoewWud+azJinOXUz+LYO62opJZgxueS0G/XC87WnkrDAZelg9Pher569E/C19aIFLBK1Q== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: 6c69248a-0510-49f3-f05c-08dcbc4c5c96 X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:13.8621 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: HSeTeUh9pYGH0goPWInLq5BlhvaCV2PvMAJkjNJt6AZHYTcRTsVmVB62B9b5jQX4ylN6e1CGPdgpFnGd6r+QDmQwyYA8+yNY8sgoqbw58a0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR08MB6284 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Use the names from RFC 9293 Signed-off-by: Mikhail Kshevetskiy Reviewed-by: Simon Glass --- include/net/tcp.h | 8 ++++---- net/tcp.c | 32 ++++++++++++++++---------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index f224d0cae2f..0694af9d5b1 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -285,8 +285,8 @@ enum tcp_state { * * @state: TCP connection state * - * @seq_init: Initial receive sequence number - * @ack_edge: Receive next + * @irs: Initial receive sequence number + * @rcv_nxt: Receive next * * @loc_timestamp: Local timestamp * @rmt_timestamp: Remote timestamp @@ -301,8 +301,8 @@ struct tcp_stream { /* TCP connection state */ enum tcp_state state; - u32 seq_init; - u32 ack_edge; + u32 irs; + u32 rcv_nxt; /* TCP option timestamp */ u32 loc_timestamp; diff --git a/net/tcp.c b/net/tcp.c index 9acf9f3ccb2..96d499a42ed 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -360,9 +360,9 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, pkt_len = pkt_hdr_len + payload_len; tcp_len = pkt_len - IP_HDR_SIZE; - tcp->ack_edge = tcp_ack_num; + tcp->rcv_nxt = tcp_ack_num; /* TCP Header */ - b->ip.hdr.tcp_ack = htonl(tcp->ack_edge); + b->ip.hdr.tcp_ack = htonl(tcp->rcv_nxt); b->ip.hdr.tcp_src = htons(tcp->lport); b->ip.hdr.tcp_dst = htons(tcp->rport); b->ip.hdr.tcp_seq = htonl(tcp_seq_num); @@ -396,10 +396,10 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, return pkt_hdr_len; } -static void tcp_update_ack_edge(struct tcp_stream *tcp) +static void tcp_update_rcv_nxt(struct tcp_stream *tcp) { - if (tcp_seq_cmp(tcp->ack_edge, tcp->lost.hill[0].l) >= 0) { - tcp->ack_edge = tcp->lost.hill[0].r; + if (tcp_seq_cmp(tcp->rcv_nxt, tcp->lost.hill[0].l) >= 0) { + tcp->rcv_nxt = tcp->lost.hill[0].r; memmove(&tcp->lost.hill[0], &tcp->lost.hill[1], (TCP_SACK_HILLS - 1) * sizeof(struct sack_edges)); @@ -434,7 +434,7 @@ void tcp_hole(struct tcp_stream *tcp, u32 tcp_seq_num, u32 len) tcp_seq_num = tcp->lost.hill[i].l; } if (tcp_seq_cmp(tcp->lost.hill[i].r, tcp_seq_num + len) >= 0) { - tcp_update_ack_edge(tcp); + tcp_update_rcv_nxt(tcp); return; } @@ -463,12 +463,12 @@ void tcp_hole(struct tcp_stream *tcp, u32 tcp_seq_num, u32 len) } } - tcp_update_ack_edge(tcp); + tcp_update_rcv_nxt(tcp); return; } if (i == TCP_SACK_HILLS) { - tcp_update_ack_edge(tcp); + tcp_update_rcv_nxt(tcp); return; } @@ -489,7 +489,7 @@ void tcp_hole(struct tcp_stream *tcp, u32 tcp_seq_num, u32 len) tcp->lost.hill[i].r = tcp_seq_num + len; tcp->lost.len = TCP_OPT_LEN_2 + cnt * TCP_OPT_LEN_8; - tcp_update_ack_edge(tcp); + tcp_update_rcv_nxt(tcp); }; /** @@ -566,8 +566,8 @@ static u8 tcp_state_machine(struct tcp_stream *tcp, u8 tcp_flags, debug_cond(DEBUG_INT_STATE, "TCP CLOSED %x\n", tcp_flags); if (tcp_syn) { action = TCP_SYN | TCP_ACK; - tcp->seq_init = tcp_seq_num; - tcp->ack_edge = tcp_seq_num + 1; + tcp->irs = tcp_seq_num; + tcp->rcv_nxt = tcp_seq_num + 1; tcp->lost.len = TCP_OPT_LEN_2; tcp->state = TCP_SYN_RECEIVED; } else if (tcp_ack || tcp_fin) { @@ -583,8 +583,8 @@ static u8 tcp_state_machine(struct tcp_stream *tcp, u8 tcp_flags, tcp->state = TCP_CLOSE_WAIT; } else if (tcp_ack || (tcp_syn && tcp_ack)) { action |= TCP_ACK; - tcp->seq_init = tcp_seq_num; - tcp->ack_edge = tcp_seq_num + 1; + tcp->irs = tcp_seq_num; + tcp->rcv_nxt = tcp_seq_num + 1; tcp->state = TCP_ESTABLISHED; if (tcp_syn && tcp_ack) @@ -633,7 +633,7 @@ static u8 tcp_state_machine(struct tcp_stream *tcp, u8 tcp_flags, case TCP_FIN_WAIT_1: debug_cond(DEBUG_INT_STATE, "TCP_FIN_WAIT_1 (%x)\n", tcp_flags); if (tcp_fin) { - tcp->ack_edge++; + tcp->rcv_nxt++; action = TCP_ACK | TCP_FIN; tcp->state = TCP_FIN_WAIT_2; } @@ -748,7 +748,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) } else if (tcp_action != TCP_DATA) { debug_cond(DEBUG_DEV_PKT, "TCP Action (action=%x,Seq=%u,Ack=%u,Pay=%d)\n", - tcp_action, tcp_ack_num, tcp->ack_edge, payload_len); + tcp_action, tcp_ack_num, tcp->rcv_nxt, payload_len); /* * Warning: Incoming Ack & Seq sequence numbers are transposed @@ -756,7 +756,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) */ net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, (tcp_action & (~TCP_PUSH)), - tcp_ack_num, tcp->ack_edge); + tcp_ack_num, tcp->rcv_nxt); } } From patchwork Wed Aug 14 10:31:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972343 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=fWQZiHJS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPjW6Q90z1yYl for ; Wed, 14 Aug 2024 20:33:19 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3FC9388A13; Wed, 14 Aug 2024 12:32:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="fWQZiHJS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8C72B88A03; Wed, 14 Aug 2024 12:32:24 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on20713.outbound.protection.outlook.com [IPv6:2a01:111:f403:2608::713]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A181488A25 for ; Wed, 14 Aug 2024 12:32:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=LpE1pt00YLEpv/AeCmL7n9qK4O2wbJewzW54pWHa5X91xME/gpTzJGENTWRJq3CZ2qZ7PvxozYzx13UoFGBxkpn+1u7IMg13qTcEc5ViXJ9LyRqgZTUeifNbFt0griIkxPsgoyruWJ87U9auFaTz1o55nxftl827+MQwwTXjeVP964hY+SB90N7LWScMX3RM4gyQGGXMhY/3putJIl6q1rqDlR51wIevG5LU108RXmaJEGnTShY427c0z3CrynHZMuyjH0zb934mOFh9FpAQL3OS5JPB7HLmjDdSq3aPoJjvI83rLzdYN8Mv+F3EU2GwyQqQOFNrjUOuWAOlfkY73g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=x69BCrgZ1ttm7Ee7cQkY5Ut61HkxPAeNjZVl/bG3nOQ=; b=vstsnvRv9Nc2BELKpwSHWw+YdqBQMCU4OqTzip9lDrQHApKOST6XI1Ourk5QoIfTEU565reiX0i/6JMol9UJyAt1QHEUQcxK+gP0SLBperighxJJpeuiE76phf/Jl2UAFs1NLkyrLTt7L92wXVe7k3WfWRrs8oZo2jua8ULIBn+jXwd7CKkU7UD3SC962e6m9yoDysSnw0JU0Bu3/eE+1tRkpzOS5RK/h3YDPcliXr+3piBiZSybLU3vf3zYW2244W6EQJ2fasuI0f7Xb44gxS3v7J25eMnZc9SUkOtGaCh5RemYGW0DKANcgoKf31waHeS3qJJhfkk5TGqPzHnQeA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=x69BCrgZ1ttm7Ee7cQkY5Ut61HkxPAeNjZVl/bG3nOQ=; b=fWQZiHJS9aWNkhShBCsPbR4koD1MXt+NHmXaUcdR4jrVgqunEeQUnwsJeY5/+kzYk6M5WzB8raoW7iduNARvXdv3Yq23zfQNSkO0nWxDLMPLB4ia0AwWKrGKSxWj0JqIDibfdDDcJbMW7p/62OoMqQwWJotD/i5rycjucSw8Yb12JxbFw4fQ3c3glowfruLBO5WYnq2Kq5JoHIG0xb6hspoQpERdnveeBFnz8YzpVdsxdVJJ3L3LAnpbCm6NWc0AGB7eV+VZ97lB3KfNMbSUInYmgI5mrHIVxkWJHDeaOGS5wWDM1m6KOTBXvc3KYHHaBTGfzD7d/w7VGxqvjqgbxw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by PAWPR08MB8887.eurprd08.prod.outlook.com (2603:10a6:102:33b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.13; Wed, 14 Aug 2024 10:32:17 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:17 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 6/9] net/tcp: improve tcp framework, use better state machine Date: Wed, 14 Aug 2024 14:31:42 +0400 Message-Id: <20240814103145.1347645-7-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|PAWPR08MB8887:EE_ X-MS-Office365-Filtering-Correlation-Id: b49f8e1a-43f9-4d13-96ce-08dcbc4c5e8a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|7416014|52116014|376014|38350700014|921020; X-Microsoft-Antispam-Message-Info: kDbZFhLgh+Uwo6e0jqWymmAhdfQ3cBNzMvXkWBRI16aKlnlqDR9gz21NA++dkAv26CY8V8ky2c7dHeId04exH16Q5n0RpZ5BmG6089ltF3JIo3h2mXpU+pdj8rpJjxI6655RFq1uhlMObIDNEogPubiwNsmNClFgiLmTK/ZcLwVG8vuMviG2ltzcabGyiY6v9XenLVm6ts4ynPcmgAuv+9Nef1uVKHpH3qn/QY0a1jYHLc47Og1iX3JP+YvTVeoUZJMrxqzAp5tS7F5mjySfSjylqpa55VA1yLTmXS9BWVRhoBVISlqAwhSwyvc0m2iVl9UJXH31xtJ09DSp16vclackgDv3bF9AYnxP8Fssgy9ptA8I/MtcVMw8kVQsJkrvhPeXJUDYZlJ8EkVTfWWb8LBBMPBISIHiTpaJ8NIhypBwN6yHQrqtECuKD5I3T8M/6ILqAZ0hVhT28VpEfF8rVcHQzB5vKXqbe1EPF07E1J5nkAnNRNjcgSUksQm06XhdIMAn7NRTMCY9cYhWVUSdgai77khqB97spHo+7wbVb2wI1l1JHn0RwQfdFiYC8+hJWvBa58ZjPjCzt+vi0UIxP3KPoIXZgcGfi7AjXJwcuOWa5AItXTBg5TtK9OTEDgMTNRuCJR2o2MVdOTniKZpsYFyylQ82fz1mRZvlGsPlKsDm18+323OncZgZ5KWr1f1yRtfFv9411Gn3PK9qslW0CjJm+lfN2VLflq4HSAllfgw= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(7416014)(52116014)(376014)(38350700014)(921020); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: yn14zyosOvy/DYxTjZ986JTrlNi2Vy90hO/317GMug1KGRFHRjIg1O/zWtuoqVSRTD8LU9iTfV5HumSEFtAHUiNINfDl3vUpSEhxugFrT8ow6MA0VYjHeA9fwct71G7pvLYXCjCmyz3w3vpuF6sI5W2HuR6MCJ7X1mzxYb/4ihsmgDtcJ+Z6EPoI6x5RoalxoBSOLkrFprMAzzXYZBcqiWjBnrnttttVe+7RWltcqzi8PUCrlhaq1vv2e0Xh3mLxXjs9LAZSTolT2qYA8IafOI4VWQ4TjUwXKLhDmujbIW4O4INX1JtGB/KBbOBje4Gg1fua125JRtJaMQRMY3liC7z/PmVCSxMi8zJsiudEEMiv4gVYIDUJVWALa9tt/XtM/93QRae24ZvJSbxZ6f3KA+Rg45NfQ/158m8J+ZuaGsoaPE1f5ZSu9EJrD9p1a/JK0QbkJwqmFYZSDixh4wPRnP4D+rN3RB7nQ3igo05MzhB8p14K/+Oas7DJHtFGtD91r/abRZrLjCKezlJiEIQAKBaXmXbz6b4FCGHLWJNR3AzR3mSx79prwUwtCqLq40X2w7iwM8dbkruPKFFZnRR3EMM/8mWneDF0roe0j4zclzGdAkAcT5Fdc6urR/O3ftv6+1Uq6gZr59uplJn5Rzwf4dHBfLceEunn7ZYsUXnbFyCckPcBQ7OEhUmahucHh5qwhDhy8A0wwKSNRz4daKuAJA7u8a1Kw0ruZitY6DCi95fw1lMWdIr7pks/dh+ri27S137e83bRdbADrBZrZ3RD3bV7ZOE5ncR8cUTIu2yFAQTgpT5qV7EnDtuXlYFwvWy+Du5lios3CPsaVAwlnzXc7p/yucyygSUTAPyRP1UvonGkerBcJyxRuo5+pqcET8Gvzd67k2Q/EOcRzrZdaEunuv4rT0zaf6EizmK5O+0Pf5c252zBqz19SveucWpwUD2D5HunlFp3teESYJKJY9XQZ9qH1t7HfkEzoknAetJh5QGFNRGkuMwME8K247iGfu8NSDbJOdP5xen5y5RvkuwsHSwsnh7rBqlU1N70N88ev/BRgWtXFyCT1LVGwyQssgbUDmypkCUFAanQyYxUaQXDFo/kXpztwualhio3SLg9rXxQHVPX4GP0SzeATVi/Ks0sy815mtB5YFTXXPEbr78IH92HmDYay9KrhNIKp1amyp5mBtIejX5MY15PuZdsxSjjW+2m7v0ptvMoBd9IDVWXDT5V35no1OkBuv+LmJdW0p/2WC9xlAMivVeFCh8YE98WdwpXohu5v2fcVjrx84HI5nQDyz0VfnLMdQIc9lDIq3IkDR+b85AycTYalgrwawq26zZDm6HmQjte26naP8aL8td7qXjMHIht+yA7vV9J1v8+4RP7itVNXWi/zYqqRTedxAND/bBku5U1PG2nojgrORJICSVazT1i/QZERy7Cmj6X3mgTopXVoLNkIUpBV4m5eOTv9vszu4pXG2JIbJZxYNMl9K0Fk2WmEOkJXhBO0HAWZUU/T/0XJdrzDDyzw7/2XcgUHCvpX6ku9hr9IZfbm7Zj9Xm3G+p4I+1FEXB3KnTUkIEKYvNjLt+ojchyaIxvYaMkJ6QyKRnMsAaSdduiVg== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: b49f8e1a-43f9-4d13-96ce-08dcbc4c5e8a X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:17.2699 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: nkqXUwMmrlca59dvjul7BUnl/8vbZBxDcyiuAwnCUEpAL7rvNHkK72BGcKKtfOL0W1DlwAM0x7J7SjOCgxux7jXyDDqA7LZZrqBjpKY4qwY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB8887 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Changes: * Fix initial send sequence always zero issue * Use state machine close to RFC 9293. This should make TCP transfers more reliable (now we can upload a huge array of data from the board to external server) * Improve TCP framework a lot. This should make tcp client code much more simple. * rewrite wget with new tcp stack * rewrite fastboot_tcp with new tcp stack Signed-off-by: Mikhail Kshevetskiy --- include/net/tcp.h | 171 +++++++-- include/net/wget.h | 8 - net/fastboot_tcp.c | 190 +++++----- net/net.c | 4 + net/tcp.c | 837 ++++++++++++++++++++++++++++++++++----------- net/wget.c | 460 +++++++------------------ 6 files changed, 992 insertions(+), 678 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 0694af9d5b1..b070354e871 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -265,6 +265,7 @@ union tcp_build_pkt { * @TCP_CLOSING: Rec FIN, sent FIN, ACK waiting for ACK * @TCP_FIN_WAIT_1: Sent FIN waiting for response * @TCP_FIN_WAIT_2: Rec ACK from FIN sent, waiting for FIN + * @TCP_LAST_ACK: Waiting for ACK of the connection termination */ enum tcp_state { TCP_CLOSED, @@ -274,7 +275,20 @@ enum tcp_state { TCP_CLOSE_WAIT, TCP_CLOSING, TCP_FIN_WAIT_1, - TCP_FIN_WAIT_2 + TCP_FIN_WAIT_2, + TCP_LAST_ACK, +}; + +/** + * enum tcp_status - TCP stream status for connection + * @TCP_ERR_OK: no rx/tx errors + * @TCP_ERR_TOUT: rx/tx timeout happened + * @TCP_ERR_RST: connection was reset + */ +enum tcp_status { + TCP_ERR_OK = 0, + TCP_ERR_TOUT, + TCP_ERR_RST, }; /** @@ -283,51 +297,150 @@ enum tcp_state { * @rport: Remote port, host byte order * @lport: Local port, host byte order * + * @priv: User private data (not used by tcp module) + * + * @max_retry_count: Maximum retransmit attempts (default 3) + * @initial_timeout: Timeout from initial TX to reTX (default 2 sec) + * @rx_inactiv_timeout: Maximum time from last rx till connection drop + * (default 30 sec) + * + * @on_closed: User callback, called just before destroying TCP stream + * @on_established: User callback, called when TCP stream enters + * TCP_ESTABLISHED state + * @on_rcv_nxt_update: User callback, called when all data in the segment + * [0..rx_bytes - 1] was received + * @on_snd_una_update: User callback, called when all data in the segment + * [0..tx_bytes - 1] were transferred and acknowledged + * @rx: User callback, called on receive of segment + * [rx_offs..rx_offs+len-1]. If NULL -- all incoming data + * will be ignored. User SHOULD store the segment and + * return the number of accepted bytes. + * WARNING: Previous segmengs may not be received yet + * @tx: User callback, called on transmit/retransmit of segment + * [tx_offs..tx_offs+maxlen-1]. If NULL -- no data will + * be transmitted. User SHOULD fill provided buffer and + * return the number of bytes in the buffer. + * WARNING: do not use tcp_stream_close() from this + * callback (it will break stream). Better use + * on_snd_una_update() callback for such purposes. + * + * @time_last_rx: Arrival time of last valid incoming package (ticks) + * @time_start: Timeout start time (ticks) + * @time_delta: Timeout duration (ticks) + * @time_handler Timeout handler for a stream + * * @state: TCP connection state + * @status: TCP stream status (OK or ERR) + * + * @fin_rx: Non-zero if TCP_FIN was received + * @fin_rx_seq: TCP sequence of rx FIN bit + * @fin_tx: Non-zero if TCP_FIN was sent (or planned to send) + * @fin_tx_seq: TCP sequence of tx FIN bit + * + * @iss: Initial send sequence number + * @snd_una: Send unacknowledged + * @snd_nxt: Send next + * @snd_wnd: Send window (in bytes) + * @snd_wl1: Segment sequence number used for last window update + * @snd_wl2: Segment acknowledgment number used for last window update * * @irs: Initial receive sequence number * @rcv_nxt: Receive next + * @rcv_wnd: Receive window (in bytes) * * @loc_timestamp: Local timestamp * @rmt_timestamp: Remote timestamp * + * @rmt_win_scale: Remote window scale factor + * * @lost: Used for SACK + * + * @retry_cnt: Number of retry attempts remaining. Only SYN, FIN + * or DATA segments are tried to retransmit. + * @retry_timeout: Current retry timeout (ms) + * @retry_action: TCP flags used for sending + * @retry_seq_num: TCP sequence for retransmit + * retry_tx_len: Number of data to transmit + * @retry_tx_offs: Position in the TX stream */ struct tcp_stream { struct in_addr rhost; u16 rport; u16 lport; - /* TCP connection state */ + void *priv; + + int max_retry_count; + int initial_timeout; + int rx_inactiv_timeout; + + void (*on_closed)(struct tcp_stream *tcp); + void (*on_established)(struct tcp_stream *tcp); + void (*on_rcv_nxt_update)(struct tcp_stream *tcp, u32 rx_bytes); + void (*on_snd_una_update)(struct tcp_stream *tcp, u32 tx_bytes); + u32 (*rx)(struct tcp_stream *tcp, u32 rx_offs, void *buf, u32 len); + u32 (*tx)(struct tcp_stream *tcp, u32 tx_offs, void *buf, u32 maxlen); + + ulong time_last_rx; + ulong time_start; + ulong time_delta; + void (*time_handler)(struct tcp_stream *tcp); + enum tcp_state state; + enum tcp_status status; + + int fin_rx; + u32 fin_rx_seq; + + int fin_tx; + u32 fin_tx_seq; + + u32 iss; + u32 snd_una; + u32 snd_nxt; + u32 snd_wnd; + u32 snd_wl1; + u32 snd_wl2; u32 irs; u32 rcv_nxt; + u32 rcv_wnd; /* TCP option timestamp */ u32 loc_timestamp; u32 rmt_timestamp; + /* TCP window scale */ + u8 rmt_win_scale; + /* TCP sliding window control used to request re-TX */ struct tcp_sack_v lost; + + /* used for data retransmission */ + int retry_cnt; + int retry_timeout; + u8 retry_action; + u32 retry_seq_num; + u32 retry_tx_len; + u32 retry_tx_offs; }; void tcp_init(void); -typedef int tcp_incoming_filter(struct in_addr rhost, - u16 rport, u16 sport); - /* - * This function sets user callback used to accept/drop incoming - * connections. Callback should: + * This function sets user callback called on TCP stream creation. + * Callback should: * + Check TCP stream endpoint and make connection verdict * - return non-zero value to accept connection * - return zero to drop connection + * + Setup TCP stream callbacks like: on_closed(), on_established(), + * n_rcv_nxt_update(), on_snd_una_update(), rx() and tx(). + * + Setup other stream related data * - * WARNING: If callback is NOT defined, all incoming connections - * will be dropped. + * WARNING: User MUST setup TCP stream on_create handler. Without it + * no connection (including outgoung) will be created. */ -void tcp_set_incoming_filter(tcp_incoming_filter *filter); +void tcp_stream_set_on_create_handler(int (*on_create)(struct tcp_stream *)); /* * tcp_stream_get -- Get or create TCP stream @@ -351,28 +464,30 @@ struct tcp_stream *tcp_stream_get(int is_new, struct in_addr rhost, */ struct tcp_stream *tcp_stream_connect(struct in_addr rhost, u16 rport); -enum tcp_state tcp_stream_get_state(struct tcp_stream *tcp); +/* + * tcp_stream_put -- Return stream to a TCP subsystem. Subsystem will + * check stream and destroy it (if stream was already + * closed). Otherwize no stream change will happen. + * @tcp: TCP stream to put + */ +void tcp_stream_put(struct tcp_stream *tcp); + +enum tcp_state tcp_stream_get_state(struct tcp_stream *tcp); +enum tcp_status tcp_stream_get_status(struct tcp_stream *tcp); + +u32 tcp_stream_rx_offs(struct tcp_stream *tcp); +u32 tcp_stream_tx_offs(struct tcp_stream *tcp); + +/* reset tcp stream */ +void tcp_stream_reset(struct tcp_stream *tcp); +/* force TCP stream closing, do NOT use from tcp->tx callback */ +void tcp_stream_close(struct tcp_stream *tcp); + +void tcp_streams_poll(void); int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, u8 action, u32 tcp_seq_num, u32 tcp_ack_num); -/** - * rxhand_tcp() - An incoming packet handler. - * @tcp: TCP stream - * @pkt: pointer to the application packet - * @dport: destination TCP port - * @sip: source IP address - * @sport: source TCP port - * @tcp_seq_num: TCP sequential number - * @tcp_ack_num: TCP acknowledgment number - * @action: TCP action (SYN, ACK, FIN, etc) - * @len: packet length - */ -typedef void rxhand_tcp(struct tcp_stream *tcp, uchar *pkt, - u32 tcp_seq_num, u32 tcp_ack_num, - u8 action, unsigned int len); -void tcp_set_tcp_handler(rxhand_tcp *f); - void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int len); u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr src, struct in_addr dest, diff --git a/include/net/wget.h b/include/net/wget.h index 6714f7ea573..9a423b30414 100644 --- a/include/net/wget.h +++ b/include/net/wget.h @@ -8,14 +8,6 @@ */ void wget_start(void); -enum wget_state { - WGET_CLOSED, - WGET_CONNECTING, - WGET_CONNECTED, - WGET_TRANSFERRING, - WGET_TRANSFERRED -}; - #define DEBUG_WGET 0 /* Set to 1 for debug messages */ #define WGET_RETRY_COUNT 30 #define WGET_TIMEOUT 2000UL diff --git a/net/fastboot_tcp.c b/net/fastboot_tcp.c index 12a4d6690be..b6f7c417a21 100644 --- a/net/fastboot_tcp.c +++ b/net/fastboot_tcp.c @@ -10,140 +10,106 @@ #define FASTBOOT_TCP_PORT 5554 -static char command[FASTBOOT_COMMAND_LEN] = {0}; -static char response[FASTBOOT_RESPONSE_LEN] = {0}; - static const unsigned short handshake_length = 4; static const uchar *handshake = "FB01"; -static u32 curr_tcp_seq_num; -static u32 curr_tcp_ack_num; -static unsigned int curr_request_len; -static enum fastboot_tcp_state { - FASTBOOT_CLOSED, - FASTBOOT_CONNECTED, - FASTBOOT_DISCONNECTING -} state = FASTBOOT_CLOSED; - -static void fastboot_tcp_answer(struct tcp_stream *tcp, u8 action, - unsigned int len) -{ - const u32 response_seq_num = curr_tcp_ack_num; - const u32 response_ack_num = curr_tcp_seq_num + - (curr_request_len > 0 ? curr_request_len : 1); +static char rxbuf[sizeof(u64) + FASTBOOT_COMMAND_LEN + 1]; +static char txbuf[sizeof(u64) + FASTBOOT_RESPONSE_LEN + 1]; - net_send_tcp_packet(len, tcp->rhost, tcp->rport, tcp->lport, - action, response_seq_num, response_ack_num); -} +static u32 data_read; +static u32 tx_last_offs, tx_last_len; -static void fastboot_tcp_reset(struct tcp_stream *tcp) +static void tcp_stream_on_rcv_nxt_update(struct tcp_stream *tcp, u32 rx_bytes) { - fastboot_tcp_answer(tcp, TCP_RST, 0); - state = FASTBOOT_CLOSED; -} + u64 cmd_size; + __be64 len_be; + char saved; + int fastboot_command_id, len; + + if ((data_read == 0) && (rx_bytes >= handshake_length)) { + if (memcmp(rxbuf, handshake, handshake_length) != 0) { + printf("fastboot: bad handshake\n"); + tcp_stream_close(tcp); + return; + } -static void fastboot_tcp_send_packet(struct tcp_stream *tcp, u8 action, - const uchar *data, unsigned int len) -{ - uchar *pkt = net_get_async_tx_pkt_buf(); + tx_last_offs = 0; + tx_last_len = handshake_length; + memcpy(txbuf, handshake, handshake_length); + + data_read += handshake_length; + rx_bytes -= handshake_length; + if (rx_bytes > 0) + memmove(rxbuf, rxbuf + handshake_length, rx_bytes); + return; + } - memset(pkt, '\0', PKTSIZE); - pkt += net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; - memcpy(pkt, data, len); - fastboot_tcp_answer(tcp, action, len); - memset(pkt, '\0', PKTSIZE); + if (rx_bytes < sizeof(u64)) + return; + + memcpy(&cmd_size, rxbuf, sizeof(u64)); + cmd_size = __be64_to_cpu(cmd_size); + if (rx_bytes < sizeof(u64) + cmd_size) + return; + + saved = rxbuf[sizeof(u64) + cmd_size]; + rxbuf[sizeof(u64) + cmd_size] = '\0'; + fastboot_command_id = fastboot_handle_command(rxbuf + sizeof(u64), + txbuf + sizeof(u64)); + fastboot_handle_boot(fastboot_command_id, + strncmp("OKAY", txbuf + sizeof(u64), 4) != 0); + rxbuf[sizeof(u64) + cmd_size] = saved; + + len = strlen(txbuf + sizeof(u64)); + len_be = __cpu_to_be64(len); + memcpy(txbuf, &len_be, sizeof(u64)); + + tx_last_offs += tx_last_len; + tx_last_len = len + sizeof(u64); + + data_read += sizeof(u64) + cmd_size; + rx_bytes -= sizeof(u64) + cmd_size; + if (rx_bytes > 0) + memmove(rxbuf, rxbuf + sizeof(u64) + cmd_size, rx_bytes); } -static void fastboot_tcp_send_message(struct tcp_stream *tcp, - const char *message, unsigned int len) +static u32 tcp_stream_rx(struct tcp_stream *tcp, u32 rx_offs, void *buf, u32 len) { - __be64 len_be = __cpu_to_be64(len); - uchar *pkt = net_get_async_tx_pkt_buf(); - - memset(pkt, '\0', PKTSIZE); - pkt += net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; - // Put first 8 bytes as a big endian message length - memcpy(pkt, &len_be, 8); - pkt += 8; - memcpy(pkt, message, len); - fastboot_tcp_answer(tcp, TCP_ACK | TCP_PUSH, len + 8); - memset(pkt, '\0', PKTSIZE); + memcpy(rxbuf + rx_offs - data_read, buf, len); + return len; } -static void fastboot_tcp_handler_ipv4(struct tcp_stream *tcp, uchar *pkt, - u32 tcp_seq_num, u32 tcp_ack_num, - u8 action, unsigned int len) +static u32 tcp_stream_tx(struct tcp_stream *tcp, u32 tx_offs, void *buf, u32 maxlen) { - int fastboot_command_id; - u64 command_size; - u8 tcp_fin = action & TCP_FIN; - u8 tcp_push = action & TCP_PUSH; - - curr_tcp_seq_num = tcp_seq_num; - curr_tcp_ack_num = tcp_ack_num; - curr_request_len = len; - - switch (state) { - case FASTBOOT_CLOSED: - if (tcp_push) { - if (len != handshake_length || - strlen(pkt) != handshake_length || - memcmp(pkt, handshake, handshake_length) != 0) { - fastboot_tcp_reset(tcp); - break; - } - fastboot_tcp_send_packet(tcp, TCP_ACK | TCP_PUSH, - handshake, handshake_length); - state = FASTBOOT_CONNECTED; - } - break; - case FASTBOOT_CONNECTED: - if (tcp_fin) { - fastboot_tcp_answer(tcp, TCP_FIN | TCP_ACK, 0); - state = FASTBOOT_DISCONNECTING; - break; - } - if (tcp_push) { - // First 8 bytes is big endian message length - command_size = __be64_to_cpu(*(u64 *)pkt); - len -= 8; - pkt += 8; - - // Only single packet messages are supported ATM - if (strlen(pkt) != command_size) { - fastboot_tcp_reset(tcp); - break; - } - strlcpy(command, pkt, len + 1); - fastboot_command_id = fastboot_handle_command(command, response); - fastboot_tcp_send_message(tcp, response, strlen(response)); - fastboot_handle_boot(fastboot_command_id, - strncmp("OKAY", response, 4) == 0); - } - break; - case FASTBOOT_DISCONNECTING: - if (tcp_push) - state = FASTBOOT_CLOSED; - break; - } + /* by design: tx_offs >= tx_last_offs */ + if (tx_offs >= tx_last_offs + tx_last_len) + return 0; - memset(command, 0, FASTBOOT_COMMAND_LEN); - memset(response, 0, FASTBOOT_RESPONSE_LEN); - curr_tcp_seq_num = 0; - curr_tcp_ack_num = 0; - curr_request_len = 0; + maxlen = tx_last_offs + tx_last_len - tx_offs; + memcpy(buf, txbuf + (tx_offs - tx_last_offs), maxlen); + return maxlen; } -static int incoming_filter(struct in_addr rhost, u16 rport, u16 lport) +static int tcp_stream_on_create(struct tcp_stream *tcp) { - return (lport == FASTBOOT_TCP_PORT); + if (tcp->lport != FASTBOOT_TCP_PORT) + return 0; + + data_read = 0; + tx_last_offs = 0; + tx_last_len = 0; + + tcp->on_rcv_nxt_update = tcp_stream_on_rcv_nxt_update; + tcp->rx = tcp_stream_rx; + tcp->tx = tcp_stream_tx; + return 1; } void fastboot_tcp_start_server(void) { + memset(net_server_ethaddr, 0, 6); + tcp_stream_set_on_create_handler(tcp_stream_on_create); + printf("Using %s device\n", eth_get_name()); printf("Listening for fastboot command on tcp %pI4\n", &net_ip); - - tcp_set_incoming_filter(incoming_filter); - tcp_set_tcp_handler(fastboot_tcp_handler_ipv4); } diff --git a/net/net.c b/net/net.c index b33ea59a9fa..86182cc504e 100644 --- a/net/net.c +++ b/net/net.c @@ -646,6 +646,9 @@ restart: * errors that may have happened. */ eth_rx(); +#if defined(CONFIG_PROT_TCP) + tcp_streams_poll(); +#endif /* * Abort if ctrl-c was pressed. @@ -952,6 +955,7 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport, + tcp_set_tcp_header(tcp, pkt + eth_hdr_size, payload_len, action, tcp_seq_num, tcp_ack_num); + tcp_stream_put(tcp); break; #endif default: diff --git a/net/tcp.c b/net/tcp.c index 96d499a42ed..2c34556c26d 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -24,9 +24,18 @@ #include #include -static int tcp_activity_count; +#define TCP_SEND_RETRY 3 +#define TCP_SEND_TIMEOUT 2000UL +#define TCP_RX_INACTIVE_TIMEOUT 30000UL +#define TCP_START_SEQ_INC 2153 /* just large prime number */ +#define TCP_RCV_WND_SIZE (PKTBUFSRX * TCP_MSS) + +#define TCP_PACKET_OK 0 +#define TCP_PACKET_DROP 1 + static struct tcp_stream tcp_stream; -static tcp_incoming_filter *incoming_filter; + +static int (*tcp_stream_on_create)(struct tcp_stream *tcp); /* * TCP lengths are stored as a rounded up number of 32 bit words. @@ -35,12 +44,10 @@ static tcp_incoming_filter *incoming_filter; */ #define LEN_B_TO_DW(x) ((x) >> 2) #define ROUND_TCPHDR_LEN(x) (LEN_B_TO_DW((x) + 3)) +#define ROUND_TCPHDR_BYTES(x) (((x) + 3) & ~3) #define SHIFT_TO_TCPHDRLEN_FIELD(x) ((x) << 4) #define GET_TCP_HDR_LEN_IN_BYTES(x) ((x) >> 2) -/* Current TCP RX packet handler */ -static rxhand_tcp *tcp_packet_handler; - #define RANDOM_PORT_START 1024 #define RANDOM_PORT_RANGE 0x4000 @@ -62,6 +69,21 @@ static inline s32 tcp_seq_cmp(u32 a, u32 b) return (s32)(a - b); } +static inline u32 tcp_get_start_seq(void) +{ + static u32 tcp_seq_inc; + u32 tcp_seq; + + tcp_seq = (get_timer(0) & 0xffffffff) + tcp_seq_inc; + tcp_seq_inc += TCP_START_SEQ_INC; + return tcp_seq; +} + +static inline ulong msec_to_ticks(ulong msec) +{ + return msec * CONFIG_SYS_HZ / 1000; +} + /** * tcp_stream_get_state() - get TCP stream state * @tcp: tcp stream @@ -84,15 +106,70 @@ static void tcp_stream_set_state(struct tcp_stream *tcp, tcp->state = new_state; } +/** + * tcp_stream_get_status() - get TCP stream status + * @tcp: tcp stream + * + * Return: TCP stream status + */ +enum tcp_status tcp_stream_get_status(struct tcp_stream *tcp) +{ + return tcp->status; +} + +/** + * tcp_stream_set_status() - set TCP stream state + * @tcp: tcp stream + * @new_satus: new TCP stream status + */ +static void tcp_stream_set_status(struct tcp_stream *tcp, + enum tcp_state new_status) +{ + tcp->status = new_status; +} + +static void tcp_stream_init(struct tcp_stream *tcp, + struct in_addr rhost, u16 rport, u16 lport) +{ + memset(tcp, 0, sizeof(struct tcp_stream)); + tcp->rhost.s_addr = rhost.s_addr; + tcp->rport = rport; + tcp->lport = lport; + tcp->state = TCP_CLOSED; + tcp->lost.len = TCP_OPT_LEN_2; + tcp->rcv_wnd = TCP_RCV_WND_SIZE; + tcp->max_retry_count = TCP_SEND_RETRY; + tcp->initial_timeout = TCP_SEND_TIMEOUT; + tcp->rx_inactiv_timeout = TCP_RX_INACTIVE_TIMEOUT; + tcp->time_last_rx = get_timer(0); +} + +static void tcp_stream_destroy(struct tcp_stream *tcp) +{ + if (tcp->on_closed != NULL) + tcp->on_closed(tcp); + memset(tcp, 0, sizeof(struct tcp_stream)); +} + void tcp_init(void) { - incoming_filter = NULL; - tcp_stream.state = TCP_CLOSED; + static int initialized; + struct tcp_stream *tcp = &tcp_stream; + + tcp_stream_on_create = NULL; + if (!initialized) { + initialized = 1; + memset(tcp, 0, sizeof(struct tcp_stream)); + } + + tcp_stream_set_state(tcp, TCP_CLOSED); + tcp_stream_set_status(tcp, TCP_ERR_RST); + tcp_stream_destroy(tcp); } -void tcp_set_incoming_filter(tcp_incoming_filter *filter) +void tcp_stream_set_on_create_handler(int (*on_create)(struct tcp_stream *)) { - incoming_filter = filter; + tcp_stream_on_create = on_create; } static struct tcp_stream *tcp_stream_add(struct in_addr rhost, @@ -100,15 +177,14 @@ static struct tcp_stream *tcp_stream_add(struct in_addr rhost, { struct tcp_stream *tcp = &tcp_stream; - if (tcp->state != TCP_CLOSED) + if ((tcp_stream_on_create == NULL) || + (tcp->state != TCP_CLOSED)) + return NULL; + + tcp_stream_init(tcp, rhost, rport, lport); + if (!tcp_stream_on_create(tcp)) return NULL; - memset(tcp, 0, sizeof(struct tcp_stream)); - tcp->rhost.s_addr = rhost.s_addr; - tcp->rport = rport; - tcp->lport = lport; - tcp->state = TCP_CLOSED; - tcp->lost.len = TCP_OPT_LEN_2; return tcp; } @@ -122,30 +198,203 @@ struct tcp_stream *tcp_stream_get(int is_new, struct in_addr rhost, (tcp->lport == lport)) return tcp; - if (!is_new || (incoming_filter == NULL) || - !incoming_filter(rhost, rport, lport)) - return NULL; + return is_new ? tcp_stream_add(rhost, rport, lport) : NULL; +} - return tcp_stream_add(rhost, rport, lport); +void tcp_stream_put(struct tcp_stream *tcp) +{ + if (tcp->state == TCP_CLOSED) + tcp_stream_destroy(tcp); } -static void dummy_handler(struct tcp_stream *tcp, uchar *pkt, - u32 tcp_seq_num, u32 tcp_ack_num, - u8 action, unsigned int len) +u32 tcp_stream_rx_offs(struct tcp_stream *tcp) { + u32 ret; + + switch (tcp->state) { + case TCP_CLOSED: + case TCP_SYN_SENT: + case TCP_SYN_RECEIVED: + return 0; + default: + break; + } + + ret = tcp->rcv_nxt - tcp->irs - 1; + if (tcp->fin_rx && (tcp->rcv_nxt == tcp->fin_rx_seq)) + ret--; + return ret; } -/** - * tcp_set_tcp_handler() - set a handler to receive data - * @f: handler - */ -void tcp_set_tcp_handler(rxhand_tcp *f) +u32 tcp_stream_tx_offs(struct tcp_stream *tcp) { - debug_cond(DEBUG_INT_STATE, "--- net_loop TCP handler set (%p)\n", f); - if (!f) - tcp_packet_handler = dummy_handler; - else - tcp_packet_handler = f; + u32 ret; + + switch (tcp->state) { + case TCP_CLOSED: + case TCP_SYN_SENT: + case TCP_SYN_RECEIVED: + return 0; + default: + break; + } + + ret = tcp->snd_una - tcp->iss - 1; + if (tcp->fin_tx && (tcp->snd_una == tcp->fin_tx_seq + 1)) + ret--; + return ret; +} + +static void tcp_stream_set_time_handler(struct tcp_stream *tcp, ulong msec, + void (*handler)(struct tcp_stream *)) +{ + if (msec == 0) { + tcp->time_handler = NULL; + return; + } + + tcp->time_handler = handler; + tcp->time_start = get_timer(0); + tcp->time_delta = msec_to_ticks(msec); +} + +static void tcp_send_packet(struct tcp_stream *tcp, u8 action, + u32 tcp_seq_num, u32 tcp_ack_num, u32 tx_len) +{ + net_send_tcp_packet(tx_len, tcp->rhost, tcp->rport, + tcp->lport, action, tcp_seq_num, + tcp_ack_num); +} + +static void tcp_send_repeat(struct tcp_stream *tcp) +{ + uchar *ptr; + u32 tcp_opts_size; + + if (tcp->retry_cnt == 0) { + puts("\nTCP: send retry counter exceeded\n"); + tcp_send_packet(tcp, TCP_RST, tcp->retry_seq_num, + tcp->rcv_nxt, 0); + tcp_stream_set_status(tcp, TCP_ERR_TOUT); + tcp_stream_set_state(tcp, TCP_CLOSED); + tcp_stream_destroy(tcp); + return; + } + tcp->retry_cnt--; + tcp->retry_timeout += tcp->initial_timeout; + + if (tcp->retry_tx_len > 0) { + tcp_opts_size = ROUND_TCPHDR_BYTES(TCP_TSOPT_SIZE + + tcp->lost.len); + ptr = net_tx_packet + net_eth_hdr_size() + + IP_TCP_HDR_SIZE + tcp_opts_size; + + if (tcp->retry_tx_len > TCP_MSS - tcp_opts_size) + tcp->retry_tx_len = TCP_MSS - tcp_opts_size; + + /* refill packet data */ + tcp->tx(tcp, tcp->retry_tx_offs, ptr, tcp->retry_tx_len); + } + tcp_send_packet(tcp, tcp->retry_action, tcp->retry_seq_num, + tcp->rcv_nxt, tcp->retry_tx_len); + + tcp_stream_set_time_handler(tcp, tcp->retry_timeout, tcp_send_repeat); +} + +static void tcp_send_packet_with_retry(struct tcp_stream *tcp, u8 action, + u32 tcp_seq_num, u32 tx_len, u32 tx_offs) +{ + tcp->retry_cnt = tcp->max_retry_count; + tcp->retry_timeout = tcp->initial_timeout; + tcp->retry_action = action; + tcp->retry_seq_num = tcp_seq_num; + tcp->retry_tx_len = tx_len; + tcp->retry_tx_offs = tx_offs; + + tcp_send_packet(tcp, action, tcp_seq_num, tcp->rcv_nxt, tx_len); + tcp_stream_set_time_handler(tcp, tcp->retry_timeout, tcp_send_repeat); +} + +static inline u8 tcp_stream_fin_needed(struct tcp_stream *tcp, u32 tcp_seq_num) +{ + return (tcp->fin_tx && (tcp_seq_num == tcp->fin_tx_seq)) ? TCP_FIN : 0; +} + +static void tcp_steam_tx_try(struct tcp_stream *tcp) +{ + uchar *ptr; + u32 tx_offs, tx_len, tcp_opts_size; + + if ((tcp->state != TCP_ESTABLISHED) || + (tcp->time_handler != NULL) || + (tcp->tx == NULL)) + return; + + tcp_opts_size = ROUND_TCPHDR_BYTES(TCP_TSOPT_SIZE + tcp->lost.len); + tx_len = TCP_MSS - tcp_opts_size; + if (tcp->fin_tx) { + /* do not try to send beyonds FIN packet limits */ + if (tcp_seq_cmp(tcp->snd_una, tcp->fin_tx_seq) >= 0) + return; + + tx_len = tcp->fin_tx_seq - tcp->snd_una; + if (tx_len > TCP_MSS - tcp_opts_size) + tx_len = TCP_MSS - tcp_opts_size; + } + + tx_offs = tcp_stream_tx_offs(tcp); + ptr = net_tx_packet + net_eth_hdr_size() + + IP_TCP_HDR_SIZE + tcp_opts_size; + + /* fill packet data and adjust size */ + tx_len = tcp->tx(tcp, tx_offs, ptr, tx_len); + if (tx_len == 0) + return; + + if (tcp_seq_cmp(tcp->snd_una + tx_len, tcp->snd_nxt) > 0) + tcp->snd_nxt = tcp->snd_una + tx_len; + + tcp_send_packet_with_retry(tcp, TCP_ACK | TCP_PUSH, + tcp->snd_una, tx_len, tx_offs); +} + +static void tcp_stream_poll(struct tcp_stream *tcp, ulong time) +{ + ulong delta; + void (*handler)(struct tcp_stream *tcp); + + if (tcp->state == TCP_CLOSED) + return; + + /* handle rx inactivity timeout */ + delta = msec_to_ticks(tcp->rx_inactiv_timeout); + if (time - tcp->time_last_rx >= delta) { + puts("\nTCP: rx inactivity timeout exceeded\n"); + tcp_stream_reset(tcp); + tcp_stream_set_status(tcp, TCP_ERR_TOUT); + tcp_stream_destroy(tcp); + return; + } + + /* handle retransmit timeout */ + if ((tcp->time_handler != NULL) && + (time - tcp->time_start >= tcp->time_delta)) { + handler = tcp->time_handler; + tcp->time_handler = NULL; + handler(tcp); + } + + tcp_steam_tx_try(tcp); +} + +void tcp_streams_poll(void) +{ + ulong time; + struct tcp_stream *tcp; + + time = get_timer(0); + tcp = &tcp_stream; + tcp_stream_poll(tcp, time); } /** @@ -300,18 +549,8 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, "TCP Hdr:SYN (%pI4, %pI4, sq=%u, ak=%u)\n", &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); - tcp_activity_count = 0; net_set_syn_options(tcp, b); - tcp_seq_num = 0; - tcp_ack_num = 0; pkt_hdr_len = IP_TCP_O_SIZE; - if (tcp->state == TCP_SYN_SENT) { /* Too many SYNs */ - action = TCP_FIN; - tcp->state = TCP_FIN_WAIT_1; - } else { - tcp->lost.len = TCP_OPT_LEN_2; - tcp->state = TCP_SYN_SENT; - } break; case TCP_SYN | TCP_ACK: case TCP_ACK: @@ -328,21 +567,16 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); payload_len = 0; pkt_hdr_len = IP_TCP_HDR_SIZE; - tcp->state = TCP_FIN_WAIT_1; break; case TCP_RST | TCP_ACK: case TCP_RST: debug_cond(DEBUG_DEV_PKT, "TCP Hdr:RST (%pI4, %pI4, s=%u, a=%u)\n", &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); - tcp->state = TCP_CLOSED; break; /* Notify connection closing */ case (TCP_FIN | TCP_ACK): case (TCP_FIN | TCP_ACK | TCP_PUSH): - if (tcp->state == TCP_CLOSE_WAIT) - tcp->state = TCP_CLOSING; - debug_cond(DEBUG_DEV_PKT, "TCP Hdr:FIN ACK PSH(%pI4, %pI4, s=%u, a=%u, A=%x)\n", &tcp->rhost, &net_ip, @@ -382,7 +616,7 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, * it is, then the u-boot tftp or nfs kernel netboot should be * considered. */ - b->ip.hdr.tcp_win = htons(PKTBUFSRX * TCP_MSS >> TCP_SCALE); + b->ip.hdr.tcp_win = htons(tcp->rcv_wnd >> TCP_SCALE); b->ip.hdr.tcp_xsum = 0; b->ip.hdr.tcp_ugr = 0; @@ -501,6 +735,7 @@ void tcp_hole(struct tcp_stream *tcp, u32 tcp_seq_num, u32 len) void tcp_parse_options(struct tcp_stream *tcp, uchar *o, int o_len) { struct tcp_t_opt *tsopt; + struct tcp_scale *wsopt; uchar *p = o; /* @@ -515,10 +750,13 @@ void tcp_parse_options(struct tcp_stream *tcp, uchar *o, int o_len) case TCP_O_END: return; case TCP_O_MSS: - case TCP_O_SCL: case TCP_P_SACK: case TCP_V_SACK: break; + case TCP_O_SCL: + wsopt = (struct tcp_scale *)p; + tcp->rmt_win_scale = wsopt->scale; + break; case TCP_O_TS: tsopt = (struct tcp_t_opt *)p; tcp->rmt_timestamp = tsopt->t_snd; @@ -533,129 +771,330 @@ void tcp_parse_options(struct tcp_stream *tcp, uchar *o, int o_len) } } -static u8 tcp_state_machine(struct tcp_stream *tcp, u8 tcp_flags, - u32 tcp_seq_num, int payload_len) +static int tcp_seg_in_wnd(struct tcp_stream *tcp, + u32 tcp_seq_num, int payload_len) +{ + if ((payload_len == 0) && (tcp->rcv_wnd == 0)) { + if (tcp_seq_num == tcp->rcv_nxt) + return 1; + } + if ((payload_len == 0) && (tcp->rcv_wnd > 0)) { + if ((tcp_seq_cmp(tcp->rcv_nxt, tcp_seq_num) <= 0) && + (tcp_seq_cmp(tcp_seq_num, tcp->rcv_nxt + tcp->rcv_wnd) < 0)) + return 1; + } + if ((payload_len > 0) && (tcp->rcv_wnd > 0)) { + if ((tcp_seq_cmp(tcp->rcv_nxt, tcp_seq_num) <= 0) && + (tcp_seq_cmp(tcp_seq_num, tcp->rcv_nxt + tcp->rcv_wnd) < 0)) + return 1; + tcp_seq_num += payload_len - 1; + if ((tcp_seq_cmp(tcp->rcv_nxt, tcp_seq_num) <= 0) && + (tcp_seq_cmp(tcp_seq_num, tcp->rcv_nxt + tcp->rcv_wnd) < 0)) + return 1; + } + return 0; +} + +static int tcp_rx_check_ack_num(struct tcp_stream *tcp, u32 tcp_seq_num, + u32 tcp_ack_num, u32 tcp_win_size) +{ + u32 old_offs, new_offs; + u8 action; + + switch (tcp->state) { + case TCP_SYN_RECEIVED: + if ((tcp_seq_cmp(tcp->snd_una, tcp_ack_num) >= 0) || + (tcp_seq_cmp(tcp_ack_num, tcp->snd_nxt) > 0)) { + // segment acknowledgment is not acceptable + tcp_send_packet(tcp, TCP_RST, tcp_ack_num, 0, 0); + return TCP_PACKET_DROP; + } + + tcp_stream_set_state(tcp, TCP_ESTABLISHED); + tcp->snd_wnd = tcp_win_size; + tcp->snd_wl1 = tcp_seq_num; + tcp->snd_wl2 = tcp_ack_num; + + if (tcp->on_established != NULL) + tcp->on_established(tcp); + + fallthrough; + + case TCP_ESTABLISHED: + case TCP_FIN_WAIT_1: + case TCP_FIN_WAIT_2: + case TCP_CLOSE_WAIT: + case TCP_CLOSING: + if (tcp_seq_cmp(tcp_ack_num, tcp->snd_nxt) > 0) { + // ACK acks something not yet sent + action = tcp_stream_fin_needed(tcp, tcp->snd_una) | TCP_ACK; + tcp_send_packet(tcp, action, tcp->snd_una, tcp->rcv_nxt, 0); + return TCP_PACKET_DROP; + } + + if (tcp_seq_cmp(tcp->snd_una, tcp_ack_num) < 0) { + old_offs = tcp_stream_tx_offs(tcp); + tcp->snd_una = tcp_ack_num; + new_offs = tcp_stream_tx_offs(tcp); + if ((tcp->time_handler != NULL) && + (tcp_seq_cmp(tcp->snd_una, tcp->retry_seq_num) > 0)) { + tcp_stream_set_time_handler(tcp, 0, NULL); + } + if ((tcp->on_snd_una_update != NULL) && + (old_offs != new_offs)) + tcp->on_snd_una_update(tcp, new_offs); + } + + if (tcp_seq_cmp(tcp->snd_una, tcp_ack_num) <= 0) { + if ((tcp_seq_cmp(tcp->snd_wl1, tcp_seq_num) < 0) || + ((tcp->snd_wl1 == tcp_seq_num) && + (tcp_seq_cmp(tcp->snd_wl2, tcp_seq_num) <= 0))) { + tcp->snd_wnd = tcp_win_size; + tcp->snd_wl1 = tcp_seq_num; + tcp->snd_wl2 = tcp_ack_num; + } + } + + if (tcp->state == TCP_FIN_WAIT_1) { + if (tcp->snd_una == tcp->snd_nxt) + tcp_stream_set_state(tcp, TCP_FIN_WAIT_2); + } + + if (tcp->state == TCP_CLOSING) { + if (tcp->snd_una == tcp->snd_nxt) + tcp_stream_set_state(tcp, TCP_CLOSED); + } + return TCP_PACKET_OK; + + case TCP_LAST_ACK: + if (tcp_ack_num == tcp->snd_nxt) + tcp_stream_set_state(tcp, TCP_CLOSED); + return TCP_PACKET_OK; + + default: + return TCP_PACKET_DROP; + } +} + +static int tcp_rx_user_data(struct tcp_stream *tcp, u32 tcp_seq_num, + char *buf, int len) +{ + u32 tmp_len, buf_offs, old_offs, new_offs; + u8 action; + + if (len == 0) + return TCP_PACKET_OK; + + switch (tcp->state) { + case TCP_ESTABLISHED: + case TCP_FIN_WAIT_1: + case TCP_FIN_WAIT_2: + break; + default: + return TCP_PACKET_DROP; + } + + tmp_len = len; + old_offs = tcp_stream_rx_offs(tcp); + buf_offs = tcp_seq_num - tcp->irs - 1; + if (tcp->rx != NULL) + tmp_len = tcp->rx(tcp, buf_offs, buf, len); + if (tmp_len) + tcp_hole(tcp, tcp_seq_num, tmp_len); + + new_offs = tcp_stream_rx_offs(tcp); + if ((tcp->on_rcv_nxt_update != NULL) && (old_offs != new_offs)) + tcp->on_rcv_nxt_update(tcp, new_offs); + + action = tcp_stream_fin_needed(tcp, tcp->snd_una) | TCP_ACK; + tcp_send_packet(tcp, action, tcp->snd_una, tcp->rcv_nxt, 0); + return TCP_PACKET_OK; +} + +void tcp_rx_state_machine(struct tcp_stream *tcp, + union tcp_build_pkt *b, unsigned int pkt_len) { - u8 tcp_fin = tcp_flags & TCP_FIN; - u8 tcp_syn = tcp_flags & TCP_SYN; - u8 tcp_rst = tcp_flags & TCP_RST; - u8 tcp_push = tcp_flags & TCP_PUSH; - u8 tcp_ack = tcp_flags & TCP_ACK; - u8 action = TCP_DATA; + int tcp_len = pkt_len - IP_HDR_SIZE; + u32 tcp_seq_num, tcp_ack_num, tcp_win_size; + int tcp_hdr_len, payload_len; + u8 tcp_flags, action; + + tcp_hdr_len = GET_TCP_HDR_LEN_IN_BYTES(b->ip.hdr.tcp_hlen); + payload_len = tcp_len - tcp_hdr_len; + if (tcp_hdr_len > TCP_HDR_SIZE) + tcp_parse_options(tcp, (uchar *)b + IP_TCP_HDR_SIZE, + tcp_hdr_len - TCP_HDR_SIZE); /* - * tcp_flags are examined to determine TX action in a given state - * tcp_push is interpreted to mean "inform the app" - * urg, ece, cer and nonce flags are not supported. - * - * exe and crw are use to signal and confirm knowledge of congestion. - * This TCP only sends a file request and acks. If it generates - * congestion, the network is broken. + * Incoming sequence and ack numbers are server's view of the numbers. + * The app must swap the numbers when responding. */ - debug_cond(DEBUG_INT_STATE, "TCP STATE ENTRY %x\n", action); - if (tcp_rst) { - action = TCP_DATA; - tcp->state = TCP_CLOSED; - net_set_state(NETLOOP_FAIL); - debug_cond(DEBUG_INT_STATE, "TCP Reset %x\n", tcp_flags); - return TCP_RST; - } + tcp_seq_num = ntohl(b->ip.hdr.tcp_seq); + tcp_ack_num = ntohl(b->ip.hdr.tcp_ack); + tcp_win_size = ntohs(b->ip.hdr.tcp_win) << tcp->rmt_win_scale; - switch (tcp->state) { + tcp_flags = b->ip.hdr.tcp_flags; + +// printf("pkt: seq=%d, ack=%d, flags=%x, len=%d\n", +// tcp_seq_num - tcp->irs, tcp_ack_num - tcp->iss, tcp_flags, pkt_len); +// printf("tcp: rcv_nxt=%d, snd_una=%d, snd_nxt=%d\n\n", +// tcp->rcv_nxt - tcp->irs, tcp->snd_una - tcp->iss, tcp->snd_nxt - tcp->iss); + + switch (tcp->state) { case TCP_CLOSED: - debug_cond(DEBUG_INT_STATE, "TCP CLOSED %x\n", tcp_flags); - if (tcp_syn) { - action = TCP_SYN | TCP_ACK; - tcp->irs = tcp_seq_num; - tcp->rcv_nxt = tcp_seq_num + 1; - tcp->lost.len = TCP_OPT_LEN_2; - tcp->state = TCP_SYN_RECEIVED; - } else if (tcp_ack || tcp_fin) { - action = TCP_DATA; + if (tcp_flags & TCP_RST) + return; + + if (tcp_flags & TCP_ACK) { + tcp_send_packet(tcp, TCP_RST, tcp_ack_num, 0, 0); + return; } - break; - case TCP_SYN_RECEIVED: + + if (!(tcp_flags & TCP_SYN)) + return; + + tcp->irs = tcp_seq_num; + tcp->rcv_nxt = tcp->irs + 1; + + tcp->iss = tcp_get_start_seq(); + tcp->snd_una = tcp->iss; + tcp->snd_nxt = tcp->iss + 1; + tcp->snd_wnd = tcp_win_size; + + tcp->time_last_rx = get_timer(0); + + tcp_stream_set_state(tcp, TCP_SYN_RECEIVED); + tcp_send_packet_with_retry(tcp, TCP_SYN | TCP_ACK, + tcp->iss, 0, 0); + return; + case TCP_SYN_SENT: - debug_cond(DEBUG_INT_STATE, "TCP_SYN_SENT | TCP_SYN_RECEIVED %x, %u\n", - tcp_flags, tcp_seq_num); - if (tcp_fin) { - action = action | TCP_PUSH; - tcp->state = TCP_CLOSE_WAIT; - } else if (tcp_ack || (tcp_syn && tcp_ack)) { - action |= TCP_ACK; - tcp->irs = tcp_seq_num; - tcp->rcv_nxt = tcp_seq_num + 1; - tcp->state = TCP_ESTABLISHED; - - if (tcp_syn && tcp_ack) - action |= TCP_PUSH; - } else { - action = TCP_DATA; + if (!(tcp_flags & TCP_ACK)) + return; + + if ((tcp_seq_cmp(tcp_ack_num, tcp->iss) <= 0) || + (tcp_seq_cmp(tcp_ack_num, tcp->snd_nxt) > 0)) { + if (!(tcp_flags & TCP_RST)) + tcp_send_packet(tcp, TCP_RST, tcp_ack_num, 0, 0); + return; } - break; + + if (tcp_flags & TCP_RST) { + tcp_stream_set_status(tcp, TCP_ERR_RST); + tcp_stream_set_state(tcp, TCP_CLOSED); + return; + } + + if (!(tcp_flags & TCP_SYN)) + return; + + /* stop retransmit of SYN */ + tcp_stream_set_time_handler(tcp, 0, NULL); + + tcp->irs = tcp_seq_num; + tcp->rcv_nxt = tcp->irs + 1; + tcp->snd_una = tcp_ack_num; + + tcp->time_last_rx = get_timer(0); + + /* our SYN has been ACKed */ + tcp_stream_set_state(tcp, TCP_ESTABLISHED); + + if (tcp->on_established != NULL) + tcp->on_established(tcp); + + action = tcp_stream_fin_needed(tcp, tcp->snd_una) | TCP_ACK; + tcp_send_packet(tcp, action, tcp->snd_una, tcp->rcv_nxt, 0); + tcp_rx_user_data(tcp, tcp_seq_num, + ((char *)b) + pkt_len - payload_len, + payload_len); + return; + + case TCP_SYN_RECEIVED: case TCP_ESTABLISHED: - debug_cond(DEBUG_INT_STATE, "TCP_ESTABLISHED %x\n", tcp_flags); - if (payload_len > 0) { - tcp_hole(tcp, tcp_seq_num, payload_len); - tcp_fin = TCP_DATA; /* cause standalone FIN */ + case TCP_FIN_WAIT_1: + case TCP_FIN_WAIT_2: + case TCP_CLOSE_WAIT: + case TCP_CLOSING: + case TCP_LAST_ACK: + if (!tcp_seg_in_wnd(tcp, tcp_seq_num, payload_len)) { + if (tcp_flags & TCP_RST) + return; + action = tcp_stream_fin_needed(tcp, tcp->snd_una) | TCP_ACK; + tcp_send_packet(tcp, action, tcp->snd_una, tcp->rcv_nxt, 0); + return; } - if ((tcp_fin) && - (!IS_ENABLED(CONFIG_PROT_TCP_SACK) || - tcp->lost.len <= TCP_OPT_LEN_2)) { - action = action | TCP_FIN | TCP_PUSH | TCP_ACK; - tcp->state = TCP_CLOSE_WAIT; - } else if (tcp_ack) { - action = TCP_DATA; + tcp->time_last_rx = get_timer(0); + + if (tcp_flags & TCP_RST) { + tcp_stream_set_status(tcp, TCP_ERR_RST); + tcp_stream_set_state(tcp, TCP_CLOSED); + return; } - if (tcp_syn) - action = TCP_ACK + TCP_RST; - else if (tcp_push) - action = action | TCP_PUSH; - break; - case TCP_CLOSE_WAIT: - debug_cond(DEBUG_INT_STATE, "TCP_CLOSE_WAIT (%x)\n", tcp_flags); - action = TCP_DATA; - break; - case TCP_FIN_WAIT_2: - debug_cond(DEBUG_INT_STATE, "TCP_FIN_WAIT_2 (%x)\n", tcp_flags); - if (tcp_ack) { - action = TCP_PUSH | TCP_ACK; - tcp->state = TCP_CLOSED; - puts("\n"); - } else if (tcp_syn) { - action = TCP_DATA; - } else if (tcp_fin) { - action = TCP_DATA; + if (tcp_flags & TCP_SYN) { + tcp_send_packet(tcp, TCP_RST, tcp_ack_num, 0, 0); + tcp_stream_set_status(tcp, TCP_ERR_RST); + tcp_stream_set_state(tcp, TCP_CLOSED); + return; } - break; - case TCP_FIN_WAIT_1: - debug_cond(DEBUG_INT_STATE, "TCP_FIN_WAIT_1 (%x)\n", tcp_flags); - if (tcp_fin) { - tcp->rcv_nxt++; - action = TCP_ACK | TCP_FIN; - tcp->state = TCP_FIN_WAIT_2; + + if (!(tcp_flags & TCP_ACK)) + return; + + if (tcp_rx_check_ack_num(tcp, tcp_seq_num, tcp_ack_num, + tcp_win_size) == TCP_PACKET_DROP) { + return; } - if (tcp_syn) - action = TCP_RST; - if (tcp_ack) - tcp->state = TCP_CLOSED; - break; - case TCP_CLOSING: - debug_cond(DEBUG_INT_STATE, "TCP_CLOSING (%x)\n", tcp_flags); - if (tcp_ack) { - action = TCP_PUSH; - tcp->state = TCP_CLOSED; - puts("\n"); - } else if (tcp_syn) { - action = TCP_RST; - } else if (tcp_fin) { - action = TCP_DATA; + + if (tcp_rx_user_data(tcp, tcp_seq_num, + ((char *)b) + pkt_len - payload_len, + payload_len) == TCP_PACKET_DROP) { + return; + } + + if (tcp_flags & TCP_FIN) { + tcp->fin_rx = 1; + tcp->fin_rx_seq = tcp_seq_num + payload_len + 1; + tcp_hole(tcp, tcp_seq_num + payload_len, 1); + action = tcp_stream_fin_needed(tcp, tcp->snd_una) | TCP_ACK; + tcp_send_packet(tcp, action, tcp->snd_una, tcp->rcv_nxt, 0); + } + + if (tcp->fin_rx && (tcp->fin_rx_seq == tcp->rcv_nxt)) { + /* all rx data were processed */ + switch (tcp->state) { + case TCP_ESTABLISHED: + tcp_stream_set_state(tcp, TCP_LAST_ACK); + tcp_send_packet_with_retry(tcp, TCP_ACK | TCP_FIN, + tcp->snd_nxt, 0, 0); + tcp->snd_nxt++; + break; + + case TCP_FIN_WAIT_1: + if (tcp_ack_num == tcp->snd_nxt) + tcp_stream_set_state(tcp, TCP_CLOSED); + else + tcp_stream_set_state(tcp, TCP_CLOSING); + break; + + case TCP_FIN_WAIT_2: + tcp_stream_set_state(tcp, TCP_CLOSED); + break; + + default: + break; + } + } + + if ((tcp->state == TCP_FIN_WAIT_1) && + tcp_stream_fin_needed(tcp, tcp->snd_una)) { + /* all tx data were acknowledged */ + tcp_send_packet_with_retry(tcp, TCP_ACK | TCP_FIN, + tcp->snd_una, 0, 0); } - break; } - return action; } /** @@ -667,9 +1106,6 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) { int tcp_len = pkt_len - IP_HDR_SIZE; u16 tcp_rx_xsum = b->ip.hdr.ip_sum; - u8 tcp_action = TCP_DATA; - u32 tcp_seq_num, tcp_ack_num; - int tcp_hdr_len, payload_len; struct tcp_stream *tcp; struct in_addr src; @@ -713,54 +1149,57 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) if (tcp == NULL) return; - tcp_hdr_len = GET_TCP_HDR_LEN_IN_BYTES(b->ip.hdr.tcp_hlen); - payload_len = tcp_len - tcp_hdr_len; + tcp_rx_state_machine(tcp, b, pkt_len); + tcp_stream_put(tcp); +} - if (tcp_hdr_len > TCP_HDR_SIZE) - tcp_parse_options(tcp, (uchar *)b + IP_TCP_HDR_SIZE, - tcp_hdr_len - TCP_HDR_SIZE); - /* - * Incoming sequence and ack numbers are server's view of the numbers. - * The app must swap the numbers when responding. - */ - tcp_seq_num = ntohl(b->ip.hdr.tcp_seq); - tcp_ack_num = ntohl(b->ip.hdr.tcp_ack); +struct tcp_stream *tcp_stream_connect(struct in_addr rhost, u16 rport) +{ + struct tcp_stream *tcp; - /* Packets are not ordered. Send to app as received. */ - tcp_action = tcp_state_machine(tcp, b->ip.hdr.tcp_flags, - tcp_seq_num, payload_len); + tcp = tcp_stream_add(rhost, rport, random_port()); + if (tcp == NULL) + return NULL; - tcp_activity_count++; - if (tcp_activity_count > TCP_ACTIVITY) { - puts("| "); - tcp_activity_count = 0; - } + tcp->iss = tcp_get_start_seq(); + tcp->snd_una = tcp->iss; + tcp->snd_nxt = tcp->iss + 1; - if ((tcp_action & TCP_PUSH) || payload_len > 0) { - debug_cond(DEBUG_DEV_PKT, - "TCP Notify (action=%x, Seq=%u,Ack=%u,Pay%d)\n", - tcp_action, tcp_seq_num, tcp_ack_num, payload_len); + tcp_stream_set_state(tcp, TCP_SYN_SENT); + tcp_send_packet_with_retry(tcp, TCP_SYN, tcp->snd_una, 0, 0); + return tcp; +} - (*tcp_packet_handler) (tcp, (uchar *)b + pkt_len - payload_len, - tcp_seq_num, tcp_ack_num, tcp_action, - payload_len); +void tcp_stream_reset(struct tcp_stream *tcp) +{ + if (tcp->state == TCP_CLOSED) + return; - } else if (tcp_action != TCP_DATA) { - debug_cond(DEBUG_DEV_PKT, - "TCP Action (action=%x,Seq=%u,Ack=%u,Pay=%d)\n", - tcp_action, tcp_ack_num, tcp->rcv_nxt, payload_len); - - /* - * Warning: Incoming Ack & Seq sequence numbers are transposed - * here to outgoing Seq & Ack sequence numbers - */ - net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, - (tcp_action & (~TCP_PUSH)), - tcp_ack_num, tcp->rcv_nxt); - } + tcp_stream_set_time_handler(tcp, 0, NULL); + tcp_send_packet(tcp, TCP_RST, tcp->snd_una, 0, 0); + tcp_stream_set_status(tcp, TCP_ERR_RST); + tcp_stream_set_state(tcp, TCP_CLOSED); } -struct tcp_stream *tcp_stream_connect(struct in_addr rhost, u16 rport) +void tcp_stream_close(struct tcp_stream *tcp) { - return tcp_stream_add(rhost, rport, random_port()); + switch (tcp->state) { + case TCP_SYN_SENT: + tcp_stream_reset(tcp); + break; + case TCP_SYN_RECEIVED: + case TCP_ESTABLISHED: + tcp->fin_tx = 1; + tcp->fin_tx_seq = tcp->snd_nxt; + if (tcp_stream_fin_needed(tcp, tcp->snd_una)) { + /* all tx data were acknowledged */ + tcp_send_packet_with_retry(tcp, TCP_ACK | TCP_FIN, + tcp->snd_una, 0, 0); + } + tcp_stream_set_state(tcp, TCP_FIN_WAIT_1); + tcp->snd_nxt++; + break; + default: + break; + } } diff --git a/net/wget.c b/net/wget.c index ad5db21e97e..ba95efa61c1 100644 --- a/net/wget.c +++ b/net/wget.c @@ -21,48 +21,26 @@ DECLARE_GLOBAL_DATA_PTR; /* The default, change with environment variable 'httpdstp' */ #define SERVER_PORT 80 +#define HASHES_PER_LINE 65 + +#define HTTP_MAX_HDR_LEN 2048 + static const char bootfile1[] = "GET "; static const char bootfile3[] = " HTTP/1.0\r\n\r\n"; static const char http_eom[] = "\r\n\r\n"; -static const char http_ok[] = "200"; -static const char content_len[] = "Content-Length"; +static const char http_ok[] = " 200 "; +static const char content_len[] = "Content-Length:"; static const char linefeed[] = "\r\n"; -static int wget_timeout_count; -struct tcp_stream *tcp; - -struct pkt_qd { - uchar *pkt; - unsigned int tcp_seq_num; - unsigned int len; -}; - -/* - * This is a control structure for out of order packets received. - * The actual packet bufers are in the kernel space, and are - * expected to be overwritten by the downloaded image. - */ -#define PKTQ_SZ (PKTBUFSRX / 4) -static struct pkt_qd pkt_q[PKTQ_SZ]; -static int pkt_q_idx; +static struct in_addr web_server_ip; +static unsigned int server_port; static unsigned long content_length; static unsigned int packets; - -static unsigned int initial_data_seq_num; -static unsigned int next_data_seq_num; - -static enum wget_state current_wget_state; +static u32 http_hdr_size; +static int wget_tsize_num_hash; static char *image_url; -static unsigned int wget_timeout = WGET_TIMEOUT; - static enum net_loop_state wget_loop_state; -/* Timeout retry parameters */ -static u8 retry_action; /* actions for TCP retry */ -static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/ -static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */ -static int retry_len; /* TCP retry length */ - static ulong wget_load_size; /** @@ -95,7 +73,6 @@ static int wget_init_load_size(void) static inline int store_block(uchar *src, unsigned int offset, unsigned int len) { ulong store_addr = image_load_addr + offset; - ulong newsize = offset + len; uchar *ptr; if (IS_ENABLED(CONFIG_LMB)) { @@ -115,330 +92,150 @@ static inline int store_block(uchar *src, unsigned int offset, unsigned int len) ptr = map_sysmem(store_addr, len); memcpy(ptr, src, len); unmap_sysmem(ptr); - - if (net_boot_file_size < (offset + len)) - net_boot_file_size = newsize; - return 0; } -/** - * wget_send_stored() - wget response dispatcher - * - * WARNING, This, and only this, is the place in wget.c where - * SEQUENCE NUMBERS are swapped between incoming (RX) - * and outgoing (TX). - * Procedure wget_handler() is correct for RX traffic. - */ -static void wget_send_stored(void) +static void show_block_marker(void) { - u8 action = retry_action; - int len = retry_len; - unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len); - unsigned int tcp_seq_num = retry_tcp_ack_num; - uchar *ptr, *offset; - - switch (current_wget_state) { - case WGET_CLOSED: - debug_cond(DEBUG_WGET, "wget: send SYN\n"); - current_wget_state = WGET_CONNECTING; - net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, action, - tcp_seq_num, tcp_ack_num); - packets = 0; - break; - case WGET_CONNECTING: - pkt_q_idx = 0; - net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, action, - tcp_seq_num, tcp_ack_num); - - ptr = net_tx_packet + net_eth_hdr_size() + - IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; - offset = ptr; - - memcpy(offset, &bootfile1, strlen(bootfile1)); - offset += strlen(bootfile1); - - memcpy(offset, image_url, strlen(image_url)); - offset += strlen(image_url); - - memcpy(offset, &bootfile3, strlen(bootfile3)); - offset += strlen(bootfile3); - net_send_tcp_packet((offset - ptr), tcp->rhost, tcp->rport, tcp->lport, - TCP_PUSH, tcp_seq_num, tcp_ack_num); - current_wget_state = WGET_CONNECTED; - break; - case WGET_CONNECTED: - case WGET_TRANSFERRING: - case WGET_TRANSFERRED: - net_send_tcp_packet(0, tcp->rhost, tcp->rport, tcp->lport, action, - tcp_seq_num, tcp_ack_num); - break; - } -} + int cnt; -static void wget_send(u8 action, unsigned int tcp_seq_num, - unsigned int tcp_ack_num, int len) -{ - retry_action = action; - retry_tcp_ack_num = tcp_ack_num; - retry_tcp_seq_num = tcp_seq_num; - retry_len = len; + if (content_length != -1) { + if (net_boot_file_size > content_length) + content_length = net_boot_file_size; - wget_send_stored(); + cnt = net_boot_file_size * 50 / content_length; + while (wget_tsize_num_hash < cnt) { + putc('#'); + wget_tsize_num_hash++; + } + } else { + if ((packets % 10) == 0) + putc('#'); + else if (((packets + 1) % (10 * HASHES_PER_LINE)) == 0) + puts("\n"); + } } -void wget_fail(char *error_message, unsigned int tcp_seq_num, - unsigned int tcp_ack_num, u8 action) +static void tcp_stream_on_closed(struct tcp_stream *tcp) { - printf("wget: Transfer Fail - %s\n", error_message); - net_set_timeout_handler(0, NULL); - wget_send(action, tcp_seq_num, tcp_ack_num, 0); + if (tcp->status != TCP_ERR_OK) + wget_loop_state = NETLOOP_FAIL; + + if (wget_loop_state != NETLOOP_SUCCESS) + printf("\nwget: Transfer Fail, TCP status - %d\n", tcp->status); + else + printf("\nPackets received %d, Transfer Successful\n", packets); + net_set_state(wget_loop_state); } -void wget_success(u8 action, unsigned int tcp_seq_num, - unsigned int tcp_ack_num, int len, int packets) +static void tcp_stream_on_rcv_nxt_update(struct tcp_stream *tcp, u32 rx_bytes) { - printf("Packets received %d, Transfer Successful\n", packets); - wget_send(action, tcp_seq_num, tcp_ack_num, len); -} + char *pos, *tail; + uchar saved, *ptr; + int i; -/* - * Interfaces of U-BOOT - */ -static void wget_timeout_handler(void) -{ - if (++wget_timeout_count > WGET_RETRY_COUNT) { - puts("\nRetry count exceeded; starting again\n"); - wget_send(TCP_RST, 0, 0, 0); - net_start_again(); - } else { - puts("T "); - net_set_timeout_handler(wget_timeout + - WGET_TIMEOUT * wget_timeout_count, - wget_timeout_handler); - wget_send_stored(); + if (http_hdr_size != 0) { + net_boot_file_size = rx_bytes - http_hdr_size; + show_block_marker(); + return; } -} -#define PKT_QUEUE_OFFSET 0x20000 -#define PKT_QUEUE_PACKET_SIZE 0x800 + ptr = map_sysmem(image_load_addr, rx_bytes + 1); -static void wget_connected(uchar *pkt, unsigned int tcp_seq_num, - u8 action, unsigned int tcp_ack_num, unsigned int len) -{ - uchar *pkt_in_q; - char *pos; - int hlen, i; - uchar *ptr1; + saved = ptr[rx_bytes]; + ptr[rx_bytes] = '\0'; + pos = strstr((char *)ptr, http_eom); + ptr[rx_bytes] = saved; - pkt[len] = '\0'; - pos = strstr((char *)pkt, http_eom); + if (pos == NULL) { + if ((rx_bytes < HTTP_MAX_HDR_LEN) && + (tcp->state == TCP_ESTABLISHED)) + goto end; - if (!pos) { - debug_cond(DEBUG_WGET, - "wget: Connected, data before Header %p\n", pkt); - pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET + - (pkt_q_idx * PKT_QUEUE_PACKET_SIZE); - - ptr1 = map_sysmem((phys_addr_t)pkt_in_q, len); - memcpy(ptr1, pkt, len); - unmap_sysmem(ptr1); - - pkt_q[pkt_q_idx].pkt = pkt_in_q; - pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num; - pkt_q[pkt_q_idx].len = len; - pkt_q_idx++; - - if (pkt_q_idx >= PKTQ_SZ) { - printf("wget: Fatal error, queue overrun!\n"); - net_set_state(NETLOOP_FAIL); + printf("ERROR: misssed HTTP header\n"); + tcp_stream_close(tcp); + wget_loop_state = NETLOOP_FAIL; + goto end; + } - return; - } - } else { - debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt); - /* sizeof(http_eom) - 1 is the string length of (http_eom) */ - hlen = pos - (char *)pkt + sizeof(http_eom) - 1; - pos = strstr((char *)pkt, linefeed); - if (pos > 0) - i = pos - (char *)pkt; - else - i = hlen; - printf("%.*s", i, pkt); - - current_wget_state = WGET_TRANSFERRING; - - initial_data_seq_num = tcp_seq_num + hlen; - next_data_seq_num = tcp_seq_num + len; - - if (strstr((char *)pkt, http_ok) == 0) { - debug_cond(DEBUG_WGET, - "wget: Connected Bad Xfer\n"); - wget_loop_state = NETLOOP_FAIL; - wget_send(action, tcp_seq_num, tcp_ack_num, len); - } else { - debug_cond(DEBUG_WGET, - "wget: Connctd pkt %p hlen %x\n", - pkt, hlen); - - pos = strstr((char *)pkt, content_len); - if (!pos) { - content_length = -1; - } else { - pos += sizeof(content_len) + 2; - strict_strtoul(pos, 10, &content_length); - debug_cond(DEBUG_WGET, - "wget: Connected Len %lu\n", - content_length); - } - - net_boot_file_size = 0; - - if (len > hlen) { - if (store_block(pkt + hlen, 0, len - hlen) != 0) { - wget_loop_state = NETLOOP_FAIL; - wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); - net_set_state(NETLOOP_FAIL); - return; - } - } + http_hdr_size = pos - (char *)ptr + strlen(http_eom); + *pos = '\0'; + + pos = strstr((char *)ptr, linefeed); + if (pos > 0) + i = pos - (char *)ptr; + else + i = http_hdr_size - strlen(http_eom); + printf("%.*s\n", i, ptr); + + if (strstr((char *)ptr, http_ok) == NULL) { + debug_cond(DEBUG_WGET, "wget: Connected Bad Xfer\n"); + tcp_stream_close(tcp); + wget_loop_state = NETLOOP_FAIL; + goto end; + } - debug_cond(DEBUG_WGET, - "wget: Connected Pkt %p hlen %x\n", - pkt, hlen); - - for (i = 0; i < pkt_q_idx; i++) { - int err; - - ptr1 = map_sysmem( - (phys_addr_t)(pkt_q[i].pkt), - pkt_q[i].len); - err = store_block(ptr1, - pkt_q[i].tcp_seq_num - - initial_data_seq_num, - pkt_q[i].len); - unmap_sysmem(ptr1); - debug_cond(DEBUG_WGET, - "wget: Connctd pkt Q %p len %x\n", - pkt_q[i].pkt, pkt_q[i].len); - if (err) { - wget_loop_state = NETLOOP_FAIL; - wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); - net_set_state(NETLOOP_FAIL); - return; - } - } - } + debug_cond(DEBUG_WGET, "wget: Connctd pkt %p hlen %x\n", + ptr, http_hdr_size); + + pos = strstr((char *)ptr, content_len); + if (!pos) { + content_length = -1; + } else { + pos += strlen(content_len) + 1; + content_length = simple_strtoul(pos, &tail, 10); + if ((*tail != '\r') && (*tail != '\n') && (*tail != '\0')) + content_length = -1; + printf("%s %d\n", content_len, (int)content_length); } - wget_send(action, tcp_seq_num, tcp_ack_num, len); + + net_boot_file_size = rx_bytes - http_hdr_size; + memmove(ptr, ptr + http_hdr_size, net_boot_file_size); + +end: + unmap_sysmem(ptr); + + wget_loop_state = NETLOOP_SUCCESS; } -/** - * wget_handler() - TCP handler of wget - * @tcp: TCP stream - * @pkt: pointer to the application packet - * @tcp_seq_num: TCP sequential number - * @tcp_ack_num: TCP acknowledgment number - * @action: TCP action (SYN, ACK, FIN, etc) - * @len: packet length - * - * In the "application push" invocation, the TCP header with all - * its information is pointed to by the packet pointer. - */ -static void wget_handler(struct tcp_stream *tcp, uchar *pkt, - u32 tcp_seq_num, u32 tcp_ack_num, - u8 action, unsigned int len) +static u32 tcp_stream_rx(struct tcp_stream *tcp, u32 rx_offs, void *buf, u32 len) { - enum tcp_state wget_tcp_state = tcp_stream_get_state(tcp); - - net_set_timeout_handler(wget_timeout, wget_timeout_handler); packets++; + store_block(buf, rx_offs - http_hdr_size, len); + return len; +} - switch (current_wget_state) { - case WGET_CLOSED: - debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n"); - break; - case WGET_CONNECTING: - debug_cond(DEBUG_WGET, - "wget: Connecting In len=%x, Seq=%u, Ack=%u\n", - len, tcp_seq_num, tcp_ack_num); - if (!len) { - if (wget_tcp_state == TCP_ESTABLISHED) { - debug_cond(DEBUG_WGET, - "wget: Cting, send, len=%x\n", len); - wget_send(action, tcp_seq_num, tcp_ack_num, - len); - } else { - printf("%.*s", len, pkt); - wget_fail("wget: Handler Connected Fail\n", - tcp_seq_num, tcp_ack_num, action); - } - } - break; - case WGET_CONNECTED: - debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n", - tcp_seq_num, len); - if (!len) { - wget_fail("Image not found, no data returned\n", - tcp_seq_num, tcp_ack_num, action); - } else { - wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len); - } - break; - case WGET_TRANSFERRING: - debug_cond(DEBUG_WGET, - "wget: Transferring, seq=%x, ack=%x,len=%x\n", - tcp_seq_num, tcp_ack_num, len); - - if (next_data_seq_num != tcp_seq_num) { - debug_cond(DEBUG_WGET, "wget: seq=%x packet was lost\n", next_data_seq_num); - return; - } - next_data_seq_num = tcp_seq_num + len; +static u32 tcp_stream_tx(struct tcp_stream *tcp, u32 tx_offs, void *buf, u32 maxlen) +{ + int ret; - if (store_block(pkt, tcp_seq_num - initial_data_seq_num, len) != 0) { - wget_fail("wget: store error\n", - tcp_seq_num, tcp_ack_num, action); - net_set_state(NETLOOP_FAIL); - return; - } + if (tx_offs != 0) + return 0; - switch (wget_tcp_state) { - case TCP_FIN_WAIT_2: - wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len); - fallthrough; - case TCP_SYN_SENT: - case TCP_SYN_RECEIVED: - case TCP_CLOSING: - case TCP_FIN_WAIT_1: - case TCP_CLOSED: - net_set_state(NETLOOP_FAIL); - break; - case TCP_ESTABLISHED: - wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, - len); - wget_loop_state = NETLOOP_SUCCESS; - break; - case TCP_CLOSE_WAIT: /* End of transfer */ - current_wget_state = WGET_TRANSFERRED; - wget_send(action | TCP_ACK | TCP_FIN, - tcp_seq_num, tcp_ack_num, len); - break; - } - break; - case WGET_TRANSFERRED: - printf("Packets received %d, Transfer Successful\n", packets); - net_set_state(wget_loop_state); - break; - } + ret = snprintf(buf, maxlen, "%s%s%s", bootfile1, image_url, bootfile3); + return ret; +} + +static int tcp_stream_on_create(struct tcp_stream *tcp) +{ + if ((tcp->rhost.s_addr != web_server_ip.s_addr) || + (tcp->rport != server_port)) + return 0; + + tcp->max_retry_count = WGET_RETRY_COUNT; + tcp->initial_timeout = WGET_TIMEOUT; + tcp->on_closed = tcp_stream_on_closed; + tcp->on_rcv_nxt_update = tcp_stream_on_rcv_nxt_update; + tcp->rx = tcp_stream_rx; + tcp->tx = tcp_stream_tx; + return 1; } #define BLOCKSIZE 512 void wget_start(void) { - struct in_addr web_server_ip; - unsigned int server_port; + struct tcp_stream *tcp; image_url = strchr(net_boot_file_name, ':'); if (image_url > 0) { @@ -486,12 +283,6 @@ void wget_start(void) } } - net_set_timeout_handler(wget_timeout, wget_timeout_handler); - tcp_set_tcp_handler(wget_handler); - - wget_timeout_count = 0; - current_wget_state = WGET_CLOSED; - /* * Zero out server ether to force arp resolution in case * the server ip for the previous u-boot command, for example dns @@ -500,14 +291,21 @@ void wget_start(void) memset(net_server_ethaddr, 0, 6); + packets = 0; + net_boot_file_size = 0; + http_hdr_size = 0; + wget_tsize_num_hash = 0; + wget_loop_state = NETLOOP_FAIL; + server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff; + tcp_stream_set_on_create_handler(tcp_stream_on_create); tcp = tcp_stream_connect(web_server_ip, server_port); if (tcp == NULL) { + printf("No free tcp streams\n"); net_set_state(NETLOOP_FAIL); return; } - - wget_send(TCP_SYN, 0, 0, 0); + tcp_stream_put(tcp); } #if (IS_ENABLED(CONFIG_CMD_DNS)) From patchwork Wed Aug 14 10:31:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972341 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=mGlOCClS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPjJ5XGbz1yYl for ; Wed, 14 Aug 2024 20:33:08 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C8DB988A05; Wed, 14 Aug 2024 12:32:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="mGlOCClS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CFD5A88A03; Wed, 14 Aug 2024 12:32:23 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2071d.outbound.protection.outlook.com [IPv6:2a01:111:f403:2612::71d]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DBBB6885FA for ; Wed, 14 Aug 2024 12:32:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=BBpM6vWZUzy1ZnsvDW0oOi3qLG9Re7QtxKXH3VozBWNbpIqnKp2XWmFBlzX+5lk8G2yb6PPG5cS7EjN+6UFacz4dKx7fkVa9nypIS8pE2WdoIR0MYon6nmTgFQe1g8z4zIAPlidB2LwoEeqRCFgsHp1v/KqY67acq8EXHU/gnyTJvfMN+Jahuwx5EMNuwoZUf4p2lSIAyHBdKq9yCya6iJPMABb3F+VYoaqiUh9OgGtG2YWLycpk/ItS5dVl/HdM9y9KetzO4yPSmH0LILk19VOv4HvzfAkHnwBNh8CuKTgHLXFOH2bpbX1nxBnpI1b1DY8Fci7Wdr/04OwhRUuTDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=r1h3vxyMLYGQObX5jLjBChuy6wcjq44izjcT+7AvEc4=; b=CwhySsTF52HYo3634yktaZvQ2YnU7pa9V1hsslE9m2o3ZTXC4UV+cKJqspvb8GyaG80JIVK8Nl3gco8HW2bPGmdMTVzaK0kOTmev+H+2Ngh6F0fc7qjqRO8Q0MIWbkvkTkP6UGcQFiCJqX3fJrQQXZHps+6PKs7BhiScWf+9zwqEcfvbWuQ2BedK7rarud/ml/DUNCq4vQLjo09t/azTMwdqNf0CkhLjiVhPE1gyP3oqtc5MSLw7MNpOEePjQYJCN3NakKUELOc74So4tTu3KZF7HWZdyVcAikPE/Ns+j3tAEGwJ/J342bhzhHwwTBjfrpDAuBtntyiK/EkOPJqB7g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=r1h3vxyMLYGQObX5jLjBChuy6wcjq44izjcT+7AvEc4=; b=mGlOCClS+UF4fJzk8SG+srLaYVqYgi725EDV+PBoLVual2EN/JIGydw6KK9GfIAMo7smhsldo5oHDutGvSDF9uNHRMzCTL5nerHlw957uuJfH3FYj3rOnIr8diK1RHLjqM+SzZgY5uS2doVOS/E76CSuacIJSUcMOo4uHupj1pkB28S661SjD0Ev6kPvxuDhmoLwUryHqA34N7XnZ1zPTDDo7MrDgibKyXU5cT+iQJPhCNGR/VFcJwas3zPUAKYHiY58zwcjECtiHZCptoxwFwfymuFy2vbkNZR5dfodq3jqckiwkb9/5yJykiJ2Im8izoTCiKHMo1Ot4c+i4ljNtw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by DBBPR08MB6284.eurprd08.prod.outlook.com (2603:10a6:10:20e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.16; Wed, 14 Aug 2024 10:32:20 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:20 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 7/9] net/tcp: simplify tcp header filling code Date: Wed, 14 Aug 2024 14:31:43 +0400 Message-Id: <20240814103145.1347645-8-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|DBBPR08MB6284:EE_ X-MS-Office365-Filtering-Correlation-Id: b787555e-88c0-45a9-b29f-08dcbc4c60ac X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|7416014|1800799024|376014|52116014|921020|38350700014; X-Microsoft-Antispam-Message-Info: 06RlIpZWfV61moEoXWuJC8nhgHoPj2dKaRpEKmSeJURquHjj0/W7EllYPmweGrMeFLU9R64GTty1ZlZyedBn4EhhDopbnLENAxP9JlqebKftqx3W+H69gtpsZuRxtJorN/igTFJ3vQxIN1mbPPe4BK2q8s8FO28vJahIVNGGovFzHoVcswi+aUCUgCPnsQUjeELjYk4MC4LLeZC7NO1RjjnAcz4mDqoiuAvmWdTqtTAovEZNHsYGItQzcLFguYg6P1knvWFYA5ws0p9H1pW8CBURQkg0fEps/cgrpegLil0aY9e0lD9ENYGy2wK0ZxKGP29v1yKVSeoAPYm9WzH4ZF9nHl1z/n2aWT8urn/D54oxzUx88RSrNYyn7TOnDUPg4TJM8OP7R9owLwfkvtU3EdZiUmsLht9dw8oaIaTkt338Y9b8eeIr80P7bRlG3z09d/l5wMVP+LbYvhCyYlq2ZLZpGf6U4EWZk08YuOjDy5SnZJmuPI4tRtlbzs+wj4aCiYSNTBBwz8fX7tzchEqlkMfMikxVZQ1Gu6I0eIxCgHW22IubNKsXkvY2XeFrxI4Ca5zFnImV4F9b2yLXhCeBdDtVllNiiwLvyYac4tguoN6phkoqWBdEZ1WOcRWQkvhIcH+/al4LD1N3wP7k5OAI8bYJKWmKSrQ/nHyVy/FEQTGGfAQTYzS1o7kSoDqUBtZvrn8mk0y9Eqc/zoaC1gRgvtZQTcm69bZ0Y1HPtoC6pLgaDArzSvvoHY11R6eCuT13PgGJFUWNEkSBf4REDgLxvh8y6r0Q36colQQk9cs3ZMvxO8JUODOprkkXZn+pgk2Z2Za5H5jHw/4fBigRJI8SQrsn9BjIKwhoOTh+71kRfdv6uAj5cm7AA5xb1aSe4b1RbPpBHl9XFqahCNP1r1mgNrS8/CADE/KQRICcKRBVg/bunuQuhjR2k6I9bQ8a00/3rQcwb72qBPQ+gwLxNoA+suWmUQXq5ACk+jr/ZYC3cmPPp5N+uPO6F4Z1ziBlJHeKW2zJ4d6JTQM3RvSbzAkCKLnRi2e9BSHV8ArMRUBKMnr4NcsJ8HeyTLzmDfeC94hd5wZOE0X7SkqO2q9ULCcnNUDjE+so61N06AnY7kt9RKzp9uOzrDe5JptyKUWWuOoEjdcf9a4gDQlvLVKTY4Ha8lRQVEGo/f/LuP61BELqL/A/pB/5vWQvzD+Rd1yNkDWPs/r04sy1DAkNrlRkmT1QA9SAE3W7cXnoSbuJD1vx8n5Q7cTZ34chsA0lFiYjbgB+eL9c48uU8MFfTVv8iXnWVSOjgqFHoqUsmx2d6arNhgGxRxIJRFEmx8hUUKWfFPRIasGuUXYu/xOyX8s/CnMpDyIbXXzXybssIABmjfqHswiBeI1oVvOBmUmJyL0yxbEgxcvUZX2x1k4IdLWuA4Ox2dGQM+EkCwlfl8Vn7f1/nME= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(7416014)(1800799024)(376014)(52116014)(921020)(38350700014); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tu+u/eHNOT6DfXTp5JoBn1J6epY6D8siaAGmEeJxT2oPscindiB0R6cTz64VM2+mXJHDNORUhNW2aZ12rCqVjk9+gA2DZEEE+ljkTIofxVe2G//obCH+NfVAKRD7mWkwMl396IZCNWZ6MmurThHoJzmuf+w+Q/CE88iKi8NR/imYD0C7rvdsDfGBr5O/weobZQL522lrHRMZwN0cjJqvACW3QYwz9uPsFW8Zye343hhIIm0zUNxjpOxU0N1KOPeUnv8gnaLIeEQ3/ZDGPwobay5NRjd5Wgxcl65VKSs9CW+AamuDLrPLV36JQ1Xi4cXegeEzcbaSIcqR0OcMa9KSwJXmfIhFHBDpK/e5d6XEo6BCPNcJqgEJD73qFlgII0/knocGEYaDUE81WFdt4BBplN7eF0x7DqPYqMdH60hukXLfvQ4xDWthM+dfHXtpTc2eiIj1z4a61NZYg0fwHUwcyUW6Jjy/3oTvocvYovjpQEuOE0UR+jGidg2034HBgG2HEWDiURiBfnfLWo8ZhXhlGgsuRiuMDhd/GSRUVBmpsj7Il8Qcp3c1BZ1slrP+yJxNNhkN4W1QPDJ+DP+4NNlYr87kHPCjnwvmU4LT9CSiVeywMJCIBFrXtqE6rODSkVb/OaKd2kNDNgH5Wlg+WXcksAzXswk1uvp+wbwmnJySadjNLErgPYakevxbhNJ4GgrRBx1XVpv2Cs3Un9ETMeUWNgHia/a/m9YkaopW6l4OBQVogY4RYE5l0LlutLl57sr14kpsSg4uxjvSdcyUL1JQbKBo8TD/dpzGWjzvlGX4vtntySjlHn9zokTUUi1OpiNwLd7IHjTPmaYlTDdq5XruyZDU3aWVfH+RyMrLFk0Q/K9FtFL8Ka68xlJ3I+2GHtjZwzGoBwSRV27WtHCnGQhCpnneAzW/u67mqQPiuBdP0RcIc/Z1F1rqehesdkMMZYyxGSVUVo0d/NGJH44p/FsuQCFtcS+T79X6yg31MMqQbfs3ivRb1sWLQE21J7h7iaov64V5aL5/eljTDBS7OXTCbZuu9Qr2UiN0Dg/Mgx2lx4eBzCwVSW8f3hUyphUK0hEyKN9G1d7JthlEJIgKszJxMjkm1AvlmoELeyWlauOsXJ9aqlGrv4sXXWJWPPR2I1uv/sWAYG6N2Gn3ZxNtRjehpBfwW9DRBMCOEVwYfnK9AjFgxxKUDHSob0uGrNNfwihCDwXwm9HcJYFosfAk2iYUo30esL9hrbjOnrsKf8K59oEe7OQfgottjRA0PwCDKfZtwDypt8pT68rb4LG6welDKgG/DVsojlEnB+AD8M4WtL1UtoumHEPa5/oOrw5udbJ60CaQ0qISLvnJJSAlkSnyTxgFdQGq1oZWEzeJ+qq7aRa7y4O9BmxYEmh3DjoBBF4jKf8fygmkzer5MRUoxaQiNfqNziQu0LnY7YijWQS5ddJZN67Pxq7rsS3RvJLWw/diAw9fBPLp9shocgMHp78JHImAQwZc9Fch0uvE98008nq2sy8UdpdQ1a/6fp5crzOhnYPOvWVBKhIJ5RMTn075I1epZIW5JIqpOpRNcZzrKDAkhEyXFqBoJeUsMB9tUDnCOFNxPnFJqqc3SaUCLBq74A== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: b787555e-88c0-45a9-b29f-08dcbc4c60ac X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:20.7003 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: r4gS0yqgc0reTDV70xPhRq04goqRjui+V8Ryhz39p4Et4JA3gQElKbM1eui6GTG4rOeOFQp3Uq8eQ5jngyAmxjcG33u8RpLDz2RK8k688Zc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR08MB6284 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Signed-off-by: Mikhail Kshevetskiy Reviewed-by: Simon Glass --- net/tcp.c | 70 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/net/tcp.c b/net/tcp.c index 2c34556c26d..7014d5b4f43 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -527,10 +527,37 @@ void net_set_syn_options(struct tcp_stream *tcp, union tcp_build_pkt *b) b->ip.end = TCP_O_END; } +const char *tcpflags_to_str(char tcpflags, char *buf, int size) +{ + int i = 0, len; + char *orig = buf; + const struct { + int bit; + const char *name; + } desc[] = {{TCP_RST, "RST"}, {TCP_SYN, "SYN"}, {TCP_PUSH, "PSH"}, + {TCP_FIN, "FIN"}, {TCP_ACK, "ACK"}, {0, NULL}}; + + *orig = '\0'; + while (desc[i].name != NULL) { + len = strlen(desc[i].name); + if (size <= len + 1) + break; + if (buf != orig) { + *buf++ = ','; + size--; + } + strcpy(buf, desc[i].name); + buf += len; + size -= len; + } + return orig; +} + int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, u8 action, u32 tcp_seq_num, u32 tcp_ack_num) { union tcp_build_pkt *b = (union tcp_build_pkt *)pkt; + char buf[24]; int pkt_hdr_len; int pkt_len; int tcp_len; @@ -540,55 +567,32 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, * 4 bits reserved options */ b->ip.hdr.tcp_flags = action; - pkt_hdr_len = IP_TCP_HDR_SIZE; b->ip.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE)); switch (action) { case TCP_SYN: debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:SYN (%pI4, %pI4, sq=%u, ak=%u)\n", - &tcp->rhost, &net_ip, - tcp_seq_num, tcp_ack_num); + "TCP Hdr:%s (%pI4, %pI4, s=%u, a=%u)\n", + tcpflags_to_str(action, buf, sizeof(buf)), + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); net_set_syn_options(tcp, b); pkt_hdr_len = IP_TCP_O_SIZE; break; - case TCP_SYN | TCP_ACK: - case TCP_ACK: - pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(tcp, b); - b->ip.hdr.tcp_flags = action; - debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:ACK (%pI4, %pI4, s=%u, a=%u, A=%x)\n", - &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num, - action); - break; - case TCP_FIN: - debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:FIN (%pI4, %pI4, s=%u, a=%u)\n", - &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); - payload_len = 0; - pkt_hdr_len = IP_TCP_HDR_SIZE; - break; case TCP_RST | TCP_ACK: case TCP_RST: debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:RST (%pI4, %pI4, s=%u, a=%u)\n", + "TCP Hdr:%s (%pI4, %pI4, s=%u, a=%u)\n", + tcpflags_to_str(action, buf, sizeof(buf)), &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); + pkt_hdr_len = IP_TCP_HDR_SIZE; break; - /* Notify connection closing */ - case (TCP_FIN | TCP_ACK): - case (TCP_FIN | TCP_ACK | TCP_PUSH): - debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:FIN ACK PSH(%pI4, %pI4, s=%u, a=%u, A=%x)\n", - &tcp->rhost, &net_ip, - tcp_seq_num, tcp_ack_num, action); - fallthrough; default: pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(tcp, b); - b->ip.hdr.tcp_flags = action | TCP_PUSH | TCP_ACK; debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:dft (%pI4, %pI4, s=%u, a=%u, A=%x)\n", - &tcp->rhost, &net_ip, - tcp_seq_num, tcp_ack_num, action); + "TCP Hdr:%s (%pI4, %pI4, s=%u, a=%u)\n", + tcpflags_to_str(action, buf, sizeof(buf)), + &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); + break; } pkt_len = pkt_hdr_len + payload_len; From patchwork Wed Aug 14 10:31:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972344 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=xdWfuSDQ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPjv0G51z1yYl for ; Wed, 14 Aug 2024 20:33:38 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5BB27889C5; Wed, 14 Aug 2024 12:32:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="xdWfuSDQ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 70743889D3; Wed, 14 Aug 2024 12:32:27 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2072f.outbound.protection.outlook.com [IPv6:2a01:111:f403:2608::72f]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 86B91889DF for ; Wed, 14 Aug 2024 12:32:25 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=A73k2nbHpevE4rWkKybMnoxQXVZ5u5s/ApIR9f3Wd8st20biwNNQt5SaZdz2+tlI1VAAoMkR3JlESt7q2Rm4nzqy4o/nuJDtt/gxqZfiqoa/dQ60G8+8QZb8WNfb2RA2maG/Lqk2HaP7DakTKm/4aCMwriAmYf9jxam24FB1V4Z1+iwNGDuItfJYjYWdiPvmAh2WRZ37powwnL0ZGVk2V/rzTagSBYho4rrJpm3s1KTe0DOdDcuAfHOOLQXc18zefOl8MKXpgPCC4UD5cJB89mK7RhTYNiYAptb4YYDUB2XzG+7ajBNCArHeXu7EMcQCMt6NMUdBgtk/lV6xS7byEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=EL8MhXh41iN9EyZl2AQPPTCSXD4sTCclynNCfPtb2Yw=; b=uaAiD/HUfjD0Kgd8ysrMWeaODxmlfqwPtU7DU6X0pXhJR9sc672WTAP3LgVgT3zoMEyume42LIRKW5+7z9KxuiMs3KI+9c5ukACBG/hh1dn6QRwq2I7wWB/IKE9TbXvrfO/NK91+QAmmrrWIcBLkXm9VUjgMFWb6O5kuVzoWTPxApRQS12C6XIEpDZe1gCmAq4q8PJMAFpf54MdgBthtzzYLtwHeGZl2CYxAufkjtx6AnZYVad9Qu5oIg327V/jZrltvZH7IdEktOQI1MHCytxlwWDesBLsF+FTMu8SPNEKUOHjp0W6+5eOxFtiojSE6JtFRcPtdK8qZulYQlpOOiQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EL8MhXh41iN9EyZl2AQPPTCSXD4sTCclynNCfPtb2Yw=; b=xdWfuSDQ2lcSqipXadNA3oR1PCuubc2nN+BspX3MhUN2hu2lX8B8WGNMW5UFe5fHeFGqpB5vy3VYNQN+RKokLpKpZ/2Ka1qlLPJcYuLL1U3xnX8knmb9YU0bxGNuXR57ziJY3X1Aiu8evlrZRuwhqhE6btxj1iGaHAVuH6zfdLB4JYYjE8h8J0HMItfsYmg2O15wsEQzEelNbfkDqEa1yYsPDLtBgfsGQ9BxES0DBYevDuGM4Y/23crt3uM5ErTHXCkNLg7/A2tsUbrnSisr+Yph/SyedzPkA7lPTABgwP1iGKgLH14cDlbJ5Wsc1hcS9fBKSm8Fll5DoYYIfpsRDQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by PAWPR08MB8887.eurprd08.prod.outlook.com (2603:10a6:102:33b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.13; Wed, 14 Aug 2024 10:32:24 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:24 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 8/9] net/tcp: define a fallback value for rcv_wnd size Date: Wed, 14 Aug 2024 14:31:44 +0400 Message-Id: <20240814103145.1347645-9-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|PAWPR08MB8887:EE_ X-MS-Office365-Filtering-Correlation-Id: ff4fe1eb-d37b-44fe-2ea8-08dcbc4c6291 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|7416014|52116014|376014|38350700014|921020; X-Microsoft-Antispam-Message-Info: WtDzxfd706usQDqmBJsJx8M9f4mPh1Di3HGW/ZBB3+RAsOn8BsNRAj2wWeYrq5E/JvacvQhE7iD+OiHJEXUaurk2fsUwOE9QwgKtfK6K2pwBs7zZ8MrJbIBs5AxDclun/sJopQkZCibpYedG/HW1B5Tl+vcLpRV+iLZKNQFCUrx8f3gTX1y5+qkKX+QKHoMO1O2nkBGvZouEiEfb2inIVcobjIwmU6skPlz8Tou+2AdHTRY+Pzn2AUrZ5M15oL2GEzXGcd+iHpuRaiG2f7swG0BsmoEG+p0C3PllfvmndsDFpmRxd403b1RRf1dYG1wnXorVttaFBjHcwdYNUiiNR18vDrUR0A+6Ucccn2pqKxrBVkZG4hw2BUL7rUIR4SIbVlHHT8aCbeqTjP2FFJCP8PgPB/u+vke5lWYXhKx8B/KAcSm+KNDIqQOj81wz3Q9QQdUmLLpNxaBLCU52tUAS67MlGZpEnEaF6sF3G3ucCqAaCWBQZAqgtEDFI6kzgA+r5eKwsB5DlLnOp7niRKC+y2G7+syrUga9D+MQzazP2Yn8lW++SNNTv1vapn/Jg6nE04dD4PyzCCCioqR48V8XEfrDRrDIFclVY8dtwJfzbYEoFHPG9vj+VJb2N2RAMb7XsNrU65uwEhO9s9yEABr7CkSJzzd3tGk/JUGnwrzxe5vOIVC353D8wYx4uE4gx8EhVHzNGYYY8D+jaP1lexrKHC9n5B3t5KbmpI/07uzsm2k= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(7416014)(52116014)(376014)(38350700014)(921020); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Qo84yKpIztMNhAYk0Zd55cT+zMtrErjLT2Y3Mmya1pD9DSOgm4wd/vVV/bBYvV11p9tmbDr6VMK6w0kp1teOAuHwEYgH8vCbLVzXbNM4iERwrdnTywNp5S2iaMPqSOEcMFHTSZExcFSNHHSlSEhIUHoGaHYbU0qGlVGQt8PQCcJaXlvDxUTscw2gMPVqBS/3RAKgQXACKHlWZHS2/9iZeEIff8rwJ3TDIN4AYlrh6YSwyB3ylOy9SaWZloIh6cj5tpBTqhhqowI5zn2+Q26pPUS/7q8WvJEDUOG9YhqCf2o1Oh86z6ng44Mvv/0Jp5O31K/AN/1uXs/5k0idolCjFNphu7yH1vzVIdhsiYEbVNQVqMggfUPfA0xOSRb8g+J6zzPipzg4tJmh6dw5+V0Gtqg0V8XJne57fvi7cy3Vm4N8/UjVlxLBLplI/6182HmE7wjk/YZwLygApib5+V1GH2l//cm2Y+Py7PMewMhn/QQxOYEmbVUvh2IhxO36DDG9A7lZAby/p07HMT2KfEBklVAIeIMusSzWyLoeHT0m3fDm2YQBMfv+V9Sx/ANqx+B8Mevrg9a2hP5OvpJ1b4ZbHMblGasCg613tfEufiNcy5sNPLHiu/4KoKkQ+wIjUuh4Dr8szsMihhN4I0Qele/0UNcG8UwOXmFmDlBdujw+C3Zasvobeeg9Ry3x9ciElWOFcVxMH9NWo3dn4YVeMRQ9cdsQG9FJnXenP0vy06Jw8VWkj1BG3vOP0ZAfuzjdUyCQMg2dCIslv657i8gRKUtB5NwNLI+uO10IyqrtiaPlPWb+sk6kgf105QrgxGQLi35godbj09PwglMtSsdxR6nTjnufaP0ZHtTwqIs+Uy97WXJZf7Cq/psM+IC5plqpWspfNNhgpGGj7iczgYSXjHJmywRLB+E+OegYsROYM8R155FpPxxVX6e9/qvxn0UXjJ3WSA0Dodro2pW0eBcHnv15ER4kNSE+llDND11h0n9Fm2zWT1Xhwiwzcmgxxu31xg4OX0DJmMW3O5RNL1NUTeX4FDH0vDm/xwU5/WxPLffbbFWr9Imc3cPZgpOGI44uMvYNDUpUEpjOq7bkErQW4BBqAsCuNEEMZRMYyjWFtpiUW5Z1oMETAx6k8912bGRF6XeohAfJbI09AXQYtHORcPUlSRNL4ADLJkrFOpOUipEU/kfAB5hN0XA6mpwa1mV/PQtu99PQr9QozrpZh1STvo2U1FxtAOHAbTIIzBveeIPlSa/wfb4oPbAm/At5COODYsjWoVrK/Kiegh8qxHZ8wqdAGL5ZTfMCcpXvf0nkxWRM8brnQYbfTr5I9YXu/fPFjwagYdbpkEyiFYq79FdMdWHIZMvaZ4aK220+N/z1tnbU/T8QDSbyI9UoLAPftshE3XG6+r7V6Y+wutr9ZxNG25wUj8Szil8Ad7SRKXNnL7UcplDXWwW5EBVHZXI7cKmnoin/gldc2N9HaQJInoQa/x9CtTYXARnqGglIEi8ZdMBl2tKLlxQb5rd8JkpYllM3DXOqkeaGkcyDni/kHOd8k40e8ZYj/CaIDJtSR6OYIVChGlBoEKD06A/+6eGF8oVhdmjR2ZGRZXWlf0beOBdAPIhgeg== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: ff4fe1eb-d37b-44fe-2ea8-08dcbc4c6291 X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:23.9006 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: PFHHo4bk2GK96BnOuM2lt1czFguk39+eSQ8hlHqCr53DWxRI6FXpZ7JWRm07/QzHD2jmI7TOuAvUdMHIzzFiqPDlbSDv4+JNvkaGAtbBkac= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB8887 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Some driver implements it's own network packet pool, so PKTBUFSRX is zero. This results in zero-size TCP receive window, so data transfer doesn't work. Avod it by setting a reasonable fallback value. Signed-off-by: Mikhail Kshevetskiy Reviewed-by: Simon Glass --- net/tcp.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/tcp.c b/net/tcp.c index 7014d5b4f43..f0889d72d44 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -28,7 +28,11 @@ #define TCP_SEND_TIMEOUT 2000UL #define TCP_RX_INACTIVE_TIMEOUT 30000UL #define TCP_START_SEQ_INC 2153 /* just large prime number */ -#define TCP_RCV_WND_SIZE (PKTBUFSRX * TCP_MSS) +#if PKTBUFSRX != 0 + #define TCP_RCV_WND_SIZE (PKTBUFSRX * TCP_MSS) +#else + #define TCP_RCV_WND_SIZE (4 * TCP_MSS) +#endif #define TCP_PACKET_OK 0 #define TCP_PACKET_DROP 1 From patchwork Wed Aug 14 10:31:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 1972345 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256 header.s=selector2 header.b=sGZiEXF3; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkPk60Gd5z1yYl for ; Wed, 14 Aug 2024 20:33:50 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BA3EA88A28; Wed, 14 Aug 2024 12:32:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.b="sGZiEXF3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 31E3188A28; Wed, 14 Aug 2024 12:32:33 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-VI1-obe.outbound.protection.outlook.com (mail-vi1eur02on20700.outbound.protection.outlook.com [IPv6:2a01:111:f403:2607::700]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id EAB7F88A3A for ; Wed, 14 Aug 2024 12:32:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=iopsys.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nfhK4E/NdAzltsEYRleK1j/oPVri0GkF/QhTN0UHycta/2Rmsi/cfPUD/k7Kk9FgPASBaDvxZ2h57nIIEEIoq0VcxALoYsbb6beefIZM7is6aCWliznud0+C7fIFOPbE8Q7T9qEFye8lJ2MCFUwv8kmold60iBnkGxwWZY+2aSlYi6Qf4BA7Aa3uzSIjkQR0Kl/gswAi9QNfKDqGLF5sJdz+ePImPMmcv0oNBbsNFwnsQwPa8AUkz+1X8ddbIdsdR9gsoQGqCn3DcSX37EZ2Q4n73eztCN9njA1Uko+g2hXz21/fJwV+DLkcvrDf3lNdb3eQxCELakbGKJAwz0+1+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Jwt1UxjoWc7Rg7yveFvfmlE+diEXxPenAePMRF8spy0=; b=odEfKBq1wFrh6G4MNv5MJjp53qkPiKin6le/39sqIBKDp/beQCpMVAwHr77Cci920JawXFik1cjFX9YO8HnLyAdHCT6aICqKPiQQCyQvlindD2PfW4DOFWP26X8MCHc2pHNGSV5UrP7N+Kwho5Uxxu9ZMWORD1lQoWc7HDDoRgDr2IOm80e+njBKUH+09h81DzIt2/TLTuQFac/YqopQ+C67KzCUCoq1KtCCS53BGS5wz1YaKjTkWywRouvCYV24O+CL6VjPkOm3RydKtHKIqhXpJw+PQgygXUh72Ipghb8kWQtUrskdPmOR0ddV7e+bO25D/nRS587c+hAoeQI8fw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu; dkim=pass header.d=iopsys.eu; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Jwt1UxjoWc7Rg7yveFvfmlE+diEXxPenAePMRF8spy0=; b=sGZiEXF30eikAQ5UPlkFLLTDNHDlZsctb9PtHl/TEsAAfM8oGGsKeDTEqliCOYzcUWyLawuy0G1lsHej2gxJ5PI19o7jodKtdXpaPKYuYNrO9KbgPIrTwy6Zh+Xbpf8D/KoXHismvI8F2iJ7B0lItTo/etEqn85HQWmG+TsyQWSwwdbI6s+xfl6X07BPVb5E2NecC7cExIaLDDrw9DiSHCu03tGWX5WNwtJRmZArzvo2QEgXGU87ptLgCuXnA6C62PDuSQPVzy8Oh9HVhvzWdOx8KDp0YZPY2dI8vnjaGELVXOQwG1R1/mLicNWGR1pfpu/gmvbzdgxPy2POcHgDUw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=iopsys.eu; Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) by AM7PR08MB5463.eurprd08.prod.outlook.com (2603:10a6:20b:106::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.15; Wed, 14 Aug 2024 10:32:27 +0000 Received: from GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com ([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.7875.012; Wed, 14 Aug 2024 10:32:27 +0000 From: Mikhail Kshevetskiy To: Tom Rini , Joe Hershberger , Ramon Fried , Mattijs Korpershoek , Simon Glass , Heinrich Schuchardt , AKASHI Takahiro , Michal Simek , Francis Laniel , Anand Moon , Marek Vasut , Ilias Apalodimas , Masahisa Kojima , Sean Anderson , Siddharth Vadapalli , Yasuharu Shibata , u-boot@lists.denx.de Cc: Mikhail Kshevetskiy Subject: [RESEND PATCH v3 9/9] net/netcat: add netcat over tcp support Date: Wed, 14 Aug 2024 14:31:45 +0400 Message-Id: <20240814103145.1347645-10-mikhail.kshevetskiy@iopsys.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> References: <20240814103145.1347645-1-mikhail.kshevetskiy@iopsys.eu> X-ClientProxiedBy: PA7P264CA0030.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:2df::18) To GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|AM7PR08MB5463:EE_ X-MS-Office365-Filtering-Correlation-Id: 7b8a9eb9-b445-41b8-f4e9-08dcbc4c64ae X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|7416014|52116014|366016|376014|921020|38350700014; X-Microsoft-Antispam-Message-Info: iV7nWy80cpcOOt5YwEo3ceCdlmS8/yUiIawR9WwRphB5maikC7SzGHor+Rdr5OjW0+SFwW0IUfs3SriGF7xOOdedkleDd0EOJXXhcicTQW9769agmOWonsuqkUO382BxIHV1k/rZquY9mLJjId36wYU8qJg6A6kX56Ul+j57pBaFDJUODEAu6TNPGxDiNcdsEP+QOYNkus7y5cSOxRn/b0k9Wtv7V3Di8DEL4y0i7sr91ZNPbCqP4t9GfUru1Cz1SDjUJOQpga6MqHJ6fdCFPbJQigGFPyrxVvXD8XaT4AdewjHYkVUaRpiD/PUg1Dpq4zS9iFTnfv8Z6dimoZFH8eHZUHN8emx1OOjFe14pIDFMJaTb4qla0kGO9Ig9CpckYRsaZEVqYLb24xTqLT5pQUEuEbdzPKsFagcH0eHwAdSTwYnRjIeZPbDWZXnaSJEpR372wxeoH8yQ1SZ58ME87jZUwbJPK2b5HwS16UYsWGedlQSJLwcXGtkfrpcUojfiQCBhHa29HkBCHkr2wfTjdxpcSWUaz+TiRSHa2q7OKnYd6Vy0hg0NWnkLCsGnzaFUhsZB0GVrxvLIT1tTY5Evife8Xg7doY3zU0vLY5f0UmHFjXshMUV1cGu/zn0mzuKcDlnqvOmIS+gitzPuttevo80ZTu5q053K3owwEIK7HOKYvgbTsQgyZNFPJoMxb7FTayUANL6BPbY3/cbQpMcjX2jnwL+888OZP4ccDBqK1f6vm/pJRpHOcineTHuzO0pqKt0r/iVAucht/QTPWsIp217nerVeZOxBp5nDzjD0lEYtm+8SFSzO6peIGE0jaQtH1FkdtvgcNH8CJXysYBW5j4yDd40VuEZmFKTXtwIASQ57b05eOgaYedC+dN746ULbxmRTZVjLGOMQCQ5bAG5xiJYdej6Z23AkmnBrK7sqUBMGTR0yjIgGfBwUYMy37jv2u91b3lj63ZTBpO9NVPk++k4+T1sIZBEOBBKV9PlAy8oTkO5eBTHeaPefl4elqzdwltdIRVTOPmW/1JLRo6UnTk0Vlapook36MtwHR3TRZWWa0VcI9KDZ6VxEl+RO4fS+IqXQq/uYGpguJ4E8jPs8+YcQ60+8CuQs1g8fMvCF99Oq0KlHUgsx0IpbTQV4n7h5Sq8wwPal1QhoVxPI5BYePvGHwLgpdE2wYGaHvF2f5CGcna3aEJVP8TFb7iSnC0T6b4AUiAbZL6C14m2NTbRZPrhk4HXMIUQeqWxxTnFw0MdVeQaK4odXSxvPcBQJjCnFLAywx5WPWXVv4vskiKeHXX3DsjqLx6ltc1kTywzt2IgAhr9+I4Q88idYT2nB1xog03b51MNh6uGbzdmi2w0GBOcSpumdnlz2wdQ8L7EmMf93ef3oVxEbhLlBCyd+Cg0l6bQPMmgeCJKlRZ2nS/Am6VkTdMVUH3vvQdhM4Hu4KgU= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(7416014)(52116014)(366016)(376014)(921020)(38350700014); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: M2RRYjfpv316VJvJWqwc2dUyUH34U9DbIsltGpKBFGSdLv6/pstMm3qgYOh8qbnRdDEs4mLOuPJGCkGpzlqPtRPub6kTeRCeLVhXHQh3rd4F5bAZKNKcOUMssZsxjcFXE8xNhUbwww3oiih1a6BaEL8fKxOl0QirLjU+EFPl/eR8Jp2f7HruwF6xdppWdquB8pC7QfRWOYybw7CJ8oXZzF430/yv5m9Ay7sUUjb7+oKxEaYIYr3e6vn/pMLcc05oTaQ4aFCgsoMshb8SJFcHcZCUdsTpC0BpH9I1ZYbl2MVmFJP+txeyhDRk3HKY6HGwe8pTkiUueLvGofb/61MEK2XkQ/J9rHlUixO1y6vTGP2qEqEvGwhzB3JzlAp2eC/0oM8QEbc3k48fUf41/9ObQzmdTEhjWlv243ZM/mdjGBwaWRs+EiJPVBMHpulb3f3EnUwiMMYk6JCgprqbMLVX9GD0p/XI0cJCasATMNHTn26RNpseMD7bOPTB31caTn0aTEobdhrkXwE75srit7oWDxyQ1brcSoNUCCRzLZnbfa2B+FeaTMoAd/Gd+K4ZtlZAizLgQ8oNYbQ1fL6IMCLZdCXtaqQY0hsiP4xPRvyvsEnqNmqyoqUBDR3PDkeBZImUwxrl8X2hwyiP7kSCqkLAgA0AWM6hYL2LsMrzisJPMn3qvywg2Y5ysGsPYr4ObWFMo6dC3Ce/IIs1qHFq8fqEFijc4n3SWq4deInF2mQRQcWSAYiIpB9rEmSzCHRRXDnBXkH+EFMABMJ/87JqaC2oAbpjSYDIDM7yb5BaVvU+JeM3G1GV5CHIFqx5D4B9c/VWB64CXdwSOTs0AfwWj92e1ZEmaHKgK0cXtTaKaSs3pUfO9xDF6rHXZ9rb8WUl7xGvfJYa7KVEDMp11GOOfCWVa/XtXtoAtkWpd314zG93MYpHxwC/EH5kZAGO/rJetkvUyo2HOnzcTXtYKA1LyoI35E0GL+pKwPbuMVH3tVHQbIkALCPY4SPDQWhpnO0clvuh20J2Rp4wSMvcAtnByCSWe8pQ9p78qRLrtScDE/0Ll9+wtJzgQP5Ny+imOwvfJW32sUGuwmeBdQJlmVUIpQqyaTxenlqglDV3sXSASs+O1eFDD2o0XKe3nTn0q8yxRXpxkxSqVMQ8FeIKJ25yZ5b9GWym5fuJBaGSr8hSOAhj3B+0lBw3GBX523MApaZC5f53eXWbkP5V6sWMoQlBssDJfwCqrFdlFqpul0rrH9TN3ppjkOdWL7HQkGoN+1kB/3QusYHzwBZWcJFPqSq6dpURJWBe9jcevIjnqfiY4LayCjVY2/Did0468DP1PMQMEPNjUtsAwMfIYjY+Xwpfm/2OzC9JbD/XRf9xampLjnd6pjCQ3VQ1nX/JFrywmo68Ecsjivyyr+/N/KtDvP8kJQUnXvsZ0D42WOpJC3FWUgKOP+iWlwCwd88JMgGREcwctfPTcxj/YbQ+7uTumx7rfUyrBATONBDNjQMOwzCBbR0W2NztltY0Td/wz6mUKtkgCGIZ+IdNLl1Fi2bRM5XV5h6Tl20TY4HBMbx0OJabPabsCv2ErWZ/uDtHzU0D3lYplZaE3x1lRiz0K/Ig0fWIHbIDCg== X-OriginatorOrg: iopsys.eu X-MS-Exchange-CrossTenant-Network-Message-Id: 7b8a9eb9-b445-41b8-f4e9-08dcbc4c64ae X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2024 10:32:27.4295 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DrznZiysJ7yyD0wF4bclzgYTR3xkpgtEn7AG7Qvkfs1M8BZGKtxbpOEu67MFoWGzp4RmIYHcVVTrwQvvIu8gucEXtH2ksut+Q8xXf4J77c8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR08MB5463 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This patch adds downloading/uploading of data with netcat. Client/server mode both supported. Signed-off-by: Mikhail Kshevetskiy --- cmd/Kconfig | 7 ++ cmd/net.c | 34 +++++++-- include/net.h | 2 +- include/net/netcat.h | 20 ++++++ net/Makefile | 1 + net/net.c | 9 +++ net/netcat.c | 159 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 6 deletions(-) create mode 100644 include/net/netcat.h create mode 100644 net/netcat.c diff --git a/cmd/Kconfig b/cmd/Kconfig index 978f44eda42..abcd003f7f1 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -2007,6 +2007,13 @@ config CMD_WGET wget is a simple command to download kernel, or other files, from a http server over TCP. +config CMD_NETCAT + bool "netcat" + select PROT_TCP + help + netcat is a simple command to load/store kernel, or other files, + using netcat like manner over TCP. + config CMD_MII bool "mii" imply CMD_MDIO diff --git a/cmd/net.c b/cmd/net.c index 53ce2bc5d0c..6ac81415f8a 100644 --- a/cmd/net.c +++ b/cmd/net.c @@ -206,6 +206,28 @@ U_BOOT_CMD( ); #endif +#if defined(CONFIG_CMD_NETCAT) +static int do_netcat(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + if (argc < 2) + return 1; + + if (strcmp(argv[1], "load") == 0) + return netboot_common(NETCAT_LOAD, cmdtp, argc - 1, argv + 1); + else if (strcmp(argv[1], "store") == 0) + return netboot_common(NETCAT_STORE, cmdtp, argc - 1, argv + 1); + else + return 1; +} + +U_BOOT_CMD( + netcat, 5, 1, do_netcat, + "load/store data via tcp netcat utility", + "load [loadAddress] [[hostIPaddr:]port]\n" + "store Address Size [[hostIPaddr:]port]\n" +); +#endif + static void netboot_update_env(void) { char tmp[46]; @@ -323,16 +345,17 @@ static int parse_args(enum proto_t proto, int argc, char *const argv[]) switch (argc) { case 1: - if (IS_ENABLED(CONFIG_CMD_TFTPPUT) && proto == TFTPPUT) + if ((IS_ENABLED(CONFIG_CMD_TFTPPUT) && proto == TFTPPUT) || + (IS_ENABLED(CONFIG_CMD_NETCAT) && proto == NETCAT_STORE)) return 1; - /* refresh bootfile name from env */ copy_filename(net_boot_file_name, env_get("bootfile"), sizeof(net_boot_file_name)); break; case 2: - if (IS_ENABLED(CONFIG_CMD_TFTPPUT) && proto == TFTPPUT) + if ((IS_ENABLED(CONFIG_CMD_TFTPPUT) && proto == TFTPPUT) || + (IS_ENABLED(CONFIG_CMD_NETCAT) && proto == NETCAT_STORE)) return 1; /* * Only one arg - accept two forms: @@ -354,7 +377,8 @@ static int parse_args(enum proto_t proto, int argc, char *const argv[]) break; case 3: - if (IS_ENABLED(CONFIG_CMD_TFTPPUT) && proto == TFTPPUT) { + if ((IS_ENABLED(CONFIG_CMD_TFTPPUT) && proto == TFTPPUT) || + (IS_ENABLED(CONFIG_CMD_NETCAT) && proto == NETCAT_STORE)) { if (parse_addr_size(argv)) return 1; } else { @@ -365,7 +389,7 @@ static int parse_args(enum proto_t proto, int argc, char *const argv[]) } break; -#ifdef CONFIG_CMD_TFTPPUT +#if defined(CONFIG_CMD_TFTPPUT) || defined(CONFIG_CMD_NETCAT) case 4: if (parse_addr_size(argv)) return 1; diff --git a/include/net.h b/include/net.h index b0ce13e0a9d..5ec826209f0 100644 --- a/include/net.h +++ b/include/net.h @@ -515,7 +515,7 @@ extern int net_restart_wrap; /* Tried all network devices */ enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, DHCP6, PING, PING6, DNS, NFS, CDP, NETCONS, SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT_UDP, FASTBOOT_TCP, - WOL, UDP, NCSI, WGET, RS + WOL, UDP, NCSI, WGET, NETCAT_LOAD, NETCAT_STORE, RS }; extern char net_boot_file_name[1024];/* Boot File name */ diff --git a/include/net/netcat.h b/include/net/netcat.h new file mode 100644 index 00000000000..09470e7f0ce --- /dev/null +++ b/include/net/netcat.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: BSD-2-Clause + * + * netcat include file + * Copyright (C) 2024 IOPSYS Software Solutions AB + * Author: Mikhail Kshevetskiy + */ +#ifndef __NET_NETCAT_TCP_H__ +#define __NET_NETCAT_TCP_H__ + +/** + * netcat_load_start() - begin netcat in loading mode + */ +void netcat_load_start(void); + +/** + * netcat_store_start() - begin netcat in data storing mode + */ +void netcat_store_start(void); + +#endif /* __NET_NETCAT_TCP_H__ */ diff --git a/net/Makefile b/net/Makefile index 64ab7ec740a..dac7b4859fb 100644 --- a/net/Makefile +++ b/net/Makefile @@ -33,6 +33,7 @@ obj-$(CONFIG_CMD_WOL) += wol.o obj-$(CONFIG_PROT_UDP) += udp.o obj-$(CONFIG_PROT_TCP) += tcp.o obj-$(CONFIG_CMD_WGET) += wget.o +obj-$(CONFIG_CMD_NETCAT) += netcat.o # Disable this warning as it is triggered by: # sprintf(buf, index ? "foo%d" : "foo", index) diff --git a/net/net.c b/net/net.c index 86182cc504e..0e05a8f2336 100644 --- a/net/net.c +++ b/net/net.c @@ -108,6 +108,7 @@ #include #include #include +#include #include "arp.h" #include "bootp.h" #include "cdp.h" @@ -564,6 +565,14 @@ restart: wget_start(); break; #endif +#if defined(CONFIG_CMD_NETCAT) + case NETCAT_LOAD: + netcat_load_start(); + break; + case NETCAT_STORE: + netcat_store_start(); + break; +#endif #if defined(CONFIG_CMD_CDP) case CDP: cdp_start(); diff --git a/net/netcat.c b/net/netcat.c new file mode 100644 index 00000000000..ea225c68c87 --- /dev/null +++ b/net/netcat.c @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * netcat support driver + * Copyright (C) 2024 IOPSYS Software Solutions AB + * Author: Mikhail Kshevetskiy + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define HASHES_PER_LINE 65 + +static struct in_addr server_ip; +static u16 server_port; +static u16 local_port; +static int listen; +static int reading; +static unsigned int packets; +static enum net_loop_state netcat_loop_state; + +static void show_block_marker(void) +{ + if ((packets % 10) == 0) + putc('#'); + else if (((packets + 1) % (10 * HASHES_PER_LINE)) == 0) + puts("\n"); +} + +static void tcp_stream_on_closed(struct tcp_stream *tcp) +{ + if (tcp->status != TCP_ERR_OK) + netcat_loop_state = NETLOOP_FAIL; + + if (netcat_loop_state != NETLOOP_SUCCESS) + printf("\nnetcat: Transfer Fail, TCP status - %d\n", tcp->status); + else + printf("\nPackets %s %d, Transfer Successful\n", + reading ? "received" : "transmitted", packets); + net_set_state(netcat_loop_state); +} + +static void tcp_stream_on_rcv_nxt_update(struct tcp_stream *tcp, u32 rx_bytes) +{ + net_boot_file_size = rx_bytes; + show_block_marker(); +} + +static void tcp_stream_on_snd_una_update(struct tcp_stream *tcp, u32 tx_bytes) +{ + show_block_marker(); + if (tx_bytes == image_save_size) + tcp_stream_close(tcp); +} + +static void tcp_stream_on_established(struct tcp_stream *tcp) +{ + netcat_loop_state = NETLOOP_SUCCESS; +} + +static u32 tcp_stream_rx(struct tcp_stream *tcp, u32 rx_offs, void *buf, u32 len) +{ + void *ptr; + + packets++; + ptr = map_sysmem(image_load_addr + rx_offs, len); + memcpy(ptr, buf, len); + unmap_sysmem(ptr); + return len; +} + +static u32 tcp_stream_tx(struct tcp_stream *tcp, u32 tx_offs, void *buf, u32 maxlen) +{ + void *ptr; + + if (tx_offs + maxlen > image_save_size) + maxlen = image_save_size - tx_offs; + if (maxlen == 0) + return 0; + + packets++; + ptr = map_sysmem(image_save_addr + tx_offs, maxlen); + memcpy(buf, ptr, maxlen); + unmap_sysmem(ptr); + return maxlen; +} + +static int tcp_stream_on_create(struct tcp_stream *tcp) +{ + if (listen) { + if (tcp->lport != local_port) + return 0; + } else { + if ((tcp->rhost.s_addr != server_ip.s_addr) || + (tcp->rport != server_port)) + return 0; + } + + netcat_loop_state = NETLOOP_FAIL; + net_boot_file_size = 0; + packets = 0; + + tcp->on_closed = tcp_stream_on_closed; + tcp->on_established = tcp_stream_on_established; + if (reading) { + tcp->on_rcv_nxt_update = tcp_stream_on_rcv_nxt_update; + tcp->rx = tcp_stream_rx; + } else { + tcp->on_snd_una_update = tcp_stream_on_snd_una_update; + tcp->tx = tcp_stream_tx; + } + return 1; +} + +static void netcat_start(void) +{ + struct tcp_stream *tcp; + + memset(net_server_ethaddr, 0, 6); + tcp_stream_set_on_create_handler(tcp_stream_on_create); + + if (strchr(net_boot_file_name, ':') == NULL) { + listen = 1; + printf("Listening on port %s...\n", net_boot_file_name); + + local_port = dectoul(net_boot_file_name, NULL); + } else { + listen = 0; + printf("Connecting to %s...\n", net_boot_file_name); + + server_ip = string_to_ip(net_boot_file_name); + server_port = dectoul(strchr(net_boot_file_name, ':') + 1, NULL); + + tcp = tcp_stream_connect(server_ip, server_port); + if (tcp == NULL) { + printf("No free tcp streams\n"); + net_set_state(NETLOOP_FAIL); + return; + } + tcp_stream_put(tcp); + } +} + +void netcat_load_start(void) +{ + reading = 1; + return netcat_start(); +} + +void netcat_store_start(void) +{ + reading = 0; + return netcat_start(); +}