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