f3893b8aa8101db403effccc30a21c5848a7f710
[obnox/wireshark/wip.git] / epan / dissectors / packet-fc.c
1 /* packet-fc.c
2  * Routines for Fibre Channel Decoding (FC Header, Link Ctl & Basic Link Svc) 
3  * Copyright 2001, Dinesh G Dutt <ddutt@cisco.com>
4  *   Copyright 2003  Ronnie Sahlberg, exchange first/last matching and 
5  *                                    tap listener and misc updates
6  *
7  * $Id$
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40
41 #ifdef HAVE_NETINET_IN_H
42 # include <netinet/in.h>
43 #endif
44
45 #include <glib.h>
46
47 #include <epan/packet.h>
48 #include <epan/prefs.h>
49 #include <epan/reassemble.h>
50 #include <epan/conversation.h>
51 #include "etypes.h"
52 #include "packet-fc.h"
53 #include "packet-fclctl.h"
54 #include "packet-fcbls.h"
55 #include <epan/tap.h>
56 #include <epan/emem.h>
57
58 #define FC_HEADER_SIZE         24
59 #define FC_RCTL_EISL           0x50
60 #define MDSHDR_TRAILER_SIZE    6 
61
62 /* Size of various fields in FC header in bytes */
63 #define FC_RCTL_SIZE           1
64 #define FC_DID_SIZE            3
65 #define FC_CSCTL_SIZE          1
66 #define FC_SID_SIZE            3
67 #define FC_TYPE_SIZE           1
68 #define FC_FCTL_SIZE           3
69 #define FC_SEQID_SIZE          1
70 #define FC_DFCTL_SIZE          1
71 #define FC_SEQCNT_SIZE         2
72 #define FC_OXID_SIZE           2
73 #define FC_RXID_SIZE           2
74 #define FC_PARAM_SIZE          4
75
76 /* Initialize the protocol and registered fields */
77 static int proto_fc = -1;
78 static int hf_fc_time = -1;
79 static int hf_fc_exchange_first_frame = -1;
80 static int hf_fc_exchange_last_frame = -1;
81 static int hf_fc_rctl = -1;
82 static int hf_fc_did = -1;
83 static int hf_fc_csctl = -1;
84 static int hf_fc_sid = -1;
85 static int hf_fc_id = -1;
86 static int hf_fc_type = -1;
87 static int hf_fc_fctl = -1;
88 static int hf_fc_fctl_exchange_responder = -1;
89 static int hf_fc_fctl_seq_recipient = -1;
90 static int hf_fc_fctl_exchange_first = -1;
91 static int hf_fc_fctl_exchange_last = -1;
92 static int hf_fc_fctl_seq_last = -1;
93 static int hf_fc_fctl_priority = -1;
94 static int hf_fc_fctl_transfer_seq_initiative = -1;
95 static int hf_fc_fctl_rexmitted_seq = -1;
96 static int hf_fc_fctl_rel_offset = -1;
97 static int hf_fc_fctl_abts_ack = -1;
98 static int hf_fc_fctl_abts_not_ack = -1;
99 static int hf_fc_fctl_last_data_frame = -1;
100 static int hf_fc_fctl_ack_0_1 = -1;
101 static int hf_fc_seqid = -1;
102 static int hf_fc_dfctl = -1;
103 static int hf_fc_seqcnt = -1;
104 static int hf_fc_oxid = -1;
105 static int hf_fc_rxid = -1;
106 static int hf_fc_param = -1;
107 static int hf_fc_ftype = -1;    /* Derived field, non-existent in FC hdr */
108 static int hf_fc_reassembled = -1;
109 static int hf_fc_eisl = -1;
110
111 /* Network_Header fields */
112 static int hf_fc_nh_da = -1;
113 static int hf_fc_nh_sa = -1;
114
115 /* For Basic Link Svc */
116 static int hf_fc_bls_seqid_vld = -1;
117 static int hf_fc_bls_lastvld_seqid = -1;
118 static int hf_fc_bls_oxid = -1;
119 static int hf_fc_bls_rxid = -1;
120 static int hf_fc_bls_lowseqcnt = -1;
121 static int hf_fc_bls_hiseqcnt = -1;
122 static int hf_fc_bls_rjtcode = -1;
123 static int hf_fc_bls_rjtdetail = -1;
124 static int hf_fc_bls_vendor = -1;
125
126
127 /* Initialize the subtree pointers */
128 static gint ett_fc = -1;
129 static gint ett_fctl = -1;
130 static gint ett_fcbls = -1;
131
132 static dissector_table_t fcftype_dissector_table;
133 static dissector_handle_t data_handle;
134
135 static int fc_tap = -1;
136
137 /* Reassembly stuff */
138 static gboolean fc_reassemble = TRUE;
139 static guint32  fc_max_frame_size = 1024;
140 static GHashTable *fc_fragment_table = NULL;
141
142 typedef struct _fcseq_conv_key {
143     guint32 conv_idx;
144 } fcseq_conv_key_t;
145
146 typedef struct _fcseq_conv_data {
147     guint32 seq_cnt;
148 } fcseq_conv_data_t;
149
150 GHashTable *fcseq_req_hash = NULL;
151
152 static GHashTable *fc_exchange_unmatched = NULL;
153 static GHashTable *fc_exchange_matched = NULL;
154
155 /*
156  * Hash Functions
157  */
158 static gint
159 fcseq_equal(gconstpointer v, gconstpointer w)
160 {
161   const fcseq_conv_key_t *v1 = v;
162   const fcseq_conv_key_t *v2 = w;
163
164   return (v1->conv_idx == v2->conv_idx);
165 }
166
167 static guint
168 fcseq_hash (gconstpointer v)
169 {
170     const fcseq_conv_key_t *key = v;
171     guint val;
172     
173     val = key->conv_idx;
174     
175     return val;
176 }
177
178 static guint
179 fc_exchange_hash_unmatched(gconstpointer v)
180 {
181     const fc_exchange_data *fced=(const fc_exchange_data *)v;
182
183     return fced->oxid;
184 }
185 static gint
186 fc_exchange_equal_unmatched(gconstpointer v1, gconstpointer v2)
187 {
188     const fc_exchange_data *fced1=(const fc_exchange_data *)v1;
189     const fc_exchange_data *fced2=(const fc_exchange_data *)v2;
190
191     /* oxid must match */
192     if(fced1->oxid!=fced2->oxid){
193         return 0;
194     }
195     /* compare s_id, d_id and treat the fc address
196        s_id==00.00.00 as a wildcard matching anything */
197     if( ((fced1->s_id.data[0]!=0)||(fced1->s_id.data[1]!=0)||(fced1->s_id.data[2]!=0)) && CMP_ADDRESS(&fced1->s_id, &fced2->s_id) ){
198         return 0;
199     }
200     if(CMP_ADDRESS(&fced1->d_id, &fced2->d_id)){
201         return 0;
202     }
203
204     return 1;
205 }
206
207 static guint
208 fc_exchange_hash_matched(gconstpointer v)
209 {
210     const fc_exchange_data *fced=(const fc_exchange_data *)v;
211
212     return fced->oxid;
213 }
214 static gint
215 fc_exchange_equal_matched(gconstpointer v1, gconstpointer v2)
216 {
217     const fc_exchange_data *fced1=(const fc_exchange_data *)v1;
218     const fc_exchange_data *fced2=(const fc_exchange_data *)v2;
219     guint32 fef1, fef2, lef1, lef2;
220
221     /* oxid must match */
222     if(fced1->oxid!=fced2->oxid){
223         return 0;
224     }
225     fef1=fced1->first_exchange_frame;
226     fef2=fced2->first_exchange_frame;
227     lef1=fced1->last_exchange_frame;
228     lef2=fced2->last_exchange_frame;
229     if(!fef1)fef1=fef2;
230     if(!fef2)fef2=fef1;
231     if(!lef1)lef1=lef2;
232     if(!lef2)lef2=lef1;
233
234     if(fef1!=fef2){
235         return 0;
236     }
237     if(lef1!=lef2){
238         return 0;
239     }
240
241     return 1;
242 }
243
244 static void
245 fc_exchange_init_protocol(void)
246 {
247     if(fc_exchange_unmatched){
248         g_hash_table_destroy(fc_exchange_unmatched);
249         fc_exchange_unmatched=NULL;
250     }
251     if(fc_exchange_matched){
252         g_hash_table_destroy(fc_exchange_matched);
253         fc_exchange_matched=NULL;
254     }
255
256     fc_exchange_unmatched=g_hash_table_new(fc_exchange_hash_unmatched, fc_exchange_equal_unmatched);
257     fc_exchange_matched=g_hash_table_new(fc_exchange_hash_matched, fc_exchange_equal_matched);
258
259     fragment_table_init(&fc_fragment_table);
260
261     if (fcseq_req_hash)
262         g_hash_table_destroy(fcseq_req_hash);
263     
264     fcseq_req_hash = g_hash_table_new(fcseq_hash, fcseq_equal);
265 }
266
267
268 const value_string fc_fc4_val[] = {
269     {FC_TYPE_ELS,        "Ext Link Svc"},
270     {FC_TYPE_LLCSNAP,    "LLC_SNAP"},
271     {FC_TYPE_IP,         "IP/FC"},
272     {FC_TYPE_SCSI,       "FCP"},
273     {FC_TYPE_FCCT,       "FC_CT"},
274     {FC_TYPE_SWILS,      "SW_ILS"},
275     {FC_TYPE_AL,         "AL"},
276     {FC_TYPE_SNMP,       "SNMP"},
277     {FC_TYPE_SB_FROM_CU, "SB-3(CU->Channel)"},
278     {FC_TYPE_SB_TO_CU,   "SB-3(Channel->CU)"},
279     {0, NULL},
280 };
281
282 static const value_string fc_ftype_vals [] = {
283     {FC_FTYPE_UNDEF ,    "Unknown frame"},
284     {FC_FTYPE_SWILS,     "SW_ILS"},
285     {FC_FTYPE_IP ,       "IP/FC"},
286     {FC_FTYPE_SCSI ,     "FCP"},
287     {FC_FTYPE_BLS ,      "Basic Link Svc"},
288     {FC_FTYPE_ELS ,      "ELS"},
289     {FC_FTYPE_FCCT ,     "FC_CT"},
290     {FC_FTYPE_LINKDATA,  "Link Data"},
291     {FC_FTYPE_VDO,       "Video Data"},
292     {FC_FTYPE_LINKCTL,   "Link Ctl"},
293     {FC_FTYPE_SBCCS,     "SBCCS"},
294     {0, NULL},
295 };
296
297 static const value_string fc_wka_vals[] = {
298     {FC_WKA_MULTICAST,    "Multicast Server"},
299     {FC_WKA_CLKSYNC,      "Clock Sync Server"},
300     {FC_WKA_KEYDIST,      "Key Distribution Server"},
301     {FC_WKA_ALIAS,        "Alias Server"},
302     {FC_WKA_QOSF,         "QoS Facilitator"},
303     {FC_WKA_MGMT,         "Management Server"},
304     {FC_WKA_TIME,         "Time Server"},
305     {FC_WKA_DNS,          "Directory Server"},
306     {FC_WKA_FABRIC_CTRLR, "Fabric Ctlr"},
307     {FC_WKA_FPORT,        "F_Port Server"},
308     {FC_WKA_BCAST,        "Broadcast ID"},
309     {0, NULL},
310 };
311
312 static const value_string fc_routing_val[] = {
313     {FC_RCTL_DEV_DATA,  "Device_Data"},
314     {FC_RCTL_ELS,       "Extended Link Services"},
315     {FC_RCTL_LINK_DATA, "FC-4 Link_Data"},
316     {FC_RCTL_VIDEO,     "Video_Data"},
317     {FC_RCTL_BLS,       "Basic Link Services"},
318     {FC_RCTL_LINK_CTL,  "Link_Control Frame"},
319     {0, NULL},
320 };
321
322 static const value_string fc_iu_val[] = {
323     {FC_IU_UNCATEGORIZED   , "Uncategorized Data"},
324     {FC_IU_SOLICITED_DATA  , "Solicited Data"},
325     {FC_IU_UNSOLICITED_CTL , "Unsolicited Control"},
326     {FC_IU_SOLICITED_CTL   , "Solicited Control"},
327     {FC_IU_UNSOLICITED_DATA, "Solicited Data"},
328     {FC_IU_DATA_DESCRIPTOR , "Data Descriptor"},
329     {FC_IU_UNSOLICITED_CMD , "Unsolicited Command"},
330     {FC_IU_CMD_STATUS      , "Command Status"},
331     {0, NULL},
332 };
333
334
335 static void fc_defragment_init(void)
336 {
337   fragment_table_init (&fc_fragment_table);
338 }
339
340
341 static gchar *
342 fctl_to_str (const guint8 *fctl, gchar *str, gboolean is_ack)
343 {
344     int stroff = 0;
345     guint8 tmp = 0;
346
347     if (str == NULL)
348         return (str);
349     
350     if (fctl[2] & 0x80) {
351         strcpy (str, "Exchange Responder, ");
352         stroff += 20;
353     }
354     else {
355         strcpy (str, "Exchange Originator, ");
356         stroff += 21;
357     }
358
359     if (fctl[2] & 0x40) {
360         strcpy (&str[stroff], "Seq Recipient, ");
361         stroff += 15;
362     }
363     else {
364         strcpy (&str[stroff], "Seq Initiator, ");
365         stroff += 15;
366     }
367
368     if (fctl[2] & 0x20) {
369         strcpy (&str[stroff], "Exchg First, ");
370         stroff += 13;
371     }
372
373     if (fctl[2] & 0x10) {
374         strcpy (&str[stroff], "Exchg Last, ");
375         stroff += 12;
376     }
377
378     if (fctl[2] & 0x8) {
379         strcpy (&str[stroff], "Seq Last, ");
380         stroff += 10;
381     }
382
383     if (fctl[2] & 0x2) {
384         strcpy (&str[stroff], "Priority, ");
385         stroff += 10;
386     }
387     else {
388         strcpy (&str[stroff], "CS_CTL, ");
389         stroff += 8;
390     }
391
392     if (fctl[2] & 0x1) {
393         strcpy (&str[stroff], "Transfer Seq Initiative, ");
394         stroff += 25;
395     }
396
397     if (fctl[1] & 0x30) {
398         strcpy (&str[stroff], "ACK_0 Reqd, ");
399         stroff += 12;
400     }
401     else if (fctl[1] & 0x10) {
402         strcpy (&str[stroff], "ACK_1 Reqd, ");
403         stroff += 12;
404     }
405
406     if (fctl[1] & 0x2) {
407         strcpy (&str[stroff], "Rexmitted Seq, ");
408         stroff += 15;
409     }
410
411     tmp = fctl[0] & 0xC0;
412     switch (tmp) {
413     case 0:
414         strcpy (&str[stroff], "Last Data Frame - No Info, ");
415         stroff += 27;
416         break;
417     case 1:
418         strcpy (&str[stroff], "Last Data Frame - Seq Imm, ");
419         stroff += 27;
420         break;
421     case 2:
422         strcpy (&str[stroff], "Last Data Frame - Seq Soon, ");
423         stroff += 28;
424         break;
425     case 3:
426         strcpy (&str[stroff], "Last Data Frame - Seq Delyd, ");
427         stroff += 29;
428         break;
429     }
430
431     tmp = fctl[0] & 0x30;
432     switch (tmp) {
433     case 0:
434         if (is_ack) {
435             strcpy (&str[stroff], "ABTS - Cont, ");
436             stroff += 13;
437         }
438         else {
439             strcpy (&str[stroff], "ABTS - Abort/MS, ");
440             stroff += 17;
441         }
442         break;
443     case 0x10:
444         if (is_ack) {
445             strcpy (&str[stroff], "ABTS - AbortABTS - Abort, ");
446             stroff += 14;
447         }
448         else {
449             strcpy (&str[stroff], "ABTS - Abort/SS, ");
450             stroff += 17;
451         }
452         break;
453     case 0x20:
454         if (is_ack) {
455             strcpy (&str[stroff], "ABTS - Stop, ");
456             stroff += 13;
457         }
458         else {
459             strcpy (&str[stroff], "ABTS - Process/IB, ");
460             stroff += 19;
461         }
462         break;
463     case 0x30:
464         if (is_ack) {
465             strcpy (&str[stroff], "ABTS - Imm Seq Retx, ");
466             stroff += 21;
467         }
468         else {
469             strcpy (&str[stroff], "ABTS - Discard/MS/Imm Retx, ");
470             stroff += 28;
471         }
472         break;
473     }
474
475     if (fctl[0] & 0x8) {
476         strcpy (&str[stroff], "Rel Offset = 1");
477         stroff += 14;
478     }
479
480     return (str);
481 }
482
483 /* BA_ACC & BA_RJT are decoded in this file itself instead of a traditional
484  * dedicated file and dissector format because the dissector would require some
485  * fields of the FC_HDR such as param in some cases, type in some others, the
486  * lower 4 bits of r_ctl in some other cases etc. So, we decode BLS & Link Ctl
487  * in this file itself.
488  */
489 static void
490 dissect_fc_ba_acc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
491 {
492     /* Set up structures needed to add the protocol subtree and manage it */
493     proto_item *ti;
494     proto_tree *acc_tree;
495     int offset = 0;
496
497     /* Make entries in Protocol column and Info column on summary display */
498     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
499         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
500
501     if (check_col(pinfo->cinfo, COL_INFO)) 
502         col_set_str(pinfo->cinfo, COL_INFO, "BA_ACC");
503
504     if (tree) {
505         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
506         acc_tree = proto_item_add_subtree (ti, ett_fcbls);
507
508         proto_tree_add_item (acc_tree, hf_fc_bls_seqid_vld, tvb, offset++, 1, FALSE);
509         proto_tree_add_item (acc_tree, hf_fc_bls_lastvld_seqid, tvb, offset++, 1, FALSE);
510         offset += 2; /* Skip reserved field */
511         proto_tree_add_item (acc_tree, hf_fc_bls_oxid, tvb, offset, 2, FALSE);
512         offset += 2;
513         proto_tree_add_item (acc_tree, hf_fc_bls_rxid, tvb, offset, 2, FALSE);
514         offset += 2;
515         proto_tree_add_item (acc_tree, hf_fc_bls_lowseqcnt, tvb, offset, 2, FALSE);
516         offset += 2;
517         proto_tree_add_item (acc_tree, hf_fc_bls_hiseqcnt, tvb, offset, 2, FALSE);
518     }
519 }
520
521 static void
522 dissect_fc_ba_rjt (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
523 {
524     /* Set up structures needed to add the protocol subtree and manage it */
525     proto_item *ti;
526     proto_tree *rjt_tree;
527     int offset = 0;
528
529     /* Make entries in Protocol column and Info column on summary display */
530     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
531         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
532
533     if (check_col(pinfo->cinfo, COL_INFO)) 
534         col_set_str(pinfo->cinfo, COL_INFO, "BA_RJT");
535
536     if (tree) {
537         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
538         rjt_tree = proto_item_add_subtree (ti, ett_fcbls);
539
540         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtcode, tvb, offset+1, 1, FALSE);
541         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtdetail, tvb, offset+2, 1, FALSE);
542         proto_tree_add_item (rjt_tree, hf_fc_bls_vendor, tvb, offset+3, 1, FALSE);
543     }
544 }
545
546 static guint8
547 fc_get_ftype (guint8 r_ctl, guint8 type)
548 {
549     /* A simple attempt to determine the upper level protocol based on the
550      * r_ctl & type fields.
551      */
552     switch (r_ctl & 0xF0) {
553     case FC_RCTL_DEV_DATA:
554         switch (type) {
555         case FC_TYPE_SWILS:
556             if ((r_ctl == 0x2) || (r_ctl == 0x3))
557                 return FC_FTYPE_SWILS;
558             else
559                 return FC_FTYPE_UNDEF;
560         case FC_TYPE_IP:
561             return FC_FTYPE_IP;
562         case FC_TYPE_SCSI:
563             return FC_FTYPE_SCSI;
564         case FC_TYPE_FCCT:
565             return FC_FTYPE_FCCT;
566         case FC_TYPE_SB_FROM_CU:
567         case FC_TYPE_SB_TO_CU:
568             return FC_FTYPE_SBCCS;
569         default:
570             return FC_FTYPE_UNDEF;
571         }
572     case FC_RCTL_ELS:
573         if (((r_ctl & 0x0F) == 0x2) || ((r_ctl & 0x0F) == 0x3))
574             return FC_FTYPE_ELS;
575         else
576             return FC_FTYPE_UNDEF;
577     case FC_RCTL_LINK_DATA:
578         return FC_FTYPE_LINKDATA;
579     case FC_RCTL_VIDEO:
580         return FC_FTYPE_VDO;
581     case FC_RCTL_BLS:
582         if (type == 0)
583             return FC_FTYPE_BLS;
584         else
585             return FC_FTYPE_UNDEF;
586     case FC_RCTL_LINK_CTL:
587         return FC_FTYPE_LINKCTL;
588     default:
589         return FC_FTYPE_UNDEF;
590     }
591 }
592
593 static const value_string abts_ack_vals[] = {
594         {0x000000,      "ABTS - Cont"},
595         {0x000010,      "ABTS - Abort"},
596         {0x000020,      "ABTS - Stop"},
597         {0x000030,      "ABTS - Imm Seq Retx"},
598         {0,NULL}
599 };
600 static const value_string abts_not_ack_vals[] = {
601         {0x000000,      "ABTS - Abort/MS"},
602         {0x000010,      "ABTS - Abort/SS"},
603         {0x000020,      "ABTS - Process/IB"},
604         {0x000030,      "ABTS - Discard/MS/Imm Retx"},
605         {0,NULL}
606 };
607 static const value_string last_data_frame_vals[] = {
608         {0x000000,      "Last Data Frame - No Info"},
609         {0x004000,      "Last Data Frame - Seq Imm"},
610         {0x008000,      "Last Data Frame - Seq Soon"},
611         {0x00c000,      "Last Data Frame - Seq Delyd"},
612         {0,NULL}
613 };
614 static const value_string ack_0_1_vals[] = {
615         {0x003000,      "ACK_0 Required"},
616         {0x002000,      "ACK_0 Required"},
617         {0x001000,      "ACK_1 Required"},
618         {0x000000,      "no ack required"},
619         {0,NULL}
620 };
621 static const true_false_string tfs_fc_fctl_exchange_responder = {
622         "Exchange Responder",
623         "Exchange Originator"
624 };
625 static const true_false_string tfs_fc_fctl_seq_recipient = {
626         "Seq Recipient",
627         "Seq Initiator"
628 };
629 static const true_false_string tfs_fc_fctl_exchange_first = {
630         "Exchg First",
631         "NOT exchg first"
632 };
633 static const true_false_string tfs_fc_fctl_exchange_last = {
634         "Exchg Last",
635         "NOT exchg last"
636 };
637 static const true_false_string tfs_fc_fctl_seq_last = {
638         "Seq Last",
639         "NOT seq last"
640 };
641 static const true_false_string tfs_fc_fctl_priority = {
642         "Priority",
643         "CS_CTL"
644 };
645 static const true_false_string tfs_fc_fctl_transfer_seq_initiative = {
646         "Transfer Seq Initiative",
647         "NOT transfer seq initiative"
648 };
649 static const true_false_string tfs_fc_fctl_rexmitted_seq = {
650         "Retransmitted Sequence",
651         "NOT retransmitted sequence"
652 };
653 static const true_false_string tfs_fc_fctl_rel_offset = {
654         "Rel Offset SET",
655         "rel offset NOT set"
656 };
657
658
659
660
661 /* code to dissect the  F_CTL bitmask */
662 static void
663 dissect_fc_fctl(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset, gboolean is_ack, guint32 fctl)
664 {
665         proto_item *item;
666         proto_tree *tree;
667         gchar str[256];
668
669         item=proto_tree_add_uint(parent_tree, hf_fc_fctl, tvb, offset, 3, fctl);
670         tree=proto_item_add_subtree(item, ett_fctl);
671
672
673         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_responder, tvb, offset, 3, fctl);
674
675         proto_tree_add_boolean(tree, hf_fc_fctl_seq_recipient, tvb, offset, 3, fctl);
676
677         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_first, tvb, offset, 3, fctl);
678
679         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_last, tvb, offset, 3, fctl);
680
681         proto_tree_add_boolean(tree, hf_fc_fctl_seq_last, tvb, offset, 3, fctl);
682
683         proto_tree_add_boolean(tree, hf_fc_fctl_priority, tvb, offset, 3, fctl);
684
685         proto_tree_add_boolean(tree, hf_fc_fctl_transfer_seq_initiative, tvb, offset, 3, fctl);
686
687         proto_tree_add_uint(tree, hf_fc_fctl_last_data_frame, tvb, offset, 3, fctl);
688
689         proto_tree_add_uint(tree, hf_fc_fctl_ack_0_1, tvb, offset, 3, fctl);
690
691
692         proto_tree_add_boolean(tree, hf_fc_fctl_rexmitted_seq, tvb, offset, 3, fctl);
693
694         if(is_ack){
695                 proto_tree_add_uint(tree, hf_fc_fctl_abts_ack, tvb, offset, 3, fctl);
696         } else {
697                 proto_tree_add_uint(tree, hf_fc_fctl_abts_ack, tvb, offset, 3, fctl);
698         }
699
700         proto_tree_add_boolean(tree, hf_fc_fctl_rel_offset, tvb, offset, 3, fctl);
701
702         fctl_to_str( ((guint8 *)&fctl), str, is_ack);
703         proto_item_append_text(item, "  %s", str);
704 }
705
706 static const value_string fc_bls_proto_val[] = {
707     {FC_BLS_NOP    , "NOP"},
708     {FC_BLS_ABTS   , "ABTS"},
709     {FC_BLS_RMC    , "RMC"},
710     {FC_BLS_BAACC  , "BA_ACC"},
711     {FC_BLS_BARJT  , "BA_RJT"},
712     {FC_BLS_PRMT   , "PRMT"},
713     {0, NULL},
714 };
715
716 static const value_string fc_els_proto_val[] = {
717     {0x01    , "Solicited Data"},
718     {0x02    , "Request"},
719     {0x03    , "Reply"},
720     {0, NULL},
721 };
722
723 /* Code to actually dissect the packets */
724 static void
725 dissect_fc_helper (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean is_ifcp)
726 {
727    /* Set up structures needed to add the protocol subtree and manage it */
728     proto_item *ti=NULL;
729     proto_tree *fc_tree = NULL;
730     tvbuff_t *next_tvb;
731     int offset = 0, next_offset, eisl_offset = -1;
732     gboolean is_lastframe_inseq, is_1frame_inseq, is_valid_frame;
733     gboolean is_exchg_resp = 0;
734     fragment_data *fcfrag_head;
735     guint32 frag_id;
736     guint32 frag_size;
737     guint8 df_ctl, seq_id;
738     
739     guint32 param;
740     guint16 real_seqcnt;
741     guint8 ftype;
742     gboolean is_ack;
743
744     static fc_hdr fchdr;
745     fc_exchange_data *fc_ex=NULL;
746
747     conversation_t *conversation;
748     fcseq_conv_data_t *cdata;
749     fcseq_conv_key_t ckey, *req_key;
750
751     fchdr.fced=NULL;
752
753     /* Make entries in Protocol column and Info column on summary display */
754     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
755         col_set_str(pinfo->cinfo, COL_PROTOCOL, "FC");
756
757     fchdr.r_ctl = tvb_get_guint8 (tvb, offset);
758
759     /* If the R_CTL is the EISL field, skip the first 8 bytes to retrieve the
760      * real FC header. EISL is Cisco-proprietary and is not decoded.
761      */
762     if (fchdr.r_ctl == FC_RCTL_EISL) {
763         eisl_offset = offset;
764         offset += 8;
765         fchdr.r_ctl = tvb_get_guint8 (tvb, offset);
766     }
767
768     /* Each fc endpoint pair gets its own TCP session in iFCP but
769      * the src/dst ids are undefined(==semi-random) in the FC header.
770      * This means we can no track conversations for FC over iFCP by using
771      * the FC src/dst addresses.
772      * For iFCP: Do not update the pinfo src/dst struct and let it remain 
773      * being tcpip src/dst so that request/response matching in the FCP layer
774      * will use ip addresses instead and still work.
775      */
776     if(!is_ifcp){    
777         SET_ADDRESS (&pinfo->dst, AT_FC, 3, tvb_get_ptr(tvb,offset+1,3));
778         SET_ADDRESS (&pinfo->src, AT_FC, 3, tvb_get_ptr(tvb,offset+5,3));
779         pinfo->srcport=0;
780         pinfo->destport=0;
781     }
782     SET_ADDRESS (&fchdr.d_id, pinfo->dst.type, pinfo->dst.len, pinfo->dst.data);
783     SET_ADDRESS (&fchdr.s_id, pinfo->src.type, pinfo->src.len, pinfo->src.data);
784
785     fchdr.cs_ctl = tvb_get_guint8 (tvb, offset+4);
786     fchdr.type  = tvb_get_guint8 (tvb, offset+8);
787     fchdr.fctl=tvb_get_ntoh24(tvb,offset+9);
788     fchdr.seqcnt = tvb_get_ntohs (tvb, offset+14);
789     fchdr.oxid=tvb_get_ntohs(tvb,offset+16);
790     fchdr.rxid=tvb_get_ntohs(tvb,offset+18);
791     param = tvb_get_ntohl (tvb, offset+20);
792     seq_id = tvb_get_guint8 (tvb, offset+12);
793
794     pinfo->oxid = fchdr.oxid;
795     pinfo->rxid = fchdr.rxid;
796     pinfo->ptype = PT_EXCHG;
797     pinfo->r_ctl = fchdr.r_ctl;
798
799     is_ack = ((fchdr.r_ctl == 0xC0) || (fchdr.r_ctl == 0xC1));
800
801     /* There are two ways to determine if this is the first frame of a
802      * sequence. Either:
803      * (i) The SOF bits indicate that this is the first frame OR
804      * (ii) This is an SOFf frame and seqcnt is 0.
805      */
806     is_1frame_inseq = (((pinfo->sof_eof & PINFO_SOF_FIRST_FRAME) == PINFO_SOF_FIRST_FRAME) ||
807                        (((pinfo->sof_eof & PINFO_SOF_SOFF) == PINFO_SOF_SOFF) &&
808                         (fchdr.seqcnt == 0)));
809     
810     is_lastframe_inseq = ((pinfo->sof_eof & PINFO_EOF_LAST_FRAME) == PINFO_EOF_LAST_FRAME);
811
812     if ((pinfo->sof_eof & PINFO_SOF_SOFF) == PINFO_SOF_SOFF) {
813         is_lastframe_inseq = fchdr.fctl & FC_FCTL_SEQ_LAST;
814     }
815     is_valid_frame = ((pinfo->sof_eof & 0x40) == 0x40);
816
817     ftype = fc_get_ftype (fchdr.r_ctl, fchdr.type);
818
819     if (check_col (pinfo->cinfo, COL_INFO)) {
820          col_add_str (pinfo->cinfo, COL_INFO, val_to_str (ftype, fc_ftype_vals,
821                                                           "Unknown Type (0x%x)"));
822
823         if (ftype == FC_FTYPE_LINKCTL)
824             col_append_fstr (pinfo->cinfo, COL_INFO, ", %s",
825                              val_to_str ((fchdr.r_ctl & 0x0F),
826                                           fc_lctl_proto_val,
827                                           "LCTL 0x%x"));
828     }
829     
830     /* In the interest of speed, if "tree" is NULL, don't do any work not
831        necessary to generate protocol tree items. */
832     if (tree) {
833         ti = proto_tree_add_protocol_format (tree, proto_fc, tvb, offset,
834                                              FC_HEADER_SIZE, "Fibre Channel");
835         fc_tree = proto_item_add_subtree (ti, ett_fc);
836     }
837
838     /* Highlight EISL header, if present */
839     if (eisl_offset != -1) {
840         proto_tree_add_item (fc_tree, hf_fc_eisl, tvb, eisl_offset, 8, 0);
841     }
842
843     /* match first exchange with last exchange */
844     if(fchdr.fctl&FC_FCTL_EXCHANGE_FIRST){
845         if(!pinfo->fd->flags.visited){
846             fc_exchange_data fced, *old_fced;
847
848             /* first check if we already have seen this exchange and it
849                is still open/unmatched. 
850             */
851             fced.oxid=fchdr.oxid;
852             SET_ADDRESS(&fced.s_id, fchdr.s_id.type, fchdr.s_id.len, fchdr.s_id.data);
853             SET_ADDRESS(&fced.d_id, fchdr.d_id.type, fchdr.d_id.len, fchdr.d_id.data);
854             old_fced=g_hash_table_lookup(fc_exchange_unmatched, &fced);
855             if(old_fced){
856                 g_hash_table_remove(fc_exchange_unmatched, old_fced);
857             }
858             old_fced=se_alloc(sizeof(fc_exchange_data));
859             old_fced->oxid=fchdr.oxid;
860             COPY_ADDRESS(&old_fced->s_id, &fchdr.s_id);
861             COPY_ADDRESS(&old_fced->d_id, &fchdr.d_id);
862             old_fced->first_exchange_frame=pinfo->fd->num;
863             old_fced->fc_time.nsecs = pinfo->fd->abs_usecs*1000;
864             old_fced->fc_time.secs = pinfo->fd->abs_secs;
865             g_hash_table_insert(fc_exchange_unmatched, old_fced, old_fced);
866             fc_ex=old_fced;
867         } else {
868             fc_exchange_data fced, *old_fced;
869             fced.oxid=fchdr.oxid;
870             fced.first_exchange_frame=pinfo->fd->num;
871             fced.last_exchange_frame=0;
872             old_fced=g_hash_table_lookup(fc_exchange_matched, &fced);
873             fc_ex=old_fced;
874         }
875     }
876     if(fchdr.fctl&FC_FCTL_EXCHANGE_LAST){
877         if(!pinfo->fd->flags.visited){
878             fc_exchange_data fced, *old_fced;
879
880             fced.oxid=fchdr.oxid;
881             SET_ADDRESS(&fced.s_id, fchdr.d_id.type, fchdr.d_id.len, fchdr.d_id.data);
882             SET_ADDRESS(&fced.d_id, fchdr.s_id.type, fchdr.s_id.len, fchdr.s_id.data);
883             old_fced=g_hash_table_lookup(fc_exchange_unmatched, &fced);
884             if(old_fced){
885                 g_hash_table_remove(fc_exchange_unmatched, old_fced);
886                 old_fced->last_exchange_frame=pinfo->fd->num;
887                 g_hash_table_insert(fc_exchange_matched, old_fced, old_fced);
888             }
889             fc_ex=old_fced;
890         } else {
891             fc_exchange_data fced, *old_fced;
892             fced.oxid=fchdr.oxid;
893             fced.first_exchange_frame=0;
894             fced.last_exchange_frame=pinfo->fd->num;
895             old_fced=g_hash_table_lookup(fc_exchange_matched, &fced);
896             fc_ex=old_fced;
897         }
898     }
899     if(fc_ex){
900         if(fchdr.fctl&FC_FCTL_EXCHANGE_FIRST){
901             proto_tree_add_uint(fc_tree, hf_fc_exchange_last_frame, tvb, 0, 0, fc_ex->last_exchange_frame);
902         }
903         if(fchdr.fctl&FC_FCTL_EXCHANGE_LAST){
904             nstime_t delta_time;
905             proto_tree_add_uint(fc_tree, hf_fc_exchange_first_frame, tvb, 0, 0, fc_ex->first_exchange_frame);
906             delta_time.secs = pinfo->fd->abs_secs - fc_ex->fc_time.secs;
907             delta_time.nsecs = pinfo->fd->abs_usecs*1000 - fc_ex->fc_time.nsecs;
908             if (delta_time.nsecs<0){
909                 delta_time.nsecs+=1000000000;
910                 delta_time.secs--;
911             }
912             proto_tree_add_time(ti, hf_fc_time, tvb, 0, 0, &delta_time);
913         }
914     }
915     fchdr.fced=fc_ex;
916
917     switch (fchdr.r_ctl & 0xF0) {
918
919     case FC_RCTL_DEV_DATA:
920     case FC_RCTL_LINK_DATA:
921     case FC_RCTL_VIDEO:
922         /* the lower 4 bits of R_CTL are the information category */
923         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
924                                     FC_RCTL_SIZE, fchdr.r_ctl,
925                                     "R_CTL: 0x%x(%s/%s)",
926                                     fchdr.r_ctl,
927                                     val_to_str ((fchdr.r_ctl & 0xF0),
928                                                 fc_routing_val, "0x%x"),
929                                     val_to_str ((fchdr.r_ctl & 0x0F),
930                                                 fc_iu_val, "0x%x")); 
931         break;
932
933     case FC_RCTL_LINK_CTL:
934         /* the lower 4 bits of R_CTL indicate the type of link ctl frame */
935         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
936                                     FC_RCTL_SIZE, fchdr.r_ctl,
937                                     "R_CTL: 0x%x(%s/%s)",
938                                     fchdr.r_ctl,
939                                     val_to_str ((fchdr.r_ctl & 0xF0),
940                                                 fc_routing_val, "0x%x"),
941                                     val_to_str ((fchdr.r_ctl & 0x0F),
942                                                 fc_lctl_proto_val, "0x%x")); 
943         break;
944
945     case FC_RCTL_BLS:
946         switch (fchdr.type) {
947
948         case 0x00:
949             /* the lower 4 bits of R_CTL indicate the type of BLS frame */
950             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
951                                         FC_RCTL_SIZE, fchdr.r_ctl,
952                                         "R_CTL: 0x%x(%s/%s)",
953                                         fchdr.r_ctl,
954                                         val_to_str ((fchdr.r_ctl & 0xF0),
955                                                     fc_routing_val, "0x%x"),
956                                         val_to_str ((fchdr.r_ctl & 0x0F),
957                                                     fc_bls_proto_val, "0x%x")); 
958             break;
959
960         default:
961             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
962                                         FC_RCTL_SIZE, fchdr.r_ctl,
963                                         "R_CTL: 0x%x(%s/0x%x)",
964                                         fchdr.r_ctl,
965                                         val_to_str ((fchdr.r_ctl & 0xF0),
966                                                     fc_routing_val, "0x%x"),
967                                         fchdr.r_ctl & 0x0F);
968             break;
969         }
970         break;
971
972     case FC_RCTL_ELS:
973         switch (fchdr.type) {
974
975         case 0x01:
976             /* the lower 4 bits of R_CTL indicate the type of ELS frame */
977             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
978                                         FC_RCTL_SIZE, fchdr.r_ctl,
979                                         "R_CTL: 0x%x(%s/%s)",
980                                         fchdr.r_ctl,
981                                         val_to_str ((fchdr.r_ctl & 0xF0),
982                                                     fc_routing_val, "0x%x"),
983                                         val_to_str ((fchdr.r_ctl & 0x0F),
984                                                     fc_els_proto_val, "0x%x")); 
985             break;
986
987         default:
988             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
989                                         FC_RCTL_SIZE, fchdr.r_ctl,
990                                         "R_CTL: 0x%x(%s/0x%x)",
991                                         fchdr.r_ctl,
992                                         val_to_str ((fchdr.r_ctl & 0xF0),
993                                                     fc_routing_val, "0x%x"),
994                                         fchdr.r_ctl & 0x0F);
995             break;
996         }
997         break;
998
999     default:
1000         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
1001                                     FC_RCTL_SIZE, fchdr.r_ctl,
1002                                     "R_CTL: 0x%x(%s/0x%x)",
1003                                     fchdr.r_ctl,
1004                                     val_to_str ((fchdr.r_ctl & 0xF0),
1005                                                 fc_routing_val, "0x%x"),
1006                                     fchdr.r_ctl & 0x0F);
1007         break;
1008     }
1009   
1010     proto_tree_add_uint_hidden (fc_tree, hf_fc_ftype, tvb, offset, 1,
1011                            ftype); 
1012
1013     /* XXX - use "fc_wka_vals[]" on this? */
1014     proto_tree_add_string (fc_tree, hf_fc_did, tvb, offset+1, 3,
1015                            fc_to_str (fchdr.d_id.data));
1016     proto_tree_add_string_hidden (fc_tree, hf_fc_id, tvb, offset+1, 3,
1017                            fc_to_str (fchdr.d_id.data));
1018
1019     proto_tree_add_uint (fc_tree, hf_fc_csctl, tvb, offset+4, 1, fchdr.cs_ctl);
1020
1021     /* XXX - use "fc_wka_vals[]" on this? */
1022     proto_tree_add_string (fc_tree, hf_fc_sid, tvb, offset+5, 3,
1023                            fc_to_str (fchdr.s_id.data));
1024     proto_tree_add_string_hidden (fc_tree, hf_fc_id, tvb, offset+5, 3,
1025                            fc_to_str (fchdr.s_id.data));
1026         
1027     if (ftype == FC_FTYPE_LINKCTL) {
1028         if (((fchdr.r_ctl & 0x0F) == FC_LCTL_FBSYB) ||
1029             ((fchdr.r_ctl & 0x0F) == FC_LCTL_FBSYL)) {
1030             /* for F_BSY frames, the upper 4 bits of the type field specify the
1031              * reason for the BSY.
1032              */
1033             proto_tree_add_uint_format (fc_tree, hf_fc_type, tvb,
1034                                         offset+8, FC_TYPE_SIZE,
1035                                         fchdr.type,"Type: 0x%x(%s)", fchdr.type, 
1036                                         fclctl_get_typestr ((guint8) (fchdr.r_ctl & 0x0F),
1037                                                             fchdr.type));
1038         } else {
1039             proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
1040         }
1041     } else {
1042         proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
1043     }
1044
1045
1046     dissect_fc_fctl(pinfo, fc_tree, tvb, offset+9, is_ack, fchdr.fctl);
1047
1048
1049     proto_tree_add_item (fc_tree, hf_fc_seqid, tvb, offset+12, 1, FALSE);
1050
1051     df_ctl = tvb_get_guint8(tvb, offset+13);
1052
1053     proto_tree_add_uint (fc_tree, hf_fc_dfctl, tvb, offset+13, 1, df_ctl);
1054     proto_tree_add_uint (fc_tree, hf_fc_seqcnt, tvb, offset+14, 2, fchdr.seqcnt);
1055     proto_tree_add_uint (fc_tree, hf_fc_oxid, tvb, offset+16, 2, fchdr.oxid);
1056     proto_tree_add_uint (fc_tree, hf_fc_rxid, tvb, offset+18, 2, fchdr.rxid);
1057
1058     if (ftype == FC_FTYPE_LINKCTL) {
1059         if (((fchdr.r_ctl & 0x0F) == FC_LCTL_FRJT) ||
1060             ((fchdr.r_ctl & 0x0F) == FC_LCTL_PRJT) ||
1061             ((fchdr.r_ctl & 0x0F) == FC_LCTL_PBSY)) {
1062             /* In all these cases of Link Ctl frame, the parameter field
1063              * encodes the detailed error message
1064              */
1065             proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
1066                                         offset+20, 4, param,
1067                                         "Parameter: 0x%x(%s)", param,
1068                                         fclctl_get_paramstr ((fchdr.r_ctl & 0x0F),
1069                                                              param));
1070         } else {
1071             proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
1072         }
1073     } else if (ftype == FC_FTYPE_BLS) {
1074         if ((fchdr.r_ctl & 0x0F) == FC_BLS_ABTS) {
1075             proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
1076                                         offset+20, 4, param, 
1077                                         "Parameter: 0x%x(%s)", param,
1078                                         ((param & 0x0F) == 1 ? "Abort Sequence" :
1079                                          "Abort Exchange"));
1080         } else {
1081             proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20,
1082                                  4, FALSE);
1083         }
1084     } else {
1085         proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
1086     }
1087
1088     /* Skip the Frame_Header */
1089     next_offset = offset + FC_HEADER_SIZE;
1090
1091     /* Network_Header present? */
1092     if (df_ctl & FC_DFCTL_NH) {
1093         /* Yes - dissect it. */
1094         if (tree) {
1095             proto_tree_add_string (fc_tree, hf_fc_nh_da, tvb, next_offset, 8,
1096                                    fcwwn_to_str (tvb_get_ptr (tvb, offset, 8)));
1097             proto_tree_add_string (fc_tree, hf_fc_nh_sa, tvb, offset+8, 8,
1098                                    fcwwn_to_str (tvb_get_ptr (tvb, offset+8, 8)));
1099         }
1100         next_offset += 16;
1101     }
1102
1103     /* XXX - handle Association_Header and Device_Header here */
1104
1105     if (ftype == FC_FTYPE_LINKCTL) {
1106         /* ACK_1 frames and other LINK_CTL frames echo the last seq bit if the
1107          * packet they're ack'ing did not have it set. So, we'll incorrectly
1108          * flag them as being fragmented when they're not. This fixes the
1109          * problem
1110          */
1111         is_lastframe_inseq = TRUE;
1112     } else {
1113         /* XXX is this right?   offset 20, shouldnt it be offset 9? */
1114         is_exchg_resp = ((tvb_get_guint8 (tvb, offset+20) & 0x80) == 0x80);
1115     }
1116
1117     if (tvb_reported_length (tvb) <= FC_HEADER_SIZE)
1118         THROW(ReportedBoundsError);
1119
1120     frag_size = tvb_reported_length (tvb)-FC_HEADER_SIZE;
1121
1122     /* If there is an MDS header, we need to subtract the MDS trailer size */
1123     if ((pinfo->ethertype == ETHERTYPE_UNK) || (pinfo->ethertype == ETHERTYPE_FCFT)) {
1124         if (frag_size <= MDSHDR_TRAILER_SIZE)
1125             THROW(ReportedBoundsError);
1126         frag_size -= MDSHDR_TRAILER_SIZE;
1127     } else if (pinfo->ethertype == ETHERTYPE_BRDWALK) {
1128         if (frag_size <= 8)
1129             THROW(ReportedBoundsError);
1130         frag_size -= 8;         /* 4 byte of FC CRC +
1131                                    4 bytes of error+EOF = 8 bytes  */
1132     }
1133
1134     if (!is_lastframe_inseq) {
1135         /* Show this only as a fragmented FC frame */
1136         if (check_col (pinfo->cinfo, COL_INFO)) {
1137             col_append_str (pinfo->cinfo, COL_INFO, " (Fragmented)");
1138         }
1139     }
1140
1141     /* If this is a fragment, attempt to check if fully reassembled frame is
1142      * present, if we're configured to reassemble.
1143      */
1144     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS) &&
1145         (!is_lastframe_inseq || !is_1frame_inseq) && fc_reassemble &&
1146         tvb_bytes_exist(tvb, FC_HEADER_SIZE, frag_size) && tree) {
1147         /* Add this to the list of fragments */
1148
1149         /* In certain cases such as FICON, the SEQ_CNT is streaming
1150          * i.e. continuously increasing. So, zero does not signify the
1151          * first frame of the sequence. To fix this, we need to save the
1152          * SEQ_CNT of the first frame in sequence and use this value to
1153          * determine the actual offset into a frame.
1154          */
1155         conversation = find_conversation (pinfo->fd->num, &pinfo->src, &pinfo->dst,
1156                                           pinfo->ptype, pinfo->oxid,
1157                                           pinfo->rxid, NO_PORT2);
1158         if (!conversation) {
1159             conversation = conversation_new (pinfo->fd->num, &pinfo->src, &pinfo->dst,
1160                                              pinfo->ptype, pinfo->oxid,
1161                                              pinfo->rxid, NO_PORT2);
1162         }
1163         
1164         ckey.conv_idx = conversation->index;
1165         
1166         cdata = (fcseq_conv_data_t *)g_hash_table_lookup (fcseq_req_hash,
1167                                                           &ckey);
1168
1169         if (is_1frame_inseq) {
1170             if (cdata) {
1171                 /* Since we never free the memory used by an exchange, this maybe a
1172                  * case of another request using the same exchange as a previous
1173                  * req. 
1174                  */
1175                 cdata->seq_cnt = fchdr.seqcnt;
1176             }
1177             else {
1178                 req_key = se_alloc (sizeof(fcseq_conv_key_t));
1179                 req_key->conv_idx = conversation->index;
1180                 
1181                 cdata = se_alloc (sizeof(fcseq_conv_data_t));
1182                 cdata->seq_cnt = fchdr.seqcnt;
1183                 
1184                 g_hash_table_insert (fcseq_req_hash, req_key, cdata);
1185             }
1186             real_seqcnt = 0;
1187         }
1188         else if (cdata != NULL) {
1189             real_seqcnt = fchdr.seqcnt - cdata->seq_cnt ;
1190         }
1191         else {
1192             real_seqcnt = fchdr.seqcnt;
1193         }
1194
1195         /* Verify that this is a valid fragment */
1196         if (is_lastframe_inseq && !is_1frame_inseq && !real_seqcnt) {
1197              /* This is a frame that purports to be the last frame in a
1198               * sequence, is not the first frame, but has a seqcnt that is
1199               * 0. This is a bogus frame, don't attempt to reassemble it.
1200               */
1201              next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1202              if (check_col (pinfo->cinfo, COL_INFO)) {
1203                   col_append_str (pinfo->cinfo, COL_INFO, " (Bogus Fragment)");
1204              }
1205         } else {
1206         
1207              frag_id = ((pinfo->oxid << 16) ^ seq_id) | is_exchg_resp ;
1208
1209              /* We assume that all frames are of the same max size */
1210              fcfrag_head = fragment_add (tvb, FC_HEADER_SIZE, pinfo, frag_id,
1211                                          fc_fragment_table,
1212                                          real_seqcnt * fc_max_frame_size,
1213                                          frag_size,
1214                                          !is_lastframe_inseq);
1215              
1216              if (fcfrag_head) {
1217                   next_tvb = tvb_new_real_data (fcfrag_head->data,
1218                                                 fcfrag_head->datalen,
1219                                                 fcfrag_head->datalen);
1220                   tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1221                   
1222                   /* Add the defragmented data to the data source list. */
1223             add_new_data_source(pinfo, next_tvb, "Reassembled FC");
1224             
1225             if (tree) {
1226                  proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1227                                                 tvb, offset+9, 1, 1);
1228             }
1229              }
1230              else {
1231                   if (tree) {
1232                        proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1233                                                       tvb, offset+9, 1, 0);
1234             }
1235                   next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1236                   call_dissector (data_handle, next_tvb, pinfo, tree);
1237                   return;
1238              }
1239         }
1240     } else {
1241         if (tree) {
1242             proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1243                                            tvb, offset+9, 1, 0);
1244         }
1245         next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1246     }
1247
1248     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS)) {
1249         /* If relative offset is used, only dissect the pdu with
1250          * offset 0 (param) */
1251         if( (fchdr.fctl&FC_FCTL_REL_OFFSET) && param ){
1252             call_dissector (data_handle, next_tvb, pinfo, tree);
1253         } else {
1254             if (!dissector_try_port (fcftype_dissector_table, ftype, 
1255                                 next_tvb, pinfo, tree)) {
1256                 call_dissector (data_handle, next_tvb, pinfo, tree);
1257             }
1258         }
1259     } else if (ftype == FC_FTYPE_BLS) {
1260         if ((fchdr.r_ctl & 0x0F) == FC_BLS_BAACC) {
1261             dissect_fc_ba_acc (next_tvb, pinfo, tree);
1262         } else if ((fchdr.r_ctl & 0x0F) == FC_BLS_BARJT) {
1263             dissect_fc_ba_rjt (next_tvb, pinfo, tree);
1264         }
1265     }
1266
1267     tap_queue_packet(fc_tap, pinfo, &fchdr);
1268 }
1269
1270 static void
1271 dissect_fc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1272 {
1273     dissect_fc_helper (tvb, pinfo, tree, FALSE);
1274 }
1275 static void
1276 dissect_fc_ifcp (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1277 {
1278     dissect_fc_helper (tvb, pinfo, tree, TRUE);
1279 }
1280
1281 /* Register the protocol with Ethereal */
1282
1283 /* this format is require because a script is used to build the C function
1284    that calls all the protocol registration.
1285 */
1286
1287 void
1288 proto_register_fc(void)
1289 {                 
1290
1291 /* Setup list of header fields  See Section 1.6.1 for details*/
1292     static hf_register_info hf[] = {
1293         { &hf_fc_rctl,
1294           { "R_CTL", "fc.r_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
1295             "R_CTL", HFILL }},
1296         { &hf_fc_ftype,
1297           {"Frame type", "fc.ftype", FT_UINT8, BASE_HEX, VALS(fc_ftype_vals),
1298            0x0, "Derived Type", HFILL}},
1299         { &hf_fc_did,
1300           { "Dest Addr", "fc.d_id", FT_STRING, BASE_HEX, NULL, 0x0,
1301             "Destination Address", HFILL}},
1302         { &hf_fc_csctl,
1303           {"CS_CTL", "fc.cs_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
1304            "CS_CTL", HFILL}},
1305         { &hf_fc_sid,
1306           {"Src Addr", "fc.s_id", FT_STRING, BASE_HEX, NULL, 0x0,
1307            "Source Address", HFILL}},
1308         { &hf_fc_id,
1309           {"Addr", "fc.id", FT_STRING, BASE_HEX, NULL, 0x0,
1310            "Source or Destination Address", HFILL}},
1311         { &hf_fc_type,
1312           {"Type", "fc.type", FT_UINT8, BASE_HEX, VALS (fc_fc4_val), 0x0,
1313            "", HFILL}},
1314         { &hf_fc_fctl,
1315           {"F_CTL", "fc.f_ctl", FT_UINT24, BASE_HEX, NULL, 0x0, "", HFILL}},
1316         { &hf_fc_seqid,
1317           {"SEQ_ID", "fc.seq_id", FT_UINT8, BASE_HEX, NULL, 0x0,
1318            "Sequence ID", HFILL}},
1319         { &hf_fc_dfctl,
1320           {"DF_CTL", "fc.df_ctl", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}},
1321         { &hf_fc_seqcnt,
1322           {"SEQ_CNT", "fc.seq_cnt", FT_UINT16, BASE_DEC, NULL, 0x0,
1323            "Sequence Count", HFILL}},
1324         { &hf_fc_oxid,
1325           {"OX_ID", "fc.ox_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Originator ID",
1326            HFILL}},
1327         { &hf_fc_rxid,
1328           {"RX_ID", "fc.rx_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Receiver ID",
1329            HFILL}},
1330         { &hf_fc_param,
1331           {"Parameter", "fc.parameter", FT_UINT32, BASE_HEX, NULL, 0x0, "Parameter",
1332            HFILL}},
1333
1334         { &hf_fc_reassembled,
1335           {"Reassembled Frame", "fc.reassembled", FT_BOOLEAN, BASE_HEX, NULL,
1336            0x0, "", HFILL}},
1337         { &hf_fc_nh_da,
1338           {"Network DA", "fc.nethdr.da", FT_STRING, BASE_HEX, NULL,
1339            0x0, "", HFILL}},
1340         { &hf_fc_nh_sa,
1341           {"Network SA", "fc.nethdr.sa", FT_STRING, BASE_HEX, NULL,
1342            0x0, "", HFILL}},
1343
1344         /* Basic Link Svc field definitions */
1345         { &hf_fc_bls_seqid_vld,
1346           {"SEQID Valid", "fc.bls_seqidvld", FT_UINT8, BASE_HEX,
1347            VALS (fc_bls_seqid_val), 0x0, "", HFILL}},
1348         { &hf_fc_bls_lastvld_seqid,
1349           {"Last Valid SEQID", "fc.bls_lastseqid", FT_UINT8, BASE_HEX, NULL,
1350            0x0, "", HFILL}},
1351         { &hf_fc_bls_oxid,
1352           {"OXID", "fc.bls_oxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
1353         { &hf_fc_bls_rxid,
1354           {"RXID", "fc.bls_rxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
1355         { &hf_fc_bls_lowseqcnt,
1356           {"Low SEQCNT", "fc.bls_lseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
1357            HFILL}},
1358         { &hf_fc_bls_hiseqcnt,
1359           {"High SEQCNT", "fc.bls_hseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
1360            HFILL}},
1361         { &hf_fc_bls_rjtcode,
1362           {"Reason", "fc.bls_reason", FT_UINT8, BASE_HEX, VALS(fc_bls_barjt_val),
1363            0x0, "", HFILL}},
1364         { &hf_fc_bls_rjtdetail,
1365           {"Reason Explanantion", "fc.bls_rjtdetail", FT_UINT8, BASE_HEX,
1366            VALS (fc_bls_barjt_det_val), 0x0, "", HFILL}},
1367         { &hf_fc_bls_vendor,
1368           {"Vendor Unique Reason", "fc.bls_vnduniq", FT_UINT8, BASE_HEX, NULL,
1369            0x0, "", HFILL}},
1370         { &hf_fc_fctl_exchange_responder,
1371           {"ExgRpd", "fc.fctl.exchange_responder", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_responder),
1372            FC_FCTL_EXCHANGE_RESPONDER, "Exchange Responder?", HFILL}},
1373         { &hf_fc_fctl_seq_recipient,
1374           {"SeqRec", "fc.fctl.seq_recipient", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_seq_recipient),
1375            FC_FCTL_SEQ_RECIPIENT, "Seq Recipient?", HFILL}},
1376         { &hf_fc_fctl_exchange_first,
1377           {"ExgFst", "fc.fctl.exchange_first", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_first),
1378            FC_FCTL_EXCHANGE_FIRST, "First Exchange?", HFILL}},
1379         { &hf_fc_fctl_exchange_last,
1380           {"ExgLst", "fc.fctl.exchange_last", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_last),
1381            FC_FCTL_EXCHANGE_LAST, "Last Exchange?", HFILL}},
1382         { &hf_fc_fctl_seq_last,
1383           {"SeqLst", "fc.fctl.seq_last", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_seq_last),
1384            FC_FCTL_SEQ_LAST, "Last Sequence?", HFILL}},
1385         { &hf_fc_fctl_priority,
1386           {"Pri", "fc.fctl.priority", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_priority),
1387            FC_FCTL_PRIORITY, "Priority", HFILL}},
1388         { &hf_fc_fctl_transfer_seq_initiative,
1389           {"TSI", "fc.fctl.transfer_seq_initiative", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_transfer_seq_initiative),
1390            FC_FCTL_TRANSFER_SEQ_INITIATIVE, "Transfer Seq Initiative", HFILL}},
1391         { &hf_fc_fctl_rexmitted_seq,
1392           {"RetSeq", "fc.fctl.rexmitted_seq", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_rexmitted_seq),
1393            FC_FCTL_REXMITTED_SEQ, "Retransmitted Sequence", HFILL}},
1394         { &hf_fc_fctl_rel_offset,
1395           {"RelOff", "fc.fctl.rel_offset", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_rel_offset),
1396            FC_FCTL_REL_OFFSET, "rel offset", HFILL}},
1397         { &hf_fc_fctl_last_data_frame,
1398           {"LDF", "fc.fctl.last_data_frame", FT_UINT24, BASE_HEX, VALS(last_data_frame_vals),
1399            FC_FCTL_LAST_DATA_FRAME_MASK, "Last Data Frame?", HFILL}},
1400         { &hf_fc_fctl_ack_0_1,
1401           {"A01", "fc.fctl.ack_0_1", FT_UINT24, BASE_HEX, VALS(ack_0_1_vals),
1402            FC_FCTL_ACK_0_1_MASK, "Ack 0/1 value", HFILL}},
1403         { &hf_fc_fctl_abts_ack,
1404           {"AA", "fc.fctl.abts_ack", FT_UINT24, BASE_HEX, VALS(abts_ack_vals),
1405            FC_FCTL_ABTS_MASK, "ABTS ACK values", HFILL}},
1406         { &hf_fc_fctl_abts_not_ack,
1407           {"AnA", "fc.fctl.abts_not_ack", FT_UINT24, BASE_HEX, VALS(abts_not_ack_vals),
1408            FC_FCTL_ABTS_MASK, "ABTS not ACK vals", HFILL}},
1409         { &hf_fc_exchange_first_frame,
1410           { "Exchange First In", "fc.exchange_first_frame", FT_FRAMENUM, BASE_NONE, NULL,
1411            0, "The first frame of this exchange is in this frame", HFILL }},
1412         { &hf_fc_exchange_last_frame,
1413           { "Exchange Last In", "fc.exchange_last_frame", FT_FRAMENUM, BASE_NONE, NULL,
1414            0, "The last frame of this exchange is in this frame", HFILL }},
1415         { &hf_fc_time,
1416           { "Time from Exchange First", "fc.time", FT_RELATIVE_TIME, BASE_NONE, NULL,
1417            0, "Time since the first frame of the Exchange", HFILL }},
1418         { &hf_fc_eisl,
1419           {"EISL Header", "fc.eisl", FT_BYTES, BASE_HEX, NULL, 0, "EISL Header", HFILL}},
1420     };
1421
1422     /* Setup protocol subtree array */
1423     static gint *ett[] = {
1424         &ett_fc,
1425         &ett_fcbls,
1426         &ett_fctl
1427     };
1428
1429     module_t *fc_module;
1430
1431     /* Register the protocol name and description */
1432     proto_fc = proto_register_protocol ("Fibre Channel", "FC", "fc");
1433     register_dissector ("fc", dissect_fc, proto_fc);
1434     register_dissector ("fc_ifcp", dissect_fc_ifcp, proto_fc);
1435     fc_tap = register_tap("fc");
1436
1437     /* Required function calls to register the header fields and subtrees used */
1438     proto_register_field_array(proto_fc, hf, array_length(hf));
1439     proto_register_subtree_array(ett, array_length(ett));
1440
1441     fcftype_dissector_table = register_dissector_table ("fc.ftype",
1442                                                         "FC Frame Type",
1443                                                         FT_UINT8, BASE_HEX);
1444
1445     /* Register preferences */
1446     fc_module = prefs_register_protocol (proto_fc, NULL);
1447     prefs_register_bool_preference (fc_module,
1448                                     "reassemble",
1449                                     "Reassemble multi-frame sequences",
1450                                     "If enabled, reassembly of multi-frame "
1451                                     "sequences is done",
1452                                     &fc_reassemble);
1453     prefs_register_uint_preference (fc_module,
1454                                     "max_frame_size", "Max FC Frame Size",
1455                                     "This is the size of non-last frames in a "
1456                                     "multi-frame sequence", 10,
1457                                     &fc_max_frame_size);
1458     
1459     register_init_routine(fc_defragment_init);
1460     register_init_routine (fc_exchange_init_protocol);
1461 }
1462
1463
1464 /* If this dissector uses sub-dissector registration add a registration routine.
1465    This format is required because a script is used to find these routines and
1466    create the code that calls these routines.
1467 */
1468 void
1469 proto_reg_handoff_fc (void)
1470 {
1471     data_handle = find_dissector("data");
1472 }