2 * Copyright 2011,2012 Jakub Zawadzki <darkjames-ws@darkjames.pl>
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #define NEW_PROTO_TREE_API
29 #include <epan/packet.h>
30 #include <epan/prefs.h>
31 #include <epan/aftypes.h>
32 #include <wiretap/wtap.h>
34 void proto_register_nflog(void);
35 void proto_reg_handoff_nflog(void);
37 /* nfulnl_attr_type enum from <linux/netfilter/nfnetlink_log.h> */
38 enum ws_nfulnl_attr_type {
41 WS_NFULA_MARK, /* __u32 nfmark */
42 WS_NFULA_TIMESTAMP, /* nfulnl_msg_packet_timestamp */
43 WS_NFULA_IFINDEX_INDEV, /* __u32 ifindex */
44 WS_NFULA_IFINDEX_OUTDEV, /* __u32 ifindex */
45 WS_NFULA_IFINDEX_PHYSINDEV, /* __u32 ifindex */
46 WS_NFULA_IFINDEX_PHYSOUTDEV, /* __u32 ifindex */
47 WS_NFULA_HWADDR, /* nfulnl_msg_packet_hw */
48 WS_NFULA_PAYLOAD, /* opaque data payload */
49 WS_NFULA_PREFIX, /* string prefix */
50 WS_NFULA_UID, /* user id of socket */
51 WS_NFULA_SEQ, /* instance-local sequence number */
52 WS_NFULA_SEQ_GLOBAL, /* global sequence number */
53 WS_NFULA_GID, /* group id of socket */
54 WS_NFULA_HWTYPE, /* hardware type */
55 WS_NFULA_HWHEADER, /* hardware header */
56 WS_NFULA_HWLEN /* hardware header length */
59 static const value_string nflog_tlv_vals[] = {
60 { WS_NFULA_UNSPEC, "NFULA_UNSPEC" },
61 { WS_NFULA_PACKET_HDR, "NFULA_PACKET_HDR" },
62 { WS_NFULA_MARK, "NFULA_MARK" },
63 { WS_NFULA_TIMESTAMP, "NFULA_TIMESTAMP" },
64 { WS_NFULA_IFINDEX_INDEV, "NFULA_IFINDEX_INDEV" },
65 { WS_NFULA_IFINDEX_OUTDEV, "NFULA_IFINDEX_OUTDEV" },
66 { WS_NFULA_IFINDEX_PHYSINDEV, "NFULA_IFINDEX_PHYSINDEV" },
67 { WS_NFULA_IFINDEX_PHYSOUTDEV, "NFULA_IFINDEX_PHYSOUTDEV" },
68 { WS_NFULA_HWADDR, "NFULA_HWADDR" },
69 { WS_NFULA_PAYLOAD, "NFULA_PAYLOAD" },
70 { WS_NFULA_PREFIX, "NFULA_PREFIX" },
71 { WS_NFULA_UID, "NFULA_UID" },
72 { WS_NFULA_SEQ, "NFULA_SEQ" },
73 { WS_NFULA_SEQ_GLOBAL, "NFULA_SEQ_GLOBAL" },
74 { WS_NFULA_GID, "NFULA_GID" },
75 { WS_NFULA_HWTYPE, "NFULA_HWTYPE" },
76 { WS_NFULA_HWHEADER, "NFULA_HWHEADER" },
77 { WS_NFULA_HWLEN, "NFULA_HWLEN" },
81 static int ett_nflog = -1;
82 static int ett_nflog_tlv = -1;
84 static header_field_info *hfi_nflog = NULL;
86 #define NFLOG_HFI_INIT HFI_INIT(proto_nflog)
89 static header_field_info hfi_nflog_family NFLOG_HFI_INIT =
90 { "Family", "nflog.family", FT_UINT8, BASE_DEC | BASE_EXT_STRING, &linux_af_vals_ext, 0x00, NULL, HFILL };
92 static header_field_info hfi_nflog_version NFLOG_HFI_INIT =
93 { "Version", "nflog.version", FT_UINT8, BASE_DEC, NULL, 0x00, NULL, HFILL };
95 static header_field_info hfi_nflog_resid NFLOG_HFI_INIT =
96 { "Resource id", "nflog.res_id", FT_UINT16, BASE_DEC, NULL, 0x00, NULL, HFILL };
99 static header_field_info hfi_nflog_tlv NFLOG_HFI_INIT =
100 { "TLV", "nflog.tlv", FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL };
102 static header_field_info hfi_nflog_tlv_length NFLOG_HFI_INIT =
103 { "Length", "nflog.tlv_length", FT_UINT16, BASE_DEC, NULL, 0x00, "TLV Length", HFILL };
105 static header_field_info hfi_nflog_tlv_type NFLOG_HFI_INIT =
106 { "Type", "nflog.tlv_type", FT_UINT16, BASE_DEC, VALS(nflog_tlv_vals), 0x7fff, "TLV Type", HFILL };
109 static header_field_info hfi_nflog_tlv_prefix NFLOG_HFI_INIT =
110 { "Prefix", "nflog.prefix", FT_STRINGZ, BASE_NONE, NULL, 0x00, "TLV Prefix Value", HFILL };
112 static header_field_info hfi_nflog_tlv_uid NFLOG_HFI_INIT =
113 { "UID", "nflog.uid", FT_INT32, BASE_DEC, NULL, 0x00, "TLV UID Value", HFILL };
115 static header_field_info hfi_nflog_tlv_gid NFLOG_HFI_INIT =
116 { "GID", "nflog.gid", FT_INT32, BASE_DEC, NULL, 0x00, "TLV GID Value", HFILL };
118 static header_field_info hfi_nflog_tlv_timestamp NFLOG_HFI_INIT =
119 { "Timestamp", "nflog.timestamp", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00, "TLV Timestamp Value", HFILL };
121 static header_field_info hfi_nflog_tlv_unknown NFLOG_HFI_INIT =
122 { "Value", "nflog.tlv_value", FT_BYTES, BASE_NONE, NULL, 0x00, "TLV Value", HFILL };
124 static dissector_handle_t ip_handle;
125 static dissector_handle_t ip6_handle;
126 static dissector_handle_t data_handle;
129 dissect_nflog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
131 const int start_tlv_offset = 4;
133 proto_tree *nflog_tree = NULL;
138 tvbuff_t *next_tvb = NULL;
141 col_set_str(pinfo->cinfo, COL_PROTOCOL, "NFLOG");
142 col_clear(pinfo->cinfo, COL_INFO);
144 aftype = tvb_get_guint8(tvb, 0);
147 if (proto_field_is_referenced(tree, hfi_nflog->id)) {
148 ti = proto_tree_add_item(tree, hfi_nflog, tvb, 0, -1, ENC_NA);
149 nflog_tree = proto_item_add_subtree(ti, ett_nflog);
151 proto_tree_add_item(nflog_tree, &hfi_nflog_family, tvb, offset, 1, ENC_NA);
154 proto_tree_add_item(nflog_tree, &hfi_nflog_version, tvb, offset, 1, ENC_NA);
157 proto_tree_add_item(nflog_tree, &hfi_nflog_resid, tvb, offset, 2, ENC_BIG_ENDIAN);
161 offset = start_tlv_offset;
163 while (tvb_reported_length_remaining(tvb, offset) >= 4) {
164 guint16 tlv_len = tvb_get_h_guint16(tvb, offset + 0);
168 proto_tree *tlv_tree;
174 value_len = tlv_len - 4;
175 tlv_type = (tvb_get_h_guint16(tvb, offset + 2) & 0x7fff);
178 gboolean handled = FALSE;
180 ti = proto_tree_add_bytes_format(nflog_tree, hfi_nflog_tlv.id,
181 tvb, offset, tlv_len, NULL,
182 "TLV Type: %s (%u), Length: %u",
183 val_to_str_const(tlv_type, nflog_tlv_vals, "Unknown"),
185 tlv_tree = proto_item_add_subtree(ti, ett_nflog_tlv);
187 proto_tree_add_item(tlv_tree, &hfi_nflog_tlv_length, tvb, offset + 0, 2, ENC_HOST_ENDIAN);
188 proto_tree_add_item(tlv_tree, &hfi_nflog_tlv_type, tvb, offset + 2, 2, ENC_HOST_ENDIAN);
190 case WS_NFULA_PAYLOAD:
194 case WS_NFULA_PREFIX:
195 if (value_len >= 1) {
196 proto_tree_add_item(tlv_tree, &hfi_nflog_tlv_prefix,
197 tvb, offset + 4, value_len, ENC_NA);
203 if (value_len == 4) {
204 proto_tree_add_item(tlv_tree, &hfi_nflog_tlv_uid,
205 tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
211 if (value_len == 4) {
212 proto_tree_add_item(tlv_tree, &hfi_nflog_tlv_gid,
213 tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
218 case WS_NFULA_TIMESTAMP:
219 if (value_len == 16) {
222 ts.secs = (time_t)tvb_get_ntoh64(tvb, offset + 4);
223 /* XXX - add an "expert info" warning if this is >= 10^9? */
224 ts.nsecs = (int)tvb_get_ntoh64(tvb, offset + 12);
225 proto_tree_add_time(tlv_tree, &hfi_nflog_tlv_timestamp,
226 tvb, offset + 4, value_len, &ts);
233 proto_tree_add_item(tlv_tree, &hfi_nflog_tlv_unknown,
234 tvb, offset + 4, value_len, ENC_NA);
237 if (tlv_type == WS_NFULA_PAYLOAD)
238 next_tvb = tvb_new_subset_length(tvb, offset + 4, value_len);
240 offset += ((tlv_len + 3) & ~3); /* next TLV aligned to 4B */
246 call_dissector(ip_handle, next_tvb, pinfo, tree);
249 call_dissector(ip6_handle, next_tvb, pinfo, tree);
252 call_dissector(data_handle, next_tvb, pinfo, tree);
259 proto_register_nflog(void)
261 #ifndef HAVE_HFI_SECTION_INIT
262 static header_field_info *hfi[] = {
269 &hfi_nflog_tlv_length,
272 &hfi_nflog_tlv_prefix,
275 &hfi_nflog_tlv_timestamp,
276 &hfi_nflog_tlv_unknown,
280 static gint *ett[] = {
287 proto_nflog = proto_register_protocol("Linux Netfilter NFLOG", "NFLOG", "nflog");
288 hfi_nflog = proto_registrar_get_nth(proto_nflog);
290 register_dissector("nflog", dissect_nflog, proto_nflog);
292 proto_register_fields(proto_nflog, hfi, array_length(hfi));
293 proto_register_subtree_array(ett, array_length(ett));
298 proto_reg_handoff_nflog(void)
300 dissector_handle_t nflog_handle;
302 ip_handle = find_dissector("ip");
303 ip6_handle = find_dissector("ipv6");
304 data_handle = find_dissector("data");
306 nflog_handle = find_dissector("nflog");
307 dissector_add_uint("wtap_encap", WTAP_ENCAP_NFLOG, nflog_handle);
316 * indent-tabs-mode: nil
319 * ex: set shiftwidth=4 tabstop=8 expandtab:
320 * :indentSize=4:tabSize=8:noTabs=true: