Remove duplicate definitions found in lapd_sapi.h
[obnox/wireshark/wip.git] / epan / dissectors / packet-lapd.c
1 /* packet-lapd.c
2  * Routines for LAPD frame disassembly
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998
10  *
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.
15  *
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.
20  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25 /*
26  * LAPD bitstream over RTP handling
27  * Copyright 2008, Ericsson AB
28  * Written by Balint Reczey <balint.reczey@ericsson.com>
29  *
30  * ISDN/LAPD references:
31  *
32  * http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/isdn.htm
33  * http://www.ece.wpi.edu/courses/ee535/hwk11cd95/agrebe/agrebe.html
34  * http://www.acacia-net.com/Clarinet/Protocol/q9213o84.htm
35  * http://www.itu.int/rec/T-REC-Q.921/en
36  * Base Station Controller - Base Transceiver Station (BSC - BTS) interface; Layer 2 specification
37  * http://www.3gpp.org/ftp/Specs/html-info/48056.htm 
38  */
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <stdio.h>
45 #include <glib.h>
46 #include <string.h>
47 #include <epan/packet.h>
48 #include <epan/conversation.h>
49 #include <epan/xdlc.h>
50 #include <epan/crc16.h>
51 #include <epan/prefs.h>
52 #include <epan/lapd_sapi.h>
53
54 static int proto_lapd = -1;
55 static int hf_lapd_direction = -1;
56 static int hf_lapd_address = -1;
57 static int hf_lapd_sapi = -1;
58 static int hf_lapd_gsm_sapi = -1;
59 static int hf_lapd_cr = -1;
60 static int hf_lapd_ea1 = -1;
61 static int hf_lapd_tei = -1;
62 static int hf_lapd_ea2 = -1;
63 static int hf_lapd_control = -1;
64 static int hf_lapd_n_r = -1;
65 static int hf_lapd_n_s = -1;
66 static int hf_lapd_p = -1;
67 static int hf_lapd_p_ext = -1;
68 static int hf_lapd_f = -1;
69 static int hf_lapd_f_ext = -1;
70 static int hf_lapd_s_ftype = -1;
71 static int hf_lapd_u_modifier_cmd = -1;
72 static int hf_lapd_u_modifier_resp = -1;
73 static int hf_lapd_ftype_i = -1;
74 static int hf_lapd_ftype_s_u = -1;
75 static int hf_lapd_ftype_s_u_ext = -1;
76 static int hf_lapd_checksum = -1;
77 static int hf_lapd_checksum_good = -1;
78 static int hf_lapd_checksum_bad = -1;
79
80 static gint ett_lapd = -1;
81 static gint ett_lapd_address = -1;
82 static gint ett_lapd_control = -1;
83 static gint ett_lapd_checksum = -1;
84 static gint pref_lapd_rtp_payload_type = 0;
85
86 static dissector_table_t lapd_sapi_dissector_table;
87 static dissector_table_t lapd_gsm_sapi_dissector_table;
88
89 /* Wether to use GSM SAPI vals or not */
90 static gboolean global_lapd_gsm_sapis = FALSE;
91
92 static dissector_handle_t data_handle;
93
94 /*
95  * Bits in the address field.
96  */
97 #define LAPD_SAPI               0xfc00  /* Service Access Point Identifier */
98 #define LAPD_SAPI_SHIFT 10
99 #define LAPD_CR                 0x0200  /* Command/Response bit */
100 #define LAPD_EA1                0x0100  /* First Address Extension bit */
101 #define LAPD_TEI                0x00fe  /* Terminal Endpoint Identifier */
102 #define LAPD_TEI_SHIFT  1
103 #define LAPD_EA2                0x0001  /* Second Address Extension bit */
104
105 static const value_string lapd_direction_vals[] = {
106         { P2P_DIR_RECV, "Network->User"},
107         { P2P_DIR_SENT, "User->Network"},
108         { 0,                    NULL }
109 };
110
111 static const value_string lapd_sapi_vals[] = {
112         { LAPD_SAPI_Q931,               "Q.931 Call control procedure" },
113         { LAPD_SAPI_PM_Q931,    "Packet mode Q.931 Call control procedure" },
114         { LAPD_SAPI_X25,                "X.25 Level 3 procedures" },
115         { LAPD_SAPI_L2,                 "Layer 2 management procedures" },
116         { 0,                    NULL }
117 };
118
119 static const value_string lapd_gsm_sapi_vals[] = {
120         { LAPD_GSM_SAPI_RA_SIG_PROC,            "Radio signalling procedures" },
121         { LAPD_GSM_SAPI_NOT_USED_1,                     "(Not used in GSM PLMN)" },
122         { LAPD_GSM_SAPI_NOT_USED_16,            "(Not used in GSM PLMN)" },
123         { LAPD_GSM_SAPI_OM_PROC,                        "Operation and maintenance procedure" },
124         { LAPD_SAPI_L2,                                         "Layer 2 management procedures" },
125         { 0,                    NULL }
126 };
127
128 /* Used only for U frames */
129 static const xdlc_cf_items lapd_cf_items = {
130         NULL,
131         NULL,
132         &hf_lapd_p,
133         &hf_lapd_f,
134         NULL,
135         &hf_lapd_u_modifier_cmd,
136         &hf_lapd_u_modifier_resp,
137         NULL,
138         &hf_lapd_ftype_s_u
139 };
140
141 /* Used only for I and S frames */
142 static const xdlc_cf_items lapd_cf_items_ext = {
143         &hf_lapd_n_r,
144         &hf_lapd_n_s,
145         &hf_lapd_p_ext,
146         &hf_lapd_f_ext,
147         &hf_lapd_s_ftype,
148         NULL,
149         NULL,
150         &hf_lapd_ftype_i,
151         &hf_lapd_ftype_s_u_ext
152 };
153
154
155 /* LAPD frame detection state */ 
156 enum lapd_bitstream_states {OUT_OF_SYNC, FLAGS, DATA};
157
158 typedef struct lapd_byte_state {
159         enum lapd_bitstream_states state;       /* frame detection state */
160         char            full_byte;              /* part of a full byte */
161         char            bit_offset;             /* number of bits already got in the full byte */
162         int             ones;                   /* number of consecutive ones since the last zero */
163 } lapd_byte_state_t;
164
165 typedef struct lapd_ppi {
166         gboolean                has_crc;                /* CRC is captured with LAPD the frames */
167         lapd_byte_state_t       start_byte_state;       /* LAPD bitstream byte state at the beginnigng of processing the packet */
168 } lapd_ppi_t;
169
170 /* Fill values in lapd_byte_state struct */
171 void fill_lapd_byte_state(lapd_byte_state_t *ptr, enum lapd_bitstream_states state, char full_byte, char bit_offset, int ones)
172 {
173         ptr->state = state;
174         ptr->full_byte = full_byte;
175         ptr->bit_offset = bit_offset;
176         ptr->ones = ones;
177 }
178
179 typedef struct lapd_convo_data {
180         address         addr_a;
181         address         addr_b;
182         guint32         port_a;
183         guint32         port_b;
184         lapd_byte_state_t       *byte_state_a;
185         lapd_byte_state_t       *byte_state_b;
186 } lapd_convo_data_t;
187
188 #define MAX_LAPD_PACKET_LEN 1024
189
190 static void
191 dissect_lapd(tvbuff_t*, packet_info*, proto_tree*);
192
193 /* got new LAPD frame byte */
194 static void new_byte(char full_byte, char data[], int *data_len) {
195         if (*data_len < MAX_LAPD_PACKET_LEN) {
196                 data[*data_len] = full_byte;
197                 (*data_len)++;
198         } else {
199                 /* XXX : we are not prepared for that big messages, drop the last byte */
200         }
201 }
202
203 static void
204 dissect_lapd_bitstream(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
205 {
206         guint8          byte, full_byte = 0x00, bit_offset = 0;
207         gboolean        bit;
208         guint8          i, ones = 0, data[MAX_LAPD_PACKET_LEN];
209         int             data_len = 0;
210         guint           offset = 0, last_packet_end_offset = 0, available;
211         guint8          *buff;
212         tvbuff_t        *new_tvb;
213         
214         enum lapd_bitstream_states state = OUT_OF_SYNC;
215         lapd_ppi_t              *lapd_ppi;
216         conversation_t          *conversation = NULL;
217         lapd_convo_data_t       *convo_data = NULL;
218         lapd_byte_state_t       *lapd_byte_state, *prev_byte_state = NULL;
219         gboolean                forward_stream = TRUE;
220
221         /* get remaining data from previous packets */
222         conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
223                 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
224         lapd_ppi = (lapd_ppi_t*)p_get_proto_data(pinfo->fd, proto_lapd);
225         if (lapd_ppi) {
226                 prev_byte_state = &lapd_ppi->start_byte_state;
227                 if (prev_byte_state) {
228                         state = prev_byte_state->state;
229                         full_byte = prev_byte_state->full_byte;
230                         bit_offset = prev_byte_state->bit_offset;
231                         ones = prev_byte_state->ones;
232                 }
233                 
234         } else if (conversation) {
235                 convo_data = (lapd_convo_data_t*)conversation_get_proto_data(conversation, proto_lapd);
236                 if (NULL != convo_data) {
237                         if (ADDRESSES_EQUAL(&convo_data->addr_a, &pinfo->src)
238                                         && ADDRESSES_EQUAL(&convo_data->addr_b, &pinfo->dst)
239                                         && convo_data-> port_a == pinfo->srcport
240                                         && convo_data-> port_b == pinfo->destport) {
241                                 /* "forward" direction */
242                                 forward_stream = TRUE;
243                                 prev_byte_state = convo_data->byte_state_a;
244                         } else if (ADDRESSES_EQUAL(&convo_data-> addr_b, &pinfo->src)
245                                         && ADDRESSES_EQUAL(&convo_data->addr_a, &pinfo->dst)
246                                         && convo_data-> port_b == pinfo->srcport
247                                         && convo_data-> port_a == pinfo->destport) {
248                                 /* "backward" direction */
249                                 forward_stream = FALSE;
250                                 prev_byte_state = convo_data->byte_state_b;
251                         }
252                 }
253                 if (prev_byte_state) {
254                         state = prev_byte_state->state;
255                         full_byte = prev_byte_state->full_byte;
256                         bit_offset = prev_byte_state->bit_offset;
257                         ones = prev_byte_state->ones;
258                 }
259         }
260
261         /* Consume tvb bytes */
262         available = tvb_length_remaining(tvb, offset);
263         while (offset < available) {
264                 byte = tvb_get_guint8(tvb,offset);
265                 offset++;
266                 for (i=0; i < 8; i++) { /* cycle through bits */
267                         bit = byte & (0x80 >> i) ? TRUE : FALSE;
268
269                         /* consume a bit */
270                         if (bit) {
271                                 ones++;
272                                 full_byte |= (1 << bit_offset++);
273                         } else {
274                                 if (ones == 5 && state == DATA) {
275                                         /* we don't increase bit_offset, it is an inserted zero */
276                                 } else if (ones == 6 && state == DATA) { /* probably starting flag sequence */
277                                         buff = g_memdup(data, data_len);
278                                         /* Allocate new tvb for the LAPD frame */
279                                         new_tvb = tvb_new_real_data(buff, data_len, data_len);
280                                         tvb_set_free_cb(new_tvb, g_free);
281                                         tvb_set_child_real_data_tvbuff(tvb, new_tvb);
282                                         add_new_data_source(pinfo, new_tvb, "Decoded LAPD bitstream");
283                                         dissect_lapd(new_tvb, pinfo, tree);
284                                         last_packet_end_offset = offset -1;
285                                         data_len = 0;
286                                         state = FLAGS;
287                                         bit_offset++;
288                                 } else if (ones >= 7) { /* frame reset or 11111111 flag byte */
289                                         data_len = 0;
290                                         state = OUT_OF_SYNC;
291                                         bit_offset++;
292                                 } else {
293                                         bit_offset++;
294                                 }
295                                 ones = 0;
296                         }
297
298                         if (bit_offset == 8) { /* we have a new complete byte */
299                                 switch (state) {
300                                         case OUT_OF_SYNC:
301                                                 if (full_byte == 0x7E) { /* we have a flag byte */
302                                                         state = FLAGS;
303                                                         full_byte = 0x00;
304                                                         bit_offset = 0;
305                                                 } else { /* no sync yet, wait for a new byte */
306                                                         full_byte = (full_byte >> 1) & 0x7F;
307                                                         bit_offset--;
308                                                 }
309                                                 break;
310                                         
311                                         case FLAGS:
312                                                 if (full_byte == 0x7E) { /* we have a flag byte */
313                                                         full_byte = 0x00;
314                                                         bit_offset = 0;
315                                                 } else { /* we got the first data byte */
316                                                         state = DATA;
317                                                         new_byte(full_byte, data, &data_len);
318                                                         full_byte = 0x00;
319                                                         bit_offset = 0;
320                                                 }
321                                                 break;
322                                                 
323                                         case DATA:
324                                                 /* we got a new data byte */
325                                                 new_byte(full_byte, data, &data_len);
326                                                 full_byte = 0x00;
327                                                 bit_offset = 0;
328                                                 break;
329                                 }
330                         }
331                 }
332         }
333
334         if (state == DATA) { /* we are in the middle of an LAPD frame, we need more bytes */
335                 pinfo->desegment_offset = 0;
336                 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
337                 return;
338         } else { /* finished processing LAPD frame(s) */
339                 if (NULL == p_get_proto_data(pinfo->fd, proto_lapd)) {
340                         /* Per packet information */
341                         lapd_ppi = g_malloc(sizeof(lapd_ppi_t));
342                         lapd_ppi->has_crc = TRUE;
343                         if (prev_byte_state)
344                                 fill_lapd_byte_state(&lapd_ppi->start_byte_state, prev_byte_state->state,
345                                                 prev_byte_state->full_byte, prev_byte_state->bit_offset,
346                                                 prev_byte_state->ones);
347                         else 
348                                 fill_lapd_byte_state(&lapd_ppi->start_byte_state, OUT_OF_SYNC, 0x00, 0, 0);
349
350                         p_add_proto_data(pinfo->fd, proto_lapd, lapd_ppi);
351                         
352                                         
353                         /* Conversation info*/
354                         
355                         if (conversation) {
356                                 if (convo_data) { /* already have lapd convo data */
357                                         if (forward_stream)
358                                                 fill_lapd_byte_state(convo_data->byte_state_a, state, full_byte, bit_offset, ones);
359                                         else {
360                                                 if (!convo_data->byte_state_b)
361                                                         convo_data->byte_state_b = g_malloc(sizeof(lapd_byte_state_t));
362                                                 fill_lapd_byte_state(convo_data->byte_state_b, state, full_byte, bit_offset, ones);
363                                         }
364                                 } else { /* lapd convo data has to be created */
365                                         lapd_byte_state = g_malloc(sizeof(lapd_byte_state_t));
366                                         fill_lapd_byte_state(lapd_byte_state, state, full_byte, bit_offset, ones);
367                                         convo_data = g_malloc(sizeof(lapd_convo_data_t));
368                                         COPY_ADDRESS(&convo_data->addr_a, &pinfo->src);
369                                         COPY_ADDRESS(&convo_data->addr_b, &pinfo->dst);
370                                         convo_data->port_a = pinfo->srcport;
371                                         convo_data->port_b = pinfo->destport;
372                                         convo_data->byte_state_a = lapd_byte_state;
373                                         convo_data->byte_state_b = NULL;
374                                         conversation_add_proto_data(conversation, proto_lapd, convo_data);
375                                 }
376                         }
377                 }
378         }
379 }
380
381
382 static void
383 dissect_lapd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
384 {
385         proto_tree      *lapd_tree, *addr_tree, *checksum_tree;
386         proto_item      *lapd_ti, *addr_ti, *checksum_ti;
387         guint16         control, checksum, checksum_calculated;
388         int             lapd_header_len, checksum_offset;
389         guint16         address, cr, sapi, tei;
390         gboolean        is_response = 0;
391         tvbuff_t        *next_tvb;
392         const char      *srcname = "?";
393         const char      *dstname = "?";
394
395         if (check_col(pinfo->cinfo, COL_PROTOCOL))
396                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPD");
397         if (check_col(pinfo->cinfo, COL_INFO))
398                 col_clear(pinfo->cinfo, COL_INFO);
399
400         address = tvb_get_ntohs(tvb, 0);
401         cr = address & LAPD_CR;
402         tei = (address & LAPD_TEI) >> LAPD_TEI_SHIFT;
403         sapi = (address & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
404         lapd_header_len = 2;    /* address */
405
406         if (check_col(pinfo->cinfo, COL_TEI))
407                 col_add_fstr(pinfo->cinfo, COL_TEI, "%u", tei);
408
409         if (pinfo->fd->lnk_t == WTAP_ENCAP_LINUX_LAPD) {
410                 /* frame is captured via libpcap */
411                 if (pinfo->pseudo_header->lapd.pkttype == 4 /*PACKET_OUTGOING*/) {
412                         if (pinfo->pseudo_header->lapd.we_network) {
413                                 is_response = cr ? FALSE : TRUE;
414                                 srcname = "Local Network";
415                                 dstname = "Remote User";
416                         } else {
417                                 srcname = "Local User";
418                                 dstname = "Remote Network";
419                         }
420                 }
421                 else if (pinfo->pseudo_header->lapd.pkttype == 3 /*PACKET_OTHERHOST*/) {
422                         /* We must be a TE, sniffing what other TE transmit */
423
424                         is_response = cr ? TRUE : FALSE;
425                         srcname = "Remote User";
426                         dstname = "Remote Network";
427                 }
428                 else {
429                         /* The frame is incoming */
430                         if (pinfo->pseudo_header->lapd.we_network) {
431                                 is_response = cr ? TRUE : FALSE;
432                                 srcname = "Remote User";
433                                 dstname = "Local Network";
434                         } else {
435                                 is_response = cr ? FALSE : TRUE;
436                                 srcname = "Remote Network";
437                                 dstname = "Local User";
438                         }
439                 }
440         }
441         else if (pinfo->p2p_dir == P2P_DIR_RECV) {
442                 is_response = cr ? FALSE : TRUE;
443                 srcname = "Network";
444                 dstname = "User";
445         }
446         else if (pinfo->p2p_dir == P2P_DIR_SENT) {
447                 is_response = cr ? TRUE : FALSE;
448                 srcname = "User";
449                 dstname = "Network";
450         }
451
452         if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
453             col_set_str(pinfo->cinfo, COL_RES_DL_SRC, srcname);
454         if(check_col(pinfo->cinfo, COL_RES_DL_DST))
455             col_set_str(pinfo->cinfo, COL_RES_DL_DST, dstname);
456
457         if (tree) {
458                 proto_item *direction_ti;
459
460                 lapd_ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, -1,
461                     FALSE);
462                 lapd_tree = proto_item_add_subtree(lapd_ti, ett_lapd);
463
464                 direction_ti = proto_tree_add_uint(lapd_tree, hf_lapd_direction,
465                                                    tvb, 0, 0, pinfo->p2p_dir);
466                 PROTO_ITEM_SET_GENERATED(direction_ti);
467
468                 addr_ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb,
469                     0, 2, address);
470                 addr_tree = proto_item_add_subtree(addr_ti, ett_lapd_address);
471
472                 if(global_lapd_gsm_sapis){
473                         proto_tree_add_uint(addr_tree, hf_lapd_gsm_sapi,tvb, 0, 1, address);
474                 }else{
475                         proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
476                 }
477                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
478                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
479                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
480                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
481         }
482         else {
483                 lapd_ti = NULL;
484                 lapd_tree = NULL;
485         }
486
487         control = dissect_xdlc_control(tvb, 2, pinfo, lapd_tree, hf_lapd_control,
488             ett_lapd_control, &lapd_cf_items, &lapd_cf_items_ext, NULL, NULL,
489             is_response, TRUE, FALSE);
490         lapd_header_len += XDLC_CONTROL_LEN(control, TRUE);
491
492         if (tree)
493                 proto_item_set_len(lapd_ti, lapd_header_len);
494
495         if (NULL != p_get_proto_data(pinfo->fd, proto_lapd)
496                         && ((lapd_ppi_t*)p_get_proto_data(pinfo->fd, proto_lapd))->has_crc) {
497                 
498                 /* check checksum */
499                 checksum_offset = tvb_length(tvb) - 2;
500                 checksum = tvb_get_guint8(tvb, checksum_offset); /* high byte */
501                 checksum <<= 8;
502                 checksum |= tvb_get_guint8(tvb, checksum_offset+1) & 0x00FF; /* low byte */
503                 checksum_calculated = g_htons(crc16_ccitt_tvb(tvb, tvb_length(tvb) - 2));
504                 
505                 if (checksum == checksum_calculated) {
506                         checksum_ti = proto_tree_add_uint_format(lapd_tree, hf_lapd_checksum, tvb, checksum_offset, 2, 0,"Checksum: 0x%04x [correct]", checksum);
507                         checksum_tree = proto_item_add_subtree(checksum_ti, ett_lapd_checksum);
508                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_good, tvb, checksum_offset, 2, TRUE);
509                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_bad, tvb, checksum_offset, 2, FALSE);
510                 } else {
511                         checksum_ti = proto_tree_add_uint_format(lapd_tree, hf_lapd_checksum, tvb, checksum_offset, 2, 0,"Checksum: 0x%04x [incorrect, should be 0x%04x]", checksum, checksum_calculated);
512                         checksum_tree = proto_item_add_subtree(checksum_ti, ett_lapd_checksum);
513                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_good, tvb, checksum_offset, 2, FALSE);
514                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_bad, tvb, checksum_offset, 2, TRUE);
515                 }
516                 
517                 next_tvb = tvb_new_subset(tvb, lapd_header_len, tvb_length_remaining(tvb,lapd_header_len) - 2, -1);
518                 
519         } else 
520                 next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
521
522         if (XDLC_IS_INFORMATION(control)) {
523                 /* call next protocol */
524                 if(global_lapd_gsm_sapis){
525                         if (!dissector_try_port(lapd_gsm_sapi_dissector_table, sapi,
526                                 next_tvb, pinfo, tree))
527                                 call_dissector(data_handle,next_tvb, pinfo, tree);
528                 }else{
529                         if (!dissector_try_port(lapd_sapi_dissector_table, sapi,
530                                 next_tvb, pinfo, tree))
531                                 call_dissector(data_handle,next_tvb, pinfo, tree);
532                 }
533         } else
534                 call_dissector(data_handle,next_tvb, pinfo, tree);
535 }
536
537 void
538 proto_reg_handoff_lapd(void)
539 {
540         static gboolean init = FALSE;
541         static dissector_handle_t lapd_bitstream_handle;
542         static gint lapd_rtp_payload_type;
543
544         if (!init) {
545                 dissector_handle_t lapd_handle;
546
547                 lapd_handle = find_dissector("lapd");
548                 dissector_add("wtap_encap", WTAP_ENCAP_LINUX_LAPD, lapd_handle);
549
550                 lapd_bitstream_handle = create_dissector_handle(dissect_lapd_bitstream, proto_lapd);
551                 data_handle = find_dissector("data");
552
553                 init = TRUE;
554         } else {
555                 if ((lapd_rtp_payload_type > 95) && (lapd_rtp_payload_type < 128))
556                         dissector_delete("rtp.pt", lapd_rtp_payload_type, lapd_bitstream_handle);
557         }
558
559         lapd_rtp_payload_type = pref_lapd_rtp_payload_type;
560         if ((lapd_rtp_payload_type > 95) && (lapd_rtp_payload_type < 128))
561                 dissector_add("rtp.pt", lapd_rtp_payload_type, lapd_bitstream_handle);
562 }
563
564 void
565 proto_register_lapd(void)
566 {
567     static hf_register_info hf[] = {
568
569         { &hf_lapd_direction,
570           { "Direction", "lapd.direction", FT_UINT8, BASE_DEC, VALS(lapd_direction_vals), 0x0,
571                 "Direction", HFILL }},
572
573         { &hf_lapd_address,
574           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0,
575                 "Address", HFILL }},
576
577         { &hf_lapd_sapi,
578           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
579                 "Service Access Point Identifier", HFILL }},
580
581         { &hf_lapd_gsm_sapi,
582           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_gsm_sapi_vals), LAPD_SAPI,
583                 "Service Access Point Identifier", HFILL }},
584
585         { &hf_lapd_cr,
586           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
587                 "Command/Response bit", HFILL }},
588
589         { &hf_lapd_ea1,
590           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
591                 "First Address Extension bit", HFILL }},
592
593         { &hf_lapd_tei,
594           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
595                 "Terminal Endpoint Identifier", HFILL }},
596
597         { &hf_lapd_ea2,
598           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
599                 "Second Address Extension bit", HFILL }},
600
601         { &hf_lapd_control,
602           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
603                 "Control field", HFILL }},
604
605         { &hf_lapd_n_r,
606             { "N(R)", "lapd.control.n_r", FT_UINT16, BASE_DEC,
607                 NULL, XDLC_N_R_EXT_MASK, "", HFILL }},
608
609         { &hf_lapd_n_s,
610             { "N(S)", "lapd.control.n_s", FT_UINT16, BASE_DEC,
611                 NULL, XDLC_N_S_EXT_MASK, "", HFILL }},
612
613         { &hf_lapd_p,
614             { "Poll", "lapd.control.p", FT_BOOLEAN, 8,
615                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
616
617         { &hf_lapd_p_ext,
618             { "Poll", "lapd.control.p", FT_BOOLEAN, 16,
619                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
620
621         { &hf_lapd_f,
622             { "Final", "lapd.control.f", FT_BOOLEAN, 8,
623                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
624
625         { &hf_lapd_f_ext,
626             { "Final", "lapd.control.f", FT_BOOLEAN, 16,
627                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
628
629         { &hf_lapd_s_ftype,
630             { "Supervisory frame type", "lapd.control.s_ftype", FT_UINT16, BASE_HEX,
631                 VALS(stype_vals), XDLC_S_FTYPE_MASK, "", HFILL }},
632
633         { &hf_lapd_u_modifier_cmd,
634             { "Command", "lapd.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
635                 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, "", HFILL }},
636
637         { &hf_lapd_u_modifier_resp,
638             { "Response", "lapd.control.u_modifier_resp", FT_UINT8, BASE_HEX,
639                 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, "", HFILL }},
640
641         { &hf_lapd_ftype_i,
642             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
643                 VALS(ftype_vals), XDLC_I_MASK, "", HFILL }},
644
645         { &hf_lapd_ftype_s_u,
646             { "Frame type", "lapd.control.ftype", FT_UINT8, BASE_HEX,
647                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
648
649         { &hf_lapd_ftype_s_u_ext,
650             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
651                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
652
653         { &hf_lapd_checksum,
654             { "Checksum", "lapd.checksum", FT_UINT16, BASE_HEX, 
655                 NULL, 0x0, "Details at: http://www.wireshark.org/docs/wsug_html_chunked/ChAdvChecksums.html", HFILL }},
656
657         { &hf_lapd_checksum_good,
658             { "Good Checksum", "lapd.checksum_good", FT_BOOLEAN, BASE_NONE, 
659                 NULL, 0x0, "True: checksum matches packet content; False: doesn't match content or not checked", HFILL }},
660
661         { &hf_lapd_checksum_bad,
662             { "Bad Checksum", "lapd.checksum_bad", FT_BOOLEAN, BASE_NONE,
663                 NULL, 0x0, "True: checksum doesn't match packet content; False: matches content or not checked", HFILL }}
664     };
665
666     static gint *ett[] = {
667         &ett_lapd,
668         &ett_lapd_address,
669         &ett_lapd_control,
670         &ett_lapd_checksum
671     };
672
673         module_t *lapd_module;
674
675         proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
676                                          "LAPD", "lapd");
677         proto_register_field_array (proto_lapd, hf, array_length(hf));
678         proto_register_subtree_array(ett, array_length(ett));
679
680         register_dissector("lapd", dissect_lapd, proto_lapd);
681
682         lapd_sapi_dissector_table = register_dissector_table("lapd.sapi",
683                                                              "LAPD SAPI", FT_UINT16, BASE_DEC);
684
685         lapd_gsm_sapi_dissector_table = register_dissector_table("lapd.gsm.sapi",
686                                                                  "LAPD GSM SAPI", FT_UINT16, BASE_DEC);
687
688         lapd_module = prefs_register_protocol(proto_lapd, proto_reg_handoff_lapd);
689
690         prefs_register_bool_preference(lapd_module, "use_gsm_sapi_values",
691                 "Use GSM SAPI values",
692                 "Use SAPI values as specified in TS 48 056",
693                 &global_lapd_gsm_sapis);
694         prefs_register_uint_preference(lapd_module, "rtp_payload_type",
695                 "RTP payload type for embedded LAPD",
696                 "RTP payload type for embedded LAPD. It must be one of the dynamic types "
697                 "from 96 to 127. Set it to 0 to disable.",
698                  10, &pref_lapd_rtp_payload_type);
699
700 }
701