updates to get the fc and scsi dissectors
[obnox/wireshark/wip.git] / epan / dissectors / packet-fcct.c
1 /* packet-fcct.c
2  * Routines for FC Common Transport Protocol (used by GS3 services)
3  * Copyright 2001, Dinesh G Dutt <ddutt@andiamo.com>
4  *
5  * $Id$
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 #include <epan/packet.h>
45 #include <epan/etypes.h>
46 #include <epan/conversation.h>
47 #include "packet-fc.h"
48 #include "packet-fcct.h"
49
50 /* Initialize the protocol and registered fields */
51 static int proto_fcct           = -1;
52 static int hf_fcct_revision     = -1;
53 static int hf_fcct_inid         = -1;
54 static int hf_fcct_gstype       = -1;
55 static int hf_fcct_gssubtype    = -1;
56 static int hf_fcct_options      = -1;
57 static int hf_fcct_server       = -1; /* derived field */
58
59 /* Extended preamble fields */
60 static int hf_fcct_ext_said     = -1;
61 static int hf_fcct_ext_tid      = -1;
62 static int hf_fcct_ext_reqname  = -1;
63 static int hf_fcct_ext_tstamp   = -1;
64 static int hf_fcct_ext_authblk  = -1;
65
66 /* Initialize the subtree pointers */
67 static gint ett_fcct = -1;
68 static gint ett_fcct_ext = -1;  /* for the extended header */
69
70 const value_string fc_ct_rjt_code_vals [] = {
71     {FCCT_RJT_INVCMDCODE, "Invalid Cmd Code"},
72     {FCCT_RJT_INVVERSION, "Invalid Version Level"},
73     {FCCT_RJT_LOGICALERR, "Logical Error"},
74     {FCCT_RJT_INVSIZE,    "Invalid CT_IU Size"},
75     {FCCT_RJT_LOGICALBSY, "Logical Busy"},
76     {FCCT_RJT_PROTOERR,   "Protocol Error"},
77     {FCCT_RJT_GENFAIL,    "Unable to Perform Cmd"},
78     {FCCT_RJT_CMDNOTSUPP, "Cmd Not Supported"},
79     {0, NULL},
80 };
81
82 const value_string fc_ct_gstype_vals[] = {
83     {FCCT_GSTYPE_KEYSVC, "Key Service"},
84     {FCCT_GSTYPE_ALIASSVC, "Alias Service"},
85     {FCCT_GSTYPE_MGMTSVC, "Management Service"},
86     {FCCT_GSTYPE_TIMESVC, "Time Service"},
87     {FCCT_GSTYPE_DIRSVC, "Directory Service"},
88     {FCCT_GSTYPE_FCTLR, "Fabric Controller"},
89     {FCCT_GSTYPE_VENDOR, "Vendor-Specific"},
90     {0, NULL},
91 };
92
93 const value_string fc_ct_gsserver_vals[] = {
94     {FCCT_GSRVR_DNS, "dNS"},
95     {FCCT_GSRVR_IP,  "IP"},
96     {FCCT_GSRVR_FCS, "Fabric Config Server"},
97     {FCCT_GSRVR_UNS, "Unzoned Name Server"},
98     {FCCT_GSRVR_FZS, "Fabric Zone Server"},
99     {FCCT_GSRVR_TS,  "Time Server"},
100     {FCCT_GSRVR_KS,  "Key Server"},
101     {FCCT_GSRVR_AS,  "Alias Server"},
102     {FCCT_GSRVR_FCTLR, "Fabric Controller"},
103     {0, NULL},
104 };
105
106 static dissector_table_t fcct_gserver_table;
107 static dissector_handle_t data_handle;
108
109 guint8
110 get_gs_server (guint8 gstype, guint8 gssubtype)
111 {
112     switch (gstype) {
113     case FCCT_GSTYPE_KEYSVC:
114         return FCCT_GSRVR_KS;
115     case FCCT_GSTYPE_ALIASSVC:
116         if (gssubtype == FCCT_GSSUBTYPE_AS)
117             return FCCT_GSRVR_AS;
118         return FCCT_GSRVR_UNKNOWN;
119     case FCCT_GSTYPE_MGMTSVC:
120         if (gssubtype == FCCT_GSSUBTYPE_FCS)
121             return FCCT_GSRVR_FCS;
122         else if (gssubtype == FCCT_GSSUBTYPE_UNS)
123             return FCCT_GSRVR_UNS;
124         else if (gssubtype == FCCT_GSSUBTYPE_FZS)
125             return FCCT_GSRVR_FZS;
126         else return FCCT_GSRVR_UNKNOWN;
127     case FCCT_GSTYPE_TIMESVC:
128         if (gssubtype == FCCT_GSSUBTYPE_TS)
129             return FCCT_GSRVR_TS;
130         return FCCT_GSRVR_UNKNOWN;
131     case FCCT_GSTYPE_DIRSVC:
132         if (gssubtype == FCCT_GSSUBTYPE_DNS)
133             return FCCT_GSRVR_DNS;
134         else if (gssubtype == FCCT_GSSUBTYPE_IP)
135             return FCCT_GSRVR_IP;
136         return FCCT_GSRVR_UNKNOWN;
137     case FCCT_GSRVR_FCTLR:
138          if (gssubtype == FCCT_GSSUBTYPE_FCTLR)
139               return (FCCT_GSRVR_FCTLR);
140          else return (FCCT_GSRVR_UNKNOWN);
141     default:
142         return FCCT_GSRVR_UNKNOWN;
143     }
144 }
145
146 /* Code to actually dissect the packets */
147 static void
148 dissect_fcct (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
149 {
150
151 /* Set up structures needed to add the protocol subtree and manage it */
152     proto_item *ti;
153     proto_tree *fcct_tree;
154     tvbuff_t *next_tvb;
155     int in_id,
156         offset = 0;
157     guint8 server;
158     fc_ct_preamble cthdr;
159
160     /* Make entries in Protocol column and Info column on summary display */
161     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
162         col_set_str(pinfo->cinfo, COL_PROTOCOL, "FC_CT");
163     
164     /*
165       cthdr.revision = tvb_get_guint8 (tvb, offset++);
166       cthdr.in_id = tvb_get_ntoh24 (tvb, offset);
167       offset += 3;
168       
169       cthdr.gstype = tvb_get_guint8 (tvb, offset++);
170       cthdr.options = tvb_get_guint8 (tvb, offset++);
171     */
172     tvb_memcpy (tvb, (guint8 *)&cthdr, offset, FCCT_PRMBL_SIZE);
173     cthdr.revision = tvb_get_guint8 (tvb, offset++);
174     cthdr.in_id = tvb_get_ntoh24 (tvb, offset);
175     cthdr.opcode = ntohs (cthdr.opcode);
176     cthdr.maxres_size = ntohs (cthdr.maxres_size);
177
178     if (check_col (pinfo->cinfo, COL_INFO)) {
179         if (cthdr.opcode < FCCT_MSG_REQ_MAX) {
180             col_append_str (pinfo->cinfo, COL_INFO, " Request");
181         }
182         else if (cthdr.opcode == FCCT_MSG_ACC) {
183             col_append_str (pinfo->cinfo, COL_INFO, " Accept");
184         }
185         else if (cthdr.opcode == FCCT_MSG_RJT) {
186             col_append_fstr (pinfo->cinfo, COL_INFO, " Reject (%s)",
187                              val_to_str (cthdr.rjt_code, fc_ct_rjt_code_vals, "0x%x")); 
188         }
189         else {
190             col_append_str (pinfo->cinfo, COL_INFO, " Reserved");
191         }
192     }
193     
194     in_id = cthdr.in_id;
195     in_id = htonl (in_id) >> 8;
196
197     /* Determine server */
198     server = get_gs_server (cthdr.gstype, cthdr.gssubtype);
199     
200     if (tree) {
201         offset = 0;
202         ti = proto_tree_add_protocol_format (tree, proto_fcct, tvb, 0, FCCT_PRMBL_SIZE,
203                                              "FC_CT");
204         fcct_tree = proto_item_add_subtree (ti, ett_fcct);
205
206         proto_tree_add_item (fcct_tree, hf_fcct_revision, tvb, offset++,
207                              sizeof (guint8), 0);
208         proto_tree_add_string (fcct_tree, hf_fcct_inid, tvb, offset, 3,
209                                fc_to_str ((guint8 *)&in_id));
210         offset += 3; /* sizeof FC address */
211         
212         proto_tree_add_item (fcct_tree, hf_fcct_gstype, tvb, offset++,
213                              sizeof (guint8), 0);
214         proto_tree_add_item (fcct_tree, hf_fcct_gssubtype, tvb, offset,
215                              sizeof (guint8), 0);
216         proto_tree_add_uint (fcct_tree, hf_fcct_server, tvb, offset++, 1,
217                              server);
218         proto_tree_add_item (fcct_tree, hf_fcct_options, tvb, offset++,
219                              sizeof (guint8), 0);
220
221     }
222     /* We do not change the starting offset for the next protocol in the
223      * chain since the fc_ct header is common to the sub-protocols.
224      */
225     next_tvb = tvb_new_subset (tvb, 0, -1, -1);
226     if (!dissector_try_port (fcct_gserver_table, server, next_tvb, pinfo,
227                              tree)) {
228         call_dissector (data_handle, next_tvb, pinfo, tree);
229     }
230 }
231
232 /* Register the protocol with Ethereal */
233
234 /* this format is require because a script is used to build the C function
235    that calls all the protocol registration.
236 */
237
238 void
239 proto_register_fcct(void)
240 {                 
241
242 /* Setup list of header fields  See Section 1.6.1 for details*/
243     static hf_register_info hf[] = {
244         { &hf_fcct_revision,
245           {"Revision", "fcct.revision", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL}}, 
246         { &hf_fcct_inid,
247           {"IN_ID", "fcct.in_id", FT_STRING, BASE_HEX, NULL, 0x0, "", HFILL}},
248         { &hf_fcct_gstype,
249           {"GS Type", "fcct.gstype", FT_UINT8, BASE_HEX, VALS(fc_ct_gstype_vals),
250            0x0, "", HFILL}},
251         { &hf_fcct_gssubtype,
252           {"GS Subtype", "fcct.gssubtype", FT_UINT8, BASE_HEX, NULL, 0x0,
253            "", HFILL}},
254         { &hf_fcct_server,
255           {"Server", "fcct.server", FT_UINT8, BASE_HEX,
256            VALS (fc_ct_gsserver_vals), 0x0,
257            "Derived from GS Type & Subtype fields", HFILL}},
258         { &hf_fcct_options,
259           {"Options", "fcct.options", FT_UINT8, BASE_HEX, NULL, 0x0, "",
260            HFILL}},
261         { &hf_fcct_ext_said,
262           {"Auth SAID", "fcct.ext_said", FT_UINT32, BASE_HEX, NULL, 0x0, "",
263            HFILL}},
264         { &hf_fcct_ext_tid,
265           {"Transaction ID", "fcct.ext_tid", FT_UINT32, BASE_HEX, NULL, 0x0, "",
266            HFILL}},
267         { &hf_fcct_ext_reqname,
268           {"Requestor Port Name", "fcct_ext_reqnm", FT_BYTES, BASE_HEX, NULL,
269            0x0, "", HFILL}},
270         { &hf_fcct_ext_tstamp,
271           {"Timestamp", "fcct_ext_tstamp", FT_BYTES, BASE_HEX, NULL, 0x0, "",
272            HFILL}},
273         { &hf_fcct_ext_authblk,
274           {"Auth Hash Blk", "fcct_ext_authblk", FT_BYTES, BASE_HEX, NULL, 0x0,
275            "", HFILL}},
276     };
277
278     /* Setup protocol subtree array */
279     static gint *ett[] = {
280         &ett_fcct,
281         &ett_fcct_ext,
282     };
283
284     /* Register the protocol name and description */
285     proto_fcct = proto_register_protocol("Fibre Channel Common Transport", "FC_CT", "fcct");
286
287     /* Required function calls to register the header fields and subtrees used */
288     proto_register_field_array(proto_fcct, hf, array_length(hf));
289     proto_register_subtree_array(ett, array_length(ett));
290
291     fcct_gserver_table = register_dissector_table ("fcct.server",
292                                                    "Server",
293                                                    FT_UINT8, BASE_HEX);
294 }
295
296 /* If this dissector uses sub-dissector registration add a registration routine.
297    This format is required because a script is used to find these routines and
298    create the code that calls these routines.
299 */
300 void
301 proto_reg_handoff_fcct (void)
302 {
303     dissector_handle_t fcct_handle;
304
305     fcct_handle = create_dissector_handle (dissect_fcct, proto_fcct);
306     dissector_add("fc.ftype", FC_FTYPE_FCCT, fcct_handle);
307
308     data_handle = find_dissector ("data");
309 }
310
311