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