Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 38EA0C77B7F for ; Mon, 8 May 2023 17:16:31 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0919C85ED7; Mon, 8 May 2023 19:16:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com 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=google.com header.i=@google.com header.b="EG1c+bZV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1C1D085F0D; Mon, 8 May 2023 19:16:05 +0200 (CEST) Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 416B885F0D for ; Mon, 8 May 2023 19:16:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3Ti5ZZAkKBjkYdhjmdiitbjjbgZ.Xjhp-Wjjogdnon.YZis.YZ@flex--dimorinny.bounces.google.com Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-b9a7553f95dso9771795276.2 for ; Mon, 08 May 2023 10:16:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683566159; x=1686158159; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=h8Jzcu8sP+YCRT1F6OuKkxCXE61b/YXFkhA5NljHh7g=; b=EG1c+bZVLf5GFTCJ3rFfowuzkmUP/XqkDj2CFiaZqwD6Oboo2Qvvp1PCQ/bbPKag6n 5UvoQwXboKGw7M7JhPTPanMasj1jiEqbdhoff9XBQcuePVhLVptYMq3Y/hUSXC5HoFJ5 rlwhy0VQlY4RUH+nuiPGAXrAV/2H/oDdzuZbzQOOphtMpeZP3sktGxVsvUAtNFu1SZZb VwTgf3XpzqToO8SkvPwUzBUXpYIbZI4t3rxVEam0JxpMSUIUpGgQDiyn8gNGVvKlyzG0 cJ4H3UrHIKbw+DdLNuxIHwtn3IzAYaA3dOoyrof6WbP9Z9uAXtnfB8hAByJi+Tuka/iG 0ldg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683566159; x=1686158159; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=h8Jzcu8sP+YCRT1F6OuKkxCXE61b/YXFkhA5NljHh7g=; b=DBE065RHU8/tJ9dkd7MX7PV1J76SctYedyx/gVERGriCU91jruR6VL5iBqAUuHpWDb RqYlOATt2kbuwNbR5dyjdeHYwGNoGr759UGyyzUkGp0ec78tcdjTgENvgmwpMVQwv6Ja h9c2JvnXc5Y3FS8NiIlr9v8a5wyOwZzRym7n+t5AmoExUIe1/VJhCi8e2o/dwQUezaaI J7af7kWPVQV+yngE/EyExEBc+0Y7uTYsUaZM5dODlkVZQPHrVCgzNCfTdtB1yUbW9oyT IMj/ZehUKW+blUFSrjCGPTdQ+kdIsNIrjj/EMZkRSCeex/qPyCXpm33DK73NMe5hz13d bHCA== X-Gm-Message-State: AC+VfDyRX+kCSJjdOwOHKn/8RhXe9dkvgy/Zf+Mc3Zb7DkEr0urQqW0A Fj1u3teqX+YHs2zTLfnu9eidi1XOqSJyyIDxfY8qliwhgpw4xTsCzD+ut32+e3vBtfWJbUjAu0A uTNW/IKLnH8gshIp+nTIbuNRKTrvgXwq+UQrC1BK10rd0XbnJp0/n4jtm9biTSiUBVR8= X-Google-Smtp-Source: ACHHUZ6OqXeGGl5JggX6Z4nWbCos6lx5J0RpJ74OjPFNdLM8a9dugIushBDarGGgfnfdZEBl8iMLsv9bLAq3SrY= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:f357:b9b:9cec:e681]) (user=dimorinny job=sendgmr) by 2002:a5b:1c1:0:b0:b9e:1436:990e with SMTP id f1-20020a5b01c1000000b00b9e1436990emr6902693ybp.3.1683566158917; Mon, 08 May 2023 10:15:58 -0700 (PDT) Date: Mon, 8 May 2023 18:15:22 +0100 In-Reply-To: <20230508171525.1082432-1-dimorinny@google.com> Mime-Version: 1.0 References: <20230508171525.1082432-1-dimorinny@google.com> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8-goog Message-ID: <20230508171525.1082432-3-dimorinny@google.com> Subject: [PATCH 3/6] net: introduce TCP/IP6 support From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: rammuthiah@google.com, Dmitrii Merkurev , Ying-Chun Liu , Simon Glass Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 Add TCP/IP6 related headers and reuse refactored TCP/IP implementation Signed-off-by: Dmitrii Merkurev Cc: Ying-Chun Liu (PaulLiu) Cc: Simon Glass =D0=A1=D1=81: Joe Hershberger =D0=A1=D1=81: Ramon Fried --- include/net/tcp6.h | 103 +++++++++++++++++++++++++++++++++++++++++++++ include/net6.h | 14 ++++++ net/Makefile | 1 + net/net.c | 6 +++ net/net6.c | 43 ++++++++++++++++++- net/tcp6.c | 95 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 include/net/tcp6.h create mode 100644 net/tcp6.c diff --git a/include/net/tcp6.h b/include/net/tcp6.h new file mode 100644 index 0000000000..102f5a240a --- /dev/null +++ b/include/net/tcp6.h @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2022 The Android Open Source Project + */ + +#ifndef __TCP6_H__ +#define __TCP6_H__ + +#include +#include + +/** + * typedef rxhand_tcp6_f() - An incoming TCP IPv6 packet handler. + * @pkt: pointer to the application packet + * @dport: destination TCP port + * @sip: source IP6 address + * @sport: source TCP port + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * @action: TCP packet type (SYN, ACK, FIN, etc) + */ +typedef void rxhand_tcp6_f(uchar *pkt, u16 dport, + struct in6_addr sip, u16 sport, + u32 tcp_seq_num, u32 tcp_ack_num, + u8 action, unsigned int len); + +/** + * struct ip6_tcp_hdr_o - IP6 + TCP header + TCP options + * @ip_hdr: IP6 + TCP header + * @tcp_hdr: TCP header + * @tcp_o: TCP options + * @end: end of IP6/TCP header + */ +struct ip6_tcp_hdr_o { + struct ip6_hdr ip_hdr; + struct tcp_hdr tcp_hdr; + struct tcp_hdr_o tcp_o; + u8 end; +} __packed; + +#define IP6_TCP_O_SIZE (sizeof(struct ip6_tcp_hdr_o)) + +/** + * struct ip6_tcp_hdr_s - IP6 + TCP header + TCP options + * @ip_hdr: IP6 + TCP header + * @tcp_hdr: TCP header + * @t_opt: TCP Timestamp Option + * @sack_v: TCP SACK Option + * @end: end of options + */ +struct ip6_tcp_hdr_s { + struct ip6_hdr ip_hdr; + struct tcp_hdr tcp_hdr; + struct tcp_t_opt t_opt; + struct tcp_sack_v sack_v; + u8 end; +} __packed; + +#define IP6_TCP_SACK_SIZE (sizeof(struct ip6_tcp_hdr_s)) + +/** + * union tcp6_build_pkt - union for building TCP/IP6 packet. + * @ip: IP6 and TCP header plus TCP options + * @sack: IP6 and TCP header plus SACK options + * @raw: buffer + */ +union tcp6_build_pkt { + struct ip6_tcp_hdr_o ip; + struct ip6_tcp_hdr_s sack; + uchar raw[1600]; +} __packed; + +/** + * net_set_tcp6_handler6() - set application TCP6 packet handler + * @param f pointer to callback function + */ +void net_set_tcp_handler6(rxhand_tcp6_f *f); + +/** + * net_set_tcp_header6() - set + * @pkt: pointer to IP6/TCP headers + * @dport: destination TCP port + * @sport: source TCP port + * @payload_len: payload length + * @action: TCP packet type (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * + * returns TCP header size + */ +int net_set_tcp_header6(uchar *pkt, u16 dport, u16 sport, int payload_len, + u8 action, u32 tcp_seq_num, u32 tcp_ack_num); + +void net_set_tcp_handler6(rxhand_tcp6_f *f); + +/** + * rxhand_tcp6() - handle incoming IP6 TCP packet + * @param b pointer to IP6/TCP packet builder struct + * @param len full packet length + */ +void rxhand_tcp6(union tcp6_build_pkt *b, unsigned int len); + +#endif // __TCP6_H__ diff --git a/include/net6.h b/include/net6.h index beafc05338..fa926f07ac 100644 --- a/include/net6.h +++ b/include/net6.h @@ -344,6 +344,20 @@ int ip6_add_hdr(uchar *xip, struct in6_addr *src, stru= ct in6_addr *dest, int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport, int sport, int len); =20 +/** + * net_send_tcp_packet6() - Make up TCP packet and send it + * + * @payload_len: TCP payload length + * @dport: destination port + * @sport: source port + * @action: TCP flag (SYN, ACL, PUSH, etc) + * @tcp_seq_num: TCP sequence number + * @tcp_ack_num: TCP ackno + * Return: 0 if send successfully, -1 otherwise + */ +int net_send_tcp_packet6(int payload_len, int dport, int sport, u8 action, + u32 tcp_seq_num, u32 tcp_ack_num); + /** * net_ip6_handler() - Handle IPv6 packet * diff --git a/net/Makefile b/net/Makefile index 3e2d061338..002b0f68a2 100644 --- a/net/Makefile +++ b/net/Makefile @@ -32,6 +32,7 @@ obj-$(CONFIG_TCP_FUNCTION_FASTBOOT) +=3D fastboot_tcp.o obj-$(CONFIG_CMD_WOL) +=3D wol.o obj-$(CONFIG_PROT_UDP) +=3D udp.o obj-$(CONFIG_PROT_TCP) +=3D tcp.o +obj-$(CONFIG_IPV6) +=3D tcp6.o obj-$(CONFIG_CMD_WGET) +=3D wget.o =20 # Disable this warning as it is triggered by: diff --git a/net/net.c b/net/net.c index 0b68bf7b13..3fc6681898 100644 --- a/net/net.c +++ b/net/net.c @@ -92,6 +92,7 @@ #include #include #include +#include #include #include #include @@ -386,6 +387,9 @@ static void net_clear_handlers(void) net_set_udp_handler(NULL); net_set_arp_handler(NULL); net_set_timeout_handler(0, NULL); +#if defined(CONFIG_IPV6) + net_set_tcp_handler6(NULL); +#endif } =20 static void net_cleanup_loop(void) @@ -916,7 +920,9 @@ int net_send_ip_packet(uchar *ether, struct in_addr des= t, int dport, int sport, { uchar *pkt; int eth_hdr_size; +#if defined(CONFIG_PROT_TCP) int ip_tcp_hdr_size; +#endif int pkt_hdr_size; =20 /* make sure the net_tx_packet is initialized (net_init() was called) */ diff --git a/net/net6.c b/net/net6.c index e395b930b0..6418611403 100644 --- a/net/net6.c +++ b/net/net6.c @@ -14,6 +14,7 @@ #include #include #include +#include #include =20 /* NULL IPv6 address */ @@ -362,8 +363,8 @@ int net_send_ip_packet6(uchar *ether, struct in6_addr *= dest, int dport, int spor pkt =3D (uchar *)net_tx_packet; =20 eth_hdr_size =3D net_set_ether(pkt, ether, PROT_IP6); - pkt_hdr_size +=3D eth_hdr_size; - pkt =3D eth_hdr_size; + pkt_hdr_size =3D eth_hdr_size; + pkt +=3D eth_hdr_size; =20 switch (proto) { #if defined(CONFIG_PROT_UDP) @@ -377,6 +378,18 @@ int net_send_ip_packet6(uchar *ether, struct in6_addr = *dest, int dport, int spor pkt_hdr_size +=3D udp_hdr_size; pkt +=3D udp_hdr_size; break; +#endif +#if defined(CONFIG_PROT_TCP) + case IPPROTO_TCP: + tcp_hdr_size =3D net_set_tcp_header6(pkt, dport, sport, + payload_len, action, tcp_seq_num, + tcp_ack_num); + ip_hdr_size =3D ip6_add_hdr(pkt, &net_ip6, dest, IPPROTO_TCP, 64, + tcp_hdr_size + payload_len); + + pkt_hdr_size +=3D ip_hdr_size + tcp_hdr_size; + pkt +=3D ip_hdr_size + tcp_hdr_size; + break; #endif default: return -EINVAL; @@ -411,11 +424,21 @@ int net_send_udp_packet6(uchar *ether, struct in6_add= r *dest, int dport, return net_send_ip_packet6(ether, dest, dport, sport, len, IPPROTO_UDP, 0= , 0, 0); } =20 +int net_send_tcp_packet6(int payload_len, int dport, int sport, u8 action, + u32 tcp_seq_num, u32 tcp_ack_num) +{ + return net_send_ip_packet6(net_server_ethaddr, &net_server_ip6, dport, + sport, payload_len, IPPROTO_TCP, action, + tcp_seq_num, tcp_ack_num); +} + int net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len) { + union tcp6_build_pkt *tcp_headers; struct in_addr zero_ip =3D {.s_addr =3D 0 }; struct icmp6hdr *icmp; struct udp_hdr *udp; + struct tcp_hdr *tcp; u16 csum; u16 csum_p; u16 hlen; @@ -475,6 +498,22 @@ int net_ip6_handler(struct ethernet_hdr *et, struct ip= 6_hdr *ip6, int len) ntohs(udp->udp_src), ntohs(udp->udp_len) - 8); break; + case IPPROTO_TCP: + tcp =3D (struct tcp_hdr *)(((uchar *)ip6) + IP6_HDR_SIZE); + csum =3D tcp->tcp_xsum; + hlen =3D ntohs(ip6->payload_len); + tcp->tcp_xsum =3D 0; + /* checksum */ + csum_p =3D csum_partial((u8 *)tcp, hlen, 0); + tcp->tcp_xsum =3D csum_ipv6_magic(&ip6->saddr, &ip6->daddr, + hlen, IPPROTO_TCP, csum_p); + + if (csum !=3D tcp->tcp_xsum) + return -EINVAL; + + tcp_headers =3D (union tcp6_build_pkt *)ip6; + rxhand_tcp6(tcp_headers, len); + break; default: return -EINVAL; } diff --git a/net/tcp6.c b/net/tcp6.c new file mode 100644 index 0000000000..9add8cddab --- /dev/null +++ b/net/tcp6.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2022 The Android Open Source Project + */ + +#include +#include +#include +#include + +static rxhand_tcp6_f *tcp6_packet_handler; + +static void dummy_handler(uchar *pkt, u16 dport, + struct in6_addr sip, u16 sport, + u32 tcp_seq_num, u32 tcp_ack_num, + u8 action, unsigned int len) +{ +} + +void net_set_tcp_handler6(rxhand_tcp6_f *f) +{ + if (!f) + tcp6_packet_handler =3D dummy_handler; + else + tcp6_packet_handler =3D f; +} + +int net_set_tcp_header6(uchar *pkt, u16 dport, u16 sport, int payload_len, + u8 action, u32 tcp_seq_num, u32 tcp_ack_num) +{ + union tcp6_build_pkt *b =3D (union tcp6_build_pkt *)pkt; + int tcp_hdr_len; + int pkt_len; + u16 csum; + + pkt_len =3D IP6_HDR_SIZE; + tcp_hdr_len =3D net_set_tcp_header_common(&b->ip.tcp_hdr, &b->ip.tcp_o, + &b->sack.t_opt, &b->sack.sack_v, + dport, sport, payload_len, action, + tcp_seq_num, tcp_ack_num); + pkt_len +=3D tcp_hdr_len; + pkt_len +=3D payload_len; + + csum =3D csum_partial((u8 *)&b->ip.tcp_hdr, tcp_hdr_len + payload_len, 0)= ; + b->ip.tcp_hdr.tcp_xsum =3D csum_ipv6_magic(&net_ip6, &net_server_ip6, + tcp_hdr_len + payload_len, + IPPROTO_TCP, csum); + + return tcp_hdr_len; +} + +void rxhand_tcp6(union tcp6_build_pkt *b, unsigned int len) +{ + int tcp_len =3D len - IP6_HDR_SIZE; + u8 tcp_action =3D TCP_DATA; + u32 tcp_seq_num, tcp_ack_num; + u32 res_tcp_seq_num, res_tcp_ack_num; + int tcp_hdr_len, payload_len; + + net_copy_ip6(&net_server_ip6, &b->ip.ip_hdr.saddr); + + tcp_hdr_len =3D GET_TCP_HDR_LEN_IN_BYTES(b->ip.tcp_hdr.tcp_hlen); + payload_len =3D tcp_len - tcp_hdr_len; + + if (tcp_hdr_len > TCP_HDR_SIZE) + tcp_parse_options((uchar *)b + IP6_HDR_SIZE + TCP_HDR_SIZE, + tcp_hdr_len - TCP_HDR_SIZE); + + tcp_seq_num =3D ntohl(b->ip.tcp_hdr.tcp_seq); + tcp_ack_num =3D ntohl(b->ip.tcp_hdr.tcp_ack); + + tcp_action =3D tcp_state_machine(b->ip.tcp_hdr.tcp_flags, + tcp_seq_num, &res_tcp_seq_num, &res_tcp_ack_num, + payload_len); + + if ((tcp_action & TCP_PUSH) || payload_len > 0) { + debug_cond(DEBUG_DEV_PKT, + "TCP Notify (action=3D%x, Seq=3D%u,Ack=3D%u,Pay%d)\n", + tcp_action, tcp_seq_num, tcp_ack_num, payload_len); + + (*tcp6_packet_handler) ((uchar *)b + len - payload_len, b->ip.tcp_hdr.tc= p_dst, + b->ip.ip_hdr.saddr, b->ip.tcp_hdr.tcp_src, tcp_seq_num, + tcp_ack_num, tcp_action, payload_len); + + } else if (tcp_action !=3D TCP_DATA) { + debug_cond(DEBUG_DEV_PKT, + "TCP Action (action=3D%x,Seq=3D%u,Ack=3D%u,Pay=3D%d)\n", + tcp_action, res_tcp_seq_num, res_tcp_ack_num, payload_len); + + net_send_tcp_packet6(0, ntohs(b->ip.tcp_hdr.tcp_src), + ntohs(b->ip.tcp_hdr.tcp_dst), + (tcp_action & (~TCP_PUSH)), + res_tcp_seq_num, res_tcp_ack_num); + } +} --=20 2.40.1.521.gf1e218fcd8-goog