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