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 EF115C7EE22 for ; Wed, 10 May 2023 17:00:40 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DA5A286068; Wed, 10 May 2023 19:00:24 +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="XnacunAC"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AFCF086071; Wed, 10 May 2023 19:00:17 +0200 (CEST) Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) (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 B877486068 for ; Wed, 10 May 2023 19:00: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=3mM1bZAkKBssuz358z44Fx55x2v.t53B-s55A2z9A9.uv4E.uv@flex--dimorinny.bounces.google.com Received: by mail-ed1-x549.google.com with SMTP id 4fb4d7f45d1cf-50bcaaeaec0so8473192a12.2 for ; Wed, 10 May 2023 10:00:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683738008; x=1686330008; 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=tT6rU2PvL+GqitTJje5tJCn9KutDJRMSb56Vqq2G3A0=; b=XnacunACQQlraIqGHBjmpyV1bjuuoeuKBxHMFBhjlQiv1Jd6ueW/6PZylJnTXj6ncG 1mXXjrqQHLFJ1NCQ5sMyReSm5wHm2ydK4MuEVk4JawuMDweYPfgqdHVOeWWl548h5xdV 0VA9Iw2Z+dWaNn7KGIuHq94ZNtAdNO0MXWu/osDEMvANpR+92i3Z51v/erJNiLXbTnwt ijmDkJqJCB9TEDsRh39K8rwPg5XxQ7YwLIiESsC1QGYpusCiDQH2yjEzMJLf1oIj5ARE V8pbP/0A89x+UXj6oB3+R50+nAM4OxEp2WwQP/fr0eO3cWjA8G7ocR3XdrnieQxsi8Kx iwHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683738008; x=1686330008; 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=tT6rU2PvL+GqitTJje5tJCn9KutDJRMSb56Vqq2G3A0=; b=lr0N0QbKJHumFtvImRpLB3XyGJrxGs7iqysJVRwCy9CnlgZBz9kewZ0RaPgSvV8gS5 BXtYQG+ZiPUN9L7FICA+/aNI5thOrU0zebUm+vDkxWM1bhBtKpZEprDdwQqR7TgQR3Gz wQ9c4RBnByBOuq9tKF0dJyr4LLsONoSQvC0ZUI6o0Jd+9tStSrfZCW+XXZ+jGq93Z2Zn ycfEjjAu0ZGhgTDWZZ4O7yasnRBVdyheyl/UK+llb1HPl3YtwukTnvOGrMk+LNvOzljN wjNl5fbsSiB3DFFcFIT1Vh212jqE4XyoRp9MzkQSmHX105ExXexFkBwhZGOz8esVbnSL PS6g== X-Gm-Message-State: AC+VfDwHnd6nO43VGwhKd6pYB3eTTtao9Shz442YTK+6eoagw1G9Zkop xNIHLpAd5ezICHk11oepEHDVgKEiBy1kpnbNW0N0B6Px2AW8pmagZkigBgWMaxcgu90Y+B7yMOZ +cRFSStC+Uy51fT6G3Od+mNdSmVmrGRtXCHL/Dq8Aum6RBpJKplL+EMFV3KKZGaSxfDU= X-Google-Smtp-Source: ACHHUZ7dqmGU96T4vs1e2z2tPxniWx5L1RM8dGu8dU+dB/ApmgS66qqj4ksI8lzUV3xjS0virknMGUcDeYnGz2c= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:8054:7d60:263c:94b9]) (user=dimorinny job=sendgmr) by 2002:a50:c341:0:b0:504:8b5c:d90d with SMTP id q1-20020a50c341000000b005048b5cd90dmr6722379edb.1.1683738008214; Wed, 10 May 2023 10:00:08 -0700 (PDT) Date: Wed, 10 May 2023 17:59:56 +0100 In-Reply-To: <20230510165959.2228978-1-dimorinny@google.com> Mime-Version: 1.0 References: <20230510165959.2228978-1-dimorinny@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Message-ID: <20230510165959.2228978-3-dimorinny@google.com> Subject: [PATCH v2 3/6] net: introduce TCP/IP6 support From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: rammuthiah@google.com, rfried.dev@gmail.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 | 106 +++++++++++++++++++++++++++++++++++++++++++++ include/net6.h | 14 ++++++ net/Makefile | 1 + net/net.c | 6 +++ net/net6.c | 72 +++++++++++++++++++++++++----- net/tcp6.c | 99 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 288 insertions(+), 10 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..3db125ecc5 --- /dev/null +++ b/include/net/tcp6.h @@ -0,0 +1,106 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2022 The Android Open Source Project + */ + +#ifndef __TCP6_H__ +#define __TCP6_H__ + +#if defined(CONFIG_PROT_TCP) + +#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 // CONFIG_PROT_TCP +#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..9ef290dc41 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) && defined(CONFIG_PROT_TCP) + 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..575726766e 100644 --- a/net/net6.c +++ b/net/net6.c @@ -14,6 +14,7 @@ #include #include #include +#include #include =20 /* NULL IPv6 address */ @@ -350,11 +351,17 @@ int net_send_ip_packet6(uchar *ether, struct in6_addr= *dest, int dport, int spor u32 tcp_ack_num) { uchar *pkt; + int pkt_hdr_size; int eth_hdr_size; +#if defined(CONFIG_PROT_UDP) || defined(CONFIG_PROT_TCP) int ip_hdr_size; +#endif +#if defined(CONFIG_PROT_UDP) int udp_hdr_size; +#endif +#if defined(CONFIG_PROT_TCP) int tcp_hdr_size; - int pkt_hdr_size; +#endif =20 if (!net_tx_packet) return -1; @@ -362,8 +369,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 +384,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 +430,25 @@ 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) { - struct in_addr zero_ip =3D {.s_addr =3D 0 }; struct icmp6hdr *icmp; +#if defined(CONFIG_PROT_UDP) + struct in_addr zero_ip =3D {.s_addr =3D 0 }; struct udp_hdr *udp; +#endif +#if defined(CONFIG_PROT_TCP) + union tcp6_build_pkt *tcp_headers; + struct tcp_hdr *tcp; +#endif u16 csum; u16 csum_p; u16 hlen; @@ -454,6 +487,7 @@ int net_ip6_handler(struct ethernet_hdr *et, struct ip6= _hdr *ip6, int len) break; } break; +#if defined(CONFIG_PROT_UDP) case IPPROTO_UDP: udp =3D (struct udp_hdr *)(((uchar *)ip6) + IP6_HDR_SIZE); csum =3D udp->udp_xsum; @@ -468,13 +502,31 @@ int net_ip6_handler(struct ethernet_hdr *et, struct i= p6_hdr *ip6, int len) return -EINVAL; =20 /* IP header OK. Pass the packet to the current handler. */ - net_get_udp_handler()((uchar *)ip6 + IP6_HDR_SIZE + - UDP_HDR_SIZE, - ntohs(udp->udp_dst), - zero_ip, - ntohs(udp->udp_src), - ntohs(udp->udp_len) - 8); + net_get_udp_handler()((uchar *)ip6 + IP6_HDR_SIZE + UDP_HDR_SIZE, + ntohs(udp->udp_dst), + zero_ip, + ntohs(udp->udp_src), + ntohs(udp->udp_len) - 8); + break; +#endif +#if defined(CONFIG_PROT_TCP) + 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; +#endif default: return -EINVAL; } diff --git a/net/tcp6.c b/net/tcp6.c new file mode 100644 index 0000000000..09b2190db7 --- /dev/null +++ b/net/tcp6.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2022 The Android Open Source Project + */ + +#if defined(CONFIG_PROT_TCP) + +#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); + } +} + +#endif // CONFIG_PROT_TCP --=20 2.40.1.606.ga4b1b128d6-goog