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