1 /* packet-gsmtap-log.c
2  * Routines for GSMTAP logging packets
3  *
4  * (C) 2016 by Harald Welte <laforge@gnumonks.org>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include "config.h"
14 
15 #include <epan/packet.h>
16 #include "packet-gsmtap.h"
17 
18 void proto_register_gsmtap_log(void);
19 void proto_reg_handoff_gsmtap_log(void);
20 
21 static int proto_gsmtap_log = -1;
22 
23 static int hf_log_ident = -1;
24 static int hf_log_subsys = -1;
25 static int hf_log_file_name = -1;
26 static int hf_log_file_line = -1;
27 static int hf_log_ts = -1;
28 static int hf_log_pid = -1;
29 static int hf_log_level = -1;
30 static int hf_log_string = -1;
31 
32 static int ett_gsmtap_log = -1;
33 
34 /* from libosmocore include/osmocom/core/logging.h */
35 static const value_string gsmtap_log_levels[] = {
36 	{ 1,	"DEBUG" },
37 	{ 3,	"INFO" },
38 	{ 5,	"NOTICE" },
39 	{ 7,	"ERROR" },
40 	{ 8,	"FATAL" },
41 	{ 0, NULL }
42 };
43 
44 /* dissect a GSMTAP header and hand payload off to respective dissector */
45 static int
dissect_gsmtap_log(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)46 dissect_gsmtap_log(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void * data _U_)
47 {
48 	proto_item *ti;
49 	proto_tree *log_tree;
50 	gint offset = 0;
51 	gint log_str_len;
52 	guint log_pid, log_level, log_src_line;
53 	const char *log_str;
54 	const guint8 *log_ident, *log_subsys, *log_src_fname;
55 
56 	ti = proto_tree_add_item(tree, proto_gsmtap_log, tvb, 0, -1, ENC_NA);
57 	log_tree = proto_item_add_subtree(ti, ett_gsmtap_log);
58 
59 	proto_tree_add_item(log_tree, hf_log_ts, tvb, offset, 8, ENC_TIME_SECS_USECS|ENC_BIG_ENDIAN);
60 	offset += 8;
61 	proto_tree_add_item_ret_string(log_tree, hf_log_ident, tvb, offset, 16, ENC_NA, pinfo->pool, &log_ident);
62 	offset += 16;
63 	proto_tree_add_item_ret_uint(log_tree, hf_log_pid, tvb, offset, 4, ENC_BIG_ENDIAN, &log_pid);
64 	offset += 4;
65 	proto_tree_add_item_ret_uint(log_tree, hf_log_level, tvb, offset++, 1, ENC_NA, &log_level);
66 	offset += 3; /* pad octets */
67 	proto_tree_add_item_ret_string(log_tree, hf_log_subsys, tvb, offset, 16, ENC_NA, pinfo->pool, &log_subsys);
68 	offset += 16;
69 	proto_tree_add_item_ret_string(log_tree, hf_log_file_name, tvb, offset, 32, ENC_NA, pinfo->pool, &log_src_fname);
70 	offset += 32;
71 	proto_tree_add_item_ret_uint(log_tree, hf_log_file_line, tvb, offset, 4, ENC_BIG_ENDIAN, &log_src_line);
72 	offset += 4;
73 
74 	/* actual log message */
75 	log_str_len = tvb_captured_length_remaining(tvb, offset);
76 	proto_tree_add_item(log_tree, hf_log_string, tvb, offset, log_str_len, ENC_ASCII|ENC_NA);
77 
78 	log_str = tvb_format_stringzpad_wsp(pinfo->pool, tvb, offset, log_str_len);
79 	col_append_str(pinfo->cinfo, COL_INFO, log_str);
80 
81 	proto_item_append_text(ti, " %s(%u): %s/%d: %s:%u %s",
82 			log_ident, log_pid, log_subsys, log_level,
83 			log_src_fname, log_src_line, log_str);
84 	return tvb_captured_length(tvb);
85 }
86 
87 void
proto_register_gsmtap_log(void)88 proto_register_gsmtap_log(void)
89 {
90 	static hf_register_info hf[] = {
91 		{ &hf_log_ident, { "Application", "gsmtap_log.ident",
92 		  FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
93 		{ &hf_log_subsys, { "Subsystem", "gsmtap_log.subsys",
94 		  FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
95 		{ &hf_log_file_name, { "Source File Name", "gsmtap_log.src_file.name",
96 		  FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
97 		{ &hf_log_file_line, { "Source File Line Number", "gsmtap_log.src_file.line_nr",
98 		  FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL } },
99 		{ &hf_log_ts, { "Timestamp", "gsmtap_log.timestamp",
100 		  FT_ABSOLUTE_TIME, ABSOLUTE_TIME_UTC, NULL, 0, NULL, HFILL } },
101 		{ &hf_log_pid, { "Process ID", "gsmtap_log.pid",
102 		  FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL } },
103 		{ &hf_log_level, { "Log Level", "gsmtap_log.level",
104 		  FT_UINT8, BASE_DEC, VALS(gsmtap_log_levels), 0, NULL, HFILL } },
105 		{ &hf_log_string, { "String", "gsmtap_log.string",
106 		  FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
107 	};
108 
109 	static gint *ett[] = {
110 		&ett_gsmtap_log,
111 	};
112 
113 	proto_gsmtap_log = proto_register_protocol("GSMTAP libosmocore logging", "GSMTAP-LOG", "gsmtap_log");
114 	proto_register_field_array(proto_gsmtap_log, hf, array_length(hf));
115 	proto_register_subtree_array(ett, array_length(ett));
116 }
117 
118 void
proto_reg_handoff_gsmtap_log(void)119 proto_reg_handoff_gsmtap_log(void)
120 {
121 	dissector_handle_t gsmtap_log_handle;
122 
123 	gsmtap_log_handle = create_dissector_handle(dissect_gsmtap_log, proto_gsmtap_log);
124 	dissector_add_uint("gsmtap.type", GSMTAP_TYPE_OSMOCORE_LOG, gsmtap_log_handle);
125 }
126 
127 /*
128  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
129  *
130  * Local variables:
131  * c-basic-offset: 8
132  * tab-width: 8
133  * indent-tabs-mode: t
134  * End:
135  *
136  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
137  * :indentSize=8:tabSize=8:noTabs=false:
138  */
139