]> The Tcpdump Group git mirrors - tcpdump/blob - print-nflog.c
Clean up whitespaces/indentation
[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 #include "extract.h"
38
39 #ifdef DLT_NFLOG
40
41 /*
42 * Structure of an NFLOG header and TLV parts, as described at
43 * https://www.tcpdump.org/linktypes/LINKTYPE_NFLOG.html
44 *
45 * The NFLOG header is big-endian.
46 *
47 * The TLV length and type are in host byte order. The value is either
48 * big-endian or is an array of bytes in some externally-specified byte
49 * order (text string, link-layer address, link-layer header, packet
50 * data, etc.).
51 */
52 typedef struct nflog_hdr {
53 nd_uint8_t nflog_family; /* address family */
54 nd_uint8_t nflog_version; /* version */
55 nd_uint16_t nflog_rid; /* resource ID */
56 } nflog_hdr_t;
57
58 typedef struct nflog_tlv {
59 nd_uint16_t tlv_length; /* tlv length */
60 nd_uint16_t tlv_type; /* tlv type */
61 /* value follows this */
62 } nflog_tlv_t;
63
64 typedef struct nflog_packet_hdr {
65 nd_uint16_t hw_protocol; /* hw protocol */
66 nd_uint8_t hook; /* netfilter hook */
67 nd_byte pad[1]; /* padding to 32 bits */
68 } nflog_packet_hdr_t;
69
70 typedef struct nflog_hwaddr {
71 nd_uint16_t hw_addrlen; /* address length */
72 nd_byte pad[2]; /* padding to 32-bit boundary */
73 nd_byte hw_addr[8]; /* address, up to 8 bytes */
74 } nflog_hwaddr_t;
75
76 typedef struct nflog_timestamp {
77 nd_uint64_t sec;
78 nd_uint64_t usec;
79 } nflog_timestamp_t;
80
81 /*
82 * TLV types.
83 */
84 #define NFULA_PACKET_HDR 1 /* nflog_packet_hdr_t */
85 #define NFULA_MARK 2 /* packet mark from skbuff */
86 #define NFULA_TIMESTAMP 3 /* nflog_timestamp_t for skbuff's time stamp */
87 #define NFULA_IFINDEX_INDEV 4 /* ifindex of device on which packet received (possibly bridge group) */
88 #define NFULA_IFINDEX_OUTDEV 5 /* ifindex of device on which packet transmitted (possibly bridge group) */
89 #define NFULA_IFINDEX_PHYSINDEV 6 /* ifindex of physical device on which packet received (not bridge group) */
90 #define NFULA_IFINDEX_PHYSOUTDEV 7 /* ifindex of physical device on which packet transmitted (not bridge group) */
91 #define NFULA_HWADDR 8 /* nflog_hwaddr_t for hardware address */
92 #define NFULA_PAYLOAD 9 /* packet payload */
93 #define NFULA_PREFIX 10 /* text string - null-terminated, count includes NUL */
94 #define NFULA_UID 11 /* UID owning socket on which packet was sent/received */
95 #define NFULA_SEQ 12 /* sequence number of packets on this NFLOG socket */
96 #define NFULA_SEQ_GLOBAL 13 /* sequence number of pakets on all NFLOG sockets */
97 #define NFULA_GID 14 /* GID owning socket on which packet was sent/received */
98 #define NFULA_HWTYPE 15 /* ARPHRD_ type of skbuff's device */
99 #define NFULA_HWHEADER 16 /* skbuff's MAC-layer header */
100 #define NFULA_HWLEN 17 /* length of skbuff's MAC-layer header */
101
102 static const struct tok nflog_values[] = {
103 { AF_INET, "IPv4" },
104 #ifdef AF_INET6
105 { AF_INET6, "IPv6" },
106 #endif /*AF_INET6*/
107 { 0, NULL }
108 };
109
110 static void
111 nflog_hdr_print(netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length)
112 {
113 ND_PRINT("version %u, resource ID %u",
114 EXTRACT_U_1(hdr->nflog_version), EXTRACT_BE_U_2(hdr->nflog_rid));
115
116 if (!ndo->ndo_qflag) {
117 ND_PRINT(", family %s (%u)",
118 tok2str(nflog_values, "Unknown",
119 EXTRACT_U_1(hdr->nflog_family)),
120 EXTRACT_U_1(hdr->nflog_family));
121 } else {
122 ND_PRINT(", %s",
123 tok2str(nflog_values,
124 "Unknown NFLOG (0x%02x)",
125 EXTRACT_U_1(hdr->nflog_family)));
126 }
127
128 ND_PRINT(", length %u: ", length);
129 }
130
131 u_int
132 nflog_if_print(netdissect_options *ndo,
133 const struct pcap_pkthdr *h, const u_char *p)
134 {
135 const nflog_hdr_t *hdr = (const nflog_hdr_t *)p;
136 uint16_t size;
137 uint16_t h_size = sizeof(nflog_hdr_t);
138 u_int caplen = h->caplen;
139 u_int length = h->len;
140
141 ndo->ndo_protocol = "nflog_if";
142 if (caplen < sizeof(nflog_hdr_t))
143 goto trunc;
144
145 ND_TCHECK_SIZE(hdr);
146 if (EXTRACT_U_1(hdr->nflog_version) != 0) {
147 ND_PRINT("version %u (unknown)", EXTRACT_U_1(hdr->nflog_version));
148 return h_size;
149 }
150
151 if (ndo->ndo_eflag)
152 nflog_hdr_print(ndo, hdr, length);
153
154 p += sizeof(nflog_hdr_t);
155 length -= sizeof(nflog_hdr_t);
156 caplen -= sizeof(nflog_hdr_t);
157
158 while (length > 0) {
159 const nflog_tlv_t *tlv;
160
161 /* We have some data. Do we have enough for the TLV header? */
162 if (caplen < sizeof(nflog_tlv_t))
163 goto trunc; /* No. */
164
165 tlv = (const nflog_tlv_t *) p;
166 ND_TCHECK_SIZE(tlv);
167 size = EXTRACT_HE_U_2(tlv->tlv_length);
168 if (size % 4 != 0)
169 size += 4 - size % 4;
170
171 /* Is the TLV's length less than the minimum? */
172 if (size < sizeof(nflog_tlv_t))
173 goto trunc; /* Yes. Give up now. */
174
175 /* Do we have enough data for the full TLV? */
176 if (caplen < size)
177 goto trunc; /* No. */
178
179 if (EXTRACT_HE_U_2(tlv->tlv_type) == NFULA_PAYLOAD) {
180 /*
181 * This TLV's data is the packet payload.
182 * Skip past the TLV header, and break out
183 * of the loop so we print the packet data.
184 */
185 p += sizeof(nflog_tlv_t);
186 h_size += sizeof(nflog_tlv_t);
187 length -= sizeof(nflog_tlv_t);
188 caplen -= sizeof(nflog_tlv_t);
189 break;
190 }
191
192 p += size;
193 h_size += size;
194 length -= size;
195 caplen -= size;
196 }
197
198 switch (EXTRACT_U_1(hdr->nflog_family)) {
199
200 case AF_INET:
201 ip_print(ndo, p, length);
202 break;
203
204 #ifdef AF_INET6
205 case AF_INET6:
206 ip6_print(ndo, p, length);
207 break;
208 #endif /* AF_INET6 */
209
210 default:
211 if (!ndo->ndo_eflag)
212 nflog_hdr_print(ndo, hdr,
213 length + sizeof(nflog_hdr_t));
214
215 if (!ndo->ndo_suppress_default_print)
216 ND_DEFAULTPRINT(p, caplen);
217 break;
218 }
219
220 return h_size;
221 trunc:
222 nd_print_trunc(ndo);
223 return h_size;
224 }
225
226 #endif /* DLT_NFLOG */