summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-nflog.c
blob: fad30bfb997ec73f0424db18001c0ae07a74e0d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* packet-nflog.c
 * Copyright 2011,2012 Jakub Zawadzki <darkjames-ws@darkjames.pl>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#include "config.h"

#include <epan/etypes.h>
#include <epan/packet.h>
#include <wiretap/wtap.h>
#include <wsutil/ws_roundup.h>

#include "packet-netlink.h"

void proto_register_nflog(void);
void proto_reg_handoff_nflog(void);

/* nfulnl_attr_type enum from <linux/netfilter/nfnetlink_log.h> */
enum ws_nfulnl_attr_type {
    WS_NFULA_UNSPEC,
    WS_NFULA_PACKET_HDR,
    WS_NFULA_MARK,               /* __u32 nfmark */
    WS_NFULA_TIMESTAMP,          /* nfulnl_msg_packet_timestamp */
    WS_NFULA_IFINDEX_INDEV,      /* __u32 ifindex */
    WS_NFULA_IFINDEX_OUTDEV,     /* __u32 ifindex */
    WS_NFULA_IFINDEX_PHYSINDEV,  /* __u32 ifindex */
    WS_NFULA_IFINDEX_PHYSOUTDEV, /* __u32 ifindex */
    WS_NFULA_HWADDR,             /* nfulnl_msg_packet_hw */
    WS_NFULA_PAYLOAD,            /* opaque data payload */
    WS_NFULA_PREFIX,             /* string prefix */
    WS_NFULA_UID,                /* user id of socket */
    WS_NFULA_SEQ,                /* instance-local sequence number */
    WS_NFULA_SEQ_GLOBAL,         /* global sequence number */
    WS_NFULA_GID,                /* group id of socket */
    WS_NFULA_HWTYPE,             /* hardware type */
    WS_NFULA_HWHEADER,           /* hardware header */
    WS_NFULA_HWLEN               /* hardware header length */
};

static const value_string nflog_tlv_vals[] = {
    { WS_NFULA_UNSPEC,             "NFULA_UNSPEC" },
    { WS_NFULA_PACKET_HDR,         "NFULA_PACKET_HDR" },
    { WS_NFULA_MARK,               "NFULA_MARK" },
    { WS_NFULA_TIMESTAMP,          "NFULA_TIMESTAMP" },
    { WS_NFULA_IFINDEX_INDEV,      "NFULA_IFINDEX_INDEV" },
    { WS_NFULA_IFINDEX_OUTDEV,     "NFULA_IFINDEX_OUTDEV" },
    { WS_NFULA_IFINDEX_PHYSINDEV,  "NFULA_IFINDEX_PHYSINDEV" },
    { WS_NFULA_IFINDEX_PHYSOUTDEV, "NFULA_IFINDEX_PHYSOUTDEV" },
    { WS_NFULA_HWADDR,             "NFULA_HWADDR" },
    { WS_NFULA_PAYLOAD,            "NFULA_PAYLOAD" },
    { WS_NFULA_PREFIX,             "NFULA_PREFIX" },
    { WS_NFULA_UID,                "NFULA_UID" },
    { WS_NFULA_SEQ,                "NFULA_SEQ" },
    { WS_NFULA_SEQ_GLOBAL,         "NFULA_SEQ_GLOBAL" },
    { WS_NFULA_GID,                "NFULA_GID" },
    { WS_NFULA_HWTYPE,             "NFULA_HWTYPE" },
    { WS_NFULA_HWHEADER,           "NFULA_HWHEADER" },
    { WS_NFULA_HWLEN,              "NFULA_HWLEN" },
    { 0, NULL }
};

static int proto_nflog = -1;

static int hf_nflog_family = -1;
static int hf_nflog_resid = -1;
static int hf_nflog_tlv = -1;
static int hf_nflog_tlv_gid = -1;
static int hf_nflog_tlv_hook = -1;
static int hf_nflog_tlv_hwprotocol = -1;
static int hf_nflog_tlv_ifindex_indev = -1;
static int hf_nflog_tlv_ifindex_outdev = -1;
static int hf_nflog_tlv_ifindex_physindev = -1;
static int hf_nflog_tlv_ifindex_physoutdev = -1;
static int hf_nflog_tlv_length = -1;
static int hf_nflog_tlv_prefix = -1;
static int hf_nflog_tlv_timestamp = -1;
static int hf_nflog_tlv_type = -1;
static int hf_nflog_tlv_uid = -1;
static int hf_nflog_tlv_unknown = -1;
static int hf_nflog_version = -1;

static int ett_nflog = -1;
static int ett_nflog_tlv = -1;

static dissector_handle_t ip_handle;
static dissector_handle_t ip6_handle;
static dissector_table_t ethertype_table;
static dissector_handle_t nflog_handle;

static int
dissect_nflog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    const int start_tlv_offset = 4;

    proto_tree *nflog_tree = NULL;
    proto_item *ti;

    int offset = 0;

    tvbuff_t *next_tvb = NULL;
    int pf;
    guint16 hw_protocol = 0;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "NFLOG");
    col_clear(pinfo->cinfo, COL_INFO);

    pf = tvb_get_guint8(tvb, 0);

    /* Header */
    if (proto_field_is_referenced(tree, proto_nflog)) {
        ti = proto_tree_add_item(tree, proto_nflog, tvb, 0, -1, ENC_NA);
        nflog_tree = proto_item_add_subtree(ti, ett_nflog);

        proto_tree_add_item(nflog_tree, hf_nflog_family, tvb, offset, 1, ENC_NA);
        offset += 1;

        proto_tree_add_item(nflog_tree, hf_nflog_version, tvb, offset, 1, ENC_NA);
        offset += 1;

        proto_tree_add_item(nflog_tree, hf_nflog_resid, tvb, offset, 2, ENC_BIG_ENDIAN);
        /*offset += 2;*/
    }

    offset = start_tlv_offset;
    /* TLVs */
    while (tvb_reported_length_remaining(tvb, offset) >= 4) {
        guint16 tlv_len = tvb_get_h_guint16(tvb, offset + 0);
        guint16 tlv_type;
        guint16 value_len;

        proto_tree *tlv_tree;

        /* malformed */
        if (tlv_len < 4)
            return offset;

        value_len = tlv_len - 4;
        tlv_type = (tvb_get_h_guint16(tvb, offset + 2) & 0x7fff);

        if (nflog_tree) {
            gboolean handled = FALSE;

            ti = proto_tree_add_bytes_format(nflog_tree, hf_nflog_tlv,
                             tvb, offset, tlv_len, NULL,
                             "TLV Type: %s (%u), Length: %u",
                             val_to_str_const(tlv_type, nflog_tlv_vals, "Unknown"),
                             tlv_type, tlv_len);
            tlv_tree = proto_item_add_subtree(ti, ett_nflog_tlv);

            proto_tree_add_item(tlv_tree, hf_nflog_tlv_length, tvb, offset + 0, 2, ENC_HOST_ENDIAN);
            proto_tree_add_item(tlv_tree, hf_nflog_tlv_type, tvb, offset + 2, 2, ENC_HOST_ENDIAN);
            switch (tlv_type) {
                case WS_NFULA_PACKET_HDR:
                    if (value_len == 4) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_hwprotocol,
                                    tvb, offset + 4, 2, ENC_BIG_ENDIAN);
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_hook,
                                    tvb, offset + 6, 1, ENC_NA);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_IFINDEX_INDEV:
                    if (value_len == 4) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_ifindex_indev, tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_IFINDEX_OUTDEV:
                    if (value_len == 4) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_ifindex_outdev, tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_IFINDEX_PHYSINDEV:
                    if (value_len == 4) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_ifindex_physindev, tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_IFINDEX_PHYSOUTDEV:
                    if (value_len == 4) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_ifindex_physoutdev, tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_PAYLOAD:
                    handled = TRUE;
                    break;

                case WS_NFULA_PREFIX:
                    if (value_len >= 1) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_prefix,
                                    tvb, offset + 4, value_len, ENC_ASCII);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_UID:
                    if (value_len == 4) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_uid,
                                    tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_GID:
                    if (value_len == 4) {
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_gid,
                                    tvb, offset + 4, value_len, ENC_BIG_ENDIAN);
                        handled = TRUE;
                    }
                    break;

                case WS_NFULA_TIMESTAMP:
                    if (value_len == 16) {
                        /*
                         * 64-bit seconds and 64-bit microseconds.
                         *
                         * XXX - add an "expert info" warning if the
                         * microseconds are >= 10^6?
                         */
                        proto_tree_add_item(tlv_tree, hf_nflog_tlv_timestamp,
                                    tvb, offset + 4, value_len,
                                    ENC_TIME_SECS_USECS|ENC_BIG_ENDIAN);
                        handled = TRUE;
                    }
                    break;
            }

            if (!handled)
                    proto_tree_add_item(tlv_tree, hf_nflog_tlv_unknown,
                                        tvb, offset + 4, value_len, ENC_NA);
        }

        if (tlv_type == WS_NFULA_PACKET_HDR && value_len == 4)
            hw_protocol = tvb_get_ntohs(tvb, offset + 4);
        if (tlv_type == WS_NFULA_PAYLOAD)
            next_tvb = tvb_new_subset_length(tvb, offset + 4, value_len);

        offset += WS_ROUNDUP_4(tlv_len); /* next TLV aligned to 4B */
    }

    if (next_tvb && hw_protocol) {
        if (!dissector_try_uint(ethertype_table, hw_protocol, next_tvb, pinfo, tree))
            call_data_dissector(next_tvb, pinfo, tree);
    } else if (next_tvb) {
        switch (pf) {
            /* Note: NFPROTO_INET is not supposed to appear here, it is mapped
             * to NFPROTO_IPV4 or NFPROTO_IPV6 */
            case WS_NFPROTO_IPV4:
                call_dissector(ip_handle, next_tvb, pinfo, tree);
                break;
            case WS_NFPROTO_IPV6:
                call_dissector(ip6_handle, next_tvb, pinfo, tree);
                break;
            default:
                call_data_dissector(next_tvb, pinfo, tree);
                break;
        }
    }
    return tvb_captured_length(tvb);
}

void
proto_register_nflog(void)
{
    static hf_register_info hf[] = {
        { &hf_nflog_family,
            { "Family", "nflog.family",
              FT_UINT8, BASE_DEC, VALS(nfproto_family_vals), 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_version,
            { "Version", "nflog.version",
              FT_UINT8, BASE_DEC, NULL, 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_resid,
            { "Resource id", "nflog.res_id",
              FT_UINT16, BASE_DEC, NULL, 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv,
            { "TLV", "nflog.tlv",
              FT_BYTES, BASE_NONE, NULL, 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv_length,
            { "Length", "nflog.tlv_length",
              FT_UINT16, BASE_DEC, NULL, 0x00,
              "TLV Length", HFILL }
        },
        { &hf_nflog_tlv_type,
            { "Type", "nflog.tlv_type",
              FT_UINT16, BASE_DEC, VALS(nflog_tlv_vals), 0x7fff,
              "TLV Type", HFILL }
        },
        { &hf_nflog_tlv_hwprotocol,
            { "HW protocol", "nflog.protocol",
              FT_UINT16, BASE_HEX, VALS(etype_vals), 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv_hook,
            { "Netfilter hook", "nflog.hook",
              FT_UINT8, BASE_DEC, VALS(netfilter_hooks_vals), 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv_ifindex_indev,
            { "IFINDEX_INDEV", "nflog.ifindex_indev",
              FT_UINT32, BASE_DEC, NULL, 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv_ifindex_outdev,
            { "IFINDEX_OUTDEV", "nflog.ifindex_outdev",
              FT_UINT32, BASE_DEC, NULL, 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv_ifindex_physindev,
            { "IFINDEX_PHYSINDEV", "nflog.ifindex_physindev",
              FT_UINT32, BASE_DEC, NULL, 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv_ifindex_physoutdev,
            { "IFINDEX_PHYSOUTDEV", "nflog.ifindex_physoutdev",
              FT_UINT32, BASE_DEC, NULL, 0x00,
              NULL, HFILL }
        },
        { &hf_nflog_tlv_prefix,
            { "Prefix", "nflog.prefix",
              FT_STRINGZ, BASE_NONE, NULL, 0x00,
              "TLV Prefix Value", HFILL }
        },
        { &hf_nflog_tlv_uid,
            { "UID", "nflog.uid",
              FT_INT32, BASE_DEC, NULL, 0x00,
              "TLV UID Value", HFILL }
        },
        { &hf_nflog_tlv_gid,
            { "GID", "nflog.gid",
              FT_INT32, BASE_DEC, NULL, 0x00,
              "TLV GID Value", HFILL }
        },
        { &hf_nflog_tlv_timestamp,
            { "Timestamp", "nflog.timestamp",
              FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00,
              "TLV Timestamp Value", HFILL }
        },
        { &hf_nflog_tlv_unknown,
            { "Value", "nflog.tlv_value",
              FT_BYTES, BASE_NONE, NULL, 0x00,
              "TLV Value", HFILL }
        },
    };

    static gint *ett[] = {
        &ett_nflog,
        &ett_nflog_tlv
    };

    proto_nflog = proto_register_protocol("Linux Netfilter NFLOG", "NFLOG", "nflog");

    nflog_handle = register_dissector("nflog", dissect_nflog, proto_nflog);

    proto_register_field_array(proto_nflog, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

}

void
proto_reg_handoff_nflog(void)
{
    ip_handle   = find_dissector_add_dependency("ip", proto_nflog);
    ip6_handle  = find_dissector_add_dependency("ipv6", proto_nflog);

    dissector_add_uint("wtap_encap", WTAP_ENCAP_NFLOG, nflog_handle);
    ethertype_table = find_dissector_table("ethertype");
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */