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