summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-hsr.c
blob: 5aa6e984d44baa79c7e265cb3da87e3940c117da (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
/* packet-hsr.c
 * Routines for HSR dissection (IEC62439 Part 3)
 * Copyright 2009, Florian Reichert <refl[AT]zhaw.ch>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald[AT]wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/etypes.h>

void proto_register_hsr(void);
void proto_reg_handoff_hsr(void);

static dissector_handle_t hsr_frame_handle;

/**********************************************************/
/* Lengths of fields within a HSR packet.                 */
/**********************************************************/
#define HSR_LSDU_PATH_LENGTH                 2
#define HSR_SEQUENZNR_LENGTH                 2

#define HSR_TOTAL_LENGTH                     4

/**********************************************************/
/* Offsets of fields within a HSR packet.                 */
/**********************************************************/
#define HSR_PATH_OFFSET                      0
#define HSR_SEQUENZNR_OFFSET                 2

static const value_string hsr_laneid_vals[] = {
    {0, "Lane A"},
    {1, "Lane B"},
    {0, NULL}
};

/**********************************************************/
/* Initialize the protocol and registered fields          */
/**********************************************************/

static int proto_hsr;

/* Initialize supervision frame fields */


static int hf_hsr_path;
static int hf_hsr_netid;
static int hf_hsr_laneid;
static int hf_hsr_lsdu_size;
static int hf_hsr_sequence_nr;
static int hf_type;

static dissector_table_t ethertype_subdissector_table;
/* Initialize the subtree pointers */
static int ett_hsr_frame;

/* Code to actually dissect the packets */
static int
dissect_hsr_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    proto_item *ti;
    proto_tree *hsr_tree;
    tvbuff_t *next_tvb;
    uint16_t etype;
    uint16_t lsdu_size, lsdu_size_correct;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "HSR");

    col_set_str(pinfo->cinfo, COL_INFO, "HSR-Data Frame");

    /* create display subtree for the protocol */

    ti = proto_tree_add_item(tree, proto_hsr, tvb, 0, HSR_TOTAL_LENGTH, ENC_NA);

    hsr_tree = proto_item_add_subtree(ti, ett_hsr_frame);

    proto_tree_add_item(hsr_tree, hf_hsr_path,
                        tvb, HSR_PATH_OFFSET, HSR_LSDU_PATH_LENGTH , ENC_BIG_ENDIAN);

    proto_tree_add_item(hsr_tree, hf_hsr_netid,
                        tvb, HSR_PATH_OFFSET, HSR_LSDU_PATH_LENGTH , ENC_BIG_ENDIAN);

    proto_tree_add_item(hsr_tree, hf_hsr_laneid,
                        tvb, HSR_PATH_OFFSET, HSR_LSDU_PATH_LENGTH , ENC_BIG_ENDIAN);


    lsdu_size = tvb_get_ntohs(tvb, HSR_PATH_OFFSET) & 0x0fff;
    lsdu_size_correct = tvb_reported_length_remaining(tvb, 0);
    if (lsdu_size == lsdu_size_correct) {
        proto_tree_add_uint_format_value(hsr_tree, hf_hsr_lsdu_size,
                                   tvb, HSR_PATH_OFFSET, HSR_LSDU_PATH_LENGTH, lsdu_size,
                                   "%d [correct]", lsdu_size);
    } else {
        proto_tree_add_uint_format_value(hsr_tree, hf_hsr_lsdu_size,
                                   tvb, HSR_PATH_OFFSET, HSR_LSDU_PATH_LENGTH, lsdu_size,
                                   "%d [WRONG, should be %d]", lsdu_size, lsdu_size_correct);
    }

    proto_tree_add_item(hsr_tree, hf_hsr_sequence_nr,
                        tvb, HSR_SEQUENZNR_OFFSET,HSR_SEQUENZNR_LENGTH, ENC_BIG_ENDIAN);

    proto_tree_add_item(hsr_tree, hf_type,
                        tvb, HSR_TOTAL_LENGTH,2, ENC_BIG_ENDIAN);

    next_tvb = tvb_new_subset_remaining(tvb, HSR_TOTAL_LENGTH + 2);

    etype = tvb_get_ntohs(tvb, HSR_TOTAL_LENGTH);

    if (!dissector_try_uint(ethertype_subdissector_table, etype, next_tvb, pinfo, tree))
        call_data_dissector(next_tvb, pinfo, hsr_tree);

    return tvb_captured_length(tvb);
}



/* Register the protocol with Wireshark */
void proto_register_hsr(void)
{
    static hf_register_info hf[] = {
        { &hf_hsr_path,
          { "Path", "hsr.path",
            FT_UINT16, BASE_DEC, NULL, 0xf000,
            NULL, HFILL }
        },

        { &hf_hsr_netid,
          { "Network id", "hsr.netid",
            FT_UINT16, BASE_DEC, NULL, 0xe000,
            NULL, HFILL }
        },

        { &hf_hsr_laneid,
          { "Lane id", "hsr.laneid",
            FT_UINT16, BASE_DEC, VALS(hsr_laneid_vals), 0x1000,
            NULL, HFILL }
        },

        { &hf_hsr_lsdu_size,
          { "LSDU size", "hsr.lsdu_size",
            FT_UINT16, BASE_DEC, NULL, 0x0fff,
            NULL, HFILL }
        },

        { &hf_hsr_sequence_nr,
          { "Sequence number", "hsr.sequence_nr",
            FT_UINT16, BASE_DEC, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_type,
          { "Type", "hsr.type",
            FT_UINT16, BASE_HEX, VALS(etype_vals), 0x00,
            NULL, HFILL }
        }

    };

    static int *ett[] = {
        &ett_hsr_frame,
    };

    /* Register the protocol name and description */
    proto_hsr = proto_register_protocol("High-availability Seamless Redundancy (IEC62439 Part 3 Chapter 5)",
                                        "HSR", "hsr");

    /* Required function calls to register the header fields and subtree used */
    proto_register_field_array(proto_hsr, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    hsr_frame_handle = register_dissector("hsr", dissect_hsr_frame, proto_hsr);
}


void proto_reg_handoff_hsr(void)
{
    dissector_add_uint("ethertype", ETHERTYPE_HSR, hsr_frame_handle);

    ethertype_subdissector_table = find_dissector_table("ethertype");
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */