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