MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-lapdm.c
blob8a8867bd5131254beb628b0bf42bc9214b5201e9
1 /* packet-lapdm.c
2 * Routines for LAPDm frame disassembly
3 * Duncan Salerno <duncan.salerno@googlemail.com>
5 * $Id$
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 /* LAPDm references:
28 * Mobile Station - Base Stations System (MS - BSS) Interface Data Link (DL) Layer Specification
29 * Base Station Controller - Base Transceiver Station (BSC - BTS) interface; Layer 2 specification
30 * http://www.3gpp.org/ftp/Specs/html-info/44006.htm
32 * From 3GPP TS 44.006:
34 * LAPDm is used for information sent on the control channels BCCH, AGCH, NCH,
35 * PCH, FACCH, SACCH and SDCCH as defined in 3GPP TS 44.003.
37 * AGCH, NCH and PCH are sometimes referred to by the collective name CCCH.
38 * FACCH, SACCH and SDCCH are, similarly, referred to by the collective name DCCH.
40 * Format A is used on DCCHs for frames where there is no information field.
41 * Formats B, Bter and B4 are used on DCCHs for frames containing an information field:
42 * Format Bter is used on request of higher layers if and only if short L2 header type 1 is
43 * supported and a UI command is to be transmitted on SAPI 0;
44 * Format B4 is used for UI frames transmitted by the network on SACCH;
45 * Format B is applied in all other cases.
46 * Format Bbis is used only on BCCH, PCH, NCH, and AGCH.
47 * In addition there is a Format C for transmission of random access signals.
49 * This module currently supports A, B, B4
50 * In the future will support Bter
51 * Bbis and C should be supported elsewhere
54 #include "config.h"
56 #include <glib.h>
57 #include <epan/packet.h>
58 #include <epan/prefs.h>
59 #include <epan/xdlc.h>
60 #include <epan/reassemble.h>
62 static int proto_lapdm = -1;
63 static int hf_lapdm_address = -1;
64 static int hf_lapdm_ea = -1;
65 static int hf_lapdm_cr = -1;
66 static int hf_lapdm_sapi = -1;
67 static int hf_lapdm_lpd = -1;
69 static int hf_lapdm_control = -1;
70 static int hf_lapdm_n_r = -1;
71 static int hf_lapdm_n_s = -1;
72 static int hf_lapdm_p = -1;
73 static int hf_lapdm_f = -1;
74 static int hf_lapdm_s_ftype = -1;
75 static int hf_lapdm_u_modifier_cmd = -1;
76 static int hf_lapdm_u_modifier_resp = -1;
77 static int hf_lapdm_ftype_i = -1;
78 static int hf_lapdm_ftype_s_u = -1;
80 static int hf_lapdm_length = -1;
81 static int hf_lapdm_el = -1;
82 static int hf_lapdm_m = -1;
83 static int hf_lapdm_len = -1;
86 * LAPDm fragment handling
88 static int hf_lapdm_fragments = -1;
89 static int hf_lapdm_fragment = -1;
90 static int hf_lapdm_fragment_overlap = -1;
91 static int hf_lapdm_fragment_overlap_conflicts = -1;
92 static int hf_lapdm_fragment_multiple_tails = -1;
93 static int hf_lapdm_fragment_too_long_fragment = -1;
94 static int hf_lapdm_fragment_error = -1;
95 static int hf_lapdm_fragment_count = -1;
96 static int hf_lapdm_reassembled_in = -1;
97 static int hf_lapdm_reassembled_length = -1;
99 static gint ett_lapdm = -1;
100 static gint ett_lapdm_address = -1;
101 static gint ett_lapdm_control = -1;
102 static gint ett_lapdm_length = -1;
103 static gint ett_lapdm_fragment = -1;
104 static gint ett_lapdm_fragments = -1;
106 static reassembly_table lapdm_reassembly_table;
108 static dissector_table_t lapdm_sapi_dissector_table;
110 static dissector_handle_t data_handle;
112 static gboolean reassemble_lapdm = TRUE;
115 * Bits in the address field.
117 #define LAPDM_SAPI 0x1c /* Service Access Point Identifier */
118 #define LAPDM_SAPI_SHIFT 2
119 #define LAPDM_CR 0x02 /* Command/Response bit */
120 #define LAPDM_EA 0x01 /* First Address Extension bit */
121 #define LAPDM_LPD 0x60 /* Link Protocol Discriminator */
124 * Bits in the length field.
126 #define LAPDM_EL 0x01 /* Extended Length = 1 */
127 #define LAPDM_M 0x02 /* More fragments */
128 #define LAPDM_M_SHIFT 1
129 #define LAPDM_LEN 0xfc /* Length */
130 #define LAPDM_LEN_SHIFT 2
132 #define LAPDM_HEADER_LEN 3
134 #define LAPDM_SAPI_RR_CC_MM 0
135 #define LAPDM_SAPI_SMS 3
137 /* Used only for U frames */
138 static const xdlc_cf_items lapdm_cf_items = {
139 &hf_lapdm_n_r,
140 &hf_lapdm_n_s,
141 &hf_lapdm_p,
142 &hf_lapdm_f,
143 &hf_lapdm_s_ftype,
144 &hf_lapdm_u_modifier_cmd,
145 &hf_lapdm_u_modifier_resp,
146 &hf_lapdm_ftype_i,
147 &hf_lapdm_ftype_s_u
150 static const value_string lapdm_ea_vals[] = {
151 { 0, "More octets" },
152 { 1, "Final octet" },
153 { 0, NULL }
156 static const value_string lapdm_sapi_vals[] = {
157 { LAPDM_SAPI_RR_CC_MM, "RR/MM/CC" },
158 { LAPDM_SAPI_SMS, "SMS/SS" },
159 { 0, NULL }
162 static const value_string lapdm_lpd_vals[] = {
163 { 0, "Normal GSM" },
164 { 1, "Cell broadcast service" },
165 { 0, NULL }
168 static const value_string lapdm_m_vals[] = {
169 { 0, "Last segment" },
170 { 1, "More segments" },
171 { 0, NULL }
174 static const value_string lapdm_el_vals[] = {
175 { 0, "More octets" },
176 { 1, "Final octet" },
177 { 0, NULL }
181 static const fragment_items lapdm_frag_items = {
182 /* Fragment subtrees */
183 &ett_lapdm_fragment,
184 &ett_lapdm_fragments,
185 /* Fragment fields */
186 &hf_lapdm_fragments,
187 &hf_lapdm_fragment,
188 &hf_lapdm_fragment_overlap,
189 &hf_lapdm_fragment_overlap_conflicts,
190 &hf_lapdm_fragment_multiple_tails,
191 &hf_lapdm_fragment_too_long_fragment,
192 &hf_lapdm_fragment_error,
193 &hf_lapdm_fragment_count,
194 /* Reassembled in field */
195 &hf_lapdm_reassembled_in,
196 /* Reassembled length field */
197 &hf_lapdm_reassembled_length,
198 /* Reassembled data field */
199 NULL,
200 /* Tag */
201 "fragments"
204 static void
205 lapdm_defragment_init (void)
207 reassembly_table_init (&lapdm_reassembly_table,
208 &addresses_reassembly_table_functions);
212 static void
213 dissect_lapdm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
215 proto_tree *lapdm_tree, *addr_tree, *length_tree;
216 proto_item *lapdm_ti, *addr_ti, *length_ti;
217 guint8 addr, length, cr, sapi, len/*, n_s*/;
218 int control;
219 gboolean m;
220 tvbuff_t *payload;
221 int available_length;
222 gboolean is_response = FALSE;
224 /* Check that there's enough data */
225 if (tvb_length(tvb) < LAPDM_HEADER_LEN)
226 return;
228 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPDm");
230 addr = tvb_get_guint8(tvb, 0);
231 length = tvb_get_guint8(tvb, 2);
233 cr = addr & LAPDM_CR;
234 if (pinfo->p2p_dir == P2P_DIR_RECV) {
235 is_response = cr ? FALSE : TRUE;
237 else if (pinfo->p2p_dir == P2P_DIR_SENT) {
238 is_response = cr ? TRUE : FALSE;
241 if (tree) {
242 lapdm_ti = proto_tree_add_item(tree, proto_lapdm, tvb, 0, LAPDM_HEADER_LEN, ENC_NA);
243 lapdm_tree = proto_item_add_subtree(lapdm_ti, ett_lapdm);
245 addr_ti = proto_tree_add_uint(lapdm_tree, hf_lapdm_address, tvb, 0, 1, addr);
246 addr_tree = proto_item_add_subtree(addr_ti, ett_lapdm_address);
248 proto_tree_add_uint(addr_tree, hf_lapdm_lpd, tvb, 0, 1, addr);
249 proto_tree_add_uint(addr_tree, hf_lapdm_sapi, tvb, 0, 1, addr);
250 proto_tree_add_uint(addr_tree, hf_lapdm_cr, tvb, 0, 1, addr);
251 proto_tree_add_uint(addr_tree, hf_lapdm_ea, tvb, 0, 1, addr);
253 else {
254 lapdm_ti = NULL;
255 lapdm_tree = NULL;
258 control = dissect_xdlc_control(tvb, 1, pinfo, lapdm_tree, hf_lapdm_control,
259 ett_lapdm_control, &lapdm_cf_items, NULL /* LAPDm doesnt support extended */, NULL, NULL,
260 is_response, FALSE, FALSE);
262 if (tree) {
263 length_ti = proto_tree_add_uint(lapdm_tree, hf_lapdm_length, tvb,
264 2, 1, length);
265 length_tree = proto_item_add_subtree(length_ti, ett_lapdm_length);
267 proto_tree_add_uint(length_tree, hf_lapdm_len, tvb, 2, 1, length);
268 proto_tree_add_uint(length_tree, hf_lapdm_m, tvb, 2, 1, length);
269 proto_tree_add_uint(length_tree, hf_lapdm_el, tvb, 2, 1, length);
272 sapi = (addr & LAPDM_SAPI) >> LAPDM_SAPI_SHIFT;
273 len = (length & LAPDM_LEN) >> LAPDM_LEN_SHIFT;
274 /*n_s = (control & XDLC_N_S_MASK) >> XDLC_N_S_SHIFT;*/
275 m = (length & LAPDM_M) >> LAPDM_M_SHIFT;
276 available_length = tvb_length(tvb) - LAPDM_HEADER_LEN;
278 /* No point in doing anything if no payload
280 if( !MIN(len, available_length) )
281 return;
283 payload = tvb_new_subset(tvb, LAPDM_HEADER_LEN, MIN(len,available_length), -1);
285 /* Potentially segmented I frame
287 if( (control & XDLC_I_MASK) == XDLC_I && reassemble_lapdm )
289 fragment_head *fd_m = NULL;
290 tvbuff_t *reassembled = NULL;
291 guint32 fragment_id;
292 gboolean save_fragmented = pinfo->fragmented;
294 pinfo->fragmented = m;
296 /* Rely on caller to provide a way to group fragments */
297 fragment_id = (pinfo->circuit_id << 4) | (sapi << 1) | pinfo->p2p_dir;
299 /* This doesn't seem the best way of doing it as doesn't
300 take N(S) into account, but N(S) isn't always 0 for
301 the first fragment!
303 fd_m = fragment_add_seq_next (&lapdm_reassembly_table, payload, 0,
304 pinfo,
305 fragment_id, /* guint32 ID for fragments belonging together */
306 NULL,
307 /*n_s guint32 fragment sequence number */
308 len, /* guint32 fragment length */
309 m); /* More fragments? */
311 reassembled = process_reassembled_data(payload, 0, pinfo,
312 "Reassembled LAPDm", fd_m, &lapdm_frag_items,
313 NULL, lapdm_tree);
315 /* Reassembled into this packet
317 if (fd_m && pinfo->fd->num == fd_m->reassembled_in) {
318 if (!dissector_try_uint(lapdm_sapi_dissector_table, sapi,
319 reassembled, pinfo, tree))
320 call_dissector(data_handle, reassembled, pinfo, tree);
322 else {
323 col_append_str(pinfo->cinfo, COL_INFO, " (Fragment)");
324 if (tree) {
325 proto_tree_add_text(lapdm_tree, payload, 0, -1, "Fragment Data");
329 /* Now reset fragmentation information in pinfo
331 pinfo->fragmented = save_fragmented;
333 else
335 /* Whole packet
336 If we have some data, try and dissect it (only happens for UI, SABM, UA or I frames)
338 if (!dissector_try_uint(lapdm_sapi_dissector_table, sapi,
339 payload, pinfo, tree))
340 call_dissector(data_handle,payload, pinfo, tree);
344 void
345 proto_register_lapdm(void)
347 static hf_register_info hf[] = {
349 { &hf_lapdm_address,
350 { "Address Field", "lapdm.address_field", FT_UINT8, BASE_HEX, NULL, 0x0,
351 "Address", HFILL }},
353 { &hf_lapdm_ea,
354 { "EA", "lapdm.ea", FT_UINT8, BASE_DEC, VALS(lapdm_ea_vals), LAPDM_EA,
355 "Address field extension bit", HFILL }},
357 { &hf_lapdm_cr,
358 { "C/R", "lapdm.cr", FT_UINT8, BASE_DEC, NULL, LAPDM_CR,
359 "Command/response field bit", HFILL }},
361 { &hf_lapdm_lpd,
362 { "LPD", "lapdm.lpd", FT_UINT8, BASE_DEC, VALS(lapdm_lpd_vals), LAPDM_LPD,
363 "Link Protocol Discriminator", HFILL }},
365 { &hf_lapdm_sapi,
366 { "SAPI", "lapdm.sapi", FT_UINT8, BASE_DEC, VALS(lapdm_sapi_vals), LAPDM_SAPI,
367 "Service access point identifier", HFILL }},
369 { &hf_lapdm_control,
370 { "Control Field", "lapdm.control_field", FT_UINT8, BASE_HEX, NULL, 0x0,
371 NULL, HFILL }},
373 { &hf_lapdm_n_r,
374 { "N(R)", "lapdm.control.n_r", FT_UINT8, BASE_DEC,
375 NULL, XDLC_N_R_MASK, NULL, HFILL }},
377 { &hf_lapdm_n_s,
378 { "N(S)", "lapdm.control.n_s", FT_UINT8, BASE_DEC,
379 NULL, XDLC_N_S_MASK, NULL, HFILL }},
381 { &hf_lapdm_p,
382 { "Poll", "lapdm.control.p", FT_BOOLEAN, 8,
383 TFS(&tfs_true_false), XDLC_P_F, NULL, HFILL }},
385 { &hf_lapdm_f,
386 { "Final", "lapdm.control.f", FT_BOOLEAN, 8,
387 TFS(&tfs_true_false), XDLC_P_F, NULL, HFILL }},
389 { &hf_lapdm_s_ftype,
390 { "Supervisory frame type", "lapdm.control.s_ftype", FT_UINT8, BASE_HEX,
391 VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},
393 { &hf_lapdm_u_modifier_cmd,
394 { "Command", "lapdm.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
395 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
397 { &hf_lapdm_u_modifier_resp,
398 { "Response", "lapdm.control.u_modifier_resp", FT_UINT8, BASE_HEX,
399 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
401 { &hf_lapdm_ftype_i,
402 { "Frame type", "lapdm.control.ftype", FT_UINT8, BASE_HEX,
403 VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},
405 { &hf_lapdm_ftype_s_u,
406 { "Frame type", "lapdm.control.ftype", FT_UINT8, BASE_HEX,
407 VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
409 { &hf_lapdm_length,
410 { "Length Field", "lapdm.length_field", FT_UINT8, BASE_HEX,
411 NULL, 0x0, NULL, HFILL }},
413 { &hf_lapdm_el,
414 { "EL", "lapdm.el", FT_UINT8, BASE_DEC,
415 VALS(lapdm_el_vals), LAPDM_EL, "Length indicator field extension bit", HFILL }},
417 { &hf_lapdm_m,
418 { "M", "lapdm.m", FT_UINT8, BASE_DEC,
419 VALS(lapdm_m_vals), LAPDM_M, "More data bit", HFILL }},
421 { &hf_lapdm_len,
422 { "Length", "lapdm.length", FT_UINT8, BASE_DEC,
423 NULL, LAPDM_LEN, "Length indicator", HFILL }},
425 /* Fragment reassembly
427 { &hf_lapdm_fragments,
428 { "Message fragments", "lapdm.fragments", FT_NONE, BASE_NONE,
429 NULL, 0x00, "LAPDm Message fragments", HFILL }},
431 { &hf_lapdm_fragment,
432 { "Message fragment", "lapdm.fragment", FT_FRAMENUM, BASE_NONE,
433 NULL, 0x00, "LAPDm Message fragment", HFILL }},
435 { &hf_lapdm_fragment_overlap,
436 { "Message fragment overlap", "lapdm.fragment.overlap", FT_BOOLEAN, BASE_NONE,
437 NULL, 0x0, "LAPDm Message fragment overlaps with other fragment(s)", HFILL }},
439 { &hf_lapdm_fragment_overlap_conflicts,
440 { "Message fragment overlapping with conflicting data", "lapdm.fragment.overlap.conflicts", FT_BOOLEAN, BASE_NONE,
441 NULL, 0x0, "LAPDm Message fragment overlaps with conflicting data", HFILL }},
443 { &hf_lapdm_fragment_multiple_tails,
444 { "Message has multiple tail fragments", "lapdm.fragment.multiple_tails", FT_BOOLEAN, BASE_NONE,
445 NULL, 0x0, "LAPDm Message fragment has multiple tail fragments", HFILL }},
447 { &hf_lapdm_fragment_too_long_fragment,
448 { "Message fragment too long", "lapdm.fragment.too_long_fragment", FT_BOOLEAN, BASE_NONE,
449 NULL, 0x0, "LAPDm Message fragment data goes beyond the packet end", HFILL }},
451 { &hf_lapdm_fragment_error,
452 { "Message defragmentation error", "lapdm.fragment.error", FT_FRAMENUM, BASE_NONE,
453 NULL, 0x00, "LAPDm Message defragmentation error due to illegal fragments", HFILL }},
455 { &hf_lapdm_fragment_count,
456 { "Message fragment count", "lapdm.fragment.count", FT_UINT32, BASE_DEC,
457 NULL, 0x00, NULL, HFILL }},
459 { &hf_lapdm_reassembled_in,
460 { "Reassembled in", "lapdm.reassembled.in", FT_FRAMENUM, BASE_NONE,
461 NULL, 0x00, "LAPDm Message has been reassembled in this packet.", HFILL }},
463 { &hf_lapdm_reassembled_length,
464 { "Reassembled LAPDm length", "lapdm.reassembled.length", FT_UINT32, BASE_DEC,
465 NULL, 0x00, "The total length of the reassembled payload", HFILL }}
468 static gint *ett[] = {
469 &ett_lapdm,
470 &ett_lapdm_address,
471 &ett_lapdm_control,
472 &ett_lapdm_length,
473 &ett_lapdm_fragment,
474 &ett_lapdm_fragments
477 module_t *lapdm_module;
479 proto_lapdm = proto_register_protocol("Link Access Procedure, Channel Dm (LAPDm)", "LAPDm", "lapdm");
480 proto_register_field_array (proto_lapdm, hf, array_length(hf));
481 proto_register_subtree_array(ett, array_length(ett));
483 register_dissector("lapdm", dissect_lapdm, proto_lapdm);
485 lapdm_sapi_dissector_table = register_dissector_table("lapdm.sapi", "LAPDm SAPI", FT_UINT8, BASE_DEC);
487 lapdm_module = prefs_register_protocol(proto_lapdm, NULL);
488 prefs_register_bool_preference(lapdm_module, "reassemble",
489 "Reassemble fragmented LAPDm packets",
490 "Whether the dissector should defragment LAPDm messages spanning multiple packets.",
491 &reassemble_lapdm);
492 register_init_routine (lapdm_defragment_init);
495 void
496 proto_reg_handoff_lapdm(void)
498 data_handle = find_dissector("data");