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