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