Kerberos: add kerberos_inject_longterm_key() helper function
[wireshark-sm.git] / ui / tap-rlc-graph.c
blobae3972f2121ef97643f368c985388e3d564a9bd9
1 /* tap-rlc-graph.c
2 * LTE RLC channel graph info
4 * Originally from tcp_graph.c by Pavel Mores <pvl@uh.cz>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
15 #include <stdlib.h>
17 #include "tap-rlc-graph.h"
19 #include <file.h>
20 #include <frame_tvbuff.h>
22 #include <epan/epan_dissect.h>
23 #include <epan/tap.h>
25 /* Return true if the 2 sets of parameters refer to the same channel. */
26 bool compare_rlc_headers(uint8_t rat1, uint8_t rat2,
27 uint16_t ueid1, uint16_t channelType1, uint16_t channelId1, uint8_t rlcMode1, uint8_t direction1,
28 uint16_t ueid2, uint16_t channelType2, uint16_t channelId2, uint8_t rlcMode2, uint8_t direction2,
29 bool frameIsControl)
31 if (rat1 != rat2) {
32 return false;
35 /* Same direction, data - OK. */
36 if (!frameIsControl) {
37 return (direction1 == direction2) &&
38 (ueid1 == ueid2) &&
39 (channelType1 == channelType2) &&
40 (channelId1 == channelId2) &&
41 (rlcMode1 == rlcMode2);
43 else {
44 /* Control case */
45 if ((rlcMode1 == RLC_AM_MODE) && (rlcMode2 == RLC_AM_MODE)) {
46 return ((direction1 != direction2) &&
47 (ueid1 == ueid2) &&
48 (channelType1 == channelType2) &&
49 (channelId1 == channelId2));
51 else {
52 return false;
57 /* This is the tap function used to identify a list of channels found in the current frame. It is only used for the single,
58 currently selected frame. */
59 static tap_packet_status
60 tap_lte_rlc_packet(void *pct, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags _U_)
62 int n;
63 bool is_unique = true;
64 th_t *th = (th_t *)pct;
65 const rlc_3gpp_tap_info *header = (const rlc_3gpp_tap_info*)vip;
67 /* Check new header details against any/all stored ones */
68 for (n=0; n < th->num_hdrs; n++) {
69 rlc_3gpp_tap_info *stored = th->rlchdrs[n];
71 if (compare_rlc_headers(stored->rat, header->rat,
72 stored->ueid, stored->channelType, stored->channelId, stored->rlcMode, stored->direction,
73 header->ueid, header->channelType, header->channelId, header->rlcMode, header->direction,
74 header->isControlPDU)) {
75 is_unique = false;
76 break;
80 /* Add address if unique and have space for it */
81 if (is_unique && (th->num_hdrs < MAX_SUPPORTED_CHANNELS)) {
82 /* Copy the tap struct in as next header */
83 /* Need to take a deep copy of the tap struct, it may not be valid
84 to read after this function returns? */
85 th->rlchdrs[th->num_hdrs] = g_new(rlc_3gpp_tap_info,1);
86 *(th->rlchdrs[th->num_hdrs]) = *header;
88 /* Store in direction of data though... */
89 if (th->rlchdrs[th->num_hdrs]->isControlPDU) {
90 th->rlchdrs[th->num_hdrs]->direction = !th->rlchdrs[th->num_hdrs]->direction;
92 th->num_hdrs++;
95 return TAP_PACKET_DONT_REDRAW; /* i.e. no immediate redraw requested */
98 /* Return an array of tap_info structs that were found while dissecting the current frame
99 * in the packet list. Errors are passed back to the caller, as they will be reported differently
100 * depending upon which GUI toolkit is being used. */
101 rlc_3gpp_tap_info* select_rlc_lte_session(capture_file *cf,
102 struct rlc_segment *hdrs,
103 char **err_msg)
105 frame_data *fdata;
106 epan_dissect_t edt;
107 dfilter_t *sfcode;
109 GString *error_string;
110 nstime_t rel_ts;
111 /* Initialised to no known channels */
112 th_t th = {0, {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}};
114 if (cf->state == FILE_CLOSED) {
115 return NULL;
118 /* No real filter yet */
119 if (!dfilter_compile("rlc-lte or rlc-nr", &sfcode, NULL)) {
120 return NULL;
123 /* Dissect the data from the current frame. */
124 if (!cf_read_current_record(cf)) {
125 dfilter_free(sfcode);
126 return NULL; /* error reading the record */
129 fdata = cf->current_frame;
131 /* Set tap listener that will populate th. */
132 error_string = register_tap_listener("rlc-3gpp", &th, NULL, 0, NULL, tap_lte_rlc_packet, NULL, NULL);
133 if (error_string){
134 fprintf(stderr, "wireshark: Couldn't register rlc_lte_graph tap: %s\n",
135 error_string->str);
136 g_string_free(error_string, TRUE);
137 dfilter_free(sfcode);
138 exit(1); /* XXX: fix this */
141 epan_dissect_init(&edt, cf->epan, true, false);
142 epan_dissect_prime_with_dfilter(&edt, sfcode);
143 epan_dissect_run_with_taps(&edt, cf->cd_t, &cf->rec,
144 frame_tvbuff_new_buffer(&cf->provider, fdata, &cf->buf),
145 fdata, NULL);
146 rel_ts = edt.pi.rel_ts;
147 epan_dissect_cleanup(&edt);
148 remove_tap_listener(&th);
150 if (th.num_hdrs == 0){
151 /* This "shouldn't happen", as the graph menu items won't
152 * even be enabled if the selected packet isn't an RLC PDU.
154 *err_msg = g_strdup("Selected packet doesn't have an RLC PDU");
155 return NULL;
157 /* XXX fix this later, we should show a dialog allowing the user
158 * to select which session he wants here */
159 if (th.num_hdrs>1){
160 /* Can only handle a single RLC channel yet */
161 *err_msg = g_strdup("The selected packet has more than one LTE RLC channel in it.");
162 return NULL;
165 /* For now, still always choose the first/only one */
166 hdrs->num = fdata->num;
167 hdrs->rel_secs = rel_ts.secs;
168 hdrs->rel_usecs = rel_ts.nsecs/1000;
170 hdrs->rat = th.rlchdrs[0]->rat;
171 hdrs->ueid = th.rlchdrs[0]->ueid;
172 hdrs->channelType = th.rlchdrs[0]->channelType;
173 hdrs->channelId = th.rlchdrs[0]->channelId;
174 hdrs->rlcMode = th.rlchdrs[0]->rlcMode;
175 hdrs->isControlPDU = th.rlchdrs[0]->isControlPDU;
176 /* Flip direction if have control PDU */
177 hdrs->direction = !hdrs->isControlPDU ? th.rlchdrs[0]->direction : !th.rlchdrs[0]->direction;
179 return th.rlchdrs[0];
182 /* This is the tapping function to update stats when dissecting the whole packet list */
183 static tap_packet_status rlc_lte_tap_for_graph_data(void *pct, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags _U_)
185 struct rlc_graph *graph = (struct rlc_graph *)pct;
186 const rlc_3gpp_tap_info *rlchdr = (const rlc_3gpp_tap_info*)vip;
188 /* See if this one matches graph's channel */
189 if (compare_rlc_headers(graph->rat, rlchdr->rat,
190 graph->ueid, graph->channelType, graph->channelId, graph->rlcMode, graph->direction,
191 rlchdr->ueid, rlchdr->channelType, rlchdr->channelId, rlchdr->rlcMode, rlchdr->direction,
192 rlchdr->isControlPDU)) {
194 /* It matches. Copy segment details out of tap struct */
195 struct rlc_segment *segment = g_new(struct rlc_segment, 1);
196 segment->next = NULL;
197 segment->num = pinfo->num;
198 segment->rel_secs = (uint32_t) pinfo->rel_ts.secs;
199 segment->rel_usecs = pinfo->rel_ts.nsecs/1000;
201 segment->rat = rlchdr->rat;
202 segment->ueid = rlchdr->ueid;
203 segment->channelType = rlchdr->channelType;
204 segment->channelId = rlchdr->channelId;
205 segment->direction = rlchdr->direction;
206 segment->rlcMode = rlchdr->rlcMode;
207 segment->sequenceNumberLength = rlchdr->sequenceNumberLength;
209 segment->isControlPDU = rlchdr->isControlPDU;
211 if (!rlchdr->isControlPDU) {
212 if (rlchdr->sequenceNumberGiven) {
213 /* Data */
214 segment->SN = rlchdr->sequenceNumber;
215 segment->isResegmented = rlchdr->isResegmented;
216 segment->pduLength = rlchdr->pduLength;
218 else {
219 /* No sequence number, so not going to show at all */
220 g_free(segment);
221 return TAP_PACKET_DONT_REDRAW; /* i.e. no immediate redraw requested */
224 else {
225 /* Status PDU */
226 int n;
227 segment->ACKNo = rlchdr->ACKNo;
228 segment->noOfNACKs = rlchdr->noOfNACKs;
229 for (n=0; (n < rlchdr->noOfNACKs) && (n < MAX_NACKs); n++) {
230 segment->NACKs[n] = rlchdr->NACKs[n];
234 /* Add segment to end of list */
235 if (graph->segments) {
236 /* Add to end of existing last element */
237 graph->last_segment->next = segment;
238 } else {
239 /* Make this the first (only) segment */
240 graph->segments = segment;
243 /* This one is now the last one */
244 graph->last_segment = segment;
247 return TAP_PACKET_DONT_REDRAW; /* i.e. no immediate redraw requested */
250 /* If don't have a channel, try to get one from current frame, then read all frames looking for data
251 * for that channel. */
252 bool rlc_graph_segment_list_get(capture_file *cf, struct rlc_graph *g, bool stream_known,
253 char **err_string)
255 struct rlc_segment current;
256 GString *error_string;
258 if (!cf || !g) {
259 /* Really shouldn't happen */
260 return false;
263 if (!stream_known) {
264 struct rlc_3gpp_tap_info *header = select_rlc_lte_session(cf, &current, err_string);
265 if (!header) {
266 /* Didn't have a channel, and current frame didn't provide one */
267 return false;
269 g->channelSet = true;
271 g->rat = header->rat;
272 g->ueid = header->ueid;
273 g->channelType = header->channelType;
274 g->channelId = header->channelId;
275 g->rlcMode = header->rlcMode;
276 g->direction = header->direction;
280 /* Rescan all the packets and pick up all interesting RLC headers.
281 * We only filter for RLC frames here for speed and do the actual compare
282 * in the tap listener
285 g->last_segment = NULL;
286 error_string = register_tap_listener("rlc-3gpp", // tap name
288 "rlc-lte or rlc-nr", // filter name
289 0, NULL, rlc_lte_tap_for_graph_data, NULL, NULL);
290 if (error_string) {
291 fprintf(stderr, "wireshark: Couldn't register rlc_graph tap: %s\n",
292 error_string->str);
293 g_string_free(error_string, TRUE);
294 exit(1); /* XXX: fix this */
296 cf_retap_packets(cf);
297 remove_tap_listener(g);
299 if (g->last_segment == NULL) {
300 *err_string = g_strdup("No packets found");
301 return false;
304 return true;
307 /* Free and zero the segments list of an rlc_graph struct */
308 void rlc_graph_segment_list_free(struct rlc_graph * g)
310 struct rlc_segment *segment;
312 /* Free all segments */
313 while (g->segments) {
314 segment = g->segments->next;
315 g_free(g->segments);
316 g->segments = segment;