]> The Tcpdump Group git mirrors - tcpdump/blob - print-nflog.c
NFLOG: Use correct AF code points on all OSes.
[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 #define NFLOG_HDR_LEN sizeof(nflog_hdr_t)
59
60 typedef struct nflog_tlv {
61 nd_uint16_t tlv_length; /* tlv length */
62 nd_uint16_t tlv_type; /* tlv type */
63 /* value follows this */
64 } nflog_tlv_t;
65
66 #define NFLOG_TLV_LEN sizeof(nflog_tlv_t)
67
68 typedef struct nflog_packet_hdr {
69 nd_uint16_t hw_protocol; /* hw protocol */
70 nd_uint8_t hook; /* netfilter hook */
71 nd_byte pad[1]; /* padding to 32 bits */
72 } nflog_packet_hdr_t;
73
74 typedef struct nflog_hwaddr {
75 nd_uint16_t hw_addrlen; /* address length */
76 nd_byte pad[2]; /* padding to 32-bit boundary */
77 nd_byte hw_addr[8]; /* address, up to 8 bytes */
78 } nflog_hwaddr_t;
79
80 typedef struct nflog_timestamp {
81 nd_uint64_t sec;
82 nd_uint64_t usec;
83 } nflog_timestamp_t;
84
85 /*
86 * TLV types.
87 */
88 #define NFULA_PACKET_HDR 1 /* nflog_packet_hdr_t */
89 #define NFULA_MARK 2 /* packet mark from skbuff */
90 #define NFULA_TIMESTAMP 3 /* nflog_timestamp_t for skbuff's time stamp */
91 #define NFULA_IFINDEX_INDEV 4 /* ifindex of device on which packet received (possibly bridge group) */
92 #define NFULA_IFINDEX_OUTDEV 5 /* ifindex of device on which packet transmitted (possibly bridge group) */
93 #define NFULA_IFINDEX_PHYSINDEV 6 /* ifindex of physical device on which packet received (not bridge group) */
94 #define NFULA_IFINDEX_PHYSOUTDEV 7 /* ifindex of physical device on which packet transmitted (not bridge group) */
95 #define NFULA_HWADDR 8 /* nflog_hwaddr_t for hardware address */
96 #define NFULA_PAYLOAD 9 /* packet payload */
97 #define NFULA_PREFIX 10 /* text string - null-terminated, count includes NUL */
98 #define NFULA_UID 11 /* UID owning socket on which packet was sent/received */
99 #define NFULA_SEQ 12 /* sequence number of packets on this NFLOG socket */
100 #define NFULA_SEQ_GLOBAL 13 /* sequence number of pakets on all NFLOG sockets */
101 #define NFULA_GID 14 /* GID owning socket on which packet was sent/received */
102 #define NFULA_HWTYPE 15 /* ARPHRD_ type of skbuff's device */
103 #define NFULA_HWHEADER 16 /* skbuff's MAC-layer header */
104 #define NFULA_HWLEN 17 /* length of skbuff's MAC-layer header */
105
106 /*
107 * Define two constants specifically for the two AF code points from the
108 * LINKTYPE_NFLOG specification above and use these constants instead of
109 * AF_INET and AF_INET6. This is the only way to dissect the "wire" encoding
110 * correctly because some BSD systems define AF_INET6 differently from Linux
111 * (see af.h) and Haiku defines both AF_INET and AF_INET6 differently from
112 * Linux.
113 */
114 #define NFLOG_AF_INET 2
115 #define NFLOG_AF_INET6 10
116 static const struct tok nflog_values[] = {
117 { NFLOG_AF_INET, "IPv4" },
118 { NFLOG_AF_INET6, "IPv6" },
119 { 0, NULL }
120 };
121
122 static void
123 nflog_hdr_print(netdissect_options *ndo, const nflog_hdr_t *hdr, u_int length)
124 {
125 ND_PRINT("version %u, resource ID %u",
126 GET_U_1(hdr->nflog_version), GET_BE_U_2(hdr->nflog_rid));
127
128 if (!ndo->ndo_qflag) {
129 ND_PRINT(", family %s (%u)",
130 tok2str(nflog_values, "Unknown",
131 GET_U_1(hdr->nflog_family)),
132 GET_U_1(hdr->nflog_family));
133 } else {
134 ND_PRINT(", %s",
135 tok2str(nflog_values,
136 "Unknown NFLOG (0x%02x)",
137 GET_U_1(hdr->nflog_family)));
138 }
139
140 ND_PRINT(", length %u: ", length);
141 }
142
143 void
144 nflog_if_print(netdissect_options *ndo,
145 const struct pcap_pkthdr *h, const u_char *p)
146 {
147 const nflog_hdr_t *hdr = (const nflog_hdr_t *)p;
148 uint16_t size;
149 uint16_t h_size = NFLOG_HDR_LEN;
150 u_int caplen = h->caplen;
151 u_int length = h->len;
152
153 ndo->ndo_protocol = "nflog";
154 if (caplen < NFLOG_HDR_LEN) {
155 nd_print_trunc(ndo);
156 ndo->ndo_ll_hdr_len += caplen;
157 return;
158 }
159 ndo->ndo_ll_hdr_len += NFLOG_HDR_LEN;
160
161 ND_TCHECK_SIZE(hdr);
162 if (GET_U_1(hdr->nflog_version) != 0) {
163 ND_PRINT("version %u (unknown)", GET_U_1(hdr->nflog_version));
164 return;
165 }
166
167 if (ndo->ndo_eflag)
168 nflog_hdr_print(ndo, hdr, length);
169
170 p += NFLOG_HDR_LEN;
171 length -= NFLOG_HDR_LEN;
172 caplen -= NFLOG_HDR_LEN;
173
174 while (length > 0) {
175 const nflog_tlv_t *tlv;
176
177 /* We have some data. Do we have enough for the TLV header? */
178 if (caplen < NFLOG_TLV_LEN)
179 goto trunc; /* No. */
180
181 tlv = (const nflog_tlv_t *) p;
182 ND_TCHECK_SIZE(tlv);
183 size = GET_HE_U_2(tlv->tlv_length);
184 if (size % 4 != 0)
185 size += 4 - size % 4;
186
187 /* Is the TLV's length less than the minimum? */
188 if (size < NFLOG_TLV_LEN)
189 goto trunc; /* Yes. Give up now. */
190
191 /* Do we have enough data for the full TLV? */
192 if (caplen < size)
193 goto trunc; /* No. */
194
195 if (GET_HE_U_2(tlv->tlv_type) == NFULA_PAYLOAD) {
196 /*
197 * This TLV's data is the packet payload.
198 * Skip past the TLV header, and break out
199 * of the loop so we print the packet data.
200 */
201 p += NFLOG_TLV_LEN;
202 h_size += NFLOG_TLV_LEN;
203 length -= NFLOG_TLV_LEN;
204 caplen -= NFLOG_TLV_LEN;
205 break;
206 }
207
208 p += size;
209 h_size += size;
210 length -= size;
211 caplen -= size;
212 }
213
214 switch (GET_U_1(hdr->nflog_family)) {
215
216 case NFLOG_AF_INET:
217 ip_print(ndo, p, length);
218 break;
219
220 case NFLOG_AF_INET6:
221 ip6_print(ndo, p, length);
222 break;
223
224 default:
225 if (!ndo->ndo_eflag)
226 nflog_hdr_print(ndo, hdr,
227 length + NFLOG_HDR_LEN);
228
229 if (!ndo->ndo_suppress_default_print)
230 ND_DEFAULTPRINT(p, caplen);
231 break;
232 }
233
234 ndo->ndo_ll_hdr_len += h_size - NFLOG_HDR_LEN;
235 return;
236 trunc:
237 nd_print_trunc(ndo);
238 ndo->ndo_ll_hdr_len += h_size - NFLOG_HDR_LEN;
239 }
240
241 #endif /* DLT_NFLOG */