Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/wireshark/wip.git] / epan / dissectors / packet-lapdm.c
1 /* packet-lapdm.c
2  * Routines for LAPDm frame disassembly
3  * Duncan Salerno <duncan.salerno@googlemail.com>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 /* LAPDm references:
25  *
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
29  *
30  * From 3GPP TS 44.006:
31  *
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.
34  *
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.
37  *
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.
46  *
47  * This module currently supports A, B, B4
48  * In the future will support Bter
49  * Bbis and C should be supported elsewhere
50  */
51
52 #include "config.h"
53
54 #include <glib.h>
55 #include <epan/packet.h>
56 #include <epan/prefs.h>
57 #include <epan/xdlc.h>
58 #include <epan/reassemble.h>
59
60 void proto_register_lapdm(void);
61 void proto_reg_handoff_lapdm(void);
62
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;
69
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;
80
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;
85
86 /*
87  * LAPDm fragment handling
88  */
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;
99
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;
106
107 static reassembly_table lapdm_reassembly_table;
108
109 static dissector_table_t lapdm_sapi_dissector_table;
110
111 static dissector_handle_t data_handle;
112
113 static gboolean reassemble_lapdm = TRUE;
114
115 /*
116  * Bits in the address field.
117  */
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 */
123
124 /*
125  * Bits in the length field.
126  */
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
132
133 #define LAPDM_HEADER_LEN 3
134
135 #define LAPDM_SAPI_RR_CC_MM     0
136 #define LAPDM_SAPI_SMS          3
137
138 /* Used only for U frames */
139 static const xdlc_cf_items lapdm_cf_items = {
140     &hf_lapdm_n_r,
141     &hf_lapdm_n_s,
142     &hf_lapdm_p,
143     &hf_lapdm_f,
144     &hf_lapdm_s_ftype,
145     &hf_lapdm_u_modifier_cmd,
146     &hf_lapdm_u_modifier_resp,
147     &hf_lapdm_ftype_i,
148     &hf_lapdm_ftype_s_u
149 };
150
151 static const value_string lapdm_ea_vals[] = {
152     { 0,                "More octets" },
153     { 1,                "Final octet" },
154     { 0,                NULL }
155 };
156
157 static const value_string lapdm_sapi_vals[] = {
158     { LAPDM_SAPI_RR_CC_MM,      "RR/MM/CC" },
159     { LAPDM_SAPI_SMS,           "SMS/SS" },
160     { 0,                        NULL }
161 };
162
163 static const value_string lapdm_lpd_vals[] = {
164     { 0,                "Normal GSM" },
165     { 1,                "Cell broadcast service" },
166     { 0,                NULL }
167 };
168
169 static const value_string lapdm_m_vals[] = {
170     { 0,                "Last segment" },
171     { 1,                "More segments" },
172     { 0,                NULL }
173 };
174
175 static const value_string lapdm_el_vals[] = {
176     { 0,                "More octets" },
177     { 1,                "Final octet" },
178     { 0,                NULL }
179 };
180
181
182 static const fragment_items lapdm_frag_items = {
183     /* Fragment subtrees */
184     &ett_lapdm_fragment,
185     &ett_lapdm_fragments,
186     /* Fragment fields */
187     &hf_lapdm_fragments,
188     &hf_lapdm_fragment,
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 */
200     NULL,
201     /* Tag */
202     "fragments"
203 };
204
205 static void
206 lapdm_defragment_init (void)
207 {
208     reassembly_table_init (&lapdm_reassembly_table,
209                            &addresses_reassembly_table_functions);
210 }
211
212
213 static void
214 dissect_lapdm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
215 {
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*/;
219     int control;
220     gboolean m;
221     tvbuff_t *payload;
222     int available_length;
223     gboolean is_response = FALSE;
224
225     /* Check that there's enough data */
226     if (tvb_length(tvb) < LAPDM_HEADER_LEN)
227         return;
228
229     col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPDm");
230
231     addr = tvb_get_guint8(tvb, 0);
232     length = tvb_get_guint8(tvb, 2);
233
234     cr = addr & LAPDM_CR;
235     if (pinfo->p2p_dir == P2P_DIR_RECV) {
236         is_response = cr ? FALSE : TRUE;
237     }
238     else if (pinfo->p2p_dir == P2P_DIR_SENT) {
239         is_response = cr ? TRUE : FALSE;
240     }
241
242     if (tree) {
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);
245
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);
248
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);
253     }
254     else {
255         lapdm_ti = NULL;
256         lapdm_tree = NULL;
257     }
258
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);
262
263     if (tree) {
264         length_ti = proto_tree_add_uint(lapdm_tree, hf_lapdm_length, tvb,
265                     2, 1, length);
266         length_tree = proto_item_add_subtree(length_ti, ett_lapdm_length);
267
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);
271     }
272
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;
278
279     /* No point in doing anything if no payload
280      */
281     if( !MIN(len, available_length) )
282         return;
283
284     payload = tvb_new_subset(tvb, LAPDM_HEADER_LEN, MIN(len,available_length), -1);
285
286     /* Potentially segmented I frame
287      */
288     if( (control & XDLC_I_MASK) == XDLC_I && reassemble_lapdm )
289     {
290             fragment_head *fd_m = NULL;
291             tvbuff_t *reassembled = NULL;
292             guint32 fragment_id;
293             gboolean save_fragmented = pinfo->fragmented;
294
295             pinfo->fragmented = m;
296
297             /* Rely on caller to provide a way to group fragments */
298             fragment_id = (pinfo->circuit_id << 4) | (sapi << 1) | pinfo->p2p_dir;
299
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
302                the first fragment!
303              */
304             fd_m = fragment_add_seq_next (&lapdm_reassembly_table, payload, 0,
305                                 pinfo,
306                                 fragment_id, /* guint32 ID for fragments belonging together */
307                                 NULL,
308                                 /*n_s guint32 fragment sequence number */
309                                 len, /* guint32 fragment length */
310                                 m); /* More fragments? */
311
312             reassembled = process_reassembled_data(payload, 0, pinfo,
313                     "Reassembled LAPDm", fd_m, &lapdm_frag_items,
314                     NULL, lapdm_tree);
315
316             /* Reassembled into this packet
317              */
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);
322             }
323             else {
324                 col_append_str(pinfo->cinfo, COL_INFO, " (Fragment)");
325                 if (tree) {
326                     proto_tree_add_text(lapdm_tree, payload, 0, -1, "Fragment Data");
327                 }
328             }
329
330             /* Now reset fragmentation information in pinfo
331              */
332             pinfo->fragmented = save_fragmented;
333     }
334     else
335     {
336         /* Whole packet
337            If we have some data, try and dissect it (only happens for UI, SABM, UA or I frames)
338          */
339         if (!dissector_try_uint(lapdm_sapi_dissector_table, sapi,
340                 payload, pinfo, tree))
341             call_dissector(data_handle,payload, pinfo, tree);
342     }
343 }
344
345 void
346 proto_register_lapdm(void)
347 {
348     static hf_register_info hf[] = {
349
350         { &hf_lapdm_address,
351         { "Address Field", "lapdm.address_field", FT_UINT8, BASE_HEX, NULL, 0x0,
352         "Address", HFILL }},
353
354         { &hf_lapdm_ea,
355         { "EA", "lapdm.ea", FT_UINT8, BASE_DEC, VALS(lapdm_ea_vals), LAPDM_EA,
356         "Address field extension bit", HFILL }},
357
358         { &hf_lapdm_cr,
359         { "C/R", "lapdm.cr", FT_UINT8, BASE_DEC, NULL, LAPDM_CR,
360         "Command/response field bit", HFILL }},
361
362         { &hf_lapdm_lpd,
363         { "LPD", "lapdm.lpd", FT_UINT8, BASE_DEC, VALS(lapdm_lpd_vals), LAPDM_LPD,
364         "Link Protocol Discriminator", HFILL }},
365
366         { &hf_lapdm_sapi,
367         { "SAPI", "lapdm.sapi", FT_UINT8, BASE_DEC, VALS(lapdm_sapi_vals), LAPDM_SAPI,
368         "Service access point identifier", HFILL }},
369
370         { &hf_lapdm_control,
371         { "Control Field", "lapdm.control_field", FT_UINT8, BASE_HEX, NULL, 0x0,
372         NULL, HFILL }},
373
374         { &hf_lapdm_n_r,
375         { "N(R)", "lapdm.control.n_r", FT_UINT8, BASE_DEC,
376         NULL, XDLC_N_R_MASK, NULL, HFILL }},
377
378         { &hf_lapdm_n_s,
379         { "N(S)", "lapdm.control.n_s", FT_UINT8, BASE_DEC,
380         NULL, XDLC_N_S_MASK, NULL, HFILL }},
381
382         { &hf_lapdm_p,
383         { "Poll", "lapdm.control.p", FT_BOOLEAN, 8,
384         TFS(&tfs_true_false), XDLC_P_F, NULL, HFILL }},
385
386         { &hf_lapdm_f,
387         { "Final", "lapdm.control.f", FT_BOOLEAN, 8,
388         TFS(&tfs_true_false), XDLC_P_F, NULL, HFILL }},
389
390         { &hf_lapdm_s_ftype,
391         { "Supervisory frame type", "lapdm.control.s_ftype", FT_UINT8, BASE_HEX,
392         VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},
393
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 }},
397
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 }},
401
402         { &hf_lapdm_ftype_i,
403         { "Frame type", "lapdm.control.ftype", FT_UINT8, BASE_HEX,
404         VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},
405
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 }},
409
410         { &hf_lapdm_length,
411         { "Length Field", "lapdm.length_field", FT_UINT8, BASE_HEX,
412         NULL, 0x0, NULL, HFILL }},
413
414         { &hf_lapdm_el,
415         { "EL", "lapdm.el", FT_UINT8, BASE_DEC,
416         VALS(lapdm_el_vals), LAPDM_EL, "Length indicator field extension bit", HFILL }},
417
418         { &hf_lapdm_m,
419         { "M", "lapdm.m", FT_UINT8, BASE_DEC,
420         VALS(lapdm_m_vals), LAPDM_M, "More data bit", HFILL }},
421
422         { &hf_lapdm_len,
423         { "Length", "lapdm.length", FT_UINT8, BASE_DEC,
424         NULL, LAPDM_LEN, "Length indicator", HFILL }},
425
426         /* Fragment reassembly
427          */
428         { &hf_lapdm_fragments,
429         { "Message fragments", "lapdm.fragments", FT_NONE, BASE_NONE,
430         NULL, 0x00, "LAPDm Message fragments", HFILL }},
431
432         { &hf_lapdm_fragment,
433         { "Message fragment", "lapdm.fragment", FT_FRAMENUM, BASE_NONE,
434         NULL, 0x00, "LAPDm Message fragment", HFILL }},
435
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 }},
439
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 }},
443
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 }},
447
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 }},
451
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 }},
455
456         { &hf_lapdm_fragment_count,
457         { "Message fragment count", "lapdm.fragment.count", FT_UINT32, BASE_DEC,
458         NULL, 0x00, NULL, HFILL }},
459
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 }},
463
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 }}
467
468     };
469     static gint *ett[] = {
470         &ett_lapdm,
471         &ett_lapdm_address,
472         &ett_lapdm_control,
473         &ett_lapdm_length,
474         &ett_lapdm_fragment,
475         &ett_lapdm_fragments
476     };
477
478     module_t *lapdm_module;
479
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));
483
484     register_dissector("lapdm", dissect_lapdm, proto_lapdm);
485
486     lapdm_sapi_dissector_table = register_dissector_table("lapdm.sapi", "LAPDm SAPI", FT_UINT8, BASE_DEC);
487
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.",
492         &reassemble_lapdm);
493     register_init_routine (lapdm_defragment_init);
494 }
495
496 void
497 proto_reg_handoff_lapdm(void)
498 {
499     data_handle = find_dissector("data");
500 }
501