Define some fcns & vars as static...
[metze/wireshark/wip.git] / epan / dissectors / packet-aoe.c
1 /* packet-aoe.c
2  * Routines for dissecting the ATA over Ethernet protocol.
3  *   Ronnie Sahlberg 2004
4  *
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <glib.h>
27
28 #include <epan/packet.h>
29 #include <epan/emem.h>
30 #include <epan/conversation.h>
31 #include <etypes.h>
32
33 static int proto_aoe;
34 static int hf_aoe_version=-1;
35 static int hf_aoe_flags_response=-1;
36 static int hf_aoe_flags_error=-1;
37 static int hf_aoe_error=-1;
38 static int hf_aoe_major=-1;
39 static int hf_aoe_minor=-1;
40 static int hf_aoe_cmd=-1;
41 static int hf_aoe_tag=-1;
42 static int hf_aoe_aflags_e=-1;
43 static int hf_aoe_aflags_d=-1;
44 static int hf_aoe_aflags_a=-1;
45 static int hf_aoe_aflags_w=-1;
46 static int hf_aoe_err_feature=-1;
47 static int hf_aoe_sector_count=-1;
48 static int hf_aoe_acmd=-1;
49 static int hf_aoe_astatus=-1;
50 static int hf_aoe_lba=-1;
51 static int hf_aoe_response_in=-1;
52 static int hf_aoe_response_to=-1;
53 static int hf_aoe_time=-1;
54
55 static gint ett_aoe = -1;
56 static gint ett_aoe_flags = -1;
57
58 #define AOE_FLAGS_RESPONSE 0x08
59 #define AOE_FLAGS_ERROR    0x04
60
61 #define AOE_AFLAGS_E    0x40
62 #define AOE_AFLAGS_D    0x10
63 #define AOE_AFLAGS_A    0x02
64 #define AOE_AFLAGS_W    0x01
65
66 static const true_false_string tfs_aflags_e = {
67   "LBA48 extended command",
68   "Normal command"
69 };
70 static const true_false_string tfs_aflags_d = {
71   "?",
72   "?"
73 };
74 static const true_false_string tfs_aflags_a = {
75   "ASYNCHRONOUS Write",
76   "synchronous write"
77 };
78 static const true_false_string tfs_aflags_w = {
79   "WRITE to the device",
80   "No write to device"
81 };
82
83 static const true_false_string tfs_response = {
84   "Response",
85   "Request"
86 };
87
88 static const true_false_string tfs_error = {
89   "Error",
90   "No error"
91 };
92
93 static const value_string error_vals[] = {
94   { 1, "Unrecognized command code" },
95   { 2, "Bad argument parameter" },
96   { 3, "Device unavailable" },
97   { 4, "Config string present" },
98   { 5, "Unsupported version" },
99   { 0, NULL}
100 };
101
102 #define AOE_CMD_ISSUE_ATA_COMMAND  0
103 #define AOE_CMD_QUERY_CONFIG_INFO  1
104 static const value_string cmd_vals[] = {
105   { AOE_CMD_ISSUE_ATA_COMMAND, "Issue ATA Command" },
106   { AOE_CMD_QUERY_CONFIG_INFO, "Query Config Information" },
107   { 0, NULL}
108 };
109
110 static const value_string ata_cmd_vals[] = {
111   { 0x00, "NOP" },
112   { 0x08, "Atapi soft reset" },
113   { 0x10, "Recalibrate" },
114   { 0x20, "Read sectors (with retry)" },
115   { 0x21, "Read sectors (no retry)" },
116   { 0x22, "Read long (with retry)" },
117   { 0x23, "Read long (no retry)" },
118   { 0x24, "Read ext" },
119   { 0x30, "Write sectors (with retry)" },
120   { 0x31, "Write sectors (no retry)" },
121   { 0x32, "Write long (with retry)" },
122   { 0x33, "Write long (no retry)" },
123   { 0x34, "Write ext" },
124   { 0x3c, "Write verify" },
125   { 0x40, "Read verify sectors (with retry)" },
126   { 0x41, "Read verify sectors (no retry)" },
127   { 0x50, "Format track" },
128   { 0x70, "Seek" },
129   { 0x90, "Execute device diagnostics" },
130   { 0x91, "Initialize device parameters" },
131   { 0x92, "Download microcode" },
132   { 0x94, "Standy immediate" },
133   { 0x95, "Idle immediate" },
134   { 0x96, "Standby" },
135   { 0x97, "Idle" },
136   { 0x98, "Check power mode" },
137   { 0x99, "Sleep" },
138   { 0xa0, "Atapi packet" },
139   { 0xa1, "Atapi identify device" },
140   { 0xa2, "Atapi service" },
141   { 0xb0, "Smart" },
142   { 0xc4, "Read multiple" },
143   { 0xc5, "Write multiple" },
144   { 0xc6, "Set multiple mode" },
145   { 0xc8, "Read dma (with retry)" },
146   { 0xc9, "Read dma (no retry)" },
147   { 0xca, "Write dma (with retry)" },
148   { 0xcb, "Write dma (no retry)" },
149   { 0xde, "Door lock" },
150   { 0xdf, "Door unlock" },
151   { 0xe0, "Standy immediate" },
152   { 0xe1, "Idle immediate" },
153   { 0xe2, "Standby" },
154   { 0xe3, "Idle" },
155   { 0xe4, "Read buffer" },
156   { 0xe5, "Check power mode" },
157   { 0xe6, "Sleep" },
158   { 0xe8, "Write buffer" },
159   { 0xec, "Identify Device" },
160   { 0xed, "Media eject" },
161   { 0xee, "Identify device dma" },
162   { 0xef, "Set features" },
163   { 0xf1, "Security set password" },
164   { 0xf2, "Security unlock" },
165   { 0xf3, "Security erase prepare" },
166   { 0xf4, "Security erase unit" },
167   { 0xf5, "Security freeze" },
168   { 0xf6, "Security disable password" },
169   { 0, NULL}
170 };
171
172 typedef struct ata_info_t {
173   guint32 tag;
174   void *conversation; /* just used to multiplex different conversations */
175   guint32 request_frame;
176   guint32 response_frame;
177   nstime_t req_time;
178   guint8 cmd;
179 } ata_info_t;
180 static GHashTable *ata_cmd_unmatched = NULL;
181 static GHashTable *ata_cmd_matched = NULL;
182
183 static guint
184 ata_cmd_hash_matched(gconstpointer k)
185 {
186   return GPOINTER_TO_UINT(k);
187 }
188
189 static gint
190 ata_cmd_equal_matched(gconstpointer k1, gconstpointer k2)
191 {
192   return k1==k2;
193 }
194
195 static guint
196 ata_cmd_hash_unmatched(gconstpointer k)
197 {
198   const ata_info_t *key = k;
199
200   return key->tag;
201 }
202
203 static gint
204 ata_cmd_equal_unmatched(gconstpointer k1, gconstpointer k2)
205 {
206   const ata_info_t *key1 = k1;
207   const ata_info_t *key2 = k2;
208
209   return (key1->tag==key2->tag)&&(key1->conversation==key2->conversation);
210 }
211
212 static void
213 dissect_ata_pdu(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean response, guint32 tag)
214 {
215   proto_item *tmp_item;
216   guint8 aflags;
217   guint64 lba;
218   ata_info_t *ata_info=NULL;
219   conversation_t *conversation;
220
221   /* only create a conversation for ATA commands */
222   conversation = find_or_create_conversation(pinfo);
223
224   if( !(pinfo->fd->flags.visited) ){
225     if(!response){
226       ata_info_t *tmp_ata_info;
227       /* first time we see this request so add a struct for request/response
228          matching */
229       ata_info=se_alloc(sizeof(ata_info_t));
230       ata_info->tag=tag;
231       ata_info->conversation=conversation;
232       ata_info->request_frame=pinfo->fd->num;
233       ata_info->response_frame=0;
234       ata_info->cmd=tvb_get_guint8(tvb, offset+3);
235       ata_info->req_time=pinfo->fd->abs_ts;
236
237       tmp_ata_info=g_hash_table_lookup(ata_cmd_unmatched, ata_info);
238       if(tmp_ata_info){
239         g_hash_table_remove(ata_cmd_unmatched, tmp_ata_info);
240       }
241       g_hash_table_insert(ata_cmd_unmatched, ata_info, ata_info);
242     } else {
243       ata_info_t tmp_ata_info;
244       /* first time we see this response so see if we can match it with
245          a request */
246       tmp_ata_info.tag=tag;
247       tmp_ata_info.conversation=conversation;
248       ata_info=g_hash_table_lookup(ata_cmd_unmatched, &tmp_ata_info);
249       /* woo hoo we could, so no need to store this in unmatched any more,
250          move both request and response to the matched table */
251       if(ata_info){
252         ata_info->response_frame=pinfo->fd->num;
253         g_hash_table_remove(ata_cmd_unmatched, ata_info);
254         g_hash_table_insert(ata_cmd_matched, GUINT_TO_POINTER(ata_info->request_frame), ata_info);
255         g_hash_table_insert(ata_cmd_matched, GUINT_TO_POINTER(ata_info->response_frame), ata_info);
256       }
257     }
258   } else {
259     ata_info=g_hash_table_lookup(ata_cmd_matched, GUINT_TO_POINTER(pinfo->fd->num));
260   }
261
262   if(ata_info){
263     if(response){
264       if(ata_info->request_frame){
265         nstime_t delta_ts;
266         tmp_item=proto_tree_add_uint(tree, hf_aoe_response_to, tvb, 0, 0, ata_info->request_frame);
267         PROTO_ITEM_SET_GENERATED(tmp_item);
268                 nstime_delta(&delta_ts, &pinfo->fd->abs_ts, &ata_info->req_time);
269         tmp_item=proto_tree_add_time(tree, hf_aoe_time, tvb, offset, 0, &delta_ts);
270         PROTO_ITEM_SET_GENERATED(tmp_item);
271       }
272     } else {
273       if(ata_info->response_frame){
274         tmp_item=proto_tree_add_uint(tree, hf_aoe_response_in, tvb, 0, 0, ata_info->response_frame);
275         PROTO_ITEM_SET_GENERATED(tmp_item);
276       }
277     }
278   }
279
280   /* aflags */
281   aflags=tvb_get_guint8(tvb, offset);
282   proto_tree_add_item(tree, hf_aoe_aflags_e, tvb, offset, 1, FALSE);
283   if(aflags&AOE_AFLAGS_E){
284     proto_tree_add_item(tree, hf_aoe_aflags_d, tvb, offset, 1, FALSE);
285   }
286   if(aflags&AOE_AFLAGS_W){
287     proto_tree_add_item(tree, hf_aoe_aflags_a, tvb, offset, 1, FALSE);
288   }
289   proto_tree_add_item(tree, hf_aoe_aflags_w, tvb, offset, 1, FALSE);
290   offset++;
291
292   /* err/feature */
293   proto_tree_add_item(tree, hf_aoe_err_feature, tvb, offset, 1, FALSE);
294   offset++;
295
296   /* sector count */
297   proto_tree_add_item(tree, hf_aoe_sector_count, tvb, offset, 1, FALSE);
298   offset++;
299
300   /* ata command/status */
301   if(!response){
302     proto_tree_add_item(tree, hf_aoe_acmd, tvb, offset, 1, FALSE);
303     if (check_col(pinfo->cinfo, COL_INFO)) {
304       col_append_fstr(pinfo->cinfo, COL_INFO, " ATA:%s", val_to_str(tvb_get_guint8(tvb, offset), ata_cmd_vals, " Unknown ATA<0x%02x>"));
305     }
306   } else {
307     proto_tree_add_item(tree, hf_aoe_astatus, tvb, offset, 1, FALSE);
308     if(ata_info && ata_info->request_frame){
309       /* we dont know what command it was unless we saw the request_frame */
310       tmp_item=proto_tree_add_uint(tree, hf_aoe_acmd, tvb, 0, 0, ata_info->cmd);
311       PROTO_ITEM_SET_GENERATED(tmp_item);
312       if (check_col(pinfo->cinfo, COL_INFO)) {
313         col_append_fstr(pinfo->cinfo, COL_INFO, " ATA:%s", val_to_str(ata_info->cmd, ata_cmd_vals, " Unknown ATA<0x%02x>"));
314       }
315     }
316   }
317   offset++;
318
319   /*lba   probably complete wrong */
320   lba=tvb_get_letohs(tvb, offset+4);
321   lba=(lba<<32)|tvb_get_letohl(tvb, offset);
322   offset+=8;
323   proto_tree_add_uint64(tree, hf_aoe_lba, tvb, offset-8, 6, lba);
324
325 }
326
327 static void
328 dissect_aoe_v1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
329 {
330   guint8 flags, cmd;
331   guint32 tag;
332   proto_item *flags_item=NULL;
333   proto_tree *flags_tree=NULL;
334
335   /* read and dissect the flags */
336   flags=tvb_get_guint8(tvb, 0)&0x0f;
337   if(tree){
338     flags_item=proto_tree_add_text(tree, tvb, 0, 1, "Flags:");
339     flags_tree=proto_item_add_subtree(flags_item, ett_aoe_flags);
340   }
341   proto_tree_add_item(flags_tree, hf_aoe_flags_response, tvb, 0, 1, FALSE);
342   proto_tree_add_item(flags_tree, hf_aoe_flags_error, tvb, 0, 1, FALSE);
343   if(flags_item){
344     proto_item_append_text(flags_item,(flags&AOE_FLAGS_RESPONSE)?" Response":" Request");
345     if(flags&AOE_FLAGS_ERROR){
346       proto_item_append_text(flags_item, " Error");
347     }
348   }
349
350
351   /* error */
352   if(flags&AOE_FLAGS_ERROR){
353     proto_tree_add_item(tree, hf_aoe_error, tvb, 1, 1, FALSE);
354     if (check_col(pinfo->cinfo, COL_INFO)) {
355       col_append_fstr(pinfo->cinfo, COL_INFO, "Error:%s ", val_to_str(tvb_get_guint8(tvb, 1), error_vals, "Unknown error<%d>"));
356     }
357   }
358
359   /* major/minor address */
360   proto_tree_add_item(tree, hf_aoe_major, tvb, 2, 2, FALSE);
361   proto_tree_add_item(tree, hf_aoe_minor, tvb, 4, 1, FALSE);
362
363   /* command */
364   cmd=tvb_get_guint8(tvb, 5);
365   proto_tree_add_item(tree, hf_aoe_cmd, tvb, 5, 1, FALSE);
366   if (check_col(pinfo->cinfo, COL_INFO)) {
367     col_append_fstr(pinfo->cinfo, COL_INFO, "%s %s", val_to_str(cmd, cmd_vals, "Unknown command<%d>"), (flags&AOE_FLAGS_RESPONSE)?"Response":"Request");
368   }
369
370   /* tag */
371   tag=tvb_get_letohl(tvb, 6);
372   proto_tree_add_item(tree, hf_aoe_tag, tvb, 6, 4, FALSE);
373
374
375   switch(cmd){
376   case AOE_CMD_ISSUE_ATA_COMMAND:
377     dissect_ata_pdu(pinfo, tree, tvb, 10, flags&AOE_FLAGS_RESPONSE, tag);
378     break;
379   case AOE_CMD_QUERY_CONFIG_INFO:
380     break;
381   }
382
383 }
384
385 static void
386 dissect_aoe(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
387 {
388   proto_item *item=NULL;
389   proto_tree *tree=NULL;
390   guint8 version;
391
392   col_set_str(pinfo->cinfo, COL_PROTOCOL, "AoE");
393   col_clear(pinfo->cinfo, COL_INFO);
394
395   if (parent_tree) {
396     item = proto_tree_add_item(parent_tree, proto_aoe, tvb, 0, -1, FALSE);
397     tree = proto_item_add_subtree(item, ett_aoe);
398   }
399
400   version=tvb_get_guint8(tvb, 0)>>4;
401   proto_tree_add_uint(tree, hf_aoe_version, tvb, 0, 1, version);
402   switch(version){
403   case 1:
404     dissect_aoe_v1(tvb, pinfo, tree);
405     break;
406   }
407 }
408
409 static void
410 ata_init(void)
411 {
412   if(ata_cmd_unmatched){
413     g_hash_table_destroy(ata_cmd_unmatched);
414     ata_cmd_unmatched=NULL;
415   }
416   ata_cmd_unmatched=g_hash_table_new(ata_cmd_hash_unmatched, ata_cmd_equal_unmatched);
417
418   if(ata_cmd_matched){
419     g_hash_table_destroy(ata_cmd_matched);
420     ata_cmd_matched=NULL;
421   }
422   ata_cmd_matched=g_hash_table_new(ata_cmd_hash_matched, ata_cmd_equal_matched);
423
424 }
425
426 void
427 proto_register_aoe(void)
428 {
429
430   static hf_register_info hf[] = {
431     { &hf_aoe_cmd,
432       { "Command", "aoe.cmd", FT_UINT8, BASE_DEC, VALS(cmd_vals), 0x0,
433         "AOE Command", HFILL}},
434     { &hf_aoe_version,
435       { "Version", "aoe.version", FT_UINT8, BASE_DEC, NULL, 0x0,
436         "Version of the AOE protocol", HFILL}},
437     { &hf_aoe_error,
438       { "Error", "aoe.error", FT_UINT8, BASE_DEC, VALS(error_vals), 0x0,
439         "Error code", HFILL}},
440     { &hf_aoe_err_feature,
441       { "Err/Feature", "aoe.err_feature", FT_UINT8, BASE_HEX, NULL, 0x0,
442         NULL, HFILL}},
443     { &hf_aoe_sector_count,
444       { "Sector Count", "aoe.sector_count", FT_UINT8, BASE_DEC, NULL, 0x0,
445         NULL, HFILL}},
446     { &hf_aoe_flags_response,
447       { "Response flag", "aoe.response", FT_BOOLEAN, 8, TFS(&tfs_response), AOE_FLAGS_RESPONSE, "Whether this is a response PDU or not", HFILL}},
448     { &hf_aoe_flags_error,
449       { "Error flag", "aoe.error", FT_BOOLEAN, 8, TFS(&tfs_error), AOE_FLAGS_ERROR, "Whether this is an error PDU or not", HFILL}},
450     { &hf_aoe_major,
451       { "Major", "aoe.major", FT_UINT16, BASE_HEX, NULL, 0x0,
452         "Major address", HFILL}},
453     { &hf_aoe_minor,
454       { "Minor", "aoe.minor", FT_UINT8, BASE_HEX, NULL, 0x0,
455         "Minor address", HFILL}},
456     { &hf_aoe_acmd,
457       { "ATA Cmd", "aoe.ata.cmd", FT_UINT8, BASE_HEX, VALS(ata_cmd_vals), 0x0,
458         "ATA command opcode", HFILL}},
459     { &hf_aoe_astatus,
460       { "ATA Status", "aoe.ata.status", FT_UINT8, BASE_HEX, NULL, 0x0,
461         "ATA status bits", HFILL}},
462     { &hf_aoe_tag,
463       { "Tag", "aoe.tag", FT_UINT32, BASE_HEX, NULL, 0x0,
464         "Command Tag", HFILL}},
465     { &hf_aoe_aflags_e,
466       { "E", "aoe.aflags.e", FT_BOOLEAN, 8, TFS(&tfs_aflags_e), AOE_AFLAGS_E, "Whether this is a normal or LBA48 command", HFILL}},
467     { &hf_aoe_aflags_d,
468       { "D", "aoe.aflags.d", FT_BOOLEAN, 8, TFS(&tfs_aflags_d), AOE_AFLAGS_D, NULL, HFILL}},
469     { &hf_aoe_aflags_a,
470       { "A", "aoe.aflags.a", FT_BOOLEAN, 8, TFS(&tfs_aflags_a), AOE_AFLAGS_A, "Whether this is an asynchronous write or not", HFILL}},
471     { &hf_aoe_aflags_w,
472       { "W", "aoe.aflags.w", FT_BOOLEAN, 8, TFS(&tfs_aflags_w), AOE_AFLAGS_W, "Is this a command writing data to the device or not", HFILL}},
473     { &hf_aoe_lba,
474       { "Lba", "aoe.lba", FT_UINT64, BASE_HEX, NULL, 0x00, "Lba address", HFILL}},
475     { &hf_aoe_response_in,
476       { "Response In", "aoe.response_in", FT_FRAMENUM, BASE_NONE, NULL, 0x0, "The response to this packet is in this frame", HFILL }},
477     { &hf_aoe_response_to,
478       { "Response To", "aoe.response_to", FT_FRAMENUM, BASE_NONE, NULL, 0x0, "This is a response to the ATA command in this frame", HFILL }},
479     { &hf_aoe_time,
480       { "Time from request", "aoe.time", FT_RELATIVE_TIME, BASE_NONE, NULL, 0, "Time between Request and Reply for ATA calls", HFILL }},
481   };
482
483   static gint *ett[] = {
484     &ett_aoe,
485     &ett_aoe_flags,
486   };
487
488   proto_aoe = proto_register_protocol("ATAoverEthernet", "AOE", "aoe");
489   proto_register_field_array(proto_aoe, hf, array_length(hf));
490   proto_register_subtree_array(ett, array_length(ett));
491
492   register_dissector("aoe", dissect_aoe, proto_aoe);
493   register_init_routine(ata_init);
494 }
495
496 void
497 proto_reg_handoff_aoe(void)
498 {
499   dissector_handle_t aoe_handle;
500
501   aoe_handle = find_dissector("aoe");
502   dissector_add("ethertype", ETHERTYPE_AOE, aoe_handle);
503
504 }