From 6488c3e8489da9aaa6642a367ac2bff70efff749 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Thu, 22 Apr 2021 17:03:43 +0200 Subject: [PATCH] tcp, udp: Replace loopback source address by gateway address This is symmetric with tap operation and addressing model, and allows again to reach the guest behind the tap interface by contacting the local address. Signed-off-by: Stefano Brivio --- tcp.c | 8 ++++++++ udp.c | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/tcp.c b/tcp.c index 1403da8..330e21a 100644 --- a/tcp.c +++ b/tcp.c @@ -1008,6 +1008,11 @@ static void tcp_conn_from_sock(struct ctx *c, int fd) memset(&tc[s].a.a4.zero, 0, sizeof(tc[s].a.a4.zero)); memset(&tc[s].a.a4.one, 0xff, sizeof(tc[s].a.a4.one)); + + if (ntohl(sa4->sin_addr.s_addr) == INADDR_LOOPBACK || + ntohl(sa4->sin_addr.s_addr) == INADDR_ANY) + sa4->sin_addr.s_addr = c->gw4; + memcpy(&tc[s].a.a4.a, &sa4->sin_addr, sizeof(tc[s].a.a4.a)); tc[s].sock_port = sa4->sin_port; @@ -1022,6 +1027,9 @@ static void tcp_conn_from_sock(struct ctx *c, int fd) } else if (sa_l.ss_family == AF_INET6) { struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&sa_r; + if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) + memcpy(&sa6->sin6_addr, &c->gw6, sizeof(c->gw6)); + memcpy(&tc[s].a.a6, &sa6->sin6_addr, sizeof(tc[s].a.a6)); tc[s].sock_port = sa6->sin6_port; diff --git a/udp.c b/udp.c index dda3a68..7be88f6 100644 --- a/udp.c +++ b/udp.c @@ -84,6 +84,10 @@ void udp_sock_handler(struct ctx *c, int s, uint32_t events) struct sockaddr_in *sr4 = (struct sockaddr_in *)&sr; struct sockaddr_in *sl4 = (struct sockaddr_in *)&sl; + if (ntohl(sr4->sin_addr.s_addr) == INADDR_LOOPBACK || + ntohl(sr4->sin_addr.s_addr) == INADDR_ANY) + sr4->sin_addr.s_addr = c->gw4; + memcpy(&a6.s6_addr[12], &sr4->sin_addr, sizeof(sr4->sin_addr)); uh->source = sr4->sin_port; uh->dest = sl4->sin_port; @@ -94,6 +98,9 @@ void udp_sock_handler(struct ctx *c, int s, uint32_t events) struct sockaddr_in6 *sr6 = (struct sockaddr_in6 *)&sr; struct sockaddr_in6 *sl6 = (struct sockaddr_in6 *)&sl; + if (IN6_IS_ADDR_LOOPBACK(&sr6->sin6_addr)) + memcpy(&sr6->sin6_addr, &c->gw6, sizeof(c->gw6)); + uh->source = sr6->sin6_port; uh->dest = sl6->sin6_port; uh->len = htons(n + sizeof(*uh));