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