Remove a bunch of duplicate semicolons.
[obnox/wireshark/wip.git] / packet-q931.c
1 /* packet-q931.c
2  * Routines for Q.931 frame disassembly
3  * Guy Harris <guy@alum.mit.edu>
4  *
5  * $Id: packet-q931.c,v 1.60 2003/09/21 20:06:00 gerald Exp $
6  *
7  * Modified by Andreas Sikkema for possible use with H.323
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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 <stdio.h>
33 #include <glib.h>
34 #include <string.h>
35 #include <epan/packet.h>
36 #include <epan/strutil.h>
37 #include "nlpid.h"
38 #include "packet-q931.h"
39 #include "prefs.h"
40
41 #include "packet-tpkt.h"
42
43 /* Q.931 references:
44  *
45  * http://www.acacia-net.com/Clarinet/Protocol/q9313svn.htm
46  * http://www.acacia-net.com/Clarinet/Protocol/q9311sc3.htm
47  * http://www.acacia-net.com/Clarinet/Protocol/q9317oz7.htm
48  * http://www.protocols.com/pbook/isdn.htm
49  * http://freesoft.org/CIE/Topics/126.htm
50  * http://noc.comstar.ru/miscdocs/ascend-faq-cause-codes.html
51  * http://www.andrews-arnold.co.uk/isdn/q931cause.html
52  * http://www.tulatelecom.ru/staff/german/DSSHelp/MessList/InfEl/InfElList.html
53  */
54
55 static int proto_q931 = -1;
56 static int hf_q931_discriminator = -1;
57 static int hf_q931_call_ref_len = -1;
58 static int hf_q931_call_ref_flag = -1;
59 static int hf_q931_call_ref = -1;
60 static int hf_q931_message_type = -1;
61 static int hf_q931_cause_value = -1;
62 static int hf_q931_calling_party_number = -1;
63 static int hf_q931_called_party_number = -1;
64 static int hf_q931_connected_number = -1;
65 static int hf_q931_redirecting_number = -1;
66
67 static gint ett_q931 = -1;
68 static gint ett_q931_ie = -1;
69
70 static dissector_table_t codeset_dissector_table;
71 static dissector_table_t ie_dissector_table;
72
73 /* desegmentation of Q.931 over TPKT over TCP */
74 static gboolean q931_desegment = TRUE;
75
76 static dissector_handle_t h225_handle;
77 static dissector_handle_t q931_tpkt_pdu_handle;
78
79 /*
80  * Q.931 message types.
81  */
82 #define Q931_ESCAPE             0x00
83 #define Q931_ALERTING           0x01
84 #define Q931_CALL_PROCEEDING    0x02
85 #define Q931_CONNECT            0x07
86 #define Q931_CONNECT_ACK        0x0F
87 #define Q931_PROGRESS           0x03
88 #define Q931_SETUP              0x05
89 #define Q931_SETUP_ACK          0x0D
90 #define Q931_HOLD               0x24
91 #define Q931_HOLD_ACK           0x28
92 #define Q931_HOLD_REJECT        0x30
93 #define Q931_RESUME             0x26
94 #define Q931_RESUME_ACK         0x2E
95 #define Q931_RESUME_REJECT      0x22
96 #define Q931_RETRIEVE           0x31
97 #define Q931_RETRIEVE_ACK       0x33
98 #define Q931_RETRIEVE_REJECT    0x37
99 #define Q931_SUSPEND            0x25
100 #define Q931_SUSPEND_ACK        0x2D
101 #define Q931_SUSPEND_REJECT     0x21
102 #define Q931_USER_INFORMATION   0x20
103 #define Q931_DISCONNECT         0x45
104 #define Q931_RELEASE            0x4D
105 #define Q931_RELEASE_COMPLETE   0x5A
106 #define Q931_RESTART            0x46
107 #define Q931_RESTART_ACK        0x4E
108 #define Q931_CONGESTION_CONTROL 0x79
109 #define Q931_FACILITY           0x62
110 #define Q931_INFORMATION        0x7B
111 #define Q931_NOTIFY             0x6E
112 #define Q931_REGISTER           0x64
113 #define Q931_SEGMENT            0x60
114 #define Q931_STATUS             0x7D
115 #define Q931_STATUS_ENQUIRY     0x75
116
117 static const value_string q931_message_type_vals[] = {
118         { Q931_ESCAPE,                  "ESCAPE" },
119         { Q931_ALERTING,                "ALERTING" },
120         { Q931_CALL_PROCEEDING,         "CALL PROCEEDING" },
121         { Q931_CONNECT,                 "CONNECT" },
122         { Q931_CONNECT_ACK,             "CONNECT ACKNOWLEDGE" },
123         { Q931_PROGRESS,                "PROGRESS" },
124         { Q931_SETUP,                   "SETUP" },
125         { Q931_SETUP_ACK,               "SETUP ACKNOWLEDGE" },
126         { Q931_HOLD,                    "HOLD" },
127         { Q931_HOLD_ACK,                "HOLD_ACKNOWLEDGE" },
128         { Q931_HOLD_REJECT,             "HOLD_REJECT" },
129         { Q931_RESUME,                  "RESUME" },
130         { Q931_RESUME_ACK,              "RESUME ACKNOWLEDGE" },
131         { Q931_RESUME_REJECT,           "RESUME REJECT" },
132         { Q931_RETRIEVE,                "RETRIEVE" },
133         { Q931_RETRIEVE_ACK,            "RETRIEVE ACKNOWLEDGE" },
134         { Q931_RETRIEVE_REJECT,         "RETRIEVE REJECT" },
135         { Q931_SUSPEND,                 "SUSPEND" },
136         { Q931_SUSPEND_ACK,             "SUSPEND ACKNOWLEDGE" },
137         { Q931_SUSPEND_REJECT,          "SUSPEND REJECT" },
138         { Q931_USER_INFORMATION,        "USER INFORMATION" },
139         { Q931_DISCONNECT,              "DISCONNECT" },
140         { Q931_RELEASE,                 "RELEASE" },
141         { Q931_RELEASE_COMPLETE,        "RELEASE COMPLETE" },
142         { Q931_RESTART,                 "RESTART" },
143         { Q931_RESTART_ACK,             "RESTART ACKNOWLEDGE" },
144         { Q931_CONGESTION_CONTROL,      "CONGESTION CONTROL" },
145         { Q931_FACILITY,                "FACILITY" },
146         { Q931_INFORMATION,             "INFORMATION" },
147         { Q931_NOTIFY,                  "NOTIFY" },
148         { Q931_REGISTER,                "REGISTER" },
149         { Q931_SEGMENT,                 "SEGMENT" },
150         { Q931_STATUS,                  "STATUS" },
151         { Q931_STATUS_ENQUIRY,          "STATUS ENQUIRY" },
152         { 0,                            NULL }
153 };
154
155 static const true_false_string tfs_call_ref_flag = {
156         "Message sent to originating side",
157         "Message sent from originating side"
158 };
159
160 /*
161  * Information elements.
162  */
163
164 /* Shifted codeset values */
165 #define CS0 0x000
166 #define CS1 0x100
167 #define CS2 0x200
168 #define CS3 0x300
169 #define CS4 0x400
170 #define CS5 0x500
171 #define CS6 0x600
172 #define CS7 0x700
173
174 #define Q931_IE_SO_MASK 0x80    /* single-octet/variable-length mask */
175 /*
176  * Single-octet IEs.
177  */
178 #define Q931_IE_SO_IDENTIFIER_MASK      0xf0    /* IE identifier mask */
179 #define Q931_IE_SO_IDENTIFIER_SHIFT     4       /* IE identifier shift */
180 #define Q931_IE_SO_IE_MASK              0x0F    /* IE mask */
181
182 #define Q931_IE_SHIFT                   0x90
183 #define Q931_IE_SHIFT_NON_LOCKING       0x08    /* non-locking shift */
184 #define Q931_IE_SHIFT_CODESET           0x0F    /* codeset */
185
186 #define Q931_IE_MORE_DATA_OR_SEND_COMP  0xA0    /* More Data or Sending Complete */
187 #define Q931_IE_MORE_DATA               0xA0
188 #define Q931_IE_SENDING_COMPLETE        0xA1
189
190 #define Q931_IE_CONGESTION_LEVEL        0xB0
191 #define Q931_IE_REPEAT_INDICATOR        0xD0
192
193 /*
194  * Variable-length IEs.
195  */
196 #define Q931_IE_VL_EXTENSION            0x80    /* Extension flag */
197
198 /*
199  * Codeset 0 (default).
200  */
201 #define Q931_IE_SEGMENTED_MESSAGE       0x00
202 #define Q931_IE_BEARER_CAPABILITY       0x04
203 #define Q931_IE_CAUSE                   0x08
204 #define Q931_IE_CALL_IDENTITY           0x10
205 #define Q931_IE_CALL_STATE              0x14
206 #define Q931_IE_CHANNEL_IDENTIFICATION  0x18
207 #define Q931_IE_FACILITY                0x1C
208 #define Q931_IE_PROGRESS_INDICATOR      0x1E
209 #define Q931_IE_NETWORK_SPECIFIC_FACIL  0x20    /* Network Specific Facilities */
210 #define Q931_IE_NOTIFICATION_INDICATOR  0x27
211 #define Q931_IE_DISPLAY                 0x28
212 #define Q931_IE_DATE_TIME               0x29
213 #define Q931_IE_KEYPAD_FACILITY         0x2C
214 #define Q931_IE_INFORMATION_REQUEST     0x32
215 #define Q931_IE_SIGNAL                  0x34
216 #define Q931_IE_SWITCHHOOK              0x36
217 #define Q931_IE_FEATURE_ACTIVATION      0x38
218 #define Q931_IE_FEATURE_INDICATION      0x39
219 #define Q931_IE_ENDPOINT_IDENTIFIER     0x3B
220 #define Q931_IE_SERVICE_PROFILE_ID      0x3A
221 #define Q931_IE_INFORMATION_RATE        0x40
222 #define Q931_IE_E2E_TRANSIT_DELAY       0x42    /* End-to-end Transit Delay */
223 #define Q931_IE_TD_SELECTION_AND_INT    0x43    /* Transit Delay Selection and Indication */
224 #define Q931_IE_PL_BINARY_PARAMETERS    0x44    /* Packet layer binary parameters */
225 #define Q931_IE_PL_WINDOW_SIZE          0x45    /* Packet layer window size */
226 #define Q931_IE_PACKET_SIZE             0x46    /* Packet size */
227 #define Q931_IE_CUG                     0x47    /* Closed user group */
228 #define Q931_IE_REVERSE_CHARGE_IND      0x4A    /* Reverse charging indication */
229 #define Q931_IE_CONNECTED_NUMBER_DEFAULT        0x4C    /* Connected Number */
230 #define Q931_IE_CALLING_PARTY_NUMBER    0x6C    /* Calling Party Number */
231 #define Q931_IE_CALLING_PARTY_SUBADDR   0x6D    /* Calling Party Subaddress */
232 #define Q931_IE_CALLED_PARTY_NUMBER     0x70    /* Called Party Number */
233 #define Q931_IE_CALLED_PARTY_SUBADDR    0x71    /* Called Party Subaddress */
234 #define Q931_IE_REDIRECTING_NUMBER      0x74
235 #define Q931_IE_REDIRECTION_NUMBER      0x76
236 #define Q931_IE_TRANSIT_NETWORK_SEL     0x78    /* Transit Network Selection */
237 #define Q931_IE_RESTART_INDICATOR       0x79
238 #define Q931_IE_LOW_LAYER_COMPAT        0x7C    /* Low-Layer Compatibility */
239 #define Q931_IE_HIGH_LAYER_COMPAT       0x7D    /* High-Layer Compatibility */
240 #define Q931_IE_USER_USER               0x7E    /* User-User */
241 #define Q931_IE_ESCAPE                  0x7F    /* Escape for extension */
242
243 /*
244  * Codeset 0 ETSI.
245  */
246 #define Q931_IE_CONNECTED_NUMBER        0x8C
247 #define Q931_IE_CONNECTED_SUBADDR       0x8D
248
249 /*
250  * Codeset 5 (National-specific) Belgium.
251  */
252 #define Q931_IE_CHARGING_ADVICE         0x1A
253
254 /*
255  * Codeset 5 (National-specific) Bellcore National ISDN.
256  */
257 #define Q931_IE_OPERATOR_SYSTEM_ACCESS  0x1D
258
259 /*
260  * Codeset 6 (Network-specific) Belgium.
261  */
262 /* 0x1A is Charging Advice, as with Codeset 5 */
263 #define Q931_IE_REDIRECTING_NUMBER      0x74
264
265 /*
266  * Codeset 6 (Network-specific) FT-Numeris.
267  */
268 /* 0x1D is User Capability */
269
270 /*
271  * Codeset 6 (Network-specific) Bellcore National ISDN.
272  */
273 #define Q931_IE_REDIRECTING_SUBADDR     0x75    /* Redirecting Subaddress */
274 /* 0x76 is Redirection Number, but that's also Codeset 0 */
275 #define Q931_IE_CALL_APPEARANCE         0x7B
276
277 /* Codeset 0 */
278 static const value_string q931_info_element_vals0[] = {
279         { Q931_IE_SEGMENTED_MESSAGE,            "Segmented message" },
280         { Q931_IE_BEARER_CAPABILITY,            "Bearer capability" },
281         { Q931_IE_CAUSE,                        "Cause" },
282         { Q931_IE_CALL_IDENTITY,                "Call identity" },
283         { Q931_IE_CALL_STATE,                   "Call state" },
284         { Q931_IE_CHANNEL_IDENTIFICATION,       "Channel identification" },
285         { Q931_IE_FACILITY,                     "Facility" },
286         { Q931_IE_PROGRESS_INDICATOR,           "Progress indicator" },
287         { Q931_IE_NETWORK_SPECIFIC_FACIL,       "Network specific facilities" },
288         { Q931_IE_NOTIFICATION_INDICATOR,       "Notification indicator" },
289         { Q931_IE_DISPLAY,                      "Display" },
290         { Q931_IE_DATE_TIME,                    "Date/Time" },
291         { Q931_IE_KEYPAD_FACILITY,              "Keypad facility" },
292         { Q931_IE_INFORMATION_REQUEST,          "Information request" },
293         { Q931_IE_SIGNAL,                       "Signal" },
294         { Q931_IE_SWITCHHOOK,                   "Switchhook" },
295         { Q931_IE_FEATURE_ACTIVATION,           "Feature activation" },
296         { Q931_IE_FEATURE_INDICATION,           "Feature Indication" },
297         { Q931_IE_ENDPOINT_IDENTIFIER,          "Endpoint identifier" },
298         { Q931_IE_SERVICE_PROFILE_ID,           "Service profile ID" },
299         { Q931_IE_INFORMATION_RATE,             "Information rate" },
300         { Q931_IE_E2E_TRANSIT_DELAY,            "End-to-end transit delay" },
301         { Q931_IE_TD_SELECTION_AND_INT,         "Transit delay selection and indication" },
302         { Q931_IE_PL_BINARY_PARAMETERS,         "Packet layer binary parameters" },
303         { Q931_IE_PL_WINDOW_SIZE,               "Packet layer window size" },
304         { Q931_IE_PACKET_SIZE,                  "Packet size" },
305         { Q931_IE_CUG,                          "Closed user group" },
306         { Q931_IE_REVERSE_CHARGE_IND,           "Reverse charging indication" },
307         { Q931_IE_CONNECTED_NUMBER_DEFAULT,     "Connected number" },
308         { Q931_IE_CALLING_PARTY_NUMBER,         "Calling party number" },
309         { Q931_IE_CALLING_PARTY_SUBADDR,        "Calling party subaddress" },
310         { Q931_IE_CALLED_PARTY_NUMBER,          "Called party number" },
311         { Q931_IE_CALLED_PARTY_SUBADDR,         "Called party subaddress" },
312         { Q931_IE_REDIRECTING_NUMBER,           "Redirecting number" },
313         { Q931_IE_REDIRECTION_NUMBER,           "Redirection number" },
314         { Q931_IE_TRANSIT_NETWORK_SEL,          "Transit network selection" },
315         { Q931_IE_RESTART_INDICATOR,            "Restart indicator" },
316         { Q931_IE_LOW_LAYER_COMPAT,             "Low-layer compatibility" },
317         { Q931_IE_HIGH_LAYER_COMPAT,            "High-layer compatibility" },
318         { Q931_IE_USER_USER,                    "User-user" },
319         { Q931_IE_ESCAPE,                       "Escape" },
320         { Q931_IE_CONNECTED_NUMBER,             "Connected number" },
321         { Q931_IE_CONNECTED_SUBADDR,            "Connected subaddress" },
322         { 0,                                    NULL }
323 };
324 /* Codeset 1 */
325 static const value_string q931_info_element_vals1[] = {
326         { 0,                                    NULL }
327 };
328 /* Codeset 2 */
329 static const value_string q931_info_element_vals2[] = {
330         { 0,                                    NULL }
331 };
332 /* Codeset 3 */
333 static const value_string q931_info_element_vals3[] = {
334         { 0,                                    NULL }
335 };
336 /* Codeset 4 */
337 static const value_string q931_info_element_vals4[] = {
338         { 0,                                    NULL }
339 };
340 /* Codeset 5 */
341 static const value_string q931_info_element_vals5[] = {
342         { Q931_IE_CHARGING_ADVICE,              "Charging advice" },
343         { Q931_IE_OPERATOR_SYSTEM_ACCESS,       "Operator system access" },
344         { 0,                                    NULL }
345 };
346 /* Codeset 6 */
347 static const value_string q931_info_element_vals6[] = {
348         { Q931_IE_REDIRECTING_NUMBER,           "Redirecting number" },
349         { Q931_IE_REDIRECTING_SUBADDR,          "Redirecting subaddress" },
350         { Q931_IE_CALL_APPEARANCE,              "Call appearance" },
351         { 0,                                    NULL }
352 };
353 /* Codeset 7 */
354 static const value_string q931_info_element_vals7[] = {
355         { 0,                                    NULL }
356 };
357
358 /* Codeset array */
359 static const value_string *q931_info_element_vals[] = {
360   q931_info_element_vals0,
361   q931_info_element_vals1,
362   q931_info_element_vals2,
363   q931_info_element_vals3,
364   q931_info_element_vals4,
365   q931_info_element_vals5,
366   q931_info_element_vals6,
367   q931_info_element_vals7,
368 };
369
370 static const value_string q931_congestion_level_vals[] = {
371         { 0x0, "Receiver ready" },
372         { 0xF, "Receiver not ready" },
373         { 0,   NULL }
374 };
375
376 static const value_string q931_repeat_indication_vals[] = {
377         { 0x2, "Prioritized list" },
378         { 0,   NULL }
379 };
380
381 /*
382  * ITU-standardized coding.
383  */
384 #define Q931_ITU_STANDARDIZED_CODING    0x00
385
386 /*
387  * Dissect a Segmented message information element.
388  */
389 static void
390 dissect_q931_segmented_message_ie(tvbuff_t *tvb, int offset, int len,
391     proto_tree *tree)
392 {
393         if (len != 2) {
394                 proto_tree_add_text(tree, tvb, offset, len,
395                     "Segmented message: length is %d, should be 2\n", len);
396                 return;
397         }
398         if (tvb_get_guint8(tvb, offset) & 0x80) {
399                 proto_tree_add_text(tree, tvb, offset, 1,
400                     "First segment: %u segments remaining",
401                     tvb_get_guint8(tvb, offset) & 0x7F);
402         } else {
403                 proto_tree_add_text(tree, tvb, offset, 1,
404                     "Not first segment: %u segments remaining",
405                     tvb_get_guint8(tvb, offset) & 0x7F);
406         }
407         proto_tree_add_text(tree, tvb, offset + 1, 1,
408             "Segmented message type: %u\n", tvb_get_guint8(tvb, offset + 1));
409 }
410
411 /*
412  * Dissect a Bearer capability or Low-layer compatibility information element.
413  */
414 static const value_string q931_bc_coding_standard_vals[] = {
415         { 0x00, "ITU-T standardized coding" },
416         { 0x20, "ISO/IEC standard" },
417         { 0x40, "National standard" },
418         { 0x60, "Standard defined for this particular network" },
419         { 0,    NULL }
420 };
421
422 static const value_string q931_information_transfer_capability_vals[] = {
423         { 0x00, "Speech" },
424         { 0x08, "Unrestricted digital information" },
425         { 0x09, "Restricted digital information" },
426         { 0x10, "3.1 kHz audio" },
427         { 0x11, "Unrestricted digital information with tones/announcements" },
428         { 0x18, "Video" },
429         { 0,    NULL }
430 };
431
432 static const value_string q931_transfer_mode_vals[] = {
433         { 0x00, "Circuit mode" },
434         { 0x40, "Packet mode" },
435         { 0,    NULL }
436 };
437
438 #define Q931_IT_RATE_MULTIRATE  0x18
439
440 static const value_string q931_information_transfer_rate_vals[] = {
441         { 0x00,                         "Packet mode" },
442         { 0x10,                         "64 kbit/s" },
443         { 0x11,                         "2 x 64 kbit/s" },
444         { 0x13,                         "384 kbit/s" },
445         { 0x15,                         "1536 kbit/s" },
446         { 0x17,                         "1920 kbit/s" },
447         { Q931_IT_RATE_MULTIRATE,       "Multirate (64 kbit/s base rate)" },
448         { 0,                            NULL }
449 };
450
451 static const value_string q931_uil1_vals[] = {
452         { 0x01, "V.110/I.460/X.30 rate adaption" },
453         { 0x02, "Recommendation G.711 u-law" },
454         { 0x03, "Recommendation G.711 A-law" },
455         { 0x04, "Recommendation G.721 32 kbit/s ADPCM and Recommendation I.460" },
456         { 0x05, "Recommendation H.221 and H.242" },
457         { 0x06, "Recommendation H.223 and H.245" },
458         { 0x07, "Non-ITU-T-standardized rate adaption" },
459         { 0x08, "V.120 rate adaption" },
460         { 0x09, "X.31 HDLC flag stuffing" },
461         { 0,    NULL },
462 };
463
464 static const value_string q931_l1_user_rate_vals[] = {
465         { 0x00, "Rate indicated by E-bits" },
466         { 0x01, "0.6 kbit/s" },
467         { 0x02, "1.2 kbit/s" },
468         { 0x03, "2.4 kbit/s" },
469         { 0x04, "3.6 kbit/s" },
470         { 0x05, "4.8 kbit/s" },
471         { 0x06, "7.2 kbit/s" },
472         { 0x07, "8 kbit/s" },
473         { 0x08, "9.6 kbit/s" },
474         { 0x09, "14.4 kbit/s" },
475         { 0x0A, "16 kbit/s" },
476         { 0x0B, "19.2 kbit/s" },
477         { 0x0C, "32 kbit/s" },
478         { 0x0E, "48 kbit/s" },
479         { 0x0F, "56 kbit/s" },
480         { 0x10, "64 kbit/s "},
481         { 0x15, "0.1345 kbit/s" },
482         { 0x16, "0.100 kbit/s" },
483         { 0x17, "0.075/1.2 kbit/s" },
484         { 0x18, "1.2/0.075 kbit/s" },
485         { 0x19, "0.050 kbit/s" },
486         { 0x1A, "0.075 kbit/s" },
487         { 0x1B, "0.110 kbit/s" },
488         { 0x1C, "0.150 kbit/s" },
489         { 0x1D, "0.200 kbit/s" },
490         { 0x1E, "0.300 kbit/s" },
491         { 0x1F, "12 kbit/s" },
492         { 0,    NULL }
493 };
494
495 static const value_string q931_l1_intermediate_rate_vals[] = {
496         { 0x20, "8 kbit/s" },
497         { 0x40, "16 kbit/s" },
498         { 0x60, "32 kbit/s" },
499         { 0,    NULL }
500 };
501
502 static const value_string q931_l1_stop_bits_vals[] = {
503         { 0x20, "1" },
504         { 0x40, "1.5" },
505         { 0x60, "2" },
506         { 0,    NULL }
507 };
508
509 static const value_string q931_l1_data_bits_vals[] = {
510         { 0x08, "5" },
511         { 0x10, "7" },
512         { 0x18, "8" },
513         { 0,    NULL }
514 };
515
516 static const value_string q931_l1_parity_vals[] = {
517         { 0x00, "Odd" },
518         { 0x02, "Even" },
519         { 0x03, "None" },
520         { 0x04, "Forced to 0" },
521         { 0x05, "Forced to 1" },
522         { 0,    NULL }
523 };
524
525 static const value_string q931_l1_modem_type_vals[] = {
526         { 0x11, "V.21" },
527         { 0x12, "V.22" },
528         { 0x13, "V.22 bis" },
529         { 0x14, "V.23" },
530         { 0x15, "V.26" },
531         { 0x16, "V.26 bis" },
532         { 0x17, "V.26 ter" },
533         { 0x18, "V.27" },
534         { 0x19, "V.27 bis" },
535         { 0x1A, "V.27 ter" },
536         { 0x1B, "V.29" },
537         { 0x1C, "V.32" },
538         { 0x1E, "V.34" },
539         { 0,    NULL }
540 };
541
542 #define Q931_UIL2_USER_SPEC     0x10
543
544 static const value_string q931_uil2_vals[] = {
545         { 0x01,                 "Basic mode ISO 1745" },
546         { 0x02,                 "Q.921/I.441" },        /* LAPD */
547         { 0x06,                 "X.25, link layer" },   /* LAPB */
548         { 0x07,                 "X.25 multilink" },     /* or 0x0F? */
549         { 0x08,                 "T.71 Extended LAPB" },
550         { 0x09,                 "HDLC ARM" },
551         { 0x0A,                 "HDLC NRM" },
552         { 0x0B,                 "HDLC ABM" },
553         { 0x0C,                 "ISO 8802/2 LLC" },
554         { 0x0D,                 "X.75 Single Link Procedure" },
555         { 0x0E,                 "Q.922" },
556         { 0x0F,                 "Core aspects of Q.922" },
557         { Q931_UIL2_USER_SPEC,  "User-specified" },
558         { 0x11,                 "ISO 7776 DTE-DTE operation" },
559         { 0,                    NULL }
560 };
561
562 static const value_string q931_mode_vals[] = {
563         { 0x20, "Normal mode" },
564         { 0x40, "Extended mode" },
565         { 0,    NULL }
566 };
567
568 #define Q931_UIL3_X25_PL        0x06
569 #define Q931_UIL3_ISO_8208      0x07    /* X.25-based */
570 #define Q931_UIL3_X223          0x08    /* X.25-based */
571 #define Q931_UIL3_TR_9577       0x0B
572 #define Q931_UIL3_USER_SPEC     0x10
573
574 static const value_string q931_uil3_vals[] = {
575         { 0x02,                 "Q.931/I.451" },
576         { Q931_UIL3_X25_PL,     "X.25, packet layer" },
577         { Q931_UIL3_ISO_8208,   "ISO/IEC 8208" },
578         { Q931_UIL3_X223,       "X.223/ISO 8878" },
579         { 0x09,                 "ISO/IEC 8473" },
580         { 0x0A,                 "T.70" },
581         { Q931_UIL3_TR_9577,    "ISO/IEC TR 9577" },
582         { Q931_UIL3_USER_SPEC,  "User-specified" },
583         { 0,                    NULL }
584 };
585
586 static void
587 dissect_q931_protocol_discriminator(tvbuff_t *tvb, int offset, proto_tree *tree)
588 {
589         unsigned int discriminator = tvb_get_guint8(tvb, offset);
590
591         if (discriminator == NLPID_Q_931) {
592                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
593                          tvb, offset, 1, discriminator,
594                          "Protocol discriminator: Q.931");
595         } else if (discriminator == NLPID_Q_2931) {
596                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
597                          tvb, offset, 1, discriminator,
598                          "Protocol discriminator: Q.2931");
599         } else if ((discriminator >= 16 && discriminator < 63)
600             || ((discriminator >= 80) && (discriminator < 254))) {
601                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
602                     tvb, offset, 1, discriminator,
603                     "Protocol discriminator: Network layer or layer 3 protocol (0x%02X)",
604                     discriminator);
605         } else if (discriminator >= 64 && discriminator <= 79) {
606                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
607                     tvb, offset, 1, discriminator,
608                     "Protocol discriminator: National use (0x%02X)",
609                     discriminator);
610         } else {
611                 proto_tree_add_uint_format(tree, hf_q931_discriminator,
612                     tvb, offset, 1, discriminator,
613                     "Protocol discriminator: Reserved (0x%02X)",
614                     discriminator);
615         }
616 }
617
618 void
619 dissect_q931_bearer_capability_ie(tvbuff_t *tvb, int offset, int len,
620     proto_tree *tree)
621 {
622         guint8 octet;
623         guint8 coding_standard;
624         guint8 it_rate;
625         guint8 modem_type;
626         guint8 uil2_protocol;
627         guint8 uil3_protocol;
628         guint8 add_l3_info;
629
630         if (len == 0)
631                 return;
632         octet = tvb_get_guint8(tvb, offset);
633         coding_standard = octet & 0x60;
634         proto_tree_add_text(tree, tvb, offset, 1,
635             "Coding standard: %s",
636             val_to_str(coding_standard, q931_bc_coding_standard_vals, NULL));
637         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
638                 /*
639                  * We don't know how the bearer capability is encoded,
640                  * so just dump it as data and be done with it.
641                  */
642                 proto_tree_add_text(tree, tvb, offset,
643                     len, "Data: %s",
644                     tvb_bytes_to_str(tvb, offset, len));
645                 return;
646         }
647         proto_tree_add_text(tree, tvb, offset, 1,
648             "Information transfer capability: %s",
649             val_to_str(octet & 0x1F, q931_information_transfer_capability_vals,
650               "Unknown (0x%02X)"));
651         offset += 1;
652         len -= 1;
653
654         /*
655          * XXX - only in Low-layer compatibility information element.
656          */
657         if (!(octet & Q931_IE_VL_EXTENSION)) {
658                 if (len == 0)
659                         return;
660                 octet = tvb_get_guint8(tvb, offset);
661                 proto_tree_add_text(tree, tvb, offset, 1,
662                     "Out-band negotiation %spossible",
663                     (octet & 0x40) ? "" : "not ");
664                 offset += 1;
665                 len -= 1;
666         }
667
668         if (len == 0)
669                 return;
670         octet = tvb_get_guint8(tvb, offset);
671         proto_tree_add_text(tree, tvb, offset, 1,
672             "Transfer mode: %s",
673             val_to_str(octet & 0x60, q931_transfer_mode_vals,
674               "Unknown (0x%02X)"));
675         it_rate = octet & 0x1F;
676         proto_tree_add_text(tree, tvb, offset, 1,
677             "Information transfer rate: %s",
678             val_to_str(it_rate, q931_information_transfer_rate_vals,
679               "Unknown (0x%02X)"));
680         offset += 1;
681         len -= 1;
682
683         if (it_rate == Q931_IT_RATE_MULTIRATE) {
684                 if (len == 0)
685                         return;
686                 proto_tree_add_text(tree, tvb, offset, 1, "Rate multiplier: %u", tvb_get_guint8(tvb, offset));
687                 offset += 1;
688                 len -= 1;
689         }
690
691         if (len == 0)
692                 return;
693         octet = tvb_get_guint8(tvb, offset);
694         if ((octet & 0x60) == 0x20) {
695                 /*
696                  * Layer 1 information.
697                  */
698                 proto_tree_add_text(tree, tvb, offset, 1,
699                     "User information layer 1 protocol: %s",
700                     val_to_str(octet & 0x1F, q931_uil1_vals,
701                       "Unknown (0x%02X)"));
702                 offset += 1;
703                 len -= 1;
704
705                 if (octet & Q931_IE_VL_EXTENSION)
706                         goto l1_done;
707                 if (len == 0)
708                         return;
709                 octet = tvb_get_guint8(tvb, offset);
710                 proto_tree_add_text(tree, tvb, offset, 1,
711                     "Layer 1 is %s",
712                     (octet & 0x40) ? "Asynchronous" : "Synchronous");
713                 proto_tree_add_text(tree, tvb, offset, 1,
714                     "Layer 1 in-band negotiation is %spossible",
715                     (octet & 0x20) ? "" : "not ");
716                 proto_tree_add_text(tree, tvb, offset, 1,
717                     "User rate: %s",
718                     val_to_str(octet & 0x1F, q931_l1_user_rate_vals,
719                       "Unknown (0x%02X)"));
720                 offset += 1;
721                 len -= 1;
722
723                 if (octet & Q931_IE_VL_EXTENSION)
724                         goto l1_done;
725                 if (len == 0)
726                         return;
727                 octet = tvb_get_guint8(tvb, offset);
728                 proto_tree_add_text(tree, tvb, offset, 1,
729                     "Intermediate rate: %s",
730                       val_to_str(octet & 0x60, q931_l1_intermediate_rate_vals,
731                        "Unknown (0x%X)"));
732                 proto_tree_add_text(tree, tvb, offset, 1,
733                     "%s to send data with network independent clock",
734                     (octet & 0x10) ? "Required" : "Not required");
735                 proto_tree_add_text(tree, tvb, offset, 1,
736                     "%s accept data with network independent clock",
737                     (octet & 0x08) ? "Can" : "Cannot");
738                 proto_tree_add_text(tree, tvb, offset, 1,
739                     "%s to send data with flow control mechanism",
740                     (octet & 0x04) ? "Required" : "Not required");
741                 proto_tree_add_text(tree, tvb, offset, 1,
742                     "%s accept data with flow control mechanism",
743                     (octet & 0x02) ? "Can" : "Cannot");
744                 offset += 1;
745                 len -= 1;
746
747                 if (octet & Q931_IE_VL_EXTENSION)
748                         goto l1_done;
749                 if (len == 0)
750                         return;
751                 octet = tvb_get_guint8(tvb, offset);
752                 proto_tree_add_text(tree, tvb, offset, 1,
753                     "Rate adaption header %sincluded",
754                     (octet & 0x40) ? "" : "not ");
755                 proto_tree_add_text(tree, tvb, offset, 1,
756                     "Multiple frame establishment %ssupported",
757                     (octet & 0x20) ? "" : "not ");
758                 proto_tree_add_text(tree, tvb, offset, 1,
759                     "%s mode of operation",
760                     (octet & 0x10) ? "Protocol sensitive" : "Bit transparent");
761                 proto_tree_add_text(tree, tvb, offset, 1,
762                     (octet & 0x08) ?
763                       "Full protocol negotiation" : "LLI = 256 only");
764                 proto_tree_add_text(tree, tvb, offset, 1,
765                     "Message originator is %s",
766                     (octet & 0x04) ? "Assignor only" : "Default assignee");
767                 proto_tree_add_text(tree, tvb, offset, 1,
768                     "Negotiation is done %s",
769                     (octet & 0x02) ? "in-band" : "out-of-band");
770                 offset += 1;
771                 len -= 1;
772
773                 if (octet & Q931_IE_VL_EXTENSION)
774                         goto l1_done;
775                 if (len == 0)
776                         return;
777                 octet = tvb_get_guint8(tvb, offset);
778                 proto_tree_add_text(tree, tvb, offset, 1,
779                     "Stop bits: %s",
780                       val_to_str(octet & 0x60, q931_l1_stop_bits_vals,
781                        "Unknown (0x%X)"));
782                 proto_tree_add_text(tree, tvb, offset, 1,
783                     "Data bits: %s",
784                       val_to_str(octet & 0x18, q931_l1_data_bits_vals,
785                        "Unknown (0x%X)"));
786                 proto_tree_add_text(tree, tvb, offset, 1,
787                     "Parity: %s",
788                       val_to_str(octet & 0x07, q931_l1_parity_vals,
789                        "Unknown (0x%X)"));
790
791                 if (octet & Q931_IE_VL_EXTENSION)
792                         goto l1_done;
793                 if (len == 0)
794                         return;
795                 octet = tvb_get_guint8(tvb, offset);
796                 proto_tree_add_text(tree, tvb, offset, 1,
797                     "%s duplex",
798                     (octet & 0x40) ? "Full" : "Half");
799                 modem_type = octet & 0x3F;
800                 if (modem_type <= 0x5 ||
801                     (modem_type >= 0x20 && modem_type <= 0x2F)) {
802                         proto_tree_add_text(tree, tvb, offset, 1,
803                             "Modem type: National use 0x%02X", modem_type);
804                 } else if (modem_type >= 0x30) {
805                         proto_tree_add_text(tree, tvb, offset, 1,
806                             "Modem type: User specified 0x%02X", modem_type);
807                 } else {
808                         proto_tree_add_text(tree, tvb, offset, 1,
809                             "Modem type: %s",
810                               val_to_str(modem_type, q931_l1_modem_type_vals,
811                               "Unknown (0x%02X)"));
812                 }
813                 offset += 1;
814                 len -= 1;
815         }
816 l1_done:
817         ;
818
819         if (len == 0)
820                 return;
821         octet = tvb_get_guint8(tvb, offset);
822         if ((octet & 0x60) == 0x40) {
823                 /*
824                  * Layer 2 information.
825                  */
826                 uil2_protocol = octet & 0x1F;
827                 proto_tree_add_text(tree, tvb, offset, 1,
828                     "User information layer 2 protocol: %s",
829                     val_to_str(uil2_protocol, q931_uil2_vals,
830                       "Unknown (0x%02X)"));
831                 offset += 1;
832                 len -= 1;
833
834                 /*
835                  * XXX - only in Low-layer compatibility information element.
836                  */
837                 if (octet & Q931_IE_VL_EXTENSION)
838                         goto l2_done;
839                 if (len == 0)
840                         return;
841                 octet = tvb_get_guint8(tvb, offset);
842                 if (uil2_protocol == Q931_UIL2_USER_SPEC) {
843                         proto_tree_add_text(tree, tvb, offset, 1,
844                             "User-specified layer 2 protocol information: 0x%02X",
845                             octet & 0x7F);
846                 } else {
847                         proto_tree_add_text(tree, tvb, offset, 1,
848                             "Mode: %s",
849                             val_to_str(octet & 0x60, q931_mode_vals,
850                               "Unknown (0x%02X)"));
851                 }
852                 offset += 1;
853                 len -= 1;
854
855                 if (octet & Q931_IE_VL_EXTENSION)
856                         goto l2_done;
857                 if (len == 0)
858                         return;
859                 octet = tvb_get_guint8(tvb, offset);
860                 proto_tree_add_text(tree, tvb, offset, 1,
861                     "Window size: %u k", octet & 0x7F);
862                 offset += 1;
863                 len -= 1;
864         }
865 l2_done:
866         ;
867
868         if (len == 0)
869                 return;
870         octet = tvb_get_guint8(tvb, offset);
871         if ((octet & 0x60) == 0x60) {
872                 /*
873                  * Layer 3 information.
874                  */
875                 uil3_protocol = octet & 0x1F;
876                 proto_tree_add_text(tree, tvb, offset, 1,
877                     "User information layer 3 protocol: %s",
878                     val_to_str(uil3_protocol, q931_uil3_vals,
879                       "Unknown (0x%02X)"));
880                 offset += 1;
881                 len -= 1;
882
883
884                 /*
885                  * XXX - only in Low-layer compatibility information element.
886                  */
887                 if (octet & Q931_IE_VL_EXTENSION)
888                         goto l3_done;
889                 if (len == 0)
890                         return;
891                 octet = tvb_get_guint8(tvb, offset);
892                 switch (uil3_protocol) {
893
894                 case Q931_UIL3_X25_PL:
895                 case Q931_UIL3_ISO_8208:
896                 case Q931_UIL3_X223:
897                         proto_tree_add_text(tree, tvb, offset, 1,
898                             "Mode: %s",
899                             val_to_str(octet & 0x60, q931_mode_vals,
900                               "Unknown (0x%02X)"));
901                         offset += 1;
902                         len -= 1;
903
904                         if (octet & Q931_IE_VL_EXTENSION)
905                                 goto l3_done;
906                         if (len == 0)
907                                 return;
908                         octet = tvb_get_guint8(tvb, offset);
909                         proto_tree_add_text(tree, tvb, offset, 1,
910                             "Default packet size: %u", octet & 0x0F);
911                         offset += 1;
912                         len -= 1;
913
914                         if (octet & Q931_IE_VL_EXTENSION)
915                                 goto l3_done;
916                         if (len == 0)
917                                 return;
918                         octet = tvb_get_guint8(tvb, offset);
919                         proto_tree_add_text(tree, tvb, offset, 1,
920                             "Packet window size: %u", octet & 0x7F);
921                         offset += 1;
922                         len -= 1;
923                         break;
924
925                 case Q931_UIL3_USER_SPEC:
926                         proto_tree_add_text(tree, tvb, offset, 1,
927                             "Default packet size: %u octets",
928                             1 << (octet & 0x0F));
929                         offset += 1;
930                         len -= 1;
931                         break;
932
933                 case Q931_UIL3_TR_9577:
934                         add_l3_info = (octet & 0x0F) << 4;
935                         if (octet & Q931_IE_VL_EXTENSION)
936                                 goto l3_done;
937                         if (len == 0)
938                                 return;
939                         octet = tvb_get_guint8(tvb, offset + 1);
940                         add_l3_info |= (octet & 0x0F);
941                         proto_tree_add_text(tree, tvb, offset, 2,
942                             "Additional layer 3 protocol information: %s",
943                             val_to_str(add_l3_info, nlpid_vals,
944                               "Unknown (0x%02X)"));
945                         offset += 2;
946                         len -= 2;
947                         break;
948                 }
949         }
950 l3_done:
951         ;
952 }
953
954 /*
955  * Dissect a Cause information element.
956  */
957 static const value_string q931_cause_coding_standard_vals[] = {
958         { 0x00, "ITU-T standardized coding" },
959         { 0x20, "ISO/IEC standard" },
960         { 0x40, "National standard" },
961         { 0x60, "Standard specific to identified location" },
962         { 0,    NULL }
963 };
964
965 const value_string q931_cause_location_vals[] = {
966         { 0x00, "User (U)" },
967         { 0x01, "Private network serving the local user (LPN)" },
968         { 0x02, "Public network serving the local user (LN)" },
969         { 0x03, "Transit network (TN)" },
970         { 0x04, "Public network serving the remote user (RLN)" },
971         { 0x05, "Private network serving the remote user (RPN)" },
972         { 0x07, "International network (INTL)" },
973         { 0x0A, "Network beyond interworking point (BI)" },
974         { 0,    NULL }
975 };
976
977 static const value_string q931_cause_recommendation_vals[] = {
978         { 0x00, "Q.931" },
979         { 0x03, "X.21" },
980         { 0x04, "X.25" },
981         { 0x05, "Q.1031/Q.1051" },
982         { 0,    NULL }
983 };
984
985 /*
986  * Cause codes for Cause.
987  */
988 #define Q931_CAUSE_UNALLOC_NUMBER       0x01
989 #define Q931_CAUSE_NO_ROUTE_TO_DEST     0x03
990 #define Q931_CAUSE_CALL_REJECTED        0x15
991 #define Q931_CAUSE_NUMBER_CHANGED       0x16
992 #define Q931_CAUSE_ACCESS_INFO_DISC     0x2B
993 #define Q931_CAUSE_QOS_UNAVAILABLE      0x31
994 #define Q931_CAUSE_CHAN_NONEXISTENT     0x52
995 #define Q931_CAUSE_INCOMPATIBLE_DEST    0x58
996 #define Q931_CAUSE_MAND_IE_MISSING      0x60
997 #define Q931_CAUSE_MT_NONEX_OR_UNIMPL   0x61
998 #define Q931_CAUSE_IE_NONEX_OR_UNIMPL   0x63
999 #define Q931_CAUSE_INVALID_IE_CONTENTS  0x64
1000 #define Q931_CAUSE_MSG_INCOMPAT_W_CS    0x65
1001 #define Q931_CAUSE_REC_TIMER_EXP        0x66
1002
1003 const value_string q931_cause_code_vals[] = {
1004         { 0x00,                         "Valid cause code not yet received" },
1005         { Q931_CAUSE_UNALLOC_NUMBER,    "Unallocated (unassigned) number" },
1006         { 0x02,                         "No route to specified transit network" },
1007         { Q931_CAUSE_NO_ROUTE_TO_DEST,  "No route to destination" },
1008         { 0x04,                         "Send special information tone" },
1009         { 0x05,                         "Misdialled trunk prefix" },
1010         { 0x06,                         "Channel unacceptable" },
1011         { 0x07,                         "Call awarded and being delivered in an established channel" },
1012         { 0x08,                         "Prefix 0 dialed but not allowed" },
1013                                         /* Q.850 - "Preemption" */
1014         { 0x09,                         "Prefix 1 dialed but not allowed" },
1015                                         /* Q.850 - "Preemption - circuit reserved for reuse" */
1016         { 0x0A,                         "Prefix 1 dialed but not required" },
1017         { 0x0B,                         "More digits received than allowed, call is proceeding" },
1018         { 0x0E,                         "QoR: ported number" },
1019         { 0x10,                         "Normal call clearing" },
1020         { 0x11,                         "User busy" },
1021         { 0x12,                         "No user responding" },
1022         { 0x13,                         "No answer from user (user alerted)" },
1023         { 0x14,                         "Subscriber absent" },
1024         { Q931_CAUSE_CALL_REJECTED,     "Call rejected" },
1025         { Q931_CAUSE_NUMBER_CHANGED,    "Number changed" },
1026         { 0x17,                         "Reverse charging rejected" },
1027                                         /* Q.850 - "Redirection to new destination" */
1028         { 0x18,                         "Call suspended" },
1029                                         /* Q.850 Amendment 1 - "Call rejected due to feature at the destination" */
1030         { 0x19,                         "Call resumed" },
1031                                         /* Q.850 - "Exchange routing error */
1032         { 0x1A,                         "Non-selected user clearing" },
1033         { 0x1B,                         "Destination out of order" },
1034         { 0x1C,                         "Invalid number format (incomplete number)" },
1035         { 0x1D,                         "Facility rejected" },
1036         { 0x1E,                         "Response to STATUS ENQUIRY" },
1037         { 0x1F,                         "Normal unspecified" },
1038         { 0x21,                         "Circuit out of order" },
1039         { 0x22,                         "No circuit/channel available" },
1040         { 0x23,                         "Destination unattainable" },
1041         { 0x25,                         "Degraded service" },
1042         { 0x26,                         "Network out of order" },
1043         { 0x27,                         "Transit delay range cannot be achieved" },
1044                                         /* Q.850 - "Permanent frame mode connection out of service" */
1045         { 0x28,                         "Throughput range cannot be achieved" },
1046                                         /* Q.850 - "Permanent frame mode connection operational" */
1047         { 0x29,                         "Temporary failure" },
1048         { 0x2A,                         "Switching equipment congestion" },
1049         { Q931_CAUSE_ACCESS_INFO_DISC,  "Access information discarded" },
1050         { 0x2C,                         "Requested circuit/channel not available" },
1051         { 0x2D,                         "Pre-empted" },
1052         { 0x2E,                         "Precedence call blocked" },
1053         { 0x2F,                         "Resources unavailable, unspecified" },
1054         { Q931_CAUSE_QOS_UNAVAILABLE,   "Quality of service unavailable" },
1055         { 0x32,                         "Requested facility not subscribed" },
1056         { 0x33,                         "Reverse charging not allowed" },
1057         { 0x34,                         "Outgoing calls barred" },
1058         { 0x35,                         "Outgoing calls barred within CUG" },
1059         { 0x36,                         "Incoming calls barred" },
1060         { 0x37,                         "Incoming calls barred within CUG" },
1061         { 0x38,                         "Call waiting not subscribed" },
1062         { 0x39,                         "Bearer capability not authorized" },
1063         { 0x3A,                         "Bearer capability not presently available" },
1064         { 0x3E,                         "Inconsistency in designated outgoing access information and subscriber class" },
1065         { 0x3F,                         "Service or option not available, unspecified" },
1066         { 0x41,                         "Bearer capability not implemented" },
1067         { 0x42,                         "Channel type not implemented" },
1068         { 0x43,                         "Transit network selection not implemented" },
1069         { 0x44,                         "Message not implemented" },
1070         { 0x45,                         "Requested facility not implemented" },
1071         { 0x46,                         "Only restricted digital information bearer capability is available" },
1072         { 0x4F,                         "Service or option not implemented, unspecified" },
1073         { 0x51,                         "Invalid call reference value" },
1074         { Q931_CAUSE_CHAN_NONEXISTENT,  "Identified channel does not exist" },
1075         { 0x53,                         "Call identity does not exist for suspended call" },
1076         { 0x54,                         "Call identity in use" },
1077         { 0x55,                         "No call suspended" },
1078         { 0x56,                         "Call having the requested call identity has been cleared" },
1079         { 0x57,                         "Called user not member of CUG" },
1080         { Q931_CAUSE_INCOMPATIBLE_DEST, "Incompatible destination" },
1081         { 0x59,                         "Non-existent abbreviated address entry" },
1082         { 0x5A,                         "Destination address missing, and direct call not subscribed" },
1083                                         /* Q.850 - "Non-existent CUG" */
1084         { 0x5B,                         "Invalid transit network selection (national use)" },
1085         { 0x5C,                         "Invalid facility parameter" },
1086         { 0x5D,                         "Mandatory information element is missing" },
1087         { 0x5F,                         "Invalid message, unspecified" },
1088         { Q931_CAUSE_MAND_IE_MISSING,   "Mandatory information element is missing" },
1089         { Q931_CAUSE_MT_NONEX_OR_UNIMPL,"Message type non-existent or not implemented" },
1090         { 0x62,                         "Message not compatible with call state or message type non-existent or not implemented" },
1091         { Q931_CAUSE_IE_NONEX_OR_UNIMPL,"Information element nonexistant or not implemented" },
1092         { Q931_CAUSE_INVALID_IE_CONTENTS,"Invalid information element contents" },
1093         { Q931_CAUSE_MSG_INCOMPAT_W_CS, "Message not compatible with call state" },
1094         { Q931_CAUSE_REC_TIMER_EXP,     "Recovery on timer expiry" },
1095         { 0x67,                         "Parameter non-existent or not implemented - passed on" },
1096         { 0x6E,                         "Message with unrecognized parameter discarded" },
1097         { 0x6F,                         "Protocol error, unspecified" },
1098         { 0x7F,                         "Internetworking, unspecified" },
1099         { 0,                            NULL }
1100 };
1101
1102 static const value_string q931_cause_condition_vals[] = {
1103         { 0x00, "Unknown" },
1104         { 0x01, "Permanent" },
1105         { 0x02, "Transient" },
1106         { 0x00, NULL }
1107 };
1108
1109 #define Q931_REJ_USER_SPECIFIC          0x00
1110 #define Q931_REJ_IE_MISSING             0x04
1111 #define Q931_REJ_IE_INSUFFICIENT        0x08
1112
1113 static const value_string q931_rejection_reason_vals[] = {
1114         { 0x00, "User specific" },
1115         { 0x04, "Information element missing" },
1116         { 0x08, "Information element contents are not sufficient" },
1117         { 0x00, NULL }
1118 };
1119
1120 void
1121 dissect_q931_cause_ie(tvbuff_t *tvb, int offset, int len,
1122     proto_tree *tree, int hf_cause_value)
1123 {
1124         guint8 octet;
1125         guint8 cause_value;
1126         guint8 coding_standard;
1127         guint8 rejection_reason;
1128
1129         if (len == 0)
1130                 return;
1131         octet = tvb_get_guint8(tvb, offset);
1132         coding_standard = octet & 0x60;
1133         proto_tree_add_text(tree, tvb, offset, 1,
1134             "Coding standard: %s",
1135             val_to_str(coding_standard, q931_cause_coding_standard_vals, NULL));
1136         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1137                 /*
1138                  * We don't know how the cause is encoded,
1139                  * so just dump it as data and be done with it.
1140                  */
1141                 proto_tree_add_text(tree, tvb, offset,
1142                     len, "Data: %s",
1143                     tvb_bytes_to_str(tvb, offset, len));
1144                 return;
1145         }
1146         proto_tree_add_text(tree, tvb, offset, 1,
1147             "Location: %s",
1148             val_to_str(octet & 0x0F, q931_cause_location_vals,
1149               "Unknown (0x%X)"));
1150         offset += 1;
1151         len -= 1;
1152
1153         if (!(octet & Q931_IE_VL_EXTENSION)) {
1154                 if (len == 0)
1155                         return;
1156                 octet = tvb_get_guint8(tvb, offset);
1157                 proto_tree_add_text(tree, tvb, offset, 1,
1158                     "Recommendation: %s",
1159                     val_to_str(octet & 0x7F, q931_cause_recommendation_vals,
1160                       "Unknown (0x%02X)"));
1161                 offset += 1;
1162                 len -= 1;
1163         }
1164
1165         if (len == 0)
1166                 return;
1167         octet = tvb_get_guint8(tvb, offset);
1168         cause_value = octet & 0x7F;
1169         proto_tree_add_uint(tree, hf_cause_value, tvb, 0, 1, cause_value);
1170         offset += 1;
1171         len -= 1;
1172
1173         if (len == 0)
1174                 return;
1175         switch (cause_value) {
1176
1177         case Q931_CAUSE_UNALLOC_NUMBER:
1178         case Q931_CAUSE_NO_ROUTE_TO_DEST:
1179         case Q931_CAUSE_QOS_UNAVAILABLE:
1180                 octet = tvb_get_guint8(tvb, offset);
1181                 proto_tree_add_text(tree, tvb, offset, 1,
1182                     "Network service: %s",
1183                     (octet & 0x80) ? "User" : "Provider");
1184                 proto_tree_add_text(tree, tvb, offset, 1,
1185                     "%s",
1186                     (octet & 0x40) ? "Abnormal" : "Normal");
1187                 proto_tree_add_text(tree, tvb, offset, 1,
1188                     "Condition: %s",
1189                     val_to_str(octet & 0x03, q931_cause_condition_vals,
1190                       "Unknown (0x%X)"));
1191                 break;
1192
1193         case Q931_CAUSE_CALL_REJECTED:
1194                 rejection_reason = octet & 0x7C;
1195                 proto_tree_add_text(tree, tvb, offset, 1,
1196                     "Rejection reason: %s",
1197                     val_to_str(octet & 0x7C, q931_rejection_reason_vals,
1198                       "Unknown (0x%X)"));
1199                 proto_tree_add_text(tree, tvb, offset, 1,
1200                     "Condition: %s",
1201                     val_to_str(octet & 0x03, q931_cause_condition_vals,
1202                       "Unknown (0x%X)"));
1203                 offset += 1;
1204                 len -= 1;
1205
1206                 if (len == 0)
1207                         return;
1208                 switch (rejection_reason) {
1209
1210                 case Q931_REJ_USER_SPECIFIC:
1211                         proto_tree_add_text(tree, tvb, offset, len,
1212                             "User specific diagnostic: %s",
1213                             tvb_bytes_to_str(tvb, offset, len));
1214                         break;
1215
1216                 case Q931_REJ_IE_MISSING:
1217                         proto_tree_add_text(tree, tvb, offset, 1,
1218                             "Missing information element: %s",
1219                             val_to_str(tvb_get_guint8(tvb, offset), q931_info_element_vals0,
1220                               "Unknown (0x%02X)"));
1221                         break;
1222
1223                 case Q931_REJ_IE_INSUFFICIENT:
1224                         proto_tree_add_text(tree, tvb, offset, 1,
1225                             "Insufficient information element: %s",
1226                             val_to_str(tvb_get_guint8(tvb, offset), q931_info_element_vals0,
1227                               "Unknown (0x%02X)"));
1228                         break;
1229
1230                 default:
1231                         proto_tree_add_text(tree, tvb, offset, len,
1232                             "Diagnostic: %s",
1233                             tvb_bytes_to_str(tvb, offset, len));
1234                         break;
1235                 }
1236                 break;
1237
1238         case Q931_CAUSE_ACCESS_INFO_DISC:
1239         case Q931_CAUSE_INCOMPATIBLE_DEST:
1240         case Q931_CAUSE_MAND_IE_MISSING:
1241         case Q931_CAUSE_IE_NONEX_OR_UNIMPL:
1242         case Q931_CAUSE_INVALID_IE_CONTENTS:
1243                 do {
1244                         proto_tree_add_text(tree, tvb, offset, 1,
1245                             "Information element: %s",
1246                             val_to_str(tvb_get_guint8(tvb, offset), q931_info_element_vals0,
1247                               "Unknown (0x%02X)"));
1248                         offset += 1;
1249                         len -= 1;
1250                 } while (len != 0);
1251                 break;
1252
1253         case Q931_CAUSE_MT_NONEX_OR_UNIMPL:
1254         case Q931_CAUSE_MSG_INCOMPAT_W_CS:
1255                 proto_tree_add_text(tree, tvb, offset, 1,
1256                     "Message type: %s",
1257                     val_to_str(tvb_get_guint8(tvb, offset), q931_message_type_vals,
1258                       "Unknown (0x%02X)"));
1259                 break;
1260
1261         case Q931_CAUSE_REC_TIMER_EXP:
1262                 if (len < 3)
1263                         return;
1264                 proto_tree_add_text(tree, tvb, offset, 3,
1265                     "Timer: %.3s", tvb_get_ptr(tvb, offset, 3));
1266                 break;
1267
1268         default:
1269                 proto_tree_add_text(tree, tvb, offset, len,
1270                     "Diagnostics: %s",
1271                     tvb_bytes_to_str(tvb, offset, len));
1272         }
1273 }
1274
1275 /*
1276  * Dissect a Call state information element.
1277  */
1278 static const value_string q931_coding_standard_vals[] = {
1279         { 0x00, "ITU-T standardized coding" },
1280         { 0x20, "ISO/IEC standard" },
1281         { 0x40, "National standard" },
1282         { 0x60, "Standard defined for the network" },
1283         { 0,    NULL }
1284 };
1285
1286 static const value_string q931_call_state_vals[] = {
1287         { 0x00, "Null" },
1288         { 0x01, "Call initiated" },
1289         { 0x02, "Overlap sending" },
1290         { 0x03, "Outgoing call proceeding" },
1291         { 0x04, "Call delivered" },
1292         { 0x06, "Call present" },
1293         { 0x07, "Call received" },
1294         { 0x08, "Connect request" },
1295         { 0x09, "Incoming call proceeding" },
1296         { 0x0A, "Active" },
1297         { 0x0B, "Disconnect request" },
1298         { 0x0C, "Disconnect indication" },
1299         { 0x0F, "Suspend request" },
1300         { 0x12, "Resume request" },
1301         { 0x13, "Release request" },
1302         { 0x16, "Call abort"},
1303         { 0x19, "Overlap receiving" },
1304         { 0x3D, "Restart request" },
1305         { 0x3E, "Restart" },
1306         { 0,    NULL }
1307 };
1308
1309 static void
1310 dissect_q931_call_state_ie(tvbuff_t *tvb, int offset, int len,
1311     proto_tree *tree)
1312 {
1313         guint8 octet;
1314         guint8 coding_standard;
1315
1316         if (len == 0)
1317                 return;
1318         octet = tvb_get_guint8(tvb, offset);
1319         coding_standard = octet & 0x60;
1320         proto_tree_add_text(tree, tvb, offset, 1,
1321             "Coding standard: %s",
1322             val_to_str(coding_standard, q931_coding_standard_vals, NULL));
1323         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1324                 /*
1325                  * We don't know how the call state is encoded,
1326                  * so just dump it as data and be done with it.
1327                  */
1328                 proto_tree_add_text(tree, tvb, offset,
1329                     len, "Data: %s",
1330                     tvb_bytes_to_str(tvb, offset, len));
1331                 return;
1332         }
1333         proto_tree_add_text(tree, tvb, offset, 1,
1334             "Call state: %s",
1335             val_to_str(octet & 0x3F, q931_call_state_vals,
1336               "Unknown (0x%02X)"));
1337 }
1338
1339 /*
1340  * Dissect a Channel identification information element.
1341  */
1342 #define Q931_INTERFACE_IDENTIFIED       0x40
1343 #define Q931_NOT_BASIC_CHANNEL          0x20
1344
1345 static const value_string q931_basic_channel_selection_vals[] = {
1346         { 0x00, "No channel" },
1347         { 0x01, "B1 channel" },
1348         { 0x02, "B2 channel" },
1349         { 0x03, "Any channel" },
1350         { 0,    NULL }
1351 };
1352
1353 static const value_string q931_not_basic_channel_selection_vals[] = {
1354         { 0x00, "No channel" },
1355         { 0x01, "Channel indicated in following octets" },
1356         { 0x03, "Any channel" },
1357         { 0,    NULL }
1358 };
1359
1360 #define Q931_IS_SLOT_MAP                0x10
1361
1362 static const value_string q931_element_type_vals[] = {
1363         { 0x03, "B-channel units" },
1364         { 0x06, "H0-channel units" },
1365         { 0x08, "H11-channel units" },
1366         { 0x09, "H12-channel units" },
1367         { 0,    NULL }
1368 };
1369
1370 static void
1371 dissect_q931_channel_identification_ie(tvbuff_t *tvb, int offset, int len,
1372     proto_tree *tree)
1373 {
1374         guint8 octet;
1375         int identifier_offset;
1376         int identifier_len;
1377         guint8 coding_standard;
1378
1379         if (len == 0)
1380                 return;
1381         octet = tvb_get_guint8(tvb, offset);
1382         proto_tree_add_text(tree, tvb, offset, 1,
1383             "Interface %s identified",
1384             (octet & Q931_INTERFACE_IDENTIFIED) ? "explicitly" : "implicitly");
1385         proto_tree_add_text(tree, tvb, offset, 1,
1386             "%s interface",
1387             (octet & Q931_NOT_BASIC_CHANNEL) ? "Not basic" : "Basic");
1388         proto_tree_add_text(tree, tvb, offset, 1,
1389             "Indicated channel is %s",
1390             (octet & 0x08) ? "required" : "preferred");
1391         proto_tree_add_text(tree, tvb, offset, 1,
1392             "Indicated channel is %sthe D-channel",
1393             (octet & 0x04) ? "" : "not ");
1394         if (octet & Q931_NOT_BASIC_CHANNEL) {
1395                 proto_tree_add_text(tree, tvb, offset, 1,
1396                     "Channel selection: %s",
1397                     val_to_str(octet & 0x03, q931_not_basic_channel_selection_vals,
1398                       "Unknown (0x%X)"));
1399         } else {
1400                 proto_tree_add_text(tree, tvb, offset, 1,
1401                     "Channel selection: %s",
1402                     val_to_str(octet & 0x03, q931_basic_channel_selection_vals,
1403                       NULL));
1404         }
1405         offset += 1;
1406         len -= 1;
1407
1408         if (octet & Q931_INTERFACE_IDENTIFIED) {
1409                 identifier_offset = offset;
1410                 identifier_len = 0;
1411                 do {
1412                         if (len == 0)
1413                                 break;
1414                         octet = tvb_get_guint8(tvb, offset);
1415                         offset += 1;
1416                         len -= 1;
1417                         identifier_len++;
1418                 } while (!(octet & Q931_IE_VL_EXTENSION));
1419
1420                 /*
1421                  * XXX - do we want to strip off the 8th bit on the
1422                  * last octet of the interface identifier?
1423                  */
1424                 if (identifier_len != 0) {
1425                         proto_tree_add_text(tree, tvb, identifier_offset,
1426                             identifier_len, "Interface identifier: %s",
1427                             bytes_to_str(
1428                               tvb_get_ptr(tvb, identifier_offset, identifier_len),
1429                               identifier_len));
1430                 }
1431         }
1432
1433         if (octet & Q931_NOT_BASIC_CHANNEL) {
1434                 if (len == 0)
1435                         return;
1436                 octet = tvb_get_guint8(tvb, offset);
1437                 coding_standard = octet & 0x60;
1438                 proto_tree_add_text(tree, tvb, offset, 1,
1439                     "Coding standard: %s",
1440                     val_to_str(coding_standard, q931_coding_standard_vals,
1441                       NULL));
1442                 if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1443                         /*
1444                          * We don't know how the channel identifier is
1445                          * encoded, so just dump it as data and be done
1446                          * with it.
1447                          */
1448                         proto_tree_add_text(tree, tvb, offset,
1449                             len, "Data: %s",
1450                             tvb_bytes_to_str(tvb, offset, len));
1451                         return;
1452                 }
1453                 proto_tree_add_text(tree, tvb, offset, 1,
1454                     "Channel is indicated by %s",
1455                     (octet & Q931_IS_SLOT_MAP) ? "slot map" : "number");
1456                 proto_tree_add_text(tree, tvb, offset, 1,
1457                     "%s type: %s",
1458                     (octet & Q931_IS_SLOT_MAP) ? "Map element" : "Channel",
1459                     val_to_str(octet & 0x0F, q931_element_type_vals,
1460                         "Unknown (0x%02X)"));
1461
1462                 /*
1463                  * XXX - dump the channel number or slot map.
1464                  */
1465         }
1466 }
1467
1468 /*
1469  * Dissect a Progress indicator information element.
1470  */
1471 static const value_string q931_progress_description_vals[] = {
1472         { 0x01, "Call is not end-to-end ISDN - progress information available in-band" },
1473         { 0x02, "Destination address is non-ISDN" },
1474         { 0x03, "Origination address is non-ISDN" },
1475         { 0x04, "Call has returned to the ISDN" },
1476         { 0x05, "Interworking has occurred and has resulted in a telecommunications service change" },
1477         { 0x08, "In-band information or an appropriate pattern is now available" },
1478         { 0,    NULL }
1479 };
1480
1481 void
1482 dissect_q931_progress_indicator_ie(tvbuff_t *tvb, int offset, int len,
1483     proto_tree *tree)
1484 {
1485         guint8 octet;
1486         guint8 coding_standard;
1487
1488         if (len == 0)
1489                 return;
1490         octet = tvb_get_guint8(tvb, offset);
1491         coding_standard = octet & 0x60;
1492         proto_tree_add_text(tree, tvb, offset, 1,
1493             "Coding standard: %s",
1494             val_to_str(coding_standard, q931_cause_coding_standard_vals, NULL));
1495         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
1496                 /*
1497                  * We don't know how the progress indicator is encoded,
1498                  * so just dump it as data and be done with it.
1499                  */
1500                 proto_tree_add_text(tree, tvb, offset,
1501                     len, "Data: %s",
1502                     tvb_bytes_to_str(tvb, offset, len));
1503                 return;
1504         }
1505         proto_tree_add_text(tree, tvb, offset, 1,
1506             "Location: %s",
1507             val_to_str(octet & 0x0F, q931_cause_location_vals,
1508               "Unknown (0x%X)"));
1509         offset += 1;
1510         len -= 1;
1511
1512         if (len == 0)
1513                 return;
1514         octet = tvb_get_guint8(tvb, offset);
1515         proto_tree_add_text(tree, tvb, offset, 1,
1516             "Progress description: %s",
1517             val_to_str(octet & 0x7F, q931_progress_description_vals,
1518               "Unknown (0x%02X)"));
1519 }
1520
1521 /*
1522  * Dissect a Network-specific facilities or Transit network selection
1523  * information element.
1524  */
1525 static const value_string q931_netid_type_vals[] = {
1526         { 0x00, "User specified" },
1527         { 0x20, "National network identification" },
1528         { 0x30, "International network identification" },
1529         { 0,    NULL }
1530 };
1531
1532 static const value_string q931_netid_plan_vals[] = {
1533         { 0x00, "Unknown" },
1534         { 0x01, "Carrier Identification Code" },
1535         { 0x03, "X.121 data network identification code" },
1536         { 0,    NULL }
1537 };
1538
1539 static void
1540 dissect_q931_ns_facilities_ie(tvbuff_t *tvb, int offset, int len,
1541     proto_tree *tree)
1542 {
1543         guint8 octet;
1544         int netid_len;
1545
1546         if (len == 0)
1547                 return;
1548         octet = tvb_get_guint8(tvb, offset);
1549         netid_len = octet & 0x7F;
1550         proto_tree_add_text(tree, tvb, offset, 1,
1551             "Network identification length: %u",
1552             netid_len);
1553         offset += 1;
1554         len -= 1;
1555         if (netid_len != 0) {
1556                 if (len == 0)
1557                         return;
1558                 octet = tvb_get_guint8(tvb, offset);
1559                 proto_tree_add_text(tree, tvb, offset, 1,
1560                     "Type of network identification: %s",
1561                     val_to_str(octet & 0x70, q931_netid_type_vals,
1562                       "Unknown (0x%02X)"));
1563                 proto_tree_add_text(tree, tvb, offset, 1,
1564                     "Network identification plan: %s",
1565                     val_to_str(octet & 0x0F, q931_netid_plan_vals,
1566                       "Unknown (0x%02X)"));
1567                 offset += 1;
1568                 len -= 1;
1569                 netid_len--;
1570
1571                 if (len == 0)
1572                         return;
1573                 if (netid_len > len)
1574                         netid_len = len;
1575                 if (netid_len != 0) {
1576                         proto_tree_add_text(tree, tvb, offset, netid_len,
1577                             "Network identification: %s",
1578                             tvb_format_text(tvb, offset, netid_len));
1579                         offset += netid_len;
1580                         len -= netid_len;
1581                 }
1582         }
1583
1584         /*
1585          * Whatever is left is the network-specific facility
1586          * specification.
1587          */
1588          if (len == 0)
1589                 return;
1590         proto_tree_add_text(tree, tvb, offset,
1591             len, "Network-specific facility specification: %s",
1592             tvb_bytes_to_str(tvb, offset, len));
1593 }
1594
1595 /*
1596  * Dissect a Notification indicator information element.
1597  */
1598 static const value_string q931_notification_description_vals[] = {
1599         { 0x00, "User suspended" },
1600         { 0x01, "User resumed" },
1601         { 0x02, "Bearer service change" },
1602         { 0,    NULL }
1603 };
1604
1605 static void
1606 dissect_q931_notification_indicator_ie(tvbuff_t *tvb, int offset, int len,
1607     proto_tree *tree)
1608 {
1609         guint8 octet;
1610
1611         if (len == 0)
1612                 return;
1613         octet = tvb_get_guint8(tvb, offset);
1614         proto_tree_add_text(tree, tvb, offset, 1,
1615             "Notification description: %s",
1616             val_to_str(octet & 0x7F, q931_notification_description_vals,
1617               "Unknown (0x%02X)"));
1618 }
1619
1620 /*
1621  * Dissect a Date/time information element.
1622  */
1623 static void
1624 dissect_q931_date_time_ie(tvbuff_t *tvb, int offset, int len,
1625     proto_tree *tree)
1626 {
1627         if (len != 6) {
1628                 proto_tree_add_text(tree, tvb, offset, len,
1629                     "Date/time: length is %d, should be 6\n", len);
1630                 return;
1631         }
1632         /*
1633          * XXX - what is "year" relative to?  Is "month" 0-origin or
1634          * 1-origin?  Q.931 doesn't say....
1635          */
1636         proto_tree_add_text(tree, tvb, offset, 6,
1637             "Date/time: %02u-%02u-%02u %02u:%02u:%02u",
1638             tvb_get_guint8(tvb, offset + 0), tvb_get_guint8(tvb, offset + 1), tvb_get_guint8(tvb, offset + 2),
1639             tvb_get_guint8(tvb, offset + 3), tvb_get_guint8(tvb, offset + 4), tvb_get_guint8(tvb, offset + 5));
1640 }
1641
1642 /*
1643  * Dissect a Signal information element.
1644  */
1645 static const value_string q931_signal_vals[] = {
1646         { 0x00, "Dial tone on" },
1647         { 0x01, "Ring tone on" },
1648         { 0x02, "Intercept tone on" },
1649         { 0x03, "Network congestion tone on" }, /* "fast busy" */
1650         { 0x04, "Busy tone on" },
1651         { 0x05, "Confirm tone on" },
1652         { 0x06, "Answer tone on" },
1653         { 0x07, "Call waiting tone on" },
1654         { 0x08, "Off-hook warning tone on" },
1655         { 0x09, "Preemption tone on" },
1656         { 0x3F, "Tones off" },
1657         { 0x40, "Alerting on - pattern 0" },
1658         { 0x41, "Alerting on - pattern 1" },
1659         { 0x42, "Alerting on - pattern 2" },
1660         { 0x43, "Alerting on - pattern 3" },
1661         { 0x44, "Alerting on - pattern 4" },
1662         { 0x45, "Alerting on - pattern 5" },
1663         { 0x46, "Alerting on - pattern 6" },
1664         { 0x47, "Alerting on - pattern 7" },
1665         { 0x4F, "Alerting off" },
1666         { 0,    NULL }
1667 };
1668
1669 static void
1670 dissect_q931_signal_ie(tvbuff_t *tvb, int offset, int len,
1671     proto_tree *tree)
1672 {
1673         if (len != 1) {
1674                 proto_tree_add_text(tree, tvb, offset, len,
1675                     "Signal: length is %d, should be 1\n", len);
1676                 return;
1677         }
1678         proto_tree_add_text(tree, tvb, offset, 1,
1679             "Signal: %s",
1680             val_to_str(tvb_get_guint8(tvb, offset), q931_signal_vals,
1681                 "Unknown (0x%02X)"));
1682 }
1683
1684 /*
1685  * Dissect an Information rate information element.
1686  */
1687 static const value_string q931_throughput_class_vals[] = {
1688         { 0x03, "75 bit/s" },
1689         { 0x04, "150 bit/s" },
1690         { 0x05, "300 bit/s" },
1691         { 0x06, "600 bit/s" },
1692         { 0x07, "1200 bit/s" },
1693         { 0x08, "2400 bit/s" },
1694         { 0x09, "4800 bit/s" },
1695         { 0x0A, "9600 bit/s" },
1696         { 0x0B, "19200 bit/s" },
1697         { 0x0C, "48000 bit/s" },
1698         { 0x0D, "64000 bit/s" },
1699         { 0,    NULL }
1700 };
1701
1702 static void
1703 dissect_q931_information_rate_ie(tvbuff_t *tvb, int offset, int len,
1704     proto_tree *tree)
1705 {
1706         if (len != 4) {
1707                 proto_tree_add_text(tree, tvb, offset, len,
1708                     "Information rate: length is %d, should be 4\n", len);
1709                 return;
1710         }
1711         proto_tree_add_text(tree, tvb, offset + 0, 1,
1712             "Incoming information rate: %s",
1713             val_to_str(tvb_get_guint8(tvb, offset + 0) & 0x1F,
1714               q931_throughput_class_vals, "Unknown (0x%02X)"));
1715         proto_tree_add_text(tree, tvb, offset + 1, 1,
1716             "Outgoing information rate: %s",
1717             val_to_str(tvb_get_guint8(tvb, offset + 1) & 0x1F,
1718               q931_throughput_class_vals, "Unknown (0x%02X)"));
1719         proto_tree_add_text(tree, tvb, offset + 2, 1,
1720             "Minimum incoming information rate: %s",
1721             val_to_str(tvb_get_guint8(tvb, offset + 2) & 0x1F,
1722               q931_throughput_class_vals, "Unknown (0x%02X)"));
1723         proto_tree_add_text(tree, tvb, offset + 3, 1,
1724             "Minimum outgoing information rate: %s",
1725             val_to_str(tvb_get_guint8(tvb, offset + 3) & 0x1F,
1726               q931_throughput_class_vals, "Unknown (0x%02X)"));
1727 }
1728
1729 static int
1730 dissect_q931_guint16_value(tvbuff_t *tvb, int offset, int len,
1731     proto_tree *tree, char *label)
1732 {
1733         guint8 octet;
1734         guint16 value;
1735         int value_len;
1736
1737         value_len = 0;
1738
1739         octet = tvb_get_guint8(tvb, offset);
1740         if (octet & Q931_IE_VL_EXTENSION) {
1741                 /*
1742                  * Only one octet long - error.
1743                  */
1744                 goto bad_length;
1745         }
1746         value = (octet & 0x3) << 14;
1747         offset += 1;
1748         len -= 1;
1749         value_len++;
1750
1751         if (len == 0) {
1752                 /*
1753                  * We've reached the end of the information element - error.
1754                  */
1755                 goto past_end;
1756         }
1757         octet = tvb_get_guint8(tvb, offset);
1758         if (octet & Q931_IE_VL_EXTENSION) {
1759                 /*
1760                  * Only two octets long - error.
1761                  */
1762                 goto bad_length;
1763         }
1764         value |= (octet & 0x7F) << 7;
1765         offset += 1;
1766         len -= 1;
1767         value_len++;
1768
1769         if (len == 0) {
1770                 /*
1771                  * We've reached the end of the information element - error.
1772                  */
1773                 goto past_end;
1774         }
1775         octet = tvb_get_guint8(tvb, offset);
1776         if (!(octet & Q931_IE_VL_EXTENSION)) {
1777                 /*
1778                  * More than three octets long - error.
1779                  */
1780                 goto bad_length;
1781         }
1782         value |= (octet & 0x7F);
1783         offset += 1;
1784         len -= 1;
1785         value_len++;
1786
1787         proto_tree_add_text(tree, tvb, offset, value_len, "%s: %u ms", label,
1788             value);
1789         return value_len;
1790
1791 past_end:
1792         proto_tree_add_text(tree, tvb, offset, len,
1793             "%s goes past end of information element", label);
1794         return -1;
1795
1796 bad_length:
1797         proto_tree_add_text(tree, tvb, offset, len, "%s isn't 3 octets long",
1798             label);
1799         return -1;
1800 }
1801
1802 /*
1803  * Dissect an End-to-end transit delay information element.
1804  */
1805 static void
1806 dissect_q931_e2e_transit_delay_ie(tvbuff_t *tvb, int offset, int len,
1807     proto_tree *tree)
1808 {
1809         int value_len;
1810
1811         if (len == 0)
1812                 return;
1813         value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
1814             "Cumulative transit delay");
1815         if (value_len < 0)
1816                 return; /* error */
1817         offset += value_len;
1818         len -= value_len;
1819
1820         if (len == 0)
1821                 return;
1822         value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
1823             "Requested end-to-end transit delay");
1824         if (value_len < 0)
1825                 return; /* error */
1826         offset += value_len;
1827         len -= value_len;
1828
1829         if (len == 0)
1830                 return;
1831         value_len = dissect_q931_guint16_value(tvb, offset, len, tree,
1832             "Maximum end-to-end transit delay");
1833 }
1834
1835 /*
1836  * Dissect a Transit delay selection and indication information element.
1837  */
1838 static void
1839 dissect_q931_td_selection_and_int_ie(tvbuff_t *tvb, int offset, int len,
1840     proto_tree *tree)
1841 {
1842         if (len == 0)
1843                 return;
1844         dissect_q931_guint16_value(tvb, offset, len, tree,
1845             "Transit delay");
1846 }
1847
1848 /*
1849  * Dissect a Packet layer binary parameters information element.
1850  */
1851 static const value_string q931_fast_selected_vals[] = {
1852         { 0x00, "Fast select not requested" },
1853         { 0x08, "Fast select not requested" },
1854         { 0x10, "Fast select requested with no restriction of response" },
1855         { 0x18, "Fast select requested with restrictions of response" },
1856         { 0x00, NULL }
1857 };
1858
1859 static void
1860 dissect_q931_pl_binary_parameters_ie(tvbuff_t *tvb, int offset, int len,
1861     proto_tree *tree)
1862 {
1863         guint8 octet;
1864
1865         if (len == 0)
1866                 return;
1867         octet = tvb_get_guint8(tvb, offset);
1868         proto_tree_add_text(tree, tvb, offset, 1,
1869             "Fast select: %s",
1870             val_to_str(octet & 0x18, q931_fast_selected_vals, NULL));
1871         proto_tree_add_text(tree, tvb, offset, 1,
1872             "%s",
1873             (octet & 0x04) ? "No request/request denied" :
1874                              "Request indicated/request accepted");
1875         proto_tree_add_text(tree, tvb, offset, 1,
1876             "%s confirmation",
1877             (octet & 0x02) ? "Link-by-link" : "End-to-end");
1878         proto_tree_add_text(tree, tvb, offset, 1,
1879             "Modulus %u sequencing",
1880             (octet & 0x01) ? 8 : 128);
1881 }
1882
1883 /*
1884  * Dissect a Packet layer window size information element.
1885  */
1886 static void
1887 dissect_q931_pl_window_size_ie(tvbuff_t *tvb, int offset, int len,
1888     proto_tree *tree)
1889 {
1890         if (len == 0)
1891                 return;
1892         proto_tree_add_text(tree, tvb, offset, 1,
1893             "Forward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
1894         offset += 1;
1895         len -= 1;
1896
1897         if (len == 0)
1898                 return;
1899         proto_tree_add_text(tree, tvb, offset, 1,
1900             "Backward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
1901 }
1902
1903 /*
1904  * Dissect a Packet size information element.
1905  */
1906 static void
1907 dissect_q931_packet_size_ie(tvbuff_t *tvb, int offset, int len,
1908     proto_tree *tree)
1909 {
1910         if (len == 0)
1911                 return;
1912         proto_tree_add_text(tree, tvb, offset, 1,
1913             "Forward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
1914         offset += 1;
1915         len -= 1;
1916
1917         if (len == 0)
1918                 return;
1919         proto_tree_add_text(tree, tvb, offset, 1,
1920             "Backward value: %u", tvb_get_guint8(tvb, offset) & 0x7F);
1921 }
1922
1923 /*
1924  * Dissect a Closed user group information element.
1925  */
1926 static const value_string q931_cug_indication_vals[] = {
1927         { 0x01, "Closed user group selection" },
1928         { 0x02, "Closed user group with outgoing access selection and indication" },
1929         { 0,    NULL }
1930 };
1931
1932 static void
1933 dissect_q931_cug_ie(tvbuff_t *tvb, int offset, int len, proto_tree *tree)
1934 {
1935         if (len == 0)
1936                 return;
1937         proto_tree_add_text(tree, tvb, offset, 1,
1938             "CUG indication: %s",
1939             val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
1940               q931_cug_indication_vals, "Unknown (0x%02X)"));
1941         offset += 1;
1942         len -= 1;
1943
1944         if (len == 0)
1945                 return;
1946         proto_tree_add_text(tree, tvb, offset, len, "CUG index code: %s",
1947             tvb_format_text(tvb, offset, len));
1948 }
1949
1950 /*
1951  * Dissect a Reverse charging indication information element.
1952  */
1953 static const value_string q931_reverse_charging_indication_vals[] = {
1954         { 0x01, "Reverse charging requested" },
1955         { 0,    NULL }
1956 };
1957
1958 static void
1959 dissect_q931_reverse_charge_ind_ie(tvbuff_t *tvb, int offset, int len,
1960     proto_tree *tree)
1961 {
1962         if (len == 0)
1963                 return;
1964         proto_tree_add_text(tree, tvb, offset, 1,
1965             "Reverse charging indication: %s",
1966             val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
1967               q931_reverse_charging_indication_vals, "Unknown (0x%02X)"));
1968 }
1969
1970 /*
1971  * Dissect a (phone) number information element.
1972  */
1973 static const value_string q931_number_type_vals[] = {
1974         { 0x00, "Unknown" },
1975         { 0x10, "International number" },
1976         { 0x20, "National number" },
1977         { 0x30, "Network specific number" },
1978         { 0x40, "Subscriber number" },
1979         { 0x60, "Abbreviated number" },
1980         { 0,    NULL }
1981 };
1982
1983 static const value_string q931_numbering_plan_vals[] = {
1984         { 0x00, "Unknown" },
1985         { 0x01, "E.164 ISDN/telephony numbering" },
1986         { 0x03, "X.121 data numbering" },
1987         { 0x04, "F.69 Telex numbering" },
1988         { 0x08, "National standard numbering" },
1989         { 0x09, "Private numbering" },
1990         { 0,    NULL }
1991 };
1992
1993 static const value_string q931_presentation_indicator_vals[] = {
1994         { 0x00, "Presentation allowed" },
1995         { 0x20, "Presentation restricted" },
1996         { 0x40, "Number not available due to interworking" },
1997         { 0,    NULL }
1998 };
1999
2000 static const value_string q931_screening_indicator_vals[] = {
2001         { 0x00, "User-provided, not screened" },
2002         { 0x01, "User-provided, verified and passed" },
2003         { 0x02, "User-provided, verified and failed" },
2004         { 0x03, "Network-provided" },
2005         { 0,    NULL }
2006 };
2007
2008 static const value_string q931_redirection_reason_vals[] = {
2009         { 0x00, "Unknown" },
2010         { 0x01, "Call forwarding busy or called DTE busy" },
2011         { 0x02, "Call forwarding no reply" },
2012         { 0x04, "Call deflection" },
2013         { 0x09, "Called DTE out of order" },
2014         { 0x0A, "Call forwarding by the called DTE" },
2015         { 0x0F, "Call forwarding unconditional or systematic call redirection" },
2016         { 0,    NULL }
2017 };
2018
2019 static void
2020 dissect_q931_number_ie(tvbuff_t *tvb, int offset, int len,
2021     proto_tree *tree, int hfindex)
2022 {
2023         guint8 octet;
2024
2025         if (len == 0)
2026                 return;
2027         octet = tvb_get_guint8(tvb, offset);
2028         proto_tree_add_text(tree, tvb, offset, 1,
2029             "Type of number: %s",
2030             val_to_str(octet & 0x70, q931_number_type_vals,
2031               "Unknown (0x%02X)"));
2032         proto_tree_add_text(tree, tvb, offset, 1,
2033             "Numbering plan: %s",
2034             val_to_str(octet & 0x0F, q931_numbering_plan_vals,
2035               "Unknown (0x%02X)"));
2036         offset += 1;
2037         len -= 1;
2038
2039         if (!(octet & Q931_IE_VL_EXTENSION)) {
2040                 if (len == 0)
2041                         return;
2042                 octet = tvb_get_guint8(tvb, offset);
2043                 proto_tree_add_text(tree, tvb, offset, 1,
2044                     "Presentation indicator: %s",
2045                     val_to_str(octet & 0x60, q931_presentation_indicator_vals,
2046                       "Unknown (0x%X)"));
2047                 proto_tree_add_text(tree, tvb, offset, 1,
2048                     "Screening indicator: %s",
2049                     val_to_str(octet & 0x03, q931_screening_indicator_vals,
2050                       "Unknown (0x%X)"));
2051                 offset += 1;
2052                 len -= 1;
2053         }
2054
2055         /*
2056          * XXX - only in a Redirecting number information element.
2057          */
2058         if (!(octet & Q931_IE_VL_EXTENSION)) {
2059                 if (len == 0)
2060                         return;
2061                 octet = tvb_get_guint8(tvb, offset);
2062                 proto_tree_add_text(tree, tvb, offset, 1,
2063                     "Reason for redirection: %s",
2064                     val_to_str(octet & 0x0F, q931_redirection_reason_vals,
2065                       "Unknown (0x%X)"));
2066                 offset += 1;
2067                 len -= 1;
2068         }
2069
2070         if (len == 0)
2071                 return;
2072         proto_tree_add_item(tree, hfindex, tvb, offset, len, FALSE);
2073 }
2074
2075 /*
2076  * Dissect a party subaddress information element.
2077  */
2078 static const value_string q931_subaddress_type_vals[] = {
2079         { 0x00, "X.213/ISO 8348 Add.2 NSAP" },
2080         { 0x20, "User-specified" },
2081         { 0,    NULL }
2082 };
2083
2084 static const value_string q931_odd_even_indicator_vals[] = {
2085         { 0x00, "Even number of address signals" },
2086         { 0x10, "Odd number of address signals" },
2087         { 0,    NULL }
2088 };
2089
2090 static void
2091 dissect_q931_party_subaddr_ie(tvbuff_t *tvb, int offset, int len,
2092     proto_tree *tree)
2093 {
2094         guint8 octet;
2095
2096         if (len == 0)
2097                 return;
2098         octet = tvb_get_guint8(tvb, offset);
2099         proto_tree_add_text(tree, tvb, offset, 1,
2100             "Type of subaddress: %s",
2101             val_to_str(octet & 0x70, q931_subaddress_type_vals,
2102               "Unknown (0x%02X)"));
2103         proto_tree_add_text(tree, tvb, offset, 1,
2104             "Odd/even indicator: %s",
2105             val_to_str(octet & 0x10, q931_odd_even_indicator_vals,
2106               NULL));
2107         offset += 1;
2108         len -= 1;
2109
2110         if (len == 0)
2111                 return;
2112         proto_tree_add_text(tree, tvb, offset, len, "Subaddress: %s",
2113             tvb_bytes_to_str(tvb, offset, len));
2114 }
2115
2116 /*
2117  * Dissect a Restart indicator information element.
2118  */
2119 static const value_string q931_restart_indicator_class_vals[] = {
2120         { 0x00, "Indicated channels" },
2121         { 0x06, "Single interface" },
2122         { 0x07, "All interfaces" },
2123         { 0,    NULL }
2124 };
2125
2126 static void
2127 dissect_q931_restart_indicator_ie(tvbuff_t *tvb, int offset, int len,
2128     proto_tree *tree)
2129 {
2130         if (len != 1) {
2131                 proto_tree_add_text(tree, tvb, offset, len,
2132                     "Restart indicator: length is %d, should be 1\n", len);
2133                 return;
2134         }
2135         proto_tree_add_text(tree, tvb, offset, 1,
2136             "Restart indicator: %s",
2137             val_to_str(tvb_get_guint8(tvb, offset) & 0x07,
2138               q931_restart_indicator_class_vals, "Unknown (0x%02X)"));
2139 }
2140
2141 /*
2142  * Dissect a High-layer compatibility information element.
2143  */
2144 #define Q931_AUDIOVISUAL        0x60
2145 static const value_string q931_high_layer_characteristics_vals[] = {
2146         { 0x01,             "Telephony" },
2147         { 0x04,             "F.182 Facsimile Group 2/3" },
2148         { 0x21,             "F.184 Facsimile Group 4 Class I" },
2149         { 0x24,             "F.230 Teletex, basic and mixed mode, and F.184 Facsimile Group 4, Classes II and III" },
2150         { 0x28,             "F.220 Teletex, basic and processable mode" },
2151         { 0x31,             "F.200 Teletex, basic mode" },
2152         { 0x32,             "F.300 and T.102 syntax-based Videotex" },
2153         { 0x33,             "F.300 and T.101 international Videotex interworking" },
2154         { 0x35,             "F.60 Telex" },
2155         { 0x38,             "X.400 Message Handling Systems" },
2156         { 0x41,             "X.200 OSI application" },
2157         { 0x42,             "FTAM application" },
2158         { 0x5E,             "Reserved for maintenance" },
2159         { 0x5F,             "Reserved for management" },
2160         { Q931_AUDIOVISUAL, "F.720/F.821 and F.731 Profile 1a videotelephony" },
2161         { 0x61,             "F.702 and F.731 Profile 1b videoconferencing" },
2162         { 0x62,             "F.702 and F.731 audiographic conferencing" },
2163         { 0,                NULL }
2164 };
2165
2166 static const value_string q931_audiovisual_characteristics_vals[] = {
2167         { 0x01, "Capability set of initial channel of H.221" },
2168         { 0x02, "Capability set of subsequent channel of H.221" },
2169         { 0x21, "Capability set of initial channel of an active 3.1kHz audio or speech call" },
2170         { 0x00, NULL }
2171 };
2172
2173 void
2174 dissect_q931_high_layer_compat_ie(tvbuff_t *tvb, int offset, int len,
2175     proto_tree *tree)
2176 {
2177         guint8 octet;
2178         guint8 coding_standard;
2179         guint8 characteristics;
2180
2181         if (len == 0)
2182                 return;
2183         octet = tvb_get_guint8(tvb, offset);
2184         coding_standard = octet & 0x60;
2185         proto_tree_add_text(tree, tvb, offset, 1,
2186             "Coding standard: %s",
2187             val_to_str(coding_standard, q931_coding_standard_vals, NULL));
2188         offset += 1;
2189         len -= 1;
2190         if (coding_standard != Q931_ITU_STANDARDIZED_CODING) {
2191                 /*
2192                  * We don't know how the call state is encoded,
2193                  * so just dump it as data and be done with it.
2194                  */
2195                 proto_tree_add_text(tree, tvb, offset,
2196                     len, "Data: %s",
2197                     tvb_bytes_to_str(tvb, offset, len));
2198                 return;
2199         }
2200
2201         if (len == 0)
2202                 return;
2203         octet = tvb_get_guint8(tvb, offset);
2204         characteristics = octet & 0x7F;
2205         proto_tree_add_text(tree, tvb, offset, 1,
2206             "High layer characteristics identification: %s",
2207             val_to_str(characteristics, q931_high_layer_characteristics_vals,
2208              "Unknown (0x%02X)"));
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                 if (characteristics == Q931_AUDIOVISUAL) {
2217                         proto_tree_add_text(tree, tvb, offset, 1,
2218                             "Extended audiovisual characteristics identification: %s",
2219                             val_to_str(octet & 0x7F,
2220                               q931_audiovisual_characteristics_vals,
2221                               "Unknown (0x%02X)"));
2222                 } else {
2223                         proto_tree_add_text(tree, tvb, offset, 1,
2224                             "Extended high layer characteristics identification: %s",
2225                             val_to_str(octet & 0x7F,
2226                               q931_high_layer_characteristics_vals,
2227                               "Unknown (0x%02X)"));
2228                 }
2229         }
2230 }
2231
2232
2233 /*
2234  * Dissect a User-user information element.
2235  */
2236 #define Q931_PROTOCOL_DISCRIMINATOR_IA5         0x04
2237 #define Q931_PROTOCOL_DISCRIMINATOR_ASN1        0x05
2238
2239 static const value_string q931_protocol_discriminator_vals[] = {
2240         { 0x00,                                 "User-specific protocol" },
2241         { 0x01,                                 "OSI high layer protocols" },
2242         { 0x02,                                 "X.244" },
2243         { Q931_PROTOCOL_DISCRIMINATOR_IA5,      "IA5 characters" },
2244         { Q931_PROTOCOL_DISCRIMINATOR_ASN1,     "X.208 and X.209 coded user information" },
2245         { 0x07,                                 "V.120 rate adaption" },
2246         { 0x08,                                 "Q.931/I.451 user-network call control messages" },
2247         { 0,                                    NULL }
2248 };
2249
2250 void
2251 dissect_q931_user_user_ie(tvbuff_t *tvb, int offset, int len,
2252     proto_tree *tree)
2253 {
2254         guint8 octet;
2255
2256         if (len == 0)
2257                 return;
2258         octet = tvb_get_guint8(tvb, offset);
2259         proto_tree_add_text(tree, tvb, offset, 1,
2260             "Protocol discriminator: %s",
2261             val_to_str(octet, q931_protocol_discriminator_vals,
2262             "Unknown (0x%02x)"));
2263         offset += 1;
2264         len -= 1;
2265
2266         if (len == 0)
2267                 return;
2268         switch (octet) {
2269
2270         case Q931_PROTOCOL_DISCRIMINATOR_IA5:
2271                 proto_tree_add_text(tree, tvb, offset, len, "User information: %s",
2272                     tvb_format_text(tvb, offset, len));
2273                 break;
2274
2275         default:
2276                 proto_tree_add_text(tree, tvb, offset, len, "User information: %s",
2277                     tvb_bytes_to_str(tvb, offset, len));
2278                 break;
2279         }
2280 }
2281
2282 /*
2283  * Dissect information elements consisting of ASCII^H^H^H^H^HIA5 text.
2284  */
2285 static void
2286 dissect_q931_ia5_ie(tvbuff_t *tvb, int offset, int len, proto_tree *tree,
2287     char *label)
2288 {
2289         if (len != 0) {
2290                 proto_tree_add_text(tree, tvb, offset, len, "%s: %s", label,
2291                     tvb_format_text(tvb, offset, len));
2292         }
2293 }
2294
2295 static const value_string q931_codeset_vals[] = {
2296         { 0x00, "Q.931 information elements" },
2297         { 0x04, "Information elements for ISO/IEC use" },
2298         { 0x05, "Information elements for national use" },
2299         { 0x06, "Information elements specific to the local network" },
2300         { 0x07, "User-specific information elements" },
2301         { 0x00, NULL },
2302 };
2303
2304 static void
2305 dissect_q931_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
2306     gboolean is_tpkt)
2307 {
2308         int             offset = 0;
2309         proto_tree      *q931_tree = NULL;
2310         proto_item      *ti;
2311         proto_tree      *ie_tree = NULL;
2312         guint8          call_ref_len;
2313         guint8          call_ref[15];
2314         guint8          message_type;
2315         guint8          info_element;
2316         guint16         info_element_len;
2317         int             codeset, locked_codeset;
2318         gboolean        non_locking_shift;
2319         tvbuff_t        *h225_tvb, *next_tvb;
2320
2321         if (check_col(pinfo->cinfo, COL_PROTOCOL))
2322                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Q.931");
2323
2324         if (tree) {
2325                 ti = proto_tree_add_item(tree, proto_q931, tvb, offset, -1,
2326                     FALSE);
2327                 q931_tree = proto_item_add_subtree(ti, ett_q931);
2328
2329                 dissect_q931_protocol_discriminator(tvb, offset, q931_tree);
2330         }
2331         offset += 1;
2332         call_ref_len = tvb_get_guint8(tvb, offset) & 0xF;       /* XXX - do as a bit field? */
2333         if (q931_tree != NULL)
2334                 proto_tree_add_uint(q931_tree, hf_q931_call_ref_len, tvb, offset, 1, call_ref_len);
2335         offset += 1;
2336         if (call_ref_len != 0) {
2337                 tvb_memcpy(tvb, call_ref, offset, call_ref_len);
2338                 if (q931_tree != NULL) {
2339                         proto_tree_add_boolean(q931_tree, hf_q931_call_ref_flag,
2340                             tvb, offset, 1, (call_ref[0] & 0x80) != 0);
2341                         call_ref[0] &= 0x7F;
2342                         proto_tree_add_bytes(q931_tree, hf_q931_call_ref,
2343                             tvb, offset, call_ref_len, call_ref);
2344                 }
2345                 offset += call_ref_len;
2346         }
2347         message_type = tvb_get_guint8(tvb, offset);
2348         if (check_col(pinfo->cinfo, COL_INFO)) {
2349                 col_add_str(pinfo->cinfo, COL_INFO,
2350                     val_to_str(message_type, q931_message_type_vals,
2351                       "Unknown message type (0x%02X)"));
2352         }
2353         if (q931_tree != NULL)
2354                 proto_tree_add_uint(q931_tree, hf_q931_message_type, tvb, offset, 1, message_type);
2355         offset += 1;
2356
2357         /*
2358          * And now for the information elements....
2359          */
2360         codeset = locked_codeset = 0;   /* start out in codeset 0 */
2361         non_locking_shift = TRUE;
2362         while (tvb_reported_length_remaining(tvb, offset) > 0) {
2363                 info_element = tvb_get_guint8(tvb, offset);
2364
2365                  /* Check for the codeset shift */
2366                 if ((info_element & Q931_IE_SO_MASK) &&
2367                     ((info_element & Q931_IE_SO_IDENTIFIER_MASK) == Q931_IE_SHIFT)) {
2368                         non_locking_shift = info_element & Q931_IE_SHIFT_NON_LOCKING;
2369                         codeset = info_element & Q931_IE_SHIFT_CODESET;
2370                         if (!non_locking_shift)
2371                                 locked_codeset = codeset;
2372                         if (q931_tree != NULL) {
2373                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2374                                     "%s shift to codeset %u: %s",
2375                                     (non_locking_shift ? "Non-locking" : "Locking"),
2376                                     codeset,
2377                                     val_to_str(codeset, q931_codeset_vals,
2378                                       "Unknown (0x%02X)"));
2379                         }
2380                         offset += 1;
2381                         continue;
2382                 }
2383
2384                 /*
2385                  * Check for the single-octet IEs.
2386                  */
2387                 if (info_element & Q931_IE_SO_MASK) {
2388                         /*
2389                          * Check for subdissectors for this IE or
2390                          * for all IEs in this codeset.
2391                          */
2392                         if (dissector_get_port_handle(codeset_dissector_table, codeset) ||
2393                             dissector_get_port_handle(ie_dissector_table, (codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK))) {
2394                                 next_tvb = tvb_new_subset (tvb, offset, 1, 1);
2395                                 if (dissector_try_port(ie_dissector_table, (codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK), next_tvb, pinfo, q931_tree) ||
2396                                     dissector_try_port(codeset_dissector_table, codeset, next_tvb, pinfo, q931_tree)) {
2397                                         offset += 1;
2398                                         codeset = locked_codeset;
2399                                         continue;
2400                                 }
2401                         }
2402
2403                         switch ((codeset << 8) | (info_element & Q931_IE_SO_IDENTIFIER_MASK)) {
2404
2405                         case CS0 | Q931_IE_MORE_DATA_OR_SEND_COMP:
2406                                 switch (info_element) { 
2407
2408                                 case Q931_IE_MORE_DATA:
2409                                         if (q931_tree != NULL) {
2410                                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2411                                                     "More data");
2412                                         }
2413                                         break;
2414
2415                                 case Q931_IE_SENDING_COMPLETE:
2416                                         if (q931_tree != NULL) {
2417                                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2418                                                     "Sending complete");
2419                                         }
2420                                         break;
2421
2422                                 default:
2423                                         if (q931_tree != NULL) {
2424                                                 proto_tree_add_text(q931_tree, tvb, offset, 1,
2425                                                     "Unknown information element (0x%02X)",
2426                                                     info_element);
2427                                         }
2428                                         break;
2429                                 }
2430                                 break;
2431
2432                         case CS0 | Q931_IE_CONGESTION_LEVEL:
2433                                 if (q931_tree != NULL) {
2434                                         proto_tree_add_text(q931_tree, tvb, offset, 1,
2435                                             "Congestion level: %s",
2436                                             val_to_str(info_element & Q931_IE_SO_IE_MASK,
2437                                               q931_congestion_level_vals,
2438                                               "Unknown (0x%X)"));
2439                                 }
2440                                 break;
2441
2442                         case CS0 | Q931_IE_REPEAT_INDICATOR:
2443                                 if (q931_tree != NULL) {
2444                                         proto_tree_add_text(q931_tree, tvb, offset, 1,
2445                                             "Repeat indicator: %s",
2446                                             val_to_str(info_element & Q931_IE_SO_IE_MASK,
2447                                           q931_repeat_indication_vals,
2448                                               "Unknown (0x%X)"));
2449                                 }
2450                                 break;
2451
2452                         default:
2453                                 if (q931_tree != NULL) {
2454                                         proto_tree_add_text(q931_tree, tvb, offset, 1,
2455                                             "Unknown information element (0x%02X)",
2456                                             info_element);
2457                                 }
2458                                 break;
2459                         }
2460                         offset += 1;
2461                         codeset = locked_codeset;
2462                         continue;
2463                 }
2464
2465                 /*
2466                  * Variable-length IE.
2467                  *
2468                  * According to page 18 from Recommendation H.225.0 :
2469                  * " Length of user-user contents contents
2470                  * - Shall be 2 octets instead of 1 (as in Figure 4-36/Q.931)"
2471                  *
2472                  * We assume that if this is Q.931-over-TPKT, it might
2473                  * be H.225 traffic, and check for the IE being a user-user
2474                  * IE with ASN.1 encoding of the user information.
2475                  */
2476                 if (is_tpkt && tvb_bytes_exist(tvb, offset, 4) &&
2477                     codeset == 0 && tvb_get_guint8(tvb, offset) == Q931_IE_USER_USER &&
2478                     tvb_get_guint8(tvb, offset + 3) == Q931_PROTOCOL_DISCRIMINATOR_ASN1)  {
2479                         info_element_len = tvb_get_ntohs(tvb, offset + 1);
2480                         if (q931_tree != NULL) {
2481                                 ti = proto_tree_add_text(q931_tree, tvb, offset,
2482                                     1+2+info_element_len, "%s",
2483                                     val_to_str(info_element,
2484                                       q931_info_element_vals[codeset],
2485                                       "Unknown information element (0x%02X)"));
2486                                 ie_tree = proto_item_add_subtree(ti,
2487                                     ett_q931_ie);
2488                                 proto_tree_add_text(ie_tree, tvb, offset, 1,
2489                                     "Information element: %s",
2490                                     val_to_str(info_element,
2491                                       q931_info_element_vals[codeset], "Unknown (0x%02X)"));
2492                                 proto_tree_add_text(ie_tree, tvb, offset + 1,
2493                                     2, "Length: %u", info_element_len);
2494                                 proto_tree_add_text(ie_tree, tvb, offset + 3,
2495                                     1, "Protocol discriminator: %s",
2496                                     val_to_str(tvb_get_guint8(tvb, offset + 3),
2497                                       q931_protocol_discriminator_vals,
2498                                       "Unknown (0x%02x)"));
2499                         }
2500
2501                         if (info_element_len > 1) {
2502                                 /*
2503                                  * Do we have a handle for the H.225
2504                                  * dissector?
2505                                  */
2506                                 if (h225_handle != NULL) {
2507                                         /*
2508                                          * Yes - call it, regardless of
2509                                          * whether we're building a
2510                                          * protocol tree or not.
2511                                          */
2512                                         h225_tvb = tvb_new_subset(tvb,
2513                                             offset + 4, info_element_len - 1,
2514                                             info_element_len - 1);
2515                                         call_dissector(h225_handle, h225_tvb,
2516                                             pinfo, tree);
2517                                 } else {
2518                                         /*
2519                                          * No - just show it as "User
2520                                          * information" (if "ie_tree" is
2521                                          * null, this won't add anything).
2522                                          */
2523                                         proto_tree_add_text(ie_tree, tvb,
2524                                             offset + 4, info_element_len - 1,
2525                                             "User information: %s",
2526                                             tvb_bytes_to_str(tvb, offset + 4,
2527                                               info_element_len - 1));
2528                                 }
2529                         }
2530                         offset += 1 + 2 + info_element_len;
2531                 } else {
2532                         info_element_len = tvb_get_guint8(tvb, offset + 1);
2533
2534                         /*
2535                          * Check for subdissectors for this IE or
2536                          * for all IEs in this codeset.
2537                          */
2538                         if (dissector_get_port_handle(codeset_dissector_table, codeset) ||
2539                             dissector_get_port_handle(ie_dissector_table, (codeset << 8) | info_element)) {
2540                                 next_tvb = tvb_new_subset (tvb, offset, info_element_len + 2, info_element_len + 2);
2541                                 if (dissector_try_port(ie_dissector_table, (codeset << 8) | info_element, next_tvb, pinfo, q931_tree) ||
2542                                     dissector_try_port(codeset_dissector_table, codeset, next_tvb, pinfo, q931_tree)) {
2543                                         offset += 2 + info_element_len;
2544                                         codeset = locked_codeset;
2545                                         continue;
2546                                 }
2547                         }
2548
2549                         if (q931_tree != NULL) {
2550                                 ti = proto_tree_add_text(q931_tree, tvb, offset,
2551                                     1+1+info_element_len, "%s",
2552                                     val_to_str(info_element, q931_info_element_vals[codeset],
2553                                       "Unknown information element (0x%02X)"));
2554                                         ie_tree = proto_item_add_subtree(ti, ett_q931_ie);
2555                                 proto_tree_add_text(ie_tree, tvb, offset, 1,
2556                                     "Information element: %s",
2557                                     val_to_str(info_element, q931_info_element_vals[codeset],
2558                                       "Unknown (0x%02X)"));
2559                                 proto_tree_add_text(ie_tree, tvb, offset + 1, 1,
2560                                     "Length: %u", info_element_len);
2561
2562                                 switch ((codeset << 8) | info_element) {
2563
2564                                 case CS0 | Q931_IE_SEGMENTED_MESSAGE:
2565                                         dissect_q931_segmented_message_ie(tvb,
2566                                             offset + 2, info_element_len,
2567                                             ie_tree);
2568                                         break;
2569
2570                                 case CS0 | Q931_IE_BEARER_CAPABILITY:
2571                                 case CS0 | Q931_IE_LOW_LAYER_COMPAT:
2572                                         dissect_q931_bearer_capability_ie(tvb,
2573                                             offset + 2, info_element_len,
2574                                             ie_tree);
2575                                         break;
2576
2577                                 case CS0 | Q931_IE_CAUSE:
2578                                         dissect_q931_cause_ie(tvb,
2579                                             offset + 2, info_element_len,
2580                                             ie_tree,
2581                                             hf_q931_cause_value);
2582                                         break;
2583
2584                                 case CS0 | Q931_IE_CALL_STATE:
2585                                         dissect_q931_call_state_ie(tvb,
2586                                             offset + 2, info_element_len,
2587                                             ie_tree);
2588                                         break;
2589
2590                                 case CS0 | Q931_IE_CHANNEL_IDENTIFICATION:
2591                                         dissect_q931_channel_identification_ie(
2592                                             tvb, offset + 2, info_element_len,
2593                                             ie_tree);
2594                                         break;
2595
2596                                 case CS0 | Q931_IE_PROGRESS_INDICATOR:
2597                                         dissect_q931_progress_indicator_ie(tvb,
2598                                             offset + 2, info_element_len,
2599                                             ie_tree);
2600                                         break;
2601
2602                                 case CS0 | Q931_IE_NETWORK_SPECIFIC_FACIL:
2603                                 case CS0 | Q931_IE_TRANSIT_NETWORK_SEL:
2604                                         dissect_q931_ns_facilities_ie(tvb,
2605                                             offset + 2, info_element_len,
2606                                             ie_tree);
2607                                         break;
2608
2609                                 case CS0 | Q931_IE_NOTIFICATION_INDICATOR:
2610                                         dissect_q931_notification_indicator_ie(
2611                                             tvb, offset + 2, info_element_len,
2612                                             ie_tree);
2613                                         break;
2614
2615                                 case CS0 | Q931_IE_DISPLAY:
2616                                         dissect_q931_ia5_ie(tvb, offset + 2,
2617                                             info_element_len, ie_tree,
2618                                             "Display information");
2619                                         break;
2620
2621                                 case CS0 | Q931_IE_DATE_TIME:
2622                                         dissect_q931_date_time_ie(tvb,
2623                                             offset + 2, info_element_len,
2624                                             ie_tree);
2625                                         break;
2626
2627                                 case CS0 | Q931_IE_KEYPAD_FACILITY:
2628                                         dissect_q931_ia5_ie(tvb, offset + 2,
2629                                             info_element_len, ie_tree,
2630                                             "Keypad facility");
2631                                         break;
2632
2633                                 case CS0 | Q931_IE_SIGNAL:
2634                                         dissect_q931_signal_ie(tvb,
2635                                             offset + 2, info_element_len,
2636                                             ie_tree);
2637                                         break;
2638
2639                                 case CS0 | Q931_IE_INFORMATION_RATE:
2640                                         dissect_q931_information_rate_ie(tvb,
2641                                             offset + 2, info_element_len,
2642                                             ie_tree);
2643                                         break;
2644
2645                                 case CS0 | Q931_IE_E2E_TRANSIT_DELAY:
2646                                         dissect_q931_e2e_transit_delay_ie(tvb,
2647                                             offset + 2, info_element_len,
2648                                             ie_tree);
2649                                         break;
2650
2651                                 case CS0 | Q931_IE_TD_SELECTION_AND_INT:
2652                                         dissect_q931_td_selection_and_int_ie(
2653                                             tvb, offset + 2, info_element_len,
2654                                             ie_tree);
2655                                         break;
2656
2657                                 case CS0 | Q931_IE_PL_BINARY_PARAMETERS:
2658                                         dissect_q931_pl_binary_parameters_ie(
2659                                             tvb, offset + 2, info_element_len,
2660                                             ie_tree);
2661                                         break;
2662
2663                                 case CS0 | Q931_IE_PL_WINDOW_SIZE:
2664                                         dissect_q931_pl_window_size_ie(tvb,
2665                                             offset + 2, info_element_len,
2666                                             ie_tree);
2667                                         break;
2668
2669                                 case CS0 | Q931_IE_PACKET_SIZE:
2670                                         dissect_q931_packet_size_ie(tvb,
2671                                             offset + 2, info_element_len,
2672                                             ie_tree);
2673                                         break;
2674
2675                                 case CS0 | Q931_IE_CUG:
2676                                         dissect_q931_cug_ie(tvb,
2677                                             offset + 2, info_element_len,
2678                                             ie_tree);
2679                                         break;
2680
2681                                 case CS0 | Q931_IE_REVERSE_CHARGE_IND:
2682                                         dissect_q931_reverse_charge_ind_ie(tvb,
2683                                             offset + 2, info_element_len,
2684                                             ie_tree);
2685                                         break;
2686
2687                                 case CS0 | Q931_IE_CALLING_PARTY_NUMBER:
2688                                         dissect_q931_number_ie(tvb,
2689                                             offset + 2, info_element_len,
2690                                             ie_tree,
2691                                             hf_q931_calling_party_number);
2692                                         break;
2693
2694                                 case CS0 | Q931_IE_CONNECTED_NUMBER_DEFAULT:
2695                                         dissect_q931_number_ie(tvb,
2696                                             offset + 2, info_element_len,
2697                                             ie_tree,
2698                                             hf_q931_connected_number);
2699                                         break;
2700
2701                                 case CS0 | Q931_IE_CALLED_PARTY_NUMBER:
2702                                         dissect_q931_number_ie(tvb,
2703                                             offset + 2, info_element_len,
2704                                             ie_tree,
2705                                             hf_q931_called_party_number);
2706                                         break;
2707
2708                                 case CS0 | Q931_IE_REDIRECTING_NUMBER:
2709                                         dissect_q931_number_ie(tvb,
2710                                             offset + 2, info_element_len,
2711                                             ie_tree,
2712                                             hf_q931_redirecting_number);
2713                                         break;
2714
2715                                 case CS0 | Q931_IE_CALLING_PARTY_SUBADDR:
2716                                 case CS0 | Q931_IE_CALLED_PARTY_SUBADDR:
2717                                         dissect_q931_party_subaddr_ie(tvb,
2718                                             offset + 2, info_element_len,
2719                                             ie_tree);
2720                                         break;
2721
2722                                 case CS0 | Q931_IE_RESTART_INDICATOR:
2723                                         dissect_q931_restart_indicator_ie(tvb,
2724                                             offset + 2, info_element_len,
2725                                             ie_tree);
2726                                         break;
2727
2728                                 case CS0 | Q931_IE_HIGH_LAYER_COMPAT:
2729                                         dissect_q931_high_layer_compat_ie(tvb,
2730                                             offset + 2, info_element_len,
2731                                             ie_tree);
2732                                         break;
2733
2734                                 case CS0 | Q931_IE_USER_USER:
2735                                         dissect_q931_user_user_ie(tvb,
2736                                             offset + 2, info_element_len,
2737                                             ie_tree);
2738                                         break;
2739
2740                                 default:
2741                                         proto_tree_add_text(ie_tree, tvb,
2742                                             offset + 2, info_element_len,
2743                                             "Data: %s",
2744                                             bytes_to_str(
2745                                               tvb_get_ptr(tvb, offset + 2,
2746                                                   info_element_len),
2747                                                   info_element_len));
2748                                         break;
2749                                 }
2750                         }
2751                         offset += 1 + 1 + info_element_len;
2752                 }
2753                 codeset = locked_codeset;
2754         }
2755 }
2756
2757 /*
2758  * Q.931-over-TPKT-over-TCP.
2759  */
2760 static gboolean
2761 dissect_q931_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2762 {
2763         int lv_tpkt_len;
2764
2765         /*
2766          * Check whether this looks like a TPKT-encapsulated
2767          * Q.931 packet.
2768          *
2769          * The minimum length of a Q.931 message is 3:
2770          * 1 byte for the protocol discriminator,
2771          * 1 for the call_reference length,
2772          * and one for the message type.
2773          */
2774         lv_tpkt_len = is_tpkt(tvb, 3);
2775         if (lv_tpkt_len == -1) {
2776                 /*
2777                  * It's not a TPKT packet; reject it.
2778                  */
2779                 return FALSE;
2780         }
2781
2782         /*
2783          * If this segment is *exactly* the length of a TPKT header,
2784          * we assume that, as it looks like a TPKT header, it
2785          * is one, and that the code put a TPKT header in one
2786          * segment and the rest of the PDU in another.
2787          */
2788         if (tvb_length(tvb) == 4) {
2789                 /*
2790                  * It is - call the "dissect TPKT over a TCP stream"
2791                  * routine.
2792                  */
2793                 dissect_tpkt_encap(tvb, pinfo, tree, q931_desegment,
2794                     q931_tpkt_pdu_handle);
2795                 return TRUE;
2796         }
2797
2798         /*
2799          * Well, we have more data than just the TPKT header;
2800          * check whether it looks like the beginning of a
2801          * Q.931 message.
2802          *
2803          * The minimum length of a Q.931 message is 3, as per the
2804          * above.
2805          *
2806          * Check that we have that many bytes past the TPKT header in
2807          * the tvbuff; we already know that the TPKT header says we
2808          * have that many bytes (as we passed 3 as the "min_len" argument
2809          * to "is_tpkt()").
2810          */
2811         if (!tvb_bytes_exist(tvb, 4, 3))
2812                 return FALSE;
2813
2814         /* Check the protocol discriminator */
2815         if (tvb_get_guint8(tvb, 4) != NLPID_Q_931) {
2816                 /* Doesn't look like Q.931 inside TPKT */
2817                 return FALSE;
2818         }
2819
2820         /*
2821          * OK, it looks like Q.931-over-TPKT.
2822          * Call the "dissect TPKT over a TCP stream" routine.
2823          */
2824         dissect_tpkt_encap(tvb, pinfo, tree, q931_desegment,
2825             q931_tpkt_pdu_handle);
2826
2827         return TRUE;
2828 }
2829
2830 static void
2831 dissect_q931_tpkt_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2832 {
2833         dissect_q931_pdu(tvb, pinfo, tree, TRUE);
2834 }
2835
2836 static void
2837 dissect_q931(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2838 {
2839         dissect_q931_pdu(tvb, pinfo, tree, FALSE);
2840 }
2841
2842 void
2843 proto_register_q931(void)
2844 {
2845         static hf_register_info hf[] = {
2846                 { &hf_q931_discriminator,
2847                   { "Protocol discriminator", "q931.disc", FT_UINT8, BASE_HEX, NULL, 0x0,
2848                         "", HFILL }},
2849
2850                 { &hf_q931_call_ref_len,
2851                   { "Call reference value length", "q931.call_ref_len", FT_UINT8, BASE_DEC, NULL, 0x0,
2852                         "", HFILL }},
2853
2854                 { &hf_q931_call_ref_flag,
2855                   { "Call reference flag", "q931.call_ref_flag", FT_BOOLEAN, BASE_NONE, TFS(&tfs_call_ref_flag), 0x0,
2856                         "", HFILL }},
2857
2858                 { &hf_q931_call_ref,
2859                   { "Call reference value", "q931.call_ref", FT_BYTES, BASE_HEX, NULL, 0x0,
2860                         "", HFILL }},
2861
2862                 { &hf_q931_message_type,
2863                   { "Message type", "q931.message_type", FT_UINT8, BASE_HEX, VALS(q931_message_type_vals), 0x0,
2864                         "", HFILL }},
2865
2866                 { &hf_q931_cause_value,
2867                   { "Cause value", "q931.cause_value", FT_UINT8, BASE_DEC, VALS(q931_cause_code_vals), 0x0,
2868                         "", HFILL }},
2869
2870                 { &hf_q931_calling_party_number,
2871                   { "Calling party number digits", "q931.calling_party_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
2872                         "", HFILL }},
2873
2874                 { &hf_q931_called_party_number,
2875                   { "Called party number digits", "q931.called_party_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
2876                         "", HFILL }},
2877
2878                 { &hf_q931_connected_number,
2879                   { "Connected party number digits", "q931.connected_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
2880                         "", HFILL }},
2881
2882                 { &hf_q931_redirecting_number,
2883                   { "Redirecting party number digits", "q931.redirecting_number.digits", FT_STRING, BASE_NONE, NULL, 0x0,
2884                         "", HFILL }},
2885         };
2886         static gint *ett[] = {
2887                 &ett_q931,
2888                 &ett_q931_ie,
2889         };
2890         module_t *q931_module;
2891
2892         proto_q931 = proto_register_protocol("Q.931", "Q.931", "q931");
2893         proto_register_field_array (proto_q931, hf, array_length(hf));
2894         proto_register_subtree_array(ett, array_length(ett));
2895
2896         register_dissector("q931", dissect_q931, proto_q931);
2897         q931_tpkt_pdu_handle = create_dissector_handle(dissect_q931_tpkt_pdu,
2898             proto_q931);
2899
2900         /* subdissector code */ 
2901         codeset_dissector_table = register_dissector_table("q931.codeset", "Q.931 Codeset", FT_UINT8, BASE_HEX);
2902         ie_dissector_table = register_dissector_table("q931.ie", "Q.931 IE", FT_UINT16, BASE_HEX);
2903
2904         q931_module = prefs_register_protocol(proto_q931, NULL);
2905         prefs_register_bool_preference(q931_module, "desegment_h323_messages",
2906             "Desegment all Q.931 messages spanning multiple TCP segments",
2907             "Whether the Q.931 dissector should desegment all messages spanning multiple TCP segments",
2908             &q931_desegment);
2909 }
2910
2911 void
2912 proto_reg_handoff_q931(void)
2913 {
2914         /*
2915          * Attempt to get a handle for the H.225 dissector.
2916          * If we can't, the handle we get is null, and we'll just
2917          * dissect putatively-H.255 Call Signaling stuff as User
2918          * Information.
2919          */
2920         h225_handle = find_dissector("h225");
2921
2922         /*
2923          * For H.323.
2924          */
2925         heur_dissector_add("tcp", dissect_q931_tpkt, proto_q931);
2926 }