Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/wireshark/wip.git] / epan / dissectors / packet-nflog.c
1 /* packet-nflog.c
2  * Copyright 2011,2012 Jakub Zawadzki <darkjames-ws@darkjames.pl>
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #define NEW_PROTO_TREE_API
24
25 #include "config.h"
26
27 #include <glib.h>
28
29 #include <epan/packet.h>
30 #include <epan/prefs.h>
31 #include <epan/aftypes.h>
32 #include <wiretap/wtap.h>
33
34 void proto_register_nflog(void);
35 void proto_reg_handoff_nflog(void);
36
37 /* nfulnl_attr_type enum from <linux/netfilter/nfnetlink_log.h> */
38 enum ws_nfulnl_attr_type {
39     WS_NFULA_UNSPEC,
40     WS_NFULA_PACKET_HDR,
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 */
57 };
58
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" },
78     { 0, NULL }
79 };
80
81 static int ett_nflog = -1;
82 static int ett_nflog_tlv = -1;
83
84 static header_field_info *hfi_nflog = NULL;
85
86 #define NFLOG_HFI_INIT HFI_INIT(proto_nflog)
87
88 /* Header */
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 };
91
92 static header_field_info hfi_nflog_version NFLOG_HFI_INIT =
93     { "Version", "nflog.version", FT_UINT8, BASE_DEC, NULL, 0x00, NULL, HFILL };
94
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 };
97
98 /* TLV */
99 static header_field_info hfi_nflog_tlv NFLOG_HFI_INIT =
100     { "TLV", "nflog.tlv", FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL };
101
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 };
104
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 };
107
108 /* TLV values */
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 };
111
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 };
114
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 };
117
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 };
120
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 };
123
124 static dissector_handle_t ip_handle;
125 static dissector_handle_t ip6_handle;
126 static dissector_handle_t data_handle;
127
128 static void
129 dissect_nflog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
130 {
131     const int start_tlv_offset = 4;
132
133     proto_tree *nflog_tree = NULL;
134     proto_item *ti;
135
136     int offset = 0;
137
138     tvbuff_t *next_tvb = NULL;
139     int aftype;
140
141     col_set_str(pinfo->cinfo, COL_PROTOCOL, "NFLOG");
142     col_clear(pinfo->cinfo, COL_INFO);
143
144     aftype = tvb_get_guint8(tvb, 0);
145
146     /* Header */
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);
150
151         proto_tree_add_item(nflog_tree, &hfi_nflog_family, tvb, offset, 1, ENC_NA);
152         offset += 1;
153
154         proto_tree_add_item(nflog_tree, &hfi_nflog_version, tvb, offset, 1, ENC_NA);
155         offset += 1;
156
157         proto_tree_add_item(nflog_tree, &hfi_nflog_resid, tvb, offset, 2, ENC_BIG_ENDIAN);
158         /*offset += 2;*/
159     }
160
161     offset = start_tlv_offset;
162     /* TLVs */
163     while (tvb_reported_length_remaining(tvb, offset) >= 4) {
164         guint16 tlv_len = tvb_get_h_guint16(tvb, offset + 0);
165         guint16 tlv_type;
166         guint16 value_len;
167
168         proto_tree *tlv_tree;
169
170         /* malformed */
171         if (tlv_len < 4)
172             return;
173
174         value_len = tlv_len - 4;
175         tlv_type = (tvb_get_h_guint16(tvb, offset + 2) & 0x7fff);
176
177         if (nflog_tree) {
178             gboolean handled = FALSE;
179
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"),
184                              tlv_type, tlv_len);
185             tlv_tree = proto_item_add_subtree(ti, ett_nflog_tlv);
186
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);
189             switch (tlv_type) {
190                 case WS_NFULA_PAYLOAD:
191                     handled = TRUE;
192                     break;
193
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);
198                         handled = TRUE;
199                     }
200                     break;
201
202                 case WS_NFULA_UID:
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);
206                         handled = TRUE;
207                     }
208                     break;
209
210                 case WS_NFULA_GID:
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);
214                         handled = TRUE;
215                     }
216                     break;
217
218                 case WS_NFULA_TIMESTAMP:
219                     if (value_len == 16) {
220                         nstime_t ts;
221
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);
227                         handled = TRUE;
228                     }
229                     break;
230             }
231
232             if (!handled)
233                     proto_tree_add_item(tlv_tree, &hfi_nflog_tlv_unknown,
234                                         tvb, offset + 4, value_len, ENC_NA);
235         }
236
237         if (tlv_type == WS_NFULA_PAYLOAD)
238             next_tvb = tvb_new_subset_length(tvb, offset + 4, value_len);
239
240         offset += ((tlv_len + 3) & ~3); /* next TLV aligned to 4B */
241     }
242
243     if (next_tvb) {
244         switch (aftype) {
245             case LINUX_AF_INET:
246                 call_dissector(ip_handle, next_tvb, pinfo, tree);
247                 break;
248             case LINUX_AF_INET6:
249                 call_dissector(ip6_handle, next_tvb, pinfo, tree);
250                 break;
251             default:
252                 call_dissector(data_handle, next_tvb, pinfo, tree);
253                 break;
254         }
255     }
256 }
257
258 void
259 proto_register_nflog(void)
260 {
261 #ifndef HAVE_HFI_SECTION_INIT
262     static header_field_info *hfi[] = {
263     /* Header */
264         &hfi_nflog_family,
265         &hfi_nflog_version,
266         &hfi_nflog_resid,
267     /* TLV */
268         &hfi_nflog_tlv,
269         &hfi_nflog_tlv_length,
270         &hfi_nflog_tlv_type,
271     /* TLV values */
272         &hfi_nflog_tlv_prefix,
273         &hfi_nflog_tlv_uid,
274         &hfi_nflog_tlv_gid,
275         &hfi_nflog_tlv_timestamp,
276         &hfi_nflog_tlv_unknown,
277     };
278 #endif
279
280     static gint *ett[] = {
281         &ett_nflog,
282         &ett_nflog_tlv
283     };
284
285     int proto_nflog;
286
287     proto_nflog = proto_register_protocol("Linux Netfilter NFLOG", "NFLOG", "nflog");
288     hfi_nflog = proto_registrar_get_nth(proto_nflog);
289
290     register_dissector("nflog", dissect_nflog, proto_nflog);
291
292     proto_register_fields(proto_nflog, hfi, array_length(hfi));
293     proto_register_subtree_array(ett, array_length(ett));
294
295 }
296
297 void
298 proto_reg_handoff_nflog(void)
299 {
300     dissector_handle_t nflog_handle;
301
302     ip_handle   = find_dissector("ip");
303     ip6_handle  = find_dissector("ipv6");
304     data_handle = find_dissector("data");
305
306     nflog_handle = find_dissector("nflog");
307     dissector_add_uint("wtap_encap", WTAP_ENCAP_NFLOG, nflog_handle);
308 }
309
310 /*
311  * Editor modelines
312  *
313  * Local Variables:
314  * c-basic-offset: 4
315  * tab-width: 8
316  * indent-tabs-mode: nil
317  * End:
318  *
319  * ex: set shiftwidth=4 tabstop=8 expandtab:
320  * :indentSize=4:tabSize=8:noTabs=true:
321  */