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