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