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