Fill value_strings.
[obnox/wireshark/wip.git] / epan / dissectors / packet-q931.c
1 /* packet-q931.c
2  * Routines for Q.931 frame disassembly
3  * Guy Harris <guy@alum.mit.edu>
4  *
5  * $Id$
6  *
7  * Modified by Andreas Sikkema for possible use with H.323
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <glib.h>
33 #include <string.h>
34 #include <epan/packet.h>
35 #include <epan/tap.h>
36 #include <epan/strutil.h>
37 #include <epan/nlpid.h>
38 #include "packet-q931.h"
39 #include "packet-e164.h"
40 #include <epan/prefs.h>
41 #include <epan/reassemble.h>
42 #include <epan/emem.h>
43
44 #include <epan/sctpppids.h>
45 #include <epan/lapd_sapi.h>
46 #include "packet-tpkt.h"
47
48
49 /* Q.931 references:
50  *
51  * http://www.acacia-net.com/Clarinet/Protocol/q9313svn.htm
52  * http://www.acacia-net.com/Clarinet/Protocol/q9311sc3.htm
53  * http://www.acacia-net.com/Clarinet/Protocol/q9317oz7.htm
54  * http://www.protocols.com/pbook/isdn.htm
55  * http://freesoft.org/CIE/Topics/126.htm
56  * http://noc.comstar.ru/miscdocs/ascend-faq-cause-codes.html
57  * http://www.andrews-arnold.co.uk/isdn/q931cause.html
58  * http://www.tulatelecom.ru/staff/german/DSSHelp/MessList/InfEl/InfElList.html
59  */
60 static void reset_q931_packet_info(q931_packet_info *pi);
61 static gboolean have_valid_q931_pi=FALSE;
62 static q931_packet_info *q931_pi=NULL;
63 static int q931_tap = -1;
64
65 static int proto_q931                                   = -1;
66 static int hf_q931_discriminator                        = -1;
67 static int hf_q931_coding_standard                      = -1;
68 static int hf_q931_interpretation                       = -1;
69 static int hf_q931_pres_meth_prot_prof                  = -1;
70 static int hf_q931_high_layer_characteristics           = -1;
71 static int hf_q931_extended_high_layer_characteristics  = -1;
72 static int hf_q931_extended_audiovisual_characteristics = -1;
73 static int hf_q931_information_transfer_capability      = -1;
74 static int hf_q931_transfer_mode                        = -1;
75 static int hf_q931_information_transfer_rate            = -1;
76 static int hf_q931_layer_ident                          = -1;
77 static int hf_q931_uil1                                 = -1;
78 static int hf_q931_call_ref_len                         = -1;
79 static int hf_q931_call_ref_flag                        = -1;
80 static int hf_q931_call_ref                             = -1;
81 static int hf_q931_message_type                         = -1;
82 static int hf_q931_maintenance_message_type     = -1;
83 static int hf_q931_segment_type                         = -1;
84 static int hf_q931_cause_location                       = -1;
85 static int hf_q931_cause_value                          = -1;
86 static int hf_q931_number_type                          = -1;
87 static int hf_q931_numbering_plan                       = -1;
88 static int hf_q931_extension_ind                        = -1;
89 static int hf_q931_calling_party_number                 = -1;
90 static int hf_q931_called_party_number                  = -1;
91 static int hf_q931_connected_number                     = -1;
92 static int hf_q931_redirecting_number                   = -1;
93 static int hf_q931_screening_ind                                = -1;
94 static int hf_q931_presentation_ind                             = -1;
95
96 /* fields for Channel Indentification IE */
97 static int hf_q931_channel_interface_explicit           = -1;
98 static int hf_q931_channel_interface_type               = -1;
99 static int hf_q931_channel_exclusive                    = -1;
100 static int hf_q931_channel_dchan                        = -1;
101 static int hf_q931_channel_selection_bri                = -1;
102 static int hf_q931_channel_selection_pri                = -1;
103 static int hf_q931_channel_map                          = -1;
104 static int hf_q931_channel_element_type                 = -1;
105 static int hf_q931_channel_number                       = -1;
106
107
108 static int hf_q931_segments = -1;
109 static int hf_q931_segment = -1;
110 static int hf_q931_segment_overlap = -1;
111 static int hf_q931_segment_overlap_conflict = -1;
112 static int hf_q931_segment_multiple_tails = -1;
113 static int hf_q931_segment_too_long_segment = -1;
114 static int hf_q931_segment_error = -1;
115 static int hf_q931_reassembled_in = -1;
116 static int hf_q931_reassembled_length = -1;
117
118 static gint ett_q931                                    = -1;
119 static gint ett_q931_ie                                 = -1;
120
121 static gint ett_q931_segments = -1;
122 static gint ett_q931_segment = -1;
123
124 static const fragment_items q931_frag_items = {
125         &ett_q931_segment,
126         &ett_q931_segments,
127
128         &hf_q931_segments,
129         &hf_q931_segment,
130         &hf_q931_segment_overlap,
131         &hf_q931_segment_overlap_conflict,
132         &hf_q931_segment_multiple_tails,
133         &hf_q931_segment_too_long_segment,
134         &hf_q931_segment_error,
135         &hf_q931_reassembled_in,
136         &hf_q931_reassembled_length,
137         "segments"
138 };
139
140 /* Tables for reassembly of fragments. */
141 static GHashTable *q931_fragment_table = NULL;
142 static GHashTable *q931_reassembled_table = NULL;
143
144 /* Preferences */
145 static gboolean q931_reassembly = TRUE;
146
147 static dissector_table_t codeset_dissector_table;
148 static dissector_table_t ie_dissector_table;
149
150 /* desegmentation of Q.931 over TPKT over TCP */
151 static gboolean q931_desegment = TRUE;
152
153 /* Subdissectors */
154 static dissector_handle_t h225_handle;
155 static dissector_handle_t q931_tpkt_handle;
156 static dissector_handle_t q931_tpkt_pdu_handle;
157 static dissector_handle_t data_handle = NULL;
158
159 static heur_dissector_list_t q931_user_heur_subdissector_list;
160
161 static void
162 dissect_q931_IEs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *root_tree,
163     proto_tree *q931_tree, gboolean is_over_ip, int offset, int initial_codeset);
164
165 const value_string q931_message_type_vals[] = {
166 /*  0 */        { Q931_ESCAPE,                          "ESCAPE" },
167 /*  1 */        { Q931_ALERTING,                        "ALERTING" },
168 /*  2 */        { Q931_CALL_PROCEEDING,         "CALL PROCEEDING" },
169 /*  3 */        { Q931_PROGRESS,                        "PROGRESS" },
170 /*  5 */        { Q931_SETUP,                           "SETUP" },
171 /*  6 */        { Q931_GROUIP_SERVICE,          "GROUP SERVICE" },
172 /*  7 */        { Q931_CONNECT,                         "CONNECT" },
173 /*  8 */        { Q931_RESYNC_REQ,                      "RESYNC REQ" },
174 /*  9 */        { Q931_RESYNC_RESP,                     "RESYNC RESP" },
175 /* 10 */        { Q931_VERSION,                         "VERSION" },
176 /* 11 */        { Q931_GROUIP_SERVICE_ACK,      "GROUP SERVICE ACK" },
177 /* 13 */        { Q931_SETUP_ACK,                       "SETUP ACKNOWLEDGE" },
178 /* 15 */        { Q931_CONNECT_ACK,                     "CONNECT ACKNOWLEDGE" },
179 /* 32 */        { Q931_USER_INFORMATION,        "USER INFORMATION" },
180 /* 33 */        { Q931_SUSPEND_REJECT,          "SUSPEND REJECT" },
181 /* 34 */        { Q931_RESUME_REJECT,           "RESUME REJECT" },
182 /* 36 */        { Q931_HOLD,                            "HOLD" },
183 /* 37 */        { Q931_SUSPEND,                         "SUSPEND" },
184 /* 38 */        { Q931_RESUME,                          "RESUME" },
185 /* 40 */        { Q931_HOLD_ACK,                        "HOLD_ACKNOWLEDGE" },
186 /* 45 */        { Q931_SUSPEND_ACK,                     "SUSPEND ACKNOWLEDGE" },
187 /* 46 */        { Q931_RESUME_ACK,                      "RESUME ACKNOWLEDGE" },
188 /* 48 */        { Q931_HOLD_REJECT,                     "HOLD_REJECT" },
189 /* 49 */        { Q931_RETRIEVE,                        "RETRIEVE" },
190 /* 51 */        { Q931_RETRIEVE_ACK,            "RETRIEVE ACKNOWLEDGE" },
191 /* 55 */        { Q931_RETRIEVE_REJECT,         "RETRIEVE REJECT" },
192 /* 64 */        { Q931_DETACH,                          "DETACH" },
193 /* 69 */        { Q931_DISCONNECT,                      "DISCONNECT" },
194 /* 70 */        { Q931_RESTART,                         "RESTART" },
195 /* 72 */        { Q931_DETACH_ACKNOWLEDGE,      "DETACH ACKNOWLEDGE" },
196 /* 77 */        { Q931_RELEASE,                         "RELEASE" },
197 /* 78 */        { Q931_RESTART_ACK,                     "RESTART ACKNOWLEDGE" },
198 /* 90 */        { Q931_RELEASE_COMPLETE,        "RELEASE COMPLETE" },
199 /* 96 */        { Q931_SEGMENT,                         "SEGMENT" },
200 /* 98 */        { Q931_FACILITY,                        "FACILITY" },
201 /*100 */        { Q931_REGISTER,                        "REGISTER" },
202 /*106 */        { Q931_FACILITY_ACKNOWLEDGE,    "FACILITY ACKNOWLEDGE" },
203 /*110 */        { Q931_NOTIFY,                          "NOTIFY" },
204 /*114 */        { Q931_FACILITY_REJECT,         "FACILITY REJECT" },
205 /*117 */        { Q931_STATUS_ENQUIRY,          "STATUS ENQUIRY" },
206 /*121 */        { Q931_CONGESTION_CONTROL,      "CONGESTION CONTROL" },
207 /*123 */        { Q931_INFORMATION,                     "INFORMATION" },
208 /*125 */        { Q931_STATUS,                          "STATUS" },
209
210         { 0,                            NULL }
211 };
212 static value_string_ext q931_message_type_vals_ext = VALUE_STRING_EXT_INIT(q931_message_type_vals);
213
214 static const value_string dms_message_type_vals[] = {
215         { DMS_SERVICE_ACKNOWLEDGE,      "SERVICE ACKNOWLEDGE" },
216         { DMS_SERVICE,                          "SERVICE" },
217         { 0,                            NULL }
218 };
219
220 /*
221  * NOTE  For call reference flag (octet 2)
222  * Bit8
223  *      0 The message is sent from the side that originates the call reference
224  *      1 The message is sent to the side that originates the call reference
225  */
226 static const true_false_string tfs_call_ref_flag = {
227         "Message sent to originating side",
228         "Message sent from originating side"
229 };
230
231 static const true_false_string tfs_interface_type = {
232         "Primary rate interface",
233         "Basic rate interface"
234 };
235
236 static const true_false_string tfs_channel_exclusive = {
237         "Exclusive; only the indicated channel is acceptable",
238         "Indicated channel is preferred"
239 };
240
241 static const true_false_string tfs_channel_map = {
242         "Channel indicated by slot map",
243         "Channel indicated by number"
244 };
245
246 /*
247  * Information elements.
248  */
249
250 /* Shifted codeset values */
251 #define CS0 0x000
252 #define CS1 0x100
253 #define CS2 0x200
254 #define CS3 0x300
255 #define CS4 0x400
256 #define CS5 0x500
257 #define CS6 0x600
258 #define CS7 0x700
259
260 #define Q931_IE_SO_MASK 0x80    /* single-octet/variable-length mask */
261 /*
262  * Single-octet IEs.
263  */
264 #define Q931_IE_SO_IDENTIFIER_MASK      0xf0    /* IE identifier mask */
265 #define Q931_IE_SO_IDENTIFIER_SHIFT     4       /* IE identifier shift */
266 #define Q931_IE_SO_IE_MASK              0x0F    /* IE mask */
267
268 #define Q931_IE_SHIFT                   0x90
269 #define Q931_IE_SHIFT_NON_LOCKING       0x08    /* non-locking shift */
270 #define Q931_IE_SHIFT_CODESET           0x07    /* codeset */
271
272 #define Q931_IE_MORE_DATA_OR_SEND_COMP  0xA0    /* More Data or Sending Complete */
273 #define Q931_IE_MORE_DATA               0xA0
274 #define Q931_IE_SENDING_COMPLETE        0xA1
275
276 #define Q931_IE_CONGESTION_LEVEL        0xB0
277 #define Q931_IE_REPEAT_INDICATOR        0xD0
278
279 /*
280  * Variable-length IEs.
281  */
282 #define Q931_IE_VL_EXTENSION            0x80    /* Extension flag */
283 /*      extension bit. The bit value "0" indicates that the octet continues through the         */
284 /*      next octet. The bit value "1" indicates that this octet is the last octet               */
285
286 static const true_false_string q931_extension_ind_value = {
287         "last octet",
288         "information continues through the next octet"
289 };
290
291
292 /*
293  * Codeset 0 (default).
294  */
295 #define Q931_IE_SEGMENTED_MESSAGE       0x00
296 #define Q931_IE_CHANGE_STATUS   0x01
297 #define Q931_IE_BEARER_CAPABILITY       0x04
298 #define Q931_IE_CAUSE                   0x08
299 #define Q931_IE_CALL_IDENTITY           0x10
300 #define Q931_IE_CALL_STATE              0x14
301 #define Q931_IE_CHANNEL_IDENTIFICATION  0x18
302 #define Q931_IE_FACILITY                0x1C
303 #define Q931_IE_PROGRESS_INDICATOR      0x1E
304 #define Q931_IE_NETWORK_SPECIFIC_FACIL  0x20    /* Network Specific Facilities */
305 #define Q931_IE_NOTIFICATION_INDICATOR  0x27
306 #define Q931_IE_DISPLAY                 0x28
307 #define Q931_IE_DATE_TIME               0x29
308 #define Q931_IE_KEYPAD_FACILITY         0x2C
309 #define Q931_IE_INFORMATION_REQUEST     0x32
310 #define Q931_IE_SIGNAL                  0x34
311 #define Q931_IE_SWITCHHOOK              0x36
312 #define Q931_IE_FEATURE_ACTIVATION      0x38
313 #define Q931_IE_FEATURE_INDICATION      0x39
314 #define Q931_IE_ENDPOINT_IDENTIFIER     0x3B
315 #define Q931_IE_SERVICE_PROFILE_ID      0x3A
316 #define Q931_IE_INFORMATION_RATE        0x40
317 #define Q931_IE_E2E_TRANSIT_DELAY       0x42    /* End-to-end Transit Delay */
318 #define Q931_IE_TD_SELECTION_AND_INT    0x43    /* Transit Delay Selection and Indication */
319 #define Q931_IE_PL_BINARY_PARAMETERS    0x44    /* Packet layer binary parameters */
320 #define Q931_IE_PL_WINDOW_SIZE          0x45    /* Packet layer window size */
321 #define Q931_IE_PACKET_SIZE             0x46    /* Packet size */
322 #define Q931_IE_CUG                     0x47    /* Closed user group */
323 #define Q931_IE_REVERSE_CHARGE_IND      0x4A    /* Reverse charging indication */
324 #define Q931_IE_CONNECTED_NUMBER_DEFAULT        0x4C    /* Connected Number */
325 #define Q931_IE_INTERFACE_SERVICE       0x66    /* q931+ Interface Service */
326 #define Q931_IE_CHANNEL_STATUS          0x67    /* q931+ Channel Status */
327 #define Q931_IE_VERSION_INFO            0x68    /* q931+ Version Info */
328 #define Q931_IE_CALLING_PARTY_NUMBER    0x6C    /* Calling Party Number */
329 #define Q931_IE_CALLING_PARTY_SUBADDR   0x6D    /* Calling Party Subaddress */
330 #define Q931_IE_CALLED_PARTY_NUMBER     0x70    /* Called Party Number */
331 #define Q931_IE_CALLED_PARTY_SUBADDR    0x71    /* Called Party Subaddress */
332 #define Q931_IE_REDIRECTING_NUMBER      0x74
333 #define Q931_IE_REDIRECTION_NUMBER      0x76
334 #define Q931_IE_TRANSIT_NETWORK_SEL     0x78    /* Transit Network Selection */
335 #define Q931_IE_RESTART_INDICATOR       0x79
336 #define Q931_IE_LOW_LAYER_COMPAT        0x7C    /* Low-Layer Compatibility */
337 #define Q931_IE_HIGH_LAYER_COMPAT       0x7D    /* High-Layer Compatibility */
338 #define Q931_IE_USER_USER               0x7E    /* User-User */
339 #define Q931_IE_ESCAPE                  0x7F    /* Escape for extension */
340
341 /*
342  * Codeset 0 ETSI.
343  */
344 #define Q931_IE_CONNECTED_NUMBER        0x8C
345 #define Q931_IE_CONNECTED_SUBADDR       0x8D
346
347 /*
348  * Codeset 5 (National-specific) Belgium.
349  */
350 #define Q931_IE_CHARGING_ADVICE         0x1A
351
352 /*
353  * Codeset 5 (National-specific) Bellcore National ISDN.
354  */
355 #define Q931_IE_OPERATOR_SYSTEM_ACCESS  0x1D
356
357 /*
358  * Codeset 5 ETSI ETS 300 192
359  */
360 #define Q931_IE_PARTY_CATEGORY          0x32
361
362 /*
363  * Codeset 6 (Network-specific) Belgium.
364  */
365 /* 0x1A is Charging Advice, as with Codeset 5 */
366 #define Q931_IE_REDIRECTING_NUMBER      0x74
367
368 /*
369  * Codeset 6 (Network-specific) FT-Numeris.
370  */
371 /* 0x1D is User Capability */
372
373 /*
374  * Codeset 6 (Network-specific) Bellcore National ISDN.
375  */
376 #define Q931_IE_REDIRECTING_SUBADDR     0x75    /* Redirecting Subaddress */
377 /* 0x76 is Redirection Number, but that's also Codeset 0 */
378 #define Q931_IE_CALL_APPEARANCE         0x7B
379
380 /* Codeset 0 */
381 static const value_string q931_info_element_vals0[] = {
382         { Q931_IE_SEGMENTED_MESSAGE,            "Segmented message" },
383         { Q931_IE_CHANGE_STATUS,                        "Change status" },
384         { Q931_IE_BEARER_CAPABILITY,            "Bearer capability" },
385         { Q931_IE_CAUSE,                        "Cause" },
386         { Q931_IE_CALL_IDENTITY,                "Call identity" },
387         { Q931_IE_CALL_STATE,                   "Call state" },
388         { Q931_IE_CHANNEL_IDENTIFICATION,       "Channel identification" },
389         { Q931_IE_FACILITY,                     "Facility" },
390         { Q931_IE_PROGRESS_INDICATOR,           "Progress indicator" },
391         { Q931_IE_NETWORK_SPECIFIC_FACIL,       "Network specific facilities" },
392         { Q931_IE_NOTIFICATION_INDICATOR,       "Notification indicator" },
393         { Q931_IE_DISPLAY,                      "Display" },
394         { Q931_IE_DATE_TIME,                    "Date/Time" },
395         { Q931_IE_KEYPAD_FACILITY,              "Keypad facility" },
396         { Q931_IE_INFORMATION_REQUEST,          "Information request" },
397         { Q931_IE_SIGNAL,                       "Signal" },
398         { Q931_IE_SWITCHHOOK,                   "Switchhook" },
399         { Q931_IE_FEATURE_ACTIVATION,           "Feature activation" },
400         { Q931_IE_FEATURE_INDICATION,           "Feature Indication" },
401         { Q931_IE_ENDPOINT_IDENTIFIER,          "Endpoint identifier" },
402         { Q931_IE_SERVICE_PROFILE_ID,           "Service profile ID" },
403         { Q931_IE_INFORMATION_RATE,             "Information rate" },
404         { Q931_IE_E2E_TRANSIT_DELAY,            "End-to-end transit delay" },
405         { Q931_IE_TD_SELECTION_AND_INT,         "Transit delay selection and indication" },
406         { Q931_IE_PL_BINARY_PARAMETERS,         "Packet layer binary parameters" },
407         { Q931_IE_PL_WINDOW_SIZE,               "Packet layer window size" },
408         { Q931_IE_PACKET_SIZE,                  "Packet size" },
409         { Q931_IE_CUG,                          "Closed user group" },
410         { Q931_IE_REVERSE_CHARGE_IND,           "Reverse charging indication" },
411         { Q931_IE_CONNECTED_NUMBER_DEFAULT,     "Connected number" },
412         { Q931_IE_INTERFACE_SERVICE,            "Interface Service" },
413         { Q931_IE_CHANNEL_STATUS,               "Channel Status" },
414         { Q931_IE_VERSION_INFO,                 "Version Info" },
415         { Q931_IE_CALLING_PARTY_NUMBER,         "Calling party number" },
416         { Q931_IE_CALLING_PARTY_SUBADDR,        "Calling party subaddress" },
417         { Q931_IE_CALLED_PARTY_NUMBER,          "Called party number" },
418         { Q931_IE_CALLED_PARTY_SUBADDR,         "Called party subaddress" },
419         { Q931_IE_REDIRECTING_NUMBER,           "Redirecting number" },
420         { Q931_IE_REDIRECTION_NUMBER,           "Redirection number" },
421         { Q931_IE_TRANSIT_NETWORK_SEL,          "Transit network selection" },
422         { Q931_IE_RESTART_INDICATOR,            "Restart indicator" },
423         { Q931_IE_LOW_LAYER_COMPAT,             "Low-layer compatibility" },
424         { Q931_IE_HIGH_LAYER_COMPAT,            "High-layer compatibility" },
425         { Q931_IE_USER_USER,                    "User-user" },
426         { Q931_IE_ESCAPE,                       "Escape" },
427         { Q931_IE_CONNECTED_NUMBER,             "Connected number" },
428         { Q931_IE_CONNECTED_SUBADDR,            "Connected subaddress" },
429         { 0,                                    NULL }
430 };
431
432 /* Codeset 1 */
433 static const value_string q931_info_element_vals1[] = {
434         { 0,                                    NULL }
435 };
436 /* Codeset 2 */
437 static const value_string q931_info_element_vals2[] = {
438         { 0,                                    NULL }
439 };
440 /* Codeset 3 */
441 static const value_string q931_info_element_vals3[] = {
442         { 0,                                    NULL }
443 };
444 /* Codeset 4 */
445 static const value_string q931_info_element_vals4[] = {
446         { 0,                                    NULL }
447 };
448 /* Codeset 5 */
449 static const value_string q931_info_element_vals5[] = {
450         { Q931_IE_CHARGING_ADVICE,              "Charging advice" },
451         { Q931_IE_OPERATOR_SYSTEM_ACCESS,       "Operator system access" },
452         { Q931_IE_PARTY_CATEGORY,               "Party category"},
453         { 0,                                    NULL }
454 };
455 /* Codeset 6 */
456 static const value_string q931_info_element_vals6[] = {
457         { Q931_IE_REDIRECTING_NUMBER,           "Redirecting number" },
458         { Q931_IE_REDIRECTING_SUBADDR,          "Redirecting subaddress" },
459         { Q931_IE_CALL_APPEARANCE,              "Call appearance" },
460         { Q931_IE_DISPLAY,                      "Avaya Display" }, /* if Avaya codeset to send display = 6 */
461         { 0,                                    NULL }
462 };
463 /* Codeset 7 */
464 static const value_string q931_info_element_vals7[] = {
465         { 0,                                    NULL }
466 };
467
468 /* Codeset array */
469 #define NUM_INFO_ELEMENT_VALS   (Q931_IE_SHIFT_CODESET+1)
470 static const value_string *q931_info_element_vals[NUM_INFO_ELEMENT_VALS] = {
471   q931_info_element_vals0,
472   q931_info_element_vals1,
473   q931_info_element_vals2,
474   q931_info_element_vals3,
475   q931_info_element_vals4,
476   q931_info_element_vals5,
477   q931_info_element_vals6,
478   q931_info_element_vals7,
479 };
480
481 static const value_string q931_congestion_level_vals[] = {
482         { 0x0, "Receiver ready" },
483         { 0xF, "Receiver not ready" },
484         { 0,   NULL }
485 };
486
487 static const value_string q931_repeat_indication_vals[] = {
488         { 0x2, "Prioritized list" },
489         { 0,   NULL }
490 };
491
492 /*
493  * ITU-standardized coding.
494  */
495 #define Q931_ITU_STANDARDIZED_CODING    0x00
496
497 /*
498  * Dissect a Segmented message information element.
499  */
500 static void
501 dissect_q931_segmented_message_ie(tvbuff_t *tvb, int offset, int len,
502     proto_tree *tree)
503 {
504         if (len != 2) {
505                 proto_tree_add_text(tree, tvb, offset, len,
506                     "Segmented message: length is %d, should be 2", len);
507                 return;
508         }
509         if (tvb_get_guint8(tvb, offset) & 0x80) {
510                 proto_tree_add_text(tree, tvb, offset, 1,
511                     "First segment: %u segments remaining",
512                     tvb_get_guint8(tvb, offset) & 0x7F);
513         } else {
514                 proto_tree_add_text(tree, tvb, offset, 1,
515                     "Not first segment: %u segments remaining",
516                     tvb_get_guint8(tvb, offset) & 0x7F);
517         }
518         proto_tree_add_item(tree, hf_q931_segment_type, tvb, offset + 1, 1, FALSE);
519 }
520
521 /*
522  * Dissect a Bearer capability or Low-layer compatibility information element.
523  */
524 static const value_string q931_coding_standard_vals[] = {
525         { 0x0, "ITU-T standardized coding" },
526         { 0x1, "ISO/IEC standard" },
527         { 0x2, "National standard" },
528         { 0x3, "Standard defined for this particular network" },
529         { 0,    NULL }
530 };
531
532 static const value_string q931_information_transfer_capability_vals[] = {
533         { 0x00, "Speech" },
534         { 0x08, "Unrestricted digital information" },
535         { 0x09, "Restricted digital information" },
536         { 0x10, "3.1 kHz audio" },
537         { 0x11, "Unrestricted digital information with tones/announcements" },
538         { 0x18, "Video" },
539         { 0,    NULL }
540 };
541
542 static const value_string q931_transfer_mode_vals[] = {
543         { 0x00, "Circuit mode" },
544         { 0x02, "Packet mode" },
545         { 0,    NULL }
546 };
547
548 #define Q931_IT_RATE_MULTIRATE  0x18
549
550 static const value_string q931_information_transfer_rate_vals[] = {
551         { 0x00,                         "Packet mode" },
552         { 0x10,                         "64 kbit/s" },
553         { 0x11,                         "2 x 64 kbit/s" },
554         { 0x13,                         "384 kbit/s" },
555         { 0x15,                         "1536 kbit/s" },
556         { 0x17,                         "1920 kbit/s" },
557         { Q931_IT_RATE_MULTIRATE,       "Multirate (64 kbit/s base rate)" },
558         { 0,                            NULL }
559 };
560
561 /*
562  * Values 0x0a and 0x0b added from Q.931 Amendment 1 (12/2002)
563  */
564 static const value_string q931_uil1_vals[] = {
565         { 0x01, "V.110/I.460/X.30 rate adaption" },
566         { 0x02, "Recommendation G.711 u-law" },
567         { 0x03, "Recommendation G.711 A-law" },
568         { 0x04, "Recommendation G.721 32 kbit/s ADPCM and Recommendation I.460" },
569         { 0x05, "Recommendation H.221 and H.242" },
570         { 0x06, "Recommendation H.223 and H.245" },
571         { 0x07, "Non-ITU-T-standardized rate adaption" },
572         { 0x08, "V.120 rate adaption" },
573         { 0x09, "X.31 HDLC flag stuffing" },
574         { 0x0a, "Recommendation G.728 LD-CELP" },
575         { 0x0b, "Recommendation G.729 CS-ACELP" },
576         { 0,    NULL }
577 };
578 static value_string_ext q931_uil1_vals_ext = VALUE_STRING_EXT_INIT(q931_uil1_vals);
579
580 static const value_string q931_l1_user_rate_vals[] = {
581         { 0x00, "Rate indicated by E-bits" },
582         { 0x01, "0.6 kbit/s" },
583         { 0x02, "1.2 kbit/s" },
584         { 0x03, "2.4 kbit/s" },
585         { 0x04, "3.6 kbit/s" },
586         { 0x05, "4.8 kbit/s" },
587         { 0x06, "7.2 kbit/s" },
588         { 0x07, "8 kbit/s" },
589         { 0x08, "9.6 kbit/s" },
590         { 0x09, "14.4 kbit/s" },
591         { 0x0A, "16 kbit/s" },
592         { 0x0B, "19.2 kbit/s" },
593         { 0x0C, "32 kbit/s" },
594         { 0x0E, "48 kbit/s" },
595         { 0x0F, "56 kbit/s" },
596         { 0x10, "64 kbit/s "},
597         { 0x15, "0.1345 kbit/s" },
598         { 0x16, "0.100 kbit/s" },
599         { 0x17, "0.075/1.2 kbit/s" },
600         { 0x18, "1.2/0.075 kbit/s" },
601         { 0x19, "0.050 kbit/s" },
602         { 0x1A, "0.075 kbit/s" },
603         { 0x1B, "0.110 kbit/s" },
604         { 0x1C, "0.150 kbit/s" },
605         { 0x1D, "0.200 kbit/s" },
606         { 0x1E, "0.300 kbit/s" },
607         { 0x1F, "12 kbit/s" },
608         { 0,    NULL }
609 };
610 static value_string_ext q931_l1_user_rate_vals_ext = VALUE_STRING_EXT_INIT(q931_l1_user_rate_vals);
611
612 static const value_string q931_l1_intermediate_rate_vals[] = {
613         { 0x20, "8 kbit/s" },
614         { 0x40, "16 kbit/s" },
615         { 0x60, "32 kbit/s" },
616         { 0,    NULL }
617 };
618
619 static const value_string q931_l1_stop_bits_vals[] = {
620         { 0x20, "1" },
621         { 0x40, "1.5" },
622         { 0x60, "2" },
623         { 0,    NULL }
624 };
625
626 static const value_string q931_l1_data_bits_vals[] = {
627         { 0x08, "5" },
628         { 0x10, "7" },
629         { 0x18, "8" },
630         { 0,    NULL }
631 };
632
633 static const value_string q931_l1_parity_vals[] = {
634         { 0x00, "Odd" },
635         { 0x02, "Even" },
636         { 0x03, "None" },
637         { 0x04, "Forced to 0" },
638         { 0x05, "Forced to 1" },
639         { 0,    NULL }
640 };
641
642 static const value_string q931_l1_modem_type_vals[] = {
643         { 0x11, "V.21" },
644         { 0x12, "V.22" },
645         { 0x13, "V.22 bis" },
646         { 0x14, "V.23" },
647         { 0x15, "V.26" },
648         { 0x16, "V.26 bis" },
649         { 0x17, "V.26 ter" },
650         { 0x18, "V.27" },
651         { 0x19, "V.27 bis" },
652         { 0x1A, "V.27 ter" },
653         { 0x1B, "V.29" },
654         { 0x1C, "V.32" },
655         { 0x1E, "V.34" },
656         { 0,    NULL }
657 };
658 static value_string_ext q931_l1_modem_type_vals_ext = VALUE_STRING_EXT_INIT(q931_l1_modem_type_vals);
659
660 #define Q931_UIL2_USER_SPEC     0x10
661
662 static const value_string q931_uil2_vals[] = {
663         { 0x01,                 "Basic mode ISO 1745" },
664         { 0x02,                 "Q.921/I.441" },        /* LAPD */
665         { 0x06,                 "X.25, link layer" },   /* LAPB */
666         { 0x07,                 "X.25 multilink" },     /* or 0x0F? */
667         { 0x08,                 "T.71 Extended LAPB" },
668         { 0x09,                 "HDLC ARM" },
669         { 0x0A,                 "HDLC NRM" },
670         { 0x0B,                 "HDLC ABM" },
671         { 0x0C,                 "ISO 8802/2 LLC" },
672         { 0x0D,                 "X.75 Single Link Procedure" },
673         { 0x0E,                 "Q.922" },
674         { 0x0F,                 "Core aspects of Q.922" },
675         { Q931_UIL2_USER_SPEC,  "User-specified" },
676         { 0x11,                 "ISO 7776 DTE-DTE operation" },
677         { 0,                    NULL }
678 };
679 static value_string_ext q931_uil2_vals_ext = VALUE_STRING_EXT_INIT(q931_uil2_vals);
680
681 static const value_string q931_mode_vals[] = {
682         { 0x20, "Normal mode" },
683         { 0x40, "Extended mode" },
684         { 0,    NULL }
685 };
686
687 #define Q931_UIL3_X25_PL        0x06
688 #define Q931_UIL3_ISO_8208      0x07    /* X.25-based */
689 #define Q931_UIL3_X223          0x08    /* X.25-based */
690 #define Q931_UIL3_TR_9577       0x0B
691 #define Q931_UIL3_USER_SPEC     0x10
692
693 static const value_string q931_uil3_vals[] = {
694         { 0x02,                 "Q.931/I.451" },
695         { Q931_UIL3_X25_PL,     "X.25, packet layer" },
696         { Q931_UIL3_ISO_8208,   "ISO/IEC 8208" },
697         { Q931_UIL3_X223,       "X.223/ISO 8878" },
698         { 0x09,                 "ISO/IEC 8473" },
699         { 0x0A,                 "T.70" },
700         { Q931_UIL3_TR_9577,    "ISO/IEC TR 9577" },
701         { Q931_UIL3_USER_SPEC,  "User-specified" },
702         { 0,                    NULL }
703 };
704
705 static void
706 dissect_q931_protocol_discriminator(tvbuff_t *tvb, int offset, proto_tree *tree)
707 {
708         unsigned int discriminator = tvb_get_guint8(tvb, offset);
709
710         if (discriminator == NLPID_DMS) {
711                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
712                          tvb, offset, 1, discriminator,
713                          "Protocol discriminator: Maintenance messages");
714         } else if (discriminator == NLPID_Q_931) {
715                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
716                          tvb, offset, 1, discriminator,
717                          "Protocol discriminator: Q.931");
718         } else if (discriminator == NLPID_Q_2931) {
719                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
720                          tvb, offset, 1, discriminator,
721                          "Protocol discriminator: Q.2931");
722         } else if ((discriminator >= 16 && discriminator < 63)
723             || ((discriminator >= 80) && (discriminator < 254))) {
724                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
725                     tvb, offset, 1, discriminator,
726                     "Protocol discriminator: Network layer or layer 3 protocol (0x%02X)",
727                     discriminator);
728         } else if (discriminator >= 64 && discriminator <= 79) {
729                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
730                     tvb, offset, 1, discriminator,
731                     "Protocol discriminator: National use (0x%02X)",
732                     discriminator);
733         } else {
734                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
735                     tvb, offset, 1, discriminator,
736                     "Protocol discriminator: Reserved (0x%02X)",
737                     discriminator);
738         }
739 }
740
741 static const value_string q931_bearer_capability_layer_ident_vals[] = {
742         { 0x01, "Layer 1 identifier" },
743         { 0x02, "Layer 2 identifier" },
744         { 0x03, "Layer 3 identifier" },
745         { 0x00, NULL }
746 };
747
748 void
749 dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
750     proto_tree *tree)
751 {
752         guint8 octet;
753         guint8 coding_standard;
754         guint8 it_rate;
755         guint8 modem_type;
756         guint8 uil2_protocol;
757         guint8 uil3_protocol;
758         guint8 add_l3_info;
759
760         if (len == 0)
761                 return;
762         octet = tvb_get_guint8(tvb, offset);
763         coding_standard = octet & 0x60;
764         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
765                 /*
766                  * We don't know how the bearer capability is encoded,
767                  * so just dump it as data and be done with it.
768                  */
769                 proto_tree_add_text(tree, tvb, offset,
770                     len, "Data: %s",
771                     tvb_bytes_to_str(tvb, offset, len));
772                 proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
773                 proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
774                 return;
775         }
776         proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
777         proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
778         proto_tree_add_uint(tree, hf_q931_information_transfer_capability, tvb, offset, 1, octet);
779         offset += 1;
780         len -= 1;
781
782         /*
783          * XXX - only in Low-layer compatibility information element.
784          */
785         if (!(octet & Q931_IE_VL_EXTENSION)) {
786                 if (len == 0)
787                         return;
788                 octet = tvb_get_guint8(tvb, offset);
789                 proto_tree_add_text(tree, tvb, offset, 1,
790                     "Out-band negotiation %spossible",
791                     (octet & 0x40) ? "" : "not ");
792                 offset += 1;
793                 len -= 1;
794         }
795
796         if (len == 0)
797                 return;
798         octet = tvb_get_guint8(tvb, offset);
799         proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
800         proto_tree_add_uint(tree, hf_q931_transfer_mode, tvb, offset, 1, octet);
801         proto_tree_add_uint(tree, hf_q931_information_transfer_rate, tvb, offset, 1, octet);
802         it_rate = octet & 0x1F;
803         offset += 1;
804         len -= 1;
805
806         if (it_rate == Q931_IT_RATE_MULTIRATE) {
807                 if (len == 0)
808                         return;
809                 proto_tree_add_text(tree, tvb, offset, 1, "Rate multiplier: %u", tvb_get_guint8(tvb, offset));
810                 offset += 1;
811                 len -= 1;
812         }
813
814         if (len == 0)
815                 return;
816         octet = tvb_get_guint8(tvb, offset);
817         if ((octet & 0x60) == 0x20) {
818                 /*
819                  * Layer 1 information.
820                  */
821                 proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
822                 proto_tree_add_uint(tree, hf_q931_layer_ident, tvb, offset, 1, octet);
823                 proto_tree_add_uint(tree, hf_q931_uil1, tvb, offset, 1, octet);
824                 offset += 1;
825                 len -= 1;
826
827                 if (octet & Q931_IE_VL_EXTENSION)
828                         goto l1_done;
829                 if (len == 0)
830                         return;
831                 octet = tvb_get_guint8(tvb, offset);
832                 proto_tree_add_text(tree, tvb, offset, 1,
833                     "Layer 1 is %s",
834                     (octet & 0x40) ? "Asynchronous" : "Synchronous");
835                 proto_tree_add_text(tree, tvb, offset, 1,
836                     "Layer 1 in-band negotiation is %spossible",
837                     (octet & 0x20) ? "" : "not ");
838                 proto_tree_add_text(tree, tvb, offset, 1,
839                     "User rate: %s",
840                     val_to_str_ext(octet & 0x1F, &q931_l1_user_rate_vals_ext,
841                       "Unknown (0x%02X)"));
842                 offset += 1;
843                 len -= 1;
844
845                 if (octet & Q931_IE_VL_EXTENSION)
846                         goto l1_done;
847                 if (len == 0)
848                         return;
849                 octet = tvb_get_guint8(tvb, offset);
850                 proto_tree_add_text(tree, tvb, offset, 1,
851                     "Intermediate rate: %s",
852                       val_to_str(octet & 0x60, q931_l1_intermediate_rate_vals,
853                        "Unknown (0x%X)"));
854                 proto_tree_add_text(tree, tvb, offset, 1,
855                     "%s to send data with network independent clock",
856                     (octet & 0x10) ? "Required" : "Not required");
857                 proto_tree_add_text(tree, tvb, offset, 1,
858                     "%s accept data with network independent clock",
859                     (octet & 0x08) ? "Can" : "Cannot");
860                 proto_tree_add_text(tree, tvb, offset, 1,
861                     "%s to send data with flow control mechanism",
862                     (octet & 0x04) ? "Required" : "Not required");
863                 proto_tree_add_text(tree, tvb, offset, 1,
864                     "%s accept data with flow control mechanism",
865                     (octet & 0x02) ? "Can" : "Cannot");
866                 offset += 1;
867                 len -= 1;
868
869                 if (octet & Q931_IE_VL_EXTENSION)
870                         goto l1_done;
871                 if (len == 0)
872                         return;
873                 octet = tvb_get_guint8(tvb, offset);
874                 proto_tree_add_text(tree, tvb, offset, 1,
875                     "Rate adaption header %sincluded",
876                     (octet & 0x40) ? "" : "not ");
877                 proto_tree_add_text(tree, tvb, offset, 1,
878                     "Multiple frame establishment %ssupported",
879                     (octet & 0x20) ? "" : "not ");
880                 proto_tree_add_text(tree, tvb, offset, 1,
881                     "%s mode of operation",
882                     (octet & 0x10) ? "Protocol sensitive" : "Bit transparent");
883                 proto_tree_add_text(tree, tvb, offset, 1,
884                     (octet & 0x08) ?
885                       "Full protocol negotiation" : "LLI = 256 only");
886                 proto_tree_add_text(tree, tvb, offset, 1,
887                     "Message originator is %s",
888                     (octet & 0x04) ? "Assignor only" : "Default assignee");
889                 proto_tree_add_text(tree, tvb, offset, 1,
890                     "Negotiation is done %s",
891                     (octet & 0x02) ? "in-band" : "out-of-band");
892                 offset += 1;
893                 len -= 1;
894
895                 if (octet & Q931_IE_VL_EXTENSION)
896                         goto l1_done;
897                 if (len == 0)
898                         return;
899                 octet = tvb_get_guint8(tvb, offset);
900                 proto_tree_add_text(tree, tvb, offset, 1,
901                     "Stop bits: %s",
902                       val_to_str(octet & 0x60, q931_l1_stop_bits_vals,
903                        "Unknown (0x%X)"));
904                 proto_tree_add_text(tree, tvb, offset, 1,
905                     "Data bits: %s",
906                       val_to_str(octet & 0x18, q931_l1_data_bits_vals,
907                        "Unknown (0x%X)"));
908                 proto_tree_add_text(tree, tvb, offset, 1,
909                     "Parity: %s",
910                       val_to_str(octet & 0x07, q931_l1_parity_vals,
911                        "Unknown (0x%X)"));
912
913                 if (octet & Q931_IE_VL_EXTENSION)
914                         goto l1_done;
915                 if (len == 0)
916                         return;
917                 octet = tvb_get_guint8(tvb, offset);
918                 proto_tree_add_text(tree, tvb, offset, 1,
919                     "%s duplex",
920                     (octet & 0x40) ? "Full" : "Half");
921                 modem_type = octet & 0x3F;
922                 if (modem_type <= 0x5 ||
923                     (modem_type >= 0x20 && modem_type <= 0x2F)) {
924                         proto_tree_add_text(tree, tvb, offset, 1,
925                             "Modem type: National use 0x%02X", modem_type);
926                 } else if (modem_type >= 0x30) {
927                         proto_tree_add_text(tree, tvb, offset, 1,
928                             "Modem type: User specified 0x%02X", modem_type);
929                 } else {
930                         proto_tree_add_text(tree, tvb, offset, 1,
931                             "Modem type: %s",
932                               val_to_str_ext(modem_type, &q931_l1_modem_type_vals_ext,
933                               "Unknown (0x%02X)"));
934                 }
935                 offset += 1;
936                 len -= 1;
937         }
938 l1_done:
939         ;
940
941         if (len == 0)
942                 return;
943         octet = tvb_get_guint8(tvb, offset);
944         if ((octet & 0x60) == 0x40) {
945                 /*
946                  * Layer 2 information.
947                  */
948                 proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
949                 proto_tree_add_uint(tree, hf_q931_layer_ident, tvb, offset, 1, octet);
950                 uil2_protocol = octet & 0x1F;
951                 proto_tree_add_text(tree, tvb, offset, 1,
952                     "User information layer 2 protocol: %s",
953                     val_to_str_ext(uil2_protocol, &q931_uil2_vals_ext,
954                       "Unknown (0x%02X)"));
955                 offset += 1;
956                 len -= 1;
957
958                 /*
959                  * XXX - only in Low-layer compatibility information element.
960                  */
961                 if (octet & Q931_IE_VL_EXTENSION)
962                         goto l2_done;
963                 if (len == 0)
964                         return;
965                 octet = tvb_get_guint8(tvb, offset);
966                 if (uil2_protocol == Q931_UIL2_USER_SPEC) {
967                         proto_tree_add_text(tree, tvb, offset, 1,
968                             "User-specified layer 2 protocol information: 0x%02X",
969                             octet & 0x7F);
970                 } else {
971                         proto_tree_add_text(tree, tvb, offset, 1,
972                             "Mode: %s",
973                             val_to_str(octet & 0x60, q931_mode_vals,
974                               "Unknown (0x%02X)"));
975                 }
976                 offset += 1;
977                 len -= 1;
978
979                 if (octet & Q931_IE_VL_EXTENSION)
980                         goto l2_done;
981                 if (len == 0)
982                         return;
983                 octet = tvb_get_guint8(tvb, offset);
984                 proto_tree_add_text(tree, tvb, offset, 1,
985                     "Window size: %u k", octet & 0x7F);
986                 offset += 1;
987                 len -= 1;
988         }
989 l2_done:
990         ;
991
992         if (len == 0)
993                 return;
994         octet = tvb_get_guint8(tvb, offset);
995         if ((octet & 0x60) == 0x60) {
996                 /*
997                  * Layer 3 information.
998                  */
999                 proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
1000                 proto_tree_add_uint(tree, hf_q931_layer_ident, tvb, offset, 1, octet);
1001                 uil3_protocol = octet & 0x1F;
1002                 proto_tree_add_text(tree, tvb, offset, 1,
1003                     "User information layer 3 protocol: %s",
1004                     val_to_str(uil3_protocol, q931_uil3_vals,
1005                       "Unknown (0x%02X)"));
1006                 offset += 1;
1007                 len -= 1;
1008
1009
1010                 /*
1011                  * XXX - only in Low-layer compatibility information element.
1012                  */
1013                 if (octet & Q931_IE_VL_EXTENSION)
1014                         goto l3_done;
1015                 if (len == 0)
1016                         return;
1017                 octet = tvb_get_guint8(tvb, offset);
1018                 switch (uil3_protocol) {
1019
1020                 case Q931_UIL3_X25_PL:
1021                 case Q931_UIL3_ISO_8208:
1022                 case Q931_UIL3_X223:
1023                         proto_tree_add_text(tree, tvb, offset, 1,
1024                             "Mode: %s",
1025                             val_to_str(octet & 0x60, q931_mode_vals,
1026                               "Unknown (0x%02X)"));
1027                         offset += 1;
1028                         len -= 1;
1029
1030                         if (octet & Q931_IE_VL_EXTENSION)
1031                                 goto l3_done;
1032                         if (len == 0)
1033                                 return;
1034                         octet = tvb_get_guint8(tvb, offset);
1035                         proto_tree_add_text(tree, tvb, offset, 1,
1036                             "Default packet size: %u", octet & 0x0F);
1037                         offset += 1;
1038                         len -= 1;
1039
1040                         if (octet & Q931_IE_VL_EXTENSION)
1041                                 goto l3_done;
1042                         if (len == 0)
1043                                 return;
1044                         octet = tvb_get_guint8(tvb, offset);
1045                         proto_tree_add_text(tree, tvb, offset, 1,
1046                             "Packet window size: %u", octet & 0x7F);
1047                         offset += 1;
1048                         len -= 1;
1049                         break;
1050
1051                 case Q931_UIL3_USER_SPEC:
1052                         proto_tree_add_text(tree, tvb, offset, 1,
1053                             "Default packet size: %u octets",
1054                             1 << (octet & 0x0F));
1055                         offset += 1;
1056                         len -= 1;
1057                         break;
1058
1059                 case Q931_UIL3_TR_9577:
1060                         add_l3_info = (octet & 0x0F) << 4;
1061                         if (octet & Q931_IE_VL_EXTENSION)
1062                                 goto l3_done;
1063                         if (len == 0)
1064                                 return;
1065                         octet = tvb_get_guint8(tvb, offset + 1);
1066                         add_l3_info |= (octet & 0x0F);
1067                         proto_tree_add_text(tree, tvb, offset, 2,
1068                             "Additional layer 3 protocol information: %s",
1069                             val_to_str(add_l3_info, nlpid_vals,
1070                               "Unknown (0x%02X)"));
1071                         offset += 2;
1072                         len -= 2;
1073                         break;
1074                 }
1075         }
1076 l3_done:
1077         ;
1078 }
1079
1080 /*
1081  * Dissect a Cause information element.
1082  */
1083
1084
1085 static const value_string q931_cause_location_vals[] = {
1086         { 0x00, "User (U)" },
1087         { 0x01, "Private network serving the local user (LPN)" },
1088         { 0x02, "Public network serving the local user (LN)" },
1089         { 0x03, "Transit network (TN)" },
1090         { 0x04, "Public network serving the remote user (RLN)" },
1091         { 0x05, "Private network serving the remote user (RPN)" },
1092         { 0x06, "Unallocated code" },
1093         { 0x07, "International network (INTL)" },
1094         { 0x08, "Unallocated code" },
1095         { 0x09, "Unallocated code" },
1096         { 0x0A, "Network beyond interworking point (BI)" },
1097         { 0,    NULL }
1098 };
1099 value_string_ext q931_cause_location_vals_ext = VALUE_STRING_EXT_INIT(q931_cause_location_vals);
1100
1101 static const value_string q931_cause_recommendation_vals[] = {
1102         { 0x00, "Q.931" },
1103         { 0x03, "X.21" },
1104         { 0x04, "X.25" },
1105         { 0x05, "Q.1031/Q.1051" },
1106         { 0,    NULL }
1107 };
1108
1109 /*
1110  * Cause codes for Cause.
1111  */
1112 #define Q931_CAUSE_UNALLOC_NUMBER       0x01
1113 #define Q931_CAUSE_NO_ROUTE_TO_DEST     0x03
1114 #define Q931_CAUSE_CALL_REJECTED        0x15
1115 #define Q931_CAUSE_NUMBER_CHANGED       0x16
1116 #define Q931_CAUSE_ACCESS_INFO_DISC     0x2B
1117 #define Q931_CAUSE_QOS_UNAVAILABLE      0x31
1118 #define Q931_CAUSE_CHAN_NONEXISTENT     0x52
1119 #define Q931_CAUSE_INCOMPATIBLE_DEST    0x58
1120 #define Q931_CAUSE_MAND_IE_MISSING      0x60
1121 #define Q931_CAUSE_MT_NONEX_OR_UNIMPL   0x61
1122 #define Q931_CAUSE_IE_NONEX_OR_UNIMPL   0x63
1123 #define Q931_CAUSE_INVALID_IE_CONTENTS  0x64
1124 #define Q931_CAUSE_MSG_INCOMPAT_W_CS    0x65
1125 #define Q931_CAUSE_REC_TIMER_EXP        0x66
1126
1127 static const value_string q931_cause_code_vals[] = {
1128         { 0x00,                         "Valid cause code not yet received" },
1129         { Q931_CAUSE_UNALLOC_NUMBER,    "Unallocated (unassigned) number" },
1130         { 0x02,                         "No route to specified transit network" },
1131         { Q931_CAUSE_NO_ROUTE_TO_DEST,  "No route to destination" },
1132         { 0x04,                         "Send special information tone" },
1133         { 0x05,                         "Misdialled trunk prefix" },
1134         { 0x06,                         "Channel unacceptable" },
1135         { 0x07,                         "Call awarded and being delivered in an established channel" },
1136         { 0x08,                         "Prefix 0 dialed but not allowed" },
1137                                         /* Q.850 - "Preemption" */
1138         { 0x09,                         "Prefix 1 dialed but not allowed" },
1139                                         /* Q.850 - "Preemption - circuit reserved for reuse" */
1140         { 0x0A,                         "Prefix 1 dialed but not required" },
1141         { 0x0B,                         "More digits received than allowed, call is proceeding" },
1142         { 0x0E,                         "QoR: ported number" },
1143         { 0x10,                         "Normal call clearing" },
1144         { 0x11,                         "User busy" },
1145         { 0x12,                         "No user responding" },
1146         { 0x13,                         "No answer from user (user alerted)" },
1147         { 0x14,                         "Subscriber absent" },
1148         { Q931_CAUSE_CALL_REJECTED,     "Call rejected" },
1149         { Q931_CAUSE_NUMBER_CHANGED,    "Number changed" },
1150         { 0x17,                         "Reverse charging rejected" },
1151                                         /* Q.850 - "Redirection to new destination" */
1152         { 0x18,                         "Call suspended" },
1153                                         /* Q.850 Amendment 1 - "Call rejected due to feature at the destination" */
1154         { 0x19,                         "Call resumed" },
1155                                         /* Q.850 - "Exchange routing error */
1156         { 0x1A,                         "Non-selected user clearing" },
1157         { 0x1B,                         "Destination out of order" },
1158         { 0x1C,                         "Invalid number format (incomplete number)" },
1159         { 0x1D,                         "Facility rejected" },
1160         { 0x1E,                         "Response to STATUS ENQUIRY" },
1161         { 0x1F,                         "Normal unspecified" },
1162         { 0x21,                         "Circuit out of order" },
1163         { 0x22,                         "No circuit/channel available" },
1164         { 0x23,                         "Destination unattainable" },
1165         { 0x25,                         "Degraded service" },
1166         { 0x26,                         "Network out of order" },
1167         { 0x27,                         "Transit delay range cannot be achieved" },
1168                                         /* Q.850 - "Permanent frame mode connection out of service" */
1169         { 0x28,                         "Throughput range cannot be achieved" },
1170                                         /* Q.850 - "Permanent frame mode connection operational" */
1171         { 0x29,                         "Temporary failure" },
1172         { 0x2A,                         "Switching equipment congestion" },
1173         { Q931_CAUSE_ACCESS_INFO_DISC,  "Access information discarded" },
1174         { 0x2C,                         "Requested circuit/channel not available" },
1175         { 0x2D,                         "Pre-empted" },
1176         { 0x2E,                         "Precedence call blocked" },
1177         { 0x2F,                         "Resources unavailable, unspecified" },
1178         { Q931_CAUSE_QOS_UNAVAILABLE,   "Quality of service unavailable" },
1179         { 0x32,                         "Requested facility not subscribed" },
1180         { 0x33,                         "Reverse charging not allowed" },
1181         { 0x34,                         "Outgoing calls barred" },
1182         { 0x35,                         "Outgoing calls barred within CUG" },
1183         { 0x36,                         "Incoming calls barred" },
1184         { 0x37,                         "Incoming calls barred within CUG" },
1185         { 0x38,                         "Call waiting not subscribed" },
1186         { 0x39,                         "Bearer capability not authorized" },
1187         { 0x3A,                         "Bearer capability not presently available" },
1188         { 0x3E,                         "Inconsistency in designated outgoing access information and subscriber class" },
1189         { 0x3F,                         "Service or option not available, unspecified" },
1190         { 0x41,                         "Bearer capability not implemented" },
1191         { 0x42,                         "Channel type not implemented" },
1192         { 0x43,                         "Transit network selection not implemented" },
1193         { 0x44,                         "Message not implemented" },
1194         { 0x45,                         "Requested facility not implemented" },
1195         { 0x46,                         "Only restricted digital information bearer capability is available" },
1196         { 0x4F,                         "Service or option not implemented, unspecified" },
1197         { 0x51,                         "Invalid call reference value" },
1198         { Q931_CAUSE_CHAN_NONEXISTENT,  "Identified channel does not exist" },
1199         { 0x53,                         "Call identity does not exist for suspended call" },
1200         { 0x54,                         "Call identity in use" },
1201         { 0x55,                         "No call suspended" },
1202         { 0x56,                         "Call having the requested call identity has been cleared" },
1203         { 0x57,                         "Called user not member of CUG" },
1204         { Q931_CAUSE_INCOMPATIBLE_DEST, "Incompatible destination" },
1205         { 0x59,                         "Non-existent abbreviated address entry" },
1206         { 0x5A,                         "Destination address missing, and direct call not subscribed" },
1207                                         /* Q.850 - "Non-existent CUG" */
1208         { 0x5B,                         "Invalid transit network selection (national use)" },
1209         { 0x5C,                         "Invalid facility parameter" },
1210         { 0x5D,                         "Mandatory information element is missing" },
1211         { 0x5F,                         "Invalid message, unspecified" },
1212         { Q931_CAUSE_MAND_IE_MISSING,   "Mandatory information element is missing" },
1213         { Q931_CAUSE_MT_NONEX_OR_UNIMPL,"Message type non-existent or not implemented" },
1214         { 0x62,                         "Message not compatible with call state or message type non-existent or not implemented" },
1215         { Q931_CAUSE_IE_NONEX_OR_UNIMPL,"Information element non-existent or not implemented" },
1216         { Q931_CAUSE_INVALID_IE_CONTENTS,"Invalid information element contents" },
1217         { Q931_CAUSE_MSG_INCOMPAT_W_CS, "Message not compatible with call state" },
1218         { Q931_CAUSE_REC_TIMER_EXP,     "Recovery on timer expiry" },
1219         { 0x67,                         "Parameter non-existent or not implemented - passed on" },
1220         { 0x6E,                         "Message with unrecognized parameter discarded" },
1221         { 0x6F,                         "Protocol error, unspecified" },
1222         { 0x7F,                         "Internetworking, unspecified" },
1223         { 0,                            NULL }
1224 };
1225 value_string_ext q931_cause_code_vals_ext = VALUE_STRING_EXT_INIT(q931_cause_code_vals);
1226
1227 static const value_string q931_cause_condition_vals[] = {
1228         { 0x00, "Unknown" },
1229         { 0x01, "Permanent" },
1230         { 0x02, "Transient" },
1231         { 0x00, NULL }
1232 };
1233
1234 #define Q931_REJ_USER_SPECIFIC          0x00
1235 #define Q931_REJ_IE_MISSING             0x04
1236 #define Q931_REJ_IE_INSUFFICIENT        0x08
1237
1238 static const value_string q931_rejection_reason_vals[] = {
1239         { 0x00, "User specific" },
1240         { 0x04, "Information element missing" },
1241         { 0x08, "Information element contents are not sufficient" },
1242         { 0x00, NULL }
1243 };
1244
1245 static const gchar *get_message_name(guint8 prot_discr, guint8 message_type) {
1246         if (prot_discr == NLPID_DMS)
1247                 return val_to_str(message_type, dms_message_type_vals, "Unknown (0x%02X)");
1248         else
1249                 return val_to_str_ext(message_type, &q931_message_type_vals_ext, "Unknown (0x%02X)");
1250 }
1251
1252 static void
1253 dissect_q931_cause_ie_unsafe(tvbuff_t *tvb, int offset, int len,
1254     proto_tree *tree, int hf_cause_value, guint8 *cause_value, const value_string *ie_vals)
1255 {
1256         guint8 octet;
1257         guint8 coding_standard;
1258         guint8 rejection_reason;
1259
1260         if (len == 0)
1261                 return;
1262         octet = tvb_get_guint8(tvb, offset);
1263         coding_standard = octet & 0x60;
1264         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1265                 /*
1266                  * We don't know how the cause is encoded,
1267                  * so just dump it as data and be done with it.
1268                  */
1269                 proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
1270                 proto_tree_add_text(tree, tvb, offset,
1271                     len, "Data: %s",
1272                     tvb_bytes_to_str(tvb, offset, len));
1273                 return;
1274         }
1275         proto_tree_add_uint(tree, hf_q931_cause_location, tvb, offset, 1, octet);
1276         proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
1277         proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
1278         offset += 1;
1279         len -= 1;
1280
1281         if (!(octet & Q931_IE_VL_EXTENSION)) {
1282                 if (len == 0)
1283                         return;
1284                 octet = tvb_get_guint8(tvb, offset);
1285                 proto_tree_add_text(tree, tvb, offset, 1,
1286                     "Recommendation: %s",
1287                     val_to_str(octet & 0x7F, q931_cause_recommendation_vals,
1288                       "Unknown (0x%02X)"));
1289                 proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
1290                 offset += 1;
1291                 len -= 1;
1292         }
1293
1294         if (len == 0)
1295                 return;
1296         octet = tvb_get_guint8(tvb, offset);
1297         *cause_value = octet & 0x7F;
1298
1299         /* add cause value to packet info for use in tap */
1300         if(have_valid_q931_pi) {
1301                 q931_pi->cause_value = *cause_value;
1302         }
1303
1304         proto_tree_add_uint(tree, hf_cause_value, tvb, offset, 1, *cause_value);
1305         proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
1306         offset += 1;
1307         len -= 1;
1308
1309         if (len == 0)
1310                 return;
1311         switch (*cause_value) {
1312
1313         case Q931_CAUSE_UNALLOC_NUMBER:
1314         case Q931_CAUSE_NO_ROUTE_TO_DEST:
1315         case Q931_CAUSE_QOS_UNAVAILABLE:
1316                 octet = tvb_get_guint8(tvb, offset);
1317                 proto_tree_add_text(tree, tvb, offset, 1,
1318                     "Network service: %s",
1319                     (octet & 0x80) ? "User" : "Provider");
1320                 proto_tree_add_text(tree, tvb, offset, 1,
1321                     "%s",
1322                     (octet & 0x40) ? "Abnormal" : "Normal");
1323                 proto_tree_add_text(tree, tvb, offset, 1,
1324                     "Condition: %s",
1325                     val_to_str(octet & 0x03, q931_cause_condition_vals,
1326                       "Unknown (0x%X)"));
1327                 break;
1328
1329         case Q931_CAUSE_CALL_REJECTED:
1330                 rejection_reason = octet & 0x7C;
1331                 proto_tree_add_text(tree, tvb, offset, 1,
1332                     "Rejection reason: %s",
1333                     val_to_str(octet & 0x7C, q931_rejection_reason_vals,
1334                       "Unknown (0x%X)"));
1335                 proto_tree_add_text(tree, tvb, offset, 1,
1336                     "Condition: %s",
1337                     val_to_str(octet & 0x03, q931_cause_condition_vals,
1338                       "Unknown (0x%X)"));
1339                 offset += 1;
1340                 len -= 1;
1341
1342                 if (len == 0)
1343                         return;
1344                 switch (rejection_reason) {
1345
1346                 case Q931_REJ_USER_SPECIFIC:
1347                         proto_tree_add_text(tree, tvb, offset, len,
1348                             "User specific diagnostic: %s",
1349                             tvb_bytes_to_str(tvb, offset, len));
1350                         break;
1351
1352                 case Q931_REJ_IE_MISSING:
1353                         proto_tree_add_text(tree, tvb, offset, 1,
1354                             "Missing information element: %s",
1355                             val_to_str(tvb_get_guint8(tvb, offset), ie_vals,
1356                               "Unknown (0x%02X)"));
1357                         break;
1358
1359                 case Q931_REJ_IE_INSUFFICIENT:
1360                         proto_tree_add_text(tree, tvb, offset, 1,
1361                             "Insufficient information element: %s",
1362                             val_to_str(tvb_get_guint8(tvb, offset), ie_vals,
1363                               "Unknown (0x%02X)"));
1364                         break;
1365
1366                 default:
1367                         proto_tree_add_text(tree, tvb, offset, len,
1368                             "Diagnostic: %s",
1369                             tvb_bytes_to_str(tvb, offset, len));
1370                         break;
1371                 }
1372                 break;
1373
1374         case Q931_CAUSE_ACCESS_INFO_DISC:
1375         case Q931_CAUSE_INCOMPATIBLE_DEST:
1376         case Q931_CAUSE_MAND_IE_MISSING:
1377         case Q931_CAUSE_IE_NONEX_OR_UNIMPL:
1378         case Q931_CAUSE_INVALID_IE_CONTENTS:
1379                 do {
1380                         proto_tree_add_text(tree, tvb, offset, 1,
1381                             "Information element: %s",
1382                             val_to_str(tvb_get_guint8(tvb, offset), ie_vals,
1383                               "Unknown (0x%02X)"));
1384                         offset += 1;
1385                         len -= 1;
1386                 } while (len != 0);
1387                 break;
1388
1389         case Q931_CAUSE_MT_NONEX_OR_UNIMPL:
1390         case Q931_CAUSE_MSG_INCOMPAT_W_CS:
1391                 proto_tree_add_text(tree, tvb, offset, 1,
1392                     "Message type: %s",
1393                     val_to_str_ext(tvb_get_guint8(tvb, offset), &q931_message_type_vals_ext,
1394                       "Unknown (0x%02X)"));
1395                 break;
1396
1397         case Q931_CAUSE_REC_TIMER_EXP:
1398                 if (len < 3)
1399                         return;
1400                 proto_tree_add_text(tree, tvb, offset, 3,
1401                     "Timer: %.3s", tvb_get_ptr(tvb, offset, 3));
1402                 break;
1403
1404         default:
1405                 proto_tree_add_text(tree, tvb, offset, len,
1406                     "Diagnostics: %s",
1407                     tvb_bytes_to_str(tvb, offset, len));
1408         }
1409 }
1410
1411 void
1412 dissect_q931_cause_ie(tvbuff_t *tvb, int offset, int len,
1413     proto_tree *tree, int hf_cause_value, guint8 *cause_value, const value_string *ie_vals)
1414 {
1415   gboolean have_valid_q931_pi_save = have_valid_q931_pi;
1416   have_valid_q931_pi = FALSE;
1417   dissect_q931_cause_ie_unsafe(tvb, offset, len, tree, hf_cause_value, cause_value, ie_vals);
1418   have_valid_q931_pi =  have_valid_q931_pi_save;
1419 }
1420
1421 /*
1422  * Dissect a Change status information element.
1423  */
1424
1425 static const value_string q931_status_preference_vals[] = {
1426         { 0x01, "Channel" },
1427         { 0,    NULL }
1428 };
1429
1430 static const value_string q931_new_status_vals[] = {
1431         { 0x00, "In Service" },
1432         { 0x01, "Maintenance" },
1433         { 0x02, "Out of Service" },
1434         { 0,    NULL }
1435 };
1436
1437 static void
1438 dissect_q931_change_status_ie(tvbuff_t *tvb, int offset, int len _U_, proto_tree *tree)
1439 {
1440         guint8 octet;
1441
1442         if (len == 0)
1443                 return;
1444         octet = tvb_get_guint8(tvb, offset);
1445
1446         proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
1447         proto_tree_add_text(tree, tvb, offset, 1, "%s",
1448                 decode_enumerated_bitfield_shifted(octet, 0x40, 8, VALS(q931_status_preference_vals), "Preference: %s"));
1449         proto_tree_add_text(tree, tvb, offset, 1, "%s",
1450                 decode_enumerated_bitfield(octet, 0x07, 8, VALS(q931_new_status_vals), "New status: %s"));
1451 }
1452
1453 /*
1454  * Dissect a Call state information element.
1455  */
1456 static const value_string q931_call_state_vals[] = {
1457         { 0x00, "Null" },
1458         { 0x01, "Call initiated" },
1459         { 0x02, "Overlap sending" },
1460         { 0x03, "Outgoing call proceeding" },
1461         { 0x04, "Call delivered" },
1462         { 0x06, "Call present" },
1463         { 0x07, "Call received" },
1464         { 0x08, "Connect request" },
1465         { 0x09, "Incoming call proceeding" },
1466         { 0x0A, "Active" },
1467         { 0x0B, "Disconnect request" },
1468         { 0x0C, "Disconnect indication" },
1469         { 0x0F, "Suspend request" },
1470         { 0x11, "Resume request" },
1471         { 0x13, "Release request" },
1472         { 0x16, "Call abort"},
1473         { 0x19, "Overlap receiving" },
1474         { 0x3D, "Restart request" },
1475         { 0x3E, "Restart" },
1476         { 0,    NULL }
1477 };
1478 value_string_ext q931_call_state_vals_ext = VALUE_STRING_EXT_INIT(q931_call_state_vals);
1479
1480 static void
1481 dissect_q931_call_state_ie(tvbuff_t *tvb, int offset, int len,
1482     proto_tree *tree)
1483 {
1484         guint8 octet;
1485         guint8 coding_standard;
1486
1487         if (len == 0)
1488                 return;
1489         octet = tvb_get_guint8(tvb, offset);
1490         coding_standard = octet & 0x60;
1491         proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
1492         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1493                 /*
1494                  * We don't know how the call state is encoded,
1495                  * so just dump it as data and be done with it.
1496                  */
1497                 proto_tree_add_text(tree, tvb, offset,
1498                     len, "Data: %s",
1499                     tvb_bytes_to_str(tvb, offset, len));
1500                 return;
1501         }
1502         proto_tree_add_text(tree, tvb, offset, 1,
1503             "Call state: %s",
1504             val_to_str_ext(octet & 0x3F, &q931_call_state_vals_ext,
1505               "Unknown (0x%02X)"));
1506 }
1507
1508 /*
1509  * Dissect a Channel identification information element.
1510  */
1511 #define Q931_INTERFACE_IDENTIFIED       0x40
1512 #define Q931_NOT_BASIC_CHANNEL          0x20
1513
1514 static const value_string q931_basic_channel_selection_vals[] = {
1515         { 0x00, "No channel" },
1516         { 0x01, "B1 channel" },
1517         { 0x02, "B2 channel" },
1518         { 0x03, "Any channel" },
1519         { 0,    NULL }
1520 };
1521
1522 static const value_string q931_not_basic_channel_selection_vals[] = {
1523         { 0x00, "No channel" },
1524         { 0x01, "Channel indicated in following octets" },
1525         { 0x03, "Any channel" },
1526         { 0,    NULL }
1527 };
1528
1529 #define Q931_IS_SLOT_MAP                0x10
1530
1531 static const value_string q931_element_type_vals[] = {
1532         { 0x03, "B-channel units" },
1533         { 0x06, "H0-channel units" },
1534         { 0x08, "H11-channel units" },
1535         { 0x09, "H12-channel units" },
1536         { 0,    NULL }
1537 };
1538
1539 static void
1540 dissect_q931_channel_identification_ie(tvbuff_t *tvb, int offset, int len,
1541     proto_tree *tree)
1542 {
1543         guint8 octet;
1544         guint8 coding_standard;
1545
1546         if (len == 0)
1547                 return;
1548         octet = tvb_get_guint8(tvb, offset);
1549
1550         proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
1551         proto_tree_add_item(tree, hf_q931_channel_interface_explicit, tvb, offset, 1, FALSE);
1552         proto_tree_add_item(tree, hf_q931_channel_interface_type, tvb, offset, 1, FALSE);
1553         proto_tree_add_item(tree, hf_q931_channel_exclusive, tvb, offset, 1, FALSE);
1554         proto_tree_add_item(tree, hf_q931_channel_dchan, tvb, offset, 1, FALSE);
1555
1556         if (octet & Q931_NOT_BASIC_CHANNEL) {
1557                 proto_tree_add_item(tree, hf_q931_channel_selection_pri, tvb, offset, 1, FALSE);
1558         } else {
1559                 proto_tree_add_item(tree, hf_q931_channel_selection_bri, tvb, offset, 1, FALSE);
1560         }
1561         offset += 1;
1562         len -= 1;
1563
1564         if (octet & Q931_INTERFACE_IDENTIFIED) {
1565                 guint8 octet2;
1566                 guint32 identifier_val = 0;
1567                 int identifier_offset = offset;
1568                 int identifier_len = 0;
1569                 do {
1570                         if (len == 0)
1571                                 break;
1572                         octet2 = tvb_get_guint8(tvb, offset);
1573                         offset += 1;
1574                         len -= 1;
1575                         identifier_len++;
1576                         identifier_val <<= 7;
1577                         identifier_val |= octet2 & 0x7F;
1578                 } while (!(octet2 & Q931_IE_VL_EXTENSION));
1579
1580                 if (identifier_len != 0) {
1581                         proto_tree_add_text(tree, tvb, identifier_offset,
1582                             identifier_len, "Interface ID: %u", identifier_val);
1583                 }
1584         }
1585
1586         if (octet & Q931_NOT_BASIC_CHANNEL) {
1587                 if (len == 0)
1588                         return;
1589                 octet = tvb_get_guint8(tvb, offset);
1590                 coding_standard = octet & 0x60;
1591                 proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
1592                 proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
1593                 if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1594                         /*
1595                          * We don't know how the channel identifier is
1596                          * encoded, so just dump it as data and be done
1597                          * with it.
1598                          */
1599                         proto_tree_add_text(tree, tvb, offset,
1600                             len, "Data: %s",
1601                             tvb_bytes_to_str(tvb, offset, len));
1602                         return;
1603                 }
1604                 proto_tree_add_item(tree, hf_q931_channel_map, tvb, offset, 1, FALSE);
1605                 proto_tree_add_item(tree, hf_q931_channel_element_type, tvb, offset, 1, FALSE);
1606
1607                 offset += 1;
1608                 len -= 1;
1609
1610                 if (octet & Q931_IS_SLOT_MAP) {
1611                         guint8 octet2;
1612                         while (len) {
1613                                 octet2 = tvb_get_guint8(tvb, offset);
1614                                 proto_tree_add_text(tree, tvb, offset, 1,
1615                                         "Slot map: 0x%02x", octet2);
1616                                 offset += 1;
1617                                 len -= 1;
1618                         }
1619                 } else {
1620                         guint8 octet2;
1621                         do {
1622                                 if (len == 0)
1623                                         break;
1624                                 octet2 = tvb_get_guint8(tvb, offset);
1625
1626                                 proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
1627                                 proto_tree_add_item(tree,hf_q931_channel_number,tvb,offset,1,FALSE);
1628
1629                                 offset += 1;
1630                                 len -= 1;
1631                         } while (!(octet2 & Q931_IE_VL_EXTENSION));
1632                 }
1633         }
1634 }
1635
1636 /*
1637  * Dissect a Progress indicator information element.
1638  */
1639 static const value_string q931_progress_description_vals[] = {
1640         { 0x01, "Call is not end-to-end ISDN - progress information available in-band" },
1641         { 0x02, "Destination address is non-ISDN" },
1642         { 0x03, "Origination address is non-ISDN" },
1643         { 0x04, "Call has returned to the ISDN" },
1644         { 0x05, "Interworking has occurred and has resulted in a telecommunications service change" },
1645         { 0x08, "In-band information or an appropriate pattern is now available" },
1646         { 0,    NULL }
1647 };
1648 value_string_ext q931_progress_description_vals_ext = VALUE_STRING_EXT_INIT(q931_progress_description_vals);
1649
1650 void
1651 dissect_q931_progress_indicator_ie(tvbuff_t *tvb, int offset, int len,
1652     proto_tree *tree)
1653 {
1654         guint8 octet;
1655         guint8 coding_standard;
1656
1657         if (len == 0)
1658                 return;
1659         octet = tvb_get_guint8(tvb, offset);
1660         coding_standard = octet & 0x60;
1661         proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
1662         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1663                 /*
1664                  * We don't know how the progress indicator is encoded,
1665                  * so just dump it as data and be done with it.
1666                  */
1667                 proto_tree_add_text(tree, tvb, offset,
1668                     len, "Data: %s",
1669                     tvb_bytes_to_str(tvb, offset, len));
1670                 return;
1671         }
1672         proto_tree_add_text(tree, tvb, offset, 1,
1673             "Location: %s",
1674             val_to_str_ext(octet & 0x0F, &q931_cause_location_vals_ext,
1675               "Unknown (0x%X)"));
1676         offset += 1;
1677         len -= 1;
1678
1679         if (len == 0)
1680                 return;
1681         octet = tvb_get_guint8(tvb, offset);
1682         proto_tree_add_text(tree, tvb, offset, 1,
1683             "Progress description: %s",
1684             val_to_str_ext(octet & 0x7F, &q931_progress_description_vals_ext,
1685               "Unknown (0x%02X)"));
1686 }
1687
1688 /*
1689  * Dissect a Network-specific facilities or Transit network selection
1690  * information element.
1691  */
1692 static const value_string q931_netid_type_vals[] = {
1693         { 0x00, "User specified" },
1694         { 0x20, "National network identification" },
1695         { 0x30, "International network identification" },
1696         { 0,    NULL }
1697 };
1698
1699 static const value_string q931_netid_plan_vals[] = {
1700         { 0x00, "Unknown" },
1701         { 0x01, "Carrier Identification Code" },
1702         { 0x03, "X.121 data network identification code" },
1703         { 0,    NULL }
1704 };
1705
1706 static void
1707 dissect_q931_ns_facilities_ie(tvbuff_t *tvb, int offset, int len,
1708     proto_tree *tree)
1709 {
1710         guint8 octet;
1711         int netid_len;
1712
1713         if (len == 0)
1714                 return;
1715         octet = tvb_get_guint8(tvb, offset);
1716         netid_len = octet & 0x7F;
1717         proto_tree_add_text(tree, tvb, offset, 1,
1718             "Network identification length: %u",
1719             netid_len);
1720         offset += 1;
1721         len -= 1;
1722         if (netid_len != 0) {
1723                 if (len == 0)
1724                         return;
1725                 octet = tvb_get_guint8(tvb, offset);
1726                 proto_tree_add_text(tree, tvb, offset, 1,
1727                     "Type of network identification: %s",
1728                     val_to_str(octet & 0x70, q931_netid_type_vals,
1729                       "Unknown (0x%02X)"));
1730                 proto_tree_add_text(tree, tvb, offset, 1,
1731                     "Network identification plan: %s",
1732                     val_to_str(octet & 0x0F, q931_netid_plan_vals,
1733                       "Unknown (0x%02X)"));
1734                 offset += 1;
1735                 len -= 1;
1736                 netid_len--;
1737
1738                 if (len == 0)
1739                         return;
1740                 if (netid_len > len)
1741                         netid_len = len;
1742                 if (netid_len != 0) {
1743                         proto_tree_add_text(tree, tvb, offset, netid_len,
1744                             "Network identification: %s",
1745                             tvb_format_text(tvb, offset, netid_len));
1746                         offset += netid_len;
1747                         len -= netid_len;
1748                 }
1749         }
1750
1751         /*
1752          * Whatever is left is the network-specific facility
1753          * specification.
1754          */
1755          if (len == 0)
1756                 return;
1757         proto_tree_add_text(tree, tvb, offset,
1758             len, "Network-specific facility specification: %s",
1759             tvb_bytes_to_str(tvb, offset, len));
1760 }
1761
1762 /*
1763  * Dissect a Notification indicator information element.
1764  */
1765 static const value_string q931_notification_description_vals[] = {
1766         { 0x00, "User suspended" },
1767         { 0x01, "User resumed" },
1768         { 0x02, "Bearer service change" },
1769         { 0,    NULL }
1770 };
1771
1772 static void
1773 dissect_q931_notification_indicator_ie(tvbuff_t *tvb, int offset, int len,
1774     proto_tree *tree)
1775 {
1776         guint8 octet;
1777
1778         if (len == 0)
1779                 return;
1780         octet = tvb_get_guint8(tvb, offset);
1781         proto_tree_add_text(tree, tvb, offset, 1,
1782             "Notification description: %s",
1783             val_to_str(octet & 0x7F, q931_notification_description_vals,
1784               "Unknown (0x%02X)"));
1785 }
1786
1787 /*
1788  * Dissect a Date/time information element.
1789  */
1790 static void
1791 dissect_q931_date_time_ie(tvbuff_t *tvb, int offset, int len,
1792     proto_tree *tree)
1793 {
1794         if (len == 6) {
1795                 /*
1796                  * XXX - what is "year" relative to?  Is "month" 0-origin or
1797                  * 1-origin?  Q.931 doesn't say....
1798                  */
1799                 proto_tree_add_text(tree, tvb, offset, 6,
1800                     "Date/time: %02u-%02u-%02u %02u:%02u:%02u",
1801                     tvb_get_guint8(tvb, offset + 0), tvb_get_guint8(tvb, offset + 1), tvb_get_guint8(tvb, offset + 2),
1802                     tvb_get_guint8(tvb, offset + 3), tvb_get_guint8(tvb, offset + 4), tvb_get_guint8(tvb, offset + 5));
1803         } else if (len == 5) {
1804                 proto_tree_add_text(tree, tvb, offset, 5,
1805                     "Date/time: %02u-%02u-%02u %02u:%02u:00",
1806                     tvb_get_guint8(tvb, offset + 0), tvb_get_guint8(tvb, offset + 1), tvb_get_guint8(tvb, offset + 2),
1807                     tvb_get_guint8(tvb, offset + 3), tvb_get_guint8(tvb, offset + 4));
1808         } else {
1809                 proto_tree_add_text(tree, tvb, offset, len,
1810                     "Date/time: length is %d, should be 5 or 6", len);
1811         }
1812 }
1813
1814 /*
1815  * Dissect a Signal information element.
1816  */
1817 static const value_string q931_signal_vals[] = {
1818         { 0x00, "Dial tone on" },
1819         { 0x01, "Ring tone on" },
1820         { 0x02, "Intercept tone on" },
1821         { 0x03, "Network congestion tone on" }, /* "fast busy" */
1822         { 0x04, "Busy tone on" },
1823         { 0x05, "Confirm tone on" },
1824         { 0x06, "Answer tone on" },
1825         { 0x07, "Call waiting tone on" },
1826         { 0x08, "Off-hook warning tone on" },
1827         { 0x09, "Preemption tone on" },
1828         { 0x3F, "Tones off" },
1829         { 0x40, "Alerting on - pattern 0" },
1830         { 0x41, "Alerting on - pattern 1" },
1831         { 0x42, "Alerting on - pattern 2" },
1832         { 0x43, "Alerting on - pattern 3" },
1833         { 0x44, "Alerting on - pattern 4" },
1834         { 0x45, "Alerting on - pattern 5" },
1835         { 0x46, "Alerting on - pattern 6" },
1836         { 0x47, "Alerting on - pattern 7" },
1837         { 0x4F, "Alerting off" },
1838         { 0,    NULL }
1839 };
1840 static value_string_ext q931_signal_vals_ext = VALUE_STRING_EXT_INIT(q931_signal_vals);
1841
1842 static void
1843 dissect_q931_signal_ie(tvbuff_t *tvb, int offset, int len,
1844     proto_tree *tree)
1845 {
1846         if (len != 1) {
1847                 proto_tree_add_text(tree, tvb, offset, len,
1848                     "Signal: length is %d, should be 1", len);
1849                 return;
1850         }
1851         proto_tree_add_text(tree, tvb, offset, 1,
1852             "Signal: %s",
1853             val_to_str_ext(tvb_get_guint8(tvb, offset), &q931_signal_vals_ext,
1854                 "Unknown (0x%02X)"));
1855 }
1856
1857 /*
1858  * Dissect an Information rate information element.
1859  */
1860 static const value_string q931_throughput_class_vals[] = {
1861         { 0x03, "75 bit/s" },
1862         { 0x04, "150 bit/s" },
1863         { 0x05, "300 bit/s" },
1864         { 0x06, "600 bit/s" },
1865         { 0x07, "1200 bit/s" },
1866         { 0x08, "2400 bit/s" },
1867         { 0x09, "4800 bit/s" },
1868         { 0x0A, "9600 bit/s" },
1869         { 0x0B, "19200 bit/s" },
1870         { 0x0C, "48000 bit/s" },
1871         { 0x0D, "64000 bit/s" },
1872         { 0,    NULL }
1873 };
1874 static value_string_ext q931_throughput_class_vals_ext = VALUE_STRING_EXT_INIT(q931_throughput_class_vals);
1875
1876 static void
1877 dissect_q931_information_rate_ie(tvbuff_t *tvb, int offset, int len,
1878     proto_tree *tree)
1879 {
1880         if (len != 4) {
1881                 proto_tree_add_text(tree, tvb, offset, len,
1882                     "Information rate: length is %d, should be 4", len);
1883                 return;
1884         }
1885         proto_tree_add_text(tree, tvb, offset + 0, 1,
1886             "Incoming information rate: %s",
1887             val_to_str_ext(tvb_get_guint8(tvb, offset + 0) & 0x1F,
1888               &q931_throughput_class_vals_ext, "Unknown (0x%02X)"));
1889         proto_tree_add_text(tree, tvb, offset + 1, 1,
1890             "Outgoing information rate: %s",
1891             val_to_str_ext(tvb_get_guint8(tvb, offset + 1) & 0x1F,
1892               &q931_throughput_class_vals_ext, "Unknown (0x%02X)"));
1893         proto_tree_add_text(tree, tvb, offset + 2, 1,
1894             "Minimum incoming information rate: %s",
1895             val_to_str_ext(tvb_get_guint8(tvb, offset + 2) & 0x1F,
1896               &q931_throughput_class_vals_ext, "Unknown (0x%02X)"));
1897         proto_tree_add_text(tree, tvb, offset + 3, 1,
1898             "Minimum outgoing information rate: %s",
1899             val_to_str_ext(tvb_get_guint8(tvb, offset + 3) & 0x1F,
1900               &q931_throughput_class_vals_ext, "Unknown (0x%02X)"));
1901 }
1902
1903 static int
1904 dissect_q931_guint16_value(tvbuff_t *tvb, int offset, int len,
1905     proto_tree *tree, const char *label)
1906 {
1907         guint8 octet;
1908         guint16 value;
1909         int value_len;
1910
1911         value_len = 0;
1912
1913         octet = tvb_get_guint8(tvb, offset);
1914         if (octet & Q931_IE_VL_EXTENSION) {
1915                 /*
1916                  * Only one octet long - error.
1917                  */
1918                 goto bad_length;
1919         }
1920         value = (octet & 0x3) << 14;
1921         offset += 1;
1922         len -= 1;
1923         value_len++;
1924
1925         if (len == 0) {
1926                 /*
1927                  * We've reached the end of the information element - error.
1928                  */
1929                 goto past_end;
1930         }
1931         octet = tvb_get_guint8(tvb, offset);
1932         if (octet & Q931_IE_VL_EXTENSION) {
1933                 /*
1934                  * Only two octets long - error.
1935                  */
1936                 goto bad_length;
1937         }
1938         value |= (octet & 0x7F) << 7;
1939         offset += 1;
1940         len -= 1;
1941         value_len++;
1942
1943         if (len == 0) {
1944                 /*
1945                  * We've reached the end of the information element - error.
1946                  */
1947                 goto past_end;
1948         }
1949         octet = tvb_get_guint8(tvb, offset);
1950         if (!(octet & Q931_IE_VL_EXTENSION)) {
1951                 /*
1952                  * More than three octets long - error.
1953                  */
1954                 goto bad_length;
1955         }
1956         value |= (octet & 0x7F);
1957         offset += 1;
1958         len -= 1;
1959         value_len++;
1960
1961         proto_tree_add_text(tree, tvb, offset, value_len, "%s: %u ms", label,
1962             value);
1963         return value_len;
1964
1965 past_end:
1966         proto_tree_add_text(tree, tvb, offset, len,
1967             "%s goes past end of information element", label);
1968         return -1;
1969
1970 bad_length:
1971         proto_tree_add_text(tree, tvb, offset, len, "%s isn't 3 octets long",
1972             label);
1973         return -1;
1974 }
1975
1976 /*
1977  * Dissect an End-to-end transit delay information element.
1978  */
1979 static void
1980 dissect_q931_e2e_transit_delay_ie(tvbuff_t *tvb, int offset, int len,
1981     proto_tree *tree)
1982 {
1983         int value_len;
1984
1985         if (len == 0)
1986                 return;
1987         value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
1988             "Cumulative transit delay");
1989         if (value_len < 0)
1990                 return; /* error */
1991         offset += value_len;
1992         len -= value_len;
1993
1994         if (len == 0)
1995                 return;
1996         value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
1997             "Requested end-to-end transit delay");
1998         if (value_len < 0)
1999                 return; /* error */
2000         offset += value_len;
2001         len -= value_len;
2002
2003         if (len == 0)
2004                 return;
2005         value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
2006             "Maximum end-to-end transit delay");
2007 }
2008
2009 /*
2010  * Dissect a Transit delay selection and indication information element.
2011  */
2012 static void
2013 dissect_q931_td_selection_and_int_ie(tvbuff_t *tvb, int offset, int len,
2014     proto_tree *tree)
2015 {
2016         if (len == 0)
2017                 return;
2018         dissect_q931_guint16_value(tvb, offset, len, tree,
2019             "Transit delay");
2020 }
2021
2022 /*
2023  * Dissect a Packet layer binary parameters information element.
2024  */
2025 static const value_string q931_fast_selected_vals[] = {
2026         { 0x00, "Fast select not requested" },
2027         { 0x08, "Fast select not requested" },
2028         { 0x10, "Fast select requested with no restriction of response" },
2029         { 0x18, "Fast select requested with restrictions of response" },
2030         { 0x00, NULL }
2031 };
2032
2033 static void
2034 dissect_q931_pl_binary_parameters_ie(tvbuff_t *tvb, int offset, int len,
2035     proto_tree *tree)
2036 {
2037         guint8 octet;
2038
2039         if (len == 0)
2040                 return;
2041         octet = tvb_get_guint8(tvb, offset);
2042         proto_tree_add_text(tree, tvb, offset, 1,
2043             "Fast select: %s",
2044             val_to_str(octet & 0x18, q931_fast_selected_vals, "Unknown (0x%02X)"));
2045         proto_tree_add_text(tree, tvb, offset, 1,
2046             "%s",
2047             (octet & 0x04) ? "No request/request denied" :
2048                              "Request indicated/request accepted");
2049         proto_tree_add_text(tree, tvb, offset, 1,
2050             "%s confirmation",
2051             (octet & 0x02) ? "Link-by-link" : "End-to-end");
2052         proto_tree_add_text(tree, tvb, offset, 1,
2053             "Modulus %u sequencing",
2054             (octet & 0x01) ? 8 : 128);
2055 }
2056
2057 /*
2058  * Dissect a Packet layer window size information element.
2059  */
2060 static void
2061 dissect_q931_pl_window_size_ie(tvbuff_t *tvb, int offset, int len,
2062     proto_tree *tree)
2063 {
2064         if (len == 0)
2065                 return;
2066         proto_tree_add_text(tree, tvb, offset, 1,
2067             "Forward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
2068         offset += 1;
2069         len -= 1;
2070
2071         if (len == 0)
2072                 return;
2073         proto_tree_add_text(tree, tvb, offset, 1,
2074             "Backward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
2075 }
2076
2077 /*
2078  * Dissect a Packet size information element.
2079  */
2080 static void
2081 dissect_q931_packet_size_ie(tvbuff_t *tvb, int offset, int len,
2082     proto_tree *tree)
2083 {
2084         if (len == 0)
2085                 return;
2086         proto_tree_add_text(tree, tvb, offset, 1,
2087             "Forward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
2088         offset += 1;
2089         len -= 1;
2090
2091         if (len == 0)
2092                 return;
2093         proto_tree_add_text(tree, tvb, offset, 1,
2094             "Backward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
2095 }
2096
2097 /*
2098  * Dissect a Closed user group information element.
2099  */
2100 static const value_string q931_cug_indication_vals[] = {
2101         { 0x01, "Closed user group selection" },
2102         { 0x02, "Closed user group with outgoing access selection and indication" },
2103         { 0,    NULL }
2104 };
2105
2106 static void
2107 dissect_q931_cug_ie(tvbuff_t *tvb, int offset, int len, proto_tree *tree)
2108 {
2109         if (len == 0)
2110                 return;
2111         proto_tree_add_text(tree, tvb, offset, 1,
2112             "CUG indication: %s",
2113             val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
2114               q931_cug_indication_vals, "Unknown (0x%02X)"));
2115         offset += 1;
2116         len -= 1;
2117
2118         if (len == 0)
2119                 return;
2120         proto_tree_add_text(tree, tvb, offset, len, "CUG index code: %s",
2121             tvb_format_text(tvb, offset, len));
2122 }
2123
2124 /*
2125  * Dissect a Reverse charging indication information element.
2126  */
2127 static const value_string q931_reverse_charging_indication_vals[] = {
2128         { 0x01, "Reverse charging requested" },
2129         { 0,    NULL }
2130 };
2131
2132 static void
2133 dissect_q931_reverse_charge_ind_ie(tvbuff_t *tvb, int offset, int len,
2134     proto_tree *tree)
2135 {
2136         if (len == 0)
2137                 return;
2138         proto_tree_add_text(tree, tvb, offset, 1,
2139             "Reverse charging indication: %s",
2140             val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
2141               q931_reverse_charging_indication_vals, "Unknown (0x%02X)"));
2142 }
2143
2144 /*
2145  * Dissect a (phone) number information element.
2146  */
2147 static const value_string q931_number_type_vals[] = {
2148         { 0x0, "Unknown" },
2149         { 0x1, "International number" },
2150         { 0x2, "National number" },
2151         { 0x3, "Network specific number" },
2152         { 0x4, "Subscriber number" },
2153         { 0x6, "Abbreviated number" },
2154         { 0,    NULL }
2155 };
2156
2157 static const value_string q931_numbering_plan_vals[] = {
2158         { 0x00, "Unknown" },
2159         { 0x01, "E.164 ISDN/telephony numbering" },
2160         { 0x03, "X.121 data numbering" },
2161         { 0x04, "F.69 Telex numbering" },
2162         { 0x08, "National standard numbering" },
2163         { 0x09, "Private numbering" },
2164         { 0,    NULL }
2165 };
2166
2167 static const value_string q931_presentation_indicator_vals[] = {
2168         { 0x00, "Presentation allowed" },
2169         { 0x01, "Presentation restricted" },
2170         { 0x02, "Number not available due to interworking" },
2171         { 0,    NULL }
2172 };
2173
2174 static const value_string q931_screening_indicator_vals[] = {
2175         { 0x00, "User-provided, not screened" },
2176         { 0x01, "User-provided, verified and passed" },
2177         { 0x02, "User-provided, verified and failed" },
2178         { 0x03, "Network-provided" },
2179         { 0,    NULL }
2180 };
2181
2182 static const value_string q931_redirection_reason_vals[] = {
2183         { 0x00, "Unknown" },
2184         { 0x01, "Call forwarding busy or called DTE busy" },
2185         { 0x02, "Call forwarding no reply" },
2186         { 0x04, "Call deflection" },
2187         { 0x09, "Called DTE out of order" },
2188         { 0x0A, "Call forwarding by the called DTE" },
2189         { 0x0F, "Call forwarding unconditional or systematic call redirection" },
2190         { 0,    NULL }
2191 };
2192
2193 static void
2194 dissect_q931_number_ie(tvbuff_t *tvb, int offset, int len,
2195     proto_tree *tree, int hfindex, e164_info_t e164_info)
2196 {
2197         guint8 octet;
2198         gint number_plan;
2199
2200         if (len == 0)
2201                 return;
2202         octet = tvb_get_guint8(tvb, offset);
2203         number_plan = octet & 0x0f;
2204         e164_info.nature_of_address = ( octet & 0x70 ) >> 4;
2205         proto_tree_add_uint(tree, hf_q931_numbering_plan, tvb, offset, 1, octet);
2206         proto_tree_add_uint(tree, hf_q931_number_type, tvb, offset, 1, octet);
2207         proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
2208
2209         offset += 1;
2210         len -= 1;
2211
2212         if (!(octet & Q931_IE_VL_EXTENSION)) {
2213                 if (len == 0)
2214                         return;
2215                 octet = tvb_get_guint8(tvb, offset);
2216                 proto_tree_add_uint(tree, hf_q931_screening_ind, tvb, offset, 1, octet);
2217                 proto_tree_add_uint(tree, hf_q931_presentation_ind, tvb, offset, 1, octet);
2218                 proto_tree_add_boolean(tree, hf_q931_extension_ind, tvb, offset, 1, octet);
2219                 offset += 1;
2220                 len -= 1;
2221         }
2222
2223         /*
2224          * XXX - only in a Redirecting number information element.
2225          */
2226         if (!(octet & Q931_IE_VL_EXTENSION)) {
2227                 if (len == 0)
2228                         return;
2229                 octet = tvb_get_guint8(tvb, offset);
2230                 proto_tree_add_text(tree, tvb, offset, 1,
2231                     "Reason for redirection: %s",
2232                     val_to_str(octet & 0x0F, q931_redirection_reason_vals,
2233                       "Unknown (0x%X)"));
2234                 offset += 1;
2235                 len -= 1;
2236         }
2237
2238         if (len == 0)
2239                 return;
2240         proto_tree_add_item(tree, hfindex, tvb, offset, len, FALSE);
2241         proto_item_append_text(proto_tree_get_parent(tree), ": '%s'", tvb_format_text(tvb, offset, len));
2242
2243         if ( number_plan == 1 ) {
2244                 if ( e164_info.e164_number_type != NONE ){
2245
2246                         e164_info.E164_number_str = tvb_get_ephemeral_string(tvb, offset, len);
2247                         e164_info.E164_number_length = len;
2248                         dissect_e164_number(tvb, tree, offset, len, e164_info);
2249                 }
2250         }
2251
2252     /* Collect q931_packet_info */
2253     if ( e164_info.e164_number_type == CALLING_PARTY_NUMBER && have_valid_q931_pi)
2254           q931_pi->calling_number = tvb_get_ephemeral_string(tvb, offset, len);
2255     if ( e164_info.e164_number_type == CALLED_PARTY_NUMBER && have_valid_q931_pi)
2256           q931_pi->called_number = tvb_get_ephemeral_string(tvb, offset, len);
2257 }
2258
2259 /*
2260  * Dissect a party subaddress information element.
2261  */
2262 static const value_string q931_subaddress_type_vals[] = {
2263         { 0x00, "X.213/ISO 8348 Add.2 NSAP" },
2264         { 0x20, "User-specified" },
2265         { 0,    NULL }
2266 };
2267
2268 static const value_string q931_odd_even_indicator_vals[] = {
2269         { 0x00, "Even number of address signals" },
2270         { 0x08, "Odd number of address signals" },
2271         { 0,    NULL }
2272 };
2273
2274 static void
2275 dissect_q931_party_subaddr_ie(tvbuff_t *tvb, int offset, int len,
2276     proto_tree *tree)
2277 {
2278         guint8 octet;
2279
2280         if (len == 0)
2281                 return;
2282         octet = tvb_get_guint8(tvb, offset);
2283         proto_tree_add_text(tree, tvb, offset, 1,
2284             "Type of subaddress: %s",
2285             val_to_str(octet & 0x70, q931_subaddress_type_vals,
2286               "Unknown (0x%02X)"));
2287         proto_tree_add_text(tree, tvb, offset, 1,
2288             "Odd/even indicator: %s",
2289             val_to_str(octet & 0x08, q931_odd_even_indicator_vals,
2290               "Unknown (0x%02X)"));
2291         offset += 1;
2292         len -= 1;
2293
2294         if (len == 0)
2295                 return;
2296         proto_tree_add_text(tree, tvb, offset, len, "Subaddress: %s",
2297             tvb_bytes_to_str(tvb, offset, len));
2298 }
2299
2300 /*
2301  * Dissect a Restart indicator information element.
2302  */
2303 static const value_string q931_restart_indicator_class_vals[] = {
2304         { 0x00, "Indicated channels" },
2305         { 0x06, "Single interface" },
2306         { 0x07, "All interfaces" },
2307         { 0,    NULL }
2308 };
2309
2310 static void
2311 dissect_q931_restart_indicator_ie(tvbuff_t *tvb, int offset, int len,
2312     proto_tree *tree)
2313 {
2314         if (len != 1) {
2315                 proto_tree_add_text(tree, tvb, offset, len,
2316                     "Restart indicator: length is %d, should be 1", len);
2317                 return;
2318         }
2319         proto_tree_add_text(tree, tvb, offset, 1,
2320             "Restart indicator: %s",
2321             val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
2322               q931_restart_indicator_class_vals, "Unknown (0x%02X)"));
2323 }
2324
2325 /*
2326  * Dissect a High-layer compatibility information element.
2327  */
2328 #define Q931_MAINTENANCE        0x5e
2329 #define Q931_MANAGEMENT         0x5f
2330 #define Q931_AUDIOVISUAL        0x60
2331 static const value_string q931_high_layer_characteristics_vals[] = {
2332         { 0x01,             "Telephony" },
2333         { 0x04,             "F.182 Facsimile Group 2/3" },
2334         { 0x21,             "F.184 Facsimile Group 4 Class I" },
2335         { 0x24,             "F.230 Teletex, basic and mixed mode, and F.184 Facsimile Group 4, Classes II and III" },
2336         { 0x28,             "F.220 Teletex, basic and processable mode" },
2337         { 0x31,             "F.200 Teletex, basic mode" },
2338         { 0x32,             "F.300 and T.102 syntax-based Videotex" },
2339         { 0x33,             "F.300 and T.101 international Videotex interworking" },
2340         { 0x35,             "F.60 Telex" },
2341         { 0x38,             "X.400 Message Handling Systems" },
2342         { 0x41,             "X.200 OSI application" },
2343         { 0x42,             "FTAM application" },
2344         { 0x5E,             "Reserved for maintenance" },
2345         { 0x5F,             "Reserved for management" },
2346         { Q931_AUDIOVISUAL, "F.720/F.821 and F.731 Profile 1a videotelephony" },
2347         { 0x61,             "F.702 and F.731 Profile 1b videoconferencing" },
2348         { 0x62,             "F.702 and F.731 audiographic conferencing" },
2349         { 0x68,             "F.700-series Multimedia services" },
2350         { 0,                NULL }
2351 };
2352 static value_string_ext q931_high_layer_characteristics_vals_ext = VALUE_STRING_EXT_INIT(q931_high_layer_characteristics_vals);
2353
2354 static const value_string q931_extended_high_layer_characteristics_vals[] = {
2355         { 0x01,             "Telephony" },
2356         { 0x04,             "F.182 Facsimile Group 2/3" },
2357         { 0x21,             "F.184 Facsimile Group 4 Class I" },
2358         { 0x24,             "F.230 Teletex, basic and mixed mode, and F.184 Facsimile Group 4, Classes II and III" },
2359         { 0x28,             "F.220 Teletex, basic and processable mode" },
2360         { 0x31,             "F.200 Teletex, basic mode" },
2361         { 0x32,             "F.300 and T.102 syntax-based Videotex" },
2362         { 0x33,             "F.300 and T.101 international Videotex interworking" },
2363         { 0x35,             "F.60 Telex" },
2364         { 0x38,             "X.400 Message Handling Systems" },
2365         { 0x41,             "X.200 OSI application" },
2366         { 0x42,             "FTAM application" },
2367         { 0x5E,             "Not available for assignment" },
2368         { 0x5F,             "Not available for assignment" },
2369         { Q931_AUDIOVISUAL, "F.720/F.821 and F.731 Profile 1a videotelephony" },
2370         { 0x61,             "F.702 and F.731 Profile 1b videoconferencing" },
2371         { 0x62,             "F.702 and F.731 audiographic conferencing" },
2372         { 0x68,             "F.700-series Multimedia services" },
2373         { 0,                NULL }
2374 };
2375 static value_string_ext q931_extended_high_layer_characteristics_vals_ext = VALUE_STRING_EXT_INIT(q931_extended_high_layer_characteristics_vals);
2376
2377 static const value_string q931_audiovisual_characteristics_vals[] = {
2378         { 0x01, "Capability set of initial channel of H.221" },
2379         { 0x02, "Capability set of subsequent channel of H.221" },
2380         { 0x21, "Capability set of initial channel of an active 3.1kHz audio or speech call" },
2381         { 0x00, NULL }
2382 };
2383
2384 static const value_string q931_interpretation_vals[] = {
2385         { 0x04, "First (primary or only) high layer characteristics identification to be used in the call" },
2386         { 0x00, NULL }
2387 };
2388
2389 static const value_string q931_pres_meth_prot_prof_vals[] = {
2390         { 0x01, "High layer protocol profile (without specification of attributes)" },
2391         { 0x00, NULL }
2392 };
2393
2394 /*
2395  * High layer protocol profile
2396  */
2397 #define Q931_HIGH_LAYER_PROTOCOL_PROFILE 0x01
2398
2399 void
2400 dissect_q931_high_layer_compat_ie(tvbuff_t *tvb, int offset, int len,
2401     proto_tree *tree)
2402 {
2403         guint8 octet;
2404         guint8 coding_standard;
2405         guint8 pres_method;
2406         guint8 characteristics;
2407
2408         if (len == 0)
2409                 return;
2410         octet = tvb_get_guint8(tvb, offset);
2411         coding_standard = octet & 0x60;
2412         pres_method = octet & 0x03;
2413
2414         proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
2415         proto_tree_add_uint(tree, hf_q931_coding_standard, tvb, offset, 1, octet);
2416         proto_tree_add_uint(tree, hf_q931_interpretation, tvb, offset, 1, octet);
2417         proto_tree_add_uint(tree, hf_q931_pres_meth_prot_prof, tvb, offset, 1, octet);
2418
2419         offset += 1;
2420         len -= 1;
2421         if ((coding_standard != Q931_ITU_STANDARDIZED_CODING) || (pres_method != Q931_HIGH_LAYER_PROTOCOL_PROFILE)) {
2422                 /*
2423                  * We don't know how the call state is encoded,
2424                  * so just dump it as data and be done with it.
2425                  */
2426                 proto_tree_add_text(tree, tvb, offset,
2427                     len, "Data: %s",
2428                     tvb_bytes_to_str(tvb, offset, len));
2429                 return;
2430         }
2431         if (len == 0)
2432                 return;
2433
2434         octet = tvb_get_guint8(tvb, offset);
2435         characteristics = octet & 0x7F;
2436
2437         proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
2438         proto_tree_add_uint(tree, hf_q931_high_layer_characteristics, tvb, offset, 1, octet);
2439
2440         offset += 1;
2441         len -= 1;
2442
2443         if (!(octet & Q931_IE_VL_EXTENSION)) {
2444                 if (len == 0)
2445                         return;
2446                 octet = tvb_get_guint8(tvb, offset);
2447                 if ((characteristics == Q931_AUDIOVISUAL) || (characteristics == 0x61) || (characteristics == 0x62) ||
2448                         (characteristics == 0x68)) {
2449                         proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
2450                         proto_tree_add_uint(tree, hf_q931_extended_audiovisual_characteristics, tvb, offset, 1, octet);
2451                 }
2452                 else if ((characteristics == Q931_MANAGEMENT) || (characteristics == Q931_MAINTENANCE)) {
2453                         proto_tree_add_item(tree, hf_q931_extension_ind, tvb, offset, 1, FALSE);
2454                         proto_tree_add_uint(tree, hf_q931_extended_high_layer_characteristics, tvb, offset, 1, octet);
2455                 }
2456         }
2457 }
2458
2459
2460 /*
2461  * Dissect a User-user information element.
2462  */
2463 #define Q931_PROTOCOL_DISCRIMINATOR_USER        0x00
2464 #define Q931_PROTOCOL_DISCRIMINATOR_IA5         0x04
2465 #define Q931_PROTOCOL_DISCRIMINATOR_ASN1        0x05
2466
2467 static const value_string q931_protocol_discriminator_vals[] = {
2468         { Q931_PROTOCOL_DISCRIMINATOR_USER, "User-specific protocol" },
2469         { 0x01,                                 "OSI high layer protocols" },
2470         { 0x02,                                 "X.244" },
2471         { 0x03,                                 "Reserved for system management convergence function" },
2472         { Q931_PROTOCOL_DISCRIMINATOR_IA5,      "IA5 characters" },
2473         { Q931_PROTOCOL_DISCRIMINATOR_ASN1,     "X.208 and X.209 coded user information" },
2474         { 0x06,                                 "Undefined" },
2475         { 0x07,                                 "V.120 rate adaption" },
2476         { 0x08,                                 "Q.931/I.451 user-network call control messages" },
2477         { 0,                                    NULL }
2478 };
2479 value_string_ext q931_protocol_discriminator_vals_ext = VALUE_STRING_EXT_INIT(q931_protocol_discriminator_vals);
2480
2481 void
2482 dissect_q931_user_user_ie(tvbuff_t *tvb, packet_info *pinfo, int offset, int len,
2483     proto_tree *tree)
2484 {
2485         guint8 octet;
2486         tvbuff_t *next_tvb = NULL;
2487
2488         if (len == 0)
2489                 return;
2490         octet = tvb_get_guint8(tvb, offset);
2491         proto_tree_add_text(tree, tvb, offset, 1,
2492             "Protocol discriminator: %s",
2493             val_to_str_ext(octet, &q931_protocol_discriminator_vals_ext,
2494             "Unknown (0x%02x)"));
2495         offset += 1;
2496         len -= 1;
2497
2498         if (len == 0)
2499                 return;
2500         switch (octet) {
2501
2502         case Q931_PROTOCOL_DISCRIMINATOR_USER:
2503                 next_tvb = tvb_new_subset(tvb, offset, len, len);
2504                 proto_tree_add_text(tree, tvb, offset, len, "User information: %d octets", len);
2505                 if (!dissector_try_heuristic(q931_user_heur_subdissector_list, next_tvb, pinfo, tree)) {
2506                 call_dissector_only(data_handle, next_tvb, pinfo, tree);
2507                 }
2508                 break;
2509
2510         case Q931_PROTOCOL_DISCRIMINATOR_IA5:
2511                 proto_tree_add_text(tree, tvb, offset, len, "User information: %s",
2512                     tvb_format_text(tvb, offset, len));
2513                 break;
2514
2515         default:
2516                 proto_tree_add_text(tree, tvb, offset, len, "User information: %s",
2517                     tvb_bytes_to_str(tvb, offset, len));
2518                 break;
2519         }
2520 }
2521
2522 static const value_string q931_party_category_vals[] = {
2523         { 0x00, "Unknown" },
2524         { 0x01, "Extension" },
2525         { 0x02, "Operator" },
2526         { 0x03, "Emergency extension" },
2527         { 0,    NULL }
2528 };
2529
2530 static void
2531 dissect_q931_party_category_ie(tvbuff_t *tvb, int offset, int len,
2532     proto_tree *tree)
2533 {
2534         guint8 octet;
2535
2536         if (len == 0)
2537                 return;
2538
2539         octet = tvb_get_guint8(tvb, offset);
2540         proto_tree_add_text(tree, tvb, offset, 1,
2541             "Party category: %s",
2542             val_to_str(octet&0x07, q931_party_category_vals,
2543             "Unknown (0x%02x)"));
2544         offset += 1;
2545         len -= 1;
2546
2547         if (len == 0)
2548                 return;
2549 }
2550
2551 /*
2552  * Dissect information elements consisting of ASCII^H^H^H^H^HIA5 text.
2553  */
2554 static void
2555 dissect_q931_ia5_ie(tvbuff_t *tvb, int offset, int len, proto_tree *tree,
2556     const char *label)
2557 {
2558         if (len != 0) {
2559                 proto_tree_add_text(tree, tvb, offset, len, "%s: %s", label,
2560                     tvb_format_text(tvb, offset, len));
2561                 proto_item_append_text(proto_tree_get_parent(tree), "  '%s'", tvb_format_text(tvb, offset, len));
2562         }
2563 }
2564
2565 static void
2566 dissect_q931_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
2567     gboolean is_over_ip)
2568 {
2569         int             offset = 0;
2570         proto_tree      *q931_tree = NULL;
2571         proto_tree      *ie_tree = NULL;
2572         proto_item      *ti, *ti_ie;
2573         guint8          prot_discr;
2574         guint8          call_ref_len;
2575         guint8          call_ref[15];
2576         guint32         call_ref_val;
2577         guint8          message_type, segmented_message_type;
2578         guint8          info_element;
2579         guint16         info_element_len;
2580         gboolean        first_frag, more_frags;
2581         guint32         frag_len;
2582         fragment_data *fd_head;
2583         tvbuff_t *next_tvb = NULL;
2584
2585         q931_pi=ep_alloc(sizeof(q931_packet_info));
2586
2587         /* Init struct for collecting q931_packet_info */
2588         reset_q931_packet_info(q931_pi);
2589         have_valid_q931_pi=TRUE;
2590
2591         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Q.931");
2592
2593         prot_discr = tvb_get_guint8(tvb, offset);
2594         if (tree) {
2595                 ti = proto_tree_add_item(tree, proto_q931, tvb, offset, -1,
2596                     FALSE);
2597                 q931_tree = proto_item_add_subtree(ti, ett_q931);
2598
2599                 dissect_q931_protocol_discriminator(tvb, offset, q931_tree);
2600         }
2601         offset += 1;
2602         call_ref_len = tvb_get_guint8(tvb, offset) & 0xF;       /* XXX - do as a bit field? */
2603         if (q931_tree != NULL)
2604                 proto_tree_add_uint(q931_tree, hf_q931_call_ref_len, tvb, offset, 1, call_ref_len);
2605         offset += 1;
2606         switch (call_ref_len) {
2607                 case 0: call_ref_val = 0; break;
2608                 case 1: call_ref_val = tvb_get_guint8(tvb, offset);     break;
2609                 case 2: call_ref_val = tvb_get_ntohs(tvb, offset); break;
2610                 case 3: call_ref_val = tvb_get_ntoh24(tvb, offset); break;
2611                 default: call_ref_val = tvb_get_ntohl(tvb, offset);
2612         }
2613         if (call_ref_len != 0) {
2614                 tvb_memcpy(tvb, call_ref, offset, call_ref_len);
2615                 if (q931_tree != NULL) {
2616                         proto_tree_add_boolean(q931_tree, hf_q931_call_ref_flag,
2617                             tvb, offset, 1, (call_ref[0] & 0x80) != 0);
2618                         call_ref[0] &= 0x7F;
2619                         proto_tree_add_bytes(q931_tree, hf_q931_call_ref,
2620                             tvb, offset, call_ref_len, call_ref);
2621                 } else
2622                 {       /* info for the tap */
2623                         call_ref[0] &= 0x7F;
2624                 }
2625                 /* XXX - Should crv be something besides a guint32? */
2626                 memcpy(&(q931_pi->crv), call_ref, call_ref_len > sizeof(q931_pi->crv) ? sizeof(q931_pi->crv) : call_ref_len );
2627                 offset += call_ref_len;
2628         }
2629         message_type = tvb_get_guint8(tvb, offset);
2630         if(have_valid_q931_pi) {
2631                 q931_pi->message_type = message_type;
2632         }
2633         if (check_col(pinfo->cinfo, COL_INFO)) {
2634                 col_add_str(pinfo->cinfo, COL_INFO, get_message_name(prot_discr, message_type));
2635         }
2636         if (q931_tree != NULL){
2637                 if (prot_discr == NLPID_DMS)
2638                         proto_tree_add_item(q931_tree, hf_q931_maintenance_message_type, tvb, offset, 1, FALSE);
2639                 else
2640                         proto_tree_add_item(q931_tree, hf_q931_message_type, tvb, offset, 1, FALSE);
2641         }
2642         offset += 1;
2643
2644         /*
2645          * And now for the information elements....
2646          */
2647         if ((message_type != Q931_SEGMENT) || !q931_reassembly ||
2648                         (tvb_reported_length_remaining(tvb, offset) <= 4)) {
2649                 dissect_q931_IEs(tvb, pinfo, tree, q931_tree, is_over_ip, offset, 0);
2650                 return;
2651         }
2652         info_element = tvb_get_guint8(tvb, offset);
2653         info_element_len = tvb_get_guint8(tvb, offset + 1);
2654         if ((info_element != Q931_IE_SEGMENTED_MESSAGE) || (info_element_len < 2)) {
2655                 dissect_q931_IEs(tvb, pinfo, tree, q931_tree, is_over_ip, offset, 0);
2656                 return;
2657         }
2658         /* Segmented message IE */
2659         ti_ie = proto_tree_add_text(q931_tree, tvb, offset, 1+1+info_element_len, "%s",
2660                                     val_to_str(info_element, q931_info_element_vals[0], "Unknown information element (0x%02X)"));
2661         ie_tree = proto_item_add_subtree(ti_ie, ett_q931_ie);
2662         proto_tree_add_text(ie_tree, tvb, offset, 1, "Information element: %s",
2663                                     val_to_str(info_element, q931_info_element_vals[0], "Unknown (0x%02X)"));
2664         proto_tree_add_text(ie_tree, tvb, offset + 1, 1, "Length: %u", info_element_len);
2665         dissect_q931_segmented_message_ie(tvb, offset + 2, info_element_len, ie_tree);
2666         first_frag = (tvb_get_guint8(tvb, offset + 2) & 0x80) != 0;
2667         more_frags = (tvb_get_guint8(tvb, offset + 2) & 0x7F) != 0;
2668         segmented_message_type = tvb_get_guint8(tvb, offset + 3);
2669         if (check_col(pinfo->cinfo, COL_INFO)) {
2670                 col_append_fstr(pinfo->cinfo, COL_INFO, " of %s",
2671                     val_to_str_ext(segmented_message_type, &q931_message_type_vals_ext, "Unknown message type (0x%02X)"));
2672         }
2673         offset += 1 + 1 + info_element_len;
2674         /* Reassembly */
2675         frag_len = tvb_reported_length_remaining(tvb, offset);
2676         if (first_frag && fragment_get(pinfo, call_ref_val,     q931_fragment_table)) {
2677                 /* there are some unreassembled segments, ignore them */
2678                 fragment_end_seq_next(pinfo, call_ref_val, q931_fragment_table, q931_reassembled_table);
2679         }
2680         fd_head = fragment_add_seq_next(tvb, offset, pinfo, call_ref_val,
2681                                                                         q931_fragment_table, q931_reassembled_table,
2682                                                                         frag_len, more_frags);
2683         if (fd_head) {
2684                 if (pinfo->fd->num == fd_head->reassembled_in) {  /* last fragment */
2685                         if (fd_head->next != NULL) {  /* 2 or more segments */
2686                                 next_tvb = tvb_new_child_real_data(tvb, fd_head->data, fd_head->len, fd_head->len);
2687                                 add_new_data_source(pinfo, next_tvb, "Reassembled Q.931 IEs");
2688                                 /* Show all fragments. */
2689                 if (tree) {
2690                     proto_item *frag_tree_item;
2691                     show_fragment_seq_tree(fd_head, &q931_frag_items, q931_tree, pinfo, next_tvb, &frag_tree_item);
2692                 }
2693                         } else {  /* only 1 segment */
2694                                 next_tvb = tvb_new_subset_remaining(tvb, offset);
2695                         }
2696                         if (check_col(pinfo->cinfo, COL_INFO)) {
2697                                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s [reassembled]",
2698                                     val_to_str_ext(segmented_message_type, &q931_message_type_vals_ext, "Unknown message type (0x%02X)"));
2699                         }
2700                 } else {
2701                         if (tree) proto_tree_add_uint(q931_tree, hf_q931_reassembled_in, tvb, offset, frag_len, fd_head->reassembled_in);
2702                 }
2703         }
2704         if (next_tvb)
2705                 dissect_q931_IEs(next_tvb, pinfo, tree, q931_tree, is_over_ip, 0, 0);
2706 }
2707
2708 static const value_string q931_codeset_vals[] = {
2709         { 0x00, "Q.931 information elements" },
2710         { 0x04, "Information elements for ISO/IEC use" },
2711         { 0x05, "Information elements for national use" },
2712         { 0x06, "Information elements specific to the local network" },
2713         { 0x07, "User-specific information elements" },
2714         { 0x00, NULL },
2715 };
2716
2717 static void
2718 dissect_q931_IEs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *root_tree,
2719     proto_tree *q931_tree, gboolean is_over_ip, int offset, int initial_codeset)
2720 {
2721         proto_item      *ti;
2722         proto_tree      *ie_tree = NULL;
2723         guint8          info_element;
2724         guint8          dummy;
2725         guint16         info_element_len;
2726         int             codeset, locked_codeset;
2727         gboolean        non_locking_shift, first_segment;
2728         tvbuff_t        *h225_tvb, *next_tvb;
2729         e164_info_t e164_info;
2730         e164_info.e164_number_type = NONE;
2731
2732         codeset = locked_codeset = initial_codeset;
2733         non_locking_shift = TRUE;
2734         first_segment = FALSE;
2735         while (tvb_reported_length_remaining(tvb, offset) > 0) {
2736                 info_element = tvb_get_guint8(tvb, offset);
2737
2738                 /* Check for the codeset shift */
2739                 if ((info_element & Q931_IE_SO_MASK) &&
2740                     ((info_element & Q931_IE_SO_IDENTIFIER_MASK) == Q931_IE_SHIFT)) {
2741                         non_locking_shift = info_element & Q931_IE_SHIFT_NON_LOCKING;
2742                         codeset = info_element & Q931_IE_SHIFT_CODESET;
2743                         if (!non_locking_shift)
2744                                 locked_codeset = codeset;
2745                         if (q931_tree != NULL) {
2746                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2747                                     "%s shift to codeset %u: %s",
2748                                     (non_locking_shift ? "Non-locking" : "Locking"),
2749                                     codeset,
2750                                     val_to_str(codeset, q931_codeset_vals,
2751                                       "Unknown (0x%02X)"));
2752                         }
2753                         offset += 1;
2754                         continue;
2755                 }
2756
2757                 /*
2758                  * Check for the single-octet IEs.
2759                  */
2760                 if (info_element & Q931_IE_SO_MASK) {
2761                         /*
2762                          * Check for subdissectors for this IE or
2763                          * for all IEs in this codeset.
2764                          */
2765                         if (dissector_get_port_handle(codeset_dissector_table, codeset) ||
2766                             dissector_get_port_handle(ie_dissector_table, (codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK))) {
2767                                 next_tvb = tvb_new_subset (tvb, offset, 1, 1);
2768                                 if (dissector_try_port(ie_dissector_table, (codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK), next_tvb, pinfo, q931_tree) ||
2769                                     dissector_try_port(codeset_dissector_table, codeset, next_tvb, pinfo, q931_tree)) {
2770                                         offset += 1;
2771                                         codeset = locked_codeset;
2772                                         continue;
2773                                 }
2774                         }
2775
2776                         switch ((codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK)) {
2777
2778                         case CS0 | Q931_IE_MORE_DATA_OR_SEND_COMP:
2779                                 switch (info_element) {
2780
2781                                 case Q931_IE_MORE_DATA:
2782                                         if (q931_tree != NULL) {
2783                                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2784                                                     "More data");
2785                                         }
2786                                         break;
2787
2788                                 case Q931_IE_SENDING_COMPLETE:
2789                                         if (q931_tree != NULL) {
2790                                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2791                                                     "Sending complete");
2792                                         }
2793                                         break;
2794
2795                                 default:
2796                                         if (q931_tree != NULL) {
2797                                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2798                                                     "Unknown information element (0x%02X)",
2799                                                     info_element);
2800                                         }
2801                                         break;
2802                                 }
2803                                 break;
2804
2805                         case CS0 | Q931_IE_CONGESTION_LEVEL:
2806                                 if (q931_tree != NULL) {
2807                                         proto_tree_add_text(q931_tree, tvb, offset, 1,
2808                                             "Congestion level: %s",
2809                                             val_to_str(info_element & Q931_IE_SO_IE_MASK,
2810                                               q931_congestion_level_vals,
2811                                               "Unknown (0x%X)"));
2812                                 }
2813                                 break;
2814
2815                         case CS0 | Q931_IE_REPEAT_INDICATOR:
2816                                 if (q931_tree != NULL) {
2817                                         proto_tree_add_text(q931_tree, tvb, offset, 1,
2818                                             "Repeat indicator: %s",
2819                                             val_to_str(info_element & Q931_IE_SO_IE_MASK,
2820                                           q931_repeat_indication_vals,
2821                                               "Unknown (0x%X)"));
2822                                 }
2823                                 break;
2824
2825                         default:
2826                                 if (q931_tree != NULL) {
2827                                         proto_tree_add_text(q931_tree, tvb, offset, 1,
2828                                             "Unknown information element (0x%02X)",
2829                                             info_element);
2830                                 }
2831                                 break;
2832                         }
2833                         offset += 1;
2834                         codeset = locked_codeset;
2835                         continue;
2836                 }
2837
2838                 /*
2839                  * Variable-length IE.
2840                  *
2841                  * According to page 18 from Recommendation H.225.0 :
2842                  * " Length of user-user contents contents
2843                  * - Shall be 2 octets instead of 1 (as in Figure 4-36/Q.931)"
2844                  *
2845                  * We assume that if this is Q.931-over-TPKT, it might
2846                  * be H.225 traffic, and check for the IE being a user-user
2847                  * IE with ASN.1 encoding of the user information.
2848                  */
2849                 if (is_over_ip && tvb_bytes_exist(tvb, offset, 4) &&
2850                     codeset == 0 && tvb_get_guint8(tvb, offset) == Q931_IE_USER_USER &&
2851                     tvb_get_guint8(tvb, offset + 3) == Q931_PROTOCOL_DISCRIMINATOR_ASN1)  {
2852                         info_element_len = tvb_get_ntohs(tvb, offset + 1);
2853                         if (q931_tree != NULL) {
2854                                 ti = proto_tree_add_text(q931_tree, tvb, offset,
2855                                     1+2+info_element_len, "%s",
2856                                     val_to_str(info_element,
2857                                       q931_info_element_vals[codeset],
2858                                       "Unknown information element (0x%02X)"));
2859                                 ie_tree = proto_item_add_subtree(ti,
2860                                     ett_q931_ie);
2861                                 proto_tree_add_text(ie_tree, tvb, offset, 1,
2862                                     "Information element: %s",
2863                                     val_to_str(info_element,
2864                                       q931_info_element_vals[codeset], "Unknown (0x%02X)"));
2865                                 proto_tree_add_text(ie_tree, tvb, offset + 1,
2866                                     2, "Length: %u", info_element_len);
2867                                 proto_tree_add_text(ie_tree, tvb, offset + 3,
2868                                     1, "Protocol discriminator: %s",
2869                                     val_to_str_ext(tvb_get_guint8(tvb, offset + 3),
2870                                       &q931_protocol_discriminator_vals_ext,
2871                                       "Unknown (0x%02x)"));
2872                         }
2873
2874                         if (info_element_len > 1) {
2875                                 /*
2876                                  * If we don't desegment limit the length
2877                                  * to the actual size in the frame
2878                                  */
2879                                 if (!pinfo->can_desegment) {
2880                                         info_element_len = MIN(info_element_len, tvb_length_remaining(tvb, offset + 3));
2881                                 }
2882                                 /*
2883                                  * Do we have a handle for the H.225
2884                                  * dissector?
2885                                  */
2886                                 if (h225_handle != NULL) {
2887                                         /*
2888                                          * Yes - call it, regardless of
2889                                          * whether we're building a
2890                                          * protocol tree or not.
2891                                          */
2892                                         h225_tvb = tvb_new_subset(tvb,
2893                                             offset + 4, info_element_len - 1,
2894                                             info_element_len - 1);
2895                                         call_dissector(h225_handle, h225_tvb,
2896                                             pinfo, root_tree);
2897                                 } else {
2898                                         /*
2899                                          * No - just show it as "User
2900                                          * information" (if "ie_tree" is
2901                                          * null, this won't add anything).
2902                                          */
2903                                         proto_tree_add_text(ie_tree, tvb,
2904                                             offset + 4, info_element_len - 1,
2905                                             "User information: %s",
2906                                             tvb_bytes_to_str(tvb, offset + 4,
2907                                               info_element_len - 1));
2908                                 }
2909                         }
2910                         offset += 1 + 2 + info_element_len;
2911                 } else {
2912                         info_element_len = tvb_get_guint8(tvb, offset + 1);
2913
2914                         if (first_segment && (tvb_reported_length_remaining(tvb, offset + 2) < info_element_len)) {  /* incomplete IE at the end of the 1st segment */
2915                                 proto_tree_add_text(q931_tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "Incomplete IE in the 1st segment");
2916                                 break;
2917                         }
2918
2919                         /*
2920                          * Check for subdissectors for this IE or
2921                          * for all IEs in this codeset.
2922                          */
2923                         if (dissector_get_port_handle(codeset_dissector_table, codeset) ||
2924                             dissector_get_port_handle(ie_dissector_table, (codeset << 8) | info_element)) {
2925                                 next_tvb = tvb_new_subset (tvb, offset, info_element_len + 2, info_element_len + 2);
2926                                 if (dissector_try_port(ie_dissector_table, (codeset << 8) | info_element, next_tvb, pinfo, q931_tree) ||
2927                                     dissector_try_port(codeset_dissector_table, codeset, next_tvb, pinfo, q931_tree)) {
2928                                         offset += 2 + info_element_len;
2929                                         codeset = locked_codeset;
2930                                         continue;
2931                                 }
2932                         }
2933
2934                         ti = proto_tree_add_text(q931_tree, tvb, offset, 1+1+info_element_len, "%s",
2935                                     val_to_str(info_element, q931_info_element_vals[codeset], "Unknown information element (0x%02X)"));
2936                         ie_tree = proto_item_add_subtree(ti, ett_q931_ie);
2937                         proto_tree_add_text(ie_tree, tvb, offset, 1, "Information element: %s",
2938                                     val_to_str(info_element, q931_info_element_vals[codeset], "Unknown (0x%02X)"));
2939                         proto_tree_add_text(ie_tree, tvb, offset + 1, 1, "Length: %u", info_element_len);
2940
2941                         if (((codeset << 8) | info_element) == (CS0 | Q931_IE_SEGMENTED_MESSAGE)) {
2942                                 dissect_q931_segmented_message_ie(tvb, offset + 2, info_element_len, ie_tree);
2943                                 if (check_col(pinfo->cinfo, COL_INFO)) {
2944                                         col_append_fstr(pinfo->cinfo, COL_INFO, " of %s",
2945                                             val_to_str_ext(tvb_get_guint8(tvb, offset + 3), &q931_message_type_vals_ext, "Unknown message type (0x%02X)"));
2946                                 }
2947                                 if (tvb_get_guint8(tvb, offset + 2) & 0x80) {  /* the 1st segment */
2948                                         first_segment = TRUE;
2949                                 } else {  /* not the 1st segment */
2950                                         proto_tree_add_text(q931_tree, tvb, offset + 4, tvb_reported_length_remaining(tvb, offset + 4), "Message segment");
2951                                         info_element_len += tvb_reported_length_remaining(tvb, offset + 4);
2952                                 }
2953                         } else {
2954                                 /*
2955                                  * For the calling number, called number,
2956                                  * and release cause IEs, don't check
2957                                  * for the tree being null, as
2958                                  * the dissectors for those IEs also
2959                                  * supply information for the tap used
2960                                  * in VoIP calls.
2961                                  */
2962                                 switch ((codeset << 8) | info_element) {
2963
2964                                 case CS0 | Q931_IE_BEARER_CAPABILITY:
2965                                 case CS0 | Q931_IE_LOW_LAYER_COMPAT:
2966                                         if (q931_tree != NULL) {
2967                                                 dissect_q931_bearer_capability_ie(tvb,
2968                                                         offset + 2, info_element_len,
2969                                                         ie_tree);
2970                                         }
2971                                         break;
2972
2973                                 case CS0 | Q931_IE_CAUSE:
2974                                         dissect_q931_cause_ie_unsafe(tvb,
2975                                                 offset + 2, info_element_len,
2976                                                 ie_tree,
2977                                                 hf_q931_cause_value, &dummy, q931_info_element_vals0);
2978                                         break;
2979
2980                                 case CS0 | Q931_IE_CHANGE_STATUS:
2981                                         if (q931_tree != NULL) {
2982                                                 dissect_q931_change_status_ie(tvb,
2983                                                         offset + 2, info_element_len,
2984                                                         ie_tree);
2985                                         }
2986                                         break;
2987
2988                                 case CS0 | Q931_IE_CALL_STATE:
2989                                         if (q931_tree != NULL) {
2990                                                 dissect_q931_call_state_ie(tvb,
2991                                                         offset + 2, info_element_len,
2992                                                         ie_tree);
2993                                         }
2994                                         break;
2995
2996                                 case CS0 | Q931_IE_CHANNEL_IDENTIFICATION:
2997                                         if (q931_tree != NULL) {
2998                                                 dissect_q931_channel_identification_ie(
2999                                                         tvb, offset + 2, info_element_len,
3000                                                         ie_tree);
3001                                         }
3002                                         break;
3003
3004                                 case CS0 | Q931_IE_PROGRESS_INDICATOR:
3005                                         if (q931_tree != NULL) {
3006                                                 dissect_q931_progress_indicator_ie(tvb,
3007                                                         offset + 2, info_element_len,
3008                                                         ie_tree);
3009                                         }
3010                                         break;
3011
3012                                 case CS0 | Q931_IE_NETWORK_SPECIFIC_FACIL:
3013                                 case CS0 | Q931_IE_TRANSIT_NETWORK_SEL:
3014                                         if (q931_tree != NULL) {
3015                                                 dissect_q931_ns_facilities_ie(tvb,
3016                                                         offset + 2, info_element_len,
3017                                                         ie_tree);
3018                                         }
3019                                         break;
3020
3021                                 case CS0 | Q931_IE_NOTIFICATION_INDICATOR:
3022                                         if (q931_tree != NULL) {
3023                                                 dissect_q931_notification_indicator_ie(
3024                                                         tvb, offset + 2, info_element_len,
3025                                                         ie_tree);
3026                                         }
3027                                         break;
3028
3029                                 case CS0 | Q931_IE_DISPLAY:
3030                                         if (q931_tree != NULL) {
3031                                                 dissect_q931_ia5_ie(tvb, offset + 2,
3032                                                         info_element_len, ie_tree,
3033                                                         "Display information");
3034                                         }
3035                                         break;
3036
3037                                 case CS0 | Q931_IE_DATE_TIME:
3038                                         if (q931_tree != NULL) {
3039                                                 dissect_q931_date_time_ie(tvb,
3040                                                         offset + 2, info_element_len,
3041                                                         ie_tree);
3042                                         }
3043                                         break;
3044
3045                                 case CS0 | Q931_IE_KEYPAD_FACILITY:
3046                                         if (q931_tree != NULL) {
3047                                                 dissect_q931_ia5_ie(tvb, offset + 2,
3048                                                         info_element_len, ie_tree,
3049                                                         "Keypad facility");
3050                                         }
3051                                         break;
3052
3053                                 case CS0 | Q931_IE_SIGNAL:
3054                                         if (q931_tree != NULL) {
3055                                                 dissect_q931_signal_ie(tvb,
3056                                                         offset + 2, info_element_len,
3057                                                         ie_tree);
3058                                         }
3059                                         break;
3060
3061                                 case CS0 | Q931_IE_INFORMATION_RATE:
3062                                         if (q931_tree != NULL) {
3063                                                 dissect_q931_information_rate_ie(tvb,
3064                                                         offset + 2, info_element_len,
3065                                                         ie_tree);
3066                                         }
3067                                         break;
3068
3069                                 case CS0 | Q931_IE_E2E_TRANSIT_DELAY:
3070                                         if (q931_tree != NULL) {
3071                                                 dissect_q931_e2e_transit_delay_ie(tvb,
3072                                                         offset + 2, info_element_len,
3073                                                         ie_tree);
3074                                         }
3075                                         break;
3076
3077                                 case CS0 | Q931_IE_TD_SELECTION_AND_INT:
3078                                         if (q931_tree != NULL) {
3079                                                 dissect_q931_td_selection_and_int_ie(
3080                                                         tvb, offset + 2, info_element_len,
3081                                                         ie_tree);
3082                                         }
3083                                         break;
3084
3085                                 case CS0 | Q931_IE_PL_BINARY_PARAMETERS:
3086                                         if (q931_tree != NULL) {
3087                                                 dissect_q931_pl_binary_parameters_ie(
3088                                                         tvb, offset + 2, info_element_len,
3089                                                         ie_tree);
3090                                         }
3091                                         break;
3092
3093                                 case CS0 | Q931_IE_PL_WINDOW_SIZE:
3094                                         if (q931_tree != NULL) {
3095                                                 dissect_q931_pl_window_size_ie(tvb,
3096                                                         offset + 2, info_element_len,
3097                                                         ie_tree);
3098                                         }
3099                                         break;
3100
3101                                 case CS0 | Q931_IE_PACKET_SIZE:
3102                                         if (q931_tree != NULL) {
3103                                                 dissect_q931_packet_size_ie(tvb,
3104                                                         offset + 2, info_element_len,
3105                                                         ie_tree);
3106                                         }
3107                                         break;
3108
3109                                 case CS0 | Q931_IE_CUG:
3110                                         if (q931_tree != NULL) {
3111                                                 dissect_q931_cug_ie(tvb,
3112                                                         offset + 2, info_element_len,
3113                                                         ie_tree);
3114                                         }
3115                                         break;
3116
3117                                 case CS0 | Q931_IE_REVERSE_CHARGE_IND:
3118                                         if (q931_tree != NULL) {
3119                                                 dissect_q931_reverse_charge_ind_ie(tvb,
3120                                                         offset + 2, info_element_len,
3121                                                         ie_tree);
3122                                         }
3123                                         break;
3124
3125                                 case CS0 | Q931_IE_CONNECTED_NUMBER_DEFAULT:
3126                                         if (q931_tree != NULL) {
3127                                                 dissect_q931_number_ie(tvb,
3128                                                         offset + 2, info_element_len,
3129                                                         ie_tree,
3130                                                         hf_q931_connected_number, e164_info);
3131                                         }
3132                                         break;
3133
3134
3135                                 case CS0 | Q931_IE_CALLING_PARTY_NUMBER:
3136                                         e164_info.e164_number_type = CALLING_PARTY_NUMBER;
3137                                         dissect_q931_number_ie(tvb,
3138                                                 offset + 2, info_element_len,
3139                                                 ie_tree,
3140                                                 hf_q931_calling_party_number, e164_info);
3141                                         break;
3142
3143                                 case CS0 | Q931_IE_CALLED_PARTY_NUMBER:
3144                                         e164_info.e164_number_type = CALLED_PARTY_NUMBER;
3145                                         dissect_q931_number_ie(tvb,
3146                                                 offset + 2, info_element_len,
3147                                                 ie_tree,
3148                                                 hf_q931_called_party_number, e164_info);
3149                                         break;
3150
3151                                 case CS0 | Q931_IE_CALLING_PARTY_SUBADDR:
3152                                 case CS0 | Q931_IE_CALLED_PARTY_SUBADDR:
3153                                         if (q931_tree != NULL) {
3154                                                 dissect_q931_party_subaddr_ie(tvb,
3155                                                         offset + 2, info_element_len,
3156                                                         ie_tree);
3157                                         }
3158                                         break;
3159
3160                                 case CS0 | Q931_IE_REDIRECTING_NUMBER:
3161                                         if (q931_tree != NULL) {
3162                                                 dissect_q931_number_ie(tvb,
3163                                                         offset + 2, info_element_len,
3164                                                         ie_tree,
3165                                                         hf_q931_redirecting_number, e164_info);
3166                                         }
3167                                         break;
3168
3169                                 case CS0 | Q931_IE_RESTART_INDICATOR:
3170                                         if (q931_tree != NULL) {
3171                                                 dissect_q931_restart_indicator_ie(tvb,
3172                                                         offset + 2, info_element_len,
3173                                                         ie_tree);
3174                                         }
3175                                         break;
3176
3177                                 case CS0 | Q931_IE_HIGH_LAYER_COMPAT:
3178                                         if (q931_tree != NULL) {
3179                                                 dissect_q931_high_layer_compat_ie(tvb,
3180                                                         offset + 2, info_element_len,
3181                                                         ie_tree);
3182                                         }
3183                                         break;
3184
3185                                 case CS0 | Q931_IE_USER_USER:
3186                                         if (q931_tree != NULL) {
3187                                                 dissect_q931_user_user_ie(tvb, pinfo,
3188                                                         offset + 2, info_element_len,
3189                                                         ie_tree);
3190                                         }
3191                                         break;
3192
3193                                 case CS5 | Q931_IE_PARTY_CATEGORY:
3194                                         if (q931_tree != NULL) {
3195                                                 dissect_q931_party_category_ie(tvb,
3196                                                         offset + 2, info_element_len,
3197                                                         ie_tree);
3198                                         }
3199                                         break;
3200
3201                                 case CS6 | Q931_IE_DISPLAY:
3202                                         if (q931_tree != NULL) {
3203                                                 dissect_q931_ia5_ie(tvb, offset + 2,
3204                                                         info_element_len, ie_tree,
3205                                                         "Avaya Display");
3206                                         }
3207                                         break;
3208
3209                                 default:
3210                                         if (q931_tree != NULL) {
3211                                                 proto_tree_add_text(ie_tree, tvb,
3212                                                         offset + 2, info_element_len,
3213                                                         "Data: %s",
3214                                                         bytes_to_str(
3215                                                           tvb_get_ptr(tvb, offset + 2,
3216                                                                   info_element_len),
3217                                                           info_element_len));
3218                                         }
3219                                         break;
3220                                 }
3221                         }
3222                         offset += 1 + 1 + info_element_len;
3223                 }
3224                 codeset = locked_codeset;
3225         }
3226         if(have_valid_q931_pi) {
3227                 tap_queue_packet(q931_tap, pinfo, q931_pi);
3228         }
3229         have_valid_q931_pi=FALSE;
3230 }
3231
3232 /*
3233  * Q.931-over-TPKT-over-TCP.
3234  */
3235 static gboolean
3236 dissect_q931_tpkt_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3237 {
3238         int lv_tpkt_len;
3239
3240         /*
3241          * Check whether this looks like a TPKT-encapsulated
3242          * Q.931 packet.
3243          *
3244          * The minimum length of a Q.931 message is 3:
3245          * 1 byte for the protocol discriminator,
3246          * 1 for the call_reference length,
3247          * and one for the message type.
3248          */
3249         lv_tpkt_len = is_tpkt(tvb, 3);
3250         if (lv_tpkt_len == -1) {
3251                 /*
3252                  * It's not a TPKT packet; reject it.
3253                  */
3254                 return FALSE;
3255         }
3256
3257         /*
3258          * If this segment is *exactly* the length of a TPKT header,
3259          * we assume that, as it looks like a TPKT header, it
3260          * is one, and that the code put a TPKT header in one
3261          * segment and the rest of the PDU in another.
3262          */
3263         if (tvb_length(tvb) == 4) {
3264                 /*
3265                  * It is - call the "dissect TPKT over a TCP stream"
3266                  * routine.
3267                  */
3268                 dissect_tpkt_encap(tvb, pinfo, tree, q931_desegment,
3269                     q931_tpkt_pdu_handle);
3270                 return TRUE;
3271         }
3272
3273         /*
3274          * Well, we have more data than just the TPKT header;
3275          * check whether it looks like the beginning of a
3276          * Q.931 message.
3277          *
3278          * The minimum length of a Q.931 message is 3, as per the
3279          * above.
3280          *
3281          * Check that we have that many bytes past the TPKT header in
3282          * the tvbuff; we already know that the TPKT header says we
3283          * have that many bytes (as we passed 3 as the "min_len" argument
3284          * to "is_tpkt()").
3285          */
3286         if (!tvb_bytes_exist(tvb, 4, 3))
3287                 return FALSE;
3288
3289         /* Check the protocol discriminator */
3290         if ((tvb_get_guint8(tvb, 4) != NLPID_Q_931) && (tvb_get_guint8(tvb, 4) != 0x03)) {
3291                 /* Doesn't look like Q.931 inside TPKT */
3292                 return FALSE;
3293         }
3294
3295         /*
3296          * OK, it looks like Q.931-over-TPKT.
3297          * Call the "dissect TPKT over a TCP stream" routine.
3298          */
3299         dissect_tpkt_encap(tvb, pinfo, tree, q931_desegment,
3300             q931_tpkt_pdu_handle);
3301
3302         return TRUE;
3303 }
3304
3305 static void
3306 dissect_q931_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3307 {
3308         dissect_q931_tpkt_heur(tvb, pinfo, tree);
3309 }
3310
3311 static void
3312 dissect_q931_tpkt_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3313 {
3314         dissect_q931_pdu(tvb, pinfo, tree, TRUE);
3315 }
3316
3317 static void
3318 dissect_q931(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3319 {
3320         dissect_q931_pdu(tvb, pinfo, tree, FALSE);
3321 }
3322
3323 static void
3324 dissect_q931_over_ip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3325 {
3326         dissect_q931_pdu(tvb, pinfo, tree, TRUE);
3327 }
3328
3329 static void
3330 dissect_q931_ie_cs0(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3331 {
3332         dissect_q931_IEs(tvb, pinfo, NULL, tree, FALSE, 0, 0);
3333 }
3334
3335 static void
3336 dissect_q931_ie_cs7(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3337 {
3338         dissect_q931_IEs(tvb, pinfo, NULL, tree, FALSE, 0, 7);
3339 }
3340
3341 static void
3342 q931_init(void) {
3343         /* Initialize the fragment and reassembly tables */
3344         fragment_table_init(&q931_fragment_table);
3345         reassembled_table_init(&q931_reassembled_table);
3346 }
3347
3348 void
3349 proto_register_q931(void)
3350 {
3351         static hf_register_info hf[] = {
3352                 { &hf_q931_discriminator,
3353                   { "Protocol discriminator", "q931.disc", FT_UINT8, BASE_HEX, NULL, 0x0,
3354                         NULL, HFILL }},
3355
3356                 { &hf_q931_call_ref_len,
3357                   { "Call reference value length", "q931.call_ref_len", FT_UINT8, BASE_DEC, NULL, 0x0,
3358                         NULL, HFILL }},
3359
3360                 { &hf_q931_call_ref_flag,
3361                   { "Call reference flag", "q931.call_ref_flag", FT_BOOLEAN, BASE_NONE, TFS(&tfs_call_ref_flag), 0x0,
3362                         NULL, HFILL }},
3363
3364                 { &hf_q931_call_ref,
3365                   { "Call reference value", "q931.call_ref", FT_BYTES, BASE_NONE, NULL, 0x0,
3366                         NULL, HFILL }},
3367
3368                 { &hf_q931_message_type,
3369                   { "Message type", "q931.message_type", FT_UINT8, BASE_HEX|BASE_EXT_STRING, &q931_message_type_vals_ext, 0x0,
3370                         NULL, HFILL }},
3371
3372                 { &hf_q931_maintenance_message_type,
3373                   { "Maintenance message type", "q931.maintenance_message_type", FT_UINT8, BASE_HEX, VALS(dms_message_type_vals), 0x0,
3374                         NULL, HFILL }},
3375
3376                 { &hf_q931_segment_type,
3377                   { "Segmented message type", "q931.segment_type", FT_UINT8, BASE_HEX|BASE_EXT_STRING, &q931_message_type_vals_ext, 0x0,
3378                         NULL, HFILL }},
3379
3380                 { &hf_q931_coding_standard,
3381                   { "Coding standard", "q931.coding_standard", FT_UINT8, BASE_HEX,
3382                          VALS(q931_coding_standard_vals), 0x60,NULL, HFILL }},
3383
3384                 { &hf_q931_interpretation,
3385                   { "Interpretation", "q931.interpretation", FT_UINT8, BASE_HEX,
3386                         VALS(q931_interpretation_vals), 0x1C, NULL, HFILL}},
3387
3388                 { &hf_q931_pres_meth_prot_prof,
3389                   { "Presentation method of protocol profile", "q931.presentation_method_protocol_profile", FT_UINT8, BASE_HEX,
3390                         VALS(q931_pres_meth_prot_prof_vals), 0x03, NULL, HFILL}},
3391
3392                 { &hf_q931_high_layer_characteristics,
3393                   { "High layer characteristics identification", "q931.high_layer_characteristics", FT_UINT8, BASE_HEX|BASE_EXT_STRING,
3394                         &q931_high_layer_characteristics_vals_ext, 0x7f, NULL, HFILL }},
3395
3396                 { &hf_q931_extended_high_layer_characteristics,
3397                   { "Extended high layer characteristics identification", "q931.extended_high_layer_characteristics", FT_UINT8, BASE_HEX|BASE_EXT_STRING,
3398                         &q931_extended_high_layer_characteristics_vals_ext, 0x7f, NULL, HFILL }},
3399
3400                 { &hf_q931_extended_audiovisual_characteristics,
3401                   { "Extended audiovisual characteristics identification", "q931.extended_audiovisual_characteristics", FT_UINT8, BASE_HEX,
3402                         VALS(q931_audiovisual_characteristics_vals), 0x7f, NULL, HFILL }},
3403
3404                 { &hf_q931_information_transfer_capability,
3405                   { "Information transfer capability", "q931.information_transfer_capability", FT_UINT8, BASE_HEX,
3406                          VALS(q931_information_transfer_capability_vals), 0x1f,NULL, HFILL }},
3407
3408                 { &hf_q931_transfer_mode,
3409                   { "Transfer mode", "q931.transfer_mode", FT_UINT8, BASE_HEX,
3410                          VALS(q931_transfer_mode_vals), 0x60,NULL, HFILL }},
3411
3412                 { &hf_q931_information_transfer_rate,
3413                   { "Information transfer rate", "q931.information_transfer_rate", FT_UINT8, BASE_HEX,
3414                          VALS(q931_information_transfer_rate_vals), 0x1f,NULL, HFILL }},
3415
3416                 { &hf_q931_layer_ident,
3417                   { "Layer identification", "q931.layer_ident", FT_UINT8, BASE_HEX,
3418                          VALS(q931_bearer_capability_layer_ident_vals), 0x60, NULL, HFILL }},
3419
3420                 { &hf_q931_uil1,
3421                   { "User information layer 1 protocol", "q931.uil1", FT_UINT8, BASE_HEX|BASE_EXT_STRING,
3422                          &q931_uil1_vals_ext, 0x1f,NULL, HFILL }},
3423
3424                 { &hf_q931_cause_location,
3425                   { "Cause location", "q931.cause_location", FT_UINT8, BASE_DEC|BASE_EXT_STRING, &q931_cause_location_vals_ext, 0x0f,
3426                         NULL, HFILL }},
3427
3428                 { &hf_q931_cause_value,
3429                   { "Cause value", "q931.cause_value", FT_UINT8, BASE_DEC|BASE_EXT_STRING, &q931_cause_code_vals_ext, 0x7f,
3430                         NULL, HFILL }},
3431
3432                 { &hf_q931_number_type,
3433                   { "Number type", "q931.number_type", FT_UINT8, BASE_HEX, VALS(q931_number_type_vals), 0x70,
3434                         NULL, HFILL }},
3435
3436                 { &hf_q931_numbering_plan,
3437                   { "Numbering plan", "q931.numbering_plan", FT_UINT8, BASE_HEX, VALS(q931_numbering_plan_vals), 0x0f,
3438                         NULL, HFILL }},
3439
3440                 { &hf_q931_screening_ind,
3441                   { "Screening indicator", "q931.screening_ind", FT_UINT8, BASE_HEX, VALS(q931_screening_indicator_vals), 0x03,
3442                         NULL, HFILL }},
3443
3444                 { &hf_q931_presentation_ind,
3445                   { "Presentation indicator", "q931.presentation_ind", FT_UINT8, BASE_HEX, VALS(q931_presentation_indicator_vals), 0x60,
3446                         NULL, HFILL }},
3447
3448                 { &hf_q931_extension_ind,
3449                   { "Extension indicator",  "q931.extension_ind",
3450                         FT_BOOLEAN, 8, TFS(&q931_extension_ind_value), 0x80,
3451                         NULL, HFILL }},
3452
3453                 { &hf_q931_calling_party_number,
3454                   { "Calling party number digits", "q931.calling_party_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
3455                         NULL, HFILL }},
3456
3457                 { &hf_q931_called_party_number,
3458                   { "Called party number digits", "q931.called_party_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
3459                         NULL, HFILL }},
3460
3461                 { &hf_q931_connected_number,
3462                   { "Connected party number digits", "q931.connected_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
3463                         NULL, HFILL }},
3464
3465                 { &hf_q931_redirecting_number,
3466                   { "Redirecting party number digits", "q931.redirecting_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
3467                         NULL, HFILL }},
3468
3469     /* fields for channel identification IE */
3470                 /* 0x80 is the extension bit */
3471
3472                 { &hf_q931_channel_interface_explicit,
3473                   { "Interface identifier present", "q931.channel.interface_id_present", FT_BOOLEAN, 8, NULL, 0x40,
3474                     "True if the interface identifier is explicit in the following octets", HFILL }},
3475
3476                 { &hf_q931_channel_interface_type,
3477                   { "Interface type", "q931.channel.interface_type", FT_BOOLEAN, 8, TFS(&tfs_interface_type), 0x20,
3478                     "Identifies the ISDN interface type", HFILL }},
3479
3480                 /* 0x10 is spare */
3481
3482                 { &hf_q931_channel_exclusive,
3483                   { "Indicated channel", "q931.channel.exclusive", FT_BOOLEAN, 8, TFS(&tfs_channel_exclusive), 0x08,
3484                     "True if only the indicated channel is acceptable", HFILL }},
3485
3486                 { &hf_q931_channel_dchan,
3487                   { "D-channel indicator", "q931.channel.dchan", FT_BOOLEAN, 8, NULL, 0x04,
3488                     "True if the identified channel is the D-Channel", HFILL }},
3489
3490                 { &hf_q931_channel_selection_bri,
3491                   { "Information channel selection", "q931.channel.selection", FT_UINT8, BASE_HEX, q931_basic_channel_selection_vals, 0x03,
3492                     "Identifies the information channel to be used", HFILL }},
3493
3494                 { &hf_q931_channel_selection_pri,
3495                   { "Information channel selection", "q931.channel.selection", FT_UINT8, BASE_HEX, q931_not_basic_channel_selection_vals, 0x03,
3496                     "Identifies the information channel to be used", HFILL }},
3497
3498                 { &hf_q931_channel_map,
3499                   { "Number/map", "q931.channel.map", FT_BOOLEAN, 8, TFS(&tfs_channel_map), 0x10,
3500                     "True if channel is indicates by channel map rather than number", HFILL }},
3501
3502                 { &hf_q931_channel_element_type,
3503                   { "Element type", "q931.channel.element_type", FT_UINT8, BASE_HEX, q931_element_type_vals, 0xF,
3504                     "Type of element in the channel number/slot map octets", HFILL }},
3505
3506                 { &hf_q931_channel_number,
3507                   { "Channel number", "q931.channel.number", FT_UINT8, BASE_DEC, NULL, 0x7F,
3508                     NULL, HFILL }},
3509
3510     /* desegmentation fields */
3511                 { &hf_q931_segment_overlap,
3512                   { "Segment overlap", "q931.segment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
3513                         "Fragment overlaps with other fragments", HFILL }},
3514
3515                 { &hf_q931_segment_overlap_conflict,
3516                   { "Conflicting data in fragment overlap", "q931.segment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
3517                         "Overlapping fragments contained conflicting data", HFILL }},
3518
3519                 { &hf_q931_segment_multiple_tails,
3520                   { "Multiple tail fragments found", "q931.segment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
3521                         "Several tails were found when defragmenting the packet", HFILL }},
3522
3523                 { &hf_q931_segment_too_long_segment,
3524                   { "Segment too long", "q931.segment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
3525                         "Segment contained data past end of packet", HFILL }},
3526
3527                 { &hf_q931_segment_error,
3528                   { "Defragmentation error", "q931.segment.error", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
3529                         "Defragmentation error due to illegal fragments", HFILL }},
3530
3531                 { &hf_q931_segment,
3532                   { "Q.931 Segment", "q931.segment", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
3533                         NULL, HFILL }},
3534
3535                 { &hf_q931_segments,
3536                   { "Q.931 Segments", "q931.segments", FT_NONE, BASE_NONE, NULL, 0x0,
3537                         NULL, HFILL }},
3538
3539                 { &hf_q931_reassembled_in,
3540                   { "Reassembled Q.931 in frame", "q931.reassembled_in", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
3541                         "This Q.931 message is reassembled in this frame", HFILL}},
3542
3543                 { &hf_q931_reassembled_length,
3544                   { "Reassembled Q.931 length", "q931.reassembled.length", FT_UINT32, BASE_DEC, NULL, 0x0,
3545                         "The total length of the reassembled payload", HFILL}},
3546         };
3547         static gint *ett[] = {
3548                 &ett_q931,
3549                 &ett_q931_ie,
3550                 &ett_q931_segments,
3551                 &ett_q931_segment,
3552         };
3553         module_t *q931_module;
3554
3555         proto_q931 = proto_register_protocol("Q.931", "Q.931", "q931");
3556         proto_register_field_array (proto_q931, hf, array_length(hf));
3557         proto_register_subtree_array(ett, array_length(ett));
3558         register_init_routine(q931_init);
3559
3560         register_dissector("q931", dissect_q931, proto_q931);
3561         register_dissector("q931.tpkt", dissect_q931_tpkt, proto_q931);
3562         q931_tpkt_handle = find_dissector("q931.tpkt");
3563         q931_tpkt_pdu_handle = create_dissector_handle(dissect_q931_tpkt_pdu,
3564             proto_q931);
3565         register_dissector("q931.over_ip", dissect_q931_over_ip, proto_q931);
3566         register_dissector("q931.ie", dissect_q931_ie_cs0, proto_q931);
3567         register_dissector("q931.ie.cs7", dissect_q931_ie_cs7, proto_q931);
3568
3569         /* subdissector code */
3570         codeset_dissector_table = register_dissector_table("q931.codeset", "Q.931 Codeset", FT_UINT8, BASE_HEX);
3571         ie_dissector_table = register_dissector_table("q931.ie", "Q.931 IE", FT_UINT16, BASE_HEX);
3572         register_heur_dissector_list("q931_user", &q931_user_heur_subdissector_list);
3573
3574         q931_module = prefs_register_protocol(proto_q931, NULL);
3575         prefs_register_bool_preference(q931_module, "desegment_h323_messages",
3576             "Reassemble Q.931 messages spanning multiple TCP segments",
3577             "Whether the Q.931 dissector should reassemble messages spanning multiple TCP segments."
3578             " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
3579             &q931_desegment);
3580         prefs_register_bool_preference(q931_module, "reassembly",
3581             "Reassemble segmented Q.931 messages",
3582             "Reassemble segmented Q.931 messages (Q.931 - Annex H)",
3583             &q931_reassembly);
3584         /* Register for tapping */
3585         q931_tap = register_tap("q931");
3586 }
3587
3588 void
3589 proto_reg_handoff_q931(void)
3590 {
3591         dissector_handle_t q931_handle;
3592         dissector_handle_t q931_over_ip_handle;
3593
3594         q931_handle = find_dissector("q931");
3595         dissector_add("lapd.sapi", LAPD_SAPI_Q931, q931_handle);
3596
3597         q931_over_ip_handle = find_dissector("q931.over_ip");
3598         dissector_add("sctp.ppi", H323_PAYLOAD_PROTOCOL_ID, q931_over_ip_handle);
3599
3600         /*
3601          * Attempt to get a handle for the H.225 dissector.
3602          * If we can't, the handle we get is null, and we'll just
3603          * dissect putatively-H.255 Call Signaling stuff as User
3604          * Information.
3605          */
3606         h225_handle = find_dissector("h225");
3607
3608         data_handle = find_dissector("data");
3609
3610         /*
3611          * For H.323.
3612          */
3613         heur_dissector_add("tcp", dissect_q931_tpkt_heur, proto_q931);
3614 }
3615
3616 static void reset_q931_packet_info(q931_packet_info *pi)
3617 {
3618     if(pi == NULL) {
3619         return;
3620     }
3621
3622     pi->calling_number = NULL;
3623     pi->called_number = NULL;
3624     pi->cause_value = 0xFF;
3625     pi->crv = -1;
3626 }