diff options
| author | Stefano Brivio <sbrivio@redhat.com> | 2026-11-24 00:09:07 +0100 |
|---|---|---|
| committer | Stefano Brivio <sbrivio@redhat.com> | 2026-11-30 00:37:09 +0100 |
| commit | 7d506434cc080aaa427ad9aa609481cc7d4014a8 (patch) | |
| tree | db8f151825f3d9fe5f66f8ea211dcc2bb7d1fe87 | |
| parent | 9206a818976540ba1f6fdb976891a2dbf5734f50 (diff) | |
| download | passt-7d506434cc080aaa427ad9aa609481cc7d4014a8.tar passt-7d506434cc080aaa427ad9aa609481cc7d4014a8.tar.gz passt-7d506434cc080aaa427ad9aa609481cc7d4014a8.tar.bz2 passt-7d506434cc080aaa427ad9aa609481cc7d4014a8.tar.lz passt-7d506434cc080aaa427ad9aa609481cc7d4014a8.tar.xz passt-7d506434cc080aaa427ad9aa609481cc7d4014a8.tar.zst passt-7d506434cc080aaa427ad9aa609481cc7d4014a8.zip | |
tap: Move tap4_handler() / tap6_handler() adddress checks to own function
This avoids some duplication and makes upcoming patches less ugly.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
| -rw-r--r-- | tap.c | 60 |
1 files changed, 35 insertions, 25 deletions
@@ -661,6 +661,39 @@ static void tap_packet_debug(const struct iphdr *iph, } /** + * tap_filter_address() - Filter packets based on source and destination address + * @af: Address family, AF_INET or AF_INET6 + * @saddr: Source address + * @daddr: Destination address + * + * Return: true if packet must be dropped, false otherwise + */ +static bool tap_filter_address(sa_family_t af, + const void *saddr, const void *daddr) +{ + /* The scope of sstr and dstr could be in theory reduced into the + * conditional block debug() expands to, but it's awkward and + * unreadable, so ignore this warning. + */ + /* cppcheck-suppress [variableScope,unmatchedSuppression] */ + char sstr[INANY_ADDRSTRLEN], dstr[INANY_ADDRSTRLEN]; + + if (af == AF_INET && + !IN4_IS_ADDR_LOOPBACK(saddr) && !IN4_IS_ADDR_LOOPBACK(daddr)) + return false; + + if (af == AF_INET6 && + !IN6_IS_ADDR_LOOPBACK(saddr) && !IN6_IS_ADDR_LOOPBACK(daddr)) + return false; + + debug("Invalid address on tap interface: %s -> %s", + inet_ntop(af, saddr, sstr, sizeof(sstr)), + inet_ntop(af, daddr, dstr, sizeof(dstr))); + + return true; +} + +/** * tap4_is_fragment() - Determine if a packet is an IP fragment * @iph: IPv4 header (length already validated) * @now: Current timestamp @@ -741,20 +774,8 @@ resume: l4len = htons(iph->tot_len) - hlen; - if (IN4_IS_ADDR_LOOPBACK(&iph->saddr) || - IN4_IS_ADDR_LOOPBACK(&iph->daddr)) { - /* The scope of sstr and dstr could be in theory reduced - * into the conditional block debug() expands to, but - * it's awkward and unreadable, so ignore this warning. - */ - /* cppcheck-suppress [variableScope,unmatchedSuppression] */ - char sstr[INET_ADDRSTRLEN], dstr[INET_ADDRSTRLEN]; - - debug("Loopback address on tap interface: %s -> %s", - inet_ntop(AF_INET, &iph->saddr, sstr, sizeof(sstr)), - inet_ntop(AF_INET, &iph->daddr, dstr, sizeof(dstr))); + if (tap_filter_address(AF_INET, &iph->saddr, &iph->daddr)) continue; - } if (!c->ip4.addr_fixed && iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr) @@ -983,19 +1004,8 @@ resume: if (!ipv6_l4hdr(&data, &proto, &l4len)) continue; - if (IN6_IS_ADDR_LOOPBACK(saddr) || IN6_IS_ADDR_LOOPBACK(daddr)) { - /* The scope of sstr and dstr could be in theory reduced - * into the conditional block debug() expands to, but - * it's awkward and unreadable, so ignore this warning. - */ - /* cppcheck-suppress [variableScope,unmatchedSuppression] */ - char sstr[INET6_ADDRSTRLEN], dstr[INET6_ADDRSTRLEN]; - - debug("Loopback address on tap interface: %s -> %s", - inet_ntop(AF_INET6, saddr, sstr, sizeof(sstr)), - inet_ntop(AF_INET6, daddr, dstr, sizeof(dstr))); + if (tap_filter_address(AF_INET6, saddr, daddr)) continue; - } if (IN6_IS_ADDR_LINKLOCAL(saddr)) { c->ip6.addr_ll_seen = *saddr; |
