2013-08-06 17:26:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Petar Alilovic,
|
|
|
|
* Faculty of Electrical Engineering and Computing, University of Zagreb
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-08-15 21:27:28 +08:00
|
|
|
/* \summary: DLT_NFLOG printer */
|
|
|
|
|
2018-01-22 04:27:08 +08:00
|
|
|
#include <config.h>
|
2013-05-31 18:16:13 +08:00
|
|
|
|
2018-01-20 22:59:49 +08:00
|
|
|
#include "netdissect-stdinc.h"
|
2013-05-31 18:16:13 +08:00
|
|
|
|
2024-02-23 09:43:48 +08:00
|
|
|
#define ND_LONGJMP_FROM_TCHECK
|
2015-09-06 05:35:58 +08:00
|
|
|
#include "netdissect.h"
|
2018-05-24 05:43:47 +08:00
|
|
|
#include "extract.h"
|
2013-05-31 18:16:13 +08:00
|
|
|
|
2018-05-24 05:43:47 +08:00
|
|
|
#ifdef DLT_NFLOG
|
2018-04-05 04:19:18 +08:00
|
|
|
|
2018-05-24 05:43:47 +08:00
|
|
|
/*
|
|
|
|
* Structure of an NFLOG header and TLV parts, as described at
|
2018-07-09 18:04:39 +08:00
|
|
|
* https://www.tcpdump.org/linktypes/LINKTYPE_NFLOG.html
|
2018-05-24 05:43:47 +08:00
|
|
|
*
|
|
|
|
* The NFLOG header is big-endian.
|
|
|
|
*
|
|
|
|
* The TLV length and type are in host byte order. The value is either
|
|
|
|
* big-endian or is an array of bytes in some externally-specified byte
|
|
|
|
* order (text string, link-layer address, link-layer header, packet
|
|
|
|
* data, etc.).
|
|
|
|
*/
|
|
|
|
typedef struct nflog_hdr {
|
|
|
|
nd_uint8_t nflog_family; /* address family */
|
|
|
|
nd_uint8_t nflog_version; /* version */
|
|
|
|
nd_uint16_t nflog_rid; /* resource ID */
|
|
|
|
} nflog_hdr_t;
|
|
|
|
|
2020-05-04 21:23:34 +08:00
|
|
|
#define NFLOG_HDR_LEN sizeof(nflog_hdr_t)
|
|
|
|
|
2018-05-24 05:43:47 +08:00
|
|
|
typedef struct nflog_tlv {
|
|
|
|
nd_uint16_t tlv_length; /* tlv length */
|
|
|
|
nd_uint16_t tlv_type; /* tlv type */
|
|
|
|
/* value follows this */
|
|
|
|
} nflog_tlv_t;
|
|
|
|
|
2020-05-04 21:23:34 +08:00
|
|
|
#define NFLOG_TLV_LEN sizeof(nflog_tlv_t)
|
|
|
|
|
2018-05-24 05:43:47 +08:00
|
|
|
typedef struct nflog_packet_hdr {
|
|
|
|
nd_uint16_t hw_protocol; /* hw protocol */
|
|
|
|
nd_uint8_t hook; /* netfilter hook */
|
|
|
|
nd_byte pad[1]; /* padding to 32 bits */
|
|
|
|
} nflog_packet_hdr_t;
|
|
|
|
|
|
|
|
typedef struct nflog_hwaddr {
|
|
|
|
nd_uint16_t hw_addrlen; /* address length */
|
|
|
|
nd_byte pad[2]; /* padding to 32-bit boundary */
|
|
|
|
nd_byte hw_addr[8]; /* address, up to 8 bytes */
|
|
|
|
} nflog_hwaddr_t;
|
|
|
|
|
|
|
|
typedef struct nflog_timestamp {
|
|
|
|
nd_uint64_t sec;
|
|
|
|
nd_uint64_t usec;
|
|
|
|
} nflog_timestamp_t;
|
2013-09-09 18:45:31 +08:00
|
|
|
|
2018-05-24 05:43:47 +08:00
|
|
|
/*
|
|
|
|
* TLV types.
|
|
|
|
*/
|
|
|
|
#define NFULA_PACKET_HDR 1 /* nflog_packet_hdr_t */
|
|
|
|
#define NFULA_MARK 2 /* packet mark from skbuff */
|
|
|
|
#define NFULA_TIMESTAMP 3 /* nflog_timestamp_t for skbuff's time stamp */
|
|
|
|
#define NFULA_IFINDEX_INDEV 4 /* ifindex of device on which packet received (possibly bridge group) */
|
|
|
|
#define NFULA_IFINDEX_OUTDEV 5 /* ifindex of device on which packet transmitted (possibly bridge group) */
|
|
|
|
#define NFULA_IFINDEX_PHYSINDEV 6 /* ifindex of physical device on which packet received (not bridge group) */
|
|
|
|
#define NFULA_IFINDEX_PHYSOUTDEV 7 /* ifindex of physical device on which packet transmitted (not bridge group) */
|
|
|
|
#define NFULA_HWADDR 8 /* nflog_hwaddr_t for hardware address */
|
|
|
|
#define NFULA_PAYLOAD 9 /* packet payload */
|
|
|
|
#define NFULA_PREFIX 10 /* text string - null-terminated, count includes NUL */
|
|
|
|
#define NFULA_UID 11 /* UID owning socket on which packet was sent/received */
|
|
|
|
#define NFULA_SEQ 12 /* sequence number of packets on this NFLOG socket */
|
2023-07-29 16:56:54 +08:00
|
|
|
#define NFULA_SEQ_GLOBAL 13 /* sequence number of packets on all NFLOG sockets */
|
2018-05-24 05:43:47 +08:00
|
|
|
#define NFULA_GID 14 /* GID owning socket on which packet was sent/received */
|
|
|
|
#define NFULA_HWTYPE 15 /* ARPHRD_ type of skbuff's device */
|
|
|
|
#define NFULA_HWHEADER 16 /* skbuff's MAC-layer header */
|
|
|
|
#define NFULA_HWLEN 17 /* length of skbuff's MAC-layer header */
|
2018-04-05 04:19:18 +08:00
|
|
|
|
2023-02-12 03:01:01 +08:00
|
|
|
/*
|
|
|
|
* Define two constants specifically for the two AF code points from the
|
|
|
|
* LINKTYPE_NFLOG specification above and use these constants instead of
|
|
|
|
* AF_INET and AF_INET6. This is the only way to dissect the "wire" encoding
|
|
|
|
* correctly because some BSD systems define AF_INET6 differently from Linux
|
|
|
|
* (see af.h) and Haiku defines both AF_INET and AF_INET6 differently from
|
|
|
|
* Linux.
|
|
|
|
*/
|
|
|
|
#define NFLOG_AF_INET 2
|
|
|
|
#define NFLOG_AF_INET6 10
|
2013-09-25 00:46:24 +08:00
|
|
|
static const struct tok nflog_values[] = {
|
2023-02-12 03:01:01 +08:00
|
|
|
{ NFLOG_AF_INET, "IPv4" },
|
|
|
|
{ NFLOG_AF_INET6, "IPv6" },
|
2014-05-01 06:29:40 +08:00
|
|
|
{ 0, NULL }
|
2013-08-06 17:26:38 +08:00
|
|
|
};
|
|
|
|
|
2018-01-26 22:15:58 +08:00
|
|
|
static void
|
2014-04-04 15:43:46 +08:00
|
|
|
nflog_hdr_print(netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length)
|
2013-08-06 17:26:38 +08:00
|
|
|
{
|
2018-05-24 05:43:47 +08:00
|
|
|
ND_PRINT("version %u, resource ID %u",
|
2018-06-16 23:23:21 +08:00
|
|
|
GET_U_1(hdr->nflog_version), GET_BE_U_2(hdr->nflog_rid));
|
2013-08-06 17:26:38 +08:00
|
|
|
|
|
|
|
if (!ndo->ndo_qflag) {
|
2018-05-24 05:43:47 +08:00
|
|
|
ND_PRINT(", family %s (%u)",
|
2018-01-09 19:15:30 +08:00
|
|
|
tok2str(nflog_values, "Unknown",
|
2018-06-16 23:23:21 +08:00
|
|
|
GET_U_1(hdr->nflog_family)),
|
|
|
|
GET_U_1(hdr->nflog_family));
|
2013-08-06 17:26:38 +08:00
|
|
|
} else {
|
2018-01-07 18:47:30 +08:00
|
|
|
ND_PRINT(", %s",
|
2018-01-09 19:15:30 +08:00
|
|
|
tok2str(nflog_values,
|
|
|
|
"Unknown NFLOG (0x%02x)",
|
2018-06-16 23:23:21 +08:00
|
|
|
GET_U_1(hdr->nflog_family)));
|
2013-08-06 17:26:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 18:47:30 +08:00
|
|
|
ND_PRINT(", length %u: ", length);
|
2013-08-06 17:26:38 +08:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:14:14 +08:00
|
|
|
void
|
2014-04-04 15:43:46 +08:00
|
|
|
nflog_if_print(netdissect_options *ndo,
|
2020-05-04 22:14:14 +08:00
|
|
|
const struct pcap_pkthdr *h, const u_char *p)
|
2013-05-31 18:16:13 +08:00
|
|
|
{
|
2013-10-31 22:58:42 +08:00
|
|
|
const nflog_hdr_t *hdr = (const nflog_hdr_t *)p;
|
2014-04-23 15:20:40 +08:00
|
|
|
uint16_t size;
|
2020-05-04 21:23:34 +08:00
|
|
|
uint16_t h_size = NFLOG_HDR_LEN;
|
2013-10-30 00:37:21 +08:00
|
|
|
u_int caplen = h->caplen;
|
|
|
|
u_int length = h->len;
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2020-05-04 22:14:14 +08:00
|
|
|
ndo->ndo_protocol = "nflog";
|
2024-02-23 09:43:48 +08:00
|
|
|
ND_ICHECK_ZU(length, <, NFLOG_HDR_LEN);
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2018-02-08 20:35:20 +08:00
|
|
|
ND_TCHECK_SIZE(hdr);
|
2024-02-23 09:43:48 +08:00
|
|
|
ndo->ndo_ll_hdr_len += NFLOG_HDR_LEN;
|
|
|
|
|
2018-06-16 23:23:21 +08:00
|
|
|
if (GET_U_1(hdr->nflog_version) != 0) {
|
|
|
|
ND_PRINT("version %u (unknown)", GET_U_1(hdr->nflog_version));
|
2020-05-04 22:14:14 +08:00
|
|
|
return;
|
2013-10-31 22:58:42 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 17:26:38 +08:00
|
|
|
if (ndo->ndo_eflag)
|
2013-10-31 22:58:42 +08:00
|
|
|
nflog_hdr_print(ndo, hdr, length);
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2020-05-04 21:23:34 +08:00
|
|
|
p += NFLOG_HDR_LEN;
|
|
|
|
length -= NFLOG_HDR_LEN;
|
|
|
|
caplen -= NFLOG_HDR_LEN;
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2024-02-27 02:41:37 +08:00
|
|
|
while (length != 0) {
|
2018-02-08 20:35:20 +08:00
|
|
|
const nflog_tlv_t *tlv;
|
|
|
|
|
2024-02-23 09:43:48 +08:00
|
|
|
ND_ICHECK_ZU(length, <, NFLOG_TLV_LEN);
|
2013-08-06 17:26:38 +08:00
|
|
|
tlv = (const nflog_tlv_t *) p;
|
2024-02-23 09:43:48 +08:00
|
|
|
/* Do we have enough data for the TLV header? */
|
2018-02-08 20:35:20 +08:00
|
|
|
ND_TCHECK_SIZE(tlv);
|
2018-06-16 23:23:21 +08:00
|
|
|
size = GET_HE_U_2(tlv->tlv_length);
|
2013-08-06 17:26:38 +08:00
|
|
|
if (size % 4 != 0)
|
|
|
|
size += 4 - size % 4;
|
|
|
|
|
2014-02-03 18:14:24 +08:00
|
|
|
/* Is the TLV's length less than the minimum? */
|
2024-02-23 09:43:48 +08:00
|
|
|
ND_ICHECK_ZU(size, <, NFLOG_TLV_LEN);
|
|
|
|
ND_ICHECK_U(length, <, size);
|
2013-10-30 00:37:21 +08:00
|
|
|
|
2014-02-03 18:14:24 +08:00
|
|
|
/* Do we have enough data for the full TLV? */
|
2024-02-23 09:43:48 +08:00
|
|
|
ND_TCHECK_LEN(tlv, size);
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2018-06-16 23:23:21 +08:00
|
|
|
if (GET_HE_U_2(tlv->tlv_type) == NFULA_PAYLOAD) {
|
2014-02-03 18:14:24 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-05-04 21:23:34 +08:00
|
|
|
p += NFLOG_TLV_LEN;
|
|
|
|
h_size += NFLOG_TLV_LEN;
|
|
|
|
length -= NFLOG_TLV_LEN;
|
|
|
|
caplen -= NFLOG_TLV_LEN;
|
2014-02-03 18:14:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2014-02-03 18:14:24 +08:00
|
|
|
p += size;
|
|
|
|
h_size += size;
|
|
|
|
length -= size;
|
|
|
|
caplen -= size;
|
|
|
|
}
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2018-06-16 23:23:21 +08:00
|
|
|
switch (GET_U_1(hdr->nflog_family)) {
|
2013-08-06 17:26:38 +08:00
|
|
|
|
2023-02-12 03:01:01 +08:00
|
|
|
case NFLOG_AF_INET:
|
2014-02-03 12:14:26 +08:00
|
|
|
ip_print(ndo, p, length);
|
2013-08-06 17:26:38 +08:00
|
|
|
break;
|
|
|
|
|
2023-02-12 03:01:01 +08:00
|
|
|
case NFLOG_AF_INET6:
|
2013-08-06 17:26:38 +08:00
|
|
|
ip6_print(ndo, p, length);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!ndo->ndo_eflag)
|
2013-10-31 22:58:42 +08:00
|
|
|
nflog_hdr_print(ndo, hdr,
|
2020-05-04 21:23:34 +08:00
|
|
|
length + NFLOG_HDR_LEN);
|
2013-08-06 17:26:38 +08:00
|
|
|
|
|
|
|
if (!ndo->ndo_suppress_default_print)
|
2014-03-26 22:52:40 +08:00
|
|
|
ND_DEFAULTPRINT(p, caplen);
|
2013-08-06 17:26:38 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-05-31 18:16:13 +08:00
|
|
|
|
2020-07-14 23:32:55 +08:00
|
|
|
ndo->ndo_ll_hdr_len += h_size - NFLOG_HDR_LEN;
|
2020-05-04 22:14:14 +08:00
|
|
|
return;
|
2024-02-23 09:43:48 +08:00
|
|
|
|
|
|
|
invalid:
|
|
|
|
nd_print_invalid(ndo);
|
2013-05-31 18:16:13 +08:00
|
|
|
}
|
|
|
|
|
2018-05-25 05:13:48 +08:00
|
|
|
#endif /* DLT_NFLOG */
|