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