Move 3 ASN1 dissectors to 'clean' group; move 1 PIDL dissector to 'dirty' group.
[metze/wireshark/wip.git] / epan / dissectors / packet-fcoib.c
1 /*
2  * packet-fcoib.c
3  * Routines for FCoIB dissection - Fibre Channel over Infiniband
4  * Copyright (c) 2010 Mellanox Technologies Ltd. (slavak@mellanox.co.il)
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * Based on packet-fcoe.c, Copyright (c) 2006 Nuova Systems, Inc. (jre@nuovasystems.com)
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27  */
28
29 #include "config.h"
30
31 #include <stdlib.h>
32 #include <epan/packet.h>
33 #include <epan/prefs.h>
34 #include <epan/crc32-tvb.h>
35 #include <epan/etypes.h>
36 #include <epan/expert.h>
37 #include <errno.h>
38 #include "packet-infiniband.h"
39
40 #ifdef HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
42 #endif
43 #ifdef HAVE_SYS_SOCKET_H
44 # include <sys/socket.h>         /* needed to define AF_ values on UNIX */
45 #endif
46 #ifdef HAVE_WINSOCK2_H
47 # include <winsock2.h>           /* needed to define AF_ values on Windows */
48 #endif
49 #ifdef NEED_INET_V6DEFS_H
50 # include "wsutil/inet_v6defs.h"
51 #endif
52
53 #define FCOIB_HEADER_LEN   16        /* header: encap. header, SOF, and padding */
54 #define FCOIB_TRAILER_LEN   8        /* trailer: FC-CRC, EOF and padding */
55 #define FCOIB_VER_OFFSET    2        /* offset of ver field (in bytes) inside FCoIB Encap. header */
56
57 /* Forward declaration we need below (for using proto_reg_handoff as a prefs callback) */
58 void proto_reg_handoff_fcoib(void);
59
60 typedef enum {
61     FCOIB_EOFn    = 0x41,
62     FCOIB_EOFt    = 0x42,
63     FCOIB_EOFrt   = 0x44,
64     FCOIB_EOFdt   = 0x46,
65     FCOIB_EOFni   = 0x49,
66     FCOIB_EOFdti  = 0x4E,
67     FCOIB_EOFrti  = 0x4F,
68     FCOIB_EOFa    = 0x50
69 } fcoib_eof_t;
70
71 typedef enum {
72     FCOIB_SOFf    = 0x28,
73     FCOIB_SOFi4   = 0x29,
74     FCOIB_SOFi2   = 0x2D,
75     FCOIB_SOFi3   = 0x2E,
76     FCOIB_SOFn4   = 0x31,
77     FCOIB_SOFn2   = 0x35,
78     FCOIB_SOFn3   = 0x36,
79     FCOIB_SOFc4   = 0x39
80 } fcoib_sof_t;
81
82 static const value_string fcoib_eof_vals[] = {
83     {FCOIB_EOFn,    "EOFn" },
84     {FCOIB_EOFt,    "EOFt" },
85     {FCOIB_EOFrt,   "EOFrt" },
86     {FCOIB_EOFdt,   "EOFdt" },
87     {FCOIB_EOFni,   "EOFni" },
88     {FCOIB_EOFdti,  "EOFdti" },
89     {FCOIB_EOFrti,  "EOFrti" },
90     {FCOIB_EOFa,    "EOFa" },
91     {0, NULL}
92 };
93
94 static const value_string fcoib_sof_vals[] = {
95     {FCOIB_SOFf,    "SOFf" },
96     {FCOIB_SOFi4,   "SOFi4" },
97     {FCOIB_SOFi2,   "SOFi2" },
98     {FCOIB_SOFi3,   "SOFi3" },
99     {FCOIB_SOFn4,   "SOFn4" },
100     {FCOIB_SOFn2,   "SOFn2" },
101     {FCOIB_SOFn3,   "SOFn3" },
102     {FCOIB_SOFc4,   "SOFc4" },
103     {0, NULL}
104 };
105
106 static int proto_fcoib          = -1;
107 static int hf_fcoib_ver         = -1;
108 static int hf_fcoib_sig         = -1;
109 static int hf_fcoib_sof         = -1;
110 static int hf_fcoib_eof         = -1;
111 static int hf_fcoib_crc         = -1;
112 static int hf_fcoib_crc_bad     = -1;
113 static int hf_fcoib_crc_good    = -1;
114
115 static int ett_fcoib            = -1;
116 static int ett_fcoib_crc        = -1;
117
118 static dissector_handle_t data_handle;
119 static dissector_handle_t fc_handle;
120
121 /* global preferences */
122 static gboolean    gPREF_HEUR_EN = TRUE;
123 static gboolean    gPREF_MAN_EN  = FALSE;
124 static gint        gPREF_TYPE[2] = {0};
125 static const char *gPREF_ID[2]   = {NULL};
126 static guint       gPREF_QP[2]   = {0};
127
128 /* source/destination addresses from preferences menu (parsed from gPREF_TYPE[?], gPREF_ID[?]) */
129 static address  manual_addr[2];
130 static void    *manual_addr_data[2];
131
132 static const enum_val_t pref_address_types[] = {
133     {"lid", "LID", 0},
134     {"gid", "GID", 1},
135     {NULL, NULL, -1}
136 };
137
138 /* checks if a packet matches the source/destination manually-configured in preferences */
139 static gboolean
140 manual_addr_match(packet_info *pinfo) {
141     if (gPREF_MAN_EN) {
142         /* If the manual settings are enabled see if this fits - in which case we can skip
143            the following checks entirely and go straight to dissecting */
144         if (    (ADDRESSES_EQUAL(&pinfo->src, &manual_addr[0]) &&
145                  ADDRESSES_EQUAL(&pinfo->dst, &manual_addr[1]) &&
146                  (pinfo->srcport == 0xffffffff /* is unknown */ || pinfo->srcport == gPREF_QP[0]) &&
147                  (pinfo->destport == 0xffffffff /* is unknown */ || pinfo->destport == gPREF_QP[1]))    ||
148                 (ADDRESSES_EQUAL(&pinfo->src, &manual_addr[1]) &&
149                  ADDRESSES_EQUAL(&pinfo->dst, &manual_addr[0]) &&
150                  (pinfo->srcport == 0xffffffff /* is unknown */ || pinfo->srcport == gPREF_QP[1]) &&
151                  (pinfo->destport == 0xffffffff /* is unknown */ || pinfo->destport == gPREF_QP[0]))    )
152             return TRUE;
153     }
154
155     return FALSE;
156 }
157
158 static gboolean
159 dissect_fcoib(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
160 {
161     gint        crc_offset;
162     gint        eof_offset;
163     gint        sof_offset;
164     gint        frame_len;
165     guint       version;
166     const char *ver;
167     gint        bytes_remaining;
168     guint8      sof          = 0;
169     guint8      eof          = 0;
170     guint8      sig          = 0;
171     const char *eof_str;
172     const char *sof_str;
173     const char *crc_msg;
174     const char *len_msg;
175     proto_item *ti;
176     proto_item *item;
177     proto_tree *fcoib_tree;
178     proto_tree *crc_tree;
179     tvbuff_t   *next_tvb;
180     gboolean    crc_exists;
181     guint32     crc_computed = 0;
182     guint32     crc          = 0;
183     gboolean    packet_match_manual;
184
185     tree = proto_tree_get_root(tree);   /* we don't want to add FCoIB under the Infiniband tree */
186
187     frame_len = tvb_reported_length_remaining(tvb, 0) -
188       FCOIB_HEADER_LEN - FCOIB_TRAILER_LEN;
189     crc_offset = FCOIB_HEADER_LEN + frame_len;
190     eof_offset = crc_offset + 4;
191     sof_offset = FCOIB_HEADER_LEN - 1;
192
193     if (frame_len <= 0)
194         return FALSE;   /* this packet isn't even long enough to contain the header+trailer w/o FC payload! */
195
196     packet_match_manual = manual_addr_match(pinfo);
197
198     if (!packet_match_manual && !gPREF_HEUR_EN)
199         return FALSE;   /* user doesn't want us trying to automatically identify FCoIB packets */
200
201     /* we start off with some basic heuristics checks to make sure this could be a FCoIB packet */
202
203     if (tvb_bytes_exist(tvb, 0, 1))
204         sig = tvb_get_guint8(tvb, 0) >> 6;
205     if (tvb_bytes_exist(tvb, eof_offset, 1))
206         eof = tvb_get_guint8(tvb, eof_offset);
207     if (tvb_bytes_exist(tvb, sof_offset, 1))
208         sof = tvb_get_guint8(tvb, sof_offset);
209
210     if (!packet_match_manual) {
211         if (sig != 1)
212             return FALSE;   /* the sig field in the FCoIB Encap. header MUST be 2'b01*/
213         if (!tvb_bytes_exist(tvb, eof_offset + 1, 3) || tvb_get_ntoh24(tvb, eof_offset + 1) != 0)
214             return FALSE;   /* 3 bytes of RESERVED field immediately after eEOF MUST be 0 */
215         if (!match_strval(sof, fcoib_sof_vals))
216             return FALSE;   /* invalid value for SOF */
217         if (!match_strval(eof, fcoib_eof_vals))
218             return FALSE;   /* invalid value for EOF */
219     }
220
221
222     col_set_str(pinfo->cinfo, COL_PROTOCOL, "FCoIB");
223     bytes_remaining = tvb_length_remaining(tvb, FCOIB_HEADER_LEN);
224     if (bytes_remaining > frame_len)
225         bytes_remaining = frame_len;        /* backing length */
226     next_tvb = tvb_new_subset(tvb, FCOIB_HEADER_LEN, bytes_remaining, frame_len);
227
228     /*
229      * Only version 0 is defined at this point.
230      * Don't print the version in the short summary if it is zero.
231      */
232     ver = "";
233     version = tvb_get_guint8(tvb, 0 + FCOIB_VER_OFFSET) >> 4;
234     if (version != 0)
235         ver = ep_strdup_printf(ver, "ver %d ", version);
236
237     eof_str = "none";
238     if (tvb_bytes_exist(tvb, eof_offset, 1)) {
239         eof_str = val_to_str(eof, fcoib_eof_vals, "0x%x");
240     }
241
242     sof_str = "none";
243     if (tvb_bytes_exist(tvb, sof_offset, 1)) {
244         sof_str = val_to_str(sof, fcoib_sof_vals, "0x%x");
245     }
246
247     /*
248      * Check the CRC.
249      */
250     crc_msg = "";
251     crc_exists = tvb_bytes_exist(tvb, crc_offset, 4);
252     if (crc_exists) {
253         crc = tvb_get_ntohl(tvb, crc_offset);
254         crc_computed = crc32_802_tvb(next_tvb, frame_len);
255         if (crc != crc_computed) {
256             crc_msg = " [bad FC CRC]";
257         }
258     }
259     len_msg = "";
260     if ((frame_len % 4) != 0 || frame_len < 24) {
261         len_msg = " [invalid length]";
262     }
263
264     ti = proto_tree_add_protocol_format(tree, proto_fcoib, tvb, 0,
265                                         FCOIB_HEADER_LEN,
266                                         "FCoIB %s(%s/%s) %d bytes%s%s", ver,
267                                         sof_str, eof_str,
268                                         frame_len, crc_msg,
269                                         len_msg);
270
271     /* Dissect the FCoIB Encapsulation header */
272
273     fcoib_tree = proto_item_add_subtree(ti, ett_fcoib);
274     proto_tree_add_uint(fcoib_tree, hf_fcoib_sig, tvb, 0, 1, sig);
275     proto_tree_add_uint(fcoib_tree, hf_fcoib_ver, tvb, FCOIB_VER_OFFSET, 1, version);
276     proto_tree_add_uint(fcoib_tree, hf_fcoib_sof, tvb, sof_offset, 1, sof);
277
278     /*
279      * Create the CRC information.
280      */
281     if (crc_exists) {
282         if (crc == crc_computed) {
283             item = proto_tree_add_uint_format(fcoib_tree, hf_fcoib_crc, tvb,
284                                               crc_offset, 4, crc,
285                                               "CRC: %8.8x [valid]", crc);
286         } else {
287             item = proto_tree_add_uint_format(fcoib_tree, hf_fcoib_crc, tvb,
288                                               crc_offset, 4, crc,
289                                               "CRC: %8.8x "
290                                               "[error: should be %8.8x]",
291                                               crc, crc_computed);
292             expert_add_info_format(pinfo, item, PI_CHECKSUM, PI_ERROR,
293                                    "Bad FC CRC %8.8x %8.x",
294                                    crc, crc_computed);
295         }
296         proto_tree_set_appendix(fcoib_tree, tvb, crc_offset,
297                                 tvb_length_remaining (tvb, crc_offset));
298     } else {
299         item = proto_tree_add_text(fcoib_tree, tvb, crc_offset, 0,
300                                    "CRC: [missing]");
301     }
302     crc_tree = proto_item_add_subtree(item, ett_fcoib_crc);
303     ti = proto_tree_add_boolean(crc_tree, hf_fcoib_crc_bad, tvb,
304                                 crc_offset, 4,
305                                 crc_exists && crc != crc_computed);
306     PROTO_ITEM_SET_GENERATED(ti);
307     ti = proto_tree_add_boolean(crc_tree, hf_fcoib_crc_good, tvb,
308                                 crc_offset, 4,
309                                 crc_exists && crc == crc_computed);
310     PROTO_ITEM_SET_GENERATED(ti);
311
312     /*
313      * Interpret the EOF.
314      */
315     if (tvb_bytes_exist(tvb, eof_offset, 1)) {
316         proto_tree_add_item(fcoib_tree, hf_fcoib_eof, tvb, eof_offset, 1, ENC_BIG_ENDIAN);
317     }
318
319     /* Set the SOF/EOF flags in the packet_info header */
320     pinfo->sof_eof = 0;
321     if (sof == FCOIB_SOFi3 || sof == FCOIB_SOFi2 || sof == FCOIB_SOFi4) {
322         pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
323     } else if (sof == FCOIB_SOFf) {
324         pinfo->sof_eof = PINFO_SOF_SOFF;
325     }
326
327     if (eof != FCOIB_EOFn) {
328         pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
329     } else if (eof != FCOIB_EOFt) {
330         pinfo->sof_eof |= PINFO_EOF_INVALID;
331     }
332
333     /* Call the FC Dissector if this is carrying an FC frame */
334
335     if (fc_handle) {
336         call_dissector(fc_handle, next_tvb, pinfo, tree);
337     } else if (data_handle) {
338         call_dissector(data_handle, next_tvb, pinfo, tree);
339     }
340
341     return TRUE;
342 }
343
344 void
345 proto_register_fcoib(void)
346 {
347     module_t *fcoib_module;
348
349     /* Setup list of header fields  See Section 1.6.1 for details*/
350     static hf_register_info hf[] = {
351         { &hf_fcoib_sof,
352           {"SOF", "fcoib.sof", FT_UINT8, BASE_HEX, VALS(fcoib_sof_vals), 0,
353            NULL, HFILL}},
354         { &hf_fcoib_eof,
355           {"EOF", "fcoib.eof", FT_UINT8, BASE_HEX, VALS(fcoib_eof_vals), 0,
356            NULL, HFILL}},
357         { &hf_fcoib_sig,
358           {"Signature", "fcoib.sig", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL}},
359         { &hf_fcoib_ver,
360           {"Version", "fcoib.ver", FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL}},
361         { &hf_fcoib_crc,
362           {"CRC", "fcoib.crc", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL}},
363         { &hf_fcoib_crc_good,
364           {"CRC good", "fcoib.crc_good", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
365            "True: CRC matches packet content; False: doesn't match or not checked.", HFILL }},
366         { &hf_fcoib_crc_bad,
367           {"CRC bad", "fcoib.crc_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
368            "True: CRC doesn't match packet content; False: matches or not checked.", HFILL }}
369     };
370     static gint *ett[] = {
371         &ett_fcoib,
372         &ett_fcoib_crc
373     };
374
375     /* Register the protocol name and description */
376     proto_fcoib = proto_register_protocol("Fibre Channel over Infiniband",
377         "FCoIB", "fcoib");
378
379     /* Required function calls to register the header fields and
380      * subtrees used */
381     proto_register_field_array(proto_fcoib, hf, array_length(hf));
382     proto_register_subtree_array(ett, array_length(ett));
383
384     fcoib_module = prefs_register_protocol(proto_fcoib, proto_reg_handoff_fcoib);
385
386     prefs_register_bool_preference(fcoib_module, "heur_en", "Enable heuristic identification of FCoIB packets",
387         "When this option is enabled Wireshark will attempt to identify FCoIB packets automatically "
388         "based on some common features (may generate false positives)",
389         &gPREF_HEUR_EN);
390
391     prefs_register_bool_preference(fcoib_module, "manual_en", "Enable manual settings",
392         "Enables dissecting packets between the manually configured source/destination as FCoIB traffic",
393         &gPREF_MAN_EN);
394
395     prefs_register_static_text_preference(fcoib_module, "addr_a", "Address A",
396         "Side A of the manually-configured connection");
397     prefs_register_enum_preference(fcoib_module, "addr_a_type", "Address Type",
398         "Type of address specified", &gPREF_TYPE[0], pref_address_types, FALSE);
399     prefs_register_string_preference(fcoib_module, "addr_a_id", "ID",
400         "LID/GID of address A", &gPREF_ID[0]);
401     prefs_register_uint_preference(fcoib_module, "addr_a_qp", "QP Number",
402         "QP Number for address A", 10, &gPREF_QP[0]);
403
404     prefs_register_static_text_preference(fcoib_module, "addr_b", "Address B",
405         "Side B of the manually-configured connection");
406     prefs_register_enum_preference(fcoib_module, "addr_b_type", "Address Type",
407         "Type of address specified", &gPREF_TYPE[1], pref_address_types, FALSE);
408     prefs_register_string_preference(fcoib_module, "addr_b_id", "ID",
409         "LID/GID of address B", &gPREF_ID[1]);
410     prefs_register_uint_preference(fcoib_module, "addr_b_qp", "QP Number",
411         "QP Number for address B", 10, &gPREF_QP[1]);
412 }
413
414 void
415 proto_reg_handoff_fcoib(void)
416 {
417     static gboolean initialized = FALSE;
418
419     if (!initialized) {
420         heur_dissector_add("infiniband.payload", dissect_fcoib, proto_fcoib);
421
422         data_handle = find_dissector("data");
423         fc_handle = find_dissector("fc");
424
425         initialized = TRUE;
426     }
427
428     if (gPREF_MAN_EN) {
429         /* the manual setting is enabled, so parse the settings into the address type */
430         gboolean error_occured = FALSE;
431         char *not_parsed;
432         int i;
433
434         for (i = 0; i < 2; i++) {
435             if (gPREF_TYPE[i] == 0) {   /* LID */
436                 errno = 0;  /* reset any previous error indicators */
437                 *((guint16*)manual_addr_data[i]) = (guint16)strtoul(gPREF_ID[i], &not_parsed, 0);
438                 if (errno || *not_parsed != '\0') {
439                     error_occured = TRUE;
440                 } else {
441                     SET_ADDRESS(&manual_addr[i], AT_IB, sizeof(guint16), manual_addr_data[i]);
442                 }
443             } else {    /* GID */
444                 if (! inet_pton(AF_INET6, gPREF_ID[i], manual_addr_data[i]) ) {
445                     error_occured = TRUE;
446                 } else {
447                     SET_ADDRESS(&manual_addr[i], AT_IB, GID_SIZE, manual_addr_data[i]);
448                 }
449             }
450
451             if (error_occured) {
452                 /* an invalid id was specified - disable manual settings until it's fixed */
453                 gPREF_MAN_EN = FALSE;
454                 break;
455             }
456         }
457
458     }
459 }