net: split IP_TCP header into separate IP/IP6 and TCP headers
Subsystems (2)
PATCH 1/6 net: split IP_TCP header into separate IP/IP6 and TCP headers
This allows us to reuse TCP logic between IP and IP6 stack.
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- include/net/tcp.h | 50 ++++++++++++++------------------- net/tcp.c | 70 +++++++++++++++++++++++------------------------ test/cmd/wget.c | 16 +++++------ 3 files changed, 64 insertions(+), 72 deletions(-)
include/net/tcp.h +21 -29
@@ -5,20 +5,16 @@* Copyright 2017 Duncan Hare, All rights reserved. */ +#ifndef __TCP_H__ +#define __TCP_H__ +#define TCP_ACTIVITY 127/* Number of packets received */ /* before console progress mark */ + +#define GET_TCP_HDR_LEN_IN_BYTES (x) ((x) >> 2) +/** -* struct ip_tcp_hdr - IP and TCP header -* @ip_hl_v: header length and version -* @ip_tos: type of service -* @ip_len: total length -* @ip_id: identification -* @ip_off: fragment offset field -* @ip_ttl: time to live -* @ip_p: protocol -* @ip_sum: checksum -* @ip_src: Source IP address -* @ip_dst: Destination IP address +* struct tcp_hdr - TCP header * @tcp_src: TCP source port * @tcp_dst: TCP destination port * @tcp_seq: TCP sequence number
@@ -28,18 +24,8 @@* @tcp_win: TCP windows size * @tcp_xsum: Checksum * @tcp_ugr: Pointer to urgent data -*/ -struct ip_tcp_hdr { -u8 ip_hl_v; -u8 ip_tos; -u16 ip_len; -u16 ip_id; -u16 ip_off; -u8 ip_ttl; -u8 ip_p; -u16 ip_sum; -struct in_addr ip_src; -struct in_addr ip_dst; +*/ +struct tcp_hdr {u16 tcp_src;u16 tcp_dst;u32 tcp_seq;
@@ -51,8 +37,8 @@ struct ip_tcp_hdr {u16 tcp_ugr; } __packed; -#define IP_TCP_HDR_SIZE (sizeof(struct ip_tcp_hdr)) -#define TCP_HDR_SIZE (IP_TCP_HDR_SIZE - IP_HDR_SIZE) +#define TCP_HDR_SIZE (sizeof(struct tcp_hdr)) +#define IP_TCP_HDR_SIZE (IP_HDR_SIZE + TCP_HDR_SIZE)#define TCP_DATA 0x00/* Data Packet - internal use only */ #define TCP_FIN 0x01/* Finish flag */
@@ -178,7 +164,8 @@ struct tcp_t_opt {/** * struct ip_tcp_hdr_o - IP + TCP header + TCP options -* @hdr: IP + TCP header +* @ip_hdr: IP + TCP header +* @tcp_hdr: TCP header * @mss: TCP MSS Option * @scale: TCP Windows Scale Option * @sack_p: TCP Sack-Permitted Option
@@ -186,7 +173,8 @@ struct tcp_t_opt {* @end: end of options */ struct ip_tcp_hdr_o { -struct ip_tcp_hdr hdr; +struct ip_hdr ip_hdr; +struct tcp_hdr tcp_hdr;struct tcp_mss mss;struct tcp_scale scale;struct tcp_sack_p sack_p;
@@ -198,13 +186,15 @@ struct ip_tcp_hdr_o {/** * struct ip_tcp_hdr_s - IP + TCP header + TCP options -* @hdr: IP + TCP header +* @ip_hdr: IP + TCP header +* @tcp_hdr: TCP header * @t_opt: TCP Timestamp Option * @sack_v: TCP SACK Option * @end: end of options */ struct ip_tcp_hdr_s { -struct ip_tcp_hdr hdr; +struct ip_hdr ip_hdr; +struct tcp_hdr tcp_hdr;struct tcp_t_opt t_opt;struct tcp_sack_v sack_v;u8 end;
@@ -303,3 +293,5 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int len);u16 tcp_set_pseudo_header (uchar *pkt,struct in_addr src,struct in_addr dest,int tcp_len,int pkt_len); + +#endif // __TCP_H__
net/tcp.c +35 -35
176 lines not on this page · raw message
test/cmd/wget.c +8 -8
@@ -52,9 +52,9 @@ static int sb_syn_handler(struct udevice *dev, void *packet, {struct eth_sandbox_priv *priv =dev_get_priv (dev);struct ethernet_hdr *eth = packet; -struct ip_tcp_hdr *tcp = packet +ETHER_HDR_SIZE ; +struct tcp_hdr *tcp = packet +ETHER_HDR_SIZE +IP_HDR_SIZE ;struct ethernet_hdr *eth_send; -struct ip_tcp_hdr *tcp_send; +struct tcp_hdr *tcp_send;/* Don't allow the buffer to overrun */ if (priv->recv_packets >=PKTBUFSRX )
@@ -64,7 +64,7 @@ static int sb_syn_handler(struct udevice *dev, void *packet,memcpy (eth_send->et_dest, eth->et_src,ARP_HLEN );memcpy (eth_send->et_src, priv->fake_host_hwaddr,ARP_HLEN ); eth_send->et_protlen =htons (PROT_IP ); - tcp_send = (void *)eth_send +ETHER_HDR_SIZE ; + tcp_send = (void *)eth_send +ETHER_HDR_SIZE +IP_HDR_SIZE ; tcp_send->tcp_src = tcp->tcp_dst; tcp_send->tcp_dst = tcp->tcp_src; tcp_send->tcp_seq =htonl (0 );
@@ -97,9 +97,9 @@ static int sb_ack_handler(struct udevice *dev, void *packet, {struct eth_sandbox_priv *priv =dev_get_priv (dev);struct ethernet_hdr *eth = packet; -struct ip_tcp_hdr *tcp = packet +ETHER_HDR_SIZE ; +struct tcp_hdr *tcp = packet +ETHER_HDR_SIZE +IP_HDR_SIZE ;struct ethernet_hdr *eth_send; -struct ip_tcp_hdr *tcp_send; +struct tcp_hdr *tcp_send;void *data;int pkt_len;int payload_len =0 ;
@@ -115,7 +115,7 @@ static int sb_ack_handler(struct udevice *dev, void *packet,memcpy (eth_send->et_dest, eth->et_src,ARP_HLEN );memcpy (eth_send->et_src, priv->fake_host_hwaddr,ARP_HLEN ); eth_send->et_protlen =htons (PROT_IP ); - tcp_send = (void *)eth_send +ETHER_HDR_SIZE ; + tcp_send = (void *)eth_send +ETHER_HDR_SIZE +IP_HDR_SIZE ; tcp_send->tcp_src = tcp->tcp_dst; tcp_send->tcp_dst = tcp->tcp_src; data = (void *)tcp_send +IP_TCP_HDR_SIZE ;
@@ -163,14 +163,14 @@ static int sb_http_handler(struct udevice *dev, void *packet, {struct ethernet_hdr *eth = packet;struct ip_hdr *ip; -struct ip_tcp_hdr *tcp; +struct tcp_hdr *tcp;if (ntohs (eth->et_protlen) ==PROT_ARP ) {return sb_arp_handler (dev, packet, len); }else if (ntohs (eth->et_protlen) ==PROT_IP ) { ip = packet +ETHER_HDR_SIZE ;if (ip->ip_p ==IPPROTO_TCP ) { - tcp = packet +ETHER_HDR_SIZE ; + tcp = ip +IP_HDR_SIZE ;if (tcp->tcp_flags ==TCP_SYN )return sb_syn_handler (dev, packet, len);else if (tcp->tcp_flags &TCP_ACK && !(tcp->tcp_flags &TCP_SYN ))
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
PATCH 2/6 net: prepare existing TCP stack to be reused by IP6
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.
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- include/net/tcp.h | 109 +++++++++++++-- net/net.c | 18 ++- net/net6.c | 78 ++++++++--- net/tcp.c | 337 ++++++++++++++++++++++++++++------------------ 4 files changed, 372 insertions(+), 170 deletions(-)
include/net/tcp.h +99 -10
@@ -8,10 +8,20 @@#ifndef __TCP_H__#define __TCP_H__ +#include <net.h> +#define TCP_ACTIVITY 127/* Number of packets received */ /* before console progress mark */ +/* +* 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)/** * 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 {*/ /** -* 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; +#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))/**
@@ -209,7 +231,7 @@ struct ip_tcp_hdr_s {/** * struct pseudo_hdr - Pseudo Header -* @padding: pseudo hdr size = ip_tcp hdr size +* @padding: pseudo hdr size = 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 {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 implementation +* +* @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 *tcp_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); +/** +* 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
net/net.c +12 -6
@@ -916,6 +916,7 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport, {uchar *pkt;int eth_hdr_size; +int ip_tcp_hdr_size;int pkt_hdr_size;/* 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 dest, int dport, int sport, pkt = (uchar *)net_tx_packet; eth_hdr_size =net_set_ether (pkt, ether,PROT_IP ); + pkt_hdr_size = eth_hdr_size; + pkt += eth_hdr_size;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 = eth_hdr_size +IP_UDP_HDR_SIZE ; + pkt_hdr_size +=IP_UDP_HDR_SIZE ;break ;#if defined(CONFIG_PROT_TCP )case IPPROTO_TCP : - pkt_hdr_size = 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 =IP_HDR_SIZE ; + ip_tcp_hdr_size +=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 += ip_tcp_hdr_size;break ;#endif default :
net/net6.c +56 -22
110 lines not on this page · raw message
net/tcp.c +205 -132
495 lines not on this page · raw message
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
PATCH 3/6 net: introduce TCP/IP6 support
In reply to Dmitrii MerkurevAdd TCP/IP6 related headers and reuse refactored TCP/IP implementation
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- 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
include/net/tcp6.h new file +103 -0
@@ -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 <net6.h> +#include <net/tcp.h> + +/** +* 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__
include/net6.h +14 -0
@@ -344,6 +344,20 @@ int ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,int net_send_udp_packet6 (uchar *ether,struct in6_addr *dest,int dport,int sport,int len); +/** +* 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 *
net/Makefile +1 -0
@@ -32,6 +32,7 @@ obj-$(CONFIG_TCP_FUNCTION_FASTBOOT) += fastboot_tcp.o obj-$(CONFIG_CMD_WOL) += wol.o obj-$(CONFIG_PROT_UDP) += udp.o obj-$(CONFIG_PROT_TCP) += tcp.o +obj-$(CONFIG_IPV6) += tcp6.o obj-$(CONFIG_CMD_WGET) += wget.o # Disable this warning as it is triggered by:
net/net.c +6 -0
@@ -92,6 +92,7 @@#include <log.h> #include <net.h> #include <net6.h> +#include <net/tcp6.h> #include <ndisc.h> #include <net/fastboot_udp.h> #include <net/fastboot_tcp.h>
@@ -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 }static void net_cleanup_loop (void )
@@ -916,7 +920,9 @@ int net_send_ip_packet(uchar *ether, struct in_addr dest, 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;/* make sure the net_tx_packet is initialized (net_init() was called) */
net/net6.c +41 -2
78 lines not on this page · raw message
net/tcp6.c new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
PATCH 4/6 net: add fastboot TCP6 support
In reply to Dmitrii Merkurevfastboot tcp command remains the same, but started listening IP6 in case it's enabled.
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- include/net/fastboot_tcp.h | 2 +- net/fastboot_tcp.c | 72 ++++++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 12 deletions(-)
include/net/fastboot_tcp.h +1 -1
@@ -7,7 +7,7 @@#define __NET_FASTBOOT_TCP_H__/** -* Wait for incoming tcp fastboot comands. +* Wait for incoming TCP fastboot comands. */ void fastboot_tcp_start_server (void );
net/fastboot_tcp.c +61 -11
146 lines not on this page · raw message
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
PATCH 5/6 net: add fastboot TCP documentation and IP6-only mode
In reply to Dmitrii MerkurevCommand to start IP6 only TCP fastboot:
fastboot tcp -ipv6
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- cmd/fastboot.c | 29 +++++++++++++++++++++++++---- doc/android/fastboot.rst | 8 +++++++- 2 files changed, 32 insertions(+), 5 deletions(-)
cmd/fastboot.c +25 -4
@@ -12,6 +12,7 @@#include <g_dnl.h> #include <fastboot.h> #include <net.h> +#include <net6.h> #include <usb.h> #include <watchdog.h> #include <linux/stringify.h>
@@ -121,10 +122,23 @@ static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc, {uintptr_t buf_addr = (uintptr_t )NULL ;size_t buf_size =0 ; +bool is_ipv6_only = false; +bool is_usb = false; +bool is_udp = false; +bool is_tcp = false;if (argc <2 )return CMD_RET_USAGE ; +if (IS_ENABLED (CONFIG_IPV6 )) { + use_ip6 = false; +/* IPv6 parameter has to be always *last* */ +if (!strcmp (argv[argc -1 ],USE_IP6_CMD_PARAM )) { + is_ipv6_only = true; + --argc; + } + } +while (argc >1 && **(argv +1 ) =='-' ) {char *arg = *++argv;
@@ -159,11 +173,18 @@ NXTARG:fastboot_init ((void *)buf_addr, buf_size); -if (!strcmp (argv[1 ],"udp" )) + is_usb =strcmp (argv[1 ],"usb" ) ==0 ; + is_udp =strcmp (argv[1 ],"udp" ) ==0 ; + is_tcp =strcmp (argv[1 ],"tcp" ) ==0 ; + +if (is_ipv6_only && is_tcp) + use_ip6 = true; + +if (is_udp)return do_fastboot_udp (argc, argv, buf_addr, buf_size); -if (!strcmp (argv[1 ],"tcp" )) +if (is_tcp)return do_fastboot_tcp (argc, argv, buf_addr, buf_size); -if (!strcmp (argv[1 ],"usb" )) { +if (is_usb) { argv++; argc--; }
@@ -174,7 +195,7 @@ NXTARG:U_BOOT_CMD ( fastboot,CONFIG_SYS_MAXARGS ,1 , do_fastboot,"run as a fastboot usb or udp device" , - "[-l addr] [-s size] usb <controller> | udp\n" + "[-l addr] [-s size] usb <controller> | udp [-ipv6] | tcp [-ipv6]\n" "\taddr - address of buffer used during data transfers (" __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n" "\tsize - size of buffer used during data transfers ("
doc/android/fastboot.rst +7 -1
@@ -181,13 +181,19 @@ Enter into fastboot by executing the fastboot command in U-Boot for either USB:: => fastboot usb 0 -or UDP:: +UDP:: => fastboot udp link up on port 0, speed 100, full duplex Using ethernet@4a100000 device Listening for fastboot command on 192.168.0.102 +or TCP:: + + => fastboot tcp + Using ethernet@4a100000 device + Listening for fastboot command on 192.168.0.102 + On the client side you can fetch the bootloader version for instance:: $ fastboot getvar version-bootloader
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
Re: [PATCH 5/6] net: add fastboot TCP documentation and IP6-only mode
Quoted 112 lines · Dmitrii Merkurev
Command to start IP6 only TCP fastboot:
fastboot tcp -ipv6Signed-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- cmd/fastboot.c | 29 +++++++++++++++++++++++++---- doc/android/fastboot.rst | 8 +++++++- 2 files changed, 32 insertions(+), 5 deletions(-)cmd/fastboot.c +25
-4@@ -12,6 +12,7 @@#include <g_dnl.h> #include <fastboot.h> #include <net.h> +#include <net6.h> #include <usb.h> #include <watchdog.h> #include <linux/stringify.h> @@ -121,10 +122,23 @@ static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc, {uintptr_t buf_addr = (uintptr_t )NULL ;size_t buf_size =0 ; +bool is_ipv6_only = false; +bool is_usb = false; +bool is_udp = false; +bool is_tcp = false;if (argc <2 )return CMD_RET_USAGE ; +if (IS_ENABLED (CONFIG_IPV6 )) { + use_ip6 = false; +/* IPv6 parameter has to be always *last* */ +if (!strcmp (argv[argc -1 ],USE_IP6_CMD_PARAM )) { + is_ipv6_only = true; + --argc; + } + } +while (argc >1 && **(argv +1 ) =='-' ) {char *arg = *++argv;@@ -159,11 +173,18 @@ NXTARG:fastboot_init ((void *)buf_addr, buf_size); -if (!strcmp (argv[1 ],"udp" )) + is_usb =strcmp (argv[1 ],"usb" ) ==0 ; + is_udp =strcmp (argv[1 ],"udp" ) ==0 ; + is_tcp =strcmp (argv[1 ],"tcp" ) ==0 ; + +if (is_ipv6_only && is_tcp) + use_ip6 = true; + +if (is_udp)return do_fastboot_udp (argc, argv, buf_addr, buf_size); -if (!strcmp (argv[1 ],"tcp" )) +if (is_tcp)return do_fastboot_tcp (argc, argv, buf_addr, buf_size); -if (!strcmp (argv[1 ],"usb" )) { +if (is_usb) { argv++; argc--; }@@ -174,7 +195,7 @@ NXTARG: U_BOOT_CMD( fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot, "run as a fastboot usb or udp device", - "[-l addr] [-s size] usb <controller> | udp\n" + "[-l addr] [-s size] usb <controller> | udp [-ipv6] | tcp[-ipv6]\n"
"\taddr - address of buffer used during data transfers ("__stringify(CONFIG_FASTBOOT_BUF_ADDR)")\n" "\tsize - size of buffer used during data transfers ("doc/android/fastboot.rst +0
-0@@ -181,13 +181,19 @@ Enter into fastboot by executing the fastbootcommand in U-Boot for either USB::
=> fastboot usb 0
doc/android/fastboot.rst +7
-2-or UDP:: +UDP:: => fastboot udp link up on port 0, speed 100, full duplex Using ethernet@4a100000 device Listening for fastboot command on 192.168.0.102 +or TCP:: + + => fastboot tcp + Using ethernet@4a100000 device + Listening for fastboot command on 192.168.0.102 + On the client side you can fetch the bootloader version for instance:: $ fastboot getvar version-bootloader --2.40.1.521.gf1e218fcd8-goog
PATCH 6/6 net: update net_ip6 from link_local address when eth device is changed
In reply to Dmitrii MerkurevCurrent active eth device may be changed (due to ethprime), so make sure current net_ip6 is updated as a reaction.
Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
--- net/net.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/net.c +4 -4
@@ -359,10 +359,10 @@ static int net_init_loop(void)if (IS_ENABLED (CONFIG_IPV6 )) {ip6_make_lladdr (&net_link_local_ip6, net_ethaddr); -if (!memcmp (&net_ip6, &net_null_addr_ip6, -sizeof (struct in6_addr ))) -memcpy (&net_ip6, &net_link_local_ip6, -sizeof (struct in6_addr )); +if (memcmp (&net_link_local_ip6, &net_null_addr_ip6, +sizeof (struct in6_addr ))) { +net_copy_ip6 (&net_ip6, &net_link_local_ip6); + } } }else
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
Hi Dmitrii,
It seems to me that this series of patches breaks the unittest of TCP stack in sandbox.
I'll see if I have some time to fix the unittest too.
The unittest can be built by the following commands.
1. "make sandbox64_defconfig"
2. turn on the options that need to be tested. For example, CMD_WGET
3. "make"
4. "sudo ./u-boot -D"
5. "ut lib net_test_wget" in the U-boot prompt.
Yours,
Paul
Quoted 431 lines · Dmitrii Merkurev
This allows us to reuse TCP logic between IP and IP6 stack.
Signed-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- include/net/tcp.h | 50 ++++++++++++++------------------- net/tcp.c | 70 +++++++++++++++++++++++------------------------ test/cmd/wget.c | 16 +++++------ 3 files changed, 64 insertions(+), 72 deletions(-)include/net/tcp.h +11
-25@@ -5,20 +5,16 @@* Copyright 2017 Duncan Hare, All rights reserved. */ +#ifndef __TCP_H__ +#define __TCP_H__ +#define TCP_ACTIVITY 127/* Number of packets received */ /* before console progress mark */ + +#define GET_TCP_HDR_LEN_IN_BYTES (x) ((x) >> 2) +/** -* struct ip_tcp_hdr - IP and TCP header -* @ip_hl_v: header length and version -* @ip_tos: type of service -* @ip_len: total length -* @ip_id: identification -* @ip_off: fragment offset field -* @ip_ttl: time to live -* @ip_p: protocol -* @ip_sum: checksum -* @ip_src: Source IP address -* @ip_dst: Destination IP address +* struct tcp_hdr - TCP header * @tcp_src: TCP source port * @tcp_dst: TCP destination port * @tcp_seq: TCP sequence number @@ -28,18 +24,8 @@* @tcp_win: TCP windows size * @tcp_xsum: Checksum * @tcp_ugr: Pointer to urgent data -*/ -struct ip_tcp_hdr { -u8 ip_hl_v; -u8 ip_tos; -u16 ip_len; -u16 ip_id; -u16 ip_off; -u8 ip_ttl; -u8 ip_p; -u16 ip_sum; -struct in_addr ip_src; -struct in_addr ip_dst; +*/ +struct tcp_hdr {u16 tcp_src;u16 tcp_dst;u32 tcp_seq;@@ -51,8 +37,8 @@ struct ip_tcp_hdr {u16 tcp_ugr; } __packed; -#define IP_TCP_HDR_SIZE (sizeof(struct ip_tcp_hdr)) -#define TCP_HDR_SIZE (IP_TCP_HDR_SIZE - IP_HDR_SIZE) +#define TCP_HDR_SIZE (sizeof(struct tcp_hdr)) +#define IP_TCP_HDR_SIZE (IP_HDR_SIZE + TCP_HDR_SIZE)#define TCP_DATA 0x00/* Data Packet - internal use only */ #define TCP_FIN 0x01/* Finish flag */include/net/tcp.h +8
-4@@ -178,7 +164,8 @@ struct tcp_t_opt {/** * struct ip_tcp_hdr_o - IP + TCP header + TCP options -* @hdr: IP + TCP header +* @ip_hdr: IP + TCP header +* @tcp_hdr: TCP header * @mss: TCP MSS Option * @scale: TCP Windows Scale Option * @sack_p: TCP Sack-Permitted Option @@ -186,7 +173,8 @@ struct tcp_t_opt {* @end: end of options */ struct ip_tcp_hdr_o { -struct ip_tcp_hdr hdr; +struct ip_hdr ip_hdr; +struct tcp_hdr tcp_hdr;struct tcp_mss mss;struct tcp_scale scale;struct tcp_sack_p sack_p;@@ -198,13 +186,15 @@ struct ip_tcp_hdr_o {/** * struct ip_tcp_hdr_s - IP + TCP header + TCP options -* @hdr: IP + TCP header +* @ip_hdr: IP + TCP header +* @tcp_hdr: TCP header * @t_opt: TCP Timestamp Option * @sack_v: TCP SACK Option * @end: end of options */ struct ip_tcp_hdr_s { -struct ip_tcp_hdr hdr; +struct ip_hdr ip_hdr; +struct tcp_hdr tcp_hdr;struct tcp_t_opt t_opt;struct tcp_sack_v sack_v;u8 end;@@ -303,3 +293,5 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int len); u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr src, struct in_addrdest,
int tcp_len, int pkt_len);include/net/tcp.h +2
-0+ +#endif // __TCP_H__ net/tcp.c +1
-1@@ -155,7 +155,7 @@ u16 tcp_set_pseudo_header(uchar *pkt, struct in_addrsrc, struct in_addr dest, */ int net_set_ack_options (union tcp_build_pkt *b) { - b->sack.hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE )); + b->sack.tcp_hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE ));b->sack.t_opt.kind = TCP_O_TS; b->sack.t_opt.len = TCP_OPT_LEN_A;net/tcp.c +0
-1@@ -187,12 +187,12 @@ int net_set_ack_options(union tcp_build_pkt *b) b->sack.sack_v.hill[3 ].r =TCP_O_NOP ; } - b->sack.hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE +
+b->sack.tcp_hdr.tcp_hlen=
SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE +TCP_TSOPT_SIZE + tcp_lost.len)); } else { b->sack.sack_v.kind = 0;-
b->sack.hdr.tcp_hlen=
SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE +
+b->sack.tcp_hdr.tcp_hlen=
SHIFT_TO_TCPHDRLEN_FIELD(ROUND_TCPHDR_LEN(TCP_HDR_SIZE +TCP_TSOPT_SIZE)); }net/tcp.c +4
-4@@ -201,7 +201,7 @@ int net_set_ack_options(union tcp_build_pkt *b)* TCP header to add to the total packet length */ -return GET_TCP_HDR_LEN_IN_BYTES (b->sack.hdr.tcp_hlen); +return GET_TCP_HDR_LEN_IN_BYTES (b->sack.tcp_hdr.tcp_hlen); }/** @@ -213,7 +213,7 @@ void net_set_syn_options(union tcp_build_pkt *b)if (IS_ENABLED (CONFIG_PROT_TCP_SACK )) tcp_lost.len =0 ; - b->ip.hdr.tcp_hlen =0xa0 ; + b->ip.tcp_hdr.tcp_hlen =0xa0 ; b->ip.mss.kind =TCP_O_MSS ; b->ip.mss.len =TCP_OPT_LEN_4 ;@@ -249,9 +249,9 @@ int tcp_set_tcp_header(uchar *pkt, int dport, intsport, int payload_len, * Header: 5 32 bit words. 4 bits TCP header Length, * 4 bits reserved options */ - b->ip.hdr.tcp_flags = action; + b->ip.tcp_hdr.tcp_flags = action; pkt_hdr_len =IP_TCP_HDR_SIZE ; - b->ip.hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE )); + b->ip.tcp_hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE ));switch (action) { case TCP_SYN:net/tcp.c +1
-1@@ -274,7 +274,7 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, case TCP_SYN | TCP_ACK:case TCP_ACK : pkt_hdr_len =IP_HDR_SIZE +net_set_ack_options (b); - b->ip.hdr.tcp_flags = action; + b->ip.tcp_hdr.tcp_flags = action; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:ACK (%pI4, %pI4, s=%u, a=%u, A=%x)\n",&net_server_ip, &net_ip, tcp_seq_num,
tcp_ack_num,net/tcp.c +1
-1@@ -308,7 +308,7 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, fallthrough;default : pkt_hdr_len =IP_HDR_SIZE +net_set_ack_options (b); - b->ip.hdr.tcp_flags = action |TCP_PUSH |TCP_ACK ; + b->ip.tcp_hdr.tcp_flags = action |TCP_PUSH |TCP_ACK ; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:dft (%pI4, %pI4, s=%u, a=%u, A=%x)\n",&net_server_ip, &net_ip,net/tcp.c +4
-4@@ -320,10 +320,10 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, tcp_ack_edge = tcp_ack_num;/* TCP Header */ - b->ip.hdr.tcp_ack =htonl (tcp_ack_edge); - b->ip.hdr.tcp_src =htons (sport); - b->ip.hdr.tcp_dst =htons (dport); - b->ip.hdr.tcp_seq =htonl (tcp_seq_num); + b->ip.tcp_hdr.tcp_ack =htonl (tcp_ack_edge); + b->ip.tcp_hdr.tcp_src =htons (sport); + b->ip.tcp_hdr.tcp_dst =htons (dport); + b->ip.tcp_hdr.tcp_seq =htonl (tcp_seq_num);/* * TCP window size - TCP header variable
tcp_win.net/tcp.c +5
-5@@ -340,13 +340,13 @@ int tcp_set_tcp_header(uchar *pkt, int dport, intsport, int payload_len, * it is, then the u-boot tftp or nfs kernel netboot should be * considered. */ - b->ip.hdr.tcp_win =htons (PKTBUFSRX *TCP_MSS >>TCP_SCALE ); + b->ip.tcp_hdr.tcp_win =htons (PKTBUFSRX *TCP_MSS >>TCP_SCALE ); - b->ip.hdr.tcp_xsum =0 ; - b->ip.hdr.tcp_ugr =0 ; + b->ip.tcp_hdr.tcp_xsum =0 ; + b->ip.tcp_hdr.tcp_ugr =0 ; - b->ip.hdr.tcp_xsum =tcp_set_pseudo_header (pkt, net_ip, net_server_ip, - tcp_len, pkt_len); + b->ip.tcp_hdr.tcp_xsum =tcp_set_pseudo_header (pkt, net_ip, net_server_ip, + tcp_len, pkt_len);net_set_ip_header((uchar *)&b->ip, net_server_ip, net_ip, pkt_len, IPPROTO_TCP);net/tcp.c +1
-1@@ -638,7 +638,7 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len)void rxhand_tcp_f (union tcp_build_pkt *b,unsigned int pkt_len) {int tcp_len = pkt_len -IP_HDR_SIZE ; -u16 tcp_rx_xsum = b->ip.hdr.ip_sum; +u16 tcp_rx_xsum = b->ip.ip_hdr.ip_sum;u8 tcp_action =TCP_DATA ;u32 tcp_seq_num, tcp_ack_num;int tcp_hdr_len, payload_len;net/tcp.c +4
-4@@ -646,11 +646,11 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) /* Verify IP header */ debug_cond(DEBUG_DEV_PKT,"TCP RX in RX Sum (to=%pI4, from=%pI4, len=%d)\n" , - &b->ip.hdr.ip_src, &b->ip.hdr.ip_dst, pkt_len); + &b->ip.ip_hdr.ip_src, &b->ip.ip_hdr.ip_dst, pkt_len); - b->ip.hdr.ip_src = net_server_ip; - b->ip.hdr.ip_dst = net_ip; - b->ip.hdr.ip_sum =0 ; + b->ip.ip_hdr.ip_src = net_server_ip; + b->ip.ip_hdr.ip_dst = net_ip; + b->ip.ip_hdr.ip_sum =0 ;if (tcp_rx_xsum !=compute_ip_checksum (b,IP_HDR_SIZE )) { debug_cond(DEBUG_DEV_PKT,"TCP RX IP xSum Error (%pI4, =%pI4, len=%d)\n",net/tcp.c +4
-4@@ -659,10 +659,10 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) }/* Build pseudo header and verify TCP header */ - tcp_rx_xsum = b->ip.hdr.tcp_xsum; - b->ip.hdr.tcp_xsum =0 ; - if (tcp_rx_xsum != tcp_set_pseudo_header((uchar *)b, b->ip.hdr.ip_src, - b->ip.hdr.ip_dst, tcp_len, + tcp_rx_xsum = b->ip.tcp_hdr.tcp_xsum; + b->ip.tcp_hdr.tcp_xsum =0 ; + if (tcp_rx_xsum != tcp_set_pseudo_header((uchar *)b, b->ip.ip_hdr.ip_src, + b->ip.ip_hdr.ip_dst,
tcp_len,pkt_len)) { debug_cond(DEBUG_DEV_PKT, "TCP RX TCP xSum Error (%pI4, %pI4, len=%d)\n",net/tcp.c +1
-1@@ -670,7 +670,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)return ; } - tcp_hdr_len =GET_TCP_HDR_LEN_IN_BYTES (b->ip.hdr.tcp_hlen); + tcp_hdr_len =GET_TCP_HDR_LEN_IN_BYTES (b->ip.tcp_hdr.tcp_hlen); payload_len = tcp_len - tcp_hdr_len;if (
tcp_hdr_len>TCP_HDR_SIZE)net/tcp.c +0
-0@@ -680,11 +680,11 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) * Incoming sequence and ack numbers are server's view of thenumbers.
* The app must swap the numbers when responding. */net/tcp.c +3
-5- tcp_seq_num =ntohl (b->ip.hdr.tcp_seq); - tcp_ack_num =ntohl (b->ip.hdr.tcp_ack); + tcp_seq_num =ntohl (b->ip.tcp_hdr.tcp_seq); + tcp_ack_num =ntohl (b->ip.tcp_hdr.tcp_ack);/* Packets are not ordered. Send to app as received. */ - tcp_action =tcp_state_machine (b->ip.hdr.tcp_flags, + tcp_action =tcp_state_machine (b->ip.tcp_hdr.tcp_flags, tcp_seq_num, payload_len); tcp_activity_count++;@@ -698,8 +698,8 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)"TCP Notify (action=%x, Seq=%u,Ack=%u,Pay%d)\n" , tcp_action, tcp_seq_num, tcp_ack_num, payload_len); - (*tcp_packet_handler) ((uchar *)b + pkt_len - payload_len, b->ip.hdr.tcp_dst, - b->ip.hdr.ip_src,
b->ip.hdr.tcp_src,tcp_seq_num,
+ (*tcp_packet_handler) ((uchar *)b +pkt_len-payload_len,b->ip.tcp_hdr.tcp_dst,
+b->ip.ip_hdr.ip_src,b->ip.tcp_hdr.tcp_src,tcp_seq_num,
tcp_ack_num,tcp_action,payload_len);} else if (tcp_action != TCP_DATA) {net/tcp.c +0
-0@@ -711,8 +711,8 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) * Warning: Incoming Ack & Seq sequence numbers aretransposed
* here to outgoing Seq & Ack sequence numbers */net/tcp.c +2
-2-net_send_tcp_packet (0 ,ntohs (b->ip.hdr.tcp_src), -ntohs (b->ip.hdr.tcp_dst), +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); }test/cmd/wget.c +2
-2@@ -52,9 +52,9 @@ static int sb_syn_handler(struct udevice *dev, void *packet, {struct eth_sandbox_priv *priv =dev_get_priv (dev);struct ethernet_hdr *eth = packet; -struct ip_tcp_hdr *tcp = packet +ETHER_HDR_SIZE ; +struct tcp_hdr *tcp = packet +ETHER_HDR_SIZE +IP_HDR_SIZE ;struct ethernet_hdr *eth_send; -struct ip_tcp_hdr *tcp_send; +struct tcp_hdr *tcp_send;/* Don't allow the buffer to overrun */ if (priv->recv_packets >= PKTBUFSRX)test/cmd/wget.c +1
-1@@ -64,7 +64,7 @@ static int sb_syn_handler(struct udevice *dev, void *packet,memcpy (eth_send->et_dest, eth->et_src,ARP_HLEN );memcpy (eth_send->et_src, priv->fake_host_hwaddr,ARP_HLEN ); eth_send->et_protlen =htons (PROT_IP ); - tcp_send = (void *)eth_send +ETHER_HDR_SIZE ; + tcp_send = (void *)eth_send +ETHER_HDR_SIZE +IP_HDR_SIZE ; tcp_send->tcp_src = tcp->tcp_dst; tcp_send->tcp_dst = tcp->tcp_src;tcp_send->tcp_seq = htonl(0);test/cmd/wget.c +2
-2@@ -97,9 +97,9 @@ static int sb_ack_handler(struct udevice *dev, void *packet, {struct eth_sandbox_priv *priv =dev_get_priv (dev);struct ethernet_hdr *eth = packet; -struct ip_tcp_hdr *tcp = packet +ETHER_HDR_SIZE ; +struct tcp_hdr *tcp = packet +ETHER_HDR_SIZE +IP_HDR_SIZE ;struct ethernet_hdr *eth_send; -struct ip_tcp_hdr *tcp_send; +struct tcp_hdr *tcp_send;void *data;int pkt_len;int payload_len = 0;test/cmd/wget.c +1
-1@@ -115,7 +115,7 @@ static int sb_ack_handler(struct udevice *dev, void *packet,memcpy (eth_send->et_dest, eth->et_src,ARP_HLEN );memcpy (eth_send->et_src, priv->fake_host_hwaddr,ARP_HLEN ); eth_send->et_protlen =htons (PROT_IP ); - tcp_send = (void *)eth_send +ETHER_HDR_SIZE ; + tcp_send = (void *)eth_send +ETHER_HDR_SIZE +IP_HDR_SIZE ; tcp_send->tcp_src = tcp->tcp_dst; tcp_send->tcp_dst = tcp->tcp_src;data = (void *)tcp_send + IP_TCP_HDR_SIZE;test/cmd/wget.c +2
-2@@ -163,14 +163,14 @@ static int sb_http_handler(struct udevice *dev, void *packet, {struct ethernet_hdr *eth = packet;struct ip_hdr *ip; -struct ip_tcp_hdr *tcp; +struct tcp_hdr *tcp;if (ntohs (eth->et_protlen) ==PROT_ARP ) {return sb_arp_handler (dev, packet, len); }else if (ntohs (eth->et_protlen) ==PROT_IP ) { ip = packet +ETHER_HDR_SIZE ;if (ip->ip_p ==IPPROTO_TCP ) { - tcp = packet +ETHER_HDR_SIZE ; + tcp = ip +IP_HDR_SIZE ;if (tcp->tcp_flags ==TCP_SYN )return sb_syn_handler (dev, packet, len);else if (tcp->tcp_flags & TCP_ACK &&!(tcp->tcp_flags &
TCP_SYN))
--
2.40.1.521.gf1e218fcd8-goog
Hey Paul,
Huge thanks for pointing this out. Uploaded v2 with a couple of fixes (including wget test on 1/6).
Quoted 403 lines · Paul Liu
Hi Dmitrii,
It seems to me that this series of patches breaks the unittest of TCP stack in sandbox.
I'll see if I have some time to fix the unittest too.
The unittest can be built by the following commands.
1. "makesandbox64_defconfig"
2. turn on the options that need to be tested. For example,CMD_WGET
3. "make"
4. "sudo ./u-boot -D"
5. "ut libnet_test_wget" in the U-boot prompt.Yours,
PaulQuoted 384 lines · Dmitrii Merkurev
This allows us to reuse TCP logic between IP and IP6 stack.
Signed-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
--- include/net/tcp.h | 50 ++++++++++++++------------------- net/tcp.c | 70 +++++++++++++++++++++++------------------------ test/cmd/wget.c | 16 +++++------ 3 files changed, 64 insertions(+), 72 deletions(-)include/net/tcp.h +21
-29@@ -5,20 +5,16 @@* Copyright 2017 Duncan Hare, All rights reserved. */ +#ifndef __TCP_H__ +#define __TCP_H__ +#define TCP_ACTIVITY 127/* Number of packets received */ /* before console progress mark */ + +#define GET_TCP_HDR_LEN_IN_BYTES (x) ((x) >> 2) +/** -* struct ip_tcp_hdr - IP and TCP header -* @ip_hl_v: header length and version -* @ip_tos: type of service -* @ip_len: total length -* @ip_id: identification -* @ip_off: fragment offset field -* @ip_ttl: time to live -* @ip_p: protocol -* @ip_sum: checksum -* @ip_src: Source IP address -* @ip_dst: Destination IP address +* struct tcp_hdr - TCP header * @tcp_src: TCP source port * @tcp_dst: TCP destination port * @tcp_seq: TCP sequence number @@ -28,18 +24,8 @@* @tcp_win: TCP windows size * @tcp_xsum: Checksum * @tcp_ugr: Pointer to urgent data -*/ -struct ip_tcp_hdr { -u8 ip_hl_v; -u8 ip_tos; -u16 ip_len; -u16 ip_id; -u16 ip_off; -u8 ip_ttl; -u8 ip_p; -u16 ip_sum; -struct in_addr ip_src; -struct in_addr ip_dst; +*/ +struct tcp_hdr {u16 tcp_src;u16 tcp_dst;u32 tcp_seq;@@ -51,8 +37,8 @@ struct ip_tcp_hdr {u16 tcp_ugr; } __packed; -#define IP_TCP_HDR_SIZE (sizeof(struct ip_tcp_hdr)) -#define TCP_HDR_SIZE (IP_TCP_HDR_SIZE - IP_HDR_SIZE) +#define TCP_HDR_SIZE (sizeof(struct tcp_hdr)) +#define IP_TCP_HDR_SIZE (IP_HDR_SIZE + TCP_HDR_SIZE)#define TCP_DATA 0x00/* Data Packet - internal use only */ #define TCP_FIN 0x01/* Finish flag */ @@ -178,7 +164,8 @@ struct tcp_t_opt {/** * struct ip_tcp_hdr_o - IP + TCP header + TCP options -* @hdr: IP + TCP header +* @ip_hdr: IP + TCP header +* @tcp_hdr: TCP header * @mss: TCP MSS Option * @scale: TCP Windows Scale Option * @sack_p: TCP Sack-Permitted Option @@ -186,7 +173,8 @@ struct tcp_t_opt {* @end: end of options */ struct ip_tcp_hdr_o { -struct ip_tcp_hdr hdr; +struct ip_hdr ip_hdr; +struct tcp_hdr tcp_hdr;struct tcp_mss mss;struct tcp_scale scale;struct tcp_sack_p sack_p;@@ -198,13 +186,15 @@ struct ip_tcp_hdr_o {/** * struct ip_tcp_hdr_s - IP + TCP header + TCP options -* @hdr: IP + TCP header +* @ip_hdr: IP + TCP header +* @tcp_hdr: TCP header * @t_opt: TCP Timestamp Option * @sack_v: TCP SACK Option * @end: end of options */ struct ip_tcp_hdr_s { -struct ip_tcp_hdr hdr; +struct ip_hdr ip_hdr; +struct tcp_hdr tcp_hdr;struct tcp_t_opt t_opt;struct tcp_sack_v sack_v;u8 end;@@ -303,3 +293,5 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int len);u16 tcp_set_pseudo_header (uchar *pkt,struct in_addr src,struct in_addr dest,int tcp_len,int pkt_len); + +#endif // __TCP_H__ net/tcp.c +35
-35@@ -155,7 +155,7 @@ u16 tcp_set_pseudo_header(uchar *pkt, struct in_addr src, struct in_addr dest,*/ int net_set_ack_options (union tcp_build_pkt *b) { - b->sack.hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE )); + b->sack.tcp_hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE )); b->sack.t_opt.kind =TCP_O_TS ; b->sack.t_opt.len =TCP_OPT_LEN_A ;@@ -187,12 +187,12 @@ int net_set_ack_options(union tcp_build_pkt *b) b->sack.sack_v.hill[3 ].r =TCP_O_NOP ; } - b->sack.hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (ROUND_TCPHDR_LEN (TCP_HDR_SIZE + + b->sack.tcp_hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (ROUND_TCPHDR_LEN (TCP_HDR_SIZE +TCP_TSOPT_SIZE + tcp_lost.len)); }else { b->sack.sack_v.kind =0 ; - b->sack.hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (ROUND_TCPHDR_LEN (TCP_HDR_SIZE + + b->sack.tcp_hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (ROUND_TCPHDR_LEN (TCP_HDR_SIZE +TCP_TSOPT_SIZE )); }@@ -201,7 +201,7 @@ int net_set_ack_options(union tcp_build_pkt *b)* TCP header to add to the total packet length */ -return GET_TCP_HDR_LEN_IN_BYTES (b->sack.hdr.tcp_hlen); +return GET_TCP_HDR_LEN_IN_BYTES (b->sack.tcp_hdr.tcp_hlen); }/** @@ -213,7 +213,7 @@ void net_set_syn_options(union tcp_build_pkt *b)if (IS_ENABLED (CONFIG_PROT_TCP_SACK )) tcp_lost.len =0 ; - b->ip.hdr.tcp_hlen =0xa0 ; + b->ip.tcp_hdr.tcp_hlen =0xa0 ; b->ip.mss.kind =TCP_O_MSS ; b->ip.mss.len =TCP_OPT_LEN_4 ;@@ -249,9 +249,9 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len,* Header: 5 32 bit words. 4 bits TCP header Length, * 4 bits reserved options */ - b->ip.hdr.tcp_flags = action; + b->ip.tcp_hdr.tcp_flags = action; pkt_hdr_len =IP_TCP_HDR_SIZE ; - b->ip.hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE )); + b->ip.tcp_hdr.tcp_hlen =SHIFT_TO_TCPHDRLEN_FIELD (LEN_B_TO_DW (TCP_HDR_SIZE ));switch (action) {case TCP_SYN :@@ -274,7 +274,7 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len,case TCP_SYN |TCP_ACK :case TCP_ACK : pkt_hdr_len =IP_HDR_SIZE +net_set_ack_options (b); - b->ip.hdr.tcp_flags = action; + b->ip.tcp_hdr.tcp_flags = action; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:ACK (%pI4, %pI4, s=%u, a=%u, A=%x)\n", &net_server_ip, &net_ip, tcp_seq_num, tcp_ack_num,@@ -308,7 +308,7 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len,fallthrough ;default : pkt_hdr_len =IP_HDR_SIZE +net_set_ack_options (b); - b->ip.hdr.tcp_flags = action |TCP_PUSH |TCP_ACK ; + b->ip.tcp_hdr.tcp_flags = action |TCP_PUSH |TCP_ACK ; debug_cond(DEBUG_DEV_PKT, "TCP Hdr:dft (%pI4, %pI4, s=%u, a=%u, A=%x)\n", &net_server_ip, &net_ip,@@ -320,10 +320,10 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len, tcp_ack_edge = tcp_ack_num;/* TCP Header */ - b->ip.hdr.tcp_ack =htonl (tcp_ack_edge); - b->ip.hdr.tcp_src =htons (sport); - b->ip.hdr.tcp_dst =htons (dport); - b->ip.hdr.tcp_seq =htonl (tcp_seq_num); + b->ip.tcp_hdr.tcp_ack =htonl (tcp_ack_edge); + b->ip.tcp_hdr.tcp_src =htons (sport); + b->ip.tcp_hdr.tcp_dst =htons (dport); + b->ip.tcp_hdr.tcp_seq =htonl (tcp_seq_num);/* * TCP window size - TCP header variable tcp_win. @@ -340,13 +340,13 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len,* it is, then the u-boot tftp or nfs kernel netboot should be * considered. */ - b->ip.hdr.tcp_win =htons (PKTBUFSRX *TCP_MSS >>TCP_SCALE ); + b->ip.tcp_hdr.tcp_win =htons (PKTBUFSRX *TCP_MSS >>TCP_SCALE ); - b->ip.hdr.tcp_xsum =0 ; - b->ip.hdr.tcp_ugr =0 ; + b->ip.tcp_hdr.tcp_xsum =0 ; + b->ip.tcp_hdr.tcp_ugr =0 ; - b->ip.hdr.tcp_xsum =tcp_set_pseudo_header (pkt, net_ip, net_server_ip, - tcp_len, pkt_len); + b->ip.tcp_hdr.tcp_xsum =tcp_set_pseudo_header (pkt, net_ip, net_server_ip, + tcp_len, pkt_len);net_set_ip_header ((uchar *)&b->ip, net_server_ip, net_ip, pkt_len,IPPROTO_TCP );@@ -638,7 +638,7 @@ static u8 tcp_state_machine(u8 tcp_flags, u32 tcp_seq_num, int payload_len)void rxhand_tcp_f (union tcp_build_pkt *b,unsigned int pkt_len) {int tcp_len = pkt_len -IP_HDR_SIZE ; -u16 tcp_rx_xsum = b->ip.hdr.ip_sum; +u16 tcp_rx_xsum = b->ip.ip_hdr.ip_sum;u8 tcp_action =TCP_DATA ;u32 tcp_seq_num, tcp_ack_num;int tcp_hdr_len, payload_len;@@ -646,11 +646,11 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)/* Verify IP header */ debug_cond (DEBUG_DEV_PKT ,"TCP RX in RX Sum (to=%pI4, from=%pI4, len=%d)\n" , - &b->ip.hdr.ip_src, &b->ip.hdr.ip_dst, pkt_len); + &b->ip.ip_hdr.ip_src, &b->ip.ip_hdr.ip_dst, pkt_len); - b->ip.hdr.ip_src = net_server_ip; - b->ip.hdr.ip_dst = net_ip; - b->ip.hdr.ip_sum =0 ; + b->ip.ip_hdr.ip_src = net_server_ip; + b->ip.ip_hdr.ip_dst = net_ip; + b->ip.ip_hdr.ip_sum =0 ;if (tcp_rx_xsum !=compute_ip_checksum (b,IP_HDR_SIZE )) { debug_cond(DEBUG_DEV_PKT, "TCP RX IP xSum Error (%pI4, =%pI4, len=%d)\n",@@ -659,10 +659,10 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len) }/* Build pseudo header and verify TCP header */ - tcp_rx_xsum = b->ip.hdr.tcp_xsum; - b->ip.hdr.tcp_xsum =0 ; -if (tcp_rx_xsum !=tcp_set_pseudo_header ((uchar *)b, b->ip.hdr.ip_src, - b->ip.hdr.ip_dst, tcp_len, + tcp_rx_xsum = b->ip.tcp_hdr.tcp_xsum; + b->ip.tcp_hdr.tcp_xsum =0 ; +if (tcp_rx_xsum !=tcp_set_pseudo_header ((uchar *)b, b->ip.ip_hdr.ip_src, + b->ip.ip_hdr.ip_dst, tcp_len, pkt_len)) { debug_cond(DEBUG_DEV_PKT, "TCP RX TCP xSum Error (%pI4, %pI4, len=%d)\n",@@ -670,7 +670,7 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)return ; } - tcp_hdr_len =GET_TCP_HDR_LEN_IN_BYTES (b->ip.hdr.tcp_hlen); + tcp_hdr_len =GET_TCP_HDR_LEN_IN_BYTES (b->ip.tcp_hdr.tcp_hlen); payload_len = tcp_len - tcp_hdr_len;if (tcp_hdr_len >TCP_HDR_SIZE )@@ -680,11 +680,11 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)* Incoming sequence and ack numbers are server's view of the numbers. * The app must swap the numbers when responding. */ - tcp_seq_num =ntohl (b->ip.hdr.tcp_seq); - tcp_ack_num =ntohl (b->ip.hdr.tcp_ack); + tcp_seq_num =ntohl (b->ip.tcp_hdr.tcp_seq); + tcp_ack_num =ntohl (b->ip.tcp_hdr.tcp_ack);/* Packets are not ordered. Send to app as received. */ - tcp_action =tcp_state_machine (b->ip.hdr.tcp_flags, + tcp_action =tcp_state_machine (b->ip.tcp_hdr.tcp_flags, tcp_seq_num, payload_len); tcp_activity_count++;@@ -698,8 +698,8 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)"TCP Notify (action=%x, Seq=%u,Ack=%u,Pay%d)\n" , tcp_action, tcp_seq_num, tcp_ack_num, payload_len); - (*tcp_packet_handler) ((uchar *)b + pkt_len - payload_len, b->ip.hdr.tcp_dst, - b->ip.hdr.ip_src, b->ip.hdr.tcp_src, tcp_seq_num, + (*tcp_packet_handler) ((uchar *)b + pkt_len - payload_len, b->ip.tcp_hdr.tcp_dst, + b->ip.ip_hdr.ip_src, b->ip.tcp_hdr.tcp_src, tcp_seq_num, tcp_ack_num, tcp_action, payload_len); }else if (tcp_action !=TCP_DATA ) {@@ -711,8 +711,8 @@ void rxhand_tcp_f(union tcp_build_pkt *b, unsigned int pkt_len)* Warning: Incoming Ack & Seq sequence numbers are transposed * here to outgoing Seq & Ack sequence numbers */ -net_send_tcp_packet (0 ,ntohs (b->ip.hdr.tcp_src), -ntohs (b->ip.hdr.tcp_dst), +net_send_tcp_packet (0 ,ntohs (b->ip.tcp_hdr.tcp_src), +ntohs (b->ip.tcp_hdr.tcp_dst), (tcp_action & (~TCP_PUSH )), tcp_ack_num, tcp_ack_edge); }test/cmd/wget.c +8
-9@@ -52,9 +52,9 @@ static int sb_syn_handler(struct udevice *dev, void *packet, {struct eth_sandbox_priv *priv =dev_get_priv (dev);struct ethernet_hdr *eth = packet; -struct ip_tcp_hdr *tcp = packet +ETHER_HDR_SIZE ; +struct tcp_hdr *tcp = packet +ETHER_HDR_SIZE +IP_HDR_SIZE ;struct ethernet_hdr *eth_send; -struct ip_tcp_hdr *tcp_send; +struct tcp_hdr *tcp_send;/* Don't allow the buffer to overrun */ if (priv->recv_packets >=PKTBUFSRX )@@ -64,7 +64,7 @@ static int sb_syn_handler(struct udevice *dev, void *packet,memcpy (eth_send->et_dest, eth->et_src,ARP_HLEN );memcpy (eth_send->et_src, priv->fake_host_hwaddr,ARP_HLEN ); eth_send->et_protlen =htons (PROT_IP ); - tcp_send = (void *)eth_send +ETHER_HDR_SIZE ; + tcp_send = (void *)eth_send +ETHER_HDR_SIZE +IP_HDR_SIZE ; tcp_send->tcp_src = tcp->tcp_dst; tcp_send->tcp_dst = tcp->tcp_src; tcp_send->tcp_seq =htonl (0 );@@ -97,9 +97,9 @@ static int sb_ack_handler(struct udevice *dev, void *packet, {struct eth_sandbox_priv *priv =dev_get_priv (dev);struct ethernet_hdr *eth = packet; -struct ip_tcp_hdr *tcp = packet +ETHER_HDR_SIZE ; +struct tcp_hdr *tcp = packet +ETHER_HDR_SIZE +IP_HDR_SIZE ;struct ethernet_hdr *eth_send; -struct ip_tcp_hdr *tcp_send; +struct tcp_hdr *tcp_send;void *data;int pkt_len;int payload_len =0 ;@@ -115,7 +115,7 @@ static int sb_ack_handler(struct udevice *dev, void *packet,memcpy (eth_send->et_dest, eth->et_src,ARP_HLEN );memcpy (eth_send->et_src, priv->fake_host_hwaddr,ARP_HLEN ); eth_send->et_protlen =htons (PROT_IP ); - tcp_send = (void *)eth_send +ETHER_HDR_SIZE ; + tcp_send = (void *)eth_send +ETHER_HDR_SIZE +IP_HDR_SIZE ; tcp_send->tcp_src = tcp->tcp_dst; tcp_send->tcp_dst = tcp->tcp_src; data = (void *)tcp_send +IP_TCP_HDR_SIZE ;@@ -163,14 +163,14 @@ static int sb_http_handler(struct udevice *dev, void *packet, {struct ethernet_hdr *eth = packet;struct ip_hdr *ip; -struct ip_tcp_hdr *tcp; +struct tcp_hdr *tcp;if (ntohs (eth->et_protlen) ==PROT_ARP ) {return sb_arp_handler (dev, packet, len); }else if (ntohs (eth->et_protlen) ==PROT_IP ) { ip = packet +ETHER_HDR_SIZE ;if (ip->ip_p ==IPPROTO_TCP ) { - tcp = packet +ETHER_HDR_SIZE ; + tcp = ip +IP_HDR_SIZE ;if (tcp->tcp_flags ==TCP_SYN )return sb_syn_handler (dev, packet, len);else if (tcp->tcp_flags &TCP_ACK && !(tcp->tcp_flags &TCP_SYN )) --2.40.1.521.gf1e218fcd8-goog