1 /* packet-gsm_r_uus1.c
2  * Routines for GSM-R UUS1 dissection
3  *
4  * Copyright 2018, Michail Koreshkov <michail.koreshkov [at] bk.ru
5  *
6  * Reference [1]
7  * Railways Telecommunications (RT);
8  * Global System for Mobile communications (GSM);
9  * Usage of the User-to-User Information Element
10  * for GSM Operation on Railways
11  * (ETSI TS 102 610 V1.2.0 (2012-08))
12  *
13  * Reference [2]
14  * EIRENE SRS: "UIC Project EIRENE System Requirements Specification".
15  *
16  * Reference [3]
17  * MORANE F 10 T 6003 4: "FFFS for Presentation of Functional Numbers
18  * to Called and Calling Parties".
19  *
20  * Reference [4]
21  * eLDA IRS (V5.0): "Interface Requirements Specification
22  * enhanced Location Dependent Addressing".
23  *
24  * SPDX-License-Identifier: GPL-2.0-or-later
25  */
26 
27 #include "config.h"
28 
29 #include <epan/packet.h>
30 #include <epan/expert.h>
31 #include <epan/tap.h>
32 #include <epan/prefs.h>
33 #include <epan/strutil.h>
34 
35 /* forward reference */
36 void proto_register_gsm_r_uus1(void);
37 void proto_reg_handoff_gsm_r_uus1(void);
38 
39 
40 /* Initialize the protocol and registered fields */
41 static int proto_gsm_r_uus1 = -1;
42 
43 static int hf_gsm_r_uus1_elem_tag                    = -1;
44 static int hf_gsm_r_uus1_elem_len                    = -1;
45 static int hf_gsm_r_uus1_pfn                         = -1;
46 static int hf_gsm_r_uus1_pfn_digits                  = -1;
47 
48 /* 5.2 and 5.3 */
49 static int hf_gsm_r_uus1_chpc                        = -1;
50 static int hf_gsm_r_uus1_chpc_t_dur                  = -1;
51 static int hf_gsm_r_uus1_chpc_t_rel                  = -1;
52 static int hf_gsm_r_uus1_chpc_pl_call                = -1;
53 static int hf_gsm_r_uus1_chpc_cause                  = -1;
54 static int hf_gsm_r_uus1_chpc_cause_power            = -1;
55 static int hf_gsm_r_uus1_chpc_cause_radio            = -1;
56 static int hf_gsm_r_uus1_chpc_cause_reserved3        = -1;
57 static int hf_gsm_r_uus1_chpc_cause_reserved4        = -1;
58 static int hf_gsm_r_uus1_chpc_cause_user_command     = -1;
59 static int hf_gsm_r_uus1_chpc_cause_reserved6        = -1;
60 static int hf_gsm_r_uus1_chpc_cause_reserved7        = -1;
61 static int hf_gsm_r_uus1_chpc_cause_reserved8        = -1;
62 static int hf_gsm_r_uus1_chpc_gref                   = -1;
63 static int hf_gsm_r_uus1_chpc_ack_cause              = -1;
64 
65 /* 5.4 */
66 static int hf_gsm_r_uus1_epfn                        = -1;
67 
68 /* 5.5 */
69 static int hf_gsm_r_uus1_present_text_str            = -1;
70 
71 /* 5.6 */
72 static int hf_gsm_r_uus1_elda          = -1;
73 static int hf_gsm_r_uus1_elda_lat      = -1;
74 static int hf_gsm_r_uus1_elda_lat_deg  = -1;
75 static int hf_gsm_r_uus1_elda_lat_min  = -1;
76 static int hf_gsm_r_uus1_elda_lat_sec  = -1;
77 static int hf_gsm_r_uus1_elda_lat_hem  = -1;
78 static int hf_gsm_r_uus1_elda_long     = -1;
79 static int hf_gsm_r_uus1_elda_long_deg = -1;
80 static int hf_gsm_r_uus1_elda_long_min = -1;
81 static int hf_gsm_r_uus1_elda_long_sec = -1;
82 static int hf_gsm_r_uus1_elda_long_hem = -1;
83 static int hf_gsm_r_uus1_elda_height   = -1;
84 static int hf_gsm_r_uus1_elda_speed    = -1;
85 static int hf_gsm_r_uus1_elda_heading  = -1;
86 static int hf_gsm_r_uus1_elda_e_time   = -1;
87 static int hf_gsm_r_uus1_elda_distance = -1;
88 static int hf_gsm_r_uus1_elda_scale    = -1;
89 static int hf_gsm_r_uus1_elda_spare    = -1;
90 
91 /* 5.7 */
92 static int hf_gsm_r_uus1_present_dsd_alarm               = -1;
93 static int hf_gsm_r_uus1_present_dsd_alarm_loco_number   = -1;
94 
95 /* 5.8 */
96 static int hf_gsm_r_uus1_alert_controller        = -1;
97 static int hf_gsm_r_uus1_alert_controller_gref   = -1;
98 
99 /* Initialize the subtree pointers */
100 static gint ett_gsm_r_uus1                   = -1;
101 static gint ett_gsm_r_uus1_pfn               = -1;
102 static gint ett_gsm_r_uus1_chpc              = -1;
103 static gint ett_gsm_r_uus1_chpc_cause        = -1;
104 static gint ett_gsm_r_uus1_epfn              = -1;
105 static gint ett_gsm_r_uus1_present_text_str  = -1;
106 static gint ett_gsm_r_uus1_elda              = -1;
107 static gint ett_gsm_r_uus1_elda_lat          = -1;
108 static gint ett_gsm_r_uus1_elda_long         = -1;
109 static gint ett_gsm_r_uus1_present_dsd_alarm = -1;
110 static gint ett_gsm_r_uus1_alert_controller  = -1;
111 
112 
113 /* Preferences */
114 static gboolean q931_u2u  = FALSE;
115 static gboolean gsm_a_u2u = TRUE;
116 
117 
118 static expert_field ei_gsm_r_uus1_not_implemented_yet = EI_INIT;
119 
120 static const value_string gsm_r_uus1_tags[] = {
121     { 2,  "Acknowledgement by Receiver of a HPC and response from device accepting the acknowledgement" },
122     { 3,  "Acknowledgement by Initiator of a HPC" },
123     { 5,  "Presentation of Functional Number" },
124     { 6,  "enhanced Location Dependent Addressing" },
125     { 7,  "enhanced Location Dependent Addressing (Reserved for future use)" },
126     { 8,  "enhanced Location Dependent Addressing (Reserved for future use)" },
127     { 9,  "ePFN Information" },
128     { 10, "User specific plain text according to alphabet indicator" },
129     { 11, "DSD Alarm Notification" },
130     { 12, "Alerting of a Controller Notification and Response" },
131     { 0, NULL }
132 };
133 
134 
135 /*
136  * 5.1 Presentation of functional number
137  *
138  */
139 static guint16
de_gsm_r_uus1_pfn(tvbuff_t * tvb,proto_tree * tree,guint32 offset)140 de_gsm_r_uus1_pfn(tvbuff_t *tvb, proto_tree *tree, guint32 offset)
141 {
142     guint32	curr_offset;
143     guint32	len;
144     const gchar *fn_str;
145     proto_item *item;
146     proto_tree *sub_tree;
147 
148     curr_offset = offset;
149 
150     len = tvb_get_guint8(tvb, offset+1);
151 
152     item = proto_tree_add_item(tree, hf_gsm_r_uus1_pfn, tvb, curr_offset, len+2, ENC_NA);
153     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_pfn);
154 
155     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, curr_offset, 1, ENC_NA);
156     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_len, tvb, curr_offset+1, 1, ENC_NA);
157     curr_offset += 2;
158 
159     if(len == 0) {
160         proto_item_append_text(item, ": No FN Available");
161     }else {
162         fn_str = tvb_bcd_dig_to_str(wmem_packet_scope(), tvb, offset+2, len, NULL, FALSE);
163         proto_tree_add_string(sub_tree, hf_gsm_r_uus1_pfn_digits, tvb, curr_offset, len, fn_str);
164         proto_item_append_text(item, ": %s", fn_str);
165 
166         curr_offset += len;
167     }
168 
169     return (curr_offset - offset);
170 }
171 
172 
173 /*
174  * 5.2 Confirmation of High Priority Calls
175  *
176  */
177 static const range_string gsm_r_uus1_chpc_priority_vals[] = {
178     { 0x00, 0x00, "no priority specified in call"},
179     { 0x01, 0x01, "eMLPP priority of 4 (Railway Information)"},
180     { 0x02, 0x02, "eMLPP priority of 3 (Railway Operation)"},
181     { 0x03, 0x03, "eMLPP priority of 2 (Public Emergency/Group Calls)"},
182     { 0x04, 0x04, "eMLPP priority of 1 (Command and Control)"},
183     { 0x05, 0x05, "eMLPP priority of 0 (Railway Emergency)"},
184     { 0x06, 0x07, "unknown" },
185     { 0, 0, NULL }
186 };
187 
188 static guint16
de_gsm_r_uus1_chpc_forward(tvbuff_t * tvb,proto_tree * tree,guint32 offset)189 de_gsm_r_uus1_chpc_forward(tvbuff_t *tvb, proto_tree *tree, guint32 offset)
190 {
191     guint32	curr_offset;
192     guint32	len;
193     guint32 t_dur;
194     guint32 t_rel;
195     proto_item *item;
196     proto_tree *sub_tree;
197 
198     curr_offset = offset;
199 
200     len = tvb_get_guint8(tvb, offset+1);
201 
202     item = proto_tree_add_item(tree, hf_gsm_r_uus1_chpc, tvb, curr_offset, len+2, ENC_NA);
203     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_chpc);
204 
205     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, curr_offset, 1, ENC_NA);
206     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_len, tvb, curr_offset+1, 1, ENC_NA);
207     curr_offset += 2;
208 
209     t_dur = tvb_get_guint24(tvb, curr_offset, ENC_LITTLE_ENDIAN);
210     proto_tree_add_uint_format_value(sub_tree, hf_gsm_r_uus1_chpc_t_dur, tvb, curr_offset, 3, t_dur, "%d ms", t_dur*100);
211     curr_offset += 3;
212 
213     t_rel = tvb_get_guint32(tvb, curr_offset, ENC_LITTLE_ENDIAN);
214     proto_tree_add_uint_format_value(sub_tree, hf_gsm_r_uus1_chpc_t_rel, tvb, curr_offset, 4, t_rel, "%d ms", t_rel*100);
215     curr_offset += 4;
216 
217     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_chpc_pl_call, tvb, curr_offset, 1, ENC_NA);
218     curr_offset += 1;
219 
220     static int * const cause_flags[] = {
221         &hf_gsm_r_uus1_chpc_cause_power,
222         &hf_gsm_r_uus1_chpc_cause_radio,
223         &hf_gsm_r_uus1_chpc_cause_reserved3,
224         &hf_gsm_r_uus1_chpc_cause_reserved4,
225         &hf_gsm_r_uus1_chpc_cause_user_command,
226         &hf_gsm_r_uus1_chpc_cause_reserved6,
227         &hf_gsm_r_uus1_chpc_cause_reserved7,
228         &hf_gsm_r_uus1_chpc_cause_reserved8,
229         NULL
230     };
231 
232     proto_tree_add_bitmask(sub_tree, tvb, curr_offset, hf_gsm_r_uus1_chpc_cause, ett_gsm_r_uus1_chpc_cause, cause_flags, ENC_NA);
233     curr_offset += 1;
234 
235     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_chpc_gref, tvb, curr_offset, 4, ENC_BCD_DIGITS_0_9);
236     curr_offset += 4;
237 
238     return (curr_offset - offset);
239 }
240 
241 /*
242  * 5.3 CHPC tag definition for collecting network device
243  *
244  * The tag is included in a RELEASE_COMPLETE message
245  * which shall have the release cause value of "Normal Call Clearing".
246  *
247  * gsm_a.dtap.msg_cc_type == 0x2a
248  */
249 static const range_string gsm_r_uus1_chpc_ack_cause_vals[] = {
250     { 0x00, 0x00, "ACK (no error)" },
251     { 0x01, 0x01, "NACK-1 (error, repetition should take place)" },
252     { 0x02, 0x7f, "Reserved for internal use" },
253     { 0x80, 0x80, "NACK-2 (fatal error, NO repetition to take place)" },
254     { 0x81, 0xff, "Reserved" },
255     { 0, 0, NULL }
256 };
257 
258 static guint16
de_gsm_r_uus1_chpc_collect(tvbuff_t * tvb,proto_tree * tree,guint32 offset)259 de_gsm_r_uus1_chpc_collect(tvbuff_t *tvb, proto_tree *tree, guint32 offset)
260 {
261     proto_item *item;
262     proto_tree *sub_tree;
263 
264     item = proto_tree_add_item(tree, hf_gsm_r_uus1_chpc, tvb, offset, 2, ENC_NA);
265     proto_item_set_text(item, "CHPC for collecting network device");
266     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_chpc);
267 
268     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, offset, 1, ENC_NA);
269     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_chpc_ack_cause, tvb, offset+1, 1, ENC_NA);
270 
271     return 2;
272 }
273 
274 /*
275  * Type of field depends on BSSAP message type. SETUP or RELEASE_COMPLETE
276  * But I don't know how to verify message type.
277  * That is why use length variable to find type of field
278  */
279 static guint16
de_gsm_r_uus1_chpc(tvbuff_t * tvb,proto_tree * tree,guint32 offset)280 de_gsm_r_uus1_chpc(tvbuff_t *tvb, proto_tree *tree, guint32 offset)
281 {
282     guint8 field_length;
283     field_length = tvb_get_guint8(tvb, offset+1);
284     if(field_length == 13)
285         return de_gsm_r_uus1_chpc_forward(tvb, tree, offset);
286     else
287         return de_gsm_r_uus1_chpc_collect(tvb, tree, offset);
288 }
289 
290 /*
291  * 5.4 Enhanced presentation of functional number
292  * (not implemented now)
293  */
294 static guint16
de_gsm_r_uus1_epfn(tvbuff_t * tvb,proto_tree * tree,packet_info * pinfo,guint32 offset)295 de_gsm_r_uus1_epfn(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, guint32 offset)
296 {
297     guint32	curr_offset;
298     guint32	len;
299     proto_item *item;
300     proto_tree *sub_tree;
301 
302     curr_offset = offset;
303 
304     len = tvb_get_guint8(tvb, offset+1);
305 
306     item = proto_tree_add_item(tree, hf_gsm_r_uus1_epfn, tvb, curr_offset, len+2, ENC_NA);
307     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_epfn);
308 
309     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, curr_offset, 1, ENC_NA);
310     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_len, tvb, curr_offset+1, 1, ENC_NA);
311     curr_offset += 2;
312 
313     proto_tree_add_expert(sub_tree, pinfo, &ei_gsm_r_uus1_not_implemented_yet, tvb, curr_offset, len);
314     curr_offset += len;
315 
316     return (curr_offset - offset);
317 }
318 
319 /*
320  * 5.5 Presentation of text strings
321  * (not implemented now)
322  */
323 static guint16
de_gsm_r_uus1_text_str(tvbuff_t * tvb,proto_tree * tree,packet_info * pinfo,guint32 offset)324 de_gsm_r_uus1_text_str(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, guint32 offset)
325 {
326     guint32	curr_offset;
327     guint32	len;
328     proto_item *item;
329     proto_tree *sub_tree;
330 
331     curr_offset = offset;
332 
333     len = tvb_get_guint8(tvb, offset+1);
334 
335     item = proto_tree_add_item(tree, hf_gsm_r_uus1_present_text_str, tvb, curr_offset, len+2, ENC_NA);
336     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_present_text_str);
337 
338     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, curr_offset, 1, ENC_NA);
339     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_len, tvb, curr_offset+1, 1, ENC_NA);
340     curr_offset += 2;
341 
342     proto_tree_add_expert(sub_tree, pinfo, &ei_gsm_r_uus1_not_implemented_yet, tvb, curr_offset, len);
343     curr_offset += len;
344 
345     return (curr_offset - offset);
346 }
347 
348 /*
349  * 5.6 Transfer of train position (eLDA)
350  *
351  */
352 static const true_false_string gsm_r_uus1_elda_lat_hem = {
353     "North",
354     "South"
355 };
356 
357 static const true_false_string gsm_r_uus1_elda_long_hem = {
358     "West",
359     "East"
360 };
361 
362 static const value_string gsm_r_uus1_elda_scale_vals[] = {
363     { 0,  "10 cm resolution" },
364     { 1,  "1 metre resolution" },
365     { 2,  "10 metre resolution" },
366     { 3,  "Odometry information not valid" },
367     { 0, NULL }
368 };
369 
370 
371 static guint16
de_gsm_r_uus1_elda(tvbuff_t * tvb,proto_tree * tree,packet_info * pinfo _U_,guint32 offset)372 de_gsm_r_uus1_elda(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo _U_, guint32 offset)
373 {
374     guint32	curr_offset;
375     guint   bit_offset;
376 
377     guint32 val;
378     guint32 lat_deg_val;
379     guint32 lat_min_val;
380     guint32 lat_sec_val;
381     guint32 lat_hem_val;
382 
383     guint32 long_deg_val;
384     guint32 long_min_val;
385     guint32 long_sec_val;
386     guint32 long_hem_val;
387 
388     guint32 t_val;
389 
390     proto_item *item;
391     proto_tree *sub_tree;
392 
393     proto_item *lat_item;
394     proto_item *long_item;
395     proto_tree *lat_tree;
396     proto_tree *long_tree;
397 
398     curr_offset = offset;
399 
400     item = proto_tree_add_item(tree, hf_gsm_r_uus1_elda, tvb, curr_offset, 16, ENC_NA);
401     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_elda);
402     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, curr_offset, 1, ENC_NA);
403     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_len, tvb, curr_offset+1, 1, ENC_NA);
404     curr_offset += 2;
405 
406     bit_offset = curr_offset * 8;
407 
408     /* Latitude */
409     lat_item = proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elda_lat, tvb, curr_offset, 4, ENC_NA);
410     lat_tree = proto_item_add_subtree(lat_item, ett_gsm_r_uus1_elda_lat);
411 
412     val = tvb_get_guint32(tvb, curr_offset, ENC_NA);
413     lat_deg_val = tvb_get_bits(tvb, bit_offset, 7, ENC_NA);
414     bit_offset += 7;
415     lat_min_val = tvb_get_bits(tvb, bit_offset, 6, ENC_NA);
416     bit_offset += 6;
417     lat_sec_val = tvb_get_bits(tvb, bit_offset, 13, ENC_NA);
418     bit_offset += 13;
419     lat_hem_val = tvb_get_bits(tvb, bit_offset, 1, ENC_NA);
420     bit_offset += 1;
421 
422     proto_tree_add_uint(lat_tree, hf_gsm_r_uus1_elda_lat_deg, tvb, curr_offset, 4, val);
423     proto_tree_add_uint(lat_tree, hf_gsm_r_uus1_elda_lat_min, tvb, curr_offset, 4, val);
424     proto_tree_add_uint_format_value(lat_tree, hf_gsm_r_uus1_elda_lat_sec, tvb, curr_offset, 4, val, "%.2f", (float)(lat_sec_val)/100);
425     proto_tree_add_boolean(lat_tree, hf_gsm_r_uus1_elda_lat_hem, tvb, curr_offset, 4, val);
426 
427     proto_item_set_text(lat_item, "Latitude: %d %d\'%.2f\"%s", lat_deg_val, lat_min_val, (float)(lat_sec_val)/100,
428         lat_hem_val ? "N" : "S");
429 
430     curr_offset += 3;
431 
432     /* Longitude */
433     long_item = proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elda_long, tvb, curr_offset, 4, ENC_NA);
434     long_tree = proto_item_add_subtree(long_item, ett_gsm_r_uus1_elda_long);
435 
436     val = tvb_get_guint32(tvb, curr_offset, ENC_NA);
437     long_deg_val = tvb_get_bits(tvb, bit_offset, 8, ENC_NA);
438     bit_offset += 8;
439     long_min_val = tvb_get_bits(tvb, bit_offset, 6, ENC_NA);
440     bit_offset += 6;
441     long_sec_val = tvb_get_bits(tvb, bit_offset, 13, ENC_NA);
442     bit_offset += 13;
443     long_hem_val = tvb_get_bits(tvb, bit_offset, 1, ENC_NA);
444     bit_offset += 1;
445 
446     proto_tree_add_uint(long_tree, hf_gsm_r_uus1_elda_long_deg, tvb, curr_offset, 4, val);
447     proto_tree_add_uint(long_tree, hf_gsm_r_uus1_elda_long_min, tvb, curr_offset, 4, val);
448     proto_tree_add_uint_format_value(long_tree, hf_gsm_r_uus1_elda_long_sec, tvb, curr_offset, 4, val, "%.2f", (float)(long_sec_val)/100);
449     proto_tree_add_boolean(long_tree, hf_gsm_r_uus1_elda_long_hem, tvb, curr_offset, 4, val);
450 
451     proto_item_set_text(long_item, "Longitude: %d %d\'%.2f\"%s", long_deg_val, long_min_val, (float)(long_sec_val)/100,
452         long_hem_val ? "W" : "E");
453 
454     curr_offset += 3;
455 
456     /* Height, Speed, Heading */
457 
458     // height step 1m. Range: -100m...+4500m
459     t_val = tvb_get_bits(tvb, bit_offset, 13, ENC_NA) - 100;
460     proto_tree_add_int(sub_tree, hf_gsm_r_uus1_elda_height, tvb, curr_offset, 4, t_val);
461     bit_offset += 13;
462 
463     // speed step 10 km/h
464     t_val = tvb_get_bits(tvb, bit_offset, 6, ENC_NA) * 10;
465     proto_tree_add_uint(sub_tree, hf_gsm_r_uus1_elda_speed, tvb, curr_offset, 4, t_val);
466     bit_offset += 6;
467 
468     // heading step 10 deg
469     t_val = tvb_get_bits(tvb, bit_offset, 6, ENC_NA) * 10;
470     proto_tree_add_uint(sub_tree, hf_gsm_r_uus1_elda_heading, tvb, curr_offset, 4, t_val);
471     bit_offset += 6;
472 
473     curr_offset += 4;
474 
475     /* Elapsed Time, Distance, Scale, Spare */
476 
477     // time step 1 second. Range: 0...2047
478     t_val = tvb_get_bits(tvb, bit_offset, 11, ENC_NA);
479     proto_tree_add_uint(sub_tree, hf_gsm_r_uus1_elda_e_time, tvb, curr_offset, 4, t_val);
480     bit_offset += 11;
481 
482     // distance step = 10 cm, 1 m or 10 m depending on the parameter Scale
483     t_val = tvb_get_bits(tvb, bit_offset, 14, ENC_NA);
484     proto_tree_add_uint(sub_tree, hf_gsm_r_uus1_elda_distance, tvb, curr_offset, 4, t_val);
485     bit_offset += 14;
486 
487     // scale
488     t_val = tvb_get_bits(tvb, bit_offset, 2, ENC_NA);
489     proto_tree_add_uint(sub_tree, hf_gsm_r_uus1_elda_scale, tvb, curr_offset, 4, t_val);
490     bit_offset += 2;
491 
492     // spare
493     t_val = tvb_get_bits(tvb, bit_offset, 5, ENC_NA);
494     proto_tree_add_uint(sub_tree, hf_gsm_r_uus1_elda_spare, tvb, curr_offset, 4, t_val);
495 
496     return 16;
497 }
498 
499 /*
500  * 5.7 Notification DSD alarm condition
501  *
502  */
503 static guint16
de_gsm_r_uus1_dsd_alarm(tvbuff_t * tvb,proto_tree * tree,packet_info * pinfo _U_,guint32 offset)504 de_gsm_r_uus1_dsd_alarm(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo _U_, guint32 offset)
505 {
506     guint32	curr_offset;
507     guint32	len;
508     const gchar *loco_engine_number;
509     proto_item *item;
510     proto_tree *sub_tree;
511 
512     curr_offset = offset;
513 
514     len = tvb_get_guint8(tvb, offset+1);
515 
516     item = proto_tree_add_item(tree, hf_gsm_r_uus1_present_dsd_alarm, tvb, curr_offset, len+2, ENC_NA);
517     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_present_dsd_alarm);
518 
519     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, curr_offset, 1, ENC_NA);
520     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_len, tvb, curr_offset+1, 1, ENC_NA);
521     curr_offset += 2;
522 
523     loco_engine_number = tvb_bcd_dig_to_str(pinfo->pool, tvb, offset+2, len, NULL, FALSE);
524     proto_tree_add_string(sub_tree, hf_gsm_r_uus1_present_dsd_alarm_loco_number, tvb, curr_offset, len, loco_engine_number);
525     proto_item_append_text(item, ": %s", loco_engine_number);
526     curr_offset += len;
527 
528     return (curr_offset - offset);
529 }
530 
531 
532 /*
533  * 5.8 Notification of a request to alert a controller
534  *
535  */
536 static guint16
de_gsm_r_uus1_alert_controller(tvbuff_t * tvb,proto_tree * tree,packet_info * pinfo _U_,guint32 offset)537 de_gsm_r_uus1_alert_controller(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo _U_, guint32 offset)
538 {
539     guint32	curr_offset;
540     guint32	len;
541     proto_item *item;
542     proto_tree *sub_tree;
543     char       *gref_str;
544 
545     curr_offset = offset;
546 
547     len = tvb_get_guint8(tvb, offset+1);
548 
549     item = proto_tree_add_item(tree, hf_gsm_r_uus1_alert_controller, tvb, curr_offset+2, len, ENC_NA);
550     sub_tree = proto_item_add_subtree(item, ett_gsm_r_uus1_alert_controller);
551 
552     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_tag, tvb, curr_offset, 1, ENC_NA);
553     proto_tree_add_item(sub_tree, hf_gsm_r_uus1_elem_len, tvb, curr_offset+1, 1, ENC_NA);
554     curr_offset += 2;
555 
556     proto_tree_add_item_ret_display_string(sub_tree, hf_gsm_r_uus1_alert_controller_gref, tvb, curr_offset, 4, ENC_BCD_DIGITS_0_9, pinfo->pool, &gref_str);
557     proto_item_append_text(item, ": %s", gref_str);
558     curr_offset += 4;
559 
560     return (curr_offset - offset);
561 }
562 
563 
564 static int
dissect_gsm_r_uus1(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)565 dissect_gsm_r_uus1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
566 {
567     guint8  elem_tag;
568     guint32	offset;
569     guint32	len;
570     proto_item   *gsm_r_uus1_item   = NULL;
571     proto_tree   *gsm_r_uus1_tree   = NULL;
572 
573     offset = 0;
574     len = tvb_captured_length(tvb);
575 
576     gsm_r_uus1_item =
577         proto_tree_add_protocol_format(tree, proto_gsm_r_uus1, tvb, 0, len, "GSM-R User-to-User Signaling");
578 
579     gsm_r_uus1_tree = proto_item_add_subtree(gsm_r_uus1_item, ett_gsm_r_uus1);
580 
581     while (offset < len){
582         elem_tag = tvb_get_guint8(tvb, offset);
583         switch (elem_tag) {
584         case 2:
585         case 3:
586             offset += de_gsm_r_uus1_chpc(tvb, gsm_r_uus1_tree, offset);
587             break;
588         case 5:
589             offset += de_gsm_r_uus1_pfn(tvb, gsm_r_uus1_tree, offset);
590             break;
591         case 6:
592         case 7:
593         case 8:
594             offset += de_gsm_r_uus1_elda(tvb, gsm_r_uus1_tree, pinfo, offset);
595             break;
596         case 9:
597             offset += de_gsm_r_uus1_epfn(tvb, gsm_r_uus1_tree, pinfo, offset);
598             break;
599         case 10:
600             offset += de_gsm_r_uus1_text_str(tvb, gsm_r_uus1_tree, pinfo, offset);
601             break;
602         case 11:
603              offset += de_gsm_r_uus1_dsd_alarm(tvb, gsm_r_uus1_tree, pinfo, offset);
604             break;
605         case 12:
606             offset += de_gsm_r_uus1_alert_controller(tvb, gsm_r_uus1_tree, pinfo, offset);
607             break;
608         default:
609             return offset;
610         }
611     }
612 
613     return offset;
614 }
615 
616 /* heuristic dissector */
617 static gboolean
dissect_gsm_r_uus1_heur(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)618 dissect_gsm_r_uus1_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
619 {
620     if (dissect_gsm_r_uus1(tvb, pinfo, tree, NULL) > 0)
621         return FALSE;
622 
623     return TRUE;
624 }
625 
626 
627 /* Register the protocol with Wireshark */
628 void
proto_register_gsm_r_uus1(void)629 proto_register_gsm_r_uus1(void)
630 {
631     /* Setup list of header fields */
632 
633     static hf_register_info hf[] = {
634         { &hf_gsm_r_uus1_elem_tag,
635           { "Element tag", "gsm-r-uus1.elem_tag",
636             FT_UINT8, BASE_DEC, VALS(gsm_r_uus1_tags), 0x0,
637             NULL, HFILL }
638         },
639         { &hf_gsm_r_uus1_elem_len,
640           { "Length", "gsm-r-uus1.elem_len",
641             FT_UINT8, BASE_DEC, NULL, 0x0,
642             NULL, HFILL }
643         },
644         { &hf_gsm_r_uus1_pfn,
645           { "Presentation of Functional Number (PFN)", "gsm-r-uus1.pfn",
646             FT_NONE, BASE_NONE, NULL, 0x0,
647             NULL, HFILL }
648         },
649         { &hf_gsm_r_uus1_pfn_digits,
650           { "Digits", "gsm-r-uus1.pfn.digits",
651             FT_STRING, BASE_NONE, NULL, 0x0,
652             NULL, HFILL }
653         },
654         { &hf_gsm_r_uus1_chpc,
655           { "Confirmation of High Priority Calls (CHPC)", "gsm-r-uus1.chpc",
656             FT_NONE, BASE_NONE, NULL, 0x0,
657             NULL, HFILL }
658         },
659         { &hf_gsm_r_uus1_chpc_t_dur,
660           { "Duration of the call", "gsm-r-uus1.chpc.t_dur",
661             FT_UINT24, BASE_DEC, NULL, 0x0,
662             NULL, HFILL }
663         },
664         { &hf_gsm_r_uus1_chpc_t_rel,
665           { "Interval between the end of the call and the transmission of the confirmation message", "gsm-r-uus1.chpc.t_rel",
666             FT_UINT32, BASE_DEC, NULL, 0x0,
667             NULL, HFILL }
668         },
669         { &hf_gsm_r_uus1_chpc_pl_call,
670           { "Priority level of the call", "gsm-r-uus1.chpc.pl_call",
671             FT_UINT8, BASE_DEC|BASE_RANGE_STRING, RVALS(gsm_r_uus1_chpc_priority_vals), 0x0,
672             NULL, HFILL }
673         },
674         { &hf_gsm_r_uus1_chpc_cause,
675           { "Reason for termination of the call", "gsm-r-uus1.chpc.cause",
676             FT_UINT8, BASE_HEX, NULL, 0x0,
677             NULL, HFILL }
678         },
679         { &hf_gsm_r_uus1_chpc_cause_power,
680           { "Mobile was powered off when receiving (power fail)", "gsm-r-uus1.chpc.cause.power",
681             FT_BOOLEAN, 8, NULL, 0x01,
682             NULL, HFILL }
683         },
684         { &hf_gsm_r_uus1_chpc_cause_radio,
685           { "Call was interrupted due to radio link error", "gsm-r-uus1.chpc.cause.radio",
686             FT_BOOLEAN, 8, NULL, 0x02,
687             NULL, HFILL }
688         },
689         { &hf_gsm_r_uus1_chpc_cause_reserved3,
690           { "Reserved", "gsm-r-uus1.chpc.cause.reserved3",
691             FT_BOOLEAN, 8, NULL, 0x04,
692             NULL, HFILL }
693         },
694         { &hf_gsm_r_uus1_chpc_cause_reserved4,
695           { "Reserved", "gsm-r-uus1.chpc.cause.reserved4",
696             FT_BOOLEAN, 8, NULL, 0x08,
697             NULL, HFILL }
698         },
699         { &hf_gsm_r_uus1_chpc_cause_user_command,
700           { "Call was left on user command", "gsm-r-uus1.chpc.cause.user_command",
701             FT_BOOLEAN, 8, NULL, 0x10,
702             NULL, HFILL }
703         },
704         { &hf_gsm_r_uus1_chpc_cause_reserved6,
705           { "Reserved", "gsm-r-uus1.chpc.cause.reserved6",
706             FT_BOOLEAN, 8, NULL, 0x20,
707             NULL, HFILL }
708         },
709         { &hf_gsm_r_uus1_chpc_cause_reserved7,
710           { "Reserved", "gsm-r-uus1.chpc.cause.reserved7",
711             FT_BOOLEAN, 8, NULL, 0x40,
712             NULL, HFILL }
713         },
714         { &hf_gsm_r_uus1_chpc_cause_reserved8,
715           { "Reserved", "gsm-r-uus1.chpc.cause.reserved8",
716             FT_BOOLEAN, 8, NULL, 0x80,
717             NULL, HFILL }
718         },
719         { &hf_gsm_r_uus1_chpc_gref,
720           { "Group call reference", "gsm-r-uus1.chpc.gref",
721             FT_STRING, BASE_NONE, NULL, 0x0,
722             NULL, HFILL }
723         },
724         { &hf_gsm_r_uus1_chpc_ack_cause,
725           { "ACK/CAUSE", "gsm-r-uus1.chpc.ack_cause",
726             FT_UINT8, BASE_DEC|BASE_RANGE_STRING, RVALS(gsm_r_uus1_chpc_ack_cause_vals), 0x0,
727             NULL, HFILL }
728         },
729         { &hf_gsm_r_uus1_epfn,
730           { "Enhanced presentation of functional number (ePFN)", "gsm-r-uus1.epfn",
731             FT_BYTES, BASE_NONE, NULL, 0x0,
732             NULL, HFILL }
733         },
734         { &hf_gsm_r_uus1_present_text_str,
735           { "Presentation of text strings", "gsm-r-uus1.present_text_str",
736             FT_BYTES, BASE_NONE, NULL, 0x0,
737             NULL, HFILL }
738         },
739         { &hf_gsm_r_uus1_elda,
740           { "Train position (eLDA)", "gsm-r-uus1.elda",
741             FT_NONE, BASE_NONE, NULL, 0x0,
742             NULL, HFILL }
743         },
744         { &hf_gsm_r_uus1_elda_lat,
745           { "Latitude", "gsm-r-uus1.elda.lat",
746             FT_NONE, BASE_NONE, NULL, 0x0,
747             NULL, HFILL }
748         },
749         { &hf_gsm_r_uus1_elda_lat_deg,
750           { "Degrees", "gsm-r-uus1.elda.lat.deg",
751             FT_UINT32, BASE_DEC, NULL, 0xFE000000,
752             NULL, HFILL }
753         },
754         { &hf_gsm_r_uus1_elda_lat_min,
755           { "Minutes", "gsm-r-uus1.elda.lat.min",
756             FT_UINT32, BASE_DEC, NULL, 0x01F80000,
757             NULL, HFILL }
758         },
759         { &hf_gsm_r_uus1_elda_lat_sec,
760           { "Seconds", "gsm-r-uus1.elda.lat.sec",
761             FT_UINT32, BASE_DEC, NULL, 0x0007FFC0,
762             NULL, HFILL }
763         },
764         { &hf_gsm_r_uus1_elda_lat_hem,
765           { "Hemisphere", "gsm-r-uus1.elda.lat.hem",
766             FT_BOOLEAN, 32, TFS(&gsm_r_uus1_elda_lat_hem), 0x00000020,
767             NULL, HFILL }
768         },
769         { &hf_gsm_r_uus1_elda_long,
770           { "Latitude", "gsm-r-uus1.elda.long",
771             FT_NONE, BASE_NONE, NULL, 0x0,
772             NULL, HFILL }
773         },
774         { &hf_gsm_r_uus1_elda_long_deg,
775           { "Degrees", "gsm-r-uus1.elda.long.deg",
776             FT_UINT32, BASE_DEC, NULL, 0x1FE00000,
777             NULL, HFILL }
778         },
779         { &hf_gsm_r_uus1_elda_long_min,
780           { "Minutes", "gsm-r-uus1.elda.long.min",
781             FT_UINT32, BASE_DEC, NULL, 0x001F8000,
782             NULL, HFILL }
783         },
784         { &hf_gsm_r_uus1_elda_long_sec,
785           { "Seconds", "gsm-r-uus1.elda_long.sec",
786             FT_UINT32, BASE_DEC, NULL, 0x00007FFC,
787             NULL, HFILL }
788         },
789         { &hf_gsm_r_uus1_elda_long_hem,
790           { "Hemisphere", "gsm-r-uus1.elda_long.hem",
791             FT_BOOLEAN, 32, TFS(&gsm_r_uus1_elda_long_hem), 0x00000002,
792             NULL, HFILL }
793         },
794         { &hf_gsm_r_uus1_elda_height,
795           { "Height (m)", "gsm-r-uus1.elda.height",
796             FT_INT32, BASE_DEC, NULL, 0x0,
797             NULL, HFILL }
798         },
799         { &hf_gsm_r_uus1_elda_speed,
800           { "Speed (km/h)", "gsm-r-uus1.elda.speed",
801             FT_UINT32, BASE_DEC, NULL, 0x0,
802             NULL, HFILL }
803         },
804         { &hf_gsm_r_uus1_elda_heading,
805           { "Heading (deg)", "gsm-r-uus1.elda.heading",
806             FT_UINT8, BASE_DEC, NULL, 0x0,
807             NULL, HFILL }
808         },
809         { &hf_gsm_r_uus1_elda_e_time,
810           { "Elapsed Time (sec)", "gsm-r-uus1.elda.e_time",
811             FT_UINT32, BASE_DEC, NULL, 0x0,
812             NULL, HFILL }
813         },
814         { &hf_gsm_r_uus1_elda_distance,
815           { "Distance", "gsm-r-uus1.elda.distance",
816             FT_UINT32, BASE_DEC, NULL, 0x0,
817             NULL, HFILL }
818         },
819         { &hf_gsm_r_uus1_elda_scale,
820           { "Scale", "gsm-r-uus1.elda.scale",
821             FT_UINT32, BASE_DEC, VALS(gsm_r_uus1_elda_scale_vals), 0x00,
822             NULL, HFILL }
823         },
824         { &hf_gsm_r_uus1_elda_spare,
825           { "Spare", "gsm-r-uus1.elda.spare",
826             FT_UINT32, BASE_DEC, NULL, 0x0,
827             NULL, HFILL }
828         },
829         { &hf_gsm_r_uus1_present_dsd_alarm,
830           { "Notification DSD alarm condition", "gsm-r-uus1.present_dsd_alarm",
831             FT_NONE, BASE_NONE, NULL, 0x0,
832             NULL, HFILL }
833         },
834         { &hf_gsm_r_uus1_present_dsd_alarm_loco_number,
835           { "Locomotive engine number", "gsm-r-uus1.present_dsd_alarm.loco_number",
836             FT_STRING, BASE_NONE, NULL, 0x0,
837             NULL, HFILL }
838         },
839         { &hf_gsm_r_uus1_alert_controller,
840           { "Notification of a request to alert a controller", "gsm-r-uus1.alert_controller",
841             FT_NONE, BASE_NONE, NULL, 0x0,
842             NULL, HFILL }
843         },
844         { &hf_gsm_r_uus1_alert_controller_gref,
845           { "Group call reference", "gsm-r-uus1.alert_controller.gref",
846             FT_STRING, BASE_NONE, NULL, 0x0,
847             NULL, HFILL }
848         },
849     };
850 
851     static ei_register_info ei[] = {
852         { &ei_gsm_r_uus1_not_implemented_yet, { "gsm-r-uus1.not_implemented_yet", PI_UNDECODED, PI_NOTE, "Not implemented yet", EXPFILL }},
853     };
854 
855     expert_module_t* expert_gsm_r_uus1;
856 
857     static gint *ett[] = {
858         &ett_gsm_r_uus1,
859         &ett_gsm_r_uus1_pfn,
860         &ett_gsm_r_uus1_chpc,
861         &ett_gsm_r_uus1_chpc_cause,
862         &ett_gsm_r_uus1_epfn,
863         &ett_gsm_r_uus1_present_text_str,
864         &ett_gsm_r_uus1_elda,
865         &ett_gsm_r_uus1_elda_lat,
866         &ett_gsm_r_uus1_elda_long,
867         &ett_gsm_r_uus1_present_dsd_alarm,
868         &ett_gsm_r_uus1_alert_controller
869     };
870 
871     module_t *gsm_r_uus1_module;
872 
873     /* Register the protocol name and description */
874     proto_gsm_r_uus1 =
875         proto_register_protocol("GSM-R User-to-User Signaling", "GSM-R", "gsm-r-uus1");
876 
877     proto_register_field_array(proto_gsm_r_uus1, hf, array_length(hf));
878 
879     proto_register_subtree_array(ett, array_length(ett));
880 
881     expert_gsm_r_uus1 = expert_register_protocol(proto_gsm_r_uus1);
882     expert_register_field_array(expert_gsm_r_uus1, ei, array_length(ei));
883 
884     /* subdissector code */
885     register_dissector("gsm-r-uus1", dissect_gsm_r_uus1, proto_gsm_r_uus1);
886 
887     gsm_r_uus1_module = prefs_register_protocol(proto_gsm_r_uus1, proto_reg_handoff_gsm_r_uus1);
888     prefs_register_bool_preference(gsm_r_uus1_module, "dissect_q931_u2u",
889         "Dissect Q.931 User-To-User information",
890         "Dissect Q.931 User-To-User information",
891         &q931_u2u);
892     prefs_register_bool_preference(gsm_r_uus1_module, "dissect_gsm_a_u2u",
893         "Dissect GSM-A User-To-User information",
894         "Dissect GSM-A User-To-User information",
895         &gsm_a_u2u);
896 }
897 
898 void
proto_reg_handoff_gsm_r_uus1(void)899 proto_reg_handoff_gsm_r_uus1(void)
900 {
901     dissector_handle_t gsm_r_uus1_handle;
902 
903     gsm_r_uus1_handle = find_dissector("gsm-r-uus1");
904 
905     if(q931_u2u){
906         heur_dissector_add("q931_user", dissect_gsm_r_uus1_heur, "GSM-R over UUS1", "gsm_r_uus1", proto_gsm_r_uus1, HEURISTIC_ENABLE);
907     } else {
908         heur_dissector_delete("q931_user", dissect_gsm_r_uus1_heur, proto_gsm_r_uus1);
909     }
910 
911 
912     if(gsm_a_u2u){
913         dissector_add_uint("gsm_a.dtap.u2u_prot_discr", 0, gsm_r_uus1_handle);
914     } else {
915         dissector_delete_uint("gsm_a.dtap.u2u_prot_discr", 0, gsm_r_uus1_handle);
916     }
917 }
918 
919 /*
920  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
921  *
922  * Local variables:
923  * c-basic-offset: 4
924  * tab-width: 8
925  * indent-tabs-mode: nil
926  * End:
927  *
928  * vi: set shiftwidth=4 tabstop=8 expandtab:
929  * :indentSize=4:tabSize=8:noTabs=true:
930  */
931