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 1CA87C0015E for ; Tue, 25 Jul 2023 21:57:24 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 87030867E3; Tue, 25 Jul 2023 23:57:17 +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="rIq0/rBI"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5406C867E3; Tue, 25 Jul 2023 23:57:15 +0200 (CEST) Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) (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 A97528475C for ; Tue, 25 Jul 2023 23:57:09 +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=3NEXAZAkKBusQVZbeVaalTbbTYR.PbZh-ObbgYVfgf.QRak.QR@flex--dimorinny.bounces.google.com Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-573d70da2dcso72815117b3.1 for ; Tue, 25 Jul 2023 14:57:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690322228; x=1690927028; 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=xcEXa0a/TBV4cVbiu3mXQp8P5jI0kMOXohOqmhvaLWo=; b=rIq0/rBId6zhz2gT/KwAhNTe3qQwJ6BoBhvSlDrq28jJUi77Uh78V7vt/gZQ0p0ehb dHBSYY5t9b2qVBKTtmrcKZSMTzEU1xpB8KfGIM/TQlhgo4nMI7mQvYOq01cpp5a0ez0W nn0GlSZwabW8L17ajwwNcwfmWBIwuvUM7sHql0mkjquAsXgDQ97KsGR+626vHjzydEJf aLrs8Gz3WY9GApWc8CzjDI4gl/3ZBKucDKRjbiLIWFOl4Ex/ubJeowMV7OVZCMRblvDk 47P7SreGF/KyfnppfWHBHEzrocyq/4fOF7jOhTIjZP70OxSCFj0EdUwtpQPXi1o7YeI4 A9ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690322228; x=1690927028; 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=xcEXa0a/TBV4cVbiu3mXQp8P5jI0kMOXohOqmhvaLWo=; b=HRLJOjxDxFcy6AKBy8rILzKxLfOw9bhuzkXQuBOBrrRhpHAmTCBtK4JhAnNPBw0EMU tREVLm/ouyUSav4tiwAXlv1zzSrc7edlHc321E0fivxWVy7J/lvX3HQGpYthCV9Ptkut UimvX0fEaTZefjJon7+fSLGDkgDmtcTHo0nyvrnYyJ1RsHjOTavArDGSKFV8Sb2NL7h+ 6rb0ZNkvj67kegXrFckq47dghu8DJtpWICLjoipxo8CgsSW92ZIBTRJAeQIPG86LDq/m gRgUF0HhpbgU+cHiaAt1A1t9sH1X3BlFwc6wUhrPzD7gEgoh1/vWeZJzX6QpMxtDnI6p eTeA== X-Gm-Message-State: ABy/qLbjQxfSNn6IMYiOQkF/YgGnlqle1Bq/UwiNQiWiL3kbkdF+JE+8 WoK2mnRl1gEDrx07ly2okIsiyLa1vDBsAkaWn+ipPmnj4jyiSSJmKUNDm4ETSXVW1rJ2pt8HN6W KRWOPn6Tp4jrHeJuAGb3twbUNvC3hAU0b6gm6EIhbzSOsWndRJIlUyBsTjie/XbpFNU8= X-Google-Smtp-Source: APBJJlFtOZc95Uh5EHyzANbny89KwB6TndtoPqBfTzCEMbHzv5CplNxN8euWQ8o+J6n5qdN/QNgrMDQq6aXcrD4= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:3ee0:2483:5af7:8f8e]) (user=dimorinny job=sendgmr) by 2002:a05:690c:728:b0:581:3939:59a2 with SMTP id bt8-20020a05690c072800b00581393959a2mr4903ywb.3.1690322228405; Tue, 25 Jul 2023 14:57:08 -0700 (PDT) Date: Tue, 25 Jul 2023 22:56:53 +0100 In-Reply-To: <20230725215658.2118831-1-dimorinny@google.com> Mime-Version: 1.0 References: <20230725215658.2118831-1-dimorinny@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230725215658.2118831-2-dimorinny@google.com> Subject: [PATCH v3 2/6] net: prepare existing TCP stack to be reused by IP6 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 Changes: 1. Separate reusable part from net_set_tcp_header to net_set_tcp_header_common 2. Make TCP signatures reusable by receiving particular IP agnostic TCP headers 3. Extract net_send_ip_packet6 from net_send_udp_packet6 to reuse the code 4. Expose TCP state machine related functions This allows us to reuse TCP logic between IP and IP6 stack. 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 Reviewed-by: Ying-Chun Liu (PaulLiu) Reviewed-by: Simon Glass --- include/net.h | 26 +++- include/net/tcp.h | 109 +++++++++++++-- net/net.c | 24 ++-- net/tcp.c | 337 ++++++++++++++++++++++++++++------------------ 4 files changed, 342 insertions(+), 154 deletions(-) diff --git a/include/net.h b/include/net.h index e254df7d7f..2e19d9defc 100644 --- a/include/net.h +++ b/include/net.h @@ -569,11 +569,29 @@ int net_eth_hdr_size(void); int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot); int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot); =20 -/* Set IP header */ -void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr sou= rce, +/** + * net_set_ip_header() - Initialize IP header for the given packet + * + * @pkt_ip_hdr: Pointer to IP header + * @dest: IPv4 destination address + * @source: IPv4 source address + * @pkt_len: Length of ip_header + tcp_header + payload + * @proto: Transport level protocol (e.g tcp/udp/etc) + */ +void net_set_ip_header(uchar *pkt_ip_hdr, struct in_addr dest, struct in_a= ddr source, u16 pkt_len, u8 proto); -void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, - int sport, int len); + +/** + * net_set_udp_header() - Initialize IP and UDP header for the given packe= t + * + * @pkt_ip_hdr: Pointer to IP header + * @dest: IPv4 destination address + * @dport: Destination port + * @sport: Source port + * @len: Payload length + */ +void net_set_udp_header(uchar *pkt_ip_hdr, struct in_addr dest, int dport, + int sport, int len); =20 /** * compute_ip_checksum() - Compute IP checksum diff --git a/include/net/tcp.h b/include/net/tcp.h index 93ed728dfe..344b4be2a4 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -8,10 +8,20 @@ #ifndef __TCP_H__ #define __TCP_H__ =20 +#include + #define TCP_ACTIVITY 127 /* Number of packets received */ /* before console progress mark */ =20 +/* + * TCP lengths are stored as a rounded up number of 32 bit words. + * Add 3 to length round up, rounded, then divided into the + * length in 32 bit words. + */ +#define LEN_B_TO_DW(x) ((x) >> 2) +#define ROUND_TCPHDR_LEN(x) (LEN_B_TO_DW((x) + 3)) #define GET_TCP_HDR_LEN_IN_BYTES(x) ((x) >> 2) +#define SHIFT_TO_TCPHDRLEN_FIELD(x) ((x) << 4) =20 /** * struct tcp_hdr - TCP header @@ -24,7 +34,7 @@ * @tcp_win: TCP windows size * @tcp_xsum: Checksum * @tcp_ugr: Pointer to urgent data -*/ + */ struct tcp_hdr { u16 tcp_src; u16 tcp_dst; @@ -163,18 +173,14 @@ struct tcp_t_opt { */ =20 /** - * struct ip_tcp_hdr_o - IP + TCP header + TCP options - * @ip_hdr: IP + TCP header - * @tcp_hdr: TCP header + * struct tcp_hdr_o - TCP options * @mss: TCP MSS Option * @scale: TCP Windows Scale Option * @sack_p: TCP Sack-Permitted Option * @t_opt: TCP Timestamp Option * @end: end of options */ -struct ip_tcp_hdr_o { - struct ip_hdr ip_hdr; - struct tcp_hdr tcp_hdr; +struct tcp_hdr_o { struct tcp_mss mss; struct tcp_scale scale; struct tcp_sack_p sack_p; @@ -182,6 +188,22 @@ struct ip_tcp_hdr_o { u8 end; } __packed; =20 +#define TCP_O_SIZE (sizeof(struct tcp_hdr_o)) + +/** + * struct ip_tcp_hdr_o - IP + TCP header + TCP options + * @ip_hdr: IP + TCP header + * @tcp_hdr: TCP header + * @tcp_o: TCP options + * @end: end of IP/TCP header + */ +struct ip_tcp_hdr_o { + struct ip_hdr ip_hdr; + struct tcp_hdr tcp_hdr; + struct tcp_hdr_o tcp_o; + u8 end; +} __packed; + #define IP_TCP_O_SIZE (sizeof(struct ip_tcp_hdr_o)) =20 /** @@ -209,7 +231,7 @@ struct ip_tcp_hdr_s { =20 /** * struct pseudo_hdr - Pseudo Header - * @padding: pseudo hdr size =3D ip_tcp hdr size + * @padding: pseudo hdr size =3D ip hdr size * @p_src: Source IP address * @p_dst: Destination IP address * @rsvd: reserved @@ -236,7 +258,6 @@ struct pseudo_hdr { * * Build Pseudo header in packed buffer * first, calculate TCP checksum, then build IP header in packed buffer. - * */ union tcp_build_pkt { struct pseudo_hdr ph; @@ -269,9 +290,77 @@ enum tcp_state { =20 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, + +/** + * net_set_tcp_header_common() - IP version agnostic TCP header building i= mplementation + * + * @tcp_hdr: pointer to TCP header struct + * @tcp_o: pointer to TCP options header struct + * @sack_t_opt: pointer to TCP sack options header struct + * @sack_v: pointer to TCP sack header struct + * @dport: destination TCP port + * @sport: source TCP port + * @payload_len: TCP payload len + * @action: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * + * returns TCP header + */ +int net_set_tcp_header_common(struct tcp_hdr *tcp_hdr, struct tcp_hdr_o *t= cp_o, + struct tcp_t_opt *sack_t_opt, struct tcp_sack_v *sack_v, + u16 dport, u16 sport, int payload_len, u8 action, + u32 tcp_seq_num, u32 tcp_ack_num); + +/** + * net_set_tcp_header() - IPv4 TCP header bulding implementation + * + * @pkt: pointer to the IP header + * @dport: destination TCP port + * @sport: source TCP port + * @payload_len: TCP payload len + * @action: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * + * returns TCP header + */ +int net_set_tcp_header(uchar *pkt, u16 dport, u16 sport, int payload_len, u8 action, u32 tcp_seq_num, u32 tcp_ack_num); =20 +/** + * tcp_parse_options() - parsing TCP options + * + * @o: pointer to the option field. + * @o_len: length of the option field. + */ +void tcp_parse_options(uchar *o, int o_len); + +/** + * tcp_state_machine() - update TCP state in a reaction to incoming packet + * + * @tcp_flags: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_seq_num_out: TCP sequential number we expect to answer with + * @tcp_ack_num_out: TCP acknowledgment number we expect to answer with + * @payload_len: TCP payload len + * + * returns TCP action we expect to answer with + */ +u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, u32 *tcp_seq_num_out, + u32 *tcp_ack_num_out, int payload_len); + +/** + * tcp_sent_state_machine() - update TCP state in a reaction to outcoming = packet + * + * @action: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * + * returns TCP action we expect to answer with + */ +u8 tcp_sent_state_machine(u8 action, u32 *tcp_seq_num, u32 *tcp_ack_num); + /** * rxhand_tcp() - An incoming packet handler. * @pkt: pointer to the application packet diff --git a/net/net.c b/net/net.c index 43abbac7c3..75a2bbaee3 100644 --- a/net/net.c +++ b/net/net.c @@ -914,8 +914,13 @@ int net_send_ip_packet(uchar *ether, struct in_addr de= st, int dport, int sport, int payload_len, int proto, u8 action, u32 tcp_seq_num, u32 tcp_ack_num) { + if ((proto =3D=3D IPPROTO_UDP && !IS_ENABLED(CONFIG_PROT_UDP)) || + (proto =3D=3D IPPROTO_TCP && !IS_ENABLED(CONFIG_PROT_TCP))) + return -EINVAL; + uchar *pkt; int eth_hdr_size; + int ip_tcp_hdr_size; int pkt_hdr_size; =20 /* make sure the net_tx_packet is initialized (net_init() was called) */ @@ -934,21 +939,24 @@ int net_send_ip_packet(uchar *ether, struct in_addr d= est, int dport, int sport, pkt =3D (uchar *)net_tx_packet; =20 eth_hdr_size =3D net_set_ether(pkt, ether, PROT_IP); + pkt_hdr_size =3D eth_hdr_size; + pkt +=3D eth_hdr_size; =20 switch (proto) { case IPPROTO_UDP: - net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport, + net_set_udp_header(pkt, dest, dport, sport, payload_len); - pkt_hdr_size =3D eth_hdr_size + IP_UDP_HDR_SIZE; + pkt_hdr_size +=3D IP_UDP_HDR_SIZE; break; -#if defined(CONFIG_PROT_TCP) case IPPROTO_TCP: - pkt_hdr_size =3D eth_hdr_size - + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport, - payload_len, action, tcp_seq_num, - tcp_ack_num); + ip_tcp_hdr_size =3D IP_HDR_SIZE; + ip_tcp_hdr_size +=3D net_set_tcp_header(pkt, dport, sport, + payload_len, action, tcp_seq_num, + tcp_ack_num); + net_set_ip_header(pkt, net_server_ip, net_ip, + ip_tcp_hdr_size + payload_len, IPPROTO_TCP); + pkt_hdr_size +=3D ip_tcp_hdr_size; break; -#endif default: return -EINVAL; } diff --git a/net/tcp.c b/net/tcp.c index 10ce799814..483c03a595 100644 --- a/net/tcp.c +++ b/net/tcp.c @@ -54,16 +54,6 @@ static struct sack_r edge_a[TCP_SACK]; static unsigned int sack_idx; static unsigned int prev_len; =20 -/* - * TCP lengths are stored as a rounded up number of 32 bit words. - * Add 3 to length round up, rounded, then divided into the - * length in 32 bit words. - */ -#define LEN_B_TO_DW(x) ((x) >> 2) -#define ROUND_TCPHDR_LEN(x) (LEN_B_TO_DW((x) + 3)) -#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; =20 @@ -149,29 +139,32 @@ u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr = src, struct in_addr dest, =20 /** * net_set_ack_options() - set TCP options in acknowledge packets - * @b: the packet + * @tcp_hdr: pointer to TCP header struct + * @t_opt: pointer to TCP t opt header struct + * @sack_v: pointer to TCP sack header struct * * Return: TCP header length */ -int net_set_ack_options(union tcp_build_pkt *b) +int net_set_ack_options(struct tcp_hdr *tcp_hdr, struct tcp_t_opt *t_opt, + struct tcp_sack_v *sack_v) { - b->sack.tcp_hdr.tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR= _SIZE)); + tcp_hdr->tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE))= ; =20 - b->sack.t_opt.kind =3D TCP_O_TS; - b->sack.t_opt.len =3D TCP_OPT_LEN_A; - b->sack.t_opt.t_snd =3D htons(loc_timestamp); - b->sack.t_opt.t_rcv =3D rmt_timestamp; - b->sack.sack_v.kind =3D TCP_1_NOP; - b->sack.sack_v.len =3D 0; + t_opt->kind =3D TCP_O_TS; + t_opt->len =3D TCP_OPT_LEN_A; + t_opt->t_snd =3D htons(loc_timestamp); + t_opt->t_rcv =3D rmt_timestamp; + sack_v->kind =3D TCP_1_NOP; + sack_v->len =3D 0; =20 if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) { 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 =3D tcp_lost.len; - b->sack.sack_v.kind =3D TCP_V_SACK; - b->sack.sack_v.hill[0].l =3D htonl(tcp_lost.hill[0].l); - b->sack.sack_v.hill[0].r =3D htonl(tcp_lost.hill[0].r); + sack_v->len =3D tcp_lost.len; + sack_v->kind =3D TCP_V_SACK; + sack_v->hill[0].l =3D htonl(tcp_lost.hill[0].l); + sack_v->hill[0].r =3D htonl(tcp_lost.hill[0].r); =20 /* * These SACK structures are initialized with NOPs to @@ -179,21 +172,21 @@ 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 =3D htonl(tcp_lost.hill[1].l); - b->sack.sack_v.hill[1].r =3D htonl(tcp_lost.hill[1].r); - b->sack.sack_v.hill[2].l =3D htonl(tcp_lost.hill[2].l); - b->sack.sack_v.hill[2].r =3D htonl(tcp_lost.hill[2].r); - b->sack.sack_v.hill[3].l =3D TCP_O_NOP; - b->sack.sack_v.hill[3].r =3D TCP_O_NOP; + sack_v->hill[1].l =3D htonl(tcp_lost.hill[1].l); + sack_v->hill[1].r =3D htonl(tcp_lost.hill[1].r); + sack_v->hill[2].l =3D htonl(tcp_lost.hill[2].l); + sack_v->hill[2].r =3D htonl(tcp_lost.hill[2].r); + sack_v->hill[3].l =3D TCP_O_NOP; + sack_v->hill[3].r =3D TCP_O_NOP; } =20 - b->sack.tcp_hdr.tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(T= CP_HDR_SIZE + - TCP_TSOPT_SIZE + - tcp_lost.len)); + tcp_hdr->tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_= SIZE + + TCP_TSOPT_SIZE + + tcp_lost.len)); } else { - b->sack.sack_v.kind =3D 0; - b->sack.tcp_hdr.tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(T= CP_HDR_SIZE + - TCP_TSOPT_SIZE)); + sack_v->kind =3D 0; + tcp_hdr->tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_= SIZE + + TCP_TSOPT_SIZE)); } =20 /* @@ -201,69 +194,61 @@ int net_set_ack_options(union tcp_build_pkt *b) * TCP header to add to the total packet length */ =20 - return GET_TCP_HDR_LEN_IN_BYTES(b->sack.tcp_hdr.tcp_hlen); + return GET_TCP_HDR_LEN_IN_BYTES(tcp_hdr->tcp_hlen); } =20 /** * net_set_ack_options() - set TCP options in SYN packets - * @b: the packet + * @tcp_hdr: pointer to TCP header struct + * @options: pointer to TCP header options struct */ -void net_set_syn_options(union tcp_build_pkt *b) +void net_set_syn_options(struct tcp_hdr *tcp_hdr, struct tcp_hdr_o *option= s) { if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) tcp_lost.len =3D 0; =20 - b->ip.tcp_hdr.tcp_hlen =3D 0xa0; + tcp_hdr->tcp_hlen =3D 0xa0; =20 - b->ip.mss.kind =3D TCP_O_MSS; - b->ip.mss.len =3D TCP_OPT_LEN_4; - b->ip.mss.mss =3D htons(TCP_MSS); - b->ip.scale.kind =3D TCP_O_SCL; - b->ip.scale.scale =3D TCP_SCALE; - b->ip.scale.len =3D TCP_OPT_LEN_3; + options->mss.kind =3D TCP_O_MSS; + options->mss.len =3D TCP_OPT_LEN_4; + options->mss.mss =3D htons(TCP_MSS); + options->scale.kind =3D TCP_O_SCL; + options->scale.scale =3D TCP_SCALE; + options->scale.len =3D TCP_OPT_LEN_3; if (IS_ENABLED(CONFIG_PROT_TCP_SACK)) { - b->ip.sack_p.kind =3D TCP_P_SACK; - b->ip.sack_p.len =3D TCP_OPT_LEN_2; + options->sack_p.kind =3D TCP_P_SACK; + options->sack_p.len =3D TCP_OPT_LEN_2; } else { - b->ip.sack_p.kind =3D TCP_1_NOP; - b->ip.sack_p.len =3D TCP_1_NOP; + options->sack_p.kind =3D TCP_1_NOP; + options->sack_p.len =3D TCP_1_NOP; } - b->ip.t_opt.kind =3D TCP_O_TS; - b->ip.t_opt.len =3D TCP_OPT_LEN_A; + options->t_opt.kind =3D TCP_O_TS; + options->t_opt.len =3D TCP_OPT_LEN_A; loc_timestamp =3D get_ticks(); rmt_timestamp =3D 0; - b->ip.t_opt.t_snd =3D 0; - b->ip.t_opt.t_rcv =3D 0; - b->ip.end =3D TCP_O_END; + + options->t_opt.t_snd =3D 0; + options->t_opt.t_rcv =3D 0; } =20 -int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, - u8 action, u32 tcp_seq_num, u32 tcp_ack_num) +/** + * tcp_sent_state_machine() - update TCP state in a reaction to outcoming = packet + * + * @action: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * + * returns TCP action we expect to answer with + */ +u8 tcp_sent_state_machine(u8 action, u32 *tcp_seq_num, u32 *tcp_ack_num) { - union tcp_build_pkt *b =3D (union tcp_build_pkt *)pkt; - int pkt_hdr_len; - int pkt_len; - int tcp_len; - - /* - * Header: 5 32 bit words. 4 bits TCP header Length, - * 4 bits reserved options - */ - b->ip.tcp_hdr.tcp_flags =3D action; - pkt_hdr_len =3D IP_TCP_HDR_SIZE; - b->ip.tcp_hdr.tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_S= IZE)); - switch (action) { case TCP_SYN: debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:SYN (%pI4, %pI4, sq=3D%u, ak=3D%u)\n", - &net_server_ip, &net_ip, - tcp_seq_num, tcp_ack_num); + "TCP Hdr:SYN (sq=3D%u, ak=3D%u)\n", *tcp_seq_num, *tcp_ack_num); tcp_activity_count =3D 0; - net_set_syn_options(b); - tcp_seq_num =3D 0; - tcp_ack_num =3D 0; - pkt_hdr_len =3D IP_TCP_O_SIZE; + *tcp_seq_num =3D 0; + *tcp_ack_num =3D 0; if (current_tcp_state =3D=3D TCP_SYN_SENT) { /* Too many SYNs */ action =3D TCP_FIN; current_tcp_state =3D TCP_FIN_WAIT_1; @@ -273,57 +258,93 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int spo= rt, int payload_len, break; case TCP_SYN | TCP_ACK: case TCP_ACK: - pkt_hdr_len =3D IP_HDR_SIZE + net_set_ack_options(b); - b->ip.tcp_hdr.tcp_flags =3D action; debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:ACK (%pI4, %pI4, s=3D%u, a=3D%u, A=3D%x)\n", - &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num, - action); + "TCP Hdr:ACK (s=3D%u, a=3D%u, A=3D%x)\n", + *tcp_seq_num, *tcp_ack_num, action); break; case TCP_FIN: debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:FIN (%pI4, %pI4, s=3D%u, a=3D%u)\n", - &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num); - payload_len =3D 0; - pkt_hdr_len =3D IP_TCP_HDR_SIZE; + "TCP Hdr:FIN (s=3D%u, a=3D%u)\n", *tcp_seq_num, *tcp_ack_num); current_tcp_state =3D TCP_FIN_WAIT_1; break; case TCP_RST | TCP_ACK: case TCP_RST: debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:RST (%pI4, %pI4, s=3D%u, a=3D%u)\n", - &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num); + "TCP Hdr:RST (s=3D%u, a=3D%u)\n", *tcp_seq_num, *tcp_ack_num); current_tcp_state =3D TCP_CLOSED; 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(s=3D%u, a=3D%u, A=3D%x)\n", + *tcp_seq_num, *tcp_ack_num, action); if (current_tcp_state =3D=3D TCP_CLOSE_WAIT) current_tcp_state =3D TCP_CLOSING; - - debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:FIN ACK PSH(%pI4, %pI4, s=3D%u, a=3D%u, A=3D%x)\n", - &net_server_ip, &net_ip, - tcp_seq_num, tcp_ack_num, action); fallthrough; default: - pkt_hdr_len =3D IP_HDR_SIZE + net_set_ack_options(b); - b->ip.tcp_hdr.tcp_flags =3D action | TCP_PUSH | TCP_ACK; + action =3D action | TCP_PUSH | TCP_ACK; debug_cond(DEBUG_DEV_PKT, - "TCP Hdr:dft (%pI4, %pI4, s=3D%u, a=3D%u, A=3D%x)\n", - &net_server_ip, &net_ip, - tcp_seq_num, tcp_ack_num, action); + "TCP Hdr:dft (s=3D%u, a=3D%u, A=3D%x)\n", + *tcp_seq_num, *tcp_ack_num, action); } =20 - pkt_len =3D pkt_hdr_len + payload_len; - tcp_len =3D pkt_len - IP_HDR_SIZE; + return action; +} + +/** + * net_set_tcp_header_common() - IP version agnostic TCP header building i= mplementation + * + * @tcp_hdr: pointer to TCP header struct + * @tcp_o: pointer to TCP options header struct + * @sack_t_opt: pointer to TCP sack options header struct + * @sack_v: pointer to TCP sack header struct + * @dport: destination TCP port + * @sport: source TCP port + * @payload_len: TCP payload len + * @action: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * + * returns TCP header size + */ +int net_set_tcp_header_common(struct tcp_hdr *tcp_hdr, struct tcp_hdr_o *t= cp_o, + struct tcp_t_opt *sack_t_opt, struct tcp_sack_v *sack_v, + u16 dport, u16 sport, int payload_len, u8 action, + u32 tcp_seq_num, u32 tcp_ack_num) +{ + u8 tcp_action =3D TCP_DATA; + int tcp_hdr_len; + + /* + * Header: 5 32 bit words. 4 bits TCP header Length, + * 4 bits reserved options + */ + tcp_hdr_len =3D TCP_HDR_SIZE; + tcp_hdr->tcp_hlen =3D SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE))= ; + + switch (action) { + case TCP_SYN: + net_set_syn_options(tcp_hdr, tcp_o); + tcp_hdr_len =3D TCP_HDR_SIZE + TCP_O_SIZE; + break; + case TCP_RST | TCP_ACK: + case TCP_RST: + case TCP_FIN: + payload_len =3D 0; + break; + default: + tcp_hdr_len =3D net_set_ack_options(tcp_hdr, sack_t_opt, sack_v); + } + + tcp_action =3D tcp_sent_state_machine(action, &tcp_seq_num, &tcp_ack_num)= ; + tcp_hdr->tcp_flags =3D tcp_action; =20 tcp_ack_edge =3D tcp_ack_num; - /* TCP Header */ - b->ip.tcp_hdr.tcp_ack =3D htonl(tcp_ack_edge); - b->ip.tcp_hdr.tcp_src =3D htons(sport); - b->ip.tcp_hdr.tcp_dst =3D htons(dport); - b->ip.tcp_hdr.tcp_seq =3D htonl(tcp_seq_num); + tcp_hdr->tcp_ack =3D htonl(tcp_ack_edge); + tcp_hdr->tcp_seq =3D htonl(tcp_seq_num); + tcp_hdr->tcp_src =3D htons(sport); + tcp_hdr->tcp_dst =3D htons(dport); =20 /* * TCP window size - TCP header variable tcp_win. @@ -340,18 +361,46 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int spo= rt, int payload_len, * it is, then the u-boot tftp or nfs kernel netboot should be * considered. */ - b->ip.tcp_hdr.tcp_win =3D htons(PKTBUFSRX * TCP_MSS >> TCP_SCALE); + tcp_hdr->tcp_win =3D htons(PKTBUFSRX * TCP_MSS >> TCP_SCALE); =20 - b->ip.tcp_hdr.tcp_xsum =3D 0; - b->ip.tcp_hdr.tcp_ugr =3D 0; + tcp_hdr->tcp_xsum =3D 0; + tcp_hdr->tcp_ugr =3D 0; =20 - b->ip.tcp_hdr.tcp_xsum =3D tcp_set_pseudo_header(pkt, net_ip, net_server_= ip, - tcp_len, pkt_len); + return tcp_hdr_len; +} =20 - net_set_ip_header((uchar *)&b->ip, net_server_ip, net_ip, - pkt_len, IPPROTO_TCP); +/** + * net_set_tcp_header() - IPv4 TCP header bulding implementation + * + * @pkt: pointer to the IP header + * @dport: destination TCP port + * @sport: source TCP port + * @payload_len: TCP payload len + * @action: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_ack_num: TCP acknowledgment number + * + * returns TCP header + payload size + */ +int net_set_tcp_header(uchar *pkt, u16 dport, u16 sport, int payload_len, + u8 action, u32 tcp_seq_num, u32 tcp_ack_num) +{ + union tcp_build_pkt *b =3D (union tcp_build_pkt *)pkt; + int tcp_hdr_len; + int pkt_len; =20 - return pkt_hdr_len; + pkt_len =3D IP_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; + + b->ip.tcp_hdr.tcp_xsum =3D tcp_set_pseudo_header(pkt, net_ip, net_server_= ip, + tcp_hdr_len + payload_len, pkt_len); + + return tcp_hdr_len; } =20 /** @@ -500,7 +549,31 @@ void tcp_parse_options(uchar *o, int o_len) } } =20 -static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len= ) +static void init_sack_options(u32 tcp_seq_num, u32 tcp_ack_num) +{ + tcp_seq_init =3D tcp_seq_num; + tcp_ack_edge =3D tcp_ack_num; + sack_idx =3D 0; + edge_a[sack_idx].se.l =3D tcp_ack_edge; + edge_a[sack_idx].se.r =3D tcp_ack_edge; + prev_len =3D 0; + for (int i =3D 0; i < TCP_SACK; i++) + edge_a[i].st =3D NOPKT; +} + +/** + * tcp_state_machine() - update TCP state in a reaction to incoming reques= t + * + * @tcp_flags: TCP action (SYN, ACK, FIN, etc) + * @tcp_seq_num: TCP sequential number + * @tcp_seq_num_out: TCP sequential number we expect to answer with + * @tcp_ack_num_out: TCP acknowledgment number we expect to answer with + * @payload_len: TCP payload len + * + * returns TCP action we expect to answer with + */ +u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, u32 *tcp_seq_num_out, + u32 *tcp_ack_num_out, int payload_len) { u8 tcp_fin =3D tcp_flags & TCP_FIN; u8 tcp_syn =3D tcp_flags & TCP_SYN; @@ -508,7 +581,6 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_n= um, int payload_len) u8 tcp_push =3D tcp_flags & TCP_PUSH; u8 tcp_ack =3D tcp_flags & TCP_ACK; u8 action =3D TCP_DATA; - int i; =20 /* * tcp_flags are examined to determine TX action in a given state @@ -533,34 +605,29 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq= _num, int payload_len) debug_cond(DEBUG_INT_STATE, "TCP CLOSED %x\n", tcp_flags); if (tcp_syn) { action =3D TCP_SYN | TCP_ACK; - tcp_seq_init =3D tcp_seq_num; - tcp_ack_edge =3D tcp_seq_num + 1; + init_sack_options(tcp_seq_num, tcp_seq_num + 1); current_tcp_state =3D TCP_SYN_RECEIVED; } else if (tcp_ack || tcp_fin) { action =3D TCP_DATA; } break; case TCP_SYN_RECEIVED: + if (tcp_ack) { + action =3D TCP_DATA; + init_sack_options(tcp_seq_num, tcp_seq_num + 1); + current_tcp_state =3D TCP_ESTABLISHED; + } + break; 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 =3D action | TCP_PUSH; current_tcp_state =3D TCP_CLOSE_WAIT; - } else if (tcp_ack || (tcp_syn && tcp_ack)) { - action |=3D TCP_ACK; - tcp_seq_init =3D tcp_seq_num; - tcp_ack_edge =3D tcp_seq_num + 1; - sack_idx =3D 0; - edge_a[sack_idx].se.l =3D tcp_ack_edge; - edge_a[sack_idx].se.r =3D tcp_ack_edge; - prev_len =3D 0; + } else if (tcp_syn && tcp_ack) { + action |=3D TCP_ACK | TCP_PUSH; + init_sack_options(tcp_seq_num, tcp_seq_num + 1); current_tcp_state =3D TCP_ESTABLISHED; - for (i =3D 0; i < TCP_SACK; i++) - edge_a[i].st =3D NOPKT; - - if (tcp_syn && tcp_ack) - action |=3D TCP_PUSH; } else { action =3D TCP_DATA; } @@ -627,6 +694,10 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_= num, int payload_len) } break; } + + *tcp_seq_num_out =3D tcp_seq_num; + *tcp_ack_num_out =3D tcp_ack_edge; + return action; } =20 @@ -641,6 +712,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int = pkt_len) u16 tcp_rx_xsum =3D b->ip.ip_hdr.ip_sum; 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; =20 /* Verify IP header */ @@ -685,7 +757,8 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int = pkt_len) =20 /* Packets are not ordered. Send to app as received. */ tcp_action =3D tcp_state_machine(b->ip.tcp_hdr.tcp_flags, - tcp_seq_num, payload_len); + tcp_seq_num, &res_tcp_seq_num, + &res_tcp_ack_num, payload_len); =20 tcp_activity_count++; if (tcp_activity_count > TCP_ACTIVITY) { @@ -705,7 +778,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int = pkt_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, tcp_ack_num, tcp_ack_edge, payload_len); + tcp_action, res_tcp_seq_num, res_tcp_ack_num, payload_len); =20 /* * Warning: Incoming Ack & Seq sequence numbers are transposed @@ -714,6 +787,6 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int = pkt_len) net_send_tcp_packet(0, ntohs(b->ip.tcp_hdr.tcp_src), ntohs(b->ip.tcp_hdr.tcp_dst), (tcp_action & (~TCP_PUSH)), - tcp_ack_num, tcp_ack_edge); + res_tcp_seq_num, res_tcp_ack_num); } } --=20 2.41.0.487.g6d72f3e995-goog