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