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 39068C7EE22 for ; Mon, 8 May 2023 17:16:16 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BB9C785F18; Mon, 8 May 2023 19:16:04 +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="cKpTIKWu"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1B2DB85F3D; Mon, 8 May 2023 19:16:01 +0200 (CEST) Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) (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 CC94785AFF for ; Mon, 8 May 2023 19:15:56 +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=3TC5ZZAkKBjcWbfhkbggrZhhZeX.Vhfn-Uhhmeblml.WXgq.WX@flex--dimorinny.bounces.google.com Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-50bffc723c5so4506937a12.1 for ; Mon, 08 May 2023 10:15:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683566156; x=1686158156; 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=B75nH9kczAPt4RGUNv+lZ+fPPipO/iQov4QjL/fstFc=; b=cKpTIKWu6tvsGS2NF7oJmOU3gXZiOuLZsuOp3GCr8DrFp+6gYFSm2UnG3UkhQ5kqe6 ugeXhSvRM0U2YmvEd5Tc0ss0vIcx0FiWdqmA9jBZU9AZhc8XVm96bD0l3ATminN5Fvho U++nJpsCMrQKtsscestPLYlEg1v4uGAVZyOp/FpMFjo2DZiS2moNwfbpQ9uul+eJcZZe QD5WsY3MXqWlbdulUyi3bIapawCFd+eHecYGVb8wkOuXuhSjTCXvNfzVPMftqQ3z/1AQ Z4qDEUcG0N/e6vT3hR+UGI4y43N3anQCaRZnpSCU3kk+B64iuun0WYeckgYjpEgWOMwq w1jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683566156; x=1686158156; 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=B75nH9kczAPt4RGUNv+lZ+fPPipO/iQov4QjL/fstFc=; b=FvrFoY9/c1ce5n2OyTsiFuMrQVGgYLydZE9H/k41OjHnr+0kJpgvPH1KKLONCWnPVK c09rnbFU3RZOz0sSN+KeFYczxFVq+c3AUObEGypj0ToV0LZpcOOCG+1Gqs+pnubFVfR4 hVRFvozLL7j09vqV//dgKIXXFg1/gdGalAg3VhXfoZzRy5gGw/MzkIb0gyS5reRVVPS4 Bbu1+4yZC9cfZIWqtvTHjaqFcDJisVnBSWVbycVEoVgQ0wAROldUe2IU7LvztDNBb9r5 SbvkdtzVvoB6S0TZwLzTa7gyhLgOi+mCrmCY4NQTpGhcKcebSEI31UhMnQIRgj/KgOk0 iqRQ== X-Gm-Message-State: AC+VfDzGuWWcxrIYLHEPuppA6qhYZokw+58OyUHHzvwgce2pFujkzMMG tYV1S6Z3NYjuCgWkS6sb+V99Uu+KSlcq9eUtEcbBr9puZe09greu9FqmVLXFWJaJ8i8C5TpO/8Z TysKDbalF8OXENZ4xX3R3qyHthHVlid/YsELwJ/MfD12qBvRWX3CgmDqrpmLsTW5e0g0= X-Google-Smtp-Source: ACHHUZ7IYgyJ3Omm7lYQAwc9mTykvjidWKdnKARxbZ5eoGl+gVwHZRe8jJC8O43ODLKgP3VfL2Re9dI4gR9isZY= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:f357:b9b:9cec:e681]) (user=dimorinny job=sendgmr) by 2002:a17:906:8471:b0:965:ecf8:4af5 with SMTP id hx17-20020a170906847100b00965ecf84af5mr4094763ejc.12.1683566156208; Mon, 08 May 2023 10:15:56 -0700 (PDT) Date: Mon, 8 May 2023 18:15:21 +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-2-dimorinny@google.com> Subject: [PATCH 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, 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 --- include/net/tcp.h | 109 +++++++++++++-- net/net.c | 18 ++- net/net6.c | 78 ++++++++--- net/tcp.c | 337 ++++++++++++++++++++++++++++------------------ 4 files changed, 372 insertions(+), 170 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 5ab1127d8b..1332f19153 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..0b68bf7b13 100644 --- a/net/net.c +++ b/net/net.c @@ -916,6 +916,7 @@ int net_send_ip_packet(uchar *ether, struct in_addr des= t, int dport, int sport, { 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,19 +935,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: diff --git a/net/net6.c b/net/net6.c index 2dd64c0e16..e395b930b0 100644 --- a/net/net6.c +++ b/net/net6.c @@ -324,15 +324,13 @@ int ip6_add_hdr(uchar *xip, struct in6_addr *src, str= uct in6_addr *dest, return sizeof(struct ip6_hdr); } =20 -int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport, - int sport, int len) +int udp6_add_hdr(uchar *xip, struct in6_addr *dest, int dport, int sport, + int len) { - uchar *pkt; struct udp_hdr *udp; u16 csum_p; =20 - udp =3D (struct udp_hdr *)((uchar *)net_tx_packet + net_eth_hdr_size() + - IP6_HDR_SIZE); + udp =3D (struct udp_hdr *)xip; =20 udp->udp_dst =3D htons(dport); udp->udp_src =3D htons(sport); @@ -344,39 +342,75 @@ int net_send_udp_packet6(uchar *ether, struct in6_add= r *dest, int dport, udp->udp_xsum =3D csum_ipv6_magic(&net_ip6, dest, len + UDP_HDR_SIZE, IPPROTO_UDP, csum_p); =20 + return sizeof(struct udp_hdr); +} + +int net_send_ip_packet6(uchar *ether, struct in6_addr *dest, int dport, in= t sport, + int payload_len, int proto, u8 action, u32 tcp_seq_num, + u32 tcp_ack_num) +{ + uchar *pkt; + int eth_hdr_size; + int ip_hdr_size; + int udp_hdr_size; + int tcp_hdr_size; + int pkt_hdr_size; + + if (!net_tx_packet) + return -1; + + pkt =3D (uchar *)net_tx_packet; + + eth_hdr_size =3D net_set_ether(pkt, ether, PROT_IP6); + pkt_hdr_size +=3D eth_hdr_size; + pkt =3D eth_hdr_size; + + switch (proto) { +#if defined(CONFIG_PROT_UDP) + case IPPROTO_UDP: + ip_hdr_size =3D ip6_add_hdr(pkt, &net_ip6, dest, IPPROTO_UDP, 64, + payload_len + UDP_HDR_SIZE); + pkt_hdr_size +=3D ip_hdr_size; + pkt +=3D ip_hdr_size; + + udp_hdr_size =3D udp6_add_hdr(pkt, dest, dport, sport, payload_len); + pkt_hdr_size +=3D udp_hdr_size; + pkt +=3D udp_hdr_size; + break; +#endif + default: + return -EINVAL; + } + /* if MAC address was not discovered yet, save the packet and do * neighbour discovery */ - if (!memcmp(ether, net_null_ethaddr, 6)) { + if (memcmp(ether, net_null_ethaddr, 6) =3D=3D 0) { + memcpy((uchar *)net_nd_tx_packet, + (uchar *)net_tx_packet, pkt_hdr_size + payload_len); + memset((uchar *)net_tx_packet, 0, pkt_hdr_size + payload_len); + net_copy_ip6(&net_nd_sol_packet_ip6, dest); net_nd_packet_mac =3D ether; - - pkt =3D net_nd_tx_packet; - pkt +=3D net_set_ether(pkt, net_nd_packet_mac, PROT_IP6); - pkt +=3D ip6_add_hdr(pkt, &net_ip6, dest, IPPROTO_UDP, 64, - len + UDP_HDR_SIZE); - memcpy(pkt, (uchar *)udp, len + UDP_HDR_SIZE); - /* size of the waiting packet */ - net_nd_tx_packet_size =3D (pkt - net_nd_tx_packet) + - UDP_HDR_SIZE + len; - - /* and do the neighbor solicitation */ + net_nd_tx_packet_size =3D pkt_hdr_size + payload_len; net_nd_try =3D 1; net_nd_timer_start =3D get_timer(0); ndisc_request(); return 1; /* waiting */ } =20 - pkt =3D (uchar *)net_tx_packet; - pkt +=3D net_set_ether(pkt, ether, PROT_IP6); - pkt +=3D ip6_add_hdr(pkt, &net_ip6, dest, IPPROTO_UDP, 64, - len + UDP_HDR_SIZE); - (void)eth_send(net_tx_packet, pkt - net_tx_packet + UDP_HDR_SIZE + len); + (void)eth_send(net_tx_packet, pkt_hdr_size + payload_len); =20 return 0; /* transmitted */ } =20 +int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport, + int sport, int len) +{ + return net_send_ip_packet6(ether, dest, dport, sport, len, IPPROTO_UDP, 0= , 0, 0); +} + int net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len) { struct in_addr zero_ip =3D {.s_addr =3D 0 }; 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.40.1.521.gf1e218fcd8-goog