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