For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[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 <glib.h>
45 #include <string.h>
46 #include <epan/packet.h>
47 #include <epan/conversation.h>
48 #include <epan/xdlc.h>
49 #include <epan/crc16-tvb.h>
50 #include <epan/prefs.h>
51 #include <epan/lapd_sapi.h>
52
53 static int proto_lapd = -1;
54 static int hf_lapd_direction = -1;
55 static int hf_lapd_address = -1;
56 static int hf_lapd_sapi = -1;
57 static int hf_lapd_gsm_sapi = -1;
58 static int hf_lapd_cr = -1;
59 static int hf_lapd_ea1 = -1;
60 static int hf_lapd_tei = -1;
61 static int hf_lapd_ea2 = -1;
62 static int hf_lapd_control = -1;
63 static int hf_lapd_n_r = -1;
64 static int hf_lapd_n_s = -1;
65 static int hf_lapd_p = -1;
66 static int hf_lapd_p_ext = -1;
67 static int hf_lapd_f = -1;
68 static int hf_lapd_f_ext = -1;
69 static int hf_lapd_s_ftype = -1;
70 static int hf_lapd_u_modifier_cmd = -1;
71 static int hf_lapd_u_modifier_resp = -1;
72 static int hf_lapd_ftype_i = -1;
73 static int hf_lapd_ftype_s_u = -1;
74 static int hf_lapd_ftype_s_u_ext = -1;
75 static int hf_lapd_checksum = -1;
76 static int hf_lapd_checksum_good = -1;
77 static int hf_lapd_checksum_bad = -1;
78
79 static gint ett_lapd = -1;
80 static gint ett_lapd_address = -1;
81 static gint ett_lapd_control = -1;
82 static gint ett_lapd_checksum = -1;
83 static gint pref_lapd_rtp_payload_type = 0;
84
85 static dissector_table_t lapd_sapi_dissector_table;
86 static dissector_table_t lapd_gsm_sapi_dissector_table;
87
88 /* Whether to use GSM SAPI vals or not */
89 static gboolean global_lapd_gsm_sapis = FALSE;
90
91 static dissector_handle_t data_handle;
92
93 /*
94  * Bits in the address field.
95  */
96 #define LAPD_SAPI               0xfc00  /* Service Access Point Identifier */
97 #define LAPD_SAPI_SHIFT         10
98 #define LAPD_CR                 0x0200  /* Command/Response bit */
99 #define LAPD_EA1                0x0100  /* First Address Extension bit */
100 #define LAPD_TEI                0x00fe  /* Terminal Endpoint Identifier */
101 #define LAPD_TEI_SHIFT          1
102 #define LAPD_EA2                0x0001  /* Second Address Extension bit */
103
104 static const value_string lapd_direction_vals[] = {
105         { P2P_DIR_RECV,         "Network->User"},
106         { P2P_DIR_SENT,         "User->Network"},
107         { 0,                    NULL }
108 };
109
110 static const value_string lapd_sapi_vals[] = {
111         { LAPD_SAPI_Q931,       "Q.931 Call control procedure" },
112         { LAPD_SAPI_PM_Q931,    "Packet mode Q.931 Call control procedure" },
113         { LAPD_SAPI_X25,        "X.25 Level 3 procedures" },
114         { LAPD_SAPI_L2,         "Layer 2 management procedures" },
115         { 0,                    NULL }
116 };
117
118 static const value_string lapd_gsm_sapi_vals[] = {
119         { LAPD_GSM_SAPI_RA_SIG_PROC,    "Radio signalling procedures" },
120         { LAPD_GSM_SAPI_NOT_USED_1,     "(Not used in GSM PLMN)" },
121         { LAPD_GSM_SAPI_NOT_USED_16,    "(Not used in GSM PLMN)" },
122         { LAPD_GSM_SAPI_OM_PROC,        "Operation and maintenance procedure" },
123         { LAPD_SAPI_L2,                 "Layer 2 management procedures" },
124         { 0,                            NULL }
125 };
126
127 /* Used only for U frames */
128 static const xdlc_cf_items lapd_cf_items = {
129         NULL,
130         NULL,
131         &hf_lapd_p,
132         &hf_lapd_f,
133         NULL,
134         &hf_lapd_u_modifier_cmd,
135         &hf_lapd_u_modifier_resp,
136         NULL,
137         &hf_lapd_ftype_s_u
138 };
139
140 /* Used only for I and S frames */
141 static const xdlc_cf_items lapd_cf_items_ext = {
142         &hf_lapd_n_r,
143         &hf_lapd_n_s,
144         &hf_lapd_p_ext,
145         &hf_lapd_f_ext,
146         &hf_lapd_s_ftype,
147         NULL,
148         NULL,
149         &hf_lapd_ftype_i,
150         &hf_lapd_ftype_s_u_ext
151 };
152
153
154 /* LAPD frame detection state */
155 enum lapd_bitstream_states {OUT_OF_SYNC, FLAGS, DATA};
156
157 typedef struct lapd_byte_state {
158         enum lapd_bitstream_states state;       /* frame detection state */
159         char            full_byte;              /* part of a full byte */
160         char            bit_offset;             /* number of bits already got in the full byte */
161         int             ones;                   /* number of consecutive ones since the last zero */
162 } lapd_byte_state_t;
163
164 typedef struct lapd_ppi {
165         gboolean                has_crc;                /* CRC is captured with LAPD the frames */
166         lapd_byte_state_t       start_byte_state;       /* LAPD bitstream byte state at the beginnigng of processing the packet */
167 } lapd_ppi_t;
168
169 /* Fill values in lapd_byte_state struct */
170 static void
171 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, 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_child_real_data(tvb, buff, data_len, data_len);
280                                         tvb_set_free_cb(new_tvb, g_free);
281                                         add_new_data_source(pinfo, new_tvb, "Decoded LAPD bitstream");
282                                         dissect_lapd(new_tvb, pinfo, tree);
283                                         data_len = 0;
284                                         state = FLAGS;
285                                         bit_offset++;
286                                 } else if (ones >= 7) { /* frame reset or 11111111 flag byte */
287                                         data_len = 0;
288                                         state = OUT_OF_SYNC;
289                                         bit_offset++;
290                                 } else {
291                                         bit_offset++;
292                                 }
293                                 ones = 0;
294                         }
295
296                         if (bit_offset == 8) { /* we have a new complete byte */
297                                 switch (state) {
298                                         case OUT_OF_SYNC:
299                                                 if (full_byte == 0x7E) { /* we have a flag byte */
300                                                         state = FLAGS;
301                                                         full_byte = 0x00;
302                                                         bit_offset = 0;
303                                                 } else { /* no sync yet, wait for a new byte */
304                                                         full_byte = (full_byte >> 1) & 0x7F;
305                                                         bit_offset--;
306                                                 }
307                                                 break;
308
309                                         case FLAGS:
310                                                 if (full_byte == 0x7E) { /* we have a flag byte */
311                                                         full_byte = 0x00;
312                                                         bit_offset = 0;
313                                                 } else { /* we got the first data byte */
314                                                         state = DATA;
315                                                         new_byte(full_byte, data, &data_len);
316                                                         full_byte = 0x00;
317                                                         bit_offset = 0;
318                                                 }
319                                                 break;
320
321                                         case DATA:
322                                                 /* we got a new data byte */
323                                                 new_byte(full_byte, data, &data_len);
324                                                 full_byte = 0x00;
325                                                 bit_offset = 0;
326                                                 break;
327                                 }
328                         }
329                 }
330         }
331
332         if (state == DATA) { /* we are in the middle of an LAPD frame, we need more bytes */
333                 pinfo->desegment_offset = 0;
334                 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
335                 return;
336         } else { /* finished processing LAPD frame(s) */
337                 if (NULL == p_get_proto_data(pinfo->fd, proto_lapd)) {
338                         /* Per packet information */
339                         lapd_ppi = g_malloc(sizeof(lapd_ppi_t));
340                         lapd_ppi->has_crc = TRUE;
341                         if (prev_byte_state)
342                                 fill_lapd_byte_state(&lapd_ppi->start_byte_state, prev_byte_state->state,
343                                                 prev_byte_state->full_byte, prev_byte_state->bit_offset,
344                                                 prev_byte_state->ones);
345                         else
346                                 fill_lapd_byte_state(&lapd_ppi->start_byte_state, OUT_OF_SYNC, 0x00, 0, 0);
347
348                         p_add_proto_data(pinfo->fd, proto_lapd, lapd_ppi);
349
350
351                         /* Conversation info*/
352
353                         if (conversation) {
354                                 if (convo_data) { /* already have lapd convo data */
355                                         if (forward_stream)
356                                                 fill_lapd_byte_state(convo_data->byte_state_a, state, full_byte, bit_offset, ones);
357                                         else {
358                                                 if (!convo_data->byte_state_b)
359                                                         convo_data->byte_state_b = g_malloc(sizeof(lapd_byte_state_t));
360                                                 fill_lapd_byte_state(convo_data->byte_state_b, state, full_byte, bit_offset, ones);
361                                         }
362                                 } else { /* lapd convo data has to be created */
363                                         lapd_byte_state = g_malloc(sizeof(lapd_byte_state_t));
364                                         fill_lapd_byte_state(lapd_byte_state, state, full_byte, bit_offset, ones);
365                                         convo_data = g_malloc(sizeof(lapd_convo_data_t));
366                                         COPY_ADDRESS(&convo_data->addr_a, &pinfo->src);
367                                         COPY_ADDRESS(&convo_data->addr_b, &pinfo->dst);
368                                         convo_data->port_a = pinfo->srcport;
369                                         convo_data->port_b = pinfo->destport;
370                                         convo_data->byte_state_a = lapd_byte_state;
371                                         convo_data->byte_state_b = NULL;
372                                         conversation_add_proto_data(conversation, proto_lapd, convo_data);
373                                 }
374                         }
375                 }
376         }
377 }
378
379
380 static void
381 dissect_lapd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
382 {
383         proto_tree      *lapd_tree, *addr_tree, *checksum_tree;
384         proto_item      *lapd_ti, *addr_ti, *checksum_ti;
385         int             direction;
386         guint16         control, checksum, checksum_calculated;
387         int             lapd_header_len, checksum_offset;
388         guint16         addr, cr, sapi, tei;
389         gboolean        is_response = 0;
390         tvbuff_t        *next_tvb;
391         const char      *srcname = "?";
392         const char      *dstname = "?";
393
394         col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPD");
395         col_clear(pinfo->cinfo, COL_INFO);
396
397         addr = tvb_get_ntohs(tvb, 0);
398         cr = addr & LAPD_CR;
399         tei = (addr & LAPD_TEI) >> LAPD_TEI_SHIFT;
400         sapi = (addr & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
401         lapd_header_len = 2;    /* addr */
402
403         if (check_col(pinfo->cinfo, COL_TEI))
404                 col_add_fstr(pinfo->cinfo, COL_TEI, "%u", tei);
405
406         if (pinfo->fd->lnk_t == WTAP_ENCAP_LINUX_LAPD) {
407                 /* frame is captured via libpcap */
408                 if (pinfo->pseudo_header->lapd.pkttype == 4 /*PACKET_OUTGOING*/) {
409                         if (pinfo->pseudo_header->lapd.we_network) {
410                                 is_response = cr ? FALSE : TRUE;
411                                 srcname = "Local Network";
412                                 dstname = "Remote User";
413                                 direction = P2P_DIR_RECV;       /* Network->User */
414                         } else {
415                                 srcname = "Local User";
416                                 dstname = "Remote Network";
417                                 direction = P2P_DIR_SENT;       /* User->Network */
418                         }
419                 }
420                 else if (pinfo->pseudo_header->lapd.pkttype == 3 /*PACKET_OTHERHOST*/) {
421                         /* We must be a TE, sniffing what other TE transmit */
422
423                         is_response = cr ? TRUE : FALSE;
424                         srcname = "Remote User";
425                         dstname = "Remote Network";
426                         direction = P2P_DIR_SENT;       /* User->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                                 direction = P2P_DIR_SENT;       /* User->Network */
435                         } else {
436                                 is_response = cr ? FALSE : TRUE;
437                                 srcname = "Remote Network";
438                                 dstname = "Local User";
439                                 direction = P2P_DIR_RECV;       /* Network->User */
440                         }
441                 }
442         } else {
443                 direction = pinfo->p2p_dir;
444                 if (pinfo->p2p_dir == P2P_DIR_RECV) {
445                         is_response = cr ? FALSE : TRUE;
446                         srcname = "Network";
447                         dstname = "User";
448                 }
449                 else if (pinfo->p2p_dir == P2P_DIR_SENT) {
450                         is_response = cr ? TRUE : FALSE;
451                         srcname = "User";
452                         dstname = "Network";
453                 }
454         }
455
456         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, srcname);
457         col_set_str(pinfo->cinfo, COL_RES_DL_DST, dstname);
458
459         if (tree) {
460                 proto_item *direction_ti;
461
462                 lapd_ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, -1,
463                     ENC_NA);
464                 lapd_tree = proto_item_add_subtree(lapd_ti, ett_lapd);
465
466                 /*
467                  * Don't show the direction if we don't know it.
468                  */
469                 if (direction != P2P_DIR_UNKNOWN) {
470                         direction_ti = proto_tree_add_uint(lapd_tree, hf_lapd_direction,
471                                                            tvb, 0, 0, pinfo->p2p_dir);
472                         PROTO_ITEM_SET_GENERATED(direction_ti);
473                 }
474
475                 addr_ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb,
476                     0, 2, addr);
477                 addr_tree = proto_item_add_subtree(addr_ti, ett_lapd_address);
478
479                 if(global_lapd_gsm_sapis){
480                         proto_tree_add_uint(addr_tree, hf_lapd_gsm_sapi,tvb, 0, 1, addr);
481                 }else{
482                         proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, addr);
483                 }
484                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, addr);
485                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, addr);
486                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, addr);
487                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, addr);
488         }
489         else {
490                 lapd_ti = NULL;
491                 lapd_tree = NULL;
492         }
493
494         control = dissect_xdlc_control(tvb, 2, pinfo, lapd_tree, hf_lapd_control,
495             ett_lapd_control, &lapd_cf_items, &lapd_cf_items_ext, NULL, NULL,
496             is_response, TRUE, FALSE);
497         lapd_header_len += XDLC_CONTROL_LEN(control, TRUE);
498
499         if (tree)
500                 proto_item_set_len(lapd_ti, lapd_header_len);
501
502         if (NULL != p_get_proto_data(pinfo->fd, proto_lapd)
503                         && ((lapd_ppi_t*)p_get_proto_data(pinfo->fd, proto_lapd))->has_crc) {
504
505                 /* check checksum */
506                 checksum_offset = tvb_length(tvb) - 2;
507                 checksum = tvb_get_guint8(tvb, checksum_offset); /* high byte */
508                 checksum <<= 8;
509                 checksum |= tvb_get_guint8(tvb, checksum_offset+1) & 0x00FF; /* low byte */
510                 checksum_calculated = g_htons(crc16_ccitt_tvb(tvb, tvb_length(tvb) - 2));
511
512                 if (checksum == checksum_calculated) {
513                         checksum_ti = proto_tree_add_uint_format(lapd_tree, hf_lapd_checksum, tvb, checksum_offset, 2, 0,"Checksum: 0x%04x [correct]", checksum);
514                         checksum_tree = proto_item_add_subtree(checksum_ti, ett_lapd_checksum);
515                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_good, tvb, checksum_offset, 2, TRUE);
516                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_bad, tvb, checksum_offset, 2, FALSE);
517                 } else {
518                         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);
519                         checksum_tree = proto_item_add_subtree(checksum_ti, ett_lapd_checksum);
520                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_good, tvb, checksum_offset, 2, FALSE);
521                         proto_tree_add_boolean(checksum_tree, hf_lapd_checksum_bad, tvb, checksum_offset, 2, TRUE);
522                 }
523
524                 next_tvb = tvb_new_subset(tvb, lapd_header_len, tvb_length_remaining(tvb,lapd_header_len) - 2, -1);
525
526         } else
527                 next_tvb = tvb_new_subset_remaining(tvb, lapd_header_len);
528
529         if (XDLC_IS_INFORMATION(control)) {
530                 /* call next protocol */
531                 if(global_lapd_gsm_sapis){
532                         if (!dissector_try_uint(lapd_gsm_sapi_dissector_table, sapi,
533                                 next_tvb, pinfo, tree))
534                                 call_dissector(data_handle,next_tvb, pinfo, tree);
535                 }else{
536                         if (!dissector_try_uint(lapd_sapi_dissector_table, sapi,
537                                 next_tvb, pinfo, tree))
538                                 call_dissector(data_handle,next_tvb, pinfo, tree);
539                 }
540         } else
541                 call_dissector(data_handle,next_tvb, pinfo, tree);
542 }
543
544 void
545 proto_reg_handoff_lapd(void);
546
547 void
548 proto_register_lapd(void)
549 {
550     static hf_register_info hf[] = {
551
552         { &hf_lapd_direction,
553           { "Direction", "lapd.direction", FT_UINT8, BASE_DEC, VALS(lapd_direction_vals), 0x0,
554                 NULL, HFILL }},
555
556         { &hf_lapd_address,
557           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0,
558                 "Address", HFILL }},
559
560         { &hf_lapd_sapi,
561           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
562                 "Service Access Point Identifier", HFILL }},
563
564         { &hf_lapd_gsm_sapi,
565           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_gsm_sapi_vals), LAPD_SAPI,
566                 "Service Access Point Identifier", HFILL }},
567
568         { &hf_lapd_cr,
569           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
570                 "Command/Response bit", HFILL }},
571
572         { &hf_lapd_ea1,
573           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
574                 "First Address Extension bit", HFILL }},
575
576         { &hf_lapd_tei,
577           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
578                 "Terminal Endpoint Identifier", HFILL }},
579
580         { &hf_lapd_ea2,
581           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
582                 "Second Address Extension bit", HFILL }},
583
584         { &hf_lapd_control,
585           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
586                 NULL, HFILL }},
587
588         { &hf_lapd_n_r,
589             { "N(R)", "lapd.control.n_r", FT_UINT16, BASE_DEC,
590                 NULL, XDLC_N_R_EXT_MASK, NULL, HFILL }},
591
592         { &hf_lapd_n_s,
593             { "N(S)", "lapd.control.n_s", FT_UINT16, BASE_DEC,
594                 NULL, XDLC_N_S_EXT_MASK, NULL, HFILL }},
595
596         { &hf_lapd_p,
597             { "Poll", "lapd.control.p", FT_BOOLEAN, 8,
598                 TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},
599
600         { &hf_lapd_p_ext,
601             { "Poll", "lapd.control.p", FT_BOOLEAN, 16,
602                 TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},
603
604         { &hf_lapd_f,
605             { "Final", "lapd.control.f", FT_BOOLEAN, 8,
606                 TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},
607
608         { &hf_lapd_f_ext,
609             { "Final", "lapd.control.f", FT_BOOLEAN, 16,
610                 TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},
611
612         { &hf_lapd_s_ftype,
613             { "Supervisory frame type", "lapd.control.s_ftype", FT_UINT16, BASE_HEX,
614                 VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},
615
616         { &hf_lapd_u_modifier_cmd,
617             { "Command", "lapd.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
618                 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
619
620         { &hf_lapd_u_modifier_resp,
621             { "Response", "lapd.control.u_modifier_resp", FT_UINT8, BASE_HEX,
622                 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
623
624         { &hf_lapd_ftype_i,
625             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
626                 VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},
627
628         { &hf_lapd_ftype_s_u,
629             { "Frame type", "lapd.control.ftype", FT_UINT8, BASE_HEX,
630                 VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
631
632         { &hf_lapd_ftype_s_u_ext,
633             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
634                 VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
635
636         { &hf_lapd_checksum,
637             { "Checksum", "lapd.checksum", FT_UINT16, BASE_HEX,
638                 NULL, 0x0, "Details at: http://www.wireshark.org/docs/wsug_html_chunked/ChAdvChecksums.html", HFILL }},
639
640         { &hf_lapd_checksum_good,
641             { "Good Checksum", "lapd.checksum_good", FT_BOOLEAN, BASE_NONE,
642                 NULL, 0x0, "True: checksum matches packet content; False: doesn't match content or not checked", HFILL }},
643
644         { &hf_lapd_checksum_bad,
645             { "Bad Checksum", "lapd.checksum_bad", FT_BOOLEAN, BASE_NONE,
646                 NULL, 0x0, "True: checksum doesn't match packet content; False: matches content or not checked", HFILL }}
647     };
648
649     static gint *ett[] = {
650         &ett_lapd,
651         &ett_lapd_address,
652         &ett_lapd_control,
653         &ett_lapd_checksum
654     };
655
656         module_t *lapd_module;
657
658         proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
659                                          "LAPD", "lapd");
660         proto_register_field_array (proto_lapd, hf, array_length(hf));
661         proto_register_subtree_array(ett, array_length(ett));
662
663         register_dissector("lapd", dissect_lapd, proto_lapd);
664
665         lapd_sapi_dissector_table = register_dissector_table("lapd.sapi",
666                                                              "LAPD SAPI", FT_UINT16, BASE_DEC);
667
668         lapd_gsm_sapi_dissector_table = register_dissector_table("lapd.gsm.sapi",
669                                                                  "LAPD GSM SAPI", FT_UINT16, BASE_DEC);
670
671         lapd_module = prefs_register_protocol(proto_lapd, proto_reg_handoff_lapd);
672
673         prefs_register_bool_preference(lapd_module, "use_gsm_sapi_values",
674                 "Use GSM SAPI values",
675                 "Use SAPI values as specified in TS 48 056",
676                 &global_lapd_gsm_sapis);
677         prefs_register_uint_preference(lapd_module, "rtp_payload_type",
678                 "RTP payload type for embedded LAPD",
679                 "RTP payload type for embedded LAPD. It must be one of the dynamic types "
680                 "from 96 to 127. Set it to 0 to disable.",
681                  10, &pref_lapd_rtp_payload_type);
682
683 }
684
685 void
686 proto_reg_handoff_lapd(void)
687 {
688         static gboolean init = FALSE;
689         static dissector_handle_t lapd_bitstream_handle;
690         static gint lapd_rtp_payload_type;
691
692         if (!init) {
693                 dissector_handle_t lapd_handle;
694
695                 lapd_handle = find_dissector("lapd");
696                 dissector_add_uint("wtap_encap", WTAP_ENCAP_LINUX_LAPD, lapd_handle);
697                 dissector_add_uint("wtap_encap", WTAP_ENCAP_LAPD, lapd_handle);
698
699                 lapd_bitstream_handle = create_dissector_handle(dissect_lapd_bitstream, proto_lapd);
700                 data_handle = find_dissector("data");
701
702                 init = TRUE;
703         } else {
704                 if ((lapd_rtp_payload_type > 95) && (lapd_rtp_payload_type < 128))
705                         dissector_delete_uint("rtp.pt", lapd_rtp_payload_type, lapd_bitstream_handle);
706         }
707
708         lapd_rtp_payload_type = pref_lapd_rtp_payload_type;
709         if ((lapd_rtp_payload_type > 95) && (lapd_rtp_payload_type < 128))
710                 dissector_add_uint("rtp.pt", lapd_rtp_payload_type, lapd_bitstream_handle);
711 }