JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Petar Alilovic, |
| 3 | * Faculty of Electrical Engineering and Computing, University of Zagreb |
| 4 | * All rights reserved |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
| 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY |
| 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 25 | * DAMAGE. |
| 26 | */ |
| 27 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 28 | /* \summary: DLT_NFLOG printer */ |
| 29 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 30 | #ifdef HAVE_CONFIG_H |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 31 | #include <config.h> |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 32 | #endif |
| 33 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 34 | #include "netdissect-stdinc.h" |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 36 | #include "netdissect.h" |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 37 | #include "extract.h" |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 39 | #ifdef DLT_NFLOG |
| 40 | |
| 41 | /* |
| 42 | * Structure of an NFLOG header and TLV parts, as described at |
| 43 | * https://www.tcpdump.org/linktypes/LINKTYPE_NFLOG.html |
| 44 | * |
| 45 | * The NFLOG header is big-endian. |
| 46 | * |
| 47 | * The TLV length and type are in host byte order. The value is either |
| 48 | * big-endian or is an array of bytes in some externally-specified byte |
| 49 | * order (text string, link-layer address, link-layer header, packet |
| 50 | * data, etc.). |
| 51 | */ |
| 52 | typedef struct nflog_hdr { |
| 53 | nd_uint8_t nflog_family; /* address family */ |
| 54 | nd_uint8_t nflog_version; /* version */ |
| 55 | nd_uint16_t nflog_rid; /* resource ID */ |
| 56 | } nflog_hdr_t; |
| 57 | |
| 58 | #define NFLOG_HDR_LEN sizeof(nflog_hdr_t) |
| 59 | |
| 60 | typedef struct nflog_tlv { |
| 61 | nd_uint16_t tlv_length; /* tlv length */ |
| 62 | nd_uint16_t tlv_type; /* tlv type */ |
| 63 | /* value follows this */ |
| 64 | } nflog_tlv_t; |
| 65 | |
| 66 | #define NFLOG_TLV_LEN sizeof(nflog_tlv_t) |
| 67 | |
| 68 | typedef struct nflog_packet_hdr { |
| 69 | nd_uint16_t hw_protocol; /* hw protocol */ |
| 70 | nd_uint8_t hook; /* netfilter hook */ |
| 71 | nd_byte pad[1]; /* padding to 32 bits */ |
| 72 | } nflog_packet_hdr_t; |
| 73 | |
| 74 | typedef struct nflog_hwaddr { |
| 75 | nd_uint16_t hw_addrlen; /* address length */ |
| 76 | nd_byte pad[2]; /* padding to 32-bit boundary */ |
| 77 | nd_byte hw_addr[8]; /* address, up to 8 bytes */ |
| 78 | } nflog_hwaddr_t; |
| 79 | |
| 80 | typedef struct nflog_timestamp { |
| 81 | nd_uint64_t sec; |
| 82 | nd_uint64_t usec; |
| 83 | } nflog_timestamp_t; |
| 84 | |
| 85 | /* |
| 86 | * TLV types. |
| 87 | */ |
| 88 | #define NFULA_PACKET_HDR 1 /* nflog_packet_hdr_t */ |
| 89 | #define NFULA_MARK 2 /* packet mark from skbuff */ |
| 90 | #define NFULA_TIMESTAMP 3 /* nflog_timestamp_t for skbuff's time stamp */ |
| 91 | #define NFULA_IFINDEX_INDEV 4 /* ifindex of device on which packet received (possibly bridge group) */ |
| 92 | #define NFULA_IFINDEX_OUTDEV 5 /* ifindex of device on which packet transmitted (possibly bridge group) */ |
| 93 | #define NFULA_IFINDEX_PHYSINDEV 6 /* ifindex of physical device on which packet received (not bridge group) */ |
| 94 | #define NFULA_IFINDEX_PHYSOUTDEV 7 /* ifindex of physical device on which packet transmitted (not bridge group) */ |
| 95 | #define NFULA_HWADDR 8 /* nflog_hwaddr_t for hardware address */ |
| 96 | #define NFULA_PAYLOAD 9 /* packet payload */ |
| 97 | #define NFULA_PREFIX 10 /* text string - null-terminated, count includes NUL */ |
| 98 | #define NFULA_UID 11 /* UID owning socket on which packet was sent/received */ |
| 99 | #define NFULA_SEQ 12 /* sequence number of packets on this NFLOG socket */ |
| 100 | #define NFULA_SEQ_GLOBAL 13 /* sequence number of pakets on all NFLOG sockets */ |
| 101 | #define NFULA_GID 14 /* GID owning socket on which packet was sent/received */ |
| 102 | #define NFULA_HWTYPE 15 /* ARPHRD_ type of skbuff's device */ |
| 103 | #define NFULA_HWHEADER 16 /* skbuff's MAC-layer header */ |
| 104 | #define NFULA_HWLEN 17 /* length of skbuff's MAC-layer header */ |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 105 | |
| 106 | static const struct tok nflog_values[] = { |
| 107 | { AF_INET, "IPv4" }, |
| 108 | { AF_INET6, "IPv6" }, |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 109 | { 0, NULL } |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 112 | static void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 113 | nflog_hdr_print(netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 114 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 115 | ND_PRINT("version %u, resource ID %u", |
| 116 | GET_U_1(hdr->nflog_version), GET_BE_U_2(hdr->nflog_rid)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 117 | |
| 118 | if (!ndo->ndo_qflag) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 119 | ND_PRINT(", family %s (%u)", |
| 120 | tok2str(nflog_values, "Unknown", |
| 121 | GET_U_1(hdr->nflog_family)), |
| 122 | GET_U_1(hdr->nflog_family)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 123 | } else { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 124 | ND_PRINT(", %s", |
| 125 | tok2str(nflog_values, |
| 126 | "Unknown NFLOG (0x%02x)", |
| 127 | GET_U_1(hdr->nflog_family))); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 130 | ND_PRINT(", length %u: ", length); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 133 | void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 134 | nflog_if_print(netdissect_options *ndo, |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 135 | const struct pcap_pkthdr *h, const u_char *p) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 136 | { |
| 137 | const nflog_hdr_t *hdr = (const nflog_hdr_t *)p; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 138 | uint16_t size; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 139 | uint16_t h_size = NFLOG_HDR_LEN; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 140 | u_int caplen = h->caplen; |
| 141 | u_int length = h->len; |
| 142 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 143 | ndo->ndo_protocol = "nflog"; |
| 144 | if (caplen < NFLOG_HDR_LEN) { |
| 145 | nd_print_trunc(ndo); |
| 146 | ndo->ndo_ll_hdr_len += caplen; |
| 147 | return; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 148 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 149 | ndo->ndo_ll_hdr_len += NFLOG_HDR_LEN; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 150 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 151 | ND_TCHECK_SIZE(hdr); |
| 152 | if (GET_U_1(hdr->nflog_version) != 0) { |
| 153 | ND_PRINT("version %u (unknown)", GET_U_1(hdr->nflog_version)); |
| 154 | return; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (ndo->ndo_eflag) |
| 158 | nflog_hdr_print(ndo, hdr, length); |
| 159 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 160 | p += NFLOG_HDR_LEN; |
| 161 | length -= NFLOG_HDR_LEN; |
| 162 | caplen -= NFLOG_HDR_LEN; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 163 | |
| 164 | while (length > 0) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 165 | const nflog_tlv_t *tlv; |
| 166 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 167 | /* We have some data. Do we have enough for the TLV header? */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 168 | if (caplen < NFLOG_TLV_LEN) |
| 169 | goto trunc; /* No. */ |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 170 | |
| 171 | tlv = (const nflog_tlv_t *) p; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 172 | ND_TCHECK_SIZE(tlv); |
| 173 | size = GET_HE_U_2(tlv->tlv_length); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 174 | if (size % 4 != 0) |
| 175 | size += 4 - size % 4; |
| 176 | |
| 177 | /* Is the TLV's length less than the minimum? */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 178 | if (size < NFLOG_TLV_LEN) |
| 179 | goto trunc; /* Yes. Give up now. */ |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 180 | |
| 181 | /* Do we have enough data for the full TLV? */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 182 | if (caplen < size) |
| 183 | goto trunc; /* No. */ |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 184 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 185 | if (GET_HE_U_2(tlv->tlv_type) == NFULA_PAYLOAD) { |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 186 | /* |
| 187 | * This TLV's data is the packet payload. |
| 188 | * Skip past the TLV header, and break out |
| 189 | * of the loop so we print the packet data. |
| 190 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 191 | p += NFLOG_TLV_LEN; |
| 192 | h_size += NFLOG_TLV_LEN; |
| 193 | length -= NFLOG_TLV_LEN; |
| 194 | caplen -= NFLOG_TLV_LEN; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | |
| 198 | p += size; |
| 199 | h_size += size; |
| 200 | length -= size; |
| 201 | caplen -= size; |
| 202 | } |
| 203 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 204 | switch (GET_U_1(hdr->nflog_family)) { |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 205 | |
| 206 | case AF_INET: |
| 207 | ip_print(ndo, p, length); |
| 208 | break; |
| 209 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 210 | case AF_INET6: |
| 211 | ip6_print(ndo, p, length); |
| 212 | break; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 213 | |
| 214 | default: |
| 215 | if (!ndo->ndo_eflag) |
| 216 | nflog_hdr_print(ndo, hdr, |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 217 | length + NFLOG_HDR_LEN); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 218 | |
| 219 | if (!ndo->ndo_suppress_default_print) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 220 | ND_DEFAULTPRINT(p, caplen); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 221 | break; |
| 222 | } |
| 223 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 224 | ndo->ndo_ll_hdr_len += h_size - NFLOG_HDR_LEN; |
| 225 | return; |
| 226 | trunc: |
| 227 | nd_print_trunc(ndo); |
| 228 | ndo->ndo_ll_hdr_len += h_size - NFLOG_HDR_LEN; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 231 | #endif /* DLT_NFLOG */ |