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 28#include <sys/cdefs.h> 29#ifndef lint 30__RCSID("$NetBSD: print-nflog.c,v 1.4 2023/08/17 20:19:40 christos Exp $"); 31#endif 32 33/* \summary: DLT_NFLOG printer */ 34 35#ifdef HAVE_CONFIG_H 36#include <config.h> 37#endif 38 39#include "netdissect-stdinc.h" 40 41#include "netdissect.h" 42#include "extract.h" 43 44#ifdef DLT_NFLOG 45 46/* 47 * Structure of an NFLOG header and TLV parts, as described at 48 * https://www.tcpdump.org/linktypes/LINKTYPE_NFLOG.html 49 * 50 * The NFLOG header is big-endian. 51 * 52 * The TLV length and type are in host byte order. The value is either 53 * big-endian or is an array of bytes in some externally-specified byte 54 * order (text string, link-layer address, link-layer header, packet 55 * data, etc.). 56 */ 57typedef struct nflog_hdr { 58 nd_uint8_t nflog_family; /* address family */ 59 nd_uint8_t nflog_version; /* version */ 60 nd_uint16_t nflog_rid; /* resource ID */ 61} nflog_hdr_t; 62 63#define NFLOG_HDR_LEN sizeof(nflog_hdr_t) 64 65typedef struct nflog_tlv { 66 nd_uint16_t tlv_length; /* tlv length */ 67 nd_uint16_t tlv_type; /* tlv type */ 68 /* value follows this */ 69} nflog_tlv_t; 70 71#define NFLOG_TLV_LEN sizeof(nflog_tlv_t) 72 73typedef struct nflog_packet_hdr { 74 nd_uint16_t hw_protocol; /* hw protocol */ 75 nd_uint8_t hook; /* netfilter hook */ 76 nd_byte pad[1]; /* padding to 32 bits */ 77} nflog_packet_hdr_t; 78 79typedef struct nflog_hwaddr { 80 nd_uint16_t hw_addrlen; /* address length */ 81 nd_byte pad[2]; /* padding to 32-bit boundary */ 82 nd_byte hw_addr[8]; /* address, up to 8 bytes */ 83} nflog_hwaddr_t; 84 85typedef struct nflog_timestamp { 86 nd_uint64_t sec; 87 nd_uint64_t usec; 88} nflog_timestamp_t; 89 90/* 91 * TLV types. 92 */ 93#define NFULA_PACKET_HDR 1 /* nflog_packet_hdr_t */ 94#define NFULA_MARK 2 /* packet mark from skbuff */ 95#define NFULA_TIMESTAMP 3 /* nflog_timestamp_t for skbuff's time stamp */ 96#define NFULA_IFINDEX_INDEV 4 /* ifindex of device on which packet received (possibly bridge group) */ 97#define NFULA_IFINDEX_OUTDEV 5 /* ifindex of device on which packet transmitted (possibly bridge group) */ 98#define NFULA_IFINDEX_PHYSINDEV 6 /* ifindex of physical device on which packet received (not bridge group) */ 99#define NFULA_IFINDEX_PHYSOUTDEV 7 /* ifindex of physical device on which packet transmitted (not bridge group) */ 100#define NFULA_HWADDR 8 /* nflog_hwaddr_t for hardware address */ 101#define NFULA_PAYLOAD 9 /* packet payload */ 102#define NFULA_PREFIX 10 /* text string - null-terminated, count includes NUL */ 103#define NFULA_UID 11 /* UID owning socket on which packet was sent/received */ 104#define NFULA_SEQ 12 /* sequence number of packets on this NFLOG socket */ 105#define NFULA_SEQ_GLOBAL 13 /* sequence number of pakets on all NFLOG sockets */ 106#define NFULA_GID 14 /* GID owning socket on which packet was sent/received */ 107#define NFULA_HWTYPE 15 /* ARPHRD_ type of skbuff's device */ 108#define NFULA_HWHEADER 16 /* skbuff's MAC-layer header */ 109#define NFULA_HWLEN 17 /* length of skbuff's MAC-layer header */ 110 111static const struct tok nflog_values[] = { 112 { AF_INET, "IPv4" }, 113 { AF_INET6, "IPv6" }, 114 { 0, NULL } 115}; 116 117static void 118nflog_hdr_print(netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length) 119{ 120 ND_PRINT("version %u, resource ID %u", 121 GET_U_1(hdr->nflog_version), GET_BE_U_2(hdr->nflog_rid)); 122 123 if (!ndo->ndo_qflag) { 124 ND_PRINT(", family %s (%u)", 125 tok2str(nflog_values, "Unknown", 126 GET_U_1(hdr->nflog_family)), 127 GET_U_1(hdr->nflog_family)); 128 } else { 129 ND_PRINT(", %s", 130 tok2str(nflog_values, 131 "Unknown NFLOG (0x%02x)", 132 GET_U_1(hdr->nflog_family))); 133 } 134 135 ND_PRINT(", length %u: ", length); 136} 137 138void 139nflog_if_print(netdissect_options *ndo, 140 const struct pcap_pkthdr *h, const u_char *p) 141{ 142 const nflog_hdr_t *hdr = (const nflog_hdr_t *)p; 143 uint16_t size; 144 uint16_t h_size = NFLOG_HDR_LEN; 145 u_int caplen = h->caplen; 146 u_int length = h->len; 147 148 ndo->ndo_protocol = "nflog"; 149 if (caplen < NFLOG_HDR_LEN) { 150 nd_print_trunc(ndo); 151 ndo->ndo_ll_hdr_len += caplen; 152 return; 153 } 154 ndo->ndo_ll_hdr_len += NFLOG_HDR_LEN; 155 156 ND_TCHECK_SIZE(hdr); 157 if (GET_U_1(hdr->nflog_version) != 0) { 158 ND_PRINT("version %u (unknown)", GET_U_1(hdr->nflog_version)); 159 return; 160 } 161 162 if (ndo->ndo_eflag) 163 nflog_hdr_print(ndo, hdr, length); 164 165 p += NFLOG_HDR_LEN; 166 length -= NFLOG_HDR_LEN; 167 caplen -= NFLOG_HDR_LEN; 168 169 while (length > 0) { 170 const nflog_tlv_t *tlv; 171 172 /* We have some data. Do we have enough for the TLV header? */ 173 if (caplen < NFLOG_TLV_LEN) 174 goto trunc; /* No. */ 175 176 tlv = (const nflog_tlv_t *) p; 177 ND_TCHECK_SIZE(tlv); 178 size = GET_HE_U_2(tlv->tlv_length); 179 if (size % 4 != 0) 180 size += 4 - size % 4; 181 182 /* Is the TLV's length less than the minimum? */ 183 if (size < NFLOG_TLV_LEN) 184 goto trunc; /* Yes. Give up now. */ 185 186 /* Do we have enough data for the full TLV? */ 187 if (caplen < size) 188 goto trunc; /* No. */ 189 190 if (GET_HE_U_2(tlv->tlv_type) == NFULA_PAYLOAD) { 191 /* 192 * This TLV's data is the packet payload. 193 * Skip past the TLV header, and break out 194 * of the loop so we print the packet data. 195 */ 196 p += NFLOG_TLV_LEN; 197 h_size += NFLOG_TLV_LEN; 198 length -= NFLOG_TLV_LEN; 199 caplen -= NFLOG_TLV_LEN; 200 break; 201 } 202 203 p += size; 204 h_size += size; 205 length -= size; 206 caplen -= size; 207 } 208 209 switch (GET_U_1(hdr->nflog_family)) { 210 211 case AF_INET: 212 ip_print(ndo, p, length); 213 break; 214 215 case AF_INET6: 216 ip6_print(ndo, p, length); 217 break; 218 219 default: 220 if (!ndo->ndo_eflag) 221 nflog_hdr_print(ndo, hdr, 222 length + NFLOG_HDR_LEN); 223 224 if (!ndo->ndo_suppress_default_print) 225 ND_DEFAULTPRINT(p, caplen); 226 break; 227 } 228 229 ndo->ndo_ll_hdr_len += h_size - NFLOG_HDR_LEN; 230 return; 231trunc: 232 nd_print_trunc(ndo); 233 ndo->ndo_ll_hdr_len += h_size - NFLOG_HDR_LEN; 234} 235 236#endif /* DLT_NFLOG */ 237