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