- Decode some more annex C fields
[obnox/wireshark/wip.git] / epan / dissectors / packet-fc.c
1 /* packet-fc.c
2  * Routines for Fibre Channel Decoding (FC Header, Link Ctl & Basic Link Svc) 
3  * Copyright 2001, Dinesh G Dutt <ddutt@cisco.com>
4  *   Copyright 2003  Ronnie Sahlberg, exchange first/last matching and 
5  *                                    tap listener and misc updates
6  *
7  * $Id$
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40
41 #ifdef HAVE_NETINET_IN_H
42 # include <netinet/in.h>
43 #endif
44
45 #include <glib.h>
46
47 #include <epan/packet.h>
48 #include <epan/prefs.h>
49 #include <epan/reassemble.h>
50 #include <epan/conversation.h>
51 #include <epan/etypes.h>
52 #include "packet-fc.h"
53 #include "packet-fclctl.h"
54 #include "packet-fcbls.h"
55 #include <epan/tap.h>
56 #include <epan/emem.h>
57
58 #define FC_HEADER_SIZE         24
59 #define FC_RCTL_EISL           0x50
60 #define MDSHDR_TRAILER_SIZE    6 
61
62 /* Size of various fields in FC header in bytes */
63 #define FC_RCTL_SIZE           1
64 #define FC_DID_SIZE            3
65 #define FC_CSCTL_SIZE          1
66 #define FC_SID_SIZE            3
67 #define FC_TYPE_SIZE           1
68 #define FC_FCTL_SIZE           3
69 #define FC_SEQID_SIZE          1
70 #define FC_DFCTL_SIZE          1
71 #define FC_SEQCNT_SIZE         2
72 #define FC_OXID_SIZE           2
73 #define FC_RXID_SIZE           2
74 #define FC_PARAM_SIZE          4
75
76 /* Initialize the protocol and registered fields */
77 static int proto_fc = -1;
78 static int hf_fc_time = -1;
79 static int hf_fc_exchange_first_frame = -1;
80 static int hf_fc_exchange_last_frame = -1;
81 static int hf_fc_rctl = -1;
82 static int hf_fc_did = -1;
83 static int hf_fc_csctl = -1;
84 static int hf_fc_sid = -1;
85 static int hf_fc_id = -1;
86 static int hf_fc_type = -1;
87 static int hf_fc_fctl = -1;
88 static int hf_fc_fctl_exchange_responder = -1;
89 static int hf_fc_fctl_seq_recipient = -1;
90 static int hf_fc_fctl_exchange_first = -1;
91 static int hf_fc_fctl_exchange_last = -1;
92 static int hf_fc_fctl_seq_last = -1;
93 static int hf_fc_fctl_priority = -1;
94 static int hf_fc_fctl_transfer_seq_initiative = -1;
95 static int hf_fc_fctl_rexmitted_seq = -1;
96 static int hf_fc_fctl_rel_offset = -1;
97 static int hf_fc_fctl_abts_ack = -1;
98 static int hf_fc_fctl_abts_not_ack = -1;
99 static int hf_fc_fctl_last_data_frame = -1;
100 static int hf_fc_fctl_ack_0_1 = -1;
101 static int hf_fc_seqid = -1;
102 static int hf_fc_dfctl = -1;
103 static int hf_fc_seqcnt = -1;
104 static int hf_fc_oxid = -1;
105 static int hf_fc_rxid = -1;
106 static int hf_fc_param = -1;
107 static int hf_fc_ftype = -1;    /* Derived field, non-existent in FC hdr */
108 static int hf_fc_reassembled = -1;
109 static int hf_fc_eisl = -1;
110
111 /* Network_Header fields */
112 static int hf_fc_nh_da = -1;
113 static int hf_fc_nh_sa = -1;
114
115 /* For Basic Link Svc */
116 static int hf_fc_bls_seqid_vld = -1;
117 static int hf_fc_bls_lastvld_seqid = -1;
118 static int hf_fc_bls_oxid = -1;
119 static int hf_fc_bls_rxid = -1;
120 static int hf_fc_bls_lowseqcnt = -1;
121 static int hf_fc_bls_hiseqcnt = -1;
122 static int hf_fc_bls_rjtcode = -1;
123 static int hf_fc_bls_rjtdetail = -1;
124 static int hf_fc_bls_vendor = -1;
125
126
127 /* Initialize the subtree pointers */
128 static gint ett_fc = -1;
129 static gint ett_fctl = -1;
130 static gint ett_fcbls = -1;
131
132 static dissector_table_t fcftype_dissector_table;
133 static dissector_handle_t data_handle;
134
135 static int fc_tap = -1;
136
137 /* Reassembly stuff */
138 static gboolean fc_reassemble = TRUE;
139 static guint32  fc_max_frame_size = 1024;
140 static GHashTable *fc_fragment_table = NULL;
141
142 typedef struct _fcseq_conv_key {
143     guint32 conv_idx;
144 } fcseq_conv_key_t;
145
146 typedef struct _fcseq_conv_data {
147     guint32 seq_cnt;
148 } fcseq_conv_data_t;
149
150 GHashTable *fcseq_req_hash = NULL;
151
152 static GHashTable *fc_exchange_unmatched = NULL;
153 static GHashTable *fc_exchange_matched = NULL;
154
155 /*
156  * Hash Functions
157  */
158 static gint
159 fcseq_equal(gconstpointer v, gconstpointer w)
160 {
161   const fcseq_conv_key_t *v1 = v;
162   const fcseq_conv_key_t *v2 = w;
163
164   return (v1->conv_idx == v2->conv_idx);
165 }
166
167 static guint
168 fcseq_hash (gconstpointer v)
169 {
170     const fcseq_conv_key_t *key = v;
171     guint val;
172     
173     val = key->conv_idx;
174     
175     return val;
176 }
177
178 static guint
179 fc_exchange_hash_unmatched(gconstpointer v)
180 {
181     const fc_exchange_data *fced=(const fc_exchange_data *)v;
182
183     return fced->oxid;
184 }
185 static gint
186 fc_exchange_equal_unmatched(gconstpointer v1, gconstpointer v2)
187 {
188     const fc_exchange_data *fced1=(const fc_exchange_data *)v1;
189     const fc_exchange_data *fced2=(const fc_exchange_data *)v2;
190
191     /* oxid must match */
192     if(fced1->oxid!=fced2->oxid){
193         return 0;
194     }
195     /* compare s_id, d_id and treat the fc address
196        s_id==00.00.00 as a wildcard matching anything */
197     if( ((fced1->s_id.data[0]!=0)||(fced1->s_id.data[1]!=0)||(fced1->s_id.data[2]!=0)) && CMP_ADDRESS(&fced1->s_id, &fced2->s_id) ){
198         return 0;
199     }
200     if(CMP_ADDRESS(&fced1->d_id, &fced2->d_id)){
201         return 0;
202     }
203
204     return 1;
205 }
206
207 static guint
208 fc_exchange_hash_matched(gconstpointer v)
209 {
210     const fc_exchange_data *fced=(const fc_exchange_data *)v;
211
212     return fced->oxid;
213 }
214 static gint
215 fc_exchange_equal_matched(gconstpointer v1, gconstpointer v2)
216 {
217     const fc_exchange_data *fced1=(const fc_exchange_data *)v1;
218     const fc_exchange_data *fced2=(const fc_exchange_data *)v2;
219     guint32 fef1, fef2, lef1, lef2;
220
221     /* oxid must match */
222     if(fced1->oxid!=fced2->oxid){
223         return 0;
224     }
225     fef1=fced1->first_exchange_frame;
226     fef2=fced2->first_exchange_frame;
227     lef1=fced1->last_exchange_frame;
228     lef2=fced2->last_exchange_frame;
229     if(!fef1)fef1=fef2;
230     if(!fef2)fef2=fef1;
231     if(!lef1)lef1=lef2;
232     if(!lef2)lef2=lef1;
233
234     if(fef1!=fef2){
235         return 0;
236     }
237     if(lef1!=lef2){
238         return 0;
239     }
240
241     return 1;
242 }
243
244 static void
245 fc_exchange_init_protocol(void)
246 {
247     if(fc_exchange_unmatched){
248         g_hash_table_destroy(fc_exchange_unmatched);
249         fc_exchange_unmatched=NULL;
250     }
251     if(fc_exchange_matched){
252         g_hash_table_destroy(fc_exchange_matched);
253         fc_exchange_matched=NULL;
254     }
255
256     fc_exchange_unmatched=g_hash_table_new(fc_exchange_hash_unmatched, fc_exchange_equal_unmatched);
257     fc_exchange_matched=g_hash_table_new(fc_exchange_hash_matched, fc_exchange_equal_matched);
258
259     fragment_table_init(&fc_fragment_table);
260
261     if (fcseq_req_hash)
262         g_hash_table_destroy(fcseq_req_hash);
263     
264     fcseq_req_hash = g_hash_table_new(fcseq_hash, fcseq_equal);
265 }
266
267
268 const value_string fc_fc4_val[] = {
269     {FC_TYPE_ELS,        "Ext Link Svc"},
270     {FC_TYPE_LLCSNAP,    "LLC_SNAP"},
271     {FC_TYPE_IP,         "IP/FC"},
272     {FC_TYPE_SCSI,       "FCP"},
273     {FC_TYPE_FCCT,       "FC_CT"},
274     {FC_TYPE_SWILS,      "SW_ILS"},
275     {FC_TYPE_AL,         "AL"},
276     {FC_TYPE_SNMP,       "SNMP"},
277     {FC_TYPE_SB_FROM_CU, "SB-3(CU->Channel)"},
278     {FC_TYPE_SB_TO_CU,   "SB-3(Channel->CU)"},
279     {0, NULL},
280 };
281
282 static const value_string fc_ftype_vals [] = {
283     {FC_FTYPE_UNDEF ,    "Unknown frame"},
284     {FC_FTYPE_SWILS,     "SW_ILS"},
285     {FC_FTYPE_IP ,       "IP/FC"},
286     {FC_FTYPE_SCSI ,     "FCP"},
287     {FC_FTYPE_BLS ,      "Basic Link Svc"},
288     {FC_FTYPE_ELS ,      "ELS"},
289     {FC_FTYPE_FCCT ,     "FC_CT"},
290     {FC_FTYPE_LINKDATA,  "Link Data"},
291     {FC_FTYPE_VDO,       "Video Data"},
292     {FC_FTYPE_LINKCTL,   "Link Ctl"},
293     {FC_FTYPE_SBCCS,     "SBCCS"},
294     {FC_FTYPE_OHMS,      "OHMS(Cisco MDS)"},
295     {0, NULL},
296 };
297
298 static const value_string fc_wka_vals[] = {
299     {FC_WKA_MULTICAST,    "Multicast Server"},
300     {FC_WKA_CLKSYNC,      "Clock Sync Server"},
301     {FC_WKA_KEYDIST,      "Key Distribution Server"},
302     {FC_WKA_ALIAS,        "Alias Server"},
303     {FC_WKA_QOSF,         "QoS Facilitator"},
304     {FC_WKA_MGMT,         "Management Server"},
305     {FC_WKA_TIME,         "Time Server"},
306     {FC_WKA_DNS,          "Directory Server"},
307     {FC_WKA_FABRIC_CTRLR, "Fabric Ctlr"},
308     {FC_WKA_FPORT,        "F_Port Server"},
309     {FC_WKA_BCAST,        "Broadcast ID"},
310     {0, NULL},
311 };
312
313 static const value_string fc_routing_val[] = {
314     {FC_RCTL_DEV_DATA,  "Device_Data"},
315     {FC_RCTL_ELS,       "Extended Link Services"},
316     {FC_RCTL_LINK_DATA, "FC-4 Link_Data"},
317     {FC_RCTL_VIDEO,     "Video_Data"},
318     {FC_RCTL_BLS,       "Basic Link Services"},
319     {FC_RCTL_LINK_CTL,  "Link_Control Frame"},
320     {0, NULL},
321 };
322
323 static const value_string fc_iu_val[] = {
324     {FC_IU_UNCATEGORIZED   , "Uncategorized Data"},
325     {FC_IU_SOLICITED_DATA  , "Solicited Data"},
326     {FC_IU_UNSOLICITED_CTL , "Unsolicited Control"},
327     {FC_IU_SOLICITED_CTL   , "Solicited Control"},
328     {FC_IU_UNSOLICITED_DATA, "Solicited Data"},
329     {FC_IU_DATA_DESCRIPTOR , "Data Descriptor"},
330     {FC_IU_UNSOLICITED_CMD , "Unsolicited Command"},
331     {FC_IU_CMD_STATUS      , "Command Status"},
332     {0, NULL},
333 };
334
335
336 static void fc_defragment_init(void)
337 {
338   fragment_table_init (&fc_fragment_table);
339 }
340
341 /* BA_ACC & BA_RJT are decoded in this file itself instead of a traditional
342  * dedicated file and dissector format because the dissector would require some
343  * fields of the FC_HDR such as param in some cases, type in some others, the
344  * lower 4 bits of r_ctl in some other cases etc. So, we decode BLS & Link Ctl
345  * in this file itself.
346  */
347 static void
348 dissect_fc_ba_acc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
349 {
350     /* Set up structures needed to add the protocol subtree and manage it */
351     proto_item *ti;
352     proto_tree *acc_tree;
353     int offset = 0;
354
355     /* Make entries in Protocol column and Info column on summary display */
356     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
357         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
358
359     if (check_col(pinfo->cinfo, COL_INFO)) 
360         col_set_str(pinfo->cinfo, COL_INFO, "BA_ACC");
361
362     if (tree) {
363         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
364         acc_tree = proto_item_add_subtree (ti, ett_fcbls);
365
366         proto_tree_add_item (acc_tree, hf_fc_bls_seqid_vld, tvb, offset++, 1, FALSE);
367         proto_tree_add_item (acc_tree, hf_fc_bls_lastvld_seqid, tvb, offset++, 1, FALSE);
368         offset += 2; /* Skip reserved field */
369         proto_tree_add_item (acc_tree, hf_fc_bls_oxid, tvb, offset, 2, FALSE);
370         offset += 2;
371         proto_tree_add_item (acc_tree, hf_fc_bls_rxid, tvb, offset, 2, FALSE);
372         offset += 2;
373         proto_tree_add_item (acc_tree, hf_fc_bls_lowseqcnt, tvb, offset, 2, FALSE);
374         offset += 2;
375         proto_tree_add_item (acc_tree, hf_fc_bls_hiseqcnt, tvb, offset, 2, FALSE);
376     }
377 }
378
379 static void
380 dissect_fc_ba_rjt (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
381 {
382     /* Set up structures needed to add the protocol subtree and manage it */
383     proto_item *ti;
384     proto_tree *rjt_tree;
385     int offset = 0;
386
387     /* Make entries in Protocol column and Info column on summary display */
388     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
389         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BLS");
390
391     if (check_col(pinfo->cinfo, COL_INFO)) 
392         col_set_str(pinfo->cinfo, COL_INFO, "BA_RJT");
393
394     if (tree) {
395         ti = proto_tree_add_text (tree, tvb, 0, tvb_length (tvb), "Basic Link Svc");
396         rjt_tree = proto_item_add_subtree (ti, ett_fcbls);
397
398         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtcode, tvb, offset+1, 1, FALSE);
399         proto_tree_add_item (rjt_tree, hf_fc_bls_rjtdetail, tvb, offset+2, 1, FALSE);
400         proto_tree_add_item (rjt_tree, hf_fc_bls_vendor, tvb, offset+3, 1, FALSE);
401     }
402 }
403
404 static guint8
405 fc_get_ftype (guint8 r_ctl, guint8 type)
406 {
407     /* A simple attempt to determine the upper level protocol based on the
408      * r_ctl & type fields.
409      */
410     switch (r_ctl & 0xF0) {
411     case FC_RCTL_DEV_DATA:
412         switch (type) {
413         case FC_TYPE_SWILS:
414             if ((r_ctl == 0x2) || (r_ctl == 0x3))
415                 return FC_FTYPE_SWILS;
416             else
417                 return FC_FTYPE_UNDEF;
418         case FC_TYPE_IP:
419             return FC_FTYPE_IP;
420         case FC_TYPE_SCSI:
421             return FC_FTYPE_SCSI;
422         case FC_TYPE_FCCT:
423             return FC_FTYPE_FCCT;
424         case FC_TYPE_SB_FROM_CU:
425         case FC_TYPE_SB_TO_CU:
426             return FC_FTYPE_SBCCS;
427         case FC_TYPE_VENDOR:
428              return FC_FTYPE_OHMS;
429         default:
430             return FC_FTYPE_UNDEF;
431         }
432     case FC_RCTL_ELS:
433         if (((r_ctl & 0x0F) == 0x2) || ((r_ctl & 0x0F) == 0x3))
434             return FC_FTYPE_ELS;
435         else if (type == FC_TYPE_ELS) 
436             return FC_FTYPE_OHMS;
437         else
438              return FC_FTYPE_UNDEF;
439     case FC_RCTL_LINK_DATA:
440         return FC_FTYPE_LINKDATA;
441     case FC_RCTL_VIDEO:
442         return FC_FTYPE_VDO;
443     case FC_RCTL_BLS:
444         if (type == 0)
445             return FC_FTYPE_BLS;
446         else
447             return FC_FTYPE_UNDEF;
448     case FC_RCTL_LINK_CTL:
449         return FC_FTYPE_LINKCTL;
450     default:
451         return FC_FTYPE_UNDEF;
452     }
453 }
454
455 static const value_string abts_ack_vals[] = {
456         {0x000000,      "ABTS - Cont"},
457         {0x000010,      "ABTS - Abort"},
458         {0x000020,      "ABTS - Stop"},
459         {0x000030,      "ABTS - Imm Seq Retx"},
460         {0,NULL}
461 };
462 static const value_string abts_not_ack_vals[] = {
463         {0x000000,      "ABTS - Abort/MS"},
464         {0x000010,      "ABTS - Abort/SS"},
465         {0x000020,      "ABTS - Process/IB"},
466         {0x000030,      "ABTS - Discard/MS/Imm Retx"},
467         {0,NULL}
468 };
469 static const value_string last_data_frame_vals[] = {
470         {0x000000,      "Last Data Frame - No Info"},
471         {0x004000,      "Last Data Frame - Seq Imm"},
472         {0x008000,      "Last Data Frame - Seq Soon"},
473         {0x00c000,      "Last Data Frame - Seq Delyd"},
474         {0,NULL}
475 };
476 static const value_string ack_0_1_vals[] = {
477         {0x003000,      "ACK_0 Required"},
478         {0x002000,      "ACK_0 Required"},
479         {0x001000,      "ACK_1 Required"},
480         {0x000000,      "no ack required"},
481         {0,NULL}
482 };
483 static const true_false_string tfs_fc_fctl_exchange_responder = {
484         "Exchange Responder",
485         "Exchange Originator"
486 };
487 static const true_false_string tfs_fc_fctl_seq_recipient = {
488         "Seq Recipient",
489         "Seq Initiator"
490 };
491 static const true_false_string tfs_fc_fctl_exchange_first = {
492         "Exchg First",
493         "NOT exchg first"
494 };
495 static const true_false_string tfs_fc_fctl_exchange_last = {
496         "Exchg Last",
497         "NOT exchg last"
498 };
499 static const true_false_string tfs_fc_fctl_seq_last = {
500         "Seq Last",
501         "NOT seq last"
502 };
503 static const true_false_string tfs_fc_fctl_priority = {
504         "Priority",
505         "CS_CTL"
506 };
507 static const true_false_string tfs_fc_fctl_transfer_seq_initiative = {
508         "Transfer Seq Initiative",
509         "NOT transfer seq initiative"
510 };
511 static const true_false_string tfs_fc_fctl_rexmitted_seq = {
512         "Retransmitted Sequence",
513         "NOT retransmitted sequence"
514 };
515 static const true_false_string tfs_fc_fctl_rel_offset = {
516         "Rel Offset SET",
517         "rel offset NOT set"
518 };
519
520
521
522
523 /* code to dissect the  F_CTL bitmask */
524 static void
525 dissect_fc_fctl(packet_info *pinfo _U_, proto_tree *parent_tree, tvbuff_t *tvb, int offset)
526 {
527         proto_item *item=NULL;
528         proto_tree *tree=NULL;
529         guint32 flags;
530
531         flags = tvb_get_guint8 (tvb, offset);
532         flags = (flags<<8) | tvb_get_guint8 (tvb, offset+1);
533         flags = (flags<<8) | tvb_get_guint8 (tvb, offset+2);
534
535         if(parent_tree){
536                 item=proto_tree_add_uint(parent_tree, hf_fc_fctl, tvb, offset, 3, flags);
537                 tree=proto_item_add_subtree(item, ett_fctl);
538         }
539
540         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_responder, tvb, offset, 3, flags);
541         if (flags&FC_FCTL_EXCHANGE_RESPONDER){
542                 proto_item_append_text(item, " Exchange Responder");
543                 if (flags & (~( FC_FCTL_EXCHANGE_RESPONDER )))
544                         proto_item_append_text(item, ",");
545         } else {
546                 proto_item_append_text(item, " Exchange Originator");
547                 if (flags & (~( FC_FCTL_EXCHANGE_RESPONDER )))
548                         proto_item_append_text(item, ",");
549         }
550         flags&=(~( FC_FCTL_EXCHANGE_RESPONDER ));
551
552         proto_tree_add_boolean(tree, hf_fc_fctl_seq_recipient, tvb, offset, 3, flags);
553         if (flags&FC_FCTL_SEQ_RECIPIENT){
554                 proto_item_append_text(item, " Seq Recipient");
555                 if (flags & (~( FC_FCTL_SEQ_RECIPIENT )))
556                         proto_item_append_text(item, ",");
557         } else {
558                 proto_item_append_text(item, " Seq Initiator");
559                 if (flags & (~( FC_FCTL_SEQ_RECIPIENT )))
560                         proto_item_append_text(item, ",");
561         }
562         flags&=(~( FC_FCTL_SEQ_RECIPIENT ));
563
564         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_first, tvb, offset, 3, flags);
565         if (flags&FC_FCTL_EXCHANGE_FIRST){
566                 proto_item_append_text(item, " Exchg First");
567                 if (flags & (~( FC_FCTL_EXCHANGE_FIRST )))
568                         proto_item_append_text(item, ",");
569         }
570         flags&=(~( FC_FCTL_EXCHANGE_FIRST ));
571
572         proto_tree_add_boolean(tree, hf_fc_fctl_exchange_last, tvb, offset, 3, flags);
573         if (flags&FC_FCTL_EXCHANGE_LAST){
574                 proto_item_append_text(item, " Exchg Last");
575                 if (flags & (~( FC_FCTL_EXCHANGE_LAST )))
576                         proto_item_append_text(item, ",");
577         }
578         flags&=(~( FC_FCTL_EXCHANGE_LAST ));
579
580         proto_tree_add_boolean(tree, hf_fc_fctl_seq_last, tvb, offset, 3, flags);
581         if (flags&FC_FCTL_SEQ_LAST){
582                 proto_item_append_text(item, " Seq Last");
583                 if (flags & (~( FC_FCTL_SEQ_LAST )))
584                         proto_item_append_text(item, ",");
585         }
586         flags&=(~( FC_FCTL_SEQ_LAST ));
587
588         proto_tree_add_boolean(tree, hf_fc_fctl_priority, tvb, offset, 3, flags);
589         if (flags&FC_FCTL_PRIORITY){
590                 proto_item_append_text(item, " Priority");
591                 if (flags & (~( FC_FCTL_PRIORITY )))
592                         proto_item_append_text(item, ",");
593         } else {
594                 proto_item_append_text(item, " CS_CTL");
595                 if (flags & (~( FC_FCTL_PRIORITY )))
596                         proto_item_append_text(item, ",");
597         }
598         flags&=(~( FC_FCTL_PRIORITY ));
599
600         proto_tree_add_boolean(tree, hf_fc_fctl_transfer_seq_initiative, tvb, offset, 3, flags);
601         if (flags&FC_FCTL_TRANSFER_SEQ_INITIATIVE){
602                 proto_item_append_text(item, " Transfer Seq Initiative");
603                 if (flags & (~( FC_FCTL_TRANSFER_SEQ_INITIATIVE )))
604                         proto_item_append_text(item, ",");
605         }
606         flags&=(~( FC_FCTL_TRANSFER_SEQ_INITIATIVE ));
607
608         proto_tree_add_uint(tree, hf_fc_fctl_last_data_frame, tvb, offset, 3, flags);
609
610         proto_tree_add_uint(tree, hf_fc_fctl_ack_0_1, tvb, offset, 3, flags);
611
612         proto_tree_add_boolean(tree, hf_fc_fctl_rexmitted_seq, tvb, offset, 3, flags);
613         if (flags&FC_FCTL_REXMITTED_SEQ){
614                 proto_item_append_text(item, " Rexmitted Seq");
615                 if (flags & (~( FC_FCTL_REXMITTED_SEQ )))
616                         proto_item_append_text(item, ",");
617         }
618         flags&=(~( FC_FCTL_REXMITTED_SEQ ));
619
620         proto_tree_add_uint(tree, hf_fc_fctl_abts_ack, tvb, offset, 3, flags);
621
622         proto_tree_add_boolean(tree, hf_fc_fctl_rel_offset, tvb, offset, 3, flags);
623         if (flags&FC_FCTL_REL_OFFSET){
624                 proto_item_append_text(item, " Rel Offset");
625                 if (flags & (~( FC_FCTL_REL_OFFSET )))
626                         proto_item_append_text(item, ",");
627         }
628         flags&=(~( FC_FCTL_REL_OFFSET ));
629
630 }
631
632 static const value_string fc_bls_proto_val[] = {
633     {FC_BLS_NOP    , "NOP"},
634     {FC_BLS_ABTS   , "ABTS"},
635     {FC_BLS_RMC    , "RMC"},
636     {FC_BLS_BAACC  , "BA_ACC"},
637     {FC_BLS_BARJT  , "BA_RJT"},
638     {FC_BLS_PRMT   , "PRMT"},
639     {0, NULL},
640 };
641
642 static const value_string fc_els_proto_val[] = {
643     {0x01    , "Solicited Data"},
644     {0x02    , "Request"},
645     {0x03    , "Reply"},
646     {0, NULL},
647 };
648
649 /* Code to actually dissect the packets */
650 static void
651 dissect_fc_helper (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean is_ifcp)
652 {
653    /* Set up structures needed to add the protocol subtree and manage it */
654     proto_item *ti=NULL;
655     proto_tree *fc_tree = NULL;
656     tvbuff_t *next_tvb;
657     int offset = 0, next_offset, eisl_offset = -1;
658     gboolean is_lastframe_inseq, is_1frame_inseq, is_valid_frame;
659     gboolean is_exchg_resp = 0;
660     fragment_data *fcfrag_head;
661     guint32 frag_id;
662     guint32 frag_size;
663     guint8 df_ctl, seq_id;
664     
665     guint32 param;
666     guint16 real_seqcnt;
667     guint8 ftype;
668     gboolean is_ack;
669
670     static fc_hdr fchdr;
671     fc_exchange_data *fc_ex=NULL;
672
673     conversation_t *conversation;
674     fcseq_conv_data_t *cdata;
675     fcseq_conv_key_t ckey, *req_key;
676
677     fchdr.fced=NULL;
678
679     /* Make entries in Protocol column and Info column on summary display */
680     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
681         col_set_str(pinfo->cinfo, COL_PROTOCOL, "FC");
682
683     fchdr.r_ctl = tvb_get_guint8 (tvb, offset);
684
685     /* If the R_CTL is the EISL field, skip the first 8 bytes to retrieve the
686      * real FC header. EISL is Cisco-proprietary and is not decoded.
687      */
688     if (fchdr.r_ctl == FC_RCTL_EISL) {
689         eisl_offset = offset;
690         offset += 8;
691         fchdr.r_ctl = tvb_get_guint8 (tvb, offset);
692     }
693
694     /* Each fc endpoint pair gets its own TCP session in iFCP but
695      * the src/dst ids are undefined(==semi-random) in the FC header.
696      * This means we can no track conversations for FC over iFCP by using
697      * the FC src/dst addresses.
698      * For iFCP: Do not update the pinfo src/dst struct and let it remain 
699      * being tcpip src/dst so that request/response matching in the FCP layer
700      * will use ip addresses instead and still work.
701      */
702     if(!is_ifcp){    
703         SET_ADDRESS (&pinfo->dst, AT_FC, 3, tvb_get_ptr(tvb,offset+1,3));
704         SET_ADDRESS (&pinfo->src, AT_FC, 3, tvb_get_ptr(tvb,offset+5,3));
705         pinfo->srcport=0;
706         pinfo->destport=0;
707     }
708     SET_ADDRESS (&fchdr.d_id, pinfo->dst.type, pinfo->dst.len, pinfo->dst.data);
709     SET_ADDRESS (&fchdr.s_id, pinfo->src.type, pinfo->src.len, pinfo->src.data);
710
711     fchdr.cs_ctl = tvb_get_guint8 (tvb, offset+4);
712     fchdr.type  = tvb_get_guint8 (tvb, offset+8);
713     fchdr.fctl=tvb_get_ntoh24(tvb,offset+9);
714     fchdr.seqcnt = tvb_get_ntohs (tvb, offset+14);
715     fchdr.oxid=tvb_get_ntohs(tvb,offset+16);
716     fchdr.rxid=tvb_get_ntohs(tvb,offset+18);
717     param = tvb_get_ntohl (tvb, offset+20);
718     seq_id = tvb_get_guint8 (tvb, offset+12);
719
720     pinfo->oxid = fchdr.oxid;
721     pinfo->rxid = fchdr.rxid;
722     pinfo->ptype = PT_EXCHG;
723     pinfo->r_ctl = fchdr.r_ctl;
724
725     is_ack = ((fchdr.r_ctl == 0xC0) || (fchdr.r_ctl == 0xC1));
726
727     /* There are two ways to determine if this is the first frame of a
728      * sequence. Either:
729      * (i) The SOF bits indicate that this is the first frame OR
730      * (ii) This is an SOFf frame and seqcnt is 0.
731      */
732     is_1frame_inseq = (((pinfo->sof_eof & PINFO_SOF_FIRST_FRAME) == PINFO_SOF_FIRST_FRAME) ||
733                        (((pinfo->sof_eof & PINFO_SOF_SOFF) == PINFO_SOF_SOFF) &&
734                         (fchdr.seqcnt == 0)));
735     
736     is_lastframe_inseq = ((pinfo->sof_eof & PINFO_EOF_LAST_FRAME) == PINFO_EOF_LAST_FRAME);
737
738     is_lastframe_inseq |= fchdr.fctl & FC_FCTL_SEQ_LAST;
739     is_valid_frame = ((pinfo->sof_eof & 0x40) == 0x40);
740
741     ftype = fc_get_ftype (fchdr.r_ctl, fchdr.type);
742
743     if (check_col (pinfo->cinfo, COL_INFO)) {
744          col_add_str (pinfo->cinfo, COL_INFO, val_to_str (ftype, fc_ftype_vals,
745                                                           "Unknown Type (0x%x)"));
746
747         if (ftype == FC_FTYPE_LINKCTL)
748             col_append_fstr (pinfo->cinfo, COL_INFO, ", %s",
749                              val_to_str ((fchdr.r_ctl & 0x0F),
750                                           fc_lctl_proto_val,
751                                           "LCTL 0x%x"));
752     }
753     
754     /* In the interest of speed, if "tree" is NULL, don't do any work not
755        necessary to generate protocol tree items. */
756     if (tree) {
757         ti = proto_tree_add_protocol_format (tree, proto_fc, tvb, offset,
758                                              FC_HEADER_SIZE, "Fibre Channel");
759         fc_tree = proto_item_add_subtree (ti, ett_fc);
760     }
761
762     /* Highlight EISL header, if present */
763     if (eisl_offset != -1) {
764         proto_tree_add_item (fc_tree, hf_fc_eisl, tvb, eisl_offset, 8, 0);
765     }
766
767     /* match first exchange with last exchange */
768     if(fchdr.fctl&FC_FCTL_EXCHANGE_FIRST){
769         if(!pinfo->fd->flags.visited){
770             fc_exchange_data fced, *old_fced;
771
772             /* first check if we already have seen this exchange and it
773                is still open/unmatched. 
774             */
775             fced.oxid=fchdr.oxid;
776             SET_ADDRESS(&fced.s_id, fchdr.s_id.type, fchdr.s_id.len, fchdr.s_id.data);
777             SET_ADDRESS(&fced.d_id, fchdr.d_id.type, fchdr.d_id.len, fchdr.d_id.data);
778             old_fced=g_hash_table_lookup(fc_exchange_unmatched, &fced);
779             if(old_fced){
780                 g_hash_table_remove(fc_exchange_unmatched, old_fced);
781             }
782             old_fced=se_alloc(sizeof(fc_exchange_data));
783             old_fced->oxid=fchdr.oxid;
784             COPY_ADDRESS(&old_fced->s_id, &fchdr.s_id);
785             COPY_ADDRESS(&old_fced->d_id, &fchdr.d_id);
786             old_fced->first_exchange_frame=pinfo->fd->num;
787             old_fced->fc_time = pinfo->fd->abs_ts;
788             g_hash_table_insert(fc_exchange_unmatched, old_fced, old_fced);
789             fc_ex=old_fced;
790         } else {
791             fc_exchange_data fced, *old_fced;
792             fced.oxid=fchdr.oxid;
793             fced.first_exchange_frame=pinfo->fd->num;
794             fced.last_exchange_frame=0;
795             old_fced=g_hash_table_lookup(fc_exchange_matched, &fced);
796             fc_ex=old_fced;
797         }
798     }
799     if(fchdr.fctl&FC_FCTL_EXCHANGE_LAST){
800         if(!pinfo->fd->flags.visited){
801             fc_exchange_data fced, *old_fced;
802
803             fced.oxid=fchdr.oxid;
804             SET_ADDRESS(&fced.s_id, fchdr.d_id.type, fchdr.d_id.len, fchdr.d_id.data);
805             SET_ADDRESS(&fced.d_id, fchdr.s_id.type, fchdr.s_id.len, fchdr.s_id.data);
806             old_fced=g_hash_table_lookup(fc_exchange_unmatched, &fced);
807             if(old_fced){
808                 g_hash_table_remove(fc_exchange_unmatched, old_fced);
809                 old_fced->last_exchange_frame=pinfo->fd->num;
810                 g_hash_table_insert(fc_exchange_matched, old_fced, old_fced);
811             }
812             fc_ex=old_fced;
813         } else {
814             fc_exchange_data fced, *old_fced;
815             fced.oxid=fchdr.oxid;
816             fced.first_exchange_frame=0;
817             fced.last_exchange_frame=pinfo->fd->num;
818             old_fced=g_hash_table_lookup(fc_exchange_matched, &fced);
819             fc_ex=old_fced;
820         }
821     }
822     if(fc_ex){
823         if(fchdr.fctl&FC_FCTL_EXCHANGE_FIRST){
824             proto_tree_add_uint(fc_tree, hf_fc_exchange_last_frame, tvb, 0, 0, fc_ex->last_exchange_frame);
825         }
826         if(fchdr.fctl&FC_FCTL_EXCHANGE_LAST){
827             nstime_t delta_ts;
828             proto_tree_add_uint(fc_tree, hf_fc_exchange_first_frame, tvb, 0, 0, fc_ex->first_exchange_frame);
829             nstime_delta(&delta_ts, &pinfo->fd->abs_ts, &fc_ex->fc_time);
830             proto_tree_add_time(ti, hf_fc_time, tvb, 0, 0, &delta_ts);
831         }
832     }
833     fchdr.fced=fc_ex;
834
835     switch (fchdr.r_ctl & 0xF0) {
836
837     case FC_RCTL_DEV_DATA:
838     case FC_RCTL_LINK_DATA:
839     case FC_RCTL_VIDEO:
840         /* the lower 4 bits of R_CTL are the information category */
841         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
842                                     FC_RCTL_SIZE, fchdr.r_ctl,
843                                     "R_CTL: 0x%x(%s/%s)",
844                                     fchdr.r_ctl,
845                                     val_to_str ((fchdr.r_ctl & 0xF0),
846                                                 fc_routing_val, "0x%x"),
847                                     val_to_str ((fchdr.r_ctl & 0x0F),
848                                                 fc_iu_val, "0x%x")); 
849         break;
850
851     case FC_RCTL_LINK_CTL:
852         /* the lower 4 bits of R_CTL indicate the type of link ctl frame */
853         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
854                                     FC_RCTL_SIZE, fchdr.r_ctl,
855                                     "R_CTL: 0x%x(%s/%s)",
856                                     fchdr.r_ctl,
857                                     val_to_str ((fchdr.r_ctl & 0xF0),
858                                                 fc_routing_val, "0x%x"),
859                                     val_to_str ((fchdr.r_ctl & 0x0F),
860                                                 fc_lctl_proto_val, "0x%x")); 
861         break;
862
863     case FC_RCTL_BLS:
864         switch (fchdr.type) {
865
866         case 0x00:
867             /* the lower 4 bits of R_CTL indicate the type of BLS frame */
868             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
869                                         FC_RCTL_SIZE, fchdr.r_ctl,
870                                         "R_CTL: 0x%x(%s/%s)",
871                                         fchdr.r_ctl,
872                                         val_to_str ((fchdr.r_ctl & 0xF0),
873                                                     fc_routing_val, "0x%x"),
874                                         val_to_str ((fchdr.r_ctl & 0x0F),
875                                                     fc_bls_proto_val, "0x%x")); 
876             break;
877
878         default:
879             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
880                                         FC_RCTL_SIZE, fchdr.r_ctl,
881                                         "R_CTL: 0x%x(%s/0x%x)",
882                                         fchdr.r_ctl,
883                                         val_to_str ((fchdr.r_ctl & 0xF0),
884                                                     fc_routing_val, "0x%x"),
885                                         fchdr.r_ctl & 0x0F);
886             break;
887         }
888         break;
889
890     case FC_RCTL_ELS:
891         switch (fchdr.type) {
892
893         case 0x01:
894             /* the lower 4 bits of R_CTL indicate the type of ELS frame */
895             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
896                                         FC_RCTL_SIZE, fchdr.r_ctl,
897                                         "R_CTL: 0x%x(%s/%s)",
898                                         fchdr.r_ctl,
899                                         val_to_str ((fchdr.r_ctl & 0xF0),
900                                                     fc_routing_val, "0x%x"),
901                                         val_to_str ((fchdr.r_ctl & 0x0F),
902                                                     fc_els_proto_val, "0x%x")); 
903             break;
904
905         default:
906             proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
907                                         FC_RCTL_SIZE, fchdr.r_ctl,
908                                         "R_CTL: 0x%x(%s/0x%x)",
909                                         fchdr.r_ctl,
910                                         val_to_str ((fchdr.r_ctl & 0xF0),
911                                                     fc_routing_val, "0x%x"),
912                                         fchdr.r_ctl & 0x0F);
913             break;
914         }
915         break;
916
917     default:
918         proto_tree_add_uint_format (fc_tree, hf_fc_rctl, tvb, offset,
919                                     FC_RCTL_SIZE, fchdr.r_ctl,
920                                     "R_CTL: 0x%x(%s/0x%x)",
921                                     fchdr.r_ctl,
922                                     val_to_str ((fchdr.r_ctl & 0xF0),
923                                                 fc_routing_val, "0x%x"),
924                                     fchdr.r_ctl & 0x0F);
925         break;
926     }
927   
928     proto_tree_add_uint_hidden (fc_tree, hf_fc_ftype, tvb, offset, 1,
929                            ftype); 
930
931     /* XXX - use "fc_wka_vals[]" on this? */
932     proto_tree_add_string (fc_tree, hf_fc_did, tvb, offset+1, 3,
933                            fc_to_str (fchdr.d_id.data));
934     proto_tree_add_string_hidden (fc_tree, hf_fc_id, tvb, offset+1, 3,
935                            fc_to_str (fchdr.d_id.data));
936
937     proto_tree_add_uint (fc_tree, hf_fc_csctl, tvb, offset+4, 1, fchdr.cs_ctl);
938
939     /* XXX - use "fc_wka_vals[]" on this? */
940     proto_tree_add_string (fc_tree, hf_fc_sid, tvb, offset+5, 3,
941                            fc_to_str (fchdr.s_id.data));
942     proto_tree_add_string_hidden (fc_tree, hf_fc_id, tvb, offset+5, 3,
943                            fc_to_str (fchdr.s_id.data));
944         
945     if (ftype == FC_FTYPE_LINKCTL) {
946         if (((fchdr.r_ctl & 0x0F) == FC_LCTL_FBSYB) ||
947             ((fchdr.r_ctl & 0x0F) == FC_LCTL_FBSYL)) {
948             /* for F_BSY frames, the upper 4 bits of the type field specify the
949              * reason for the BSY.
950              */
951             proto_tree_add_uint_format (fc_tree, hf_fc_type, tvb,
952                                         offset+8, FC_TYPE_SIZE,
953                                         fchdr.type,"Type: 0x%x(%s)", fchdr.type, 
954                                         fclctl_get_typestr ((guint8) (fchdr.r_ctl & 0x0F),
955                                                             fchdr.type));
956         } else {
957             proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
958         }
959     } else {
960         proto_tree_add_item (fc_tree, hf_fc_type, tvb, offset+8, 1, FALSE);
961     }
962
963
964     dissect_fc_fctl(pinfo, fc_tree, tvb, offset+9);
965
966
967     proto_tree_add_item (fc_tree, hf_fc_seqid, tvb, offset+12, 1, FALSE);
968
969     df_ctl = tvb_get_guint8(tvb, offset+13);
970
971     proto_tree_add_uint (fc_tree, hf_fc_dfctl, tvb, offset+13, 1, df_ctl);
972     proto_tree_add_uint (fc_tree, hf_fc_seqcnt, tvb, offset+14, 2, fchdr.seqcnt);
973     proto_tree_add_uint (fc_tree, hf_fc_oxid, tvb, offset+16, 2, fchdr.oxid);
974     proto_tree_add_uint (fc_tree, hf_fc_rxid, tvb, offset+18, 2, fchdr.rxid);
975
976     if (ftype == FC_FTYPE_LINKCTL) {
977         if (((fchdr.r_ctl & 0x0F) == FC_LCTL_FRJT) ||
978             ((fchdr.r_ctl & 0x0F) == FC_LCTL_PRJT) ||
979             ((fchdr.r_ctl & 0x0F) == FC_LCTL_PBSY)) {
980             /* In all these cases of Link Ctl frame, the parameter field
981              * encodes the detailed error message
982              */
983             proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
984                                         offset+20, 4, param,
985                                         "Parameter: 0x%x(%s)", param,
986                                         fclctl_get_paramstr ((fchdr.r_ctl & 0x0F),
987                                                              param));
988         } else {
989             proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
990         }
991     } else if (ftype == FC_FTYPE_BLS) {
992         if ((fchdr.r_ctl & 0x0F) == FC_BLS_ABTS) {
993             proto_tree_add_uint_format (fc_tree, hf_fc_param, tvb,
994                                         offset+20, 4, param, 
995                                         "Parameter: 0x%x(%s)", param,
996                                         ((param & 0x0F) == 1 ? "Abort Sequence" :
997                                          "Abort Exchange"));
998         } else {
999             proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20,
1000                                  4, FALSE);
1001         }
1002     } else {
1003         proto_tree_add_item (fc_tree, hf_fc_param, tvb, offset+20, 4, FALSE);
1004     }
1005
1006     /* Skip the Frame_Header */
1007     next_offset = offset + FC_HEADER_SIZE;
1008
1009     /* Network_Header present? */
1010     if (df_ctl & FC_DFCTL_NH) {
1011         /* Yes - dissect it. */
1012         if (tree) {
1013             proto_tree_add_string (fc_tree, hf_fc_nh_da, tvb, next_offset, 8,
1014                                    fcwwn_to_str (tvb_get_ptr (tvb, offset, 8)));
1015             proto_tree_add_string (fc_tree, hf_fc_nh_sa, tvb, offset+8, 8,
1016                                    fcwwn_to_str (tvb_get_ptr (tvb, offset+8, 8)));
1017         }
1018         next_offset += 16;
1019     }
1020
1021     /* XXX - handle Association_Header and Device_Header here */
1022
1023     if (ftype == FC_FTYPE_LINKCTL) {
1024         /* ACK_1 frames and other LINK_CTL frames echo the last seq bit if the
1025          * packet they're ack'ing did not have it set. So, we'll incorrectly
1026          * flag them as being fragmented when they're not. This fixes the
1027          * problem
1028          */
1029         is_lastframe_inseq = TRUE;
1030     } else {
1031         /* XXX is this right?   offset 20, shouldnt it be offset 9? */
1032         is_exchg_resp = ((tvb_get_guint8 (tvb, offset+20) & 0x80) == 0x80);
1033     }
1034
1035     if (tvb_reported_length (tvb) <= FC_HEADER_SIZE)
1036         THROW(ReportedBoundsError);
1037
1038     frag_size = tvb_reported_length (tvb)-FC_HEADER_SIZE;
1039
1040     /* If there is an MDS header, we need to subtract the MDS trailer size
1041      * Link Ctl, BLS & OHMS are all (encap header + FC Header + encap trailer)
1042      * and are never fragmented and so we ignore the frag_size assertion for
1043      *  these frames.
1044      */
1045     if ((pinfo->ethertype == ETHERTYPE_UNK) || (pinfo->ethertype == ETHERTYPE_FCFT)) {
1046          if ((frag_size <= MDSHDR_TRAILER_SIZE) ||
1047              ((frag_size == MDSHDR_TRAILER_SIZE) && (ftype != FC_FTYPE_LINKCTL) &&
1048               (ftype != FC_FTYPE_BLS) && (ftype != FC_FTYPE_OHMS)))
1049             THROW(ReportedBoundsError);
1050         frag_size -= MDSHDR_TRAILER_SIZE;
1051     } else if (pinfo->ethertype == ETHERTYPE_BRDWALK) {
1052          if ((frag_size <= 8) ||
1053              ((frag_size == MDSHDR_TRAILER_SIZE) && (ftype != FC_FTYPE_LINKCTL) &&
1054               (ftype != FC_FTYPE_BLS) && (ftype != FC_FTYPE_OHMS)))
1055               THROW(ReportedBoundsError);
1056         frag_size -= 8;         /* 4 byte of FC CRC +
1057                                    4 bytes of error+EOF = 8 bytes  */
1058     }
1059
1060     if (!is_lastframe_inseq) {
1061         /* Show this only as a fragmented FC frame */
1062         if (check_col (pinfo->cinfo, COL_INFO)) {
1063             col_append_str (pinfo->cinfo, COL_INFO, " (Fragmented)");
1064         }
1065     }
1066
1067     /* If this is a fragment, attempt to check if fully reassembled frame is
1068      * present, if we're configured to reassemble.
1069      */
1070     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS) &&
1071         (ftype != FC_FTYPE_OHMS) &&
1072         (!is_lastframe_inseq || !is_1frame_inseq) && fc_reassemble &&
1073         tvb_bytes_exist(tvb, FC_HEADER_SIZE, frag_size) && tree) {
1074         /* Add this to the list of fragments */
1075
1076         /* In certain cases such as FICON, the SEQ_CNT is streaming
1077          * i.e. continuously increasing. So, zero does not signify the
1078          * first frame of the sequence. To fix this, we need to save the
1079          * SEQ_CNT of the first frame in sequence and use this value to
1080          * determine the actual offset into a frame.
1081          */
1082         conversation = find_conversation (pinfo->fd->num, &pinfo->src, &pinfo->dst,
1083                                           pinfo->ptype, pinfo->oxid,
1084                                           pinfo->rxid, NO_PORT2);
1085         if (!conversation) {
1086             conversation = conversation_new (pinfo->fd->num, &pinfo->src, &pinfo->dst,
1087                                              pinfo->ptype, pinfo->oxid,
1088                                              pinfo->rxid, NO_PORT2);
1089         }
1090         
1091         ckey.conv_idx = conversation->index;
1092         
1093         cdata = (fcseq_conv_data_t *)g_hash_table_lookup (fcseq_req_hash,
1094                                                           &ckey);
1095
1096         if (is_1frame_inseq) {
1097             if (cdata) {
1098                 /* Since we never free the memory used by an exchange, this maybe a
1099                  * case of another request using the same exchange as a previous
1100                  * req. 
1101                  */
1102                 cdata->seq_cnt = fchdr.seqcnt;
1103             }
1104             else {
1105                 req_key = se_alloc (sizeof(fcseq_conv_key_t));
1106                 req_key->conv_idx = conversation->index;
1107                 
1108                 cdata = se_alloc (sizeof(fcseq_conv_data_t));
1109                 cdata->seq_cnt = fchdr.seqcnt;
1110                 
1111                 g_hash_table_insert (fcseq_req_hash, req_key, cdata);
1112             }
1113             real_seqcnt = 0;
1114         }
1115         else if (cdata != NULL) {
1116             real_seqcnt = fchdr.seqcnt - cdata->seq_cnt ;
1117         }
1118         else {
1119             real_seqcnt = fchdr.seqcnt;
1120         }
1121
1122         /* Verify that this is a valid fragment */
1123         if (is_lastframe_inseq && !is_1frame_inseq && !real_seqcnt) {
1124              /* This is a frame that purports to be the last frame in a
1125               * sequence, is not the first frame, but has a seqcnt that is
1126               * 0. This is a bogus frame, don't attempt to reassemble it.
1127               */
1128              next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1129              if (check_col (pinfo->cinfo, COL_INFO)) {
1130                   col_append_str (pinfo->cinfo, COL_INFO, " (Bogus Fragment)");
1131              }
1132         } else {
1133         
1134              frag_id = ((pinfo->oxid << 16) ^ seq_id) | is_exchg_resp ;
1135
1136              /* We assume that all frames are of the same max size */
1137              fcfrag_head = fragment_add (tvb, FC_HEADER_SIZE, pinfo, frag_id,
1138                                          fc_fragment_table,
1139                                          real_seqcnt * fc_max_frame_size,
1140                                          frag_size,
1141                                          !is_lastframe_inseq);
1142              
1143              if (fcfrag_head) {
1144                   next_tvb = tvb_new_real_data (fcfrag_head->data,
1145                                                 fcfrag_head->datalen,
1146                                                 fcfrag_head->datalen);
1147                   tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1148                   
1149                   /* Add the defragmented data to the data source list. */
1150             add_new_data_source(pinfo, next_tvb, "Reassembled FC");
1151             
1152             if (tree) {
1153                  proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1154                                                 tvb, offset+9, 1, 1);
1155             }
1156              }
1157              else {
1158                   if (tree) {
1159                        proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1160                                                       tvb, offset+9, 1, 0);
1161             }
1162                   next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1163                   call_dissector (data_handle, next_tvb, pinfo, tree);
1164                   return;
1165              }
1166         }
1167     } else {
1168         if (tree) {
1169             proto_tree_add_boolean_hidden (fc_tree, hf_fc_reassembled,
1170                                            tvb, offset+9, 1, 0);
1171         }
1172         next_tvb = tvb_new_subset (tvb, next_offset, -1, -1);
1173     }
1174
1175     if ((ftype != FC_FTYPE_LINKCTL) && (ftype != FC_FTYPE_BLS)) {
1176         /* If relative offset is used, only dissect the pdu with
1177          * offset 0 (param) */
1178         if( (fchdr.fctl&FC_FCTL_REL_OFFSET) && param ){
1179             call_dissector (data_handle, next_tvb, pinfo, tree);
1180         } else {
1181             if (!dissector_try_port (fcftype_dissector_table, ftype, 
1182                                 next_tvb, pinfo, tree)) {
1183                 call_dissector (data_handle, next_tvb, pinfo, tree);
1184             }
1185         }
1186     } else if (ftype == FC_FTYPE_BLS) {
1187         if ((fchdr.r_ctl & 0x0F) == FC_BLS_BAACC) {
1188             dissect_fc_ba_acc (next_tvb, pinfo, tree);
1189         } else if ((fchdr.r_ctl & 0x0F) == FC_BLS_BARJT) {
1190             dissect_fc_ba_rjt (next_tvb, pinfo, tree);
1191         }
1192     }
1193
1194     tap_queue_packet(fc_tap, pinfo, &fchdr);
1195 }
1196
1197 static void
1198 dissect_fc (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1199 {
1200     dissect_fc_helper (tvb, pinfo, tree, FALSE);
1201 }
1202 static void
1203 dissect_fc_ifcp (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1204 {
1205     dissect_fc_helper (tvb, pinfo, tree, TRUE);
1206 }
1207
1208 /* Register the protocol with Ethereal */
1209
1210 /* this format is require because a script is used to build the C function
1211    that calls all the protocol registration.
1212 */
1213
1214 void
1215 proto_register_fc(void)
1216 {                 
1217
1218 /* Setup list of header fields  See Section 1.6.1 for details*/
1219     static hf_register_info hf[] = {
1220         { &hf_fc_rctl,
1221           { "R_CTL", "fc.r_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
1222             "R_CTL", HFILL }},
1223         { &hf_fc_ftype,
1224           {"Frame type", "fc.ftype", FT_UINT8, BASE_HEX, VALS(fc_ftype_vals),
1225            0x0, "Derived Type", HFILL}},
1226         { &hf_fc_did,
1227           { "Dest Addr", "fc.d_id", FT_STRING, BASE_HEX, NULL, 0x0,
1228             "Destination Address", HFILL}},
1229         { &hf_fc_csctl,
1230           {"CS_CTL", "fc.cs_ctl", FT_UINT8, BASE_HEX, NULL, 0x0,
1231            "CS_CTL", HFILL}},
1232         { &hf_fc_sid,
1233           {"Src Addr", "fc.s_id", FT_STRING, BASE_HEX, NULL, 0x0,
1234            "Source Address", HFILL}},
1235         { &hf_fc_id,
1236           {"Addr", "fc.id", FT_STRING, BASE_HEX, NULL, 0x0,
1237            "Source or Destination Address", HFILL}},
1238         { &hf_fc_type,
1239           {"Type", "fc.type", FT_UINT8, BASE_HEX, VALS (fc_fc4_val), 0x0,
1240            "", HFILL}},
1241         { &hf_fc_fctl,
1242           {"F_CTL", "fc.f_ctl", FT_UINT24, BASE_HEX, NULL, 0x0, "", HFILL}},
1243         { &hf_fc_seqid,
1244           {"SEQ_ID", "fc.seq_id", FT_UINT8, BASE_HEX, NULL, 0x0,
1245            "Sequence ID", HFILL}},
1246         { &hf_fc_dfctl,
1247           {"DF_CTL", "fc.df_ctl", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}},
1248         { &hf_fc_seqcnt,
1249           {"SEQ_CNT", "fc.seq_cnt", FT_UINT16, BASE_DEC, NULL, 0x0,
1250            "Sequence Count", HFILL}},
1251         { &hf_fc_oxid,
1252           {"OX_ID", "fc.ox_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Originator ID",
1253            HFILL}},
1254         { &hf_fc_rxid,
1255           {"RX_ID", "fc.rx_id", FT_UINT16, BASE_HEX, NULL, 0x0, "Receiver ID",
1256            HFILL}},
1257         { &hf_fc_param,
1258           {"Parameter", "fc.parameter", FT_UINT32, BASE_HEX, NULL, 0x0, "Parameter",
1259            HFILL}},
1260
1261         { &hf_fc_reassembled,
1262           {"Reassembled Frame", "fc.reassembled", FT_BOOLEAN, BASE_HEX, NULL,
1263            0x0, "", HFILL}},
1264         { &hf_fc_nh_da,
1265           {"Network DA", "fc.nethdr.da", FT_STRING, BASE_HEX, NULL,
1266            0x0, "", HFILL}},
1267         { &hf_fc_nh_sa,
1268           {"Network SA", "fc.nethdr.sa", FT_STRING, BASE_HEX, NULL,
1269            0x0, "", HFILL}},
1270
1271         /* Basic Link Svc field definitions */
1272         { &hf_fc_bls_seqid_vld,
1273           {"SEQID Valid", "fc.bls_seqidvld", FT_UINT8, BASE_HEX,
1274            VALS (fc_bls_seqid_val), 0x0, "", HFILL}},
1275         { &hf_fc_bls_lastvld_seqid,
1276           {"Last Valid SEQID", "fc.bls_lastseqid", FT_UINT8, BASE_HEX, NULL,
1277            0x0, "", HFILL}},
1278         { &hf_fc_bls_oxid,
1279           {"OXID", "fc.bls_oxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
1280         { &hf_fc_bls_rxid,
1281           {"RXID", "fc.bls_rxid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL}},
1282         { &hf_fc_bls_lowseqcnt,
1283           {"Low SEQCNT", "fc.bls_lseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
1284            HFILL}},
1285         { &hf_fc_bls_hiseqcnt,
1286           {"High SEQCNT", "fc.bls_hseqcnt", FT_UINT16, BASE_HEX, NULL, 0x0, "",
1287            HFILL}},
1288         { &hf_fc_bls_rjtcode,
1289           {"Reason", "fc.bls_reason", FT_UINT8, BASE_HEX, VALS(fc_bls_barjt_val),
1290            0x0, "", HFILL}},
1291         { &hf_fc_bls_rjtdetail,
1292           {"Reason Explanantion", "fc.bls_rjtdetail", FT_UINT8, BASE_HEX,
1293            VALS (fc_bls_barjt_det_val), 0x0, "", HFILL}},
1294         { &hf_fc_bls_vendor,
1295           {"Vendor Unique Reason", "fc.bls_vnduniq", FT_UINT8, BASE_HEX, NULL,
1296            0x0, "", HFILL}},
1297         { &hf_fc_fctl_exchange_responder,
1298           {"ExgRpd", "fc.fctl.exchange_responder", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_responder),
1299            FC_FCTL_EXCHANGE_RESPONDER, "Exchange Responder?", HFILL}},
1300         { &hf_fc_fctl_seq_recipient,
1301           {"SeqRec", "fc.fctl.seq_recipient", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_seq_recipient),
1302            FC_FCTL_SEQ_RECIPIENT, "Seq Recipient?", HFILL}},
1303         { &hf_fc_fctl_exchange_first,
1304           {"ExgFst", "fc.fctl.exchange_first", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_first),
1305            FC_FCTL_EXCHANGE_FIRST, "First Exchange?", HFILL}},
1306         { &hf_fc_fctl_exchange_last,
1307           {"ExgLst", "fc.fctl.exchange_last", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_exchange_last),
1308            FC_FCTL_EXCHANGE_LAST, "Last Exchange?", HFILL}},
1309         { &hf_fc_fctl_seq_last,
1310           {"SeqLst", "fc.fctl.seq_last", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_seq_last),
1311            FC_FCTL_SEQ_LAST, "Last Sequence?", HFILL}},
1312         { &hf_fc_fctl_priority,
1313           {"Pri", "fc.fctl.priority", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_priority),
1314            FC_FCTL_PRIORITY, "Priority", HFILL}},
1315         { &hf_fc_fctl_transfer_seq_initiative,
1316           {"TSI", "fc.fctl.transfer_seq_initiative", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_transfer_seq_initiative),
1317            FC_FCTL_TRANSFER_SEQ_INITIATIVE, "Transfer Seq Initiative", HFILL}},
1318         { &hf_fc_fctl_rexmitted_seq,
1319           {"RetSeq", "fc.fctl.rexmitted_seq", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_rexmitted_seq),
1320            FC_FCTL_REXMITTED_SEQ, "Retransmitted Sequence", HFILL}},
1321         { &hf_fc_fctl_rel_offset,
1322           {"RelOff", "fc.fctl.rel_offset", FT_BOOLEAN, 24, TFS(&tfs_fc_fctl_rel_offset),
1323            FC_FCTL_REL_OFFSET, "rel offset", HFILL}},
1324         { &hf_fc_fctl_last_data_frame,
1325           {"LDF", "fc.fctl.last_data_frame", FT_UINT24, BASE_HEX, VALS(last_data_frame_vals),
1326            FC_FCTL_LAST_DATA_FRAME_MASK, "Last Data Frame?", HFILL}},
1327         { &hf_fc_fctl_ack_0_1,
1328           {"A01", "fc.fctl.ack_0_1", FT_UINT24, BASE_HEX, VALS(ack_0_1_vals),
1329            FC_FCTL_ACK_0_1_MASK, "Ack 0/1 value", HFILL}},
1330         { &hf_fc_fctl_abts_ack,
1331           {"AA", "fc.fctl.abts_ack", FT_UINT24, BASE_HEX, VALS(abts_ack_vals),
1332            FC_FCTL_ABTS_MASK, "ABTS ACK values", HFILL}},
1333         { &hf_fc_fctl_abts_not_ack,
1334           {"AnA", "fc.fctl.abts_not_ack", FT_UINT24, BASE_HEX, VALS(abts_not_ack_vals),
1335            FC_FCTL_ABTS_MASK, "ABTS not ACK vals", HFILL}},
1336         { &hf_fc_exchange_first_frame,
1337           { "Exchange First In", "fc.exchange_first_frame", FT_FRAMENUM, BASE_NONE, NULL,
1338            0, "The first frame of this exchange is in this frame", HFILL }},
1339         { &hf_fc_exchange_last_frame,
1340           { "Exchange Last In", "fc.exchange_last_frame", FT_FRAMENUM, BASE_NONE, NULL,
1341            0, "The last frame of this exchange is in this frame", HFILL }},
1342         { &hf_fc_time,
1343           { "Time from Exchange First", "fc.time", FT_RELATIVE_TIME, BASE_NONE, NULL,
1344            0, "Time since the first frame of the Exchange", HFILL }},
1345         { &hf_fc_eisl,
1346           {"EISL Header", "fc.eisl", FT_BYTES, BASE_HEX, NULL, 0, "EISL Header", HFILL}},
1347     };
1348
1349     /* Setup protocol subtree array */
1350     static gint *ett[] = {
1351         &ett_fc,
1352         &ett_fcbls,
1353         &ett_fctl
1354     };
1355
1356     module_t *fc_module;
1357
1358     /* Register the protocol name and description */
1359     proto_fc = proto_register_protocol ("Fibre Channel", "FC", "fc");
1360     register_dissector ("fc", dissect_fc, proto_fc);
1361     register_dissector ("fc_ifcp", dissect_fc_ifcp, proto_fc);
1362     fc_tap = register_tap("fc");
1363
1364     /* Required function calls to register the header fields and subtrees used */
1365     proto_register_field_array(proto_fc, hf, array_length(hf));
1366     proto_register_subtree_array(ett, array_length(ett));
1367
1368     fcftype_dissector_table = register_dissector_table ("fc.ftype",
1369                                                         "FC Frame Type",
1370                                                         FT_UINT8, BASE_HEX);
1371
1372     /* Register preferences */
1373     fc_module = prefs_register_protocol (proto_fc, NULL);
1374     prefs_register_bool_preference (fc_module,
1375                                     "reassemble",
1376                                     "Reassemble multi-frame sequences",
1377                                     "If enabled, reassembly of multi-frame "
1378                                     "sequences is done",
1379                                     &fc_reassemble);
1380     prefs_register_uint_preference (fc_module,
1381                                     "max_frame_size", "Max FC Frame Size",
1382                                     "This is the size of non-last frames in a "
1383                                     "multi-frame sequence", 10,
1384                                     &fc_max_frame_size);
1385     
1386     register_init_routine(fc_defragment_init);
1387     register_init_routine (fc_exchange_init_protocol);
1388 }
1389
1390
1391 /* If this dissector uses sub-dissector registration add a registration routine.
1392    This format is required because a script is used to find these routines and
1393    create the code that calls these routines.
1394 */
1395 void
1396 proto_reg_handoff_fc (void)
1397 {
1398     data_handle = find_dissector("data");
1399 }