print-nflog.c revision 1.3
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.3 2017/02/05 04:05:05 spz 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 43#if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H) 44#include <pcap/nflog.h> 45 46static const struct tok nflog_values[] = { 47 { AF_INET, "IPv4" }, 48#ifdef AF_INET6 49 { AF_INET6, "IPv6" }, 50#endif /*AF_INET6*/ 51 { 0, NULL } 52}; 53 54static inline void 55nflog_hdr_print(netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length) 56{ 57 ND_PRINT((ndo, "version %d, resource ID %d", hdr->nflog_version, ntohs(hdr->nflog_rid))); 58 59 if (!ndo->ndo_qflag) { 60 ND_PRINT((ndo,", family %s (%d)", 61 tok2str(nflog_values, "Unknown", 62 hdr->nflog_family), 63 hdr->nflog_family)); 64 } else { 65 ND_PRINT((ndo,", %s", 66 tok2str(nflog_values, 67 "Unknown NFLOG (0x%02x)", 68 hdr->nflog_family))); 69 } 70 71 ND_PRINT((ndo, ", length %u: ", length)); 72} 73 74u_int 75nflog_if_print(netdissect_options *ndo, 76 const struct pcap_pkthdr *h, const u_char *p) 77{ 78 const nflog_hdr_t *hdr = (const nflog_hdr_t *)p; 79 const nflog_tlv_t *tlv; 80 uint16_t size; 81 uint16_t h_size = sizeof(nflog_hdr_t); 82 u_int caplen = h->caplen; 83 u_int length = h->len; 84 85 if (caplen < (int) sizeof(nflog_hdr_t) || length < (int) sizeof(nflog_hdr_t)) { 86 ND_PRINT((ndo, "[|nflog]")); 87 return h_size; 88 } 89 90 if (hdr->nflog_version != 0) { 91 ND_PRINT((ndo, "version %u (unknown)", hdr->nflog_version)); 92 return h_size; 93 } 94 95 if (ndo->ndo_eflag) 96 nflog_hdr_print(ndo, hdr, length); 97 98 p += sizeof(nflog_hdr_t); 99 length -= sizeof(nflog_hdr_t); 100 caplen -= sizeof(nflog_hdr_t); 101 102 while (length > 0) { 103 /* We have some data. Do we have enough for the TLV header? */ 104 if (caplen < sizeof(nflog_tlv_t) || length < sizeof(nflog_tlv_t)) { 105 /* No. */ 106 ND_PRINT((ndo, "[|nflog]")); 107 return h_size; 108 } 109 110 tlv = (const nflog_tlv_t *) p; 111 size = tlv->tlv_length; 112 if (size % 4 != 0) 113 size += 4 - size % 4; 114 115 /* Is the TLV's length less than the minimum? */ 116 if (size < sizeof(nflog_tlv_t)) { 117 /* Yes. Give up now. */ 118 ND_PRINT((ndo, "[|nflog]")); 119 return h_size; 120 } 121 122 /* Do we have enough data for the full TLV? */ 123 if (caplen < size || length < size) { 124 /* No. */ 125 ND_PRINT((ndo, "[|nflog]")); 126 return h_size; 127 } 128 129 if (tlv->tlv_type == NFULA_PAYLOAD) { 130 /* 131 * This TLV's data is the packet payload. 132 * Skip past the TLV header, and break out 133 * of the loop so we print the packet data. 134 */ 135 p += sizeof(nflog_tlv_t); 136 h_size += sizeof(nflog_tlv_t); 137 length -= sizeof(nflog_tlv_t); 138 caplen -= sizeof(nflog_tlv_t); 139 break; 140 } 141 142 p += size; 143 h_size += size; 144 length -= size; 145 caplen -= size; 146 } 147 148 switch (hdr->nflog_family) { 149 150 case AF_INET: 151 ip_print(ndo, p, length); 152 break; 153 154#ifdef AF_INET6 155 case AF_INET6: 156 ip6_print(ndo, p, length); 157 break; 158#endif /* AF_INET6 */ 159 160 default: 161 if (!ndo->ndo_eflag) 162 nflog_hdr_print(ndo, hdr, 163 length + sizeof(nflog_hdr_t)); 164 165 if (!ndo->ndo_suppress_default_print) 166 ND_DEFAULTPRINT(p, caplen); 167 break; 168 } 169 170 return h_size; 171} 172 173#endif /* defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H) */ 174