Add compat_macros.h to the tarball
[obnox/wireshark/wip.git] / packet-sna.c
1 /* packet-sna.c
2  * Routines for SNA
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id: packet-sna.c,v 1.43 2002/09/23 21:58:22 gram Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
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 <glib.h>
31 #include <epan/packet.h>
32 #include "llcsaps.h"
33 #include "ppptypes.h"
34 #include <epan/sna-utils.h>
35 #include "prefs.h"
36 #include "reassemble.h"
37
38 /*
39  * http://www.wanresources.com/snacell.html
40  * ftp://ftp.software.ibm.com/networking/pub/standards/aiw/formats/
41  *
42  */
43
44 static int proto_sna = -1;
45 static int hf_sna_th = -1;
46 static int hf_sna_th_0 = -1;
47 static int hf_sna_th_fid = -1;
48 static int hf_sna_th_mpf = -1;
49 static int hf_sna_th_odai = -1;
50 static int hf_sna_th_efi = -1;
51 static int hf_sna_th_daf = -1;
52 static int hf_sna_th_oaf = -1;
53 static int hf_sna_th_snf = -1;
54 static int hf_sna_th_dcf = -1;
55 static int hf_sna_th_lsid = -1;
56 static int hf_sna_th_tg_sweep = -1;
57 static int hf_sna_th_er_vr_supp_ind = -1;
58 static int hf_sna_th_vr_pac_cnt_ind = -1;
59 static int hf_sna_th_ntwk_prty = -1;
60 static int hf_sna_th_tgsf = -1;
61 static int hf_sna_th_mft = -1;
62 static int hf_sna_th_piubf = -1;
63 static int hf_sna_th_iern = -1;
64 static int hf_sna_th_nlpoi = -1;
65 static int hf_sna_th_nlp_cp = -1;
66 static int hf_sna_th_ern = -1;
67 static int hf_sna_th_vrn = -1;
68 static int hf_sna_th_tpf = -1;
69 static int hf_sna_th_vr_cwi = -1;
70 static int hf_sna_th_tg_nonfifo_ind = -1;
71 static int hf_sna_th_vr_sqti = -1;
72 static int hf_sna_th_tg_snf = -1;
73 static int hf_sna_th_vrprq = -1;
74 static int hf_sna_th_vrprs = -1;
75 static int hf_sna_th_vr_cwri = -1;
76 static int hf_sna_th_vr_rwi = -1;
77 static int hf_sna_th_vr_snf_send = -1;
78 static int hf_sna_th_dsaf = -1;
79 static int hf_sna_th_osaf = -1;
80 static int hf_sna_th_snai = -1;
81 static int hf_sna_th_def = -1;
82 static int hf_sna_th_oef = -1;
83 static int hf_sna_th_sa = -1;
84 static int hf_sna_th_cmd_fmt = -1;
85 static int hf_sna_th_cmd_type = -1;
86 static int hf_sna_th_cmd_sn = -1;
87
88 static int hf_sna_nlp_nhdr = -1;
89 static int hf_sna_nlp_nhdr_0 = -1;
90 static int hf_sna_nlp_sm = -1;
91 static int hf_sna_nlp_tpf = -1;
92 static int hf_sna_nlp_nhdr_1 = -1;
93 static int hf_sna_nlp_ft = -1;
94 static int hf_sna_nlp_tspi = -1;
95 static int hf_sna_nlp_slowdn1 = -1;
96 static int hf_sna_nlp_slowdn2 = -1;
97 static int hf_sna_nlp_fra = -1;
98 static int hf_sna_nlp_anr = -1;
99 static int hf_sna_nlp_frh = -1;
100 static int hf_sna_nlp_thdr = -1;
101 static int hf_sna_nlp_tcid = -1;
102 static int hf_sna_nlp_thdr_8 = -1;
103 static int hf_sna_nlp_setupi = -1;
104 static int hf_sna_nlp_somi = -1;
105 static int hf_sna_nlp_eomi = -1;
106 static int hf_sna_nlp_sri = -1;
107 static int hf_sna_nlp_rasapi = -1;
108 static int hf_sna_nlp_retryi = -1;
109 static int hf_sna_nlp_thdr_9 = -1;
110 static int hf_sna_nlp_lmi = -1;
111 static int hf_sna_nlp_cqfi = -1;
112 static int hf_sna_nlp_osi = -1;
113 static int hf_sna_nlp_offset = -1;
114 static int hf_sna_nlp_dlf = -1;
115 static int hf_sna_nlp_bsn = -1;
116
117 static int hf_sna_rh = -1;
118 static int hf_sna_rh_0 = -1;
119 static int hf_sna_rh_1 = -1;
120 static int hf_sna_rh_2 = -1;
121 static int hf_sna_rh_rri = -1;
122 static int hf_sna_rh_ru_category = -1;
123 static int hf_sna_rh_fi = -1;
124 static int hf_sna_rh_sdi = -1;
125 static int hf_sna_rh_bci = -1;
126 static int hf_sna_rh_eci = -1;
127 static int hf_sna_rh_dr1 = -1;
128 static int hf_sna_rh_lcci = -1;
129 static int hf_sna_rh_dr2 = -1;
130 static int hf_sna_rh_eri = -1;
131 static int hf_sna_rh_rti = -1;
132 static int hf_sna_rh_rlwi = -1;
133 static int hf_sna_rh_qri = -1;
134 static int hf_sna_rh_pi = -1;
135 static int hf_sna_rh_bbi = -1;
136 static int hf_sna_rh_ebi = -1;
137 static int hf_sna_rh_cdi = -1;
138 static int hf_sna_rh_csi = -1;
139 static int hf_sna_rh_edi = -1;
140 static int hf_sna_rh_pdi = -1;
141 static int hf_sna_rh_cebi = -1;
142 /*static int hf_sna_ru = -1;*/
143
144 static gint ett_sna = -1;
145 static gint ett_sna_th = -1;
146 static gint ett_sna_th_fid = -1;
147 static gint ett_sna_nlp_nhdr = -1;
148 static gint ett_sna_nlp_nhdr_0 = -1;
149 static gint ett_sna_nlp_nhdr_1 = -1;
150 static gint ett_sna_nlp_thdr = -1;
151 static gint ett_sna_nlp_thdr_8 = -1;
152 static gint ett_sna_nlp_thdr_9 = -1;
153 static gint ett_sna_rh = -1;
154 static gint ett_sna_rh_0 = -1;
155 static gint ett_sna_rh_1 = -1;
156 static gint ett_sna_rh_2 = -1;
157
158 static dissector_handle_t data_handle;
159
160 /* Defragment fragmented SNA BIUs*/
161 static gboolean sna_defragment = FALSE;
162 static GHashTable *sna_fragment_table = NULL;
163 static GHashTable *sna_reassembled_table = NULL;
164
165 /* Format Identifier */
166 static const value_string sna_th_fid_vals[] = {
167         { 0x0,  "SNA device <--> Non-SNA Device" },
168         { 0x1,  "Subarea Nodes, without ER or VR" },
169         { 0x2,  "Subarea Node <--> PU2" },
170         { 0x3,  "Subarea Node or SNA host <--> Subarea Node" },
171         { 0x4,  "Subarea Nodes, supporting ER and VR" },
172         { 0x5,  "HPR RTP endpoint nodes" },
173         { 0xa,  "HPR NLP Frame Routing" },
174         { 0xb,  "HPR NLP Frame Routing" },
175         { 0xc,  "HPR NLP Automatic Network Routing" },
176         { 0xd,  "HPR NLP Automatic Network Routing" },
177         { 0xf,  "Adjaced Subarea Nodes, supporting ER and VR" },
178         { 0x0,  NULL }
179 };
180
181 /* Mapping Field */
182 #define MPF_MIDDLE_SEGMENT  0
183 #define MPF_LAST_SEGMENT    1
184 #define MPF_FIRST_SEGMENT   2
185 #define MPF_WHOLE_BIU       3
186
187 static const value_string sna_th_mpf_vals[] = {
188         { MPF_MIDDLE_SEGMENT,   "Middle segment of a BIU" },
189         { MPF_LAST_SEGMENT,     "Last segment of a BIU" },
190         { MPF_FIRST_SEGMENT,    "First segment of a BIU" },
191         { MPF_WHOLE_BIU,        "Whole BIU" },
192         { 0,   NULL }
193 };
194
195 /* Expedited Flow Indicator */
196 static const value_string sna_th_efi_vals[] = {
197         { 0, "Normal Flow" },
198         { 1, "Expedited Flow" },
199         { 0x0,  NULL }
200 };
201
202 /* Request/Response Indicator */
203 static const value_string sna_rh_rri_vals[] = {
204         { 0, "Request" },
205         { 1, "Response" },
206         { 0x0,  NULL }
207 };
208
209 /* Request/Response Unit Category */
210 static const value_string sna_rh_ru_category_vals[] = {
211         { 0, "Function Management Data (FMD)" },
212         { 1, "Network Control (NC)" },
213         { 2, "Data Flow Control (DFC)" },
214         { 3, "Session Control (SC)" },
215         { 0x0,  NULL }
216 };
217
218 /* Format Indicator */
219 static const true_false_string sna_rh_fi_truth =
220         { "FM Header", "No FM Header" };
221
222 /* Sense Data Included */
223 static const true_false_string sna_rh_sdi_truth =
224         { "Included", "Not Included" };
225
226 /* Begin Chain Indicator */
227 static const true_false_string sna_rh_bci_truth =
228         { "First in Chain", "Not First in Chain" };
229
230 /* End Chain Indicator */
231 static const true_false_string sna_rh_eci_truth =
232         { "Last in Chain", "Not Last in Chain" };
233
234 /* Lengith-Checked Compression Indicator */
235 static const true_false_string sna_rh_lcci_truth =
236         { "Compressed", "Not Compressed" };
237
238 /* Response Type Indicator */
239 static const true_false_string sna_rh_rti_truth =
240         { "Negative", "Positive" };
241
242 /* Exception Response Indicator */
243 static const true_false_string sna_rh_eri_truth =
244         { "Exception", "Definite" };
245
246 /* Queued Response Indicator */
247 static const true_false_string sna_rh_qri_truth =
248         { "Enqueue response in TC queues", "Response bypasses TC queues" };
249
250 /* Code Selection Indicator */
251 static const value_string sna_rh_csi_vals[] = {
252         { 0, "EBCDIC" },
253         { 1, "ASCII" },
254         { 0x0,  NULL }
255 };
256
257 /* TG Sweep */
258 static const value_string sna_th_tg_sweep_vals[] = {
259         { 0, "This PIU may overtake any PU ahead of it." },
260         { 1, "This PIU does not ovetake any PIU ahead of it." },
261         { 0x0,  NULL }
262 };
263
264 /* ER_VR_SUPP_IND */
265 static const value_string sna_th_er_vr_supp_ind_vals[] = {
266         { 0, "Each node supports ER and VR protocols" },
267         { 1, "Includes at least one node that does not support ER and VR protocols"  },
268         { 0x0,  NULL }
269 };
270
271 /* VR_PAC_CNT_IND */
272 static const value_string sna_th_vr_pac_cnt_ind_vals[] = {
273         { 0, "Pacing count on the VR has not reached 0" },
274         { 1, "Pacing count on the VR has reached 0" },
275         { 0x0,  NULL }
276 };
277
278 /* NTWK_PRTY */
279 static const value_string sna_th_ntwk_prty_vals[] = {
280         { 0, "PIU flows at a lower priority" },
281         { 1, "PIU flows at network priority (highest transmission priority)" },
282         { 0x0,  NULL }
283 };
284
285 /* TGSF */
286 static const value_string sna_th_tgsf_vals[] = {
287         { 0, "Not segmented" },
288         { 1, "Last segment" },
289         { 2, "First segment" },
290         { 3, "Middle segment" },
291         { 0x0,  NULL }
292 };
293
294 /* PIUBF */
295 static const value_string sna_th_piubf_vals[] = {
296         { 0, "Single PIU frame" },
297         { 1, "Last PIU of a multiple PIU frame" },
298         { 2, "First PIU of a multiple PIU frame" },
299         { 3, "Middle PIU of a multiple PIU frame" },
300         { 0x0,  NULL }
301 };
302
303 /* NLPOI */
304 static const value_string sna_th_nlpoi_vals[] = {
305         { 0, "NLP starts within this FID4 TH" },
306         { 1, "NLP byte 0 starts after RH byte 0 following NLP C/P pad" },
307         { 0x0,  NULL }
308 };
309
310 /* TPF */
311 static const value_string sna_th_tpf_vals[] = {
312         { 0, "Low Priority" },
313         { 1, "Medium Priority" },
314         { 2, "High Priority" },
315         { 3, "Network Priority" },
316         { 0x0,  NULL }
317 };
318
319 /* VR_CWI */
320 static const value_string sna_th_vr_cwi_vals[] = {
321         { 0, "Increment window size" },
322         { 1, "Decrement window size" },
323         { 0x0,  NULL }
324 };
325
326 /* TG_NONFIFO_IND */
327 static const true_false_string sna_th_tg_nonfifo_ind_truth =
328         { "TG FIFO is not required", "TG FIFO is required" };
329
330 /* VR_SQTI */
331 static const value_string sna_th_vr_sqti_vals[] = {
332         { 0, "Non-sequenced, Non-supervisory" },
333         { 1, "Non-sequenced, Supervisory" },
334         { 2, "Singly-sequenced" },
335         { 0x0,  NULL }
336 };
337
338 /* VRPRQ */
339 static const true_false_string sna_th_vrprq_truth = {
340         "VR pacing request is sent asking for a VR pacing response",
341         "No VR pacing response is requested",
342 };
343
344 /* VRPRS */
345 static const true_false_string sna_th_vrprs_truth = {
346         "VR pacing response is sent in response to a VRPRQ bit set",
347         "No pacing response sent",
348 };
349
350 /* VR_CWRI */
351 static const value_string sna_th_vr_cwri_vals[] = {
352         { 0, "Increment window size by 1" },
353         { 1, "Decrement window size by 1" },
354         { 0x0,  NULL }
355 };
356
357 /* VR_RWI */
358 static const true_false_string sna_th_vr_rwi_truth = {
359         "Reset window size to the minimum specified in NC_ACTVR",
360         "Do not reset window size",
361 };
362
363 /* Switching Mode */
364 static const value_string sna_nlp_sm_vals[] = {
365         { 5, "Function routing" },
366         { 6, "Automatic network routing" },
367         { 0x0,  NULL }
368 };
369
370 static const true_false_string sna_nlp_tspi_truth =
371         { "Time sensitive", "Not time sensitive" };
372
373 static const true_false_string sna_nlp_slowdn1_truth =
374         { "Minor congestion", "No minor congestion" };
375
376 static const true_false_string sna_nlp_slowdn2_truth =
377         { "Major congestion", "No major congestion" };
378
379 /* Function Type */
380 static const value_string sna_nlp_ft_vals[] = {
381         { 0x10, "LDLC" },
382         { 0x0,  NULL }
383 };
384
385 static const value_string sna_nlp_frh_vals[] = {
386         { 0x03, "XID complete request" },
387         { 0x04, "XID complete response" },
388         { 0x0,  NULL }
389 };
390
391 static const true_false_string sna_nlp_setupi_truth =
392         { "Connection setup segment present", "Connection setup segment not present" };
393
394 static const true_false_string sna_nlp_somi_truth =
395         { "Start of message", "Not start of message" };
396
397 static const true_false_string sna_nlp_eomi_truth =
398         { "End of message", "Not end of message" };
399
400 static const true_false_string sna_nlp_sri_truth =
401         { "Status requested", "No status requested" };
402
403 static const true_false_string sna_nlp_rasapi_truth =
404         { "Reply as soon as possible", "No need to reply as soon as possible" };
405
406 static const true_false_string sna_nlp_retryi_truth =
407         { "Undefined", "Sender will retransmit" };
408
409 static const true_false_string sna_nlp_lmi_truth =
410         { "Last message", "Not last message" };
411
412 static const true_false_string sna_nlp_cqfi_truth =
413         { "CQFI included", "CQFI not included" };
414
415 static const true_false_string sna_nlp_osi_truth =
416         { "Optional segments present", "No optional segments present" };
417
418
419 /* Values to direct the top-most dissector what to dissect
420  * after the TH. */
421 enum next_dissection_enum {
422     stop_here,
423     rh_only,
424     everything
425 };
426
427 typedef enum next_dissection_enum next_dissection_t;
428
429
430 static int  dissect_fid0_1 (tvbuff_t*, packet_info*, proto_tree*);
431 static int  dissect_fid2 (tvbuff_t*, packet_info*, proto_tree*, tvbuff_t**,
432         next_dissection_t*);
433 static int  dissect_fid3 (tvbuff_t*, proto_tree*);
434 static int  dissect_fid4 (tvbuff_t*, packet_info*, proto_tree*);
435 static int  dissect_fid5 (tvbuff_t*, proto_tree*);
436 static int  dissect_fidf (tvbuff_t*, proto_tree*);
437 static void dissect_fid (tvbuff_t*, packet_info*, proto_tree*, proto_tree*);
438 static void dissect_nlp (tvbuff_t*, packet_info*, proto_tree*, proto_tree*);
439 static void dissect_rh (tvbuff_t*, int, proto_tree*);
440
441 static unsigned int
442 mpf_value(guint8 th_byte)
443 {
444         return (th_byte & 0x0c) >> 2;
445 }
446
447
448 static void
449 dissect_sna(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
450 {
451         guint8          fid;
452         proto_tree      *sna_tree = NULL;
453         proto_item      *sna_ti = NULL;
454
455         if (check_col(pinfo->cinfo, COL_PROTOCOL))
456                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SNA");
457         if (check_col(pinfo->cinfo, COL_INFO))
458                 col_clear(pinfo->cinfo, COL_INFO);
459
460         /* SNA data should be printed in EBCDIC, not ASCII */
461         pinfo->fd->flags.encoding = CHAR_EBCDIC;
462
463         if (tree) {
464
465                 /* Don't bother setting length. We'll set it later after we find
466                  * the lengths of TH/RH/RU */
467                 sna_ti = proto_tree_add_item(tree, proto_sna, tvb, 0, -1, FALSE);
468                 sna_tree = proto_item_add_subtree(sna_ti, ett_sna);
469         }
470
471         /* Transmission Header Format Identifier */
472         fid = hi_nibble(tvb_get_guint8(tvb, 0));
473         switch(fid) {
474                 case 0xa:       /* HPR Network Layer Packet */
475                 case 0xb:
476                 case 0xc:
477                 case 0xd:
478                         dissect_nlp(tvb, pinfo, sna_tree, tree);
479                         break;
480                 default:
481                         dissect_fid(tvb, pinfo, sna_tree, tree);
482         }
483 }
484
485 #define RH_LEN  3
486
487 static void
488 dissect_fid(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
489     proto_tree *parent_tree)
490 {
491
492         proto_tree      *th_tree = NULL, *rh_tree = NULL;
493         proto_item      *th_ti = NULL, *rh_ti = NULL;
494         guint8          th_fid;
495         int             th_header_len = 0;
496         int             offset, rh_offset;
497         tvbuff_t        *rh_tvb = NULL;
498     next_dissection_t continue_dissecting = everything;
499
500         /* Transmission Header Format Identifier */
501         th_fid = hi_nibble(tvb_get_guint8(tvb, 0));
502
503         /* Summary information */
504         if (check_col(pinfo->cinfo, COL_INFO))
505                 col_add_str(pinfo->cinfo, COL_INFO,
506                                 val_to_str(th_fid, sna_th_fid_vals, "Unknown FID: %01x"));
507
508         if (tree) {
509
510                 /* --- TH --- */
511                 /* Don't bother setting length. We'll set it later after we find
512                  * the length of TH */
513                 th_ti = proto_tree_add_item(tree, hf_sna_th, tvb,  0, -1, FALSE);
514                 th_tree = proto_item_add_subtree(th_ti, ett_sna_th);
515         }
516
517         /* Get size of TH */
518         switch(th_fid) {
519                 case 0x0:
520                 case 0x1:
521                         th_header_len = dissect_fid0_1(tvb, pinfo, th_tree);
522                         break;
523                 case 0x2:
524                         th_header_len = dissect_fid2(tvb, pinfo, th_tree, &rh_tvb,
525                     &continue_dissecting);
526                         break;
527                 case 0x3:
528                         th_header_len = dissect_fid3(tvb, th_tree);
529                         break;
530                 case 0x4:
531                         th_header_len = dissect_fid4(tvb, pinfo, th_tree);
532                         break;
533                 case 0x5:
534                         th_header_len = dissect_fid5(tvb, th_tree);
535                         break;
536                 case 0xf:
537                         th_header_len = dissect_fidf(tvb, th_tree);
538                         break;
539                 default:
540                         call_dissector(data_handle,
541                             tvb_new_subset(tvb, 1, -1, -1), pinfo, parent_tree);
542                         return;
543         }
544
545         offset = th_header_len;
546
547     /* Short-circuit ? */
548     if (continue_dissecting == stop_here) {
549         if (tree) {
550             proto_tree_add_text(tree, tvb, offset, -1,
551                     "BIU segment data");
552         }
553         return;
554     }
555
556
557         /* If the FID dissector function didn't create an rh_tvb, then we just
558          * use the rest of our tvbuff as the rh_tvb. */
559         if (!rh_tvb) {
560                 rh_tvb = tvb_new_subset(tvb, offset, -1, -1);
561         }
562         rh_offset = 0;
563
564         /* Process the rest of the SNA packet, starting with RH */
565         if (tree) {
566
567                 proto_item_set_len(th_ti, th_header_len);
568
569                 /* --- RH --- */
570                 rh_ti = proto_tree_add_item(tree, hf_sna_rh, rh_tvb, rh_offset, RH_LEN, FALSE);
571                 rh_tree = proto_item_add_subtree(rh_ti, ett_sna_rh);
572                 dissect_rh(rh_tvb, rh_offset, rh_tree);
573         }
574
575
576         rh_offset += RH_LEN;
577
578         if (tvb_offset_exists(rh_tvb, rh_offset+1)) {
579         /* Short-circuit ? */
580         if (continue_dissecting == rh_only) {
581             if (tree) {
582                 proto_tree_add_text(tree, rh_tvb, rh_offset, -1,
583                         "BIU segment data");
584             }
585             return;
586         }
587
588                 call_dissector(data_handle, tvb_new_subset(rh_tvb, rh_offset, -1, -1),
589                     pinfo, parent_tree);
590         }
591 }
592
593 #define FIRST_FRAG_NUMBER       0
594 #define MIDDLE_FRAG_NUMBER      1
595 #define LAST_FRAG_NUMBER        2
596
597 /* FID2 is defragged by sequence. The weird thing is that we have neither
598  * absolute sequence numbers, nor byte offets. Other FIDs have byte offsets
599  * (the DCF field), but not FID2. The only thing we have to go with is "FIRST",
600  * "MIDDLE", or "LAST". If the BIU is split into 3 frames, then everything is
601  * fine, * "FIRST", "MIDDLE", and "LAST" map nicely onto frag-number 0, 1,
602  * and 2. However, if the BIU is split into 2 frames, then we only have
603  * "FIRST" and "LAST", and the mapping *should* be frag-number 0 and 1,
604  * *NOT* 0 and 2.
605  *
606  * The SNA docs say "FID2 PIUs cannot be blocked because there is no DCF in the
607  * TH format for deblocking" (note on Figure 4-2 in the IBM SNA documention,
608  * see the FTP URL in the comment near the top of this file). I *think*
609  * this means that the fragmented frames cannot arrive out of order.
610  * Well, I *want* it to mean this, because w/o this limitation, if you
611  * get a "FIRST" frame and a "LAST" frame, how long should you wait to
612  * see if a "MIDDLE" frame every arrives????? Thus, if frames *have* to
613  * arrive in order, then we're saved.
614  *
615  * The problem then boils down to figuring out if "LAST" means frag-number 1
616  * (in the case of a BIU split into 2 frames) or frag-number 2
617  * (in the case of a BIU split into 3 frames).
618  *
619  * Assuming fragmented FID2 BIU frames *do* arrive in order, the obvious
620  * way to handle the mapping of "LAST" to either frag-number 1 or
621  * frag-number 2 is to keep a hash which tracks the frames seen, etc.
622  * This consumes resources. A trickier way, but a way which works, is to
623  * always map the "LAST" BIU segment to frag-number 2. Here's the trickery:
624  * if we add frag-number 2, which we know to be the "LAST" BIU segment,
625  * and the reassembly code tells us that the the BIU is still not reassmebled,
626  * then, owing to the, ahem, /fact/, that fragmented BIU segments arrive
627  * in order :), we know that 1) "FIRST" did come, and 2) there's no "MIDDLE",
628  * because this BIU was fragmented into 2 frames, not 3. So, we'll be
629  * tricky and add a zero-length "MIDDLE" BIU frame (i.e, frag-number 1)
630  * to complete the reassembly.
631  */
632 static tvbuff_t*
633 defragment_by_sequence(packet_info *pinfo, tvbuff_t *tvb, int offset, int mpf, int id)
634 {
635         fragment_data *fd_head;
636         int frag_number = -1;
637         int more_frags = TRUE;
638         tvbuff_t *rh_tvb = NULL;
639
640         /* Determine frag_number and more_frags */
641         switch(mpf) {
642                 case MPF_WHOLE_BIU:
643                         /* nothing */
644                         break;
645                 case MPF_FIRST_SEGMENT:
646                         frag_number = FIRST_FRAG_NUMBER;
647                         break;
648                 case MPF_MIDDLE_SEGMENT:
649                         frag_number = MIDDLE_FRAG_NUMBER;
650                         break;
651                 case MPF_LAST_SEGMENT:
652                         frag_number = LAST_FRAG_NUMBER;
653                         more_frags = FALSE;
654                         break;
655                 default:
656                         g_assert_not_reached();
657         }
658
659         /* If sna_defragment is on, and this is a fragment.. */
660         if (frag_number > -1) {
661
662                 /* XXX - check length ??? */
663                 fd_head = fragment_add_seq(tvb, offset, pinfo, id,
664                                 sna_fragment_table,
665                                 frag_number,
666                                 tvb_length_remaining(tvb, offset),
667                                 more_frags);
668
669                 /* We added the LAST segment and reassembly didn't complete. Insert
670                  * a zero-length MIDDLE segment to turn a 2-frame BIU-fragmentation
671          * into a 3-frame BIU-fragmentation (empty middle frag).
672          * See above long comment about this trickery. */
673                 if (mpf == MPF_LAST_SEGMENT && !fd_head) {
674                         fd_head = fragment_add_seq(tvb, offset, pinfo, id,
675                                         sna_fragment_table, MIDDLE_FRAG_NUMBER,
676                                         0, TRUE);
677                 }
678
679                 if (fd_head != NULL) {
680                         /* We have the complete reassembled payload. */
681                         rh_tvb = tvb_new_real_data(fd_head->data,
682                                         fd_head->len, fd_head->len);
683
684                         /* Add the tvbuff to the chain of tvbuffs so that
685                          * it will get cleaned up too. */
686                         tvb_set_child_real_data_tvbuff(tvb, rh_tvb);
687
688                         /* Add the defragmented data to the data source list. */
689                         add_new_data_source(pinfo, rh_tvb, "Reassembled SNA BIU");
690                 }
691         }
692         return rh_tvb;
693 }
694
695 #define SNA_FID01_ADDR_LEN      2
696
697 /* FID Types 0 and 1 */
698 static int
699 dissect_fid0_1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
700 {
701         proto_tree      *bf_tree;
702         proto_item      *bf_item;
703         guint8          th_0;
704         const guint8    *ptr;
705
706         const int bytes_in_header = 10;
707
708         if (tree) {
709                 /* Byte 0 */
710                 th_0 = tvb_get_guint8(tvb, 0);
711                 bf_item = proto_tree_add_uint(tree, hf_sna_th_0, tvb, 0, 1, th_0);
712                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
713
714                 proto_tree_add_uint(bf_tree, hf_sna_th_fid, tvb, 0, 1, th_0);
715                 proto_tree_add_uint(bf_tree, hf_sna_th_mpf, tvb, 0, 1, th_0);
716                 proto_tree_add_uint(bf_tree, hf_sna_th_efi, tvb, 0, 1, th_0);
717
718                 /* Byte 1 */
719                 proto_tree_add_text(tree, tvb, 1, 1, "Reserved");
720
721                 /* Bytes 2-3 */
722                 proto_tree_add_item(tree, hf_sna_th_daf, tvb, 2, 2, FALSE);
723         }
724
725         /* Set DST addr */
726         ptr = tvb_get_ptr(tvb, 2, SNA_FID01_ADDR_LEN);
727         SET_ADDRESS(&pinfo->net_dst, AT_SNA, SNA_FID01_ADDR_LEN, ptr);
728         SET_ADDRESS(&pinfo->dst, AT_SNA, SNA_FID01_ADDR_LEN, ptr);
729
730         if (tree) {
731                 proto_tree_add_item(tree, hf_sna_th_oaf, tvb, 4, 2, FALSE);
732         }
733
734         /* Set SRC addr */
735         ptr = tvb_get_ptr(tvb, 4, SNA_FID01_ADDR_LEN);
736         SET_ADDRESS(&pinfo->net_src, AT_SNA, SNA_FID01_ADDR_LEN, ptr);
737         SET_ADDRESS(&pinfo->src, AT_SNA, SNA_FID01_ADDR_LEN, ptr);
738
739         /* If we're not filling a proto_tree, return now */
740         if (tree) {
741                 return bytes_in_header;
742         }
743
744         proto_tree_add_item(tree, hf_sna_th_snf, tvb, 6, 2, FALSE);
745         proto_tree_add_item(tree, hf_sna_th_dcf, tvb, 8, 2, FALSE);
746
747         return bytes_in_header;
748 }
749
750 #define SNA_FID2_ADDR_LEN       1
751
752 /* FID Type 2 */
753 static int
754 dissect_fid2(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
755         tvbuff_t **rh_tvb_ptr, next_dissection_t *continue_dissecting)
756 {
757         proto_tree      *bf_tree;
758         proto_item      *bf_item;
759         guint8          th_0=0, daf=0, oaf=0;
760         const guint8    *ptr;
761         unsigned int    mpf, id;
762
763         const int bytes_in_header = 6;
764
765         th_0 = tvb_get_guint8(tvb, 0);
766         mpf = mpf_value(th_0);
767
768         if (tree) {
769                 daf = tvb_get_guint8(tvb, 2);
770                 oaf = tvb_get_guint8(tvb, 3);
771
772                 /* Byte 0 */
773                 bf_item = proto_tree_add_uint(tree, hf_sna_th_0, tvb, 0, 1, th_0);
774                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
775
776                 proto_tree_add_uint(bf_tree, hf_sna_th_fid, tvb, 0, 1, th_0);
777                 proto_tree_add_uint(bf_tree, hf_sna_th_mpf, tvb, 0, 1, th_0);
778                 proto_tree_add_uint(bf_tree, hf_sna_th_odai,tvb, 0, 1, th_0);
779                 proto_tree_add_uint(bf_tree, hf_sna_th_efi, tvb, 0, 1, th_0);
780
781
782                 /* Byte 1 */
783                 proto_tree_add_text(tree, tvb, 1, 1, "Reserved");
784
785                 /* Byte 2 */
786                 proto_tree_add_uint_format(tree, hf_sna_th_daf, tvb, 2, 1, daf,
787                                 "Destination Address Field: 0x%02x", daf);
788         }
789
790         /* Set DST addr */
791         ptr = tvb_get_ptr(tvb, 2, SNA_FID2_ADDR_LEN);
792         SET_ADDRESS(&pinfo->net_dst, AT_SNA, SNA_FID2_ADDR_LEN, ptr);
793         SET_ADDRESS(&pinfo->dst, AT_SNA, SNA_FID2_ADDR_LEN, ptr);
794
795         if (tree) {
796                 /* Byte 3 */
797                 proto_tree_add_uint_format(tree, hf_sna_th_oaf, tvb, 3, 1, oaf,
798                                 "Origin Address Field: 0x%02x", oaf);
799         }
800
801         /* Set SRC addr */
802         ptr = tvb_get_ptr(tvb, 3, SNA_FID2_ADDR_LEN);
803         SET_ADDRESS(&pinfo->net_src, AT_SNA, SNA_FID2_ADDR_LEN, ptr);
804         SET_ADDRESS(&pinfo->src, AT_SNA, SNA_FID2_ADDR_LEN, ptr);
805
806         id = tvb_get_ntohs(tvb, 4);
807         if (tree) {
808                 proto_tree_add_uint(tree, hf_sna_th_snf, tvb, 4, 2, id);
809         }
810
811     if (mpf != MPF_WHOLE_BIU && !sna_defragment) {
812         if (mpf == MPF_FIRST_SEGMENT) {
813             *continue_dissecting = rh_only;
814         }
815         else {
816             *continue_dissecting = stop_here;
817         }
818
819     }
820     else if (sna_defragment) {
821         *rh_tvb_ptr = defragment_by_sequence(pinfo, tvb, bytes_in_header,
822                 mpf, id);
823     }
824
825         return bytes_in_header;
826 }
827
828 /* FID Type 3 */
829 static int
830 dissect_fid3(tvbuff_t *tvb, proto_tree *tree)
831 {
832         proto_tree      *bf_tree;
833         proto_item      *bf_item;
834         guint8          th_0;
835
836         const int bytes_in_header = 2;
837
838         /* If we're not filling a proto_tree, return now */
839         if (!tree) {
840                 return bytes_in_header;
841         }
842
843         th_0 = tvb_get_guint8(tvb, 0);
844
845         /* Create the bitfield tree */
846         bf_item = proto_tree_add_uint(tree, hf_sna_th_0, tvb, 0, 1, th_0);
847         bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
848
849         proto_tree_add_uint(bf_tree, hf_sna_th_fid, tvb, 0, 1, th_0);
850         proto_tree_add_uint(bf_tree, hf_sna_th_mpf, tvb, 0, 1, th_0);
851         proto_tree_add_uint(bf_tree, hf_sna_th_efi, tvb, 0, 1, th_0);
852
853         proto_tree_add_item(tree, hf_sna_th_lsid, tvb, 1, 1, FALSE);
854
855         return bytes_in_header;
856 }
857
858
859 static int
860 dissect_fid4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
861 {
862         proto_tree      *bf_tree;
863         proto_item      *bf_item;
864         int             offset = 0;
865         guint8          th_byte, mft;
866         guint16         th_word;
867         guint16         def, oef;
868         guint32         dsaf, osaf;
869         static struct sna_fid_type_4_addr src, dst;
870
871         const int bytes_in_header = 26;
872
873         /* If we're not filling a proto_tree, return now */
874         if (!tree) {
875                 return bytes_in_header;
876         }
877
878         if (tree) {
879                 th_byte = tvb_get_guint8(tvb, offset);
880
881                 /* Create the bitfield tree */
882                 bf_item = proto_tree_add_uint(tree, hf_sna_th_0, tvb, offset, 1, th_byte);
883                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
884
885                 /* Byte 0 */
886                 proto_tree_add_uint(bf_tree, hf_sna_th_fid, tvb, offset, 1, th_byte);
887                 proto_tree_add_uint(bf_tree, hf_sna_th_tg_sweep, tvb, offset, 1, th_byte);
888                 proto_tree_add_uint(bf_tree, hf_sna_th_er_vr_supp_ind, tvb, offset, 1, th_byte);
889                 proto_tree_add_uint(bf_tree, hf_sna_th_vr_pac_cnt_ind, tvb, offset, 1, th_byte);
890                 proto_tree_add_uint(bf_tree, hf_sna_th_ntwk_prty, tvb, offset, 1, th_byte);
891
892                 offset += 1;
893                 th_byte = tvb_get_guint8(tvb, offset);
894
895                 /* Create the bitfield tree */
896                 bf_item = proto_tree_add_text(tree, tvb, offset, 1, "Transmision Header Byte 1");
897                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
898
899                 /* Byte 1 */
900                 proto_tree_add_uint(bf_tree, hf_sna_th_tgsf, tvb, offset, 1, th_byte);
901                 proto_tree_add_boolean(bf_tree, hf_sna_th_mft, tvb, offset, 1, th_byte);
902                 proto_tree_add_uint(bf_tree, hf_sna_th_piubf, tvb, offset, 1, th_byte);
903
904                 mft = th_byte & 0x04;
905                 offset += 1;
906                 th_byte = tvb_get_guint8(tvb, offset);
907
908                 /* Create the bitfield tree */
909                 bf_item = proto_tree_add_text(tree, tvb, offset, 1, "Transmision Header Byte 2");
910                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
911
912                 /* Byte 2 */
913                 if (mft) {
914                         proto_tree_add_uint(bf_tree, hf_sna_th_nlpoi, tvb, offset, 1, th_byte);
915                         proto_tree_add_uint(bf_tree, hf_sna_th_nlp_cp, tvb, offset, 1, th_byte);
916                 }
917                 else {
918                         proto_tree_add_uint(bf_tree, hf_sna_th_iern, tvb, offset, 1, th_byte);
919                 }
920                 proto_tree_add_uint(bf_tree, hf_sna_th_ern, tvb, offset, 1, th_byte);
921
922                 offset += 1;
923                 th_byte = tvb_get_guint8(tvb, offset);
924
925                 /* Create the bitfield tree */
926                 bf_item = proto_tree_add_text(tree, tvb, offset, 1, "Transmision Header Byte 3");
927                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
928
929                 /* Byte 3 */
930                 proto_tree_add_uint(bf_tree, hf_sna_th_vrn, tvb, offset, 1, th_byte);
931                 proto_tree_add_uint(bf_tree, hf_sna_th_tpf, tvb, offset, 1, th_byte);
932
933                 offset += 1;
934                 th_word = tvb_get_ntohs(tvb, offset);
935
936                 /* Create the bitfield tree */
937                 bf_item = proto_tree_add_text(tree, tvb, offset, 2, "Transmision Header Bytes 4-5");
938                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
939
940                 /* Bytes 4-5 */
941                 proto_tree_add_uint(bf_tree, hf_sna_th_vr_cwi, tvb, offset, 2, th_word);
942                 proto_tree_add_boolean(bf_tree, hf_sna_th_tg_nonfifo_ind, tvb, offset, 2, th_word);
943                 proto_tree_add_uint(bf_tree, hf_sna_th_vr_sqti, tvb, offset, 2, th_word);
944
945                 /* I'm not sure about byte-order on this one... */
946                 proto_tree_add_uint(bf_tree, hf_sna_th_tg_snf, tvb, offset, 2, th_word);
947
948                 offset += 2;
949                 th_word = tvb_get_ntohs(tvb, offset);
950
951                 /* Create the bitfield tree */
952                 bf_item = proto_tree_add_text(tree, tvb, offset, 2, "Transmision Header Bytes 6-7");
953                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
954
955                 /* Bytes 6-7 */
956                 proto_tree_add_boolean(bf_tree, hf_sna_th_vrprq, tvb, offset, 2, th_word);
957                 proto_tree_add_boolean(bf_tree, hf_sna_th_vrprs, tvb, offset, 2, th_word);
958                 proto_tree_add_uint(bf_tree, hf_sna_th_vr_cwri, tvb, offset, 2, th_word);
959                 proto_tree_add_boolean(bf_tree, hf_sna_th_vr_rwi, tvb, offset, 2, th_word);
960
961                 /* I'm not sure about byte-order on this one... */
962                 proto_tree_add_uint(bf_tree, hf_sna_th_vr_snf_send, tvb, offset, 2, th_word);
963
964                 offset += 2;
965         }
966
967         dsaf = tvb_get_ntohl(tvb, 8);
968         if (tree) {
969                 /* Bytes 8-11 */
970                 proto_tree_add_uint(tree, hf_sna_th_dsaf, tvb, offset, 4, dsaf);
971
972                 offset += 4;
973         }
974
975         osaf = tvb_get_ntohl(tvb, 12);
976         if (tree) {
977                 /* Bytes 12-15 */
978                 proto_tree_add_uint(tree, hf_sna_th_osaf, tvb, offset, 4, osaf);
979
980                 offset += 4;
981                 th_byte = tvb_get_guint8(tvb, offset);
982
983                 /* Create the bitfield tree */
984                 bf_item = proto_tree_add_text(tree, tvb, offset, 2, "Transmision Header Byte 16");
985                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
986
987                 /* Byte 16 */
988                 proto_tree_add_boolean(tree, hf_sna_th_snai, tvb, offset, 1, th_byte);
989
990                 /* We luck out here because in their infinite wisdom the SNA
991                  * architects placed the MPF and EFI fields in the same bitfield
992                  * locations, even though for FID4 they're not in byte 0.
993                  * Thank you IBM! */
994                 proto_tree_add_uint(tree, hf_sna_th_mpf, tvb, offset, 1, th_byte);
995                 proto_tree_add_uint(tree, hf_sna_th_efi, tvb, offset, 1, th_byte);
996
997                 offset += 2; /* 1 for byte 16, 1 for byte 17 which is reserved */
998         }
999
1000
1001         def = tvb_get_ntohs(tvb, 18);
1002         if (tree) {
1003                 /* Bytes 18-25 */
1004                 proto_tree_add_uint(tree, hf_sna_th_def, tvb, offset, 2, def);
1005         }
1006
1007         /* Addresses in FID 4 are discontiguous, sigh */
1008         dst.saf = dsaf;
1009         dst.ef = def;
1010         SET_ADDRESS(&pinfo->net_dst, AT_SNA, SNA_FID_TYPE_4_ADDR_LEN, (guint8* )&dst);
1011         SET_ADDRESS(&pinfo->dst, AT_SNA, SNA_FID_TYPE_4_ADDR_LEN, (guint8 *)&dst);
1012
1013
1014         oef = tvb_get_ntohs(tvb, 20);
1015         if (tree) {
1016                 proto_tree_add_uint(tree, hf_sna_th_oef, tvb, offset+2, 2, oef);
1017         }
1018
1019         /* Addresses in FID 4 are discontiguous, sigh */
1020         src.saf = osaf;
1021         src.ef = oef;
1022         SET_ADDRESS(&pinfo->net_src, AT_SNA, SNA_FID_TYPE_4_ADDR_LEN, (guint8 *)&src);
1023         SET_ADDRESS(&pinfo->src, AT_SNA, SNA_FID_TYPE_4_ADDR_LEN, (guint8 *)&src);
1024
1025         if (tree) {
1026                 proto_tree_add_item(tree, hf_sna_th_snf, tvb, offset+4, 2, FALSE);
1027                 proto_tree_add_item(tree, hf_sna_th_dcf, tvb, offset+6, 2, FALSE);
1028         }
1029
1030         return bytes_in_header;
1031 }
1032
1033 /* FID Type 5 */
1034 static int
1035 dissect_fid5(tvbuff_t *tvb, proto_tree *tree)
1036 {
1037         proto_tree      *bf_tree;
1038         proto_item      *bf_item;
1039         guint8          th_0;
1040
1041         const int bytes_in_header = 12;
1042
1043         /* If we're not filling a proto_tree, return now */
1044         if (!tree) {
1045                 return bytes_in_header;
1046         }
1047
1048         th_0 = tvb_get_guint8(tvb, 0);
1049
1050         /* Create the bitfield tree */
1051         bf_item = proto_tree_add_uint(tree, hf_sna_th_0, tvb, 0, 1, th_0);
1052         bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
1053
1054         proto_tree_add_uint(bf_tree, hf_sna_th_fid, tvb, 0, 1, th_0);
1055         proto_tree_add_uint(bf_tree, hf_sna_th_mpf, tvb, 0, 1, th_0);
1056         proto_tree_add_uint(bf_tree, hf_sna_th_efi, tvb, 0, 1, th_0);
1057
1058         proto_tree_add_text(tree, tvb, 1, 1, "Reserved");
1059         proto_tree_add_item(tree, hf_sna_th_snf, tvb, 2, 2, FALSE);
1060
1061         proto_tree_add_item(tree, hf_sna_th_sa, tvb, 4, 8, FALSE);
1062
1063         return bytes_in_header;
1064
1065 }
1066
1067 /* FID Type f */
1068 static int
1069 dissect_fidf(tvbuff_t *tvb, proto_tree *tree)
1070 {
1071         proto_tree      *bf_tree;
1072         proto_item      *bf_item;
1073         guint8          th_0;
1074
1075         const int bytes_in_header = 26;
1076
1077         /* If we're not filling a proto_tree, return now */
1078         if (!tree) {
1079                 return bytes_in_header;
1080         }
1081
1082         th_0 = tvb_get_guint8(tvb, 0);
1083
1084         /* Create the bitfield tree */
1085         bf_item = proto_tree_add_uint(tree, hf_sna_th_0, tvb, 0, 1, th_0);
1086         bf_tree = proto_item_add_subtree(bf_item, ett_sna_th_fid);
1087
1088         proto_tree_add_uint(bf_tree, hf_sna_th_fid, tvb, 0, 1, th_0);
1089         proto_tree_add_text(tree, tvb, 1, 1, "Reserved");
1090
1091         proto_tree_add_item(tree, hf_sna_th_cmd_fmt, tvb,  2, 1, FALSE);
1092         proto_tree_add_item(tree, hf_sna_th_cmd_type, tvb, 3, 1, FALSE);
1093         proto_tree_add_item(tree, hf_sna_th_cmd_sn, tvb,   4, 2, FALSE);
1094
1095         /* Yup, bytes 6-23 are reserved! */
1096         proto_tree_add_text(tree, tvb, 6, 18, "Reserved");
1097
1098         proto_tree_add_item(tree, hf_sna_th_dcf, tvb, 24, 2, FALSE);
1099
1100         return bytes_in_header;
1101 }
1102
1103 /* HPR Network Layer Packet */
1104 static void
1105 dissect_nlp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
1106     proto_tree *parent_tree)
1107 {
1108         proto_tree      *nlp_tree, *bf_tree;
1109         proto_item      *nlp_item, *bf_item, *h_item;
1110         guint8          nhdr_0, nhdr_1, nhdr_x, thdr_8, thdr_9;
1111         guint32         thdr_len, thdr_dlf, thdr_bsn;
1112
1113         int index = 0, counter = 0;
1114
1115         nlp_tree = NULL;
1116         nlp_item = NULL;
1117
1118         nhdr_0 = tvb_get_guint8(tvb, index);
1119         nhdr_1 = tvb_get_guint8(tvb, index+1);
1120
1121         if (check_col(pinfo->cinfo, COL_INFO))
1122                 col_add_str(pinfo->cinfo, COL_INFO, "HPR NLP Packet");
1123
1124         if (tree) {
1125                 /* Don't bother setting length. We'll set it later after we find
1126                  * the lengths of NHDR */
1127                 nlp_item = proto_tree_add_item(tree, hf_sna_nlp_nhdr, tvb, index, -1, FALSE);
1128                 nlp_tree = proto_item_add_subtree(nlp_item, ett_sna_nlp_nhdr);
1129
1130                 bf_item = proto_tree_add_uint(nlp_tree, hf_sna_nlp_nhdr_0, tvb, index, 1, nhdr_0);
1131                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_nlp_nhdr_0);
1132
1133                 proto_tree_add_uint(bf_tree, hf_sna_nlp_sm, tvb, index, 1, nhdr_0);
1134                 proto_tree_add_uint(bf_tree, hf_sna_nlp_tpf, tvb, index, 1, nhdr_0);
1135
1136                 bf_item = proto_tree_add_uint(nlp_tree, hf_sna_nlp_nhdr_1, tvb, index+1, 1, nhdr_1);
1137                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_nlp_nhdr_1);
1138
1139                 proto_tree_add_uint(bf_tree, hf_sna_nlp_ft, tvb, index+1, 1, nhdr_1);
1140                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_tspi, tvb, index+1, 1, nhdr_1);
1141                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_slowdn1, tvb, index+1, 1, nhdr_1);
1142                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_slowdn2, tvb, index+1, 1, nhdr_1);
1143         }
1144         /* ANR or FR lists */
1145
1146         index += 2;
1147         counter = 0;
1148
1149         if ((nhdr_0 & 0xe0) == 0xa0) {
1150                 do {
1151                         nhdr_x = tvb_get_guint8(tvb, index + counter);
1152                         counter ++;
1153                 } while (nhdr_x != 0xff);
1154                 if (tree)
1155                         h_item = proto_tree_add_item(nlp_tree, hf_sna_nlp_fra, tvb, index, counter, FALSE);
1156                 index += counter;
1157
1158                 index++; /* 1 Byte Reserved */
1159
1160                 if (tree) {
1161                         proto_item_set_len(nlp_item, index);
1162                 }
1163                 if ((nhdr_1 & 0x80) == 0x10) {
1164                         nhdr_x = tvb_get_guint8(tvb, index);
1165                         if (tree) {
1166                                 proto_tree_add_uint(tree, hf_sna_nlp_frh, tvb, index, 1, nhdr_x);
1167                         }
1168                         index ++;
1169
1170                         if (tvb_offset_exists(tvb, index+1)) {
1171                                 call_dissector(data_handle,
1172                                         tvb_new_subset(tvb, index, -1, -1),
1173                                         pinfo, parent_tree);
1174                         }
1175                         return;
1176                 }
1177         }
1178         if ((nhdr_0 & 0xe0) == 0xc0) {
1179                 do {
1180                         nhdr_x = tvb_get_guint8(tvb, index + counter);
1181                         counter ++;
1182                 } while (nhdr_x != 0xff);
1183                 if (tree)
1184                         h_item = proto_tree_add_item(nlp_tree, hf_sna_nlp_anr, tvb, index, counter, FALSE);
1185                 index += counter;
1186
1187                 index++; /* 1 Byte Reserved */
1188
1189                 if (tree) {
1190                         proto_item_set_len(nlp_item, index);
1191                 }
1192
1193         }
1194
1195         thdr_8 = tvb_get_guint8(tvb, index+8);
1196         thdr_9 = tvb_get_guint8(tvb, index+9);
1197         thdr_len = tvb_get_ntohs(tvb, index+10);
1198         thdr_dlf = tvb_get_ntohl(tvb, index+12);
1199         thdr_bsn = tvb_get_ntohl(tvb, index+16);
1200
1201         if (tree) {
1202                 /* Don't bother setting length. We'll set it later after we find
1203                  * the lengths of NHDR */
1204                 nlp_item = proto_tree_add_item(tree, hf_sna_nlp_thdr, tvb, index, -1, FALSE);
1205                 nlp_tree = proto_item_add_subtree(nlp_item, ett_sna_nlp_thdr);
1206
1207                 bf_item = proto_tree_add_item(nlp_tree, hf_sna_nlp_tcid, tvb, index, 8, FALSE);
1208
1209                 bf_item = proto_tree_add_uint(nlp_tree, hf_sna_nlp_thdr_8, tvb, index+8, 1, thdr_8);
1210                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_nlp_thdr_8);
1211
1212                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_setupi, tvb, index+8, 1, thdr_8);
1213                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_somi, tvb, index+8, 1, thdr_8);
1214                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_eomi, tvb, index+8, 1, thdr_8);
1215                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_sri, tvb, index+8, 1, thdr_8);
1216                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_rasapi, tvb, index+8, 1, thdr_8);
1217                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_retryi, tvb, index+8, 1, thdr_8);
1218
1219                 bf_item = proto_tree_add_uint(nlp_tree, hf_sna_nlp_thdr_9, tvb, index+9, 1, thdr_9);
1220                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_nlp_thdr_9);
1221
1222                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_lmi, tvb, index+9, 1, thdr_9);
1223                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_cqfi, tvb, index+9, 1, thdr_9);
1224                 proto_tree_add_boolean(bf_tree, hf_sna_nlp_osi, tvb, index+9, 1, thdr_9);
1225
1226                 proto_tree_add_uint(nlp_tree, hf_sna_nlp_offset, tvb, index+10, 2, thdr_len);
1227                 proto_tree_add_uint(nlp_tree, hf_sna_nlp_dlf, tvb, index+12, 4, thdr_dlf);
1228                 proto_tree_add_uint(nlp_tree, hf_sna_nlp_bsn, tvb, index+16, 4, thdr_bsn);
1229
1230                 proto_item_set_len(nlp_item, thdr_len);
1231         }
1232         index += (thdr_len << 2);
1233         if (((thdr_8 & 0x20) == 0) && thdr_dlf) {
1234                 if (check_col(pinfo->cinfo, COL_INFO))
1235                         col_add_str(pinfo->cinfo, COL_INFO, "HPR Fragment");
1236                 if (tvb_offset_exists(tvb, index+1)) {
1237                         call_dissector(data_handle,
1238                                 tvb_new_subset(tvb, index, -1, -1), pinfo,
1239                                 parent_tree);
1240                 }
1241                 return;
1242         }
1243         if (tvb_offset_exists(tvb, index+1)) {
1244                 dissect_fid(tvb_new_subset(tvb, index, -1, -1), pinfo, tree,
1245                         parent_tree);
1246         }
1247 }
1248
1249 /* RH */
1250 static void
1251 dissect_rh(tvbuff_t *tvb, int offset, proto_tree *tree)
1252 {
1253         proto_tree      *bf_tree;
1254         proto_item      *bf_item;
1255         gboolean        is_response;
1256         guint8          rh_0, rh_1, rh_2;
1257
1258
1259         /* Create the bitfield tree for byte 0*/
1260         rh_0 = tvb_get_guint8(tvb, offset);
1261         is_response = (rh_0 & 0x80);
1262
1263         bf_item = proto_tree_add_uint(tree, hf_sna_rh_0, tvb, offset, 1, rh_0);
1264         bf_tree = proto_item_add_subtree(bf_item, ett_sna_rh_0);
1265
1266         proto_tree_add_uint(bf_tree, hf_sna_rh_rri, tvb, offset, 1, rh_0);
1267         proto_tree_add_uint(bf_tree, hf_sna_rh_ru_category, tvb, offset, 1, rh_0);
1268         proto_tree_add_boolean(bf_tree, hf_sna_rh_fi, tvb, offset, 1, rh_0);
1269         proto_tree_add_boolean(bf_tree, hf_sna_rh_sdi, tvb, offset, 1, rh_0);
1270         proto_tree_add_boolean(bf_tree, hf_sna_rh_bci, tvb, offset, 1, rh_0);
1271         proto_tree_add_boolean(bf_tree, hf_sna_rh_eci, tvb, offset, 1, rh_0);
1272
1273         offset += 1;
1274         rh_1 = tvb_get_guint8(tvb, offset);
1275
1276         /* Create the bitfield tree for byte 1*/
1277         bf_item = proto_tree_add_uint(tree, hf_sna_rh_1, tvb, offset, 1, rh_1);
1278         bf_tree = proto_item_add_subtree(bf_item, ett_sna_rh_1);
1279
1280         proto_tree_add_boolean(bf_tree, hf_sna_rh_dr1, tvb,  offset, 1, rh_1);
1281
1282         if (!is_response) {
1283                 proto_tree_add_boolean(bf_tree, hf_sna_rh_lcci, tvb, offset, 1, rh_1);
1284         }
1285
1286         proto_tree_add_boolean(bf_tree, hf_sna_rh_dr2, tvb,  offset, 1, rh_1);
1287
1288         if (is_response) {
1289                 proto_tree_add_boolean(bf_tree, hf_sna_rh_rti, tvb,  offset, 1, rh_1);
1290         }
1291         else {
1292                 proto_tree_add_boolean(bf_tree, hf_sna_rh_eri, tvb,  offset, 1, rh_1);
1293                 proto_tree_add_boolean(bf_tree, hf_sna_rh_rlwi, tvb, offset, 1, rh_1);
1294         }
1295
1296         proto_tree_add_boolean(bf_tree, hf_sna_rh_qri, tvb, offset, 1, rh_1);
1297         proto_tree_add_boolean(bf_tree, hf_sna_rh_pi, tvb,  offset, 1, rh_1);
1298
1299         offset += 1;
1300         rh_2 = tvb_get_guint8(tvb, offset);
1301
1302         /* Create the bitfield tree for byte 2*/
1303         bf_item = proto_tree_add_uint(tree, hf_sna_rh_2, tvb, offset, 1, rh_2);
1304
1305         if (!is_response) {
1306                 bf_tree = proto_item_add_subtree(bf_item, ett_sna_rh_2);
1307
1308                 proto_tree_add_boolean(bf_tree, hf_sna_rh_bbi, tvb,  offset, 1, rh_2);
1309                 proto_tree_add_boolean(bf_tree, hf_sna_rh_ebi, tvb,  offset, 1, rh_2);
1310                 proto_tree_add_boolean(bf_tree, hf_sna_rh_cdi, tvb,  offset, 1, rh_2);
1311                 proto_tree_add_uint(bf_tree, hf_sna_rh_csi, tvb,  offset, 1, rh_2);
1312                 proto_tree_add_boolean(bf_tree, hf_sna_rh_edi, tvb,  offset, 1, rh_2);
1313                 proto_tree_add_boolean(bf_tree, hf_sna_rh_pdi, tvb,  offset, 1, rh_2);
1314                 proto_tree_add_boolean(bf_tree, hf_sna_rh_cebi, tvb, offset, 1, rh_2);
1315         }
1316
1317         /* XXX - check for sdi. If TRUE, the next 4 bytes will be sense data */
1318 }
1319
1320 static void
1321 sna_init(void)
1322 {
1323         fragment_table_init(&sna_fragment_table);
1324         reassembled_table_init(&sna_reassembled_table);
1325 }
1326
1327
1328 void
1329 proto_register_sna(void)
1330 {
1331         static hf_register_info hf[] = {
1332                 { &hf_sna_th,
1333                 { "Transmission Header",        "sna.th", FT_NONE, BASE_NONE, NULL, 0x0,
1334                         "", HFILL }},
1335
1336                 { &hf_sna_th_0,
1337                 { "Transmission Header Byte 0", "sna.th.0", FT_UINT8, BASE_HEX, NULL, 0x0,
1338                         "Byte 0 of Tranmission Header contains FID, MPF, ODAI,"
1339                         " and EFI as bitfields.", HFILL }},
1340
1341                 { &hf_sna_th_fid,
1342                 { "Format Identifer",           "sna.th.fid", FT_UINT8, BASE_HEX, VALS(sna_th_fid_vals), 0xf0,
1343                         "Format Identification", HFILL }},
1344
1345                 { &hf_sna_th_mpf,
1346                 { "Mapping Field",              "sna.th.mpf", FT_UINT8, BASE_DEC, VALS(sna_th_mpf_vals), 0x0c,
1347                         "The Mapping Field specifies whether the information field"
1348                         " associated with the TH is a complete or partial BIU.", HFILL }},
1349
1350                 { &hf_sna_th_odai,
1351                 { "ODAI Assignment Indicator",  "sna.th.odai", FT_UINT8, BASE_DEC, NULL, 0x02,
1352                         "The ODAI indicates which node assigned the OAF'-DAF' values"
1353                         " carried in the TH.", HFILL }},
1354
1355                 { &hf_sna_th_efi,
1356                 { "Expedited Flow Indicator",   "sna.th.efi", FT_UINT8, BASE_DEC, VALS(sna_th_efi_vals), 0x01,
1357                         "The EFI designates whether the PIU belongs to the normal"
1358                         " or expedited flow.", HFILL }},
1359
1360                 { &hf_sna_th_daf,
1361                 { "Destination Address Field",  "sna.th.daf", FT_UINT16, BASE_HEX, NULL, 0x0,
1362                         "", HFILL }},
1363
1364                 { &hf_sna_th_oaf,
1365                 { "Origin Address Field",       "sna.th.oaf", FT_UINT16, BASE_HEX, NULL, 0x0,
1366                         "", HFILL }},
1367
1368                 { &hf_sna_th_snf,
1369                 { "Sequence Number Field",      "sna.th.snf", FT_UINT16, BASE_DEC, NULL, 0x0,
1370                         "The Sequence Number Field contains a numerical identifier for"
1371                         " the associated BIU.", HFILL }},
1372
1373                 { &hf_sna_th_dcf,
1374                 { "Data Count Field",   "sna.th.dcf", FT_UINT16, BASE_DEC, NULL, 0x0,
1375                         "A binary count of the number of bytes in the BIU or BIU segment associated "
1376                         "with the tranmission header. The count does not include any of the bytes "
1377                         "in the transmission header.", HFILL }},
1378
1379                 { &hf_sna_th_lsid,
1380                 { "Local Session Identification",       "sna.th.lsid", FT_UINT8, BASE_HEX, NULL, 0x0,
1381                         "", HFILL }},
1382
1383                 { &hf_sna_th_tg_sweep,
1384                 { "Transmission Group Sweep",           "sna.th.tg_sweep", FT_UINT8, BASE_DEC,
1385                         VALS(sna_th_tg_sweep_vals), 0x08,
1386                         "", HFILL }},
1387
1388                 { &hf_sna_th_er_vr_supp_ind,
1389                 { "ER and VR Support Indicator",        "sna.th.er_vr_supp_ind", FT_UINT8, BASE_DEC,
1390                         VALS(sna_th_er_vr_supp_ind_vals), 0x04,
1391                         "", HFILL }},
1392
1393                 { &hf_sna_th_vr_pac_cnt_ind,
1394                 { "Virtual Route Pacing Count Indicator",       "sna.th.vr_pac_cnt_ind",
1395                         FT_UINT8, BASE_DEC, VALS(sna_th_vr_pac_cnt_ind_vals), 0x02,
1396                         "", HFILL }},
1397
1398                 { &hf_sna_th_ntwk_prty,
1399                 { "Network Priority",   "sna.th.ntwk_prty",
1400                         FT_UINT8, BASE_DEC, VALS(sna_th_ntwk_prty_vals), 0x01,
1401                         "", HFILL }},
1402
1403                 { &hf_sna_th_tgsf,
1404                 { "Transmission Group Segmenting Field",        "sna.th.tgsf",
1405                         FT_UINT8, BASE_HEX, VALS(sna_th_tgsf_vals), 0xc0,
1406                         "", HFILL }},
1407
1408                 { &hf_sna_th_mft,
1409                 { "MPR FID4 Type",      "sna.th.mft", FT_BOOLEAN, BASE_NONE, NULL, 0x04,
1410                         "", HFILL }},
1411
1412                 { &hf_sna_th_piubf,
1413                 { "PIU Blocking Field", "sna.th.piubf", FT_UINT8, BASE_HEX,
1414                         VALS(sna_th_piubf_vals), 0x03,
1415                         "Specifies whether this frame contains a single PIU or multiple PIUs.", HFILL }},
1416
1417                 { &hf_sna_th_iern,
1418                 { "Initial Explicit Route Number",      "sna.th.iern", FT_UINT8, BASE_DEC, NULL, 0xf0,
1419                         "", HFILL }},
1420
1421                 { &hf_sna_th_nlpoi,
1422                 { "NLP Offset Indicator",       "sna.th.nlpoi", FT_UINT8, BASE_DEC,
1423                         VALS(sna_th_nlpoi_vals), 0x80,
1424                         "", HFILL }},
1425
1426                 { &hf_sna_th_nlp_cp,
1427                 { "NLP Count or Padding",       "sna.th.nlp_cp", FT_UINT8, BASE_DEC, NULL, 0x70,
1428                         "", HFILL }},
1429
1430                 { &hf_sna_th_ern,
1431                 { "Explicit Route Number",      "sna.th.ern", FT_UINT8, BASE_DEC, NULL, 0x0f,
1432                         "The ERN in a TH identifies an explicit route direction of flow.", HFILL }},
1433
1434                 { &hf_sna_th_vrn,
1435                 { "Virtual Route Number",       "sna.th.vrn", FT_UINT8, BASE_DEC, NULL, 0xf0,
1436                         "", HFILL }},
1437
1438                 { &hf_sna_th_tpf,
1439                 { "Transmission Priority Field",        "sna.th.tpf", FT_UINT8, BASE_HEX,
1440                         VALS(sna_th_tpf_vals), 0x03,
1441                         "", HFILL }},
1442
1443                 { &hf_sna_th_vr_cwi,
1444                 { "Virtual Route Change Window Indicator",      "sna.th.vr_cwi", FT_UINT16, BASE_DEC,
1445                         VALS(sna_th_vr_cwi_vals), 0x8000,
1446                         "Used to change the window size of the virtual route by 1.", HFILL }},
1447
1448                 { &hf_sna_th_tg_nonfifo_ind,
1449                 { "Transmission Group Non-FIFO Indicator",      "sna.th.tg_nonfifo_ind", FT_BOOLEAN, 16,
1450                         TFS(&sna_th_tg_nonfifo_ind_truth), 0x4000,
1451                         "Indicates whether or not FIFO discipline is to enforced in "
1452                         "transmitting PIUs through the tranmission groups to prevent the PIUs "
1453                         "getting out of sequence during transmission over the TGs.", HFILL }},
1454
1455                 { &hf_sna_th_vr_sqti,
1456                 { "Virtual Route Sequence and Type Indicator",  "sna.th.vr_sqti", FT_UINT16, BASE_HEX,
1457                         VALS(sna_th_vr_sqti_vals), 0x3000,
1458                         "Specifies the PIU type.", HFILL }},
1459
1460                 { &hf_sna_th_tg_snf,
1461                 { "Transmission Group Sequence Number Field",   "sna.th.tg_snf", FT_UINT16, BASE_DEC,
1462                         NULL, 0x0fff,
1463                         "", HFILL }},
1464
1465                 { &hf_sna_th_vrprq,
1466                 { "Virtual Route Pacing Request",       "sna.th.vrprq", FT_BOOLEAN, 16,
1467                         TFS(&sna_th_vrprq_truth), 0x8000,
1468                         "", HFILL }},
1469
1470                 { &hf_sna_th_vrprs,
1471                 { "Virtual Route Pacing Response",      "sna.th.vrprs", FT_BOOLEAN, 16,
1472                         TFS(&sna_th_vrprs_truth), 0x4000,
1473                         "", HFILL }},
1474
1475                 { &hf_sna_th_vr_cwri,
1476                 { "Virtual Route Change Window Reply Indicator",        "sna.th.vr_cwri", FT_UINT16, BASE_DEC,
1477                         VALS(sna_th_vr_cwri_vals), 0x2000,
1478                         "Permits changing of the window size by 1 for PIUs received by the "
1479                         "sender of this bit.", HFILL }},
1480
1481                 { &hf_sna_th_vr_rwi,
1482                 { "Virtual Route Reset Window Indicator",       "sna.th.vr_rwi", FT_BOOLEAN, 16,
1483                         TFS(&sna_th_vr_rwi_truth), 0x1000,
1484                         "Indicates severe congestion in a node on the virtual route.", HFILL }},
1485
1486                 { &hf_sna_th_vr_snf_send,
1487                 { "Virtual Route Send Sequence Number Field",   "sna.th.vr_snf_send", FT_UINT16, BASE_DEC,
1488                         NULL, 0x0fff,
1489                         "", HFILL }},
1490
1491                 { &hf_sna_th_dsaf,
1492                 { "Destination Subarea Address Field",  "sna.th.dsaf", FT_UINT32, BASE_HEX, NULL, 0x0,
1493                         "", HFILL }},
1494
1495                 { &hf_sna_th_osaf,
1496                 { "Origin Subarea Address Field",       "sna.th.osaf", FT_UINT32, BASE_HEX, NULL, 0x0,
1497                         "", HFILL }},
1498
1499                 { &hf_sna_th_snai,
1500                 { "SNA Indicator",      "sna.th.snai", FT_BOOLEAN, 8, NULL, 0x10,
1501                         "Used to identify whether the PIU originated or is destined for "
1502                         "an SNA or non-SNA device.", HFILL }},
1503
1504                 { &hf_sna_th_def,
1505                 { "Destination Element Field",  "sna.th.def", FT_UINT16, BASE_HEX, NULL, 0x0,
1506                         "", HFILL }},
1507
1508                 { &hf_sna_th_oef,
1509                 { "Origin Element Field",       "sna.th.oef", FT_UINT16, BASE_HEX, NULL, 0x0,
1510                         "", HFILL }},
1511
1512                 { &hf_sna_th_sa,
1513                 { "Session Address",    "sna.th.sa", FT_BYTES, BASE_HEX, NULL, 0x0,
1514                         "", HFILL }},
1515
1516                 { &hf_sna_th_cmd_fmt,
1517                 { "Command Format",     "sna.th.cmd_fmt", FT_UINT8, BASE_HEX, NULL, 0x0,
1518                         "", HFILL }},
1519
1520                 { &hf_sna_th_cmd_type,
1521                 { "Command Type",       "sna.th.cmd_type", FT_UINT8, BASE_HEX, NULL, 0x0,
1522                         "", HFILL }},
1523
1524                 { &hf_sna_th_cmd_sn,
1525                 { "Command Sequence Number",    "sna.th.cmd_sn", FT_UINT16, BASE_DEC, NULL, 0x0,
1526                         "", HFILL }},
1527
1528                 { &hf_sna_nlp_nhdr,
1529                 { "Network Layer Packet Header",        "sna.nlp.nhdr", FT_NONE, BASE_NONE, NULL, 0x0,
1530                         "Network Layer Packet Header (NHDR)", HFILL }},
1531
1532                 { &hf_sna_nlp_nhdr_0,
1533                 { "Network Layer Packet Header Byte 0", "sna.nlp.nhdr.0", FT_UINT8, BASE_HEX, NULL, 0x0,
1534                         "Byte 0 of Network Layer Packet contains SM and TPF", HFILL }},
1535
1536                 { &hf_sna_nlp_nhdr_1,
1537                 { "Network Layer Packet Header Bype 1", "sna.nlp.nhdr.1", FT_UINT8, BASE_HEX, NULL, 0x0,
1538                         "Byte 1 of Network Layer Packet contains FT,"
1539                         " Time Sensitive Packet Indicator and Congestion Indicator", HFILL }},
1540
1541                 { &hf_sna_nlp_sm,
1542                 { "Switching Mode Field",       "sna.nlp.nhdr.sm", FT_UINT8, BASE_HEX,
1543                         VALS(sna_nlp_sm_vals), 0xe0,
1544                         "", HFILL }},
1545
1546                 { &hf_sna_nlp_tpf,
1547                 { "Transmission Priority Field",        "sna.nlp.nhdr.tpf", FT_UINT8, BASE_HEX,
1548                         VALS(sna_th_tpf_vals), 0x06,
1549                         "", HFILL }},
1550
1551                 { &hf_sna_nlp_ft,
1552                 { "Function Type",      "sna.nlp.nhdr.ft", FT_UINT8, BASE_HEX,
1553                         VALS(sna_nlp_ft_vals), 0xF0,
1554                         "", HFILL }},
1555
1556                 { &hf_sna_nlp_tspi,
1557                 { "Time Sensitive Packet Indicator",    "sna.nlp.nhdr.tspi", FT_BOOLEAN, 8,
1558                         TFS(&sna_nlp_tspi_truth), 0x08,
1559                         "", HFILL }},
1560
1561                 { &hf_sna_nlp_slowdn1,
1562                 { "Slowdown 1", "sna.nlp.nhdr.slowdn1", FT_BOOLEAN, 8,
1563                         TFS(&sna_nlp_slowdn1_truth), 0x04,
1564                         "", HFILL }},
1565
1566                 { &hf_sna_nlp_slowdn2,
1567                 { "Slowdown 2", "sna.nlp.nhdr.slowdn2", FT_BOOLEAN, 8,
1568                         TFS(&sna_nlp_slowdn2_truth), 0x02,
1569                         "", HFILL }},
1570
1571                 { &hf_sna_nlp_fra,
1572                 { "Function Routing Address Entry",     "sna.nlp.nhdr.fra", FT_BYTES, BASE_NONE, NULL, 0,
1573                         "", HFILL }},
1574
1575                 { &hf_sna_nlp_anr,
1576                 { "Automatic Network Routing Entry",    "sna.nlp.nhdr.anr", FT_BYTES, BASE_HEX, NULL, 0,
1577                         "", HFILL }},
1578
1579                 { &hf_sna_nlp_frh,
1580                 { "Transmission Priority Field",        "sna.nlp.frh", FT_UINT8, BASE_HEX,
1581                         VALS(sna_nlp_frh_vals), 0, "", HFILL }},
1582
1583                 { &hf_sna_nlp_thdr,
1584                 { "RTP Transport Header",       "sna.nlp.thdr", FT_NONE, BASE_NONE, NULL, 0x0,
1585                         "RTP Transport Header (THDR)", HFILL }},
1586
1587                 { &hf_sna_nlp_tcid,
1588                 { "Transport Connection Identifier",    "sna.nlp.thdr.tcid", FT_BYTES, BASE_HEX, NULL, 0x0,
1589                         "Transport Connection Identifier (TCID)", HFILL }},
1590
1591                 { &hf_sna_nlp_thdr_8,
1592                 { "RTP Transport Packet Header Bype 8", "sna.nlp.thdr.8", FT_UINT8, BASE_HEX, NULL, 0x0,
1593                         "Byte 8 of RTP Transport Packet Header", HFILL }},
1594
1595                 { &hf_sna_nlp_setupi,
1596                 { "Setup Indicator",    "sna.nlp.thdr.setupi", FT_BOOLEAN, 8,
1597                         TFS(&sna_nlp_setupi_truth), 0x40,
1598                         "", HFILL }},
1599
1600                 { &hf_sna_nlp_somi,
1601                 { "Start Of Message Indicator", "sna.nlp.thdr.somi", FT_BOOLEAN, 8,
1602                         TFS(&sna_nlp_somi_truth), 0x20,
1603                         "", HFILL }},
1604
1605                 { &hf_sna_nlp_eomi,
1606                 { "End Of Message Indicator",   "sna.nlp.thdr.eomi", FT_BOOLEAN, 8,
1607                         TFS(&sna_nlp_eomi_truth), 0x10,
1608                         "", HFILL }},
1609
1610                 { &hf_sna_nlp_sri,
1611                 { "Session Request Indicator",  "sna.nlp.thdr.sri", FT_BOOLEAN, 8,
1612                         TFS(&sna_nlp_sri_truth), 0x08,
1613                         "", HFILL }},
1614
1615                 { &hf_sna_nlp_rasapi,
1616                 { "Reply ASAP Indicator",       "sna.nlp.thdr.rasapi", FT_BOOLEAN, 8,
1617                         TFS(&sna_nlp_rasapi_truth), 0x04,
1618                         "", HFILL }},
1619
1620                 { &hf_sna_nlp_retryi,
1621                 { "Retry Indicator",    "sna.nlp.thdr.retryi", FT_BOOLEAN, 8,
1622                         TFS(&sna_nlp_retryi_truth), 0x02,
1623                         "", HFILL }},
1624
1625                 { &hf_sna_nlp_thdr_9,
1626                 { "RTP Transport Packet Header Bype 9", "sna.nlp.thdr.9", FT_UINT8, BASE_HEX, NULL, 0x0,
1627                         "Byte 9 of RTP Transport Packet Header", HFILL }},
1628
1629                 { &hf_sna_nlp_lmi,
1630                 { "Last Message Indicator",     "sna.nlp.thdr.lmi", FT_BOOLEAN, 8,
1631                         TFS(&sna_nlp_lmi_truth), 0x80,
1632                         "", HFILL }},
1633
1634                 { &hf_sna_nlp_cqfi,
1635                 { "Connection Qualifyer Field Indicator",       "sna.nlp.thdr.cqfi", FT_BOOLEAN, 8,
1636                         TFS(&sna_nlp_cqfi_truth), 0x08,
1637                         "", HFILL }},
1638
1639                 { &hf_sna_nlp_osi,
1640                 { "Optional Segments Present Indicator",        "sna.nlp.thdr.osi", FT_BOOLEAN, 8,
1641                         TFS(&sna_nlp_osi_truth), 0x04,
1642                         "", HFILL }},
1643
1644                 { &hf_sna_nlp_offset,
1645                 { "Data Offset/4",      "sna.nlp.thdr.offset", FT_UINT16, BASE_HEX, NULL, 0x0,
1646                         "Data Offset in words", HFILL }},
1647
1648                 { &hf_sna_nlp_dlf,
1649                 { "Data Length Field",  "sna.nlp.thdr.dlf", FT_UINT32, BASE_HEX, NULL, 0x0,
1650                         "Data Length Field", HFILL }},
1651
1652                 { &hf_sna_nlp_bsn,
1653                 { "Byte Sequence Number",       "sna.nlp.thdr.bsn", FT_UINT32, BASE_HEX, NULL, 0x0,
1654                         "Byte Sequence Number", HFILL }},
1655
1656
1657                 { &hf_sna_rh,
1658                 { "Request/Response Header",    "sna.rh", FT_NONE, BASE_NONE, NULL, 0x0,
1659                         "", HFILL }},
1660
1661                 { &hf_sna_rh_0,
1662                 { "Request/Response Header Byte 0",     "sna.rh.0", FT_UINT8, BASE_HEX, NULL, 0x0,
1663                         "", HFILL }},
1664
1665                 { &hf_sna_rh_1,
1666                 { "Request/Response Header Byte 1",     "sna.rh.1", FT_UINT8, BASE_HEX, NULL, 0x0,
1667                         "", HFILL }},
1668
1669                 { &hf_sna_rh_2,
1670                 { "Request/Response Header Byte 2",     "sna.rh.2", FT_UINT8, BASE_HEX, NULL, 0x0,
1671                         "", HFILL }},
1672
1673                 { &hf_sna_rh_rri,
1674                 { "Request/Response Indicator", "sna.rh.rri", FT_UINT8, BASE_DEC, VALS(sna_rh_rri_vals), 0x80,
1675                         "Denotes whether this is a request or a response.", HFILL }},
1676
1677                 { &hf_sna_rh_ru_category,
1678                 { "Request/Response Unit Category",     "sna.rh.ru_category", FT_UINT8, BASE_HEX,
1679                         VALS(sna_rh_ru_category_vals), 0x60,
1680                         "", HFILL }},
1681
1682                 { &hf_sna_rh_fi,
1683                 { "Format Indicator",           "sna.rh.fi", FT_BOOLEAN, 8, TFS(&sna_rh_fi_truth), 0x08,
1684                         "", HFILL }},
1685
1686                 { &hf_sna_rh_sdi,
1687                 { "Sense Data Included",        "sna.rh.sdi", FT_BOOLEAN, 8, TFS(&sna_rh_sdi_truth), 0x04,
1688                         "Indicates that a 4-byte sense data field is included in the associated RU.", HFILL }},
1689
1690                 { &hf_sna_rh_bci,
1691                 { "Begin Chain Indicator",      "sna.rh.bci", FT_BOOLEAN, 8, TFS(&sna_rh_bci_truth), 0x02,
1692                         "", HFILL }},
1693
1694                 { &hf_sna_rh_eci,
1695                 { "End Chain Indicator",        "sna.rh.eci", FT_BOOLEAN, 8, TFS(&sna_rh_eci_truth), 0x01,
1696                         "", HFILL }},
1697
1698                 { &hf_sna_rh_dr1,
1699                 { "Definite Response 1 Indicator",      "sna.rh.dr1", FT_BOOLEAN, 8, NULL, 0x80,
1700                         "", HFILL }},
1701
1702                 { &hf_sna_rh_lcci,
1703                 { "Length-Checked Compression Indicator",       "sna.rh.lcci", FT_BOOLEAN, 8,
1704                         TFS(&sna_rh_lcci_truth), 0x40,
1705                         "", HFILL }},
1706
1707                 { &hf_sna_rh_dr2,
1708                 { "Definite Response 2 Indicator",      "sna.rh.dr2", FT_BOOLEAN, 8, NULL, 0x20,
1709                         "", HFILL }},
1710
1711                 { &hf_sna_rh_eri,
1712                 { "Exception Response Indicator",       "sna.rh.eri", FT_BOOLEAN, 8, NULL, 0x10,
1713                         "Used in conjunction with DR1I and DR2I to indicate, in a request, "
1714                         "the form of response requested.", HFILL }},
1715
1716                 { &hf_sna_rh_rti,
1717                 { "Response Type Indicator",    "sna.rh.rti", FT_BOOLEAN, 8, TFS(&sna_rh_rti_truth), 0x10,
1718                         "", HFILL }},
1719
1720                 { &hf_sna_rh_rlwi,
1721                 { "Request Larger Window Indicator",    "sna.rh.rlwi", FT_BOOLEAN, 8, NULL, 0x04,
1722                         "Indicates whether a larger pacing window was requested.", HFILL }},
1723
1724                 { &hf_sna_rh_qri,
1725                 { "Queued Response Indicator",  "sna.rh.qri", FT_BOOLEAN, 8, TFS(&sna_rh_qri_truth), 0x02,
1726                         "", HFILL }},
1727
1728                 { &hf_sna_rh_pi,
1729                 { "Pacing Indicator",   "sna.rh.pi", FT_BOOLEAN, 8, NULL, 0x01,
1730                         "", HFILL }},
1731
1732                 { &hf_sna_rh_bbi,
1733                 { "Begin Bracket Indicator",    "sna.rh.bbi", FT_BOOLEAN, 8, NULL, 0x80,
1734                         "", HFILL }},
1735
1736                 { &hf_sna_rh_ebi,
1737                 { "End Bracket Indicator",      "sna.rh.ebi", FT_BOOLEAN, 8, NULL, 0x40,
1738                         "", HFILL }},
1739
1740                 { &hf_sna_rh_cdi,
1741                 { "Change Direction Indicator", "sna.rh.cdi", FT_BOOLEAN, 8, NULL, 0x20,
1742                         "", HFILL }},
1743
1744                 { &hf_sna_rh_csi,
1745                 { "Code Selection Indicator",   "sna.rh.csi", FT_UINT8, BASE_DEC, VALS(sna_rh_csi_vals), 0x08,
1746                         "Specifies the encoding used for the associated FMD RU.", HFILL }},
1747
1748                 { &hf_sna_rh_edi,
1749                 { "Enciphered Data Indicator",  "sna.rh.edi", FT_BOOLEAN, 8, NULL, 0x04,
1750                         "Indicates that information in the associated RU is enciphered under "
1751                         "session-level cryptography protocols.", HFILL }},
1752
1753                 { &hf_sna_rh_pdi,
1754                 { "Padded Data Indicator",      "sna.rh.pdi", FT_BOOLEAN, 8, NULL, 0x02,
1755                         "Indicates that the RU was padded at the end, before encipherment, to the next "
1756                         "integral multiple of 8 bytes.", HFILL }},
1757
1758                 { &hf_sna_rh_cebi,
1759                 { "Conditional End Bracket Indicator",  "sna.rh.cebi", FT_BOOLEAN, 8, NULL, 0x01,
1760                         "Used to indicate the beginning or end of a group of exchanged "
1761                         "requests and responses called a bracket. Only used on LU-LU sessions.", HFILL }},
1762
1763 /*                { &hf_sna_ru,
1764                 { "Request/Response Unit",      "sna.ru", FT_NONE, BASE_NONE, NULL, 0x0,
1765                         "", HFILL }},*/
1766         };
1767         static gint *ett[] = {
1768                 &ett_sna,
1769                 &ett_sna_th,
1770                 &ett_sna_th_fid,
1771                 &ett_sna_nlp_nhdr,
1772                 &ett_sna_nlp_nhdr_0,
1773                 &ett_sna_nlp_nhdr_1,
1774                 &ett_sna_nlp_thdr,
1775                 &ett_sna_nlp_thdr_8,
1776                 &ett_sna_nlp_thdr_9,
1777                 &ett_sna_rh,
1778                 &ett_sna_rh_0,
1779                 &ett_sna_rh_1,
1780                 &ett_sna_rh_2,
1781         };
1782         module_t *sna_module;
1783
1784         proto_sna = proto_register_protocol("Systems Network Architecture",
1785             "SNA", "sna");
1786         proto_register_field_array(proto_sna, hf, array_length(hf));
1787         proto_register_subtree_array(ett, array_length(ett));
1788         register_dissector("sna", dissect_sna, proto_sna);
1789
1790         /* Register configuration options */
1791         sna_module = prefs_register_protocol(proto_sna, NULL);
1792         prefs_register_bool_preference(sna_module, "defragment",
1793                 "Reassemble fragmented BIUs",
1794                 "Whether fragmented BIUs should be reassembled",
1795                 &sna_defragment);
1796
1797 }
1798
1799 void
1800 proto_reg_handoff_sna(void)
1801 {
1802         dissector_handle_t sna_handle;
1803
1804         sna_handle = find_dissector("sna");
1805         dissector_add("llc.dsap", SAP_SNA_PATHCTRL, sna_handle);
1806         /* RFC 2043 */
1807         dissector_add("ppp.protocol", PPP_SNA, sna_handle);
1808         data_handle = find_dissector("data");
1809
1810     register_init_routine(sna_init);
1811 }