2 * Routines for IEC 61850 Sampled Values packet dissection
3 * Michael Bernhard 2008
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include <epan/packet.h>
15 #include <epan/asn1.h>
16 #include <epan/etypes.h>
17 #include <epan/expert.h>
18 #include <epan/prefs.h>
19 #include <epan/addr_resolv.h>
20 #include <wsutil/array.h>
22 #include "packet-ber.h"
23 #include "packet-acse.h"
27 #include "packet-sv.h"
29 #define PNAME "IEC61850 Sampled Values"
33 /* see IEC61850-8-1 8.2 */
34 #define Q_VALIDITY_GOOD (0x0U << 0)
35 #define Q_VALIDITY_INVALID_BW (0x1U << 0)
36 #define Q_VALIDITY_INVALID (0x2U << 0)
37 #define Q_VALIDITY_QUESTIONABLE (0x3U << 0)
38 #define Q_VALIDITY_MASK (0x3U << 0)
40 #define Q_OVERFLOW (1U << 2)
41 #define Q_OUTOFRANGE (1U << 3)
42 #define Q_BADREFERENCE (1U << 4)
43 #define Q_OSCILLATORY (1U << 5)
44 #define Q_FAILURE (1U << 6)
45 #define Q_OLDDATA (1U << 7)
46 #define Q_INCONSISTENT (1U << 8)
47 #define Q_INACCURATE (1U << 9)
49 #define Q_SOURCE_PROCESS (0U << 10)
50 #define Q_SOURCE_SUBSTITUTED (1U << 10)
51 #define Q_SOURCE_MASK (1U << 10)
53 #define Q_TEST (1U << 11)
54 #define Q_OPERATORBLOCKED (1U << 12)
56 /* see UCA Implementation Guideline for IEC 61850-9-2 */
57 #define Q_DERIVED (1U << 13)
59 /* Bit fields in the Reserved attributes */
60 #define F_RESERVE1_S_BIT 0x8000
63 void proto_register_sv(void);
64 void proto_reg_handoff_sv(void);
68 static sv_frame_data sv_data
;
70 /* Initialize the protocol and registered fields */
72 static int hf_sv_appid
;
73 static int hf_sv_length
;
74 static int hf_sv_reserve1
;
75 static int hf_sv_reserve1_s_bit
;
76 static int hf_sv_reserve2
;
77 static int hf_sv_phmeas_instmag_i
;
78 static int hf_sv_phsmeas_q
;
79 static int hf_sv_phsmeas_q_validity
;
80 static int hf_sv_phsmeas_q_overflow
;
81 static int hf_sv_phsmeas_q_outofrange
;
82 static int hf_sv_phsmeas_q_badreference
;
83 static int hf_sv_phsmeas_q_oscillatory
;
84 static int hf_sv_phsmeas_q_failure
;
85 static int hf_sv_phsmeas_q_olddata
;
86 static int hf_sv_phsmeas_q_inconsistent
;
87 static int hf_sv_phsmeas_q_inaccurate
;
88 static int hf_sv_phsmeas_q_source
;
89 static int hf_sv_phsmeas_q_test
;
90 static int hf_sv_phsmeas_q_operatorblocked
;
91 static int hf_sv_phsmeas_q_derived
;
92 static int hf_sv_gmidentity
;
93 static int hf_sv_gmidentity_manuf
;
95 #include "packet-sv-hf.c"
97 /* Initialize the subtree pointers */
99 static int ett_phsmeas
;
100 static int ett_phsmeas_q
;
101 static int ett_gmidentity
;
102 static int ett_reserve1
;
105 #include "packet-sv-ett.c"
107 static expert_field ei_sv_mal_utctime
;
108 static expert_field ei_sv_zero_pdu
;
109 static expert_field ei_sv_mal_gmidentity
;
111 static bool sv_decode_data_as_phsmeas
;
113 static dissector_handle_t sv_handle
;
117 * IEC 61850-9-2 Edition 2.1 2020-02,
118 * Section 8.6 Definitions for basic data types – Presentation layer functionality,
121 * Be aware that in the specification the bits are numbered in reverse (it
122 * specifies the least significant bit as bit 31 instead of as bit 0)!
125 static const value_string sv_q_validity_vals
[] = {
127 { 1, "invalid (backwards compatible)" },
129 { 3, "questionable" },
133 static const value_string sv_q_source_vals
[] = {
135 { 1, "substituted" },
140 dissect_PhsMeas1(bool implicit_tag
, packet_info
*pinfo
, proto_tree
*tree
, tvbuff_t
*tvb
, int offset
, int hf_id _U_
)
151 static int * const q_flags
[] = {
152 &hf_sv_phsmeas_q_validity
,
153 &hf_sv_phsmeas_q_overflow
,
154 &hf_sv_phsmeas_q_outofrange
,
155 &hf_sv_phsmeas_q_badreference
,
156 &hf_sv_phsmeas_q_oscillatory
,
157 &hf_sv_phsmeas_q_failure
,
158 &hf_sv_phsmeas_q_olddata
,
159 &hf_sv_phsmeas_q_inconsistent
,
160 &hf_sv_phsmeas_q_inaccurate
,
161 &hf_sv_phsmeas_q_source
,
162 &hf_sv_phsmeas_q_test
,
163 &hf_sv_phsmeas_q_operatorblocked
,
164 &hf_sv_phsmeas_q_derived
,
169 offset
=dissect_ber_identifier(pinfo
, tree
, tvb
, offset
, &ber_class
, &pc
, &tag
);
170 offset
=dissect_ber_length(pinfo
, tree
, tvb
, offset
, &len
, NULL
);
172 len
=tvb_reported_length_remaining(tvb
, offset
);
175 subtree
= proto_tree_add_subtree(tree
, tvb
, offset
, len
, ett_phsmeas
, NULL
, "PhsMeas1");
177 sv_data
.num_phsMeas
= 0;
178 for (i
= 0; i
< len
/8; i
++) {
179 if (tree
&& subtree
) {
180 value
= tvb_get_ntohl(tvb
, offset
);
181 qual
= tvb_get_ntohl(tvb
, offset
+ 4);
183 proto_tree_add_item(subtree
, hf_sv_phmeas_instmag_i
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
184 proto_tree_add_bitmask(subtree
, tvb
, offset
+ 4, hf_sv_phsmeas_q
, ett_phsmeas_q
, q_flags
, ENC_BIG_ENDIAN
);
186 if (i
< IEC61850_SV_MAX_PHSMEAS_ENTRIES
) {
187 sv_data
.phsMeas
[i
].value
= value
;
188 sv_data
.phsMeas
[i
].qual
= qual
;
189 sv_data
.num_phsMeas
++;
199 #include "packet-sv-fn.c"
202 * Dissect SV PDUs inside a PPDU.
205 dissect_sv(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*parent_tree
, void* data _U_
)
209 unsigned sv_length
= 0;
213 static int * const reserve1_flags
[] = {
214 &hf_sv_reserve1_s_bit
,
220 asn1_ctx_init(&asn1_ctx
, ASN1_ENC_BER
, true, pinfo
);
222 item
= proto_tree_add_item(parent_tree
, proto_sv
, tvb
, 0, -1, ENC_NA
);
223 tree
= proto_item_add_subtree(item
, ett_sv
);
225 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, PNAME
);
226 col_clear(pinfo
->cinfo
, COL_INFO
);
229 proto_tree_add_item(tree
, hf_sv_appid
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
232 proto_tree_add_item_ret_uint(tree
, hf_sv_length
, tvb
, offset
+ 2, 2, ENC_BIG_ENDIAN
, &sv_length
);
235 proto_tree_add_bitmask(tree
, tvb
, offset
+ 4, hf_sv_reserve1
, ett_reserve1
,
236 reserve1_flags
, ENC_BIG_ENDIAN
);
240 proto_tree_add_item(tree
, hf_sv_reserve2
, tvb
, offset
+ 6, 2, ENC_BIG_ENDIAN
);
243 set_actual_length(tvb
, sv_length
);
244 while (tvb_reported_length_remaining(tvb
, offset
) > 0) {
246 offset
= dissect_sv_SampledValues(false, tvb
, offset
, &asn1_ctx
, tree
, -1);
247 if (offset
== old_offset
) {
248 proto_tree_add_expert(tree
, pinfo
, &ei_sv_zero_pdu
, tvb
, offset
, -1);
253 tap_queue_packet(sv_tap
, pinfo
, &sv_data
);
254 return tvb_captured_length(tvb
);
258 /*--- proto_register_sv -------------------------------------------*/
259 void proto_register_sv(void) {
262 static hf_register_info hf
[] = {
264 { "APPID", "sv.appid", FT_UINT16
, BASE_HEX
, NULL
, 0x0, NULL
, HFILL
}},
267 { "Length", "sv.length", FT_UINT16
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
}},
270 { "Reserved 1", "sv.reserve1", FT_UINT16
, BASE_HEX_DEC
, NULL
, 0x0, NULL
, HFILL
}},
272 { &hf_sv_reserve1_s_bit
,
273 { "Simulated", "sv.reserve1.s_bit",
274 FT_BOOLEAN
, 16, NULL
, F_RESERVE1_S_BIT
, NULL
, HFILL
} },
277 { "Reserved 2", "sv.reserve2", FT_UINT16
, BASE_HEX_DEC
, NULL
, 0x0, NULL
, HFILL
}},
279 { &hf_sv_phmeas_instmag_i
,
280 { "value", "sv.meas_value", FT_INT32
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
}},
283 { "quality", "sv.meas_quality", FT_UINT32
, BASE_HEX
, NULL
, 0x0, NULL
, HFILL
}},
285 { &hf_sv_phsmeas_q_validity
,
286 { "validity", "sv.meas_quality.validity", FT_UINT32
, BASE_HEX
, VALS(sv_q_validity_vals
), Q_VALIDITY_MASK
, NULL
, HFILL
}},
288 { &hf_sv_phsmeas_q_overflow
,
289 { "overflow", "sv.meas_quality.overflow", FT_BOOLEAN
, 32, NULL
, Q_OVERFLOW
, NULL
, HFILL
}},
291 { &hf_sv_phsmeas_q_outofrange
,
292 { "out of range", "sv.meas_quality.outofrange", FT_BOOLEAN
, 32, NULL
, Q_OUTOFRANGE
, NULL
, HFILL
}},
294 { &hf_sv_phsmeas_q_badreference
,
295 { "bad reference", "sv.meas_quality.badreference", FT_BOOLEAN
, 32, NULL
, Q_BADREFERENCE
, NULL
, HFILL
}},
297 { &hf_sv_phsmeas_q_oscillatory
,
298 { "oscillatory", "sv.meas_quality.oscillatory", FT_BOOLEAN
, 32, NULL
, Q_OSCILLATORY
, NULL
, HFILL
}},
300 { &hf_sv_phsmeas_q_failure
,
301 { "failure", "sv.meas_quality.failure", FT_BOOLEAN
, 32, NULL
, Q_FAILURE
, NULL
, HFILL
}},
303 { &hf_sv_phsmeas_q_olddata
,
304 { "old data", "sv.meas_quality.olddata", FT_BOOLEAN
, 32, NULL
, Q_OLDDATA
, NULL
, HFILL
}},
306 { &hf_sv_phsmeas_q_inconsistent
,
307 { "inconsistent", "sv.meas_quality.inconsistent", FT_BOOLEAN
, 32, NULL
, Q_INCONSISTENT
, NULL
, HFILL
}},
309 { &hf_sv_phsmeas_q_inaccurate
,
310 { "inaccurate", "sv.meas_quality.inaccurate", FT_BOOLEAN
, 32, NULL
, Q_INACCURATE
, NULL
, HFILL
}},
312 { &hf_sv_phsmeas_q_source
,
313 { "source", "sv.meas_quality.source", FT_UINT32
, BASE_HEX
, VALS(sv_q_source_vals
), Q_SOURCE_MASK
, NULL
, HFILL
}},
315 { &hf_sv_phsmeas_q_test
,
316 { "test", "sv.meas_quality.test", FT_BOOLEAN
, 32, NULL
, Q_TEST
, NULL
, HFILL
}},
318 { &hf_sv_phsmeas_q_operatorblocked
,
319 { "operator blocked", "sv.meas_quality.operatorblocked", FT_BOOLEAN
, 32, NULL
, Q_OPERATORBLOCKED
, NULL
, HFILL
}},
321 { &hf_sv_phsmeas_q_derived
,
322 { "derived", "sv.meas_quality.derived", FT_BOOLEAN
, 32, NULL
, Q_DERIVED
, NULL
, HFILL
}},
325 { "gmIdentity", "sv.gmidentity", FT_UINT64
, BASE_HEX
, NULL
, 0x00, NULL
, HFILL
}},
327 { &hf_sv_gmidentity_manuf
,
328 { "MAC Vendor", "sv.gmidentity_manuf", FT_BYTES
, BASE_NONE
, NULL
, 0x00, NULL
, HFILL
}},
331 #include "packet-sv-hfarr.c"
334 /* List of subtrees */
335 static int *ett
[] = {
341 #include "packet-sv-ettarr.c"
344 static ei_register_info ei
[] = {
345 { &ei_sv_mal_utctime
, { "sv.malformed.utctime", PI_MALFORMED
, PI_WARN
, "BER Error: malformed UTCTime encoding", EXPFILL
}},
346 { &ei_sv_zero_pdu
, { "sv.zero_pdu", PI_PROTOCOL
, PI_ERROR
, "Internal error, zero-byte SV PDU", EXPFILL
}},
347 { &ei_sv_mal_gmidentity
, { "sv.malformed.gmidentity", PI_MALFORMED
, PI_WARN
, "BER Error: malformed gmIdentity encoding", EXPFILL
}},
350 expert_module_t
* expert_sv
;
353 /* Register protocol */
354 proto_sv
= proto_register_protocol(PNAME
, PSNAME
, PFNAME
);
355 sv_handle
= register_dissector("sv", dissect_sv
, proto_sv
);
357 /* Register fields and subtrees */
358 proto_register_field_array(proto_sv
, hf
, array_length(hf
));
359 proto_register_subtree_array(ett
, array_length(ett
));
360 expert_sv
= expert_register_protocol(proto_sv
);
361 expert_register_field_array(expert_sv
, ei
, array_length(ei
));
362 sv_module
= prefs_register_protocol(proto_sv
, NULL
);
363 prefs_register_bool_preference(sv_module
, "decode_data_as_phsmeas",
364 "Force decoding of seqData as PhsMeas",
365 NULL
, &sv_decode_data_as_phsmeas
);
368 sv_tap
= register_tap("sv");
371 /*--- proto_reg_handoff_sv --- */
372 void proto_reg_handoff_sv(void) {
373 dissector_add_uint("ethertype", ETHERTYPE_IEC61850_SV
, sv_handle
);