aboutgitcodebugslistschat
path: root/tcp.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-09-08 11:49:46 +1000
committerStefano Brivio <sbrivio@redhat.com>2023-09-08 09:15:46 +0200
commit043a70b88591777f3eb0b3597c188cfc59ed03f6 (patch)
treecb76b07c0feb5107caaea5105b445af22e63787f /tcp.c
parentee58f37db060535bee298bc98f61497eac37f152 (diff)
downloadpasst-043a70b88591777f3eb0b3597c188cfc59ed03f6.tar
passt-043a70b88591777f3eb0b3597c188cfc59ed03f6.tar.gz
passt-043a70b88591777f3eb0b3597c188cfc59ed03f6.tar.bz2
passt-043a70b88591777f3eb0b3597c188cfc59ed03f6.tar.lz
passt-043a70b88591777f3eb0b3597c188cfc59ed03f6.tar.xz
passt-043a70b88591777f3eb0b3597c188cfc59ed03f6.tar.zst
passt-043a70b88591777f3eb0b3597c188cfc59ed03f6.zip
tcp, tap: Correctly advance through packets in tcp_tap_handler()
In both tap4_handler() and tap6_handler(), once we've sorted incoming l3 packets into "sequences", we then step through all the packets in each TCP sequence calling tcp_tap_handler(). Or so it appears. In fact, tcp_tap_handler() doesn't take an index and always looks at packet 0 of the sequence, except when it calls tcp_data_from_tap() to process data packets. It appears to be written with the idea that the struct pool is a queue, from which it consumes packets as it processes them, but that's not how the pool data structure works - they are more like an array of packets. We only get away with this, because setup packets for TCP tend to come in separate batches (because we need to reply in between) and so we only get a bunch of packets for the same connection together when they're data packets (tcp_data_from_tap() has its own loop through packets). Correct this by adding an index parameter to tcp_tap_handler() and altering the loops in tap.c to step through the pool properly. Link: https://bugs.passt.top/show_bug.cgi?id=68 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.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/tcp.c b/tcp.c
index c89e6e4..d8c2327 100644
--- a/tcp.c
+++ b/tcp.c
@@ -2294,11 +2294,12 @@ err:
* @c: Execution context
* @conn: Connection pointer
* @p: Pool of TCP packets, with TCP headers
+ * @idx: Index of first data packet in pool
*
* #syscalls sendmsg
*/
static void tcp_data_from_tap(struct ctx *c, struct tcp_tap_conn *conn,
- const struct pool *p)
+ const struct pool *p, int idx)
{
int i, iov_i, ack = 0, fin = 0, retr = 0, keep = -1, partial_send = 0;
uint16_t max_ack_seq_wnd = conn->wnd_from_tap;
@@ -2313,7 +2314,7 @@ static void tcp_data_from_tap(struct ctx *c, struct tcp_tap_conn *conn,
ASSERT(conn->events & ESTABLISHED);
- for (i = 0, iov_i = 0; i < (int)p->count; i++) {
+ for (i = idx, iov_i = 0; i < (int)p->count; i++) {
uint32_t seq, seq_offset, ack_seq;
struct tcphdr *th;
char *data;
@@ -2530,12 +2531,13 @@ static void tcp_conn_from_sock_finish(struct ctx *c, struct tcp_tap_conn *conn,
* @saddr: Source address
* @daddr: Destination address
* @p: Pool of TCP packets, with TCP headers
+ * @idx: Index of first packet in pool to process
* @now: Current timestamp
*
* Return: count of consumed packets
*/
int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
- const struct pool *p, const struct timespec *now)
+ const struct pool *p, int idx, const struct timespec *now)
{
struct tcp_tap_conn *conn;
size_t optlen, len;
@@ -2543,17 +2545,17 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
int ack_due = 0;
char *opts;
- if (!packet_get(p, 0, 0, 0, &len))
+ if (!packet_get(p, idx, 0, 0, &len))
return 1;
- th = packet_get(p, 0, 0, sizeof(*th), NULL);
+ th = packet_get(p, idx, 0, sizeof(*th), NULL);
if (!th)
return 1;
optlen = th->doff * 4UL - sizeof(*th);
/* Static checkers might fail to see this: */
optlen = MIN(optlen, ((1UL << 4) /* from doff width */ - 6) * 4UL);
- opts = packet_get(p, 0, sizeof(*th), optlen, NULL);
+ opts = packet_get(p, idx, sizeof(*th), optlen, NULL);
conn = tcp_hash_lookup(c, af, daddr, htons(th->source), htons(th->dest));
@@ -2569,7 +2571,7 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
if (th->rst) {
conn_event(c, conn, CLOSED);
- return p->count;
+ return p->count - idx;
}
if (th->ack && !(conn->events & ESTABLISHED))
@@ -2591,7 +2593,7 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
if (conn->events & TAP_SYN_RCVD) {
if (!(conn->events & TAP_SYN_ACK_SENT)) {
tcp_rst(c, conn);
- return p->count;
+ return p->count - idx;
}
conn_event(c, conn, ESTABLISHED);
@@ -2603,19 +2605,19 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
tcp_send_flag(c, conn, ACK);
conn_event(c, conn, SOCK_FIN_SENT);
- return p->count;
+ return p->count - idx;
}
if (!th->ack) {
tcp_rst(c, conn);
- return p->count;
+ return p->count - idx;
}
tcp_clamp_window(c, conn, ntohs(th->window));
tcp_data_from_sock(c, conn);
- if (p->count == 1)
+ if (p->count - idx == 1)
return 1;
}
@@ -2631,7 +2633,7 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
}
/* Established connections accepting data from tap */
- tcp_data_from_tap(c, conn, p);
+ tcp_data_from_tap(c, conn, p, idx);
if (conn->seq_ack_to_tap != conn->seq_from_tap)
ack_due = 1;
@@ -2645,7 +2647,7 @@ int tcp_tap_handler(struct ctx *c, int af, const void *saddr, const void *daddr,
if (ack_due)
conn_flag(c, conn, ACK_TO_TAP_DUE);
- return p->count;
+ return p->count - idx;
}
/**