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