more sprintf removals,
[metze/wireshark/wip.git] / epan / dissectors / packet-x25.c
1 /* packet-x25.c
2  * Routines for X.25 packet disassembly
3  * Olivier Abad <oabad@noos.fr>
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <glib.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "llcsaps.h"
35 #include <epan/packet.h>
36 #include <epan/circuit.h>
37 #include <epan/reassemble.h>
38 #include <epan/prefs.h>
39 #include <epan/emem.h>
40 #include "nlpid.h"
41 #include "x264_prt_id.h"
42
43 /*
44  * Direction of packet.
45  */
46 typedef enum {
47         X25_FROM_DCE,           /* DCE->DTE */
48         X25_FROM_DTE,           /* DTE->DCE */
49         X25_UNKNOWN             /* direction unknown */
50 } x25_dir_t;
51
52 /*
53  * 0 for data packets, 1 for non-data packets.
54  */
55 #define X25_NONDATA_BIT                 0x01
56
57 #define X25_CALL_REQUEST                0x0B
58 #define X25_CALL_ACCEPTED               0x0F
59 #define X25_CLEAR_REQUEST               0x13
60 #define X25_CLEAR_CONFIRMATION          0x17
61 #define X25_INTERRUPT                   0x23
62 #define X25_INTERRUPT_CONFIRMATION      0x27
63 #define X25_RESET_REQUEST               0x1B
64 #define X25_RESET_CONFIRMATION          0x1F
65 #define X25_RESTART_REQUEST             0xFB
66 #define X25_RESTART_CONFIRMATION        0xFF
67 #define X25_REGISTRATION_REQUEST        0xF3
68 #define X25_REGISTRATION_CONFIRMATION   0xF7
69 #define X25_DIAGNOSTIC                  0xF1
70 #define X25_RR                          0x01
71 #define X25_RNR                         0x05
72 #define X25_REJ                         0x09
73 #define X25_DATA                        0x00
74
75 #define PACKET_IS_DATA(type)            (!(type & X25_NONDATA_BIT))
76 #define PACKET_TYPE_FC(type)            (type & 0x1F)
77
78 #define X25_MBIT_MOD8                   0x10
79 #define X25_MBIT_MOD128                 0x01
80
81 #define X25_ABIT                        0x8000
82
83 #define X25_QBIT                        0x8000
84 #define X25_DBIT                        0x4000
85
86 #define X25_FAC_CLASS_MASK              0xC0
87
88 #define X25_FAC_CLASS_A                 0x00
89 #define X25_FAC_CLASS_B                 0x40
90 #define X25_FAC_CLASS_C                 0x80
91 #define X25_FAC_CLASS_D                 0xC0
92
93 #define X25_FAC_COMP_MARK               0x00
94 #define X25_FAC_REVERSE                 0x01
95 #define X25_FAC_THROUGHPUT              0x02
96 #define X25_FAC_CUG                     0x03
97 #define X25_FAC_CALLED_MODIF            0x08
98 #define X25_FAC_CUG_OUTGOING_ACC        0x09
99 #define X25_FAC_THROUGHPUT_MIN          0x0A
100 #define X25_FAC_EXPRESS_DATA            0x0B
101 #define X25_FAC_BILATERAL_CUG           0x41
102 #define X25_FAC_PACKET_SIZE             0x42
103 #define X25_FAC_WINDOW_SIZE             0x43
104 #define X25_FAC_RPOA_SELECTION          0x44
105 #define X25_FAC_TRANSIT_DELAY           0x49
106 #define X25_FAC_CALL_TRANSFER           0xC3
107 #define X25_FAC_CALLED_ADDR_EXT         0xC9
108 #define X25_FAC_ETE_TRANSIT_DELAY       0xCA
109 #define X25_FAC_CALLING_ADDR_EXT        0xCB
110 #define X25_FAC_CALL_DEFLECT            0xD1
111 #define X25_FAC_PRIORITY                0xD2
112
113 static int proto_x25 = -1;
114 static int hf_x25_gfi = -1;
115 static int hf_x25_abit = -1;
116 static int hf_x25_qbit = -1;
117 static int hf_x25_dbit = -1;
118 static int hf_x25_mod = -1;
119 static int hf_x25_lcn = -1;
120 static int hf_x25_type = -1;
121 static int hf_x25_type_fc_mod8 = -1;
122 static int hf_x25_type_data = -1;
123 static int hf_x25_p_r_mod8 = -1;
124 static int hf_x25_p_r_mod128 = -1;
125 static int hf_x25_mbit_mod8 = -1;
126 static int hf_x25_mbit_mod128 = -1;
127 static int hf_x25_p_s_mod8 = -1;
128 static int hf_x25_p_s_mod128 = -1;
129
130 static gint ett_x25 = -1;
131 static gint ett_x25_gfi = -1;
132 static gint ett_x25_fac = -1;
133 static gint ett_x25_fac_unknown = -1;
134 static gint ett_x25_fac_mark = -1;
135 static gint ett_x25_fac_reverse = -1;
136 static gint ett_x25_fac_throughput = -1;
137 static gint ett_x25_fac_cug = -1;
138 static gint ett_x25_fac_called_modif = -1;
139 static gint ett_x25_fac_cug_outgoing_acc = -1;
140 static gint ett_x25_fac_throughput_min = -1;
141 static gint ett_x25_fac_express_data = -1;
142 static gint ett_x25_fac_bilateral_cug = -1;
143 static gint ett_x25_fac_packet_size = -1;
144 static gint ett_x25_fac_window_size = -1;
145 static gint ett_x25_fac_rpoa_selection = -1;
146 static gint ett_x25_fac_transit_delay = -1;
147 static gint ett_x25_fac_call_transfer = -1;
148 static gint ett_x25_fac_called_addr_ext = -1;
149 static gint ett_x25_fac_ete_transit_delay = -1;
150 static gint ett_x25_fac_calling_addr_ext = -1;
151 static gint ett_x25_fac_call_deflect = -1;
152 static gint ett_x25_fac_priority = -1;
153 static gint ett_x25_user_data = -1;
154
155 static gint ett_x25_segment = -1;
156 static gint ett_x25_segments = -1;
157 static gint hf_x25_segments = -1;
158 static gint hf_x25_segment = -1;
159 static gint hf_x25_segment_overlap = -1;
160 static gint hf_x25_segment_overlap_conflict = -1;
161 static gint hf_x25_segment_multiple_tails = -1;
162 static gint hf_x25_segment_too_long_segment = -1;
163 static gint hf_x25_segment_error = -1;
164
165 static const value_string vals_modulo[] = {
166         { 1, "8" },
167         { 2, "128" },
168         { 0, NULL}
169 };
170
171 static const value_string vals_x25_type[] = {
172         { X25_CALL_REQUEST, "Call" },
173         { X25_CALL_ACCEPTED, "Call Accepted" },
174         { X25_CLEAR_REQUEST, "Clear" },
175         { X25_CLEAR_CONFIRMATION, "Clear Confirmation" },
176         { X25_INTERRUPT, "Interrupt" },
177         { X25_INTERRUPT_CONFIRMATION, "Interrupt Confirmation" },
178         { X25_RESET_REQUEST, "Reset" },
179         { X25_RESET_CONFIRMATION, "Reset Confirmation" },
180         { X25_RESTART_REQUEST, "Restart" },
181         { X25_RESTART_CONFIRMATION, "Restart Confirmation" },
182         { X25_REGISTRATION_REQUEST, "Registration" },
183         { X25_REGISTRATION_CONFIRMATION, "Registration Confirmation" },
184         { X25_DIAGNOSTIC, "Diagnostic" },
185         { X25_RR, "RR" },
186         { X25_RNR, "RNR" },
187         { X25_REJ, "REJ" },
188         { X25_DATA, "Data" },
189         { 0,   NULL}
190 };
191
192 static struct true_false_string m_bit_tfs = {
193         "More data follows",
194         "End of data"
195 };
196
197 static const fragment_items x25_frag_items = {
198         &ett_x25_segment,
199         &ett_x25_segments,
200         &hf_x25_segments,
201         &hf_x25_segment,
202         &hf_x25_segment_overlap,
203         &hf_x25_segment_overlap_conflict,
204         &hf_x25_segment_multiple_tails,
205         &hf_x25_segment_too_long_segment,
206         &hf_x25_segment_error,
207         NULL,
208         "segments"
209 };
210
211 static dissector_handle_t ip_handle;
212 static dissector_handle_t clnp_handle;
213 static dissector_handle_t ositp_handle;
214 static dissector_handle_t qllc_handle;
215 static dissector_handle_t data_handle;
216
217 /* Preferences */
218 static gboolean payload_is_qllc_sna = FALSE;
219 static gboolean reassemble_x25 = FALSE;
220
221 /* Reassembly of X.25 */
222
223 static GHashTable *x25_segment_table = NULL;
224 static GHashTable *x25_reassembled_table = NULL;
225
226 static dissector_table_t x25_subdissector_table;
227 static heur_dissector_list_t x25_heur_subdissector_list;
228
229 static void
230 x25_hash_add_proto_start(guint16 vc, guint32 frame, dissector_handle_t dissect)
231 {
232   circuit_t *circuit;
233
234   /*
235    * Is there already a circuit with this VC number?
236    */
237   circuit = find_circuit(CT_X25, vc, frame);
238   if (circuit != NULL) {
239     /*
240      * Yes - close it, as we're creating a new one.
241      */
242     close_circuit(circuit, frame - 1);
243   }
244
245   /*
246    * Set up a new circuit.
247    */
248   circuit = circuit_new(CT_X25, vc, frame);
249
250   /*
251    * Set its dissector.
252    */
253   circuit_set_dissector(circuit, dissect);
254 }
255
256 static void
257 x25_hash_add_proto_end(guint16 vc, guint32 frame)
258 {
259   circuit_t *circuit;
260
261   /*
262    * Try to find the circuit.
263    */
264   circuit = find_circuit(CT_X25, vc, frame);
265
266   /*
267    * If we succeeded, close it.
268    */
269   if (circuit != NULL)
270     close_circuit(circuit, frame);
271 }
272
273 static const char *clear_code(unsigned char code)
274 {
275     static char *buffer;
276
277     if (code == 0x00 || (code & 0x80) == 0x80)
278         return "DTE Originated";
279     if (code == 0x01)
280         return "Number Busy";
281     if (code == 0x03)
282         return "Invalid Facility Requested";
283     if (code == 0x05)
284         return "Network Congestion";
285     if (code == 0x09)
286         return "Out Of Order";
287     if (code == 0x0B)
288         return "Access Barred";
289     if (code == 0x0D)
290         return "Not Obtainable";
291     if (code == 0x11)
292         return "Remote Procedure Error";
293     if (code == 0x13)
294         return "Local Procedure Error";
295     if (code == 0x15)
296         return "RPOA Out Of Order";
297     if (code == 0x19)
298         return "Reverse Charging Acceptance Not Subscribed";
299     if (code == 0x21)
300         return "Incompatible Destination";
301     if (code == 0x29)
302         return "Fast Select Acceptance Not Subscribed";
303     if (code == 0x39)
304         return "Destination Absent";
305
306     buffer=ep_alloc(32);
307     g_snprintf(buffer, 32, "Unknown %02X", code);
308
309     return buffer;
310 }
311
312 static const char *clear_diag(unsigned char code)
313 {
314     static char *buffer;
315
316     if (code == 0)
317         return "No additional information";
318     if (code == 1)
319         return "Invalid P(S)";
320     if (code == 2)
321         return "Invalid P(R)";
322     if (code == 16)
323         return "Packet type invalid";
324     if (code == 17)
325         return "Packet type invalid for state r1";
326     if (code == 18)
327         return "Packet type invalid for state r2";
328     if (code == 19)
329         return "Packet type invalid for state r3";
330     if (code == 20)
331         return "Packet type invalid for state p1";
332     if (code == 21)
333         return "Packet type invalid for state p2";
334     if (code == 22)
335         return "Packet type invalid for state p3";
336     if (code == 23)
337         return "Packet type invalid for state p4";
338     if (code == 24)
339         return "Packet type invalid for state p5";
340     if (code == 25)
341         return "Packet type invalid for state p6";
342     if (code == 26)
343         return "Packet type invalid for state p7";
344     if (code == 27)
345         return "Packet type invalid for state d1";
346     if (code == 28)
347         return "Packet type invalid for state d2";
348     if (code == 29)
349         return "Packet type invalid for state d3";
350     if (code == 32)
351         return "Packet not allowed";
352     if (code == 33)
353         return "Unidentifiable packet";
354     if (code == 34)
355         return "Call on one-way logical channel";
356     if (code == 35)
357         return "Invalid packet type on a PVC";
358     if (code == 36)
359         return "Packet on unassigned LC";
360     if (code == 37)
361         return "Reject not subscribed to";
362     if (code == 38)
363         return "Packet too short";
364     if (code == 39)
365         return "Packet too long";
366     if (code == 40)
367         return "Invalid general format identifier";
368     if (code == 41)
369         return "Restart/registration packet with nonzero bits";
370     if (code == 42)
371         return "Packet type not compatible with facility";
372     if (code == 43)
373         return "Unauthorised interrupt confirmation";
374     if (code == 44)
375         return "Unauthorised interrupt";
376     if (code == 45)
377         return "Unauthorised reject";
378     if (code == 48)
379         return "Time expired";
380     if (code == 49)
381         return "Time expired for incoming call";
382     if (code == 50)
383         return "Time expired for clear indication";
384     if (code == 51)
385         return "Time expired for reset indication";
386     if (code == 52)
387         return "Time expired for restart indication";
388     if (code == 53)
389         return "Time expired for call deflection";
390     if (code == 64)
391         return "Call set-up/clearing or registration pb.";
392     if (code == 65)
393         return "Facility/registration code not allowed";
394     if (code == 66)
395         return "Facility parameter not allowed";
396     if (code == 67)
397         return "Invalid called DTE address";
398     if (code == 68)
399         return "Invalid calling DTE address";
400     if (code == 69)
401         return "Invalid facility/registration length";
402     if (code == 70)
403         return "Incoming call barred";
404     if (code == 71)
405         return "No logical channel available";
406     if (code == 72)
407         return "Call collision";
408     if (code == 73)
409         return "Duplicate facility requested";
410     if (code == 74)
411         return "Non zero address length";
412     if (code == 75)
413         return "Non zero facility length";
414     if (code == 76)
415         return "Facility not provided when expected";
416     if (code == 77)
417         return "Invalid CCITT-specified DTE facility";
418     if (code == 78)
419         return "Max. nb of call redir/defl. exceeded";
420     if (code == 80)
421         return "Miscellaneous";
422     if (code == 81)
423         return "Improper cause code from DTE";
424     if (code == 82)
425         return "Not aligned octet";
426     if (code == 83)
427         return "Inconsistent Q bit setting";
428     if (code == 84)
429         return "NUI problem";
430     if (code == 112)
431         return "International problem";
432     if (code == 113)
433         return "Remote network problem";
434     if (code == 114)
435         return "International protocol problem";
436     if (code == 115)
437         return "International link out of order";
438     if (code == 116)
439         return "International link busy";
440     if (code == 117)
441         return "Transit network facility problem";
442     if (code == 118)
443         return "Remote network facility problem";
444     if (code == 119)
445         return "International routing problem";
446     if (code == 120)
447         return "Temporary routing problem";
448     if (code == 121)
449         return "Unknown called DNIC";
450     if (code == 122)
451         return "Maintenance action";
452     if (code == 144)
453         return "Timer expired or retransmission count surpassed";
454     if (code == 145)
455         return "Timer expired or retransmission count surpassed for INTERRUPT";
456     if (code == 146)
457         return "Timer expired or retransmission count surpassed for DATA "
458                "packet transmission";
459     if (code == 147)
460         return "Timer expired or retransmission count surpassed for REJECT";
461     if (code == 160)
462         return "DTE-specific signals";
463     if (code == 161)
464         return "DTE operational";
465     if (code == 162)
466         return "DTE not operational";
467     if (code == 163)
468         return "DTE resource constraint";
469     if (code == 164)
470         return "Fast select not subscribed";
471     if (code == 165)
472         return "Invalid partially full DATA packet";
473     if (code == 166)
474         return "D-bit procedure not supported";
475     if (code == 167)
476         return "Registration/Cancellation confirmed";
477     if (code == 224)
478         return "OSI network service problem";
479     if (code == 225)
480         return "Disconnection (transient condition)";
481     if (code == 226)
482         return "Disconnection (permanent condition)";
483     if (code == 227)
484         return "Connection rejection - reason unspecified (transient "
485                "condition)";
486     if (code == 228)
487         return "Connection rejection - reason unspecified (permanent "
488                "condition)";
489     if (code == 229)
490         return "Connection rejection - quality of service not available "
491                "transient condition)";
492     if (code == 230)
493         return "Connection rejection - quality of service not available "
494                "permanent condition)";
495     if (code == 231)
496         return "Connection rejection - NSAP unreachable (transient condition)";
497     if (code == 232)
498         return "Connection rejection - NSAP unreachable (permanent condition)";
499     if (code == 233)
500         return "reset - reason unspecified";
501     if (code == 234)
502         return "reset - congestion";
503     if (code == 235)
504         return "Connection rejection - NSAP address unknown (permanent "
505                "condition)";
506     if (code == 240)
507         return "Higher layer initiated";
508     if (code == 241)
509         return "Disconnection - normal";
510     if (code == 242)
511         return "Disconnection - abnormal";
512     if (code == 243)
513         return "Disconnection - incompatible information in user data";
514     if (code == 244)
515         return "Connection rejection - reason unspecified (transient "
516                "condition)";
517     if (code == 245)
518         return "Connection rejection - reason unspecified (permanent "
519                "condition)";
520     if (code == 246)
521         return "Connection rejection - quality of service not available "
522                "(transient condition)";
523     if (code == 247)
524         return "Connection rejection - quality of service not available "
525                "(permanent condition)";
526     if (code == 248)
527         return "Connection rejection - incompatible information in user data";
528     if (code == 249)
529         return "Connection rejection - unrecognizable protocol indentifier "
530                "in user data";
531     if (code == 250)
532         return "Reset - user resynchronization";
533
534     buffer=ep_alloc(32);
535     g_snprintf(buffer, 32, "Unknown %d", code);
536
537     return buffer;
538 }
539
540 static const char *reset_code(unsigned char code)
541 {
542     static char *buffer;
543
544     if (code == 0x00 || (code & 0x80) == 0x80)
545         return "DTE Originated";
546     if (code == 0x01)
547         return "Out of order";
548     if (code == 0x03)
549         return "Remote Procedure Error";
550     if (code == 0x05)
551         return "Local Procedure Error";
552     if (code == 0x07)
553         return "Network Congestion";
554     if (code == 0x09)
555         return "Remote DTE operational";
556     if (code == 0x0F)
557         return "Network operational";
558     if (code == 0x11)
559         return "Incompatible Destination";
560     if (code == 0x1D)
561         return "Network out of order";
562
563     buffer=ep_alloc(32);
564     g_snprintf(buffer, 32, "Unknown %02X", code);
565
566     return buffer;
567 }
568
569 static const char *restart_code(unsigned char code)
570 {
571     static char *buffer;
572
573     if (code == 0x00 || (code & 0x80) == 0x80)
574         return "DTE Originated";
575     if (code == 0x01)
576         return "Local Procedure Error";
577     if (code == 0x03)
578         return "Network Congestion";
579     if (code == 0x07)
580         return "Network Operational";
581     if (code == 0x7F)
582         return "Registration/cancellation confirmed";
583
584     buffer=ep_alloc(32);
585     g_snprintf(buffer, 32, "Unknown %02X", code);
586
587     return buffer;
588 }
589
590 static const char *registration_code(unsigned char code)
591 {
592     static char *buffer;
593
594     if (code == 0x03)
595         return "Invalid facility request";
596     if (code == 0x05)
597         return "Network congestion";
598     if (code == 0x13)
599         return "Local procedure error";
600     if (code == 0x7F)
601         return "Registration/cancellation confirmed";
602
603     buffer=ep_alloc(32);
604     g_snprintf(buffer, 32, "Unknown %02X", code);
605
606     return buffer;
607 }
608
609 static void
610 dump_facilities(proto_tree *tree, int *offset, tvbuff_t *tvb)
611 {
612     guint8 fac, byte1, byte2, byte3;
613     guint32 len;      /* facilities length */
614     proto_item *ti=0;
615     proto_tree *fac_tree = 0;
616     proto_tree *fac_subtree;
617
618     len = tvb_get_guint8(tvb, *offset);
619     if (len && tree) {
620         ti = proto_tree_add_text(tree, tvb, *offset, len + 1,
621                                  "Facilities");
622         fac_tree = proto_item_add_subtree(ti, ett_x25_fac);
623         proto_tree_add_text(fac_tree, tvb, *offset, 1,
624                             "Facilities length: %d", len);
625     }
626     (*offset)++;
627
628     while (len > 0) {
629         fac = tvb_get_guint8(tvb, *offset);
630         switch(fac & X25_FAC_CLASS_MASK) {
631         case X25_FAC_CLASS_A:
632             switch (fac) {
633             case X25_FAC_COMP_MARK:
634                 if (fac_tree)
635                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
636                             "Code : 00 (Marker)");
637                 switch (tvb_get_guint8(tvb, *offset + 1)) {
638                 case 0x00:
639                     if (fac_tree) {
640                         fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
641                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
642                                             "Parameter : 00 (Network complementary "
643                                             "services - calling DTE)");
644                     }
645                     break;
646                 case 0xFF:
647                     if (fac_tree) {
648                         fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
649                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
650                                             "Parameter : FF (Network complementary "
651                                             "services - called DTE)");
652                     }
653                     break;
654                 case 0x0F:
655                     if (fac_tree) {
656                         fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
657                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
658                                             "Parameter : 0F (DTE complementary "
659                                             "services)");
660                     }
661                     break;
662                 default:
663                     if (fac_tree) {
664                         fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
665                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
666                                             "Parameter : %02X (Unknown marker)",
667                                             tvb_get_guint8(tvb, *offset+1));
668                     }
669                     break;
670                 }
671                 break;
672             case X25_FAC_REVERSE:
673                 if (fac_tree) {
674                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
675                             "(Reverse charging / Fast select)", fac);
676                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_reverse);
677                     byte1 = tvb_get_guint8(tvb, *offset + 1);
678                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
679                             "Parameter : %02X", byte1);
680                     if (byte1 & 0xC0)
681                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
682                                 "11.. .... = Fast select with restriction");
683                     else if (byte1 & 0x80)
684                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
685                                 "10.. .... = Fast select - no restriction");
686                     else
687                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
688                                 "00.. .... = Fast select not requested");
689                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
690                             decode_boolean_bitfield(byte1, 0x01, 1*8,
691                                 "Reverse charging requested",
692                                 "Reverse charging not requested"));
693                 }
694                 break;
695             case X25_FAC_THROUGHPUT:
696                 if (fac_tree) {
697                     char *tmpbuf;
698
699                     tmpbuf=ep_alloc(80);
700                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
701                             "(Throughput class negociation)", fac);
702                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_throughput);
703                     byte1 = tvb_get_guint8(tvb, *offset + 1);
704                     switch (byte1 >> 4)
705                     {
706                     case 3:
707                     case 4:
708                     case 5:
709                     case 6:
710                     case 7:
711                     case 8:
712                     case 9:
713                     case 10:
714                     case 11:
715                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (%d bps)",
716                                 75*(1<<((byte1 >> 4)-3)));
717                         break;
718                     case 12:
719                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (48000 bps)");
720                         break;
721                     case 13:
722                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (64000 bps)");
723                         break;
724                     default:
725                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (Reserved)");
726                     }
727                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
728                             decode_numeric_bitfield(byte1, 0xF0, 1*8, tmpbuf));
729                     switch (byte1 & 0x0F)
730                     {
731                     case 3:
732                     case 4:
733                     case 5:
734                     case 6:
735                     case 7:
736                     case 8:
737                     case 9:
738                     case 10:
739                     case 11:
740                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (%d bps)",
741                                 75*(1<<((byte1 & 0x0F)-3)));
742                         break;
743                     case 12:
744                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (48000 bps)");
745                         break;
746                     case 13:
747                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (64000 bps)");
748                         break;
749                     default:
750                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (Reserved)");
751                     }
752                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
753                             decode_numeric_bitfield(byte1, 0x0F, 1*8, tmpbuf));
754                 }
755                 break;
756             case X25_FAC_CUG:
757                 if (fac_tree) {
758                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
759                             "(Closed user group selection)", fac);
760                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_cug);
761                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
762                             "Closed user group: %02X", tvb_get_guint8(tvb, *offset+1));
763                 }
764                 break;
765             case X25_FAC_CALLED_MODIF:
766                 if (fac_tree) {
767                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
768                             "(Called address modified)", fac);
769                     fac_subtree = proto_item_add_subtree(ti,
770                             ett_x25_fac_called_modif);
771                     proto_tree_add_text(fac_tree, tvb, *offset+1, 1,
772                             "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
773                 }
774                 break;
775             case X25_FAC_CUG_OUTGOING_ACC:
776                 if (fac_tree) {
777                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
778                             "(Closed user group with outgoing access selection)",
779                             fac);
780                     fac_subtree = proto_item_add_subtree(ti,
781                             ett_x25_fac_cug_outgoing_acc);
782                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
783                             "Closed user group: %02X", tvb_get_guint8(tvb, *offset+1));
784                 }
785                 break;
786             case X25_FAC_THROUGHPUT_MIN:
787                 if (fac_tree) {
788                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
789                             "(Minimum throughput class)", fac);
790                     fac_subtree = proto_item_add_subtree(ti,
791                             ett_x25_fac_throughput_min);
792                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
793                             "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
794                 }
795                 break;
796             case X25_FAC_EXPRESS_DATA:
797                 if (fac_tree) {
798                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
799                             "(Negociation of express data)", fac);
800                     fac_subtree = proto_item_add_subtree(ti,
801                             ett_x25_fac_express_data);
802                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
803                             "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
804                 }
805                 break;
806             default:
807                 if (fac_tree) {
808                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
809                             "Code : %02X (Unknown class A)", fac);
810                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
811                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
812                             "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
813                 }
814                 break;
815             }
816             (*offset) += 2;
817             len -= 2;
818             break;
819         case X25_FAC_CLASS_B:
820             switch (fac) {
821             case X25_FAC_BILATERAL_CUG:
822                 if (fac_tree) {
823                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
824                             "(Bilateral closed user group selection)", fac);
825                     fac_subtree = proto_item_add_subtree(ti,
826                             ett_x25_fac_bilateral_cug);
827                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
828                                         "Bilateral CUG: %04X",
829                                         tvb_get_ntohs(tvb, *offset+1));
830                 }
831                 break;
832             case X25_FAC_PACKET_SIZE:
833                 if (fac_tree)
834                 {
835                     char *tmpbuf;
836
837                     tmpbuf=ep_alloc(80);
838                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
839                             "(Packet size)", fac);
840                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_packet_size);
841                     byte1 = tvb_get_guint8(tvb, *offset + 1);
842                     switch (byte1)
843                     {
844                     case 0x04:
845                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (16)");
846                         break;
847                     case 0x05:
848                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (32)");
849                         break;
850                     case 0x06:
851                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (64)");
852                         break;
853                     case 0x07:
854                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (128)");
855                         break;
856                     case 0x08:
857                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (256)");
858                         break;
859                     case 0x0D:
860                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (512)");
861                         break;
862                     case 0x0C:
863                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (1024)");
864                         break;
865                     case 0x0E:
866                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (2048)");
867                         break;
868                     case 0x0F:
869                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (4096)");
870                         break;
871                     default:
872                         g_snprintf(tmpbuf, 80, "From the called DTE : %%u (Unknown)");
873                         break;
874                     }
875                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
876                             decode_numeric_bitfield(byte1, 0x0F, 1*8, tmpbuf));
877
878                     byte2 = tvb_get_guint8(tvb, *offset + 1);
879                     switch (byte2)
880                     {
881                     case 0x04:
882                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (16)");
883                         break;
884                     case 0x05:
885                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (32)");
886                         break;
887                     case 0x06:
888                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (64)");
889                         break;
890                     case 0x07:
891                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (128)");
892                         break;
893                     case 0x08:
894                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (256)");
895                         break;
896                     case 0x0D:
897                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (512)");
898                         break;
899                     case 0x0C:
900                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (1024)");
901                         break;
902                     case 0x0E:
903                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (2048)");
904                         break;
905                     case 0x0F:
906                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (4096)");
907                         break;
908                     default:
909                         g_snprintf(tmpbuf, 80, "From the calling DTE : %%u (Unknown)");
910                         break;
911                     }
912                     proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
913                             decode_numeric_bitfield(byte2, 0x0F, 1*8, tmpbuf));
914                 }
915                 break;
916             case X25_FAC_WINDOW_SIZE:
917                 if (fac_tree) {
918                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
919                             "(Window size)", fac);
920                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_window_size);
921                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
922                             decode_numeric_bitfield(tvb_get_guint8(tvb, *offset+1),
923                                 0x7F, 1*8, "From the called DTE: %u"));
924                     proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
925                             decode_numeric_bitfield(tvb_get_guint8(tvb, *offset+2),
926                                 0x7F, 1*8, "From the calling DTE: %u"));
927                 }
928                 break;
929             case X25_FAC_RPOA_SELECTION:
930                 if (fac_tree) {
931                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
932                             "(RPOA selection)", fac);
933                     fac_subtree = proto_item_add_subtree(ti,
934                             ett_x25_fac_rpoa_selection);
935                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
936                                         "Data network identification code : %04X",
937                                         tvb_get_ntohs(tvb, *offset+1));
938                 }
939                 break;
940             case X25_FAC_TRANSIT_DELAY:
941                 if (fac_tree) {
942                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
943                             "(Transit delay selection and indication)", fac);
944                     fac_subtree = proto_item_add_subtree(ti,
945                             ett_x25_fac_transit_delay);
946                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
947                                         "Transit delay: %d ms",
948                                         tvb_get_ntohs(tvb, *offset+1));
949                 }
950                 break;
951             default:
952                 if (fac_tree) {
953                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
954                             "Code : %02X (Unknown class B)", fac);
955                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
956                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
957                             "Parameter %04X", tvb_get_ntohs(tvb, *offset+1));
958                 }
959                 break;
960             }
961             (*offset) += 3;
962             len -= 3;
963             break;
964         case X25_FAC_CLASS_C:
965             if (fac_tree) {
966                 ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
967                         "Code : %02X (Unknown class C)", fac);
968                 fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
969                 proto_tree_add_text(fac_subtree, tvb, *offset+1, 3,
970                         "Parameter %06X",
971                         tvb_get_ntoh24(tvb, *offset+1));
972             }
973             (*offset) += 4;
974             len -= 4;
975             break;
976         case X25_FAC_CLASS_D:
977             switch (fac) {
978             case X25_FAC_CALL_TRANSFER:
979                 if (fac_tree) {
980                     int i;
981                     char *tmpbuf;
982
983                     tmpbuf=ep_alloc(258);
984                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
985                             "(Call redirection or deflection notification)", fac);
986                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_call_transfer);
987                     byte1 = tvb_get_guint8(tvb, *offset+1);
988                     if (byte1 < 2) {
989                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
990                                 "Bogus length : %d", byte1);
991                         return;
992                     } else {
993                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
994                                 "Length : %u", byte1);
995                     }
996                     byte2 = tvb_get_guint8(tvb, *offset+2);
997                     if ((byte2 & 0xC0) == 0xC0) {
998                         proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
999                                 "Reason : call deflection by the originally "
1000                                 "called DTE address");
1001                     }
1002                     else {
1003                         switch (byte2) {
1004                         case 0x01:
1005                             proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1006                                     "Reason : originally called DTE busy");
1007                             break;
1008                         case 0x07:
1009                             proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1010                                     "Reason : call dist. within a hunt group");
1011                             break;
1012                         case 0x09:
1013                             proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1014                                     "Reason : originally called DTE out of order");
1015                             break;
1016                         case 0x0F:
1017                             proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1018                                     "Reason : systematic call redirection");
1019                             break;
1020                         default:
1021                             proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1022                                     "Reason : unknown");
1023                             break;
1024                         }
1025                     }
1026                     byte3 = tvb_get_guint8(tvb, *offset+3);
1027                     proto_tree_add_text(fac_subtree, tvb, *offset+3, 1,
1028                             "Number of semi-octets in DTE address : %u",
1029                             byte3);
1030                     for (i = 0; (i<byte3)&&(i<256); i++) {
1031                         if (i % 2 == 0) {
1032                             tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+4+i/2) >> 4)
1033                                     & 0x0F) + '0';
1034                             /* if > 9, convert to the right hexadecimal letter */
1035                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1036                         } else {
1037                             tmpbuf[i] = (tvb_get_guint8(tvb, *offset+4+i/2)
1038                                     & 0x0F) + '0';
1039                             /* if > 9, convert to the right hexadecimal letter */
1040                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1041                         }
1042                     }
1043                     tmpbuf[i] = 0;
1044                     proto_tree_add_text(fac_subtree, tvb, *offset+4, byte1 - 2,
1045                             "DTE address : %s", tmpbuf);
1046                 }
1047                 break;
1048             case X25_FAC_CALLING_ADDR_EXT:
1049                 if (fac_tree) {
1050                     int i;
1051                     char *tmpbuf;
1052
1053                     tmpbuf=ep_alloc(258);
1054                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
1055                             "(Calling address extension)", fac);
1056                     fac_subtree = proto_item_add_subtree(ti,
1057                             ett_x25_fac_calling_addr_ext);
1058                     byte1 = tvb_get_guint8(tvb, *offset+1);
1059                     if (byte1 < 1) {
1060                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1061                                 "Bogus length : %d", byte1);
1062                         return;
1063                     } else {
1064                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1065                                 "Length : %u", byte1);
1066                     }
1067                     byte2 = tvb_get_guint8(tvb, *offset+2) & 0x3F;
1068                     proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1069                             "Number of semi-octets in DTE address : %u", byte2);
1070                     for (i = 0; (i<byte2)&&(i<256) ; i++) {
1071                         if (i % 2 == 0) {
1072                             tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+3+i/2) >> 4)
1073                                     & 0x0F) + '0';
1074                             /* if > 9, convert to the right hexadecimal letter */
1075                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1076                         } else {
1077                             tmpbuf[i] = (tvb_get_guint8(tvb, *offset+3+i/2)
1078                                     & 0x0F) + '0';
1079                             /* if > 9, convert to the right hexadecimal letter */
1080                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1081                         }
1082                     }
1083                     tmpbuf[i] = 0;
1084                     proto_tree_add_text(fac_subtree, tvb, *offset+3, byte1 - 1,
1085                             "DTE address : %s", tmpbuf);
1086                 }
1087                 break;
1088             case X25_FAC_CALLED_ADDR_EXT:
1089                 if (fac_tree) {
1090                     int i;
1091                     char *tmpbuf;
1092
1093                     tmpbuf=ep_alloc(258);
1094                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
1095                             "(Called address extension)", fac);
1096                     fac_subtree = proto_item_add_subtree(ti,
1097                             ett_x25_fac_called_addr_ext);
1098                     byte1 = tvb_get_guint8(tvb, *offset+1);
1099                     if (byte1 < 1) {
1100                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1101                                 "Bogus length : %d", byte1);
1102                         return;
1103                     } else {
1104                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1105                                 "Length : %u", byte1);
1106                     }
1107                     byte2 = tvb_get_guint8(tvb, *offset+2) & 0x3F;
1108                     proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1109                             "Number of semi-octets in DTE address : %u", byte2);
1110                     for (i = 0; (i<byte2)&&(i<256) ; i++) {
1111                         if (i % 2 == 0) {
1112                             tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+3+i/2) >> 4)
1113                                     & 0x0F) + '0';
1114                             /* if > 9, convert to the right hexadecimal letter */
1115                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1116                         } else {
1117                             tmpbuf[i] = (tvb_get_guint8(tvb, *offset+3+i/2)
1118                                     & 0x0F) + '0';
1119                             /* if > 9, convert to the right hexadecimal letter */
1120                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1121                         }
1122                     }
1123                     tmpbuf[i] = 0;
1124                     proto_tree_add_text(fac_subtree, tvb, *offset+3, byte1 - 1,
1125                             "DTE address : %s", tmpbuf);
1126                 }
1127                 break;
1128             case X25_FAC_ETE_TRANSIT_DELAY:
1129                 if (fac_tree) {
1130                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
1131                             "(End to end transit delay)", fac);
1132                     fac_subtree = proto_item_add_subtree(ti,
1133                             ett_x25_fac_ete_transit_delay);
1134                     byte1 = tvb_get_guint8(tvb, *offset+1);
1135                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1136                             "Length : %u", byte1);
1137                     proto_tree_add_text(fac_subtree, tvb, *offset+2, byte1, "Value");
1138                 }
1139                 break;
1140             case X25_FAC_CALL_DEFLECT:
1141                 if (fac_tree) {
1142                     int i;
1143                     char *tmpbuf;
1144
1145                     tmpbuf=ep_alloc(258);
1146                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
1147                             "(Call deflection selection)", fac);
1148                     fac_subtree = proto_item_add_subtree(ti,
1149                             ett_x25_fac_call_deflect);
1150                     byte1 = tvb_get_guint8(tvb, *offset+1);
1151                     if (byte1 < 2) {
1152                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1153                                 "Bogus length : %d", byte1);
1154                         return;
1155                     } else {
1156                         proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1157                                 "Length : %u", byte1);
1158                     }
1159                     byte2 = tvb_get_guint8(tvb, *offset+2);
1160                     if ((byte2 & 0xC0) == 0xC0)
1161                         proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1162                                 "Reason : call DTE originated");
1163                     else
1164                         proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
1165                                 "Reason : unknown");
1166                     byte3 = tvb_get_guint8(tvb, *offset+3);
1167                     proto_tree_add_text(fac_subtree, tvb, *offset+3, 1,
1168                             "Number of semi-octets in the alternative DTE address : %u",
1169                             byte3);
1170                     for (i = 0; (i<byte3)&&(i<256) ; i++) {
1171                         if (i % 2 == 0) {
1172                             tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+4+i/2) >> 4)
1173                                     & 0x0F) + '0';
1174                             /* if > 9, convert to the right hexadecimal letter */
1175                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1176                         } else {
1177                             tmpbuf[i] = (tvb_get_guint8(tvb, *offset+4+i/2)
1178                                     & 0x0F) + '0';
1179                             /* if > 9, convert to the right hexadecimal letter */
1180                             if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
1181                         }
1182                     }
1183                     tmpbuf[i] = 0;
1184                     proto_tree_add_text(fac_subtree, tvb, *offset+4, byte1 - 2,
1185                             "Alternative DTE address : %s", tmpbuf);
1186                 }
1187                 break;
1188             case X25_FAC_PRIORITY:
1189                 if (fac_tree) {
1190                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
1191                             "Code : %02X (Priority)", fac);
1192                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_priority);
1193                     byte1 = tvb_get_guint8(tvb, *offset+1);
1194                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1195                             "Length : %u", byte1);
1196                     proto_tree_add_text(fac_subtree, tvb, *offset+2, byte1, "Value");
1197                 }
1198                 break;
1199             default:
1200                 if (fac_tree) {
1201                     ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
1202                             "Code : %02X (Unknown class D)", fac);
1203                     fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
1204                     byte1 = tvb_get_guint8(tvb, *offset+1);
1205                     proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
1206                             "Length : %u", byte1);
1207                     proto_tree_add_text(fac_subtree, tvb, *offset+2, byte1, "Value");
1208                 }
1209             }
1210             byte1 = tvb_get_guint8(tvb, *offset+1);
1211             (*offset) += byte1+2;
1212             len -= byte1+2;
1213             break;
1214         }
1215     }
1216 }
1217
1218 static void
1219 x25_ntoa(proto_tree *tree, int *offset, tvbuff_t *tvb,
1220          packet_info *pinfo, gboolean is_registration)
1221 {
1222     int len1, len2;
1223     int i;
1224     char *addr1, *addr2;
1225     char *first, *second;
1226     guint8 byte;
1227     int localoffset;
1228
1229     addr1=ep_alloc(16);
1230     addr2=ep_alloc(16);
1231
1232     byte = tvb_get_guint8(tvb, *offset);
1233     len1 = (byte >> 0) & 0x0F;
1234     len2 = (byte >> 4) & 0x0F;
1235
1236     if (tree) {
1237         proto_tree_add_text(tree, tvb, *offset, 1,
1238                 decode_numeric_bitfield(byte, 0xF0, 1*8,
1239                         is_registration ?
1240                           "DTE address length : %u" :
1241                           "Calling address length : %u"));
1242         proto_tree_add_text(tree, tvb, *offset, 1,
1243                 decode_numeric_bitfield(byte, 0x0F, 1*8,
1244                         is_registration ?
1245                           "DCE address length : %u" :
1246                           "Called address length : %u"));
1247     }
1248     (*offset)++;
1249
1250     localoffset = *offset;
1251     byte = tvb_get_guint8(tvb, localoffset);
1252
1253     first=addr1;
1254     second=addr2;
1255     for (i = 0; i < (len1 + len2); i++) {
1256         if (i < len1) {
1257             if (i % 2 != 0) {
1258                 *first++ = ((byte >> 0) & 0x0F) + '0';
1259                 localoffset++;
1260                 byte = tvb_get_guint8(tvb, localoffset);
1261             } else {
1262                 *first++ = ((byte >> 4) & 0x0F) + '0';
1263             }
1264         } else {
1265             if (i % 2 != 0) {
1266                 *second++ = ((byte >> 0) & 0x0F) + '0';
1267                 localoffset++;
1268                 byte = tvb_get_guint8(tvb, localoffset);
1269             } else {
1270                 *second++ = ((byte >> 4) & 0x0F) + '0';
1271             }
1272         }
1273     }
1274
1275     *first  = '\0';
1276     *second = '\0';
1277
1278     if (len1) {
1279         if (check_col(pinfo->cinfo, COL_RES_DL_DST))
1280             col_add_str(pinfo->cinfo, COL_RES_DL_DST, addr1);
1281         if (tree)
1282             proto_tree_add_text(tree, tvb, *offset,
1283                                 (len1 + 1) / 2,
1284                                 is_registration ?
1285                                   "DCE address : %s" :
1286                                   "Called address : %s",
1287                                 addr1);
1288     }
1289     if (len2) {
1290         if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
1291             col_add_str(pinfo->cinfo, COL_RES_DL_SRC, addr2);
1292         if (tree)
1293             proto_tree_add_text(tree, tvb, *offset + len1/2,
1294                                 (len2+1)/2+(len1%2+(len2+1)%2)/2,
1295                                 is_registration ?
1296                                   "DTE address : %s" :
1297                                   "Calling address : %s",
1298                                 addr2);
1299     }
1300     (*offset) += ((len1 + len2 + 1) / 2);
1301 }
1302
1303 static void
1304 x25_toa(proto_tree *tree, int *offset, tvbuff_t *tvb,
1305         packet_info *pinfo)
1306 {
1307     int len1, len2;
1308     int i;
1309     char *addr1, *addr2;
1310     char *first, *second;
1311     guint8 byte;
1312     int localoffset;
1313
1314     addr1=ep_alloc(256);
1315     addr2=ep_alloc(256);
1316
1317     len1 = tvb_get_guint8(tvb, *offset);
1318     if (tree) {
1319         proto_tree_add_text(tree, tvb, *offset, 1,
1320                     "Called address length : %u",
1321                     len1);
1322     }
1323     (*offset)++;
1324
1325     len2 = tvb_get_guint8(tvb, *offset);
1326     if (tree) {
1327         proto_tree_add_text(tree, tvb, *offset, 1,
1328                     "Calling address length : %u",
1329                     len2);
1330     }
1331     (*offset)++;
1332
1333     localoffset = *offset;
1334     byte = tvb_get_guint8(tvb, localoffset);
1335
1336     /*
1337      * XXX - the first two half-octets of the address are the TOA and
1338      * NPI; process them as such and, if the TOA says an address is
1339      * an alternative address, process it correctly (i.e., not as a
1340      * sequence of half-octets containing digit values).
1341      */
1342     first=addr1;
1343     second=addr2;
1344     for (i = 0; i < (len1 + len2); i++) {
1345         if (i < len1) {
1346             if (i % 2 != 0) {
1347                 *first++ = ((byte >> 0) & 0x0F) + '0';
1348                 localoffset++;
1349                 byte = tvb_get_guint8(tvb, localoffset);
1350             } else {
1351                 *first++ = ((byte >> 4) & 0x0F) + '0';
1352             }
1353         } else {
1354             if (i % 2 != 0) {
1355                 *second++ = ((byte >> 0) & 0x0F) + '0';
1356                 localoffset++;
1357                 byte = tvb_get_guint8(tvb, localoffset);
1358             } else {
1359                 *second++ = ((byte >> 4) & 0x0F) + '0';
1360             }
1361         }
1362     }
1363
1364     *first  = '\0';
1365     *second = '\0';
1366
1367     if (len1) {
1368         if (check_col(pinfo->cinfo, COL_RES_DL_DST))
1369             col_add_str(pinfo->cinfo, COL_RES_DL_DST, addr1);
1370         if (tree)
1371             proto_tree_add_text(tree, tvb, *offset,
1372                                 (len1 + 1) / 2,
1373                                 "Called address : %s",
1374                                 addr1);
1375     }
1376     if (len2) {
1377         if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
1378             col_add_str(pinfo->cinfo, COL_RES_DL_SRC, addr2);
1379         if (tree)
1380             proto_tree_add_text(tree, tvb, *offset + len1/2,
1381                                 (len2+1)/2+(len1%2+(len2+1)%2)/2,
1382                                 "Calling address : %s",
1383                                 addr2);
1384     }
1385     (*offset) += ((len1 + len2 + 1) / 2);
1386 }
1387
1388 static int
1389 get_x25_pkt_len(tvbuff_t *tvb)
1390 {
1391     guint length, called_len, calling_len, dte_len, dce_len;
1392     guint8 byte2, bytex;
1393
1394     byte2 = tvb_get_guint8(tvb, 2);
1395     switch (byte2)
1396     {
1397     case X25_CALL_REQUEST:
1398         bytex = tvb_get_guint8(tvb, 3);
1399         called_len  = (bytex >> 0) & 0x0F;
1400         calling_len = (bytex >> 4) & 0x0F;
1401         length = 4 + (called_len + calling_len + 1) / 2; /* addr */
1402         if (length < tvb_reported_length(tvb))
1403             length += (1 + tvb_get_guint8(tvb, length)); /* facilities */
1404
1405         return MIN(tvb_reported_length(tvb),length);
1406
1407     case X25_CALL_ACCEPTED:
1408         /* The calling/called address length byte (following the packet type)
1409          * is not mandatory, so we must check the packet length before trying
1410          * to read it */
1411         if (tvb_reported_length(tvb) == 3)
1412             return(3);
1413         bytex = tvb_get_guint8(tvb, 3);
1414         called_len  = (bytex >> 0) & 0x0F;
1415         calling_len = (bytex >> 4) & 0x0F;
1416         length = 4 + (called_len + calling_len + 1) / 2; /* addr */
1417         if (length < tvb_reported_length(tvb))
1418             length += (1 + tvb_get_guint8(tvb, length)); /* facilities */
1419
1420         return MIN(tvb_reported_length(tvb),length);
1421
1422     case X25_CLEAR_REQUEST:
1423     case X25_RESET_REQUEST:
1424     case X25_RESTART_REQUEST:
1425         return MIN(tvb_reported_length(tvb),5);
1426
1427     case X25_DIAGNOSTIC:
1428         return MIN(tvb_reported_length(tvb),4);
1429
1430     case X25_CLEAR_CONFIRMATION:
1431     case X25_INTERRUPT:
1432     case X25_INTERRUPT_CONFIRMATION:
1433     case X25_RESET_CONFIRMATION:
1434     case X25_RESTART_CONFIRMATION:
1435         return MIN(tvb_reported_length(tvb),3);
1436
1437     case X25_REGISTRATION_REQUEST:
1438         bytex = tvb_get_guint8(tvb, 3);
1439         dce_len = (bytex >> 0) & 0x0F;
1440         dte_len = (bytex >> 4) & 0x0F;
1441         length = 4 + (dte_len + dce_len + 1) / 2; /* addr */
1442         if (length < tvb_reported_length(tvb))
1443             length += (1 + tvb_get_guint8(tvb, length)); /* registration */
1444
1445         return MIN(tvb_reported_length(tvb),length);
1446
1447     case X25_REGISTRATION_CONFIRMATION:
1448         bytex = tvb_get_guint8(tvb, 5);
1449         dce_len = (bytex >> 0) & 0x0F;
1450         dte_len = (bytex >> 4) & 0x0F;
1451         length = 6 + (dte_len + dce_len + 1) / 2; /* addr */
1452         if (length < tvb_reported_length(tvb))
1453             length += (1 + tvb_get_guint8(tvb, length)); /* registration */
1454
1455         return MIN(tvb_reported_length(tvb),length);
1456     }
1457
1458     if (PACKET_IS_DATA(byte2))
1459         return MIN(tvb_reported_length(tvb),3);
1460
1461     switch (PACKET_TYPE_FC(byte2))
1462     {
1463     case X25_RR:
1464         return MIN(tvb_reported_length(tvb),3);
1465
1466     case X25_RNR:
1467         return MIN(tvb_reported_length(tvb),3);
1468
1469     case X25_REJ:
1470         return MIN(tvb_reported_length(tvb),3);
1471     }
1472
1473     return 0;
1474 }
1475
1476 static const value_string prt_id_vals[] = {
1477         {PRT_ID_ISO_8073,           "ISO 8073 COTP"},
1478         {PRT_ID_ISO_8602,           "ISO 8602 CLTP"},
1479         {PRT_ID_ISO_10736_ISO_8073, "ISO 10736 in conjunction with ISO 8073 COTP"},
1480         {PRT_ID_ISO_10736_ISO_8602, "ISO 10736 in conjunction with ISO 8602 CLTP"},
1481         {0x00,                      NULL}
1482 };
1483
1484 static const value_string sharing_strategy_vals[] = {
1485         {0x00,            "No sharing"},
1486         {0x00,            NULL}
1487 };
1488
1489 static void
1490 dissect_x25_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
1491     x25_dir_t dir, gboolean side)
1492 {
1493     proto_tree *x25_tree=0, *gfi_tree=0, *userdata_tree=0;
1494     proto_item *ti;
1495     guint localoffset=0;
1496     guint x25_pkt_len;
1497     int modulo;
1498     guint16 vc;
1499     dissector_handle_t dissect = NULL;
1500     gboolean toa;         /* TOA/NPI address format */
1501     guint16 bytes0_1;
1502     guint8 pkt_type;
1503     const char *short_name = NULL, *long_name = NULL;
1504     tvbuff_t *next_tvb = NULL;
1505     gboolean q_bit_set = FALSE;
1506     gboolean m_bit_set;
1507     gint payload_len;
1508     guint32 frag_key;
1509     void *saved_private_data;
1510     fragment_data *fd_head;
1511
1512     if (check_col(pinfo->cinfo, COL_PROTOCOL))
1513         col_set_str(pinfo->cinfo, COL_PROTOCOL, "X.25");
1514
1515     bytes0_1 = tvb_get_ntohs(tvb, 0);
1516
1517     modulo = ((bytes0_1 & 0x2000) ? 128 : 8);
1518     vc     = (int)(bytes0_1 & 0x0FFF);
1519
1520     pinfo->ctype = CT_X25;
1521     pinfo->circuit_id = vc;
1522
1523     if (bytes0_1 & X25_ABIT) toa = TRUE;
1524     else toa = FALSE;
1525
1526     x25_pkt_len = get_x25_pkt_len(tvb);
1527     if (x25_pkt_len < 3) /* packet too short */
1528     {
1529         if (check_col(pinfo->cinfo, COL_INFO))
1530             col_set_str(pinfo->cinfo, COL_INFO, "Invalid/short X.25 packet");
1531         if (tree)
1532             proto_tree_add_protocol_format(tree, proto_x25, tvb, 0, -1,
1533                     "Invalid/short X.25 packet");
1534         return;
1535     }
1536
1537     pkt_type = tvb_get_guint8(tvb, 2);
1538     if (PACKET_IS_DATA(pkt_type)) {
1539         if (bytes0_1 & X25_QBIT)
1540             q_bit_set = TRUE;
1541     }
1542
1543     if (tree) {
1544         ti = proto_tree_add_item(tree, proto_x25, tvb, 0, x25_pkt_len, FALSE);
1545         x25_tree = proto_item_add_subtree(ti, ett_x25);
1546         ti = proto_tree_add_item(x25_tree, hf_x25_gfi, tvb, 0, 2, FALSE);
1547         gfi_tree = proto_item_add_subtree(ti, ett_x25_gfi);
1548
1549         if (PACKET_IS_DATA(pkt_type)) {
1550             proto_tree_add_boolean(gfi_tree, hf_x25_qbit, tvb, 0, 2,
1551                 bytes0_1);
1552         }
1553         else if (pkt_type == X25_CALL_REQUEST ||
1554             pkt_type == X25_CALL_ACCEPTED ||
1555             pkt_type == X25_CLEAR_REQUEST ||
1556             pkt_type == X25_CLEAR_CONFIRMATION) {
1557             proto_tree_add_boolean(gfi_tree, hf_x25_abit, tvb, 0, 2,
1558                 bytes0_1);
1559         }
1560
1561         if (pkt_type == X25_CALL_REQUEST || pkt_type == X25_CALL_ACCEPTED ||
1562             PACKET_IS_DATA(pkt_type)) {
1563             proto_tree_add_boolean(gfi_tree, hf_x25_dbit, tvb, 0, 2,
1564                 bytes0_1);
1565         }
1566         proto_tree_add_uint(gfi_tree, hf_x25_mod, tvb, 0, 2, bytes0_1);
1567     }
1568
1569     switch (pkt_type) {
1570     case X25_CALL_REQUEST:
1571         switch (dir) {
1572
1573         case X25_FROM_DCE:
1574             short_name = "Inc. call";
1575             long_name = "Incoming call";
1576             break;
1577
1578         case X25_FROM_DTE:
1579             short_name = "Call req.";
1580             long_name = "Call request";
1581             break;
1582
1583         case X25_UNKNOWN:
1584             short_name = "Inc. call/Call req.";
1585             long_name = "Incoming call/Call request";
1586             break;
1587         }
1588         if (check_col(pinfo->cinfo, COL_INFO))
1589             col_add_fstr(pinfo->cinfo, COL_INFO, "%s VC:%d", short_name, vc);
1590         if (x25_tree) {
1591             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb,
1592                     0, 2, bytes0_1);
1593             proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
1594                     X25_CALL_REQUEST, "Packet Type: %s", long_name);
1595         }
1596         localoffset = 3;
1597         if (localoffset < x25_pkt_len) { /* calling/called addresses */
1598             if (toa)
1599                 x25_toa(x25_tree, &localoffset, tvb, pinfo);
1600             else
1601                 x25_ntoa(x25_tree, &localoffset, tvb, pinfo, FALSE);
1602         }
1603
1604         if (localoffset < x25_pkt_len) /* facilities */
1605             dump_facilities(x25_tree, &localoffset, tvb);
1606
1607         if (localoffset < tvb_reported_length(tvb)) /* user data */
1608         {
1609             guint8 spi;
1610             int is_x_264;
1611             guint8 prt_id;
1612
1613             if (x25_tree) {
1614                 ti = proto_tree_add_text(x25_tree, tvb, localoffset, -1,
1615                         "User data");
1616                 userdata_tree = proto_item_add_subtree(ti, ett_x25_user_data);
1617             }
1618
1619             /* X.263/ISO 9577 says that:
1620
1621                     When CLNP or ESIS are run over X.25, the SPI
1622                     is 0x81 or 0x82, respectively; those are the
1623                     NLPIDs for those protocol.
1624
1625                     When X.224/ISO 8073 COTP is run over X.25, and
1626                     when ISO 11570 explicit identification is being
1627                     used, the first octet of the user data field is
1628                     a TPDU length field, and the rest is "as defined
1629                     in ITU-T Rec. X.225 | ISO/IEC 8073, Annex B,
1630                     or ITU-T Rec. X.264 and ISO/IEC 11570".
1631
1632                     When X.264/ISO 11570 default identification is
1633                     being used, there is no user data field in the
1634                     CALL REQUEST packet.  This is for X.225/ISO 8073
1635                     COTP.
1636
1637                It also says that SPI values from 0x03 through 0x3f are
1638                reserved and are in use by X.224/ISO 8073 Annex B and
1639                X.264/ISO 11570.  The note says that those values are
1640                not NLPIDs, they're "used by the respective higher layer
1641                protocol" and "not used for higher layer protocol
1642                identification".  I infer from this and from what
1643                X.264/ISO 11570 says that this means that values in those
1644                range are valid values for the first octet of an
1645                X.224/ISO 8073 packet or for X.264/ISO 11570.
1646
1647                Annex B of X.225/ISO 8073 mentions some additional TPDU
1648                types that can be put in what I presume is the user
1649                data of connect requests.  It says that:
1650
1651                     The sending transport entity shall:
1652
1653                         a) either not transmit any TPDU in the NS-user data
1654                            parameter of the N-CONNECT request primitive; or
1655
1656                         b) transmit the UN-TPDU (see ITU-T Rec. X.264 and
1657                            ISO/IEC 11570) followed by the NCM-TPDU in the
1658                            NS-user data parameter of the N-CONNECT request
1659                            primitive.
1660
1661                I don't know if this means that the user data field
1662                will contain a UN TPDU followed by an NCM TPDU or not.
1663
1664                X.264/ISO 11570 says that:
1665
1666                     When default identification is being used,
1667                     X.225/ISO 8073 COTP is identified.  No user data
1668                     is sent in the network-layer connection request.
1669
1670                     When explicit identification is being used,
1671                     the user data is a UN TPDU ("Use of network
1672                     connection TPDU"), which specifies the transport
1673                     protocol to use over this network connection.
1674                     It also says that the length of a UN TPDU shall
1675                     not exceed 32 octets, i.e. shall not exceed 0x20;
1676                     it says this is "due to the desire not to conflict
1677                     with the protocol identifier field carried by X.25
1678                     CALL REQUEST/INCOMING CALL packets", and says that
1679                     field has values specified in X.244.  X.244 has been
1680                     superseded by X.263/ISO 9577, so that presumably
1681                     means the goal is to allow a UN TPDU's length
1682                     field to be distinguished from an NLPID, allowing
1683                     you to tell whether X.264/ISO 11570 explicit
1684                     identification is being used or an NLPID is
1685                     being used as the SPI.
1686
1687                I read this as meaning that, if the ISO mechanisms are
1688                used to identify the protocol being carried over X.25:
1689
1690                     if there's no user data in the CALL REQUEST/
1691                     INCOMING CALL packet, it's COTP;
1692
1693                     if there is user data, then:
1694
1695                         if the first octet is less than or equal to
1696                         32, it might be a UN TPDU, and that identifies
1697                         the transport protocol being used, and
1698                         it may be followed by more data, such
1699                         as a COTP NCM TPDU if it's COTP;
1700
1701                         if the first octet is greater than 32, it's
1702                         an NLPID, *not* a TPDU length, and the
1703                         stuff following it is *not* a TPDU.
1704
1705                Figure A.2 of X.263/ISO 9577 seems to say that the
1706                first octet of the user data is a TPDU length field,
1707                in the range 0x03 through 0x82, and says they are
1708                for X.225/ISO 8073 Annex B or X.264/ISO 11570.
1709
1710                However, X.264/ISO 11570 seems to imply that the length
1711                field would be that of a UN TPDU, which must be less
1712                than or equal to 0x20, and X.225/ISO 8073 Annex B seems
1713                to indicate that the user data must begin with
1714                an X.264/ISO 11570 UN TPDU, so I'd say that A.2 should
1715                have said "in the range 0x03 through 0x20", instead
1716                (the length value doesn't include the length field,
1717                and the minimum UN TPDU has length, type, PRT-ID,
1718                and SHARE, so that's 3 bytes without the length). */
1719             spi = tvb_get_guint8(tvb, localoffset);
1720             if (spi > 32 || spi < 3) {
1721                 /* First octet is > 32, or < 3, so the user data isn't an
1722                    X.264/ISO 11570 UN TPDU */
1723                 is_x_264 = FALSE;
1724             } else {
1725                 /* First octet is >= 3 and <= 32, so the user data *might*
1726                    be an X.264/ISO 11570 UN TPDU.  Check whether we have
1727                    enough data to see if it is. */
1728                 if (tvb_bytes_exist(tvb, localoffset+1, 1)) {
1729                     /* We do; check whether the second octet is 1. */
1730                     if (tvb_get_guint8(tvb, localoffset+1) == 0x01) {
1731                         /* Yes, the second byte is 1, so it looks like
1732                            a UN TPDU. */
1733                         is_x_264 = TRUE;
1734                     } else {
1735                         /* No, the second byte is not 1, so it's not a
1736                            UN TPDU. */
1737                         is_x_264 = FALSE;
1738                     }
1739                 } else {
1740                     /* We can't see the second byte of the putative UN
1741                        TPDU, so we don't know if that's what it is. */
1742                     is_x_264 = -1;
1743                 }
1744             }
1745             if (is_x_264 == -1) {
1746                 /*
1747                  * We don't know what it is; just skip it.
1748                  */
1749                 localoffset = tvb_length(tvb);
1750             } else if (is_x_264) {
1751                 /* It looks like an X.264 UN TPDU, so show it as such. */
1752                 if (userdata_tree) {
1753                     proto_tree_add_text(userdata_tree, tvb, localoffset, 1,
1754                                         "X.264 length indicator: %u",
1755                                         spi);
1756                     proto_tree_add_text(userdata_tree, tvb, localoffset+1, 1,
1757                                         "X.264 UN TPDU identifier: 0x%02X",
1758                                         tvb_get_guint8(tvb, localoffset+1));
1759                 }
1760                 prt_id = tvb_get_guint8(tvb, localoffset+2);
1761                 if (userdata_tree) {
1762                     proto_tree_add_text(userdata_tree, tvb, localoffset+2, 1,
1763                                         "X.264 protocol identifier: %s",
1764                                         val_to_str(prt_id, prt_id_vals,
1765                                                "Unknown (0x%02X)"));
1766                     proto_tree_add_text(userdata_tree, tvb, localoffset+3, 1,
1767                                         "X.264 sharing strategy: %s",
1768                                         val_to_str(tvb_get_guint8(tvb, localoffset+3),
1769                                         sharing_strategy_vals, "Unknown (0x%02X)"));
1770                 }
1771
1772                 /* XXX - dissect the variable part? */
1773
1774                 /* The length doesn't include the length octet itself. */
1775                 localoffset += spi + 1;
1776
1777                 switch (prt_id) {
1778
1779                 case PRT_ID_ISO_8073:
1780                     /* ISO 8073 COTP */
1781                     if (!pinfo->fd->flags.visited)
1782                         x25_hash_add_proto_start(vc, pinfo->fd->num, ositp_handle);
1783                     /* XXX - dissect the rest of the user data as COTP?
1784                        That needs support for NCM TPDUs, etc. */
1785                     break;
1786
1787                 case PRT_ID_ISO_8602:
1788                     /* ISO 8602 CLTP */
1789                     if (!pinfo->fd->flags.visited)
1790                         x25_hash_add_proto_start(vc, pinfo->fd->num, ositp_handle);
1791                     break;
1792                 }
1793             } else if (is_x_264 == 0) {
1794                 /* It doesn't look like a UN TPDU, so compare the first
1795                    octet of the CALL REQUEST packet with various X.263/
1796                    ISO 9577 NLPIDs, as per Annex A of X.263/ISO 9577. */
1797
1798                 if (userdata_tree) {
1799                     proto_tree_add_text(userdata_tree, tvb, localoffset, 1,
1800                                         "X.263 secondary protocol ID: %s",
1801                                         val_to_str(spi, nlpid_vals, "Unknown (0x%02x)"));
1802                 }
1803
1804                 if (!pinfo->fd->flags.visited) {
1805                     /*
1806                      * Is there a dissector handle for this SPI?
1807                      * If so, assign it to this virtual circuit.
1808                      */
1809                     dissect = dissector_get_port_handle(x25_subdissector_table, spi);
1810                     if (dissect != NULL)
1811                         x25_hash_add_proto_start(vc, pinfo->fd->num, dissect);
1812                 }
1813
1814                 /*
1815                  * If there's only one octet of user data, it's just
1816                  * an NLPID; don't try to dissect it.
1817                  */
1818                 if (localoffset + 1 == tvb_reported_length(tvb))
1819                     return;
1820
1821                 /*
1822                  * There's more than one octet of user data, so we'll
1823                  * dissect it; for some protocols, the NLPID is considered
1824                  * to be part of the PDU, so, for those cases, we don't
1825                  * skip past it.  For other protocols, we skip the NLPID.
1826                  */
1827                 switch (spi) {
1828
1829                 case NLPID_ISO8473_CLNP:
1830                 case NLPID_ISO9542_ESIS:
1831                 case NLPID_ISO10589_ISIS:
1832                 case NLPID_ISO10747_IDRP:
1833                 case NLPID_SNDCF:
1834                     /*
1835                      * The NLPID is part of the PDU.  Don't skip it.
1836                      * But if it's all there is to the PDU, don't
1837                      * bother dissecting it.
1838                      */
1839                     break;
1840
1841                 case NLPID_SPI_X_29:
1842                     /*
1843                      * The first 4 bytes of the call user data are
1844                      * the SPI plus 3 reserved bytes; they are not
1845                      * part of the data to be dissected as X.29 data.
1846                      */
1847                     localoffset += 4;
1848                     break;
1849
1850                 default:
1851                     /*
1852                      * The NLPID isn't part of the PDU - skip it.
1853                      * If that means there's nothing to dissect
1854                      */
1855                     localoffset++;
1856                 }
1857             }
1858         }
1859         break;
1860     case X25_CALL_ACCEPTED:
1861         switch (dir) {
1862
1863         case X25_FROM_DCE:
1864             short_name = "Call conn.";
1865             long_name = "Call connected";
1866             break;
1867
1868         case X25_FROM_DTE:
1869             short_name = "Call acc.";
1870             long_name = "Call accepted";
1871             break;
1872
1873         case X25_UNKNOWN:
1874             short_name = "Call conn./Call acc.";
1875             long_name = "Call connected/Call accepted";
1876             break;
1877         }
1878         if(check_col(pinfo->cinfo, COL_INFO))
1879             col_add_fstr(pinfo->cinfo, COL_INFO, "%s VC:%d", short_name, vc);
1880         if (x25_tree) {
1881             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
1882             proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
1883                     X25_CALL_ACCEPTED, "Packet Type: %s", long_name);
1884         }
1885         localoffset = 3;
1886         if (localoffset < x25_pkt_len) { /* calling/called addresses */
1887             if (toa)
1888                 x25_toa(x25_tree, &localoffset, tvb, pinfo);
1889             else
1890                 x25_ntoa(x25_tree, &localoffset, tvb, pinfo, FALSE);
1891         }
1892
1893         if (localoffset < x25_pkt_len) /* facilities */
1894             dump_facilities(x25_tree, &localoffset, tvb);
1895         break;
1896     case X25_CLEAR_REQUEST:
1897         switch (dir) {
1898
1899         case X25_FROM_DCE:
1900             short_name = "Clear ind.";
1901             long_name = "Clear indication";
1902             break;
1903
1904         case X25_FROM_DTE:
1905             short_name = "Clear req.";
1906             long_name = "Clear request";
1907             break;
1908
1909         case X25_UNKNOWN:
1910             short_name = "Clear ind./Clear req.";
1911             long_name = "Clear indication/Clear request";
1912             break;
1913         }
1914         if(check_col(pinfo->cinfo, COL_INFO)) {
1915             col_add_fstr(pinfo->cinfo, COL_INFO, "%s VC:%d %s - %s", short_name,
1916                     vc, clear_code(tvb_get_guint8(tvb, 3)),
1917                     clear_diag(tvb_get_guint8(tvb, 4)));
1918         }
1919         x25_hash_add_proto_end(vc, pinfo->fd->num);
1920         if (x25_tree) {
1921             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
1922             proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb,
1923                     localoffset+2, 1, X25_CLEAR_REQUEST, "Packet Type: %s",
1924                     long_name);
1925             proto_tree_add_text(x25_tree, tvb, 3, 1,
1926                     "Cause : %s", clear_code(tvb_get_guint8(tvb, 3)));
1927             proto_tree_add_text(x25_tree, tvb, 4, 1,
1928                     "Diagnostic : %s", clear_diag(tvb_get_guint8(tvb, 4)));
1929         }
1930         localoffset = x25_pkt_len;
1931         break;
1932     case X25_CLEAR_CONFIRMATION:
1933         if(check_col(pinfo->cinfo, COL_INFO))
1934             col_add_fstr(pinfo->cinfo, COL_INFO, "Clear Conf. VC:%d", vc);
1935         if (x25_tree) {
1936             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
1937             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
1938                     X25_CLEAR_CONFIRMATION);
1939         }
1940         localoffset = x25_pkt_len;
1941
1942         if (localoffset < tvb_reported_length(tvb)) { /* extended clear conf format */
1943             if (toa)
1944                 x25_toa(x25_tree, &localoffset, tvb, pinfo);
1945             else
1946                 x25_ntoa(x25_tree, &localoffset, tvb, pinfo, FALSE);
1947         }
1948
1949         if (localoffset < tvb_reported_length(tvb)) /* facilities */
1950             dump_facilities(x25_tree, &localoffset, tvb);
1951         break;
1952     case X25_DIAGNOSTIC:
1953         if(check_col(pinfo->cinfo, COL_INFO)) {
1954             col_add_fstr(pinfo->cinfo, COL_INFO, "Diag. %d",
1955                     (int)tvb_get_guint8(tvb, 3));
1956         }
1957         if (x25_tree) {
1958             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
1959                     X25_DIAGNOSTIC);
1960             proto_tree_add_text(x25_tree, tvb, 3, 1,
1961                     "Diagnostic : %d", (int)tvb_get_guint8(tvb, 3));
1962         }
1963         localoffset = x25_pkt_len;
1964         break;
1965     case X25_INTERRUPT:
1966         if(check_col(pinfo->cinfo, COL_INFO))
1967             col_add_fstr(pinfo->cinfo, COL_INFO, "Interrupt VC:%d", vc);
1968         if (x25_tree) {
1969             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
1970             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
1971                     X25_INTERRUPT);
1972         }
1973         localoffset = x25_pkt_len;
1974         break;
1975     case X25_INTERRUPT_CONFIRMATION:
1976         if(check_col(pinfo->cinfo, COL_INFO))
1977             col_add_fstr(pinfo->cinfo, COL_INFO, "Interrupt Conf. VC:%d", vc);
1978         if (x25_tree) {
1979             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
1980             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
1981                     X25_INTERRUPT_CONFIRMATION);
1982         }
1983         localoffset = x25_pkt_len;
1984         break;
1985     case X25_RESET_REQUEST:
1986         switch (dir) {
1987
1988         case X25_FROM_DCE:
1989             short_name = "Reset ind.";
1990             long_name = "Reset indication";
1991             break;
1992
1993         case X25_FROM_DTE:
1994             short_name = "Reset req.";
1995             long_name = "Reset request";
1996             break;
1997
1998         case X25_UNKNOWN:
1999             short_name = "Reset ind./Reset req.";
2000             long_name = "Reset indication/Reset request";
2001             break;
2002         }
2003         if(check_col(pinfo->cinfo, COL_INFO)) {
2004             col_add_fstr(pinfo->cinfo, COL_INFO, "%s VC:%d %s - Diag.:%d",
2005                     short_name, vc, reset_code(tvb_get_guint8(tvb, 3)),
2006                     (int)tvb_get_guint8(tvb, 4));
2007         }
2008         x25_hash_add_proto_end(vc, pinfo->fd->num);
2009         if (x25_tree) {
2010             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
2011             proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
2012                     X25_RESET_REQUEST, "Packet Type: %s", long_name);
2013             proto_tree_add_text(x25_tree, tvb, 3, 1,
2014                     "Cause : %s", reset_code(tvb_get_guint8(tvb, 3)));
2015             proto_tree_add_text(x25_tree, tvb, 4, 1,
2016                     "Diagnostic : %d", (int)tvb_get_guint8(tvb, 4));
2017         }
2018         localoffset = x25_pkt_len;
2019         break;
2020     case X25_RESET_CONFIRMATION:
2021         if(check_col(pinfo->cinfo, COL_INFO))
2022             col_add_fstr(pinfo->cinfo, COL_INFO, "Reset conf. VC:%d", vc);
2023         if (x25_tree) {
2024             proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
2025             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
2026                     X25_RESET_CONFIRMATION);
2027         }
2028         localoffset = x25_pkt_len;
2029         break;
2030     case X25_RESTART_REQUEST:
2031         switch (dir) {
2032
2033         case X25_FROM_DCE:
2034             short_name = "Restart ind.";
2035             long_name = "Restart indication";
2036             break;
2037
2038         case X25_FROM_DTE:
2039             short_name = "Restart req.";
2040             long_name = "Restart request";
2041             break;
2042
2043         case X25_UNKNOWN:
2044             short_name = "Restart ind./Restart req.";
2045             long_name = "Restart indication/Restart request";
2046             break;
2047         }
2048         if(check_col(pinfo->cinfo, COL_INFO)) {
2049             col_add_fstr(pinfo->cinfo, COL_INFO, "%s %s - Diag.:%d",
2050                     short_name,
2051                     restart_code(tvb_get_guint8(tvb, 3)),
2052                     (int)tvb_get_guint8(tvb, 3));
2053         }
2054         if (x25_tree) {
2055             proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
2056                     X25_RESTART_REQUEST, "Packet Type: %s", long_name);
2057             proto_tree_add_text(x25_tree, tvb, 3, 1,
2058                     "Cause : %s", restart_code(tvb_get_guint8(tvb, 3)));
2059             proto_tree_add_text(x25_tree, tvb, 4, 1,
2060                     "Diagnostic : %d", (int)tvb_get_guint8(tvb, 4));
2061         }
2062         localoffset = x25_pkt_len;
2063         break;
2064     case X25_RESTART_CONFIRMATION:
2065         if(check_col(pinfo->cinfo, COL_INFO))
2066             col_set_str(pinfo->cinfo, COL_INFO, "Restart conf.");
2067         if (x25_tree)
2068             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
2069                     X25_RESTART_CONFIRMATION);
2070         localoffset = x25_pkt_len;
2071         break;
2072     case X25_REGISTRATION_REQUEST:
2073         if(check_col(pinfo->cinfo, COL_INFO))
2074             col_set_str(pinfo->cinfo, COL_INFO, "Registration req.");
2075         if (x25_tree)
2076             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
2077                     X25_REGISTRATION_REQUEST);
2078         localoffset = 3;
2079         if (localoffset < x25_pkt_len)
2080             x25_ntoa(x25_tree, &localoffset, tvb, pinfo, TRUE);
2081
2082         if (x25_tree) {
2083             if (localoffset < x25_pkt_len)
2084                 proto_tree_add_text(x25_tree, tvb, localoffset, 1,
2085                         "Registration length: %d",
2086                         tvb_get_guint8(tvb, localoffset) & 0x7F);
2087             if (localoffset+1 < x25_pkt_len)
2088                 proto_tree_add_text(x25_tree, tvb, localoffset+1,
2089                         tvb_get_guint8(tvb, localoffset) & 0x7F,
2090                         "Registration");
2091         }
2092         localoffset = tvb_reported_length(tvb);
2093         break;
2094     case X25_REGISTRATION_CONFIRMATION:
2095         if(check_col(pinfo->cinfo, COL_INFO))
2096             col_set_str(pinfo->cinfo, COL_INFO, "Registration conf.");
2097         if (x25_tree) {
2098             proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
2099                     X25_REGISTRATION_CONFIRMATION);
2100             proto_tree_add_text(x25_tree, tvb, 3, 1,
2101                     "Cause: %s", registration_code(tvb_get_guint8(tvb, 3)));
2102             proto_tree_add_text(x25_tree, tvb, 4, 1,
2103                     "Diagnostic: %s", registration_code(tvb_get_guint8(tvb, 4)));
2104         }
2105         localoffset = 5;
2106         if (localoffset < x25_pkt_len)
2107             x25_ntoa(x25_tree, &localoffset, tvb, pinfo, TRUE);
2108
2109         if (x25_tree) {
2110             if (localoffset < x25_pkt_len)
2111                 proto_tree_add_text(x25_tree, tvb, localoffset, 1,
2112                         "Registration length: %d",
2113                         tvb_get_guint8(tvb, localoffset) & 0x7F);
2114             if (localoffset+1 < x25_pkt_len)
2115                 proto_tree_add_text(x25_tree, tvb, localoffset+1,
2116                         tvb_get_guint8(tvb, localoffset) & 0x7F,
2117                         "Registration");
2118         }
2119         localoffset = tvb_reported_length(tvb);
2120         break;
2121     default :
2122         localoffset = 2;
2123         if (PACKET_IS_DATA(pkt_type))
2124         {
2125             if(check_col(pinfo->cinfo, COL_INFO)) {
2126                 if (modulo == 8)
2127                     col_add_fstr(pinfo->cinfo, COL_INFO,
2128                             "Data VC:%d P(S):%d P(R):%d %s", vc,
2129                             (pkt_type >> 1) & 0x07,
2130                             (pkt_type >> 5) & 0x07,
2131                             (pkt_type & X25_MBIT_MOD8) ? " M" : "");
2132                 else
2133                     col_add_fstr(pinfo->cinfo, COL_INFO,
2134                             "Data VC:%d P(S):%d P(R):%d %s", vc,
2135                             tvb_get_guint8(tvb, localoffset+1) >> 1,
2136                             pkt_type >> 1,
2137                             (tvb_get_guint8(tvb, localoffset+1) & X25_MBIT_MOD128) ? " M" : "");
2138             }
2139             if (x25_tree) {
2140                 proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
2141                         2, bytes0_1);
2142                 if (modulo == 8) {
2143                     proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
2144                             localoffset, 1, pkt_type);
2145                     proto_tree_add_boolean(x25_tree, hf_x25_mbit_mod8, tvb,
2146                             localoffset, 1, pkt_type);
2147                     proto_tree_add_uint(x25_tree, hf_x25_p_s_mod8, tvb,
2148                             localoffset, 1, pkt_type);
2149                     proto_tree_add_uint(x25_tree, hf_x25_type_data, tvb,
2150                             localoffset, 1, pkt_type);
2151                 }
2152                 else {
2153                     proto_tree_add_uint(x25_tree, hf_x25_p_r_mod128, tvb,
2154                             localoffset, 1, pkt_type);
2155                     proto_tree_add_uint(x25_tree, hf_x25_type_data, tvb,
2156                             localoffset, 1, pkt_type);
2157                     proto_tree_add_uint(x25_tree, hf_x25_p_s_mod128, tvb,
2158                             localoffset+1, 1,
2159                             tvb_get_guint8(tvb, localoffset+1));
2160                     proto_tree_add_boolean(x25_tree, hf_x25_mbit_mod128, tvb,
2161                             localoffset+1, 1,
2162                             tvb_get_guint8(tvb, localoffset+1));
2163                 }
2164             }
2165             if (modulo == 8) {
2166                 m_bit_set = pkt_type & X25_MBIT_MOD8;
2167                 localoffset += 1;
2168             } else {
2169                 m_bit_set = tvb_get_guint8(tvb, localoffset+1) & X25_MBIT_MOD128;
2170                 localoffset += 2;
2171             }
2172             payload_len = tvb_reported_length_remaining(tvb, localoffset);
2173             if (reassemble_x25) {
2174                 /*
2175                  * Reassemble received and sent traffic separately.
2176                  * We don't reassemble traffic with an unknown direction
2177                  * at all.
2178                  */
2179                 frag_key = vc;
2180                 if (side) {
2181                     /*
2182                      * OR in an extra bit to distinguish from traffic
2183                      * in the other direction.
2184                      */
2185                     frag_key |= 0x10000;
2186                 }
2187                 fd_head = fragment_add_seq_next(tvb, localoffset, 
2188                                                 pinfo, frag_key,
2189                                                 x25_segment_table,
2190                                                 x25_reassembled_table,
2191                                                 payload_len, m_bit_set);
2192                 pinfo->fragmented = m_bit_set;
2193               
2194                 if (fd_head) {
2195                     if (fd_head->next) {
2196                         proto_item *frag_tree_item;
2197
2198                         /* This is the last packet */
2199                         next_tvb = tvb_new_real_data(fd_head->data, 
2200                                                      fd_head->len,
2201                                                      fd_head->len);
2202                         tvb_set_child_real_data_tvbuff(tvb, next_tvb);
2203                         add_new_data_source(pinfo, next_tvb, "Reassembled X.25");
2204                         show_fragment_seq_tree(fd_head, 
2205                                                &x25_frag_items, 
2206                                                x25_tree, 
2207                                                pinfo, next_tvb, &frag_tree_item);
2208                     }
2209                 }
2210
2211                 if (m_bit_set && next_tvb == NULL) {
2212                     /*
2213                      * This isn't the last packet, so just
2214                      * show it as X.25 user data.
2215                      */
2216                     proto_tree_add_text(x25_tree, tvb, localoffset, -1,
2217                         "User data (%u byte%s)", payload_len,
2218                         plurality(payload_len, "", "s"));
2219                     return;
2220                 }
2221             }
2222             break;
2223         }
2224
2225         /*
2226          * Non-data packets (RR, RNR, REJ).
2227          */
2228         switch (PACKET_TYPE_FC(pkt_type))
2229         {
2230         case X25_RR:
2231             if(check_col(pinfo->cinfo, COL_INFO)) {
2232                 if (modulo == 8)
2233                     col_add_fstr(pinfo->cinfo, COL_INFO, "RR VC:%d P(R):%d",
2234                             vc, (pkt_type >> 5) & 0x07);
2235                 else
2236                     col_add_fstr(pinfo->cinfo, COL_INFO, "RR VC:%d P(R):%d",
2237                             vc, tvb_get_guint8(tvb, localoffset+1) >> 1);
2238             }
2239             if (x25_tree) {
2240                 proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
2241                         2, bytes0_1);
2242                 if (modulo == 8) {
2243                     proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
2244                             localoffset, 1, pkt_type);
2245                     proto_tree_add_uint(x25_tree, hf_x25_type_fc_mod8, tvb,
2246                             localoffset, 1, X25_RR);
2247                 }
2248                 else {
2249                     proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
2250                             localoffset, 1, X25_RR);
2251                     proto_tree_add_item(x25_tree, hf_x25_p_r_mod128, tvb,
2252                             localoffset+1, 1, FALSE);
2253                 }
2254             }
2255             break;
2256
2257         case X25_RNR:
2258             if(check_col(pinfo->cinfo, COL_INFO)) {
2259                 if (modulo == 8)
2260                     col_add_fstr(pinfo->cinfo, COL_INFO, "RNR VC:%d P(R):%d",
2261                             vc, (pkt_type >> 5) & 0x07);
2262                 else
2263                     col_add_fstr(pinfo->cinfo, COL_INFO, "RNR VC:%d P(R):%d",
2264                             vc, tvb_get_guint8(tvb, localoffset+1) >> 1);
2265             }
2266             if (x25_tree) {
2267                 proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
2268                         2, bytes0_1);
2269                 if (modulo == 8) {
2270                     proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
2271                             localoffset, 1, pkt_type);
2272                     proto_tree_add_uint(x25_tree, hf_x25_type_fc_mod8, tvb,
2273                             localoffset, 1, X25_RNR);
2274                 }
2275                 else {
2276                     proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
2277                             localoffset, 1, X25_RNR);
2278                     proto_tree_add_item(x25_tree, hf_x25_p_r_mod128, tvb,
2279                             localoffset+1, 1, FALSE);
2280                 }
2281             }
2282             break;
2283
2284         case X25_REJ:
2285             if(check_col(pinfo->cinfo, COL_INFO)) {
2286                 if (modulo == 8)
2287                     col_add_fstr(pinfo->cinfo, COL_INFO, "REJ VC:%d P(R):%d",
2288                             vc, (pkt_type >> 5) & 0x07);
2289                 else
2290                     col_add_fstr(pinfo->cinfo, COL_INFO, "REJ VC:%d P(R):%d",
2291                             vc, tvb_get_guint8(tvb, localoffset+1) >> 1);
2292             }
2293             if (x25_tree) {
2294                 proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
2295                         2, bytes0_1);
2296                 if (modulo == 8) {
2297                     proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
2298                             localoffset, 1, pkt_type);
2299                     proto_tree_add_uint(x25_tree, hf_x25_type_fc_mod8, tvb,
2300                             localoffset, 1, X25_REJ);
2301                 }
2302                 else {
2303                     proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
2304                             localoffset, 1, X25_REJ);
2305                     proto_tree_add_item(x25_tree, hf_x25_p_r_mod128, tvb,
2306                             localoffset+1, 1, FALSE);
2307                 }
2308             }
2309         }
2310         localoffset += (modulo == 8) ? 1 : 2;
2311     }
2312
2313     if (localoffset >= tvb_reported_length(tvb))
2314       return;
2315     if (pinfo->fragmented)
2316       return;
2317
2318     if (!next_tvb)
2319       next_tvb = tvb_new_subset(tvb, localoffset, -1, -1);
2320
2321     saved_private_data = pinfo->private_data;
2322     pinfo->private_data = &q_bit_set;
2323
2324     /* See if there's already a dissector for this circuit. */
2325     if (try_circuit_dissector(CT_X25, vc, pinfo->fd->num, next_tvb, pinfo,
2326                               tree)) {
2327         pinfo->private_data = saved_private_data;
2328         return; /* found it and dissected it */
2329     }
2330
2331     /* Did the user suggest QLLC/SNA? */
2332     if (payload_is_qllc_sna) {
2333         /* Yes - dissect it as QLLC/SNA. */
2334         if (!pinfo->fd->flags.visited)
2335             x25_hash_add_proto_start(vc, pinfo->fd->num, qllc_handle);
2336         call_dissector(qllc_handle, next_tvb, pinfo, tree);
2337         pinfo->private_data = saved_private_data;
2338         return;
2339     }
2340
2341     /* If the Call Req. has not been captured, let's look at the first
2342        byte of the payload to see if this looks like IP or CLNP. */
2343     switch (tvb_get_guint8(tvb, localoffset)) {
2344
2345     case 0x45:
2346         /* Looks like an IP header */
2347         if (!pinfo->fd->flags.visited)
2348             x25_hash_add_proto_start(vc, pinfo->fd->num, ip_handle);
2349         call_dissector(ip_handle, next_tvb, pinfo, tree);
2350         pinfo->private_data = saved_private_data;
2351         return;
2352
2353     case NLPID_ISO8473_CLNP:
2354         if (!pinfo->fd->flags.visited)
2355             x25_hash_add_proto_start(vc, pinfo->fd->num, clnp_handle);
2356         call_dissector(clnp_handle, next_tvb, pinfo, tree);
2357         pinfo->private_data = saved_private_data;
2358         return;
2359     }
2360
2361     /* Try the heuristic dissectors. */
2362     if (dissector_try_heuristic(x25_heur_subdissector_list, next_tvb, pinfo,
2363                                 tree)) {
2364         pinfo->private_data = saved_private_data;
2365         return;
2366     }
2367
2368     /* All else failed; dissect it as raw data */
2369     call_dissector(data_handle, next_tvb, pinfo, tree);
2370     pinfo->private_data = saved_private_data;
2371 }
2372
2373 /*
2374  * X.25 dissector for use when "pinfo->pseudo_header" points to a
2375  * "struct x25_phdr".
2376  */
2377 static void
2378 dissect_x25_dir(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2379 {
2380     dissect_x25_common(tvb, pinfo, tree,
2381         (pinfo->pseudo_header->x25.flags & FROM_DCE) ? X25_FROM_DCE :
2382                                                        X25_FROM_DTE,
2383         pinfo->pseudo_header->x25.flags & FROM_DCE);
2384 }
2385
2386 /*
2387  * X.25 dissector for use when "pinfo->pseudo_header" doesn't point to a
2388  * "struct x25_phdr".
2389  */
2390 static void
2391 dissect_x25(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2392 {
2393     int direction;
2394
2395     /*
2396      * We don't know if this packet is DTE->DCE or DCE->DCE.
2397      * However, we can, at least, distinguish between the two
2398      * sides of the conversation, based on the addresses and
2399      * ports.
2400      */
2401     direction = CMP_ADDRESS(&pinfo->src, &pinfo->dst);
2402     if (direction == 0)
2403         direction = (pinfo->srcport > pinfo->destport)*2 - 1;
2404     dissect_x25_common(tvb, pinfo, tree, X25_UNKNOWN, direction > 0);
2405 }
2406
2407 static void
2408 x25_reassemble_init(void)
2409 {
2410   fragment_table_init(&x25_segment_table);
2411   reassembled_table_init(&x25_reassembled_table);
2412 }
2413
2414 void
2415 proto_register_x25(void)
2416 {
2417     static hf_register_info hf[] = {
2418         { &hf_x25_gfi,
2419           { "GFI", "x.25.gfi", FT_UINT16, BASE_DEC, NULL, 0xF000,
2420                 "General format identifier", HFILL }},
2421         { &hf_x25_abit,
2422           { "A Bit", "x.25.a", FT_BOOLEAN, 16, NULL, X25_ABIT,
2423                 "Address Bit", HFILL }},
2424         { &hf_x25_qbit,
2425           { "Q Bit", "x.25.q", FT_BOOLEAN, 16, NULL, X25_QBIT,
2426                 "Qualifier Bit", HFILL }},
2427         { &hf_x25_dbit,
2428           { "D Bit", "x.25.d", FT_BOOLEAN, 16, NULL, X25_DBIT,
2429                 "Delivery Confirmation Bit", HFILL }},
2430         { &hf_x25_mod,
2431           { "Modulo", "x.25.mod", FT_UINT16, BASE_DEC, VALS(vals_modulo), 0x3000,
2432                 "Specifies whether the frame is modulo 8 or 128", HFILL }},
2433         { &hf_x25_lcn,
2434           { "Logical Channel", "x.25.lcn", FT_UINT16, BASE_DEC, NULL, 0x0FFF,
2435                 "Logical Channel Number", HFILL }},
2436         { &hf_x25_type,
2437           { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX, VALS(vals_x25_type), 0x0,
2438                 "Packet Type", HFILL }},
2439         { &hf_x25_type_fc_mod8,
2440           { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX, VALS(vals_x25_type), 0x1F,
2441                 "Packet Type", HFILL }},
2442         { &hf_x25_type_data,
2443           { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX, VALS(vals_x25_type), 0x01,
2444                 "Packet Type", HFILL }},
2445         { &hf_x25_p_r_mod8,
2446           { "P(R)", "x.25.p_r", FT_UINT8, BASE_DEC, NULL, 0xE0,
2447                 "Packet Receive Sequence Number", HFILL }},
2448         { &hf_x25_p_r_mod128,
2449           { "P(R)", "x.25.p_r", FT_UINT8, BASE_DEC, NULL, 0xFE,
2450                 "Packet Receive Sequence Number", HFILL }},
2451         { &hf_x25_mbit_mod8,
2452           { "M Bit", "x.25.m", FT_BOOLEAN, 8, TFS(&m_bit_tfs), X25_MBIT_MOD8,
2453                 "More Bit", HFILL }},
2454         { &hf_x25_mbit_mod128,
2455           { "M Bit", "x.25.m", FT_BOOLEAN, 8, TFS(&m_bit_tfs), X25_MBIT_MOD128,
2456                 "More Bit", HFILL }},
2457         { &hf_x25_p_s_mod8,
2458           { "P(S)", "x.25.p_s", FT_UINT8, BASE_DEC, NULL, 0x0E,
2459                 "Packet Send Sequence Number", HFILL }},
2460         { &hf_x25_p_s_mod128,
2461           { "P(S)", "x.25.p_s", FT_UINT8, BASE_DEC, NULL, 0xFE,
2462                 "Packet Send Sequence Number", HFILL }},
2463         { &hf_x25_segment_overlap,
2464           { "Fragment overlap", "x25.fragment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2465             "Fragment overlaps with other fragments", HFILL }},
2466         
2467         { &hf_x25_segment_overlap_conflict,
2468           { "Conflicting data in fragment overlap",     "x25.fragment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2469             "Overlapping fragments contained conflicting data", HFILL }},
2470         
2471         { &hf_x25_segment_multiple_tails,
2472           { "Multiple tail fragments found",    "x25.fragment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2473             "Several tails were found when defragmenting the packet", HFILL }},
2474         
2475         { &hf_x25_segment_too_long_segment,
2476           { "Fragment too long",        "x25.fragment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2477             "Fragment contained data past end of packet", HFILL }},
2478         
2479         { &hf_x25_segment_error,
2480           { "Defragmentation error", "x25.fragment.error", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2481             "Defragmentation error due to illegal fragments", HFILL }},
2482         
2483         { &hf_x25_segment,
2484           { "X.25 Fragment", "x25.fragment", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2485             "X25 Fragment", HFILL }},
2486         
2487         { &hf_x25_segments,
2488           { "X.25 Fragments", "x25.fragments", FT_NONE, BASE_NONE, NULL, 0x0,
2489             "X.25 Fragments", HFILL }},
2490     };
2491     static gint *ett[] = {
2492         &ett_x25,
2493         &ett_x25_gfi,
2494         &ett_x25_fac,
2495         &ett_x25_fac_unknown,
2496         &ett_x25_fac_mark,
2497         &ett_x25_fac_reverse,
2498         &ett_x25_fac_throughput,
2499         &ett_x25_fac_cug,
2500         &ett_x25_fac_called_modif,
2501         &ett_x25_fac_cug_outgoing_acc,
2502         &ett_x25_fac_throughput_min,
2503         &ett_x25_fac_express_data,
2504         &ett_x25_fac_bilateral_cug,
2505         &ett_x25_fac_packet_size,
2506         &ett_x25_fac_window_size,
2507         &ett_x25_fac_rpoa_selection,
2508         &ett_x25_fac_transit_delay,
2509         &ett_x25_fac_call_transfer,
2510         &ett_x25_fac_called_addr_ext,
2511         &ett_x25_fac_ete_transit_delay,
2512         &ett_x25_fac_calling_addr_ext,
2513         &ett_x25_fac_call_deflect,
2514         &ett_x25_fac_priority,
2515         &ett_x25_user_data,
2516         &ett_x25_segment,
2517         &ett_x25_segments
2518     };
2519     module_t *x25_module;
2520
2521     proto_x25 = proto_register_protocol ("X.25", "X.25", "x.25");
2522     proto_register_field_array (proto_x25, hf, array_length(hf));
2523     proto_register_subtree_array(ett, array_length(ett));
2524
2525     x25_subdissector_table = register_dissector_table("x.25.spi",
2526         "X.25 secondary protocol identifier", FT_UINT8, BASE_HEX);
2527     register_heur_dissector_list("x.25", &x25_heur_subdissector_list);
2528
2529     register_dissector("x.25_dir", dissect_x25_dir, proto_x25);
2530     register_dissector("x.25", dissect_x25, proto_x25);
2531
2532     /* Preferences */
2533     x25_module = prefs_register_protocol(proto_x25, NULL);
2534     prefs_register_obsolete_preference(x25_module, "non_q_bit_is_sna");
2535     prefs_register_bool_preference(x25_module, "payload_is_qllc_sna",
2536             "Default to QLLC/SNA",
2537             "If CALL REQUEST not seen or didn't specify protocol, dissect as QLLC/SNA",
2538             &payload_is_qllc_sna);
2539     prefs_register_bool_preference(x25_module, "reassemble",
2540                                    "Reassemble fragmented X.25 packets",
2541                                    "Reassemble fragmented X.25 packets",
2542                                    &reassemble_x25);
2543     register_init_routine(&x25_reassemble_init);
2544 }
2545
2546 void
2547 proto_reg_handoff_x25(void)
2548 {
2549     dissector_handle_t x25_handle;
2550
2551     /*
2552      * Get handles for various dissectors.
2553      */
2554     ip_handle = find_dissector("ip");
2555     clnp_handle = find_dissector("clnp");
2556     ositp_handle = find_dissector("ositp");
2557     qllc_handle = find_dissector("qllc");
2558     data_handle = find_dissector("data");
2559
2560     x25_handle = find_dissector("x.25");
2561     dissector_add("llc.dsap", SAP_X25, x25_handle);
2562 }