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