X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/63a44fc49e9cdb6142b1ebe64ab933247d357178..486704db7c840dcfb51f70f1812d9c3ad37ad39c:/print-nflog.c diff --git a/print-nflog.c b/print-nflog.c index fdd21e69..a29b930c 100644 --- a/print-nflog.c +++ b/print-nflog.c @@ -31,30 +31,22 @@ #include -#include -#include - -#include "netdissect.h" #include "interface.h" -#ifdef HAVE_LINUX_NETFILTER_NFNETLINK_LOG_H -#include -#include "nflog.h" - -#ifdef DLT_NFLOG +#if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H) +#include -const struct tok nflog_values[] = { +static const struct tok nflog_values[] = { { AF_INET, "IPv4" }, +#ifdef INET6 { AF_INET6, "IPv6" }, - { 0, NULL } +#endif /*INET6*/ + { 0, NULL } }; static inline void -nflog_hdr_print(struct netdissect_options *ndo, const u_char *bp, u_int length) +nflog_hdr_print(netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length) { - const nflog_hdr_t *hdr; - hdr = (const nflog_hdr_t *)bp; - ND_PRINT((ndo, "version %d, resource ID %d", hdr->nflog_version, ntohs(hdr->nflog_rid))); if (!ndo->ndo_qflag) { @@ -72,83 +64,103 @@ nflog_hdr_print(struct netdissect_options *ndo, const u_char *bp, u_int length) ND_PRINT((ndo, ", length %u: ", length)); } -static void -nflog_print(struct netdissect_options *ndo, const u_char *p, u_int length, u_int caplen) +u_int +nflog_if_print(netdissect_options *ndo, + const struct pcap_pkthdr *h, const u_char *p) { - const nflog_hdr_t *hdr; + const nflog_hdr_t *hdr = (const nflog_hdr_t *)p; const nflog_tlv_t *tlv; - u_int16_t size; + uint16_t size; + uint16_t h_size = sizeof(nflog_hdr_t); + u_int caplen = h->caplen; + u_int length = h->len; - if (caplen < (int) sizeof(nflog_hdr_t)) { + if (caplen < (int) sizeof(nflog_hdr_t) || length < (int) sizeof(nflog_hdr_t)) { ND_PRINT((ndo, "[|nflog]")); - return; + return h_size; + } + + if (!(hdr->nflog_version) == 0) { + ND_PRINT((ndo, "version %u (unknown)", hdr->nflog_version)); + return h_size; } if (ndo->ndo_eflag) - nflog_hdr_print(ndo, p, length); + nflog_hdr_print(ndo, hdr, length); + p += sizeof(nflog_hdr_t); length -= sizeof(nflog_hdr_t); caplen -= sizeof(nflog_hdr_t); - hdr = (const nflog_hdr_t *)p; - p += sizeof(nflog_hdr_t); - do { + while (length > 0) { + /* We have some data. Do we have enough for the TLV header? */ + if (caplen < sizeof(nflog_tlv_t) || length < sizeof(nflog_tlv_t)) { + /* No. */ + ND_PRINT((ndo, "[|nflog]")); + return h_size; + } + tlv = (const nflog_tlv_t *) p; size = tlv->tlv_length; - - /* wrong size of the packet */ - if (size > length ) - return; - - /* wrong tlv type */ - if (tlv->tlv_type > NFULA_MAX) - return; - if (size % 4 != 0) size += 4 - size % 4; - p += size; - length = length - size; - caplen = caplen - size; + /* Is the TLV's length less than the minimum? */ + if (size < sizeof(nflog_tlv_t)) { + /* Yes. Give up now. */ + ND_PRINT((ndo, "[|nflog]")); + return h_size; + } + + /* Do we have enough data for the full TLV? */ + if (caplen < size || length < size) { + /* No. */ + ND_PRINT((ndo, "[|nflog]")); + return h_size; + } - } while (tlv->tlv_type != NFULA_PAYLOAD); + if (tlv->tlv_type == NFULA_PAYLOAD) { + /* + * This TLV's data is the packet payload. + * Skip past the TLV header, and break out + * of the loop so we print the packet data. + */ + p += sizeof(nflog_tlv_t); + h_size += sizeof(nflog_tlv_t); + length -= sizeof(nflog_tlv_t); + caplen -= sizeof(nflog_tlv_t); + break; + } - /* dont skip payload just tlv length and type */ - p = p - size + 4; - length += size - 4; - caplen += size - 4; + p += size; + h_size += size; + length -= size; + caplen -= size; + } switch (hdr->nflog_family) { case AF_INET: - ip_print(ndo, p, length); + ip_print(ndo, p, length); break; -#ifdef INET6 +#ifdef AF_INET6 case AF_INET6: ip6_print(ndo, p, length); break; -#endif /*INET6*/ +#endif /* AF_INET6 */ default: if (!ndo->ndo_eflag) - nflog_hdr_print(ndo, (u_char *)hdr, + nflog_hdr_print(ndo, hdr, length + sizeof(nflog_hdr_t)); if (!ndo->ndo_suppress_default_print) - ndo->ndo_default_print(ndo, p, caplen); + ND_DEFAULTPRINT(p, caplen); break; } -} - -u_int -nflog_if_print(struct netdissect_options *ndo, - const struct pcap_pkthdr *h, const u_char *p) -{ - nflog_print(ndo, p, h->len, h->caplen); - return (sizeof(nflog_hdr_t)); + return h_size; } -#endif /* HAVE_LINUX_NETFILTER_NFNETLINK_LOG_H */ -#endif /* DLT_NFLOG */ +#endif /* defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H) */