Panic if a preference starts with the name of the module to which it
[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  *
5  * $Id: packet-fc.c,v 1.3 2003/01/28 23:56:38 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <glib.h>
43
44 #ifdef NEED_SNPRINTF_H
45 # include "snprintf.h"
46 #endif
47
48 #include <epan/packet.h>
49 #include "prefs.h"
50 #include "reassemble.h"
51 #include "etypes.h"
52 #include "packet-fc.h"
53 #include "packet-fclctl.h"
54 #include "packet-fcbls.h"
55
56 #define FC_HEADER_SIZE         24
57 #define FC_RCTL_EISL           0x50
58
59 /* Size of various fields in FC header in bytes */
60 #define FC_RCTL_SIZE           1
61 #define FC_DID_SIZE            3
62 #define FC_CSCTL_SIZE          1
63 #define FC_SID_SIZE            3
64 #define FC_TYPE_SIZE           1
65 #define FC_FCTL_SIZE           3
66 #define FC_SEQID_SIZE          1
67 #define FC_DFCTL_SIZE          1
68 #define FC_SEQCNT_SIZE         2
69 #define FC_OXID_SIZE           2
70 #define FC_RXID_SIZE           2
71 #define FC_PARAM_SIZE          4
72
73 /* Initialize the protocol and registered fields */
74 static int proto_fc = -1;
75 static int hf_fc_rctl = -1;
76 static int hf_fc_did = -1;
77 static int hf_fc_csctl = -1;
78 static int hf_fc_sid = -1;
79 static int hf_fc_type = -1;
80 static int hf_fc_fctl = -1;
81 static int hf_fc_seqid = -1;
82 static int hf_fc_dfctl = -1;
83 static int hf_fc_seqcnt = -1;
84 static int hf_fc_oxid = -1;
85 static int hf_fc_rxid = -1;
86 static int hf_fc_param = -1;
87 static int hf_fc_ftype = -1;    /* Derived field, non-existent in FC hdr */
88 static int hf_fc_exchg_orig = -1;
89 static int hf_fc_exchg_resp = -1;
90 static int hf_fc_reassembled = -1;
91
92 /* Network_Header fields */
93 static int hf_fc_nh_da = -1;
94 static int hf_fc_nh_sa = -1;
95
96 /* For Basic Link Svc */
97 static int hf_fc_bls_seqid_vld = -1;
98 static int hf_fc_bls_lastvld_seqid = -1;
99 static int hf_fc_bls_oxid = -1;
100 static int hf_fc_bls_rxid = -1;
101 static int hf_fc_bls_lowseqcnt = -1;
102 static int hf_fc_bls_hiseqcnt = -1;
103 static int hf_fc_bls_rjtcode = -1;
104 static int hf_fc_bls_rjtdetail = -1;
105 static int hf_fc_bls_vendor = -1;
106
107
108 /* Initialize the subtree pointers */
109 static gint ett_fc = -1;
110 static gint ett_fcbls = -1;
111
112 static dissector_table_t fcftype_dissector_table;
113 static dissector_handle_t data_handle;
114
115 /* Reassembly stuff */
116 static gboolean fc_reassemble = TRUE;
117 static guint32  fc_max_frame_size = 1024;
118 static GHashTable *fc_fragment_table = NULL;
119
120 const value_string fc_fc4_val[] = {
121     {FC_TYPE_ELS     , "Ext Link Svc"},
122     {FC_TYPE_LLCSNAP , "LLC_SNAP"},
123     {FC_TYPE_IP      , "IP/FC"},
124     {FC_TYPE_SCSI    , "FCP"},
125     {FC_TYPE_FCCT    , "FC_CT"},
126     {FC_TYPE_SWILS   , "SW_ILS"},
127     {FC_TYPE_AL      , "AL"},
128     {FC_TYPE_SNMP    , "SNMP"},
129     {0, NULL},
130 };
131
132 static const value_string fc_ftype_vals [] = {
133     {FC_FTYPE_UNDEF ,    "Unknown frame"},
134     {FC_FTYPE_SWILS,     "SW_ILS"},
135     {FC_FTYPE_IP ,       "IP/FC"},
136     {FC_FTYPE_SCSI ,     "FCP"},
137     {FC_FTYPE_BLS ,      "Basic Link Svc"},
138     {FC_FTYPE_ELS ,      "ELS"},
139     {FC_FTYPE_FCCT ,     "FC_CT"},
140     {FC_FTYPE_LINKDATA,  "Link Data"},
141     {FC_FTYPE_VDO,       "Video Data"},
142     {FC_FTYPE_LINKCTL,   "Link Ctl"},
143     {0, NULL},
144 };
145
146 static const value_string fc_wka_vals[] = {
147     {FC_WKA_MULTICAST,    "Multicast Server"},
148     {FC_WKA_CLKSYNC,      "Clock Sync Server"},
149     {FC_WKA_KEYDIST,      "Key Distribution Server"},
150     {FC_WKA_ALIAS,        "Alias Server"},
151     {FC_WKA_QOSF,         "QoS Facilitator"},
152     {FC_WKA_MGMT,         "Management Server"},
153     {FC_WKA_TIME,         "Time Server"},
154     {FC_WKA_DNS,          "Directory Server"},
155     {FC_WKA_FABRIC_CTRLR, "Fabric Ctlr"},
156     {FC_WKA_FPORT,        "F_Port Server"},
157     {FC_WKA_BCAST,        "Broadcast ID"},
158     {0, NULL},
159 };
160
161 static const value_string fc_iu_val[] = {
162     {FC_IU_UNCATEGORIZED   , "Uncategorized Data"},
163     {FC_IU_SOLICITED_DATA  , "Solicited Data"},
164     {FC_IU_UNSOLICITED_CTL , "Unsolicited Control"},
165     {FC_IU_SOLICITED_CTL   , "Solicited Control"},
166     {FC_IU_UNSOLICITED_DATA, "Solicited Data"},
167     {FC_IU_DATA_DESCRIPTOR , "Data Descriptor"},
168     {FC_IU_UNSOLICITED_CMD , "Unsolicited Command"},
169     {FC_IU_CMD_STATUS      , "Command Status"},
170     {0, NULL},
171 };
172
173 static void fc_defragment_init(void)
174 {
175   fragment_table_init(&fc_fragment_table);
176 }
177
178
179 static gchar *
180 fctl_to_str (const guint8 *fctl, gchar *str, gboolean is_ack)
181 {
182     int stroff = 0;
183     guint8 tmp = 0;
184
185     if (str == NULL)
186         return (str);
187     
188     if (fctl[0] & 0x80) {
189         strcpy (str, "Exchange Responder, ");
190         stroff += 20;
191     }
192     else {
193         strcpy (str, "Exchange Originator, ");
194         stroff += 21;
195     }
196
197     if (fctl[0] & 0x40) {
198         strcpy (&str[stroff], "Seq Recipient, ");
199         stroff += 15;
200     }
201     else {
202         strcpy (&str[stroff], "Seq Initiator, ");
203         stroff += 15;
204     }
205
206     if (fctl[0] & 0x20) {
207         strcpy (&str[stroff], "Exchg First, ");
208         stroff += 13;
209     }
210
211     if (fctl[0] & 0x10) {
212         strcpy (&str[stroff], "Exchg Last, ");
213         stroff += 12;
214     }
215
216     if (fctl[0] & 0x8) {
217         strcpy (&str[stroff], "Seq Last, ");
218         stroff += 10;
219     }
220
221     if (fctl[0] & 0x2) {
222         strcpy (&str[stroff], "Priority, ");
223         stroff += 10;
224     }
225     else {
226         strcpy (&str[stroff], "CS_CTL, ");
227         stroff += 8;
228     }
229
230     if (fctl[0] & 0x1) {
231         strcpy (&str[stroff], "Transfer Seq Initiative, ");
232         stroff += 25;
233     }
234
235     if (fctl[1] & 0x30) {
236         strcpy (&str[stroff], "ACK_0 Reqd, ");
237         stroff += 12;
238     }
239     else if (fctl[1] & 0x10) {
240         strcpy (&str[stroff], "ACK_1 Reqd, ");
241         stroff += 12;
242     }
243
244     if (fctl[1] & 0x2) {
245         strcpy (&str[stroff], "Rexmitted Seq, ");
246         stroff += 15;
247     }
248
249     tmp = fctl[2] & 0xC0;
250     switch (tmp) {
251     case 0:
252         strcpy (&str[stroff], "Last Data Frame - No Info, ");
253         stroff += 27;
254         break;
255     case 1:
256         strcpy (&str[stroff], "Last Data Frame - Seq Imm, ");
257         stroff += 27;
258         break;
259     case 2:
260         strcpy (&str[stroff], "Last Data Frame - Seq Soon, ");
261         stroff += 28;
262         break;
263     case 3:
264         strcpy (&str[stroff], "Last Data Frame - Seq Delyd, ");
265         stroff += 29;
266         break;
267     }
268
269     tmp = fctl[2] & 0x30;
270     switch (tmp) {
271     case 0:
272         if (is_ack) {
273             strcpy (&str[stroff], "ABTS - Cont, ");
274             stroff += 13;
275         }
276         else {
277             strcpy (&str[stroff], "ABTS - Abort/MS, ");
278             stroff += 17;
279         }
280         break;
281     case 0x10:
282         if (is_ack) {
283             strcpy (&str[stroff], "ABTS - Abort, ");
284             stroff += 14;
285         }
286         else {
287             strcpy (&str[stroff], "ABTS - Abort/SS, ");
288             stroff += 17;
289         }
290         break;
291     case 0x20:
292         if (is_ack) {
293             strcpy (&str[stroff], "ABTS - Stop, ");
294             stroff += 13;
295         }
296         else {
297             strcpy (&str[stroff], "ABTS - Process/IB, ");
298             stroff += 19;
299         }
300         break;
301     case 0x30:
302         if (is_ack) {
303             strcpy (&str[stroff], "ABTS - Imm Seq Retx, ");
304             stroff += 21;
305         }
306         else {
307             strcpy (&str[stroff], "ABTS - Discard/MS/Imm Retx, ");
308             stroff += 28;
309         }
310         break;
311     }
312
313     if (fctl[2] & 0x8) {
314         strcpy (&str[stroff], "Rel Offset = 1");
315         stroff += 14;
316     }
317
318     return (str);
319 }
320
321 /* BA_ACC & BA_RJT are decoded in this file itself instead of a traditional
322  * dedicated file and dissector format because the dissector would require some
323  * fields of the FC_HDR such as param in some cases, type in some others, the
324  * lower 4 bits of r_ctl in some other cases etc. So, we decode BLS & Link Ctl
325  * in this file itself.
326  */
327 static void
328 dissect_fc_ba_acc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
329 {
330     /* Set up structures needed to add the protocol subtree and manage it */
331     proto_item *ti;
332     proto_tree *acc_tree;
333     int offset = 0;
334
335     /* Make entries in Protocol column and Info column on summary display */
336     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
337         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
338
339     if (check_col(pinfo->cinfo, COL_INFO)) 
340         col_set_str(pinfo->cinfo, COL_INFO, "BA_ACC");
341
342     if (tree) {
343         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
344         acc_tree = proto_item_add_subtree (ti, ett_fcbls);
345
346         proto_tree_add_item (acc_tree, hf_fc_bls_seqid_vld, tvb, offset++, 1, FALSE);
347         proto_tree_add_item (acc_tree, hf_fc_bls_lastvld_seqid, tvb, offset++, 1, FALSE);
348         offset += 2; /* Skip reserved field */
349         proto_tree_add_item (acc_tree, hf_fc_bls_oxid, tvb, offset, 2, FALSE);
350         offset += 2;
351         proto_tree_add_item (acc_tree, hf_fc_bls_rxid, tvb, offset, 2, FALSE);
352         offset += 2;
353         proto_tree_add_item (acc_tree, hf_fc_bls_lowseqcnt, tvb, offset, 2, FALSE);
354         offset += 2;
355         proto_tree_add_item (acc_tree, hf_fc_bls_hiseqcnt, tvb, offset, 2, FALSE);
356     }
357 }
358
359 static void
360 dissect_fc_ba_rjt (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
361 {
362     /* Set up structures needed to add the protocol subtree and manage it */
363     proto_item *ti;
364     proto_tree *rjt_tree;
365     int offset = 0;
366
367     /* Make entries in Protocol column and Info column on summary display */
368     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
369         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
370
371     if (check_col(pinfo->cinfo, COL_INFO)) 
372         col_set_str(pinfo->cinfo, COL_INFO, "BA_RJT");
373
374     if (tree) {
375         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
376         rjt_tree = proto_item_add_subtree (ti, ett_fcbls);
377
378         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtcode, tvb, offset+1, 1, FALSE);
379         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtdetail, tvb, offset+2, 1, FALSE);
380         proto_tree_add_item (rjt_tree, hf_fc_bls_vendor, tvb, offset+3, 1, FALSE);
381     }
382 }
383
384 static guint8
385 fc_get_ftype (guint8 r_ctl, guint8 type)
386 {
387     /* A simple attempt to determine the upper level protocol based on the
388      * r_ctl & type fields.
389      */
390     switch (r_ctl & 0xF0) {
391     case FC_RCTL_DEV_DATA:
392         switch (type) {
393         case FC_TYPE_SWILS:
394             if ((r_ctl == 0x2) || (r_ctl == 0x3))
395                 return FC_FTYPE_SWILS;
396             else
397                 return FC_FTYPE_UNDEF;
398         case FC_TYPE_IP:
399             return FC_FTYPE_IP;
400         case FC_TYPE_SCSI:
401             return FC_FTYPE_SCSI;
402         case FC_TYPE_FCCT:
403             return FC_FTYPE_FCCT;
404         default:
405             return FC_FTYPE_UNDEF;
406         }
407         break;
408     case FC_RCTL_ELS:
409         if (((r_ctl & 0x0F) == 0x2) || ((r_ctl & 0x0F) == 0x3))
410             return FC_FTYPE_ELS;
411         else
412             return FC_FTYPE_UNDEF;
413         break;
414     case FC_RCTL_LINK_DATA:
415         return FC_FTYPE_LINKDATA;
416         break;
417     case FC_RCTL_VIDEO:
418         return FC_FTYPE_VDO;
419         break;
420     case FC_RCTL_BLS:
421         if (type == 0)
422             return FC_FTYPE_BLS;
423         else
424             return FC_FTYPE_UNDEF;
425         break;
426     case FC_RCTL_LINK_CTL:
427         return FC_FTYPE_LINKCTL;
428         break;
429     default:
430         return FC_FTYPE_UNDEF;
431         break;
432     }
433 }
434 /* Code to actually dissect the packets */
435 static void
436 dissect_fc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
437 {
438    /* Set up structures needed to add the protocol subtree and manage it */
439     proto_item *ti;
440     proto_tree *fc_tree = NULL;
441     tvbuff_t *next_tvb;
442     int offset = 0, next_offset;
443     gboolean is_lastframe_inseq;
444     gboolean is_exchg_resp = 0;
445     fragment_data *fcfrag_head;
446     guint32 frag_id;
447     guint32 frag_size;
448     guint8 r_ctl, type, df_ctl;
449     
450     gchar str[256];
451     guint32 param;
452     guint16 seqcnt;
453     guint8 ftype;
454     gboolean is_ack;
455     
456     /* Make entries in Protocol column and Info column on summary display */
457     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
458         col_set_str(pinfo->cinfo, COL_PROTOCOL, "FC");
459
460     r_ctl = tvb_get_guint8 (tvb, offset);
461
462     /* If the R_CTL is the EISL field, skip the first 8 bytes to retrieve the
463      * real FC header. EISL is Cisco-proprietary and is not decoded.
464      */
465     if (r_ctl == FC_RCTL_EISL) {
466         offset += 8;
467         r_ctl = tvb_get_guint8 (tvb, offset);
468     }
469     
470     type  = tvb_get_guint8 (tvb, offset+8);
471     seqcnt = tvb_get_ntohs (tvb, offset+14);
472     param = tvb_get_ntohl (tvb, offset+20);
473
474     SET_ADDRESS (&pinfo->dst, AT_FC, 3, tvb_get_ptr (tvb, 1, 3));
475     SET_ADDRESS (&pinfo->src, AT_FC, 3, tvb_get_ptr (tvb, 5, 3));
476     pinfo->oxid = tvb_get_ntohs (tvb, offset+16);
477     pinfo->rxid = tvb_get_ntohs (tvb, offset+18);
478     pinfo->ptype = PT_EXCHG;
479     pinfo->r_ctl = r_ctl;
480
481     is_ack = ((r_ctl == 0xC0) || (r_ctl == 0xC1));
482
483     ftype = fc_get_ftype (r_ctl, type);
484     
485     if (check_col (pinfo->cinfo, COL_INFO)) {
486         col_add_str (pinfo->cinfo, COL_INFO, match_strval (ftype, fc_ftype_vals));
487
488         if (ftype == FC_FTYPE_LINKCTL)
489             col_append_fstr (pinfo->cinfo, COL_INFO, ", %s",
490                              match_strval ((r_ctl & 0x0F),
491                                            fc_lctl_proto_val));
492     }
493     
494     /* In the interest of speed, if "tree" is NULL, don't do any work not
495        necessary to generate protocol tree items. */
496     if (tree) {
497         ti = proto_tree_add_protocol_format (tree, proto_fc, tvb, offset,
498                                              FC_HEADER_SIZE, "Fibre Channel");
499         fc_tree = proto_item_add_subtree (ti, ett_fc);
500
501         if (ftype == FC_FTYPE_LINKCTL) {
502             /* the lower 4 bits of R_CTL indicate the type of link ctl frame */
503             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
504                                         FC_RCTL_SIZE, r_ctl,
505                                         "R_CTL: 0x%x(%s)",
506                                         r_ctl,
507                                         val_to_str ((r_ctl & 0x0F),
508                                                     fc_lctl_proto_val, "0x%x")); 
509         }
510         else if (ftype == FC_FTYPE_BLS) {
511             /* the lower 4 bits of R_CTL indicate the type of BLS frame */
512             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
513                                         FC_RCTL_SIZE, r_ctl,
514                                         "R_CTL: 0x%x(%s)",
515                                         r_ctl,
516                                         val_to_str ((r_ctl & 0x0F),
517                                                     fc_bls_proto_val, "0x%x")); 
518         }
519         else {
520             proto_tree_add_item (fc_tree, hf_fc_rctl, tvb, offset, 1, FALSE);
521         }
522         
523         proto_tree_add_uint_hidden (fc_tree, hf_fc_ftype, tvb, offset, 1,
524                                     ftype); 
525         proto_tree_add_string (fc_tree, hf_fc_did, tvb, offset+1, 3,
526                                fc_to_str ((guint8 *)tvb_get_ptr (tvb,
527                                                                  offset+1, 3)));
528         proto_tree_add_item (fc_tree, hf_fc_csctl, tvb, offset+4, 1, FALSE);
529
530         proto_tree_add_string (fc_tree, hf_fc_sid, tvb, offset+5, 3,
531                                fc_to_str ((guint8 *)tvb_get_ptr (tvb,
532                                                                  offset+5, 3)));
533         
534         if (ftype == FC_FTYPE_LINKCTL) {
535             if (((r_ctl & 0x0F) == FC_LCTL_FBSYB) ||
536                 ((r_ctl & 0x0F) == FC_LCTL_FBSYL)) {
537                 /* for F_BSY frames, the upper 4 bits of the type field specify the
538                  * reason for the BSY.
539                  */
540                 proto_tree_add_uint_format (fc_tree, hf_fc_type, tvb,
541                                             offset+8, FC_TYPE_SIZE,
542                                             type, "Type: 0x%x(%s)", type, 
543                                             fclctl_get_typestr (r_ctl & 0x0F,
544                                                                 type));
545             }
546             else {
547                 proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
548             }
549         }
550         else {
551             proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
552         }
553
554         proto_tree_add_uint_format (fc_tree, hf_fc_fctl, tvb, offset+9,
555                                     3, tvb_get_ntoh24 (tvb, offset+9),
556                                     "F_CTL: 0x%x (%s)",
557                                     tvb_get_ntoh24 (tvb, offset+9),
558                                     fctl_to_str (tvb_get_ptr (tvb, offset+9, 3),
559                                                  str, is_ack));
560
561         /* Bit 23 if set => this frame is from the exchange originator */
562         if (tvb_get_guint8 (tvb, offset+9) & 0x80) {
563             proto_tree_add_boolean_hidden (fc_tree, hf_fc_exchg_orig, tvb,
564                                            offset+9, 1, 0);
565             proto_tree_add_boolean_hidden (fc_tree, hf_fc_exchg_resp, tvb,
566                                            offset+9, 1, 1);
567         }
568         else {
569             proto_tree_add_boolean_hidden (fc_tree, hf_fc_exchg_orig, tvb,
570                                            offset+9, 1, 1);
571             proto_tree_add_boolean_hidden (fc_tree, hf_fc_exchg_resp, tvb,
572                                            offset+9, 1, 0);
573         }
574         
575         proto_tree_add_item (fc_tree, hf_fc_seqid, tvb, offset+12, 1, FALSE);
576     }
577     df_ctl = tvb_get_guint8(tvb, offset+13);
578     if (tree) {
579         proto_tree_add_uint (fc_tree, hf_fc_dfctl, tvb, offset+13, 1, df_ctl);
580         proto_tree_add_item (fc_tree, hf_fc_seqcnt, tvb, offset+14, 2, FALSE);
581         proto_tree_add_item (fc_tree, hf_fc_oxid, tvb, offset+16, 2, FALSE);
582         proto_tree_add_item (fc_tree, hf_fc_rxid, tvb, offset+18, 2, FALSE);
583
584         if (ftype == FC_FTYPE_LINKCTL) {
585             if (((r_ctl & 0x0F) == FC_LCTL_FRJT) ||
586                 ((r_ctl & 0x0F) == FC_LCTL_PRJT) ||
587                 ((r_ctl & 0x0F) == FC_LCTL_PBSY)) {
588                 /* In all these cases of Link Ctl frame, the parameter field
589                  * encodes the detailed error message
590                  */
591                 proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
592                                             offset+20, 4, param,
593                                             "Parameter: 0x%x(%s)", param,
594                                             fclctl_get_paramstr ((r_ctl & 0x0F),
595                                                                  param));
596             }
597             else {
598                 proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
599             }
600         }
601         else if (ftype == FC_FTYPE_BLS) {
602             if ((r_ctl & 0x0F) == FC_BLS_ABTS) {
603                 proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
604                                             offset+20, 4, param, 
605                                             "Parameter: 0x%x(%s)", param,
606                                             ((param & 0x0F) == 1 ? "Abort Sequence" :
607                                              "Abort Exchange"));
608             }
609             else {
610                 proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20,
611                                      4, FALSE);
612             }
613         }
614         else {
615             proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
616         }
617     }
618
619     /* Skip the Frame_Header */
620     next_offset = offset + FC_HEADER_SIZE;
621
622     /* Network_Header present? */
623     if (df_ctl & FC_DFCTL_NH) {
624         /* Yes - dissect it. */
625         if (tree) {
626             proto_tree_add_string (fc_tree, hf_fc_nh_da, tvb, next_offset, 8,
627                                    fcwwn_to_str (tvb_get_ptr (tvb, offset, 8)));
628             proto_tree_add_string (fc_tree, hf_fc_nh_sa, tvb, offset+8, 8,
629                                    fcwwn_to_str (tvb_get_ptr (tvb, offset+8, 8)));
630         }
631         next_offset += 16;
632     }
633
634     /* XXX - handle Association_Header and Device_Header here */
635
636     if (ftype == FC_FTYPE_LINKCTL) {
637         /* ACK_1 frames and other LINK_CTL frames echo the last seq bit if the
638          * packet they're ack'ing did not have it set. So, we'll incorrectly
639          * flag them as being fragmented when they're not. This fixes the
640          * problem
641          */
642         is_lastframe_inseq = TRUE;
643     }
644     else {
645         is_lastframe_inseq = tvb_get_guint8 (tvb, offset+9) & 0x08;
646         is_exchg_resp = ((tvb_get_guint8 (tvb, offset+20) & 0x80) == 0x80);
647     }
648
649     frag_size = tvb_reported_length (tvb)-FC_HEADER_SIZE;
650
651     if (!is_lastframe_inseq) {
652         /* Show this only as a fragmented FC frame */
653         if (check_col (pinfo->cinfo, COL_INFO)) {
654             col_append_str (pinfo->cinfo, COL_INFO, " (Fragmented)");
655         }
656     }
657
658     /* If this is a fragment, attempt to check if fully reassembled frame is
659      * present, if we're configured to reassemble.
660      */
661     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS) &&
662         seqcnt && fc_reassemble) {
663         /* Add this to the list of fragments */
664         frag_id = (pinfo->oxid << 16) | is_exchg_resp;
665
666         /* We assume that all frames are of the same max size */
667         fcfrag_head = fragment_add (tvb, FC_HEADER_SIZE, pinfo, frag_id,
668                                     fc_fragment_table,
669                                     seqcnt * fc_max_frame_size,
670                                     frag_size,
671                                     TRUE);
672         
673         if (fcfrag_head) {
674             next_tvb = tvb_new_real_data (fcfrag_head->data,
675                                           fcfrag_head->datalen,
676                                           fcfrag_head->datalen);
677             tvb_set_child_real_data_tvbuff(tvb, next_tvb);
678             
679             /* Add the defragmented data to the data source list. */
680             add_new_data_source(pinfo, next_tvb, "Reassembled FC");
681             
682             if (tree) {
683                 proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
684                                                tvb, offset+9, 1, 1);
685             }
686         }
687         else {
688             if (tree) {
689                 proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
690                                                tvb, offset+9, 1, 0);
691             }
692             next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
693             call_dissector (data_handle, next_tvb, pinfo, tree);
694             return;
695         }
696     }
697     else {
698         if (tree) {
699             proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
700                                            tvb, offset+9, 1, 0);
701         }
702         next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
703     }
704
705     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS)) {
706         if (!dissector_try_port (fcftype_dissector_table, ftype, next_tvb,
707                                  pinfo, tree)) {
708             call_dissector (data_handle, next_tvb, pinfo, tree);
709         }
710     }
711     else if (ftype == FC_FTYPE_BLS) {
712         if ((r_ctl & 0x0F) == FC_BLS_BAACC) {
713             dissect_fc_ba_acc (next_tvb, pinfo, tree);
714         }
715         else if ((r_ctl & 0x0F) == FC_BLS_BARJT) {
716             dissect_fc_ba_rjt (next_tvb, pinfo, tree);
717         }
718     }
719 }
720
721
722 /* Register the protocol with Ethereal */
723
724 /* this format is require because a script is used to build the C function
725    that calls all the protocol registration.
726 */
727
728 void
729 proto_register_fc(void)
730 {                 
731
732 /* Setup list of header fields  See Section 1.6.1 for details*/
733     static hf_register_info hf[] = {
734         { &hf_fc_rctl,
735           { "R_CTL", "fc.r_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
736             "R_CTL", HFILL }},
737         { &hf_fc_ftype,
738           {"Frame type", "fc.ftype", FT_UINT8, BASE_HEX, VALS(fc_ftype_vals),
739            0x0, "Derived Type", HFILL}},
740         { &hf_fc_did,
741           { "Dest Addr", "fc.d_id", FT_STRING, BASE_HEX, NULL, 0x0,
742             "Destination Address", HFILL}},
743         { &hf_fc_csctl,
744           {"CS_CTL", "fc.cs_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
745            "CS_CTL", HFILL}},
746         { &hf_fc_sid,
747           {"Src Addr", "fc.s_id", FT_STRING, BASE_HEX, NULL, 0x0,
748            "Source Address", HFILL}},
749         { &hf_fc_type,
750           {"Type", "fc.type", FT_UINT8, BASE_HEX, VALS (fc_fc4_val), 0x0,
751            "", HFILL}},
752         { &hf_fc_fctl,
753           {"F_CTL", "fc.f_ctl", FT_UINT24, BASE_HEX, NULL, 0x0, "", HFILL}},
754         { &hf_fc_seqid,
755           {"SEQ_ID", "fc.seq_id", FT_UINT8, BASE_HEX, NULL, 0x0,
756            "Sequence ID", HFILL}},
757         { &hf_fc_dfctl,
758           {"DF_CTL", "fc.df_ctl", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}},
759         { &hf_fc_seqcnt,
760           {"SEQ_CNT", "fc.seq_cnt", FT_UINT16, BASE_DEC, NULL, 0x0,
761            "Sequence Count", HFILL}},
762         { &hf_fc_oxid,
763           {"OX_ID", "fc.ox_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Originator ID",
764            HFILL}},
765         { &hf_fc_rxid,
766           {"RX_ID", "fc.rx_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Receiver ID",
767            HFILL}},
768         { &hf_fc_param,
769           {"Parameter", "fc.parameter", FT_UINT32, BASE_HEX, NULL, 0x0, "Parameter",
770            HFILL}},
771
772         { &hf_fc_exchg_orig,
773           {"Exchange Originator", "fc.xchg_orig", FT_BOOLEAN, BASE_HEX, NULL,
774            0x0, "", HFILL}},
775         { &hf_fc_exchg_resp,
776           {"Exchange Responder", "fc.xchg_resp", FT_BOOLEAN, BASE_HEX, NULL,
777            0x0, "", HFILL}},
778         { &hf_fc_reassembled,
779           {"Reassembled Frame", "fc.reassembled", FT_BOOLEAN, BASE_HEX, NULL,
780            0x0, "", HFILL}},
781         { &hf_fc_nh_da,
782           {"Network DA", "fc.nethdr.da", FT_STRING, BASE_HEX, NULL,
783            0x0, "", HFILL}},
784         { &hf_fc_nh_sa,
785           {"Network SA", "fc.nethdr.sa", FT_STRING, BASE_HEX, NULL,
786            0x0, "", HFILL}},
787
788         /* Basic Link Svc field definitions */
789         { &hf_fc_bls_seqid_vld,
790           {"SEQID Valid", "fc.bls_seqidvld", FT_UINT8, BASE_HEX,
791            VALS (fc_bls_seqid_val), 0x0, "", HFILL}},
792         { &hf_fc_bls_lastvld_seqid,
793           {"Last Valid SEQID", "fc.bls_lastseqid", FT_UINT8, BASE_HEX, NULL,
794            0x0, "", HFILL}},
795         { &hf_fc_bls_oxid,
796           {"OXID", "fc.bls_oxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
797         { &hf_fc_bls_rxid,
798           {"RXID", "fc.bls_rxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
799         { &hf_fc_bls_lowseqcnt,
800           {"Low SEQCNT", "fc.bls_lseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
801            HFILL}},
802         { &hf_fc_bls_hiseqcnt,
803           {"High SEQCNT", "fc.bls_hseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
804            HFILL}},
805         { &hf_fc_bls_rjtcode,
806           {"Reason", "fc.bls_reason", FT_UINT8, BASE_HEX, VALS(fc_bls_barjt_val),
807            0x0, "", HFILL}},
808         { &hf_fc_bls_rjtdetail,
809           {"Reason Explanantion", "fc.bls_rjtdetail", FT_UINT8, BASE_HEX,
810            VALS (fc_bls_barjt_det_val), 0x0, "", HFILL}},
811         { &hf_fc_bls_vendor,
812           {"Vendor Unique Reason", "fc.bls_vnduniq", FT_UINT8, BASE_HEX, NULL,
813            0x0, "", HFILL}},
814     };
815
816     /* Setup protocol subtree array */
817     static gint *ett[] = {
818         &ett_fc,
819         &ett_fcbls,
820     };
821
822     module_t *fc_module;
823
824     /* Register the protocol name and description */
825     proto_fc = proto_register_protocol ("Fibre Channel", "FC", "fc");
826     register_dissector ("fc", dissect_fc, proto_fc);
827
828     /* Required function calls to register the header fields and subtrees used */
829     proto_register_field_array(proto_fc, hf, array_length(hf));
830     proto_register_subtree_array(ett, array_length(ett));
831
832     fcftype_dissector_table = register_dissector_table ("fc.ftype",
833                                                         "FC Frame Type",
834                                                         FT_UINT8, BASE_HEX);
835
836     /* Register preferences */
837     fc_module = prefs_register_protocol (proto_fc, NULL);
838     prefs_register_bool_preference (fc_module,
839                                     "reassemble",
840                                     "Reassemble multi-frame sequences",
841                                     "If enabled, reassembly of multi-frame "
842                                     "sequences is done",
843                                     &fc_reassemble);
844     prefs_register_uint_preference (fc_module,
845                                     "max_frame_size", "Max FC Frame Size",
846                                     "This is the size of non-last frames in a "
847                                     "multi-frame sequence", 10,
848                                     &fc_max_frame_size);
849     
850     register_init_routine(fc_defragment_init);
851 }
852
853
854 /* If this dissector uses sub-dissector registration add a registration routine.
855    This format is required because a script is used to find these routines and
856    create the code that calls these routines.
857 */
858 void
859 proto_reg_handoff_fc (void)
860 {
861     dissector_handle_t fc_handle;
862
863     fc_handle = create_dissector_handle (dissect_fc, proto_fc);
864
865     data_handle = find_dissector("data");
866 }