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