aboutgitcodebugslistschat
path: root/tcp.c
diff options
context:
space:
mode:
authorLaurent Vivier <lvivier@redhat.com>2024-03-06 16:58:36 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-03-06 08:03:47 +0100
commit7df624e79a450ba6b737b972040d9007ecc61672 (patch)
treeb5b04fd7ad258104870e5dcf2d123578458fa247 /tcp.c
parentfeb4900c25deabaf9b54760e40aea5e65499e987 (diff)
downloadpasst-7df624e79a450ba6b737b972040d9007ecc61672.tar
passt-7df624e79a450ba6b737b972040d9007ecc61672.tar.gz
passt-7df624e79a450ba6b737b972040d9007ecc61672.tar.bz2
passt-7df624e79a450ba6b737b972040d9007ecc61672.tar.lz
passt-7df624e79a450ba6b737b972040d9007ecc61672.tar.xz
passt-7df624e79a450ba6b737b972040d9007ecc61672.tar.zst
passt-7df624e79a450ba6b737b972040d9007ecc61672.zip
checksum: introduce functions to compute the header part checksum for TCP/UDP
The TCP and UDP checksums are computed using the data in the TCP/UDP payload but also some informations in the IP header (protocol, length, source and destination addresses). We add two functions, proto_ipv4_header_psum() and proto_ipv6_header_psum(), to compute the checksum of the IP header part. Signed-off-by: Laurent Vivier <lvivier@redhat.com> Message-ID: <20240303135114.1023026-8-lvivier@redhat.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'tcp.c')
-rw-r--r--tcp.c50
1 files changed, 23 insertions, 27 deletions
diff --git a/tcp.c b/tcp.c
index 7fb9dba..a7156ee 100644
--- a/tcp.c
+++ b/tcp.c
@@ -937,41 +937,33 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
/**
* tcp_update_check_tcp4() - Update TCP checksum from stored one
- * @buf: L2 packet buffer with final IPv4 header
+ * @iph: IPv4 header
+ * @th: TCP header followed by TCP payload
*/
-static void tcp_update_check_tcp4(struct tcp4_l2_buf_t *buf)
+static void tcp_update_check_tcp4(const struct iphdr *iph, struct tcphdr *th)
{
- uint16_t tlen = ntohs(buf->iph.tot_len) - 20;
- uint32_t sum = htons(IPPROTO_TCP);
+ uint16_t tlen = ntohs(iph->tot_len) - sizeof(struct iphdr);
+ struct in_addr saddr = { .s_addr = iph->saddr };
+ struct in_addr daddr = { .s_addr = iph->daddr };
+ uint32_t sum = proto_ipv4_header_psum(tlen, IPPROTO_TCP, saddr, daddr);
- sum += (buf->iph.saddr >> 16) & 0xffff;
- sum += buf->iph.saddr & 0xffff;
- sum += (buf->iph.daddr >> 16) & 0xffff;
- sum += buf->iph.daddr & 0xffff;
- sum += htons(ntohs(buf->iph.tot_len) - 20);
-
- buf->th.check = 0;
- buf->th.check = csum(&buf->th, tlen, sum);
+ th->check = 0;
+ th->check = csum(th, tlen, sum);
}
/**
* tcp_update_check_tcp6() - Calculate TCP checksum for IPv6
- * @buf: L2 packet buffer with final IPv6 header
+ * @ip6h: IPv6 header
+ * @th: TCP header followed by TCP payload
*/
-static void tcp_update_check_tcp6(struct tcp6_l2_buf_t *buf)
+static void tcp_update_check_tcp6(struct ipv6hdr *ip6h, struct tcphdr *th)
{
- int len = ntohs(buf->ip6h.payload_len) + sizeof(struct ipv6hdr);
-
- buf->ip6h.hop_limit = IPPROTO_TCP;
- buf->ip6h.version = 0;
- buf->ip6h.nexthdr = 0;
+ uint16_t payload_len = ntohs(ip6h->payload_len);
+ uint32_t sum = proto_ipv6_header_psum(payload_len, IPPROTO_TCP,
+ &ip6h->saddr, &ip6h->daddr);
- buf->th.check = 0;
- buf->th.check = csum(&buf->ip6h, len, 0);
-
- buf->ip6h.hop_limit = 255;
- buf->ip6h.version = 6;
- buf->ip6h.nexthdr = IPPROTO_TCP;
+ th->check = 0;
+ th->check = csum(th, payload_len, sum);
}
/**
@@ -1383,7 +1375,7 @@ do { \
SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq);
- tcp_update_check_tcp4(b);
+ tcp_update_check_tcp4(&b->iph, &b->th);
tlen = tap_iov_len(c, &b->taph, ip_len);
} else {
@@ -1402,7 +1394,11 @@ do { \
SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq);
- tcp_update_check_tcp6(b);
+ tcp_update_check_tcp6(&b->ip6h, &b->th);
+
+ b->ip6h.hop_limit = 255;
+ b->ip6h.version = 6;
+ b->ip6h.nexthdr = IPPROTO_TCP;
b->ip6h.flow_lbl[0] = (conn->sock >> 16) & 0xf;
b->ip6h.flow_lbl[1] = (conn->sock >> 8) & 0xff;