In "col_set_addr()", don't do anything other than clearing the column
[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.14 2003/08/24 01:06:20 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40
41 #ifdef HAVE_NETINET_IN_H
42 # include <netinet/in.h>
43 #endif
44
45 #include <glib.h>
46
47 #ifdef NEED_SNPRINTF_H
48 # include "snprintf.h"
49 #endif
50
51 #include <epan/packet.h>
52 #include "prefs.h"
53 #include "reassemble.h"
54 #include "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.data[0]!=0)||(fced1->s_id.data[1]!=0)||(fced1->s_id.data[2]!=0)) && CMP_ADDRESS(&fced1->s_id, &fced2->s_id) ){
169         return 0;
170     }
171     if(CMP_ADDRESS(&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_routing_val[] = {
283     {FC_RCTL_DEV_DATA,  "Device_Data"},
284     {FC_RCTL_ELS,       "Extended Link Services"},
285     {FC_RCTL_LINK_DATA, "FC-4 Link_Data"},
286     {FC_RCTL_VIDEO,     "Video_Data"},
287     {FC_RCTL_BLS,       "Basic Link Services"},
288     {FC_RCTL_LINK_CTL,  "Link_Control Frame"},
289     {0, NULL},
290 };
291
292 static const value_string fc_iu_val[] = {
293     {FC_IU_UNCATEGORIZED   , "Uncategorized Data"},
294     {FC_IU_SOLICITED_DATA  , "Solicited Data"},
295     {FC_IU_UNSOLICITED_CTL , "Unsolicited Control"},
296     {FC_IU_SOLICITED_CTL   , "Solicited Control"},
297     {FC_IU_UNSOLICITED_DATA, "Solicited Data"},
298     {FC_IU_DATA_DESCRIPTOR , "Data Descriptor"},
299     {FC_IU_UNSOLICITED_CMD , "Unsolicited Command"},
300     {FC_IU_CMD_STATUS      , "Command Status"},
301     {0, NULL},
302 };
303
304
305 static void fc_defragment_init(void)
306 {
307   fragment_table_init(&fc_fragment_table);
308 }
309
310
311 static gchar *
312 fctl_to_str (const guint8 *fctl, gchar *str, gboolean is_ack)
313 {
314     int stroff = 0;
315     guint8 tmp = 0;
316
317     if (str == NULL)
318         return (str);
319     
320     if (fctl[0] & 0x80) {
321         strcpy (str, "Exchange Responder, ");
322         stroff += 20;
323     }
324     else {
325         strcpy (str, "Exchange Originator, ");
326         stroff += 21;
327     }
328
329     if (fctl[0] & 0x40) {
330         strcpy (&str[stroff], "Seq Recipient, ");
331         stroff += 15;
332     }
333     else {
334         strcpy (&str[stroff], "Seq Initiator, ");
335         stroff += 15;
336     }
337
338     if (fctl[0] & 0x20) {
339         strcpy (&str[stroff], "Exchg First, ");
340         stroff += 13;
341     }
342
343     if (fctl[0] & 0x10) {
344         strcpy (&str[stroff], "Exchg Last, ");
345         stroff += 12;
346     }
347
348     if (fctl[0] & 0x8) {
349         strcpy (&str[stroff], "Seq Last, ");
350         stroff += 10;
351     }
352
353     if (fctl[0] & 0x2) {
354         strcpy (&str[stroff], "Priority, ");
355         stroff += 10;
356     }
357     else {
358         strcpy (&str[stroff], "CS_CTL, ");
359         stroff += 8;
360     }
361
362     if (fctl[0] & 0x1) {
363         strcpy (&str[stroff], "Transfer Seq Initiative, ");
364         stroff += 25;
365     }
366
367     if (fctl[1] & 0x30) {
368         strcpy (&str[stroff], "ACK_0 Reqd, ");
369         stroff += 12;
370     }
371     else if (fctl[1] & 0x10) {
372         strcpy (&str[stroff], "ACK_1 Reqd, ");
373         stroff += 12;
374     }
375
376     if (fctl[1] & 0x2) {
377         strcpy (&str[stroff], "Rexmitted Seq, ");
378         stroff += 15;
379     }
380
381     tmp = fctl[2] & 0xC0;
382     switch (tmp) {
383     case 0:
384         strcpy (&str[stroff], "Last Data Frame - No Info, ");
385         stroff += 27;
386         break;
387     case 1:
388         strcpy (&str[stroff], "Last Data Frame - Seq Imm, ");
389         stroff += 27;
390         break;
391     case 2:
392         strcpy (&str[stroff], "Last Data Frame - Seq Soon, ");
393         stroff += 28;
394         break;
395     case 3:
396         strcpy (&str[stroff], "Last Data Frame - Seq Delyd, ");
397         stroff += 29;
398         break;
399     }
400
401     tmp = fctl[2] & 0x30;
402     switch (tmp) {
403     case 0:
404         if (is_ack) {
405             strcpy (&str[stroff], "ABTS - Cont, ");
406             stroff += 13;
407         }
408         else {
409             strcpy (&str[stroff], "ABTS - Abort/MS, ");
410             stroff += 17;
411         }
412         break;
413     case 0x10:
414         if (is_ack) {
415             strcpy (&str[stroff], "ABTS - AbortABTS - Abort, ");
416             stroff += 14;
417         }
418         else {
419             strcpy (&str[stroff], "ABTS - Abort/SS, ");
420             stroff += 17;
421         }
422         break;
423     case 0x20:
424         if (is_ack) {
425             strcpy (&str[stroff], "ABTS - Stop, ");
426             stroff += 13;
427         }
428         else {
429             strcpy (&str[stroff], "ABTS - Process/IB, ");
430             stroff += 19;
431         }
432         break;
433     case 0x30:
434         if (is_ack) {
435             strcpy (&str[stroff], "ABTS - Imm Seq Retx, ");
436             stroff += 21;
437         }
438         else {
439             strcpy (&str[stroff], "ABTS - Discard/MS/Imm Retx, ");
440             stroff += 28;
441         }
442         break;
443     }
444
445     if (fctl[2] & 0x8) {
446         strcpy (&str[stroff], "Rel Offset = 1");
447         stroff += 14;
448     }
449
450     return (str);
451 }
452
453 /* BA_ACC & BA_RJT are decoded in this file itself instead of a traditional
454  * dedicated file and dissector format because the dissector would require some
455  * fields of the FC_HDR such as param in some cases, type in some others, the
456  * lower 4 bits of r_ctl in some other cases etc. So, we decode BLS & Link Ctl
457  * in this file itself.
458  */
459 static void
460 dissect_fc_ba_acc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
461 {
462     /* Set up structures needed to add the protocol subtree and manage it */
463     proto_item *ti;
464     proto_tree *acc_tree;
465     int offset = 0;
466
467     /* Make entries in Protocol column and Info column on summary display */
468     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
469         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
470
471     if (check_col(pinfo->cinfo, COL_INFO)) 
472         col_set_str(pinfo->cinfo, COL_INFO, "BA_ACC");
473
474     if (tree) {
475         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
476         acc_tree = proto_item_add_subtree (ti, ett_fcbls);
477
478         proto_tree_add_item (acc_tree, hf_fc_bls_seqid_vld, tvb, offset++, 1, FALSE);
479         proto_tree_add_item (acc_tree, hf_fc_bls_lastvld_seqid, tvb, offset++, 1, FALSE);
480         offset += 2; /* Skip reserved field */
481         proto_tree_add_item (acc_tree, hf_fc_bls_oxid, tvb, offset, 2, FALSE);
482         offset += 2;
483         proto_tree_add_item (acc_tree, hf_fc_bls_rxid, tvb, offset, 2, FALSE);
484         offset += 2;
485         proto_tree_add_item (acc_tree, hf_fc_bls_lowseqcnt, tvb, offset, 2, FALSE);
486         offset += 2;
487         proto_tree_add_item (acc_tree, hf_fc_bls_hiseqcnt, tvb, offset, 2, FALSE);
488     }
489 }
490
491 static void
492 dissect_fc_ba_rjt (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
493 {
494     /* Set up structures needed to add the protocol subtree and manage it */
495     proto_item *ti;
496     proto_tree *rjt_tree;
497     int offset = 0;
498
499     /* Make entries in Protocol column and Info column on summary display */
500     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
501         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
502
503     if (check_col(pinfo->cinfo, COL_INFO)) 
504         col_set_str(pinfo->cinfo, COL_INFO, "BA_RJT");
505
506     if (tree) {
507         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
508         rjt_tree = proto_item_add_subtree (ti, ett_fcbls);
509
510         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtcode, tvb, offset+1, 1, FALSE);
511         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtdetail, tvb, offset+2, 1, FALSE);
512         proto_tree_add_item (rjt_tree, hf_fc_bls_vendor, tvb, offset+3, 1, FALSE);
513     }
514 }
515
516 static guint8
517 fc_get_ftype (guint8 r_ctl, guint8 type)
518 {
519     /* A simple attempt to determine the upper level protocol based on the
520      * r_ctl & type fields.
521      */
522     switch (r_ctl & 0xF0) {
523     case FC_RCTL_DEV_DATA:
524         switch (type) {
525         case FC_TYPE_SWILS:
526             if ((r_ctl == 0x2) || (r_ctl == 0x3))
527                 return FC_FTYPE_SWILS;
528             else
529                 return FC_FTYPE_UNDEF;
530         case FC_TYPE_IP:
531             return FC_FTYPE_IP;
532         case FC_TYPE_SCSI:
533             return FC_FTYPE_SCSI;
534         case FC_TYPE_FCCT:
535             return FC_FTYPE_FCCT;
536         default:
537             return FC_FTYPE_UNDEF;
538         }
539         break;
540     case FC_RCTL_ELS:
541         if (((r_ctl & 0x0F) == 0x2) || ((r_ctl & 0x0F) == 0x3))
542             return FC_FTYPE_ELS;
543         else
544             return FC_FTYPE_UNDEF;
545         break;
546     case FC_RCTL_LINK_DATA:
547         return FC_FTYPE_LINKDATA;
548         break;
549     case FC_RCTL_VIDEO:
550         return FC_FTYPE_VDO;
551         break;
552     case FC_RCTL_BLS:
553         if (type == 0)
554             return FC_FTYPE_BLS;
555         else
556             return FC_FTYPE_UNDEF;
557         break;
558     case FC_RCTL_LINK_CTL:
559         return FC_FTYPE_LINKCTL;
560         break;
561     default:
562         return FC_FTYPE_UNDEF;
563         break;
564     }
565 }
566
567 static const value_string abts_ack_vals[] = {
568         {0x000000,      "ABTS - Cont"},
569         {0x000010,      "ABTS - Abort"},
570         {0x000020,      "ABTS - Stop"},
571         {0x000030,      "ABTS - Imm Seq Retx"},
572         {0,NULL}
573 };
574 static const value_string abts_not_ack_vals[] = {
575         {0x000000,      "ABTS - Abort/MS"},
576         {0x000010,      "ABTS - Abort/SS"},
577         {0x000020,      "ABTS - Process/IB"},
578         {0x000030,      "ABTS - Discard/MS/Imm Retx"},
579         {0,NULL}
580 };
581 static const value_string last_data_frame_vals[] = {
582         {0x000000,      "Last Data Frame - No Info"},
583         {0x004000,      "Last Data Frame - Seq Imm"},
584         {0x008000,      "Last Data Frame - Seq Soon"},
585         {0x00c000,      "Last Data Frame - Seq Delyd"},
586         {0,NULL}
587 };
588 static const value_string ack_0_1_vals[] = {
589         {0x003000,      "ACK_0 Required"},
590         {0x002000,      "ACK_0 Required"},
591         {0x001000,      "ACK_1 Required"},
592         {0x000000,      "no ack required"},
593         {0,NULL}
594 };
595 static const true_false_string tfs_fc_fctl_exchange_responder = {
596         "Exchange Responder",
597         "Exchange Originator"
598 };
599 static const true_false_string tfs_fc_fctl_seq_recipient = {
600         "Seq Recipient",
601         "Seq Initiator"
602 };
603 static const true_false_string tfs_fc_fctl_exchange_first = {
604         "Exchg First",
605         "NOT exchg first"
606 };
607 static const true_false_string tfs_fc_fctl_exchange_last = {
608         "Exchg Last",
609         "NOT exchg last"
610 };
611 static const true_false_string tfs_fc_fctl_seq_last = {
612         "Seq Last",
613         "NOT seq last"
614 };
615 static const true_false_string tfs_fc_fctl_priority = {
616         "Priority",
617         "CS_CTL"
618 };
619 static const true_false_string tfs_fc_fctl_transfer_seq_initiative = {
620         "Transfer Seq Initiative",
621         "NOT transfer seq initiative"
622 };
623 static const true_false_string tfs_fc_fctl_rexmitted_seq = {
624         "Retransmitted Sequence",
625         "NOT retransmitted sequence"
626 };
627 static const true_false_string tfs_fc_fctl_rel_offset = {
628         "Rel Offset SET",
629         "rel offset NOT set"
630 };
631
632
633
634
635 /* code to dissect the  F_CTL bitmask */
636 static void
637 dissect_fc_fctl(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset, gboolean is_ack, guint32 fctl)
638 {
639         proto_item *item;
640         proto_tree *tree;
641         gchar str[256];
642
643         item=proto_tree_add_uint(parent_tree, hf_fc_fctl, tvb, offset, 3, fctl);
644         tree=proto_item_add_subtree(item, ett_fctl);
645
646
647         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_responder, tvb, offset, 3, fctl);
648
649         proto_tree_add_boolean(tree, hf_fc_fctl_seq_recipient, tvb, offset, 3, fctl);
650
651         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_first, tvb, offset, 3, fctl);
652
653         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_last, tvb, offset, 3, fctl);
654
655         proto_tree_add_boolean(tree, hf_fc_fctl_seq_last, tvb, offset, 3, fctl);
656
657         proto_tree_add_boolean(tree, hf_fc_fctl_priority, tvb, offset, 3, fctl);
658
659         proto_tree_add_boolean(tree, hf_fc_fctl_transfer_seq_initiative, tvb, offset, 3, fctl);
660
661         proto_tree_add_uint(tree, hf_fc_fctl_last_data_frame, tvb, offset, 3, fctl);
662
663         proto_tree_add_uint(tree, hf_fc_fctl_ack_0_1, tvb, offset, 3, fctl);
664
665
666         proto_tree_add_boolean(tree, hf_fc_fctl_rexmitted_seq, tvb, offset, 3, fctl);
667
668         if(is_ack){
669                 proto_tree_add_uint(tree, hf_fc_fctl_abts_ack, tvb, offset, 3, fctl);
670         } else {
671                 proto_tree_add_uint(tree, hf_fc_fctl_abts_ack, tvb, offset, 3, fctl);
672         }
673
674         proto_tree_add_boolean(tree, hf_fc_fctl_rel_offset, tvb, offset, 3, fctl);
675
676         fctl_to_str( ((guint8 *)&fctl)+1, str, is_ack);
677         proto_item_append_text(item, "  %s", str);
678 }
679
680 static const value_string fc_bls_proto_val[] = {
681     {FC_BLS_NOP    , "NOP"},
682     {FC_BLS_ABTS   , "ABTS"},
683     {FC_BLS_RMC    , "RMC"},
684     {FC_BLS_BAACC  , "BA_ACC"},
685     {FC_BLS_BARJT  , "BA_RJT"},
686     {FC_BLS_PRMT   , "PRMT"},
687     {0, NULL},
688 };
689
690 static const value_string fc_els_proto_val[] = {
691     {0x01    , "Solicited Data"},
692     {0x02    , "Request"},
693     {0x03    , "Reply"},
694     {0, NULL},
695 };
696
697 /* Code to actually dissect the packets */
698 static void
699 dissect_fc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
700 {
701    /* Set up structures needed to add the protocol subtree and manage it */
702     proto_item *ti=NULL;
703     proto_tree *fc_tree = NULL;
704     tvbuff_t *next_tvb;
705     int offset = 0, next_offset;
706     gboolean is_lastframe_inseq;
707     gboolean is_exchg_resp = 0;
708     fragment_data *fcfrag_head;
709     guint32 frag_id;
710     guint32 frag_size;
711     guint8 df_ctl;
712     
713     guint32 param;
714     guint8 ftype;
715     gboolean is_ack;
716
717     static fc_hdr fchdr;
718     fc_exchange_data *fc_ex=NULL;
719
720     fchdr.fced=NULL;
721
722     /* Make entries in Protocol column and Info column on summary display */
723     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
724         col_set_str(pinfo->cinfo, COL_PROTOCOL, "FC");
725
726     fchdr.r_ctl = tvb_get_guint8 (tvb, offset);
727
728     /* If the R_CTL is the EISL field, skip the first 8 bytes to retrieve the
729      * real FC header. EISL is Cisco-proprietary and is not decoded.
730      */
731     if (fchdr.r_ctl == FC_RCTL_EISL) {
732         offset += 8;
733         fchdr.r_ctl = tvb_get_guint8 (tvb, offset);
734     }
735     
736     SET_ADDRESS (&pinfo->dst, AT_FC, 3, tvb_get_ptr(tvb,offset+1,3));
737     SET_ADDRESS (&fchdr.d_id, AT_FC, 3, tvb_get_ptr(tvb,offset+1,3));
738     SET_ADDRESS (&pinfo->src, AT_FC, 3, tvb_get_ptr(tvb,offset+5,3));
739     SET_ADDRESS (&fchdr.s_id, AT_FC, 3, tvb_get_ptr(tvb,offset+5,3));
740     fchdr.cs_ctl = tvb_get_guint8 (tvb, offset+4);
741     fchdr.type  = tvb_get_guint8 (tvb, offset+8);
742     fchdr.fctl=tvb_get_ntoh24(tvb,offset+9);
743     fchdr.seqcnt = tvb_get_ntohs (tvb, offset+14);
744     fchdr.oxid=tvb_get_ntohs(tvb,offset+16);
745     fchdr.rxid=tvb_get_ntohs(tvb,offset+18);
746     param = tvb_get_ntohl (tvb, offset+20);
747
748     pinfo->oxid = fchdr.oxid;
749     pinfo->rxid = fchdr.rxid;
750     pinfo->ptype = PT_EXCHG;
751     pinfo->r_ctl = fchdr.r_ctl;
752
753     is_ack = ((fchdr.r_ctl == 0xC0) || (fchdr.r_ctl == 0xC1));
754
755     ftype = fc_get_ftype (fchdr.r_ctl, fchdr.type);
756     
757     if (check_col (pinfo->cinfo, COL_INFO)) {
758         col_add_str (pinfo->cinfo, COL_INFO, match_strval (ftype, fc_ftype_vals));
759
760         if (ftype == FC_FTYPE_LINKCTL)
761             col_append_fstr (pinfo->cinfo, COL_INFO, ", %s",
762                              match_strval ((fchdr.r_ctl & 0x0F),
763                                            fc_lctl_proto_val));
764     }
765     
766     /* In the interest of speed, if "tree" is NULL, don't do any work not
767        necessary to generate protocol tree items. */
768     if (tree) {
769         ti = proto_tree_add_protocol_format (tree, proto_fc, tvb, offset,
770                                              FC_HEADER_SIZE, "Fibre Channel");
771         fc_tree = proto_item_add_subtree (ti, ett_fc);
772     }
773
774     /* match first exchange with last exchange */
775     if(fchdr.fctl&FC_FCTL_EXCHANGE_FIRST){
776         if(!pinfo->fd->flags.visited){
777             fc_exchange_data fced, *old_fced;
778
779             /* first check if we already have seen this exchange and it
780                is still open/unmatched. 
781             */
782             fced.oxid=fchdr.oxid;
783             SET_ADDRESS(&fced.s_id, fchdr.s_id.type, fchdr.s_id.len, fchdr.s_id.data);
784             SET_ADDRESS(&fced.d_id, fchdr.d_id.type, fchdr.d_id.len, fchdr.d_id.data);
785             old_fced=g_hash_table_lookup(fc_exchange_unmatched, &fced);
786             if(old_fced){
787                 g_hash_table_remove(fc_exchange_unmatched, old_fced);
788             }
789             old_fced=g_mem_chunk_alloc(fc_exchange_vals);
790             old_fced->oxid=fchdr.oxid;
791             COPY_ADDRESS(&old_fced->s_id, &fchdr.s_id);
792             COPY_ADDRESS(&old_fced->d_id, &fchdr.d_id);
793             old_fced->first_exchange_frame=pinfo->fd->num;
794             old_fced->fc_time.nsecs = pinfo->fd->abs_usecs*1000;
795             old_fced->fc_time.secs = pinfo->fd->abs_secs;
796             g_hash_table_insert(fc_exchange_unmatched, old_fced, old_fced);
797             fc_ex=old_fced;
798         } else {
799             fc_exchange_data fced, *old_fced;
800             fced.oxid=fchdr.oxid;
801             fced.first_exchange_frame=pinfo->fd->num;
802             fced.last_exchange_frame=0;
803             old_fced=g_hash_table_lookup(fc_exchange_matched, &fced);
804             fc_ex=old_fced;
805         }
806     }
807     if(fchdr.fctl&FC_FCTL_EXCHANGE_LAST){
808         if(!pinfo->fd->flags.visited){
809             fc_exchange_data fced, *old_fced;
810
811             fced.oxid=fchdr.oxid;
812             SET_ADDRESS(&fced.s_id, fchdr.d_id.type, fchdr.d_id.len, fchdr.d_id.data);
813             SET_ADDRESS(&fced.d_id, fchdr.s_id.type, fchdr.s_id.len, fchdr.s_id.data);
814             old_fced=g_hash_table_lookup(fc_exchange_unmatched, &fced);
815             if(old_fced){
816                 g_hash_table_remove(fc_exchange_unmatched, old_fced);
817                 old_fced->last_exchange_frame=pinfo->fd->num;
818                 g_hash_table_insert(fc_exchange_matched, old_fced, old_fced);
819             }
820             fc_ex=old_fced;
821         } else {
822             fc_exchange_data fced, *old_fced;
823             fced.oxid=fchdr.oxid;
824             fced.first_exchange_frame=0;
825             fced.last_exchange_frame=pinfo->fd->num;
826             old_fced=g_hash_table_lookup(fc_exchange_matched, &fced);
827             fc_ex=old_fced;
828         }
829     }
830     if(fc_ex){
831         if(fchdr.fctl&FC_FCTL_EXCHANGE_FIRST){
832             proto_tree_add_uint(fc_tree, hf_fc_exchange_last_frame, tvb, 0, 0, fc_ex->last_exchange_frame);
833         }
834         if(fchdr.fctl&FC_FCTL_EXCHANGE_LAST){
835             nstime_t delta_time;
836             proto_tree_add_uint(fc_tree, hf_fc_exchange_first_frame, tvb, 0, 0, fc_ex->first_exchange_frame);
837             delta_time.secs = pinfo->fd->abs_secs - fc_ex->fc_time.secs;
838             delta_time.nsecs = pinfo->fd->abs_usecs*1000 - fc_ex->fc_time.nsecs;
839             if (delta_time.nsecs<0){
840                 delta_time.nsecs+=1000000000;
841                 delta_time.secs--;
842             }
843             proto_tree_add_time(ti, hf_fc_time, tvb, 0, 0, &delta_time);
844         }
845     }
846     fchdr.fced=fc_ex;
847
848     switch (fchdr.r_ctl & 0xF0) {
849
850     case FC_RCTL_DEV_DATA:
851     case FC_RCTL_LINK_DATA:
852     case FC_RCTL_VIDEO:
853         /* the lower 4 bits of R_CTL are the information category */
854         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
855                                     FC_RCTL_SIZE, fchdr.r_ctl,
856                                     "R_CTL: 0x%x(%s/%s)",
857                                     fchdr.r_ctl,
858                                     val_to_str ((fchdr.r_ctl & 0xF0),
859                                                 fc_routing_val, "0x%x"),
860                                     val_to_str ((fchdr.r_ctl & 0x0F),
861                                                 fc_iu_val, "0x%x")); 
862         break;
863
864     case FC_RCTL_LINK_CTL:
865         /* the lower 4 bits of R_CTL indicate the type of link ctl frame */
866         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
867                                     FC_RCTL_SIZE, fchdr.r_ctl,
868                                     "R_CTL: 0x%x(%s/%s)",
869                                     fchdr.r_ctl,
870                                     val_to_str ((fchdr.r_ctl & 0xF0),
871                                                 fc_routing_val, "0x%x"),
872                                     val_to_str ((fchdr.r_ctl & 0x0F),
873                                                 fc_lctl_proto_val, "0x%x")); 
874         break;
875
876     case FC_RCTL_BLS:
877         switch (fchdr.type) {
878
879         case 0x00:
880             /* the lower 4 bits of R_CTL indicate the type of BLS frame */
881             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
882                                         FC_RCTL_SIZE, fchdr.r_ctl,
883                                         "R_CTL: 0x%x(%s/%s)",
884                                         fchdr.r_ctl,
885                                         val_to_str ((fchdr.r_ctl & 0xF0),
886                                                     fc_routing_val, "0x%x"),
887                                         val_to_str ((fchdr.r_ctl & 0x0F),
888                                                     fc_bls_proto_val, "0x%x")); 
889             break;
890
891         default:
892             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
893                                         FC_RCTL_SIZE, fchdr.r_ctl,
894                                         "R_CTL: 0x%x(%s/0x%x)",
895                                         fchdr.r_ctl,
896                                         val_to_str ((fchdr.r_ctl & 0xF0),
897                                                     fc_routing_val, "0x%x"),
898                                         fchdr.r_ctl & 0x0F);
899             break;
900         }
901         break;
902
903     case FC_RCTL_ELS:
904         switch (fchdr.type) {
905
906         case 0x01:
907             /* the lower 4 bits of R_CTL indicate the type of ELS frame */
908             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
909                                         FC_RCTL_SIZE, fchdr.r_ctl,
910                                         "R_CTL: 0x%x(%s/%s)",
911                                         fchdr.r_ctl,
912                                         val_to_str ((fchdr.r_ctl & 0xF0),
913                                                     fc_routing_val, "0x%x"),
914                                         val_to_str ((fchdr.r_ctl & 0x0F),
915                                                     fc_els_proto_val, "0x%x")); 
916             break;
917
918         default:
919             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
920                                         FC_RCTL_SIZE, fchdr.r_ctl,
921                                         "R_CTL: 0x%x(%s/0x%x)",
922                                         fchdr.r_ctl,
923                                         val_to_str ((fchdr.r_ctl & 0xF0),
924                                                     fc_routing_val, "0x%x"),
925                                         fchdr.r_ctl & 0x0F);
926             break;
927         }
928         break;
929
930     default:
931         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
932                                     FC_RCTL_SIZE, fchdr.r_ctl,
933                                     "R_CTL: 0x%x(%s/0x%x)",
934                                     fchdr.r_ctl,
935                                     val_to_str ((fchdr.r_ctl & 0xF0),
936                                                 fc_routing_val, "0x%x"),
937                                     fchdr.r_ctl & 0x0F);
938         break;
939     }
940         
941     proto_tree_add_uint_hidden (fc_tree, hf_fc_ftype, tvb, offset, 1,
942                            ftype); 
943
944     /* XXX - use "fc_wka_vals[]" on this? */
945     proto_tree_add_string (fc_tree, hf_fc_did, tvb, offset+1, 3,
946                            fc_to_str (fchdr.d_id.data));
947     proto_tree_add_string_hidden (fc_tree, hf_fc_id, tvb, offset+1, 3,
948                            fc_to_str (fchdr.d_id.data));
949
950     proto_tree_add_uint (fc_tree, hf_fc_csctl, tvb, offset+4, 1, fchdr.cs_ctl);
951
952     /* XXX - use "fc_wka_vals[]" on this? */
953     proto_tree_add_string (fc_tree, hf_fc_sid, tvb, offset+5, 3,
954                            fc_to_str (fchdr.s_id.data));
955     proto_tree_add_string_hidden (fc_tree, hf_fc_id, tvb, offset+5, 3,
956                            fc_to_str (fchdr.s_id.data));
957         
958     if (ftype == FC_FTYPE_LINKCTL) {
959         if (((fchdr.r_ctl & 0x0F) == FC_LCTL_FBSYB) ||
960             ((fchdr.r_ctl & 0x0F) == FC_LCTL_FBSYL)) {
961             /* for F_BSY frames, the upper 4 bits of the type field specify the
962              * reason for the BSY.
963              */
964             proto_tree_add_uint_format (fc_tree, hf_fc_type, tvb,
965                                         offset+8, FC_TYPE_SIZE,
966                                         fchdr.type,"Type: 0x%x(%s)", fchdr.type, 
967                                         fclctl_get_typestr (fchdr.r_ctl & 0x0F,
968                                                             fchdr.type));
969         } else {
970             proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
971         }
972     } else {
973         proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
974     }
975
976
977     dissect_fc_fctl(pinfo, fc_tree, tvb, offset+9, is_ack, fchdr.fctl);
978
979
980     proto_tree_add_item (fc_tree, hf_fc_seqid, tvb, offset+12, 1, FALSE);
981
982     df_ctl = tvb_get_guint8(tvb, offset+13);
983
984     proto_tree_add_uint (fc_tree, hf_fc_dfctl, tvb, offset+13, 1, df_ctl);
985     proto_tree_add_uint (fc_tree, hf_fc_seqcnt, tvb, offset+14, 2, fchdr.seqcnt);
986     proto_tree_add_uint (fc_tree, hf_fc_oxid, tvb, offset+16, 2, fchdr.oxid);
987     proto_tree_add_uint (fc_tree, hf_fc_rxid, tvb, offset+18, 2, fchdr.rxid);
988
989     if (ftype == FC_FTYPE_LINKCTL) {
990         if (((fchdr.r_ctl & 0x0F) == FC_LCTL_FRJT) ||
991             ((fchdr.r_ctl & 0x0F) == FC_LCTL_PRJT) ||
992             ((fchdr.r_ctl & 0x0F) == FC_LCTL_PBSY)) {
993             /* In all these cases of Link Ctl frame, the parameter field
994              * encodes the detailed error message
995              */
996             proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
997                                         offset+20, 4, param,
998                                         "Parameter: 0x%x(%s)", param,
999                                         fclctl_get_paramstr ((fchdr.r_ctl & 0x0F),
1000                                                              param));
1001         } else {
1002             proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
1003         }
1004     } else if (ftype == FC_FTYPE_BLS) {
1005         if ((fchdr.r_ctl & 0x0F) == FC_BLS_ABTS) {
1006             proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
1007                                         offset+20, 4, param, 
1008                                         "Parameter: 0x%x(%s)", param,
1009                                         ((param & 0x0F) == 1 ? "Abort Sequence" :
1010                                          "Abort Exchange"));
1011         } else {
1012             proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20,
1013                                  4, FALSE);
1014         }
1015     } else {
1016         proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
1017     }
1018
1019     /* Skip the Frame_Header */
1020     next_offset = offset + FC_HEADER_SIZE;
1021
1022     /* Network_Header present? */
1023     if (df_ctl & FC_DFCTL_NH) {
1024         /* Yes - dissect it. */
1025         if (tree) {
1026             proto_tree_add_string (fc_tree, hf_fc_nh_da, tvb, next_offset, 8,
1027                                    fcwwn_to_str (tvb_get_ptr (tvb, offset, 8)));
1028             proto_tree_add_string (fc_tree, hf_fc_nh_sa, tvb, offset+8, 8,
1029                                    fcwwn_to_str (tvb_get_ptr (tvb, offset+8, 8)));
1030         }
1031         next_offset += 16;
1032     }
1033
1034     /* XXX - handle Association_Header and Device_Header here */
1035
1036     if (ftype == FC_FTYPE_LINKCTL) {
1037         /* ACK_1 frames and other LINK_CTL frames echo the last seq bit if the
1038          * packet they're ack'ing did not have it set. So, we'll incorrectly
1039          * flag them as being fragmented when they're not. This fixes the
1040          * problem
1041          */
1042         is_lastframe_inseq = TRUE;
1043     } else {
1044         is_lastframe_inseq = fchdr.fctl & FC_FCTL_SEQ_LAST;
1045         /* XXX is this right?   offset 20, shouldnt it be offset 9? */
1046         is_exchg_resp = ((tvb_get_guint8 (tvb, offset+20) & 0x80) == 0x80);
1047     }
1048
1049     frag_size = tvb_reported_length (tvb)-FC_HEADER_SIZE;
1050
1051     /* If there is an MDS header, we need to subtract the MDS trailer size */
1052     if ((pinfo->ethertype == ETHERTYPE_UNK) || (pinfo->ethertype == ETHERTYPE_FCFT)) {
1053         frag_size -= MDSHDR_TRAILER_SIZE;
1054     } else if (pinfo->ethertype == ETHERTYPE_BRDWALK) {
1055         frag_size -= 8;         /* 4 byte of FC CRC +
1056                                    4 bytes of error+EOF = 8 bytes  */
1057     }
1058
1059     if (!is_lastframe_inseq) {
1060         /* Show this only as a fragmented FC frame */
1061         if (check_col (pinfo->cinfo, COL_INFO)) {
1062             col_append_str (pinfo->cinfo, COL_INFO, " (Fragmented)");
1063         }
1064     }
1065
1066     /* If this is a fragment, attempt to check if fully reassembled frame is
1067      * present, if we're configured to reassemble.
1068      */
1069     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS) &&
1070         (!is_lastframe_inseq || fchdr.seqcnt) && fc_reassemble &&
1071         tvb_bytes_exist(tvb, FC_HEADER_SIZE, frag_size)) {
1072         /* Add this to the list of fragments */
1073         frag_id = (pinfo->oxid << 16) | is_exchg_resp;
1074
1075         /* We assume that all frames are of the same max size */
1076         fcfrag_head = fragment_add (tvb, FC_HEADER_SIZE, pinfo, frag_id,
1077                                     fc_fragment_table,
1078                                     fchdr.seqcnt * fc_max_frame_size,
1079                                     frag_size,
1080                                     !is_lastframe_inseq);
1081         
1082         if (fcfrag_head) {
1083             next_tvb = tvb_new_real_data (fcfrag_head->data,
1084                                           fcfrag_head->datalen,
1085                                           fcfrag_head->datalen);
1086             tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1087             
1088             /* Add the defragmented data to the data source list. */
1089             add_new_data_source(pinfo, next_tvb, "Reassembled FC");
1090             
1091             if (tree) {
1092                 proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1093                                                tvb, offset+9, 1, 1);
1094             }
1095         } else {
1096             if (tree) {
1097                 proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1098                                                tvb, offset+9, 1, 0);
1099             }
1100             next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1101             call_dissector (data_handle, next_tvb, pinfo, tree);
1102             return;
1103         }
1104     } else {
1105         if (tree) {
1106             proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1107                                            tvb, offset+9, 1, 0);
1108         }
1109         next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1110     }
1111
1112     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS)) {
1113         if (!dissector_try_port (fcftype_dissector_table, ftype, next_tvb,
1114                                  pinfo, tree)) {
1115             call_dissector (data_handle, next_tvb, pinfo, tree);
1116         }
1117     } else if (ftype == FC_FTYPE_BLS) {
1118         if ((fchdr.r_ctl & 0x0F) == FC_BLS_BAACC) {
1119             dissect_fc_ba_acc (next_tvb, pinfo, tree);
1120         } else if ((fchdr.r_ctl & 0x0F) == FC_BLS_BARJT) {
1121             dissect_fc_ba_rjt (next_tvb, pinfo, tree);
1122         }
1123     }
1124
1125     tap_queue_packet(fc_tap, pinfo, &fchdr);
1126 }
1127
1128
1129 /* Register the protocol with Ethereal */
1130
1131 /* this format is require because a script is used to build the C function
1132    that calls all the protocol registration.
1133 */
1134
1135 void
1136 proto_register_fc(void)
1137 {                 
1138
1139 /* Setup list of header fields  See Section 1.6.1 for details*/
1140     static hf_register_info hf[] = {
1141         { &hf_fc_rctl,
1142           { "R_CTL", "fc.r_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
1143             "R_CTL", HFILL }},
1144         { &hf_fc_ftype,
1145           {"Frame type", "fc.ftype", FT_UINT8, BASE_HEX, VALS(fc_ftype_vals),
1146            0x0, "Derived Type", HFILL}},
1147         { &hf_fc_did,
1148           { "Dest Addr", "fc.d_id", FT_STRING, BASE_HEX, NULL, 0x0,
1149             "Destination Address", HFILL}},
1150         { &hf_fc_csctl,
1151           {"CS_CTL", "fc.cs_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
1152            "CS_CTL", HFILL}},
1153         { &hf_fc_sid,
1154           {"Src Addr", "fc.s_id", FT_STRING, BASE_HEX, NULL, 0x0,
1155            "Source Address", HFILL}},
1156         { &hf_fc_id,
1157           {"Addr", "fc.id", FT_STRING, BASE_HEX, NULL, 0x0,
1158            "Source or Destination Address", HFILL}},
1159         { &hf_fc_type,
1160           {"Type", "fc.type", FT_UINT8, BASE_HEX, VALS (fc_fc4_val), 0x0,
1161            "", HFILL}},
1162         { &hf_fc_fctl,
1163           {"F_CTL", "fc.f_ctl", FT_UINT24, BASE_HEX, NULL, 0x0, "", HFILL}},
1164         { &hf_fc_seqid,
1165           {"SEQ_ID", "fc.seq_id", FT_UINT8, BASE_HEX, NULL, 0x0,
1166            "Sequence ID", HFILL}},
1167         { &hf_fc_dfctl,
1168           {"DF_CTL", "fc.df_ctl", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}},
1169         { &hf_fc_seqcnt,
1170           {"SEQ_CNT", "fc.seq_cnt", FT_UINT16, BASE_DEC, NULL, 0x0,
1171            "Sequence Count", HFILL}},
1172         { &hf_fc_oxid,
1173           {"OX_ID", "fc.ox_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Originator ID",
1174            HFILL}},
1175         { &hf_fc_rxid,
1176           {"RX_ID", "fc.rx_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Receiver ID",
1177            HFILL}},
1178         { &hf_fc_param,
1179           {"Parameter", "fc.parameter", FT_UINT32, BASE_HEX, NULL, 0x0, "Parameter",
1180            HFILL}},
1181
1182         { &hf_fc_reassembled,
1183           {"Reassembled Frame", "fc.reassembled", FT_BOOLEAN, BASE_HEX, NULL,
1184            0x0, "", HFILL}},
1185         { &hf_fc_nh_da,
1186           {"Network DA", "fc.nethdr.da", FT_STRING, BASE_HEX, NULL,
1187            0x0, "", HFILL}},
1188         { &hf_fc_nh_sa,
1189           {"Network SA", "fc.nethdr.sa", FT_STRING, BASE_HEX, NULL,
1190            0x0, "", HFILL}},
1191
1192         /* Basic Link Svc field definitions */
1193         { &hf_fc_bls_seqid_vld,
1194           {"SEQID Valid", "fc.bls_seqidvld", FT_UINT8, BASE_HEX,
1195            VALS (fc_bls_seqid_val), 0x0, "", HFILL}},
1196         { &hf_fc_bls_lastvld_seqid,
1197           {"Last Valid SEQID", "fc.bls_lastseqid", FT_UINT8, BASE_HEX, NULL,
1198            0x0, "", HFILL}},
1199         { &hf_fc_bls_oxid,
1200           {"OXID", "fc.bls_oxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
1201         { &hf_fc_bls_rxid,
1202           {"RXID", "fc.bls_rxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
1203         { &hf_fc_bls_lowseqcnt,
1204           {"Low SEQCNT", "fc.bls_lseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
1205            HFILL}},
1206         { &hf_fc_bls_hiseqcnt,
1207           {"High SEQCNT", "fc.bls_hseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
1208            HFILL}},
1209         { &hf_fc_bls_rjtcode,
1210           {"Reason", "fc.bls_reason", FT_UINT8, BASE_HEX, VALS(fc_bls_barjt_val),
1211            0x0, "", HFILL}},
1212         { &hf_fc_bls_rjtdetail,
1213           {"Reason Explanantion", "fc.bls_rjtdetail", FT_UINT8, BASE_HEX,
1214            VALS (fc_bls_barjt_det_val), 0x0, "", HFILL}},
1215         { &hf_fc_bls_vendor,
1216           {"Vendor Unique Reason", "fc.bls_vnduniq", FT_UINT8, BASE_HEX, NULL,
1217            0x0, "", HFILL}},
1218         { &hf_fc_fctl_exchange_responder,
1219           {"ExgRpd", "fc.fctl.exchange_responder", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_responder),
1220            FC_FCTL_EXCHANGE_RESPONDER, "Exchange Responder?", HFILL}},
1221         { &hf_fc_fctl_seq_recipient,
1222           {"SeqRec", "fc.fctl.seq_recipient", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_seq_recipient),
1223            FC_FCTL_SEQ_RECIPIENT, "Seq Recipient?", HFILL}},
1224         { &hf_fc_fctl_exchange_first,
1225           {"ExgFst", "fc.fctl.exchange_first", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_first),
1226            FC_FCTL_EXCHANGE_FIRST, "First Exchange?", HFILL}},
1227         { &hf_fc_fctl_exchange_last,
1228           {"ExgLst", "fc.fctl.exchange_last", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_last),
1229            FC_FCTL_EXCHANGE_LAST, "Last Exchange?", HFILL}},
1230         { &hf_fc_fctl_seq_last,
1231           {"SeqLst", "fc.fctl.seq_last", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_seq_last),
1232            FC_FCTL_SEQ_LAST, "Last Sequence?", HFILL}},
1233         { &hf_fc_fctl_priority,
1234           {"Pri", "fc.fctl.priority", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_priority),
1235            FC_FCTL_PRIORITY, "Priority", HFILL}},
1236         { &hf_fc_fctl_transfer_seq_initiative,
1237           {"TSI", "fc.fctl.transfer_seq_initiative", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_transfer_seq_initiative),
1238            FC_FCTL_TRANSFER_SEQ_INITIATIVE, "Transfer Seq Initiative", HFILL}},
1239         { &hf_fc_fctl_rexmitted_seq,
1240           {"RetSeq", "fc.fctl.rexmitted_seq", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_rexmitted_seq),
1241            FC_FCTL_REXMITTED_SEQ, "Retransmitted Sequence", HFILL}},
1242         { &hf_fc_fctl_rel_offset,
1243           {"RelOff", "fc.fctl.rel_offset", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_rel_offset),
1244            FC_FCTL_REL_OFFSET, "rel offset", HFILL}},
1245         { &hf_fc_fctl_last_data_frame,
1246           {"LDF", "fc.fctl.last_data_frame", FT_UINT24, BASE_HEX, VALS(last_data_frame_vals),
1247            FC_FCTL_LAST_DATA_FRAME_MASK, "Last Data Frame?", HFILL}},
1248         { &hf_fc_fctl_ack_0_1,
1249           {"A01", "fc.fctl.ack_0_1", FT_UINT24, BASE_HEX, VALS(ack_0_1_vals),
1250            FC_FCTL_ACK_0_1_MASK, "Ack 0/1 value", HFILL}},
1251         { &hf_fc_fctl_abts_ack,
1252           {"AA", "fc.fctl.abts_ack", FT_UINT24, BASE_HEX, VALS(abts_ack_vals),
1253            FC_FCTL_ABTS_MASK, "ABTS ACK values", HFILL}},
1254         { &hf_fc_fctl_abts_not_ack,
1255           {"AnA", "fc.fctl.abts_not_ack", FT_UINT24, BASE_HEX, VALS(abts_not_ack_vals),
1256            FC_FCTL_ABTS_MASK, "ABTS not ACK vals", HFILL}},
1257         { &hf_fc_exchange_first_frame,
1258           { "Exchange First In", "fc.exchange_first_frame", FT_FRAMENUM, BASE_NONE, NULL,
1259            0, "The first frame of this exchange is in this frame", HFILL }},
1260         { &hf_fc_exchange_last_frame,
1261           { "Exchange Last In", "fc.exchange_last_frame", FT_FRAMENUM, BASE_NONE, NULL,
1262            0, "The last frame of this exchange is in this frame", HFILL }},
1263         { &hf_fc_time,
1264           { "Time from Exchange First", "fc.time", FT_RELATIVE_TIME, BASE_NONE, NULL,
1265            0, "Time since the first frame of the Exchange", HFILL }},
1266     };
1267
1268     /* Setup protocol subtree array */
1269     static gint *ett[] = {
1270         &ett_fc,
1271         &ett_fcbls,
1272         &ett_fctl
1273     };
1274
1275     module_t *fc_module;
1276
1277     /* Register the protocol name and description */
1278     proto_fc = proto_register_protocol ("Fibre Channel", "FC", "fc");
1279     register_dissector ("fc", dissect_fc, proto_fc);
1280     fc_tap = register_tap("fc");
1281
1282     /* Required function calls to register the header fields and subtrees used */
1283     proto_register_field_array(proto_fc, hf, array_length(hf));
1284     proto_register_subtree_array(ett, array_length(ett));
1285
1286     fcftype_dissector_table = register_dissector_table ("fc.ftype",
1287                                                         "FC Frame Type",
1288                                                         FT_UINT8, BASE_HEX);
1289
1290     /* Register preferences */
1291     fc_module = prefs_register_protocol (proto_fc, NULL);
1292     prefs_register_bool_preference (fc_module,
1293                                     "reassemble",
1294                                     "Reassemble multi-frame sequences",
1295                                     "If enabled, reassembly of multi-frame "
1296                                     "sequences is done",
1297                                     &fc_reassemble);
1298     prefs_register_uint_preference (fc_module,
1299                                     "max_frame_size", "Max FC Frame Size",
1300                                     "This is the size of non-last frames in a "
1301                                     "multi-frame sequence", 10,
1302                                     &fc_max_frame_size);
1303     
1304     register_init_routine(fc_defragment_init);
1305     register_init_routine (fc_exchange_init_protocol);
1306 }
1307
1308
1309 /* If this dissector uses sub-dissector registration add a registration routine.
1310    This format is required because a script is used to find these routines and
1311    create the code that calls these routines.
1312 */
1313 void
1314 proto_reg_handoff_fc (void)
1315 {
1316     dissector_handle_t fc_handle;
1317
1318     fc_handle = create_dissector_handle (dissect_fc, proto_fc);
1319
1320     data_handle = find_dissector("data");
1321 }