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