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