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