From 8af961b85bbfd48f4304851bcd42f106fa447bb9 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Mon, 26 Jul 2021 18:20:01 +0200 Subject: tcp, udp: Map source address to gateway for any traffic from 127.0.0.0/8 ...instead of just 127.0.0.1. Signed-off-by: Stefano Brivio --- tcp.c | 6 +++--- udp.c | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tcp.c b/tcp.c index 1a914c9..99841dc 100644 --- a/tcp.c +++ b/tcp.c @@ -2291,13 +2291,13 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, tcp_hash_insert(c, conn, AF_INET6, &sa6->sin6_addr); } else { struct sockaddr_in *sa4 = (struct sockaddr_in *)&sa; + in_addr_t s_addr = ntohl(sa4->sin_addr.s_addr); memset(&conn->a.a4.zero, 0, sizeof(conn->a.a4.zero)); memset(&conn->a.a4.one, 0xff, sizeof(conn->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->addr4_seen) + if (s_addr >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET || + s_addr == INADDR_ANY || s_addr == c->addr4_seen) sa4->sin_addr.s_addr = c->gw4; memcpy(&conn->a.a4.a, &sa4->sin_addr, sizeof(conn->a.a4.a)); diff --git a/udp.c b/udp.c index 92f21d3..184b736 100644 --- a/udp.c +++ b/udp.c @@ -728,15 +728,16 @@ void udp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, for (i = 0; i < n; i++) { struct udp4_l2_buf_t *b = &udp4_l2_buf[i]; size_t ip_len, iov_len; + in_addr_t s_addr; ip_len = udp4_l2_mh_sock[i].msg_len + sizeof(b->iph) + sizeof(b->uh); b->iph.tot_len = htons(ip_len); - if (ntohl(b->s_in.sin_addr.s_addr) == INADDR_LOOPBACK || - ntohl(b->s_in.sin_addr.s_addr) == INADDR_ANY || - b->s_in.sin_addr.s_addr == c->addr4_seen) { + s_addr = ntohl(b->s_in.sin_addr.s_addr); + if (s_addr >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET || + s_addr == INADDR_ANY || s_addr == c->addr4_seen) { in_port_t src = htons(b->s_in.sin_port); b->iph.saddr = c->gw4; -- cgit v1.2.3