2 * Routines for LAPDm frame disassembly
3 * Duncan Salerno <duncan.salerno@googlemail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * Mobile Station - Base Stations System (MS - BSS) Interface Data Link (DL) Layer Specification
27 * Base Station Controller - Base Transceiver Station (BSC - BTS) interface; Layer 2 specification
28 * http://www.3gpp.org/ftp/Specs/html-info/44006.htm
30 * From 3GPP TS 44.006:
32 * LAPDm is used for information sent on the control channels BCCH, AGCH, NCH,
33 * PCH, FACCH, SACCH and SDCCH as defined in 3GPP TS 44.003.
35 * AGCH, NCH and PCH are sometimes referred to by the collective name CCCH.
36 * FACCH, SACCH and SDCCH are, similarly, referred to by the collective name DCCH.
38 * Format A is used on DCCHs for frames where there is no information field.
39 * Formats B, Bter and B4 are used on DCCHs for frames containing an information field:
40 * Format Bter is used on request of higher layers if and only if short L2 header type 1 is
41 * supported and a UI command is to be transmitted on SAPI 0;
42 * Format B4 is used for UI frames transmitted by the network on SACCH;
43 * Format B is applied in all other cases.
44 * Format Bbis is used only on BCCH, PCH, NCH, and AGCH.
45 * In addition there is a Format C for transmission of random access signals.
47 * This module currently supports A, B, B4
48 * In the future will support Bter
49 * Bbis and C should be supported elsewhere
55 #include <epan/packet.h>
56 #include <epan/prefs.h>
57 #include <epan/xdlc.h>
58 #include <epan/reassemble.h>
60 void proto_register_lapdm(void);
61 void proto_reg_handoff_lapdm(void);
63 static int proto_lapdm = -1;
64 static int hf_lapdm_address = -1;
65 static int hf_lapdm_ea = -1;
66 static int hf_lapdm_cr = -1;
67 static int hf_lapdm_sapi = -1;
68 static int hf_lapdm_lpd = -1;
70 static int hf_lapdm_control = -1;
71 static int hf_lapdm_n_r = -1;
72 static int hf_lapdm_n_s = -1;
73 static int hf_lapdm_p = -1;
74 static int hf_lapdm_f = -1;
75 static int hf_lapdm_s_ftype = -1;
76 static int hf_lapdm_u_modifier_cmd = -1;
77 static int hf_lapdm_u_modifier_resp = -1;
78 static int hf_lapdm_ftype_i = -1;
79 static int hf_lapdm_ftype_s_u = -1;
81 static int hf_lapdm_length = -1;
82 static int hf_lapdm_el = -1;
83 static int hf_lapdm_m = -1;
84 static int hf_lapdm_len = -1;
87 * LAPDm fragment handling
89 static int hf_lapdm_fragments = -1;
90 static int hf_lapdm_fragment = -1;
91 static int hf_lapdm_fragment_overlap = -1;
92 static int hf_lapdm_fragment_overlap_conflicts = -1;
93 static int hf_lapdm_fragment_multiple_tails = -1;
94 static int hf_lapdm_fragment_too_long_fragment = -1;
95 static int hf_lapdm_fragment_error = -1;
96 static int hf_lapdm_fragment_count = -1;
97 static int hf_lapdm_reassembled_in = -1;
98 static int hf_lapdm_reassembled_length = -1;
100 static gint ett_lapdm = -1;
101 static gint ett_lapdm_address = -1;
102 static gint ett_lapdm_control = -1;
103 static gint ett_lapdm_length = -1;
104 static gint ett_lapdm_fragment = -1;
105 static gint ett_lapdm_fragments = -1;
107 static reassembly_table lapdm_reassembly_table;
109 static dissector_table_t lapdm_sapi_dissector_table;
111 static dissector_handle_t data_handle;
113 static gboolean reassemble_lapdm = TRUE;
116 * Bits in the address field.
118 #define LAPDM_SAPI 0x1c /* Service Access Point Identifier */
119 #define LAPDM_SAPI_SHIFT 2
120 #define LAPDM_CR 0x02 /* Command/Response bit */
121 #define LAPDM_EA 0x01 /* First Address Extension bit */
122 #define LAPDM_LPD 0x60 /* Link Protocol Discriminator */
125 * Bits in the length field.
127 #define LAPDM_EL 0x01 /* Extended Length = 1 */
128 #define LAPDM_M 0x02 /* More fragments */
129 #define LAPDM_M_SHIFT 1
130 #define LAPDM_LEN 0xfc /* Length */
131 #define LAPDM_LEN_SHIFT 2
133 #define LAPDM_HEADER_LEN 3
135 #define LAPDM_SAPI_RR_CC_MM 0
136 #define LAPDM_SAPI_SMS 3
138 /* Used only for U frames */
139 static const xdlc_cf_items lapdm_cf_items = {
145 &hf_lapdm_u_modifier_cmd,
146 &hf_lapdm_u_modifier_resp,
151 static const value_string lapdm_ea_vals[] = {
152 { 0, "More octets" },
153 { 1, "Final octet" },
157 static const value_string lapdm_sapi_vals[] = {
158 { LAPDM_SAPI_RR_CC_MM, "RR/MM/CC" },
159 { LAPDM_SAPI_SMS, "SMS/SS" },
163 static const value_string lapdm_lpd_vals[] = {
165 { 1, "Cell broadcast service" },
169 static const value_string lapdm_m_vals[] = {
170 { 0, "Last segment" },
171 { 1, "More segments" },
175 static const value_string lapdm_el_vals[] = {
176 { 0, "More octets" },
177 { 1, "Final octet" },
182 static const fragment_items lapdm_frag_items = {
183 /* Fragment subtrees */
185 &ett_lapdm_fragments,
186 /* Fragment fields */
189 &hf_lapdm_fragment_overlap,
190 &hf_lapdm_fragment_overlap_conflicts,
191 &hf_lapdm_fragment_multiple_tails,
192 &hf_lapdm_fragment_too_long_fragment,
193 &hf_lapdm_fragment_error,
194 &hf_lapdm_fragment_count,
195 /* Reassembled in field */
196 &hf_lapdm_reassembled_in,
197 /* Reassembled length field */
198 &hf_lapdm_reassembled_length,
199 /* Reassembled data field */
206 lapdm_defragment_init (void)
208 reassembly_table_init (&lapdm_reassembly_table,
209 &addresses_reassembly_table_functions);
214 dissect_lapdm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
216 proto_tree *lapdm_tree, *addr_tree, *length_tree;
217 proto_item *lapdm_ti, *addr_ti, *length_ti;
218 guint8 addr, length, cr, sapi, len/*, n_s*/;
222 int available_length;
223 gboolean is_response = FALSE;
225 /* Check that there's enough data */
226 if (tvb_length(tvb) < LAPDM_HEADER_LEN)
229 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPDm");
231 addr = tvb_get_guint8(tvb, 0);
232 length = tvb_get_guint8(tvb, 2);
234 cr = addr & LAPDM_CR;
235 if (pinfo->p2p_dir == P2P_DIR_RECV) {
236 is_response = cr ? FALSE : TRUE;
238 else if (pinfo->p2p_dir == P2P_DIR_SENT) {
239 is_response = cr ? TRUE : FALSE;
243 lapdm_ti = proto_tree_add_item(tree, proto_lapdm, tvb, 0, LAPDM_HEADER_LEN, ENC_NA);
244 lapdm_tree = proto_item_add_subtree(lapdm_ti, ett_lapdm);
246 addr_ti = proto_tree_add_uint(lapdm_tree, hf_lapdm_address, tvb, 0, 1, addr);
247 addr_tree = proto_item_add_subtree(addr_ti, ett_lapdm_address);
249 proto_tree_add_uint(addr_tree, hf_lapdm_lpd, tvb, 0, 1, addr);
250 proto_tree_add_uint(addr_tree, hf_lapdm_sapi, tvb, 0, 1, addr);
251 proto_tree_add_uint(addr_tree, hf_lapdm_cr, tvb, 0, 1, addr);
252 proto_tree_add_uint(addr_tree, hf_lapdm_ea, tvb, 0, 1, addr);
259 control = dissect_xdlc_control(tvb, 1, pinfo, lapdm_tree, hf_lapdm_control,
260 ett_lapdm_control, &lapdm_cf_items, NULL /* LAPDm doesnt support extended */, NULL, NULL,
261 is_response, FALSE, FALSE);
264 length_ti = proto_tree_add_uint(lapdm_tree, hf_lapdm_length, tvb,
266 length_tree = proto_item_add_subtree(length_ti, ett_lapdm_length);
268 proto_tree_add_uint(length_tree, hf_lapdm_len, tvb, 2, 1, length);
269 proto_tree_add_uint(length_tree, hf_lapdm_m, tvb, 2, 1, length);
270 proto_tree_add_uint(length_tree, hf_lapdm_el, tvb, 2, 1, length);
273 sapi = (addr & LAPDM_SAPI) >> LAPDM_SAPI_SHIFT;
274 len = (length & LAPDM_LEN) >> LAPDM_LEN_SHIFT;
275 /*n_s = (control & XDLC_N_S_MASK) >> XDLC_N_S_SHIFT;*/
276 m = (length & LAPDM_M) >> LAPDM_M_SHIFT;
277 available_length = tvb_length(tvb) - LAPDM_HEADER_LEN;
279 /* No point in doing anything if no payload
281 if( !MIN(len, available_length) )
284 payload = tvb_new_subset(tvb, LAPDM_HEADER_LEN, MIN(len,available_length), -1);
286 /* Potentially segmented I frame
288 if( (control & XDLC_I_MASK) == XDLC_I && reassemble_lapdm )
290 fragment_head *fd_m = NULL;
291 tvbuff_t *reassembled = NULL;
293 gboolean save_fragmented = pinfo->fragmented;
295 pinfo->fragmented = m;
297 /* Rely on caller to provide a way to group fragments */
298 fragment_id = (pinfo->circuit_id << 4) | (sapi << 1) | pinfo->p2p_dir;
300 /* This doesn't seem the best way of doing it as doesn't
301 take N(S) into account, but N(S) isn't always 0 for
304 fd_m = fragment_add_seq_next (&lapdm_reassembly_table, payload, 0,
306 fragment_id, /* guint32 ID for fragments belonging together */
308 /*n_s guint32 fragment sequence number */
309 len, /* guint32 fragment length */
310 m); /* More fragments? */
312 reassembled = process_reassembled_data(payload, 0, pinfo,
313 "Reassembled LAPDm", fd_m, &lapdm_frag_items,
316 /* Reassembled into this packet
318 if (fd_m && pinfo->fd->num == fd_m->reassembled_in) {
319 if (!dissector_try_uint(lapdm_sapi_dissector_table, sapi,
320 reassembled, pinfo, tree))
321 call_dissector(data_handle, reassembled, pinfo, tree);
324 col_append_str(pinfo->cinfo, COL_INFO, " (Fragment)");
326 proto_tree_add_text(lapdm_tree, payload, 0, -1, "Fragment Data");
330 /* Now reset fragmentation information in pinfo
332 pinfo->fragmented = save_fragmented;
337 If we have some data, try and dissect it (only happens for UI, SABM, UA or I frames)
339 if (!dissector_try_uint(lapdm_sapi_dissector_table, sapi,
340 payload, pinfo, tree))
341 call_dissector(data_handle,payload, pinfo, tree);
346 proto_register_lapdm(void)
348 static hf_register_info hf[] = {
351 { "Address Field", "lapdm.address_field", FT_UINT8, BASE_HEX, NULL, 0x0,
355 { "EA", "lapdm.ea", FT_UINT8, BASE_DEC, VALS(lapdm_ea_vals), LAPDM_EA,
356 "Address field extension bit", HFILL }},
359 { "C/R", "lapdm.cr", FT_UINT8, BASE_DEC, NULL, LAPDM_CR,
360 "Command/response field bit", HFILL }},
363 { "LPD", "lapdm.lpd", FT_UINT8, BASE_DEC, VALS(lapdm_lpd_vals), LAPDM_LPD,
364 "Link Protocol Discriminator", HFILL }},
367 { "SAPI", "lapdm.sapi", FT_UINT8, BASE_DEC, VALS(lapdm_sapi_vals), LAPDM_SAPI,
368 "Service access point identifier", HFILL }},
371 { "Control Field", "lapdm.control_field", FT_UINT8, BASE_HEX, NULL, 0x0,
375 { "N(R)", "lapdm.control.n_r", FT_UINT8, BASE_DEC,
376 NULL, XDLC_N_R_MASK, NULL, HFILL }},
379 { "N(S)", "lapdm.control.n_s", FT_UINT8, BASE_DEC,
380 NULL, XDLC_N_S_MASK, NULL, HFILL }},
383 { "Poll", "lapdm.control.p", FT_BOOLEAN, 8,
384 TFS(&tfs_true_false), XDLC_P_F, NULL, HFILL }},
387 { "Final", "lapdm.control.f", FT_BOOLEAN, 8,
388 TFS(&tfs_true_false), XDLC_P_F, NULL, HFILL }},
391 { "Supervisory frame type", "lapdm.control.s_ftype", FT_UINT8, BASE_HEX,
392 VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},
394 { &hf_lapdm_u_modifier_cmd,
395 { "Command", "lapdm.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
396 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
398 { &hf_lapdm_u_modifier_resp,
399 { "Response", "lapdm.control.u_modifier_resp", FT_UINT8, BASE_HEX,
400 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
403 { "Frame type", "lapdm.control.ftype", FT_UINT8, BASE_HEX,
404 VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},
406 { &hf_lapdm_ftype_s_u,
407 { "Frame type", "lapdm.control.ftype", FT_UINT8, BASE_HEX,
408 VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
411 { "Length Field", "lapdm.length_field", FT_UINT8, BASE_HEX,
412 NULL, 0x0, NULL, HFILL }},
415 { "EL", "lapdm.el", FT_UINT8, BASE_DEC,
416 VALS(lapdm_el_vals), LAPDM_EL, "Length indicator field extension bit", HFILL }},
419 { "M", "lapdm.m", FT_UINT8, BASE_DEC,
420 VALS(lapdm_m_vals), LAPDM_M, "More data bit", HFILL }},
423 { "Length", "lapdm.length", FT_UINT8, BASE_DEC,
424 NULL, LAPDM_LEN, "Length indicator", HFILL }},
426 /* Fragment reassembly
428 { &hf_lapdm_fragments,
429 { "Message fragments", "lapdm.fragments", FT_NONE, BASE_NONE,
430 NULL, 0x00, "LAPDm Message fragments", HFILL }},
432 { &hf_lapdm_fragment,
433 { "Message fragment", "lapdm.fragment", FT_FRAMENUM, BASE_NONE,
434 NULL, 0x00, "LAPDm Message fragment", HFILL }},
436 { &hf_lapdm_fragment_overlap,
437 { "Message fragment overlap", "lapdm.fragment.overlap", FT_BOOLEAN, BASE_NONE,
438 NULL, 0x0, "LAPDm Message fragment overlaps with other fragment(s)", HFILL }},
440 { &hf_lapdm_fragment_overlap_conflicts,
441 { "Message fragment overlapping with conflicting data", "lapdm.fragment.overlap.conflicts", FT_BOOLEAN, BASE_NONE,
442 NULL, 0x0, "LAPDm Message fragment overlaps with conflicting data", HFILL }},
444 { &hf_lapdm_fragment_multiple_tails,
445 { "Message has multiple tail fragments", "lapdm.fragment.multiple_tails", FT_BOOLEAN, BASE_NONE,
446 NULL, 0x0, "LAPDm Message fragment has multiple tail fragments", HFILL }},
448 { &hf_lapdm_fragment_too_long_fragment,
449 { "Message fragment too long", "lapdm.fragment.too_long_fragment", FT_BOOLEAN, BASE_NONE,
450 NULL, 0x0, "LAPDm Message fragment data goes beyond the packet end", HFILL }},
452 { &hf_lapdm_fragment_error,
453 { "Message defragmentation error", "lapdm.fragment.error", FT_FRAMENUM, BASE_NONE,
454 NULL, 0x00, "LAPDm Message defragmentation error due to illegal fragments", HFILL }},
456 { &hf_lapdm_fragment_count,
457 { "Message fragment count", "lapdm.fragment.count", FT_UINT32, BASE_DEC,
458 NULL, 0x00, NULL, HFILL }},
460 { &hf_lapdm_reassembled_in,
461 { "Reassembled in", "lapdm.reassembled.in", FT_FRAMENUM, BASE_NONE,
462 NULL, 0x00, "LAPDm Message has been reassembled in this packet.", HFILL }},
464 { &hf_lapdm_reassembled_length,
465 { "Reassembled LAPDm length", "lapdm.reassembled.length", FT_UINT32, BASE_DEC,
466 NULL, 0x00, "The total length of the reassembled payload", HFILL }}
469 static gint *ett[] = {
478 module_t *lapdm_module;
480 proto_lapdm = proto_register_protocol("Link Access Procedure, Channel Dm (LAPDm)", "LAPDm", "lapdm");
481 proto_register_field_array (proto_lapdm, hf, array_length(hf));
482 proto_register_subtree_array(ett, array_length(ett));
484 register_dissector("lapdm", dissect_lapdm, proto_lapdm);
486 lapdm_sapi_dissector_table = register_dissector_table("lapdm.sapi", "LAPDm SAPI", FT_UINT8, BASE_DEC);
488 lapdm_module = prefs_register_protocol(proto_lapdm, NULL);
489 prefs_register_bool_preference(lapdm_module, "reassemble",
490 "Reassemble fragmented LAPDm packets",
491 "Whether the dissector should defragment LAPDm messages spanning multiple packets.",
493 register_init_routine (lapdm_defragment_init);
497 proto_reg_handoff_lapdm(void)
499 data_handle = find_dissector("data");