]> The Tcpdump Group git mirrors - tcpdump/blob - print-nflog.c
8e7cd30bba43d538cd2fb8a883a43a9a5e2c142c
[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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <tcpdump-stdinc.h>
33
34 #include <stdio.h>
35 #include <pcap.h>
36
37 #include "netdissect.h"
38 #include "interface.h"
39
40 /*
41 * Structure of an NFLOG header and TLV parts, as described at
42 * http://www.tcpdump.org/linktypes/LINKTYPE_NFLOG.html
43 */
44 typedef struct nflog_hdr {
45 u_int8_t nflog_family; /* adress family */
46 u_int8_t nflog_version; /* version */
47 u_int16_t nflog_rid; /* resource ID */
48 } nflog_hdr_t;
49
50 typedef struct nflog_tlv {
51 u_int16_t tlv_length; /* tlv length */
52 u_int16_t tlv_type; /* tlv type */
53 void* tlv_value; /* tlv value */
54 } nflog_tlv_t;
55
56 #ifdef DLT_NFLOG
57
58 #define NFULA_PAYLOAD 9
59
60 static const struct tok nflog_values[] = {
61 { AF_INET, "IPv4" },
62 { AF_INET6, "IPv6" },
63 { 0, NULL }
64 };
65
66 static inline void
67 nflog_hdr_print(struct netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length)
68 {
69 ND_PRINT((ndo, "version %d, resource ID %d", hdr->nflog_version, ntohs(hdr->nflog_rid)));
70
71 if (!ndo->ndo_qflag) {
72 ND_PRINT((ndo,", family %s (%d)",
73 tok2str(nflog_values, "Unknown",
74 hdr->nflog_family),
75 hdr->nflog_family));
76 } else {
77 ND_PRINT((ndo,", %s",
78 tok2str(nflog_values,
79 "Unknown NFLOG (0x%02x)",
80 hdr->nflog_family)));
81 }
82
83 ND_PRINT((ndo, ", length %u: ", length));
84 }
85
86 u_int
87 nflog_if_print(struct netdissect_options *ndo,
88 const struct pcap_pkthdr *h, const u_char *p)
89 {
90 const nflog_hdr_t *hdr = (const nflog_hdr_t *)p;
91 const nflog_tlv_t *tlv;
92 u_int16_t size;
93 u_int16_t h_size = sizeof(nflog_hdr_t);
94 u_int caplen = h->caplen;
95 u_int length = h->len;
96
97 if (caplen < (int) sizeof(nflog_hdr_t) || length < (int) sizeof(nflog_hdr_t)) {
98 ND_PRINT((ndo, "[|nflog]"));
99 return h_size;
100 }
101
102 if (!(hdr->nflog_version) == 0) {
103 ND_PRINT((ndo, "version %u (unknown)", hdr->nflog_version));
104 return h_size;
105 }
106
107 if (ndo->ndo_eflag)
108 nflog_hdr_print(ndo, hdr, length);
109
110 length -= sizeof(nflog_hdr_t);
111 caplen -= sizeof(nflog_hdr_t);
112 p += sizeof(nflog_hdr_t);
113
114 do {
115 tlv = (const nflog_tlv_t *) p;
116 size = tlv->tlv_length;
117
118 if (size % 4 != 0)
119 size += 4 - size % 4;
120
121 h_size = h_size + size;
122
123 /* wrong size of the packet */
124 if (size > length || size == 0)
125 return h_size;
126
127 p += size;
128 length = length - size;
129 caplen = caplen - size;
130
131 } while (tlv->tlv_type != NFULA_PAYLOAD);
132
133 /* dont skip payload just tlv length and type */
134 p = p - size + 4;
135 length += size - 4;
136 caplen += size - 4;
137 h_size -= length;
138
139 switch (hdr->nflog_family) {
140
141 case AF_INET:
142 ip_print(ndo, p, length);
143 break;
144
145 #ifdef INET6
146 case AF_INET6:
147 ip6_print(ndo, p, length);
148 break;
149 #endif /*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 ndo->ndo_default_print(ndo, p, caplen);
158 break;
159 }
160
161 return h_size;
162 }
163
164 #endif /* DLT_NFLOG */