net/tcp: simplify tcp header filling code

This patch:
 * remove useless code,
 * use a special function for pretty printing of tcp flags,
 * simplify the code

The behavior should not be changed.

Signed-off-by: Mikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Mikhail Kshevetskiy
2024-12-28 13:46:35 +03:00
committed by Tom Rini
parent ece1631f5e
commit 3a44b8ba94

View File

@@ -563,10 +563,33 @@ void net_set_syn_options(struct tcp_stream *tcp, union tcp_build_pkt *b)
b->ip.end = TCP_O_END; b->ip.end = TCP_O_END;
} }
const char *tcpflags_to_str(char tcpflags, char *buf, int size)
{
int i;
static const struct {
int bit;
const char *name;
} desc[] = {{TCP_RST, "RST"}, {TCP_SYN, "SYN"}, {TCP_PUSH, "PSH"},
{TCP_FIN, "FIN"}, {TCP_ACK, "ACK"}};
*buf = '\0';
for (i = 0; i < ARRAY_SIZE(desc); i++) {
if (!(tcpflags & desc[i].bit))
continue;
if (*buf)
strlcat(buf, ",", size);
strlcat(buf, desc[i].name, size);
}
return buf;
}
int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len, int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len,
u8 action, u32 tcp_seq_num, u32 tcp_ack_num) u8 action, u32 tcp_seq_num, u32 tcp_ack_num)
{ {
union tcp_build_pkt *b = (union tcp_build_pkt *)pkt; union tcp_build_pkt *b = (union tcp_build_pkt *)pkt;
char buf[24];
int pkt_hdr_len; int pkt_hdr_len;
int pkt_len; int pkt_len;
int tcp_len; int tcp_len;
@@ -576,55 +599,32 @@ int tcp_set_tcp_header(struct tcp_stream *tcp, uchar *pkt, int payload_len,
* 4 bits reserved options * 4 bits reserved options
*/ */
b->ip.hdr.tcp_flags = action; b->ip.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.hdr.tcp_hlen = SHIFT_TO_TCPHDRLEN_FIELD(LEN_B_TO_DW(TCP_HDR_SIZE));
switch (action) { switch (action) {
case TCP_SYN: case TCP_SYN:
debug_cond(DEBUG_DEV_PKT, debug_cond(DEBUG_DEV_PKT,
"TCP Hdr:SYN (%pI4, %pI4, sq=%u, ak=%u)\n", "TCP Hdr:%s (%pI4, %pI4, s=%u, a=%u)\n",
&tcp->rhost, &net_ip, tcpflags_to_str(action, buf, sizeof(buf)),
tcp_seq_num, tcp_ack_num); &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num);
net_set_syn_options(tcp, b); net_set_syn_options(tcp, b);
pkt_hdr_len = IP_TCP_O_SIZE; pkt_hdr_len = IP_TCP_O_SIZE;
break; break;
case TCP_SYN | TCP_ACK:
case TCP_ACK:
pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(tcp, b);
b->ip.hdr.tcp_flags = action;
debug_cond(DEBUG_DEV_PKT,
"TCP Hdr:ACK (%pI4, %pI4, s=%u, a=%u, A=%x)\n",
&tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num,
action);
break;
case TCP_FIN:
debug_cond(DEBUG_DEV_PKT,
"TCP Hdr:FIN (%pI4, %pI4, s=%u, a=%u)\n",
&tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num);
payload_len = 0;
pkt_hdr_len = IP_TCP_HDR_SIZE;
break;
case TCP_RST | TCP_ACK: case TCP_RST | TCP_ACK:
case TCP_RST: case TCP_RST:
debug_cond(DEBUG_DEV_PKT, debug_cond(DEBUG_DEV_PKT,
"TCP Hdr:RST (%pI4, %pI4, s=%u, a=%u)\n", "TCP Hdr:%s (%pI4, %pI4, s=%u, a=%u)\n",
tcpflags_to_str(action, buf, sizeof(buf)),
&tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num); &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num);
pkt_hdr_len = IP_TCP_HDR_SIZE;
break; 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(%pI4, %pI4, s=%u, a=%u, A=%x)\n",
&tcp->rhost, &net_ip,
tcp_seq_num, tcp_ack_num, action);
fallthrough;
default: default:
pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(tcp, b); pkt_hdr_len = IP_HDR_SIZE + net_set_ack_options(tcp, b);
b->ip.hdr.tcp_flags = action | TCP_PUSH | TCP_ACK;
debug_cond(DEBUG_DEV_PKT, debug_cond(DEBUG_DEV_PKT,
"TCP Hdr:dft (%pI4, %pI4, s=%u, a=%u, A=%x)\n", "TCP Hdr:%s (%pI4, %pI4, s=%u, a=%u)\n",
&tcp->rhost, &net_ip, tcpflags_to_str(action, buf, sizeof(buf)),
tcp_seq_num, tcp_ack_num, action); &tcp->rhost, &net_ip, tcp_seq_num, tcp_ack_num);
break;
} }
pkt_len = pkt_hdr_len + payload_len; pkt_len = pkt_hdr_len + payload_len;